@storyteller-platform/epub 0.7.1 → 0.8.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.
@@ -1,240 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
7
- var __typeError = (msg) => {
8
- throw TypeError(msg);
9
- };
10
- var __export = (target, all) => {
11
- for (var name in all)
12
- __defProp(target, name, { get: all[name], enumerable: true });
13
- };
14
- var __copyProps = (to, from, except, desc) => {
15
- if (from && typeof from === "object" || typeof from === "function") {
16
- for (let key of __getOwnPropNames(from))
17
- if (!__hasOwnProp.call(to, key) && key !== except)
18
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
19
- }
20
- return to;
21
- };
22
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
23
- var __using = (stack, value, async) => {
24
- if (value != null) {
25
- if (typeof value !== "object" && typeof value !== "function") __typeError("Object expected");
26
- var dispose, inner;
27
- if (async) dispose = value[__knownSymbol("asyncDispose")];
28
- if (dispose === void 0) {
29
- dispose = value[__knownSymbol("dispose")];
30
- if (async) inner = dispose;
31
- }
32
- if (typeof dispose !== "function") __typeError("Object not disposable");
33
- if (inner) dispose = function() {
34
- try {
35
- inner.call(this);
36
- } catch (e) {
37
- return Promise.reject(e);
38
- }
39
- };
40
- stack.push([async, dispose, value]);
41
- } else if (async) {
42
- stack.push([async]);
43
- }
44
- return value;
45
- };
46
- var __callDispose = (stack, error, hasError) => {
47
- var E = typeof SuppressedError === "function" ? SuppressedError : function(e, s, m, _) {
48
- return _ = Error(m), _.name = "SuppressedError", _.error = e, _.suppressed = s, _;
49
- };
50
- var fail = (e) => error = hasError ? new E(e, error, "An error was suppressed during disposal") : (hasError = true, e);
51
- var next = (it) => {
52
- while (it = stack.pop()) {
53
- try {
54
- var result = it[1] && it[1].call(it[2]);
55
- if (it[0]) return Promise.resolve(result).then(next, (e) => (fail(e), next()));
56
- } catch (e) {
57
- fail(e);
58
- }
59
- }
60
- if (hasError) throw error;
61
- };
62
- return next();
63
- };
64
- var tmpfs_exports = {};
65
- __export(tmpfs_exports, {
66
- TmpFsAdapter: () => TmpFsAdapter
67
- });
68
- module.exports = __toCommonJS(tmpfs_exports);
69
- var import_node_crypto = require("node:crypto");
70
- var import_node_fs = require("node:fs");
71
- var import_promises = require("node:fs/promises");
72
- var import_promises2 = require("node:stream/promises");
73
- var import_yazl = require("yazl");
74
- var import_fs = require("@storyteller-platform/fs");
75
- var import_media_types = require("@storyteller-platform/media-types");
76
- var import_path = require("@storyteller-platform/path");
77
- var import_zip = require("../zip.cjs");
78
- const PRECOMPRESSED_KINDS = /* @__PURE__ */ new Set([
79
- "audio",
80
- "image",
81
- "archive",
82
- "package"
83
- ]);
84
- function mintRootPath() {
85
- return (0, import_fs.scratchPath)(`storyteller-platform-epub-${(0, import_node_crypto.randomUUID)()}`);
86
- }
87
- function isStorableEntryName(entry) {
88
- return !entry.split("/").some((segment) => segment === "" || segment === "." || segment === "..");
89
- }
90
- class TmpFsAdapter {
91
- constructor(rootPath) {
92
- this.rootPath = rootPath;
93
- }
94
- static kind = "extracted-dir";
95
- static capabilities = { writable: true };
96
- static async init(source) {
97
- const rootPath = mintRootPath();
98
- try {
99
- var _stack = [];
100
- try {
101
- const zipfile = await (0, import_zip.openZip)(source);
102
- const stack = __using(_stack, new AsyncDisposableStack(), true);
103
- stack.defer(async () => {
104
- await zipfile.close();
105
- });
106
- for await (const entry of zipfile) {
107
- if (entry.filename.endsWith("/")) {
108
- continue;
109
- }
110
- if (!isStorableEntryName(entry.filename)) continue;
111
- const writePath = (0, import_path.join)(rootPath, ...entry.filename.split("/"));
112
- const readStream = await entry.openReadStream();
113
- await (0, import_promises.mkdir)((0, import_path.dirname)(writePath), { recursive: true });
114
- const writeStream = (0, import_node_fs.createWriteStream)(writePath);
115
- await (0, import_promises2.pipeline)(readStream, writeStream);
116
- }
117
- } catch (_) {
118
- var _error = _, _hasError = true;
119
- } finally {
120
- var _promise = __callDispose(_stack, _error, _hasError);
121
- _promise && await _promise;
122
- }
123
- } catch (error) {
124
- (0, import_node_fs.rmSync)(rootPath, { force: true, recursive: true });
125
- throw error;
126
- }
127
- return new TmpFsAdapter(rootPath);
128
- }
129
- static async initEmpty() {
130
- const rootPath = mintRootPath();
131
- await (0, import_promises.mkdir)(rootPath, { recursive: true });
132
- return new TmpFsAdapter(rootPath);
133
- }
134
- diskPath(entry) {
135
- if (!isStorableEntryName(entry)) {
136
- const err = new Error(`ENOENT: no such entry in EPUB archive: ${entry}`);
137
- err.code = "ENOENT";
138
- throw err;
139
- }
140
- return (0, import_path.join)(this.rootPath, ...entry.split("/"));
141
- }
142
- async read(entry, encoding) {
143
- return await (0, import_promises.readFile)(this.diskPath(entry), encoding);
144
- }
145
- async write(entry, data, encoding) {
146
- const path = this.diskPath(entry);
147
- await (0, import_promises.mkdir)((0, import_path.dirname)(path), { recursive: true });
148
- await (0, import_promises.writeFile)(path, data, encoding);
149
- }
150
- async remove(entry) {
151
- await (0, import_promises.rm)(this.diskPath(entry));
152
- }
153
- async exists(entry) {
154
- try {
155
- await (0, import_promises.stat)(this.diskPath(entry));
156
- return true;
157
- } catch {
158
- return false;
159
- }
160
- }
161
- async archiveLength(entry) {
162
- try {
163
- const st = await (0, import_promises.stat)(this.diskPath(entry));
164
- return st.size;
165
- } catch {
166
- return 0;
167
- }
168
- }
169
- async *list() {
170
- const entries = await (0, import_promises.readdir)(this.rootPath, {
171
- recursive: true,
172
- withFileTypes: true
173
- });
174
- for (const entry of entries) {
175
- if (entry.isDirectory()) continue;
176
- const absolutePath = (0, import_path.join)(entry.parentPath, entry.name);
177
- yield {
178
- absolutePath,
179
- relativePath: absolutePath.slice(this.rootPath.length + 1).split(import_path.sep).join("/")
180
- };
181
- }
182
- }
183
- async duplicate() {
184
- const newRoot = mintRootPath();
185
- try {
186
- await (0, import_promises.cp)(this.rootPath, newRoot, { recursive: true });
187
- } catch (error) {
188
- (0, import_node_fs.rmSync)(newRoot, { force: true, recursive: true });
189
- throw error;
190
- }
191
- return new TmpFsAdapter(newRoot);
192
- }
193
- async serialize(targetPath) {
194
- var _a;
195
- var _stack = [];
196
- try {
197
- const tmpArchivePath = mintRootPath();
198
- const { promise, resolve, reject } = Promise.withResolvers();
199
- const zipfile = new import_yazl.ZipFile();
200
- const writeStream = (0, import_node_fs.createWriteStream)(tmpArchivePath);
201
- writeStream.on("close", () => {
202
- resolve();
203
- });
204
- writeStream.on("error", reject);
205
- zipfile.outputStream.on("error", reject);
206
- const stack = __using(_stack, new AsyncDisposableStack(), true);
207
- stack.defer(async () => {
208
- writeStream.close();
209
- await (0, import_promises.rm)(tmpArchivePath, { force: true });
210
- });
211
- zipfile.outputStream.pipe(writeStream);
212
- zipfile.addBuffer(Buffer.from(import_media_types.MediaType.EPUB.mime), "mimetype", {
213
- compress: false
214
- });
215
- for await (const entry of this.list()) {
216
- if (entry.relativePath === "mimetype") continue;
217
- zipfile.addFile(entry.absolutePath, entry.relativePath, {
218
- compress: !PRECOMPRESSED_KINDS.has(
219
- (_a = import_media_types.MediaType.fromPath(entry.absolutePath)) == null ? void 0 : _a.kind
220
- )
221
- });
222
- }
223
- zipfile.end();
224
- await promise;
225
- await (0, import_promises.cp)(tmpArchivePath, targetPath);
226
- } catch (_) {
227
- var _error = _, _hasError = true;
228
- } finally {
229
- var _promise = __callDispose(_stack, _error, _hasError);
230
- _promise && await _promise;
231
- }
232
- }
233
- dispose() {
234
- (0, import_node_fs.rmSync)(this.rootPath, { recursive: true, force: true });
235
- }
236
- }
237
- // Annotate the CommonJS export names for ESM import in node:
238
- 0 && (module.exports = {
239
- TmpFsAdapter
240
- });
@@ -1,31 +0,0 @@
1
- import { EpubStorageAdapter, EpubListEntry } from './interface.cjs';
2
-
3
- /**
4
- * Extracts an EPUB archive to a temp directory and serves all I/O off
5
- * the real filesystem.
6
- * `Epub.using(TmpFsAdapter).from(...)` returns a writable {@link Epub}.
7
- */
8
- declare class TmpFsAdapter implements EpubStorageAdapter {
9
- private readonly rootPath;
10
- static readonly kind: "extracted-dir";
11
- static readonly capabilities: {
12
- readonly writable: true;
13
- };
14
- static init(source: string | Uint8Array): Promise<TmpFsAdapter>;
15
- static initEmpty(): Promise<TmpFsAdapter>;
16
- private constructor();
17
- private diskPath;
18
- read(entry: string): Promise<Uint8Array>;
19
- read(entry: string, encoding: "utf-8"): Promise<string>;
20
- write(entry: string, data: Uint8Array): Promise<void>;
21
- write(entry: string, data: string, encoding: "utf-8"): Promise<void>;
22
- remove(entry: string): Promise<void>;
23
- exists(entry: string): Promise<boolean>;
24
- archiveLength(entry: string): Promise<number>;
25
- list(): AsyncIterable<EpubListEntry>;
26
- duplicate(): Promise<TmpFsAdapter>;
27
- serialize(targetPath: string): Promise<void>;
28
- dispose(): void;
29
- }
30
-
31
- export { TmpFsAdapter };
@@ -1,50 +0,0 @@
1
- var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
2
- var __typeError = (msg) => {
3
- throw TypeError(msg);
4
- };
5
- var __using = (stack, value, async) => {
6
- if (value != null) {
7
- if (typeof value !== "object" && typeof value !== "function") __typeError("Object expected");
8
- var dispose, inner;
9
- if (async) dispose = value[__knownSymbol("asyncDispose")];
10
- if (dispose === void 0) {
11
- dispose = value[__knownSymbol("dispose")];
12
- if (async) inner = dispose;
13
- }
14
- if (typeof dispose !== "function") __typeError("Object not disposable");
15
- if (inner) dispose = function() {
16
- try {
17
- inner.call(this);
18
- } catch (e) {
19
- return Promise.reject(e);
20
- }
21
- };
22
- stack.push([async, dispose, value]);
23
- } else if (async) {
24
- stack.push([async]);
25
- }
26
- return value;
27
- };
28
- var __callDispose = (stack, error, hasError) => {
29
- var E = typeof SuppressedError === "function" ? SuppressedError : function(e, s, m, _) {
30
- return _ = Error(m), _.name = "SuppressedError", _.error = e, _.suppressed = s, _;
31
- };
32
- var fail = (e) => error = hasError ? new E(e, error, "An error was suppressed during disposal") : (hasError = true, e);
33
- var next = (it) => {
34
- while (it = stack.pop()) {
35
- try {
36
- var result = it[1] && it[1].call(it[2]);
37
- if (it[0]) return Promise.resolve(result).then(next, (e) => (fail(e), next()));
38
- } catch (e) {
39
- fail(e);
40
- }
41
- }
42
- if (hasError) throw error;
43
- };
44
- return next();
45
- };
46
-
47
- export {
48
- __using,
49
- __callDispose
50
- };
@@ -1,60 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var epubcheck_exports = {};
20
- __export(epubcheck_exports, {
21
- epubcheck: () => epubcheck
22
- });
23
- module.exports = __toCommonJS(epubcheck_exports);
24
- var import_node_child_process = require("node:child_process");
25
- var import_node_crypto = require("node:crypto");
26
- var import_promises = require("node:fs/promises");
27
- var import_node_path = require("node:path");
28
- var import_node_url = require("node:url");
29
- var import_node_util = require("node:util");
30
- var import_fs = require("@storyteller-platform/fs");
31
- const import_meta = {};
32
- const here = (0, import_node_path.dirname)((0, import_node_url.fileURLToPath)(import_meta.url));
33
- const JAR = (0, import_node_path.join)(here, "vendors", "epubcheck-5.3.0", "epubcheck.jar");
34
- async function epubcheck(epubFile) {
35
- const output = (0, import_fs.scratchPath)(`epubcheck-${(0, import_node_crypto.randomUUID)()}.json`);
36
- const execFilePromise = (0, import_node_util.promisify)(import_node_child_process.execFile);
37
- try {
38
- await execFilePromise("java", [
39
- "-jar",
40
- JAR,
41
- "--json",
42
- output,
43
- epubFile
44
- ]).catch((error) => {
45
- if (error instanceof Error && error.message.includes("Check finished with errors")) {
46
- return;
47
- }
48
- throw error;
49
- });
50
- const json = await (0, import_promises.readFile)(output, "utf8");
51
- return JSON.parse(json);
52
- } finally {
53
- await (0, import_promises.unlink)(output).catch(() => {
54
- });
55
- }
56
- }
57
- // Annotate the CommonJS export names for ESM import in node:
58
- 0 && (module.exports = {
59
- epubcheck
60
- });
@@ -1,23 +0,0 @@
1
- interface Location {
2
- url: {
3
- hierachical: boolean;
4
- opaque: boolean;
5
- };
6
- path: string;
7
- line: number;
8
- column: number;
9
- context: string | null;
10
- }
11
- interface Message {
12
- ID: string;
13
- severity: "FATAL" | "ERROR" | "WARNING" | "INFO" | "USAGE";
14
- message: string;
15
- additionalLocations: number;
16
- locations: Location[];
17
- }
18
- interface Report {
19
- messages: Message[];
20
- }
21
- declare function epubcheck(epubFile: string): Promise<Report>;
22
-
23
- export { type Location, type Message, type Report, epubcheck };