module: fix $ pattern replacements

PR-URL: https://github.com/nodejs/node/pull/40044
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Zeyu Yang <himself65@outlook.com>
This commit is contained in:
Guy Bedford 2021-09-08 14:15:57 -07:00
parent 1b2749ecbe
commit 5c1adda0ae
2 changed files with 8 additions and 5 deletions

View File

@ -10,6 +10,7 @@ const {
ObjectGetOwnPropertyNames,
ObjectPrototypeHasOwnProperty,
RegExp,
RegExpPrototypeSymbolReplace,
RegExpPrototypeTest,
SafeMap,
SafeSet,
@ -18,7 +19,6 @@ const {
StringPrototypeIncludes,
StringPrototypeIndexOf,
StringPrototypeLastIndexOf,
StringPrototypeReplace,
StringPrototypeSlice,
StringPrototypeSplit,
StringPrototypeStartsWith,
@ -470,7 +470,7 @@ function resolvePackageTargetString(
} catch {}
if (!isURL) {
const exportTarget = pattern ?
StringPrototypeReplace(target, patternRegEx, subpath) :
RegExpPrototypeSymbolReplace(patternRegEx, target, () => subpath) :
target + subpath;
return packageResolve(exportTarget, packageJSONUrl, conditions);
}
@ -494,8 +494,8 @@ function resolvePackageTargetString(
throwInvalidSubpath(match + subpath, packageJSONUrl, internal, base);
if (pattern)
return new URL(StringPrototypeReplace(resolved.href, patternRegEx,
subpath));
return new URL(RegExpPrototypeSymbolReplace(patternRegEx, resolved.href,
() => subpath));
return new URL(subpath, resolved);
}
@ -939,7 +939,8 @@ function resolveAsCommonJS(specifier, parentURL) {
// Normalize the path separator to give a valid suggestion
// on Windows
if (process.platform === 'win32') {
found = StringPrototypeReplace(found, new RegExp(`\\${sep}`, 'g'), '/');
found = RegExpPrototypeSymbolReplace(new RegExp(`\\${sep}`, 'g'),
found, '/');
}
return found;
} catch {

View File

@ -156,6 +156,8 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js';
['pkgexports/no-ext', `pkgexports${sep}asdf`],
// Pattern specificity
['pkgexports/dir2/trailer', `subpath${sep}dir2.js`],
// Pattern double $$ escaping!
['pkgexports/a/$$', `subpath${sep}$$.js`],
]);
if (!isRequire) {