@open-artifact/wasm-export 0.0.0-stage → 0.2.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 +202 -0
- package/NOTICE +5 -0
- package/README.md +5 -2
- package/generated/bytes.d.ts +3 -0
- package/generated/bytes.js +2 -0
- package/generated/open_artifact_export.d.ts +99 -0
- package/generated/open_artifact_export.js +402 -0
- package/package.json +34 -4
- package/src/index.d.ts +42 -0
- package/src/index.js +146 -0
- package/src/run.d.ts +14 -0
- package/src/run.js +108 -0
- package/src/types.d.ts +110 -0
- package/src/types.js +5 -0
- package/src/worker.d.ts +8 -0
- package/src/worker.js +19 -0
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
// Generated by scripts/build-wasm.mjs from crates/open-artifact-export. Do not edit.
|
|
2
|
+
/**
|
|
3
|
+
* A PDF being written.
|
|
4
|
+
*/
|
|
5
|
+
export class PdfWriter {
|
|
6
|
+
__destroy_into_raw() {
|
|
7
|
+
const ptr = this.__wbg_ptr;
|
|
8
|
+
this.__wbg_ptr = 0;
|
|
9
|
+
PdfWriterFinalization.unregister(this);
|
|
10
|
+
return ptr;
|
|
11
|
+
}
|
|
12
|
+
free() {
|
|
13
|
+
const ptr = this.__destroy_into_raw();
|
|
14
|
+
wasm.__wbg_pdfwriter_free(ptr, 0);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Adds a page: a JPEG of `pixel_width` × `pixel_height` filling a page
|
|
18
|
+
* of `width` × `height` pt. `texts` holds the text-layer runs joined by
|
|
19
|
+
* `\0`, `text_boxes` four numbers per run (x, baseline, size, width);
|
|
20
|
+
* `uris` and `link_boxes` (x, y, width, height) the same for links.
|
|
21
|
+
* @param {Uint8Array} jpeg
|
|
22
|
+
* @param {number} pixel_width
|
|
23
|
+
* @param {number} pixel_height
|
|
24
|
+
* @param {number} width
|
|
25
|
+
* @param {number} height
|
|
26
|
+
* @param {string} texts
|
|
27
|
+
* @param {Float64Array} text_boxes
|
|
28
|
+
* @param {string} uris
|
|
29
|
+
* @param {Float64Array} link_boxes
|
|
30
|
+
* @param {string | null} [bookmark]
|
|
31
|
+
*/
|
|
32
|
+
addPage(jpeg, pixel_width, pixel_height, width, height, texts, text_boxes, uris, link_boxes, bookmark) {
|
|
33
|
+
const ptr0 = passArray8ToWasm0(jpeg, wasm.__wbindgen_malloc);
|
|
34
|
+
const len0 = WASM_VECTOR_LEN;
|
|
35
|
+
const ptr1 = passStringToWasm0(texts, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
36
|
+
const len1 = WASM_VECTOR_LEN;
|
|
37
|
+
const ptr2 = passArrayF64ToWasm0(text_boxes, wasm.__wbindgen_malloc);
|
|
38
|
+
const len2 = WASM_VECTOR_LEN;
|
|
39
|
+
const ptr3 = passStringToWasm0(uris, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
40
|
+
const len3 = WASM_VECTOR_LEN;
|
|
41
|
+
const ptr4 = passArrayF64ToWasm0(link_boxes, wasm.__wbindgen_malloc);
|
|
42
|
+
const len4 = WASM_VECTOR_LEN;
|
|
43
|
+
var ptr5 = isLikeNone(bookmark) ? 0 : passStringToWasm0(bookmark, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
44
|
+
var len5 = WASM_VECTOR_LEN;
|
|
45
|
+
const ret = wasm.pdfwriter_addPage(this.__wbg_ptr, ptr0, len0, pixel_width, pixel_height, width, height, ptr1, len1, ptr2, len2, ptr3, len3, ptr4, len4, ptr5, len5);
|
|
46
|
+
if (ret[1]) {
|
|
47
|
+
throw takeFromExternrefTable0(ret[0]);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* The document's bytes. `date_ms` is the creation time (ms since 1970).
|
|
52
|
+
* @param {string | null | undefined} title
|
|
53
|
+
* @param {number} date_ms
|
|
54
|
+
* @returns {Uint8Array}
|
|
55
|
+
*/
|
|
56
|
+
finish(title, date_ms) {
|
|
57
|
+
var ptr0 = isLikeNone(title) ? 0 : passStringToWasm0(title, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
58
|
+
var len0 = WASM_VECTOR_LEN;
|
|
59
|
+
const ret = wasm.pdfwriter_finish(this.__wbg_ptr, ptr0, len0, date_ms);
|
|
60
|
+
if (ret[3]) {
|
|
61
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
62
|
+
}
|
|
63
|
+
var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
|
|
64
|
+
wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
|
|
65
|
+
return v2;
|
|
66
|
+
}
|
|
67
|
+
constructor() {
|
|
68
|
+
const ret = wasm.pdfwriter_new();
|
|
69
|
+
this.__wbg_ptr = ret >>> 0;
|
|
70
|
+
PdfWriterFinalization.register(this, this.__wbg_ptr, this);
|
|
71
|
+
return this;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if (Symbol.dispose) PdfWriter.prototype[Symbol.dispose] = PdfWriter.prototype.free;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* A zip archive being written.
|
|
78
|
+
*/
|
|
79
|
+
export class ZipWriter {
|
|
80
|
+
__destroy_into_raw() {
|
|
81
|
+
const ptr = this.__wbg_ptr;
|
|
82
|
+
this.__wbg_ptr = 0;
|
|
83
|
+
ZipWriterFinalization.unregister(this);
|
|
84
|
+
return ptr;
|
|
85
|
+
}
|
|
86
|
+
free() {
|
|
87
|
+
const ptr = this.__destroy_into_raw();
|
|
88
|
+
wasm.__wbg_zipwriter_free(ptr, 0);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Adds an entry, deflated at `level` (0 stores it). False for a name
|
|
92
|
+
* already in the archive (the entry is dropped).
|
|
93
|
+
* @param {string} name
|
|
94
|
+
* @param {Uint8Array} data
|
|
95
|
+
* @param {number} level
|
|
96
|
+
* @returns {boolean}
|
|
97
|
+
*/
|
|
98
|
+
add(name, data, level) {
|
|
99
|
+
const ptr0 = passStringToWasm0(name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
100
|
+
const len0 = WASM_VECTOR_LEN;
|
|
101
|
+
const ptr1 = passArray8ToWasm0(data, wasm.__wbindgen_malloc);
|
|
102
|
+
const len1 = WASM_VECTOR_LEN;
|
|
103
|
+
const ret = wasm.zipwriter_add(this.__wbg_ptr, ptr0, len0, ptr1, len1, level);
|
|
104
|
+
if (ret[2]) {
|
|
105
|
+
throw takeFromExternrefTable0(ret[1]);
|
|
106
|
+
}
|
|
107
|
+
return ret[0] !== 0;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* The archive's bytes.
|
|
111
|
+
* @returns {Uint8Array}
|
|
112
|
+
*/
|
|
113
|
+
finish() {
|
|
114
|
+
const ret = wasm.zipwriter_finish(this.__wbg_ptr);
|
|
115
|
+
if (ret[3]) {
|
|
116
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
117
|
+
}
|
|
118
|
+
var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
|
|
119
|
+
wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
|
|
120
|
+
return v1;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* A new archive whose entries carry the DOS `time` and `date`.
|
|
124
|
+
* @param {number} time
|
|
125
|
+
* @param {number} date
|
|
126
|
+
*/
|
|
127
|
+
constructor(time, date) {
|
|
128
|
+
const ret = wasm.zipwriter_new(time, date);
|
|
129
|
+
this.__wbg_ptr = ret >>> 0;
|
|
130
|
+
ZipWriterFinalization.register(this, this.__wbg_ptr, this);
|
|
131
|
+
return this;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
if (Symbol.dispose) ZipWriter.prototype[Symbol.dispose] = ZipWriter.prototype.free;
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* CRC-32 (IEEE) of bytes.
|
|
138
|
+
* @param {Uint8Array} bytes
|
|
139
|
+
* @returns {number}
|
|
140
|
+
*/
|
|
141
|
+
export function crc32(bytes) {
|
|
142
|
+
const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc);
|
|
143
|
+
const len0 = WASM_VECTOR_LEN;
|
|
144
|
+
const ret = wasm.crc32(ptr0, len0);
|
|
145
|
+
return ret >>> 0;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* The PNG re-compressed at `level` (1–10); empty when it cannot be read or
|
|
150
|
+
* would not get smaller (the caller keeps its bytes).
|
|
151
|
+
* @param {Uint8Array} bytes
|
|
152
|
+
* @param {number} level
|
|
153
|
+
* @returns {Uint8Array}
|
|
154
|
+
*/
|
|
155
|
+
export function optimizePng(bytes, level) {
|
|
156
|
+
const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc);
|
|
157
|
+
const len0 = WASM_VECTOR_LEN;
|
|
158
|
+
const ret = wasm.optimizePng(ptr0, len0, level);
|
|
159
|
+
var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
|
|
160
|
+
wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
|
|
161
|
+
return v2;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function __wbg_get_imports() {
|
|
165
|
+
const import0 = {
|
|
166
|
+
__proto__: null,
|
|
167
|
+
__wbg_Error_2e59b1b37a9a34c3: function(arg0, arg1) {
|
|
168
|
+
const ret = Error(getStringFromWasm0(arg0, arg1));
|
|
169
|
+
return ret;
|
|
170
|
+
},
|
|
171
|
+
__wbg___wbindgen_throw_81fc77679af83bc6: function(arg0, arg1) {
|
|
172
|
+
throw new Error(getStringFromWasm0(arg0, arg1));
|
|
173
|
+
},
|
|
174
|
+
__wbindgen_init_externref_table: function() {
|
|
175
|
+
const table = wasm.__wbindgen_externrefs;
|
|
176
|
+
const offset = table.grow(4);
|
|
177
|
+
table.set(0, undefined);
|
|
178
|
+
table.set(offset + 0, undefined);
|
|
179
|
+
table.set(offset + 1, null);
|
|
180
|
+
table.set(offset + 2, true);
|
|
181
|
+
table.set(offset + 3, false);
|
|
182
|
+
},
|
|
183
|
+
};
|
|
184
|
+
return {
|
|
185
|
+
__proto__: null,
|
|
186
|
+
"./open_artifact_export_bg.js": import0,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const PdfWriterFinalization = (typeof FinalizationRegistry === 'undefined')
|
|
191
|
+
? { register: () => {}, unregister: () => {} }
|
|
192
|
+
: new FinalizationRegistry(ptr => wasm.__wbg_pdfwriter_free(ptr >>> 0, 1));
|
|
193
|
+
const ZipWriterFinalization = (typeof FinalizationRegistry === 'undefined')
|
|
194
|
+
? { register: () => {}, unregister: () => {} }
|
|
195
|
+
: new FinalizationRegistry(ptr => wasm.__wbg_zipwriter_free(ptr >>> 0, 1));
|
|
196
|
+
|
|
197
|
+
function getArrayU8FromWasm0(ptr, len) {
|
|
198
|
+
ptr = ptr >>> 0;
|
|
199
|
+
return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
let cachedFloat64ArrayMemory0 = null;
|
|
203
|
+
function getFloat64ArrayMemory0() {
|
|
204
|
+
if (cachedFloat64ArrayMemory0 === null || cachedFloat64ArrayMemory0.byteLength === 0) {
|
|
205
|
+
cachedFloat64ArrayMemory0 = new Float64Array(wasm.memory.buffer);
|
|
206
|
+
}
|
|
207
|
+
return cachedFloat64ArrayMemory0;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function getStringFromWasm0(ptr, len) {
|
|
211
|
+
ptr = ptr >>> 0;
|
|
212
|
+
return decodeText(ptr, len);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
let cachedUint8ArrayMemory0 = null;
|
|
216
|
+
function getUint8ArrayMemory0() {
|
|
217
|
+
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
|
|
218
|
+
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
|
|
219
|
+
}
|
|
220
|
+
return cachedUint8ArrayMemory0;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function isLikeNone(x) {
|
|
224
|
+
return x === undefined || x === null;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function passArray8ToWasm0(arg, malloc) {
|
|
228
|
+
const ptr = malloc(arg.length * 1, 1) >>> 0;
|
|
229
|
+
getUint8ArrayMemory0().set(arg, ptr / 1);
|
|
230
|
+
WASM_VECTOR_LEN = arg.length;
|
|
231
|
+
return ptr;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function passArrayF64ToWasm0(arg, malloc) {
|
|
235
|
+
const ptr = malloc(arg.length * 8, 8) >>> 0;
|
|
236
|
+
getFloat64ArrayMemory0().set(arg, ptr / 8);
|
|
237
|
+
WASM_VECTOR_LEN = arg.length;
|
|
238
|
+
return ptr;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function passStringToWasm0(arg, malloc, realloc) {
|
|
242
|
+
if (realloc === undefined) {
|
|
243
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
244
|
+
const ptr = malloc(buf.length, 1) >>> 0;
|
|
245
|
+
getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
|
|
246
|
+
WASM_VECTOR_LEN = buf.length;
|
|
247
|
+
return ptr;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
let len = arg.length;
|
|
251
|
+
let ptr = malloc(len, 1) >>> 0;
|
|
252
|
+
|
|
253
|
+
const mem = getUint8ArrayMemory0();
|
|
254
|
+
|
|
255
|
+
let offset = 0;
|
|
256
|
+
|
|
257
|
+
for (; offset < len; offset++) {
|
|
258
|
+
const code = arg.charCodeAt(offset);
|
|
259
|
+
if (code > 0x7F) break;
|
|
260
|
+
mem[ptr + offset] = code;
|
|
261
|
+
}
|
|
262
|
+
if (offset !== len) {
|
|
263
|
+
if (offset !== 0) {
|
|
264
|
+
arg = arg.slice(offset);
|
|
265
|
+
}
|
|
266
|
+
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
|
|
267
|
+
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
|
|
268
|
+
const ret = cachedTextEncoder.encodeInto(arg, view);
|
|
269
|
+
|
|
270
|
+
offset += ret.written;
|
|
271
|
+
ptr = realloc(ptr, len, offset, 1) >>> 0;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
WASM_VECTOR_LEN = offset;
|
|
275
|
+
return ptr;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function takeFromExternrefTable0(idx) {
|
|
279
|
+
const value = wasm.__wbindgen_externrefs.get(idx);
|
|
280
|
+
wasm.__externref_table_dealloc(idx);
|
|
281
|
+
return value;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
285
|
+
cachedTextDecoder.decode();
|
|
286
|
+
const MAX_SAFARI_DECODE_BYTES = 2146435072;
|
|
287
|
+
let numBytesDecoded = 0;
|
|
288
|
+
function decodeText(ptr, len) {
|
|
289
|
+
numBytesDecoded += len;
|
|
290
|
+
if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
|
|
291
|
+
cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
292
|
+
cachedTextDecoder.decode();
|
|
293
|
+
numBytesDecoded = len;
|
|
294
|
+
}
|
|
295
|
+
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
const cachedTextEncoder = new TextEncoder();
|
|
299
|
+
|
|
300
|
+
if (!('encodeInto' in cachedTextEncoder)) {
|
|
301
|
+
cachedTextEncoder.encodeInto = function (arg, view) {
|
|
302
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
303
|
+
view.set(buf);
|
|
304
|
+
return {
|
|
305
|
+
read: arg.length,
|
|
306
|
+
written: buf.length
|
|
307
|
+
};
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
let WASM_VECTOR_LEN = 0;
|
|
312
|
+
|
|
313
|
+
let wasmModule, wasm;
|
|
314
|
+
function __wbg_finalize_init(instance, module) {
|
|
315
|
+
wasm = instance.exports;
|
|
316
|
+
wasmModule = module;
|
|
317
|
+
cachedFloat64ArrayMemory0 = null;
|
|
318
|
+
cachedUint8ArrayMemory0 = null;
|
|
319
|
+
wasm.__wbindgen_start();
|
|
320
|
+
return wasm;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
async function __wbg_load(module, imports) {
|
|
324
|
+
if (typeof Response === 'function' && module instanceof Response) {
|
|
325
|
+
if (typeof WebAssembly.instantiateStreaming === 'function') {
|
|
326
|
+
try {
|
|
327
|
+
return await WebAssembly.instantiateStreaming(module, imports);
|
|
328
|
+
} catch (e) {
|
|
329
|
+
const validResponse = module.ok && expectedResponseType(module.type);
|
|
330
|
+
|
|
331
|
+
if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
|
|
332
|
+
console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
|
|
333
|
+
|
|
334
|
+
} else { throw e; }
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
const bytes = await module.arrayBuffer();
|
|
339
|
+
return await WebAssembly.instantiate(bytes, imports);
|
|
340
|
+
} else {
|
|
341
|
+
const instance = await WebAssembly.instantiate(module, imports);
|
|
342
|
+
|
|
343
|
+
if (instance instanceof WebAssembly.Instance) {
|
|
344
|
+
return { instance, module };
|
|
345
|
+
} else {
|
|
346
|
+
return instance;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
function expectedResponseType(type) {
|
|
351
|
+
switch (type) {
|
|
352
|
+
case 'basic': case 'cors': case 'default': return true;
|
|
353
|
+
}
|
|
354
|
+
return false;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
function initSync(module) {
|
|
359
|
+
if (wasm !== undefined) return wasm;
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
if (module !== undefined) {
|
|
363
|
+
if (Object.getPrototypeOf(module) === Object.prototype) {
|
|
364
|
+
({module} = module)
|
|
365
|
+
} else {
|
|
366
|
+
console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
const imports = __wbg_get_imports();
|
|
371
|
+
if (!(module instanceof WebAssembly.Module)) {
|
|
372
|
+
module = new WebAssembly.Module(module);
|
|
373
|
+
}
|
|
374
|
+
const instance = new WebAssembly.Instance(module, imports);
|
|
375
|
+
return __wbg_finalize_init(instance, module);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
async function __wbg_init(module_or_path) {
|
|
379
|
+
if (wasm !== undefined) return wasm;
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
if (module_or_path !== undefined) {
|
|
383
|
+
if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
|
|
384
|
+
({module_or_path} = module_or_path)
|
|
385
|
+
} else {
|
|
386
|
+
console.warn('using deprecated parameters for the initialization function; pass a single object instead')
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
const imports = __wbg_get_imports();
|
|
392
|
+
|
|
393
|
+
if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
|
|
394
|
+
module_or_path = fetch(module_or_path);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
const { instance, module } = await __wbg_load(await module_or_path, imports);
|
|
398
|
+
|
|
399
|
+
return __wbg_finalize_init(instance, module);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
export { initSync, __wbg_init as default };
|
package/package.json
CHANGED
|
@@ -1,6 +1,36 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-artifact/wasm-export",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "crates/open-artifact-export compiled to WebAssembly (generated/, built by scripts/build-wasm.mjs).",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./src/index.d.ts",
|
|
9
|
+
"default": "./src/index.js"
|
|
10
|
+
},
|
|
11
|
+
"./package.json": "./package.json"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"src",
|
|
15
|
+
"generated",
|
|
16
|
+
"LICENSE",
|
|
17
|
+
"NOTICE"
|
|
18
|
+
],
|
|
19
|
+
"license": "Apache-2.0",
|
|
20
|
+
"author": {
|
|
21
|
+
"name": "Jim Liu 宝玉",
|
|
22
|
+
"url": "https://github.com/JimLiu"
|
|
23
|
+
},
|
|
24
|
+
"homepage": "https://github.com/JimLiu/open-artifact#readme",
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/JimLiu/open-artifact.git",
|
|
28
|
+
"directory": "packages/wasm-export"
|
|
29
|
+
},
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/JimLiu/open-artifact/issues"
|
|
32
|
+
},
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
}
|
|
36
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Export packing for the editors: zip archives (deflate), PDFs of page
|
|
3
|
+
* pictures and lossless PNG re-compression, done by `crates/open-artifact-export`
|
|
4
|
+
* compiled to WebAssembly.
|
|
5
|
+
*
|
|
6
|
+
* In a page the work runs in a Worker (started on first use, stopped when
|
|
7
|
+
* idle) so packing never blocks the page; results come back transferred.
|
|
8
|
+
* Where there are no Workers (Node tests, the CLI), or the Worker cannot
|
|
9
|
+
* start, the same code runs in-thread. Either way the module's bytes are
|
|
10
|
+
* embedded: nothing to serve beyond the Worker script itself.
|
|
11
|
+
*
|
|
12
|
+
* Data may be given as bytes, text or Blobs; Blobs travel to the Worker by
|
|
13
|
+
* reference and are read there, so the page never copies a picture.
|
|
14
|
+
*/
|
|
15
|
+
import type { PdfOptions, PdfPage, PngOptions, Progress, ZipEntry, ZipOptions } from './types.ts';
|
|
16
|
+
export type { ExportBytes, PdfLink, PdfOptions, PdfPage, PdfTextRun, PngOptions, Progress, ZipEntry, ZipOptions } from './types.ts';
|
|
17
|
+
/** CSS pixels to points (96 px = 72 pt, as browsers print). */
|
|
18
|
+
export declare const PT_PER_PX = 0.75;
|
|
19
|
+
/** Where jobs run: in a Worker, or in-thread (no Workers here, or the Worker failed to start). */
|
|
20
|
+
export declare function exportBackend(): 'worker' | 'inline';
|
|
21
|
+
/**
|
|
22
|
+
* The bytes of a zip holding the entries (a later entry with a name already
|
|
23
|
+
* used is dropped). Entries are deflated unless stored on request or by
|
|
24
|
+
* their name (pictures, fonts and other compressed files).
|
|
25
|
+
*/
|
|
26
|
+
export declare function packZip(entries: readonly ZipEntry[], options?: ZipOptions & {
|
|
27
|
+
onProgress?: Progress;
|
|
28
|
+
}): Promise<Uint8Array>;
|
|
29
|
+
/**
|
|
30
|
+
* The bytes of a PDF with one picture page per entry: the JPEG fills the
|
|
31
|
+
* page, with an invisible text layer, links and an outline entry where the
|
|
32
|
+
* page has them.
|
|
33
|
+
*/
|
|
34
|
+
export declare function buildPdf(pages: readonly PdfPage[], options?: PdfOptions & {
|
|
35
|
+
onProgress?: Progress;
|
|
36
|
+
}): Promise<Uint8Array>;
|
|
37
|
+
/**
|
|
38
|
+
* A PNG re-compressed losslessly (fewer channels, a palette where few
|
|
39
|
+
* colors, filters per row, best deflate); the bytes as they are when that
|
|
40
|
+
* is not smaller or the picture is not one it reads.
|
|
41
|
+
*/
|
|
42
|
+
export declare function optimizePng(data: Uint8Array | Blob, options?: PngOptions): Promise<Uint8Array>;
|
package/src/index.js
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Export packing for the editors: zip archives (deflate), PDFs of page
|
|
3
|
+
* pictures and lossless PNG re-compression, done by `crates/open-artifact-export`
|
|
4
|
+
* compiled to WebAssembly.
|
|
5
|
+
*
|
|
6
|
+
* In a page the work runs in a Worker (started on first use, stopped when
|
|
7
|
+
* idle) so packing never blocks the page; results come back transferred.
|
|
8
|
+
* Where there are no Workers (Node tests, the CLI), or the Worker cannot
|
|
9
|
+
* start, the same code runs in-thread. Either way the module's bytes are
|
|
10
|
+
* embedded: nothing to serve beyond the Worker script itself.
|
|
11
|
+
*
|
|
12
|
+
* Data may be given as bytes, text or Blobs; Blobs travel to the Worker by
|
|
13
|
+
* reference and are read there, so the page never copies a picture.
|
|
14
|
+
*/
|
|
15
|
+
/** CSS pixels to points (96 px = 72 pt, as browsers print). */
|
|
16
|
+
export const PT_PER_PX = 0.75;
|
|
17
|
+
/** How long an idle Worker is kept (its memory is released when it stops), ms. */
|
|
18
|
+
const IDLE_MS = 20_000;
|
|
19
|
+
let worker = null;
|
|
20
|
+
/** The Worker could not start here: run in-thread from now on. */
|
|
21
|
+
let workerBroken = false;
|
|
22
|
+
let idleTimer;
|
|
23
|
+
let nextId = 1;
|
|
24
|
+
const pending = new Map();
|
|
25
|
+
/** Where jobs run: in a Worker, or in-thread (no Workers here, or the Worker failed to start). */
|
|
26
|
+
export function exportBackend() {
|
|
27
|
+
return typeof Worker === 'undefined' || workerBroken ? 'inline' : 'worker';
|
|
28
|
+
}
|
|
29
|
+
/** Runs a job in this thread. */
|
|
30
|
+
async function runInline(job, progress) {
|
|
31
|
+
const { runJob } = await import("./run.js");
|
|
32
|
+
return runJob(job, progress);
|
|
33
|
+
}
|
|
34
|
+
function stopWorker() {
|
|
35
|
+
clearTimeout(idleTimer);
|
|
36
|
+
worker?.terminate();
|
|
37
|
+
worker = null;
|
|
38
|
+
}
|
|
39
|
+
/** Stops the Worker once nothing is pending for a while. */
|
|
40
|
+
function scheduleIdle() {
|
|
41
|
+
clearTimeout(idleTimer);
|
|
42
|
+
if (pending.size === 0)
|
|
43
|
+
idleTimer = setTimeout(stopWorker, IDLE_MS);
|
|
44
|
+
}
|
|
45
|
+
/** The Worker failed to load: run what it held in-thread. */
|
|
46
|
+
function abandonWorker() {
|
|
47
|
+
workerBroken = true;
|
|
48
|
+
stopWorker();
|
|
49
|
+
const held = [...pending.values()];
|
|
50
|
+
pending.clear();
|
|
51
|
+
for (const job of held)
|
|
52
|
+
runInline(job.job, job.progress).then(job.resolve, job.reject);
|
|
53
|
+
}
|
|
54
|
+
function onMessage(event) {
|
|
55
|
+
const message = event.data;
|
|
56
|
+
const job = pending.get(message.id);
|
|
57
|
+
if (!job)
|
|
58
|
+
return;
|
|
59
|
+
if (message.type === 'progress') {
|
|
60
|
+
job.progress?.(message.done, message.total);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
pending.delete(message.id);
|
|
64
|
+
if (message.type === 'done')
|
|
65
|
+
job.resolve(message.bytes);
|
|
66
|
+
else {
|
|
67
|
+
// A trapped module is not reused.
|
|
68
|
+
if (message.fatal)
|
|
69
|
+
stopWorker();
|
|
70
|
+
job.reject(new Error(message.message));
|
|
71
|
+
}
|
|
72
|
+
scheduleIdle();
|
|
73
|
+
}
|
|
74
|
+
/** The Worker, started on first use; `null` where there is none. */
|
|
75
|
+
function getWorker() {
|
|
76
|
+
if (worker)
|
|
77
|
+
return worker;
|
|
78
|
+
if (workerBroken || typeof Worker === 'undefined')
|
|
79
|
+
return null;
|
|
80
|
+
try {
|
|
81
|
+
worker = new Worker(new URL('./worker.js', import.meta.url), { type: 'module', name: 'open-artifact-export' });
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
workerBroken = true;
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
worker.addEventListener('message', onMessage);
|
|
88
|
+
worker.addEventListener('error', (event) => {
|
|
89
|
+
event.preventDefault();
|
|
90
|
+
abandonWorker();
|
|
91
|
+
});
|
|
92
|
+
return worker;
|
|
93
|
+
}
|
|
94
|
+
/** Runs a job in the Worker, or in-thread without one. */
|
|
95
|
+
function run(job, progress) {
|
|
96
|
+
const target = getWorker();
|
|
97
|
+
if (!target)
|
|
98
|
+
return runInline(job, progress);
|
|
99
|
+
clearTimeout(idleTimer);
|
|
100
|
+
return new Promise((resolve, reject) => {
|
|
101
|
+
const id = nextId++;
|
|
102
|
+
pending.set(id, { job, resolve, reject, progress });
|
|
103
|
+
try {
|
|
104
|
+
target.postMessage({ id, job });
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
pending.delete(id);
|
|
108
|
+
reject(error instanceof Error ? error : new Error(String(error)));
|
|
109
|
+
scheduleIdle();
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
/** DOS time and date of a moment (local time, as zip tools show it). */
|
|
114
|
+
function dosTime(date) {
|
|
115
|
+
return {
|
|
116
|
+
time: (date.getHours() << 11) | (date.getMinutes() << 5) | Math.floor(date.getSeconds() / 2),
|
|
117
|
+
date: ((Math.min(2107, Math.max(1980, date.getFullYear())) - 1980) << 9) | ((date.getMonth() + 1) << 5) | date.getDate(),
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* The bytes of a zip holding the entries (a later entry with a name already
|
|
122
|
+
* used is dropped). Entries are deflated unless stored on request or by
|
|
123
|
+
* their name (pictures, fonts and other compressed files).
|
|
124
|
+
*/
|
|
125
|
+
export function packZip(entries, options = {}) {
|
|
126
|
+
const { time, date } = dosTime(options.date ?? new Date());
|
|
127
|
+
const level = Math.max(1, Math.min(10, Math.round(options.level ?? 6)));
|
|
128
|
+
return run({ kind: 'zip', entries, time, date, level, optimizePng: options.optimizePng ?? false }, options.onProgress);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* The bytes of a PDF with one picture page per entry: the JPEG fills the
|
|
132
|
+
* page, with an invisible text layer, links and an outline entry where the
|
|
133
|
+
* page has them.
|
|
134
|
+
*/
|
|
135
|
+
export function buildPdf(pages, options = {}) {
|
|
136
|
+
return run({ kind: 'pdf', pages, title: options.title, date: (options.date ?? new Date()).getTime() }, options.onProgress);
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* A PNG re-compressed losslessly (fewer channels, a palette where few
|
|
140
|
+
* colors, filters per row, best deflate); the bytes as they are when that
|
|
141
|
+
* is not smaller or the picture is not one it reads.
|
|
142
|
+
*/
|
|
143
|
+
export function optimizePng(data, options = {}) {
|
|
144
|
+
const level = Math.max(1, Math.min(10, Math.round(options.level ?? 9)));
|
|
145
|
+
return run({ kind: 'png', data, level }, undefined);
|
|
146
|
+
}
|
package/src/run.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runs export jobs on the WebAssembly module (`crates/open-artifact-export`). The
|
|
3
|
+
* Worker runs them off the page's thread; where there are no Workers (Node
|
|
4
|
+
* tests, the CLI) the client runs them here in-thread.
|
|
5
|
+
*
|
|
6
|
+
* The module's bytes are embedded, so it instantiates synchronously
|
|
7
|
+
* wherever this runs.
|
|
8
|
+
*/
|
|
9
|
+
import * as glue from '../generated/open_artifact_export.js';
|
|
10
|
+
import type { Job, Progress } from './types.ts';
|
|
11
|
+
/** The module's exports, instantiating it on the first call. */
|
|
12
|
+
export declare function exportModule(): Omit<typeof glue, 'default' | 'initSync'>;
|
|
13
|
+
/** Runs a job, reporting progress as entries or pages are packed. */
|
|
14
|
+
export declare function runJob(job: Job, progress?: Progress): Promise<Uint8Array>;
|