Polymorphic readFile() function

This commit is contained in:
Julien Chaumond 2014-04-02 16:41:38 +02:00
parent 87388fd80c
commit 88f3c28067

24
epub.js
View File

@ -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;