Skip to content

module: allow multiple chain #53332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
Closed
18 changes: 17 additions & 1 deletion doc/api/module.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ changes:
Register a module that exports [hooks][] that customize Node.js module
resolution and loading behavior. See [Customization hooks][].

All hooks, despite of where they were registered (main thread or worker thread),
are loaded and executed in a single shared thread that only serves hooks. Therefore any
side effect present will affect all threads that use the same hook.

If global data must be stored in hooks, make sure to scope it using the `thread`
property that is present in the context of all customization hooks.

All worker thread inherits the parent thread hooks chain when created. All subsequent modifications
in the parent or worker thread do not affect other chains.

### `module.syncBuiltinESMExports()`

<!-- YAML
Expand Down Expand Up @@ -363,7 +373,7 @@ module resolution and loading process. The exported functions must have specific
names and signatures, and they must be exported as named exports.

```mjs
export async function initialize({ number, port }) {
export async function initialize({ number, port }, context) {
// Receives data from `register`.
}

Expand Down Expand Up @@ -404,6 +414,8 @@ added:
> Stability: 1.2 - Release candidate

* `data` {any} The data from `register(loader, import.meta.url, { data })`.
* `context` {Object}
* `threadId` {string} The calling thread ID

The `initialize` hook provides a way to define a custom function that runs in
the hooks thread when the hooks module is initialized. Initialization happens
Expand Down Expand Up @@ -504,6 +516,8 @@ changes:
attributes for the module to import
* `parentURL` {string|undefined} The module importing this one, or undefined
if this is the Node.js entry point
* `threadId` {string} The calling thread ID
* `data` {any} The data passed to `initialize`
* `nextResolve` {Function} The subsequent `resolve` hook in the chain, or the
Node.js default `resolve` hook after the last user-supplied `resolve` hook
* `specifier` {string}
Expand Down Expand Up @@ -599,6 +613,8 @@ changes:
* `format` {string|null|undefined} The format optionally supplied by the
`resolve` hook chain
* `importAttributes` {Object}
* `threadId` {string} The calling thread ID
* `data` {any} The data passed to `initialize`
* `nextLoad` {Function} The subsequent `load` hook in the chain, or the
Node.js default `load` hook after the last user-supplied `load` hook
* `specifier` {string}
Expand Down
10 changes: 9 additions & 1 deletion lib/internal/main/worker_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ port.on('message', (message) => {
filename,
hasStdin,
publicPort,
hooksPort,
workerData,
} = message;

Expand All @@ -109,6 +110,7 @@ port.on('message', (message) => {
}

require('internal/worker').assignEnvironmentData(environmentData);
require('internal/worker').hooksPort = hooksPort;

if (SharedArrayBuffer !== undefined) {
// The counter is only passed to the workers created by the main thread,
Expand Down Expand Up @@ -144,7 +146,13 @@ port.on('message', (message) => {
case 'internal': {
// Create this WeakMap in js-land because V8 has no C++ API for WeakMap.
internalBinding('module_wrap').callbackMap = new SafeWeakMap();
require(filename)(workerData, publicPort);
let entrypoint = require(filename);

if (typeof entrypoint !== 'function') {
entrypoint = entrypoint.entrypoint;
}

entrypoint(workerData, publicPort);
break;
}

Expand Down
Loading