Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

url: refactor to use more primordials #45966

Merged
merged 11 commits into from
Jan 22, 2023
Prev Previous commit
Next Next commit
Use RegExpPrototypeSymbolReplace over StringPrototypeReplaceAll
  • Loading branch information
aduh95 committed Dec 25, 2022
commit 80452de08ad947d67708ae810cbb7fc805d2b33f
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