oxc-coverage-instrument 0.6.0 → 0.7.1

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/browser.js ADDED
@@ -0,0 +1 @@
1
+ export * from '@oxc-coverage-instrument/binding-wasm32-wasi'
@@ -0,0 +1,64 @@
1
+ import {
2
+ createOnMessage as __wasmCreateOnMessageForFsProxy,
3
+ getDefaultContext as __emnapiGetDefaultContext,
4
+ instantiateNapiModuleSync as __emnapiInstantiateNapiModuleSync,
5
+ WASI as __WASI,
6
+ } from '@napi-rs/wasm-runtime'
7
+
8
+
9
+
10
+ const __wasi = new __WASI({
11
+ version: 'preview1',
12
+ })
13
+
14
+ const __wasmUrl = new URL('./coverage-instrument.wasm32-wasi.wasm', import.meta.url).href
15
+ const __emnapiContext = __emnapiGetDefaultContext()
16
+
17
+
18
+ const __sharedMemory = new WebAssembly.Memory({
19
+ initial: 4000,
20
+ maximum: 65536,
21
+ shared: true,
22
+ })
23
+
24
+ const __wasmFile = await fetch(__wasmUrl).then((res) => res.arrayBuffer())
25
+
26
+ const {
27
+ instance: __napiInstance,
28
+ module: __wasiModule,
29
+ napiModule: __napiModule,
30
+ } = __emnapiInstantiateNapiModuleSync(__wasmFile, {
31
+ context: __emnapiContext,
32
+ asyncWorkPoolSize: 4,
33
+ wasi: __wasi,
34
+ onCreateWorker() {
35
+ const worker = new Worker(new URL('./wasi-worker-browser.mjs', import.meta.url), {
36
+ type: 'module',
37
+ })
38
+
39
+
40
+ return worker
41
+ },
42
+ overwriteImports(importObject) {
43
+ importObject.env = {
44
+ ...importObject.env,
45
+ ...importObject.napi,
46
+ ...importObject.emnapi,
47
+ memory: __sharedMemory,
48
+ }
49
+ return importObject
50
+ },
51
+ beforeInit({ instance }) {
52
+ for (const name of Object.keys(instance.exports)) {
53
+ if (name.startsWith('__napi_register__')) {
54
+ instance.exports[name]()
55
+ }
56
+ }
57
+ },
58
+ })
59
+ export default __napiModule.exports
60
+ export const instrument = __napiModule.exports.instrument
61
+ export const remapCoverageMap = __napiModule.exports.remapCoverageMap
62
+ export const remapCoverageMapWithLoader = __napiModule.exports.remapCoverageMapWithLoader
63
+ export const v8ToIstanbul = __napiModule.exports.v8ToIstanbul
64
+ export const v8ToIstanbulWithLoader = __napiModule.exports.v8ToIstanbulWithLoader
@@ -0,0 +1,115 @@
1
+ /* eslint-disable */
2
+ /* prettier-ignore */
3
+
4
+ /* auto-generated by NAPI-RS */
5
+
6
+ const __nodeFs = require('node:fs')
7
+ const __nodePath = require('node:path')
8
+ const { WASI: __nodeWASI } = require('node:wasi')
9
+ const { Worker } = require('node:worker_threads')
10
+
11
+ const {
12
+ createOnMessage: __wasmCreateOnMessageForFsProxy,
13
+ getDefaultContext: __emnapiGetDefaultContext,
14
+ instantiateNapiModuleSync: __emnapiInstantiateNapiModuleSync,
15
+ } = require('@napi-rs/wasm-runtime')
16
+
17
+ const __rootDir = __nodePath.parse(process.cwd()).root
18
+
19
+ const __wasi = new __nodeWASI({
20
+ version: 'preview1',
21
+ env: process.env,
22
+ preopens: {
23
+ [__rootDir]: __rootDir,
24
+ }
25
+ })
26
+
27
+ const __emnapiContext = __emnapiGetDefaultContext()
28
+
29
+ const __sharedMemory = new WebAssembly.Memory({
30
+ initial: 4000,
31
+ maximum: 65536,
32
+ shared: true,
33
+ })
34
+
35
+ let __wasmFilePath = __nodePath.join(__dirname, 'coverage-instrument.wasm32-wasi.wasm')
36
+ const __wasmDebugFilePath = __nodePath.join(__dirname, 'coverage-instrument.wasm32-wasi.debug.wasm')
37
+
38
+ if (__nodeFs.existsSync(__wasmDebugFilePath)) {
39
+ __wasmFilePath = __wasmDebugFilePath
40
+ } else if (!__nodeFs.existsSync(__wasmFilePath)) {
41
+ try {
42
+ __wasmFilePath = require.resolve('@oxc-coverage-instrument/binding-wasm32-wasi/coverage-instrument.wasm32-wasi.wasm')
43
+ } catch {
44
+ throw new Error('Cannot find coverage-instrument.wasm32-wasi.wasm file, and @oxc-coverage-instrument/binding-wasm32-wasi package is not installed.')
45
+ }
46
+ }
47
+
48
+ const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule } = __emnapiInstantiateNapiModuleSync(__nodeFs.readFileSync(__wasmFilePath), {
49
+ context: __emnapiContext,
50
+ asyncWorkPoolSize: (function() {
51
+ const threadsSizeFromEnv = Number(process.env.NAPI_RS_ASYNC_WORK_POOL_SIZE ?? process.env.UV_THREADPOOL_SIZE)
52
+ // NaN > 0 is false
53
+ if (threadsSizeFromEnv > 0) {
54
+ return threadsSizeFromEnv
55
+ } else {
56
+ return 4
57
+ }
58
+ })(),
59
+ reuseWorker: true,
60
+ wasi: __wasi,
61
+ onCreateWorker() {
62
+ const worker = new Worker(__nodePath.join(__dirname, 'wasi-worker.mjs'), {
63
+ env: process.env,
64
+ })
65
+ worker.onmessage = ({ data }) => {
66
+ __wasmCreateOnMessageForFsProxy(__nodeFs)(data)
67
+ }
68
+
69
+ // The main thread of Node.js waits for all the active handles before exiting.
70
+ // But Rust threads are never waited without `thread::join`.
71
+ // So here we hack the code of Node.js to prevent the workers from being referenced (active).
72
+ // According to https://github.com/nodejs/node/blob/19e0d472728c79d418b74bddff588bea70a403d0/lib/internal/worker.js#L415,
73
+ // a worker is consist of two handles: kPublicPort and kHandle.
74
+ {
75
+ const kPublicPort = Object.getOwnPropertySymbols(worker).find(s =>
76
+ s.toString().includes("kPublicPort")
77
+ );
78
+ if (kPublicPort) {
79
+ worker[kPublicPort].ref = () => {};
80
+ }
81
+
82
+ const kHandle = Object.getOwnPropertySymbols(worker).find(s =>
83
+ s.toString().includes("kHandle")
84
+ );
85
+ if (kHandle) {
86
+ worker[kHandle].ref = () => {};
87
+ }
88
+
89
+ worker.unref();
90
+ }
91
+ return worker
92
+ },
93
+ overwriteImports(importObject) {
94
+ importObject.env = {
95
+ ...importObject.env,
96
+ ...importObject.napi,
97
+ ...importObject.emnapi,
98
+ memory: __sharedMemory,
99
+ }
100
+ return importObject
101
+ },
102
+ beforeInit({ instance }) {
103
+ for (const name of Object.keys(instance.exports)) {
104
+ if (name.startsWith('__napi_register__')) {
105
+ instance.exports[name]()
106
+ }
107
+ }
108
+ },
109
+ })
110
+ module.exports = __napiModule.exports
111
+ module.exports.instrument = __napiModule.exports.instrument
112
+ module.exports.remapCoverageMap = __napiModule.exports.remapCoverageMap
113
+ module.exports.remapCoverageMapWithLoader = __napiModule.exports.remapCoverageMapWithLoader
114
+ module.exports.v8ToIstanbul = __napiModule.exports.v8ToIstanbul
115
+ module.exports.v8ToIstanbulWithLoader = __napiModule.exports.v8ToIstanbulWithLoader
package/index.d.ts CHANGED
@@ -30,6 +30,48 @@ export interface InstrumentOptions {
30
30
  * not be executable as JavaScript (no error is returned).
31
31
  */
32
32
  stripTypescript?: boolean
33
+ /**
34
+ * When true, lower TypeScript `experimentalDecorators` syntax
35
+ * (`@Injectable()` / `@Controller()` style used by NestJS, Angular,
36
+ * class-validator, TypeORM) into runtime `_decorate(...)`
37
+ * calls. Mirrors the `experimentalDecorators` flag in `tsconfig.json`.
38
+ *
39
+ * The instrumented output references imports from `@oxc-project/runtime` at
40
+ * runtime; install `@oxc-project/runtime` (or provide an equivalent shim).
41
+ * See the README for details and troubleshooting.
42
+ *
43
+ * Has no effect unless `stripTypescript` is also true. Defaults to false.
44
+ */
45
+ experimentalDecorators?: boolean
46
+ /**
47
+ * When true, emit TypeScript-style decorator metadata
48
+ * (`design:type`, `design:paramtypes`, `design:returntype`) alongside
49
+ * each decorated class / method / property. Required for NestJS
50
+ * dependency injection, TypeORM column type inference, and
51
+ * class-validator's metadata-driven validation. Mirrors the
52
+ * `emitDecoratorMetadata` flag in `tsconfig.json`.
53
+ *
54
+ * Requires `experimentalDecorators: true`; passing
55
+ * `emitDecoratorMetadata: true` with `experimentalDecorators: false`
56
+ * (or omitted) is invalid and throws a JS `Error` instead of being
57
+ * silently promoted. The instrumented output requires
58
+ * `@oxc-project/runtime` at execution; see the README.
59
+ *
60
+ * Has no effect unless `stripTypescript` is also true. Defaults to false.
61
+ */
62
+ emitDecoratorMetadata?: boolean
63
+ /**
64
+ * When true, attach an optional `x_fallow_functionMap` overlay to the
65
+ * returned coverage map. The overlay carries a stable
66
+ * `fallow:fn:<hex>` identity per function, keyed by the same ids as
67
+ * `fnMap`, derived from `(path, name, decl span, loc span)`. Standard
68
+ * Istanbul consumers ignore the `x_`-prefixed field; downstream code
69
+ * quality tools (Fallow et al.) use it as a long-lived join key.
70
+ *
71
+ * Defaults to false. The default JSON output stays byte-identical to
72
+ * what Istanbul consumers expect.
73
+ */
74
+ functionIdentityOverlay?: boolean
33
75
  }
