-
-
Notifications
You must be signed in to change notification settings - Fork 34.2k
url: refactor to use more primordials #45966
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
Changes from 1 commit
44655cc
80452de
2475de3
b2ab16c
b8d193f
00cd18f
1fc0fb0
3a61d80
1ceaa2c
fc4fc3f
256e4bd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ const { | |
| ArrayPrototypeSlice, | ||
| FunctionPrototypeBind, | ||
| Int8Array, | ||
| IteratorPrototype, | ||
| Number, | ||
| ObjectCreate, | ||
| ObjectDefineProperties, | ||
|
|
@@ -20,9 +21,11 @@ const { | |
| ReflectGetOwnPropertyDescriptor, | ||
| ReflectOwnKeys, | ||
| String, | ||
| StringPrototypeCharAt, | ||
| StringPrototypeCharCodeAt, | ||
| StringPrototypeCodePointAt, | ||
| StringPrototypeIncludes, | ||
| StringPrototypeReplace, | ||
| StringPrototypeIndexOf, | ||
| StringPrototypeReplaceAll, | ||
| StringPrototypeSlice, | ||
| StringPrototypeSplit, | ||
|
|
@@ -139,11 +142,6 @@ function lazyCryptoRandom() { | |
| return cryptoRandom; | ||
| } | ||
|
|
||
| // https://tc39.github.io/ecma262/#sec-%iteratorprototype%-object | ||
| const IteratorPrototype = ObjectGetPrototypeOf( | ||
| ObjectGetPrototypeOf([][SymbolIterator]()) | ||
| ); | ||
|
|
||
| // Refs: https://html.spec.whatwg.org/multipage/browsers.html#concept-origin-opaque | ||
| const kOpaqueOrigin = 'null'; | ||
|
|
||
|
|
@@ -1377,7 +1375,7 @@ defineIDLClass(URLSearchParamsIteratorPrototype, 'URLSearchParams Iterator', { | |
| }, | ||
| [] | ||
| ); | ||
| const breakLn = inspect(output, innerOpts).includes('\n'); | ||
| const breakLn = StringPrototypeIncludes(inspect(output, innerOpts), '\n'); | ||
| const outputStrs = ArrayPrototypeMap(output, (p) => inspect(p, innerOpts)); | ||
| let outputStr; | ||
| if (breakLn) { | ||
|
|
@@ -1435,7 +1433,7 @@ function getPathFromURLWin32(url) { | |
| let pathname = url.pathname; | ||
| for (let n = 0; n < pathname.length; n++) { | ||
| if (pathname[n] === '%') { | ||
| const third = pathname.codePointAt(n + 2) | 0x20; | ||
| const third = StringPrototypeCodePointAt(pathname, n + 2) | 0x20; | ||
| if ((pathname[n + 1] === '2' && third === 102) || // 2f 2F / | ||
| (pathname[n + 1] === '5' && third === 99)) { // 5c 5C \ | ||
| throw new ERR_INVALID_FILE_URL_PATH( | ||
|
|
@@ -1456,13 +1454,13 @@ function getPathFromURLWin32(url) { | |
| return `\\\\${domainToUnicode(hostname)}${pathname}`; | ||
| } | ||
| // Otherwise, it's a local path that requires a drive letter | ||
| const letter = pathname.codePointAt(1) | 0x20; | ||
| const sep = pathname[2]; | ||
| const letter = StringPrototypeCodePointAt(pathname, 1) | 0x20; | ||
| const sep = StringPrototypeCharAt(pathname, 2); | ||
| if (letter < CHAR_LOWERCASE_A || letter > CHAR_LOWERCASE_Z || // a..z A..Z | ||
| (sep !== ':')) { | ||
| throw new ERR_INVALID_FILE_URL_PATH('must be absolute'); | ||
| } | ||
| return pathname.slice(1); | ||
| return StringPrototypeSlice(pathname, 1); | ||
| } | ||
|
|
||
| function getPathFromURLPosix(url) { | ||
|
|
@@ -1472,7 +1470,7 @@ function getPathFromURLPosix(url) { | |
| const pathname = url.pathname; | ||
| for (let n = 0; n < pathname.length; n++) { | ||
| if (pathname[n] === '%') { | ||
| const third = pathname.codePointAt(n + 2) | 0x20; | ||
| const third = StringPrototypeCodePointAt(pathname, n + 2) | 0x20; | ||
| if (pathname[n + 1] === '2' && third === 102) { | ||
| throw new ERR_INVALID_FILE_URL_PATH( | ||
| 'must not include encoded / characters' | ||
|
|
@@ -1504,50 +1502,39 @@ 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 = /%/g; | ||
| const backslashRegEx = /\\/g; | ||
| const newlineRegEx = /\n/g; | ||
| const carriageReturnRegEx = /\r/g; | ||
| const tabRegEx = /\t/g; | ||
|
|
||
| function encodePathChars(filepath) { | ||
| if (StringPrototypeIncludes(filepath, '%')) | ||
| filepath = StringPrototypeReplace(filepath, percentRegEx, '%25'); | ||
| filepath = StringPrototypeReplaceAll(filepath, '%', '%25'); | ||
| // In posix, backslash is a valid character in paths: | ||
| if (!isWindows && StringPrototypeIncludes(filepath, '\\')) | ||
| filepath = StringPrototypeReplace(filepath, backslashRegEx, '%5C'); | ||
| if (StringPrototypeIncludes(filepath, '\n')) | ||
| filepath = StringPrototypeReplace(filepath, newlineRegEx, '%0A'); | ||
| if (StringPrototypeIncludes(filepath, '\r')) | ||
| filepath = StringPrototypeReplace(filepath, carriageReturnRegEx, '%0D'); | ||
| if (StringPrototypeIncludes(filepath, '\t')) | ||
| filepath = StringPrototypeReplace(filepath, tabRegEx, '%09'); | ||
| if (!isWindows) filepath = StringPrototypeReplaceAll(filepath, '\\', '%5C'); | ||
| filepath = StringPrototypeReplaceAll(filepath, '\n', '%0A'); | ||
| filepath = StringPrototypeReplaceAll(filepath, '\r', '%0D'); | ||
| filepath = StringPrototypeReplaceAll(filepath, '\t', '%09'); | ||
|
||
| return filepath; | ||
| } | ||
|
|
||
| function pathToFileURL(filepath) { | ||
| const outURL = new URL('file://'); | ||
| if (isWindows && StringPrototypeStartsWith(filepath, '\\\\')) { | ||
| // UNC path format: \\server\share\resource | ||
| const paths = StringPrototypeSplit(filepath, '\\'); | ||
| if (paths.length <= 3) { | ||
| const hostnameEndIndex = StringPrototypeIndexOf('\\', 2); | ||
anonrig marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
aduh95 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (hostnameEndIndex === -1) { | ||
| throw new ERR_INVALID_ARG_VALUE( | ||
| 'filepath', | ||
| filepath, | ||
| 'Missing UNC resource path' | ||
| ); | ||
| } | ||
| const hostname = paths[2]; | ||
| if (hostname.length === 0) { | ||
| if (hostnameEndIndex === 3) { | ||
| throw new ERR_INVALID_ARG_VALUE( | ||
| 'filepath', | ||
| filepath, | ||
| 'Empty UNC servername' | ||
| ); | ||
| } | ||
| const hostname = StringPrototypeSlice(filepath, 2, hostnameEndIndex); | ||
| outURL.hostname = domainToASCII(hostname); | ||
| outURL.pathname = encodePathChars( | ||
| ArrayPrototypeJoin(ArrayPrototypeSlice(paths, 3), '/')); | ||
| StringPrototypeReplaceAll(StringPrototypeSlice(filepath, hostnameEndIndex), '\\', '/')); | ||
|
||
| } else { | ||
| let resolved = path.resolve(filepath); | ||
| // path.resolve strips trailing slashes so we must add them back | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: should we add this url to primordials declaration too?