esm: fix import.meta.resolve crash

PR-URL: https://github.com/nodejs/node/pull/55777
Fixes: https://github.com/nodejs/node/issues/55518
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
Marco Ippolito authored and Antoine du Hamel committed 2024-11-16 18:00:55 +01:00
1 parent 9aeb671677
commit 83ba688d8f
2 files changed
+16 -1

No files matched your search

+4 -1
View File
@@ -412,7 +412,10 @@ void BindingData::GetPackageScopeConfig(
}
auto file_url = url::FileURLToPath(realm->env(), *package_json_url);
CHECK(file_url);
if (!file_url) {
url::ThrowInvalidURL(realm->env(), resolved.ToStringView(), std::nullopt);
return;
}
error_context.specifier = resolved.ToString();
auto package_json = GetPackageJSON(realm, *file_url, &error_context);
if (package_json != nullptr) {
@@ -106,3 +106,15 @@ await assert.rejects(import('data:text/javascript,export default import.meta.res
stdout: 'http://example.com/\n',
});
}
{
const result = await spawnPromisified(execPath, [
'--no-warnings',
'--experimental-import-meta-resolve',
'--eval',
'import.meta.resolve("foo", "http://example.com/bar.js")',
]);
assert.match(result.stderr, /ERR_INVALID_URL/);
assert.strictEqual(result.stdout, '');
assert.strictEqual(result.code, 1);
}