New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@ridi/epub-parser

Package Overview
Dependencies
Maintainers
12
Versions
81
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ridi/epub-parser - npm Package Compare versions

Comparing version 0.3.0-alpha-6 to 0.3.0-alpha-7

lib/model/ParseContext.js

805

lib/EpubParser.js

@@ -7,10 +7,2 @@ 'use strict';

var _extends2 = require('babel-runtime/helpers/extends');
var _extends3 = _interopRequireDefault(_extends2);
var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray');
var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2);
var _promise = require('babel-runtime/core-js/promise');

@@ -28,2 +20,10 @@

var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray');
var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2);
var _extends2 = require('babel-runtime/helpers/extends');
var _extends3 = _interopRequireDefault(_extends2);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

@@ -33,2 +33,6 @@

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
var _createClass2 = require('babel-runtime/helpers/createClass');

@@ -38,5 +42,5 @@

var _weakMap = require('babel-runtime/core-js/weak-map');
var _inherits2 = require('babel-runtime/helpers/inherits');
var _weakMap2 = _interopRequireDefault(_weakMap);
var _inherits3 = _interopRequireDefault(_inherits2);

@@ -47,6 +51,2 @@ var _parserCore = require('@ridi/parser-core');

var _fs = require('fs');
var _fs2 = _interopRequireDefault(_fs);
var _Book = require('./model/Book');

@@ -56,6 +56,2 @@

var _Context = require('./model/Context');
var _Context2 = _interopRequireDefault(_Context);
var _CssItem = require('./model/CssItem');

@@ -97,2 +93,6 @@

var _ReadContext = require('./model/ReadContext');
var _ReadContext2 = _interopRequireDefault(_ReadContext);
var _SpineItem = require('./model/SpineItem');

@@ -110,2 +110,6 @@

var _ParseContext = require('./model/ParseContext');
var _ParseContext2 = _interopRequireDefault(_ParseContext);
var _xmlLoader4 = require('./loader/xmlLoader');

@@ -117,48 +121,5 @@

