From 869e88c6a8ebe780edd70dd81fbebf108e5e817d Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sat, 2 Nov 2024 16:13:23 +0100 Subject: [PATCH] module: simplify `findPackageJSON` implementation PR-URL: https://github.com/nodejs/node/pull/55543 Reviewed-By: Jacob Smith Reviewed-By: Marco Ippolito --- lib/internal/modules/package_json_reader.js | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/lib/internal/modules/package_json_reader.js b/lib/internal/modules/package_json_reader.js index d55ccbad64d0e6..92f46ed3971341 100644 --- a/lib/internal/modules/package_json_reader.js +++ b/lib/internal/modules/package_json_reader.js @@ -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; }