Skip to content

Commit b64c3ee

Browse files
authored
esm: remove return value for Module.register
The current API shape si not great because it's too limited and redundant with the use of `MessagePort`. PR-URL: nodejs#49529 Reviewed-By: Geoffrey Booth <[email protected]> Reviewed-By: Jacob Smith <[email protected]>
1 parent 0a2ab4c commit b64c3ee

File tree

6 files changed

+16
-30
lines changed

6 files changed

+16
-30
lines changed

doc/api/module.md

+7-19
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ added: v20.6.0
9999
[`initialize`][] hook.
100100
* `transferList` {Object\[]} [transferrable objects][] to be passed into the
101101
`initialize` hook.
102-
* Returns: {any} returns whatever was returned by the `initialize` hook.
103102
104103
Register a module that exports [hooks][] that customize Node.js module
105104
resolution and loading behavior. See [Customization hooks][].
@@ -348,7 +347,7 @@ names and signatures, and they must be exported as named exports.
348347
349348
```mjs
350349
export async function initialize({ number, port }) {
351-
// Receive data from `register`, return data to `register`.
350+
// Receives data from `register`.
352351
}
353352

354353
export async function resolve(specifier, context, nextResolve) {
@@ -386,20 +385,15 @@ added: v20.6.0
386385
> Stability: 1.1 - Active development
387386
388387
* `data` {any} The data from `register(loader, import.meta.url, { data })`.
389-
* Returns: {any} The data to be returned to the caller of `register`.
390388
391389
The `initialize` hook provides a way to define a custom function that runs in
392390
the hooks thread when the hooks module is initialized. Initialization happens
393391
when the hooks module is registered via [`register`][].
394392
395-
This hook can send and receive data from a [`register`][] invocation, including
396-
ports and other transferrable objects. The return value of `initialize` must be
397-
either:
398-
399-
* `undefined`,
400-
* something that can be posted as a message between threads (e.g. the input to
401-
[`port.postMessage`][]),
402-
* a `Promise` resolving to one of the aforementioned values.
393+
This hook can receive data from a [`register`][] invocation, including
394+
ports and other transferrable objects. The return value of `initialize` can be a
395+
{Promise}, in which case it will be awaited before the main application thread
396+
execution resumes.
403397
404398
Module customization code:
405399
@@ -408,7 +402,6 @@ Module customization code:
408402

409403
export async function initialize({ number, port }) {
410404
port.postMessage(`increment: ${number + 1}`);
411-
return 'ok';
412405
}
413406
```
414407
@@ -428,13 +421,11 @@ port1.on('message', (msg) => {
428421
assert.strictEqual(msg, 'increment: 2');
429422
});
430423

