@oxc-minify/binding-wasm32-wasi 0.151.0 → 0.153.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/minify.wasi.d.cts CHANGED
@@ -341,6 +341,39 @@ export declare const enum Severity {
341
341
  Advice = 'Advice',
342
342
  }
343
343
 
344
+ /** See <https://tc39.es/ecma426/#sec-source-map-format>. */
345
+ export interface JSONSourceMap {
346
+ /** The version field, must be 3. */
347
+ version: number
348
+ /** An optional name of the generated code that this source map is associated with. */
349
+ file?: string
350
+ /** A string with the encoded mapping data. */
351
+ mappings: string
352
+ /**
353
+ * An optional source root, useful for relocating source files on a server or removing repeated values in the "sources" entry.
354
+ * This value is prepended to the individual entries in the "source" field.
355
+ */
356
+ sourceRoot?: string
357
+ /** A list of original sources used by the "mappings" entry. */
358
+ sources: Array<string>
359
+ /**
360
+ * An optional list of source content, useful when the "source" can't be hosted.
361
+ * The contents are listed in the same order as the sources in line 5. "null" may be used if some original sources should be retrieved by name.
362
+ */
363
+ sourcesContent?: Array<string | undefined | null>
364
+ /** A list of symbol names used by the "mappings" entry. */
365
+ names: Array<string>
366
+ /** An optional field containing the debugId for this sourcemap. */
367
+ debugId?: string
368
+ /**
369
+ * Identifies third-party sources (such as framework code or bundler-generated code), allowing developers to avoid code that they don't want to see or step through, without having to configure this beforehand.
370
+ * The `ignoreList` field refers to the `sources` array, and lists the indices of all the known third-party sources in that source map.
371
+ * When parsing the source map, developer tools can use this to determine sections of the code that the browser loads and runs that could be automatically ignore-listed.
372
+ * JSON input falls back to the deprecated `x_google_ignoreList` when `ignoreList` is missing or null.
373
+ */
374
+ ignoreList?: Array<number>
375
+ }
376
+
344
377
  export interface SourceMap {
345
378
  file?: string
346
379
  mappings: string
@@ -349,5 +382,5 @@ export interface SourceMap {
349
382
  sources: Array<string>
350
383
  sourcesContent?: Array<string>
351
384
  version: number
352
- x_google_ignoreList?: Array<number>
385
+ ignoreList?: Array<number>
353
386
  }
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oxc-minify/binding-wasm32-wasi",
3
- "version": "0.151.0",
3
+ "version": "0.153.0",
4
4
  "main": "minify.wasi.cjs",
5
5
  "files": [
6
6
  "minify.wasm32-wasi.wasm",
@@ -40,7 +40,7 @@
40
40
  "browser": "minify.wasi-browser.js",
41
41
  "type": "module",
42
42
  "dependencies": {
43
- "@napi-rs/wasm-runtime": "~1.2.4",
43
+ "@napi-rs/wasm-runtime": "~1.2.5",
44
44
  "@emnapi/core": "2.0.0-alpha.3",
45
45
  "@emnapi/runtime": "2.0.0-alpha.3"
46
46
  }
@@ -38,6 +38,30 @@ const handler = new MessageHandler({
38
38
  },
39
39
  })
40
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
+
41
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
+ }
42
66
  handler.handle(e)
43
67
  }
package/wasi-worker.mjs CHANGED
@@ -2,80 +2,184 @@ import fs from 'node:fs'
2
2
  import { createRequire } from 'node:module'
3
3
  import { parse } from 'node:path'
4
4
  import { WASI } from 'node:wasi'
5
- import { parentPort, Worker, workerData } from 'node:worker_threads'
5
+ import { parentPort, threadId, Worker, workerData } from 'node:worker_threads'
6
6
 
7
7
  const require = createRequire(import.meta.url)
8
8
 
9
- const {
10
- instantiateNapiModuleSync,
11
- MessageHandler,
12
- getDefaultContext,
13
- emnapiAsyncWorkPlugin,
14
- emnapiTSFNPlugin,
15
- } = require('@napi-rs/wasm-runtime')
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')
16
22
 
17
- if (parentPort) {
18
- parentPort.on('message', (data) => {
19
- globalThis.onmessage({ data })
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
+ },
20
42
  })
21
- }
22
43
 
23
- Object.assign(globalThis, {
24
- self: globalThis,
25
- require,
26
- Worker,
27
- importScripts: function (f) {
28
- // oxlint-disable-next-line no-eval -- WASI importScripts polyfill
29
- ;(0, eval)(fs.readFileSync(f, 'utf8') + '//# sourceURL=' + f)
30
- },
31
- postMessage: function (msg) {
32
- if (parentPort) {
33
- parentPort.postMessage(msg)
34
- }
35
- },
36
- })
44
+ const emnapiContext = getDefaultContext()
37
45
 
38
- const emnapiContext = getDefaultContext()
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)
39
53
 
40
- const __cwd = process.cwd()
41
- const __rootDir =
42
- (workerData && typeof workerData.rootDir === 'string' && workerData.rootDir) ||
43
- parse(__cwd).root
44
- const __hostRoot =
45
- (workerData && typeof workerData.hostRoot === 'string' && workerData.hostRoot) ||
46
- (process.platform === 'android' ? __cwd : __rootDir)
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
+ })
47
64
 
48
- const handler = new MessageHandler({
49
- onLoad({ wasmModule, wasmMemory }) {
50
- const wasi = new WASI({
51
- version: 'preview1',
52
- env: process.env,
53
- preopens: {
54
- [__rootDir]: __hostRoot,
55
- [__hostRoot]: __hostRoot,
56
- },
57
- })
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
+ }
58
91
 
59
- return instantiateNapiModuleSync(wasmModule, {
60
- childThread: true,
61
- wasi,
62
- context: emnapiContext,
63
- // The wasm links a "basic" emnapi archive (no C async-work /
64
- // threadsafe-function implementations), so every thread that
65
- // instantiates it must provide the JavaScript implementations
66
- // through the emnapi plugins.
67
- plugins: [emnapiAsyncWorkPlugin, emnapiTSFNPlugin],
68
- overwriteImports(importObject) {
69
- importObject.env = {
70
- ...importObject.env,
71
- ...importObject.napi,
72
- ...importObject.emnapi,
73
- memory: wasmMemory,
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()
74
108
  }
75
- },
76
- })
77
- },
78
- })
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
+ }
79
183
 
80
184
  globalThis.onmessage = function (e) {
81
185
  handler.handle(e)