Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c10ee5d

Browse filesBrowse files
Hakerh400MylesBorins
authored andcommitted
esm: better error message for unsupported URL
The default ESM loader supports only file and data URLs. This PR adds better error message for it. PR-URL: #31129 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent b95bb38 commit c10ee5d
Copy full SHA for c10ee5d

File tree

Expand file treeCollapse file tree

4 files changed

+21
-10
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+21
-10
lines changed
Open diff view settings
Collapse file

‎doc/api/errors.md‎

Copy file name to clipboardExpand all lines: doc/api/errors.md
+5Lines changed: 5 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -1936,6 +1936,11 @@ An attempt was made to load a module with an unknown or unsupported format.
19361936
An invalid or unknown process signal was passed to an API expecting a valid
19371937
signal (such as [`subprocess.kill()`][]).
19381938

1939+
<a id="ERR_UNSUPPORTED_ESM_URL_SCHEME"></a>
1940+
### `ERR_UNSUPPORTED_ESM_URL_SCHEME`
1941+
1942+
`import` with URL schemes other than `file` and `data` is unsupported.
1943+
19391944
<a id="ERR_V8BREAKITERATOR"></a>
19401945
### `ERR_V8BREAKITERATOR`
19411946

Collapse file

‎lib/internal/errors.js‎

Copy file name to clipboardExpand all lines: lib/internal/errors.js
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,8 @@ E('ERR_UNKNOWN_FILE_EXTENSION',
12181218
TypeError);
12191219
E('ERR_UNKNOWN_MODULE_FORMAT', 'Unknown module format: %s', RangeError);
12201220
E('ERR_UNKNOWN_SIGNAL', 'Unknown signal: %s', TypeError);
1221+
E('ERR_UNSUPPORTED_ESM_URL_SCHEME', 'Only file and data URLs are supported ' +
1222+
'by the default ESM loader', Error);
12211223

12221224
E('ERR_V8BREAKITERATOR',
12231225
'Full ICU data not installed. See https://github.com/nodejs/node/wiki/Intl',
Collapse file

‎lib/internal/modules/esm/default_resolve.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/default_resolve.js
+6-2Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ const { resolve: moduleWrapResolve,
1717
getPackageType } = internalBinding('module_wrap');
1818
const { URL, pathToFileURL, fileURLToPath } = require('internal/url');
1919
const { ERR_INPUT_TYPE_NOT_ALLOWED,
20-
ERR_UNKNOWN_FILE_EXTENSION } = require('internal/errors').codes;
20+
ERR_UNKNOWN_FILE_EXTENSION,
21+
ERR_UNSUPPORTED_ESM_URL_SCHEME } = require('internal/errors').codes;
2122

2223
const { SafeMap } = primordials;
2324

@@ -50,8 +51,9 @@ if (experimentalJsonModules)
5051
extensionFormatMap['.json'] = legacyExtensionFormatMap['.json'] = 'json';
5152

5253
function resolve(specifier, parentURL) {
54+
let parsed;
5355
try {
54-
const parsed = new URL(specifier);
56+
parsed = new URL(specifier);
5557
if (parsed.protocol === 'data:') {
5658
const [ , mime ] = /^([^/]+\/[^;,]+)(?:[^,]*?)(;base64)?,/.exec(parsed.pathname) || [ null, null, null ];
5759
const format = ({
@@ -66,6 +68,8 @@ function resolve(specifier, parentURL) {
6668
};
6769
}
6870
} catch {}
71+
if (parsed && parsed.protocol !== 'file:' && parsed.protocol !== 'data:')
72+
throw new ERR_UNSUPPORTED_ESM_URL_SCHEME();
6973
if (NativeModule.canBeRequiredByUsers(specifier)) {
7074
return {
7175
url: specifier,
Collapse file

‎test/es-module/test-esm-dynamic-import.js‎

Copy file name to clipboardExpand all lines: test/es-module/test-esm-dynamic-import.js
+8-8Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ function expectErrorProperty(result, propertyKey, value) {
1717
}));
1818
}
1919

20-
function expectMissingModuleError(result) {
21-
expectErrorProperty(result, 'code', 'ERR_MODULE_NOT_FOUND');
20+
function expectModuleError(result, err) {
21+
expectErrorProperty(result, 'code', err);
2222
}
2323

2424
function expectOkNamespace(result) {
@@ -55,10 +55,10 @@ function expectFsNamespace(result) {
5555
expectFsNamespace(eval('import("fs")'));
5656
expectFsNamespace(eval('import("fs")'));
5757

58-
expectMissingModuleError(import('./not-an-existing-module.mjs'));
59-
// TODO(jkrems): Right now this doesn't hit a protocol error because the
60-
// module resolution step already rejects it. These arguably should be
61-
// protocol errors.
62-
expectMissingModuleError(import('node:fs'));
63-
expectMissingModuleError(import('http://example.com/foo.js'));
58+
expectModuleError(import('./not-an-existing-module.mjs'),
59+
'ERR_MODULE_NOT_FOUND');
60+
expectModuleError(import('node:fs'),
61+
'ERR_UNSUPPORTED_ESM_URL_SCHEME');
62+
expectModuleError(import('http://example.com/foo.js'),
63+
'ERR_UNSUPPORTED_ESM_URL_SCHEME');
6464
})();

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.