module: simplify findPackageJSON implementation

PR-URL: https://github.com/nodejs/node/pull/55543
Backport-PR-URL: https://github.com/nodejs/node/pull/56494
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
This commit is contained in:
Antoine du Hamel committed 2025-02-06 01:10:43 +01:00
1 parent 6d6cffa9cc
commit bbedffa0f0
1 file changed
+4 -13
+4 -13
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;
}