@daisy/epub-utils
Advanced tools
Comparing version 1.2.0-beta.6 to 1.2.0-beta.7
@@ -14,4 +14,4 @@ // input: unzipped book directory | ||
const fileUrl = require('file-url'); | ||
const DOMParser = require('xmldom-alpha').DOMParser; | ||
const XMLSerializer = require('xmldom-alpha').XMLSerializer; | ||
const DOMParser = require('xmldom').DOMParser; | ||
const XMLSerializer = require('xmldom').XMLSerializer; | ||
const fs = require('fs'); | ||
@@ -45,3 +45,6 @@ const path = require('path'); | ||
const content = fs.readFileSync(fullpath).toString(); | ||
//FIXME hack to workaround xmldom-alpha regex test | ||
// not application/xhtml+xml because: | ||
// https://github.com/jindw/xmldom/pull/208 | ||
// https://github.com/jindw/xmldom/pull/242 | ||
// https://github.com/xmldom/xmldom/blob/3db6ccf3f7ecbde73608490d71f96c727abdd69a/lib/dom-parser.js#L12 | ||
const doc = new DOMParser({ errorHandler }).parseFromString(content, 'application/xhtml'); | ||
@@ -85,7 +88,18 @@ | ||
const result = {}; | ||
select('//dc:*', doc).forEach(dcElem => { | ||
select('/opf:package/opf:metadata/dc:*', doc).forEach(dcElem => { | ||
addMeta(`dc:${dcElem.localName}`, dcElem.textContent, result); | ||
}); | ||
select('//opf:meta[not(@refines)]', doc).forEach(meta => { | ||
addMeta(meta.getAttribute('property'), meta.textContent, result); | ||
select('/opf:package/opf:metadata/opf:meta[not(@refines)]', doc).forEach(meta => { | ||
const prop = meta.getAttribute('property'); | ||
if (prop) { | ||
if (meta.textContent) { | ||
addMeta(prop, meta.textContent, result); | ||
} | ||
} else { | ||
const name = meta.getAttribute('name'); | ||
const content = meta.getAttribute('content'); | ||
if (name && content) { | ||
addMeta(name, content, result); | ||
} | ||
} | ||
}); | ||
@@ -106,4 +120,4 @@ return result; | ||
const result = {}; | ||
select('//opf:link[not(@refines)]', doc).forEach(link => { | ||
addLink(link.getAttribute('rel'), decodeURI(link.getAttribute('href')), result); | ||
select('/opf:package/opf:metadata/opf:link[not(@refines)]', doc).forEach(link => { | ||
addLink(link.getAttribute('rel') || link.getAttribute('property'), decodeURI(link.getAttribute('href')), result); | ||
}); | ||
@@ -145,5 +159,5 @@ return result; | ||
const spineItemIdrefs = select('//opf:itemref/@idref', doc); | ||
const spineItemIdrefs = select('/opf:package/opf:spine/opf:itemref/@idref', doc); | ||
spineItemIdrefs.forEach(idref => { | ||
const manifestItem = select(`//opf:item[@id='${idref.nodeValue}']`, doc); | ||
const manifestItem = select(`/opf:package/opf:manifest/opf:item[@id='${idref.nodeValue}']`, doc); | ||
if (manifestItem.length > 0) { | ||
@@ -169,3 +183,3 @@ const contentType = (manifestItem[0].getAttribute('media-type') || '').trim(); | ||
const navDocRef = select('//opf:item' + '[contains(concat(" ", normalize-space(@properties), " ")," nav ")]' + '/@href', doc); | ||
const navDocRef = select('/opf:package/opf:manifest/opf:item' + '[contains(concat(" ", normalize-space(@properties), " ")," nav ")]' + '/@href', doc); | ||
if (navDocRef.length > 0) { | ||
@@ -177,4 +191,5 @@ const navDocPath = decodeURI(navDocRef[0].nodeValue); | ||
this.hasBindings = select('//opf:bindings', doc).length > 0; | ||
this.hasManifestFallbacks = select('//opf:item[@fallback]', doc).length > 0; | ||
this.hasGuide = select('/opf:package/opf:guide', doc).length > 0; | ||
this.hasBindings = select('/opf:package/opf:bindings', doc).length > 0; | ||
this.hasManifestFallbacks = select('/opf:package/opf:manifest/opf:item[@fallback]', doc).length > 0; | ||
}; | ||
@@ -184,6 +199,9 @@ | ||
const content = fs.readFileSync(filepath).toString(); | ||
//FIXME hack to workaround xmldom-alpha regex test | ||
// not application/xhtml+xml because: | ||
// https://github.com/jindw/xmldom/pull/208 | ||
// https://github.com/jindw/xmldom/pull/242 | ||
// https://github.com/xmldom/xmldom/blob/3db6ccf3f7ecbde73608490d71f96c727abdd69a/lib/dom-parser.js#L12 | ||
const doc = new DOMParser({ errorHandler }).parseFromString(content, 'application/xhtml'); | ||
const select = xpath.useNamespaces({ html: "http://www.w3.org/1999/xhtml", epub: "http://www.idpf.org/2007/ops" }); | ||
const title = select('//html:title/text()', doc); | ||
const title = select('/html:html/html:head/html:title/text()', doc); | ||
if (title.length > 0) { | ||
@@ -201,3 +219,3 @@ return title[0].nodeValue; | ||
const select = xpath.useNamespaces({ ocf: 'urn:oasis:names:tc:opendocument:xmlns:container' }); | ||
const rootfiles = select('//ocf:rootfile[@media-type="application/oebps-package+xml"]/@full-path', doc); | ||
const rootfiles = select('/ocf:container/ocf:rootfiles/ocf:rootfile[@media-type="application/oebps-package+xml"]/@full-path', doc); | ||
// just grab the first one as we're not handling the case of multiple renditions | ||
@@ -204,0 +222,0 @@ if (rootfiles.length > 0) { |
@@ -15,4 +15,4 @@ 'use strict'; | ||
async function unzip(path, useLegacyZipLib) { | ||
const tmpdir = tmp.dirSync({ unsafeCleanup: true, keep: LOG_DEBUG_URLS }).name; | ||
async function unzip(unzipDir, path, useLegacyZipLib) { | ||
const tmpdir = unzipDir || tmp.dirSync({ unsafeCleanup: true, keep: LOG_DEBUG_URLS }).name; | ||
if (LOG_DEBUG_URLS) { | ||
@@ -25,11 +25,9 @@ console.log(">>>>>> LOG_DEBUG_URLS"); | ||
if (useLegacyZipLib) { | ||
extractZip(path, { dir: tmpdir }, err => { | ||
if (err) { | ||
if (LOG_DEBUG_URLS) { | ||
console.log(err); | ||
} | ||
reject(err); | ||
} else { | ||
resolve(tmpdir); | ||
extractZip(path, { dir: tmpdir }).then(() => { | ||
resolve(tmpdir); | ||
}).catch(err => { | ||
if (LOG_DEBUG_URLS) { | ||
console.log(err); | ||
} | ||
reject(err); | ||
}); | ||
@@ -75,3 +73,3 @@ } else { | ||
async function retryUnzip(epub, error) { | ||
async function retryUnzip(unzipDir, epub, error) { | ||
if (error.message === undefined) throw error; | ||
@@ -102,3 +100,3 @@ winston.info('Trying to repair the archive and unzip again...'); | ||
fs.truncateSync(tmpEPUB, truncatedSize); | ||
const res = await unzip(tmpEPUB, true); | ||
const res = await unzip(unzipDir, unzipDirtmpEPUB, true); | ||
if (needsDelete) { | ||
@@ -134,3 +132,3 @@ process.nextTick(() => { | ||
async extract() { | ||
async extract(unzipDir) { | ||
if (this.basedir !== undefined) { | ||
@@ -146,3 +144,3 @@ return this; | ||
try { | ||
unzippedDir = await unzip(this.path); | ||
unzippedDir = await unzip(unzipDir, this.path); | ||
} catch (error) { | ||
@@ -152,3 +150,3 @@ winston.error('Failed to unzip EPUB (the ZIP archive may be corrupt). TRYING LEGACY ZIP LIB ...'); | ||
try { | ||
unzippedDir = await unzip(this.path, true); | ||
unzippedDir = await unzip(unzipDir, this.path, true); | ||
} catch (error) { | ||
@@ -158,3 +156,3 @@ winston.error('Failed to unzip EPUB again (the ZIP archive may be corrupt). TRYING ZIP PATCH ...'); | ||
try { | ||
unzippedDir = await retryUnzip(this, error); | ||
unzippedDir = await retryUnzip(unzipDir, this, error); | ||
} catch (error) { | ||
@@ -161,0 +159,0 @@ throw error; |
{ | ||
"name": "@daisy/epub-utils", | ||
"version": "1.2.0-beta.6", | ||
"version": "1.2.0-beta.7", | ||
"description": "EPUB parser and model, used by Ace", | ||
@@ -21,10 +21,10 @@ "author": { | ||
"dependencies": { | ||
"extract-zip": "^1.6.7", | ||
"extract-zip": "^2.0.0", | ||
"file-url": "^3.0.0", | ||
"fs-extra": "^8.1.0", | ||
"node-stream-zip": "^1.8.2", | ||
"fs-extra": "^9.0.0", | ||
"node-stream-zip": "^1.9.1", | ||
"tmp": "^0.1.0", | ||
"winston": "^3.2.1", | ||
"xmldom-alpha": "^0.1.28", | ||
"xpath": "0.0.24" | ||
"xmldom": "^0.3.0", | ||
"xpath": "^0.0.27" | ||
}, | ||
@@ -31,0 +31,0 @@ "publishConfig": { |
14464
362
+ Addedxmldom@^0.3.0
+ Added@types/node@22.13.1(transitive)
+ Added@types/yauzl@2.10.3(transitive)
+ Addedat-least-node@1.0.0(transitive)
+ Addeddebug@4.4.0(transitive)
+ Addedend-of-stream@1.4.4(transitive)
+ Addedextract-zip@2.0.1(transitive)
+ Addedfs-extra@9.1.0(transitive)
+ Addedget-stream@5.2.0(transitive)
+ Addedjsonfile@6.1.0(transitive)
+ Addedpump@3.0.2(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedstring_decoder@1.3.0(transitive)
+ Addedundici-types@6.20.0(transitive)
+ Addeduniversalify@2.0.1(transitive)
+ Addedxmldom@0.3.0(transitive)
+ Addedxpath@0.0.27(transitive)
- Removedxmldom-alpha@^0.1.28
- Removedbuffer-from@1.1.2(transitive)
- Removedconcat-stream@1.6.2(transitive)
- Removedcore-util-is@1.0.3(transitive)
- Removeddebug@2.6.9(transitive)
- Removedextract-zip@1.7.0(transitive)
- Removedfs-extra@8.1.0(transitive)
- Removedisarray@1.0.0(transitive)
- Removedjsonfile@4.0.0(transitive)
- Removedminimist@1.2.8(transitive)
- Removedmkdirp@0.5.6(transitive)
- Removedms@2.0.0(transitive)
- Removedprocess-nextick-args@2.0.1(transitive)
- Removedreadable-stream@2.3.8(transitive)
- Removedsafe-buffer@5.1.2(transitive)
- Removedstring_decoder@1.1.1(transitive)
- Removedtypedarray@0.0.6(transitive)
- Removeduniversalify@0.1.2(transitive)
- Removedxmldom-alpha@0.1.28(transitive)
- Removedxpath@0.0.24(transitive)
Updatedextract-zip@^2.0.0
Updatedfs-extra@^9.0.0
Updatednode-stream-zip@^1.9.1
Updatedxpath@^0.0.27