tldts-core 7.5.6 → 7.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.js +579 -141
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/src/extract-hostname.js +222 -92
- package/dist/cjs/src/extract-hostname.js.map +1 -1
- package/dist/cjs/src/factory.js +25 -1
- package/dist/cjs/src/factory.js.map +1 -1
- package/dist/cjs/src/ip-mapping.js +19 -0
- package/dist/cjs/src/ip-mapping.js.map +1 -0
- package/dist/cjs/src/is-ip.js +305 -44
- package/dist/cjs/src/is-ip.js.map +1 -1
- package/dist/cjs/src/is-valid.js +11 -4
- package/dist/cjs/src/is-valid.js.map +1 -1
- package/dist/cjs/src/options.js +2 -1
- package/dist/cjs/src/options.js.map +1 -1
- package/dist/cjs/tsconfig.tsbuildinfo +1 -1
- package/dist/es6/src/extract-hostname.js +221 -91
- package/dist/es6/src/extract-hostname.js.map +1 -1
- package/dist/es6/src/factory.js +26 -2
- package/dist/es6/src/factory.js.map +1 -1
- package/dist/es6/src/ip-mapping.js +16 -0
- package/dist/es6/src/ip-mapping.js.map +1 -0
- package/dist/es6/src/is-ip.js +305 -44
- package/dist/es6/src/is-ip.js.map +1 -1
- package/dist/es6/src/is-valid.js +11 -4
- package/dist/es6/src/is-valid.js.map +1 -1
- package/dist/es6/src/options.js +2 -1
- package/dist/es6/src/options.js.map +1 -1
- package/dist/es6/tsconfig.bundle.tsbuildinfo +1 -1
- package/dist/types/src/extract-hostname.d.ts +1 -0
- package/dist/types/src/ip-mapping.d.ts +4 -0
- package/dist/types/src/is-ip.d.ts +13 -4
- package/dist/types/src/options.d.ts +1 -1
- package/package.json +2 -2
- package/src/extract-hostname.ts +253 -91
- package/src/factory.ts +36 -4
- package/src/ip-mapping.ts +15 -0
- package/src/is-ip.ts +336 -48
- package/src/is-valid.ts +12 -4
- package/src/options.ts +3 -2
package/dist/cjs/index.js
CHANGED
|
@@ -92,12 +92,23 @@ function getDomainWithoutSuffix(domain, suffix) {
|
|
|
92
92
|
return domain.slice(0, -suffix.length - 1);
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
+
// `charCodeAt.call(url, i)`, not `url.charCodeAt(i)`: see `is-valid.ts`.
|
|
96
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
97
|
+
const charCodeAt$1 = String.prototype.charCodeAt;
|
|
95
98
|
/**
|
|
96
99
|
* Matches an ASCII tab (U+0009) or newline (U+000A / U+000D). The WHATWG URL
|
|
97
100
|
* parser strips these before parsing; we only allocate a cleaned copy (and
|
|
98
101
|
* re-parse) on the rare input that actually contains one.
|
|
99
102
|
*/
|
|
100
103
|
const CONTROL_CHARS = /[\t\n\r]/g;
|
|
104
|
+
const HAS_CONTROL_CHAR = /[\t\n\r]/;
|
|
105
|
+
/**
|
|
106
|
+
* Matches a forbidden domain code point
|
|
107
|
+
* (https://url.spec.whatwg.org/#forbidden-domain-code-point): a C0 control,
|
|
108
|
+
* space, '#', '%', '/', ':', '<', '>', '?', '@', '[', '\', ']', '^', '|' or DEL.
|
|
109
|
+
*/
|
|
110
|
+
// eslint-disable-next-line no-control-regex
|
|
111
|
+
const FORBIDDEN_DOMAIN_CHAR = /[\x00-\x20#%/:<>?@[\\\]^|\x7f]/;
|
|
101
112
|
// Set by `extractHostname` (a module-scope flag, read synchronously by
|
|
102
113
|
// `parseImpl` right after the call — same pattern as the reused RESULT object).
|
|
103
114
|
// `true` ONLY when extraction validated the returned host inline (a confirmed-
|
|
@@ -108,6 +119,10 @@ const CONTROL_CHARS = /[\t\n\r]/g;
|
|
|
108
119
|
// fast path can only ever SKIP a redundant scan for hosts already known valid,
|
|
109
120
|
// never accept an invalid one.
|
|
110
121
|
let extractedHostnameValidated = false;
|
|
122
|
+
// Set by `extractHostname`, like `extractedHostnameValidated`: `true` ONLY when
|
|
123
|
+
// the returned host was in brackets ("http://[::1]/"). `parseImpl` then checks
|
|
124
|
+
// that it is an IPv6 address, as browsers do.
|
|
125
|
+
let extractedBracketedHost = false;
|
|
111
126
|
/**
|
|
112
127
|
* True if char `code` is a valid hostname character. This is the per-char half
|
|
113
128
|
* of `is-valid.ts`'s `isValidAscii` (a-z, 0-9, > U+007F) PLUS three additions:
|
|
@@ -126,6 +141,68 @@ function isValidHostnameChar(code) {
|
|
|
126
141
|
code === 95 // '_'
|
|
127
142
|
);
|
|
128
143
|
}
|
|
144
|
+
/**
|
|
145
|
+
* No hostname in `url`, unless a tab or newline, which browsers remove before
|
|
146
|
+
* anything else, hides one ("sc:\t//host", "/\t/host"): `url` is then read
|
|
147
|
+
* again without them. Only reached on inputs without a host.
|
|
148
|
+
*/
|
|
149
|
+
function noHostname(url, urlIsValidHostname, validate) {
|
|
150
|
+
return HAS_CONTROL_CHAR.test(url)
|
|
151
|
+
? extractHostname(url.replace(CONTROL_CHARS, ''), urlIsValidHostname, validate)
|
|
152
|
+
: null;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* True if char `code` may follow the first letter of a scheme: an ASCII letter,
|
|
156
|
+
* a digit, '+', '-' or '.' (https://url.spec.whatwg.org/#scheme-state).
|
|
157
|
+
*/
|
|
158
|
+
function isSchemeChar(code) {
|
|
159
|
+
return (((code | 32) >= 97 && (code | 32) <= 122) || // [a-zA-Z] (fold letters only)
|
|
160
|
+
(code >= 48 && code <= 57) || // [0, 9]
|
|
161
|
+
code === 46 || // '.'
|
|
162
|
+
code === 45 || // '-'
|
|
163
|
+
code === 43 // '+'
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* `hostname` percent-decoded as browsers decode the host of a special URL,
|
|
168
|
+
* lower-cased and without trailing dots, or null where they find no valid host:
|
|
169
|
+
* bytes that are no UTF-8 ("%FF"), a '%' not followed by two hexadecimal digits
|
|
170
|
+
* ("%zz"), and characters forbidden in a domain once decoded ("%2F", "%3A",
|
|
171
|
+
* "%5B::1%5D"). https://url.spec.whatwg.org/#concept-host-parser
|
|
172
|
+
*/
|
|
173
|
+
function decodeHostname(hostname) {
|
|
174
|
+
let decoded;
|
|
175
|
+
try {
|
|
176
|
+
decoded = decodeURIComponent(hostname);
|
|
177
|
+
}
|
|
178
|
+
catch (_a) {
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
181
|
+
if (FORBIDDEN_DOMAIN_CHAR.test(decoded)) {
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
let end = decoded.length;
|
|
185
|
+
while (end > 1 && charCodeAt$1.call(decoded, end - 1) === 46 /* '.' */) {
|
|
186
|
+
end -= 1;
|
|
187
|
+
}
|
|
188
|
+
return decoded.slice(0, end).toLowerCase();
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* True if `url.slice(start, end)` is a scheme, as browsers read one: an ASCII
|
|
192
|
+
* letter, then scheme characters.
|
|
193
|
+
*/
|
|
194
|
+
function isScheme(url, start, end) {
|
|
195
|
+
const first = charCodeAt$1.call(url, start) | 32;
|
|
196
|
+
if (start === end || first < 97 || first > 122) {
|
|
197
|
+
return false;
|
|
198
|
+
}
|
|
199
|
+
for (let i = start + 1; i < end; i += 1) {
|
|
200
|
+
if (!isSchemeChar(charCodeAt$1.call(url, i))) {
|
|
201
|
+
return false;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return true;
|
|
205
|
+
}
|
|
129
206
|
/**
|
|
130
207
|
* Classify scheme `url.slice(schemeStart, colonIndex)` as a WHATWG special
|
|
131
208
|
* scheme without allocating a substring (case-insensitive via `| 32`).
|
|
@@ -137,13 +214,15 @@ function isValidHostnameChar(code) {
|
|
|
137
214
|
*/
|
|
138
215
|
function getSpecialScheme(url, schemeStart, colonIndex) {
|
|
139
216
|
const length = colonIndex - schemeStart;
|
|
140
|
-
const c0 =
|
|
217
|
+
const c0 = charCodeAt$1.call(url, schemeStart) | 32;
|
|
141
218
|
if (length === 2) {
|
|
142
|
-
return c0 === 119 && (
|
|
219
|
+
return c0 === 119 && (charCodeAt$1.call(url, schemeStart + 1) | 32) === 115
|
|
220
|
+
? 1
|
|
221
|
+
: 0; // ws
|
|
143
222
|
}
|
|
144
223
|
else if (length === 3) {
|
|
145
|
-
const c1 =
|
|
146
|
-
const c2 =
|
|
224
|
+
const c1 = charCodeAt$1.call(url, schemeStart + 1) | 32;
|
|
225
|
+
const c2 = charCodeAt$1.call(url, schemeStart + 2) | 32;
|
|
147
226
|
if (c0 === 119 && c1 === 115 && c2 === 115)
|
|
148
227
|
return 1; // wss
|
|
149
228
|
if (c0 === 102 && c1 === 116 && c2 === 112)
|
|
@@ -151,9 +230,9 @@ function getSpecialScheme(url, schemeStart, colonIndex) {
|
|
|
151
230
|
return 0;
|
|
152
231
|
}
|
|
153
232
|
else if (length === 4) {
|
|
154
|
-
const c1 =
|
|
155
|
-
const c2 =
|
|
156
|
-
const c3 =
|
|
233
|
+
const c1 = charCodeAt$1.call(url, schemeStart + 1) | 32;
|
|
234
|
+
const c2 = charCodeAt$1.call(url, schemeStart + 2) | 32;
|
|
235
|
+
const c3 = charCodeAt$1.call(url, schemeStart + 3) | 32;
|
|
157
236
|
if (c0 === 104 && c1 === 116 && c2 === 116 && c3 === 112)
|
|
158
237
|
return 1; // http
|
|
159
238
|
if (c0 === 102 && c1 === 105 && c2 === 108 && c3 === 101)
|
|
@@ -162,10 +241,10 @@ function getSpecialScheme(url, schemeStart, colonIndex) {
|
|
|
162
241
|
}
|
|
163
242
|
else if (length === 5) {
|
|
164
243
|
return c0 === 104 &&
|
|
165
|
-
(
|
|
166
|
-
(
|
|
167
|
-
(
|
|
168
|
-
(
|
|
244
|
+
(charCodeAt$1.call(url, schemeStart + 1) | 32) === 116 &&
|
|
245
|
+
(charCodeAt$1.call(url, schemeStart + 2) | 32) === 116 &&
|
|
246
|
+
(charCodeAt$1.call(url, schemeStart + 3) | 32) === 112 &&
|
|
247
|
+
(charCodeAt$1.call(url, schemeStart + 4) | 32) === 115
|
|
169
248
|
? 1
|
|
170
249
|
: 0; // https
|
|
171
250
|
}
|
|
@@ -190,46 +269,65 @@ function extractHostname(url, urlIsValidHostname, validate = false) {
|
|
|
190
269
|
let start = 0;
|
|
191
270
|
let end = url.length;
|
|
192
271
|
let hasUpper = false;
|
|
272
|
+
let failedValidation = false; // inline validation failed (always so with a '%')
|
|
193
273
|
let isSpecial = true; // '\' ends the host, unless non-special "scheme://"
|
|
194
274
|
extractedHostnameValidated = false;
|
|
275
|
+
extractedBracketedHost = false;
|
|
195
276
|
if (!urlIsValidHostname) {
|
|
196
277
|
// WHATWG step 1: trim leading/trailing C0 control or space (<= U+0020).
|
|
197
278
|
// Tab/newline elsewhere are handled lazily below.
|
|
198
|
-
while (start < url.length &&
|
|
279
|
+
while (start < url.length && charCodeAt$1.call(url, start) <= 32) {
|
|
199
280
|
start += 1;
|
|
200
281
|
}
|
|
201
282
|
// "data:"/"javascript:" never reach a host (inline payload; data URLs may be
|
|
202
283
|
// huge). `| 32` lowercases letters only; tab/newline is re-parsed below.
|
|
203
|
-
if ((
|
|
204
|
-
(
|
|
205
|
-
(
|
|
206
|
-
(
|
|
207
|
-
(
|
|
208
|
-
(
|
|
209
|
-
(
|
|
210
|
-
(
|
|
211
|
-
(
|
|
212
|
-
(
|
|
213
|
-
(
|
|
214
|
-
(
|
|
215
|
-
(
|
|
216
|
-
(
|
|
217
|
-
(
|
|
218
|
-
(
|
|
284
|
+
if ((charCodeAt$1.call(url, start + 4) === 58 /* ':' */ &&
|
|
285
|
+
(charCodeAt$1.call(url, start) | 32) === 100 /* d */ &&
|
|
286
|
+
(charCodeAt$1.call(url, start + 1) | 32) === 97 /* a */ &&
|
|
287
|
+
(charCodeAt$1.call(url, start + 2) | 32) === 116 /* t */ &&
|
|
288
|
+
(charCodeAt$1.call(url, start + 3) | 32) === 97) /* a */ ||
|
|
289
|
+
(charCodeAt$1.call(url, start + 10) === 58 /* ':' */ &&
|
|
290
|
+
(charCodeAt$1.call(url, start) | 32) === 106 /* j */ &&
|
|
291
|
+
(charCodeAt$1.call(url, start + 1) | 32) === 97 /* a */ &&
|
|
292
|
+
(charCodeAt$1.call(url, start + 2) | 32) === 118 /* v */ &&
|
|
293
|
+
(charCodeAt$1.call(url, start + 3) | 32) === 97 /* a */ &&
|
|
294
|
+
(charCodeAt$1.call(url, start + 4) | 32) === 115 /* s */ &&
|
|
295
|
+
(charCodeAt$1.call(url, start + 5) | 32) === 99 /* c */ &&
|
|
296
|
+
(charCodeAt$1.call(url, start + 6) | 32) === 114 /* r */ &&
|
|
297
|
+
(charCodeAt$1.call(url, start + 7) | 32) === 105 /* i */ &&
|
|
298
|
+
(charCodeAt$1.call(url, start + 8) | 32) === 112 /* p */ &&
|
|
299
|
+
(charCodeAt$1.call(url, start + 9) | 32) === 116) /* t */) {
|
|
219
300
|
return null;
|
|
220
301
|
}
|
|
221
|
-
while (end > start + 1 &&
|
|
302
|
+
while (end > start + 1 && charCodeAt$1.call(url, end - 1) <= 32) {
|
|
222
303
|
end -= 1;
|
|
223
304
|
}
|
|
224
|
-
if ((
|
|
225
|
-
|
|
226
|
-
(
|
|
227
|
-
|
|
228
|
-
// Scheme-relative reference ("//host/path").
|
|
305
|
+
if ((charCodeAt$1.call(url, start) === 47 /* '/' */ ||
|
|
306
|
+
charCodeAt$1.call(url, start) === 92) /* '\' */ &&
|
|
307
|
+
(charCodeAt$1.call(url, start + 1) === 47 /* '/' */ ||
|
|
308
|
+
charCodeAt$1.call(url, start + 1) === 92) /* '\' */) {
|
|
309
|
+
// Scheme-relative reference ("//host/path"). Relative to a web page, a
|
|
310
|
+
// special URL, browsers skip any further slashes ("///host").
|
|
229
311
|
start += 2;
|
|
312
|
+
while (charCodeAt$1.call(url, start) === 47 /* '/' */ ||
|
|
313
|
+
charCodeAt$1.call(url, start) === 92 /* '\' */) {
|
|
314
|
+
start += 1;
|
|
315
|
+
}
|
|
230
316
|
}
|
|
231
317
|
else {
|
|
232
|
-
|
|
318
|
+
// The scheme ends at the first ":/". Most URLs start with "http://" or
|
|
319
|
+
// "https://", so check for those before searching.
|
|
320
|
+
let indexOfProtocol = charCodeAt$1.call(url, start + 4) === 115 /* 's' */
|
|
321
|
+
? start + 5
|
|
322
|
+
: start + 4;
|
|
323
|
+
if (charCodeAt$1.call(url, start) !== 104 /* 'h' */ ||
|
|
324
|
+
charCodeAt$1.call(url, start + 1) !== 116 /* 't' */ ||
|
|
325
|
+
charCodeAt$1.call(url, start + 2) !== 116 /* 't' */ ||
|
|
326
|
+
charCodeAt$1.call(url, start + 3) !== 112 /* 'p' */ ||
|
|
327
|
+
charCodeAt$1.call(url, indexOfProtocol) !== 58 /* ':' */ ||
|
|
328
|
+
charCodeAt$1.call(url, indexOfProtocol + 1) !== 47 /* '/' */) {
|
|
329
|
+
indexOfProtocol = url.indexOf(':/', start);
|
|
330
|
+
}
|
|
233
331
|
if (indexOfProtocol !== -1) {
|
|
234
332
|
// "scheme://…". Classify the scheme, then position `start` at the host.
|
|
235
333
|
const special = getSpecialScheme(url, start, indexOfProtocol);
|
|
@@ -237,8 +335,8 @@ function extractHostname(url, urlIsValidHostname, validate = false) {
|
|
|
237
335
|
// Special scheme: skip the run of '/' and '\' after it
|
|
238
336
|
// (special-authority-(ignore-)slashes states; '\' acts as '/').
|
|
239
337
|
start = indexOfProtocol + 2;
|
|
240
|
-
while (
|
|
241
|
-
|
|
338
|
+
while (charCodeAt$1.call(url, start) === 47 /* '/' */ ||
|
|
339
|
+
charCodeAt$1.call(url, start) === 92 /* '\' */) {
|
|
242
340
|
start += 1;
|
|
243
341
|
}
|
|
244
342
|
}
|
|
@@ -247,43 +345,53 @@ function extractHostname(url, urlIsValidHostname, validate = false) {
|
|
|
247
345
|
// "file://h/x" => "h" but "file:///x" / "file:/x" => no host.
|
|
248
346
|
start = indexOfProtocol + 1;
|
|
249
347
|
let slashes = 0;
|
|
250
|
-
while ((
|
|
348
|
+
while ((charCodeAt$1.call(url, start) === 47 ||
|
|
349
|
+
charCodeAt$1.call(url, start) === 92) &&
|
|
251
350
|
slashes < 2) {
|
|
252
351
|
start += 1;
|
|
253
352
|
slashes += 1;
|
|
254
353
|
}
|
|
255
354
|
if (slashes < 2) {
|
|
256
|
-
return
|
|
355
|
+
return noHostname(url, urlIsValidHostname, validate);
|
|
257
356
|
}
|
|
258
357
|
}
|
|
358
|
+
else if (!isScheme(url, start, start + 1)) {
|
|
359
|
+
// No scheme: one starts with an ASCII letter ("1a://x" is no URL for
|
|
360
|
+
// browsers). Read the input as an authority, as below.
|
|
361
|
+
indexOfProtocol = -1;
|
|
362
|
+
}
|
|
259
363
|
else {
|
|
260
364
|
// Unknown scheme: validate the WHATWG scheme grammar [A-Za-z0-9+.-];
|
|
261
365
|
// a control char means it was split by a tab/newline (strip + re-parse).
|
|
262
366
|
for (let i = start; i < indexOfProtocol; i += 1) {
|
|
263
|
-
const code =
|
|
264
|
-
if (!(
|
|
265
|
-
(code >= 48 && code <= 57) || // [0, 9]
|
|
266
|
-
code === 46 || // '.'
|
|
267
|
-
code === 45 || // '-'
|
|
268
|
-
code === 43 // '+'
|
|
269
|
-
)) {
|
|
367
|
+
const code = charCodeAt$1.call(url, i);
|
|
368
|
+
if (!isSchemeChar(code)) {
|
|
270
369
|
if (code === 9 /* tab */ ||
|
|
271
370
|
code === 10 /* LF */ ||
|
|
272
371
|
code === 13 /* CR */) {
|
|
273
372
|
return extractHostname(url.replace(CONTROL_CHARS, ''), urlIsValidHostname, validate);
|
|
274
373
|
}
|
|
275
|
-
// A
|
|
276
|
-
//
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
374
|
+
// A scheme ending at a first ':' (other than a slash-less special
|
|
375
|
+
// one) makes this ':/' part of an opaque path ("foo:bar://x", no
|
|
376
|
+
// host), unless a '/', '\', '?' or '#' first puts it in a path,
|
|
377
|
+
// query or fragment ("a.com/r?u=https://x"). Otherwise no scheme
|
|
378
|
+
// ends here: this ':/' is an empty port, after a slash-less
|
|
379
|
+
// special scheme ("http:a.com:/x") or an authority
|
|
380
|
+
// ("user@host:/x"). Read the input as such below.
|
|
381
|
+
if (code === 58 /* ':' */ &&
|
|
382
|
+
getSpecialScheme(url, start, i) !== 1) {
|
|
383
|
+
for (let j = i; j < indexOfProtocol; j += 1) {
|
|
384
|
+
const c = charCodeAt$1.call(url, j);
|
|
385
|
+
if (c === 47 || c === 92 || c === 63 || c === 35) {
|
|
386
|
+
indexOfProtocol = -1;
|
|
387
|
+
break;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
if (indexOfProtocol !== -1) {
|
|
391
|
+
return noHostname(url, urlIsValidHostname, validate);
|
|
282
392
|
}
|
|
283
393
|
}
|
|
284
|
-
|
|
285
|
-
return null;
|
|
286
|
-
}
|
|
394
|
+
indexOfProtocol = -1;
|
|
287
395
|
break;
|
|
288
396
|
}
|
|
289
397
|
}
|
|
@@ -291,17 +399,18 @@ function extractHostname(url, urlIsValidHostname, validate = false) {
|
|
|
291
399
|
// A non-special scheme has an authority only after "//" (else it is
|
|
292
400
|
// an opaque path with no host). `indexOf(':/')` already gave the
|
|
293
401
|
// first '/'.
|
|
294
|
-
if (
|
|
402
|
+
if (charCodeAt$1.call(url, indexOfProtocol + 2) === 47 /* '/' */) {
|
|
295
403
|
isSpecial = false;
|
|
296
404
|
start = indexOfProtocol + 3;
|
|
297
405
|
}
|
|
298
406
|
else {
|
|
299
|
-
return
|
|
407
|
+
return noHostname(url, urlIsValidHostname, validate);
|
|
300
408
|
}
|
|
301
409
|
}
|
|
302
410
|
}
|
|
303
411
|
}
|
|
304
|
-
if (indexOfProtocol === -1 &&
|
|
412
|
+
if (indexOfProtocol === -1 &&
|
|
413
|
+
charCodeAt$1.call(url, start) !== 91 /* '[' */) {
|
|
305
414
|
// Cold path: no "scheme://" prefix, and not a bare IPv6 literal (whose
|
|
306
415
|
// first ':' would otherwise look like a scheme separator; "[…]" falls
|
|
307
416
|
// through to the ipv6 handling below). May be a bare host, a host:port,
|
|
@@ -309,7 +418,7 @@ function extractHostname(url, urlIsValidHostname, validate = false) {
|
|
|
309
418
|
// URI ("mailto:", "tel:", "urn:…").
|
|
310
419
|
let indexOfColon = -1;
|
|
311
420
|
for (let i = start; i < end; i += 1) {
|
|
312
|
-
const code =
|
|
421
|
+
const code = charCodeAt$1.call(url, i);
|
|
313
422
|
if (code === 9 || code === 10 || code === 13) {
|
|
314
423
|
return extractHostname(url.replace(CONTROL_CHARS, ''), urlIsValidHostname, validate);
|
|
315
424
|
}
|
|
@@ -324,9 +433,11 @@ function extractHostname(url, urlIsValidHostname, validate = false) {
|
|
|
324
433
|
if (indexOfColon !== -1) {
|
|
325
434
|
// An '@' before the next delimiter => the ':' is userinfo, not a
|
|
326
435
|
// scheme ("user:pass@host", "mailto:a@b"): keep the whole authority.
|
|
436
|
+
// Not after "file:", which has no userinfo and no host without "//".
|
|
437
|
+
const special = getSpecialScheme(url, start, indexOfColon);
|
|
327
438
|
let hasIdentifier = false;
|
|
328
|
-
for (let i = indexOfColon + 1; i < end; i += 1) {
|
|
329
|
-
const code =
|
|
439
|
+
for (let i = special === 2 ? end : indexOfColon + 1; i < end; i += 1) {
|
|
440
|
+
const code = charCodeAt$1.call(url, i);
|
|
330
441
|
if (code === 47 || code === 92 || code === 63 || code === 35) {
|
|
331
442
|
break;
|
|
332
443
|
}
|
|
@@ -338,10 +449,11 @@ function extractHostname(url, urlIsValidHostname, validate = false) {
|
|
|
338
449
|
if (!hasIdentifier) {
|
|
339
450
|
// All-digits after ':' => a bare "host:port" (tldts accepts
|
|
340
451
|
// hostnames too); keep `start` and let the port handling trim it.
|
|
341
|
-
|
|
452
|
+
// Not after a special scheme though: "http:8080" is a URL.
|
|
453
|
+
let allDigits = special === 0;
|
|
342
454
|
let i = indexOfColon + 1;
|
|
343
455
|
for (; i < end; i += 1) {
|
|
344
|
-
const code =
|
|
456
|
+
const code = charCodeAt$1.call(url, i);
|
|
345
457
|
if (code === 47 || code === 92 || code === 63 || code === 35) {
|
|
346
458
|
break;
|
|
347
459
|
}
|
|
@@ -354,7 +466,6 @@ function extractHostname(url, urlIsValidHostname, validate = false) {
|
|
|
354
466
|
allDigits = false; // nothing after ':' => not a port
|
|
355
467
|
}
|
|
356
468
|
if (!allDigits) {
|
|
357
|
-
const special = getSpecialScheme(url, start, indexOfColon);
|
|
358
469
|
if (special === 0) {
|
|
359
470
|
// No "scheme://" on the cold path and not a special scheme.
|
|
360
471
|
// A second ':' before the host's end marks a bare, unbracketed
|
|
@@ -363,7 +474,7 @@ function extractHostname(url, urlIsValidHostname, validate = false) {
|
|
|
363
474
|
// with no host ("mailto:x", "foo:bar").
|
|
364
475
|
let isBareIpv6 = false;
|
|
365
476
|
for (let j = indexOfColon + 1; j < end; j += 1) {
|
|
366
|
-
const code =
|
|
477
|
+
const code = charCodeAt$1.call(url, j);
|
|
367
478
|
if (code === 47 ||
|
|
368
479
|
code === 92 ||
|
|
369
480
|
code === 63 ||
|
|
@@ -375,8 +486,18 @@ function extractHostname(url, urlIsValidHostname, validate = false) {
|
|
|
375
486
|
break;
|
|
376
487
|
}
|
|
377
488
|
}
|
|
378
|
-
|
|
379
|
-
|
|
489
|
+
// Not a scheme either ("@host:", "a_b:/x"): an authority, if
|
|
490
|
+
// its ':' starts an empty port (digits were read above).
|
|
491
|
+
// Browsers have no host for "a_b:x".
|
|
492
|
+
const next = charCodeAt$1.call(url, i);
|
|
493
|
+
if (!isBareIpv6 &&
|
|
494
|
+
(isScheme(url, start, indexOfColon) ||
|
|
495
|
+
(i < end &&
|
|
496
|
+
next !== 47 &&
|
|
497
|
+
next !== 92 &&
|
|
498
|
+
next !== 63 &&
|
|
499
|
+
next !== 35))) {
|
|
500
|
+
return noHostname(url, urlIsValidHostname, validate);
|
|
380
501
|
}
|
|
381
502
|
}
|
|
382
503
|
else {
|
|
@@ -384,19 +505,19 @@ function extractHostname(url, urlIsValidHostname, validate = false) {
|
|
|
384
505
|
if (special === 2) {
|
|
385
506
|
// file (e.g. "file:\\host"): host only between "//" and next slash.
|
|
386
507
|
let slashes = 0;
|
|
387
|
-
while ((
|
|
388
|
-
|
|
508
|
+
while ((charCodeAt$1.call(url, start) === 47 ||
|
|
509
|
+
charCodeAt$1.call(url, start) === 92) &&
|
|
389
510
|
slashes < 2) {
|
|
390
511
|
start += 1;
|
|
391
512
|
slashes += 1;
|
|
392
513
|
}
|
|
393
514
|
if (slashes < 2) {
|
|
394
|
-
return
|
|
515
|
+
return noHostname(url, urlIsValidHostname, validate);
|
|
395
516
|
}
|
|
396
517
|
}
|
|
397
518
|
else {
|
|
398
|
-
while (
|
|
399
|
-
|
|
519
|
+
while (charCodeAt$1.call(url, start) === 47 ||
|
|
520
|
+
charCodeAt$1.call(url, start) === 92) {
|
|
400
521
|
start += 1;
|
|
401
522
|
}
|
|
402
523
|
}
|
|
@@ -407,8 +528,8 @@ function extractHostname(url, urlIsValidHostname, validate = false) {
|
|
|
407
528
|
}
|
|
408
529
|
}
|
|
409
530
|
// Find the host's end: first '/', '?' or '#' (and '\' for special URLs,
|
|
410
|
-
// which WHATWG treats like '/'). Track the last '@'
|
|
411
|
-
//
|
|
531
|
+
// which WHATWG treats like '/'). Track the last '@' and ':' for userinfo
|
|
532
|
+
// and port, plus the first ':' of the host (reset at each
|
|
412
533
|
// '@') to tell a bare IPv6 (>= 2 colons) from a host:port (exactly one);
|
|
413
534
|
// flag uppercase and a stray tab/newline. The loop is split on `code < 64`
|
|
414
535
|
// so common host characters take fewer comparisons.
|
|
@@ -419,7 +540,6 @@ function extractHostname(url, urlIsValidHostname, validate = false) {
|
|
|
419
540
|
// brackets, control or trailing dot); those cases clear it / are rejected by
|
|
420
541
|
// the guard below, falling back to `isValidHostname`.
|
|
421
542
|
let indexOfIdentifier = -1;
|
|
422
|
-
let indexOfClosingBracket = -1;
|
|
423
543
|
let indexOfPort = -1;
|
|
424
544
|
let indexOfFirstColon = -1;
|
|
425
545
|
let hasControl = false;
|
|
@@ -428,7 +548,7 @@ function extractHostname(url, urlIsValidHostname, validate = false) {
|
|
|
428
548
|
let vLastCode = -1;
|
|
429
549
|
if (validate && start < end) {
|
|
430
550
|
// First-char rule: must be a valid host char, '.', or '_' (NOT '-').
|
|
431
|
-
const c0 =
|
|
551
|
+
const c0 = charCodeAt$1.call(url, start);
|
|
432
552
|
if (
|
|
433
553
|
// Keep these parens: they scope /*@__INLINE__*/ for terser.
|
|
434
554
|
// prettier-ignore
|
|
@@ -441,7 +561,7 @@ function extractHostname(url, urlIsValidHostname, validate = false) {
|
|
|
441
561
|
}
|
|
442
562
|
}
|
|
443
563
|
for (let i = start; i < end; i += 1) {
|
|
444
|
-
const code =
|
|
564
|
+
const code = charCodeAt$1.call(url, i);
|
|
445
565
|
if (code < 64) {
|
|
446
566
|
if (code === 47 || code === 35 || code === 63) {
|
|
447
567
|
end = i;
|
|
@@ -483,9 +603,6 @@ function extractHostname(url, urlIsValidHostname, validate = false) {
|
|
|
483
603
|
indexOfIdentifier = i;
|
|
484
604
|
indexOfFirstColon = -1; // colons before '@' are userinfo, not the host
|
|
485
605
|
}
|
|
486
|
-
else if (code === 93 /* ']' */) {
|
|
487
|
-
indexOfClosingBracket = i;
|
|
488
|
-
}
|
|
489
606
|
else if (code >= 65 && code <= 90) {
|
|
490
607
|
hasUpper = true;
|
|
491
608
|
}
|
|
@@ -507,10 +624,17 @@ function extractHostname(url, urlIsValidHostname, validate = false) {
|
|
|
507
624
|
indexOfIdentifier < end) {
|
|
508
625
|
start = indexOfIdentifier + 1;
|
|
509
626
|
}
|
|
510
|
-
if (
|
|
511
|
-
// ipv6 address: return what is between the brackets, or null if
|
|
512
|
-
|
|
513
|
-
|
|
627
|
+
if (charCodeAt$1.call(url, start) === 91 /* '[' */) {
|
|
628
|
+
// ipv6 address: return what is between the brackets, or null if they are
|
|
629
|
+
// unclosed or followed by anything but a port ("[::1]x"). `parseImpl`
|
|
630
|
+
// checks that they hold an IPv6 address (see `extractedBracketedHost`).
|
|
631
|
+
const closingBracket = url.indexOf(']', start);
|
|
632
|
+
if (closingBracket !== -1 &&
|
|
633
|
+
closingBracket < end &&
|
|
634
|
+
(closingBracket + 1 === end ||
|
|
635
|
+
charCodeAt$1.call(url, closingBracket + 1) === 58) /* ':' */) {
|
|
636
|
+
extractedBracketedHost = true;
|
|
637
|
+
return url.slice(start + 1, closingBracket).toLowerCase();
|
|
514
638
|
}
|
|
515
639
|
return null;
|
|
516
640
|
}
|
|
@@ -523,10 +647,11 @@ function extractHostname(url, urlIsValidHostname, validate = false) {
|
|
|
523
647
|
indexOfFirstColon === indexOfPort) {
|
|
524
648
|
end = indexOfPort; // trim ':port'
|
|
525
649
|
}
|
|
526
|
-
// Empty authority ("http://", "file:///path", "//")
|
|
527
|
-
//
|
|
650
|
+
// Empty authority ("http://", "file:///path", "//"), unless a tab or
|
|
651
|
+
// newline hides one ("/\t/host"); only reachable here via extraction — a
|
|
652
|
+
// bare valid hostname never lands here.
|
|
528
653
|
if (start >= end) {
|
|
529
|
-
return
|
|
654
|
+
return noHostname(url, urlIsValidHostname, validate);
|
|
530
655
|
}
|
|
531
656
|
// Publish the inline-validation verdict — but only for a "simple" authority,
|
|
532
657
|
// where the scanned run equals the final host: no userinfo skip, no port
|
|
@@ -543,92 +668,373 @@ function extractHostname(url, urlIsValidHostname, validate = false) {
|
|
|
543
668
|
vValid &&
|
|
544
669
|
indexOfIdentifier === -1 &&
|
|
545
670
|
indexOfPort === -1 &&
|
|
546
|
-
|
|
547
|
-
url.charCodeAt(end - 1) !== 46 /* no trailing dot */ &&
|
|
671
|
+
charCodeAt$1.call(url, end - 1) !== 46 /* no trailing dot */ &&
|
|
548
672
|
end - start <= 255 && // total length
|
|
549
673
|
end - vLastDot - 1 <= 63 && // last label length
|
|
550
674
|
vLastCode !== 45 /* last char not '-' */) {
|
|
551
675
|
extractedHostnameValidated = true;
|
|
552
676
|
}
|
|
677
|
+
failedValidation = validate && !vValid;
|
|
553
678
|
}
|
|
554
679
|
// Trim trailing dots
|
|
555
|
-
while (end > start + 1 &&
|
|
680
|
+
while (end > start + 1 && charCodeAt$1.call(url, end - 1) === 46 /* '.' */) {
|
|
556
681
|
end -= 1;
|
|
557
682
|
}
|
|
558
683
|
const hostname = start !== 0 || end !== url.length ? url.slice(start, end) : url;
|
|
684
|
+
// Browsers percent-decode the host of a special URL ("%74racker.com" is
|
|
685
|
+
// tracker.com). Only when validating: otherwise the host is kept as written.
|
|
686
|
+
if (failedValidation && isSpecial && hostname.includes('%')) {
|
|
687
|
+
return decodeHostname(hostname);
|
|
688
|
+
}
|
|
559
689
|
if (hasUpper) {
|
|
560
690
|
return hostname.toLowerCase();
|
|
561
691
|
}
|
|
562
692
|
return hostname;
|
|
563
693
|
}
|
|
564
694
|
|
|
695
|
+
// Auto-generated by packages/tldts-utils/bin/ip-mapping.js (Node.js v26.13.1, Unicode 18.0). Do not edit.
|
|
696
|
+
//
|
|
697
|
+
// Non-ASCII code points that the host parser of browsers deletes, or maps to
|
|
698
|
+
// '.', or to digits, 'a'-'f' and 'x' only, as ranges: code points
|
|
699
|
+
// `ipMappingFirst[k]` to `ipMappingLast[k]` map, with `value = ipMappingValue[k]`
|
|
700
|
+
// and `offset` the position in the range: to nothing if `value` is 0, to the
|
|
701
|
+
// character `value + offset` if `value` is below 128, to the decimal number
|
|
702
|
+
// `value - 1000 + offset` if `value` is below 2000, and else to the two
|
|
703
|
+
// characters `value >> 8` and `value & 255`.
|
|
704
|
+
const ipMappingFirst = /*#__PURE__*/ new Uint32Array([170, 173, 178, 185, 739, 847, 4447, 6068, 6155, 7468, 7470, 7472, 7491, 7495, 7496, 7580, 7584, 8203, 8288, 8298, 8304, 8308, 8320, 8336, 8337, 8339, 8450, 8492, 8495, 8496, 8517, 8518, 8553, 8557, 8569, 8573, 9312, 9321, 9398, 9421, 9424, 9447, 9450, 12290, 12644, 12881, 12977, 13170, 13252, 13253, 13256, 42994, 42995, 64256, 65024, 65279, 65294, 65296, 65313, 65336, 65345, 65368, 65377, 65440, 113824, 117974, 117997, 118000, 119155, 119808, 119831, 119834, 119857, 119860, 119883, 119886, 119909, 119912, 119935, 119938, 119961, 119964, 119966, 119987, 119990, 119995, 120013, 120016, 120039, 120042, 120065, 120068, 120071, 120091, 120094, 120117, 120120, 120123, 120143, 120146, 120169, 120172, 120195, 120198, 120221, 120224, 120247, 120250, 120273, 120276, 120299, 120302, 120325, 120328, 120351, 120354, 120377, 120380, 120403, 120406, 120429, 120432, 120455, 120458, 120481, 120782, 120792, 120802, 120812, 120822, 127275, 127277, 127280, 127303, 130032, 917760]);
|
|
705
|
+
const ipMappingLast = /*#__PURE__*/ new Uint32Array([170, 173, 179, 185, 739, 847, 4448, 6069, 6159, 7468, 7470, 7473, 7491, 7495, 7497, 7580, 7584, 8203, 8292, 8303, 8304, 8313, 8329, 8336, 8337, 8339, 8450, 8493, 8495, 8497, 8517, 8519, 8553, 8558, 8569, 8574, 9320, 9331, 9403, 9421, 9429, 9447, 9450, 12290, 12644, 12895, 12991, 13170, 13252, 13253, 13256, 42994, 42995, 64256, 65039, 65279, 65294, 65305, 65318, 65336, 65350, 65368, 65377, 65440, 113827, 117979, 117997, 118009, 119162, 119813, 119831, 119839, 119857, 119865, 119883, 119891, 119909, 119917, 119935, 119943, 119961, 119964, 119967, 119987, 119993, 119995, 120013, 120021, 120039, 120047, 120065, 120069, 120073, 120091, 120099, 120117, 120121, 120125, 120143, 120151, 120169, 120177, 120195, 120203, 120221, 120229, 120247, 120255, 120273, 120281, 120299, 120307, 120325, 120333, 120351, 120359, 120377, 120385, 120403, 120411, 120429, 120437, 120455, 120463, 120481, 120791, 120801, 120811, 120821, 120831, 127275, 127277, 127285, 127303, 130041, 917999]);
|
|
706
|
+
const ipMappingValue = /*#__PURE__*/ new Uint16Array([97, 0, 50, 49, 120, 0, 0, 0, 0, 97, 98, 100, 97, 98, 100, 99, 102, 0, 0, 0, 48, 52, 48, 97, 101, 120, 99, 98, 101, 101, 100, 100, 120, 99, 120, 99, 49, 1010, 97, 120, 97, 120, 48, 46, 0, 1021, 1036, 25697, 25443, 25444, 25698, 99, 102, 26214, 0, 0, 46, 48, 97, 120, 97, 120, 46, 0, 0, 97, 120, 48, 0, 97, 120, 97, 120, 97, 120, 97, 120, 97, 120, 97, 120, 97, 99, 120, 97, 102, 120, 97, 120, 97, 120, 97, 100, 120, 97, 120, 97, 100, 120, 97, 120, 97, 120, 97, 120, 97, 120, 97, 120, 97, 120, 97, 120, 97, 120, 97, 120, 97, 120, 97, 120, 97, 120, 97, 120, 48, 48, 48, 48, 48, 99, 25444, 97, 120, 48, 0]);
|
|
707
|
+
// Bit `(cp >> 8) & 31` of `ipMappingBlocks[cp >> 13]` is set when the block of
|
|
708
|
+
// 256 code points holding `cp` (below U+10000) holds any of them.
|
|
709
|
+
const ipMappingBlocks = /*#__PURE__*/ new Uint32Array([562167821, 983059, 0, 0, 0, 128, 0, 3355443200]);
|
|
710
|
+
|
|
565
711
|
/**
|
|
566
|
-
*
|
|
567
|
-
*
|
|
712
|
+
* Detection of IP addresses, as defined by the host parser of the WHATWG URL
|
|
713
|
+
* standard (https://url.spec.whatwg.org/#host-parsing), which browsers follow.
|
|
714
|
+
* Addresses are recognized, never rewritten: `0x7f.1` is 127.0.0.1 for a
|
|
715
|
+
* browser, hence an IP here, but stays `0x7f.1`.
|
|
568
716
|
*/
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
if (hostname.length > 15) {
|
|
576
|
-
return false;
|
|
717
|
+
/**
|
|
718
|
+
* Value of the hexadecimal digit `code` (0 to 15), or 16 if it is not one.
|
|
719
|
+
*/
|
|
720
|
+
function hexValue(code) {
|
|
721
|
+
if (code >= 48 && code <= 57) {
|
|
722
|
+
return code - 48; // 0-9
|
|
577
723
|
}
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
724
|
+
const letter = code | 32; // lower-case
|
|
725
|
+
return letter >= 97 && letter <= 102 ? letter - 87 : 16; // a-f
|
|
726
|
+
}
|
|
727
|
+
/**
|
|
728
|
+
* Check if `hostname` is an IPv4 address: one to four numbers separated by
|
|
729
|
+
* dots, each one decimal, octal (leading `0`) or hexadecimal (leading `0x`).
|
|
730
|
+
* The last number fills the bytes the others leave, so `127.1` and
|
|
731
|
+
* `2130706433` are both 127.0.0.1.
|
|
732
|
+
* https://url.spec.whatwg.org/#concept-ipv4-parser
|
|
733
|
+
*/
|
|
734
|
+
function isIpv4(hostname) {
|
|
735
|
+
const end = hostname.length;
|
|
736
|
+
let i = 0;
|
|
737
|
+
for (let numbers = 1; numbers <= 4; numbers += 1) {
|
|
738
|
+
let radix = 10;
|
|
739
|
+
if (i + 1 < end && hostname.charCodeAt(i) === 48 /* '0' */) {
|
|
740
|
+
radix = 8;
|
|
741
|
+
if ((hostname.charCodeAt(i + 1) | 32) === 120 /* 'x' */) {
|
|
742
|
+
radix = 16;
|
|
743
|
+
i += 2;
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
const start = i;
|
|
747
|
+
let value = 0;
|
|
748
|
+
for (; i < end; i += 1) {
|
|
749
|
+
const code = hostname.charCodeAt(i);
|
|
750
|
+
if (code === 46 /* '.' */) {
|
|
751
|
+
break;
|
|
752
|
+
}
|
|
753
|
+
const digit = hexValue(code);
|
|
754
|
+
if (digit >= radix) {
|
|
755
|
+
return false;
|
|
756
|
+
}
|
|
757
|
+
value = value * radix + digit;
|
|
758
|
+
}
|
|
759
|
+
// A number has digits, except "0x" which is 0.
|
|
760
|
+
if (i === start && radix !== 16) {
|
|
761
|
+
return false;
|
|
762
|
+
}
|
|
763
|
+
if (i === end) {
|
|
764
|
+
return value <= 0xffffffff >>> (8 * (numbers - 1));
|
|
583
765
|
}
|
|
584
|
-
|
|
766
|
+
if (value > 255) {
|
|
585
767
|
return false;
|
|
586
768
|
}
|
|
769
|
+
i += 1; // '.'
|
|
587
770
|
}
|
|
588
|
-
return
|
|
589
|
-
hostname.charCodeAt(0) !== 46 /* '.' */ &&
|
|
590
|
-
hostname.charCodeAt(hostname.length - 1) !== 46 /* '.' */);
|
|
771
|
+
return false;
|
|
591
772
|
}
|
|
592
773
|
/**
|
|
593
|
-
*
|
|
774
|
+
* Check if `hostname[i, end)` is made of four decimal numbers separated by
|
|
775
|
+
* dots, each one between 0 and 255 and without leading zero.
|
|
594
776
|
*/
|
|
595
|
-
function
|
|
596
|
-
|
|
597
|
-
|
|
777
|
+
function isDottedDecimal(hostname, i, end) {
|
|
778
|
+
for (let numbers = 1; numbers <= 4; numbers += 1) {
|
|
779
|
+
const start = i;
|
|
780
|
+
let value = 0;
|
|
781
|
+
for (; i < end; i += 1) {
|
|
782
|
+
const digit = hostname.charCodeAt(i) - 48;
|
|
783
|
+
if (digit < 0 || digit > 9) {
|
|
784
|
+
break;
|
|
785
|
+
}
|
|
786
|
+
value = value * 10 + digit;
|
|
787
|
+
// Only a leading zero keeps a number of several digits below 10.
|
|
788
|
+
if (value > 255 || (value < 10 && i !== start)) {
|
|
789
|
+
return false;
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
if (i === start) {
|
|
793
|
+
return false;
|
|
794
|
+
}
|
|
795
|
+
if (numbers < 4) {
|
|
796
|
+
if (hostname.charCodeAt(i) !== 46 /* '.' */) {
|
|
797
|
+
return false;
|
|
798
|
+
}
|
|
799
|
+
i += 1;
|
|
800
|
+
}
|
|
598
801
|
}
|
|
599
|
-
|
|
802
|
+
return i === end;
|
|
803
|
+
}
|
|
804
|
+
/**
|
|
805
|
+
* Check if `hostname` is an IPv6 address, with or without its brackets: eight
|
|
806
|
+
* groups of one to four hexadecimal digits separated by colons, where a `::`
|
|
807
|
+
* may stand once for one or more missing groups, and a dotted-decimal IPv4
|
|
808
|
+
* address may stand for the last two.
|
|
809
|
+
* https://url.spec.whatwg.org/#concept-ipv6-parser
|
|
810
|
+
*/
|
|
811
|
+
function isIpv6(hostname) {
|
|
812
|
+
let i = 0;
|
|
600
813
|
let end = hostname.length;
|
|
601
|
-
if (hostname
|
|
814
|
+
if (hostname.charCodeAt(0) === 91 /* '[' */) {
|
|
815
|
+
if (hostname.charCodeAt(end - 1) !== 93 /* ']' */) {
|
|
816
|
+
return false;
|
|
817
|
+
}
|
|
818
|
+
i = 1;
|
|
602
819
|
end -= 1;
|
|
603
820
|
}
|
|
604
|
-
//
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
821
|
+
// An address only starts with a ':' if it starts with "::".
|
|
822
|
+
if (i < end && hostname.charCodeAt(i) === 58 /* ':' */) {
|
|
823
|
+
if (i + 1 === end || hostname.charCodeAt(i + 1) !== 58 /* ':' */) {
|
|
824
|
+
return false;
|
|
825
|
+
}
|
|
826
|
+
i += 1;
|
|
609
827
|
}
|
|
610
|
-
let
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
828
|
+
let groups = 0;
|
|
829
|
+
let compressed = false; // "::" was seen, and counted as one group
|
|
830
|
+
// Here `i` is where a group starts: a ':' can only be the second of a "::".
|
|
831
|
+
while (i < end && groups < 8) {
|
|
832
|
+
if (hostname.charCodeAt(i) === 58 /* ':' */) {
|
|
833
|
+
if (compressed) {
|
|
834
|
+
return false;
|
|
835
|
+
}
|
|
836
|
+
compressed = true;
|
|
837
|
+
groups += 1;
|
|
838
|
+
i += 1;
|
|
839
|
+
continue;
|
|
840
|
+
}
|
|
841
|
+
const start = i;
|
|
842
|
+
while (i < end &&
|
|
843
|
+
i - start < 4 &&
|
|
844
|
+
hexValue(hostname.charCodeAt(i)) !== 16) {
|
|
845
|
+
i += 1;
|
|
615
846
|
}
|
|
616
|
-
|
|
617
|
-
(code >= 97 && code <= 102) || // a-f
|
|
618
|
-
(code >= 65 && code <= 70) // A-F (RFC 4291 §2.2: an IPv6 hextet is hex digits only)
|
|
619
|
-
)) {
|
|
847
|
+
if (i === start) {
|
|
620
848
|
return false;
|
|
621
849
|
}
|
|
850
|
+
if (i === end) {
|
|
851
|
+
return compressed || groups === 7;
|
|
852
|
+
}
|
|
853
|
+
if (hostname.charCodeAt(i) === 46 /* '.' */) {
|
|
854
|
+
return ((compressed ? groups <= 6 : groups === 6) &&
|
|
855
|
+
isDottedDecimal(hostname, start, end));
|
|
856
|
+
}
|
|
857
|
+
// Anything else is a ':' followed by another group, or by a ':'.
|
|
858
|
+
if (hostname.charCodeAt(i) !== 58 /* ':' */ || i + 1 === end) {
|
|
859
|
+
return false;
|
|
860
|
+
}
|
|
861
|
+
groups += 1;
|
|
862
|
+
i += 1;
|
|
622
863
|
}
|
|
623
|
-
return
|
|
864
|
+
return compressed && i === end;
|
|
624
865
|
}
|
|
625
866
|
/**
|
|
626
|
-
*
|
|
627
|
-
*
|
|
628
|
-
*
|
|
867
|
+
* What the host parser of browsers maps the non-ASCII code point `cp` to, as
|
|
868
|
+
* far as IP detection goes: nothing (deleted), '.', digits, 'a'-'f' or 'x', or
|
|
869
|
+
* else 'g', standing for any character that keeps a label from being a number.
|
|
870
|
+
* https://www.unicode.org/reports/tr46/#Processing
|
|
871
|
+
*/
|
|
872
|
+
function mapCodePoint(cp) {
|
|
873
|
+
// Most letters, as in internationalized domain names, are in blocks of the
|
|
874
|
+
// table without any entry.
|
|
875
|
+
if (cp < 0x10000 &&
|
|
876
|
+
((ipMappingBlocks[cp >> 13] >>> ((cp >> 8) & 31)) & 1) === 0) {
|
|
877
|
+
return 'g';
|
|
878
|
+
}
|
|
879
|
+
let low = 0;
|
|
880
|
+
let high = ipMappingFirst.length - 1;
|
|
881
|
+
while (low <= high) {
|
|
882
|
+
const mid = (low + high) >>> 1;
|
|
883
|
+
if (cp < ipMappingFirst[mid]) {
|
|
884
|
+
high = mid - 1;
|
|
885
|
+
}
|
|
886
|
+
else if (cp > ipMappingLast[mid]) {
|
|
887
|
+
low = mid + 1;
|
|
888
|
+
}
|
|
889
|
+
else {
|
|
890
|
+
const value = ipMappingValue[mid];
|
|
891
|
+
const offset = cp - ipMappingFirst[mid];
|
|
892
|
+
if (value === 0) {
|
|
893
|
+
return '';
|
|
894
|
+
}
|
|
895
|
+
if (value < 128) {
|
|
896
|
+
return String.fromCharCode(value + offset);
|
|
897
|
+
}
|
|
898
|
+
if (value < 2000) {
|
|
899
|
+
return String(value - 1000 + offset);
|
|
900
|
+
}
|
|
901
|
+
return String.fromCharCode(value >> 8, value & 255);
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
return 'g';
|
|
905
|
+
}
|
|
906
|
+
/**
|
|
907
|
+
* `isIp` for a host holding non-ASCII characters, read as browsers read it:
|
|
908
|
+
* they first map some characters to digits or dots and delete others, so that
|
|
909
|
+
* `127.0.0.1`, or `127.0.0.1` followed by a zero-width space, is 127.0.0.1
|
|
910
|
+
* for them. The host is mapped the same way, as far as IP detection goes, and
|
|
911
|
+
* checked again.
|
|
912
|
+
*/
|
|
913
|
+
function isMappedIp(hostname) {
|
|
914
|
+
// Most such hosts are internationalized domain names, with a character that
|
|
915
|
+
// keeps their last label from being a number: find it first.
|
|
916
|
+
for (let i = hostname.length - 1; i >= 0; i -= 1) {
|
|
917
|
+
let cp = hostname.charCodeAt(i);
|
|
918
|
+
if (cp === 46 /* '.' */) {
|
|
919
|
+
break;
|
|
920
|
+
}
|
|
921
|
+
if (cp < 128) {
|
|
922
|
+
if (hexValue(cp) === 16 && (cp | 32) !== 120 /* 'x' */) {
|
|
923
|
+
return false;
|
|
924
|
+
}
|
|
925
|
+
continue;
|
|
926
|
+
}
|
|
927
|
+
// Most letters are in blocks of 256 code points without any entry.
|
|
928
|
+
if (cp < 0xd800 &&
|
|
929
|
+
((ipMappingBlocks[cp >> 13] >>> ((cp >> 8) & 31)) & 1) === 0) {
|
|
930
|
+
return false;
|
|
931
|
+
}
|
|
932
|
+
if (cp >= 0xdc00 && cp <= 0xdfff && i > 0) {
|
|
933
|
+
const high = hostname.charCodeAt(i - 1);
|
|
934
|
+
if (high >= 0xd800 && high <= 0xdbff) {
|
|
935
|
+
cp = (high - 0xd800) * 0x400 + (cp - 0xdc00) + 0x10000;
|
|
936
|
+
i -= 1;
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
const mapped = mapCodePoint(cp);
|
|
940
|
+
if (mapped === 'g') {
|
|
941
|
+
return false;
|
|
942
|
+
}
|
|
943
|
+
if (mapped === '.') {
|
|
944
|
+
break;
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
// Browsers read an IPv6 address, between brackets, as written: a host
|
|
948
|
+
// holding non-ASCII characters is never one.
|
|
949
|
+
if (hostname.includes(':')) {
|
|
950
|
+
return false;
|
|
951
|
+
}
|
|
952
|
+
let ascii = '';
|
|
953
|
+
for (let i = 0; i < hostname.length; i += 1) {
|
|
954
|
+
const code = hostname.charCodeAt(i);
|
|
955
|
+
if (code < 128) {
|
|
956
|
+
ascii += hostname.charAt(i);
|
|
957
|
+
}
|
|
958
|
+
else {
|
|
959
|
+
let cp = code;
|
|
960
|
+
if (code >= 0xd800 && code <= 0xdbff) {
|
|
961
|
+
const low = hostname.charCodeAt(i + 1);
|
|
962
|
+
if (low >= 0xdc00 && low <= 0xdfff) {
|
|
963
|
+
cp = (code - 0xd800) * 0x400 + (low - 0xdc00) + 0x10000;
|
|
964
|
+
i += 1;
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
ascii += mapCodePoint(cp);
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
// Trailing dots go, as callers remove them from `hostname`.
|
|
971
|
+
let end = ascii.length;
|
|
972
|
+
while (end > 1 && ascii.charCodeAt(end - 1) === 46 /* '.' */) {
|
|
973
|
+
end -= 1;
|
|
974
|
+
}
|
|
975
|
+
return isIp(ascii.slice(0, end));
|
|
976
|
+
}
|
|
977
|
+
/**
|
|
978
|
+
* For a host ending in a number without being an IP address in ASCII: `null`,
|
|
979
|
+
* unless it holds non-ASCII characters, which browsers may map (see
|
|
980
|
+
* `isMappedIp`).
|
|
981
|
+
*/
|
|
982
|
+
function isNonAsciiIp(hostname) {
|
|
983
|
+
for (let i = 0; i < hostname.length; i += 1) {
|
|
984
|
+
if (hostname.charCodeAt(i) >= 128) {
|
|
985
|
+
return isMappedIp(hostname);
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
return null;
|
|
989
|
+
}
|
|
990
|
+
/**
|
|
991
|
+
* Check if `hostname` is an IP address (either IPv4 or IPv6).
|
|
992
|
+
*
|
|
993
|
+
* The answer is `null` for a host ending in a number without being an IPv4
|
|
994
|
+
* address (`1.2.3.4.5`, `example.123`): browsers read such a host as an
|
|
995
|
+
* address and nothing else, so it is not a valid host, and never a domain.
|
|
996
|
+
* `hostname` has no trailing dot: callers strip them.
|
|
629
997
|
*/
|
|
630
998
|
function isIp(hostname) {
|
|
631
|
-
|
|
999
|
+
// Go backward over the digits ending `hostname`, decimal ones first, then
|
|
1000
|
+
// hexadecimal ones, down to `code`, the character before them (NaN before
|
|
1001
|
+
// the start of `hostname`). It tells what `hostname` may be, and most often
|
|
1002
|
+
// that it is not an address.
|
|
1003
|
+
const last = hostname.length - 1;
|
|
1004
|
+
let i = last;
|
|
1005
|
+
let code = hostname.charCodeAt(i);
|
|
1006
|
+
while (code >= 48 && code <= 57 /* 0-9 */) {
|
|
1007
|
+
i -= 1;
|
|
1008
|
+
code = hostname.charCodeAt(i);
|
|
1009
|
+
}
|
|
1010
|
+
const decimal = i;
|
|
1011
|
+
while (hexValue(code) !== 16) {
|
|
1012
|
+
i -= 1;
|
|
1013
|
+
code = hostname.charCodeAt(i);
|
|
1014
|
+
}
|
|
1015
|
+
// An IPv6 address ends with a group, which follows a ':', with "::" or
|
|
1016
|
+
// with a ']'. Its last label is not a number then.
|
|
1017
|
+
if (code === 58 /* ':' */ || code === 93 /* ']' */) {
|
|
1018
|
+
return isIpv6(hostname);
|
|
1019
|
+
}
|
|
1020
|
+
// An IPv4 address ends with a number: a last label only made of decimal
|
|
1021
|
+
// digits, or of "0x" followed by hexadecimal digits. An IPv6 address ending
|
|
1022
|
+
// with an IPv4 one ends with a number too.
|
|
1023
|
+
// https://url.spec.whatwg.org/#ends-in-a-number-checker
|
|
1024
|
+
if ((i === decimal && i !== last && (code === 46 /* '.' */ || i === -1)) ||
|
|
1025
|
+
((code | 32) === 120 /* 'x' */ &&
|
|
1026
|
+
hostname.charCodeAt(i - 1) === 48 /* '0' */ &&
|
|
1027
|
+
(i === 1 || hostname.charCodeAt(i - 2) === 46)) /* '.' */) {
|
|
1028
|
+
return isIpv4(hostname) || isIpv6(hostname) ? true : isNonAsciiIp(hostname);
|
|
1029
|
+
}
|
|
1030
|
+
// A non-ASCII character right before the digits ending `hostname`, or one
|
|
1031
|
+
// or two characters before their "x", may be one that browsers map to a
|
|
1032
|
+
// digit or a dot, or delete: `hostname` may end in a number for them.
|
|
1033
|
+
return code >= 128 ||
|
|
1034
|
+
((code | 32) === 120 /* 'x' */ &&
|
|
1035
|
+
(hostname.charCodeAt(i - 1) >= 128 || hostname.charCodeAt(i - 2) >= 128))
|
|
1036
|
+
? isMappedIp(hostname)
|
|
1037
|
+
: false;
|
|
632
1038
|
}
|
|
633
1039
|
|
|
634
1040
|
/**
|
|
@@ -701,6 +1107,13 @@ function isSpecialUse(hostname) {
|
|
|
701
1107
|
*
|
|
702
1108
|
* If you need stricter validation, consider using an external library.
|
|
703
1109
|
*/
|
|
1110
|
+
// `charCodeAt.call(s, i)`, not `s.charCodeAt(i)`: V8 specializes a
|
|
1111
|
+
// `s.charCodeAt` call site for at most four kinds of strings (flat, sliced,
|
|
1112
|
+
// concatenated, internalized, of one or two bytes per character...). Past
|
|
1113
|
+
// that, it finds the method through a generic cache and no longer inlines it,
|
|
1114
|
+
// on every call. Hostnames come here from callers and from URLs, of any kind.
|
|
1115
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
1116
|
+
const charCodeAt = String.prototype.charCodeAt;
|
|
704
1117
|
// KEEP IN SYNC with `extract-hostname.ts` `isValidHostnameChar` + its inline
|
|
705
1118
|
// scan/verdict, which duplicate these structural rules to validate during
|
|
706
1119
|
// extraction (a perf fusion). That copy additionally accepts A-Z (the host is
|
|
@@ -723,9 +1136,9 @@ function isValidHostname (hostname) {
|
|
|
723
1136
|
return false;
|
|
724
1137
|
}
|
|
725
1138
|
if (
|
|
726
|
-
/*@__INLINE__*/ !isValidAscii(
|
|
727
|
-
|
|
728
|
-
|
|
1139
|
+
/*@__INLINE__*/ !isValidAscii(charCodeAt.call(hostname, 0)) &&
|
|
1140
|
+
charCodeAt.call(hostname, 0) !== 46 && // '.' (dot)
|
|
1141
|
+
charCodeAt.call(hostname, 0) !== 95 // '_' (underscore)
|
|
729
1142
|
) {
|
|
730
1143
|
return false;
|
|
731
1144
|
}
|
|
@@ -734,7 +1147,7 @@ function isValidHostname (hostname) {
|
|
|
734
1147
|
let lastCharCode = -1;
|
|
735
1148
|
const len = hostname.length;
|
|
736
1149
|
for (let i = 0; i < len; i += 1) {
|
|
737
|
-
const code =
|
|
1150
|
+
const code = charCodeAt.call(hostname, i);
|
|
738
1151
|
if (code === 46 /* '.' */) {
|
|
739
1152
|
if (
|
|
740
1153
|
// Check that previous label is < 63 bytes long (64 = 63 + '.')
|
|
@@ -870,7 +1283,8 @@ function setDefaultsImpl({ allowIcannDomains = true, allowPrivateDomains = false
|
|
|
870
1283
|
}
|
|
871
1284
|
const DEFAULT_OPTIONS = /*@__INLINE__*/ setDefaultsImpl({});
|
|
872
1285
|
function setDefaults(options) {
|
|
873
|
-
|
|
1286
|
+
// `null` as well: untyped callers pass it to mean "no options".
|
|
1287
|
+
if (options === undefined || options === null) {
|
|
874
1288
|
return DEFAULT_OPTIONS;
|
|
875
1289
|
}
|
|
876
1290
|
return /*@__INLINE__*/ setDefaultsImpl(options);
|
|
@@ -953,12 +1367,36 @@ function parseImpl(url, step, suffixLookup, partialOptions, result) {
|
|
|
953
1367
|
else {
|
|
954
1368
|
result.hostname = extractHostname(url, false, options.validateHostname);
|
|
955
1369
|
}
|
|
1370
|
+
// A host in brackets is an IPv6 address, or the URL is invalid, as browsers
|
|
1371
|
+
// read it: "http://[1.2.3.4]/" and "http://[a.com]/" have no hostname. An
|
|
1372
|
+
// IPv4 address has no ':', every IPv6 address has one.
|
|
1373
|
+
if (options.extractHostname &&
|
|
1374
|
+
extractedBracketedHost &&
|
|
1375
|
+
result.hostname !== null) {
|
|
1376
|
+
if (isIp(result.hostname) !== true || !result.hostname.includes(':')) {
|
|
1377
|
+
result.hostname = null;
|
|
1378
|
+
return result;
|
|
1379
|
+
}
|
|
1380
|
+
if (options.detectIp) {
|
|
1381
|
+
result.isIp = true;
|
|
1382
|
+
return result;
|
|
1383
|
+
}
|
|
1384
|
+
}
|
|
956
1385
|
// Check if `hostname` is a valid ip address
|
|
957
1386
|
if (options.detectIp && result.hostname !== null) {
|
|
958
|
-
|
|
1387
|
+
const isIpAddress = isIp(result.hostname);
|
|
1388
|
+
result.isIp = isIpAddress === true;
|
|
959
1389
|
if (result.isIp) {
|
|
960
1390
|
return result;
|
|
961
1391
|
}
|
|
1392
|
+
// `null`: the host ends in a number, as an IPv4 address, without being
|
|
1393
|
+
// one. It is not a valid host then, and never a domain name.
|
|
1394
|
+
if (isIpAddress === null &&
|
|
1395
|
+
options.validateHostname &&
|
|
1396
|
+
options.extractHostname) {
|
|
1397
|
+
result.hostname = null;
|
|
1398
|
+
return result;
|
|
1399
|
+
}
|
|
962
1400
|
}
|
|
963
1401
|
// Perform hostname validation if enabled. If hostname is not valid, no need to
|
|
964
1402
|
// go further as there will be no valid domain or sub-domain. This validation
|