@storyteller-platform/epub 0.7.0 → 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.
- package/README.md +762 -1054
- package/dist/adapters/interface.d.ts +31 -27
- package/dist/adapters/interface.js +1 -0
- package/dist/adapters/memory.d.ts +7 -11
- package/dist/adapters/memory.js +70 -78
- package/dist/adapters/tmpfs.d.ts +11 -12
- package/dist/adapters/tmpfs.js +238 -165
- package/dist/epubcheck.d.ts +4 -6
- package/dist/epubcheck.js +25 -27
- package/dist/index.d.ts +1161 -5
- package/dist/index.js +2595 -2661
- package/dist/upgrade.d.ts +23 -1169
- package/dist/upgrade.js +411 -463
- package/dist/zip.d.ts +2 -0
- package/dist/zip.js +137 -0
- package/package.json +18 -32
- package/dist/adapters/interface.cjs +0 -16
- package/dist/adapters/interface.d.cts +0 -69
- package/dist/adapters/memory.cjs +0 -103
- package/dist/adapters/memory.d.cts +0 -37
- package/dist/adapters/tmpfs.cjs +0 -236
- package/dist/adapters/tmpfs.d.cts +0 -29
- package/dist/chunk-BIEQXUOY.js +0 -50
- package/dist/epubcheck.cjs +0 -60
- package/dist/epubcheck.d.cts +0 -23
- package/dist/index.cjs +0 -2749
- package/dist/index.d.cts +0 -5
- package/dist/upgrade.cjs +0 -555
- package/dist/upgrade.d.cts +0 -1182
package/dist/zip.d.ts
ADDED
package/dist/zip.js
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { createReadStream } from "node:fs";
|
|
2
|
+
import { open as openFile } from "node:fs/promises";
|
|
3
|
+
import { Reader, fromBuffer, fromReader, open, } from "yauzl-promise";
|
|
4
|
+
const EOCDR_SIGNATURE = Buffer.from([0x50, 0x4b, 0x05, 0x06]);
|
|
5
|
+
const EOCDR_SIZE = 22;
|
|
6
|
+
const MAX_TAIL_SCAN = 1024 * 1024;
|
|
7
|
+
function toBuffer(source) {
|
|
8
|
+
return Buffer.isBuffer(source)
|
|
9
|
+
? source
|
|
10
|
+
: Buffer.from(source.buffer, source.byteOffset, source.byteLength);
|
|
11
|
+
}
|
|
12
|
+
function findArchiveEnd(tail, tailStart, fileSize) {
|
|
13
|
+
let searchFrom = tail.length - EOCDR_SIZE;
|
|
14
|
+
while (searchFrom >= 0) {
|
|
15
|
+
const index = tail.lastIndexOf(EOCDR_SIGNATURE, searchFrom);
|
|
16
|
+
if (index === -1)
|
|
17
|
+
return null;
|
|
18
|
+
const commentLength = tail.readUInt16LE(index + 20);
|
|
19
|
+
const end = tailStart + index + EOCDR_SIZE + commentLength;
|
|
20
|
+
if (end < fileSize)
|
|
21
|
+
return end;
|
|
22
|
+
searchFrom = index - 1;
|
|
23
|
+
}
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
async function readExactly(handle, length, position) {
|
|
27
|
+
const buffer = Buffer.allocUnsafe(length);
|
|
28
|
+
let offset = 0;
|
|
29
|
+
while (offset < length) {
|
|
30
|
+
const { bytesRead } = await handle.read(buffer, offset, length - offset, position + offset);
|
|
31
|
+
if (bytesRead === 0)
|
|
32
|
+
return null;
|
|
33
|
+
offset += bytesRead;
|
|
34
|
+
}
|
|
35
|
+
return buffer;
|
|
36
|
+
}
|
|
37
|
+
async function findFileArchiveEnd(path) {
|
|
38
|
+
let handle;
|
|
39
|
+
try {
|
|
40
|
+
handle = await openFile(path, "r");
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
try {
|
|
46
|
+
const { size } = await handle.stat();
|
|
47
|
+
const tailLength = Math.min(size, MAX_TAIL_SCAN);
|
|
48
|
+
if (tailLength < EOCDR_SIZE)
|
|
49
|
+
return null;
|
|
50
|
+
const tailStart = size - tailLength;
|
|
51
|
+
const tail = await readExactly(handle, tailLength, tailStart);
|
|
52
|
+
if (!tail)
|
|
53
|
+
return null;
|
|
54
|
+
return findArchiveEnd(tail, tailStart, size);
|
|
55
|
+
}
|
|
56
|
+
finally {
|
|
57
|
+
await handle.close();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Basically sometimes EPUB 2s have some garbage at the end of the file after
|
|
62
|
+
* the End of Central Directory Record, which leads to
|
|
63
|
+
* `yauzl` throwing an "End of Central Directory Record not found" error
|
|
64
|
+
*
|
|
65
|
+
* This is an attempt to, when that error happens, try to read the file from the end and find the End of Central Directory Record, then clean up the garbage at the end of the file.
|
|
66
|
+
*/
|
|
67
|
+
class TruncatedFileReader extends Reader {
|
|
68
|
+
path;
|
|
69
|
+
handle = null;
|
|
70
|
+
constructor(path) {
|
|
71
|
+
super();
|
|
72
|
+
this.path = path;
|
|
73
|
+
}
|
|
74
|
+
async _open() {
|
|
75
|
+
this.handle = await openFile(this.path, "r");
|
|
76
|
+
return {};
|
|
77
|
+
}
|
|
78
|
+
async _close() {
|
|
79
|
+
const handle = this.handle;
|
|
80
|
+
this.handle = null;
|
|
81
|
+
if (handle)
|
|
82
|
+
await handle.close();
|
|
83
|
+
return {};
|
|
84
|
+
}
|
|
85
|
+
async _read(start, length) {
|
|
86
|
+
if (!this.handle)
|
|
87
|
+
throw new Error(`reader for ${this.path} is not open`);
|
|
88
|
+
const buffer = await readExactly(this.handle, length, start);
|
|
89
|
+
if (!buffer) {
|
|
90
|
+
throw new Error(`unexpected end of file reading ${this.path}`);
|
|
91
|
+
}
|
|
92
|
+
return buffer;
|
|
93
|
+
}
|
|
94
|
+
_createReadStream(start, length) {
|
|
95
|
+
return createReadStream(this.path, { start, end: start + length - 1 });
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
async function openBufferZip(data) {
|
|
99
|
+
try {
|
|
100
|
+
return await fromBuffer(data);
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
const tailStart = Math.max(0, data.length - MAX_TAIL_SCAN);
|
|
104
|
+
const end = findArchiveEnd(data.subarray(tailStart), tailStart, data.length);
|
|
105
|
+
if (end === null)
|
|
106
|
+
throw error;
|
|
107
|
+
try {
|
|
108
|
+
return await fromBuffer(data.subarray(0, end));
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
throw error;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
async function openFileZip(path) {
|
|
116
|
+
try {
|
|
117
|
+
return await open(path);
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
const end = await findFileArchiveEnd(path);
|
|
121
|
+
if (end === null)
|
|
122
|
+
throw error;
|
|
123
|
+
const reader = new TruncatedFileReader(path);
|
|
124
|
+
try {
|
|
125
|
+
return await fromReader(reader, end);
|
|
126
|
+
}
|
|
127
|
+
catch {
|
|
128
|
+
await reader._close().catch(() => undefined);
|
|
129
|
+
throw error;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
export async function openZip(source) {
|
|
134
|
+
return typeof source === "string"
|
|
135
|
+
? openFileZip(source)
|
|
136
|
+
: openBufferZip(toBuffer(source));
|
|
137
|
+
}
|
package/package.json
CHANGED
|
@@ -1,60 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@storyteller-platform/epub",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"module": "dist/index.js",
|
|
6
5
|
"types": "dist/index.d.ts",
|
|
7
6
|
"license": "MIT",
|
|
8
7
|
"exports": {
|
|
9
|
-
"
|
|
10
|
-
|
|
11
|
-
"default": "./dist/index.js"
|
|
12
|
-
},
|
|
13
|
-
"require": {
|
|
14
|
-
"types": "./dist/index.d.cts",
|
|
15
|
-
"default": "./dist/index.cjs"
|
|
16
|
-
}
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"default": "./dist/index.js"
|
|
17
10
|
},
|
|
18
11
|
"files": [
|
|
19
12
|
"dist/"
|
|
20
13
|
],
|
|
21
14
|
"scripts": {
|
|
22
|
-
"build": "
|
|
15
|
+
"build": "rm -rf dist .tsbuild && tsc -b tsconfig.build.json",
|
|
23
16
|
"readme:toc": "markdown-toc --maxdepth=5 --append='\n- [API Docs](#api-docs)' --bullets='-' -i readme-stub.md",
|
|
24
17
|
"readme:api": "typedoc",
|
|
25
18
|
"readme": "yarn readme:api && yarn readme:toc && cat readme-stub.md > README.md && tail -n +2 gen/README.md >> README.md",
|
|
26
19
|
"install-epubcheck": "tsx scripts/install-epubcheck.ts",
|
|
27
|
-
"test": "yarn install-epubcheck &&
|
|
28
|
-
"test:watch": "yarn install-epubcheck &&
|
|
20
|
+
"test": "yarn install-epubcheck && vitest run",
|
|
21
|
+
"test:watch": "yarn install-epubcheck && vitest",
|
|
29
22
|
"prepack": "yarn build"
|
|
30
23
|
},
|
|
31
24
|
"devDependencies": {
|
|
32
|
-
"@storyteller-platform/eslint": "0.1.0",
|
|
33
|
-
"@storyteller-platform/tsup": "^0.1.0",
|
|
34
25
|
"@tsconfig/strictest": "^2.0.5",
|
|
35
|
-
"@types/mime-types": "^2",
|
|
36
26
|
"@types/node": "^24.0.0",
|
|
37
27
|
"@types/yauzl-promise": "^4",
|
|
38
28
|
"@types/yazl": "^3",
|
|
39
|
-
"
|
|
29
|
+
"@typescript/native": "npm:typescript@^7.0.2",
|
|
40
30
|
"markdown-toc": "^1.2.0",
|
|
41
31
|
"remark-toc": "^9.0.0",
|
|
42
|
-
"tsup": "^8.5.0",
|
|
43
32
|
"tsx": "^4.19.2",
|
|
44
|
-
"typedoc": "^0.28.
|
|
33
|
+
"typedoc": "^0.28.20",
|
|
45
34
|
"typedoc-plugin-markdown": "^4.6.3",
|
|
46
35
|
"typedoc-plugin-remark": "^1.2.0",
|
|
47
|
-
"typescript": "
|
|
36
|
+
"typescript": "npm:@typescript/typescript6@^6.0.2",
|
|
37
|
+
"vite": "^8.0.0",
|
|
38
|
+
"vitest": "^5.0.0"
|
|
48
39
|
},
|
|
49
40
|
"dependencies": {
|
|
50
41
|
"@readium/shared": "^2.2.0",
|
|
51
|
-
"@storyteller-platform/fs": "^0.
|
|
52
|
-
"@storyteller-platform/
|
|
42
|
+
"@storyteller-platform/fs": "^0.3.0",
|
|
43
|
+
"@storyteller-platform/media-types": "^0.2.0",
|
|
44
|
+
"@storyteller-platform/path": "^0.3.0",
|
|
53
45
|
"@zip.js/zip.js": "^2.0.0",
|
|
54
46
|
"async-mutex": "^0.5.0",
|
|
55
|
-
"fast-xml-
|
|
47
|
+
"fast-xml-builder": "^1.3.1",
|
|
48
|
+
"fast-xml-parser": "^5.11.1",
|
|
56
49
|
"mem": "^8.0.0",
|
|
57
|
-
"mime-types": "^3.0.1",
|
|
58
50
|
"nanoid": "^5.1.5",
|
|
59
51
|
"yauzl-promise": "^4.0.0",
|
|
60
52
|
"yazl": "^3.3.1"
|
|
@@ -62,14 +54,8 @@
|
|
|
62
54
|
"publishConfig": {
|
|
63
55
|
"access": "public",
|
|
64
56
|
"exports": {
|
|
65
|
-
"
|
|
66
|
-
|
|
67
|
-
"default": "./dist/index.js"
|
|
68
|
-
},
|
|
69
|
-
"require": {
|
|
70
|
-
"types": "./dist/index.d.cts",
|
|
71
|
-
"default": "./dist/index.cjs"
|
|
72
|
-
}
|
|
57
|
+
"types": "./dist/index.d.ts",
|
|
58
|
+
"default": "./dist/index.js"
|
|
73
59
|
}
|
|
74
60
|
}
|
|
75
61
|
}
|
|
@@ -1,16 +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 __copyProps = (to, from, except, desc) => {
|
|
7
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
-
for (let key of __getOwnPropNames(from))
|
|
9
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
-
}
|
|
12
|
-
return to;
|
|
13
|
-
};
|
|
14
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
-
var interface_exports = {};
|
|
16
|
-
module.exports = __toCommonJS(interface_exports);
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Pluggable storage backend for an Epub instance
|
|
3
|
-
*
|
|
4
|
-
* Built-in adapters:
|
|
5
|
-
* - {@link TmpFsAdapter} extracts the archive to a tmp directory and
|
|
6
|
-
* supports the full read/write/save/upgrade
|
|
7
|
-
* - {@link MemoryAdapter} keeps an in-memory zip handle and supports
|
|
8
|
-
* reads only
|
|
9
|
-
*/
|
|
10
|
-
type EpubStorageKind = "tmp-dir" | "in-memory" | (string & {});
|
|
11
|
-
interface EpubStorageCapabilities {
|
|
12
|
-
/** when false, the Epub class refuses every mutation method at runtime */
|
|
13
|
-
readonly writable: boolean;
|
|
14
|
-
}
|
|
15
|
-
interface EpubListEntry {
|
|
16
|
-
/** absolute path under {@link EpubStorageAdapter.rootPath} */
|
|
17
|
-
absolutePath: string;
|
|
18
|
-
/** path relative to {@link EpubStorageAdapter.rootPath}, slash-separated */
|
|
19
|
-
relativePath: string;
|
|
20
|
-
}
|
|
21
|
-
interface EpubStorageAdapter {
|
|
22
|
-
/**
|
|
23
|
-
* Opaque path prefix used by `resolveInternalHref` to anchor absolute
|
|
24
|
-
* paths within the archive. For TmpFsAdapter this is the tmp dir;
|
|
25
|
-
* for MemoryAdapter it's a virtual prefix that's never written to disk
|
|
26
|
-
*/
|
|
27
|
-
readonly rootPath: string;
|
|
28
|
-
read(path: string): Promise<Uint8Array>;
|
|
29
|
-
read(path: string, encoding: "utf-8"): Promise<string>;
|
|
30
|
-
/**
|
|
31
|
-
* Length of an entry in bytes, for the readium page-count heuristic
|
|
32
|
-
* which expects compressed size when available
|
|
33
|
-
*/
|
|
34
|
-
archiveLength(path: string): Promise<number>;
|
|
35
|
-
/** Required for any mutation method on Epub */
|
|
36
|
-
write?(path: string, data: Uint8Array): Promise<void>;
|
|
37
|
-
write?(path: string, data: string, encoding: "utf-8"): Promise<void>;
|
|
38
|
-
/** Required for removeManifestItem / setCoverImage replacement */
|
|
39
|
-
remove?(path: string): Promise<void>;
|
|
40
|
-
/** Required for Epub.saveAndClose to walk the contents */
|
|
41
|
-
list?(): AsyncIterable<EpubListEntry>;
|
|
42
|
-
/** Required for Epub.copy */
|
|
43
|
-
duplicate?(): Promise<EpubStorageAdapter>;
|
|
44
|
-
/** Required for Epub.saveAndClose */
|
|
45
|
-
serialize?(targetPath: string): Promise<void>;
|
|
46
|
-
/** Always called on close or error */
|
|
47
|
-
dispose(): void | Promise<void>;
|
|
48
|
-
}
|
|
49
|
-
interface EpubStorageAdapterClass<Opts extends object = object> {
|
|
50
|
-
readonly kind: EpubStorageKind;
|
|
51
|
-
readonly capabilities: EpubStorageCapabilities;
|
|
52
|
-
/**
|
|
53
|
-
* Open an archive from a path or buffer. The returned adapter is
|
|
54
|
-
* ready to read; the Epub class will assert it's a valid EPUB 3
|
|
55
|
-
* afterwards.
|
|
56
|
-
*/
|
|
57
|
-
init(source: string | Uint8Array, opts?: Opts): Promise<EpubStorageAdapter>;
|
|
58
|
-
/**
|
|
59
|
-
* Initialize an empty extract root for {@link Epub.create}.
|
|
60
|
-
*
|
|
61
|
-
* Optional — adapters that can't be written to from scratch
|
|
62
|
-
* (e.g. read-only zip handles) should leave this off, and
|
|
63
|
-
* `Epub.using(...).create(...)` will throw.
|
|
64
|
-
*/
|
|
65
|
-
initEmpty?(opts?: Opts): Promise<EpubStorageAdapter>;
|
|
66
|
-
}
|
|
67
|
-
type AdapterOptions<A> = A extends EpubStorageAdapterClass<infer Opts> ? Opts : object;
|
|
68
|
-
|
|
69
|
-
export type { AdapterOptions, EpubListEntry, EpubStorageAdapter, EpubStorageAdapterClass, EpubStorageCapabilities, EpubStorageKind };
|
package/dist/adapters/memory.cjs
DELETED
|
@@ -1,103 +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 memory_exports = {};
|
|
20
|
-
__export(memory_exports, {
|
|
21
|
-
MemoryAdapter: () => MemoryAdapter
|
|
22
|
-
});
|
|
23
|
-
module.exports = __toCommonJS(memory_exports);
|
|
24
|
-
var import_node_crypto = require("node:crypto");
|
|
25
|
-
var import_node_os = require("node:os");
|
|
26
|
-
var import_yauzl_promise = require("yauzl-promise");
|
|
27
|
-
var import_path = require("@storyteller-platform/path");
|
|
28
|
-
class MemoryAdapter {
|
|
29
|
-
constructor(rootPath, zip, entries, cache) {
|
|
30
|
-
this.rootPath = rootPath;
|
|
31
|
-
this.zip = zip;
|
|
32
|
-
this.entries = entries;
|
|
33
|
-
this.cache = cache;
|
|
34
|
-
}
|
|
35
|
-
static kind = "in-memory";
|
|
36
|
-
static capabilities = { writable: false };
|
|
37
|
-
static async init(source, opts = {}) {
|
|
38
|
-
const { cache = true, signal } = opts;
|
|
39
|
-
signal == null ? void 0 : signal.throwIfAborted();
|
|
40
|
-
const zip = typeof source === "string" ? await (0, import_yauzl_promise.open)(source) : await (0, import_yauzl_promise.fromBuffer)(Buffer.from(source));
|
|
41
|
-
const entries = /* @__PURE__ */ new Map();
|
|
42
|
-
try {
|
|
43
|
-
for await (const entry of zip) {
|
|
44
|
-
signal == null ? void 0 : signal.throwIfAborted();
|
|
45
|
-
if (entry.filename.endsWith("/")) continue;
|
|
46
|
-
entries.set(entry.filename, entry);
|
|
47
|
-
}
|
|
48
|
-
} catch (error) {
|
|
49
|
-
await zip.close();
|
|
50
|
-
throw error;
|
|
51
|
-
}
|
|
52
|
-
const rootPath = (0, import_path.join)(
|
|
53
|
-
(0, import_node_os.tmpdir)(),
|
|
54
|
-
`storyteller-platform-epub-zip-${(0, import_node_crypto.randomUUID)()}.epub`
|
|
55
|
-
);
|
|
56
|
-
return new MemoryAdapter(rootPath, zip, entries, cache ? /* @__PURE__ */ new Map() : null);
|
|
57
|
-
}
|
|
58
|
-
toZipEntryKey(path) {
|
|
59
|
-
const prefix = this.rootPath.endsWith(import_path.sep) ? this.rootPath : this.rootPath + import_path.sep;
|
|
60
|
-
const rel = path.startsWith(prefix) ? path.slice(prefix.length) : path;
|
|
61
|
-
return import_path.sep === "/" ? rel : rel.split(import_path.sep).join("/");
|
|
62
|
-
}
|
|
63
|
-
async read(path, encoding) {
|
|
64
|
-
var _a, _b;
|
|
65
|
-
const key = this.toZipEntryKey(path);
|
|
66
|
-
const cached = (_a = this.cache) == null ? void 0 : _a.get(key);
|
|
67
|
-
if (cached) {
|
|
68
|
-
return encoding ? cached.toString(encoding) : new Uint8Array(cached);
|
|
69
|
-
}
|
|
70
|
-
const entry = this.entries.get(key);
|
|
71
|
-
if (!entry) {
|
|
72
|
-
const err = new Error(
|
|
73
|
-
`ENOENT: zip entry not found in EPUB archive: ${key}`
|
|
74
|
-
);
|
|
75
|
-
err.code = "ENOENT";
|
|
76
|
-
throw err;
|
|
77
|
-
}
|
|
78
|
-
const stream = await entry.openReadStream();
|
|
79
|
-
const chunks = [];
|
|
80
|
-
for await (const chunk of stream) {
|
|
81
|
-
chunks.push(chunk);
|
|
82
|
-
}
|
|
83
|
-
const buf = Buffer.concat(chunks);
|
|
84
|
-
(_b = this.cache) == null ? void 0 : _b.set(key, buf);
|
|
85
|
-
return encoding ? buf.toString(encoding) : new Uint8Array(buf);
|
|
86
|
-
}
|
|
87
|
-
// eslint-disable-next-line @typescript-eslint/require-await
|
|
88
|
-
async archiveLength(path) {
|
|
89
|
-
const entry = this.entries.get(this.toZipEntryKey(path));
|
|
90
|
-
if (!entry) return 0;
|
|
91
|
-
return entry.compressedSize || entry.uncompressedSize;
|
|
92
|
-
}
|
|
93
|
-
dispose() {
|
|
94
|
-
var _a;
|
|
95
|
-
this.entries.clear();
|
|
96
|
-
(_a = this.cache) == null ? void 0 : _a.clear();
|
|
97
|
-
this.zip.close().catch(() => void 0);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
101
|
-
0 && (module.exports = {
|
|
102
|
-
MemoryAdapter
|
|
103
|
-
});
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { EpubStorageAdapter } from './interface.cjs';
|
|
2
|
-
|
|
3
|
-
interface MemoryAdapterOptions {
|
|
4
|
-
/**
|
|
5
|
-
* per-entry decompressed buffer cache
|
|
6
|
-
* turn it off when opening many EPUBs and reading each entry at most once
|
|
7
|
-
* keeps resident size bounded by the entry index, not the decompressed payload sum
|
|
8
|
-
* @default true
|
|
9
|
-
*/
|
|
10
|
-
cache?: boolean;
|
|
11
|
-
signal?: AbortSignal;
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Loads an EPUB archive into memory and serves all I/O off the in-memory zip handle.
|
|
15
|
-
*
|
|
16
|
-
* read only
|
|
17
|
-
*
|
|
18
|
-
*/
|
|
19
|
-
declare class MemoryAdapter implements EpubStorageAdapter {
|
|
20
|
-
readonly rootPath: string;
|
|
21
|
-
private zip;
|
|
22
|
-
private entries;
|
|
23
|
-
private cache;
|
|
24
|
-
static readonly kind: "in-memory";
|
|
25
|
-
static readonly capabilities: {
|
|
26
|
-
readonly writable: false;
|
|
27
|
-
};
|
|
28
|
-
static init(source: string | Uint8Array, opts?: MemoryAdapterOptions): Promise<MemoryAdapter>;
|
|
29
|
-
private constructor();
|
|
30
|
-
private toZipEntryKey;
|
|
31
|
-
read(path: string): Promise<Uint8Array>;
|
|
32
|
-
read(path: string, encoding: "utf-8"): Promise<string>;
|
|
33
|
-
archiveLength(path: string): Promise<number>;
|
|
34
|
-
dispose(): void;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export { MemoryAdapter, type MemoryAdapterOptions };
|