Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@ridi/comic-parser

Package Overview
Dependencies
Maintainers
12
Versions
69
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ridi/comic-parser - npm Package Compare versions

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

lib/model/ParseContext.js

555

lib/ComicParser.js

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

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

@@ -24,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');

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

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

@@ -34,12 +42,8 @@

var _weakMap = require('babel-runtime/core-js/weak-map');
var _inherits2 = require('babel-runtime/helpers/inherits');
var _weakMap2 = _interopRequireDefault(_weakMap);
var _inherits3 = _interopRequireDefault(_inherits2);
var _parserCore = require('@ridi/parser-core');
var _fs = require('fs');
var _fs2 = _interopRequireDefault(_fs);
var _naturalOrderby = require('natural-orderby');

@@ -51,6 +55,2 @@

var _Context = require('./model/Context');
var _Context2 = _interopRequireDefault(_Context);
var _Book = require('./model/Book');

@@ -64,50 +64,15 @@

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _ReadContext = require('./model/ReadContext');
var privateProps = new _weakMap2.default();
var _ReadContext2 = _interopRequireDefault(_ReadContext);
var logger = new _parserCore.Logger('ComicParser');
var _ParseContext = require('./model/ParseContext');
var ComicParser = function () {
(0, _createClass3.default)(ComicParser, [{
key: 'input',
var _ParseContext2 = _interopRequireDefault(_ParseContext);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* 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 ComicParser
* @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 ComicParser('./foo/bar.zip' or './foo/bar');
*/
}], [{
var ComicParser = function (_Parser) {
(0, _inherits3.default)(ComicParser, _Parser);
(0, _createClass3.default)(ComicParser, null, [{
key: 'parseDefaultOptions',

@@ -119,10 +84,6 @@

get: function get() {
return {
// If specified, uncompress to that path. (only using if input is Zip file.)
unzipPath: undefined,
// If true, overwrite to unzipPath when uncompress. (only using if unzipPath specified.)
overwrite: true,
return (0, _extends3.default)({}, _Parser.parseDefaultOptions, {
// File extension to allow when extracting lists.
ext: ['jpg', 'jpeg', 'png', 'bmp', 'gif']
};
});
}

@@ -137,7 +98,5 @@

get: function get() {
return {
unzipPath: 'String|Undefined',
overwrite: 'Boolean',
return (0, _extends3.default)({}, _Parser.parseOptionTypes, {
ext: 'Array'
};
});
}

@@ -152,6 +111,6 @@

get: function get() {
return {
return (0, _extends3.default)({}, _Parser.readDefaultOptions, {
// If false, reads image into a buffer.
base64: false
};
});
}

@@ -166,6 +125,16 @@

get: function get() {
return {
return (0, _extends3.default)({}, _Parser.readOptionTypes, {
base64: 'Boolean'
};
});
}
/**
* Create new ComicParser
* @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 ComicParser('./foo/bar.zip' or './foo/bar');
*/
}]);

@@ -175,132 +144,54 @@

(0, _classCallCheck3.default)(this, ComicParser);
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, 'ComicParser'));
}
/**
* Comic Parsing
* @param {?object} options parse options
* @returns {Promise.<Book>} return Book
* @see ComicParser.parseDefaultOptions
* @see ComicParser.parseOptionTypes
* @example
* const options = { unzipPath: './foo/bar' };
* parser.parse(options).then((book) => {
* ...
* });
* @returns {ParseContext}
*/
ComicParser.prototype.parse = function () {
var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2() {
var _this = this;
ComicParser.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._parse, name: 'parse' }, { 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());
ComicParser.prototype._getBookClass = function _getBookClass() {
return _Book2.default;
};
case 4:
return _context2.abrupt('return', context);
/**
* @returns {ReadContext}
*/
case 5:
case 'end':
return _context2.stop();
}
}
}, _callee2, this);
}));
function parse() {
return _ref.apply(this, arguments);
}
ComicParser.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 ComicParser.parseDefaultOptions
* @see ComicParser.parseOptionTypes
* @returns {Item}
*/
ComicParser.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, ComicParser.parseOptionTypes);
context = new _Context2.default();
ComicParser.prototype._getReadItemClass = function _getReadItemClass() {
return _Item2.default;
};
context.options = (0, _parserCore.mergeObjects)(ComicParser.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);
}));
ComicParser.prototype._parseTasks = function _parseTasks() {
return [].concat((0, _toConsumableArray3.default)(_Parser.prototype._parseTasks.call(this)), [{ func: this._parse, name: 'parse' }]);
};
function _prepareParse() {
return _ref3.apply(this, arguments);
}
return _prepareParse;
}();
/**
* extracts only necessary metadata from entries and create item list
* @param {Context} context intermediate result
* @returns {Promise.<Context>} return Context containing item list
* @param {ReadContext} context intermediate result
* @returns {Promise.<ReadContext>} return Context containing item list
* @see ComicParser.parseDefaultOptions.filter

@@ -311,9 +202,9 @@ */

