Skip to content

Commit 96e3572

Browse files
committed
test browser compatitable worker js file
1 parent 16732c1 commit 96e3572

File tree

2 files changed

+79
-62
lines changed

2 files changed

+79
-62
lines changed

packages/test/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"cmake-js": "^7.1.1",
1111
"cross-env": "^7.0.3",
1212
"glob": "^7.2.0",
13+
"memfs-browser": "^3.4.13000",
1314
"node-addon-api": "6.0.0",
1415
"why-is-node-running": "^2.2.2"
1516
},

packages/test/worker.js

Lines changed: 78 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,89 @@
1-
// globalThis.ENVIRONMENT_IS_PTHREAD = true
1+
/* eslint-disable no-eval */
2+
/* eslint-disable no-undef */
23

3-
// const log = (...args) => {
4-
// const str = require('util').format(...args)
5-
// require('fs').writeSync(1, str + '\n')
6-
// }
7-
// const error = (...args) => {
8-
// const str = require('util').format(...args)
9-
// require('fs').writeSync(2, str + '\n')
10-
// }
4+
(function () {
5+
// const log = (...args) => {
6+
// const str = require('util').format(...args)
7+
// require('fs').writeSync(1, str + '\n')
8+
// }
9+
// const error = (...args) => {
10+
// const str = require('util').format(...args)
11+
// require('fs').writeSync(2, str + '\n')
12+
// }
13+
let fs, WASI
1114

12-
const nodeWorkerThreads = require('worker_threads')
15+
const ENVIRONMENT_IS_NODE =
16+
typeof process === 'object' && process !== null &&
17+
typeof process.versions === 'object' && process.versions !== null &&
18+
typeof process.versions.node === 'string'
1319

14-
const parentPort = nodeWorkerThreads.parentPort
20+
if (ENVIRONMENT_IS_NODE) {
21+
const nodeWorkerThreads = require('worker_threads')
1522

16-
parentPort.on('message', (data) => {
17-
onmessage({ data })
18-
})
23+
const parentPort = nodeWorkerThreads.parentPort
1924

20-
const fs = require('fs')
25+
parentPort.on('message', (data) => {
26+
globalThis.onmessage({ data })
27+
})
2128

22-
Object.assign(global, {
23-
self: global,
24-
require,
25-
// Module,
26-
location: {
27-
href: __filename
28-
},
29-
Worker: nodeWorkerThreads.Worker,
30-
importScripts: function (f) {
31-
// eslint-disable-next-line no-eval
32-
(0, eval)(fs.readFileSync(f, 'utf8') + '//# sourceURL=' + f)
33-
},
34-
postMessage: function (msg) {
35-
parentPort.postMessage(msg)
36-
},
37-
performance: global.performance || {
38-
now: function () {
39-
return Date.now()
40-
}
29+
fs = require('fs')
30+
31+
Object.assign(globalThis, {
32+
self: globalThis,
33+
require,
34+
Worker: nodeWorkerThreads.Worker,
35+
importScripts: function (f) {
36+
(0, eval)(fs.readFileSync(f, 'utf8') + '//# sourceURL=' + f)
37+
},
38+
postMessage: function (msg) {
39+
parentPort.postMessage(msg)
40+
}
41+
})
42+
43+
WASI = require('./wasi').WASI
44+
} else {
45+
importScripts('../../node_modules/memfs-browser/dist/memfs.js')
46+
importScripts('../../node_modules/@tybys/wasm-util/dist/wasm-util.js')
47+
48+
const { Volume, createFsFromVolume } = memfs
49+
fs = createFsFromVolume(Volume.from({
50+
'/': null
51+
}))
52+
53+
WASI = globalThis.wasmUtil.WASI
4154
}
42-
})
4355

44-
const { WASI } = require('./wasi')
45-
const { loadNapiModuleSync, handleMessage } = require('@emnapi/core')
56+
importScripts('../../node_modules/@emnapi/core/dist/emnapi-core.js')
4657

47-
function instantiate (wasmMemory, wasmModule, tid, arg) {
48-
const wasi = new WASI({
49-
fs,
50-
print (...args) {
51-
const str = require('util').format(...args)
52-
fs.writeSync(1, str + '\n')
53-
}
54-
})
58+
const { loadNapiModuleSync, handleMessage } = globalThis.emnapiCore
5559

56-
loadNapiModuleSync(wasmModule, {
57-
childThread: true,
58-
wasi,
59-
overwriteImports (importObject) {
60-
importObject.env.memory = wasmMemory
61-
},
62-
tid,
63-
arg
64-
})
65-
}
60+
function onLoad (payload) {
61+
const wasi = new WASI({
62+
fs,
63+
print: ENVIRONMENT_IS_NODE
64+
? (...args) => {
65+
const str = require('util').format(...args)
66+
fs.writeSync(1, str + '\n')
67+
}
68+
: function () { console.log.apply(console, arguments) }
69+
})
6670

67-
self.onmessage = function (e) {
68-
handleMessage(e, (type, payload) => {
69-
if (type === 'load') {
70-
instantiate(payload.wasmMemory, payload.wasmModule, payload.tid, payload.arg)
71-
}
72-
})
73-
}
71+
loadNapiModuleSync(payload.wasmModule, {
72+
childThread: true,
73+
wasi,
74+
overwriteImports (importObject) {
75+
importObject.env.memory = payload.wasmMemory
76+
},
77+
tid: payload.tid,
78+
arg: payload.arg
79+
})
80+
}
81+
82+
globalThis.onmessage = function (e) {
83+
handleMessage(e, (type, payload) => {
84+
if (type === 'load') {
85+
onLoad(payload)
86+
}
87+
})
88+
}
89+
})()

0 commit comments

Comments
 (0)