Skip to content

Commit

Permalink
esm: identify parent importing a url with invalid host
Browse files Browse the repository at this point in the history
PR-URL: nodejs#49736
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Geoffrey Booth <[email protected]>
  • Loading branch information
JakobJingleheimer authored and targos committed Nov 11, 2023
1 parent aadc39f commit efd4e8c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,16 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
resolved.pathname, 'must not include encoded "/" or "\\" characters',
fileURLToPath(base));

let path = fileURLToPath(resolved);
let path;
try {
path = fileURLToPath(resolved);
} catch (err) {
const { setOwnProperty } = require('internal/util');
setOwnProperty(err, 'input', `${resolved}`);
setOwnProperty(err, 'module', `${base}`);
throw err;
}

if (getOptionValue('--experimental-specifier-resolution') === 'node') {
let file = resolveExtensionsWithTryExactName(resolved);

Expand Down
14 changes: 14 additions & 0 deletions test/es-module/test-esm-loader-default-resolver.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,18 @@ describe('default resolver', () => {
assert.strictEqual(stdout.trim(), 'index.byoe!');
assert.strictEqual(stderr, '');
});

it('should identify the parent module of an invalid URL host in import specifier', async () => {
if (process.platform === 'win32') return;

const { code, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
fixtures.path('es-modules', 'invalid-posix-host.mjs'),
]);

assert.match(stderr, /ERR_INVALID_FILE_URL_HOST/);
assert.match(stderr, /file:\/\/hmm\.js/);
assert.match(stderr, /invalid-posix-host\.mjs/);
assert.strictEqual(code, 1);
});
});
1 change: 1 addition & 0 deletions test/fixtures/es-modules/invalid-posix-host.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "file://hmm.js";

0 comments on commit efd4e8c

Please sign in to comment.