431-
const result = register('./path-to-my-hooks.js', {
424+
register('./path-to-my-hooks.js', {
432425
parentURL: import.meta.url,
433426
data: { number: 1, port: port2 },
434427
transferList: [port2],
435428
});
436-
437-
assert.strictEqual(result, 'ok');
438429
```
439430
440431
```cjs
@@ -452,13 +443,11 @@ port1.on('message', (msg) => {
452443
assert.strictEqual(msg, 'increment: 2');
453444
});
454445

455-
const result = register('./path-to-my-hooks.js', {
446+
register('./path-to-my-hooks.js', {
456447
parentURL: pathToFileURL(__filename),
457448
data: { number: 1, port: port2 },
458449
transferList: [port2],
459450
});
460-
461-
assert.strictEqual(result, 'ok');
462451
```
463452
464453
#### `resolve(specifier, context, nextResolve)`
@@ -1116,7 +1105,6 @@ returned object contains the following keys:
11161105
[`Uint8Array`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array
11171106
[`initialize`]: #initialize
11181107
[`module`]: modules.md#the-module-object
1119-
[`port.postMessage`]: worker_threads.md#portpostmessagevalue-transferlist
11201108
[`port.ref()`]: worker_threads.md#portref
11211109
[`port.unref()`]: worker_threads.md#portunref
11221110
[`register`]: #moduleregisterspecifier-parenturl-options

lib/internal/modules/esm/hooks.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class Hooks {
140140
parentURL,
141141
kEmptyObject,
142142
);
143-
return this.addCustomLoader(urlOrSpecifier, keyedExports, data);
143+
await this.addCustomLoader(urlOrSpecifier, keyedExports, data);
144144
}
145145

146146
/**
@@ -150,7 +150,7 @@ class Hooks {
150150
* @param {Record<string, unknown>} exports
151151
* @param {any} [data] Arbitrary data to be passed from the custom loader (user-land)
152152
* to the worker.
153-
* @returns {any} The result of the loader's `initialize` hook, if provided.
153+
* @returns {any | Promise<any>} User data, ignored unless it's a promise, in which case it will be awaited.
154154
*/
155155
addCustomLoader(url, exports, data) {
156156
const {

lib/internal/modules/esm/loader.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ function getHooksProxy() {
548548
* @param {string} [options.parentURL] Base to use when resolving `specifier`
549549
* @param {any} [options.data] Arbitrary data passed to the loader's `initialize` hook
550550
* @param {any[]} [options.transferList] Objects in `data` that are changing ownership
551-
* @returns {any} The result of the loader's initialize hook, if any
551+
* @returns {void} We want to reserve the return value for potential future extension of the API.
552552
* @example
553553
* ```js
554554
* register('./myLoader.js');
@@ -574,7 +574,7 @@ function register(specifier, parentURL = undefined, options) {
574574
options = parentURL;
575575
parentURL = options.parentURL;
576576
}
577-
return moduleLoader.register(
577+
moduleLoader.register(
578578
`${specifier}`,
579579
parentURL ?? 'data:',
580580
options?.data,

test/es-module/test-esm-loader-hooks.mjs

+5-5
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ describe('Loader hooks', { concurrency: true }, () => {
621621
]);
622622

623623
assert.strictEqual(stderr, '');
624-
assert.deepStrictEqual(stdout.split('\n'), [ 'register ok',
624+
assert.deepStrictEqual(stdout.split('\n'), [ 'register undefined',
625625
'message initialize',
626626
'message resolve node:os',
627627
'' ]);
@@ -699,10 +699,10 @@ describe('Loader hooks', { concurrency: true }, () => {
699699
'--eval',
700700
`
701701
import {register} from 'node:module';
702-
console.log('result', register(
702+
console.log('result 1', register(
703703
${JSON.stringify(fixtures.fileURL('es-module-loaders/hooks-initialize.mjs'))}
704704
));
705-
console.log('result', register(
705+
console.log('result 2', register(
706706
${JSON.stringify(fixtures.fileURL('es-module-loaders/hooks-initialize.mjs'))}
707707
));
708708
@@ -712,9 +712,9 @@ describe('Loader hooks', { concurrency: true }, () => {
712712

713713
assert.strictEqual(stderr, '');
714714
assert.deepStrictEqual(stdout.split('\n'), [ 'hooks initialize 1',
715-
'result 1',
715+
'result 1 undefined',
716716
'hooks initialize 2',
717-
'result 2',
717+
'result 2 undefined',
718718
'' ]);
719719
assert.strictEqual(code, 0);
720720
assert.strictEqual(signal, null);

test/fixtures/es-module-loaders/hooks-initialize-port.mjs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ let thePort = null;
33
export async function initialize(port) {
44
port.postMessage('initialize');
55
thePort = port;
6-
return 'ok';
76
}
87

98
export async function resolve(specifier, context, next) {

test/fixtures/es-module-loaders/hooks-initialize.mjs

-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ let counter = 0;
44

55
export async function initialize() {
66
writeFileSync(1, `hooks initialize ${++counter}\n`);
7-
return counter;
87
}

0 commit comments

Comments
 (0)