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.
Files changed (132) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1434 -2
  3. package/dist/src/batch.d.ts +34 -0
  4. package/dist/src/batch.js +137 -0
  5. package/dist/src/cli.d.ts +7 -0
  6. package/dist/src/cli.js +275 -0
  7. package/dist/src/index.d.ts +23 -0
  8. package/dist/src/index.js +24 -0
  9. package/dist/src/optimization.d.ts +7 -0
  10. package/dist/src/optimization.js +207 -0
  11. package/dist/src/pipeline.d.ts +2 -0
  12. package/dist/src/pipeline.js +48 -0
  13. package/dist/src/processors/archive-processor.d.ts +26 -0
  14. package/dist/src/processors/archive-processor.js +170 -0
  15. package/dist/src/processors/epub-processor.d.ts +17 -0
  16. package/dist/src/processors/epub-processor.js +133 -0
  17. package/dist/src/processors/font-characters.d.ts +2 -0
  18. package/dist/src/processors/font-characters.js +227 -0
  19. package/dist/src/processors/font-engine.d.ts +6 -0
  20. package/dist/src/processors/font-engine.js +101 -0
  21. package/dist/src/processors/font-limits.d.ts +3 -0
  22. package/dist/src/processors/font-limits.js +3 -0
  23. package/dist/src/processors/font-processor.d.ts +4 -0
  24. package/dist/src/processors/font-processor.js +121 -0
  25. package/dist/src/processors/font-runner.d.ts +8 -0
  26. package/dist/src/processors/font-runner.js +94 -0
  27. package/dist/src/processors/font-worker.d.ts +1 -0
  28. package/dist/src/processors/font-worker.js +12 -0
  29. package/dist/src/processors/html-processor.d.ts +22 -0
  30. package/dist/src/processors/html-processor.js +190 -0
  31. package/dist/src/processors/image-converter.d.ts +14 -0
  32. package/dist/src/processors/image-converter.js +481 -0
  33. package/dist/src/processors/image-processor.d.ts +24 -0
  34. package/dist/src/processors/image-processor.js +165 -0
  35. package/dist/src/processors/js-processor.d.ts +7 -0
  36. package/dist/src/processors/js-processor.js +59 -0
  37. package/dist/src/processors/lazy-img.d.ts +5 -0
  38. package/dist/src/processors/lazy-img.js +48 -0
  39. package/dist/src/processors/svg-optimizer.d.ts +5 -0
  40. package/dist/src/processors/svg-optimizer.js +49 -0
  41. package/dist/src/scripts/create-epub.d.ts +2 -0
  42. package/dist/src/scripts/create-epub.js +29 -0
  43. package/dist/src/scripts/fix/fix-span-tags.d.ts +2 -0
  44. package/dist/src/scripts/fix/fix-span-tags.js +36 -0
  45. package/dist/src/scripts/fix/fix-xml.d.ts +3 -0
  46. package/dist/src/scripts/fix/fix-xml.js +59 -0
  47. package/dist/src/scripts/fix/index.d.ts +5 -0
  48. package/dist/src/scripts/fix/index.js +20 -0
  49. package/dist/src/scripts/fix/remove-empty-styles.d.ts +10 -0
  50. package/dist/src/scripts/fix/remove-empty-styles.js +53 -0
  51. package/dist/src/scripts/fix/xhtml-files.d.ts +2 -0
  52. package/dist/src/scripts/fix/xhtml-files.js +34 -0
  53. package/dist/src/scripts/ops/add-chapter-sections-to-toc.d.ts +5 -0
  54. package/dist/src/scripts/ops/add-chapter-sections-to-toc.js +242 -0
  55. package/dist/src/scripts/ops/add-cover-image-property.d.ts +2 -0
  56. package/dist/src/scripts/ops/add-cover-image-property.js +27 -0
  57. package/dist/src/scripts/ops/ncx.d.ts +6 -0
  58. package/dist/src/scripts/ops/ncx.js +19 -0
  59. package/dist/src/scripts/ops/update-cover-linear.d.ts +11 -0
  60. package/dist/src/scripts/ops/update-cover-linear.js +45 -0
  61. package/dist/src/scripts/ops/update-structure.d.ts +5 -0
  62. package/dist/src/scripts/ops/update-structure.js +22 -0
  63. package/dist/src/scripts/ops/update-summary-page.d.ts +2 -0
  64. package/dist/src/scripts/ops/update-summary-page.js +53 -0
  65. package/dist/src/scripts/ops/update-toc-with-cover.d.ts +2 -0
  66. package/dist/src/scripts/ops/update-toc-with-cover.js +140 -0
  67. package/dist/src/scripts/utils.d.ts +53 -0
  68. package/dist/src/scripts/utils.js +92 -0
  69. package/dist/src/scripts/validate-epub.d.ts +12 -0
  70. package/dist/src/scripts/validate-epub.js +185 -0
  71. package/dist/src/storage.d.ts +30 -0
  72. package/dist/src/storage.js +286 -0
  73. package/dist/src/types.d.ts +51 -0
  74. package/dist/src/types.js +1 -0
  75. package/dist/src/utils/author-workflow-config.d.ts +17 -0
  76. package/dist/src/utils/author-workflow-config.js +95 -0
  77. package/dist/src/utils/buffered-process.d.ts +8 -0
  78. package/dist/src/utils/buffered-process.js +36 -0
  79. package/dist/src/utils/cli-config.d.ts +4 -0
  80. package/dist/src/utils/cli-config.js +84 -0
  81. package/dist/src/utils/config.d.ts +49 -0
  82. package/dist/src/utils/config.js +58 -0
  83. package/dist/src/utils/diagnostics.d.ts +11 -0
  84. package/dist/src/utils/diagnostics.js +38 -0
  85. package/dist/src/utils/epub-doctor.d.ts +78 -0
  86. package/dist/src/utils/epub-doctor.js +301 -0
  87. package/dist/src/utils/epub-features.d.ts +20 -0
  88. package/dist/src/utils/epub-features.js +105 -0
  89. package/dist/src/utils/epub-integrity.d.ts +25 -0
  90. package/dist/src/utils/epub-integrity.js +197 -0
  91. package/dist/src/utils/epub-text.d.ts +13 -0
  92. package/dist/src/utils/epub-text.js +106 -0
  93. package/dist/src/utils/epub-utils.d.ts +79 -0
  94. package/dist/src/utils/epub-utils.js +207 -0
  95. package/dist/src/utils/epubcheck-report.d.ts +35 -0
  96. package/dist/src/utils/epubcheck-report.js +68 -0
  97. package/dist/src/utils/file-size.d.ts +13 -0
  98. package/dist/src/utils/file-size.js +40 -0
  99. package/dist/src/utils/files.d.ts +14 -0
  100. package/dist/src/utils/files.js +39 -0
  101. package/dist/src/utils/html-report.d.ts +17 -0
  102. package/dist/src/utils/html-report.js +95 -0
  103. package/dist/src/utils/i18n.d.ts +5 -0
  104. package/dist/src/utils/i18n.js +9 -0
  105. package/dist/src/utils/output-transaction.d.ts +8 -0
  106. package/dist/src/utils/output-transaction.js +146 -0
  107. package/dist/src/utils/package-root.d.ts +2 -0
  108. package/dist/src/utils/package-root.js +15 -0
  109. package/dist/src/utils/path-safety.d.ts +2 -0
  110. package/dist/src/utils/path-safety.js +30 -0
  111. package/dist/src/utils/pipeline-options.d.ts +34 -0
  112. package/dist/src/utils/pipeline-options.js +49 -0
  113. package/dist/src/utils/png-animation.d.ts +7 -0
  114. package/dist/src/utils/png-animation.js +44 -0
  115. package/dist/src/utils/run-report.d.ts +49 -0
  116. package/dist/src/utils/run-report.js +71 -0
  117. package/dist/src/utils/temp-dir.d.ts +11 -0
  118. package/dist/src/utils/temp-dir.js +80 -0
  119. package/dist/src/utils/watch-lock.d.ts +8 -0
  120. package/dist/src/utils/watch-lock.js +100 -0
  121. package/dist/src/utils/watch-summary.d.ts +13 -0
  122. package/dist/src/utils/watch-summary.js +130 -0
  123. package/dist/src/utils/xhtml-structure.d.ts +7 -0
  124. package/dist/src/utils/xhtml-structure.js +53 -0
  125. package/dist/src/utils/xhtml.d.ts +27 -0
  126. package/dist/src/utils/xhtml.js +205 -0
  127. package/dist/src/utils/xml-dtd.d.ts +6 -0
  128. package/dist/src/utils/xml-dtd.js +133 -0
  129. package/dist/src/watch.d.ts +21 -0
  130. package/dist/src/watch.js +328 -0
  131. package/package.json +99 -3
  132. package/scripts/install-epubcheck.sh +44 -0
