-
Notifications
You must be signed in to change notification settings - Fork 63
/
worker.js
63 lines (52 loc) · 2.19 KB
/
worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// The worker has its own scope and no direct access to functions/objects of the
// global scope. We import the generated JS file to make `wasm_bindgen`
// available which we need to initialize our WASM code.
const { parentPort } = require('node:worker_threads');
const Comlink = require('comlink');
const nodeEndpoint = require('comlink/dist/umd/node-adapter.js');
const { Client } = require('./worker-wasm/index.js');
// WebSocket was added to Node in v22. Polyfill it for older versions.
if (!global.WebSocket) {
console.debug("Polyfilling WebSocket");
// Provide a global WebSocket implementation, which is expected by the WASM code built for browsers.
const { w3cwebsocket } = require('websocket');
global.WebSocket = w3cwebsocket;
}
// Workaround for Node.js as it currently lacks support for Web Workers by pretending there is
// a WorkerGlobalScope object available which is checked within the libp2p's websocket-websys transport.
global.WorkerGlobalScope = global;
// Defined both here and in main thread exports.js
Comlink.transferHandlers.set('function', {
canHandle: (_obj) => false, // Cannot send functions to main thread
deserialize(port) {
return Comlink.transferHandlers.get('proxy').deserialize(port);
},
});
Comlink.transferHandlers.set('plain', {
canHandle: (_obj) => false, // Cannot send class instances to main thread
deserialize(plain) {
return plain;
},
});
let initialized = false;
async function init(config) {
if (initialized) throw new Error('Already initialized');
initialized = true;
console.log('Initializing WASM worker');
const client = await Client.create(config);
Comlink.expose(client, nodeEndpoint(parentPort));
};
parentPort.addListener('message', async (event) => {
const { type } = event;
if (type !== 'NIMIQ_INIT') return;
let { config } = event;
if (!config || typeof config !== 'object') config = {};
try {
await init(config);
parentPort.postMessage({ ok: true });
} catch (error) {
parentPort.postMessage({ ok: false, error: error.message, stack: error.stack });
}
});
parentPort.postMessage('NIMIQ_ONLOAD');
console.log('Launched WASM worker, ready for init');