Skip to content

Commit c6f4a6f

Browse files
aduh95juanarbol
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 a6bceae commit c6f4a6f

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
@@ -1050,8 +1050,6 @@ function resolveAsCommonJS(specifier, parentURL) {
10501050
// TODO(@JakobJingleheimer): de-dupe `specifier` & `parsed`
10511051
function checkIfDisallowedImport(specifier, parsed, parsedParentURL) {
10521052
if (parsedParentURL) {
1053-
const parentURL = fileURLToPath(parsedParentURL?.href);
1054-
10551053
if (
10561054
parsedParentURL.protocol === 'http:' ||
10571055
parsedParentURL.protocol === 'https:'
@@ -1065,7 +1063,7 @@ function checkIfDisallowedImport(specifier, parsed, parsedParentURL) {
10651063
) {
10661064
throw new ERR_NETWORK_IMPORT_DISALLOWED(
10671065
specifier,
1068-
parentURL,
1066+
parsedParentURL,
10691067
'remote imports cannot import from a local location.'
10701068
);
10711069
}
@@ -1075,14 +1073,14 @@ function checkIfDisallowedImport(specifier, parsed, parsedParentURL) {
10751073
if (NativeModule.canBeRequiredByUsers(specifier)) {
10761074
throw new ERR_NETWORK_IMPORT_DISALLOWED(
10771075
specifier,
1078-
parentURL,
1076+
parsedParentURL,
10791077
'remote imports cannot import from a local location.'
10801078
);
10811079
}
10821080

10831081
throw new ERR_NETWORK_IMPORT_DISALLOWED(
10841082
specifier,
1085-
parentURL,
1083+
parsedParentURL,
10861084
'only relative and absolute specifiers are supported.'
10871085
);
10881086
}

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
@@ -162,7 +162,7 @@ for (const { protocol, createServer } of [
162162
export default 1;`);
163163
await assert.rejects(
164164
import(fileDep.href),
165-
{ code: 'ERR_INVALID_URL_SCHEME' }
165+
{ code: 'ERR_NETWORK_IMPORT_DISALLOWED' }
166166
);
167167

168168
const builtinDep = new URL(url.href);
@@ -172,7 +172,7 @@ for (const { protocol, createServer } of [
172172
`);
173173
await assert.rejects(
174174
import(builtinDep.href),
175-
{ code: 'ERR_INVALID_URL_SCHEME' }
175+
{ code: 'ERR_NETWORK_IMPORT_DISALLOWED' }
176176
);
177177

178178
const unprefixedBuiltinDep = new URL(url.href);
@@ -182,7 +182,7 @@ for (const { protocol, createServer } of [
182182
`);
183183
await assert.rejects(
184184
import(unprefixedBuiltinDep.href),
185-
{ code: 'ERR_INVALID_URL_SCHEME' }
185+
{ code: 'ERR_NETWORK_IMPORT_DISALLOWED' }
186186
);
187187

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

0 commit comments

Comments
 (0)