var privateProps = new _weakMap2.default();
var logger = new _parserCore.Logger('EpubParser');
var EpubParser = function () {
(0, _createClass3.default)(EpubParser, [{
key: 'input',
/**
* Get file or directory
*/
get: function get() {
return privateProps.get(this).input;
}
/**
* Get en/decrypto provider
*/
}, {
key: 'cryptoProvider',
get: function get() {
return privateProps.get(this).cryptoProvider;
}
/**
* Get logger
*/
}, {
key: 'logger',
get: function get() {
return logger;
}
/**
* Create new EpubParser
* @param {string} input file or directory
* @param {CryptoProvider} cryptoProvider en/decrypto provider
* @throws {Errors.ENOENT} no such file or directory
* @throws {Errors.EINVAL} invalid input
* @example new EpubParser('./foo/bar.epub' or './foo/bar');
*/
}], [{
var EpubParser = function (_Parser) {
(0, _inherits3.default)(EpubParser, _Parser);
(0, _createClass3.default)(EpubParser, null, [{
key: 'parseDefaultOptions',

@@ -170,3 +131,3 @@

get: function get() {
return {
return (0, _extends3.default)({}, _Parser.parseDefaultOptions, {
// If true, validation package specifications in IDPF listed below. (only using if input is EPUB file.)

@@ -181,13 +142,2 @@ // - Zip header should not corrupt.

allowNcxFileMissing: true,
// If specified, uncompress to that path. (only using if input is EPUB file.)
unzipPath: undefined,
// If true, overwrite to unzipPath when uncompress. (only using if unzipPath specified.)
overwrite: true,
// If true, ignore index difference caused by isLinear property of SpineItem.
// e.g. If left is true, right is false.
// [{ index: 0, isLinear: true, ... }, [{ index: 0, isLinear: true, ... },
// { index: 1, isLinear: true, ... }, { index: 1, isLinear: true, ... },
// { index: 2, isLinear: false, ... }, { index: -1, isLinear: false, ... },
// { index: 3, isLinear: true, ... }] { index: 2, isLinear: true, ... }]
ignoreLinear: false,
// If true, styles used for spine is described, and one namespace is given per CSS file or inline style.

@@ -199,3 +149,3 @@ // Otherwise it CssItem.namespace, SpineItem.styles is undefined.

styleNamespacePrefix: 'ridi_style'
};
});
}

@@ -210,11 +160,8 @@

get: function get() {
return {
return (0, _extends3.default)({}, _Parser.parseOptionTypes, {
validatePackage: 'Boolean',
allowNcxFileMissing: 'Boolean',
unzipPath: 'String|Undefined',
overwrite: 'Boolean',
ignoreLinear: 'Boolean',
parseStyle: 'Boolean',
styleNamespacePrefix: 'String'
};
});
}

@@ -229,3 +176,3 @@

get: function get() {
return {
return (0, _extends3.default)({}, _Parser.readDefaultOptions, {
// If specified, change base path of paths used by spine and css.

@@ -248,3 +195,3 @@ // e.g. '../Images/cover.jpg' -> '{basePath}/OEBPS/Images/cover.jpg'

removeClasses: []
};
});
}

@@ -259,3 +206,3 @@

get: function get() {
return {
return (0, _extends3.default)({}, _Parser.readOptionTypes, {
basePath: 'String|Undefined',

@@ -268,4 +215,14 @@ extractBody: 'Boolean|Function',

removeClasses: 'Array'
};
});
}
/**
* Create new EpubParser
* @param {string} input file or directory
* @param {CryptoProvider} cryptoProvider en/decrypto provider
* @throws {Errors.ENOENT} no such file or directory
* @throws {Errors.EINVAL} invalid input
* @example new EpubParser('./foo/bar.epub' or './foo/bar');
*/
}]);

@@ -275,135 +232,63 @@

(0, _classCallCheck3.default)(this, EpubParser);
if ((0, _parserCore.isString)(input)) {
if (!_fs2.default.existsSync(input)) {
throw (0, _parserCore.createError)(_parserCore.Errors.ENOENT, input);
}
} else {
throw (0, _parserCore.createError)(_parserCore.Errors.EINVAL, 'input', 'input', input);
}
privateProps.set(this, { input: input, cryptoProvider: cryptoProvider });
return (0, _possibleConstructorReturn3.default)(this, _Parser.call(this, input, cryptoProvider, 'EpubParser'));
}
/**
* EPUB Parsing
* @param {?object} options parse options
* @returns {Promise.<Book>} return Book
* @see EpubParser.parseDefaultOptions
* @see EpubParser.parseOptionTypes
* @example
* const options = { validatePackage: true, unzipPath: './foo/bar' };
* parser.parse(options).then((book) => {
* ...
* });
* @returns {ParseContext}
*/
EpubParser.prototype.parse = function () {
var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2() {
var _this = this;
EpubParser.prototype._getParseContextClass = function _getParseContextClass() {
return _ParseContext2.default;
};
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var tasks, context;
return _regenerator2.default.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
tasks = [{ func: this._prepareParse, name: 'prepareParse' }, { func: this._validatePackageIfNeeded, name: 'validatePackageIfNeeded' }, { func: this._parseMetaInf, name: 'parseMetaInf' }, { func: this._parseOpf, name: 'parseOpf' }, { func: this._parseNcx, name: 'parseNcx' }, { func: this._unzipIfNeeded, name: 'unzipIfNeeded' }, { func: this._createBook, name: 'createBook' }];
context = options;
_context2.next = 4;
return tasks.reduce(function (prevPromise, task) {
// eslint-disable-line arrow-body-style
return prevPromise.then((0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee() {
var func, name;
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
func = task.func, name = task.name;
_context.next = 3;
return logger.measure(func, _this, [context], name);
/**
* @returns {Book}
*/
case 3:
context = _context.sent;
case 4:
case 'end':
return _context.stop();
}
}
}, _callee, _this);
})));
}, _promise2.default.resolve());
EpubParser.prototype._getBookClass = function _getBookClass() {
return _Book2.default;
};
case 4:
if (!(0, _parserCore.isExists)(context.cover)) {
logger.warn('Cover image not found in EPUB.');
}
return _context2.abrupt('return', context);
/**
* @returns {ReadContext}
*/
case 6:
case 'end':
return _context2.stop();
}
}
}, _callee2, this);
}));
function parse() {
return _ref.apply(this, arguments);
}
EpubParser.prototype._getReadContextClass = function _getReadContextClass() {
return _ReadContext2.default;
};
return parse;
}();
/**
* Validate parse options and get entries from input
* @param {?object} options parse options
* @returns {Promise.<Context>} return Context containing parse options, entries
* @throws {Errors.EINVAL} invalid options or value type
* @throws {Errors.ENOENT} no such file or directory
* @throws {Errors.ENOFILE} no such file
* @see EpubParser.parseDefaultOptions
* @see EpubParser.parseOptionTypes
* @returns {Item}
*/
EpubParser.prototype._prepareParse = function () {
var _ref3 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee3() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var context;
return _regenerator2.default.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
(0, _parserCore.validateOptions)(options, EpubParser.parseOptionTypes);
context = new _Context2.default();
EpubParser.prototype._getReadItemClass = function _getReadItemClass() {
return _Item2.default;
};
context.options = (0, _parserCore.mergeObjects)(EpubParser.parseDefaultOptions, options);
_context3.next = 5;
return (0, _parserCore.readEntries)(this.input, this.cryptoProvider, logger);
/**
* @returns {ParseTask[]} return tasks
*/
case 5:
context.entries = _context3.sent;
return _context3.abrupt('return', context);
case 7:
case 'end':
return _context3.stop();
}
}
}, _callee3, this);
}));
EpubParser.prototype._parseTasks = function _parseTasks() {
return [].concat((0, _toConsumableArray3.default)(_Parser.prototype._parseTasks.call(this)), [{ func: this._validatePackageIfNeeded, name: 'validatePackageIfNeeded' }, { func: this._parseMetaInf, name: 'parseMetaInf' }, { func: this._parseOpf, name: 'parseOpf' }, { func: this._parseNcx, name: 'parseNcx' }]);
};
function _prepareParse() {
return _ref3.apply(this, arguments);
}
/**
* @returns {ParseTask[]} return after tasks
*/
return _prepareParse;
}();
EpubParser.prototype._parseAfterTasks = function _parseAfterTasks() {
return [].concat((0, _toConsumableArray3.default)(_Parser.prototype._parseAfterTasks.call(this)), [{ func: this._checkResult, name: 'checkResult' }]);
};
/**
* Validate package spec if zip source and validatePackage option specified
* @param {Context} context intermediate result
* @returns {Promise.<Context>} return Context (no change at this step)
* @param {ParseContext} context intermediate result
* @returns {Promise.<ParseContext>} return Context (no change at this step)
* @throws {Errors.EINVAL} invalid package

@@ -415,8 +300,8 @@ * @see EpubParser.parseDefaultOptions.validatePackage

EpubParser.prototype._validatePackageIfNeeded = function () {
var _ref4 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee4(context) {
var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(context) {
var entries, options, firstEntry, signature, reason, _reason;
return _regenerator2.default.wrap(function _callee4$(_context4) {
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context4.prev = _context4.next) {
switch (_context.prev = _context.next) {
case 0:

@@ -426,3 +311,3 @@ entries = context.entries, options = context.options;

if (!(!(0, _parserCore.isString)(entries.source) && options.validatePackage)) {
_context4.next = 22;
_context.next = 22;
break;

@@ -433,10 +318,10 @@ }

_context4.next = 5;
_context.next = 5;
return firstEntry.getFile('utf8');
case 5:
signature = _context4.sent;
signature = _context.sent;
if (!(firstEntry.entryPath !== 'mimetype')) {
_context4.next = 10;
_context.next = 10;
break;

@@ -449,3 +334,3 @@ }

if (!(firstEntry.method !== 0 /* STORED */)) {
_context4.next = 14;
_context.next = 14;
break;

@@ -458,3 +343,3 @@ }

if (!(signature !== 'application/epub+zip')) {
_context4.next = 19;
_context.next = 19;
break;

@@ -468,3 +353,3 @@ }

if (!(firstEntry.extraFieldLength > 0)) {
_context4.next = 22;
_context.next = 22;
break;

@@ -477,14 +362,14 @@ }

case 22:
return _context4.abrupt('return', context);
return _context.abrupt('return', context);
case 23:
case 'end':
return _context4.stop();
return _context.stop();
}
}
}, _callee4, this);
}, _callee, this);
}));
function _validatePackageIfNeeded(_x3) {
return _ref4.apply(this, arguments);
function _validatePackageIfNeeded(_x) {
return _ref.apply(this, arguments);
}

@@ -497,4 +382,4 @@

* Locate OPF and base path in container.xml
* @param {Context} context intermediate result
* @return {Promise.<Context>} return Context containing OPF and base path
* @param {ParseContext} context intermediate result
* @return {Promise.<ParseContext>} return Context containing OPF and base path
* @throws {Errors.ENOFILE} container.xml not found

@@ -508,8 +393,8 @@ * @throws {Errors.EINVAL} invalid XML

EpubParser.prototype._parseMetaInf = function () {
var _ref5 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee5(context) {
var _ref2 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2(context) {
var entryPath, containerEntry, _xmlLoader, container, rootfiles, rootfile, opfPath;
return _regenerator2.default.wrap(function _callee5$(_context5) {
return _regenerator2.default.wrap(function _callee2$(_context2) {
while (1) {
switch (_context5.prev = _context5.next) {
switch (_context2.prev = _context2.next) {
case 0:

@@ -520,3 +405,3 @@ entryPath = 'META-INF/container.xml';

if ((0, _parserCore.isExists)(containerEntry)) {
_context5.next = 4;
_context2.next = 4;
break;

@@ -528,13 +413,13 @@ }

case 4:
_context5.t0 = _xmlLoader5.default;
_context5.next = 7;
_context2.t0 = _xmlLoader5.default;
_context2.next = 7;
return containerEntry.getFile('utf8');
case 7:
_context5.t1 = _context5.sent;
_xmlLoader = (0, _context5.t0)(_context5.t1);
_context2.t1 = _context2.sent;
_xmlLoader = (0, _context2.t0)(_context2.t1);
container = _xmlLoader.container;
if ((0, _parserCore.isExists)(container)) {
_context5.next = 12;
_context2.next = 12;
break;

@@ -547,3 +432,3 @@ }

if ((0, _parserCore.isExists)(container.rootfiles)) {
_context5.next = 14;
_context2.next = 14;
break;

@@ -563,3 +448,3 @@ }

if ((0, _parserCore.isExists)(rootfile)) {
_context5.next = 18;
_context2.next = 18;
break;

@@ -574,3 +459,3 @@ }

if ((0, _parserCore.isExists)(opfPath)) {
_context5.next = 21;
_context2.next = 21;
break;

@@ -586,14 +471,14 @@ }

return _context5.abrupt('return', context);
return _context2.abrupt('return', context);
case 24:
case 'end':
return _context5.stop();
return _context2.stop();
}
}
}, _callee5, this);
}, _callee2, this);
}));
function _parseMetaInf(_x4) {
return _ref5.apply(this, arguments);
function _parseMetaInf(_x2) {
return _ref2.apply(this, arguments);
}

@@ -606,4 +491,4 @@

* OPF parsing
* @param {Context} context intermediate result
* @returns {Promise.<Context>} return Context containing OPF parsing result
* @param {ParseContext} context intermediate result
* @returns {Promise.<ParseContext>} return Context containing OPF parsing result
* @throws {Errors.EINVAL} invalid xml

@@ -616,14 +501,14 @@ * @throws {Errors.ENOFILE} OPF not found

EpubParser.prototype._parseOpf = function () {
var _ref6 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee6(context) {
var _context6, entries, opfPath, opfEntry, _xmlLoader2, root;
var _ref3 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee3(context) {
var _context3, entries, opfPath, opfEntry, _xmlLoader2, root;
return _regenerator2.default.wrap(function _callee6$(_context7) {
return _regenerator2.default.wrap(function _callee3$(_context4) {
while (1) {
switch (_context7.prev = _context7.next) {
switch (_context4.prev = _context4.next) {
case 0:
_context6 = context, entries = _context6.entries, opfPath = _context6.opfPath;
_context3 = context, entries = _context3.entries, opfPath = _context3.opfPath;
opfEntry = entries.find(opfPath);
if ((0, _parserCore.isExists)(opfEntry)) {
_context7.next = 4;
_context4.next = 4;
break;

@@ -635,13 +520,13 @@ }

case 4:
_context7.t0 = _xmlLoader5.default;
_context7.next = 7;
_context4.t0 = _xmlLoader5.default;
_context4.next = 7;
return opfEntry.getFile('utf8');
case 7:
_context7.t1 = _context7.sent;
_xmlLoader2 = (0, _context7.t0)(_context7.t1);
_context4.t1 = _context4.sent;
_xmlLoader2 = (0, _context4.t0)(_context4.t1);
root = _xmlLoader2.package;
if ((0, _parserCore.isExists)(root)) {
_context7.next = 12;
_context4.next = 12;
break;

@@ -654,3 +539,3 @@ }

if (!(!(0, _parserCore.isExists)(root.metadata) || !(0, _parserCore.isExists)(root.manifest) || !(0, _parserCore.isExists)(root.spine))) {
_context7.next = 14;
_context4.next = 14;
break;

@@ -664,29 +549,29 @@ }

context.rawBook.version = root.version;
_context7.next = 17;
_context4.next = 17;
return this._parseMetadata(root.metadata, context);
case 17:
context = _context7.sent;
_context7.next = 20;
context = _context4.sent;
_context4.next = 20;
return this._parseManifestAndSpine(root.manifest, root.spine, context);
case 20:
context = _context7.sent;
_context7.next = 23;
context = _context4.sent;
_context4.next = 23;
return this._parseGuide(root.guide, context);
case 23:
context = _context7.sent;
return _context7.abrupt('return', context);
context = _context4.sent;
return _context4.abrupt('return', context);
case 25:
case 'end':
return _context7.stop();
return _context4.stop();
}
}
}, _callee6, this);
}, _callee3, this);
}));
function _parseOpf(_x5) {
return _ref6.apply(this, arguments);
function _parseOpf(_x3) {
return _ref3.apply(this, arguments);
}

@@ -700,4 +585,4 @@

* @param {object} metadata metadata AST
* @param {Context} context intermediate result
* @returns {Promise.<Context>} return Context containing metadata
* @param {ParseContext} context intermediate result
* @returns {Promise.<ParseContext>} return Context containing metadata
*/

@@ -758,4 +643,4 @@

* @param {object} spine spine AST
* @param {Context} context intermediate result
* @returns {Promise.<Context>} return Context containing manifest and spine
* @param {ParseContext} context intermediate result
* @returns {Promise.<ParseContext>} return Context containing manifest and spine
* @see EpubParser.parseDefaultOptions.parseStyle

@@ -767,9 +652,9 @@ * @see EpubParser.parseDefaultOptions.styleNamespacePrefix

EpubParser.prototype._parseManifestAndSpine = function () {
var _ref7 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee8(manifest, spine, context) {
var _ref4 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee5(manifest, spine, context) {
var _this2 = this;
var rawBook, basePath, options, entries, tocId, items, itemRefs, coverMeta, inlineStyles;
return _regenerator2.default.wrap(function _callee8$(_context9) {
return _regenerator2.default.wrap(function _callee5$(_context6) {
while (1) {
switch (_context9.prev = _context9.next) {
switch (_context6.prev = _context6.next) {
case 0:

@@ -787,10 +672,10 @@ rawBook = context.rawBook, basePath = context.basePath, options = context.options, entries = context.entries;

rawBook.items = [];
_context9.next = 9;
_context6.next = 9;
return items.reduce(function (prevPromise, item, idx) {
// eslint-disable-line arrow-body-style
return prevPromise.then((0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee7() {
return prevPromise.then((0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee4() {
var rawItem, itemEntry, refIndex, ref, result;
return _regenerator2.default.wrap(function _callee7$(_context8) {
return _regenerator2.default.wrap(function _callee4$(_context5) {
while (1) {
switch (_context8.prev = _context8.next) {
switch (_context5.prev = _context5.next) {
case 0:

@@ -808,3 +693,3 @@ rawItem = {};

rawItem.reason = _DeadItem2.default.Reason.NOT_SUPPORT_TYPE;
logger.warn('Referenced resource \'%s\' ignored. (reason: %s)', rawItem.id, rawItem.reason);
_this2.logger.warn('Referenced resource \'%s\' ignored. (reason: %s)', rawItem.id, rawItem.reason);
}

@@ -815,3 +700,3 @@

if (!(0, _parserCore.isExists)(itemEntry)) {
_context8.next = 23;
_context5.next = 23;
break;

@@ -834,9 +719,7 @@ }

rawItem.isLinear = (0, _parserCore.isExists)(ref.linear) ? (0, _parserCore.parseBool)(ref.linear) : true;
if (options.ignoreLinear || rawItem.isLinear) {
rawItem.index = refIndex;
}
rawItem.index = refIndex;
} else {
rawItem.itemType = _DeadItem2.default;
rawItem.reason = _DeadItem2.default.Reason.NOT_SPINE;
logger.warn('Referenced resource \'%s\' ignored. (reason: %s)', rawItem.id, rawItem.reason);
_this2.logger.warn('Referenced resource \'%s\' ignored. (reason: %s)', rawItem.id, rawItem.reason);
}

@@ -862,3 +745,3 @@ } else if (rawItem.itemType === _ImageItem2.default) {

rawItem.reason = _DeadItem2.default.Reason.NOT_NCX;
logger.warn('Referenced resource \'%s\' ignored. (reason: %s)', rawItem.id, rawItem.reason);
_this2.logger.warn('Referenced resource \'%s\' ignored. (reason: %s)', rawItem.id, rawItem.reason);
}

@@ -868,3 +751,3 @@ }

if (!options.parseStyle) {
_context8.next = 21;
_context5.next = 21;
break;

@@ -874,3 +757,3 @@ }

if (!(rawItem.itemType === _CssItem2.default)) {
_context8.next = 15;
_context5.next = 15;
break;

@@ -880,3 +763,3 @@ }

rawItem.namespace = '' + options.styleNamespacePrefix + idx;
_context8.next = 21;
_context5.next = 21;
break;

@@ -886,11 +769,11 @@

if (!(rawItem.itemType === _SpineItem2.default)) {
_context8.next = 21;
_context5.next = 21;
break;
}
_context8.next = 18;
_context5.next = 18;
return _this2._parseSpineStyle(rawItem, itemEntry, options);
case 18:
result = _context8.sent;
result = _context5.sent;

@@ -901,3 +784,3 @@ rawItem.styles = result.styles;

case 21:
_context8.next = 26;
_context5.next = 26;
break;

@@ -908,3 +791,3 @@

rawItem.reason = _DeadItem2.default.Reason.NOT_EXISTS;
logger.warn('Referenced resource \'%s\' ignored. (reason: %s)', rawItem.id, rawItem.reason);
_this2.logger.warn('Referenced resource \'%s\' ignored. (reason: %s)', rawItem.id, rawItem.reason);

@@ -916,6 +799,6 @@ case 26:

case 'end':
return _context8.stop();
return _context5.stop();
}
}
}, _callee7, _this2);
}, _callee4, _this2);
})));

@@ -928,14 +811,14 @@ }, _promise2.default.resolve());

return _context9.abrupt('return', context);
return _context6.abrupt('return', context);
case 11:
case 'end':
return _context9.stop();
return _context6.stop();
}
}
}, _callee8, this);
}, _callee5, this);
}));
function _parseManifestAndSpine(_x6, _x7, _x8) {
return _ref7.apply(this, arguments);
function _parseManifestAndSpine(_x4, _x5, _x6) {
return _ref4.apply(this, arguments);
}

@@ -961,7 +844,7 @@

EpubParser.prototype._parseSpineStyle = function () {
var _ref9 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee9(rawItem, entry, options) {
var _ref6 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee6(rawItem, entry, options) {
var styles, inlineStyles, find, filter, document, html, head;
return _regenerator2.default.wrap(function _callee9$(_context10) {
return _regenerator2.default.wrap(function _callee6$(_context7) {
while (1) {
switch (_context10.prev = _context10.next) {
switch (_context7.prev = _context7.next) {
case 0:

@@ -983,9 +866,9 @@ styles = [];

_context10.t0 = _himalaya.parse;
_context10.next = 7;
_context7.t0 = _himalaya.parse;
_context7.next = 7;
return entry.getFile('utf8');
case 7:
_context10.t1 = _context10.sent;
document = (0, _context10.t0)(_context10.t1);
_context7.t1 = _context7.sent;
document = (0, _context7.t0)(_context7.t1);
html = find(document, 'tagName', 'html');

@@ -1035,14 +918,14 @@ head = find(html.children, 'tagName', 'head');

return _context10.abrupt('return', { styles: styles, inlineStyles: inlineStyles });
return _context7.abrupt('return', { styles: styles, inlineStyles: inlineStyles });
case 14:
case 'end':
return _context10.stop();
return _context7.stop();
}
}
}, _callee9, this);
}, _callee6, this);
}));
function _parseSpineStyle(_x9, _x10, _x11) {
return _ref9.apply(this, arguments);
function _parseSpineStyle(_x7, _x8, _x9) {
return _ref6.apply(this, arguments);
}

@@ -1056,4 +939,4 @@

* @param {object} guide guide AST
* @param {Context} context intermediate result
* @returns {Promise.<Context>} return Context containing guide
* @param {ParseContext} context intermediate result
* @returns {Promise.<ParseContext>} return Context containing guide
*/

@@ -1089,4 +972,4 @@

* NCX parsing
* @param {Context} context intermediate result
* @returns {Promise.<Context>} return Context containing ncx if exists
* @param {ParseContext} context intermediate result
* @returns {Promise.<ParseContext>} return Context containing ncx if exists
* @throws {Errors.EINVAL} invalid XML

@@ -1101,8 +984,8 @@ * @throws {Errors.EINVAL} can not found ncx attribute OPF

EpubParser.prototype._parseNcx = function () {
var _ref10 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee10(context) {
var _ref7 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee7(context) {
var rawBook, entries, options, allowNcxFileMissing, ncxItem, ncxEntry, _xmlLoader3, ncx, normalizeSrc, keyTranslator;
return _regenerator2.default.wrap(function _callee10$(_context11) {
return _regenerator2.default.wrap(function _callee7$(_context8) {
while (1) {
switch (_context11.prev = _context11.next) {
switch (_context8.prev = _context8.next) {
case 0:

@@ -1116,3 +999,3 @@ rawBook = context.rawBook, entries = context.entries, options = context.options;

if (!(0, _parserCore.isExists)(ncxItem)) {
_context11.next = 23;
_context8.next = 23;
break;

@@ -1124,3 +1007,3 @@ }

if (!(!allowNcxFileMissing && !(0, _parserCore.isExists)(ncxEntry))) {
_context11.next = 7;
_context8.next = 7;
break;

@@ -1132,13 +1015,13 @@ }

case 7:
_context11.t0 = _xmlLoader5.default;
_context11.next = 10;
_context8.t0 = _xmlLoader5.default;
_context8.next = 10;
return ncxEntry.getFile('utf8');
case 10:
_context11.t1 = _context11.sent;
_xmlLoader3 = (0, _context11.t0)(_context11.t1);
_context8.t1 = _context8.sent;
_xmlLoader3 = (0, _context8.t0)(_context8.t1);
ncx = _xmlLoader3.ncx;
if ((0, _parserCore.isExists)(ncx)) {
_context11.next = 15;
_context8.next = 15;
break;

@@ -1151,3 +1034,3 @@ }

if ((0, _parserCore.isExists)(ncx.navMap)) {
_context11.next = 17;
_context8.next = 17;
break;

@@ -1183,3 +1066,3 @@ }

});
_context11.next = 25;
_context8.next = 25;
break;

@@ -1189,3 +1072,3 @@

if (allowNcxFileMissing) {
_context11.next = 25;
_context8.next = 25;
break;

@@ -1197,14 +1080,14 @@ }

case 25:
return _context11.abrupt('return', context);
return _context8.abrupt('return', context);
case 26:
case 'end':
return _context11.stop();
return _context8.stop();
}
}
}, _callee10, this);
}, _callee7, this);
}));
function _parseNcx(_x12) {
return _ref10.apply(this, arguments);
function _parseNcx(_x10) {
return _ref7.apply(this, arguments);
}

@@ -1216,244 +1099,14 @@

/**
* Unzipping if zip source and unzipPath option specified
* @param {Context} context intermediate result
* @returns {Promise.<Context>} return Context (no change at this step)
* @throws {Errors.ENOENT} no such file or directory
* @see EpubParser.parseDefaultOptions.unzipPath
* @see EpubParser.parseDefaultOptions.overwrite
* @param {Book} book
*/
EpubParser.prototype._unzipIfNeeded = function () {
var _ref11 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee11(context) {
var options, entries, unzipPath, overwrite;
return _regenerator2.default.wrap(function _callee11$(_context12) {
while (1) {
switch (_context12.prev = _context12.next) {
case 0:
options = context.options, entries = context.entries;
unzipPath = options.unzipPath, overwrite = options.overwrite;
if (!(!(0, _parserCore.isString)(entries.source) && (0, _parserCore.isExists)(unzipPath))) {
_context12.next = 6;
break;
}
_context12.next = 5;
return entries.source.extractAll(unzipPath, overwrite);
case 5:
privateProps.set(this, (0, _extends3.default)({}, privateProps.get(this), { input: unzipPath }));
case 6:
return _context12.abrupt('return', context);
case 7:
case 'end':
return _context12.stop();
}
}
}, _callee11, this);
}));
function _unzipIfNeeded(_x13) {
return _ref11.apply(this, arguments);
EpubParser.prototype._checkResult = function _checkResult(book) {
if (!(0, _parserCore.isExists)(book.cover)) {
this.logger.warn('Cover image not found in EPUB.');
}
return _unzipIfNeeded;
}();
/**
* Create new Book from context
* @param {Context} context intermediate result
* @returns {Promise.<Book>} return Book
*/
EpubParser.prototype._createBook = function _createBook(context) {
return new _promise2.default(function (resolve) {
resolve(new _Book2.default(context.rawBook));
});
return book;
};
/**
* Reading contents of Item
* @param {Item} item target
* @param {?object} options read options
* @returns {(string|Buffer)} reading result
* @see EpubParser.readDefaultOptions
* @see EpubParser.readOptionTypes
* @example
* const options = { ... };
* parse.readItem(book.spine[0], options).then((result) => {
* ...
* });
*/
EpubParser.prototype.readItem = function () {
var _ref12 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee12(item) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var results;
return _regenerator2.default.wrap(function _callee12$(_context13) {
while (1) {
switch (_context13.prev = _context13.next) {
case 0:
_context13.next = 2;
return this.readItems([item], options);
case 2:
results = _context13.sent;
return _context13.abrupt('return', results[0]);
case 4:
case 'end':
return _context13.stop();
}
}
}, _callee12, this);
}));
function readItem(_x15) {
return _ref12.apply(this, arguments);
}
return readItem;
}();
/**
* Reading contents of Items
* @param {Item[]} items targets
* @param {?object} options read options
* @returns {(string|Buffer)[]} reading results
* @see EpubParser.readDefaultOptions
* @see EpubParser.readOptionTypes
* @example
* const options = { ... };
* parse.readItems(book.styles, options).then((results) => {
* ...
* });
*/
EpubParser.prototype.readItems = function () {
var _ref13 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee14(items) {
var _this3 = this;
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var tasks, context;
return _regenerator2.default.wrap(function _callee14$(_context15) {
while (1) {
switch (_context15.prev = _context15.next) {
case 0:
tasks = [{ func: this._prepareRead, name: 'prepareRead' }, { func: this._read, name: 'read' }];
context = [items, options];
_context15.next = 4;
return tasks.reduce(function (prevPromise, task) {
// eslint-disable-line arrow-body-style
return prevPromise.then((0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee13() {
var func, name;
return _regenerator2.default.wrap(function _callee13$(_context14) {
while (1) {
switch (_context14.prev = _context14.next) {
case 0:
func = task.func, name = task.name;
_context14.next = 3;
return logger.measure(func, _this3, (0, _parserCore.isArray)(context) ? context : [context], name);
case 3:
context = _context14.sent;
case 4:
case 'end':
return _context14.stop();
}
}
}, _callee13, _this3);
})));
}, _promise2.default.resolve());
case 4:
return _context15.abrupt('return', context);
case 5:
case 'end':
return _context15.stop();
}
}
}, _callee14, this);
}));
function readItems(_x17) {
return _ref13.apply(this, arguments);
}
return readItems;
}();
/**
* @typedef ReadContext
* @property {Item[]} items targets
* @property {object} entries from input
* @property {object} options read options
*/
/**
* Validate read options and get entries from input
* @param {Item[]} items targets
* @param {?object} options read options
* @returns {Promise.<ReadContext>}
* returns ReadContext containing target items, read options, entries
* @throws {Errors.EINVAL} invalid options or value type
* @throws {Errors.ENOENT} no such file or directory
* @throws {Errors.ENOFILE} no such file
* @see EpubParser.readDefaultOptions
* @see EpubParser.readOptionTypes
*/
EpubParser.prototype._prepareRead = function () {
var _ref15 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee15(items) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var entries;
return _regenerator2.default.wrap(function _callee15$(_context16) {
while (1) {
switch (_context16.prev = _context16.next) {
case 0:
if (!items.find(function (item) {
return !(item instanceof _Item2.default);
})) {
_context16.next = 2;
break;
}
throw (0, _parserCore.createError)(_parserCore.Errors.EINVAL, 'item', 'reason', 'item must be Item type');
case 2:
(0, _parserCore.validateOptions)(options, EpubParser.readOptionTypes);
_context16.next = 5;
return (0, _parserCore.readEntries)(this.input, this.cryptoProvider, logger);
case 5:
entries = _context16.sent;
return _context16.abrupt('return', {
items: items,
entries: entries,
options: (0, _parserCore.mergeObjects)(EpubParser.readDefaultOptions, options)
});
case 7:
case 'end':
return _context16.stop();
}
}
}, _callee15, this);
}));
function _prepareRead(_x19) {
return _ref15.apply(this, arguments);
}
return _prepareRead;
}();
/**
* Contents is read using loader suitable for context

@@ -1467,23 +1120,23 @@ * @param {ReadContext} context properties required for reading

EpubParser.prototype._read = function () {
var _ref16 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee17(context) {
var _this4 = this;
var _ref8 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee9(context) {
var _this3 = this;
var items, entries, options, results;
return _regenerator2.default.wrap(function _callee17$(_context18) {
return _regenerator2.default.wrap(function _callee9$(_context10) {
while (1) {
switch (_context18.prev = _context18.next) {
switch (_context10.prev = _context10.next) {
case 0:
items = context.items, entries = context.entries, options = context.options;
results = [];
_context18.next = 4;
_context10.next = 4;
return items.reduce(function (prevPromise, item) {
// eslint-disable-line arrow-body-style
return prevPromise.then((0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee16() {
return prevPromise.then((0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee8() {
var entry, file;
return _regenerator2.default.wrap(function _callee16$(_context17) {
return _regenerator2.default.wrap(function _callee8$(_context9) {
while (1) {
switch (_context17.prev = _context17.next) {
switch (_context9.prev = _context9.next) {
case 0:
if (!(item instanceof _InlineCssItem2.default)) {
_context17.next = 3;
_context9.next = 3;
break;

@@ -1493,3 +1146,3 @@ }

results.push((0, _cssLoader2.default)(item, item.text, options));
return _context17.abrupt('return');
return _context9.abrupt('return');

@@ -1500,3 +1153,3 @@ case 3:

if ((0, _parserCore.isExists)(entry)) {
_context17.next = 6;
_context9.next = 6;
break;

@@ -1508,7 +1161,7 @@ }

case 6:
_context17.next = 8;
_context9.next = 8;
return entry.getFile(item.defaultEncoding);
case 8:
file = _context17.sent;
file = _context9.sent;

@@ -1525,6 +1178,6 @@ if (item instanceof _SpineItem2.default) {

case 'end':
return _context17.stop();
return _context9.stop();
}
}
}, _callee16, _this4);
}, _callee8, _this3);
})));

@@ -1534,14 +1187,14 @@ }, _promise2.default.resolve());

case 4:
return _context18.abrupt('return', results);
return _context10.abrupt('return', results);
case 5:
case 'end':
return _context18.stop();
return _context10.stop();
}
}
}, _callee17, this);
}, _callee9, this);
}));
function _read(_x20) {
return _ref16.apply(this, arguments);
function _read(_x11) {
return _ref8.apply(this, arguments);
}

@@ -1592,5 +1245,5 @@

return EpubParser;
}();
}(_parserCore.Parser);
exports.default = EpubParser;
module.exports = exports.default;

@@ -78,3 +78,3 @@ 'use strict';

spines.sort(function (s1, s2) {
return s1.index < 0 || s2.index < 0 ? 0 : s1.index - s2.index;
return s1.index - s2.index;
}).forEach(function (spine, idx, list) {

@@ -81,0 +81,0 @@ var prevSpine = list[idx - 1];

@@ -35,4 +35,2 @@ 'use strict';

var ignoredIndex = -1;
var SpineItem = function (_Item) {

@@ -54,3 +52,3 @@ (0, _inherits3.default)(SpineItem, _Item);

_this.index = (0, _parserCore.isExists)(rawObj.index) ? rawObj.index : ignoredIndex;
_this.index = rawObj.index;
_this.isLinear = (0, _parserCore.isExists)(rawObj.isLinear) ? rawObj.isLinear : true;

@@ -95,5 +93,3 @@ if ((0, _parserCore.isExists)(rawObj.styles)) {

SpineItem.IGNORED_INDEX = ignoredIndex;
exports.default = SpineItem;
module.exports = exports.default;
{
"name": "@ridi/epub-parser",
"version": "0.3.0-alpha-6",
"version": "0.3.0-alpha-7",
"description": "Common EPUB2 data parser for Ridibooks services",

@@ -31,3 +31,3 @@ "author": {

"dependencies": {
"@ridi/parser-core": "^0.3.0-alpha-6",
"@ridi/parser-core": "^0.3.0-alpha-7",
"css-tree": "^1.0.0-alpha.29",

@@ -34,0 +34,0 @@ "fast-xml-parser": "^3.11.2",

@@ -314,3 +314,2 @@ # @ridi/epub-parser

* [overwrite](#overwrite)
* [ignoreLinear](#ignoreLinear)
* [parseStyle](#parseStyle)

@@ -369,20 +368,2 @@ * [styleNamespacePrefix](#styleNamespacePrefix)

<a id="ignoreLinear"></a>
### ignoreLinear: *`boolean`*
If true, ignore `index` difference caused by `isLinear` property of [SpineItem](#spineItem).
```js
// e.g. If left is false, right is true.
[{ index: 0, isLinear: true, ... }, [{ index: 0, isLinear: true, ... },
{ index: 1, isLinear: true, ... }, { index: 1, isLinear: true, ... },
{ index: -2, isLinear: false, ... }, { index: 2, isLinear: false, ... },
{ index: 3, isLinear: true, ... }] { index: 3, isLinear: true, ... }]
```
**Default:** `false`
---
<a id="parseStyle"></a>

@@ -389,0 +370,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc