From a0638a23b099b5ca174680324e856533d765be09 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Fri, 29 Apr 2022 16:04:12 +0200 Subject: [PATCH] esm: fix imports from non-file module Fixes: https://github.com/nodejs/node/issues/42860 PR-URL: https://github.com/nodejs/node/pull/42881 Reviewed-By: Geoffrey Booth Reviewed-By: Jacob Smith Reviewed-By: Guy Bedford --- lib/internal/modules/esm/resolve.js | 8 +++----- test/es-module/test-esm-data-urls.js | 5 +++++ test/es-module/test-http-imports.mjs | 6 +++--- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index f1298e58dc445c..8767109d7c4177 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -1015,8 +1015,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:' @@ -1030,7 +1028,7 @@ function checkIfDisallowedImport(specifier, parsed, parsedParentURL) { ) { throw new ERR_NETWORK_IMPORT_DISALLOWED( specifier, - parentURL, + parsedParentURL, 'remote imports cannot import from a local location.' ); } @@ -1041,14 +1039,14 @@ function checkIfDisallowedImport(specifier, parsed, parsedParentURL) { NativeModule.canBeRequiredWithoutScheme(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.' ); } diff --git a/test/es-module/test-esm-data-urls.js b/test/es-module/test-esm-data-urls.js index 9d0deb70a1568c..5be45d0f7af3b6 100644 --- a/test/es-module/test-esm-data-urls.js +++ b/test/es-module/test-esm-data-urls.js @@ -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}`; @@ -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()); diff --git a/test/es-module/test-http-imports.mjs b/test/es-module/test-http-imports.mjs index 5b1f2dbc578e35..5139a2e69e0f60 100644 --- a/test/es-module/test-http-imports.mjs +++ b/test/es-module/test-http-imports.mjs @@ -167,7 +167,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); @@ -177,7 +177,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); @@ -187,7 +187,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);