| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 7b6a731 commit 521a932
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -84,14 +84,18 @@ isBuiltin('wss'); // false | |||
| 84 | 84 | ||
| 85 | 85 | <!-- YAML | |
| 86 | 86 | added: v20.6.0 | |
| 87 | + changes: | ||
| 88 | + - version: REPLACEME | ||
| 89 | + pr-url: https://github.com/nodejs/node/pull/49655 | ||
| 90 | + description: Add support for WHATWG URL instances. | ||
| 87 | 91 | --> | |
| 88 | 92 | ||
| 89 | 93 | > Stability: 1.2 - Release candidate | |
| 90 | 94 | ||
| 91 | - * `specifier` {string} Customization hooks to be registered; this should be the | ||
| 92 | - same string that would be passed to `import()`, except that if it is relative, | ||
| 93 | - it is resolved relative to `parentURL`. | ||
| 94 | - * `parentURL` {string} If you want to resolve `specifier` relative to a base | ||
| 95 | + * `specifier` {string|URL} Customization hooks to be registered; this should be | ||
| 96 | + the same string that would be passed to `import()`, except that if it is | ||
| 97 | + relative, it is resolved relative to `parentURL`. | ||
| 98 | + * `parentURL` {string|URL} If you want to resolve `specifier` relative to a base | ||
| 95 | 99 | URL, such as `import.meta.url`, you can pass that URL here. **Default:** | |
| 96 | 100 | `'data:'` | |
| 97 | 101 | * `options` {Object} | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,7 +14,7 @@ const { | |||
| 14 | 14 | ERR_UNKNOWN_MODULE_FORMAT, | |
| 15 | 15 | } = require('internal/errors').codes; | |
| 16 | 16 | const { getOptionValue } = require('internal/options'); | |
| 17 | - const { pathToFileURL } = require('internal/url'); | ||
| 17 | + const { pathToFileURL, isURL } = require('internal/url'); | ||
| 18 | 18 | const { emitExperimentalWarning } = require('internal/util'); | |
| 19 | 19 | const { | |
| 20 | 20 | getDefaultConditions, | |
@@ -320,7 +320,7 @@ class ModuleLoader { | |||
| 320 | 320 | // eslint-disable-next-line no-use-before-define | |
| 321 | 321 | this.setCustomizations(new CustomizedModuleLoader()); | |
| 322 | 322 | } | |
| 323 | - return this.#customizations.register(specifier, parentURL, data, transferList); | ||
| 323 | + return this.#customizations.register(`${specifier}`, `${parentURL}`, data, transferList); | ||
| 324 | 324 | } | |
| 325 | 325 | ||
| 326 | 326 | /** | |
@@ -541,11 +541,11 @@ function getHooksProxy() { | |||
| 541 | 541 | ||
| 542 | 542 | /** | |
| 543 | 543 | * Register a single loader programmatically. | |
| 544 | - * @param {string} specifier | ||
| 545 | - * @param {string} [parentURL] Base to use when resolving `specifier`; optional if | ||
| 544 | + * @param {string|import('url').URL} specifier | ||
| 545 | + * @param {string|import('url').URL} [parentURL] Base to use when resolving `specifier`; optional if | ||
| 546 | 546 | * `specifier` is absolute. Same as `options.parentUrl`, just inline | |
| 547 | 547 | * @param {object} [options] Additional options to apply, described below. | |
| 548 | - * @param {string} [options.parentURL] Base to use when resolving `specifier` | ||
| 548 | + * @param {string|import('url').URL} [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 | 551 | * @returns {void} We want to reserve the return value for potential future extension of the API. | |
@@ -570,12 +570,12 @@ function getHooksProxy() { | |||
| 570 | 570 | */ | |
| 571 | 571 | function register(specifier, parentURL = undefined, options) { | |
| 572 | 572 | const moduleLoader = require('internal/process/esm_loader').esmLoader; | |
| 573 | - if (parentURL != null && typeof parentURL === 'object') { | ||
| 573 | + if (parentURL != null && typeof parentURL === 'object' && !isURL(parentURL)) { | ||
| 574 | 574 | options = parentURL; | |
| 575 | 575 | parentURL = options.parentURL; | |
| 576 | 576 | } | |
| 577 | 577 | moduleLoader.register( | |
| 578 | - `${specifier}`, | ||
| 578 | + specifier, | ||
| 579 | 579 | parentURL ?? 'data:', | |
| 580 | 580 | options?.data, | |
| 581 | 581 | options?.transferList, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -507,6 +507,37 @@ describe('Loader hooks', { concurrency: true }, () => { | |||
| 507 | 507 | assert.strictEqual(signal, null); | |
| 508 | 508 | }); | |
| 509 | 509 | ||
| 510 | + it('should have `register` accept URL objects as `parentURL`', async () => { | ||
| 511 | + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ | ||
| 512 | + '--no-warnings', | ||
| 513 | + '--import', | ||
| 514 | + `data:text/javascript,${encodeURIComponent( | ||
| 515 | + 'import{ register } from "node:module";' + | ||
| 516 | + 'import { pathToFileURL } from "node:url";' + | ||
| 517 | + 'register("./hooks-initialize.mjs", pathToFileURL("./"));' | ||
| 518 | + )}`, | ||
| 519 | + '--input-type=module', | ||
| 520 | + '--eval', | ||
| 521 | + ` | ||
| 522 | + import {register} from 'node:module'; | ||
| 523 | + register( | ||
| 524 | + ${JSON.stringify(fixtures.fileURL('es-module-loaders/loader-load-foo-or-42.mjs'))}, | ||
| 525 | + new URL('data:'), | ||
| 526 | + ); | ||
| 527 | + | ||
| 528 | + import('node:os').then((result) => { | ||
| 529 | + console.log(JSON.stringify(result)); | ||
| 530 | + }); | ||
| 531 | + `, | ||
| 532 | + ], { cwd: fixtures.fileURL('es-module-loaders/') }); | ||
| 533 | + | ||
| 534 | + assert.strictEqual(stderr, ''); | ||
| 535 | + assert.deepStrictEqual(stdout.split('\n').sort(), ['hooks initialize 1', '{"default":"foo"}', ''].sort()); | ||
| 536 | + | ||
| 537 | + assert.strictEqual(code, 0); | ||
| 538 | + assert.strictEqual(signal, null); | ||
| 539 | + }); | ||
| 540 | + | ||
| 510 | 541 | it('should have `register` work with cjs', async () => { | |
| 511 | 542 | const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ | |
| 512 | 543 | '--no-warnings', | |
| Back | FazBrowse Home | New Git URL |
0 commit comments