Skip to content

Commit bbd5318

Browse files
committed
esm: add a fallback when importer in not a file
PR-URL: #55471 Reviewed-By: Jacob Smith <jacob@frende.me> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jake Yuesong Li <jake.yuesong@gmail.com>
1 parent 8634e05 commit bbd5318

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

lib/internal/modules/esm/resolve.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,15 @@ const encodedSepRegEx = /%2F|%5C/i;
227227
*/
228228
function finalizeResolution(resolved, base, preserveSymlinks) {
229229
if (RegExpPrototypeExec(encodedSepRegEx, resolved.pathname) !== null) {
230+
let basePath;
231+
try {
232+
basePath = fileURLToPath(base);
233+
} catch {
234+
basePath = base;
235+
}
230236
throw new ERR_INVALID_MODULE_SPECIFIER(
231237
resolved.pathname, 'must not include encoded "/" or "\\" characters',
232-
fileURLToPath(base));
238+
basePath);
233239
}
234240

235241
let path;
@@ -248,14 +254,26 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
248254

249255
// Check for stats.isDirectory()
250256
if (stats === 1) {
251-
throw new ERR_UNSUPPORTED_DIR_IMPORT(path, fileURLToPath(base), String(resolved));
257+
let basePath;
258+
try {
259+
basePath = fileURLToPath(base);
260+
} catch {
261+
basePath = base;
262+
}
263+
throw new ERR_UNSUPPORTED_DIR_IMPORT(path, basePath, String(resolved));
252264
} else if (stats !== 0) {
253265
// Check for !stats.isFile()
254266
if (process.env.WATCH_REPORT_DEPENDENCIES && process.send) {
255267
process.send({ 'watch:require': [path || resolved.pathname] });
256268
}
269+
let basePath;
270+
try {
271+
basePath = fileURLToPath(base);
272+
} catch {
273+
basePath = base;
274+
}
257275
throw new ERR_MODULE_NOT_FOUND(
258-
path || resolved.pathname, base && fileURLToPath(base), resolved);
276+
path || resolved.pathname, basePath, resolved);
259277
}
260278

261279
if (!preserveSymlinks) {

test/es-module/test-esm-main-lookup.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ await assert.rejects(import('../fixtures/es-modules/pjson-main'), {
1515
code: 'ERR_UNSUPPORTED_DIR_IMPORT',
1616
url: fixtures.fileURL('es-modules/pjson-main').href,
1717
});
18+
await assert.rejects(import(`data:text/javascript,import${encodeURIComponent(JSON.stringify(fixtures.fileURL('es-modules/pjson-main')))}`), {
19+
code: 'ERR_UNSUPPORTED_DIR_IMPORT',
20+
url: fixtures.fileURL('es-modules/pjson-main').href,
21+
});
22+
await assert.rejects(import(`data:text/javascript,import${encodeURIComponent(JSON.stringify(fixtures.fileURL('es-modules/does-not-exist')))}`), {
23+
code: 'ERR_MODULE_NOT_FOUND',
24+
url: fixtures.fileURL('es-modules/does-not-exist').href,
25+
});
1826

1927
assert.deepStrictEqual(
2028
{ ...await import('../fixtures/es-modules/pjson-main/main.mjs') },

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy