epub-optimizer 0.0.0-stage → 3.16.1
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,227 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import * as cheerio from "cheerio";
|
|
4
|
+
import { decodeXML } from "entities";
|
|
5
|
+
import { hasInternalDtdInText } from "../utils/xml-dtd.js";
|
|
6
|
+
import { getOPFPath } from "../utils/epub-utils.js";
|
|
7
|
+
import { resolvePathInside } from "../utils/path-safety.js";
|
|
8
|
+
import { MAX_FONT_CHARACTERS } from "./font-limits.js";
|
|
9
|
+
const TEXT_EXTENSIONS = new Set([
|
|
10
|
+
".xhtml",
|
|
11
|
+
".html",
|
|
12
|
+
".htm",
|
|
13
|
+
".svg",
|
|
14
|
+
".xml",
|
|
15
|
+
".opf",
|
|
16
|
+
".ncx",
|
|
17
|
+
".css",
|
|
18
|
+
]);
|
|
19
|
+
const MAX_TEXT_BYTES = 16 * 1024 * 1024;
|
|
20
|
+
/** Remove CSS comments outside strings, then decode CSS escapes for conservative checks. */
|
|
21
|
+
function cssText(text) {
|
|
22
|
+
let output = "";
|
|
23
|
+
let quote = "";
|
|
24
|
+
for (let i = 0; i < text.length; i++) {
|
|
25
|
+
const character = text[i] ?? "";
|
|
26
|
+
if (character === "\\") {
|
|
27
|
+
output += character + (text[++i] ?? "");
|
|
28
|
+
}
|
|
29
|
+
else if (quote) {
|
|
30
|
+
output += character;
|
|
31
|
+
if (character === quote)
|
|
32
|
+
quote = "";
|
|
33
|
+
}
|
|
34
|
+
else if (character === '"' || character === "'") {
|
|
35
|
+
quote = character;
|
|
36
|
+
output += character;
|
|
37
|
+
}
|
|
38
|
+
else if (character === "/" && text[i + 1] === "*") {
|
|
39
|
+
const end = text.indexOf("*/", i + 2);
|
|
40
|
+
if (end < 0)
|
|
41
|
+
throw new Error("Unterminated CSS comment");
|
|
42
|
+
i = end + 1;
|
|
43
|
+
}
|
|
44
|
+
else
|
|
45
|
+
output += character;
|
|
46
|
+
}
|
|
47
|
+
return output
|
|
48
|
+
.replace(/\\(?:\r\n|[\n\r\f])/g, "")
|
|
49
|
+
.replace(/\\([\da-f]{1,6})(?:\r\n|[\t\n\f\r ])?/gi, (_, hex) => {
|
|
50
|
+
const cp = Number.parseInt(hex, 16);
|
|
51
|
+
return cp > 0 && cp <= 0x10ffff ? String.fromCodePoint(cp) : "\ufffd";
|
|
52
|
+
})
|
|
53
|
+
.replace(/\\([^\da-f\n\r\f])/gi, "$1");
|
|
54
|
+
}
|
|
55
|
+
/** Deliberately over-collects literal text; refuses content whose glyphs are not statically known. */
|
|
56
|
+
export async function collectFontCharacters(root) {
|
|
57
|
+
root = path.resolve(root);
|
|
58
|
+
let visited = 0;
|
|
59
|
+
let bytes = 0;
|
|
60
|
+
let documents = 0;
|
|
61
|
+
const characters = new Set();
|
|
62
|
+
const resources = new Map();
|
|
63
|
+
const declared = new Map();
|
|
64
|
+
// Common punctuation, spaces, bullets, Latin diacritics and language-dependent quotation marks.
|
|
65
|
+
for (const [start, end] of [
|
|
66
|
+
[0x20, 0x2ff],
|
|
67
|
+
[0x2000, 0x206f],
|
|
68
|
+
[0x3000, 0x303f],
|
|
69
|
+
])
|
|
70
|
+
for (let cp = start; cp <= end; cp++)
|
|
71
|
+
characters.add(String.fromCodePoint(cp));
|
|
72
|
+
for (const char of "◦▪□●○■")
|
|
73
|
+
characters.add(char);
|
|
74
|
+
function add(text) {
|
|
75
|
+
// Add equivalent forms without rewriting any publication resource. Inline text nodes
|
|
76
|
+
// must be joined before normalization (e.g. <em>e</em> + combining acute).
|
|
77
|
+
for (const variant of [text, text.toUpperCase(), text.toLowerCase()]) {
|
|
78
|
+
for (const normalized of [variant.normalize("NFC"), variant.normalize("NFD")]) {
|
|
79
|
+
for (const character of new Set(normalized)) {
|
|
80
|
+
characters.add(character);
|
|
81
|
+
if (characters.size > MAX_FONT_CHARACTERS)
|
|
82
|
+
throw new Error("Font character limit exceeded");
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
function addStylesheet(source) {
|
|
88
|
+
const css = cssText(source);
|
|
89
|
+
if (/\b(?:counter|counters|attr|var|symbols)\s*\(|@(?:import|counter-style)\b/i.test(css))
|
|
90
|
+
throw new Error("Dynamic or imported CSS text cannot be safely subset");
|
|
91
|
+
for (const match of css.matchAll(/\bcontent\s*:\s*([^;}]+)/gi)) {
|
|
92
|
+
if (!["none", "normal", '""', "''"].includes((match[1] ?? "")
|
|
93
|
+
.replace(/!important\s*$/i, "")
|
|
94
|
+
.trim()
|
|
95
|
+
.toLowerCase()))
|
|
96
|
+
throw new Error("Generated CSS content cannot be safely subset");
|
|
97
|
+
}
|
|
98
|
+
for (const match of css.matchAll(/\btext-transform\s*:\s*([^;}]+)/gi)) {
|
|
99
|
+
if (!/^(?:none|uppercase|lowercase|capitalize)(?:\s*!important)?\s*$/i.test((match[1] ?? "").trim()))
|
|
100
|
+
throw new Error("Unsupported CSS text transformation");
|
|
101
|
+
}
|
|
102
|
+
for (const match of css.matchAll(/\blist-style(?:-type)?\s*:\s*([^;}]+)/gi)) {
|
|
103
|
+
if (!/^(?:none|disc|circle|square|decimal|decimal-leading-zero|lower-latin|upper-latin|lower-alpha|upper-alpha|lower-roman|upper-roman)(?:\s*!important)?\s*$/i.test((match[1] ?? "").trim()))
|
|
104
|
+
throw new Error("Unsupported generated list markers");
|
|
105
|
+
}
|
|
106
|
+
add(source);
|
|
107
|
+
add(css);
|
|
108
|
+
}
|
|
109
|
+
async function walk(dir) {
|
|
110
|
+
for (const entry of await fs.readdir(dir, { withFileTypes: true })) {
|
|
111
|
+
if (++visited > 4096)
|
|
112
|
+
throw new Error("Font resource count limit exceeded");
|
|
113
|
+
const file = path.join(dir, entry.name);
|
|
114
|
+
const stat = await fs.lstat(file);
|
|
115
|
+
if (stat.isSymbolicLink())
|
|
116
|
+
throw new Error("Symlink in font resource tree");
|
|
117
|
+
if (stat.isDirectory()) {
|
|
118
|
+
await walk(file);
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
if (!stat.isFile())
|
|
122
|
+
throw new Error("Non-regular font resource");
|
|
123
|
+
resources.set(file, stat.size);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
if (!(await fs.lstat(root)).isDirectory())
|
|
127
|
+
throw new Error("Invalid font resource root");
|
|
128
|
+
await walk(root);
|
|
129
|
+
const container = path.join(root, "META-INF/container.xml");
|
|
130
|
+
if ((resources.get(container) ?? MAX_TEXT_BYTES) >= MAX_TEXT_BYTES)
|
|
131
|
+
throw new Error("Missing or excessive font package metadata");
|
|
132
|
+
const opfPath = await getOPFPath(root);
|
|
133
|
+
if ((resources.get(opfPath) ?? MAX_TEXT_BYTES) >= MAX_TEXT_BYTES)
|
|
134
|
+
throw new Error("Missing or excessive font package metadata");
|
|
135
|
+
const opf = cheerio.load(new TextDecoder("utf-8", { fatal: true }).decode(await fs.readFile(opfPath)), { xmlMode: true });
|
|
136
|
+
for (const element of opf("[media-type]").toArray()) {
|
|
137
|
+
const media = (opf(element).attr("media-type") ?? "").trim().toLowerCase();
|
|
138
|
+
if (!media.startsWith("text/") &&
|
|
139
|
+
!media.endsWith("+xml") &&
|
|
140
|
+
!["application/xml", "application/javascript"].includes(media))
|
|
141
|
+
continue;
|
|
142
|
+
const href = opf(element).attr("href");
|
|
143
|
+
if (!href)
|
|
144
|
+
throw new Error("Missing text resource href");
|
|
145
|
+
const file = resolvePathInside(root, path.dirname(opfPath), href, "font text resource");
|
|
146
|
+
if (!resources.has(file))
|
|
147
|
+
throw new Error("Missing font text resource");
|
|
148
|
+
declared.set(file, media);
|
|
149
|
+
}
|
|
150
|
+
for (const [file, size] of resources) {
|
|
151
|
+
const extension = path.extname(file).toLowerCase();
|
|
152
|
+
if ([".js", ".mjs", ".cjs"].includes(extension) ||
|
|
153
|
+
/javascript|ecmascript/.test(declared.get(file) ?? ""))
|
|
154
|
+
throw new Error("Scripted text cannot be safely subset");
|
|
155
|
+
// Probe unknown extensions, since XML resources need not use .xhtml or .xml.
|
|
156
|
+
const handle = await fs.open(file, "r");
|
|
157
|
+
let probe;
|
|
158
|
+
try {
|
|
159
|
+
probe = Buffer.alloc(256);
|
|
160
|
+
const read = await handle.read(probe, 0, 256, 0);
|
|
161
|
+
probe = probe.subarray(0, read.bytesRead);
|
|
162
|
+
}
|
|
163
|
+
finally {
|
|
164
|
+
await handle.close();
|
|
165
|
+
}
|
|
166
|
+
if (!TEXT_EXTENSIONS.has(extension) &&
|
|
167
|
+
!declared.has(file) &&
|
|
168
|
+
!/^\s*(?:\uFEFF)?\s*<(?:\?xml|(?:[\w.-]+:)?(?:html|svg)\b)/i.test(probe.toString("utf8")))
|
|
169
|
+
continue;
|
|
170
|
+
bytes += size;
|
|
171
|
+
if (bytes >= MAX_TEXT_BYTES)
|
|
172
|
+
throw new Error("Font text resources exceed 16 MiB");
|
|
173
|
+
const text = new TextDecoder("utf-8", { fatal: true }).decode(await fs.readFile(file));
|
|
174
|
+
if (hasInternalDtdInText(text))
|
|
175
|
+
throw new Error("Internal DTD cannot be safely subset");
|
|
176
|
+
const decoded = decodeXML(text);
|
|
177
|
+
add(decoded);
|
|
178
|
+
if (extension === ".css" || declared.get(file) === "text/css") {
|
|
179
|
+
addStylesheet(text);
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
if (/<\?xml-stylesheet\b/i.test(text))
|
|
183
|
+
throw new Error("External XML stylesheet cannot be safely subset");
|
|
184
|
+
const $ = cheerio.load(text, { xmlMode: true });
|
|
185
|
+
for (const element of $("*").toArray()) {
|
|
186
|
+
if (!("attribs" in element))
|
|
187
|
+
continue;
|
|
188
|
+
const localName = element.name.split(":").pop()?.toLowerCase();
|
|
189
|
+
if ([
|
|
190
|
+
"script",
|
|
191
|
+
"math",
|
|
192
|
+
"object",
|
|
193
|
+
"iframe",
|
|
194
|
+
"embed",
|
|
195
|
+
"tref",
|
|
196
|
+
"altglyph",
|
|
197
|
+
"altglyphdef",
|
|
198
|
+
"glyphref",
|
|
199
|
+
].includes(localName ?? ""))
|
|
200
|
+
throw new Error("Dynamic or mathematical text cannot be safely subset");
|
|
201
|
+
if (Object.keys(element.attribs).some((name) => /^on/i.test(name.split(":").pop() ?? "")))
|
|
202
|
+
throw new Error("Scripted text cannot be safely subset");
|
|
203
|
+
if ((element.attribs.properties ?? "").split(/\s+/).includes("scripted"))
|
|
204
|
+
throw new Error("Scripted text cannot be safely subset");
|
|
205
|
+
if (localName === "link" &&
|
|
206
|
+
(element.attribs.rel ?? "").toLowerCase().split(/\s+/).includes("stylesheet") &&
|
|
207
|
+
/^(?:[a-z][a-z\d+.-]*:|\/\/)/i.test((element.attribs.href ?? "").trim()))
|
|
208
|
+
throw new Error("Remote stylesheet cannot be safely subset");
|
|
209
|
+
if (localName === "use" &&
|
|
210
|
+
!(element.attribs.href ?? element.attribs["xlink:href"] ?? "").trim().startsWith("#"))
|
|
211
|
+
throw new Error("External SVG text cannot be safely subset");
|
|
212
|
+
for (const name of ["text-transform", "list-style-type", "list-style", "content"])
|
|
213
|
+
if (element.attribs[name])
|
|
214
|
+
addStylesheet(`${name}:${element.attribs[name]}`);
|
|
215
|
+
if (localName === "style")
|
|
216
|
+
addStylesheet($(element).text());
|
|
217
|
+
if (element.attribs.style)
|
|
218
|
+
addStylesheet(element.attribs.style);
|
|
219
|
+
if (localName === "html" || localName === "svg")
|
|
220
|
+
documents++;
|
|
221
|
+
}
|
|
222
|
+
add($.root().text());
|
|
223
|
+
}
|
|
224
|
+
if (!documents)
|
|
225
|
+
throw new Error("No readable text documents for font subsetting");
|
|
226
|
+
return [...characters].join("");
|
|
227
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export interface FontSubsetResult {
|
|
2
|
+
data?: Buffer;
|
|
3
|
+
reason?: string;
|
|
4
|
+
}
|
|
5
|
+
/** Runs only on bounded buffers; production callers execute this in a child process. */
|
|
6
|
+
export declare function subsetFontBuffer(font: Buffer, characters: string): Promise<FontSubsetResult>;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
const { MAX_FONT_BYTES, MAX_SFNT_BYTES, MAX_FONT_CHARACTERS } = await import(new URL(import.meta.url.endsWith(".ts") ? "./font-limits.ts" : "./font-limits.js", import.meta.url).href);
|
|
3
|
+
const require = createRequire(import.meta.url);
|
|
4
|
+
const converter = require("fontverter");
|
|
5
|
+
const subset = require("subset-font");
|
|
6
|
+
/** Bounds-check every SFNT directory entry before interpreting font metadata. */
|
|
7
|
+
function tables(data) {
|
|
8
|
+
if (data.length < 12 || !["00010000", "4f54544f"].includes(data.subarray(0, 4).toString("hex")))
|
|
9
|
+
throw new Error("Unsupported or malformed SFNT font");
|
|
10
|
+
const count = data.readUInt16BE(4);
|
|
11
|
+
if (!count || count > 256 || 12 + count * 16 > data.length)
|
|
12
|
+
throw new Error("Invalid font table directory");
|
|
13
|
+
const result = new Map();
|
|
14
|
+
for (let i = 0; i < count; i++) {
|
|
15
|
+
const entry = 12 + i * 16;
|
|
16
|
+
const tag = data.toString("ascii", entry, entry + 4);
|
|
17
|
+
const offset = data.readUInt32BE(entry + 8);
|
|
18
|
+
const length = data.readUInt32BE(entry + 12);
|
|
19
|
+
if (offset < 12 + count * 16 || offset + length > data.length || result.has(tag))
|
|
20
|
+
throw new Error("Invalid font table bounds");
|
|
21
|
+
result.set(tag, data.subarray(offset, offset + length));
|
|
22
|
+
}
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
25
|
+
function names(data) {
|
|
26
|
+
if (!data || data.length < 6)
|
|
27
|
+
throw new Error("Missing font naming table");
|
|
28
|
+
const count = data.readUInt16BE(2);
|
|
29
|
+
const start = data.readUInt16BE(4);
|
|
30
|
+
if (count > 4096 || 6 + count * 12 > data.length || start < 6 + count * 12)
|
|
31
|
+
throw new Error("Invalid font naming table");
|
|
32
|
+
const ids = new Set();
|
|
33
|
+
const notices = new Set();
|
|
34
|
+
for (let i = 0; i < count; i++) {
|
|
35
|
+
const entry = 6 + i * 12;
|
|
36
|
+
const platform = data.readUInt16BE(entry);
|
|
37
|
+
const encoding = data.readUInt16BE(entry + 2);
|
|
38
|
+
const id = data.readUInt16BE(entry + 6);
|
|
39
|
+
const length = data.readUInt16BE(entry + 8);
|
|
40
|
+
const offset = start + data.readUInt16BE(entry + 10);
|
|
41
|
+
if (offset + length > data.length)
|
|
42
|
+
throw new Error("Invalid font name bounds");
|
|
43
|
+
ids.add(id);
|
|
44
|
+
if (![0, 13, 14].includes(id))
|
|
45
|
+
continue;
|
|
46
|
+
if (!(platform === 0 || platform === 3 || (platform === 1 && encoding === 0)))
|
|
47
|
+
throw new Error("Unsupported copyright/license name encoding");
|
|
48
|
+
const decoder = new TextDecoder(platform === 1 ? "macintosh" : "utf-16be", {
|
|
49
|
+
fatal: true,
|
|
50
|
+
});
|
|
51
|
+
notices.add(JSON.stringify([id, decoder.decode(data.subarray(offset, offset + length))]));
|
|
52
|
+
}
|
|
53
|
+
return { ids: [...ids], notices: [...notices].sort() };
|
|
54
|
+
}
|
|
55
|
+
/** Runs only on bounded buffers; production callers execute this in a child process. */
|
|
56
|
+
export async function subsetFontBuffer(font, characters) {
|
|
57
|
+
if (!Buffer.isBuffer(font) || !font.length || font.length > MAX_FONT_BYTES)
|
|
58
|
+
throw new Error("Font exceeds the 8 MiB input limit or is empty");
|
|
59
|
+
if (typeof characters !== "string" ||
|
|
60
|
+
!characters.length ||
|
|
61
|
+
characters.length > MAX_FONT_CHARACTERS * 2)
|
|
62
|
+
throw new Error("Invalid font character set");
|
|
63
|
+
if (font.toString("ascii", 0, 4) === "ttcf")
|
|
64
|
+
return { reason: "font collection" };
|
|
65
|
+
const format = converter.detectFormat(font);
|
|
66
|
+
if (!["sfnt", "woff", "woff2"].includes(format))
|
|
67
|
+
return { reason: "unsupported font container" };
|
|
68
|
+
// WOFF(2) declares its expanded SFNT size before decompression.
|
|
69
|
+
if (format !== "sfnt" && (font.length < 20 || font.readUInt32BE(16) > MAX_SFNT_BYTES))
|
|
70
|
+
throw new Error("Expanded font exceeds the 16 MiB limit");
|
|
71
|
+
const sfnt = await converter.convert(font, "truetype");
|
|
72
|
+
if (sfnt.length > MAX_SFNT_BYTES)
|
|
73
|
+
throw new Error("Expanded font exceeds the 16 MiB limit");
|
|
74
|
+
const original = tables(sfnt);
|
|
75
|
+
const os2 = original.get("OS/2");
|
|
76
|
+
if (!os2 || os2.length < 10)
|
|
77
|
+
throw new Error("Missing font embedding permissions");
|
|
78
|
+
const permissions = os2.readUInt16BE(8);
|
|
79
|
+
if (permissions & (0x0002 | 0x0100 | 0x0200))
|
|
80
|
+
return { reason: "font embedding/subsetting restriction" };
|
|
81
|
+
if (["fvar", "CFF2", "COLR", "CBDT", "sbix", "SVG ", "MATH", "Silf"].some((tag) => original.has(tag)))
|
|
82
|
+
return { reason: "variable, color, mathematical or Graphite font" };
|
|
83
|
+
const metadata = names(original.get("name"));
|
|
84
|
+
const data = await subset(font, characters, {
|
|
85
|
+
targetFormat: format,
|
|
86
|
+
glyphNames: true,
|
|
87
|
+
preserveNameIds: metadata.ids,
|
|
88
|
+
noHinting: false,
|
|
89
|
+
});
|
|
90
|
+
if (!data.length || data.length >= font.length)
|
|
91
|
+
return { reason: "no size benefit" };
|
|
92
|
+
if (converter.detectFormat(data) !== format)
|
|
93
|
+
throw new Error("Font container changed");
|
|
94
|
+
const output = tables(await converter.convert(data, "truetype"));
|
|
95
|
+
const outputOs2 = output.get("OS/2");
|
|
96
|
+
if (!outputOs2 || outputOs2.length < 10 || outputOs2.readUInt16BE(8) !== permissions)
|
|
97
|
+
throw new Error("Font embedding permissions changed");
|
|
98
|
+
if (JSON.stringify(names(output.get("name")).notices) !== JSON.stringify(metadata.notices))
|
|
99
|
+
throw new Error("Font copyright/license text changed");
|
|
100
|
+
return { data };
|
|
101
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { randomUUID } from "node:crypto";
|
|
4
|
+
import { reportInfo, reportWarning } from "../utils/diagnostics.js";
|
|
5
|
+
import { collectManifestResources } from "../utils/epub-utils.js";
|
|
6
|
+
import { collectFontCharacters } from "./font-characters.js";
|
|
7
|
+
import { runFontWorker } from "./font-runner.js";
|
|
8
|
+
import { MAX_FONT_BYTES } from "./font-limits.js";
|
|
9
|
+
const FONT_EXTENSIONS = new Set([".ttf", ".otf", ".woff", ".woff2"]);
|
|
10
|
+
const FONT_MEDIA_TYPES = new Set([
|
|
11
|
+
"application/vnd.ms-opentype",
|
|
12
|
+
"application/font-sfnt",
|
|
13
|
+
"application/font-woff",
|
|
14
|
+
"application/x-font-ttf",
|
|
15
|
+
"application/x-font-opentype",
|
|
16
|
+
"application/x-font-otf",
|
|
17
|
+
"application/x-font-woff",
|
|
18
|
+
"font/ttf",
|
|
19
|
+
"font/otf",
|
|
20
|
+
"font/sfnt",
|
|
21
|
+
"font/woff",
|
|
22
|
+
"font/woff2",
|
|
23
|
+
]);
|
|
24
|
+
class FontCandidateCleanupError extends Error {
|
|
25
|
+
}
|
|
26
|
+
async function removeOwnedCandidate(candidate, owned) {
|
|
27
|
+
try {
|
|
28
|
+
if (!owned)
|
|
29
|
+
throw new Error("Candidate ownership unavailable");
|
|
30
|
+
const stat = await fs.lstat(candidate);
|
|
31
|
+
if (!stat.isFile() || stat.dev !== owned.dev || stat.ino !== owned.ino)
|
|
32
|
+
throw new Error("Candidate ownership changed");
|
|
33
|
+
await fs.unlink(candidate);
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
// An unremoved candidate must never leak into the packaged EPUB, even in non-strict mode.
|
|
37
|
+
throw new FontCandidateCleanupError("Cannot safely clean up font candidate", { cause: error });
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/** Opt-in only. Any uncertainty preserves the original font rather than guessing its glyphs. */
|
|
41
|
+
export async function subsetFonts(epubDir, options = {}) {
|
|
42
|
+
if (!options.enabled) {
|
|
43
|
+
reportInfo("Font subsetting disabled; pass --fonts to enable it.");
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
try {
|
|
47
|
+
// Standalone callers receive the same conservative protection as the main pipeline.
|
|
48
|
+
try {
|
|
49
|
+
await fs.lstat(path.join(epubDir, "META-INF", "encryption.xml"));
|
|
50
|
+
reportInfo("Keeping fonts unchanged: encryption metadata is present.");
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
if (error.code !== "ENOENT")
|
|
55
|
+
throw error;
|
|
56
|
+
}
|
|
57
|
+
const files = await collectManifestResources(epubDir, {
|
|
58
|
+
extensions: FONT_EXTENSIONS,
|
|
59
|
+
mediaTypes: FONT_MEDIA_TYPES,
|
|
60
|
+
});
|
|
61
|
+
if (files.length > 16)
|
|
62
|
+
throw new Error("Font count exceeds the 16-font budget");
|
|
63
|
+
if (!files.length) {
|
|
64
|
+
reportInfo("No embedded fonts to subset.");
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
const characters = await collectFontCharacters(epubDir);
|
|
68
|
+
for (const file of files) {
|
|
69
|
+
let candidate;
|
|
70
|
+
let created = false;
|
|
71
|
+
let owned;
|
|
72
|
+
try {
|
|
73
|
+
const originalStat = await fs.lstat(file);
|
|
74
|
+
if (!originalStat.isFile() || originalStat.nlink !== 1)
|
|
75
|
+
throw new Error("Unsafe font file");
|
|
76
|
+
if (!originalStat.size || originalStat.size > MAX_FONT_BYTES) {
|
|
77
|
+
reportInfo(`Keeping font ${path.basename(file)}: empty or exceeds 8 MiB.`);
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
const original = await fs.readFile(file);
|
|
81
|
+
const result = await runFontWorker(original, characters);
|
|
82
|
+
if (!result.data || result.data.length >= original.length) {
|
|
83
|
+
reportInfo(`Keeping font ${path.basename(file)}: ${result.reason ?? "no size benefit"}.`);
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
candidate = path.join(path.dirname(file), `.font-${randomUUID()}.tmp`);
|
|
87
|
+
const handle = await fs.open(candidate, "wx", 0o600);
|
|
88
|
+
created = true;
|
|
89
|
+
try {
|
|
90
|
+
const stat = await handle.stat();
|
|
91
|
+
owned = { dev: stat.dev, ino: stat.ino };
|
|
92
|
+
await handle.writeFile(result.data);
|
|
93
|
+
}
|
|
94
|
+
finally {
|
|
95
|
+
await handle.close();
|
|
96
|
+
}
|
|
97
|
+
const current = await fs.lstat(file);
|
|
98
|
+
if (!current.isFile() ||
|
|
99
|
+
current.dev !== originalStat.dev ||
|
|
100
|
+
current.ino !== originalStat.ino ||
|
|
101
|
+
!(await fs.readFile(file)).equals(original))
|
|
102
|
+
throw new Error("Font changed during subsetting");
|
|
103
|
+
await fs.rename(candidate, file);
|
|
104
|
+
candidate = undefined;
|
|
105
|
+
reportInfo(`Subset font ${path.basename(file)}: ${original.length} → ${result.data.length} bytes.`);
|
|
106
|
+
}
|
|
107
|
+
catch (error) {
|
|
108
|
+
reportWarning(`Keeping font ${path.basename(file)}: ${error instanceof Error ? error.message : String(error)}`);
|
|
109
|
+
}
|
|
110
|
+
finally {
|
|
111
|
+
if (candidate && created)
|
|
112
|
+
await removeOwnedCandidate(candidate, owned);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
if (error instanceof FontCandidateCleanupError)
|
|
118
|
+
throw error;
|
|
119
|
+
reportWarning(`Font subsetting skipped: ${error instanceof Error ? error.message : String(error)}`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { FontSubsetResult } from "./font-engine.js";
|
|
2
|
+
interface WorkerLimits {
|
|
3
|
+
timeoutMs?: number;
|
|
4
|
+
rssBytes?: number;
|
|
5
|
+
workerUrl?: URL;
|
|
6
|
+
}
|
|
7
|
+
export declare function runFontWorker(font: Buffer, characters: string, limits?: WorkerLimits): Promise<FontSubsetResult>;
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { fork } from "node:child_process";
|
|
2
|
+
import { execFile } from "node:child_process";
|
|
3
|
+
import { promisify } from "node:util";
|
|
4
|
+
import fs from "node:fs/promises";
|
|
5
|
+
import { MAX_FONT_BYTES } from "./font-limits.js";
|
|
6
|
+
const exec = promisify(execFile);
|
|
7
|
+
/** RSS is sampled, not an OS-enforced hard limit; V8 heap has its own cap. */
|
|
8
|
+
async function residentBytes(pid) {
|
|
9
|
+
if (process.platform === "linux") {
|
|
10
|
+
const status = await fs.readFile(`/proc/${pid}/status`, "utf8");
|
|
11
|
+
const match = /^VmRSS:\s+(\d+)\s+kB/m.exec(status);
|
|
12
|
+
if (!match)
|
|
13
|
+
throw new Error("Cannot read font worker RSS");
|
|
14
|
+
return Number(match[1]) * 1024;
|
|
15
|
+
}
|
|
16
|
+
const { stdout } = await exec("/bin/ps", ["-o", "rss=", "-p", String(pid)], {
|
|
17
|
+
timeout: 1000,
|
|
18
|
+
maxBuffer: 1024,
|
|
19
|
+
});
|
|
20
|
+
const value = Number(stdout.trim());
|
|
21
|
+
if (!value || !Number.isFinite(value))
|
|
22
|
+
throw new Error("Cannot read font worker RSS");
|
|
23
|
+
return value * 1024;
|
|
24
|
+
}
|
|
25
|
+
export async function runFontWorker(font, characters, limits = {}) {
|
|
26
|
+
if (!["darwin", "linux"].includes(process.platform))
|
|
27
|
+
throw new Error("Font worker resource monitoring is unavailable on this platform");
|
|
28
|
+
const url = limits.workerUrl ??
|
|
29
|
+
new URL(import.meta.url.endsWith(".ts") ? "./font-worker.ts" : "./font-worker.js", import.meta.url);
|
|
30
|
+
return new Promise((resolve, reject) => {
|
|
31
|
+
const child = fork(url, [], {
|
|
32
|
+
execArgv: ["--experimental-strip-types", "--max-old-space-size=128"],
|
|
33
|
+
env: { NODE_ENV: "production" },
|
|
34
|
+
stdio: ["ignore", "ignore", "ignore", "ipc"],
|
|
35
|
+
serialization: "advanced",
|
|
36
|
+
});
|
|
37
|
+
let result;
|
|
38
|
+
let failure;
|
|
39
|
+
let active = true;
|
|
40
|
+
let checking = false;
|
|
41
|
+
const stop = (error) => {
|
|
42
|
+
failure ??= error;
|
|
43
|
+
child.kill("SIGKILL");
|
|
44
|
+
};
|
|
45
|
+
const timeout = setTimeout(() => stop(new Error("Font worker timed out")), limits.timeoutMs ?? 10_000);
|
|
46
|
+
const monitor = setInterval(() => {
|
|
47
|
+
if (checking || !active || !child.pid)
|
|
48
|
+
return;
|
|
49
|
+
checking = true;
|
|
50
|
+
void residentBytes(child.pid)
|
|
51
|
+
.then((rss) => {
|
|
52
|
+
if (active && rss > (limits.rssBytes ?? 256 * 1024 * 1024))
|
|
53
|
+
stop(new Error("Font worker exceeded the sampled 256 MiB RSS budget"));
|
|
54
|
+
})
|
|
55
|
+
.catch(() => {
|
|
56
|
+
if (active && !result)
|
|
57
|
+
stop(new Error("Font worker memory monitoring failed"));
|
|
58
|
+
})
|
|
59
|
+
.finally(() => {
|
|
60
|
+
checking = false;
|
|
61
|
+
});
|
|
62
|
+
}, 100);
|
|
63
|
+
child.on("message", (message) => {
|
|
64
|
+
if (!message || typeof message !== "object" || result)
|
|
65
|
+
return stop(new Error("Invalid font worker response"));
|
|
66
|
+
const value = message;
|
|
67
|
+
if (typeof value.error === "string")
|
|
68
|
+
return stop(new Error(value.error.slice(0, 240)));
|
|
69
|
+
if (Buffer.isBuffer(value.data) && value.data.length && value.data.length <= MAX_FONT_BYTES)
|
|
70
|
+
result = { data: value.data };
|
|
71
|
+
else if (typeof value.reason === "string")
|
|
72
|
+
result = { reason: value.reason.slice(0, 240) };
|
|
73
|
+
else
|
|
74
|
+
stop(new Error("Invalid font worker response"));
|
|
75
|
+
});
|
|
76
|
+
const finish = (error) => {
|
|
77
|
+
if (!active)
|
|
78
|
+
return;
|
|
79
|
+
active = false;
|
|
80
|
+
clearTimeout(timeout);
|
|
81
|
+
clearInterval(monitor);
|
|
82
|
+
if (error || failure || !result)
|
|
83
|
+
reject(error ?? failure ?? new Error("Font worker exited without a result"));
|
|
84
|
+
else
|
|
85
|
+
resolve(result);
|
|
86
|
+
};
|
|
87
|
+
child.once("error", finish);
|
|
88
|
+
child.once("exit", (code) => finish(code === 0 ? undefined : (failure ?? new Error("Font worker failed"))));
|
|
89
|
+
child.send({ font, characters }, (error) => {
|
|
90
|
+
if (error && active)
|
|
91
|
+
stop(error);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// The worker has no EPUB paths or publication responsibilities. Only font bytes cross IPC.
|
|
2
|
+
process.once("message", async (message) => {
|
|
3
|
+
try {
|
|
4
|
+
const engine = await import(new URL(import.meta.url.endsWith(".ts") ? "./font-engine.ts" : "./font-engine.js", import.meta.url).href);
|
|
5
|
+
const result = await engine.subsetFontBuffer(message.font, message.characters);
|
|
6
|
+
process.send?.(result, () => process.disconnect());
|
|
7
|
+
}
|
|
8
|
+
catch (error) {
|
|
9
|
+
process.send?.({ error: error instanceof Error ? error.message.slice(0, 240) : "Font subsetting failed" }, () => process.disconnect());
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Process HTML and CSS files in a directory recursively.
|
|
3
|
+
* Minifies HTML/XHTML and CSS files in parallel (bounded) to reduce file size.
|
|
4
|
+
* @param dir Directory to process
|
|
5
|
+
* @throws Error if any file fails to minify
|
|
6
|
+
*/
|
|
7
|
+
declare function processHTML(dir: string): Promise<void>;
|
|
8
|
+
/**
|
|
9
|
+
* Minify HTML/XHTML file using cheerio and html-minifier-terser
|
|
10
|
+
* @param filePath Path to HTML/XHTML file
|
|
11
|
+
* @throws Error if minification fails
|
|
12
|
+
*/
|
|
13
|
+
declare function minifyHTML(filePath: string, options?: {
|
|
14
|
+
xhtml?: boolean;
|
|
15
|
+
}): Promise<void>;
|
|
16
|
+
/**
|
|
17
|
+
* Minify CSS file using CleanCSS
|
|
18
|
+
* @param filePath Path to CSS file
|
|
19
|
+
* @throws Error if minification fails
|
|
20
|
+
*/
|
|
21
|
+
declare function minifyCSS(filePath: string): Promise<void>;
|
|
22
|
+
export { processHTML, minifyHTML, minifyCSS };
|