|
|
@ -6,6 +6,7 @@ const { |
|
|
|
isHexTable |
|
|
|
} = require('internal/querystring'); |
|
|
|
const { getConstructorOf } = require('internal/util'); |
|
|
|
const errors = require('internal/errors'); |
|
|
|
const binding = process.binding('url'); |
|
|
|
const context = Symbol('context'); |
|
|
|
const cannotBeBase = Symbol('cannot-be-base'); |
|
|
@ -14,9 +15,9 @@ const cannotHaveUsernamePasswordPort = |
|
|
|
const special = Symbol('special'); |
|
|
|
const searchParams = Symbol('query'); |
|
|
|
const querystring = require('querystring'); |
|
|
|
const os = require('os'); |
|
|
|
|
|
|
|
const isWindows = process.platform === 'win32'; |
|
|
|
const { platform } = process; |
|
|
|
const isWindows = platform === 'win32'; |
|
|
|
|
|
|
|
const kFormat = Symbol('format'); |
|
|
|
|
|
|
@ -88,7 +89,7 @@ function onParseComplete(flags, protocol, username, password, |
|
|
|
} |
|
|
|
|
|
|
|
function onParseError(flags, input) { |
|
|
|
const error = new TypeError('Invalid URL: ' + input); |
|
|
|
const error = new errors.TypeError('ERR_INVALID_URL', input); |
|
|
|
error.input = input; |
|
|
|
throw error; |
|
|
|
} |
|
|
@ -202,7 +203,7 @@ class URL { |
|
|
|
[util.inspect.custom](depth, opts) { |
|
|
|
if (this == null || |
|
|
|
Object.getPrototypeOf(this[context]) !== URLContext.prototype) { |
|
|
|
throw new TypeError('Value of `this` is not a URL'); |
|
|
|
throw new errors.TypeError('ERR_INVALID_THIS', 'URL'); |
|
|
|
} |
|
|
|
|
|
|
|
const ctx = this[context]; |
|
|
@ -247,7 +248,7 @@ Object.defineProperties(URL.prototype, { |
|
|
|
// eslint-disable-next-line func-name-matching
|
|
|
|
value: function format(options) { |
|
|
|
if (options && typeof options !== 'object') |
|
|
|
throw new TypeError('options must be an object'); |
|
|
|
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'object'); |
|
|
|
options = Object.assign({ |
|
|
|
fragment: true, |
|
|
|
unicode: false, |
|
|
@ -810,7 +811,7 @@ class URLSearchParams { |
|
|
|
this[searchParams] = childParams.slice(); |
|
|
|
} else if (method !== null && method !== undefined) { |
|
|
|
if (typeof method !== 'function') { |
|
|
|
throw new TypeError('Query pairs must be iterable'); |
|
|
|
throw new errors.TypeError('ERR_ARG_NOT_ITERABLE', 'Query pairs'); |
|
|
|
} |
|
|
|
|
|
|
|
// sequence<sequence<USVString>>
|
|
|
@ -819,7 +820,8 @@ class URLSearchParams { |
|
|
|
for (const pair of init) { |
|
|
|
if (typeof pair !== 'object' || |
|
|
|
typeof pair[Symbol.iterator] !== 'function') { |
|
|
|
throw new TypeError('Each query pair must be iterable'); |
|
|
|
throw new errors.TypeError('ERR_INVALID_TUPLE', 'Each query pair', |
|
|
|
'[name, value]'); |
|
|
|
} |
|
|
|
pairs.push(Array.from(pair)); |
|
|
|
} |
|
|
@ -827,7 +829,8 @@ class URLSearchParams { |
|
|
|
this[searchParams] = []; |
|
|
|
for (const pair of pairs) { |
|
|
|
if (pair.length !== 2) { |
|
|
|
throw new TypeError('Each query pair must be a name/value tuple'); |
|
|
|
throw new errors.TypeError('ERR_INVALID_TUPLE', 'Each query pair', |
|
|
|
'[name, value]'); |
|
|
|
} |
|
|
|
const key = toUSVString(pair[0]); |
|
|
|
const value = toUSVString(pair[1]); |
|
|
@ -855,7 +858,7 @@ class URLSearchParams { |
|
|
|
|
|
|
|
[util.inspect.custom](recurseTimes, ctx) { |
|
|
|
if (!this || !this[searchParams] || this[searchParams][searchParams]) { |
|
|
|
throw new TypeError('Value of `this` is not a URLSearchParams'); |
|
|
|
throw new errors.TypeError('ERR_INVALID_THIS', 'URLSearchParams'); |
|
|
|
} |
|
|
|
|
|
|
|
if (typeof recurseTimes === 'number' && recurseTimes < 0) |
|
|
@ -920,10 +923,10 @@ function merge(out, start, mid, end, lBuffer, rBuffer) { |
|
|
|
defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', { |
|
|
|
append(name, value) { |
|
|
|
if (!this || !this[searchParams] || this[searchParams][searchParams]) { |
|
|
|
throw new TypeError('Value of `this` is not a URLSearchParams'); |
|
|
|
throw new errors.TypeError('ERR_INVALID_THIS', 'URLSearchParams'); |
|
|
|
} |
|
|
|
if (arguments.length < 2) { |
|
|
|
throw new TypeError('"name" and "value" arguments must be specified'); |
|
|
|
throw new errors.TypeError('ERR_MISSING_ARGS', 'name', 'value'); |
|
|
|
} |
|
|
|
|
|
|
|
name = toUSVString(name); |
|
|
@ -934,10 +937,10 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', { |
|
|
|
|
|
|
|
delete(name) { |
|
|
|
if (!this || !this[searchParams] || this[searchParams][searchParams]) { |
|
|
|
throw new TypeError('Value of `this` is not a URLSearchParams'); |
|
|
|
throw new errors.TypeError('ERR_INVALID_THIS', 'URLSearchParams'); |
|
|
|
} |
|
|
|
if (arguments.length < 1) { |
|
|
|
throw new TypeError('"name" argument must be specified'); |
|
|
|
throw new errors.TypeError('ERR_MISSING_ARGS', 'name'); |
|
|
|
} |
|
|
|
|
|
|
|
const list = this[searchParams]; |
|
|
@ -955,10 +958,10 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', { |
|
|
|
|
|
|
|
get(name) { |
|
|
|
if (!this || !this[searchParams] || this[searchParams][searchParams]) { |
|
|
|
throw new TypeError('Value of `this` is not a URLSearchParams'); |
|
|
|
throw new errors.TypeError('ERR_INVALID_THIS', 'URLSearchParams'); |
|
|
|
} |
|
|
|
if (arguments.length < 1) { |
|
|
|
throw new TypeError('"name" argument must be specified'); |
|
|
|
throw new errors.TypeError('ERR_MISSING_ARGS', 'name'); |
|
|
|
} |
|
|
|
|
|
|
|
const list = this[searchParams]; |
|
|
@ -973,10 +976,10 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', { |
|
|
|
|
|
|
|
getAll(name) { |
|
|
|
if (!this || !this[searchParams] || this[searchParams][searchParams]) { |
|
|
|
throw new TypeError('Value of `this` is not a URLSearchParams'); |
|
|
|
throw new errors.TypeError('ERR_INVALID_THIS', 'URLSearchParams'); |
|
|
|
} |
|
|
|
if (arguments.length < 1) { |
|
|
|
throw new TypeError('"name" argument must be specified'); |
|
|
|
throw new errors.TypeError('ERR_MISSING_ARGS', 'name'); |
|
|
|
} |
|
|
|
|
|
|
|
const list = this[searchParams]; |
|
|
@ -992,10 +995,10 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', { |
|
|
|
|
|
|
|
has(name) { |
|
|
|
if (!this || !this[searchParams] || this[searchParams][searchParams]) { |
|
|
|
throw new TypeError('Value of `this` is not a URLSearchParams'); |
|
|
|
throw new errors.TypeError('ERR_INVALID_THIS', 'URLSearchParams'); |
|
|
|
} |
|
|
|
if (arguments.length < 1) { |
|
|
|
throw new TypeError('"name" argument must be specified'); |
|
|
|
throw new errors.TypeError('ERR_MISSING_ARGS', 'name'); |
|
|
|
} |
|
|
|
|
|
|
|
const list = this[searchParams]; |
|
|
@ -1010,10 +1013,10 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', { |
|
|
|
|
|
|
|
set(name, value) { |
|
|
|
if (!this || !this[searchParams] || this[searchParams][searchParams]) { |
|
|
|
throw new TypeError('Value of `this` is not a URLSearchParams'); |
|
|
|
throw new errors.TypeError('ERR_INVALID_THIS', 'URLSearchParams'); |
|
|
|
} |
|
|
|
if (arguments.length < 2) { |
|
|
|
throw new TypeError('"name" and "value" arguments must be specified'); |
|
|
|
throw new errors.TypeError('ERR_MISSING_ARGS', 'name', 'value'); |
|
|
|
} |
|
|
|
|
|
|
|
const list = this[searchParams]; |
|
|
@ -1098,7 +1101,7 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', { |
|
|
|
// must be set to `entries`.
|
|
|
|
entries() { |
|
|
|
if (!this || !this[searchParams] || this[searchParams][searchParams]) { |
|
|
|
throw new TypeError('Value of `this` is not a URLSearchParams'); |
|
|
|
throw new errors.TypeError('ERR_INVALID_THIS', 'URLSearchParams'); |
|
|
|
} |
|
|
|
|
|
|
|
return createSearchParamsIterator(this, 'key+value'); |
|
|
@ -1106,10 +1109,10 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', { |
|
|
|
|
|
|
|
forEach(callback, thisArg = undefined) { |
|
|
|
if (!this || !this[searchParams] || this[searchParams][searchParams]) { |
|
|
|
throw new TypeError('Value of `this` is not a URLSearchParams'); |
|
|
|
throw new errors.TypeError('ERR_INVALID_THIS', 'URLSearchParams'); |
|
|
|
} |
|
|
|
if (typeof callback !== 'function') { |
|
|
|
throw new TypeError('"callback" argument must be a function'); |
|
|
|
throw new errors.TypeError('ERR_INVALID_CALLBACK'); |
|
|
|
} |
|
|
|
|
|
|
|
let list = this[searchParams]; |
|
|
@ -1128,7 +1131,7 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', { |
|
|
|
// https://heycam.github.io/webidl/#es-iterable
|
|
|
|
keys() { |
|
|
|
if (!this || !this[searchParams] || this[searchParams][searchParams]) { |
|
|
|
throw new TypeError('Value of `this` is not a URLSearchParams'); |
|
|
|
throw new errors.TypeError('ERR_INVALID_THIS', 'URLSearchParams'); |
|
|
|
} |
|
|
|
|
|
|
|
return createSearchParamsIterator(this, 'key'); |
|
|
@ -1136,7 +1139,7 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', { |
|
|
|
|
|
|
|
values() { |
|
|
|
if (!this || !this[searchParams] || this[searchParams][searchParams]) { |
|
|
|
throw new TypeError('Value of `this` is not a URLSearchParams'); |
|
|
|
throw new errors.TypeError('ERR_INVALID_THIS', 'URLSearchParams'); |
|
|
|
} |
|
|
|
|
|
|
|
return createSearchParamsIterator(this, 'value'); |
|
|
@ -1146,7 +1149,7 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', { |
|
|
|
// https://url.spec.whatwg.org/#urlsearchparams-stringification-behavior
|
|
|
|
toString() { |
|
|
|
if (!this || !this[searchParams] || this[searchParams][searchParams]) { |
|
|
|
throw new TypeError('Value of `this` is not a URLSearchParams'); |
|
|
|
throw new errors.TypeError('ERR_INVALID_THIS', 'URLSearchParams'); |
|
|
|
} |
|
|
|
|
|
|
|
return serializeParams(this[searchParams]); |
|
|
@ -1178,7 +1181,7 @@ defineIDLClass(URLSearchParamsIteratorPrototype, 'URLSearchParamsIterator', { |
|
|
|
next() { |
|
|
|
if (!this || |
|
|
|
Object.getPrototypeOf(this) !== URLSearchParamsIteratorPrototype) { |
|
|
|
throw new TypeError('Value of `this` is not a URLSearchParamsIterator'); |
|
|
|
throw new errors.TypeError('ERR_INVALID_THIS', 'URLSearchParamsIterator'); |
|
|
|
} |
|
|
|
|
|
|
|
const { |
|
|
@ -1215,7 +1218,7 @@ defineIDLClass(URLSearchParamsIteratorPrototype, 'URLSearchParamsIterator', { |
|
|
|
}, |
|
|
|
[util.inspect.custom](recurseTimes, ctx) { |
|
|
|
if (this == null || this[context] == null || this[context].target == null) |
|
|
|
throw new TypeError('Value of `this` is not a URLSearchParamsIterator'); |
|
|
|
throw new errors.TypeError('ERR_INVALID_THIS', 'URLSearchParamsIterator'); |
|
|
|
|
|
|
|
if (typeof recurseTimes === 'number' && recurseTimes < 0) |
|
|
|
return ctx.stylize('[Object]', 'special'); |
|
|
@ -1254,7 +1257,7 @@ defineIDLClass(URLSearchParamsIteratorPrototype, 'URLSearchParamsIterator', { |
|
|
|
|
|
|
|
function domainToASCII(domain) { |
|
|
|
if (arguments.length < 1) |
|
|
|
throw new TypeError('"domain" argument must be specified'); |
|
|
|
throw new errors.TypeError('ERR_MISSING_ARGS', 'domain'); |
|
|
|
|
|
|
|
// toUSVString is not needed.
|
|
|
|
return binding.domainToASCII(`${domain}`); |
|
|
@ -1262,7 +1265,7 @@ function domainToASCII(domain) { |
|
|
|
|
|
|
|
function domainToUnicode(domain) { |
|
|
|
if (arguments.length < 1) |
|
|
|
throw new TypeError('"domain" argument must be specified'); |
|
|
|
throw new errors.TypeError('ERR_MISSING_ARGS', 'domain'); |
|
|
|
|
|
|
|
// toUSVString is not needed.
|
|
|
|
return binding.domainToUnicode(`${domain}`); |
|
|
@ -1299,8 +1302,9 @@ function getPathFromURLWin32(url) { |
|
|
|
var third = pathname.codePointAt(n + 2) | 0x20; |
|
|
|
if ((pathname[n + 1] === '2' && third === 102) || // 2f 2F /
|
|
|
|
(pathname[n + 1] === '5' && third === 99)) { // 5c 5C \
|
|
|
|
return new TypeError( |
|
|
|
'Path must not include encoded \\ or / characters'); |
|
|
|
return new errors.TypeError( |
|
|
|
'ERR_INVALID_FILE_URL_PATH', |
|
|
|
'must not include encoded \\ or / characters'); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -1319,7 +1323,8 @@ function getPathFromURLWin32(url) { |
|
|
|
var sep = pathname[2]; |
|
|
|
if (letter < 97 || letter > 122 || // a..z A..Z
|
|
|
|
(sep !== ':')) { |
|
|
|
return new TypeError('File URLs must specify absolute paths'); |
|
|
|
return new errors.TypeError('ERR_INVALID_FILE_URL_PATH', |
|
|
|
'must be absolute'); |
|
|
|
} |
|
|
|
return pathname.slice(1); |
|
|
|
} |
|
|
@ -1327,16 +1332,16 @@ function getPathFromURLWin32(url) { |
|
|
|
|
|
|
|
function getPathFromURLPosix(url) { |
|
|
|
if (url.hostname !== '') { |
|
|
|
return new TypeError( |
|
|
|
`File URLs on ${os.platform()} must use hostname 'localhost'` + |
|
|
|
' or not specify any hostname'); |
|
|
|
return new errors.TypeError('ERR_INVALID_FILE_URL_HOST', |
|
|
|
`must be "localhost" or empty on ${platform}`); |
|
|
|
} |
|
|
|
var pathname = url.pathname; |
|
|
|
for (var n = 0; n < pathname.length; n++) { |
|
|
|
if (pathname[n] === '%') { |
|
|
|
var third = pathname.codePointAt(n + 2) | 0x20; |
|
|
|
if (pathname[n + 1] === '2' && third === 102) { |
|
|
|
return new TypeError('Path must not include encoded / characters'); |
|
|
|
return new errors.TypeError('ERR_INVALID_FILE_URL_PATH', |
|
|
|
'must not include encoded / characters'); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -1349,7 +1354,7 @@ function getPathFromURL(path) { |
|
|
|
return path; |
|
|
|
} |
|
|
|
if (path.protocol !== 'file:') |
|
|
|
return new TypeError('Only `file:` URLs are supported'); |
|
|
|
return new errors.TypeError('ERR_INVALID_URL_SCHEME', 'file'); |
|
|
|
return isWindows ? getPathFromURLWin32(path) : getPathFromURLPosix(path); |
|
|
|
} |
|
|
|
|
|
|
|