@ridi/epub-parser
Advanced tools
Comparing version 0.3.0-alpha.3 to 0.3.0-alpha.4
@@ -354,16 +354,13 @@ 'use strict'; | ||
(0, _parserCore.validateOptions)(options, EpubParser.parseOptionTypes); | ||
if (this.cryptoProvider) { | ||
this.cryptoProvider.status = _parserCore.CryptoProvider.Status.PARSE; | ||
} | ||
context = new _Context2.default(); | ||
context.options = (0, _parserCore.mergeObjects)(EpubParser.parseDefaultOptions, options); | ||
_context2.next = 6; | ||
_context2.next = 5; | ||
return (0, _parserCore.readEntries)(this.input, this.cryptoProvider); | ||
case 6: | ||
case 5: | ||
context.entries = _context2.sent; | ||
return _context2.abrupt('return', context); | ||
case 8: | ||
case 7: | ||
case 'end': | ||
@@ -1165,19 +1162,16 @@ return _context2.stop(); | ||
if (!(!(0, _parserCore.isString)(entries.source) && (0, _parserCore.isExists)(unzipPath))) { | ||
_context11.next = 7; | ||
_context11.next = 6; | ||
break; | ||
} | ||
if (this.cryptoProvider) { | ||
this.cryptoProvider.status = _parserCore.CryptoProvider.Status.UNZIP; | ||
} | ||
_context11.next = 6; | ||
_context11.next = 5; | ||
return entries.source.extractAll(unzipPath, overwrite); | ||
case 6: | ||
case 5: | ||
privateProps.set(this, (0, _extends3.default)({}, privateProps.get(this), { input: unzipPath })); | ||
case 7: | ||
case 6: | ||
return _context11.abrupt('return', context); | ||
case 8: | ||
case 7: | ||
case 'end': | ||
@@ -1349,6 +1343,2 @@ return _context11.stop(); | ||
entries = _context14.sent; | ||
if (this.cryptoProvider) { | ||
this.cryptoProvider.status = _parserCore.CryptoProvider.Status.READ; | ||
} | ||
return _context14.abrupt('return', { | ||
@@ -1360,3 +1350,3 @@ items: items, | ||
case 8: | ||
case 7: | ||
case 'end': | ||
@@ -1363,0 +1353,0 @@ return _context14.stop(); |
{ | ||
"name": "@ridi/epub-parser", | ||
"version": "0.3.0-alpha.3", | ||
"version": "0.3.0-alpha.4", | ||
"description": "Common EPUB2 data parser for Ridibooks services", | ||
@@ -31,3 +31,3 @@ "author": { | ||
"dependencies": { | ||
"@ridi/parser-core": "^0.3.0-alpha.3", | ||
"@ridi/parser-core": "^0.3.0-alpha.4", | ||
"css-tree": "^1.0.0-alpha.29", | ||
@@ -34,0 +34,0 @@ "fast-xml-parser": "^3.11.2", |
@@ -62,3 +62,3 @@ # @ridi/epub-parser | ||
const { Status } = CryptoProvider; | ||
const { Purpose } = CryptoProvider; | ||
const { Modes, Padding } = Cryptor; | ||
@@ -69,26 +69,38 @@ | ||
super(); | ||
this.cryptor = new Cryptor(Modes.ECB, { key, padding: Padding.PKCS7 }); | ||
this.cryptor = new Cryptor(Modes.ECB, { key }); | ||
} | ||
// Encrypt all content when unzipping and decrypt it when read. | ||
run(data, filePath) { | ||
if (this.status === Status.UNZIP) { | ||
return this.encrypt(data); | ||
} else if (this.status === Status.READ) { | ||
return Buffer.from(this.decrypt(data)); | ||
getCryptor(filePath, purpose) { | ||
return this.cryptor; | ||
} | ||
// If use as follows: | ||
// const provider = new ContentCryptoProvider(...); | ||
// const parser = new EpubParser('encrypted.epub', provider); | ||
// const book = await parser.parse({ unzipPath: ... }); | ||
// const firstSpine = await parser.readItem(book.spines[0]); | ||
// | ||
// It will be called as follows: | ||
// 1. run(data, 'encrypted.epub', Purpose.READ_IN_DIR) | ||
// 2. run(data, 'META-INF/container.xml', Purpose.READ_IN_ZIP) | ||
// 3. run(data, 'OEBPS/content.opf', Purpose.READ_IN_ZIP) | ||
// ... | ||
// 4. run(data, 'mimetype', Purpose.WRITE) | ||
// ... | ||
// 5. run(data, 'OEBPS/Text/Section0001.xhtml', Purpose.READ_IN_DIR) | ||
// | ||
run(data, filePath, purpose) { | ||
const cryptor = this.getCryptor(filePath, purpose); | ||
const padding = Padding.AUTO; | ||
if (purpose === Purpose.READ_IN_DIR) { | ||
return cryptor.decrypt(data, padding); | ||
} else if (purpose === Purpose.WRITE) { | ||
return cryptor.encrypt(data, padding); | ||
} | ||
return data; | ||
} | ||
encrypt(data, filePath) { | ||
return this.cryptor.encrypt(data); | ||
} | ||
decrypt(data, filePath) { | ||
return this.cryptor.decrypt(data); | ||
} | ||
} | ||
const cryptoProvider = new ContentCryptoProvider(key); | ||
const parser = new EpubParser('./foo/bar.epub' or './unzippedPath', cryptoProvider); | ||
const parser = new EpubParser('./encrypted.epub' or './unzippedPath', cryptoProvider); | ||
``` | ||
@@ -95,0 +107,0 @@ |
120483
568
2615