34
76
 
35
77
  /** Result of instrumenting a source file. */
package/index.js CHANGED
@@ -77,8 +77,8 @@ function requireNative() {
77
77
  try {
78
78
  const binding = require('@oxc-coverage-instrument/binding-android-arm64')
79
79
  const bindingPackageVersion = require('@oxc-coverage-instrument/binding-android-arm64/package.json').version
80
- if (bindingPackageVersion !== '0.6.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
81
- throw new Error(`Native binding package version mismatch, expected 0.6.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
80
+ if (bindingPackageVersion !== '0.7.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
81
+ throw new Error(`Native binding package version mismatch, expected 0.7.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
82
82
  }
83
83
  return binding
84
84
  } catch (e) {
@@ -93,8 +93,8 @@ function requireNative() {
93
93
  try {
94
94
  const binding = require('@oxc-coverage-instrument/binding-android-arm-eabi')
95
95
  const bindingPackageVersion = require('@oxc-coverage-instrument/binding-android-arm-eabi/package.json').version
96
- if (bindingPackageVersion !== '0.6.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
97
- throw new Error(`Native binding package version mismatch, expected 0.6.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
96
+ if (bindingPackageVersion !== '0.7.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
97
+ throw new Error(`Native binding package version mismatch, expected 0.7.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
98
98
  }
99
99
  return binding
100
100
  } catch (e) {
@@ -114,8 +114,8 @@ function requireNative() {
114
114
  try {
115
115
  const binding = require('@oxc-coverage-instrument/binding-win32-x64-gnu')
116
116
  const bindingPackageVersion = require('@oxc-coverage-instrument/binding-win32-x64-gnu/package.json').version
117
- if (bindingPackageVersion !== '0.6.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
118
- throw new Error(`Native binding package version mismatch, expected 0.6.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
117
+ if (bindingPackageVersion !== '0.7.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
118
+ throw new Error(`Native binding package version mismatch, expected 0.7.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
119
119
  }
120
120
  return binding
121
121
  } catch (e) {
@@ -130,8 +130,8 @@ function requireNative() {
130
130
  try {
131
131
  const binding = require('@oxc-coverage-instrument/binding-win32-x64-msvc')
132
132
  const bindingPackageVersion = require('@oxc-coverage-instrument/binding-win32-x64-msvc/package.json').version
133
- if (bindingPackageVersion !== '0.6.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
134
- throw new Error(`Native binding package version mismatch, expected 0.6.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
133
+ if (bindingPackageVersion !== '0.7.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
134
+ throw new Error(`Native binding package version mismatch, expected 0.7.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
135
135
  }
136
136
  return binding
137
137
  } catch (e) {
@@ -147,8 +147,8 @@ function requireNative() {
147
147
  try {
148
148
  const binding = require('@oxc-coverage-instrument/binding-win32-ia32-msvc')
149
149
  const bindingPackageVersion = require('@oxc-coverage-instrument/binding-win32-ia32-msvc/package.json').version
150
- if (bindingPackageVersion !== '0.6.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
151
- throw new Error(`Native binding package version mismatch, expected 0.6.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
150
+ if (bindingPackageVersion !== '0.7.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
151
+ throw new Error(`Native binding package version mismatch, expected 0.7.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
152
152
  }
153
153
  return binding
154
154
  } catch (e) {
@@ -163,8 +163,8 @@ function requireNative() {
163
163
  try {
164
164
  const binding = require('@oxc-coverage-instrument/binding-win32-arm64-msvc')
165
165
  const bindingPackageVersion = require('@oxc-coverage-instrument/binding-win32-arm64-msvc/package.json').version
166
- if (bindingPackageVersion !== '0.6.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
167
- throw new Error(`Native binding package version mismatch, expected 0.6.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
166
+ if (bindingPackageVersion !== '0.7.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
167
+ throw new Error(`Native binding package version mismatch, expected 0.7.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
168
168
  }
169
169
  return binding
170
170
  } catch (e) {
@@ -182,8 +182,8 @@ function requireNative() {
182
182
  try {
183
183
  const binding = require('@oxc-coverage-instrument/binding-darwin-universal')
184
184
  const bindingPackageVersion = require('@oxc-coverage-instrument/binding-darwin-universal/package.json').version
185
- if (bindingPackageVersion !== '0.6.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
186
- throw new Error(`Native binding package version mismatch, expected 0.6.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
185
+ if (bindingPackageVersion !== '0.7.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
186
+ throw new Error(`Native binding package version mismatch, expected 0.7.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
187
187
  }
188
188
  return binding
189
189
  } catch (e) {
@@ -198,8 +198,8 @@ function requireNative() {
198
198
  try {
199
199
  const binding = require('@oxc-coverage-instrument/binding-darwin-x64')
200
200
  const bindingPackageVersion = require('@oxc-coverage-instrument/binding-darwin-x64/package.json').version
201
- if (bindingPackageVersion !== '0.6.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
202
- throw new Error(`Native binding package version mismatch, expected 0.6.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
201
+ if (bindingPackageVersion !== '0.7.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
202
+ throw new Error(`Native binding package version mismatch, expected 0.7.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
203
203
  }
204
204
  return binding
205
205
  } catch (e) {
@@ -214,8 +214,8 @@ function requireNative() {
214
214
  try {
215
215
  const binding = require('@oxc-coverage-instrument/binding-darwin-arm64')
216
216
  const bindingPackageVersion = require('@oxc-coverage-instrument/binding-darwin-arm64/package.json').version
217
- if (bindingPackageVersion !== '0.6.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
218
- throw new Error(`Native binding package version mismatch, expected 0.6.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
217
+ if (bindingPackageVersion !== '0.7.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
218
+ throw new Error(`Native binding package version mismatch, expected 0.7.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
219
219
  }
220
220
  return binding
221
221
  } catch (e) {
@@ -234,8 +234,8 @@ function requireNative() {
234
234
  try {
235
235
  const binding = require('@oxc-coverage-instrument/binding-freebsd-x64')
236
236
  const bindingPackageVersion = require('@oxc-coverage-instrument/binding-freebsd-x64/package.json').version
237
- if (bindingPackageVersion !== '0.6.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
238
- throw new Error(`Native binding package version mismatch, expected 0.6.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
237
+ if (bindingPackageVersion !== '0.7.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
238
+ throw new Error(`Native binding package version mismatch, expected 0.7.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
239
239
  }
240
240
  return binding
241
241
  } catch (e) {
@@ -250,8 +250,8 @@ function requireNative() {
250
250
  try {
251
251
  const binding = require('@oxc-coverage-instrument/binding-freebsd-arm64')
252
252
  const bindingPackageVersion = require('@oxc-coverage-instrument/binding-freebsd-arm64/package.json').version
253
- if (bindingPackageVersion !== '0.6.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
254
- throw new Error(`Native binding package version mismatch, expected 0.6.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
253
+ if (bindingPackageVersion !== '0.7.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
254
+ throw new Error(`Native binding package version mismatch, expected 0.7.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
255
255
  }
256
256
  return binding
257
257
  } catch (e) {
@@ -271,8 +271,8 @@ function requireNative() {
271
271
  try {
272
272
  const binding = require('@oxc-coverage-instrument/binding-linux-x64-musl')
273
273
  const bindingPackageVersion = require('@oxc-coverage-instrument/binding-linux-x64-musl/package.json').version
274
- if (bindingPackageVersion !== '0.6.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
275
- throw new Error(`Native binding package version mismatch, expected 0.6.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
274
+ if (bindingPackageVersion !== '0.7.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
275
+ throw new Error(`Native binding package version mismatch, expected 0.7.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
276
276
  }
277
277
  return binding
278
278
  } catch (e) {
@@ -287,8 +287,8 @@ function requireNative() {
287
287
  try {
288
288
  const binding = require('@oxc-coverage-instrument/binding-linux-x64-gnu')
289
289
  const bindingPackageVersion = require('@oxc-coverage-instrument/binding-linux-x64-gnu/package.json').version
290
- if (bindingPackageVersion !== '0.6.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
291
- throw new Error(`Native binding package version mismatch, expected 0.6.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
290
+ if (bindingPackageVersion !== '0.7.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
291
+ throw new Error(`Native binding package version mismatch, expected 0.7.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
292
292
  }
293
293
  return binding
294
294
  } catch (e) {
@@ -305,8 +305,8 @@ function requireNative() {
305
305
  try {
306
306
  const binding = require('@oxc-coverage-instrument/binding-linux-arm64-musl')
307
307
  const bindingPackageVersion = require('@oxc-coverage-instrument/binding-linux-arm64-musl/package.json').version
308
- if (bindingPackageVersion !== '0.6.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
309
- throw new Error(`Native binding package version mismatch, expected 0.6.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
308
+ if (bindingPackageVersion !== '0.7.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
309
+ throw new Error(`Native binding package version mismatch, expected 0.7.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
310
310
  }
311
311
  return binding
312
312
  } catch (e) {
@@ -321,8 +321,8 @@ function requireNative() {
321
321
  try {
322
322
  const binding = require('@oxc-coverage-instrument/binding-linux-arm64-gnu')
323
323
  const bindingPackageVersion = require('@oxc-coverage-instrument/binding-linux-arm64-gnu/package.json').version
324
- if (bindingPackageVersion !== '0.6.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
325
- throw new Error(`Native binding package version mismatch, expected 0.6.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
324
+ if (bindingPackageVersion !== '0.7.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
325
+ throw new Error(`Native binding package version mismatch, expected 0.7.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
326
326
  }
327
327
  return binding
328
328
  } catch (e) {
@@ -339,8 +339,8 @@ function requireNative() {
339
339
  try {
340
340
  const binding = require('@oxc-coverage-instrument/binding-linux-arm-musleabihf')
341
341
  const bindingPackageVersion = require('@oxc-coverage-instrument/binding-linux-arm-musleabihf/package.json').version
342
- if (bindingPackageVersion !== '0.6.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
343
- throw new Error(`Native binding package version mismatch, expected 0.6.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
342
+ if (bindingPackageVersion !== '0.7.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
343
+ throw new Error(`Native binding package version mismatch, expected 0.7.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
344
344
  }
345
345
  return binding
346
346
  } catch (e) {
@@ -355,8 +355,8 @@ function requireNative() {
355
355
  try {
356
356
  const binding = require('@oxc-coverage-instrument/binding-linux-arm-gnueabihf')
357
357
  const bindingPackageVersion = require('@oxc-coverage-instrument/binding-linux-arm-gnueabihf/package.json').version
358
- if (bindingPackageVersion !== '0.6.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
359
- throw new Error(`Native binding package version mismatch, expected 0.6.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
358
+ if (bindingPackageVersion !== '0.7.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
359
+ throw new Error(`Native binding package version mismatch, expected 0.7.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
360
360
  }
361
361
  return binding
362
362
  } catch (e) {
@@ -373,8 +373,8 @@ function requireNative() {
373
373
  try {
374
374
  const binding = require('@oxc-coverage-instrument/binding-linux-loong64-musl')
375
375
  const bindingPackageVersion = require('@oxc-coverage-instrument/binding-linux-loong64-musl/package.json').version
376
- if (bindingPackageVersion !== '0.6.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
377
- throw new Error(`Native binding package version mismatch, expected 0.6.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
376
+ if (bindingPackageVersion !== '0.7.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
377
+ throw new Error(`Native binding package version mismatch, expected 0.7.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
378
378
  }
379
379
  return binding
380
380
  } catch (e) {
@@ -389,8 +389,8 @@ function requireNative() {
389
389
  try {
390
390
  const binding = require('@oxc-coverage-instrument/binding-linux-loong64-gnu')
391
391
  const bindingPackageVersion = require('@oxc-coverage-instrument/binding-linux-loong64-gnu/package.json').version
392
- if (bindingPackageVersion !== '0.6.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
393
- throw new Error(`Native binding package version mismatch, expected 0.6.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
392
+ if (bindingPackageVersion !== '0.7.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
393
+ throw new Error(`Native binding package version mismatch, expected 0.7.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
394
394
  }
395
395
  return binding
396
396
  } catch (e) {
@@ -407,8 +407,8 @@ function requireNative() {
407
407
  try {
408
408
  const binding = require('@oxc-coverage-instrument/binding-linux-riscv64-musl')
409
409
  const bindingPackageVersion = require('@oxc-coverage-instrument/binding-linux-riscv64-musl/package.json').version
410
- if (bindingPackageVersion !== '0.6.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
411
- throw new Error(`Native binding package version mismatch, expected 0.6.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
410
+ if (bindingPackageVersion !== '0.7.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
411
+ throw new Error(`Native binding package version mismatch, expected 0.7.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
412
412
  }
413
413
  return binding
414
414
  } catch (e) {
@@ -423,8 +423,8 @@ function requireNative() {
423
423
  try {
424
424
  const binding = require('@oxc-coverage-instrument/binding-linux-riscv64-gnu')
425
425
  const bindingPackageVersion = require('@oxc-coverage-instrument/binding-linux-riscv64-gnu/package.json').version
426
- if (bindingPackageVersion !== '0.6.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
427
- throw new Error(`Native binding package version mismatch, expected 0.6.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
426
+ if (bindingPackageVersion !== '0.7.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
427
+ throw new Error(`Native binding package version mismatch, expected 0.7.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
428
428
  }
429
429
  return binding
430
430
  } catch (e) {
@@ -440,8 +440,8 @@ function requireNative() {
440
440
  try {
441
441
  const binding = require('@oxc-coverage-instrument/binding-linux-ppc64-gnu')
442
442
  const bindingPackageVersion = require('@oxc-coverage-instrument/binding-linux-ppc64-gnu/package.json').version
443
- if (bindingPackageVersion !== '0.6.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
444
- throw new Error(`Native binding package version mismatch, expected 0.6.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
443
+ if (bindingPackageVersion !== '0.7.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
444
+ throw new Error(`Native binding package version mismatch, expected 0.7.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
445
445
  }
446
446
  return binding
447
447
  } catch (e) {
@@ -456,8 +456,8 @@ function requireNative() {
456
456
  try {
457
457
  const binding = require('@oxc-coverage-instrument/binding-linux-s390x-gnu')
458
458
  const bindingPackageVersion = require('@oxc-coverage-instrument/binding-linux-s390x-gnu/package.json').version
459
- if (bindingPackageVersion !== '0.6.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
460
- throw new Error(`Native binding package version mismatch, expected 0.6.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
459
+ if (bindingPackageVersion !== '0.7.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
460
+ throw new Error(`Native binding package version mismatch, expected 0.7.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
461
461
  }
462
462
  return binding
463
463
  } catch (e) {
@@ -476,8 +476,8 @@ function requireNative() {
476
476
  try {
477
477
  const binding = require('@oxc-coverage-instrument/binding-openharmony-arm64')
478
478
  const bindingPackageVersion = require('@oxc-coverage-instrument/binding-openharmony-arm64/package.json').version
479
- if (bindingPackageVersion !== '0.6.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
480
- throw new Error(`Native binding package version mismatch, expected 0.6.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
479
+ if (bindingPackageVersion !== '0.7.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
480
+ throw new Error(`Native binding package version mismatch, expected 0.7.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
481
481
  }
482
482
  return binding
483
483
  } catch (e) {
@@ -492,8 +492,8 @@ function requireNative() {
492
492
  try {
493
493
  const binding = require('@oxc-coverage-instrument/binding-openharmony-x64')
494
494
  const bindingPackageVersion = require('@oxc-coverage-instrument/binding-openharmony-x64/package.json').version
495
- if (bindingPackageVersion !== '0.6.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
496
- throw new Error(`Native binding package version mismatch, expected 0.6.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
495
+ if (bindingPackageVersion !== '0.7.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
496
+ throw new Error(`Native binding package version mismatch, expected 0.7.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
497
497
  }
498
498
  return binding
499
499
  } catch (e) {
@@ -508,8 +508,8 @@ function requireNative() {
508
508
  try {
509
509
  const binding = require('@oxc-coverage-instrument/binding-openharmony-arm')
510
510
  const bindingPackageVersion = require('@oxc-coverage-instrument/binding-openharmony-arm/package.json').version
511
- if (bindingPackageVersion !== '0.6.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
512
- throw new Error(`Native binding package version mismatch, expected 0.6.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
511
+ if (bindingPackageVersion !== '0.7.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
512
+ throw new Error(`Native binding package version mismatch, expected 0.7.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
513
513
  }
514
514
  return binding
515
515
  } catch (e) {
package/package.json CHANGED
@@ -1,12 +1,14 @@
1
1
  {
2
2
  "name": "oxc-coverage-instrument",
3
- "version": "0.6.0",
3
+ "version": "0.7.1",
4
4
  "description": "Istanbul-compatible JavaScript/TypeScript coverage instrumentation using the Oxc AST. 8-48x faster than istanbul-lib-instrument.",
5
5
  "main": "index.js",
6
+ "browser": "browser.js",
6
7
  "types": "index.d.ts",
7
8
  "exports": {
8
9
  ".": {
9
10
  "types": "./index.d.ts",
11
+ "browser": "./browser.js",
10
12
  "default": "./index.js"
11
13
  },
12
14
  "./vitest": {
@@ -17,6 +19,11 @@
17
19
  "files": [
18
20
  "index.js",
19
21
  "index.d.ts",
22
+ "browser.js",
23
+ "coverage-instrument.wasi.cjs",
24
+ "coverage-instrument.wasi-browser.js",
25
+ "wasi-worker.mjs",
26
+ "wasi-worker-browser.mjs",
20
27
  "vitest.js",
21
28
  "vitest.d.ts"
22
29
  ],
@@ -47,9 +54,13 @@
47
54
  "aarch64-unknown-linux-gnu",
48
55
  "x86_64-unknown-linux-musl",
49
56
  "x86_64-pc-windows-msvc",
50
- "aarch64-pc-windows-msvc"
57
+ "aarch64-pc-windows-msvc",
58
+ "wasm32-wasip1-threads"
51
59
  ]
52
60
  },
61
+ "dependencies": {
62
+ "@napi-rs/wasm-runtime": "^1.1.4"
63
+ },
53
64
  "devDependencies": {
54
65
  "@napi-rs/cli": "^3.6.0",
55
66
  "istanbul-lib-instrument": "^6.0.3"
@@ -62,12 +73,13 @@
62
73
  "test": "node test.mjs"
63
74
  },
64
75
  "optionalDependencies": {
65
- "@oxc-coverage-instrument/binding-darwin-arm64": "0.6.0",
66
- "@oxc-coverage-instrument/binding-darwin-x64": "0.6.0",
67
- "@oxc-coverage-instrument/binding-linux-arm64-gnu": "0.6.0",
68
- "@oxc-coverage-instrument/binding-linux-x64-gnu": "0.6.0",
69
- "@oxc-coverage-instrument/binding-linux-x64-musl": "0.6.0",
70
- "@oxc-coverage-instrument/binding-win32-arm64-msvc": "0.6.0",
71
- "@oxc-coverage-instrument/binding-win32-x64-msvc": "0.6.0"
76
+ "@oxc-coverage-instrument/binding-darwin-arm64": "0.7.1",
77
+ "@oxc-coverage-instrument/binding-darwin-x64": "0.7.1",
78
+ "@oxc-coverage-instrument/binding-linux-arm64-gnu": "0.7.1",
79
+ "@oxc-coverage-instrument/binding-linux-x64-gnu": "0.7.1",
80
+ "@oxc-coverage-instrument/binding-linux-x64-musl": "0.7.1",
81
+ "@oxc-coverage-instrument/binding-win32-arm64-msvc": "0.7.1",
82
+ "@oxc-coverage-instrument/binding-win32-x64-msvc": "0.7.1",
83
+ "@oxc-coverage-instrument/binding-wasm32-wasi": "0.7.1"
72
84
  }
73
85
  }
package/vitest.d.ts CHANGED
@@ -11,6 +11,11 @@ export interface OxcInstrumenterOptions {
11
11
  ignoreClassMethods?: string[]
12
12
  /** When true, adds truthy-value tracking (bT) for logical expressions. */
13
13
  reportLogic?: boolean
14
+ /**
15
+ * Attach the optional `x_fallow_functionMap` extension to the last file
16
+ * coverage object. Defaults to false.
17
+ */
18
+ functionIdentityOverlay?: boolean
14
19
  /**
15
20
  * Run the TypeScript-strip pass before instrumentation. When omitted
16
21
  * (default), the adapter auto-detects: it strips when the filename matches
package/vitest.js CHANGED
@@ -52,6 +52,23 @@ const TS_EXTENSION_REGEX = /\.([mc]ts|tsx?)$/i;
52
52
  * strict because callers passing a string like `'auto'` (a prior design
53
53
  * discussion shape) would otherwise be silently coerced to `Boolean('auto')`
54
54
  * which is `true`, force-stripping every file.
55
+ * @param {boolean} [options.experimentalDecorators] Lower legacy
56
+ * `experimentalDecorators` syntax into `_decorate(...)` calls (matches the
57
+ * `experimentalDecorators` flag in `tsconfig.json`). Defaults to false; pass
58
+ * true for NestJS / Angular / class-validator / TypeORM. Only active when
59
+ * the strip pass runs. Requires `@oxc-project/runtime` at execution; see the
60
+ * README's "Legacy decorators" section. No auto-detect from `tsconfig.json`
61
+ * in this version; set explicitly when you need it.
62
+ * @param {boolean} [options.emitDecoratorMetadata] Emit
63
+ * `_decorateMetadata("design:type" / "design:paramtypes" / "design:returntype", ...)`
64
+ * alongside each decorated member (matches `emitDecoratorMetadata` in
65
+ * `tsconfig.json`). Required for NestJS DI and TypeORM column type inference.
66
+ * Setting this to true auto-promotes `experimentalDecorators` to true at
67
+ * this layer, matching tsconfig.json semantics. The bare `instrument()`
68
+ * napi entry point rejects the same combination with an Error; the vitest
69
+ * adapter preserves the tsconfig-style ergonomics for the vitest UX.
70
+ * @param {boolean} [options.functionIdentityOverlay] Attach the optional
71
+ * `x_fallow_functionMap` extension to the last file coverage object.
55
72
  * @returns {{ instrumentSync, lastSourceMap, lastFileCoverage }}
56
73
  */
57
74
  function createOxcInstrumenter(options) {
@@ -59,12 +76,36 @@ function createOxcInstrumenter(options) {
59
76
  const coverageVariable = options.coverageVariable || '__coverage__';
60
77
  const ignoreClassMethods = options.ignoreClassMethods || [];
61
78
  const reportLogic = options.reportLogic || false;
79
+ const functionIdentityOverlay = options.functionIdentityOverlay || false;
80
+ if (typeof functionIdentityOverlay !== 'boolean') {
81
+ throw new TypeError(
82
+ `oxc-coverage-instrument: createOxcInstrumenter({ functionIdentityOverlay }) must be a boolean or undefined, got ${typeof options.functionIdentityOverlay}`,
83
+ );
84
+ }
62
85
  const stripTypescriptOverride = options.stripTypescript;
63
86
  if (stripTypescriptOverride !== undefined && typeof stripTypescriptOverride !== 'boolean') {
64
87
  throw new TypeError(
65
88
  `oxc-coverage-instrument: createOxcInstrumenter({ stripTypescript }) must be a boolean or undefined, got ${typeof stripTypescriptOverride}`,
66
89
  );
67
90
  }
91
+ const experimentalDecoratorsRaw = options.experimentalDecorators || false;
92
+ if (typeof experimentalDecoratorsRaw !== 'boolean') {
93
+ throw new TypeError(
94
+ `oxc-coverage-instrument: createOxcInstrumenter({ experimentalDecorators }) must be a boolean or undefined, got ${typeof options.experimentalDecorators}`,
95
+ );
96
+ }
97
+ const emitDecoratorMetadata = options.emitDecoratorMetadata || false;
98
+ if (typeof emitDecoratorMetadata !== 'boolean') {
99
+ throw new TypeError(
100
+ `oxc-coverage-instrument: createOxcInstrumenter({ emitDecoratorMetadata }) must be a boolean or undefined, got ${typeof options.emitDecoratorMetadata}`,
101
+ );
102
+ }
103
+ // Auto-promote at the vitest layer: setting emitDecoratorMetadata alone is a
104
+ // tsconfig.json idiom (TypeScript itself silently enables experimentalDecorators
105
+ // in that case). The bare napi `instrument()` rejects the same combination so
106
+ // the underlying Rust DecoratorMode enum stays unrepresentable-invalid; this
107
+ // adapter is the place to mirror tsconfig ergonomics for vitest users.
108
+ const experimentalDecorators = experimentalDecoratorsRaw || emitDecoratorMetadata;
68
109
 
69
110
  // Raw JSON strings from the last instrument call — parsed lazily on first access.
70
111
  let _lastCoverageMapJson = null;
@@ -103,6 +144,9 @@ function createOxcInstrumenter(options) {
103
144
  reportLogic,
104
145
  ignoreClassMethods,
105
146
  stripTypescript,
147
+ experimentalDecorators,
148
+ emitDecoratorMetadata,
149
+ functionIdentityOverlay,
106
150
  });
107
151
 
108
152
  // Store raw JSON — defer parsing until actually needed.
@@ -0,0 +1,36 @@
1
+ import { instantiateNapiModuleSync, MessageHandler, WASI } from '@napi-rs/wasm-runtime'
2
+
3
+ const errorOutputs = []
4
+
5
+ const handler = new MessageHandler({
6
+ onLoad({ wasmModule, wasmMemory }) {
7
+ const wasi = new WASI({
8
+ print: function () {
9
+ // eslint-disable-next-line no-console
10
+ console.log.apply(console, arguments)
11
+ },
12
+ printErr: function() {
13
+ // eslint-disable-next-line no-console
14
+ console.error.apply(console, arguments)
15
+
16
+ },
17
+ })
18
+ return instantiateNapiModuleSync(wasmModule, {
19
+ childThread: true,
20
+ wasi,
21
+ overwriteImports(importObject) {
22
+ importObject.env = {
23
+ ...importObject.env,
24
+ ...importObject.napi,
25
+ ...importObject.emnapi,
26
+ memory: wasmMemory,
27
+ }
28
+ },
29
+ })
30
+ },
31
+
32
+ })
33
+
34
+ globalThis.onmessage = function (e) {
35
+ handler.handle(e)
36
+ }
@@ -0,0 +1,63 @@
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 { instantiateNapiModuleSync, MessageHandler, getDefaultContext } = require("@napi-rs/wasm-runtime");
10
+
11
+ if (parentPort) {
12
+ parentPort.on("message", (data) => {
13
+ globalThis.onmessage({ data });
14
+ });
15
+ }
16
+
17
+ Object.assign(globalThis, {
18
+ self: globalThis,
19
+ require,
20
+ Worker,
21
+ importScripts: function (f) {
22
+ ;(0, eval)(fs.readFileSync(f, "utf8") + "//# sourceURL=" + f);
23
+ },
24
+ postMessage: function (msg) {
25
+ if (parentPort) {
26
+ parentPort.postMessage(msg);
27
+ }
28
+ },
29
+ });
30
+
31
+ const emnapiContext = getDefaultContext();
32
+
33
+ const __rootDir = parse(process.cwd()).root;
34
+
35
+ const handler = new MessageHandler({
36
+ onLoad({ wasmModule, wasmMemory }) {
37
+ const wasi = new WASI({
38
+ version: 'preview1',
39
+ env: process.env,
40
+ preopens: {
41
+ [__rootDir]: __rootDir,
42
+ },
43
+ });
44
+
45
+ return instantiateNapiModuleSync(wasmModule, {
46
+ childThread: true,
47
+ wasi,
48
+ context: emnapiContext,
49
+ overwriteImports(importObject) {
50
+ importObject.env = {
51
+ ...importObject.env,
52
+ ...importObject.napi,
53
+ ...importObject.emnapi,
54
+ memory: wasmMemory
55
+ };
56
+ },
57
+ });
58
+ },
59
+ });
60
+
61
+ globalThis.onmessage = function (e) {
62
+ handler.handle(e);
63
+ };