@oxc-coverage-instrument/binding-wasm32-wasi 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/README.md +10 -0
- package/coverage-instrument.wasi-browser.js +64 -0
- package/coverage-instrument.wasi.cjs +115 -0
- package/coverage-instrument.wasm32-wasi.wasm +0 -0
- package/package.json +36 -0
- package/wasi-worker-browser.mjs +36 -0
- package/wasi-worker.mjs +63 -0
package/README.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# `@oxc-coverage-instrument/binding-wasm32-wasi`
|
|
2
|
+
|
|
3
|
+
This is the **wasm32-wasip1-threads** binding for `@oxc-coverage-instrument/binding`. It is selected automatically by the host package `oxc-coverage-instrument` when:
|
|
4
|
+
|
|
5
|
+
- the host runtime has no matching native binding (e.g., browsers with `SharedArrayBuffer` enabled, Deno using the `node:wasi` shim), OR
|
|
6
|
+
- the consumer sets `NAPI_RS_FORCE_WASI=1` explicitly to bypass the native binding.
|
|
7
|
+
|
|
8
|
+
**Currently unsupported runtimes**: Cloudflare Workers lacks `SharedArrayBuffer`, which `wasm32-wasip1-threads` requires. Bun's `node:wasi` is incomplete (no `wasi.initialize()`), so the wasm binding does not load there; Bun on its officially-supported platforms uses the native binding instead.
|
|
9
|
+
|
|
10
|
+
See the main package's README for the full runtime support matrix.
|
|
@@ -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
|
|
Binary file
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@oxc-coverage-instrument/binding-wasm32-wasi",
|
|
3
|
+
"version": "0.7.1",
|
|
4
|
+
"main": "coverage-instrument.wasi.cjs",
|
|
5
|
+
"browser": "coverage-instrument.wasi-browser.js",
|
|
6
|
+
"files": [
|
|
7
|
+
"coverage-instrument.wasm32-wasi.wasm",
|
|
8
|
+
"coverage-instrument.wasi.cjs",
|
|
9
|
+
"coverage-instrument.wasi-browser.js",
|
|
10
|
+
"wasi-worker.mjs",
|
|
11
|
+
"wasi-worker-browser.mjs"
|
|
12
|
+
],
|
|
13
|
+
"description": "Istanbul-compatible JavaScript/TypeScript coverage instrumentation using the Oxc AST. 8-48x faster than istanbul-lib-instrument.",
|
|
14
|
+
"keywords": [
|
|
15
|
+
"oxc",
|
|
16
|
+
"istanbul",
|
|
17
|
+
"coverage",
|
|
18
|
+
"javascript",
|
|
19
|
+
"typescript",
|
|
20
|
+
"instrumentation",
|
|
21
|
+
"code-coverage",
|
|
22
|
+
"wasm",
|
|
23
|
+
"wasi"
|
|
24
|
+
],
|
|
25
|
+
"homepage": "https://github.com/fallow-rs/oxc-coverage-instrument",
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "git+https://github.com/fallow-rs/oxc-coverage-instrument.git",
|
|
30
|
+
"directory": "napi"
|
|
31
|
+
},
|
|
32
|
+
"bugs": "https://github.com/fallow-rs/oxc-coverage-instrument/issues",
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@napi-rs/wasm-runtime": "^1.1.4"
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -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
|
+
}
|
package/wasi-worker.mjs
ADDED
|
@@ -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
|
+
};
|