@swc/anyformat-image-wasm32-wasi 0.0.0 → 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/README.md +2 -6
- package/anyformat-image.wasi-browser.js +1465 -0
- package/anyformat-image.wasi.cjs +2322 -0
- package/anyformat-image.wasi.d.cts +356 -0
- package/anyformat-image.wasm32-wasi.wasm +0 -0
- package/package.json +25 -2
- package/wasi-worker-browser.mjs +67 -0
- package/wasi-worker.mjs +186 -0
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
/* auto-generated by NAPI-RS */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
|
|
4
|
+
/** The WASI flavor this loader instantiates. */
|
|
5
|
+
export declare const __napiBindingTarget: 'wasm32-wasi'
|
|
6
|
+
|
|
7
|
+
/** Returns the bounds of the pixels with a minimum alpha on the thread pool. */
|
|
8
|
+
export declare function alphaBounds(image: Uint8Array | RawImageInput, options?: AlphaBoundsOptions | undefined | null): Promise<Rect | null>
|
|
9
|
+
|
|
10
|
+
/** Options of `alphaBounds`. */
|
|
11
|
+
export interface AlphaBoundsOptions {
|
|
12
|
+
/** From 0 to 255. */
|
|
13
|
+
minAlpha?: number
|
|
14
|
+
/** The decode options for an input that is an encoded file. */
|
|
15
|
+
decode?: DecodeOptions
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Returns the smallest rectangle that contains every pixel whose alpha is
|
|
20
|
+
* `minAlpha` or more. Returns `null` when there is no such pixel.
|
|
21
|
+
*/
|
|
22
|
+
export declare function alphaBoundsSync(image: Uint8Array | RawImageInput, options?: AlphaBoundsOptions | undefined | null): Rect | null
|
|
23
|
+
|
|
24
|
+
/** Compares two images on the thread pool. */
|
|
25
|
+
export declare function compareImages(a: Uint8Array | RawImageInput, b: Uint8Array | RawImageInput, options?: CompareOptions | undefined | null): Promise<CompareResult>
|
|
26
|
+
|
|
27
|
+
/** Compares two images that are raw images or encoded files. */
|
|
28
|
+
export declare function compareImagesSync(a: Uint8Array | RawImageInput, b: Uint8Array | RawImageInput, options?: CompareOptions | undefined | null): CompareResult
|
|
29
|
+
|
|
30
|
+
/** Options of `compareImages`. */
|
|
31
|
+
export interface CompareOptions {
|
|
32
|
+
/** 'pixelmatch8', 'pixelmatch7', or 'pixelmatch6'. */
|
|
33
|
+
algorithm?: string
|
|
34
|
+
/** From 0 to 1. */
|
|
35
|
+
threshold?: number
|
|
36
|
+
/** Count anti-aliased pixels as differences. */
|
|
37
|
+
includeAA?: boolean
|
|
38
|
+
/** The opacity of unchanged pixels in the diff image. */
|
|
39
|
+
alpha?: number
|
|
40
|
+
/** The color of anti-aliased pixels. */
|
|
41
|
+
aaColor?: Array<number>
|
|
42
|
+
/** The color of different pixels. */
|
|
43
|
+
diffColor?: Array<number>
|
|
44
|
+
/** The color of different pixels that are darker in the second image. */
|
|
45
|
+
diffColorAlt?: Array<number> | null
|
|
46
|
+
/** Draw the diff on a transparent background. */
|
|
47
|
+
diffMask?: boolean
|
|
48
|
+
/** Blend semi-transparent pixels with a checkerboard. */
|
|
49
|
+
checkerboard?: boolean
|
|
50
|
+
/** Also calculate the largest diff count in any N x N window. */
|
|
51
|
+
windowSize?: number | null
|
|
52
|
+
/** One byte for each pixel. The comparison skips pixels with a non-zero value. */
|
|
53
|
+
ignoreMask?: Uint8Array | Uint8ClampedArray | null
|
|
54
|
+
/** The decode options for inputs that are encoded files. */
|
|
55
|
+
decode?: DecodeOptions
|
|
56
|
+
/** 'raw' or encode options. */
|
|
57
|
+
diff?: 'raw' | EncodeOptions | null
|
|
58
|
+
/** Return the decoded pixels of the inputs that are encoded files. */
|
|
59
|
+
decoded?: boolean
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** The result of `compareImages`. */
|
|
63
|
+
export interface CompareResult {
|
|
64
|
+
/** The width of the images. */
|
|
65
|
+
width: number
|
|
66
|
+
/** The height of the images. */
|
|
67
|
+
height: number
|
|
68
|
+
/** The number of different pixels. */
|
|
69
|
+
diffPixels: number
|
|
70
|
+
/** The number of different pixels that the comparison ignored as anti-aliasing. */
|
|
71
|
+
antialiasedPixels: number
|
|
72
|
+
/** The largest number of different pixels in any window. */
|
|
73
|
+
windowDiffPixels: number | null
|
|
74
|
+
/** The smallest rectangle that contains all different pixels. */
|
|
75
|
+
diffBounds: Rect | null
|
|
76
|
+
/** The diff image when `diff` is 'raw'. */
|
|
77
|
+
diffImage: RawImage | null
|
|
78
|
+
/** The encoded diff image when `diff` is encode options. */
|
|
79
|
+
diffEncoded: Buffer | null
|
|
80
|
+
/** The decoded pixels of input a when `decoded` is true and input a is an encoded file. */
|
|
81
|
+
decodedA: RawImage | null
|
|
82
|
+
/** The decoded pixels of input b when `decoded` is true and input b is an encoded file. */
|
|
83
|
+
decodedB: RawImage | null
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Copies a rectangle of an image on the thread pool. */
|
|
87
|
+
export declare function crop(image: Uint8Array | RawImageInput, rect: RectInput, options?: CropOptions | undefined | null): Promise<RawImage | Buffer>
|
|
88
|
+
|
|
89
|
+
/** Options of `crop`. */
|
|
90
|
+
export interface CropOptions {
|
|
91
|
+
/** The decode options for an input that is an encoded file. */
|
|
92
|
+
decode?: DecodeOptions
|
|
93
|
+
/** 'raw' or encode options. */
|
|
94
|
+
output?: 'raw' | EncodeOptions | null
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Copies a rectangle of an image that is a raw image or an encoded file. */
|
|
98
|
+
export declare function cropSync(image: Uint8Array | RawImageInput, rect: RectInput, options?: CropOptions | undefined | null): RawImage | Buffer
|
|
99
|
+
|
|
100
|
+
/** Decodes an encoded image on the thread pool. */
|
|
101
|
+
export declare function decode(data: Uint8Array, options?: DecodeOptions | undefined | null): Promise<DecodedImage>
|
|
102
|
+
|
|
103
|
+
/** A decoded image and the metadata of its file. */
|
|
104
|
+
export interface DecodedImage {
|
|
105
|
+
/** The width in pixels, after the orientation is applied. */
|
|
106
|
+
width: number
|
|
107
|
+
/** The height in pixels, after the orientation is applied. */
|
|
108
|
+
height: number
|
|
109
|
+
/** The pixel format. */
|
|
110
|
+
pixelFormat: string
|
|
111
|
+
/** The pixel data. */
|
|
112
|
+
data: Buffer
|
|
113
|
+
/** The metadata of the file. */
|
|
114
|
+
info: ImageInfo
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Options of `decode`. */
|
|
118
|
+
export interface DecodeOptions {
|
|
119
|
+
/** Accept only these formats. */
|
|
120
|
+
formats?: Array<string>
|
|
121
|
+
/** The maximum value of width * height. */
|
|
122
|
+
maxPixels?: number
|
|
123
|
+
/** 'apply' or 'ignore'. */
|
|
124
|
+
orientation?: string
|
|
125
|
+
/** The pixel format of the output. */
|
|
126
|
+
pixelFormat?: string
|
|
127
|
+
/** Options for PNG files. */
|
|
128
|
+
png?: PngDecodeOptionsInput
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Decodes an encoded image. */
|
|
132
|
+
export declare function decodeSync(data: Uint8Array, options?: DecodeOptions | undefined | null): DecodedImage
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Identifies the image format of `data` from its leading bytes, on the
|
|
136
|
+
* thread pool. Copies only the leading bytes that `detectPrefixLenSync` asks
|
|
137
|
+
* for, so the caller can change `data` after the call.
|
|
138
|
+
*
|
|
139
|
+
* Returns `null` when the format is not recognized.
|
|
140
|
+
*/
|
|
141
|
+
export declare function detect(data: Uint8Array): Promise<DetectedFormat | null>
|
|
142
|
+
|
|
143
|
+
/** A format identified by `detect`. */
|
|
144
|
+
export interface DetectedFormat {
|
|
145
|
+
/** The stable lowercase identifier of the format, for example `"png"`. */
|
|
146
|
+
format: string
|
|
147
|
+
/** The most common MIME type of the format. */
|
|
148
|
+
mimeType: string
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Identifies the image format of the file at `path` on the thread pool.
|
|
153
|
+
* Reads only the leading bytes that `detectPrefixLenSync` asks for.
|
|
154
|
+
*
|
|
155
|
+
* Returns `null` when the format is not recognized.
|
|
156
|
+
*/
|
|
157
|
+
export declare function detectFile(path: string): Promise<DetectedFormat | null>
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Identifies the image format of the file at `path`. Reads only the leading
|
|
161
|
+
* bytes that `detectPrefixLenSync` asks for.
|
|
162
|
+
*
|
|
163
|
+
* Returns `null` when the format is not recognized.
|
|
164
|
+
*/
|
|
165
|
+
export declare function detectFileSync(path: string): DetectedFormat | null
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* The number of leading bytes that `detect` needs to give its final result
|
|
169
|
+
* for this input. The value can grow when more bytes are available. The
|
|
170
|
+
* maximum value is 16 MiB.
|
|
171
|
+
*/
|
|
172
|
+
export declare function detectPrefixLenSync(data: Uint8Array): number
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Identifies the image format of `data` from its leading bytes.
|
|
176
|
+
*
|
|
177
|
+
* Returns `null` when the format is not recognized.
|
|
178
|
+
*/
|
|
179
|
+
export declare function detectSync(data: Uint8Array): DetectedFormat | null
|
|
180
|
+
|
|
181
|
+
/** Encodes a raw image on the thread pool. */
|
|
182
|
+
export declare function encode(image: RawImageInput, options: EncodeOptions): Promise<Buffer>
|
|
183
|
+
|
|
184
|
+
/** Options of `encode`. `format` selects the output format. */
|
|
185
|
+
export interface EncodeOptions {
|
|
186
|
+
/** The output format. */
|
|
187
|
+
format: string
|
|
188
|
+
/** The background color for an output without alpha. */
|
|
189
|
+
background?: Array<number>
|
|
190
|
+
/** PNG: the color type. */
|
|
191
|
+
colorType?: string
|
|
192
|
+
/** PNG: the compression level. */
|
|
193
|
+
compression?: string
|
|
194
|
+
/** PNG: the scanline filter. */
|
|
195
|
+
filter?: string
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** Encodes a raw image in the format that `options.format` selects. */
|
|
199
|
+
export declare function encodeSync(image: RawImageInput, options: EncodeOptions): Buffer
|
|
200
|
+
|
|
201
|
+
/** The operations that are available for a format. */
|
|
202
|
+
export interface FormatSupport {
|
|
203
|
+
/** The format. */
|
|
204
|
+
format: string
|
|
205
|
+
/** The MIME type of the format. */
|
|
206
|
+
mimeType: string
|
|
207
|
+
/** `readImageInfo` can read files of the format. */
|
|
208
|
+
readInfo: boolean
|
|
209
|
+
/** `decode` can decode files of the format. */
|
|
210
|
+
decode: boolean
|
|
211
|
+
/** `encode` can encode files of the format. */
|
|
212
|
+
encode: boolean
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** The header and the metadata of an encoded image. */
|
|
216
|
+
export interface ImageInfo {
|
|
217
|
+
/** The format of the file. */
|
|
218
|
+
format: string
|
|
219
|
+
/** The MIME type of the format. */
|
|
220
|
+
mimeType: string
|
|
221
|
+
/** The width that the file stores. */
|
|
222
|
+
width: number
|
|
223
|
+
/** The height that the file stores. */
|
|
224
|
+
height: number
|
|
225
|
+
/** The image has an alpha channel or a tRNS chunk. */
|
|
226
|
+
hasAlpha: boolean
|
|
227
|
+
/** The number of frames, pages, or sub-images. */
|
|
228
|
+
frameCount: number
|
|
229
|
+
/** The EXIF orientation from 1 to 8. */
|
|
230
|
+
orientation: number
|
|
231
|
+
/** The file contains an ICC profile. */
|
|
232
|
+
hasIccProfile: boolean
|
|
233
|
+
/** PNG: the bit depth of each sample. */
|
|
234
|
+
bitDepth: number | null
|
|
235
|
+
/** PNG: the color type. */
|
|
236
|
+
colorType: string | null
|
|
237
|
+
/** PNG: the file uses Adam7 interlacing. */
|
|
238
|
+
interlaced: boolean | null
|
|
239
|
+
/** PNG: the value of the gAMA chunk, or null when there is none or its value is 0. */
|
|
240
|
+
gamma: number | null
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Returns the operations that are available for each format. The public
|
|
245
|
+
* API exposes the result as the `formatSupport` constant.
|
|
246
|
+
*/
|
|
247
|
+
export declare function listFormatSupport(): Array<FormatSupport>
|
|
248
|
+
|
|
249
|
+
/** Decode options for PNG files. */
|
|
250
|
+
export interface PngDecodeOptionsInput {
|
|
251
|
+
/**
|
|
252
|
+
* true: reject a bad CRC in IHDR, PLTE, or IDAT, and ignore an ancillary
|
|
253
|
+
* chunk with a bad CRC. false: use every chunk without a CRC check.
|
|
254
|
+
* Default: true.
|
|
255
|
+
*/
|
|
256
|
+
checkCrc?: boolean
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** A raw image that a function returns. */
|
|
260
|
+
export interface RawImage {
|
|
261
|
+
/** The width in pixels. */
|
|
262
|
+
width: number
|
|
263
|
+
/** The height in pixels. */
|
|
264
|
+
height: number
|
|
265
|
+
/** The pixel format. */
|
|
266
|
+
pixelFormat: string
|
|
267
|
+
/** The pixel data. */
|
|
268
|
+
data: Buffer
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/** A raw image from JavaScript: pixels in memory, not an encoded file. */
|
|
272
|
+
export interface RawImageInput {
|
|
273
|
+
/** The width in pixels. */
|
|
274
|
+
width: number
|
|
275
|
+
/** The height in pixels. */
|
|
276
|
+
height: number
|
|
277
|
+
/** The pixel format. Default: 'rgba8'. */
|
|
278
|
+
pixelFormat?: string
|
|
279
|
+
/** The pixel data. */
|
|
280
|
+
data: Uint8Array | Uint8ClampedArray
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** Reads the header and the metadata of an encoded image on the thread pool. */
|
|
284
|
+
export declare function readImageInfo(data: Uint8Array, options?: ReadImageInfoOptions | undefined | null): Promise<ImageInfo>
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Reads the header and the metadata of the image in the file at `path` on
|
|
288
|
+
* the thread pool. Reads only the byte ranges that it needs. For PNG, it
|
|
289
|
+
* stops at the first `IDAT` chunk.
|
|
290
|
+
*/
|
|
291
|
+
export declare function readImageInfoFile(path: string, options?: ReadImageInfoOptions | undefined | null): Promise<ImageInfo>
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Reads the header and the metadata of the image in the file at `path`.
|
|
295
|
+
* Reads only the byte ranges that it needs. For PNG, it stops at the first
|
|
296
|
+
* `IDAT` chunk.
|
|
297
|
+
*/
|
|
298
|
+
export declare function readImageInfoFileSync(path: string, options?: ReadImageInfoOptions | undefined | null): ImageInfo
|
|
299
|
+
|
|
300
|
+
/** Options of `readImageInfo`. */
|
|
301
|
+
export interface ReadImageInfoOptions {
|
|
302
|
+
/** Accept only these formats. */
|
|
303
|
+
formats?: Array<string>
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/** Reads the header and the metadata of an encoded image. */
|
|
307
|
+
export declare function readImageInfoSync(data: Uint8Array, options?: ReadImageInfoOptions | undefined | null): ImageInfo
|
|
308
|
+
|
|
309
|
+
/** A rectangle in pixels. */
|
|
310
|
+
export interface Rect {
|
|
311
|
+
/** The left edge. */
|
|
312
|
+
x: number
|
|
313
|
+
/** The top edge. */
|
|
314
|
+
y: number
|
|
315
|
+
/** The width. */
|
|
316
|
+
width: number
|
|
317
|
+
/** The height. */
|
|
318
|
+
height: number
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/** A rectangle from JavaScript. */
|
|
322
|
+
export interface RectInput {
|
|
323
|
+
/** The left edge. */
|
|
324
|
+
x: number
|
|
325
|
+
/** The top edge. */
|
|
326
|
+
y: number
|
|
327
|
+
/** The width. */
|
|
328
|
+
width: number
|
|
329
|
+
/** The height. */
|
|
330
|
+
height: number
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/** Resizes an image on the thread pool. */
|
|
334
|
+
export declare function resize(image: Uint8Array | RawImageInput, options: ResizeOptions): Promise<RawImage | Buffer>
|
|
335
|
+
|
|
336
|
+
/** Options of `resize`. */
|
|
337
|
+
export interface ResizeOptions {
|
|
338
|
+
/** The target width. */
|
|
339
|
+
width?: number
|
|
340
|
+
/** The target height. */
|
|
341
|
+
height?: number
|
|
342
|
+
/** 'fill', 'inside', or 'outside'. */
|
|
343
|
+
fit?: string
|
|
344
|
+
/** 'box' or 'nearest'. */
|
|
345
|
+
filter?: string
|
|
346
|
+
/** The decode options for an input that is an encoded file. */
|
|
347
|
+
decode?: DecodeOptions
|
|
348
|
+
/** 'raw' or encode options. */
|
|
349
|
+
output?: 'raw' | EncodeOptions | null
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/** Resizes an image that is a raw image or an encoded file. */
|
|
353
|
+
export declare function resizeSync(image: Uint8Array | RawImageInput, options: ResizeOptions): RawImage | Buffer
|
|
354
|
+
|
|
355
|
+
/** Returns the identifiers of every format that `detect` can return. */
|
|
356
|
+
export declare function supportedFormats(): Array<string>
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,8 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@swc/anyformat-image-wasm32-wasi",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"cpu": [
|
|
5
|
+
"wasm32"
|
|
6
|
+
],
|
|
7
|
+
"main": "anyformat-image.wasi.cjs",
|
|
8
|
+
"files": [
|
|
9
|
+
"anyformat-image.wasm32-wasi.wasm",
|
|
10
|
+
"anyformat-image.wasi.cjs",
|
|
11
|
+
"anyformat-image.wasi.d.cts",
|
|
12
|
+
"anyformat-image.wasi-browser.js",
|
|
13
|
+
"wasi-worker.mjs",
|
|
14
|
+
"wasi-worker-browser.mjs"
|
|
15
|
+
],
|
|
16
|
+
"description": "High-performance image processing for Node.js",
|
|
5
17
|
"license": "Apache-2.0",
|
|
18
|
+
"engines": {
|
|
19
|
+
"node": "^20.19.0 || ^22.13.0 || >=23.5.0"
|
|
20
|
+
},
|
|
6
21
|
"repository": {
|
|
7
22
|
"type": "git",
|
|
8
23
|
"url": "git+https://github.com/swc-project/anyformat.git",
|
|
@@ -10,5 +25,13 @@
|
|
|
10
25
|
},
|
|
11
26
|
"publishConfig": {
|
|
12
27
|
"access": "public"
|
|
28
|
+
},
|
|
29
|
+
"types": "anyformat-image.wasi.d.cts",
|
|
30
|
+
"browser": "anyformat-image.wasi-browser.js",
|
|
31
|
+
"type": "module",
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@napi-rs/wasm-runtime": "~1.2.7",
|
|
34
|
+
"@emnapi/core": "2.0.0-beta.3",
|
|
35
|
+
"@emnapi/runtime": "2.0.0-beta.3"
|
|
13
36
|
}
|
|
14
37
|
}
|
|
@@ -0,0 +1,67 @@
|
|
|
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
|
+
return instantiateNapiModuleSync(wasmModule, {
|
|
22
|
+
childThread: true,
|
|
23
|
+
wasi,
|
|
24
|
+
// The wasm links a "basic" emnapi archive (no C async-work /
|
|
25
|
+
// threadsafe-function implementations), so every thread that
|
|
26
|
+
// instantiates it must provide the JavaScript implementations
|
|
27
|
+
// through the emnapi plugins.
|
|
28
|
+
plugins: [emnapiAsyncWorkPlugin, emnapiTSFNPlugin],
|
|
29
|
+
overwriteImports(importObject) {
|
|
30
|
+
importObject.env = {
|
|
31
|
+
...importObject.env,
|
|
32
|
+
...importObject.napi,
|
|
33
|
+
...importObject.emnapi,
|
|
34
|
+
memory: wasmMemory,
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
})
|
|
38
|
+
},
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
// When this wasm thread dies, raise the addon's crash flag before emnapi reports
|
|
42
|
+
// it. The loader thread may be inside wasm, in a cleanup call that waits on this
|
|
43
|
+
// thread; the flag is what those waits check between short slices, and once it
|
|
44
|
+
// is up they trap instead of waiting for good. It is a word in the shared wasm
|
|
45
|
+
// memory, and the loader posts a view of it (see
|
|
46
|
+
// napi_wasm_thread_crash_flag_address) after it instantiated the wasm, or right
|
|
47
|
+
// after it created this worker. No view, no flag: an addon built with an older
|
|
48
|
+
// napi, or a failure before the view arrived.
|
|
49
|
+
let __addonCrashFlag
|
|
50
|
+
const __beforeReportError = handler.beforeReportError
|
|
51
|
+
handler.beforeReportError = function (...args) {
|
|
52
|
+
if (__addonCrashFlag !== undefined) {
|
|
53
|
+
try {
|
|
54
|
+
Atomics.store(__addonCrashFlag, 0, 1)
|
|
55
|
+
} catch {}
|
|
56
|
+
}
|
|
57
|
+
return __beforeReportError.apply(this, args)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
globalThis.onmessage = function (e) {
|
|
61
|
+
const data = e && e.data
|
|
62
|
+
if (data && data.__napiRsAddonCrashFlag instanceof Int32Array) {
|
|
63
|
+
__addonCrashFlag = data.__napiRsAddonCrashFlag
|
|
64
|
+
return
|
|
65
|
+
}
|
|
66
|
+
handler.handle(e)
|
|
67
|
+
}
|
package/wasi-worker.mjs
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
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, threadId, Worker, workerData } from 'node:worker_threads'
|
|
6
|
+
|
|
7
|
+
const require = createRequire(import.meta.url)
|
|
8
|
+
|
|
9
|
+
// The thread spawn that created this worker has already returned, so the loader
|
|
10
|
+
// thread may be waiting on this thread. A failure before the crash hook below is
|
|
11
|
+
// in place (a missing '@napi-rs/wasm-runtime', say) has to raise the crash flags
|
|
12
|
+
// too, then fail the worker as before.
|
|
13
|
+
let handler
|
|
14
|
+
try {
|
|
15
|
+
const {
|
|
16
|
+
instantiateNapiModuleSync,
|
|
17
|
+
MessageHandler,
|
|
18
|
+
getDefaultContext,
|
|
19
|
+
emnapiAsyncWorkPlugin,
|
|
20
|
+
emnapiTSFNPlugin,
|
|
21
|
+
} = require('@napi-rs/wasm-runtime')
|
|
22
|
+
|
|
23
|
+
if (parentPort) {
|
|
24
|
+
parentPort.on('message', (data) => {
|
|
25
|
+
globalThis.onmessage({ data })
|
|
26
|
+
})
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
Object.assign(globalThis, {
|
|
30
|
+
self: globalThis,
|
|
31
|
+
require,
|
|
32
|
+
Worker,
|
|
33
|
+
importScripts: function (f) {
|
|
34
|
+
// oxlint-disable-next-line no-eval -- WASI importScripts polyfill
|
|
35
|
+
;(0, eval)(fs.readFileSync(f, 'utf8') + '//# sourceURL=' + f)
|
|
36
|
+
},
|
|
37
|
+
postMessage: function (msg) {
|
|
38
|
+
if (parentPort) {
|
|
39
|
+
parentPort.postMessage(msg)
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
const emnapiContext = getDefaultContext()
|
|
45
|
+
|
|
46
|
+
const __cwd = process.cwd()
|
|
47
|
+
const __rootDir =
|
|
48
|
+
(workerData && typeof workerData.rootDir === 'string' && workerData.rootDir) ||
|
|
49
|
+
parse(__cwd).root
|
|
50
|
+
const __hostRoot =
|
|
51
|
+
(workerData && typeof workerData.hostRoot === 'string' && workerData.hostRoot) ||
|
|
52
|
+
(process.platform === 'android' ? __cwd : __rootDir)
|
|
53
|
+
|
|
54
|
+
handler = new MessageHandler({
|
|
55
|
+
onLoad({ wasmModule, wasmMemory }) {
|
|
56
|
+
const wasi = new WASI({
|
|
57
|
+
version: 'preview1',
|
|
58
|
+
env: process.env,
|
|
59
|
+
preopens: {
|
|
60
|
+
[__rootDir]: __hostRoot,
|
|
61
|
+
[__hostRoot]: __hostRoot,
|
|
62
|
+
},
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
return instantiateNapiModuleSync(wasmModule, {
|
|
66
|
+
childThread: true,
|
|
67
|
+
wasi,
|
|
68
|
+
context: emnapiContext,
|
|
69
|
+
// The wasm links a "basic" emnapi archive (no C async-work /
|
|
70
|
+
// threadsafe-function implementations), so every thread that
|
|
71
|
+
// instantiates it must provide the JavaScript implementations
|
|
72
|
+
// through the emnapi plugins.
|
|
73
|
+
plugins: [emnapiAsyncWorkPlugin, emnapiTSFNPlugin],
|
|
74
|
+
overwriteImports(importObject) {
|
|
75
|
+
importObject.env = {
|
|
76
|
+
...importObject.env,
|
|
77
|
+
...importObject.napi,
|
|
78
|
+
...importObject.emnapi,
|
|
79
|
+
memory: wasmMemory,
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
})
|
|
83
|
+
},
|
|
84
|
+
})
|
|
85
|
+
} catch (error) {
|
|
86
|
+
if (workerData && workerData.crashFlag instanceof Int32Array) {
|
|
87
|
+
__raiseWasiThreadCrashFlags(error)
|
|
88
|
+
}
|
|
89
|
+
throw error
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Tell the loader thread synchronously that this wasm thread died, so its 'exit'
|
|
93
|
+
// teardown does not re-enter wasm and wait on this thread forever. emnapi calls
|
|
94
|
+
// beforeReportError (which runs emnapi_thread_crashed, so the main thread may
|
|
95
|
+
// throw 'unwind' and start exiting) before the 'error' event is even posted.
|
|
96
|
+
// A loader that predates the flag passes none.
|
|
97
|
+
if (workerData && workerData.crashFlag instanceof Int32Array) {
|
|
98
|
+
const __beforeReportError = handler.beforeReportError
|
|
99
|
+
handler.beforeReportError = function (...args) {
|
|
100
|
+
if (!__raiseWasiThreadCrashFlags(args[0])) {
|
|
101
|
+
// No view (the thread was spawned before the loader read the address,
|
|
102
|
+
// say): the export stores into the same word, but only a worker that
|
|
103
|
+
// loaded has an instance to call it on.
|
|
104
|
+
try {
|
|
105
|
+
const __napiThreadCrashed = this.instance?.exports?.napi_wasm_thread_crashed
|
|
106
|
+
if (typeof __napiThreadCrashed === 'function') {
|
|
107
|
+
__napiThreadCrashed()
|
|
108
|
+
}
|
|
109
|
+
} catch {}
|
|
110
|
+
}
|
|
111
|
+
return __beforeReportError.apply(this, args)
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// The error goes into the shared crash report first. The 'error' event that
|
|
116
|
+
// would carry it is dropped when the loader terminates this worker before the
|
|
117
|
+
// event is sent, so the report is the only copy the loader can count on.
|
|
118
|
+
// Layout: three Int32 words — state (0 empty, 1 writing, 2 written), byte
|
|
119
|
+
// length, threadId — then the UTF-8 JSON of { name, message, stack }. Only the
|
|
120
|
+
// first worker to crash writes it, and it is complete before the flag is raised.
|
|
121
|
+
//
|
|
122
|
+
// Then the loader's flag, then the addon's. The loader thread may already be
|
|
123
|
+
// inside wasm, in a cleanup call that waits on this thread; the addon's flag is
|
|
124
|
+
// what those waits check between short slices. It is a word in the shared wasm
|
|
125
|
+
// memory (the loader passes a view of it, see napi_wasm_thread_crash_flag_address),
|
|
126
|
+
// so no instance is needed: a worker that failed while loading raises it too.
|
|
127
|
+
// Once it is up the waits trap, and the loader, whose flag is already up, turns
|
|
128
|
+
// the throw into its crash rejection. Returns whether the addon's flag was raised.
|
|
129
|
+
function __raiseWasiThreadCrashFlags(error) {
|
|
130
|
+
try {
|
|
131
|
+
__writeCrashReport(workerData.crashReport, error)
|
|
132
|
+
} catch {}
|
|
133
|
+
try {
|
|
134
|
+
Atomics.store(workerData.crashFlag, 0, 1)
|
|
135
|
+
} catch {}
|
|
136
|
+
const addonCrashFlag = workerData.addonCrashFlag
|
|
137
|
+
if (!(addonCrashFlag instanceof Int32Array)) {
|
|
138
|
+
return false
|
|
139
|
+
}
|
|
140
|
+
try {
|
|
141
|
+
Atomics.store(addonCrashFlag, 0, 1)
|
|
142
|
+
return true
|
|
143
|
+
} catch {
|
|
144
|
+
return false
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function __writeCrashReport(report, error) {
|
|
149
|
+
if (!(report instanceof SharedArrayBuffer) || report.byteLength <= 12) {
|
|
150
|
+
return
|
|
151
|
+
}
|
|
152
|
+
const header = new Int32Array(report, 0, 3)
|
|
153
|
+
if (Atomics.compareExchange(header, 0, 0, 1) !== 0) {
|
|
154
|
+
return
|
|
155
|
+
}
|
|
156
|
+
let length = 0
|
|
157
|
+
try {
|
|
158
|
+
const body = new Uint8Array(report, 12)
|
|
159
|
+
const isObject =
|
|
160
|
+
error !== null && (typeof error === 'object' || typeof error === 'function')
|
|
161
|
+
const name = isObject && typeof error.name === 'string' ? error.name : 'Error'
|
|
162
|
+
const message = isObject && typeof error.message === 'string'
|
|
163
|
+
? error.message
|
|
164
|
+
: String(error)
|
|
165
|
+
const stack = isObject && typeof error.stack === 'string' ? error.stack : undefined
|
|
166
|
+
const encoder = new TextEncoder()
|
|
167
|
+
let bytes = encoder.encode(JSON.stringify({ name, message, stack }))
|
|
168
|
+
if (bytes.length > body.length) {
|
|
169
|
+
bytes = encoder.encode(
|
|
170
|
+
JSON.stringify({ name, message: message.slice(0, body.length >> 3) }),
|
|
171
|
+
)
|
|
172
|
+
}
|
|
173
|
+
if (bytes.length <= body.length) {
|
|
174
|
+
body.set(bytes)
|
|
175
|
+
length = bytes.length
|
|
176
|
+
}
|
|
177
|
+
Atomics.store(header, 2, threadId)
|
|
178
|
+
} finally {
|
|
179
|
+
Atomics.store(header, 1, length)
|
|
180
|
+
Atomics.store(header, 0, 2)
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
globalThis.onmessage = function (e) {
|
|
185
|
+
handler.handle(e)
|
|
186
|
+
}
|