esm: identify parent importing a url with invalid host

PR-URL: https://github.com/nodejs/node/pull/49736
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
This commit is contained in:
Jacob Smith 2023-09-23 07:41:26 +02:00 committed by GitHub
parent 017998971b
commit 645b788bea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 1 deletions

View file

@ -231,7 +231,15 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
fileURLToPath(base));
}
const 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;
}
const stats = internalModuleStat(toNamespacedPath(StringPrototypeEndsWith(path, '/') ?
StringPrototypeSlice(path, -1) : path));

View file

@ -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);
});
});

View file

@ -0,0 +1 @@
import "file://hmm.js";