From 88f3c28067dea895c42f827fa721c34f39d27e0f Mon Sep 17 00:00:00 2001 From: Julien Chaumond Date: Wed, 2 Apr 2014 16:41:38 +0200 Subject: [PATCH] Polymorphic readFile() function --- epub.js | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/epub.js b/epub.js index aab8f4b..3d9aafa 100644 --- a/epub.js +++ b/epub.js @@ -1,5 +1,5 @@ var XML2JS = require("xml2js").Parser; -var utillib = require("util"); +var util = require('util'); var EventEmitter = require('events').EventEmitter; try { @@ -66,7 +66,7 @@ function EPub(fname, imageroot, linkroot) { this.linkroot += "/"; } } -utillib.inherits(EPub, EventEmitter); +util.inherits(EPub, EventEmitter); /** * EPub#parse() -> undefined @@ -729,5 +729,25 @@ EPub.prototype.getFile = function (id, callback) { }; +EPub.prototype.readFile = function(filename, options, callback_) { + var callback = arguments[arguments.length - 1]; + + if (util.isFunction(options) || !options) { + this.zip.readFile(filename, callback); + } else if (util.isString(options)) { + // options is an encoding + this.zip.readFile(filename, function(err, data) { + if (err) { + callback(new Error('Reading archive failed')); + return; + } + callback(null, data.toString(options)); + }); + } else { + throw new TypeError('Bad arguments'); + } +}; + + // Expose to the world module.exports = EPub; \ No newline at end of file