Skip to content

Commit a0638a2

Browse files
aduh95targos
authored andcommitted
esm: fix imports from non-file module
Fixes: #42860 PR-URL: #42881 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Jacob Smith <jacob@frende.me> Reviewed-By: Guy Bedford <guybedford@gmail.com>
1 parent 3c796f8 commit a0638a2

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

lib/internal/modules/esm/resolve.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1015,8 +1015,6 @@ function resolveAsCommonJS(specifier, parentURL) {
10151015
// TODO(@JakobJingleheimer): de-dupe `specifier` & `parsed`
10161016
function checkIfDisallowedImport(specifier, parsed, parsedParentURL) {
10171017
if (parsedParentURL) {
1018-
const parentURL = fileURLToPath(parsedParentURL?.href);
1019-
10201018
if (
10211019
parsedParentURL.protocol === 'http:' ||
10221020
parsedParentURL.protocol === 'https:'
@@ -1030,7 +1028,7 @@ function checkIfDisallowedImport(specifier, parsed, parsedParentURL) {
10301028
) {
10311029
throw new ERR_NETWORK_IMPORT_DISALLOWED(
10321030
specifier,
1033-
parentURL,
1031+
parsedParentURL,
10341032
'remote imports cannot import from a local location.'
10351033
);
10361034
}
@@ -1041,14 +1039,14 @@ function checkIfDisallowedImport(specifier, parsed, parsedParentURL) {
10411039
NativeModule.canBeRequiredWithoutScheme(specifier)) {
10421040
throw new ERR_NETWORK_IMPORT_DISALLOWED(
10431041
specifier,
1044-
parentURL,
1042+
parsedParentURL,
10451043
'remote imports cannot import from a local location.'
10461044
);
10471045
}
10481046

10491047
throw new ERR_NETWORK_IMPORT_DISALLOWED(
10501048
specifier,
1051-
parentURL,
1049+
parsedParentURL,
10521050
'only relative and absolute specifiers are supported.'
10531051
);
10541052
}

test/es-module/test-esm-data-urls.js

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22
const common = require('../common');
3+
const fixtures = require('../common/fixtures');
34
const assert = require('assert');
45
function createURL(mime, body) {
56
return `data:${mime},${body}`;
@@ -107,4 +108,8 @@ function createBase64URL(mime, body) {
107108
const module = await import(plainESMURL);
108109
assert.strictEqual(module.default, 2);
109110
}
111+
{
112+
const plainESMURL = `data:text/javascript,${encodeURIComponent(`import ${JSON.stringify(fixtures.fileURL('es-module-url', 'empty.js'))}`)}`;
113+
await import(plainESMURL);
114+
}
110115
})().then(common.mustCall());

test/es-module/test-http-imports.mjs

+3-3
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ for (const { protocol, createServer } of [
167167
export default 1;`);
168168
await assert.rejects(
169169
import(fileDep.href),
170-
{ code: 'ERR_INVALID_URL_SCHEME' }
170+
{ code: 'ERR_NETWORK_IMPORT_DISALLOWED' }
171171
);
172172

173173
const builtinDep = new URL(url.href);
@@ -177,7 +177,7 @@ for (const { protocol, createServer } of [
177177
`);
178178
await assert.rejects(
179179
import(builtinDep.href),
180-
{ code: 'ERR_INVALID_URL_SCHEME' }
180+
{ code: 'ERR_NETWORK_IMPORT_DISALLOWED' }
181181
);
182182

183183
const unprefixedBuiltinDep = new URL(url.href);
@@ -187,7 +187,7 @@ for (const { protocol, createServer } of [
187187
`);
188188
await assert.rejects(
189189
import(unprefixedBuiltinDep.href),
190-
{ code: 'ERR_INVALID_URL_SCHEME' }
190+
{ code: 'ERR_NETWORK_IMPORT_DISALLOWED' }
191191
);
192192

193193
const unsupportedMIME = new URL(url.href);

0 commit comments

Comments
 (0)