From 0862fd2da04eff8646d8e0a640a1278591751ab8 Mon Sep 17 00:00:00 2001 From: Julien Chaumond Date: Wed, 2 Apr 2014 12:50:34 +0200 Subject: [PATCH] [v0.1.5] Optionnally rely on pure-JS zip dependency --- README.md | 5 +++++ epub.js | 23 +++++++++++++++++++++++ package.json | 7 +++++-- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 57b06f6..918c75f 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,11 @@ npm install epub +Or, if you want a pure-JS version (useful if used in a Node-Webkit app for example): + + npm install epub --no-optional + + ## Usage var EPub = require("epub"); diff --git a/epub.js b/epub.js index 7ee4380..c1e1a5a 100644 --- a/epub.js +++ b/epub.js @@ -3,6 +3,29 @@ var XML2JS = require("xml2js").Parser; var utillib = require("util"); var EventEmitter = require('events').EventEmitter; +try { + // zipfile is an optional dependency: + var ZipFile = require("zipfile").ZipFile; +} catch (err) { + // Mock zipfile using pure-JS adm-zip: + var AdmZip = require('adm-zip'); + + var ZipFile = function(filename) { + this.admZip = new AdmZip(filename); + this.names = this.admZip.getEntries().map(function(zipEntry) { + return zipEntry.entryName; + }); + this.count = this.names.length; + }; + ZipFile.prototype.readFile = function(name, cb) { + this.admZip.readFileAsync(this.admZip.getEntry(name), function(buffer, error) { + // `error` is bogus right now, so let's just drop it. + // see https://github.com/cthackers/adm-zip/pull/88 + return cb(null, buffer); + }); + }; +} + //TODO: Cache parsed data /** diff --git a/package.json b/package.json index 5362d4f..5b7ed5a 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "epub", "description": "Parse ePub electronic book files with Node.JS", - "version": "0.1.4", + "version": "0.1.5", "author": "Andris Reinman", "contributors": [ { @@ -27,9 +27,12 @@ } ], "dependencies": { - "zipfile": "^0.5.0", + "adm-zip": "^0.4.4", "xml2js": "~0.1.0" }, + "optionalDependencies": { + "zipfile": "^0.5.0" + }, "engine": [ "node >=0.3.0" ],