@build-qube/papyra-native-wasm32-wasi 0.0.3

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.
@@ -0,0 +1,58 @@
1
+ /* auto-generated by NAPI-RS */
2
+ /* eslint-disable */
3
+ export declare class PdfDocument {
4
+ /** Load a PDF from raw bytes. */
5
+ static load(data: Uint8Array): PdfDocument
6
+ /** Load an encrypted PDF. */
7
+ static loadWithPassword(data: Uint8Array, password: string): PdfDocument
8
+ get pageCount(): number
9
+ pageSize(index: number): PageDimensions
10
+ /** Render one page on the calling thread. Blocks; prefer `renderPageAsync`. */
11
+ renderPage(index: number, dpi?: number | undefined | null): RenderedPage
12
+ /** Render one page off the JS thread. Several of these run concurrently. */
13
+ renderPageAsync(index: number, dpi?: number | undefined | null): Promise<RenderedPage>
14
+ /** Render `[start, end)` in one async task, parallelised internally with rayon. */
15
+ renderPagesAsync(start: number, end: number, dpi?: number | undefined | null): Promise<Array<RenderedPage>>
16
+ }
17
+
18
+ /** Name of the rendering backend, currently always `"hayro"`. */
19
+ export declare function backendName(): string
20
+
21
+ /**
22
+ * Size the rayon pool used by `renderPagesAsync`.
23
+ *
24
+ * Required on wasm: `available_parallelism()` is unsupported there, so rayon otherwise
25
+ * pins itself to a single thread. Must be called before any other rayon use — the
26
+ * global pool can only be built once.
27
+ */
28
+ export declare function configureThreadPool(threads: number, stackSizeMb?: number | undefined | null): number
29
+
30
+ export interface PageDimensions {
31
+ /** Width in PDF points (1/72 inch). */
32
+ width: number
33
+ /** Height in PDF points (1/72 inch). */
34
+ height: number
35
+ }
36
+
37
+ export interface RenderedPage {
38
+ width: number
39
+ height: number
40
+ stride: number
41
+ /** Always `"rgba8"` for the hayro backend. */
42
+ format: string
43
+ data: Uint8Array
44
+ }
45
+
46
+ /**
47
+ * Grow the wasm heap up front, from the calling (single) thread.
48
+ *
49
+ * Linear memory grows via `memory.grow`; several render workers hitting that on a cold
50
+ * heap traps intermittently with "memory access out of bounds" (~14% of cold runs at 23
51
+ * pages / 150 DPI / 8-way). Growing once, single-threaded, makes later allocations come
52
+ * from the free list. Called automatically on first load; call it explicitly only to
53
+ * raise the reservation before rendering something unusually large. No-op on native.
54
+ */
55
+ export declare function reserveMemory(megabytes: number): number
56
+
57
+ /** `"native"` or `"wasm"`. The wrapper uses this to pick a concurrency strategy. */
58
+ export declare function runtime(): string
Binary file
@@ -0,0 +1,45 @@
1
+ import {
2
+ instantiateNapiModuleSync,
3
+ MessageHandler,
4
+ WASI,
5
+ emnapiAsyncWorkPlugin,
6
+ emnapiTSFNPlugin,
7
+ } from '@napi-rs/wasm-runtime'
8
+
9
+ const handler = new MessageHandler({
10
+ onLoad({ wasmModule, wasmMemory }) {
11
+ const wasi = new WASI({
12
+ print: function () {
13
+ // eslint-disable-next-line no-console
14
+ console.log.apply(console, arguments)
15
+ },
16
+ printErr: function() {
17
+ // eslint-disable-next-line no-console
18
+ console.error.apply(console, arguments)
19
+
20
+ },
21
+ })
22
+ return instantiateNapiModuleSync(wasmModule, {
23
+ childThread: true,
24
+ wasi,
25
+ // The wasm links a "basic" emnapi archive (no C async-work /
26
+ // threadsafe-function implementations), so every thread that
27
+ // instantiates it must provide the JavaScript implementations
28
+ // through the emnapi plugins.
29
+ plugins: [emnapiAsyncWorkPlugin, emnapiTSFNPlugin],
30
+ overwriteImports(importObject) {
31
+ importObject.env = {
32
+ ...importObject.env,
33
+ ...importObject.napi,
34
+ ...importObject.emnapi,
35
+ memory: wasmMemory,
36
+ }
37
+ },
38
+ })
39
+ },
40
+
41
+ })
42
+
43
+ globalThis.onmessage = function (e) {
44
+ handler.handle(e)
45
+ }
@@ -0,0 +1,74 @@
1
+ import fs from "node:fs";
2
+ import { createRequire } from "node:module";
3
+ import { parse } from "node:path";
4
+ import { WASI } from "node:wasi";
5
+ import { parentPort, Worker } from "node:worker_threads";
6
+
7
+ const require = createRequire(import.meta.url);
8
+
9
+ const {
10
+ instantiateNapiModuleSync,
11
+ MessageHandler,
12
+ getDefaultContext,
13
+ emnapiAsyncWorkPlugin,
14
+ emnapiTSFNPlugin,
15
+ } = require("@napi-rs/wasm-runtime");
16
+
17
+ if (parentPort) {
18
+ parentPort.on("message", (data) => {
19
+ globalThis.onmessage({ data });
20
+ });
21
+ }
22
+
23
+ Object.assign(globalThis, {
24
+ self: globalThis,
25
+ require,
26
+ Worker,
27
+ importScripts: function (f) {
28
+ ;(0, eval)(fs.readFileSync(f, "utf8") + "//# sourceURL=" + f);
29
+ },
30
+ postMessage: function (msg) {
31
+ if (parentPort) {
32
+ parentPort.postMessage(msg);
33
+ }
34
+ },
35
+ });
36
+
37
+ const emnapiContext = getDefaultContext();
38
+
39
+ const __rootDir = parse(process.cwd()).root;
40
+
41
+ const handler = new MessageHandler({
42
+ onLoad({ wasmModule, wasmMemory }) {
43
+ const wasi = new WASI({
44
+ version: 'preview1',
45
+ env: process.env,
46
+ preopens: {
47
+ [__rootDir]: __rootDir,
48
+ },
49
+ });
50
+
51
+ return instantiateNapiModuleSync(wasmModule, {
52
+ childThread: true,
53
+ wasi,
54
+ context: emnapiContext,
55
+ // The wasm links a "basic" emnapi archive (no C async-work /
56
+ // threadsafe-function implementations), so every thread that
57
+ // instantiates it must provide the JavaScript implementations
58
+ // through the emnapi plugins.
59
+ plugins: [emnapiAsyncWorkPlugin, emnapiTSFNPlugin],
60
+ overwriteImports(importObject) {
61
+ importObject.env = {
62
+ ...importObject.env,
63
+ ...importObject.napi,
64
+ ...importObject.emnapi,
65
+ memory: wasmMemory
66
+ };
67
+ },
68
+ });
69
+ },
70
+ });
71
+
72
+ globalThis.onmessage = function (e) {
73
+ handler.handle(e);
74
+ };