epub-optimizer 0.0.0-stage → 3.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +1434 -2
- package/dist/src/batch.d.ts +34 -0
- package/dist/src/batch.js +137 -0
- package/dist/src/cli.d.ts +7 -0
- package/dist/src/cli.js +275 -0
- package/dist/src/index.d.ts +23 -0
- package/dist/src/index.js +24 -0
- package/dist/src/optimization.d.ts +7 -0
- package/dist/src/optimization.js +207 -0
- package/dist/src/pipeline.d.ts +2 -0
- package/dist/src/pipeline.js +48 -0
- package/dist/src/processors/archive-processor.d.ts +26 -0
- package/dist/src/processors/archive-processor.js +170 -0
- package/dist/src/processors/epub-processor.d.ts +17 -0
- package/dist/src/processors/epub-processor.js +133 -0
- package/dist/src/processors/font-characters.d.ts +2 -0
- package/dist/src/processors/font-characters.js +227 -0
- package/dist/src/processors/font-engine.d.ts +6 -0
- package/dist/src/processors/font-engine.js +101 -0
- package/dist/src/processors/font-limits.d.ts +3 -0
- package/dist/src/processors/font-limits.js +3 -0
- package/dist/src/processors/font-processor.d.ts +4 -0
- package/dist/src/processors/font-processor.js +121 -0
- package/dist/src/processors/font-runner.d.ts +8 -0
- package/dist/src/processors/font-runner.js +94 -0
- package/dist/src/processors/font-worker.d.ts +1 -0
- package/dist/src/processors/font-worker.js +12 -0
- package/dist/src/processors/html-processor.d.ts +22 -0
- package/dist/src/processors/html-processor.js +190 -0
- package/dist/src/processors/image-converter.d.ts +14 -0
- package/dist/src/processors/image-converter.js +481 -0
- package/dist/src/processors/image-processor.d.ts +24 -0
- package/dist/src/processors/image-processor.js +165 -0
- package/dist/src/processors/js-processor.d.ts +7 -0
- package/dist/src/processors/js-processor.js +59 -0
- package/dist/src/processors/lazy-img.d.ts +5 -0
- package/dist/src/processors/lazy-img.js +48 -0
- package/dist/src/processors/svg-optimizer.d.ts +5 -0
- package/dist/src/processors/svg-optimizer.js +49 -0
- package/dist/src/scripts/create-epub.d.ts +2 -0
- package/dist/src/scripts/create-epub.js +29 -0
- package/dist/src/scripts/fix/fix-span-tags.d.ts +2 -0
- package/dist/src/scripts/fix/fix-span-tags.js +36 -0
- package/dist/src/scripts/fix/fix-xml.d.ts +3 -0
- package/dist/src/scripts/fix/fix-xml.js +59 -0
- package/dist/src/scripts/fix/index.d.ts +5 -0
- package/dist/src/scripts/fix/index.js +20 -0
- package/dist/src/scripts/fix/remove-empty-styles.d.ts +10 -0
- package/dist/src/scripts/fix/remove-empty-styles.js +53 -0
- package/dist/src/scripts/fix/xhtml-files.d.ts +2 -0
- package/dist/src/scripts/fix/xhtml-files.js +34 -0
- package/dist/src/scripts/ops/add-chapter-sections-to-toc.d.ts +5 -0
- package/dist/src/scripts/ops/add-chapter-sections-to-toc.js +242 -0
- package/dist/src/scripts/ops/add-cover-image-property.d.ts +2 -0
- package/dist/src/scripts/ops/add-cover-image-property.js +27 -0
- package/dist/src/scripts/ops/ncx.d.ts +6 -0
- package/dist/src/scripts/ops/ncx.js +19 -0
- package/dist/src/scripts/ops/update-cover-linear.d.ts +11 -0
- package/dist/src/scripts/ops/update-cover-linear.js +45 -0
- package/dist/src/scripts/ops/update-structure.d.ts +5 -0
- package/dist/src/scripts/ops/update-structure.js +22 -0
- package/dist/src/scripts/ops/update-summary-page.d.ts +2 -0
- package/dist/src/scripts/ops/update-summary-page.js +53 -0
- package/dist/src/scripts/ops/update-toc-with-cover.d.ts +2 -0
- package/dist/src/scripts/ops/update-toc-with-cover.js +140 -0
- package/dist/src/scripts/utils.d.ts +53 -0
- package/dist/src/scripts/utils.js +92 -0
- package/dist/src/scripts/validate-epub.d.ts +12 -0
- package/dist/src/scripts/validate-epub.js +185 -0
- package/dist/src/storage.d.ts +30 -0
- package/dist/src/storage.js +286 -0
- package/dist/src/types.d.ts +51 -0
- package/dist/src/types.js +1 -0
- package/dist/src/utils/author-workflow-config.d.ts +17 -0
- package/dist/src/utils/author-workflow-config.js +95 -0
- package/dist/src/utils/buffered-process.d.ts +8 -0
- package/dist/src/utils/buffered-process.js +36 -0
- package/dist/src/utils/cli-config.d.ts +4 -0
- package/dist/src/utils/cli-config.js +84 -0
- package/dist/src/utils/config.d.ts +49 -0
- package/dist/src/utils/config.js +58 -0
- package/dist/src/utils/diagnostics.d.ts +11 -0
- package/dist/src/utils/diagnostics.js +38 -0
- package/dist/src/utils/epub-doctor.d.ts +78 -0
- package/dist/src/utils/epub-doctor.js +301 -0
- package/dist/src/utils/epub-features.d.ts +20 -0
- package/dist/src/utils/epub-features.js +105 -0
- package/dist/src/utils/epub-integrity.d.ts +25 -0
- package/dist/src/utils/epub-integrity.js +197 -0
- package/dist/src/utils/epub-text.d.ts +13 -0
- package/dist/src/utils/epub-text.js +106 -0
- package/dist/src/utils/epub-utils.d.ts +79 -0
- package/dist/src/utils/epub-utils.js +207 -0
- package/dist/src/utils/epubcheck-report.d.ts +35 -0
- package/dist/src/utils/epubcheck-report.js +68 -0
- package/dist/src/utils/file-size.d.ts +13 -0
- package/dist/src/utils/file-size.js +40 -0
- package/dist/src/utils/files.d.ts +14 -0
- package/dist/src/utils/files.js +39 -0
- package/dist/src/utils/html-report.d.ts +17 -0
- package/dist/src/utils/html-report.js +95 -0
- package/dist/src/utils/i18n.d.ts +5 -0
- package/dist/src/utils/i18n.js +9 -0
- package/dist/src/utils/output-transaction.d.ts +8 -0
- package/dist/src/utils/output-transaction.js +146 -0
- package/dist/src/utils/package-root.d.ts +2 -0
- package/dist/src/utils/package-root.js +15 -0
- package/dist/src/utils/path-safety.d.ts +2 -0
- package/dist/src/utils/path-safety.js +30 -0
- package/dist/src/utils/pipeline-options.d.ts +34 -0
- package/dist/src/utils/pipeline-options.js +49 -0
- package/dist/src/utils/png-animation.d.ts +7 -0
- package/dist/src/utils/png-animation.js +44 -0
- package/dist/src/utils/run-report.d.ts +49 -0
- package/dist/src/utils/run-report.js +71 -0
- package/dist/src/utils/temp-dir.d.ts +11 -0
- package/dist/src/utils/temp-dir.js +80 -0
- package/dist/src/utils/watch-lock.d.ts +8 -0
- package/dist/src/utils/watch-lock.js +100 -0
- package/dist/src/utils/watch-summary.d.ts +13 -0
- package/dist/src/utils/watch-summary.js +130 -0
- package/dist/src/utils/xhtml-structure.d.ts +7 -0
- package/dist/src/utils/xhtml-structure.js +53 -0
- package/dist/src/utils/xhtml.d.ts +27 -0
- package/dist/src/utils/xhtml.js +205 -0
- package/dist/src/utils/xml-dtd.d.ts +6 -0
- package/dist/src/utils/xml-dtd.js +133 -0
- package/dist/src/watch.d.ts +21 -0
- package/dist/src/watch.js +328 -0
- package/package.json +99 -3
- package/scripts/install-epubcheck.sh +44 -0
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { createReadStream } from "node:fs";
|
|
2
|
+
import fs from "fs-extra";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { collectFiles, forEachFileLimited } from "./files.js";
|
|
5
|
+
/** Scan only the XML prolog; never interpret or expand a DTD. */
|
|
6
|
+
function prologScanner() {
|
|
7
|
+
let buffer = "";
|
|
8
|
+
let state = "prolog";
|
|
9
|
+
let quote;
|
|
10
|
+
return (text) => {
|
|
11
|
+
buffer += text;
|
|
12
|
+
for (;;) {
|
|
13
|
+
if (state === "comment" || state === "pi") {
|
|
14
|
+
const terminator = state === "comment" ? "-->" : "?>";
|
|
15
|
+
const end = buffer.indexOf(terminator);
|
|
16
|
+
if (end === -1) {
|
|
17
|
+
// Retain only a possible terminator split across stream chunks.
|
|
18
|
+
buffer = buffer.slice(-(terminator.length - 1));
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
buffer = buffer.slice(end + terminator.length);
|
|
22
|
+
state = "prolog";
|
|
23
|
+
}
|
|
24
|
+
else if (state === "doctype") {
|
|
25
|
+
let ended = false;
|
|
26
|
+
for (let i = 0; i < buffer.length; i++) {
|
|
27
|
+
const char = buffer[i];
|
|
28
|
+
if (quote) {
|
|
29
|
+
if (char === quote)
|
|
30
|
+
quote = undefined;
|
|
31
|
+
}
|
|
32
|
+
else if (char === '"' || char === "'") {
|
|
33
|
+
quote = char;
|
|
34
|
+
}
|
|
35
|
+
else if (char === "[") {
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
else if (char === ">") {
|
|
39
|
+
buffer = buffer.slice(i + 1);
|
|
40
|
+
state = "prolog";
|
|
41
|
+
ended = true;
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (!ended) {
|
|
46
|
+
buffer = "";
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
buffer = buffer.trimStart();
|
|
52
|
+
if (!buffer)
|
|
53
|
+
return undefined;
|
|
54
|
+
if (buffer.startsWith("<!--")) {
|
|
55
|
+
buffer = buffer.slice(4);
|
|
56
|
+
state = "comment";
|
|
57
|
+
}
|
|
58
|
+
else if (buffer.startsWith("<?")) {
|
|
59
|
+
buffer = buffer.slice(2);
|
|
60
|
+
state = "pi";
|
|
61
|
+
}
|
|
62
|
+
else if (/^<!DOCTYPE[\t\n\r ]/i.test(buffer)) {
|
|
63
|
+
buffer = buffer.slice(9);
|
|
64
|
+
state = "doctype";
|
|
65
|
+
}
|
|
66
|
+
else if (["<!--", "<?", "<!DOCTYPE "].some((token) => token.startsWith(buffer.toUpperCase()))) {
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
// A root element or non-XML data ends the prolog. Markup-like
|
|
71
|
+
// strings inside attributes, scripts, styles or CDATA are inert.
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
/** Reuse the prolog scanner for already bounded, decoded XML text. */
|
|
79
|
+
export function hasInternalDtdInText(text) {
|
|
80
|
+
return prologScanner()(text) === true;
|
|
81
|
+
}
|
|
82
|
+
function xmlDecoder(prefix) {
|
|
83
|
+
if ((prefix[0] === 0xfe && prefix[1] === 0xff) || (prefix[0] === 0 && prefix[1] === 0x3c))
|
|
84
|
+
return new TextDecoder("utf-16be");
|
|
85
|
+
if ((prefix[0] === 0xff && prefix[1] === 0xfe) || (prefix[0] === 0x3c && prefix[1] === 0))
|
|
86
|
+
return new TextDecoder("utf-16le");
|
|
87
|
+
return new TextDecoder("utf-8");
|
|
88
|
+
}
|
|
89
|
+
async function hasInternalDtd(file) {
|
|
90
|
+
const scan = prologScanner();
|
|
91
|
+
let decoder;
|
|
92
|
+
let prefix = Buffer.alloc(0);
|
|
93
|
+
for await (const chunk of createReadStream(file, { highWaterMark: 16 * 1024 })) {
|
|
94
|
+
let bytes = chunk;
|
|
95
|
+
if (!decoder) {
|
|
96
|
+
prefix = Buffer.concat([prefix, bytes]);
|
|
97
|
+
if (prefix.length < 4)
|
|
98
|
+
continue;
|
|
99
|
+
decoder = xmlDecoder(prefix);
|
|
100
|
+
bytes = prefix;
|
|
101
|
+
prefix = Buffer.alloc(0);
|
|
102
|
+
}
|
|
103
|
+
const result = scan(decoder.decode(bytes, { stream: true }));
|
|
104
|
+
if (result !== undefined)
|
|
105
|
+
return result;
|
|
106
|
+
}
|
|
107
|
+
return scan(decoder ? decoder.decode() : xmlDecoder(prefix).decode(prefix)) === true;
|
|
108
|
+
}
|
|
109
|
+
/** List unsupported declarations without parsing XML or resolving entities. */
|
|
110
|
+
export async function findInternalDtdSubsets(epubDir) {
|
|
111
|
+
// Inspect regular archive entries regardless of their extension: content
|
|
112
|
+
// documents and even the OPF may have producer-specific filenames.
|
|
113
|
+
const files = await collectFiles(epubDir, () => true);
|
|
114
|
+
const matches = [];
|
|
115
|
+
await forEachFileLimited(files, async (file) => {
|
|
116
|
+
if (!(await fs.lstat(file)).isFile())
|
|
117
|
+
return;
|
|
118
|
+
if (await hasInternalDtd(file)) {
|
|
119
|
+
const relative = path.relative(epubDir, file).split(path.sep).join("/");
|
|
120
|
+
matches.push(relative);
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
return matches.sort();
|
|
124
|
+
}
|
|
125
|
+
/** Reject unsupported DTD semantics before parsing or changing any EPUB resource. */
|
|
126
|
+
export async function assertNoInternalDtdSubsets(epubDir) {
|
|
127
|
+
const files = await findInternalDtdSubsets(epubDir);
|
|
128
|
+
if (files.length > 0) {
|
|
129
|
+
throw new Error(`Internal DTD subsets are not supported: ${JSON.stringify(files[0])}. ` +
|
|
130
|
+
"Optimization was refused to preserve entity-defined content. " +
|
|
131
|
+
"Replace internal declarations with literal text or numeric character references before retrying.");
|
|
132
|
+
}
|
|
133
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Args } from "./types.js";
|
|
2
|
+
/** Structurally compatible with AbortSignal, without requiring DOM or Node typings. */
|
|
3
|
+
export interface WatchSignal {
|
|
4
|
+
readonly aborted: boolean;
|
|
5
|
+
addEventListener(type: "abort", listener: () => void, options?: {
|
|
6
|
+
once?: boolean;
|
|
7
|
+
}): void;
|
|
8
|
+
removeEventListener(type: "abort", listener: () => void): void;
|
|
9
|
+
}
|
|
10
|
+
export interface WatchOptions {
|
|
11
|
+
/** Stop accepting books; finish the current optimization before returning. */
|
|
12
|
+
signal: WatchSignal;
|
|
13
|
+
pollIntervalMs?: number;
|
|
14
|
+
}
|
|
15
|
+
export interface WatchReport {
|
|
16
|
+
succeeded: number;
|
|
17
|
+
failed: number;
|
|
18
|
+
skipped: number;
|
|
19
|
+
}
|
|
20
|
+
/** Polling also works on mounted Docker folders; no dependency on filesystem events. */
|
|
21
|
+
export declare function runWatch(args: Args, options: WatchOptions): Promise<WatchReport>;
|
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
import fs from "fs-extra";
|
|
2
|
+
import { acquireKernelWatchLock, clearRecoverableWatchRecord } from "./utils/watch-lock.js";
|
|
3
|
+
import { refreshWatchSummary, writeWatchFile } from "./utils/watch-summary.js";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import os from "node:os";
|
|
6
|
+
import { constants } from "node:fs";
|
|
7
|
+
import { open } from "node:fs/promises";
|
|
8
|
+
import { pipeline } from "node:stream/promises";
|
|
9
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
10
|
+
import { setTimeout as delay } from "node:timers/promises";
|
|
11
|
+
import { runOptimization } from "./optimization.js";
|
|
12
|
+
import { reportError, reportInfo } from "./utils/diagnostics.js";
|
|
13
|
+
import { assertDistinctPathSet } from "./utils/output-transaction.js";
|
|
14
|
+
import { resolvePngBackground } from "./utils/pipeline-options.js";
|
|
15
|
+
import { loadAuthorWorkflowConfig, } from "./utils/author-workflow-config.js";
|
|
16
|
+
import { findPackageRoot } from "./utils/package-root.js";
|
|
17
|
+
function fingerprint(s) {
|
|
18
|
+
return `${s.dev}:${s.ino}:${s.size}:${s.mtimeMs}:${s.ctimeMs}`;
|
|
19
|
+
}
|
|
20
|
+
async function ownedDirectory(directory, owner) {
|
|
21
|
+
const current = await fs.lstat(directory);
|
|
22
|
+
if (!current.isDirectory() || current.dev !== owner.dev || current.ino !== owner.ino)
|
|
23
|
+
throw new Error(`Watch directory ownership changed: ${directory}`);
|
|
24
|
+
}
|
|
25
|
+
async function removeOwnedLock(file, owner) {
|
|
26
|
+
const current = await fs.lstat(file);
|
|
27
|
+
if (!current.isFile() || current.dev !== owner.dev || current.ino !== owner.ino)
|
|
28
|
+
throw new Error("Refusing to remove a replaced watch lock.");
|
|
29
|
+
await fs.unlink(file);
|
|
30
|
+
}
|
|
31
|
+
async function digest(file) {
|
|
32
|
+
const handle = await open(file, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
|
33
|
+
try {
|
|
34
|
+
if (!(await handle.stat()).isFile())
|
|
35
|
+
throw new Error("Expected a regular file.");
|
|
36
|
+
const hash = createHash("sha256");
|
|
37
|
+
for await (const chunk of handle.createReadStream({ autoClose: false }))
|
|
38
|
+
hash.update(chunk);
|
|
39
|
+
return hash.digest("hex");
|
|
40
|
+
}
|
|
41
|
+
finally {
|
|
42
|
+
await handle.close();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function settingsDigest(args, authorConfig) {
|
|
46
|
+
const ignored = new Set([
|
|
47
|
+
"input",
|
|
48
|
+
"i",
|
|
49
|
+
"output",
|
|
50
|
+
"o",
|
|
51
|
+
"temp",
|
|
52
|
+
"t",
|
|
53
|
+
"batch",
|
|
54
|
+
"watch",
|
|
55
|
+
"outputDir",
|
|
56
|
+
"output-dir",
|
|
57
|
+
"reportJson",
|
|
58
|
+
"report-json",
|
|
59
|
+
"reportHtml",
|
|
60
|
+
"report-html",
|
|
61
|
+
"watchSettleMs",
|
|
62
|
+
"watch-settle-ms",
|
|
63
|
+
"clean",
|
|
64
|
+
"profile",
|
|
65
|
+
"_",
|
|
66
|
+
"$0",
|
|
67
|
+
]);
|
|
68
|
+
const settings = Object.fromEntries(Object.entries(args)
|
|
69
|
+
.filter(([key]) => !ignored.has(key))
|
|
70
|
+
.sort(([a], [b]) => a.localeCompare(b)));
|
|
71
|
+
const version = fs.readJsonSync(path.join(findPackageRoot(import.meta.url), "package.json")).version;
|
|
72
|
+
return createHash("sha256")
|
|
73
|
+
.update(JSON.stringify({ version, settings, authorConfig }))
|
|
74
|
+
.digest("hex")
|
|
75
|
+
.slice(0, 16);
|
|
76
|
+
}
|
|
77
|
+
/** Polling also works on mounted Docker folders; no dependency on filesystem events. */
|
|
78
|
+
export async function runWatch(args, options) {
|
|
79
|
+
resolvePngBackground(args);
|
|
80
|
+
if (!args.watch || !args.outputDir || args.batch || args.doctor || args.inspect)
|
|
81
|
+
throw new Error("Watch requires --watch and --output-dir, without batch or doctor mode.");
|
|
82
|
+
if (args.reportJson || args.reportHtml)
|
|
83
|
+
throw new Error("Watch writes per-book JSON and HTML reports automatically.");
|
|
84
|
+
const interval = options.pollIntervalMs ?? 1000;
|
|
85
|
+
const settle = args.watchSettleMs ?? 3000;
|
|
86
|
+
if (!Number.isSafeInteger(interval) ||
|
|
87
|
+
interval < 1 ||
|
|
88
|
+
!Number.isSafeInteger(settle) ||
|
|
89
|
+
settle < 1)
|
|
90
|
+
throw new Error("Watch polling and settling intervals must be positive integers.");
|
|
91
|
+
const input = path.resolve(args.watch), output = path.resolve(args.outputDir);
|
|
92
|
+
const inputOwner = await fs.lstat(input);
|
|
93
|
+
if (!inputOwner.isDirectory())
|
|
94
|
+
throw new Error("Watch input must be a real directory, not a symbolic link.");
|
|
95
|
+
assertDistinctPathSet([input, output]);
|
|
96
|
+
await fs.ensureDir(output);
|
|
97
|
+
const outputOwner = await fs.lstat(output);
|
|
98
|
+
if (!outputOwner.isDirectory())
|
|
99
|
+
throw new Error("Watch output must be a real directory, not a symbolic link.");
|
|
100
|
+
// Exclusive lock prevents two watchers from publishing the same jobs concurrently.
|
|
101
|
+
const authorConfig = args.authorWorkflow || args.preset === "author"
|
|
102
|
+
? await loadAuthorWorkflowConfig(args.authorConfig)
|
|
103
|
+
: undefined;
|
|
104
|
+
const settings = settingsDigest(args, authorConfig);
|
|
105
|
+
const lockPath = path.join(output, ".epub-watch.lock");
|
|
106
|
+
const kernel = await acquireKernelWatchLock(output);
|
|
107
|
+
let lock;
|
|
108
|
+
try {
|
|
109
|
+
if (kernel)
|
|
110
|
+
await clearRecoverableWatchRecord(lockPath, kernel.identity);
|
|
111
|
+
lock = await open(lockPath, "wx", 0o600);
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
await kernel?.release();
|
|
115
|
+
throw error;
|
|
116
|
+
}
|
|
117
|
+
const lockOwner = await lock.stat();
|
|
118
|
+
const result = { succeeded: 0, failed: 0, skipped: 0 };
|
|
119
|
+
const observations = new Map();
|
|
120
|
+
try {
|
|
121
|
+
await lock.writeFile(JSON.stringify({
|
|
122
|
+
pid: process.pid,
|
|
123
|
+
input,
|
|
124
|
+
method: kernel ? "linux-flock-v1" : "exclusive-file-v1",
|
|
125
|
+
guard: kernel?.identity,
|
|
126
|
+
}));
|
|
127
|
+
await refreshWatchSummary(output);
|
|
128
|
+
reportInfo(`Watching ${input}; output: ${output}. Stop with Ctrl+C.`);
|
|
129
|
+
while (!options.signal.aborted) {
|
|
130
|
+
await ownedDirectory(input, inputOwner);
|
|
131
|
+
await ownedDirectory(output, outputOwner);
|
|
132
|
+
const entries = (await fs.readdir(input, { withFileTypes: true })).sort((a, b) => a.name.localeCompare(b.name));
|
|
133
|
+
const present = new Set();
|
|
134
|
+
for (const entry of entries) {
|
|
135
|
+
if (options.signal.aborted)
|
|
136
|
+
break;
|
|
137
|
+
if (!/\.epub$/i.test(entry.name))
|
|
138
|
+
continue;
|
|
139
|
+
const source = path.join(input, entry.name);
|
|
140
|
+
present.add(entry.name);
|
|
141
|
+
let stats;
|
|
142
|
+
try {
|
|
143
|
+
stats = await fs.lstat(source);
|
|
144
|
+
}
|
|
145
|
+
catch (error) {
|
|
146
|
+
if (error.code === "ENOENT")
|
|
147
|
+
continue;
|
|
148
|
+
throw error;
|
|
149
|
+
}
|
|
150
|
+
const identity = fingerprint(stats);
|
|
151
|
+
const prior = observations.get(entry.name);
|
|
152
|
+
if (!prior || prior.fingerprint !== identity) {
|
|
153
|
+
observations.set(entry.name, {
|
|
154
|
+
fingerprint: identity,
|
|
155
|
+
since: performance.now(),
|
|
156
|
+
attempted: false,
|
|
157
|
+
});
|
|
158
|
+
if (!stats.isFile())
|
|
159
|
+
reportError(`Ignoring non-regular watch input: ${entry.name}`);
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
if (prior.attempted ||
|
|
163
|
+
!stats.isFile() ||
|
|
164
|
+
stats.size === 0 ||
|
|
165
|
+
performance.now() - prior.since < settle)
|
|
166
|
+
continue;
|
|
167
|
+
prior.attempted = true;
|
|
168
|
+
// Copy a stable, no-follow file descriptor into a private snapshot. Never extract the live drop file.
|
|
169
|
+
const snapshotDirectory = await fs.mkdtemp(path.join(os.tmpdir(), "epub-watch-"));
|
|
170
|
+
const snapshotOwner = await fs.lstat(snapshotDirectory);
|
|
171
|
+
const snapshot = path.join(snapshotDirectory, "input.epub");
|
|
172
|
+
try {
|
|
173
|
+
const handle = await open(source, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
|
174
|
+
try {
|
|
175
|
+
if (fingerprint(await handle.stat()) !== identity) {
|
|
176
|
+
observations.delete(entry.name);
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
await pipeline(handle.createReadStream({ autoClose: false }), fs.createWriteStream(snapshot, { flags: "wx", mode: 0o600 }));
|
|
180
|
+
if (fingerprint(await handle.stat()) !== identity ||
|
|
181
|
+
fingerprint(await fs.lstat(source)) !== identity) {
|
|
182
|
+
observations.delete(entry.name);
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
finally {
|
|
187
|
+
await handle.close();
|
|
188
|
+
}
|
|
189
|
+
if (options.signal.aborted)
|
|
190
|
+
break;
|
|
191
|
+
const hash = await digest(snapshot);
|
|
192
|
+
const nameHash = createHash("sha256").update(entry.name).digest("hex").slice(0, 16);
|
|
193
|
+
const stem = path
|
|
194
|
+
.basename(entry.name, path.extname(entry.name))
|
|
195
|
+
.replace(/[^a-zA-Z0-9_-]/g, "_")
|
|
196
|
+
.slice(0, 64) || "book";
|
|
197
|
+
const job = path.join(output, `${stem}-${nameHash}-${hash}-${settings}`);
|
|
198
|
+
await ownedDirectory(output, outputOwner);
|
|
199
|
+
try {
|
|
200
|
+
await fs.mkdir(job);
|
|
201
|
+
}
|
|
202
|
+
catch (error) {
|
|
203
|
+
if (error.code !== "EEXIST")
|
|
204
|
+
throw error;
|
|
205
|
+
}
|
|
206
|
+
const jobOwner = await fs.lstat(job);
|
|
207
|
+
if (!jobOwner.isDirectory())
|
|
208
|
+
throw new Error("Refusing unsafe watch job directory.");
|
|
209
|
+
const target = path.join(job, "optimized.epub"), marker = path.join(job, "completed.json");
|
|
210
|
+
if (await fs.pathExists(marker)) {
|
|
211
|
+
const m = await fs.lstat(marker);
|
|
212
|
+
if (!m.isFile() || m.size > 65536)
|
|
213
|
+
throw new Error("Unsafe watch completion record.");
|
|
214
|
+
const saved = await fs.readJson(marker);
|
|
215
|
+
if (saved.inputSha256 !== hash ||
|
|
216
|
+
saved.settings !== settings ||
|
|
217
|
+
saved.outputSha256 !== (await digest(target)))
|
|
218
|
+
throw new Error("Watch completion record or output no longer matches; preserve it and choose a fresh output directory.");
|
|
219
|
+
result.skipped++;
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
assertDistinctPathSet([
|
|
223
|
+
...entries
|
|
224
|
+
.filter((item) => item.isFile() && /\.epub$/i.test(item.name))
|
|
225
|
+
.map((item) => path.join(input, item.name)),
|
|
226
|
+
snapshot,
|
|
227
|
+
target,
|
|
228
|
+
marker,
|
|
229
|
+
path.join(job, "report.json"),
|
|
230
|
+
path.join(job, "report.html"),
|
|
231
|
+
]);
|
|
232
|
+
const authorConfigPath = authorConfig
|
|
233
|
+
? path.join(snapshotDirectory, "author-config.json")
|
|
234
|
+
: undefined;
|
|
235
|
+
if (authorConfigPath)
|
|
236
|
+
await fs.writeJson(authorConfigPath, authorConfig);
|
|
237
|
+
reportInfo(`Watch book: ${entry.name}`);
|
|
238
|
+
let detail;
|
|
239
|
+
let processingError;
|
|
240
|
+
let processingFailed = false;
|
|
241
|
+
try {
|
|
242
|
+
await runOptimization({
|
|
243
|
+
...args,
|
|
244
|
+
watch: undefined,
|
|
245
|
+
authorConfig: authorConfigPath,
|
|
246
|
+
outputDir: undefined,
|
|
247
|
+
input: snapshot,
|
|
248
|
+
output: target,
|
|
249
|
+
temp: path.join(os.tmpdir(), `epub-watch-extract-${randomUUID()}`),
|
|
250
|
+
reportJson: path.join(job, "report.json"),
|
|
251
|
+
reportHtml: path.join(job, "report.html"),
|
|
252
|
+
}, {
|
|
253
|
+
onReport: (report) => {
|
|
254
|
+
detail = report;
|
|
255
|
+
},
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
catch (error) {
|
|
259
|
+
processingError = error;
|
|
260
|
+
processingFailed = true;
|
|
261
|
+
}
|
|
262
|
+
if (processingFailed) {
|
|
263
|
+
await ownedDirectory(job, jobOwner);
|
|
264
|
+
await writeWatchFile(path.join(job, "watch-status.json"), JSON.stringify({
|
|
265
|
+
name: entry.name,
|
|
266
|
+
success: !processingFailed,
|
|
267
|
+
outputPublished: detail?.steps.some((step) => step.name === "Publish validated EPUB" && step.status === "success") ?? false,
|
|
268
|
+
error: processingFailed
|
|
269
|
+
? String(processingError instanceof Error ? processingError.message : processingError)
|
|
270
|
+
: undefined,
|
|
271
|
+
reductionPercent: detail?.sizes?.reductionPercent,
|
|
272
|
+
}));
|
|
273
|
+
await refreshWatchSummary(output);
|
|
274
|
+
throw processingError;
|
|
275
|
+
}
|
|
276
|
+
await ownedDirectory(output, outputOwner);
|
|
277
|
+
await ownedDirectory(job, jobOwner);
|
|
278
|
+
await fs.writeFile(marker, JSON.stringify({ input: source, inputSha256: hash, settings, outputSha256: await digest(target) }, null, 2), { flag: "wx", mode: 0o600 });
|
|
279
|
+
await ownedDirectory(job, jobOwner);
|
|
280
|
+
await writeWatchFile(path.join(job, "watch-status.json"), JSON.stringify({
|
|
281
|
+
name: entry.name,
|
|
282
|
+
success: !processingFailed,
|
|
283
|
+
outputPublished: detail?.steps.some((step) => step.name === "Publish validated EPUB" && step.status === "success") ?? false,
|
|
284
|
+
error: processingFailed
|
|
285
|
+
? String(processingError instanceof Error ? processingError.message : processingError)
|
|
286
|
+
: undefined,
|
|
287
|
+
reductionPercent: detail?.sizes?.reductionPercent,
|
|
288
|
+
}));
|
|
289
|
+
await refreshWatchSummary(output);
|
|
290
|
+
result.succeeded++;
|
|
291
|
+
reportInfo(`Watch completed: ${entry.name} → ${target}`);
|
|
292
|
+
}
|
|
293
|
+
catch (error) {
|
|
294
|
+
result.failed++;
|
|
295
|
+
reportError(`Watch book failed: ${entry.name}: ${error instanceof Error ? error.message : String(error)}`);
|
|
296
|
+
}
|
|
297
|
+
finally {
|
|
298
|
+
await ownedDirectory(snapshotDirectory, snapshotOwner);
|
|
299
|
+
await fs.remove(snapshotDirectory);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
for (const name of observations.keys())
|
|
303
|
+
if (!present.has(name))
|
|
304
|
+
observations.delete(name);
|
|
305
|
+
if (!options.signal.aborted) {
|
|
306
|
+
try {
|
|
307
|
+
await delay(interval, undefined, { signal: options.signal });
|
|
308
|
+
}
|
|
309
|
+
catch (error) {
|
|
310
|
+
if (!options.signal.aborted)
|
|
311
|
+
throw error;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
reportInfo(`Watch stopped: ${result.succeeded} succeeded, ${result.failed} failed, ${result.skipped} already completed.`);
|
|
316
|
+
return result;
|
|
317
|
+
}
|
|
318
|
+
finally {
|
|
319
|
+
try {
|
|
320
|
+
await lock.close();
|
|
321
|
+
await ownedDirectory(output, outputOwner);
|
|
322
|
+
await removeOwnedLock(lockPath, lockOwner);
|
|
323
|
+
}
|
|
324
|
+
finally {
|
|
325
|
+
await kernel?.release();
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,102 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "epub-optimizer",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "3.16.0",
|
|
4
|
+
"description": "Optimize EPUB files by compressing HTML, CSS, images and recompressing the archive",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/kiki-le-singe/epub-optimizer.git"
|
|
8
|
+
},
|
|
9
|
+
"homepage": "https://github.com/kiki-le-singe/epub-optimizer#readme",
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/kiki-le-singe/epub-optimizer/issues"
|
|
12
|
+
},
|
|
13
|
+
"main": "dist/src/index.js",
|
|
14
|
+
"types": "dist/src/index.d.ts",
|
|
15
|
+
"type": "module",
|
|
16
|
+
"bin": {
|
|
17
|
+
"epub-optimizer": "dist/src/pipeline.js"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist",
|
|
21
|
+
"README.md",
|
|
22
|
+
"LICENSE",
|
|
23
|
+
"scripts/install-epubcheck.sh"
|
|
24
|
+
],
|
|
25
|
+
"author": "kiki-le-singe",
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"cheerio": "^1.2.0",
|
|
29
|
+
"clean-css": "^5.3.3",
|
|
30
|
+
"entities": "^7.0.1",
|
|
31
|
+
"fontverter": "2.0.0",
|
|
32
|
+
"fs-extra": "^11.4.1",
|
|
33
|
+
"html-minifier-terser": "^7.2.0",
|
|
34
|
+
"p-limit": "^7.3.3",
|
|
35
|
+
"sharp": "^0.35.5",
|
|
36
|
+
"subset-font": "2.9.0",
|
|
37
|
+
"svgo": "^4.1.0",
|
|
38
|
+
"terser": "^5.51.2",
|
|
39
|
+
"unzipper": "^0.12.5",
|
|
40
|
+
"yargs": "^18.2.0",
|
|
41
|
+
"yazl": "^3.3.1"
|
|
42
|
+
},
|
|
43
|
+
"engines": {
|
|
44
|
+
"node": ">=22.12.0"
|
|
45
|
+
},
|
|
46
|
+
"keywords": [
|
|
47
|
+
"epub",
|
|
48
|
+
"optimizer",
|
|
49
|
+
"compression",
|
|
50
|
+
"minification",
|
|
51
|
+
"ebook"
|
|
52
|
+
],
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@eslint/js": "^10.0.1",
|
|
55
|
+
"@types/clean-css": "^4.2.11",
|
|
56
|
+
"@types/fs-extra": "^11.0.4",
|
|
57
|
+
"@types/html-minifier-terser": "^7.0.2",
|
|
58
|
+
"@types/node": "^22.20.5",
|
|
59
|
+
"@types/unzipper": "^0.10.11",
|
|
60
|
+
"@types/yargs": "^17.0.35",
|
|
61
|
+
"@types/yazl": "^3.3.1",
|
|
62
|
+
"@typescript-eslint/eslint-plugin": "^8.72.2",
|
|
63
|
+
"@typescript-eslint/parser": "^8.72.2",
|
|
64
|
+
"@vitest/coverage-v8": "^5.1.0",
|
|
65
|
+
"@vitest/ui": "^5.1.0",
|
|
66
|
+
"eslint": "^10.14.0",
|
|
67
|
+
"eslint-config-prettier": "^10.1.8",
|
|
68
|
+
"harfbuzzjs": "1.6.4",
|
|
69
|
+
"prettier": "^3.9.10",
|
|
70
|
+
"typescript": "~6.0.3",
|
|
71
|
+
"typescript-eslint": "^8.72.2",
|
|
72
|
+
"vite": "^8.3.7",
|
|
73
|
+
"vitest": "^5.1.0"
|
|
74
|
+
},
|
|
75
|
+
"scripts": {
|
|
76
|
+
"# Build commands": "# Complete pipeline processes",
|
|
77
|
+
"build": "pnpm run build:prod",
|
|
78
|
+
"build:dev": "tsc",
|
|
79
|
+
"build:prod": "node --experimental-strip-types scripts/clean-path.ts dist && tsc -p tsconfig.prod.json",
|
|
80
|
+
"minify:safe": "node --experimental-strip-types scripts/minify-dist.ts",
|
|
81
|
+
"optimize": "node dist/src/pipeline.js",
|
|
82
|
+
"optimize:author": "node dist/src/pipeline.js --preset author",
|
|
83
|
+
"optimize:repair": "node dist/src/pipeline.js --repair",
|
|
84
|
+
"optimize:lossless": "node dist/src/pipeline.js --preset lossless",
|
|
85
|
+
"optimize:clean": "node dist/src/pipeline.js --clean",
|
|
86
|
+
"cleanup": "node --experimental-strip-types scripts/cleanup-temp.ts",
|
|
87
|
+
"# Dev tools": "# Development utilities",
|
|
88
|
+
"test": "vitest",
|
|
89
|
+
"test:run": "vitest run",
|
|
90
|
+
"test:coverage": "vitest run --coverage",
|
|
91
|
+
"test:e2e": "node --experimental-strip-types scripts/e2e-epubcheck.ts",
|
|
92
|
+
"test:package": "node --experimental-strip-types scripts/package-e2e.ts",
|
|
93
|
+
"test:docker": "node --experimental-strip-types scripts/docker-e2e.ts",
|
|
94
|
+
"release:check": "node --experimental-strip-types scripts/validate-release.ts",
|
|
95
|
+
"release:prepare": "node --experimental-strip-types scripts/prepare-release.ts",
|
|
96
|
+
"lint": "eslint src scripts --ext .ts",
|
|
97
|
+
"lint:fix": "eslint src scripts --ext .ts --fix",
|
|
98
|
+
"format": "prettier --write \"**/*.{ts,json,md}\"",
|
|
99
|
+
"format:check": "prettier --check \"**/*.{ts,json,md}\"",
|
|
100
|
+
"benchmark:batch": "node --experimental-strip-types scripts/benchmark-batch.ts"
|
|
101
|
+
}
|
|
6
102
|
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# Single source of truth for the EPUBCheck version.
|
|
3
|
+
# Used by CI (ci.yml), the Release workflow (release.yml), and the Docker image
|
|
4
|
+
# (Dockerfile). To upgrade EPUBCheck, change EPUBCHECK_VERSION and
|
|
5
|
+
# EPUBCHECK_SHA256 here ONLY (compute the new hash from the official zip).
|
|
6
|
+
set -euo pipefail
|
|
7
|
+
|
|
8
|
+
EPUBCHECK_VERSION="5.4.0"
|
|
9
|
+
# GitHub release assets can be replaced without changing the tag, so the
|
|
10
|
+
# pinned version alone does not freeze the bytes — the checksum does.
|
|
11
|
+
EPUBCHECK_SHA256="33350c61038e71dfb3d45a76aed04bf5481e6d5500cb780f6e98db8bbd15a28c"
|
|
12
|
+
|
|
13
|
+
# Install target directory (default: ./epubcheck for local + CI; the Docker
|
|
14
|
+
# build passes an absolute path).
|
|
15
|
+
TARGET_DIR="${1:-epubcheck}"
|
|
16
|
+
ZIP_URL="https://github.com/w3c/epubcheck/releases/download/v${EPUBCHECK_VERSION}/epubcheck-${EPUBCHECK_VERSION}.zip"
|
|
17
|
+
|
|
18
|
+
tmp="$(mktemp -d)"
|
|
19
|
+
trap 'rm -rf "$tmp"' EXIT
|
|
20
|
+
|
|
21
|
+
# Retry transient download failures (e.g. GitHub CDN 5xx/timeouts).
|
|
22
|
+
if command -v curl >/dev/null 2>&1; then
|
|
23
|
+
curl -fsSL --retry 5 --retry-delay 2 --retry-all-errors "$ZIP_URL" -o "$tmp/epubcheck.zip"
|
|
24
|
+
else
|
|
25
|
+
wget -q --tries=5 --waitretry=3 --retry-connrefused "$ZIP_URL" -O "$tmp/epubcheck.zip"
|
|
26
|
+
fi
|
|
27
|
+
|
|
28
|
+
if command -v sha256sum >/dev/null 2>&1; then
|
|
29
|
+
ACTUAL_SHA256="$(sha256sum "$tmp/epubcheck.zip" | awk '{print $1}')"
|
|
30
|
+
else
|
|
31
|
+
ACTUAL_SHA256="$(shasum -a 256 "$tmp/epubcheck.zip" | awk '{print $1}')"
|
|
32
|
+
fi
|
|
33
|
+
if [ "$ACTUAL_SHA256" != "$EPUBCHECK_SHA256" ]; then
|
|
34
|
+
echo "ERROR: epubcheck-${EPUBCHECK_VERSION}.zip sha256 mismatch" >&2
|
|
35
|
+
echo " expected: $EPUBCHECK_SHA256" >&2
|
|
36
|
+
echo " actual: $ACTUAL_SHA256" >&2
|
|
37
|
+
exit 1
|
|
38
|
+
fi
|
|
39
|
+
|
|
40
|
+
unzip -q "$tmp/epubcheck.zip" -d "$tmp"
|
|
41
|
+
rm -rf "$TARGET_DIR"
|
|
42
|
+
mv "$tmp/epubcheck-${EPUBCHECK_VERSION}" "$TARGET_DIR"
|
|
43
|
+
|
|
44
|
+
echo "EPUBCheck ${EPUBCHECK_VERSION} installed to ${TARGET_DIR}"
|