Skip to content

Commit 4be5612

Browse files
aduh95targos
authored andcommitted
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: #49529 Backport-PR-URL: #50669 Reviewed-By: Geoffrey Booth <[email protected]> Reviewed-By: Jacob Smith <[email protected]>
1 parent 0875867 commit 4be5612

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
@@ -97,7 +97,6 @@ added: REPLACEME
9797
[`initialize`][] hook.
9898
* `transferList` {Object\[]} [transferrable objects][] to be passed into the
9999
`initialize` hook.
100-
* Returns: {any} returns whatever was returned by the `initialize` hook.
101100
102101
Register a module that exports [hooks][] that customize Node.js module
103102
resolution and loading behavior. See [Customization hooks][].
@@ -346,7 +345,7 @@ names and signatures, and they must be exported as named exports.
346345
347346
```mjs
348347
export async function initialize({ number, port }) {
349-
// Receive data from `register`, return data to `register`.
348+
// Receives data from `register`.
350349
}
351350

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

407401
export async function initialize({ number, port }) {
408402
port.postMessage(`increment: ${number + 1}`);
409-
return 'ok';
410403
}
411404
```
412405
@@ -426,13 +419,11 @@ port1.on('message', (msg) => {
426419
assert.strictEqual(msg, 'increment: 2');
427420
});
428421

429-
const result = register('./path-to-my-hooks.js', {
422+
register('./path-to-my-hooks.js', {
430423
parentURL: import.meta.url,
431424
data: { number: 1, port: port2 },
432425
transferList: [port2],
433426
});
434-
435-
assert.strictEqual(result, 'ok');
436427
```
437428
438429
```cjs
@@ -450,13 +441,11 @@ port1.on('message', (msg) => {
450441
assert.strictEqual(msg, 'increment: 2');
451442
});
452443

453-
const result = register('./path-to-my-hooks.js', {
444+
register('./path-to-my-hooks.js', {
454445
parentURL: pathToFileURL(__filename),
455446
data: { number: 1, port: port2 },
456447
transferList: [port2],
457448
});
458-
459-
assert.strictEqual(result, 'ok');
460449
```
461450
462451
#### `resolve(specifier, context, nextResolve)`
@@ -1080,7 +1069,6 @@ returned object contains the following keys:
10801069
[`Uint8Array`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array
10811070
[`initialize`]: #initialize
10821071
[`module`]: modules.md#the-module-object
1083-
[`port.postMessage`]: worker_threads.md#portpostmessagevalue-transferlist
10841072
[`port.ref()`]: worker_threads.md#portref
10851073
[`port.unref()`]: worker_threads.md#portunref
10861074
[`register`]: #moduleregisterspecifier-parenturl-options

lib/internal/modules/esm/hooks.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ class Hooks {
139139
parentURL,
140140
kEmptyObject,
141141
);
142-
return this.addCustomLoader(urlOrSpecifier, keyedExports, data);
142+
await this.addCustomLoader(urlOrSpecifier, keyedExports, data);
143143
}
144144

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

lib/internal/modules/esm/loader.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ function getHooksProxy() {
536536
* @param {string} [options.parentURL] Base to use when resolving `specifier`
537537
* @param {any} [options.data] Arbitrary data passed to the loader's `initialize` hook
538538
* @param {any[]} [options.transferList] Objects in `data` that are changing ownership
539-
* @returns {any} The result of the loader's initialize hook, if any
539+
* @returns {void} We want to reserve the return value for potential future extension of the API.
540540
* @example
541541
* ```js
542542
* register('./myLoader.js');
@@ -562,7 +562,7 @@ function register(specifier, parentURL = undefined, options) {
562562
options = parentURL;
563563
parentURL = options.parentURL;
564564
}
565-
return moduleLoader.register(
565+
moduleLoader.register(
566566
`${specifier}`,
567567
parentURL ?? 'data:',
568568
options?.data,

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

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

571571
assert.strictEqual(stderr, '');
572-
assert.deepStrictEqual(stdout.split('\n'), [ 'register ok',
572+
assert.deepStrictEqual(stdout.split('\n'), [ 'register undefined',
573573
'message initialize',
574574
'message resolve node:os',
575575
'' ]);
@@ -647,10 +647,10 @@ describe('Loader hooks', { concurrency: true }, () => {
647647
'--eval',
648648
`
649649
import {register} from 'node:module';
650-
console.log('result', register(
650+
console.log('result 1', register(
651651
${JSON.stringify(fixtures.fileURL('es-module-loaders/hooks-initialize.mjs'))}
652652
));
653-
console.log('result', register(
653+
console.log('result 2', register(
654654
${JSON.stringify(fixtures.fileURL('es-module-loaders/hooks-initialize.mjs'))}
655655
));
656656
@@ -660,9 +660,9 @@ describe('Loader hooks', { concurrency: true }, () => {
660660

661661
assert.strictEqual(stderr, '');
662662
assert.deepStrictEqual(stdout.split('\n'), [ 'hooks initialize 1',
663-
'result 1',
663+
'result 1 undefined',
664664
'hooks initialize 2',
665-
'result 2',
665+
'result 2 undefined',
666666
'' ]);
667667
assert.strictEqual(code, 0);
668668
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)