Skip to content

Commit

Permalink
Use RegExpPrototypeSymbolReplace over StringPrototypeReplaceAll
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed Dec 25, 2022
1 parent 44655cc commit 80452de
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,21 @@ const {
ReflectApply,
ReflectGetOwnPropertyDescriptor,
ReflectOwnKeys,
RegExpPrototypeSymbolReplace,
String,
StringPrototypeCharAt,
StringPrototypeCharCodeAt,
StringPrototypeCodePointAt,
StringPrototypeIncludes,
StringPrototypeIndexOf,
StringPrototypeReplaceAll,
StringPrototypeSlice,
StringPrototypeSplit,
StringPrototypeStartsWith,
Symbol,
SymbolIterator,
SymbolToStringTag,
decodeURIComponent,
hardenRegExp,
} = primordials;

const { inspect } = require('internal/util/inspect');
Expand Down Expand Up @@ -116,6 +117,8 @@ const {
revokeDataObject,
} = internalBinding('blob');

const FORWARD_SLASH = hardenRegExp(/\//g);

const context = Symbol('context');
const cannotBeBase = Symbol('cannot-be-base');
const cannotHaveUsernamePasswordPort =
Expand Down Expand Up @@ -1442,7 +1445,7 @@ function getPathFromURLWin32(url) {
}
}
}
pathname = StringPrototypeReplaceAll(pathname, '/', '\\');
pathname = RegExpPrototypeSymbolReplace(FORWARD_SLASH, pathname, '\\');
pathname = decodeURIComponent(pathname);
if (hostname !== '') {
// If hostname is set, then we have a UNC path
Expand Down Expand Up @@ -1502,13 +1505,19 @@ function fileURLToPath(path) {
// - CR: The carriage return character is also stripped out by the `pathname`
// setter.
// - TAB: The tab character is also stripped out by the `pathname` setter.
const percentRegEx = hardenRegExp(/%/g);
const backslashRegEx = hardenRegExp(/\\/g);
const newlineRegEx = hardenRegExp(/\n/g);
const carriageReturnRegEx = hardenRegExp(/\r/g);
const tabRegEx = hardenRegExp(/\t/g);

function encodePathChars(filepath) {
filepath = StringPrototypeReplaceAll(filepath, '%', '%25');
filepath = RegExpPrototypeSymbolReplace(percentRegEx, filepath, '%25');
// In posix, backslash is a valid character in paths:
if (!isWindows) filepath = StringPrototypeReplaceAll(filepath, '\\', '%5C');
filepath = StringPrototypeReplaceAll(filepath, '\n', '%0A');
filepath = StringPrototypeReplaceAll(filepath, '\r', '%0D');
filepath = StringPrototypeReplaceAll(filepath, '\t', '%09');
if (!isWindows) filepath = RegExpPrototypeSymbolReplace(backslashRegEx, filepath, '%5C');
filepath = RegExpPrototypeSymbolReplace(newlineRegEx, filepath, '%0A');
filepath = RegExpPrototypeSymbolReplace(carriageReturnRegEx, filepath, '%0D');
filepath = RegExpPrototypeSymbolReplace(tabRegEx, filepath, '%09');
return filepath;
}

Expand All @@ -1534,7 +1543,7 @@ function pathToFileURL(filepath) {
const hostname = StringPrototypeSlice(filepath, 2, hostnameEndIndex);
outURL.hostname = domainToASCII(hostname);
outURL.pathname = encodePathChars(
StringPrototypeReplaceAll(StringPrototypeSlice(filepath, hostnameEndIndex), '\\', '/'));
RegExpPrototypeSymbolReplace(backslashRegEx, StringPrototypeSlice(filepath, hostnameEndIndex), '/'));
} else {
let resolved = path.resolve(filepath);
// path.resolve strips trailing slashes so we must add them back
Expand Down

0 comments on commit 80452de

Please sign in to comment.