ComicParser.prototype._parse = function () {
var _ref4 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee5(context) {
var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2(context) {
var _this2 = this;
var entries, rawBook, options, items;
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:

@@ -333,9 +224,9 @@ entries = context.entries, rawBook = context.rawBook, options = context.options;

rawBook.items = [];
_context5.next = 5;
_context2.next = 5;
return items.reduce(function (prevPromise, entry, index) {
// eslint-disable-line arrow-body-style
return prevPromise.then((0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee4() {
return _regenerator2.default.wrap(function _callee4$(_context4) {
return prevPromise.then((0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee() {
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context4.prev = _context4.next) {
switch (_context.prev = _context.next) {
case 0:

@@ -350,6 +241,6 @@ rawBook.items.push({

case 'end':
return _context4.stop();
return _context.stop();
}
}
}, _callee4, _this2);
}, _callee, _this2);
})));

@@ -359,14 +250,14 @@ }, _promise2.default.resolve());

case 5:
return _context5.abrupt('return', context);
return _context2.abrupt('return', context);
case 6:
case 'end':
return _context5.stop();
return _context2.stop();
}
}
}, _callee5, this);
}, _callee2, this);
}));
function _parse(_x3) {
return _ref4.apply(this, arguments);
function _parse(_x) {
return _ref.apply(this, arguments);
}

@@ -378,244 +269,2 @@

/**
* 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 ComicParser.parseDefaultOptions.unzipPath
* @see ComicParser.parseDefaultOptions.overwrite
*/
ComicParser.prototype._unzipIfNeeded = function () {
var _ref6 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee6(context) {
var options, entries, unzipPath, overwrite;
return _regenerator2.default.wrap(function _callee6$(_context6) {
while (1) {
switch (_context6.prev = _context6.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))) {
_context6.next = 6;
break;
}
_context6.next = 5;
return entries.source.extractAll(unzipPath, overwrite);
case 5:
privateProps.set(this, (0, _extends3.default)({}, privateProps.get(this), { input: unzipPath }));
case 6:
return _context6.abrupt('return', context);
case 7:
case 'end':
return _context6.stop();
}
}
}, _callee6, this);
}));
function _unzipIfNeeded(_x4) {
return _ref6.apply(this, arguments);
}
return _unzipIfNeeded;
}();
/**
* Create new Book from context
* @param {Context} context intermediate result
* @returns {Promise.<Book>} return Book
*/
ComicParser.prototype._createBook = function _createBook(context) {
return new _promise2.default(function (resolve) {
resolve(new _Book2.default(context.rawBook));
});
};
/**
* Reading contents of Item
* @param {Item} item target
* @param {?object} options read options
* @returns {(string|Buffer)} reading result
* @see ComicParser.readDefaultOptions
* @see ComicParser.readOptionTypes
* @example
* const options = { ... };
* parse.readItem(book.spine[0], options).then((result) => {
* ...
* });
*/
ComicParser.prototype.readItem = function () {
var _ref7 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee7(item) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var results;
return _regenerator2.default.wrap(function _callee7$(_context7) {
while (1) {
switch (_context7.prev = _context7.next) {
case 0:
_context7.next = 2;
return this.readItems([item], options);
case 2:
results = _context7.sent;
return _context7.abrupt('return', results[0]);
case 4:
case 'end':
return _context7.stop();
}
}
}, _callee7, this);
}));
function readItem(_x6) {
return _ref7.apply(this, arguments);
}
return readItem;
}();
/**
* Reading contents of Items
* @param {Item[]} items targets
* @param {?object} options read options
* @returns {(string|Buffer)[]} reading results
* @see ComicParser.readDefaultOptions
* @see ComicParser.readOptionTypes
* @example
* const options = { ... };
* parse.readItems(book.styles, options).then((results) => {
* ...
* });
*/
ComicParser.prototype.readItems = function () {
var _ref8 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee9(items) {
var _this3 = this;
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var tasks, context;
return _regenerator2.default.wrap(function _callee9$(_context9) {
while (1) {
switch (_context9.prev = _context9.next) {
case 0:
tasks = [{ func: this._prepareRead, name: 'prepareRead' }, { func: this._read, name: 'read' }];
context = [items, options];
_context9.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 _callee8() {
var func, name;
return _regenerator2.default.wrap(function _callee8$(_context8) {
while (1) {
switch (_context8.prev = _context8.next) {
case 0:
func = task.func, name = task.name;
_context8.next = 3;
return logger.measure(func, _this3, (0, _parserCore.isArray)(context) ? context : [context], name);
case 3:
context = _context8.sent;
case 4:
case 'end':
return _context8.stop();
}
}
}, _callee8, _this3);
})));
}, _promise2.default.resolve());
case 4:
return _context9.abrupt('return', context);
case 5:
case 'end':
return _context9.stop();
}
}
}, _callee9, this);
}));
function readItems(_x8) {
return _ref8.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 ComicParser.readDefaultOptions
* @see ComicParser.readOptionTypes
*/
ComicParser.prototype._prepareRead = function () {
var _ref10 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee10(items) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var entries;
return _regenerator2.default.wrap(function _callee10$(_context10) {
while (1) {
switch (_context10.prev = _context10.next) {
case 0:
if (!items.find(function (item) {
return !(item instanceof _Item2.default);
})) {
_context10.next = 2;
break;
}
throw (0, _parserCore.createError)(_parserCore.Errors.EINVAL, 'item', 'reason', 'item must be Item type');
case 2:
(0, _parserCore.validateOptions)(options, ComicParser.readOptionTypes);
_context10.next = 5;
return (0, _parserCore.readEntries)(this.input, this.cryptoProvider, logger);
case 5:
entries = _context10.sent;
return _context10.abrupt('return', {
items: items,
entries: entries,
options: (0, _parserCore.mergeObjects)(ComicParser.readDefaultOptions, options)
});
case 7:
case 'end':
return _context10.stop();
}
}
}, _callee10, this);
}));
function _prepareRead(_x10) {
return _ref10.apply(this, arguments);
}
return _prepareRead;
}();
/**
* Contents is read using loader suitable for context

@@ -629,20 +278,20 @@ * @param {ReadContext} context properties required for reading

ComicParser.prototype._read = function () {
var _ref11 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee12(context) {
var _this4 = this;
var _ref3 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee4(context) {
var _this3 = this;
var items, entries, options, results;
return _regenerator2.default.wrap(function _callee12$(_context12) {
return _regenerator2.default.wrap(function _callee4$(_context4) {
while (1) {
switch (_context12.prev = _context12.next) {
switch (_context4.prev = _context4.next) {
case 0:
items = context.items, entries = context.entries, options = context.options;
results = [];
_context12.next = 4;
_context4.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 _callee11() {
return prevPromise.then((0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee3() {
var entry, file;
return _regenerator2.default.wrap(function _callee11$(_context11) {
return _regenerator2.default.wrap(function _callee3$(_context3) {
while (1) {
switch (_context11.prev = _context11.next) {
switch (_context3.prev = _context3.next) {
case 0:

@@ -652,3 +301,3 @@ entry = entries.find(item.path);

if ((0, _parserCore.isExists)(entry)) {
_context11.next = 3;
_context3.next = 3;
break;

@@ -660,7 +309,7 @@ }

case 3:
_context11.next = 5;
_context3.next = 5;
return entry.getFile();
case 5:
file = _context11.sent;
file = _context3.sent;

@@ -675,6 +324,6 @@ if (options.base64) {

case 'end':
return _context11.stop();
return _context3.stop();
}
}
}, _callee11, _this4);
}, _callee3, _this3);
})));

@@ -684,14 +333,14 @@ }, _promise2.default.resolve());

case 4:
return _context12.abrupt('return', results);
return _context4.abrupt('return', results);
case 5:
case 'end':
return _context12.stop();
return _context4.stop();
}
}
}, _callee12, this);
}, _callee4, this);
}));
function _read(_x11) {
return _ref11.apply(this, arguments);
function _read(_x2) {
return _ref3.apply(this, arguments);
}

@@ -703,5 +352,5 @@

return ComicParser;
}();
}(_parserCore.Parser);
exports.default = ComicParser;
module.exports = exports.default;
{
"name": "@ridi/comic-parser",
"version": "0.3.0-alpha-6",
"version": "0.3.0-alpha-7",
"description": "Common comic data parser for Ridibooks services",

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

"dependencies": {
"@ridi/parser-core": "^0.3.0-alpha-6",
"@ridi/parser-core": "^0.3.0-alpha-7",
"fs-extra": "^7.0.1",

@@ -24,0 +24,0 @@ "natural-orderby": "^1.0.2"

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