modern-pdf-lib 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +455 -0
- package/dist/documentMerge-CNPWlWic.mjs +18351 -0
- package/dist/documentMerge-DnLzOg5P.cjs +18878 -0
- package/dist/fflateAdapter-D2mv_ttM.mjs +196 -0
- package/dist/fflateAdapter-cT4YeY_h.cjs +207 -0
- package/dist/fontSubset-BOGts8y9.mjs +203 -0
- package/dist/fontSubset-C0Rm9ih6.cjs +226 -0
- package/dist/index.cjs +4597 -0
- package/dist/index.d.cts +7898 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +7898 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +4306 -0
- package/dist/libdeflateWasm-QVHmuzw-.mjs +220 -0
- package/dist/libdeflateWasm-to2bG6NG.cjs +237 -0
- package/dist/loader-D9LTYmrX.mjs +162 -0
- package/dist/loader-mwt5wRJz.cjs +164 -0
- package/dist/pdfCatalog-DTXk0tbK.cjs +627 -0
- package/dist/pdfCatalog-Dk4qUVvx.mjs +532 -0
- package/dist/pdfPage-C9vw_D1J.cjs +5203 -0
- package/dist/pdfPage-DZA6XJzR.mjs +4544 -0
- package/dist/pngEmbed-BN-gMJrb.cjs +536 -0
- package/dist/pngEmbed-DgeNWlbS.mjs +525 -0
- package/dist/rolldown-runtime-95iHPtFO.mjs +18 -0
- package/dist/rolldown-runtime-CKhH4XqG.cjs +24 -0
- package/package.json +94 -0
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { t as __exportAll } from "./rolldown-runtime-95iHPtFO.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/compression/libdeflateWasm.ts
|
|
4
|
+
var libdeflateWasm_exports = /* @__PURE__ */ __exportAll({
|
|
5
|
+
LibdeflateWasm: () => LibdeflateWasm,
|
|
6
|
+
deflateSync: () => deflateSync,
|
|
7
|
+
dispose: () => dispose,
|
|
8
|
+
inflateSync: () => inflateSync,
|
|
9
|
+
initDeflateWasm: () => initDeflateWasm,
|
|
10
|
+
isAvailable: () => isAvailable
|
|
11
|
+
});
|
|
12
|
+
/** The instantiated WASM module exports, or `null` if not yet loaded. */
|
|
13
|
+
let wasmExports = null;
|
|
14
|
+
/** Whether initialization has been attempted. */
|
|
15
|
+
let initAttempted = false;
|
|
16
|
+
/** Whether the WASM module is ready for use. */
|
|
17
|
+
let available = false;
|
|
18
|
+
/**
|
|
19
|
+
* Get a view of the WASM linear memory.
|
|
20
|
+
*
|
|
21
|
+
* This must be called fresh each time because growing the WASM memory
|
|
22
|
+
* invalidates prior `Uint8Array` views.
|
|
23
|
+
*/
|
|
24
|
+
function wasmMemory() {
|
|
25
|
+
if (!wasmExports) throw new Error("libdeflate WASM not initialized");
|
|
26
|
+
return new Uint8Array(wasmExports.memory.buffer);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Copy data into WASM linear memory at the given pointer.
|
|
30
|
+
*
|
|
31
|
+
* @param ptr - Target pointer in WASM memory.
|
|
32
|
+
* @param data - Source bytes to copy.
|
|
33
|
+
*/
|
|
34
|
+
function copyToWasm(ptr, data) {
|
|
35
|
+
wasmMemory().set(data, ptr);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Copy data out of WASM linear memory.
|
|
39
|
+
*
|
|
40
|
+
* @param ptr - Source pointer in WASM memory.
|
|
41
|
+
* @param len - Number of bytes to copy.
|
|
42
|
+
* @returns A new Uint8Array with the copied data.
|
|
43
|
+
*/
|
|
44
|
+
function copyFromWasm(ptr, len) {
|
|
45
|
+
const copy = new Uint8Array(len);
|
|
46
|
+
copy.set(wasmMemory().subarray(ptr, ptr + len));
|
|
47
|
+
return copy;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Initialize the libdeflate WASM module.
|
|
51
|
+
*
|
|
52
|
+
* This must be called before using {@link LibdeflateWasm} or the
|
|
53
|
+
* synchronous helpers. It is safe to call multiple times -- subsequent
|
|
54
|
+
* calls are no-ops.
|
|
55
|
+
*
|
|
56
|
+
* @param wasmBytes - Optional pre-loaded WASM binary. When omitted,
|
|
57
|
+
* the module is loaded via the universal WASM loader
|
|
58
|
+
* (see `src/wasm/loader.ts`).
|
|
59
|
+
* @throws If the WASM module cannot be loaded or instantiated.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* // Auto-load from default location
|
|
64
|
+
* await initDeflateWasm();
|
|
65
|
+
*
|
|
66
|
+
* // Or provide bytes directly (e.g. bundled)
|
|
67
|
+
* await initDeflateWasm(myWasmBytes);
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
async function initDeflateWasm(wasmBytes) {
|
|
71
|
+
if (available) return;
|
|
72
|
+
if (initAttempted) return;
|
|
73
|
+
initAttempted = true;
|
|
74
|
+
try {
|
|
75
|
+
let bytes;
|
|
76
|
+
if (wasmBytes) bytes = wasmBytes;
|
|
77
|
+
else {
|
|
78
|
+
const { loadWasmModule } = await import("./loader-D9LTYmrX.mjs");
|
|
79
|
+
bytes = await loadWasmModule("libdeflate");
|
|
80
|
+
}
|
|
81
|
+
const compiled = await WebAssembly.compile(new Uint8Array(bytes));
|
|
82
|
+
wasmExports = (await WebAssembly.instantiate(compiled, {})).exports;
|
|
83
|
+
available = true;
|
|
84
|
+
} catch (err) {
|
|
85
|
+
wasmExports = null;
|
|
86
|
+
available = false;
|
|
87
|
+
throw new Error(`Failed to initialize libdeflate WASM: ${err instanceof Error ? err.message : String(err)}`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Synchronously compress data using libdeflate WASM.
|
|
92
|
+
*
|
|
93
|
+
* The WASM module must already be initialized via {@link initDeflateWasm}.
|
|
94
|
+
*
|
|
95
|
+
* @param input - Uncompressed bytes.
|
|
96
|
+
* @param level - Compression level 1-12. Default: `6`.
|
|
97
|
+
* @returns Compressed bytes (raw deflate).
|
|
98
|
+
* @throws If WASM is not initialized.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```ts
|
|
102
|
+
* await initDeflateWasm(bytes);
|
|
103
|
+
* const compressed = deflateSync(myData, 9);
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
function deflateSync(input, level = 6) {
|
|
107
|
+
if (!wasmExports) throw new Error("libdeflate WASM not initialized. Call initDeflateWasm() first.");
|
|
108
|
+
const clampedLevel = Math.max(1, Math.min(12, Math.round(level)));
|
|
109
|
+
const maxOutputSize = input.length + Math.ceil(input.length / 16) + 64 + 3;
|
|
110
|
+
const inputPtr = wasmExports.alloc(input.length);
|
|
111
|
+
const outputPtr = wasmExports.alloc(maxOutputSize);
|
|
112
|
+
try {
|
|
113
|
+
copyToWasm(inputPtr, input);
|
|
114
|
+
const actualSize = wasmExports.compress(inputPtr, input.length, outputPtr, maxOutputSize, clampedLevel);
|
|
115
|
+
if (actualSize <= 0) throw new Error("libdeflate compression failed (returned zero or negative size)");
|
|
116
|
+
return copyFromWasm(outputPtr, actualSize);
|
|
117
|
+
} finally {
|
|
118
|
+
wasmExports.dealloc(inputPtr, input.length);
|
|
119
|
+
wasmExports.dealloc(outputPtr, maxOutputSize);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Synchronously decompress raw deflate data using libdeflate WASM.
|
|
124
|
+
*
|
|
125
|
+
* @param input - Compressed bytes (raw deflate).
|
|
126
|
+
* @param outputSize - Maximum expected output size. Must be >= actual
|
|
127
|
+
* decompressed size. Default: `input.length * 4`.
|
|
128
|
+
* @returns Decompressed bytes.
|
|
129
|
+
* @throws If WASM is not initialized or decompression fails.
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```ts
|
|
133
|
+
* const original = inflateSync(compressed, knownOriginalSize);
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
136
|
+
function inflateSync(input, outputSize = input.length * 4) {
|
|
137
|
+
if (!wasmExports) throw new Error("libdeflate WASM not initialized. Call initDeflateWasm() first.");
|
|
138
|
+
const maxOutput = Math.max(outputSize, input.length * 2);
|
|
139
|
+
const inputPtr = wasmExports.alloc(input.length);
|
|
140
|
+
const outputPtr = wasmExports.alloc(maxOutput);
|
|
141
|
+
try {
|
|
142
|
+
copyToWasm(inputPtr, input);
|
|
143
|
+
const actualSize = wasmExports.decompress(inputPtr, input.length, outputPtr, maxOutput);
|
|
144
|
+
if (actualSize < 0) throw new Error("libdeflate decompression failed. The input may be corrupt or the output buffer may be too small.");
|
|
145
|
+
return copyFromWasm(outputPtr, actualSize);
|
|
146
|
+
} finally {
|
|
147
|
+
wasmExports.dealloc(inputPtr, input.length);
|
|
148
|
+
wasmExports.dealloc(outputPtr, maxOutput);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Check whether the libdeflate WASM module is initialized and available.
|
|
153
|
+
*
|
|
154
|
+
* @returns `true` if WASM is ready for use.
|
|
155
|
+
*/
|
|
156
|
+
function isAvailable() {
|
|
157
|
+
return available;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Release the WASM module and free associated resources.
|
|
161
|
+
*
|
|
162
|
+
* After calling this, the module must be re-initialized with
|
|
163
|
+
* {@link initDeflateWasm} before further use.
|
|
164
|
+
*/
|
|
165
|
+
function dispose() {
|
|
166
|
+
wasmExports = null;
|
|
167
|
+
available = false;
|
|
168
|
+
initAttempted = false;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* A {@link CompressionEngine} backed by libdeflate compiled to WASM.
|
|
172
|
+
*
|
|
173
|
+
* This provides the fastest compression available in the library,
|
|
174
|
+
* with support for levels 1-12 (libdeflate's native range).
|
|
175
|
+
*
|
|
176
|
+
* The WASM module must be initialized before constructing this class.
|
|
177
|
+
* Use {@link initDeflateWasm} to load the module.
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* ```ts
|
|
181
|
+
* await initDeflateWasm(wasmBytes);
|
|
182
|
+
* const engine = new LibdeflateWasm();
|
|
183
|
+
* const compressed = await engine.compress(data, 6);
|
|
184
|
+
* ```
|
|
185
|
+
*/
|
|
186
|
+
var LibdeflateWasm = class {
|
|
187
|
+
name = "libdeflate-wasm";
|
|
188
|
+
minLevel = 1;
|
|
189
|
+
maxLevel = 12;
|
|
190
|
+
/**
|
|
191
|
+
* Compress data using libdeflate WASM.
|
|
192
|
+
*
|
|
193
|
+
* @param data - Uncompressed input bytes.
|
|
194
|
+
* @param level - Compression level 1-12 (clamped). Default: `6`.
|
|
195
|
+
* @returns Compressed bytes (raw deflate).
|
|
196
|
+
*/
|
|
197
|
+
async compress(data, level = 6) {
|
|
198
|
+
return deflateSync(data, level);
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Decompress raw deflate data using libdeflate WASM.
|
|
202
|
+
*
|
|
203
|
+
* @param data - Compressed bytes (raw deflate).
|
|
204
|
+
* @param outputSize - Expected output size hint. Default: `data.length * 4`.
|
|
205
|
+
* @returns Decompressed bytes.
|
|
206
|
+
*/
|
|
207
|
+
async decompress(data, outputSize) {
|
|
208
|
+
return inflateSync(data, outputSize);
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Release the WASM module resources.
|
|
212
|
+
*/
|
|
213
|
+
dispose() {
|
|
214
|
+
dispose();
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
//#endregion
|
|
219
|
+
export { isAvailable as n, libdeflateWasm_exports as r, deflateSync as t };
|
|
220
|
+
//# sourceMappingURL=libdeflateWasm-QVHmuzw-.mjs.map
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('./rolldown-runtime-CKhH4XqG.cjs');
|
|
2
|
+
|
|
3
|
+
//#region src/compression/libdeflateWasm.ts
|
|
4
|
+
var libdeflateWasm_exports = /* @__PURE__ */ require_rolldown_runtime.__exportAll({
|
|
5
|
+
LibdeflateWasm: () => LibdeflateWasm,
|
|
6
|
+
deflateSync: () => deflateSync,
|
|
7
|
+
dispose: () => dispose,
|
|
8
|
+
inflateSync: () => inflateSync,
|
|
9
|
+
initDeflateWasm: () => initDeflateWasm,
|
|
10
|
+
isAvailable: () => isAvailable
|
|
11
|
+
});
|
|
12
|
+
/** The instantiated WASM module exports, or `null` if not yet loaded. */
|
|
13
|
+
let wasmExports = null;
|
|
14
|
+
/** Whether initialization has been attempted. */
|
|
15
|
+
let initAttempted = false;
|
|
16
|
+
/** Whether the WASM module is ready for use. */
|
|
17
|
+
let available = false;
|
|
18
|
+
/**
|
|
19
|
+
* Get a view of the WASM linear memory.
|
|
20
|
+
*
|
|
21
|
+
* This must be called fresh each time because growing the WASM memory
|
|
22
|
+
* invalidates prior `Uint8Array` views.
|
|
23
|
+
*/
|
|
24
|
+
function wasmMemory() {
|
|
25
|
+
if (!wasmExports) throw new Error("libdeflate WASM not initialized");
|
|
26
|
+
return new Uint8Array(wasmExports.memory.buffer);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Copy data into WASM linear memory at the given pointer.
|
|
30
|
+
*
|
|
31
|
+
* @param ptr - Target pointer in WASM memory.
|
|
32
|
+
* @param data - Source bytes to copy.
|
|
33
|
+
*/
|
|
34
|
+
function copyToWasm(ptr, data) {
|
|
35
|
+
wasmMemory().set(data, ptr);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Copy data out of WASM linear memory.
|
|
39
|
+
*
|
|
40
|
+
* @param ptr - Source pointer in WASM memory.
|
|
41
|
+
* @param len - Number of bytes to copy.
|
|
42
|
+
* @returns A new Uint8Array with the copied data.
|
|
43
|
+
*/
|
|
44
|
+
function copyFromWasm(ptr, len) {
|
|
45
|
+
const copy = new Uint8Array(len);
|
|
46
|
+
copy.set(wasmMemory().subarray(ptr, ptr + len));
|
|
47
|
+
return copy;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Initialize the libdeflate WASM module.
|
|
51
|
+
*
|
|
52
|
+
* This must be called before using {@link LibdeflateWasm} or the
|
|
53
|
+
* synchronous helpers. It is safe to call multiple times -- subsequent
|
|
54
|
+
* calls are no-ops.
|
|
55
|
+
*
|
|
56
|
+
* @param wasmBytes - Optional pre-loaded WASM binary. When omitted,
|
|
57
|
+
* the module is loaded via the universal WASM loader
|
|
58
|
+
* (see `src/wasm/loader.ts`).
|
|
59
|
+
* @throws If the WASM module cannot be loaded or instantiated.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* // Auto-load from default location
|
|
64
|
+
* await initDeflateWasm();
|
|
65
|
+
*
|
|
66
|
+
* // Or provide bytes directly (e.g. bundled)
|
|
67
|
+
* await initDeflateWasm(myWasmBytes);
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
async function initDeflateWasm(wasmBytes) {
|
|
71
|
+
if (available) return;
|
|
72
|
+
if (initAttempted) return;
|
|
73
|
+
initAttempted = true;
|
|
74
|
+
try {
|
|
75
|
+
let bytes;
|
|
76
|
+
if (wasmBytes) bytes = wasmBytes;
|
|
77
|
+
else {
|
|
78
|
+
const { loadWasmModule } = await Promise.resolve().then(() => require("./loader-mwt5wRJz.cjs"));
|
|
79
|
+
bytes = await loadWasmModule("libdeflate");
|
|
80
|
+
}
|
|
81
|
+
const compiled = await WebAssembly.compile(new Uint8Array(bytes));
|
|
82
|
+
wasmExports = (await WebAssembly.instantiate(compiled, {})).exports;
|
|
83
|
+
available = true;
|
|
84
|
+
} catch (err) {
|
|
85
|
+
wasmExports = null;
|
|
86
|
+
available = false;
|
|
87
|
+
throw new Error(`Failed to initialize libdeflate WASM: ${err instanceof Error ? err.message : String(err)}`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Synchronously compress data using libdeflate WASM.
|
|
92
|
+
*
|
|
93
|
+
* The WASM module must already be initialized via {@link initDeflateWasm}.
|
|
94
|
+
*
|
|
95
|
+
* @param input - Uncompressed bytes.
|
|
96
|
+
* @param level - Compression level 1-12. Default: `6`.
|
|
97
|
+
* @returns Compressed bytes (raw deflate).
|
|
98
|
+
* @throws If WASM is not initialized.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```ts
|
|
102
|
+
* await initDeflateWasm(bytes);
|
|
103
|
+
* const compressed = deflateSync(myData, 9);
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
function deflateSync(input, level = 6) {
|
|
107
|
+
if (!wasmExports) throw new Error("libdeflate WASM not initialized. Call initDeflateWasm() first.");
|
|
108
|
+
const clampedLevel = Math.max(1, Math.min(12, Math.round(level)));
|
|
109
|
+
const maxOutputSize = input.length + Math.ceil(input.length / 16) + 64 + 3;
|
|
110
|
+
const inputPtr = wasmExports.alloc(input.length);
|
|
111
|
+
const outputPtr = wasmExports.alloc(maxOutputSize);
|
|
112
|
+
try {
|
|
113
|
+
copyToWasm(inputPtr, input);
|
|
114
|
+
const actualSize = wasmExports.compress(inputPtr, input.length, outputPtr, maxOutputSize, clampedLevel);
|
|
115
|
+
if (actualSize <= 0) throw new Error("libdeflate compression failed (returned zero or negative size)");
|
|
116
|
+
return copyFromWasm(outputPtr, actualSize);
|
|
117
|
+
} finally {
|
|
118
|
+
wasmExports.dealloc(inputPtr, input.length);
|
|
119
|
+
wasmExports.dealloc(outputPtr, maxOutputSize);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Synchronously decompress raw deflate data using libdeflate WASM.
|
|
124
|
+
*
|
|
125
|
+
* @param input - Compressed bytes (raw deflate).
|
|
126
|
+
* @param outputSize - Maximum expected output size. Must be >= actual
|
|
127
|
+
* decompressed size. Default: `input.length * 4`.
|
|
128
|
+
* @returns Decompressed bytes.
|
|
129
|
+
* @throws If WASM is not initialized or decompression fails.
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```ts
|
|
133
|
+
* const original = inflateSync(compressed, knownOriginalSize);
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
136
|
+
function inflateSync(input, outputSize = input.length * 4) {
|
|
137
|
+
if (!wasmExports) throw new Error("libdeflate WASM not initialized. Call initDeflateWasm() first.");
|
|
138
|
+
const maxOutput = Math.max(outputSize, input.length * 2);
|
|
139
|
+
const inputPtr = wasmExports.alloc(input.length);
|
|
140
|
+
const outputPtr = wasmExports.alloc(maxOutput);
|
|
141
|
+
try {
|
|
142
|
+
copyToWasm(inputPtr, input);
|
|
143
|
+
const actualSize = wasmExports.decompress(inputPtr, input.length, outputPtr, maxOutput);
|
|
144
|
+
if (actualSize < 0) throw new Error("libdeflate decompression failed. The input may be corrupt or the output buffer may be too small.");
|
|
145
|
+
return copyFromWasm(outputPtr, actualSize);
|
|
146
|
+
} finally {
|
|
147
|
+
wasmExports.dealloc(inputPtr, input.length);
|
|
148
|
+
wasmExports.dealloc(outputPtr, maxOutput);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Check whether the libdeflate WASM module is initialized and available.
|
|
153
|
+
*
|
|
154
|
+
* @returns `true` if WASM is ready for use.
|
|
155
|
+
*/
|
|
156
|
+
function isAvailable() {
|
|
157
|
+
return available;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Release the WASM module and free associated resources.
|
|
161
|
+
*
|
|
162
|
+
* After calling this, the module must be re-initialized with
|
|
163
|
+
* {@link initDeflateWasm} before further use.
|
|
164
|
+
*/
|
|
165
|
+
function dispose() {
|
|
166
|
+
wasmExports = null;
|
|
167
|
+
available = false;
|
|
168
|
+
initAttempted = false;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* A {@link CompressionEngine} backed by libdeflate compiled to WASM.
|
|
172
|
+
*
|
|
173
|
+
* This provides the fastest compression available in the library,
|
|
174
|
+
* with support for levels 1-12 (libdeflate's native range).
|
|
175
|
+
*
|
|
176
|
+
* The WASM module must be initialized before constructing this class.
|
|
177
|
+
* Use {@link initDeflateWasm} to load the module.
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* ```ts
|
|
181
|
+
* await initDeflateWasm(wasmBytes);
|
|
182
|
+
* const engine = new LibdeflateWasm();
|
|
183
|
+
* const compressed = await engine.compress(data, 6);
|
|
184
|
+
* ```
|
|
185
|
+
*/
|
|
186
|
+
var LibdeflateWasm = class {
|
|
187
|
+
name = "libdeflate-wasm";
|
|
188
|
+
minLevel = 1;
|
|
189
|
+
maxLevel = 12;
|
|
190
|
+
/**
|
|
191
|
+
* Compress data using libdeflate WASM.
|
|
192
|
+
*
|
|
193
|
+
* @param data - Uncompressed input bytes.
|
|
194
|
+
* @param level - Compression level 1-12 (clamped). Default: `6`.
|
|
195
|
+
* @returns Compressed bytes (raw deflate).
|
|
196
|
+
*/
|
|
197
|
+
async compress(data, level = 6) {
|
|
198
|
+
return deflateSync(data, level);
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Decompress raw deflate data using libdeflate WASM.
|
|
202
|
+
*
|
|
203
|
+
* @param data - Compressed bytes (raw deflate).
|
|
204
|
+
* @param outputSize - Expected output size hint. Default: `data.length * 4`.
|
|
205
|
+
* @returns Decompressed bytes.
|
|
206
|
+
*/
|
|
207
|
+
async decompress(data, outputSize) {
|
|
208
|
+
return inflateSync(data, outputSize);
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Release the WASM module resources.
|
|
212
|
+
*/
|
|
213
|
+
dispose() {
|
|
214
|
+
dispose();
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
//#endregion
|
|
219
|
+
Object.defineProperty(exports, 'deflateSync', {
|
|
220
|
+
enumerable: true,
|
|
221
|
+
get: function () {
|
|
222
|
+
return deflateSync;
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
Object.defineProperty(exports, 'isAvailable', {
|
|
226
|
+
enumerable: true,
|
|
227
|
+
get: function () {
|
|
228
|
+
return isAvailable;
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
Object.defineProperty(exports, 'libdeflateWasm_exports', {
|
|
232
|
+
enumerable: true,
|
|
233
|
+
get: function () {
|
|
234
|
+
return libdeflateWasm_exports;
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
//# sourceMappingURL=libdeflateWasm-to2bG6NG.cjs.map
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
//#region src/wasm/loader.ts
|
|
2
|
+
/** Map of module names to their default `.wasm` filenames. */
|
|
3
|
+
const DEFAULT_FILENAMES = {
|
|
4
|
+
libdeflate: "modern_pdf_deflate_bg.wasm",
|
|
5
|
+
png: "modern_pdf_png_bg.wasm",
|
|
6
|
+
ttf: "modern_pdf_ttf_bg.wasm",
|
|
7
|
+
shaping: "modern_pdf_shaping_bg.wasm"
|
|
8
|
+
};
|
|
9
|
+
/** Cached loaded WASM modules. */
|
|
10
|
+
const moduleCache = /* @__PURE__ */ new Map();
|
|
11
|
+
/** Global loader configuration. */
|
|
12
|
+
let globalConfig = {};
|
|
13
|
+
/**
|
|
14
|
+
* Detect the current JavaScript runtime environment.
|
|
15
|
+
*
|
|
16
|
+
* @returns The detected runtime kind.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* const runtime = detectRuntime();
|
|
21
|
+
* if (runtime === 'node') { ... }
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
function detectRuntime() {
|
|
25
|
+
if (typeof globalThis !== "undefined" && "caches" in globalThis && typeof globalThis["HTMLElement"] === "undefined" && typeof globalThis["process"] === "undefined") {
|
|
26
|
+
if (typeof globalThis["navigator"] === "object") {
|
|
27
|
+
if ((globalThis.navigator?.userAgent ?? "").includes("Cloudflare-Workers")) return "workerd";
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (typeof globalThis["Deno"] === "object") return "deno";
|
|
31
|
+
if (typeof globalThis["Bun"] === "object") return "bun";
|
|
32
|
+
if (typeof globalThis["process"] === "object" && typeof globalThis["process"]?.["versions"] === "object" && typeof (globalThis["process"]?.["versions"])?.["node"] === "string") return "node";
|
|
33
|
+
if (typeof globalThis.document === "object" || typeof globalThis.window === "object") return "browser";
|
|
34
|
+
return "unknown";
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Get the current WASM loader configuration (for testing/inspection).
|
|
38
|
+
*/
|
|
39
|
+
function getWasmLoaderConfig() {
|
|
40
|
+
return globalConfig;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Load WASM bytes using `fetch()` (works in browsers, Deno, Bun, and
|
|
44
|
+
* Node.js 18+).
|
|
45
|
+
*
|
|
46
|
+
* @param url - URL or path to the `.wasm` file.
|
|
47
|
+
* @returns The raw WASM bytes.
|
|
48
|
+
*/
|
|
49
|
+
async function loadViaFetch(url) {
|
|
50
|
+
const response = await fetch(url);
|
|
51
|
+
if (!response.ok) throw new Error(`Failed to fetch WASM module: ${response.status} ${response.statusText} (${url})`);
|
|
52
|
+
const buffer = await response.arrayBuffer();
|
|
53
|
+
return new Uint8Array(buffer);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Load WASM bytes from the filesystem using Node.js `fs/promises`.
|
|
57
|
+
*
|
|
58
|
+
* Uses dynamic import to avoid top-level CJS dependency.
|
|
59
|
+
*
|
|
60
|
+
* @param filePath - Absolute or relative filesystem path.
|
|
61
|
+
* @returns The raw WASM bytes.
|
|
62
|
+
*/
|
|
63
|
+
async function loadViaNodeFs(filePath) {
|
|
64
|
+
try {
|
|
65
|
+
const { readFile } = await import("node:fs/promises");
|
|
66
|
+
const buffer = await readFile(filePath);
|
|
67
|
+
return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
68
|
+
} catch (err) {
|
|
69
|
+
throw new Error(`Failed to load WASM module from filesystem: ${filePath} -- ${err instanceof Error ? err.message : String(err)}`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Resolve the path/URL for a WASM module given its name.
|
|
74
|
+
*
|
|
75
|
+
* Resolution order:
|
|
76
|
+
* 1. `config.modulePaths[name]` (explicit per-module path)
|
|
77
|
+
* 2. `config.basePath + DEFAULT_FILENAMES[name]` (base path + default name)
|
|
78
|
+
* 3. Relative to the package directory (best-effort)
|
|
79
|
+
*
|
|
80
|
+
* @param name - Module name (e.g. `'libdeflate'`).
|
|
81
|
+
* @param config - Current loader config.
|
|
82
|
+
* @param runtime - Detected runtime.
|
|
83
|
+
* @returns Resolved path or URL string.
|
|
84
|
+
*/
|
|
85
|
+
function resolveModulePath(name, config, runtime) {
|
|
86
|
+
if (config.modulePaths?.[name]) return config.modulePaths[name];
|
|
87
|
+
const filename = DEFAULT_FILENAMES[name];
|
|
88
|
+
if (!filename) throw new Error(`Unknown WASM module: "${name}". Known modules: ${Object.keys(DEFAULT_FILENAMES).join(", ")}`);
|
|
89
|
+
if (config.basePath) return `${config.basePath.endsWith("/") ? config.basePath : `${config.basePath}/`}${filename}`;
|
|
90
|
+
switch (runtime) {
|
|
91
|
+
case "browser": return `/wasm/${filename}`;
|
|
92
|
+
case "node":
|
|
93
|
+
case "bun": try {
|
|
94
|
+
return new URL(`../wasm/${name}/pkg/${filename}`, import.meta.url).pathname;
|
|
95
|
+
} catch {
|
|
96
|
+
return `./wasm/${name}/pkg/${filename}`;
|
|
97
|
+
}
|
|
98
|
+
case "deno": try {
|
|
99
|
+
return new URL(`../wasm/${name}/pkg/${filename}`, import.meta.url).href;
|
|
100
|
+
} catch {
|
|
101
|
+
return `./wasm/${name}/pkg/${filename}`;
|
|
102
|
+
}
|
|
103
|
+
case "workerd": throw new Error(`Cannot auto-resolve WASM path in Workers/Edge runtime. Provide WASM bytes via configureWasmLoader({ moduleBytes: { ${name}: bytes } }) or use modulePaths configuration.`);
|
|
104
|
+
default: return `./wasm/${name}/pkg/${filename}`;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Load a WASM module by name.
|
|
109
|
+
*
|
|
110
|
+
* Returns the raw `.wasm` bytes, suitable for passing to
|
|
111
|
+
* `WebAssembly.compile()` or `WebAssembly.instantiate()`.
|
|
112
|
+
*
|
|
113
|
+
* Results are cached -- subsequent calls for the same module name
|
|
114
|
+
* return the cached bytes without re-fetching.
|
|
115
|
+
*
|
|
116
|
+
* @param name - Module name. One of: `'libdeflate'`, `'png'`, `'ttf'`, `'shaping'`.
|
|
117
|
+
* @returns The raw WASM bytes.
|
|
118
|
+
* @throws If the module cannot be loaded in the current runtime.
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* ```ts
|
|
122
|
+
* // Auto-detect runtime and load
|
|
123
|
+
* const wasmBytes = await loadWasmModule('libdeflate');
|
|
124
|
+
*
|
|
125
|
+
* // Pre-configure for Workers
|
|
126
|
+
* configureWasmLoader({ moduleBytes: { libdeflate: bundledBytes } });
|
|
127
|
+
* const bytes = await loadWasmModule('libdeflate');
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
async function loadWasmModule(name) {
|
|
131
|
+
const cached = moduleCache.get(name);
|
|
132
|
+
if (cached) return cached;
|
|
133
|
+
if (globalConfig.moduleBytes?.[name]) {
|
|
134
|
+
const bytes = globalConfig.moduleBytes[name];
|
|
135
|
+
moduleCache.set(name, bytes);
|
|
136
|
+
return bytes;
|
|
137
|
+
}
|
|
138
|
+
const runtime = detectRuntime();
|
|
139
|
+
const modulePath = resolveModulePath(name, globalConfig, runtime);
|
|
140
|
+
let bytes;
|
|
141
|
+
switch (runtime) {
|
|
142
|
+
case "node":
|
|
143
|
+
if (modulePath.startsWith("http://") || modulePath.startsWith("https://")) bytes = await loadViaFetch(modulePath);
|
|
144
|
+
else bytes = await loadViaNodeFs(modulePath);
|
|
145
|
+
break;
|
|
146
|
+
case "deno":
|
|
147
|
+
case "bun":
|
|
148
|
+
case "browser":
|
|
149
|
+
bytes = await loadViaFetch(modulePath);
|
|
150
|
+
break;
|
|
151
|
+
case "workerd": throw new Error(`WASM module "${name}" cannot be auto-loaded in Workers/Edge. Provide bytes via configureWasmLoader().`);
|
|
152
|
+
default:
|
|
153
|
+
bytes = await loadViaFetch(modulePath);
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
moduleCache.set(name, bytes);
|
|
157
|
+
return bytes;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
//#endregion
|
|
161
|
+
export { getWasmLoaderConfig, loadWasmModule };
|
|
162
|
+
//# sourceMappingURL=loader-D9LTYmrX.mjs.map
|