module: simplify findPackageJSON implementation

PR-URL: https://github.com/nodejs/node/pull/55543
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
This commit is contained in:
Antoine du Hamel 2024-11-02 16:13:23 +01:00 committed by GitHub
parent 10cce655ca
commit c0db893f04
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -7,7 +7,6 @@ const {
RegExpPrototypeExec,
StringPrototypeIndexOf,
StringPrototypeSlice,
StringPrototypeStartsWith,
} = primordials;
const {
fileURLToPath,
@ -285,22 +284,14 @@ function findPackageJSON(specifier, base = 'data:') {
validateString(specifier, 'specifier');
}
let parentURL;
if (isURL(base)) {
parentURL = new URL(base);
} else {
let parentURL = base;
if (!isURL(base)) {
validateString(base, 'base');
if (
path.isAbsolute(base) ||
(URLCanParse(base) && !StringPrototypeStartsWith(base, 'file:'))
) {
parentURL = pathToFileURL(path.toNamespacedPath(base));
} else {
parentURL = URL.parse(base) || pathToFileURL(base);
}
parentURL = path.isAbsolute(base) ? pathToFileURL(base) : new URL(base);
}
if (specifier && specifier[0] !== '.' && specifier[0] !== '/' && !URLCanParse(specifier)) {
// If `specifier` is a bare specifier.
const { packageJSONPath } = getPackageJSONURL(specifier, parentURL);
return packageJSONPath;
}