GeezerVison is showing github.com — esm: improve validation of resolved URLs · nodejs/node@bd71706
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: lib/internal/modules/esm/loader.js +5-2Lines changed: 5 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -29,7 +29,7 @@ const { | 29 | 29 | ERR_INVALID_RETURN_VALUE, | 30 | 30 | ERR_UNKNOWN_MODULE_FORMAT | 31 | 31 | } = require('internal/errors').codes; | 32 | | -const { pathToFileURL, isURLInstance } = require('internal/url'); | | 32 | +const { pathToFileURL, isURLInstance, URL } = require('internal/url'); | 33 | 33 | const { | 34 | 34 | isAnyArrayBuffer, | 35 | 35 | isArrayBufferView, | @@ -558,7 +558,8 @@ class ESMLoader { | 558 | 558 | format, | 559 | 559 | ); | 560 | 560 | } | 561 | | - if (typeof url !== 'string') { | | 561 | + | | 562 | + if (typeof url !== 'string') { // non-strings can be coerced to a url string | 562 | 563 | throw new ERR_INVALID_RETURN_PROPERTY_VALUE( | 563 | 564 | 'string', | 564 | 565 | 'loader resolve', | @@ -567,6 +568,8 @@ class ESMLoader { | 567 | 568 | ); | 568 | 569 | } | 569 | 570 |
| | 571 | + new URL(url); // Intentionally trigger error if `url` is invalid | | 572 | + | 570 | 573 | return { | 571 | 574 | format, | 572 | 575 | url, | |
Collapse file Copy file name to clipboardExpand all lines: test/es-module/test-esm-loader-invalid-url.mjs +1-5Lines changed: 1 addition & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -4,11 +4,7 @@ import assert from 'assert'; | 4 | 4 |
| 5 | 5 | import('../fixtures/es-modules/test-esm-ok.mjs') | 6 | 6 | .then(assert.fail, (error) => { | 7 | | - expectsError({ | 8 | | - code: 'ERR_INVALID_URL', | 9 | | - message: 'Invalid URL' | 10 | | - })(error); | 11 | | - | | 7 | + expectsError({ code: 'ERR_INVALID_URL' })(error); | 12 | 8 | assert.strictEqual(error.input, '../fixtures/es-modules/test-esm-ok.mjs'); | 13 | 9 | }) | 14 | 10 | .then(mustCall()); |
Collapse file Copy file name to clipboardExpand all lines: test/fixtures/es-module-loaders/hooks-custom.mjs +8-7Lines changed: 8 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { pathToFileURL } from 'node:url'; | 1 | 2 | import count from '../es-modules/stateful.mjs'; | 2 | 3 |
| 3 | 4 |
| @@ -24,28 +25,28 @@ export function load(url, context, next) { | 24 | 25 | format: 'module', | 25 | 26 | }); | 26 | 27 |
| 27 | | - if (url === 'esmHook/badReturnVal.mjs') return 'export function returnShouldBeObject() {}'; | | 28 | + if (url.endsWith('esmHook/badReturnVal.mjs')) return 'export function returnShouldBeObject() {}'; | 28 | 29 |
| 29 | | - if (url === 'esmHook/badReturnFormatVal.mjs') return { | | 30 | + if (url.endsWith('esmHook/badReturnFormatVal.mjs')) return { | 30 | 31 | format: Array(0), | 31 | 32 | source: '', | 32 | 33 | } | 33 | | - if (url === 'esmHook/unsupportedReturnFormatVal.mjs') return { | | 34 | + if (url.endsWith('esmHook/unsupportedReturnFormatVal.mjs')) return { | 34 | 35 | format: 'foo', // Not one of the allowable inputs: no translator named 'foo' | 35 | 36 | source: '', | 36 | 37 | } | 37 | 38 |
| 38 | | - if (url === 'esmHook/badReturnSourceVal.mjs') return { | | 39 | + if (url.endsWith('esmHook/badReturnSourceVal.mjs')) return { | 39 | 40 | format: 'module', | 40 | 41 | source: Array(0), | 41 | 42 | } | 42 | 43 |
| 43 | | - if (url === 'esmHook/preknownFormat.pre') return { | | 44 | + if (url.endsWith('esmHook/preknownFormat.pre')) return { | 44 | 45 | format: context.format, | 45 | 46 | source: `const msg = 'hello world'; export default msg;` | 46 | 47 | }; | 47 | 48 |
| 48 | | - if (url === 'esmHook/virtual.mjs') return { | | 49 | + if (url.endsWith('esmHook/virtual.mjs')) return { | 49 | 50 | format: 'module', | 50 | 51 | source: `export const message = 'Woohoo!'.toUpperCase();`, | 51 | 52 | }; | @@ -62,7 +63,7 @@ export function resolve(specifier, context, next) { | 62 | 63 |
| 63 | 64 | if (specifier.startsWith('esmHook')) return { | 64 | 65 | format, | 65 | | - url: specifier, | | 66 | + url: pathToFileURL(specifier).href, | 66 | 67 | importAssertions: context.importAssertions, | 67 | 68 | }; | 68 | 69 |
| |
|