GeezerVison is showing github.com — esm: remove return value for `Module.register` · nodejs/node@be48267
File treeExpand file treeCollapse file tree Open diff view settings fixtures/es-module-loaders
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/module.md +7-19Lines changed: 7 additions & 19 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -99,7 +99,6 @@ added: v20.6.0 | 99 | 99 | [`initialize`][] hook. | 100 | 100 | * `transferList` {Object\[]} [transferrable objects][] to be passed into the | 101 | 101 | `initialize` hook. | 102 | | -* Returns: {any} returns whatever was returned by the `initialize` hook. | 103 | 102 |
| 104 | 103 | Register a module that exports [hooks][] that customize Node.js module | 105 | 104 | resolution and loading behavior. See [Customization hooks][]. | @@ -348,7 +347,7 @@ names and signatures, and they must be exported as named exports. | 348 | 347 |
| 349 | 348 | ```mjs | 350 | 349 | export async function initialize({ number, port }) { | 351 | | - // Receive data from `register`, return data to `register`. | | 350 | + // Receives data from `register`. | 352 | 351 | } | 353 | 352 |
| 354 | 353 | export async function resolve(specifier, context, nextResolve) { | @@ -386,20 +385,15 @@ added: v20.6.0 | 386 | 385 | > Stability: 1.1 - Active development | 387 | 386 |
| 388 | 387 | * `data` {any} The data from `register(loader, import.meta.url, { data })`. | 389 | | -* Returns: {any} The data to be returned to the caller of `register`. | 390 | 388 |
| 391 | 389 | The `initialize` hook provides a way to define a custom function that runs in | 392 | 390 | the hooks thread when the hooks module is initialized. Initialization happens | 393 | 391 | when the hooks module is registered via [`register`][]. | 394 | 392 |
| 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. | 403 | 397 |
| 404 | 398 | Module customization code: | 405 | 399 |
| @@ -408,7 +402,6 @@ Module customization code: | 408 | 402 |
| 409 | 403 | export async function initialize({ number, port }) { | 410 | 404 | port.postMessage(`increment: ${number + 1}`); | 411 | | - return 'ok'; | 412 | 405 | } | 413 | 406 | ``` | 414 | 407 |
| @@ -428,13 +421,11 @@ port1.on('message', (msg) => { | 428 | 421 | assert.strictEqual(msg, 'increment: 2'); | 429 | 422 | }); | 430 | 423 |
| 431 | | -const result = register('./path-to-my-hooks.js', { | | 424 | +register('./path-to-my-hooks.js', { | 432 | 425 | parentURL: import.meta.url, | 433 | 426 | data: { number: 1, port: port2 }, | 434 | 427 | transferList: [port2], | 435 | 428 | }); | 436 | | - | 437 | | -assert.strictEqual(result, 'ok'); | 438 | 429 | ``` | 439 | 430 |
| 440 | 431 | ```cjs | @@ -452,13 +443,11 @@ port1.on('message', (msg) => { | 452 | 443 | assert.strictEqual(msg, 'increment: 2'); | 453 | 444 | }); | 454 | 445 |
| 455 | | -const result = register('./path-to-my-hooks.js', { | | 446 | +register('./path-to-my-hooks.js', { | 456 | 447 | parentURL: pathToFileURL(__filename), | 457 | 448 | data: { number: 1, port: port2 }, | 458 | 449 | transferList: [port2], | 459 | 450 | }); | 460 | | - | 461 | | -assert.strictEqual(result, 'ok'); | 462 | 451 | ``` | 463 | 452 |
| 464 | 453 | #### `resolve(specifier, context, nextResolve)` | @@ -1116,7 +1105,6 @@ returned object contains the following keys: | 1116 | 1105 | [`Uint8Array`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array | 1117 | 1106 | [`initialize`]: #initialize | 1118 | 1107 | [`module`]: modules.md#the-module-object | 1119 | | -[`port.postMessage`]: worker_threads.md#portpostmessagevalue-transferlist | 1120 | 1108 | [`port.ref()`]: worker_threads.md#portref | 1121 | 1109 | [`port.unref()`]: worker_threads.md#portunref | 1122 | 1110 | [`register`]: #moduleregisterspecifier-parenturl-options | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/esm/hooks.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -140,7 +140,7 @@ class Hooks { | 140 | 140 | parentURL, | 141 | 141 | kEmptyObject, | 142 | 142 | ); | 143 | | - return this.addCustomLoader(urlOrSpecifier, keyedExports, data); | | 143 | + await this.addCustomLoader(urlOrSpecifier, keyedExports, data); | 144 | 144 | } | 145 | 145 |
| 146 | 146 | /** | @@ -150,7 +150,7 @@ class Hooks { | 150 | 150 | * @param {Record<string, unknown>} exports | 151 | 151 | * @param {any} [data] Arbitrary data to be passed from the custom loader (user-land) | 152 | 152 | * 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. | 154 | 154 | */ | 155 | 155 | addCustomLoader(url, exports, data) { | 156 | 156 | const { | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/esm/loader.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -548,7 +548,7 @@ function getHooksProxy() { | 548 | 548 | * @param {string} [options.parentURL] Base to use when resolving `specifier` | 549 | 549 | * @param {any} [options.data] Arbitrary data passed to the loader's `initialize` hook | 550 | 550 | * @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. | 552 | 552 | * @example | 553 | 553 | * ```js | 554 | 554 | * register('./myLoader.js'); | @@ -574,7 +574,7 @@ function register(specifier, parentURL = undefined, options) { | 574 | 574 | options = parentURL; | 575 | 575 | parentURL = options.parentURL; | 576 | 576 | } | 577 | | - return moduleLoader.register( | | 577 | + moduleLoader.register( | 578 | 578 | `${specifier}`, | 579 | 579 | parentURL ?? 'data:', | 580 | 580 | options?.data, | |
Collapse file Copy file name to clipboardExpand all lines: test/es-module/test-esm-loader-hooks.mjs +5-5Lines changed: 5 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -621,7 +621,7 @@ describe('Loader hooks', { concurrency: true }, () => { | 621 | 621 | ]); | 622 | 622 |
| 623 | 623 | assert.strictEqual(stderr, ''); | 624 | | - assert.deepStrictEqual(stdout.split('\n'), [ 'register ok', | | 624 | + assert.deepStrictEqual(stdout.split('\n'), [ 'register undefined', | 625 | 625 | 'message initialize', | 626 | 626 | 'message resolve node:os', | 627 | 627 | '' ]); | @@ -699,10 +699,10 @@ describe('Loader hooks', { concurrency: true }, () => { | 699 | 699 | '--eval', | 700 | 700 | ` | 701 | 701 | import {register} from 'node:module'; | 702 | | - console.log('result', register( | | 702 | + console.log('result 1', register( | 703 | 703 | ${JSON.stringify(fixtures.fileURL('es-module-loaders/hooks-initialize.mjs'))} | 704 | 704 | )); | 705 | | - console.log('result', register( | | 705 | + console.log('result 2', register( | 706 | 706 | ${JSON.stringify(fixtures.fileURL('es-module-loaders/hooks-initialize.mjs'))} | 707 | 707 | )); | 708 | 708 |
| @@ -712,9 +712,9 @@ describe('Loader hooks', { concurrency: true }, () => { | 712 | 712 |
| 713 | 713 | assert.strictEqual(stderr, ''); | 714 | 714 | assert.deepStrictEqual(stdout.split('\n'), [ 'hooks initialize 1', | 715 | | - 'result 1', | | 715 | + 'result 1 undefined', | 716 | 716 | 'hooks initialize 2', | 717 | | - 'result 2', | | 717 | + 'result 2 undefined', | 718 | 718 | '' ]); | 719 | 719 | assert.strictEqual(code, 0); | 720 | 720 | assert.strictEqual(signal, null); | |
Collapse file Copy file name to clipboardExpand all lines: test/fixtures/es-module-loaders/hooks-initialize-port.mjs -1Lines changed: 0 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -3,7 +3,6 @@ let thePort = null; | 3 | 3 | export async function initialize(port) { | 4 | 4 | port.postMessage('initialize'); | 5 | 5 | thePort = port; | 6 | | - return 'ok'; | 7 | 6 | } | 8 | 7 |
| 9 | 8 | export async function resolve(specifier, context, next) { | |
Collapse file Copy file name to clipboardExpand all lines: test/fixtures/es-module-loaders/hooks-initialize.mjs -1Lines changed: 0 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -4,5 +4,4 @@ let counter = 0; | 4 | 4 |
| 5 | 5 | export async function initialize() { | 6 | 6 | writeFileSync(1, `hooks initialize ${++counter}\n`); | 7 | | - return counter; | 8 | 7 | } |
|