forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.js
89 lines (76 loc) · 2.86 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
'use strict';
const {
Int32Array,
ReflectApply,
SafeWeakMap,
globalThis: {
Atomics,
},
} = primordials;
const {
ERR_INVALID_RETURN_VALUE,
} = require('internal/errors').codes;
// Create this WeakMap in js-land because V8 has no C++ API for WeakMap.
internalBinding('module_wrap').callbackMap = new SafeWeakMap();
const { isMainThread, workerData } = require('worker_threads');
if (isMainThread) { return; } // Needed to pass some tests that happen to load this file on the main thread
// lock = 0 -> main sleeps
// lock = 1 -> worker sleeps
const lock = new Int32Array(workerData.lock); // Required by Atomics
const { syncCommPort } = workerData; // To receive work signals.
function releaseLock() {
Atomics.store(lock, 0, 1); // Send response to main
Atomics.notify(lock, 0); // Notify main of new response
}
/**
* ! Run everything possible within this function so errors get reported.
*/
(async function setupESMWorker() {
const { initializeESM, initializeHooks } = require('internal/modules/esm/utils');
let initializationError, hooks;
try {
initializeESM();
hooks = await initializeHooks();
} catch (exception) {
// If there was an error while parsing and executing a user loader, for example if because a loader contained a syntax error,
// then we need to send the error to the main thread so it can be thrown and printed.
initializationError = exception;
}
syncCommPort.on('message', handleSyncMessage);
// ! Put as little above this line as possible
releaseLock(); // Send 'ready' signal to main
const { deserialize, serialize } = require('v8');
async function handleSyncMessage() {
const { method, args } = deserialize(data);
TypedArrayPrototypeFill(data, 0);
// Each potential exception needs to be caught individually so that the correct error is sent to the main thread
let response;
if (initializationError) {
response = initializationError;
} else {
try {
response = await ReflectApply(hooks[method], hooks, args);
if (!response) {
throw new ERR_INVALID_RETURN_VALUE('object', method, response)
}
} catch (exception) {
response = exception;
}
}
// Send the method response (or exception) to the main thread
try {
syncCommPort.postMessage(response);
} catch (exception) {
// Or send the exception thrown when trying to send the response
syncCommPort.postMessage(exception);
}
releaseLock();
}
})().catch((err) => {
// The triggerUncaughtException call below does not terminate the process or surface errors to the user;
// putting process._rawDebug here as a temporary workaround to ensure that the error is printed.
process._rawDebug('Uncaught exception in setupESMWorker:', err);
const { triggerUncaughtException } = internalBinding('errors');
releaseLock();
triggerUncaughtException(err);
});