2016-05-31 18:52:19 +00:00
|
|
|
'use strict';
|
|
|
|
|
2019-11-22 17:04:46 +00:00
|
|
|
const {
|
2019-11-23 09:09:05 +00:00
|
|
|
Array,
|
2020-11-20 09:20:15 +00:00
|
|
|
ArrayPrototypeJoin,
|
|
|
|
ArrayPrototypeMap,
|
|
|
|
ArrayPrototypePush,
|
|
|
|
ArrayPrototypeReduce,
|
|
|
|
ArrayPrototypeSlice,
|
2023-03-03 21:34:56 +00:00
|
|
|
Boolean,
|
2020-07-05 00:47:39 +00:00
|
|
|
Int8Array,
|
2023-01-22 13:13:24 +00:00
|
|
|
IteratorPrototype,
|
2019-11-28 09:30:51 +00:00
|
|
|
Number,
|
2019-11-22 17:04:46 +00:00
|
|
|
ObjectDefineProperties,
|
|
|
|
ObjectDefineProperty,
|
|
|
|
ObjectGetOwnPropertySymbols,
|
|
|
|
ObjectGetPrototypeOf,
|
|
|
|
ObjectKeys,
|
|
|
|
ReflectGetOwnPropertyDescriptor,
|
|
|
|
ReflectOwnKeys,
|
2023-01-22 13:13:24 +00:00
|
|
|
RegExpPrototypeSymbolReplace,
|
|
|
|
StringPrototypeCharAt,
|
2020-11-20 09:20:15 +00:00
|
|
|
StringPrototypeCharCodeAt,
|
2023-01-22 13:13:24 +00:00
|
|
|
StringPrototypeCodePointAt,
|
2020-11-20 09:20:15 +00:00
|
|
|
StringPrototypeIncludes,
|
2023-01-22 13:13:24 +00:00
|
|
|
StringPrototypeIndexOf,
|
2020-11-20 09:20:15 +00:00
|
|
|
StringPrototypeSlice,
|
|
|
|
StringPrototypeSplit,
|
|
|
|
StringPrototypeStartsWith,
|
2019-11-30 15:55:29 +00:00
|
|
|
Symbol,
|
2019-12-08 21:35:19 +00:00
|
|
|
SymbolIterator,
|
2019-12-11 20:08:38 +00:00
|
|
|
SymbolToStringTag,
|
2021-02-16 13:31:29 +00:00
|
|
|
decodeURIComponent,
|
2019-11-22 17:04:46 +00:00
|
|
|
} = primordials;
|
2019-03-31 11:30:12 +00:00
|
|
|
|
2019-03-20 12:34:03 +00:00
|
|
|
const { inspect } = require('internal/util/inspect');
|
2017-03-15 04:01:04 +00:00
|
|
|
const {
|
2018-11-08 06:17:14 +00:00
|
|
|
encodeStr,
|
2017-03-15 04:01:04 +00:00
|
|
|
hexTable,
|
2023-02-26 10:34:02 +00:00
|
|
|
isHexTable,
|
2017-03-15 04:01:04 +00:00
|
|
|
} = require('internal/querystring');
|
2017-04-28 03:14:26 +00:00
|
|
|
|
2021-08-19 19:40:18 +00:00
|
|
|
const {
|
|
|
|
getConstructorOf,
|
|
|
|
removeColors,
|
|
|
|
toUSVString,
|
2022-02-11 17:30:47 +00:00
|
|
|
kEnumerableProperty,
|
2023-01-22 13:13:24 +00:00
|
|
|
SideEffectFreeRegExpPrototypeSymbolReplace,
|
2021-08-19 19:40:18 +00:00
|
|
|
} = require('internal/util');
|
|
|
|
|
2018-03-04 21:16:24 +00:00
|
|
|
const {
|
2021-08-07 02:26:37 +00:00
|
|
|
codes: {
|
|
|
|
ERR_ARG_NOT_ITERABLE,
|
|
|
|
ERR_INVALID_ARG_TYPE,
|
|
|
|
ERR_INVALID_ARG_VALUE,
|
|
|
|
ERR_INVALID_FILE_URL_HOST,
|
|
|
|
ERR_INVALID_FILE_URL_PATH,
|
|
|
|
ERR_INVALID_THIS,
|
|
|
|
ERR_INVALID_TUPLE,
|
|
|
|
ERR_INVALID_URL,
|
|
|
|
ERR_INVALID_URL_SCHEME,
|
|
|
|
ERR_MISSING_ARGS,
|
|
|
|
ERR_NO_CRYPTO,
|
|
|
|
},
|
|
|
|
} = require('internal/errors');
|
2018-03-12 15:59:31 +00:00
|
|
|
const {
|
|
|
|
CHAR_AMPERSAND,
|
2018-08-24 16:13:32 +00:00
|
|
|
CHAR_BACKWARD_SLASH,
|
2018-03-12 15:59:31 +00:00
|
|
|
CHAR_EQUAL,
|
2018-08-24 16:13:32 +00:00
|
|
|
CHAR_FORWARD_SLASH,
|
2018-03-12 15:59:31 +00:00
|
|
|
CHAR_LOWERCASE_A,
|
|
|
|
CHAR_LOWERCASE_Z,
|
2018-08-24 16:13:32 +00:00
|
|
|
CHAR_PERCENT,
|
2023-02-26 10:34:02 +00:00
|
|
|
CHAR_PLUS,
|
2018-03-12 15:59:31 +00:00
|
|
|
} = require('internal/constants');
|
2018-08-24 16:13:32 +00:00
|
|
|
const path = require('path');
|
2018-05-06 21:08:42 +00:00
|
|
|
|
2021-01-21 11:03:00 +00:00
|
|
|
const {
|
2022-01-24 16:09:16 +00:00
|
|
|
validateFunction,
|
2021-01-21 11:03:00 +00:00
|
|
|
} = require('internal/validators');
|
2020-12-23 11:22:00 +00:00
|
|
|
|
2021-05-19 18:50:13 +00:00
|
|
|
const querystring = require('querystring');
|
2017-04-28 03:14:26 +00:00
|
|
|
|
|
|
|
const { platform } = process;
|
|
|
|
const isWindows = platform === 'win32';
|
|
|
|
|
|
|
|
const {
|
|
|
|
domainToASCII: _domainToASCII,
|
|
|
|
domainToUnicode: _domainToUnicode,
|
2018-11-13 23:53:35 +00:00
|
|
|
parse,
|
2023-02-06 13:30:55 +00:00
|
|
|
updateUrl,
|
2018-08-09 06:00:10 +00:00
|
|
|
} = internalBinding('url');
|
2017-04-28 03:14:26 +00:00
|
|
|
|
2021-08-07 02:26:37 +00:00
|
|
|
const {
|
|
|
|
storeDataObject,
|
|
|
|
revokeDataObject,
|
|
|
|
} = internalBinding('blob');
|
|
|
|
|
2023-01-22 13:13:24 +00:00
|
|
|
const FORWARD_SLASH = /\//g;
|
|
|
|
|
2016-05-31 18:52:19 +00:00
|
|
|
const context = Symbol('context');
|
|
|
|
const searchParams = Symbol('query');
|
|
|
|
|
2023-02-06 13:30:55 +00:00
|
|
|
const updateActions = {
|
|
|
|
kProtocol: 0,
|
|
|
|
kHost: 1,
|
|
|
|
kHostname: 2,
|
|
|
|
kPort: 3,
|
|
|
|
kUsername: 4,
|
|
|
|
kPassword: 5,
|
|
|
|
kPathname: 6,
|
|
|
|
kSearch: 7,
|
|
|
|
kHash: 8,
|
|
|
|
kHref: 9,
|
|
|
|
};
|
2021-08-07 02:26:37 +00:00
|
|
|
let blob;
|
|
|
|
let cryptoRandom;
|
|
|
|
|
|
|
|
function lazyBlob() {
|
|
|
|
blob ??= require('internal/blob');
|
|
|
|
return blob;
|
|
|
|
}
|
|
|
|
|
|
|
|
function lazyCryptoRandom() {
|
|
|
|
try {
|
|
|
|
cryptoRandom ??= require('internal/crypto/random');
|
|
|
|
} catch {
|
|
|
|
// If Node.js built without crypto support, we'll fall
|
|
|
|
// through here and handle it later.
|
|
|
|
}
|
|
|
|
return cryptoRandom;
|
|
|
|
}
|
|
|
|
|
2017-03-22 18:39:13 +00:00
|
|
|
// This class provides the internal state of a URL object. An instance of this
|
|
|
|
// class is stored in every URL object and is accessed internally by setters
|
|
|
|
// and getters. It roughly corresponds to the concept of a URL record in the
|
|
|
|
// URL Standard, with a few differences. It is also the object transported to
|
|
|
|
// the C++ binding.
|
|
|
|
// Refs: https://url.spec.whatwg.org/#concept-url
|
|
|
|
class URLContext {
|
2023-02-06 13:30:55 +00:00
|
|
|
href = '';
|
|
|
|
origin = '';
|
|
|
|
protocol = '';
|
|
|
|
hostname = '';
|
|
|
|
pathname = '';
|
|
|
|
search = '';
|
|
|
|
username = '';
|
|
|
|
password = '';
|
|
|
|
port = '';
|
|
|
|
hash = '';
|
2017-03-22 18:39:13 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 09:34:51 +00:00
|
|
|
function isURLSearchParams(self) {
|
|
|
|
return self && self[searchParams] && !self[searchParams][searchParams];
|
|
|
|
}
|
|
|
|
|
2017-07-01 18:51:22 +00:00
|
|
|
class URLSearchParams {
|
|
|
|
// URL Standard says the default value is '', but as undefined and '' have
|
|
|
|
// the same result, undefined is used to prevent unnecessary parsing.
|
|
|
|
// Default parameter is necessary to keep URLSearchParams.length === 0 in
|
|
|
|
// accordance with Web IDL spec.
|
|
|
|
constructor(init = undefined) {
|
|
|
|
if (init === null || init === undefined) {
|
|
|
|
this[searchParams] = [];
|
2018-02-10 16:38:45 +00:00
|
|
|
} else if (typeof init === 'object' || typeof init === 'function') {
|
2019-12-08 21:35:19 +00:00
|
|
|
const method = init[SymbolIterator];
|
|
|
|
if (method === this[SymbolIterator]) {
|
2017-07-01 18:51:22 +00:00
|
|
|
// While the spec does not have this branch, we can use it as a
|
|
|
|
// shortcut to avoid having to go through the costly generic iterator.
|
|
|
|
const childParams = init[searchParams];
|
|
|
|
this[searchParams] = childParams.slice();
|
|
|
|
} else if (method !== null && method !== undefined) {
|
|
|
|
if (typeof method !== 'function') {
|
2018-03-04 21:16:24 +00:00
|
|
|
throw new ERR_ARG_NOT_ITERABLE('Query pairs');
|
2017-07-01 18:51:22 +00:00
|
|
|
}
|
|
|
|
|
2019-03-22 02:44:26 +00:00
|
|
|
// Sequence<sequence<USVString>>
|
2017-07-01 18:51:22 +00:00
|
|
|
// Note: per spec we have to first exhaust the lists then process them
|
|
|
|
const pairs = [];
|
|
|
|
for (const pair of init) {
|
|
|
|
if ((typeof pair !== 'object' && typeof pair !== 'function') ||
|
|
|
|
pair === null ||
|
2019-12-08 21:35:19 +00:00
|
|
|
typeof pair[SymbolIterator] !== 'function') {
|
2018-03-04 21:16:24 +00:00
|
|
|
throw new ERR_INVALID_TUPLE('Each query pair', '[name, value]');
|
2017-07-01 18:51:22 +00:00
|
|
|
}
|
|
|
|
const convertedPair = [];
|
|
|
|
for (const element of pair)
|
2020-11-20 09:20:15 +00:00
|
|
|
ArrayPrototypePush(convertedPair, toUSVString(element));
|
|
|
|
ArrayPrototypePush(pairs, convertedPair);
|
2017-07-01 18:51:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this[searchParams] = [];
|
|
|
|
for (const pair of pairs) {
|
|
|
|
if (pair.length !== 2) {
|
2018-03-04 21:16:24 +00:00
|
|
|
throw new ERR_INVALID_TUPLE('Each query pair', '[name, value]');
|
2017-07-01 18:51:22 +00:00
|
|
|
}
|
2020-11-20 09:20:15 +00:00
|
|
|
ArrayPrototypePush(this[searchParams], pair[0], pair[1]);
|
2017-07-01 18:51:22 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-03-22 02:44:26 +00:00
|
|
|
// Record<USVString, USVString>
|
2017-07-01 18:51:22 +00:00
|
|
|
// Need to use reflection APIs for full spec compliance.
|
2021-08-30 18:07:39 +00:00
|
|
|
const visited = {};
|
2017-07-01 18:51:22 +00:00
|
|
|
this[searchParams] = [];
|
2019-11-22 17:04:46 +00:00
|
|
|
const keys = ReflectOwnKeys(init);
|
2019-11-12 16:20:02 +00:00
|
|
|
for (let i = 0; i < keys.length; i++) {
|
2017-07-01 18:51:22 +00:00
|
|
|
const key = keys[i];
|
2019-11-22 17:04:46 +00:00
|
|
|
const desc = ReflectGetOwnPropertyDescriptor(init, key);
|
2017-07-01 18:51:22 +00:00
|
|
|
if (desc !== undefined && desc.enumerable) {
|
|
|
|
const typedKey = toUSVString(key);
|
|
|
|
const typedValue = toUSVString(init[key]);
|
2021-08-30 18:07:39 +00:00
|
|
|
|
|
|
|
// Two different key may result same after `toUSVString()`, we only
|
|
|
|
// leave the later one. Refers to WPT.
|
|
|
|
if (visited[typedKey] !== undefined) {
|
|
|
|
this[searchParams][visited[typedKey]] = typedValue;
|
|
|
|
} else {
|
|
|
|
visited[typedKey] = ArrayPrototypePush(this[searchParams],
|
|
|
|
typedKey,
|
|
|
|
typedValue) - 1;
|
|
|
|
}
|
2017-07-01 18:51:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// USVString
|
|
|
|
init = toUSVString(init);
|
2023-02-27 14:31:18 +00:00
|
|
|
this[searchParams] = init ? parseParams(init) : [];
|
2017-07-01 18:51:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// "associated url object"
|
|
|
|
this[context] = null;
|
|
|
|
}
|
|
|
|
|
2019-03-20 12:34:03 +00:00
|
|
|
[inspect.custom](recurseTimes, ctx) {
|
2021-08-04 09:34:51 +00:00
|
|
|
if (!isURLSearchParams(this))
|
2018-03-04 21:16:24 +00:00
|
|
|
throw new ERR_INVALID_THIS('URLSearchParams');
|
2017-07-01 18:51:22 +00:00
|
|
|
|
|
|
|
if (typeof recurseTimes === 'number' && recurseTimes < 0)
|
|
|
|
return ctx.stylize('[Object]', 'special');
|
|
|
|
|
2019-03-26 04:21:27 +00:00
|
|
|
const separator = ', ';
|
|
|
|
const innerOpts = { ...ctx };
|
2017-07-01 18:51:22 +00:00
|
|
|
if (recurseTimes !== null) {
|
|
|
|
innerOpts.depth = recurseTimes - 1;
|
|
|
|
}
|
2019-03-26 04:21:27 +00:00
|
|
|
const innerInspect = (v) => inspect(v, innerOpts);
|
2017-07-01 18:51:22 +00:00
|
|
|
|
2019-03-26 04:21:27 +00:00
|
|
|
const list = this[searchParams];
|
|
|
|
const output = [];
|
2019-11-12 16:20:02 +00:00
|
|
|
for (let i = 0; i < list.length; i += 2)
|
2020-11-20 09:20:15 +00:00
|
|
|
ArrayPrototypePush(
|
|
|
|
output,
|
|
|
|
`${innerInspect(list[i])} => ${innerInspect(list[i + 1])}`);
|
2017-07-01 18:51:22 +00:00
|
|
|
|
2020-11-20 09:20:15 +00:00
|
|
|
const length = ArrayPrototypeReduce(
|
|
|
|
output,
|
2017-12-11 08:32:59 +00:00
|
|
|
(prev, cur) => prev + removeColors(cur).length + separator.length,
|
2023-02-14 17:45:16 +00:00
|
|
|
-separator.length,
|
2017-07-01 18:51:22 +00:00
|
|
|
);
|
|
|
|
if (length > ctx.breakLength) {
|
2020-11-20 09:20:15 +00:00
|
|
|
return `${this.constructor.name} {\n` +
|
|
|
|
` ${ArrayPrototypeJoin(output, ',\n ')} }`;
|
2017-07-01 18:51:22 +00:00
|
|
|
} else if (output.length) {
|
2020-11-20 09:20:15 +00:00
|
|
|
return `${this.constructor.name} { ` +
|
|
|
|
`${ArrayPrototypeJoin(output, separator)} }`;
|
2017-07-01 18:51:22 +00:00
|
|
|
}
|
2020-04-08 16:58:03 +00:00
|
|
|
return `${this.constructor.name} {}`;
|
2017-07-01 18:51:22 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 20:14:11 +00:00
|
|
|
get size() {
|
|
|
|
if (!isURLSearchParams(this))
|
|
|
|
throw new ERR_INVALID_THIS('URLSearchParams');
|
|
|
|
return this[searchParams].length / 2;
|
|
|
|
}
|
|
|
|
|
2021-01-06 08:20:00 +00:00
|
|
|
append(name, value) {
|
2021-08-04 09:34:51 +00:00
|
|
|
if (!isURLSearchParams(this))
|
2021-01-06 08:20:00 +00:00
|
|
|
throw new ERR_INVALID_THIS('URLSearchParams');
|
2021-08-04 09:34:51 +00:00
|
|
|
|
2021-01-06 08:20:00 +00:00
|
|
|
if (arguments.length < 2) {
|
|
|
|
throw new ERR_MISSING_ARGS('name', 'value');
|
|
|
|
}
|
|
|
|
|
|
|
|
name = toUSVString(name);
|
|
|
|
value = toUSVString(value);
|
|
|
|
ArrayPrototypePush(this[searchParams], name, value);
|
2023-02-24 22:35:05 +00:00
|
|
|
if (this[context]) {
|
|
|
|
this[context].search = this.toString();
|
|
|
|
}
|
2021-01-05 12:20:00 +00:00
|
|
|
}
|
2021-01-06 08:20:00 +00:00
|
|
|
|
|
|
|
delete(name) {
|
2021-08-04 09:34:51 +00:00
|
|
|
if (!isURLSearchParams(this))
|
2021-01-06 08:20:00 +00:00
|
|
|
throw new ERR_INVALID_THIS('URLSearchParams');
|
2021-08-04 09:34:51 +00:00
|
|
|
|
2021-01-06 08:20:00 +00:00
|
|
|
if (arguments.length < 1) {
|
|
|
|
throw new ERR_MISSING_ARGS('name');
|
|
|
|
}
|
|
|
|
|
|
|
|
const list = this[searchParams];
|
|
|
|
name = toUSVString(name);
|
|
|
|
for (let i = 0; i < list.length;) {
|
|
|
|
const cur = list[i];
|
|
|
|
if (cur === name) {
|
|
|
|
list.splice(i, 2);
|
|
|
|
} else {
|
|
|
|
i += 2;
|
|
|
|
}
|
|
|
|
}
|
2023-02-24 22:35:05 +00:00
|
|
|
if (this[context]) {
|
|
|
|
this[context].search = this.toString();
|
|
|
|
}
|
2021-01-05 12:20:00 +00:00
|
|
|
}
|
2021-01-06 08:20:00 +00:00
|
|
|
|
|
|
|
get(name) {
|
2021-08-04 09:34:51 +00:00
|
|
|
if (!isURLSearchParams(this))
|
2021-01-06 08:20:00 +00:00
|
|
|
throw new ERR_INVALID_THIS('URLSearchParams');
|
2021-08-04 09:34:51 +00:00
|
|
|
|
2021-01-06 08:20:00 +00:00
|
|
|
if (arguments.length < 1) {
|
|
|
|
throw new ERR_MISSING_ARGS('name');
|
|
|
|
}
|
|
|
|
|
|
|
|
const list = this[searchParams];
|
|
|
|
name = toUSVString(name);
|
|
|
|
for (let i = 0; i < list.length; i += 2) {
|
|
|
|
if (list[i] === name) {
|
|
|
|
return list[i + 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2021-01-05 12:20:00 +00:00
|
|
|
}
|
2021-01-06 08:20:00 +00:00
|
|
|
|
|
|
|
getAll(name) {
|
2021-08-04 09:34:51 +00:00
|
|
|
if (!isURLSearchParams(this))
|
2021-01-06 08:20:00 +00:00
|
|
|
throw new ERR_INVALID_THIS('URLSearchParams');
|
2021-08-04 09:34:51 +00:00
|
|
|
|
2021-01-06 08:20:00 +00:00
|
|
|
if (arguments.length < 1) {
|
|
|
|
throw new ERR_MISSING_ARGS('name');
|
|
|
|
}
|
|
|
|
|
|
|
|
const list = this[searchParams];
|
|
|
|
const values = [];
|
|
|
|
name = toUSVString(name);
|
|
|
|
for (let i = 0; i < list.length; i += 2) {
|
|
|
|
if (list[i] === name) {
|
|
|
|
values.push(list[i + 1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return values;
|
2021-01-05 12:20:00 +00:00
|
|
|
}
|
2021-01-06 08:20:00 +00:00
|
|
|
|
|
|
|
has(name) {
|
2021-08-04 09:34:51 +00:00
|
|
|
if (!isURLSearchParams(this))
|
2021-01-06 08:20:00 +00:00
|
|
|
throw new ERR_INVALID_THIS('URLSearchParams');
|
2021-08-04 09:34:51 +00:00
|
|
|
|
2021-01-06 08:20:00 +00:00
|
|
|
if (arguments.length < 1) {
|
|
|
|
throw new ERR_MISSING_ARGS('name');
|
|
|
|
}
|
|
|
|
|
|
|
|
const list = this[searchParams];
|
|
|
|
name = toUSVString(name);
|
|
|
|
for (let i = 0; i < list.length; i += 2) {
|
|
|
|
if (list[i] === name) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2021-01-05 12:20:00 +00:00
|
|
|
}
|
2021-01-06 08:20:00 +00:00
|
|
|
|
|
|
|
set(name, value) {
|
2021-08-04 09:34:51 +00:00
|
|
|
if (!isURLSearchParams(this))
|
2021-01-06 08:20:00 +00:00
|
|
|
throw new ERR_INVALID_THIS('URLSearchParams');
|
2021-08-04 09:34:51 +00:00
|
|
|
|
2021-01-06 08:20:00 +00:00
|
|
|
if (arguments.length < 2) {
|
|
|
|
throw new ERR_MISSING_ARGS('name', 'value');
|
|
|
|
}
|
|
|
|
|
|
|
|
const list = this[searchParams];
|
|
|
|
name = toUSVString(name);
|
|
|
|
value = toUSVString(value);
|
|
|
|
|
|
|
|
// If there are any name-value pairs whose name is `name`, in `list`, set
|
|
|
|
// the value of the first such name-value pair to `value` and remove the
|
|
|
|
// others.
|
|
|
|
let found = false;
|
|
|
|
for (let i = 0; i < list.length;) {
|
|
|
|
const cur = list[i];
|
|
|
|
if (cur === name) {
|
|
|
|
if (!found) {
|
|
|
|
list[i + 1] = value;
|
|
|
|
found = true;
|
|
|
|
i += 2;
|
|
|
|
} else {
|
|
|
|
list.splice(i, 2);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
i += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, append a new name-value pair whose name is `name` and value
|
|
|
|
// is `value`, to `list`.
|
|
|
|
if (!found) {
|
|
|
|
ArrayPrototypePush(list, name, value);
|
|
|
|
}
|
|
|
|
|
2023-02-24 22:35:05 +00:00
|
|
|
if (this[context]) {
|
|
|
|
this[context].search = this.toString();
|
|
|
|
}
|
2021-01-05 12:20:00 +00:00
|
|
|
}
|
2021-01-06 08:20:00 +00:00
|
|
|
|
|
|
|
sort() {
|
|
|
|
const a = this[searchParams];
|
|
|
|
const len = a.length;
|
|
|
|
|
|
|
|
if (len <= 2) {
|
|
|
|
// Nothing needs to be done.
|
|
|
|
} else if (len < 100) {
|
|
|
|
// 100 is found through testing.
|
|
|
|
// Simple stable in-place insertion sort
|
|
|
|
// Derived from v8/src/js/array.js
|
|
|
|
for (let i = 2; i < len; i += 2) {
|
|
|
|
const curKey = a[i];
|
|
|
|
const curVal = a[i + 1];
|
|
|
|
let j;
|
|
|
|
for (j = i - 2; j >= 0; j -= 2) {
|
|
|
|
if (a[j] > curKey) {
|
|
|
|
a[j + 2] = a[j];
|
|
|
|
a[j + 3] = a[j + 1];
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
a[j + 2] = curKey;
|
|
|
|
a[j + 3] = curVal;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Bottom-up iterative stable merge sort
|
|
|
|
const lBuffer = new Array(len);
|
|
|
|
const rBuffer = new Array(len);
|
|
|
|
for (let step = 2; step < len; step *= 2) {
|
|
|
|
for (let start = 0; start < len - 2; start += 2 * step) {
|
|
|
|
const mid = start + step;
|
|
|
|
let end = mid + step;
|
|
|
|
end = end < len ? end : len;
|
|
|
|
if (mid > end)
|
|
|
|
continue;
|
|
|
|
merge(a, start, mid, end, lBuffer, rBuffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-24 22:35:05 +00:00
|
|
|
if (this[context]) {
|
|
|
|
this[context].search = this.toString();
|
|
|
|
}
|
2021-01-05 12:20:00 +00:00
|
|
|
}
|
2021-01-06 08:20:00 +00:00
|
|
|
|
|
|
|
// https://heycam.github.io/webidl/#es-iterators
|
|
|
|
// Define entries here rather than [Symbol.iterator] as the function name
|
|
|
|
// must be set to `entries`.
|
|
|
|
entries() {
|
2021-08-04 09:34:51 +00:00
|
|
|
if (!isURLSearchParams(this))
|
2021-01-06 08:20:00 +00:00
|
|
|
throw new ERR_INVALID_THIS('URLSearchParams');
|
|
|
|
|
|
|
|
return createSearchParamsIterator(this, 'key+value');
|
2021-01-05 12:20:00 +00:00
|
|
|
}
|
2021-01-06 08:20:00 +00:00
|
|
|
|
|
|
|
forEach(callback, thisArg = undefined) {
|
2021-08-04 09:34:51 +00:00
|
|
|
if (!isURLSearchParams(this))
|
2021-01-06 08:20:00 +00:00
|
|
|
throw new ERR_INVALID_THIS('URLSearchParams');
|
2021-08-04 09:34:51 +00:00
|
|
|
|
2022-01-24 16:09:16 +00:00
|
|
|
validateFunction(callback, 'callback');
|
2021-01-06 08:20:00 +00:00
|
|
|
|
|
|
|
let list = this[searchParams];
|
|
|
|
|
|
|
|
let i = 0;
|
|
|
|
while (i < list.length) {
|
|
|
|
const key = list[i];
|
|
|
|
const value = list[i + 1];
|
|
|
|
callback.call(thisArg, value, key, this);
|
|
|
|
// In case the URL object's `search` is updated
|
|
|
|
list = this[searchParams];
|
|
|
|
i += 2;
|
|
|
|
}
|
2021-01-05 12:20:00 +00:00
|
|
|
}
|
2021-01-06 08:20:00 +00:00
|
|
|
|
|
|
|
// https://heycam.github.io/webidl/#es-iterable
|
|
|
|
keys() {
|
2021-08-04 09:34:51 +00:00
|
|
|
if (!isURLSearchParams(this))
|
2021-01-06 08:20:00 +00:00
|
|
|
throw new ERR_INVALID_THIS('URLSearchParams');
|
|
|
|
|
|
|
|
return createSearchParamsIterator(this, 'key');
|
2021-01-05 12:20:00 +00:00
|
|
|
}
|
2021-01-06 08:20:00 +00:00
|
|
|
|
|
|
|
values() {
|
2021-08-04 09:34:51 +00:00
|
|
|
if (!isURLSearchParams(this))
|
2021-01-06 08:20:00 +00:00
|
|
|
throw new ERR_INVALID_THIS('URLSearchParams');
|
|
|
|
|
|
|
|
return createSearchParamsIterator(this, 'value');
|
2021-01-05 12:20:00 +00:00
|
|
|
}
|
2021-01-06 08:20:00 +00:00
|
|
|
|
|
|
|
// https://heycam.github.io/webidl/#es-stringifier
|
|
|
|
// https://url.spec.whatwg.org/#urlsearchparams-stringification-behavior
|
|
|
|
toString() {
|
2021-08-04 09:34:51 +00:00
|
|
|
if (!isURLSearchParams(this))
|
2021-01-06 08:20:00 +00:00
|
|
|
throw new ERR_INVALID_THIS('URLSearchParams');
|
|
|
|
|
|
|
|
return serializeParams(this[searchParams]);
|
|
|
|
}
|
2021-01-05 12:20:00 +00:00
|
|
|
}
|
2021-01-06 08:20:00 +00:00
|
|
|
|
2021-01-05 12:20:00 +00:00
|
|
|
ObjectDefineProperties(URLSearchParams.prototype, {
|
2022-02-11 17:30:47 +00:00
|
|
|
append: kEnumerableProperty,
|
|
|
|
delete: kEnumerableProperty,
|
|
|
|
get: kEnumerableProperty,
|
|
|
|
getAll: kEnumerableProperty,
|
|
|
|
has: kEnumerableProperty,
|
|
|
|
set: kEnumerableProperty,
|
2023-01-22 20:14:11 +00:00
|
|
|
size: kEnumerableProperty,
|
2022-02-11 17:30:47 +00:00
|
|
|
sort: kEnumerableProperty,
|
|
|
|
entries: kEnumerableProperty,
|
|
|
|
forEach: kEnumerableProperty,
|
|
|
|
keys: kEnumerableProperty,
|
|
|
|
values: kEnumerableProperty,
|
|
|
|
toString: kEnumerableProperty,
|
2022-06-03 08:23:58 +00:00
|
|
|
[SymbolToStringTag]: { __proto__: null, configurable: true, value: 'URLSearchParams' },
|
2021-01-05 12:20:00 +00:00
|
|
|
|
|
|
|
// https://heycam.github.io/webidl/#es-iterable-entries
|
|
|
|
[SymbolIterator]: {
|
2022-06-03 08:23:58 +00:00
|
|
|
__proto__: null,
|
2021-01-05 12:20:00 +00:00
|
|
|
configurable: true,
|
|
|
|
writable: true,
|
|
|
|
value: URLSearchParams.prototype.entries,
|
|
|
|
},
|
2021-01-06 08:20:00 +00:00
|
|
|
});
|
|
|
|
|
2023-03-03 21:34:56 +00:00
|
|
|
/**
|
|
|
|
* Checks if a value has the shape of a WHATWG URL object.
|
|
|
|
*
|
|
|
|
* Using a symbol or instanceof would not be able to recognize URL objects
|
|
|
|
* coming from other implementations (e.g. in Electron), so instead we are
|
|
|
|
* checking some well known properties for a lack of a better test.
|
|
|
|
*
|
|
|
|
* @param {*} self
|
|
|
|
* @returns {self is URL}
|
|
|
|
*/
|
2023-02-27 14:31:18 +00:00
|
|
|
function isURL(self) {
|
2023-03-03 21:34:56 +00:00
|
|
|
return Boolean(self?.href && self.origin);
|
2021-08-12 22:45:54 +00:00
|
|
|
}
|
|
|
|
|
2016-05-31 18:52:19 +00:00
|
|
|
class URL {
|
2023-03-03 21:34:56 +00:00
|
|
|
#context = new URLContext();
|
|
|
|
#searchParams;
|
|
|
|
|
2021-08-23 09:58:47 +00:00
|
|
|
constructor(input, base = undefined) {
|
2017-02-04 05:53:27 +00:00
|
|
|
// toUSVString is not needed.
|
2017-03-08 03:31:29 +00:00
|
|
|
input = `${input}`;
|
2016-05-31 18:52:19 +00:00
|
|
|
|
2023-02-06 13:30:55 +00:00
|
|
|
if (base !== undefined) {
|
|
|
|
base = `${base}`;
|
|
|
|
}
|
2016-05-31 18:52:19 +00:00
|
|
|
|
2023-02-06 13:30:55 +00:00
|
|
|
const isValid = parse(input,
|
|
|
|
base,
|
|
|
|
this.#onParseComplete);
|
2016-05-31 18:52:19 +00:00
|
|
|
|
2023-02-06 13:30:55 +00:00
|
|
|
if (!isValid) {
|
|
|
|
throw new ERR_INVALID_URL(input);
|
|
|
|
}
|
2017-04-19 18:34:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 12:34:03 +00:00
|
|
|
[inspect.custom](depth, opts) {
|
2017-04-05 04:03:14 +00:00
|
|
|
if (typeof depth === 'number' && depth < 0)
|
2018-12-11 21:42:34 +00:00
|
|
|
return this;
|
2017-04-05 04:03:14 +00:00
|
|
|
|
2020-05-17 17:07:04 +00:00
|
|
|
const constructor = getConstructorOf(this) || URL;
|
2023-01-10 05:38:36 +00:00
|
|
|
const obj = { __proto__: { constructor } };
|
2017-04-05 04:03:14 +00:00
|
|
|
|
|
|
|
obj.href = this.href;
|
|
|
|
obj.origin = this.origin;
|
|
|
|
obj.protocol = this.protocol;
|
|
|
|
obj.username = this.username;
|
2017-04-15 04:42:25 +00:00
|
|
|
obj.password = this.password;
|
2017-04-05 04:03:14 +00:00
|
|
|
obj.host = this.host;
|
|
|
|
obj.hostname = this.hostname;
|
|
|
|
obj.port = this.port;
|
|
|
|
obj.pathname = this.pathname;
|
|
|
|
obj.search = this.search;
|
|
|
|
obj.searchParams = this.searchParams;
|
|
|
|
obj.hash = this.hash;
|
|
|
|
|
2016-05-31 18:52:19 +00:00
|
|
|
if (opts.showHidden) {
|
2023-03-03 21:34:56 +00:00
|
|
|
obj[context] = this.#context;
|
2016-05-31 18:52:19 +00:00
|
|
|
}
|
2017-04-05 04:03:14 +00:00
|
|
|
|
2020-05-17 17:07:04 +00:00
|
|
|
return `${constructor.name} ${inspect(obj, opts)}`;
|
2016-05-31 18:52:19 +00:00
|
|
|
}
|
|
|
|
|
2023-02-14 20:33:03 +00:00
|
|
|
#onParseComplete = (href, origin, protocol, hostname, pathname,
|
2023-02-24 22:35:05 +00:00
|
|
|
search, username, password, port, hash) => {
|
2023-03-03 21:34:56 +00:00
|
|
|
this.#context.href = href;
|
|
|
|
this.#context.origin = origin;
|
|
|
|
this.#context.protocol = protocol;
|
|
|
|
this.#context.hostname = hostname;
|
|
|
|
this.#context.pathname = pathname;
|
|
|
|
this.#context.search = search;
|
|
|
|
this.#context.username = username;
|
|
|
|
this.#context.password = password;
|
|
|
|
this.#context.port = port;
|
|
|
|
this.#context.hash = hash;
|
|
|
|
if (this.#searchParams) {
|
|
|
|
this.#searchParams[searchParams] = parseParams(search);
|
2016-12-22 09:16:08 +00:00
|
|
|
}
|
2023-02-06 13:30:55 +00:00
|
|
|
};
|
2021-01-05 12:20:00 +00:00
|
|
|
|
|
|
|
toString() {
|
2023-03-03 21:34:56 +00:00
|
|
|
return this.#context.href;
|
2021-01-05 12:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get href() {
|
2023-03-03 21:34:56 +00:00
|
|
|
return this.#context.href;
|
2021-01-05 12:20:00 +00:00
|
|
|
}
|
|
|
|
|
2023-02-06 13:30:55 +00:00
|
|
|
set href(value) {
|
2023-03-03 21:34:56 +00:00
|
|
|
const valid = updateUrl(this.#context.href, updateActions.kHref, `${value}`, this.#onParseComplete);
|
2023-02-06 13:30:55 +00:00
|
|
|
if (!valid) { throw ERR_INVALID_URL(`${value}`); }
|
2021-01-05 12:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// readonly
|
|
|
|
get origin() {
|
2023-03-03 21:34:56 +00:00
|
|
|
return this.#context.origin;
|
2021-01-05 12:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get protocol() {
|
2023-03-03 21:34:56 +00:00
|
|
|
return this.#context.protocol;
|
2021-01-05 12:20:00 +00:00
|
|
|
}
|
|
|
|
|
2023-02-06 13:30:55 +00:00
|
|
|
set protocol(value) {
|
2023-03-03 21:34:56 +00:00
|
|
|
updateUrl(this.#context.href, updateActions.kProtocol, `${value}`, this.#onParseComplete);
|
2021-01-05 12:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get username() {
|
2023-03-03 21:34:56 +00:00
|
|
|
return this.#context.username;
|
2021-01-05 12:20:00 +00:00
|
|
|
}
|
|
|
|
|
2023-02-06 13:30:55 +00:00
|
|
|
set username(value) {
|
2023-03-03 21:34:56 +00:00
|
|
|
updateUrl(this.#context.href, updateActions.kUsername, `${value}`, this.#onParseComplete);
|
2021-01-05 12:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get password() {
|
2023-03-03 21:34:56 +00:00
|
|
|
return this.#context.password;
|
2021-01-05 12:20:00 +00:00
|
|
|
}
|
|
|
|
|
2023-02-06 13:30:55 +00:00
|
|
|
set password(value) {
|
2023-03-03 21:34:56 +00:00
|
|
|
updateUrl(this.#context.href, updateActions.kPassword, `${value}`, this.#onParseComplete);
|
2021-01-05 12:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get host() {
|
2023-03-03 21:34:56 +00:00
|
|
|
const port = this.#context.port;
|
2023-02-14 20:33:03 +00:00
|
|
|
const suffix = port.length > 0 ? `:${port}` : '';
|
2023-03-03 21:34:56 +00:00
|
|
|
return this.#context.hostname + suffix;
|
2021-01-05 12:20:00 +00:00
|
|
|
}
|
|
|
|
|
2023-02-06 13:30:55 +00:00
|
|
|
set host(value) {
|
2023-03-03 21:34:56 +00:00
|
|
|
updateUrl(this.#context.href, updateActions.kHost, `${value}`, this.#onParseComplete);
|
2021-01-05 12:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get hostname() {
|
2023-03-03 21:34:56 +00:00
|
|
|
return this.#context.hostname;
|
2021-01-05 12:20:00 +00:00
|
|
|
}
|
|
|
|
|
2023-02-06 13:30:55 +00:00
|
|
|
set hostname(value) {
|
2023-03-03 21:34:56 +00:00
|
|
|
updateUrl(this.#context.href, updateActions.kHostname, `${value}`, this.#onParseComplete);
|
2021-01-05 12:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get port() {
|
2023-03-03 21:34:56 +00:00
|
|
|
return this.#context.port;
|
2021-01-05 12:20:00 +00:00
|
|
|
}
|
|
|
|
|
2023-02-06 13:30:55 +00:00
|
|
|
set port(value) {
|
2023-03-03 21:34:56 +00:00
|
|
|
updateUrl(this.#context.href, updateActions.kPort, `${value}`, this.#onParseComplete);
|
2021-01-05 12:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get pathname() {
|
2023-03-03 21:34:56 +00:00
|
|
|
return this.#context.pathname;
|
2021-01-05 12:20:00 +00:00
|
|
|
}
|
|
|
|
|
2023-02-06 13:30:55 +00:00
|
|
|
set pathname(value) {
|
2023-03-03 21:34:56 +00:00
|
|
|
updateUrl(this.#context.href, updateActions.kPathname, `${value}`, this.#onParseComplete);
|
2021-01-05 12:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get search() {
|
2023-03-03 21:34:56 +00:00
|
|
|
return this.#context.search;
|
2021-01-05 12:20:00 +00:00
|
|
|
}
|
|
|
|
|
2023-02-27 14:31:18 +00:00
|
|
|
set search(value) {
|
2023-03-03 21:34:56 +00:00
|
|
|
updateUrl(this.#context.href, updateActions.kSearch, toUSVString(value), this.#onParseComplete);
|
2021-01-05 12:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// readonly
|
|
|
|
get searchParams() {
|
2023-02-27 14:31:18 +00:00
|
|
|
// Create URLSearchParams on demand to greatly improve the URL performance.
|
2023-03-03 21:34:56 +00:00
|
|
|
if (this.#searchParams == null) {
|
|
|
|
this.#searchParams = new URLSearchParams(this.#context.search);
|
|
|
|
this.#searchParams[context] = this;
|
2023-02-27 14:31:18 +00:00
|
|
|
}
|
2023-03-03 21:34:56 +00:00
|
|
|
return this.#searchParams;
|
2021-01-05 12:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get hash() {
|
2023-03-03 21:34:56 +00:00
|
|
|
return this.#context.hash;
|
2016-12-22 09:16:08 +00:00
|
|
|
}
|
2021-01-05 12:20:00 +00:00
|
|
|
|
2023-02-06 13:30:55 +00:00
|
|
|
set hash(value) {
|
2023-03-03 21:34:56 +00:00
|
|
|
updateUrl(this.#context.href, updateActions.kHash, `${value}`, this.#onParseComplete);
|
2021-01-05 12:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
toJSON() {
|
2023-03-03 21:34:56 +00:00
|
|
|
return this.#context.href;
|
2021-01-05 12:20:00 +00:00
|
|
|
}
|
2021-08-07 02:26:37 +00:00
|
|
|
|
|
|
|
static createObjectURL(obj) {
|
|
|
|
const cryptoRandom = lazyCryptoRandom();
|
|
|
|
if (cryptoRandom === undefined)
|
|
|
|
throw new ERR_NO_CRYPTO();
|
|
|
|
|
|
|
|
const blob = lazyBlob();
|
|
|
|
if (!blob.isBlob(obj))
|
|
|
|
throw new ERR_INVALID_ARG_TYPE('obj', 'Blob', obj);
|
|
|
|
|
|
|
|
const id = cryptoRandom.randomUUID();
|
|
|
|
|
|
|
|
storeDataObject(id, obj[blob.kHandle], obj.size, obj.type);
|
|
|
|
|
|
|
|
return `blob:nodedata:${id}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
static revokeObjectURL(url) {
|
|
|
|
url = `${url}`;
|
|
|
|
try {
|
2023-02-06 13:30:55 +00:00
|
|
|
// TODO(@anonrig): Remove this try/catch by calling `parse` directly.
|
2021-08-07 02:26:37 +00:00
|
|
|
const parsed = new URL(url);
|
|
|
|
const split = StringPrototypeSplit(parsed.pathname, ':');
|
|
|
|
if (split.length === 2)
|
|
|
|
revokeDataObject(split[1]);
|
|
|
|
} catch {
|
|
|
|
// If there's an error, it's ignored.
|
|
|
|
}
|
|
|
|
}
|
2021-01-05 12:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ObjectDefineProperties(URL.prototype, {
|
2022-06-03 08:23:58 +00:00
|
|
|
[SymbolToStringTag]: { __proto__: null, configurable: true, value: 'URL' },
|
2022-02-11 17:30:47 +00:00
|
|
|
toString: kEnumerableProperty,
|
|
|
|
href: kEnumerableProperty,
|
|
|
|
origin: kEnumerableProperty,
|
|
|
|
protocol: kEnumerableProperty,
|
|
|
|
username: kEnumerableProperty,
|
|
|
|
password: kEnumerableProperty,
|
|
|
|
host: kEnumerableProperty,
|
|
|
|
hostname: kEnumerableProperty,
|
|
|
|
port: kEnumerableProperty,
|
|
|
|
pathname: kEnumerableProperty,
|
|
|
|
search: kEnumerableProperty,
|
|
|
|
searchParams: kEnumerableProperty,
|
|
|
|
hash: kEnumerableProperty,
|
|
|
|
toJSON: kEnumerableProperty,
|
2016-12-22 09:16:08 +00:00
|
|
|
});
|
|
|
|
|
2022-10-14 23:49:39 +00:00
|
|
|
ObjectDefineProperties(URL, {
|
|
|
|
createObjectURL: kEnumerableProperty,
|
|
|
|
revokeObjectURL: kEnumerableProperty,
|
|
|
|
});
|
|
|
|
|
2017-03-15 04:01:04 +00:00
|
|
|
// application/x-www-form-urlencoded parser
|
|
|
|
// Ref: https://url.spec.whatwg.org/#concept-urlencoded-parser
|
|
|
|
function parseParams(qs) {
|
|
|
|
const out = [];
|
2019-11-12 16:20:02 +00:00
|
|
|
let seenSep = false;
|
|
|
|
let buf = '';
|
|
|
|
let encoded = false;
|
|
|
|
let encodeCheck = 0;
|
2023-02-06 13:30:55 +00:00
|
|
|
let i = qs[0] === '?' ? 1 : 0;
|
|
|
|
let pairStart = i;
|
|
|
|
let lastPos = i;
|
|
|
|
for (; i < qs.length; ++i) {
|
2020-11-20 09:20:15 +00:00
|
|
|
const code = StringPrototypeCharCodeAt(qs, i);
|
2017-03-15 04:01:04 +00:00
|
|
|
|
|
|
|
// Try matching key/value pair separator
|
2018-03-12 15:59:31 +00:00
|
|
|
if (code === CHAR_AMPERSAND) {
|
2017-03-15 04:01:04 +00:00
|
|
|
if (pairStart === i) {
|
|
|
|
// We saw an empty substring between pair separators
|
|
|
|
lastPos = pairStart = i + 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lastPos < i)
|
|
|
|
buf += qs.slice(lastPos, i);
|
|
|
|
if (encoded)
|
|
|
|
buf = querystring.unescape(buf);
|
|
|
|
out.push(buf);
|
|
|
|
|
|
|
|
// If `buf` is the key, add an empty value.
|
|
|
|
if (!seenSep)
|
|
|
|
out.push('');
|
|
|
|
|
|
|
|
seenSep = false;
|
|
|
|
buf = '';
|
|
|
|
encoded = false;
|
|
|
|
encodeCheck = 0;
|
|
|
|
lastPos = pairStart = i + 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try matching key/value separator (e.g. '=') if we haven't already
|
2018-03-12 15:59:31 +00:00
|
|
|
if (!seenSep && code === CHAR_EQUAL) {
|
2017-03-15 04:01:04 +00:00
|
|
|
// Key/value separator match!
|
|
|
|
if (lastPos < i)
|
|
|
|
buf += qs.slice(lastPos, i);
|
|
|
|
if (encoded)
|
|
|
|
buf = querystring.unescape(buf);
|
|
|
|
out.push(buf);
|
|
|
|
|
|
|
|
seenSep = true;
|
|
|
|
buf = '';
|
|
|
|
encoded = false;
|
|
|
|
encodeCheck = 0;
|
|
|
|
lastPos = i + 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle + and percent decoding.
|
2018-03-12 15:59:31 +00:00
|
|
|
if (code === CHAR_PLUS) {
|
2017-03-15 04:01:04 +00:00
|
|
|
if (lastPos < i)
|
2020-11-20 09:20:15 +00:00
|
|
|
buf += StringPrototypeSlice(qs, lastPos, i);
|
2017-03-15 04:01:04 +00:00
|
|
|
buf += ' ';
|
|
|
|
lastPos = i + 1;
|
|
|
|
} else if (!encoded) {
|
|
|
|
// Try to match an (valid) encoded byte (once) to minimize unnecessary
|
|
|
|
// calls to string decoding functions
|
2018-03-12 15:59:31 +00:00
|
|
|
if (code === CHAR_PERCENT) {
|
2017-03-15 04:01:04 +00:00
|
|
|
encodeCheck = 1;
|
|
|
|
} else if (encodeCheck > 0) {
|
2018-12-12 18:05:17 +00:00
|
|
|
if (isHexTable[code] === 1) {
|
2018-05-06 21:08:42 +00:00
|
|
|
if (++encodeCheck === 3) {
|
2017-03-15 04:01:04 +00:00
|
|
|
encoded = true;
|
2018-05-06 21:08:42 +00:00
|
|
|
}
|
2017-03-15 04:01:04 +00:00
|
|
|
} else {
|
|
|
|
encodeCheck = 0;
|
|
|
|
}
|
|
|
|
}
|
2016-11-06 00:37:24 +00:00
|
|
|
}
|
2016-05-31 18:52:19 +00:00
|
|
|
}
|
2017-03-15 04:01:04 +00:00
|
|
|
|
|
|
|
// Deal with any leftover key or value data
|
|
|
|
|
|
|
|
// There is a trailing &. No more processing is needed.
|
|
|
|
if (pairStart === i)
|
|
|
|
return out;
|
|
|
|
|
|
|
|
if (lastPos < i)
|
2020-11-20 09:20:15 +00:00
|
|
|
buf += StringPrototypeSlice(qs, lastPos, i);
|
2017-03-15 04:01:04 +00:00
|
|
|
if (encoded)
|
|
|
|
buf = querystring.unescape(buf);
|
2020-11-20 09:20:15 +00:00
|
|
|
ArrayPrototypePush(out, buf);
|
2017-03-15 04:01:04 +00:00
|
|
|
|
|
|
|
// If `buf` is the key, add an empty value.
|
|
|
|
if (!seenSep)
|
2020-11-20 09:20:15 +00:00
|
|
|
ArrayPrototypePush(out, '');
|
2017-03-15 04:01:04 +00:00
|
|
|
|
|
|
|
return out;
|
2016-05-31 18:52:19 +00:00
|
|
|
}
|
|
|
|
|
2017-02-04 01:34:47 +00:00
|
|
|
// Adapted from querystring's implementation.
|
|
|
|
// Ref: https://url.spec.whatwg.org/#concept-urlencoded-byte-serializer
|
2020-07-05 00:47:39 +00:00
|
|
|
const noEscape = new Int8Array([
|
2018-03-25 14:27:38 +00:00
|
|
|
/*
|
|
|
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
|
|
|
|
*/
|
2017-02-04 01:34:47 +00:00
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x00 - 0x0F
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x10 - 0x1F
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, // 0x20 - 0x2F
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, // 0x30 - 0x3F
|
|
|
|
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x40 - 0x4F
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, // 0x50 - 0x5F
|
|
|
|
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x60 - 0x6F
|
2021-02-06 13:36:58 +00:00
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, // 0x70 - 0x7F
|
2020-07-05 00:47:39 +00:00
|
|
|
]);
|
2017-02-04 01:34:47 +00:00
|
|
|
|
|
|
|
// Special version of hexTable that uses `+` for U+0020 SPACE.
|
|
|
|
const paramHexTable = hexTable.slice();
|
|
|
|
paramHexTable[0x20] = '+';
|
|
|
|
|
|
|
|
// application/x-www-form-urlencoded serializer
|
|
|
|
// Ref: https://url.spec.whatwg.org/#concept-urlencoded-serializer
|
|
|
|
function serializeParams(array) {
|
|
|
|
const len = array.length;
|
|
|
|
if (len === 0)
|
|
|
|
return '';
|
|
|
|
|
2018-03-01 18:56:39 +00:00
|
|
|
const firstEncodedParam = encodeStr(array[0], noEscape, paramHexTable);
|
|
|
|
const firstEncodedValue = encodeStr(array[1], noEscape, paramHexTable);
|
2018-03-01 19:18:37 +00:00
|
|
|
let output = `${firstEncodedParam}=${firstEncodedValue}`;
|
2018-03-01 18:56:39 +00:00
|
|
|
|
2019-11-12 16:20:02 +00:00
|
|
|
for (let i = 2; i < len; i += 2) {
|
2018-03-01 18:56:39 +00:00
|
|
|
const encodedParam = encodeStr(array[i], noEscape, paramHexTable);
|
|
|
|
const encodedValue = encodeStr(array[i + 1], noEscape, paramHexTable);
|
|
|
|
output += `&${encodedParam}=${encodedValue}`;
|
|
|
|
}
|
|
|
|
|
2017-02-04 01:34:47 +00:00
|
|
|
return output;
|
2016-12-22 09:16:08 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 01:06:58 +00:00
|
|
|
// Mainly to mitigate func-name-matching ESLint rule
|
|
|
|
function defineIDLClass(proto, classStr, obj) {
|
|
|
|
// https://heycam.github.io/webidl/#dfn-class-string
|
2019-12-11 20:08:38 +00:00
|
|
|
ObjectDefineProperty(proto, SymbolToStringTag, {
|
2022-06-03 08:23:58 +00:00
|
|
|
__proto__: null,
|
2017-01-24 01:06:58 +00:00
|
|
|
writable: false,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true,
|
2023-02-26 10:34:02 +00:00
|
|
|
value: classStr,
|
2017-01-24 01:06:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// https://heycam.github.io/webidl/#es-operations
|
2019-11-22 17:04:46 +00:00
|
|
|
for (const key of ObjectKeys(obj)) {
|
|
|
|
ObjectDefineProperty(proto, key, {
|
2022-06-03 08:23:58 +00:00
|
|
|
__proto__: null,
|
2017-01-24 01:06:58 +00:00
|
|
|
writable: true,
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
2023-02-26 10:34:02 +00:00
|
|
|
value: obj[key],
|
2017-01-24 01:06:58 +00:00
|
|
|
});
|
|
|
|
}
|
2019-11-22 17:04:46 +00:00
|
|
|
for (const key of ObjectGetOwnPropertySymbols(obj)) {
|
|
|
|
ObjectDefineProperty(proto, key, {
|
2022-06-03 08:23:58 +00:00
|
|
|
__proto__: null,
|
2017-01-24 01:06:58 +00:00
|
|
|
writable: true,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true,
|
2023-02-26 10:34:02 +00:00
|
|
|
value: obj[key],
|
2017-01-24 01:06:58 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-28 21:59:13 +00:00
|
|
|
// for merge sort
|
|
|
|
function merge(out, start, mid, end, lBuffer, rBuffer) {
|
|
|
|
const sizeLeft = mid - start;
|
|
|
|
const sizeRight = end - mid;
|
2019-11-12 16:20:02 +00:00
|
|
|
let l, r, o;
|
2017-01-28 21:59:13 +00:00
|
|
|
|
|
|
|
for (l = 0; l < sizeLeft; l++)
|
|
|
|
lBuffer[l] = out[start + l];
|
|
|
|
for (r = 0; r < sizeRight; r++)
|
|
|
|
rBuffer[r] = out[mid + r];
|
|
|
|
|
|
|
|
l = 0;
|
|
|
|
r = 0;
|
|
|
|
o = start;
|
|
|
|
while (l < sizeLeft && r < sizeRight) {
|
|
|
|
if (lBuffer[l] <= rBuffer[r]) {
|
|
|
|
out[o++] = lBuffer[l++];
|
|
|
|
out[o++] = lBuffer[l++];
|
|
|
|
} else {
|
|
|
|
out[o++] = rBuffer[r++];
|
|
|
|
out[o++] = rBuffer[r++];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (l < sizeLeft)
|
|
|
|
out[o++] = lBuffer[l++];
|
|
|
|
while (r < sizeRight)
|
|
|
|
out[o++] = rBuffer[r++];
|
|
|
|
}
|
|
|
|
|
2016-11-06 00:37:24 +00:00
|
|
|
// https://heycam.github.io/webidl/#dfn-default-iterator-object
|
|
|
|
function createSearchParamsIterator(target, kind) {
|
2023-01-10 05:38:36 +00:00
|
|
|
const iterator = { __proto__: URLSearchParamsIteratorPrototype };
|
2016-11-06 00:37:24 +00:00
|
|
|
iterator[context] = {
|
|
|
|
target,
|
|
|
|
kind,
|
2023-02-26 10:34:02 +00:00
|
|
|
index: 0,
|
2016-11-06 00:37:24 +00:00
|
|
|
};
|
|
|
|
return iterator;
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://heycam.github.io/webidl/#dfn-iterator-prototype-object
|
2023-01-10 05:38:36 +00:00
|
|
|
const URLSearchParamsIteratorPrototype = { __proto__: IteratorPrototype };
|
2017-01-24 01:06:58 +00:00
|
|
|
|
2017-12-09 02:23:45 +00:00
|
|
|
defineIDLClass(URLSearchParamsIteratorPrototype, 'URLSearchParams Iterator', {
|
2016-11-06 00:37:24 +00:00
|
|
|
next() {
|
|
|
|
if (!this ||
|
2019-11-22 17:04:46 +00:00
|
|
|
ObjectGetPrototypeOf(this) !== URLSearchParamsIteratorPrototype) {
|
2018-03-04 21:16:24 +00:00
|
|
|
throw new ERR_INVALID_THIS('URLSearchParamsIterator');
|
2016-11-06 00:37:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const {
|
|
|
|
target,
|
|
|
|
kind,
|
2023-02-26 10:34:02 +00:00
|
|
|
index,
|
2016-11-06 00:37:24 +00:00
|
|
|
} = this[context];
|
2016-11-24 23:55:56 +00:00
|
|
|
const values = target[searchParams];
|
2016-11-06 00:37:24 +00:00
|
|
|
const len = values.length;
|
|
|
|
if (index >= len) {
|
|
|
|
return {
|
|
|
|
value: undefined,
|
2023-02-26 10:34:02 +00:00
|
|
|
done: true,
|
2016-11-06 00:37:24 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-11-24 23:55:56 +00:00
|
|
|
const name = values[index];
|
|
|
|
const value = values[index + 1];
|
|
|
|
this[context].index = index + 2;
|
2016-11-06 00:37:24 +00:00
|
|
|
|
|
|
|
let result;
|
|
|
|
if (kind === 'key') {
|
2016-11-24 23:55:56 +00:00
|
|
|
result = name;
|
2016-11-06 00:37:24 +00:00
|
|
|
} else if (kind === 'value') {
|
2016-11-24 23:55:56 +00:00
|
|
|
result = value;
|
2016-11-06 00:37:24 +00:00
|
|
|
} else {
|
2016-11-24 23:55:56 +00:00
|
|
|
result = [name, value];
|
2016-11-06 00:37:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
value: result,
|
2023-02-26 10:34:02 +00:00
|
|
|
done: false,
|
2016-11-06 00:37:24 +00:00
|
|
|
};
|
2016-11-24 23:55:56 +00:00
|
|
|
},
|
2019-03-20 12:34:03 +00:00
|
|
|
[inspect.custom](recurseTimes, ctx) {
|
2017-04-05 04:03:14 +00:00
|
|
|
if (this == null || this[context] == null || this[context].target == null)
|
2018-03-04 21:16:24 +00:00
|
|
|
throw new ERR_INVALID_THIS('URLSearchParamsIterator');
|
2017-04-05 04:03:14 +00:00
|
|
|
|
|
|
|
if (typeof recurseTimes === 'number' && recurseTimes < 0)
|
|
|
|
return ctx.stylize('[Object]', 'special');
|
|
|
|
|
2018-12-18 02:15:57 +00:00
|
|
|
const innerOpts = { ...ctx };
|
2016-11-24 23:55:56 +00:00
|
|
|
if (recurseTimes !== null) {
|
|
|
|
innerOpts.depth = recurseTimes - 1;
|
|
|
|
}
|
|
|
|
const {
|
|
|
|
target,
|
|
|
|
kind,
|
2023-02-26 10:34:02 +00:00
|
|
|
index,
|
2016-11-24 23:55:56 +00:00
|
|
|
} = this[context];
|
2020-11-20 09:20:15 +00:00
|
|
|
const output = ArrayPrototypeReduce(
|
|
|
|
ArrayPrototypeSlice(target[searchParams], index),
|
|
|
|
(prev, cur, i) => {
|
|
|
|
const key = i % 2 === 0;
|
|
|
|
if (kind === 'key' && key) {
|
|
|
|
ArrayPrototypePush(prev, cur);
|
|
|
|
} else if (kind === 'value' && !key) {
|
|
|
|
ArrayPrototypePush(prev, cur);
|
|
|
|
} else if (kind === 'key+value' && !key) {
|
|
|
|
ArrayPrototypePush(prev, [target[searchParams][index + i - 1], cur]);
|
|
|
|
}
|
|
|
|
return prev;
|
|
|
|
},
|
2023-02-14 17:45:16 +00:00
|
|
|
[],
|
2020-11-20 09:20:15 +00:00
|
|
|
);
|
2023-01-22 13:13:24 +00:00
|
|
|
const breakLn = StringPrototypeIncludes(inspect(output, innerOpts), '\n');
|
2020-11-20 09:20:15 +00:00
|
|
|
const outputStrs = ArrayPrototypeMap(output, (p) => inspect(p, innerOpts));
|
2016-11-24 23:55:56 +00:00
|
|
|
let outputStr;
|
|
|
|
if (breakLn) {
|
2020-11-20 09:20:15 +00:00
|
|
|
outputStr = `\n ${ArrayPrototypeJoin(outputStrs, ',\n ')}`;
|
2016-11-24 23:55:56 +00:00
|
|
|
} else {
|
2020-11-20 09:20:15 +00:00
|
|
|
outputStr = ` ${ArrayPrototypeJoin(outputStrs, ', ')}`;
|
2016-11-24 23:55:56 +00:00
|
|
|
}
|
2019-12-11 20:08:38 +00:00
|
|
|
return `${this[SymbolToStringTag]} {${outputStr} }`;
|
2023-02-26 10:34:02 +00:00
|
|
|
},
|
2016-11-06 00:37:24 +00:00
|
|
|
});
|
2016-05-31 18:52:19 +00:00
|
|
|
|
2016-12-28 23:55:00 +00:00
|
|
|
function domainToASCII(domain) {
|
2017-03-20 21:29:54 +00:00
|
|
|
if (arguments.length < 1)
|
2018-03-04 21:16:24 +00:00
|
|
|
throw new ERR_MISSING_ARGS('domain');
|
2017-03-20 21:29:54 +00:00
|
|
|
|
2017-02-04 05:53:27 +00:00
|
|
|
// toUSVString is not needed.
|
2017-04-28 03:14:26 +00:00
|
|
|
return _domainToASCII(`${domain}`);
|
2016-12-28 23:55:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function domainToUnicode(domain) {
|
2017-03-20 21:29:54 +00:00
|
|
|
if (arguments.length < 1)
|
2018-03-04 21:16:24 +00:00
|
|
|
throw new ERR_MISSING_ARGS('domain');
|
2017-03-20 21:29:54 +00:00
|
|
|
|
2017-02-04 05:53:27 +00:00
|
|
|
// toUSVString is not needed.
|
2017-04-28 03:14:26 +00:00
|
|
|
return _domainToUnicode(`${domain}`);
|
2016-12-28 23:55:00 +00:00
|
|
|
}
|
2016-05-31 18:52:19 +00:00
|
|
|
|
2017-01-05 18:25:46 +00:00
|
|
|
// Utility function that converts a URL object into an ordinary
|
|
|
|
// options object as expected by the http.request and https.request
|
|
|
|
// APIs.
|
2021-01-06 08:10:28 +00:00
|
|
|
function urlToHttpOptions(url) {
|
2019-03-26 04:21:27 +00:00
|
|
|
const options = {
|
2017-01-05 18:25:46 +00:00
|
|
|
protocol: url.protocol,
|
2023-02-27 14:31:18 +00:00
|
|
|
hostname: url.hostname && StringPrototypeStartsWith(url.hostname, '[') ?
|
2020-11-20 09:20:15 +00:00
|
|
|
StringPrototypeSlice(url.hostname, 1, -1) :
|
2018-04-01 06:52:10 +00:00
|
|
|
url.hostname,
|
2017-01-05 18:25:46 +00:00
|
|
|
hash: url.hash,
|
|
|
|
search: url.search,
|
|
|
|
pathname: url.pathname,
|
2019-02-21 01:47:25 +00:00
|
|
|
path: `${url.pathname || ''}${url.search || ''}`,
|
2023-02-26 10:34:02 +00:00
|
|
|
href: url.href,
|
2017-01-05 18:25:46 +00:00
|
|
|
};
|
|
|
|
if (url.port !== '') {
|
|
|
|
options.port = Number(url.port);
|
|
|
|
}
|
|
|
|
if (url.username || url.password) {
|
2021-07-08 15:43:26 +00:00
|
|
|
options.auth = `${decodeURIComponent(url.username)}:${decodeURIComponent(url.password)}`;
|
2017-01-05 18:25:46 +00:00
|
|
|
}
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2017-01-11 15:02:40 +00:00
|
|
|
function getPathFromURLWin32(url) {
|
2019-03-26 04:21:27 +00:00
|
|
|
const hostname = url.hostname;
|
2019-11-12 16:20:02 +00:00
|
|
|
let pathname = url.pathname;
|
|
|
|
for (let n = 0; n < pathname.length; n++) {
|
2017-01-11 15:02:40 +00:00
|
|
|
if (pathname[n] === '%') {
|
2023-01-22 13:13:24 +00:00
|
|
|
const third = StringPrototypeCodePointAt(pathname, n + 2) | 0x20;
|
2017-01-11 15:02:40 +00:00
|
|
|
if ((pathname[n + 1] === '2' && third === 102) || // 2f 2F /
|
|
|
|
(pathname[n + 1] === '5' && third === 99)) { // 5c 5C \
|
2018-03-04 21:16:24 +00:00
|
|
|
throw new ERR_INVALID_FILE_URL_PATH(
|
2023-02-14 17:45:16 +00:00
|
|
|
'must not include encoded \\ or / characters',
|
2018-03-04 21:16:24 +00:00
|
|
|
);
|
2017-01-11 15:02:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-22 13:13:24 +00:00
|
|
|
pathname = SideEffectFreeRegExpPrototypeSymbolReplace(FORWARD_SLASH, pathname, '\\');
|
2017-01-11 15:02:40 +00:00
|
|
|
pathname = decodeURIComponent(pathname);
|
|
|
|
if (hostname !== '') {
|
|
|
|
// If hostname is set, then we have a UNC path
|
|
|
|
// Pass the hostname through domainToUnicode just in case
|
|
|
|
// it is an IDN using punycode encoding. We do not need to worry
|
|
|
|
// about percent encoding because the URL parser will have
|
|
|
|
// already taken care of that for us. Note that this only
|
|
|
|
// causes IDNs with an appropriate `xn--` prefix to be decoded.
|
2018-12-29 20:57:39 +00:00
|
|
|
return `\\\\${domainToUnicode(hostname)}${pathname}`;
|
2017-01-11 15:02:40 +00:00
|
|
|
}
|
2020-04-08 16:58:03 +00:00
|
|
|
// Otherwise, it's a local path that requires a drive letter
|
2023-01-22 13:13:24 +00:00
|
|
|
const letter = StringPrototypeCodePointAt(pathname, 1) | 0x20;
|
|
|
|
const sep = StringPrototypeCharAt(pathname, 2);
|
2020-04-08 16:58:03 +00:00
|
|
|
if (letter < CHAR_LOWERCASE_A || letter > CHAR_LOWERCASE_Z || // a..z A..Z
|
|
|
|
(sep !== ':')) {
|
|
|
|
throw new ERR_INVALID_FILE_URL_PATH('must be absolute');
|
|
|
|
}
|
2023-01-22 13:13:24 +00:00
|
|
|
return StringPrototypeSlice(pathname, 1);
|
2017-01-11 15:02:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getPathFromURLPosix(url) {
|
|
|
|
if (url.hostname !== '') {
|
2018-03-04 21:16:24 +00:00
|
|
|
throw new ERR_INVALID_FILE_URL_HOST(platform);
|
2017-01-11 15:02:40 +00:00
|
|
|
}
|
2019-03-26 04:21:27 +00:00
|
|
|
const pathname = url.pathname;
|
2019-11-12 16:20:02 +00:00
|
|
|
for (let n = 0; n < pathname.length; n++) {
|
2017-01-11 15:02:40 +00:00
|
|
|
if (pathname[n] === '%') {
|
2023-01-22 13:13:24 +00:00
|
|
|
const third = StringPrototypeCodePointAt(pathname, n + 2) | 0x20;
|
2017-01-11 15:02:40 +00:00
|
|
|
if (pathname[n + 1] === '2' && third === 102) {
|
2018-03-04 21:16:24 +00:00
|
|
|
throw new ERR_INVALID_FILE_URL_PATH(
|
2023-02-14 17:45:16 +00:00
|
|
|
'must not include encoded / characters',
|
2018-03-04 21:16:24 +00:00
|
|
|
);
|
2017-01-11 15:02:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return decodeURIComponent(pathname);
|
|
|
|
}
|
|
|
|
|
2018-08-24 16:13:32 +00:00
|
|
|
function fileURLToPath(path) {
|
|
|
|
if (typeof path === 'string')
|
|
|
|
path = new URL(path);
|
2023-03-03 21:34:56 +00:00
|
|
|
else if (!isURL(path))
|
2018-08-24 16:13:32 +00:00
|
|
|
throw new ERR_INVALID_ARG_TYPE('path', ['string', 'URL'], path);
|
2017-01-11 15:02:40 +00:00
|
|
|
if (path.protocol !== 'file:')
|
2018-03-04 21:16:24 +00:00
|
|
|
throw new ERR_INVALID_URL_SCHEME('file');
|
2017-01-11 15:02:40 +00:00
|
|
|
return isWindows ? getPathFromURLWin32(path) : getPathFromURLPosix(path);
|
|
|
|
}
|
|
|
|
|
2018-10-17 20:05:37 +00:00
|
|
|
// The following characters are percent-encoded when converting from file path
|
|
|
|
// to URL:
|
|
|
|
// - %: The percent character is the only character not encoded by the
|
|
|
|
// `pathname` setter.
|
|
|
|
// - \: Backslash is encoded on non-windows platforms since it's a valid
|
|
|
|
// character but the `pathname` setters replaces it by a forward slash.
|
|
|
|
// - LF: The newline character is stripped out by the `pathname` setter.
|
|
|
|
// (See whatwg/url#419)
|
|
|
|
// - 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.
|
2017-10-10 18:44:00 +00:00
|
|
|
const percentRegEx = /%/g;
|
2018-08-24 16:13:32 +00:00
|
|
|
const backslashRegEx = /\\/g;
|
2018-10-17 20:05:37 +00:00
|
|
|
const newlineRegEx = /\n/g;
|
|
|
|
const carriageReturnRegEx = /\r/g;
|
|
|
|
const tabRegEx = /\t/g;
|
2020-08-12 01:12:47 +00:00
|
|
|
|
|
|
|
function encodePathChars(filepath) {
|
2020-11-20 09:20:15 +00:00
|
|
|
if (StringPrototypeIncludes(filepath, '%'))
|
2023-01-22 13:13:24 +00:00
|
|
|
filepath = RegExpPrototypeSymbolReplace(percentRegEx, filepath, '%25');
|
2020-08-12 01:12:47 +00:00
|
|
|
// In posix, backslash is a valid character in paths:
|
2020-11-20 09:20:15 +00:00
|
|
|
if (!isWindows && StringPrototypeIncludes(filepath, '\\'))
|
2023-01-22 13:13:24 +00:00
|
|
|
filepath = RegExpPrototypeSymbolReplace(backslashRegEx, filepath, '%5C');
|
2020-11-20 09:20:15 +00:00
|
|
|
if (StringPrototypeIncludes(filepath, '\n'))
|
2023-01-22 13:13:24 +00:00
|
|
|
filepath = RegExpPrototypeSymbolReplace(newlineRegEx, filepath, '%0A');
|
2020-11-20 09:20:15 +00:00
|
|
|
if (StringPrototypeIncludes(filepath, '\r'))
|
2023-01-22 13:13:24 +00:00
|
|
|
filepath = RegExpPrototypeSymbolReplace(carriageReturnRegEx, filepath, '%0D');
|
2020-11-20 09:20:15 +00:00
|
|
|
if (StringPrototypeIncludes(filepath, '\t'))
|
2023-01-22 13:13:24 +00:00
|
|
|
filepath = RegExpPrototypeSymbolReplace(tabRegEx, filepath, '%09');
|
2020-08-12 01:12:47 +00:00
|
|
|
return filepath;
|
|
|
|
}
|
|
|
|
|
2018-08-24 16:13:32 +00:00
|
|
|
function pathToFileURL(filepath) {
|
|
|
|
const outURL = new URL('file://');
|
2020-11-20 09:20:15 +00:00
|
|
|
if (isWindows && StringPrototypeStartsWith(filepath, '\\\\')) {
|
2020-08-12 01:12:47 +00:00
|
|
|
// UNC path format: \\server\share\resource
|
2023-01-22 13:13:24 +00:00
|
|
|
const hostnameEndIndex = StringPrototypeIndexOf(filepath, '\\', 2);
|
|
|
|
if (hostnameEndIndex === -1) {
|
2020-08-12 01:12:47 +00:00
|
|
|
throw new ERR_INVALID_ARG_VALUE(
|
|
|
|
'filepath',
|
|
|
|
filepath,
|
2023-02-14 17:45:16 +00:00
|
|
|
'Missing UNC resource path',
|
2020-08-12 01:12:47 +00:00
|
|
|
);
|
|
|
|
}
|
2023-01-22 13:13:24 +00:00
|
|
|
if (hostnameEndIndex === 2) {
|
2020-08-12 01:12:47 +00:00
|
|
|
throw new ERR_INVALID_ARG_VALUE(
|
|
|
|
'filepath',
|
|
|
|
filepath,
|
2023-02-14 17:45:16 +00:00
|
|
|
'Empty UNC servername',
|
2020-08-12 01:12:47 +00:00
|
|
|
);
|
|
|
|
}
|
2023-01-22 13:13:24 +00:00
|
|
|
const hostname = StringPrototypeSlice(filepath, 2, hostnameEndIndex);
|
2020-08-12 01:12:47 +00:00
|
|
|
outURL.hostname = domainToASCII(hostname);
|
2020-11-20 09:20:15 +00:00
|
|
|
outURL.pathname = encodePathChars(
|
2023-01-22 13:13:24 +00:00
|
|
|
RegExpPrototypeSymbolReplace(backslashRegEx, StringPrototypeSlice(filepath, hostnameEndIndex), '/'));
|
2020-08-12 01:12:47 +00:00
|
|
|
} else {
|
|
|
|
let resolved = path.resolve(filepath);
|
|
|
|
// path.resolve strips trailing slashes so we must add them back
|
2020-11-20 09:20:15 +00:00
|
|
|
const filePathLast = StringPrototypeCharCodeAt(filepath,
|
|
|
|
filepath.length - 1);
|
2020-08-12 01:12:47 +00:00
|
|
|
if ((filePathLast === CHAR_FORWARD_SLASH ||
|
|
|
|
(isWindows && filePathLast === CHAR_BACKWARD_SLASH)) &&
|
|
|
|
resolved[resolved.length - 1] !== path.sep)
|
|
|
|
resolved += '/';
|
|
|
|
outURL.pathname = encodePathChars(resolved);
|
|
|
|
}
|
2018-08-24 16:13:32 +00:00
|
|
|
return outURL;
|
|
|
|
}
|
|
|
|
|
|
|
|
function toPathIfFileURL(fileURLOrPath) {
|
2023-03-03 21:34:56 +00:00
|
|
|
if (!isURL(fileURLOrPath))
|
2018-08-24 16:13:32 +00:00
|
|
|
return fileURLOrPath;
|
|
|
|
return fileURLToPath(fileURLOrPath);
|
2017-06-06 00:44:56 +00:00
|
|
|
}
|
|
|
|
|
2017-02-04 05:53:27 +00:00
|
|
|
module.exports = {
|
|
|
|
toUSVString,
|
2018-08-24 16:13:32 +00:00
|
|
|
fileURLToPath,
|
|
|
|
pathToFileURL,
|
|
|
|
toPathIfFileURL,
|
2017-02-04 05:53:27 +00:00
|
|
|
URL,
|
|
|
|
URLSearchParams,
|
|
|
|
domainToASCII,
|
|
|
|
domainToUnicode,
|
2021-01-06 08:10:28 +00:00
|
|
|
urlToHttpOptions,
|
2023-02-26 10:34:02 +00:00
|
|
|
encodeStr,
|
2023-02-27 14:31:18 +00:00
|
|
|
isURL,
|
2017-02-04 05:53:27 +00:00
|
|
|
};
|