From 5c1adda0aeadc976689c3619af6c5bcd798792ab Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Wed, 8 Sep 2021 14:15:57 -0700 Subject: [PATCH] module: fix $ pattern replacements PR-URL: https://github.com/nodejs/node/pull/40044 Reviewed-By: Antoine du Hamel Reviewed-By: James M Snell Reviewed-By: Zeyu Yang --- lib/internal/modules/esm/resolve.js | 11 ++++++----- test/es-module/test-esm-exports.mjs | 2 ++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index 29d4a8298ca9b8..f9b7329b071d38 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -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 { diff --git a/test/es-module/test-esm-exports.mjs b/test/es-module/test-esm-exports.mjs index a99814b0950d10..0bf361d4863fff 100644 --- a/test/es-module/test-esm-exports.mjs +++ b/test/es-module/test-esm-exports.mjs @@ -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) {