@@ -0,0 +1,95 @@
1
+ import fs from "fs-extra";
2
+ import path from "node:path";
3
+ import { resolvePathInside } from "./path-safety.js";
4
+ export const DEFAULT_AUTHOR_WORKFLOW_CONFIG = Object.freeze({
5
+ summaryHref: "chapter-2.xhtml",
6
+ coverHref: "cover.xhtml",
7
+ coverSpineId: "cover",
8
+ chapterClasses: Object.freeze(["p6", "p8"]),
9
+ sectionClasses: Object.freeze(["p7"]),
10
+ summaryEntryClass: "p6",
11
+ coverNavClass: "s3",
12
+ sectionNavClass: "s4",
13
+ });
14
+ const CONFIG_KEYS = new Set(Object.keys(DEFAULT_AUTHOR_WORKFLOW_CONFIG));
15
+ const CLASS_NAME_PATTERN = /^[A-Za-z_][A-Za-z0-9_-]*$/;
16
+ const XML_ID_PATTERN = /^[A-Za-z_][A-Za-z0-9_.-]*$/;
17
+ function validateHref(value, name) {
18
+ if (typeof value !== "string" || value.trim() === "" || /[?#]/.test(value)) {
19
+ throw new Error(`${name} must be a non-empty local href without a query or fragment.`);
20
+ }
21
+ resolvePathInside("/epub-author-config", "/epub-author-config", value, name);
22
+ return value;
23
+ }
24
+ function validateClassName(value, name) {
25
+ if (typeof value !== "string" || !CLASS_NAME_PATTERN.test(value)) {
26
+ throw new Error(`${name} must be a single valid CSS class name.`);
27
+ }
28
+ return value;
29
+ }
30
+ function validateClassList(value, name) {
31
+ if (!Array.isArray(value) || value.length === 0) {
32
+ throw new Error(`${name} must contain at least one CSS class name.`);
33
+ }
34
+ return value.map((entry, index) => validateClassName(entry, `${name}[${index}]`));
35
+ }
36
+ export function resolveAuthorWorkflowConfig(overrides = {}) {
37
+ for (const key of Object.keys(overrides)) {
38
+ if (!CONFIG_KEYS.has(key)) {
39
+ throw new Error(`Unknown author workflow configuration key: ${key}`);
40
+ }
41
+ }
42
+ const merged = { ...DEFAULT_AUTHOR_WORKFLOW_CONFIG, ...overrides };
43
+ if (typeof merged.coverSpineId !== "string" || !XML_ID_PATTERN.test(merged.coverSpineId)) {
44
+ throw new Error("coverSpineId must be a valid XML identifier.");
45
+ }
46
+ return {
47
+ summaryHref: validateHref(merged.summaryHref, "summaryHref"),
48
+ coverHref: validateHref(merged.coverHref, "coverHref"),
49
+ coverSpineId: merged.coverSpineId,
50
+ chapterClasses: validateClassList(merged.chapterClasses, "chapterClasses"),
51
+ sectionClasses: validateClassList(merged.sectionClasses, "sectionClasses"),
52
+ summaryEntryClass: validateClassName(merged.summaryEntryClass, "summaryEntryClass"),
53
+ coverNavClass: validateClassName(merged.coverNavClass, "coverNavClass"),
54
+ sectionNavClass: validateClassName(merged.sectionNavClass, "sectionNavClass"),
55
+ };
56
+ }
57
+ export async function loadAuthorWorkflowConfig(configPath) {
58
+ if (!configPath) {
59
+ return resolveAuthorWorkflowConfig();
60
+ }
61
+ const absolutePath = path.resolve(configPath);
62
+ let parsed;
63
+ try {
64
+ parsed = await fs.readJson(absolutePath);
65
+ }
66
+ catch (error) {
67
+ throw new Error(`Failed to read author workflow configuration ${absolutePath}: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
68
+ }
69
+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
70
+ throw new Error("Author workflow configuration must be a JSON object.");
71
+ }
72
+ return resolveAuthorWorkflowConfig(parsed);
73
+ }
74
+ export function resolveAuthorWorkflowHref(contentDir, href, label) {
75
+ return resolvePathInside(contentDir, contentDir, href, label);
76
+ }
77
+ export function createOwnerRelativeHref(ownerFile, targetFile) {
78
+ return encodeURI(path.relative(path.dirname(ownerFile), targetFile).split(path.sep).join("/"));
79
+ }
80
+ export function referenceTargetsFile(epubDir, ownerFile, reference, targetFile) {
81
+ if (!reference)
82
+ return false;
83
+ try {
84
+ return resolvePathInside(epubDir, path.dirname(ownerFile), reference) === targetFile;
85
+ }
86
+ catch {
87
+ return false;
88
+ }
89
+ }
90
+ export function hasAnyClass(classValue, classes) {
91
+ if (!classValue)
92
+ return false;
93
+ const tokens = new Set(classValue.split(/\s+/).filter(Boolean));
94
+ return classes.some((className) => tokens.has(className));
95
+ }
@@ -0,0 +1,8 @@
1
+ export interface BufferedResult {
2
+ status: number | null;
3
+ stdout: string;
4
+ stderr: string;
5
+ error?: Error;
6
+ }
7
+ /** Bounded async capture; timeout and output overflow kill the child before completion. */
8
+ export declare function runBufferedProcess(command: string, args: string[], timeoutMs: number, maxBytes?: number): Promise<BufferedResult>;
@@ -0,0 +1,36 @@
1
+ import { spawn } from "node:child_process";
2
+ /** Bounded async capture; timeout and output overflow kill the child before completion. */
3
+ export function runBufferedProcess(command, args, timeoutMs, maxBytes = 16 * 1024 * 1024) {
4
+ return new Promise((resolve) => {
5
+ const child = spawn(command, args, { stdio: ["ignore", "pipe", "pipe"] });
6
+ const stdout = [], stderr = [];
7
+ let size = 0, error;
8
+ const stop = (message, code) => {
9
+ error ??= Object.assign(new Error(message), { code });
10
+ child.kill("SIGKILL");
11
+ };
12
+ const timer = setTimeout(() => stop("Process timed out", "ETIMEDOUT"), timeoutMs);
13
+ const capture = (chunks) => (chunk) => {
14
+ const available = Math.max(0, maxBytes - size);
15
+ if (available)
16
+ chunks.push(chunk.subarray(0, available));
17
+ size += chunk.length;
18
+ if (size > maxBytes)
19
+ stop("Process output exceeded its buffer limit", "ENOBUFS");
20
+ };
21
+ child.stdout.on("data", capture(stdout));
22
+ child.stderr.on("data", capture(stderr));
23
+ child.once("error", (failure) => {
24
+ error ??= failure;
25
+ });
26
+ child.once("close", (status) => {
27
+ clearTimeout(timer);
28
+ resolve({
29
+ status,
30
+ stdout: Buffer.concat(stdout).toString("utf8"),
31
+ stderr: Buffer.concat(stderr).toString("utf8"),
32
+ error,
33
+ });
34
+ });
35
+ });
36
+ }
@@ -0,0 +1,4 @@
1
+ /** JSON only: no executable configuration, includes, environment interpolation or purge actions. */
2
+ export declare function readCliConfig(file: string): Record<string, unknown>;
3
+ /** A settings file is an input too: generated output must never replace it. */
4
+ export declare function protectCliConfig(file: string, targets: Array<string | undefined>, outputDirectory?: string): void;
@@ -0,0 +1,84 @@
1
+ import { assertDistinctInputOutput } from "./output-transaction.js";
2
+ import fs from "node:fs";
3
+ import path from "node:path";
4
+ const paths = new Set([
5
+ "input",
6
+ "output",
7
+ "temp",
8
+ "batch",
9
+ "watch",
10
+ "output-dir",
11
+ "report-json",
12
+ "report-html",
13
+ "author-config",
14
+ ]);
15
+ const strings = new Set([...paths, "preset", "lang", "png-background"]);
16
+ const numbers = new Set([
17
+ "jpg-quality",
18
+ "png-quality",
19
+ "max-image-dim",
20
+ "image-concurrency",
21
+ "validation-timeout",
22
+ "watch-settle-ms",
23
+ "jobs",
24
+ ]);
25
+ const booleans = new Set([
26
+ "fonts",
27
+ "repair",
28
+ "author-workflow",
29
+ "convert-png",
30
+ "lazy-loading",
31
+ "strict",
32
+ "profile",
33
+ "clean",
34
+ "doctor",
35
+ ]);
36
+ /** JSON only: no executable configuration, includes, environment interpolation or purge actions. */
37
+ export function readCliConfig(file) {
38
+ const handle = fs.openSync(file, fs.constants.O_RDONLY | fs.constants.O_NOFOLLOW | fs.constants.O_NONBLOCK);
39
+ try {
40
+ const stat = fs.fstatSync(handle);
41
+ if (!stat.isFile() || stat.size > 65536)
42
+ throw new Error("Configuration must be a regular JSON file of at most 64 KiB.");
43
+ const bytes = Buffer.alloc(65537);
44
+ let count = 0;
45
+ while (count < bytes.length) {
46
+ const size = fs.readSync(handle, bytes, count, bytes.length - count, count);
47
+ if (!size)
48
+ break;
49
+ count += size;
50
+ }
51
+ if (count > 65536)
52
+ throw new Error("Configuration exceeds 64 KiB.");
53
+ const value = JSON.parse(bytes.subarray(0, count).toString("utf8"));
54
+ if (!value || typeof value !== "object" || Array.isArray(value))
55
+ throw new Error("Configuration must be a JSON object.");
56
+ const result = {};
57
+ for (const [key, item] of Object.entries(value)) {
58
+ if (!strings.has(key) && !numbers.has(key) && !booleans.has(key))
59
+ throw new Error(`Unsupported configuration key: ${key}`);
60
+ if ((strings.has(key) && (typeof item !== "string" || !item.trim())) ||
61
+ (numbers.has(key) && (typeof item !== "number" || !Number.isFinite(item))) ||
62
+ (booleans.has(key) && typeof item !== "boolean"))
63
+ throw new Error(`Invalid configuration value: ${key}`);
64
+ result[key] = paths.has(key)
65
+ ? path.resolve(path.dirname(path.resolve(file)), item)
66
+ : item;
67
+ }
68
+ return result;
69
+ }
70
+ finally {
71
+ fs.closeSync(handle);
72
+ }
73
+ }
74
+ /** A settings file is an input too: generated output must never replace it. */
75
+ export function protectCliConfig(file, targets, outputDirectory) {
76
+ for (const target of targets)
77
+ if (target)
78
+ assertDistinctInputOutput(file, target);
79
+ if (outputDirectory && fs.existsSync(outputDirectory)) {
80
+ const relative = path.relative(fs.realpathSync(outputDirectory), fs.realpathSync(file));
81
+ if (relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative)))
82
+ throw new Error("Keep --config outside the batch/watch output directory.");
83
+ }
84
+ }
@@ -0,0 +1,49 @@
1
+ declare const config: {
2
+ inputEPUB: string;
3
+ outputEPUB: string;
4
+ tempDir: string;
5
+ lang: string;
6
+ defaultLang: string;
7
+ htmlOptions: {
8
+ collapseWhitespace: boolean;
9
+ removeComments: boolean;
10
+ minifyCSS: {
11
+ inline: boolean;
12
+ };
13
+ minifyJS: boolean;
14
+ caseSensitive: boolean;
15
+ keepClosingSlash: boolean;
16
+ minifyURLs: boolean;
17
+ };
18
+ cssOptions: {
19
+ level: number;
20
+ };
21
+ jpegOptions: {
22
+ quality: number;
23
+ };
24
+ pngOptions: {
25
+ quality: number;
26
+ };
27
+ archiveOptions: {
28
+ zlib: {
29
+ level: number;
30
+ };
31
+ };
32
+ archiveLimits: {
33
+ maxEntries: number;
34
+ maxEntryBytes: number;
35
+ maxTotalBytes: number;
36
+ maxCompressionRatio: number;
37
+ };
38
+ epubcheckPath: string;
39
+ epubcheckTimeoutMs: number;
40
+ labels: {
41
+ fr: {
42
+ cover: string;
43
+ };
44
+ en: {
45
+ cover: string;
46
+ };
47
+ };
48
+ };
49
+ export default config;
@@ -0,0 +1,58 @@
1
+ import path from "node:path";
2
+ import { findPackageRoot } from "./package-root.js";
3
+ const config = {
4
+ // Default file paths
5
+ inputEPUB: "mybook.epub",
6
+ outputEPUB: "mybook_opt.epub",
7
+ tempDir: "temp_epub",
8
+ // Language settings
9
+ lang: "fr", // Current language (fr = French, en = English)
10
+ defaultLang: "en",
11
+ // HTML optimization options
12
+ htmlOptions: {
13
+ // CSS may make whitespace significant, including in external stylesheets.
14
+ collapseWhitespace: false,
15
+ removeComments: true,
16
+ // Keep @import rules as written instead of inlining them from the working directory.
17
+ minifyCSS: { inline: false },
18
+ minifyJS: true,
19
+ caseSensitive: true, // Preserve XML case sensitivity
20
+ keepClosingSlash: true, // Preserve self-closing tags like <img /> and <span />
21
+ minifyURLs: false, // Don't modify URLs in XML
22
+ },
23
+ // CSS optimization options
24
+ cssOptions: {
25
+ level: 2,
26
+ },
27
+ // Image optimization options
28
+ jpegOptions: {
29
+ quality: 70,
30
+ },
31
+ pngOptions: {
32
+ quality: 0.6,
33
+ },
34
+ // Archive options
35
+ archiveOptions: {
36
+ zlib: { level: 9 },
37
+ },
38
+ archiveLimits: {
39
+ maxEntries: 20_000,
40
+ maxEntryBytes: 512 * 1024 * 1024,
41
+ maxTotalBytes: 2 * 1024 * 1024 * 1024,
42
+ maxCompressionRatio: 1_000,
43
+ },
44
+ // EPUBCheck path
45
+ epubcheckPath: process.env.EPUBCHECK_PATH ??
46
+ path.join(findPackageRoot(import.meta.url), "epubcheck", "epubcheck.jar"),
47
+ epubcheckTimeoutMs: 120_000,
48
+ // UI Localization
49
+ labels: {
50
+ fr: {
51
+ cover: "Couverture",
52
+ },
53
+ en: {
54
+ cover: "Cover",
55
+ },
56
+ },
57
+ };
58
+ export default config;
@@ -0,0 +1,11 @@
1
+ export interface Diagnostic {
2
+ readonly severity: "warning" | "error";
3
+ readonly message: string;
4
+ }
5
+ /** Collect explicit diagnostics in this async operation, without changing console. */
6
+ export declare function collectDiagnostics<T>(operation: () => Promise<T> | T, consume: (diagnostic: Diagnostic) => void): Promise<T>;
7
+ export declare function reportInfo(...args: unknown[]): void;
8
+ export declare function reportWarning(...args: unknown[]): void;
9
+ /** Preserve the stdout channel of existing warning-labelled CLI messages. */
10
+ export declare function reportWarningToStdout(...args: unknown[]): void;
11
+ export declare function reportError(...args: unknown[]): void;
@@ -0,0 +1,38 @@
1
+ import { AsyncLocalStorage } from "node:async_hooks";
2
+ const scopes = new AsyncLocalStorage();
3
+ /** Collect explicit diagnostics in this async operation, without changing console. */
4
+ export async function collectDiagnostics(operation, consume) {
5
+ const scope = { active: true, consume, parent: scopes.getStore() };
6
+ try {
7
+ return await scopes.run(scope, operation);
8
+ }
9
+ finally {
10
+ // Detached callbacks may outlive the step; completed reports stay immutable.
11
+ scope.active = false;
12
+ }
13
+ }
14
+ function emit(severity, args) {
15
+ const diagnostic = Object.freeze({ severity, message: args.map(String).join(" ") });
16
+ // Nested steps also belong to their enclosing operation; concurrent operations
17
+ // have separate async-local scope chains and cannot collect each other's logs.
18
+ for (let scope = scopes.getStore(); scope; scope = scope.parent) {
19
+ if (scope.active)
20
+ scope.consume(diagnostic);
21
+ }
22
+ }
23
+ export function reportInfo(...args) {
24
+ console.log(...args);
25
+ }
26
+ export function reportWarning(...args) {
27
+ emit("warning", args);
28
+ console.warn(...args);
29
+ }
30
+ /** Preserve the stdout channel of existing warning-labelled CLI messages. */
31
+ export function reportWarningToStdout(...args) {
32
+ emit("warning", args);
33
+ console.log(...args);
34
+ }
35
+ export function reportError(...args) {
36
+ emit("error", args);
37
+ console.error(...args);
38
+ }
@@ -0,0 +1,78 @@
1
+ import { type EpubContentMetrics } from "./epub-integrity.js";
2
+ export interface DoctorImage {
3
+ href: string;
4
+ mediaType: string;
5
+ bytes: number;
6
+ width?: number;
7
+ height?: number;
8
+ hasAlpha?: boolean;
9
+ exists: boolean;
10
+ }
11
+ export interface DoctorRisk {
12
+ level: "info" | "warning" | "error";
13
+ code: string;
14
+ message: string;
15
+ }
16
+ interface DoctorReportBase {
17
+ input: string;
18
+ archiveBytes: number;
19
+ optimization: {
20
+ blocked: boolean;
21
+ blockers: DoctorRisk[];
22
+ };
23
+ risks: DoctorRisk[];
24
+ }
25
+ export interface BlockedDoctorReport extends DoctorReportBase {
26
+ analysisComplete: false;
27
+ }
28
+ export interface EpubDoctorReport extends DoctorReportBase {
29
+ analysisComplete: true;
30
+ opf: {
31
+ path: string;
32
+ version?: string;
33
+ manifestItems: number;
34
+ spineItems: number;
35
+ contentDirectory: string;
36
+ };
37
+ navigation: {
38
+ hasEpub3Nav: boolean;
39
+ hasNcx: boolean;
40
+ entries: number;
41
+ };
42
+ references: {
43
+ internal: number;
44
+ missing: number;
45
+ missingTargets: string[];
46
+ };
47
+ images: {
48
+ count: number;
49
+ totalBytes: number;
50
+ compressedBytes: number | null;
51
+ archivePercent: number | null;
52
+ byMediaType: Record<string, {
53
+ count: number;
54
+ bytes: number;
55
+ }>;
56
+ largest: DoctorImage[];
57
+ duplicateBasenames: Array<{
58
+ basename: string;
59
+ hrefs: string[];
60
+ }>;
61
+ largeRasterImages: DoctorImage[];
62
+ opaquePngCandidates: DoctorImage[];
63
+ };
64
+ content: EpubContentMetrics;
65
+ risks: DoctorRisk[];
66
+ }
67
+ export type DoctorReport = EpubDoctorReport | BlockedDoctorReport;
68
+ export declare function inspectExtractedEpub(epubDir: string, options: {
69
+ input: string;
70
+ archiveBytes: number;
71
+ archiveEntries?: ReadonlyMap<string, number>;
72
+ }): Promise<DoctorReport>;
73
+ export declare function printDoctorReport(report: DoctorReport): void;
74
+ export declare function runDoctor(options: {
75
+ input: string;
76
+ reportJson?: string;
77
+ }): Promise<DoctorReport>;
78
+ export {};