Skip to content

Commit

Permalink
esm: better error message for unsupported URL
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Hakerh400 authored and BridgeAR committed Jan 3, 2020
1 parent bd6a29c commit a6c2502
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
5 changes: 5 additions & 0 deletions doc/api/errors.md
Expand Up @@ -1957,6 +1957,11 @@ An attempt was made to load a module with an unknown or unsupported format.
An invalid or unknown process signal was passed to an API expecting a valid An invalid or unknown process signal was passed to an API expecting a valid
signal (such as [`subprocess.kill()`][]). signal (such as [`subprocess.kill()`][]).


<a id="ERR_UNSUPPORTED_ESM_URL_SCHEME"></a>
### `ERR_UNSUPPORTED_ESM_URL_SCHEME`

`import` with URL schemes other than `file` and `data` is unsupported.

<a id="ERR_V8BREAKITERATOR"></a> <a id="ERR_V8BREAKITERATOR"></a>
### `ERR_V8BREAKITERATOR` ### `ERR_V8BREAKITERATOR`


Expand Down
2 changes: 2 additions & 0 deletions lib/internal/errors.js
Expand Up @@ -1335,6 +1335,8 @@ E('ERR_UNKNOWN_FILE_EXTENSION',
TypeError); TypeError);
E('ERR_UNKNOWN_MODULE_FORMAT', 'Unknown module format: %s', RangeError); E('ERR_UNKNOWN_MODULE_FORMAT', 'Unknown module format: %s', RangeError);
E('ERR_UNKNOWN_SIGNAL', 'Unknown signal: %s', TypeError); E('ERR_UNKNOWN_SIGNAL', 'Unknown signal: %s', TypeError);
E('ERR_UNSUPPORTED_ESM_URL_SCHEME', 'Only file and data URLs are supported ' +
'by the default ESM loader', Error);


E('ERR_V8BREAKITERATOR', E('ERR_V8BREAKITERATOR',
'Full ICU data not installed. See https://github.com/nodejs/node/wiki/Intl', 'Full ICU data not installed. See https://github.com/nodejs/node/wiki/Intl',
Expand Down
8 changes: 6 additions & 2 deletions lib/internal/modules/esm/default_resolve.js
Expand Up @@ -21,7 +21,8 @@ const { resolve: moduleWrapResolve,
getPackageType } = internalBinding('module_wrap'); getPackageType } = internalBinding('module_wrap');
const { URL, pathToFileURL, fileURLToPath } = require('internal/url'); const { URL, pathToFileURL, fileURLToPath } = require('internal/url');
const { ERR_INPUT_TYPE_NOT_ALLOWED, const { ERR_INPUT_TYPE_NOT_ALLOWED,
ERR_UNKNOWN_FILE_EXTENSION } = require('internal/errors').codes; ERR_UNKNOWN_FILE_EXTENSION,
ERR_UNSUPPORTED_ESM_URL_SCHEME } = require('internal/errors').codes;


const realpathCache = new SafeMap(); const realpathCache = new SafeMap();


Expand Down Expand Up @@ -52,8 +53,9 @@ if (experimentalJsonModules)
extensionFormatMap['.json'] = legacyExtensionFormatMap['.json'] = 'json'; extensionFormatMap['.json'] = legacyExtensionFormatMap['.json'] = 'json';


function resolve(specifier, parentURL) { function resolve(specifier, parentURL) {
let parsed;
try { try {
const parsed = new URL(specifier); parsed = new URL(specifier);
if (parsed.protocol === 'data:') { if (parsed.protocol === 'data:') {
const [ , mime ] = /^([^/]+\/[^;,]+)(?:[^,]*?)(;base64)?,/.exec(parsed.pathname) || [ null, null, null ]; const [ , mime ] = /^([^/]+\/[^;,]+)(?:[^,]*?)(;base64)?,/.exec(parsed.pathname) || [ null, null, null ];
const format = ({ const format = ({
Expand All @@ -68,6 +70,8 @@ function resolve(specifier, parentURL) {
}; };
} }
} catch {} } catch {}
if (parsed && parsed.protocol !== 'file:' && parsed.protocol !== 'data:')
throw new ERR_UNSUPPORTED_ESM_URL_SCHEME();
if (NativeModule.canBeRequiredByUsers(specifier)) { if (NativeModule.canBeRequiredByUsers(specifier)) {
return { return {
url: specifier, url: specifier,
Expand Down
16 changes: 8 additions & 8 deletions test/es-module/test-esm-dynamic-import.js
Expand Up @@ -15,8 +15,8 @@ function expectErrorProperty(result, propertyKey, value) {
})); }));
} }


function expectMissingModuleError(result) { function expectModuleError(result, err) {
expectErrorProperty(result, 'code', 'ERR_MODULE_NOT_FOUND'); expectErrorProperty(result, 'code', err);
} }


function expectOkNamespace(result) { function expectOkNamespace(result) {
Expand Down Expand Up @@ -56,10 +56,10 @@ function expectFsNamespace(result) {
expectFsNamespace(eval('import("fs")')); expectFsNamespace(eval('import("fs")'));
expectFsNamespace(eval('import("fs")')); expectFsNamespace(eval('import("fs")'));


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

0 comments on commit a6c2502

Please sign in to comment.