Skip to content

Commit

Permalink
esm: fix imports from non-file module
Browse files Browse the repository at this point in the history
Fixes: #42860

PR-URL: #42881
Reviewed-By: Geoffrey Booth <[email protected]>
Reviewed-By: Jacob Smith <[email protected]>
Reviewed-By: Guy Bedford <[email protected]>
  • Loading branch information
aduh95 authored and juanarbol committed May 31, 2022
1 parent a6bceae commit c6f4a6f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
8 changes: 3 additions & 5 deletions lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -1050,8 +1050,6 @@ function resolveAsCommonJS(specifier, parentURL) {
// TODO(@JakobJingleheimer): de-dupe `specifier` & `parsed`
function checkIfDisallowedImport(specifier, parsed, parsedParentURL) {
if (parsedParentURL) {
const parentURL = fileURLToPath(parsedParentURL?.href);

if (
parsedParentURL.protocol === 'http:' ||
parsedParentURL.protocol === 'https:'
Expand All @@ -1065,7 +1063,7 @@ function checkIfDisallowedImport(specifier, parsed, parsedParentURL) {
) {
throw new ERR_NETWORK_IMPORT_DISALLOWED(
specifier,
parentURL,
parsedParentURL,
'remote imports cannot import from a local location.'
);
}
Expand All @@ -1075,14 +1073,14 @@ function checkIfDisallowedImport(specifier, parsed, parsedParentURL) {
if (NativeModule.canBeRequiredByUsers(specifier)) {
throw new ERR_NETWORK_IMPORT_DISALLOWED(
specifier,
parentURL,
parsedParentURL,
'remote imports cannot import from a local location.'
);
}

throw new ERR_NETWORK_IMPORT_DISALLOWED(
specifier,
parentURL,
parsedParentURL,
'only relative and absolute specifiers are supported.'
);
}
Expand Down
5 changes: 5 additions & 0 deletions test/es-module/test-esm-data-urls.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
const common = require('../common');
const fixtures = require('../common/fixtures');
const assert = require('assert');
function createURL(mime, body) {
return `data:${mime},${body}`;
Expand Down Expand Up @@ -107,4 +108,8 @@ function createBase64URL(mime, body) {
const module = await import(plainESMURL);
assert.strictEqual(module.default, 2);
}
{
const plainESMURL = `data:text/javascript,${encodeURIComponent(`import ${JSON.stringify(fixtures.fileURL('es-module-url', 'empty.js'))}`)}`;
await import(plainESMURL);
}
})().then(common.mustCall());
6 changes: 3 additions & 3 deletions test/es-module/test-http-imports.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ for (const { protocol, createServer } of [
export default 1;`);
await assert.rejects(
import(fileDep.href),
{ code: 'ERR_INVALID_URL_SCHEME' }
{ code: 'ERR_NETWORK_IMPORT_DISALLOWED' }
);

const builtinDep = new URL(url.href);
Expand All @@ -172,7 +172,7 @@ for (const { protocol, createServer } of [
`);
await assert.rejects(
import(builtinDep.href),
{ code: 'ERR_INVALID_URL_SCHEME' }
{ code: 'ERR_NETWORK_IMPORT_DISALLOWED' }
);

const unprefixedBuiltinDep = new URL(url.href);
Expand All @@ -182,7 +182,7 @@ for (const { protocol, createServer } of [
`);
await assert.rejects(
import(unprefixedBuiltinDep.href),
{ code: 'ERR_INVALID_URL_SCHEME' }
{ code: 'ERR_NETWORK_IMPORT_DISALLOWED' }
);

const unsupportedMIME = new URL(url.href);
Expand Down

0 comments on commit c6f4a6f

Please sign in to comment.