| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 0875867 commit 4be5612
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -97,7 +97,6 @@ added: REPLACEME | |||
| 97 | 97 | [`initialize`][] hook. | |
| 98 | 98 | * `transferList` {Object\[]} [transferrable objects][] to be passed into the | |
| 99 | 99 | `initialize` hook. | |
| 100 | - * Returns: {any} returns whatever was returned by the `initialize` hook. | ||
| 101 | 100 | ||
| 102 | 101 | Register a module that exports [hooks][] that customize Node.js module | |
| 103 | 102 | resolution and loading behavior. See [Customization hooks][]. | |
@@ -346,7 +345,7 @@ names and signatures, and they must be exported as named exports. | |||
| 346 | 345 | ||
| 347 | 346 | ```mjs | |
| 348 | 347 | export async function initialize({ number, port }) { | |
| 349 | - // Receive data from `register`, return data to `register`. | ||
| 348 | + // Receives data from `register`. | ||
| 350 | 349 | } | |
| 351 | 350 | ||
| 352 | 351 | export async function resolve(specifier, context, nextResolve) { | |
@@ -384,20 +383,15 @@ added: REPLACEME | |||
| 384 | 383 | > Stability: 1.1 - Active development | |
| 385 | 384 | ||
| 386 | 385 | * `data` {any} The data from `register(loader, import.meta.url, { data })`. | |
| 387 | - * Returns: {any} The data to be returned to the caller of `register`. | ||
| 388 | 386 | ||
| 389 | 387 | The `initialize` hook provides a way to define a custom function that runs in | |
| 390 | 388 | the hooks thread when the hooks module is initialized. Initialization happens | |
| 391 | 389 | when the hooks module is registered via [`register`][]. | |
| 392 | 390 | ||
| 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. | ||
| 401 | 395 | ||
| 402 | 396 | Module customization code: | |
| 403 | 397 | ||
@@ -406,7 +400,6 @@ Module customization code: | |||
| 406 | 400 | ||
| 407 | 401 | export async function initialize({ number, port }) { | |
| 408 | 402 | port.postMessage(`increment: ${number + 1}`); | |
| 409 | - return 'ok'; | ||
| 410 | 403 | } | |
| 411 | 404 | ``` | |
| 412 | 405 | ||
@@ -426,13 +419,11 @@ port1.on('message', (msg) => { | |||
| 426 | 419 | assert.strictEqual(msg, 'increment: 2'); | |
| 427 | 420 | }); | |
| 428 | 421 | ||
| 429 | - const result = register('./path-to-my-hooks.js', { | ||
| 422 | + register('./path-to-my-hooks.js', { | ||
| 430 | 423 | parentURL: import.meta.url, | |
| 431 | 424 | data: { number: 1, port: port2 }, | |
| 432 | 425 | transferList: [port2], | |
| 433 | 426 | }); | |
| 434 | - | ||
| 435 | - assert.strictEqual(result, 'ok'); | ||
| 436 | 427 | ``` | |
| 437 | 428 | ||
| 438 | 429 | ```cjs | |
@@ -450,13 +441,11 @@ port1.on('message', (msg) => { | |||
| 450 | 441 | assert.strictEqual(msg, 'increment: 2'); | |
| 451 | 442 | }); | |
| 452 | 443 | ||
| 453 | - const result = register('./path-to-my-hooks.js', { | ||
| 444 | + register('./path-to-my-hooks.js', { | ||
| 454 | 445 | parentURL: pathToFileURL(__filename), | |
| 455 | 446 | data: { number: 1, port: port2 }, | |
| 456 | 447 | transferList: [port2], | |
| 457 | 448 | }); | |
| 458 | - | ||
| 459 | - assert.strictEqual(result, 'ok'); | ||
| 460 | 449 | ``` | |
| 461 | 450 | ||
| 462 | 451 | #### `resolve(specifier, context, nextResolve)` | |
@@ -1080,7 +1069,6 @@ returned object contains the following keys: | |||
| 1080 | 1069 | [`Uint8Array`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array | |
| 1081 | 1070 | [`initialize`]: #initialize | |
| 1082 | 1071 | [`module`]: modules.md#the-module-object | |
| 1083 | - [`port.postMessage`]: worker_threads.md#portpostmessagevalue-transferlist | ||
| 1084 | 1072 | [`port.ref()`]: worker_threads.md#portref | |
| 1085 | 1073 | [`port.unref()`]: worker_threads.md#portunref | |
| 1086 | 1074 | [`register`]: #moduleregisterspecifier-parenturl-options | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -139,7 +139,7 @@ class Hooks { | |||
| 139 | 139 | parentURL, | |
| 140 | 140 | kEmptyObject, | |
| 141 | 141 | ); | |
| 142 | - return this.addCustomLoader(urlOrSpecifier, keyedExports, data); | ||
| 142 | + await this.addCustomLoader(urlOrSpecifier, keyedExports, data); | ||
| 143 | 143 | } | |
| 144 | 144 | ||
| 145 | 145 | /** | |
@@ -149,7 +149,7 @@ class Hooks { | |||
| 149 | 149 | * @param {Record<string, unknown>} exports | |
| 150 | 150 | * @param {any} [data] Arbitrary data to be passed from the custom loader (user-land) | |
| 151 | 151 | * 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. | ||
| 153 | 153 | */ | |
| 154 | 154 | addCustomLoader(url, exports, data) { | |
| 155 | 155 | const { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -536,7 +536,7 @@ function getHooksProxy() { | |||
| 536 | 536 | * @param {string} [options.parentURL] Base to use when resolving `specifier` | |
| 537 | 537 | * @param {any} [options.data] Arbitrary data passed to the loader's `initialize` hook | |
| 538 | 538 | * @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. | ||
| 540 | 540 | * @example | |
| 541 | 541 | * ```js | |
| 542 | 542 | * register('./myLoader.js'); | |
@@ -562,7 +562,7 @@ function register(specifier, parentURL = undefined, options) { | |||
| 562 | 562 | options = parentURL; | |
| 563 | 563 | parentURL = options.parentURL; | |
| 564 | 564 | } | |
| 565 | - return moduleLoader.register( | ||
| 565 | + moduleLoader.register( | ||
| 566 | 566 | `${specifier}`, | |
| 567 | 567 | parentURL ?? 'data:', | |
| 568 | 568 | options?.data, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -569,7 +569,7 @@ describe('Loader hooks', { concurrency: true }, () => { | |||
| 569 | 569 | ]); | |
| 570 | 570 | ||
| 571 | 571 | assert.strictEqual(stderr, ''); | |
| 572 | - assert.deepStrictEqual(stdout.split('\n'), [ 'register ok', | ||
| 572 | + assert.deepStrictEqual(stdout.split('\n'), [ 'register undefined', | ||
| 573 | 573 | 'message initialize', | |
| 574 | 574 | 'message resolve node:os', | |
| 575 | 575 | '' ]); | |
@@ -647,10 +647,10 @@ describe('Loader hooks', { concurrency: true }, () => { | |||
| 647 | 647 | '--eval', | |
| 648 | 648 | ` | |
| 649 | 649 | import {register} from 'node:module'; | |
| 650 | - console.log('result', register( | ||
| 650 | + console.log('result 1', register( | ||
| 651 | 651 | ${JSON.stringify(fixtures.fileURL('es-module-loaders/hooks-initialize.mjs'))} | |
| 652 | 652 | )); | |
| 653 | - console.log('result', register( | ||
| 653 | + console.log('result 2', register( | ||
| 654 | 654 | ${JSON.stringify(fixtures.fileURL('es-module-loaders/hooks-initialize.mjs'))} | |
| 655 | 655 | )); | |
| 656 | 656 | ||
@@ -660,9 +660,9 @@ describe('Loader hooks', { concurrency: true }, () => { | |||
| 660 | 660 | ||
| 661 | 661 | assert.strictEqual(stderr, ''); | |
| 662 | 662 | assert.deepStrictEqual(stdout.split('\n'), [ 'hooks initialize 1', | |
| 663 | - 'result 1', | ||
| 663 | + 'result 1 undefined', | ||
| 664 | 664 | 'hooks initialize 2', | |
| 665 | - 'result 2', | ||
| 665 | + 'result 2 undefined', | ||
| 666 | 666 | '' ]); | |
| 667 | 667 | assert.strictEqual(code, 0); | |
| 668 | 668 | assert.strictEqual(signal, null); | |
| 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) { | |
| 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 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments