|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +// Workaround for an upstream incompatibility in `@oxc-parser/binding-wasm32-wasi` when |
| 10 | +// falling back to WebAssembly (e.g., inside WebContainers or StackBlitz). |
| 11 | +// |
| 12 | +// `@oxc-parser/binding-wasm32-wasi` declares a dependency on `@emnapi/core@2.0.0-alpha.6`, |
| 13 | +// but depends on `@napi-rs/wasm-runtime@~1.2.6` which expects `@emnapi/core@>=2.0.0-beta.2`. |
| 14 | +// In `@napi-rs/wasm-runtime@1.2.6`, the runtime attempts to assign `initWorker` to |
| 15 | +// `emnapi_shared.addonModule`, which is undefined in `alpha.6` where the property was named |
| 16 | +// `napiModule`. This results in `TypeError: Cannot set properties of undefined (setting 'initWorker')`, |
| 17 | +// causing WASI binding initialization to fail and surface as a misleading "WASI binding not found" error |
| 18 | +// or an `ERR_REQUIRE_CYCLE_MODULE` failure. |
| 19 | +// |
| 20 | +// Aliasing `addonModule` to `this.napiModule` on `Object.prototype` allows the WASI runtime to |
| 21 | +// initialize successfully. |
| 22 | +// |
| 23 | +// See: https://github.com/oxc-project/oxc/issues/28717 and https://github.com/napi-rs/napi-rs/issues/3674 |
| 24 | +if ( |
| 25 | + (globalThis.process?.versions?.['webcontainer'] || |
| 26 | + globalThis.process?.env?.['NAPI_RS_FORCE_WASI'] === 'true') && |
| 27 | + !('addonModule' in Object.prototype) |
| 28 | +) { |
| 29 | + Object.defineProperty(Object.prototype, 'addonModule', { |
| 30 | + get() { |
| 31 | + return (this as { napiModule?: unknown }).napiModule; |
| 32 | + }, |
| 33 | + set(value) { |
| 34 | + Object.defineProperty(this, 'addonModule', { |
| 35 | + value, |
| 36 | + writable: true, |
| 37 | + configurable: true, |
| 38 | + enumerable: true, |
| 39 | + }); |
| 40 | + }, |
| 41 | + configurable: true, |
| 42 | + enumerable: false, |
| 43 | + }); |
| 44 | +} |
0 commit comments