Skip to content

Commit 9fe5fde

Browse files
committed
esm/worker.js
1 parent 0e31f4f commit 9fe5fde

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

lib/internal/modules/esm/worker.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ const {
3232
const { initializeHooks } = require('internal/modules/esm/utils');
3333
const { isMarkedAsUntransferable } = require('internal/buffer');
3434

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+
*/
3540
function transferArrayBuffer(hasError, source) {
3641
if (hasError || source == null) { return; }
3742
let arrayBuffer;
@@ -47,6 +52,11 @@ function transferArrayBuffer(hasError, source) {
4752
}
4853
}
4954

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+
*/
5060
function wrapMessage(status, body) {
5161
if (status === 'success' || body === null ||
5262
(typeof body !== 'object' &&
@@ -73,6 +83,14 @@ function wrapMessage(status, body) {
7383
};
7484
}
7585

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+
*/
7694
async function customizedModuleWorker(lock, syncCommPort, errorHandler) {
7795
let hooks;
7896
let initializationError;
@@ -114,6 +132,9 @@ async function customizedModuleWorker(lock, syncCommPort, errorHandler) {
114132
AtomicsNotify(lock, WORKER_TO_MAIN_THREAD_NOTIFICATION);
115133

116134
let immediate;
135+
/**
136+
* Checks for messages on the syncCommPort and handles them asynchronously.
137+
*/
117138
function checkForMessages() {
118139
immediate = setImmediate(checkForMessages).unref();
119140
// 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) {
147168
setImmediate(() => {});
148169
});
149170

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+
*/
150178
async function handleMessage({ method, args, port }) {
151179
// Each potential exception needs to be caught individually so that the correct error is sent to
152180
// the main thread.
@@ -205,11 +233,19 @@ async function customizedModuleWorker(lock, syncCommPort, errorHandler) {
205233
}
206234

207235
/**
236+
* Initializes a worker thread for a module with customized hooks.
208237
* ! 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.
209240
*/
210241
module.exports = function setupModuleWorker(workerData, syncCommPort) {
211242
const lock = new Int32Array(workerData.lock);
212243

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+
*/
213249
function errorHandler(err, origin = 'unhandledRejection') {
214250
AtomicsAdd(lock, WORKER_TO_MAIN_THREAD_NOTIFICATION, 1);
215251
AtomicsNotify(lock, WORKER_TO_MAIN_THREAD_NOTIFICATION);

0 commit comments

Comments
 (0)