@oxc-minify/binding-wasm32-wasi 0.149.0 → 0.151.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-browser.js +427 -26
- package/minify.wasi.cjs +460 -41
- package/minify.wasi.d.cts +27 -3
- package/minify.wasm32-wasi.wasm +0 -0
- package/package.json +3 -3
- package/wasi-worker-browser.mjs +1 -3
- package/wasi-worker.mjs +31 -23
package/minify.wasi.cjs
CHANGED
|
@@ -1,9 +1,57 @@
|
|
|
1
1
|
// napi-rs-artifact-metadata:{"version":2,"rootEntry":"index.js","exports":["LegalCommentsMode","minify","minifySync","Severity"],"managedRootEntries":["browser.js","index.js","minify.wasm","minify.debug.wasm"]}
|
|
2
2
|
/* eslint-disable */
|
|
3
|
-
/* prettier-ignore */
|
|
4
|
-
|
|
5
3
|
/* auto-generated by NAPI-RS */
|
|
6
4
|
|
|
5
|
+
const __napiBindingTarget = 'wasm32-wasi'
|
|
6
|
+
function __napiStampBindingTarget(exportsObject, target) {
|
|
7
|
+
if (
|
|
8
|
+
Object.prototype.hasOwnProperty.call(exportsObject, '__napiBindingTarget')
|
|
9
|
+
) {
|
|
10
|
+
if (exportsObject.__napiBindingTarget === target) {
|
|
11
|
+
// Already ours: the root entry aliases the object it loaded, so a WASI
|
|
12
|
+
// fallback candidate — or a `NAPI_RS_NATIVE_LIBRARY_PATH` override that
|
|
13
|
+
// is a generated loader — arrives already stamped with this same value.
|
|
14
|
+
return target
|
|
15
|
+
}
|
|
16
|
+
const error = new Error(
|
|
17
|
+
'`__napiBindingTarget` is reserved by the generated binding loader, but the loaded binding already exports it. Rename the export, e.g. #[napi(js_name = "...")].',
|
|
18
|
+
)
|
|
19
|
+
error.code = 'ERR_NAPI_BINDING_TARGET_CONFLICT'
|
|
20
|
+
throw error
|
|
21
|
+
}
|
|
22
|
+
if (!Object.isExtensible(exportsObject)) {
|
|
23
|
+
// A `#[napi(module_exports)]` hook may seal or freeze this object
|
|
24
|
+
// (`Object::seal` / `Object::freeze`). Reporting the artifact is metadata,
|
|
25
|
+
// never a reason to fail an otherwise successful load, so the stamp is
|
|
26
|
+
// skipped. What a consumer still sees then follows the entry point: the
|
|
27
|
+
// browser and deferred loaders declare `__napiBindingTarget` at module
|
|
28
|
+
// level and go on reporting it, while the CommonJS entries hand back this
|
|
29
|
+
// very object as `module.exports`, so there the value is absent.
|
|
30
|
+
return target
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
// [[Define]], not [[Set]]: an ordinary assignment walks the prototype
|
|
34
|
+
// chain, so an inherited accessor could swallow the value or throw and
|
|
35
|
+
// fail an otherwise successful load. The descriptor is what a successful
|
|
36
|
+
// assignment would have produced.
|
|
37
|
+
Object.defineProperty(exportsObject, '__napiBindingTarget', {
|
|
38
|
+
configurable: true,
|
|
39
|
+
enumerable: true,
|
|
40
|
+
value: target,
|
|
41
|
+
writable: true,
|
|
42
|
+
})
|
|
43
|
+
} catch {
|
|
44
|
+
// Same rule as the non-extensible skip above: reporting the artifact is
|
|
45
|
+
// metadata, never a reason to fail an otherwise successful load. An exotic
|
|
46
|
+
// object (a Proxy whose defineProperty trap refuses) is skipped, not
|
|
47
|
+
// thrown over.
|
|
48
|
+
}
|
|
49
|
+
// The CommonJS loaders assign this return value so `cjs-module-lexer` — and
|
|
50
|
+
// therefore Node's CJS -> ESM named export detection — can see
|
|
51
|
+
// `__napiBindingTarget` statically.
|
|
52
|
+
return target
|
|
53
|
+
}
|
|
54
|
+
|
|
7
55
|
const __nodeFs = require('node:fs')
|
|
8
56
|
const __nodePath = require('node:path')
|
|
9
57
|
const { WASI: __nodeWASI } = require('node:wasi')
|
|
@@ -91,6 +139,7 @@ function __createWasiWorker(filename) {
|
|
|
91
139
|
return new Worker(filename, {
|
|
92
140
|
env: process.env,
|
|
93
141
|
execArgv: __workerExecArgv,
|
|
142
|
+
workerData: { hostRoot: __hostRoot, rootDir: __rootDir },
|
|
94
143
|
})
|
|
95
144
|
} catch (error) {
|
|
96
145
|
if (!error || error.code !== 'ERR_WORKER_INVALID_EXEC_ARGV') {
|
|
@@ -106,14 +155,18 @@ function __createWasiWorker(filename) {
|
|
|
106
155
|
}
|
|
107
156
|
}
|
|
108
157
|
|
|
109
|
-
const
|
|
158
|
+
const __cwd = process.cwd()
|
|
159
|
+
const __rootDir = __nodePath.parse(__cwd).root
|
|
160
|
+
const __hostRoot =
|
|
161
|
+
process.platform === 'android' ? __cwd : __rootDir
|
|
110
162
|
|
|
111
163
|
const __wasi = new __nodeWASI({
|
|
112
164
|
version: 'preview1',
|
|
113
165
|
env: process.env,
|
|
114
166
|
preopens: {
|
|
115
|
-
[__rootDir]:
|
|
116
|
-
|
|
167
|
+
[__rootDir]: __hostRoot,
|
|
168
|
+
[__hostRoot]: __hostRoot,
|
|
169
|
+
},
|
|
117
170
|
})
|
|
118
171
|
|
|
119
172
|
const __sharedMemory = new WebAssembly.Memory({
|
|
@@ -146,20 +199,51 @@ let __emnapiContext
|
|
|
146
199
|
|
|
147
200
|
const __wasiDisposeSymbol = Symbol.for('napi.rs.wasi.dispose')
|
|
148
201
|
const __wasiWorkers = new Set()
|
|
202
|
+
// The thread manager has to be reachable *before* anything that can throw
|
|
203
|
+
// during load or registration. Initialization can fail after the pool has
|
|
204
|
+
// already spawned workers, and the rollback still has to mark their
|
|
205
|
+
// terminations as expected — but `__napiModule` is assigned only when
|
|
206
|
+
// instantiation RETURNS, so on exactly that path it is still undefined. A
|
|
207
|
+
// plugin factory runs while the emnapi module is being created, before the
|
|
208
|
+
// wasm is loaded and before any registration function runs, and its context
|
|
209
|
+
// carries the very same manager instance.
|
|
210
|
+
let __wasiThreadManager
|
|
211
|
+
|
|
212
|
+
function __captureWasiThreadManager(context) {
|
|
213
|
+
if (context && context.PThread) {
|
|
214
|
+
__wasiThreadManager = context.PThread
|
|
215
|
+
}
|
|
216
|
+
return {}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function __getWasiThreadManager() {
|
|
220
|
+
const manager =
|
|
221
|
+
__wasiThreadManager !== undefined
|
|
222
|
+
? __wasiThreadManager
|
|
223
|
+
: __napiModule
|
|
224
|
+
? __napiModule.PThread
|
|
225
|
+
: undefined
|
|
226
|
+
if (manager && typeof manager.terminateWorker === 'function') {
|
|
227
|
+
return manager
|
|
228
|
+
}
|
|
229
|
+
return undefined
|
|
230
|
+
}
|
|
149
231
|
let __napiInstance
|
|
150
232
|
let __emnapiContextDestroyed = false
|
|
151
233
|
let __emnapiContextDestroyPromise
|
|
152
234
|
let __emnapiWasmEnvCleanupPrepared = false
|
|
235
|
+
let __emnapiWasmEnvCleanupPreparing = false
|
|
153
236
|
let __emnapiWasmEnvCleanupRan = false
|
|
154
237
|
let __emnapiWasmEnvCleanupDrained = false
|
|
155
238
|
let __emnapiWasmEnvCleanupDrainPromise
|
|
156
239
|
let __wasiDisposed = false
|
|
240
|
+
let __wasiAsyncWorkDrainPromise
|
|
157
241
|
let __wasiDisposePromise
|
|
158
|
-
let __completeWasiDisposal = function() {}
|
|
242
|
+
let __completeWasiDisposal = function () {}
|
|
159
243
|
// Overridden by loader flavors that have a last-resort reclaim for a rollback
|
|
160
244
|
// that stopped short of destroying the context. See
|
|
161
245
|
// `__rollbackWasiInitialization`.
|
|
162
|
-
let __retainWasiRollbackForRetry = function() {}
|
|
246
|
+
let __retainWasiRollbackForRetry = function () {}
|
|
163
247
|
|
|
164
248
|
function __isThenable(value) {
|
|
165
249
|
return (
|
|
@@ -223,13 +307,58 @@ function __attachCleanupErrors(error, cleanupErrors) {
|
|
|
223
307
|
return aggregate
|
|
224
308
|
}
|
|
225
309
|
|
|
310
|
+
function __wrapEmnapiContextDestroyForSettlement(
|
|
311
|
+
context,
|
|
312
|
+
prepareEnvCleanup,
|
|
313
|
+
isPreparingEnvCleanup,
|
|
314
|
+
) {
|
|
315
|
+
let destroy
|
|
316
|
+
try {
|
|
317
|
+
destroy = context.destroy
|
|
318
|
+
} catch {
|
|
319
|
+
return context
|
|
320
|
+
}
|
|
321
|
+
if (typeof destroy !== 'function') {
|
|
322
|
+
return context
|
|
323
|
+
}
|
|
324
|
+
try {
|
|
325
|
+
Object.defineProperty(context, 'destroy', {
|
|
326
|
+
configurable: true,
|
|
327
|
+
enumerable: false,
|
|
328
|
+
writable: true,
|
|
329
|
+
value: function () {
|
|
330
|
+
// Reentered from a promise hook that fired inside the barrier: the
|
|
331
|
+
// frame running it destroys as soon as it returns.
|
|
332
|
+
if (isPreparingEnvCleanup?.()) {
|
|
333
|
+
return
|
|
334
|
+
}
|
|
335
|
+
prepareEnvCleanup?.()
|
|
336
|
+
return Reflect.apply(destroy, this, arguments)
|
|
337
|
+
},
|
|
338
|
+
})
|
|
339
|
+
} catch {}
|
|
340
|
+
return context
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
function __isPreparingWasmEnvCleanup() {
|
|
344
|
+
return __emnapiWasmEnvCleanupPreparing
|
|
345
|
+
}
|
|
346
|
+
|
|
226
347
|
function __prepareWasmEnvCleanup() {
|
|
227
|
-
if (__emnapiWasmEnvCleanupPrepared) {
|
|
348
|
+
if (__emnapiWasmEnvCleanupPrepared || __emnapiWasmEnvCleanupPreparing) {
|
|
228
349
|
return
|
|
229
350
|
}
|
|
230
351
|
const prepare = __napiInstance?.exports?.napi_prepare_wasm_env_cleanup
|
|
231
352
|
if (typeof prepare === 'function') {
|
|
232
|
-
|
|
353
|
+
// The addon settles the promises it cancels synchronously, under a
|
|
354
|
+
// non-reentrant lifecycle mutex: anything a promise hook calls from in
|
|
355
|
+
// here must not reach this export again.
|
|
356
|
+
__emnapiWasmEnvCleanupPreparing = true
|
|
357
|
+
try {
|
|
358
|
+
prepare()
|
|
359
|
+
} finally {
|
|
360
|
+
__emnapiWasmEnvCleanupPreparing = false
|
|
361
|
+
}
|
|
233
362
|
__emnapiWasmEnvCleanupRan = true
|
|
234
363
|
}
|
|
235
364
|
__emnapiWasmEnvCleanupPrepared = true
|
|
@@ -266,6 +395,24 @@ const __scheduleMacrotask = (function () {
|
|
|
266
395
|
}
|
|
267
396
|
})()
|
|
268
397
|
|
|
398
|
+
// A real, *referenced* timer, for waits that must let the whole host make
|
|
399
|
+
// progress between looks — the async-work drain polls the addon rather than
|
|
400
|
+
// interleaving with the @emnapi/core dispatch, so a zero-delay macrotask there
|
|
401
|
+
// would spin the loop instead of yielding it. Falls back to the macrotask
|
|
402
|
+
// scheduler on a host without timers.
|
|
403
|
+
function __scheduleTimer(callback, delay) {
|
|
404
|
+
const setTimer = globalThis.setTimeout
|
|
405
|
+
if (typeof setTimer !== 'function') {
|
|
406
|
+
__scheduleMacrotask(callback)
|
|
407
|
+
return
|
|
408
|
+
}
|
|
409
|
+
try {
|
|
410
|
+
setTimer(callback, delay)
|
|
411
|
+
} catch {
|
|
412
|
+
__scheduleMacrotask(callback)
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
|
|
269
416
|
// Turns to wait for while the addon still reports queued settlements. Reaching
|
|
270
417
|
// zero is the only success. A counter still nonzero at this bound rejects the
|
|
271
418
|
// disposal as retryable (`ERR_NAPI_WASI_CLEANUP_PENDING`) rather than
|
|
@@ -422,13 +569,209 @@ function __destroyEmnapiContext() {
|
|
|
422
569
|
return destroyPromise
|
|
423
570
|
}
|
|
424
571
|
|
|
572
|
+
/**
|
|
573
|
+
* Holds the event loop open until `work` settles.
|
|
574
|
+
*
|
|
575
|
+
* Nothing else can: the pool workers are deliberately unreferenced so an idle
|
|
576
|
+
* binding cannot keep a process alive, and referencing them again for the
|
|
577
|
+
* termination does not hold either — emnapi unreferences a worker the moment it
|
|
578
|
+
* reports `async-thread-ready`, which for a worker that was still starting
|
|
579
|
+
* lands *after* the termination began. Without a handle of its own, an
|
|
580
|
+
* `await dispose()` with nothing else pending exits the process with its
|
|
581
|
+
* promise unsettled, and everything after the `await` is skipped.
|
|
582
|
+
*
|
|
583
|
+
* The timer is cleared as soon as the work settles, so this never outlives the
|
|
584
|
+
* disposal that asked for it.
|
|
585
|
+
*/
|
|
586
|
+
function __keepEventLoopAliveUntil(work) {
|
|
587
|
+
const setTimer = globalThis.setInterval
|
|
588
|
+
const clearTimer = globalThis.clearInterval
|
|
589
|
+
if (typeof setTimer !== 'function' || typeof clearTimer !== 'function') {
|
|
590
|
+
return work
|
|
591
|
+
}
|
|
592
|
+
let timer
|
|
593
|
+
try {
|
|
594
|
+
timer = setTimer(function () {}, 50)
|
|
595
|
+
} catch {
|
|
596
|
+
return work
|
|
597
|
+
}
|
|
598
|
+
const release = function () {
|
|
599
|
+
try {
|
|
600
|
+
clearTimer(timer)
|
|
601
|
+
} catch {}
|
|
602
|
+
}
|
|
603
|
+
return work.then(
|
|
604
|
+
(value) => {
|
|
605
|
+
release()
|
|
606
|
+
return value
|
|
607
|
+
},
|
|
608
|
+
(error) => {
|
|
609
|
+
release()
|
|
610
|
+
throw error
|
|
611
|
+
},
|
|
612
|
+
)
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
// How often to re-read `napi_wasm_async_work_pending` while waiting. The wait
|
|
616
|
+
// ends when the addon reports zero, so this only decides how promptly disposal
|
|
617
|
+
// notices — not how long it waits.
|
|
618
|
+
const __WASI_ASYNC_WORK_POLL_INTERVAL_MS = 1
|
|
619
|
+
|
|
620
|
+
/**
|
|
621
|
+
* Settles this addon's outstanding `napi_async_work` before the teardown that
|
|
622
|
+
* would strand it.
|
|
623
|
+
*
|
|
624
|
+
* `napi_prepare_wasm_env_cleanup` does not cover async work, and nothing about
|
|
625
|
+
* it is observable from JavaScript: the threadless archive resolves
|
|
626
|
+
* `napi_*_async_work` through the `@emnapi/core` plugins, but the threaded one
|
|
627
|
+
* links the C `async_work.c` on the uv threadpool, so there the wasm neither
|
|
628
|
+
* imports nor exports those symbols and the only brackets a loader could watch
|
|
629
|
+
* (`_emnapi_ctx_*_waiting_request_counter`) are shared with threadsafe
|
|
630
|
+
* functions. The addon is the one place both flavors go through, so it answers
|
|
631
|
+
* for both, through the same kind of handshake the settlement drain uses:
|
|
632
|
+
*
|
|
633
|
+
* - `napi_wasm_cancel_pending_async_work()` cancels what no thread has
|
|
634
|
+
* started. Those completion callbacks run with `napi_cancelled`, which
|
|
635
|
+
* napi-rs turns into a promise rejected with an `AbortError`.
|
|
636
|
+
* - `napi_wasm_async_work_pending()` counts what is still owed a completion
|
|
637
|
+
* callback. Work already executing refuses cancellation and stays counted
|
|
638
|
+
* until it finishes normally — which it can, because this runs before the
|
|
639
|
+
* barrier, before `Context.destroy()` and before anything is terminated.
|
|
640
|
+
*
|
|
641
|
+
* Both exports are optional: an addon built against a napi crate that predates
|
|
642
|
+
* them drains nothing and keeps the previous behavior, exactly as the
|
|
643
|
+
* `napi_wasm_env_cleanup_pending` handshake degrades.
|
|
644
|
+
*
|
|
645
|
+
* Returns nothing when there is nothing outstanding, which keeps disposal
|
|
646
|
+
* synchronous in the common case. The promise it returns otherwise never
|
|
647
|
+
* rejects.
|
|
648
|
+
*
|
|
649
|
+
* The wait has no deadline, and that is the point: giving up would destroy the
|
|
650
|
+
* environment with a completion callback still owed, which is the stranding
|
|
651
|
+
* this exists to prevent. A task whose `execute` never returns already keeps an
|
|
652
|
+
* *undisposed* process alive in exactly the same way, so disposal inherits that
|
|
653
|
+
* rather than inventing a bound it cannot honor.
|
|
654
|
+
*
|
|
655
|
+
* Safe to call from inside a completion callback, which is reachable: settling
|
|
656
|
+
* a task runs addon code that can re-enter JavaScript — a setter on the value
|
|
657
|
+
* being handed back, a threadsafe-function callback — and that JavaScript can
|
|
658
|
+
* call `dispose()`. Two things make it terminate rather than wait on itself:
|
|
659
|
+
*
|
|
660
|
+
* - The addon keeps a work registered until its completion callback
|
|
661
|
+
* *finishes*, so the count read here is at least one and this takes the
|
|
662
|
+
* polling path instead of declaring the environment drained and tearing it
|
|
663
|
+
* down from inside the frame that is still settling a promise.
|
|
664
|
+
* - The poll is a timer, so it cannot run until the callback has returned to
|
|
665
|
+
* the host — by which time that work has left the registry. The count the
|
|
666
|
+
* next poll reads is the one taken after the callback finished.
|
|
667
|
+
*
|
|
668
|
+
* `__disposeWasiBinding` hands every caller the same in-flight promise, so the
|
|
669
|
+
* nested call joins this disposal rather than starting a second one.
|
|
670
|
+
*/
|
|
671
|
+
function __drainWasiAsyncWork() {
|
|
672
|
+
if (__wasiAsyncWorkDrainPromise !== undefined) {
|
|
673
|
+
return __wasiAsyncWorkDrainPromise
|
|
674
|
+
}
|
|
675
|
+
const exports = __napiInstance?.exports
|
|
676
|
+
const pending = exports?.napi_wasm_async_work_pending
|
|
677
|
+
const cancelPending = exports?.napi_wasm_cancel_pending_async_work
|
|
678
|
+
if (typeof pending !== 'function' || typeof cancelPending !== 'function') {
|
|
679
|
+
return
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
const readPending = () => {
|
|
683
|
+
try {
|
|
684
|
+
return pending()
|
|
685
|
+
} catch (error) {
|
|
686
|
+
// A trap is the only way this call fails: it reads a counter and cannot
|
|
687
|
+
// allocate or call back into JavaScript. A trapped instance can no longer
|
|
688
|
+
// run anything, so its outstanding work is unreachable by definition —
|
|
689
|
+
// there is nothing left to wait for, and refusing to dispose would only
|
|
690
|
+
// keep a dead instance and its stuck counter alive. Best-effort here is
|
|
691
|
+
// the honest answer, and it is what disposal did before this drain
|
|
692
|
+
// existed.
|
|
693
|
+
//
|
|
694
|
+
// Only a trap. Anything else means the export is not what this loader
|
|
695
|
+
// thinks it is, which is a defect worth surfacing rather than disposing
|
|
696
|
+
// over.
|
|
697
|
+
if (error instanceof globalThis.WebAssembly.RuntimeError) {
|
|
698
|
+
return 0
|
|
699
|
+
}
|
|
700
|
+
throw error
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
if (!readPending()) {
|
|
705
|
+
return
|
|
706
|
+
}
|
|
707
|
+
try {
|
|
708
|
+
cancelPending()
|
|
709
|
+
} catch {
|
|
710
|
+
// Cancellation is an optimization: it bounds the wait by the work already
|
|
711
|
+
// executing. Failing it only means waiting for the whole queue instead.
|
|
712
|
+
}
|
|
713
|
+
if (!readPending()) {
|
|
714
|
+
return
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
const drainPromise = __keepEventLoopAliveUntil(
|
|
718
|
+
(async () => {
|
|
719
|
+
while (readPending()) {
|
|
720
|
+
await new Promise((resolve) => {
|
|
721
|
+
__scheduleTimer(resolve, __WASI_ASYNC_WORK_POLL_INTERVAL_MS)
|
|
722
|
+
})
|
|
723
|
+
}
|
|
724
|
+
})(),
|
|
725
|
+
).then(
|
|
726
|
+
() => {
|
|
727
|
+
__wasiAsyncWorkDrainPromise = undefined
|
|
728
|
+
},
|
|
729
|
+
(error) => {
|
|
730
|
+
// A wait that could not run is not a wait that finished. The only way
|
|
731
|
+
// here is a host whose timers and macrotask primitives all refuse, and
|
|
732
|
+
// the work is still outstanding — reporting success would destroy the
|
|
733
|
+
// environment over it, which is the stranding this exists to prevent.
|
|
734
|
+
// Reject instead: disposal stays retryable, and the context is not
|
|
735
|
+
// destroyed. Clearing the memo first is what makes the retry re-run this.
|
|
736
|
+
__wasiAsyncWorkDrainPromise = undefined
|
|
737
|
+
throw error
|
|
738
|
+
},
|
|
739
|
+
)
|
|
740
|
+
__wasiAsyncWorkDrainPromise = drainPromise
|
|
741
|
+
return drainPromise
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
/**
|
|
745
|
+
* `@emnapi/wasi-threads` counts a worker exit as expected only when its own
|
|
746
|
+
* thread manager performed the termination. A bare `worker.terminate()` reaches
|
|
747
|
+
* the manager's `exit` listener instead, which reports
|
|
748
|
+
* `worker (tid = N) sent an error! ... stopped with exit code 1` and rethrows
|
|
749
|
+
* inside the emit — aborting the `once('exit')` that backs the terminate
|
|
750
|
+
* promise, so disposal never settles and the process dies with an uncaught
|
|
751
|
+
* exception. Mark the termination through the manager first.
|
|
752
|
+
*
|
|
753
|
+
* The manager comes from `__getWasiThreadManager`, not from `__napiModule`:
|
|
754
|
+
* the initialization rollback runs on the one path where instantiation never
|
|
755
|
+
* returned, so `__napiModule` is still undefined there while the workers it
|
|
756
|
+
* spawned are already registered and loaded.
|
|
757
|
+
*
|
|
758
|
+
* Not `terminateAllThreads()`: that one recreates the pool it just shut down.
|
|
759
|
+
*/
|
|
425
760
|
function __terminateWasiWorkers() {
|
|
426
761
|
const cleanupErrors = []
|
|
427
762
|
const pending = []
|
|
763
|
+
const threadManager = __getWasiThreadManager()
|
|
428
764
|
|
|
429
765
|
for (const worker of __wasiWorkers) {
|
|
430
766
|
let result
|
|
431
767
|
try {
|
|
768
|
+
if (threadManager) {
|
|
769
|
+
threadManager.terminateWorker(worker)
|
|
770
|
+
// `terminateWorker` leaves behind a reporter that logs every message
|
|
771
|
+
// still queued on the port, which Node flushes on exit. Nothing is
|
|
772
|
+
// listening for those any more.
|
|
773
|
+
worker.onmessage = undefined
|
|
774
|
+
}
|
|
432
775
|
result = worker.terminate()
|
|
433
776
|
} catch (error) {
|
|
434
777
|
cleanupErrors.push(error)
|
|
@@ -458,7 +801,9 @@ function __terminateWasiWorkers() {
|
|
|
458
801
|
)
|
|
459
802
|
}
|
|
460
803
|
}
|
|
461
|
-
return pending.length > 0
|
|
804
|
+
return pending.length > 0
|
|
805
|
+
? __keepEventLoopAliveUntil(Promise.all(pending)).then(finish)
|
|
806
|
+
: finish()
|
|
462
807
|
}
|
|
463
808
|
|
|
464
809
|
function __finishWasiDisposal() {
|
|
@@ -477,7 +822,7 @@ function __continueWasiDisposal() {
|
|
|
477
822
|
return __finishWasiDisposal()
|
|
478
823
|
}
|
|
479
824
|
|
|
480
|
-
function
|
|
825
|
+
function __cleanUpWasmEnvForWasiDisposal() {
|
|
481
826
|
// Run the pre-teardown barrier, then let the settlements it queued actually
|
|
482
827
|
// reach JavaScript, and only then destroy the environment. Doing these two
|
|
483
828
|
// back to back is what strands them.
|
|
@@ -489,6 +834,21 @@ function __startWasiDisposal() {
|
|
|
489
834
|
return __continueWasiDisposal()
|
|
490
835
|
}
|
|
491
836
|
|
|
837
|
+
function __startWasiDisposal() {
|
|
838
|
+
// Outstanding `napi_async_work` goes first, while the environment is still
|
|
839
|
+
// completely live: the completion callbacks run addon code, and everything
|
|
840
|
+
// after this point takes that away from them — the barrier shuts the async
|
|
841
|
+
// runtime down, `Context.destroy()` stops JavaScript calls, and terminating
|
|
842
|
+
// the pool threads removes what would have reported the work finished.
|
|
843
|
+
const asyncWorkResult = __drainWasiAsyncWork()
|
|
844
|
+
if (__isThenable(asyncWorkResult)) {
|
|
845
|
+
return Promise.resolve(asyncWorkResult).then(
|
|
846
|
+
__cleanUpWasmEnvForWasiDisposal,
|
|
847
|
+
)
|
|
848
|
+
}
|
|
849
|
+
return __cleanUpWasmEnvForWasiDisposal()
|
|
850
|
+
}
|
|
851
|
+
|
|
492
852
|
/**
|
|
493
853
|
* Disposes this generated WASI binding.
|
|
494
854
|
*
|
|
@@ -630,29 +990,60 @@ function __retainFailedWasiRollback(cleanupErrors) {
|
|
|
630
990
|
* bug with no upper bound, while the retained bookkeeping is bounded by the page.
|
|
631
991
|
*/
|
|
632
992
|
function __rollbackWasiInitialization() {
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
993
|
+
// The environment teardown this rollback performs, kept nested so it cannot
|
|
994
|
+
// be reached without the async-work drain below running first.
|
|
995
|
+
function __rollbackWasmEnvForWasiInitialization() {
|
|
996
|
+
const cleanupErrors = []
|
|
997
|
+
let drainResult
|
|
998
|
+
let settlementsUnreached = false
|
|
999
|
+
try {
|
|
1000
|
+
__prepareWasmEnvCleanup()
|
|
1001
|
+
drainResult = __drainWasmEnvCleanup()
|
|
1002
|
+
} catch (cleanupError) {
|
|
1003
|
+
cleanupErrors.push(cleanupError)
|
|
1004
|
+
settlementsUnreached = true
|
|
1005
|
+
}
|
|
1006
|
+
if (__isThenable(drainResult)) {
|
|
1007
|
+
return Promise.resolve(drainResult).then(
|
|
1008
|
+
() => __destroyContextForWasiRollback(cleanupErrors),
|
|
1009
|
+
(cleanupError) => {
|
|
1010
|
+
cleanupErrors.push(cleanupError)
|
|
1011
|
+
return __retainFailedWasiRollback(cleanupErrors)
|
|
1012
|
+
},
|
|
1013
|
+
)
|
|
1014
|
+
}
|
|
1015
|
+
if (settlementsUnreached) {
|
|
1016
|
+
return __retainFailedWasiRollback(cleanupErrors)
|
|
1017
|
+
}
|
|
1018
|
+
return __destroyContextForWasiRollback(cleanupErrors)
|
|
1019
|
+
}
|
|
1020
|
+
|
|
1021
|
+
// Same reason as `__startWasiDisposal`: a module-init hook can start async
|
|
1022
|
+
// work before the load goes on to fail, and this rollback tears down exactly
|
|
1023
|
+
// what those completions need. Settle them while everything is still live,
|
|
1024
|
+
// before the barrier and the teardown above take that away.
|
|
1025
|
+
//
|
|
1026
|
+
// A drain that could not finish leaves async work possibly outstanding, and
|
|
1027
|
+
// destroying the context over it would strand exactly what this rollback is
|
|
1028
|
+
// there to settle. Stop short and retain instead — the same trade
|
|
1029
|
+
// `__rollbackWasmEnvForWasiInitialization` makes for the settlement drain, so
|
|
1030
|
+
// the context stays reclaimable by a retry or by this flavor's own
|
|
1031
|
+
// last-resort teardown.
|
|
1032
|
+
const __retainAfterAsyncWorkDrainFailure = (cleanupError) =>
|
|
1033
|
+
__retainFailedWasiRollback([cleanupError])
|
|
1034
|
+
let asyncWorkResult
|
|
636
1035
|
try {
|
|
637
|
-
|
|
638
|
-
drainResult = __drainWasmEnvCleanup()
|
|
1036
|
+
asyncWorkResult = __drainWasiAsyncWork()
|
|
639
1037
|
} catch (cleanupError) {
|
|
640
|
-
|
|
641
|
-
settlementsUnreached = true
|
|
1038
|
+
return __retainAfterAsyncWorkDrainFailure(cleanupError)
|
|
642
1039
|
}
|
|
643
|
-
if (__isThenable(
|
|
644
|
-
return Promise.resolve(
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
cleanupErrors.push(cleanupError)
|
|
648
|
-
return __retainFailedWasiRollback(cleanupErrors)
|
|
649
|
-
},
|
|
1040
|
+
if (__isThenable(asyncWorkResult)) {
|
|
1041
|
+
return Promise.resolve(asyncWorkResult).then(
|
|
1042
|
+
__rollbackWasmEnvForWasiInitialization,
|
|
1043
|
+
__retainAfterAsyncWorkDrainFailure,
|
|
650
1044
|
)
|
|
651
1045
|
}
|
|
652
|
-
|
|
653
|
-
return __retainFailedWasiRollback(cleanupErrors)
|
|
654
|
-
}
|
|
655
|
-
return __destroyContextForWasiRollback(cleanupErrors)
|
|
1046
|
+
return __rollbackWasmEnvForWasiInitialization()
|
|
656
1047
|
}
|
|
657
1048
|
|
|
658
1049
|
const __wasiRollbackRegistrySymbol = Symbol.for('napi.rs.wasi.rollback.registry.v1')
|
|
@@ -828,7 +1219,11 @@ function __captureEmnapiAutoDestroyListener() {
|
|
|
828
1219
|
try {
|
|
829
1220
|
const __finishAutoDestroyCapture = __captureEmnapiAutoDestroyListener()
|
|
830
1221
|
try {
|
|
831
|
-
__emnapiContext =
|
|
1222
|
+
__emnapiContext = __wrapEmnapiContextDestroyForSettlement(
|
|
1223
|
+
__emnapiCreateContext({ autoDestroy: false }),
|
|
1224
|
+
__prepareWasmEnvCleanup,
|
|
1225
|
+
__isPreparingWasmEnvCleanup,
|
|
1226
|
+
)
|
|
832
1227
|
// emnapi 2.x still registers an unconditional once-listener for
|
|
833
1228
|
// beforeExit that auto-destroys the context, and suppressDestroy() only
|
|
834
1229
|
// neutralizes its callback without removing it. This loader owns cleanup
|
|
@@ -846,7 +1241,7 @@ try {
|
|
|
846
1241
|
napiModule: __napiModule,
|
|
847
1242
|
} = __emnapiInstantiateNapiModuleSync(__wasmFile, {
|
|
848
1243
|
context: __emnapiContext,
|
|
849
|
-
asyncWorkPoolSize: (function() {
|
|
1244
|
+
asyncWorkPoolSize: (function () {
|
|
850
1245
|
const threadsSizeFromEnv = Number(process.env.NAPI_RS_ASYNC_WORK_POOL_SIZE ?? process.env.UV_THREADPOOL_SIZE)
|
|
851
1246
|
// NaN > 0 is false
|
|
852
1247
|
if (threadsSizeFromEnv > 0) {
|
|
@@ -856,7 +1251,11 @@ try {
|
|
|
856
1251
|
}
|
|
857
1252
|
})(),
|
|
858
1253
|
reuseWorker: true,
|
|
859
|
-
plugins: [
|
|
1254
|
+
plugins: [
|
|
1255
|
+
__captureWasiThreadManager,
|
|
1256
|
+
__emnapiAsyncWorkPlugin,
|
|
1257
|
+
__emnapiTSFNPlugin,
|
|
1258
|
+
],
|
|
860
1259
|
wasi: __wasi,
|
|
861
1260
|
onCreateWorker() {
|
|
862
1261
|
const worker = __createWasiWorker(__nodePath.join(__dirname, 'wasi-worker.mjs'))
|
|
@@ -871,21 +1270,25 @@ try {
|
|
|
871
1270
|
// According to https://github.com/nodejs/node/blob/19e0d472728c79d418b74bddff588bea70a403d0/lib/internal/worker.js#L415,
|
|
872
1271
|
// a worker is consist of two handles: kPublicPort and kHandle.
|
|
873
1272
|
{
|
|
874
|
-
const kPublicPort = Object.getOwnPropertySymbols(worker).find(s =>
|
|
875
|
-
s.toString().includes(
|
|
876
|
-
)
|
|
1273
|
+
const kPublicPort = Object.getOwnPropertySymbols(worker).find((s) =>
|
|
1274
|
+
s.toString().includes('kPublicPort'),
|
|
1275
|
+
)
|
|
877
1276
|
if (kPublicPort) {
|
|
878
|
-
worker[kPublicPort].ref = () => {}
|
|
1277
|
+
worker[kPublicPort].ref = () => {}
|
|
879
1278
|
}
|
|
880
1279
|
|
|
881
|
-
const kHandle = Object.getOwnPropertySymbols(worker).find(s =>
|
|
882
|
-
s.toString().includes(
|
|
883
|
-
)
|
|
1280
|
+
const kHandle = Object.getOwnPropertySymbols(worker).find((s) =>
|
|
1281
|
+
s.toString().includes('kHandle'),
|
|
1282
|
+
)
|
|
884
1283
|
if (kHandle) {
|
|
885
|
-
worker[kHandle].ref = () => {}
|
|
1284
|
+
worker[kHandle].ref = () => {}
|
|
886
1285
|
}
|
|
887
1286
|
|
|
888
|
-
worker.unref()
|
|
1287
|
+
worker.unref()
|
|
1288
|
+
// These stubs stay in place for the worker's whole life, disposal
|
|
1289
|
+
// included: `__keepEventLoopAliveUntil` is what holds the process open
|
|
1290
|
+
// while a termination is pending, precisely because a worker's own
|
|
1291
|
+
// references cannot be relied on for it.
|
|
889
1292
|
}
|
|
890
1293
|
return worker
|
|
891
1294
|
},
|
|
@@ -908,6 +1311,22 @@ try {
|
|
|
908
1311
|
},
|
|
909
1312
|
}))
|
|
910
1313
|
__publishWasiDispose(__napiModule.exports)
|
|
1314
|
+
// The CommonJS tail below aliases `__napiModule.exports`; a named module
|
|
1315
|
+
// export does not travel with it, so carry the marker on the binding itself
|
|
1316
|
+
// too. Three things pin the stamp to exactly this spot:
|
|
1317
|
+
// - inside this `try`, because the guard throws on a
|
|
1318
|
+
// `#[napi(module_exports)]` hook that claimed the name, and only the
|
|
1319
|
+
// catch below tears the environment — context, workers, exit listener —
|
|
1320
|
+
// back down;
|
|
1321
|
+
// - after the async runtime host install, which hands this same object to
|
|
1322
|
+
// addon-provided registration functions that may put anything on it;
|
|
1323
|
+
// - assigning onto the loader's own `module.exports`, which is still the
|
|
1324
|
+
// original object here, so an addon accessor with a refusing setter is
|
|
1325
|
+
// never written through. `cjs-module-lexer` — Node's CJS -> ESM named
|
|
1326
|
+
// export detection — reads the static `module.exports.<name> =` either
|
|
1327
|
+
// way, and the later `module.exports = __napiModule.exports` does not
|
|
1328
|
+
// undo that.
|
|
1329
|
+
module.exports.__napiBindingTarget = __napiStampBindingTarget(__napiModule.exports, __napiBindingTarget)
|
|
911
1330
|
__registerWasiExitListener()
|
|
912
1331
|
} catch (error) {
|
|
913
1332
|
const rollback = {
|