@@ -32,6 +32,11 @@ const {
32
32
const { initializeHooks } = require ( 'internal/modules/esm/utils' ) ;
33
33
const { isMarkedAsUntransferable } = require ( 'internal/buffer' ) ;
34
34
35
+ /**
36
+ * Transfers an ArrayBuffer, TypedArray, or DataView to a worker thread.
37
+ * @param {boolean } hasError - Whether an error occurred during transfer.
38
+ * @param {ArrayBuffer | TypedArray | DataView } source - The data to transfer.
39
+ */
35
40
function transferArrayBuffer ( hasError , source ) {
36
41
if ( hasError || source == null ) { return ; }
37
42
let arrayBuffer ;
@@ -47,6 +52,11 @@ function transferArrayBuffer(hasError, source) {
47
52
}
48
53
}
49
54
55
+ /**
56
+ * Wraps a message with a status and body, and serializes the body if necessary.
57
+ * @param {string } status - The status of the message.
58
+ * @param {unknown } body - The body of the message.
59
+ */
50
60
function wrapMessage ( status , body ) {
51
61
if ( status === 'success' || body === null ||
52
62
( typeof body !== 'object' &&
@@ -73,6 +83,14 @@ function wrapMessage(status, body) {
73
83
} ;
74
84
}
75
85
86
+ /**
87
+ * Initializes a worker thread for a customized module loader.
88
+ * @param {SharedArrayBuffer } lock - The lock used to synchronize communication between the worker and the main thread.
89
+ * @param {MessagePort } syncCommPort - The message port used for synchronous communication between the worker and the
90
+ * main thread.
91
+ * @param {(err: Error, origin?: string) => void } errorHandler - The function to use for uncaught exceptions.
92
+ * @returns {Promise<void> } A promise that resolves when the worker thread has been initialized.
93
+ */
76
94
async function customizedModuleWorker ( lock , syncCommPort , errorHandler ) {
77
95
let hooks ;
78
96
let initializationError ;
@@ -114,6 +132,9 @@ async function customizedModuleWorker(lock, syncCommPort, errorHandler) {
114
132
AtomicsNotify ( lock , WORKER_TO_MAIN_THREAD_NOTIFICATION ) ;
115
133
116
134
let immediate ;
135
+ /**
136
+ * Checks for messages on the syncCommPort and handles them asynchronously.
137
+ */
117
138
function checkForMessages ( ) {
118
139
immediate = setImmediate ( checkForMessages ) . unref ( ) ;
119
140
// We need to let the event loop tick a few times to give the main thread a chance to send
@@ -147,6 +168,13 @@ async function customizedModuleWorker(lock, syncCommPort, errorHandler) {
147
168
setImmediate ( ( ) => { } ) ;
148
169
} ) ;
149
170
171
+ /**
172
+ * Handles incoming messages from the main thread or other workers.
173
+ * @param {object } options - The options object.
174
+ * @param {string } options.method - The name of the hook.
175
+ * @param {Array } options.args - The arguments to pass to the method.
176
+ * @param {MessagePort } options.port - The message port to use for communication.
177
+ */
150
178
async function handleMessage ( { method, args, port } ) {
151
179
// Each potential exception needs to be caught individually so that the correct error is sent to
152
180
// the main thread.
@@ -205,11 +233,19 @@ async function customizedModuleWorker(lock, syncCommPort, errorHandler) {
205
233
}
206
234
207
235
/**
236
+ * Initializes a worker thread for a module with customized hooks.
208
237
* ! Run everything possible within this function so errors get reported.
238
+ * @param {{lock: SharedArrayBuffer} } workerData - The lock used to synchronize with the main thread.
239
+ * @param {MessagePort } syncCommPort - The communication port used to communicate with the main thread.
209
240
*/
210
241
module . exports = function setupModuleWorker ( workerData , syncCommPort ) {
211
242
const lock = new Int32Array ( workerData . lock ) ;
212
243
244
+ /**
245
+ * Handles errors that occur in the worker thread.
246
+ * @param {Error } err - The error that occurred.
247
+ * @param {string } [origin='unhandledRejection'] - The origin of the error.
248
+ */
213
249
function errorHandler ( err , origin = 'unhandledRejection' ) {
214
250
AtomicsAdd ( lock , WORKER_TO_MAIN_THREAD_NOTIFICATION , 1 ) ;
215
251
AtomicsNotify ( lock , WORKER_TO_MAIN_THREAD_NOTIFICATION ) ;
0 commit comments