Skip to content

Commit

Permalink
module: allow multiple chains
Browse files Browse the repository at this point in the history
  • Loading branch information
ShogunPanda committed Jun 7, 2024
1 parent d9169d4 commit 55501e8
Show file tree
Hide file tree
Showing 22 changed files with 541 additions and 137 deletions.
21 changes: 20 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` or the `chain`
properties that are 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,9 @@ 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
* `workerData` {any} The worker thread data
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 +517,9 @@ 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`
* `workerData` {any} The worker thread data
* `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 +615,9 @@ 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`
* `workerData` {any} The worker thread data
* `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
8 changes: 7 additions & 1 deletion lib/internal/main/worker_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,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

0 comments on commit 55501e8

Please sign in to comment.