tldts-core 7.5.6 → 7.6.1
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/src/extract-hostname.ts
CHANGED
|
@@ -1,9 +1,22 @@
|
|
|
1
|
+
// `charCodeAt.call(url, i)`, not `url.charCodeAt(i)`: see `is-valid.ts`.
|
|
2
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
3
|
+
const charCodeAt = String.prototype.charCodeAt;
|
|
4
|
+
|
|
1
5
|
/**
|
|
2
6
|
* Matches an ASCII tab (U+0009) or newline (U+000A / U+000D). The WHATWG URL
|
|
3
7
|
* parser strips these before parsing; we only allocate a cleaned copy (and
|
|
4
8
|
* re-parse) on the rare input that actually contains one.
|
|
5
9
|
*/
|
|
6
10
|
const CONTROL_CHARS = /[\t\n\r]/g;
|
|
11
|
+
const HAS_CONTROL_CHAR = /[\t\n\r]/;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Matches a forbidden domain code point
|
|
15
|
+
* (https://url.spec.whatwg.org/#forbidden-domain-code-point): a C0 control,
|
|
16
|
+
* space, '#', '%', '/', ':', '<', '>', '?', '@', '[', '\', ']', '^', '|' or DEL.
|
|
17
|
+
*/
|
|
18
|
+
// eslint-disable-next-line no-control-regex
|
|
19
|
+
const FORBIDDEN_DOMAIN_CHAR = /[\x00-\x20#%/:<>?@[\\\]^|\x7f]/;
|
|
7
20
|
|
|
8
21
|
// Set by `extractHostname` (a module-scope flag, read synchronously by
|
|
9
22
|
// `parseImpl` right after the call — same pattern as the reused RESULT object).
|
|
@@ -16,6 +29,11 @@ const CONTROL_CHARS = /[\t\n\r]/g;
|
|
|
16
29
|
// never accept an invalid one.
|
|
17
30
|
export let extractedHostnameValidated = false;
|
|
18
31
|
|
|
32
|
+
// Set by `extractHostname`, like `extractedHostnameValidated`: `true` ONLY when
|
|
33
|
+
// the returned host was in brackets ("http://[::1]/"). `parseImpl` then checks
|
|
34
|
+
// that it is an IPv6 address, as browsers do.
|
|
35
|
+
export let extractedBracketedHost = false;
|
|
36
|
+
|
|
19
37
|
/**
|
|
20
38
|
* True if char `code` is a valid hostname character. This is the per-char half
|
|
21
39
|
* of `is-valid.ts`'s `isValidAscii` (a-z, 0-9, > U+007F) PLUS three additions:
|
|
@@ -36,6 +54,80 @@ function isValidHostnameChar(code: number): boolean {
|
|
|
36
54
|
);
|
|
37
55
|
}
|
|
38
56
|
|
|
57
|
+
/**
|
|
58
|
+
* No hostname in `url`, unless a tab or newline, which browsers remove before
|
|
59
|
+
* anything else, hides one ("sc:\t//host", "/\t/host"): `url` is then read
|
|
60
|
+
* again without them. Only reached on inputs without a host.
|
|
61
|
+
*/
|
|
62
|
+
function noHostname(
|
|
63
|
+
url: string,
|
|
64
|
+
urlIsValidHostname: boolean,
|
|
65
|
+
validate: boolean,
|
|
66
|
+
): string | null {
|
|
67
|
+
return HAS_CONTROL_CHAR.test(url)
|
|
68
|
+
? extractHostname(
|
|
69
|
+
url.replace(CONTROL_CHARS, ''),
|
|
70
|
+
urlIsValidHostname,
|
|
71
|
+
validate,
|
|
72
|
+
)
|
|
73
|
+
: null;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* True if char `code` may follow the first letter of a scheme: an ASCII letter,
|
|
78
|
+
* a digit, '+', '-' or '.' (https://url.spec.whatwg.org/#scheme-state).
|
|
79
|
+
*/
|
|
80
|
+
function isSchemeChar(code: number): boolean {
|
|
81
|
+
return (
|
|
82
|
+
((code | 32) >= 97 && (code | 32) <= 122) || // [a-zA-Z] (fold letters only)
|
|
83
|
+
(code >= 48 && code <= 57) || // [0, 9]
|
|
84
|
+
code === 46 || // '.'
|
|
85
|
+
code === 45 || // '-'
|
|
86
|
+
code === 43 // '+'
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* `hostname` percent-decoded as browsers decode the host of a special URL,
|
|
92
|
+
* lower-cased and without trailing dots, or null where they find no valid host:
|
|
93
|
+
* bytes that are no UTF-8 ("%FF"), a '%' not followed by two hexadecimal digits
|
|
94
|
+
* ("%zz"), and characters forbidden in a domain once decoded ("%2F", "%3A",
|
|
95
|
+
* "%5B::1%5D"). https://url.spec.whatwg.org/#concept-host-parser
|
|
96
|
+
*/
|
|
97
|
+
function decodeHostname(hostname: string): string | null {
|
|
98
|
+
let decoded: string;
|
|
99
|
+
try {
|
|
100
|
+
decoded = decodeURIComponent(hostname);
|
|
101
|
+
} catch {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
if (FORBIDDEN_DOMAIN_CHAR.test(decoded)) {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
let end = decoded.length;
|
|
108
|
+
while (end > 1 && charCodeAt.call(decoded, end - 1) === 46 /* '.' */) {
|
|
109
|
+
end -= 1;
|
|
110
|
+
}
|
|
111
|
+
return decoded.slice(0, end).toLowerCase();
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* True if `url.slice(start, end)` is a scheme, as browsers read one: an ASCII
|
|
116
|
+
* letter, then scheme characters.
|
|
117
|
+
*/
|
|
118
|
+
function isScheme(url: string, start: number, end: number): boolean {
|
|
119
|
+
const first = charCodeAt.call(url, start) | 32;
|
|
120
|
+
if (start === end || first < 97 || first > 122) {
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
for (let i = start + 1; i < end; i += 1) {
|
|
124
|
+
if (!isSchemeChar(charCodeAt.call(url, i))) {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
|
|
39
131
|
/**
|
|
40
132
|
* Classify scheme `url.slice(schemeStart, colonIndex)` as a WHATWG special
|
|
41
133
|
* scheme without allocating a substring (case-insensitive via `| 32`).
|
|
@@ -51,28 +143,30 @@ function getSpecialScheme(
|
|
|
51
143
|
colonIndex: number,
|
|
52
144
|
): number {
|
|
53
145
|
const length = colonIndex - schemeStart;
|
|
54
|
-
const c0 =
|
|
146
|
+
const c0 = charCodeAt.call(url, schemeStart) | 32;
|
|
55
147
|
if (length === 2) {
|
|
56
|
-
return c0 === 119 && (
|
|
148
|
+
return c0 === 119 && (charCodeAt.call(url, schemeStart + 1) | 32) === 115
|
|
149
|
+
? 1
|
|
150
|
+
: 0; // ws
|
|
57
151
|
} else if (length === 3) {
|
|
58
|
-
const c1 =
|
|
59
|
-
const c2 =
|
|
152
|
+
const c1 = charCodeAt.call(url, schemeStart + 1) | 32;
|
|
153
|
+
const c2 = charCodeAt.call(url, schemeStart + 2) | 32;
|
|
60
154
|
if (c0 === 119 && c1 === 115 && c2 === 115) return 1; // wss
|
|
61
155
|
if (c0 === 102 && c1 === 116 && c2 === 112) return 1; // ftp
|
|
62
156
|
return 0;
|
|
63
157
|
} else if (length === 4) {
|
|
64
|
-
const c1 =
|
|
65
|
-
const c2 =
|
|
66
|
-
const c3 =
|
|
158
|
+
const c1 = charCodeAt.call(url, schemeStart + 1) | 32;
|
|
159
|
+
const c2 = charCodeAt.call(url, schemeStart + 2) | 32;
|
|
160
|
+
const c3 = charCodeAt.call(url, schemeStart + 3) | 32;
|
|
67
161
|
if (c0 === 104 && c1 === 116 && c2 === 116 && c3 === 112) return 1; // http
|
|
68
162
|
if (c0 === 102 && c1 === 105 && c2 === 108 && c3 === 101) return 2; // file
|
|
69
163
|
return 0;
|
|
70
164
|
} else if (length === 5) {
|
|
71
165
|
return c0 === 104 &&
|
|
72
|
-
(
|
|
73
|
-
(
|
|
74
|
-
(
|
|
75
|
-
(
|
|
166
|
+
(charCodeAt.call(url, schemeStart + 1) | 32) === 116 &&
|
|
167
|
+
(charCodeAt.call(url, schemeStart + 2) | 32) === 116 &&
|
|
168
|
+
(charCodeAt.call(url, schemeStart + 3) | 32) === 112 &&
|
|
169
|
+
(charCodeAt.call(url, schemeStart + 4) | 32) === 115
|
|
76
170
|
? 1
|
|
77
171
|
: 0; // https
|
|
78
172
|
}
|
|
@@ -102,53 +196,77 @@ export default function extractHostname(
|
|
|
102
196
|
let start = 0;
|
|
103
197
|
let end: number = url.length;
|
|
104
198
|
let hasUpper = false;
|
|
199
|
+
let failedValidation = false; // inline validation failed (always so with a '%')
|
|
105
200
|
let isSpecial = true; // '\' ends the host, unless non-special "scheme://"
|
|
106
201
|
extractedHostnameValidated = false;
|
|
202
|
+
extractedBracketedHost = false;
|
|
107
203
|
|
|
108
204
|
if (!urlIsValidHostname) {
|
|
109
205
|
// WHATWG step 1: trim leading/trailing C0 control or space (<= U+0020).
|
|
110
206
|
// Tab/newline elsewhere are handled lazily below.
|
|
111
|
-
while (start < url.length &&
|
|
207
|
+
while (start < url.length && charCodeAt.call(url, start) <= 32) {
|
|
112
208
|
start += 1;
|
|
113
209
|
}
|
|
114
210
|
|
|
115
211
|
// "data:"/"javascript:" never reach a host (inline payload; data URLs may be
|
|
116
212
|
// huge). `| 32` lowercases letters only; tab/newline is re-parsed below.
|
|
117
213
|
if (
|
|
118
|
-
(
|
|
119
|
-
(
|
|
120
|
-
(
|
|
121
|
-
(
|
|
122
|
-
(
|
|
123
|
-
(
|
|
124
|
-
(
|
|
125
|
-
(
|
|
126
|
-
(
|
|
127
|
-
(
|
|
128
|
-
(
|
|
129
|
-
(
|
|
130
|
-
(
|
|
131
|
-
(
|
|
132
|
-
(
|
|
133
|
-
(
|
|
214
|
+
(charCodeAt.call(url, start + 4) === 58 /* ':' */ &&
|
|
215
|
+
(charCodeAt.call(url, start) | 32) === 100 /* d */ &&
|
|
216
|
+
(charCodeAt.call(url, start + 1) | 32) === 97 /* a */ &&
|
|
217
|
+
(charCodeAt.call(url, start + 2) | 32) === 116 /* t */ &&
|
|
218
|
+
(charCodeAt.call(url, start + 3) | 32) === 97) /* a */ ||
|
|
219
|
+
(charCodeAt.call(url, start + 10) === 58 /* ':' */ &&
|
|
220
|
+
(charCodeAt.call(url, start) | 32) === 106 /* j */ &&
|
|
221
|
+
(charCodeAt.call(url, start + 1) | 32) === 97 /* a */ &&
|
|
222
|
+
(charCodeAt.call(url, start + 2) | 32) === 118 /* v */ &&
|
|
223
|
+
(charCodeAt.call(url, start + 3) | 32) === 97 /* a */ &&
|
|
224
|
+
(charCodeAt.call(url, start + 4) | 32) === 115 /* s */ &&
|
|
225
|
+
(charCodeAt.call(url, start + 5) | 32) === 99 /* c */ &&
|
|
226
|
+
(charCodeAt.call(url, start + 6) | 32) === 114 /* r */ &&
|
|
227
|
+
(charCodeAt.call(url, start + 7) | 32) === 105 /* i */ &&
|
|
228
|
+
(charCodeAt.call(url, start + 8) | 32) === 112 /* p */ &&
|
|
229
|
+
(charCodeAt.call(url, start + 9) | 32) === 116) /* t */
|
|
134
230
|
) {
|
|
135
231
|
return null;
|
|
136
232
|
}
|
|
137
233
|
|
|
138
|
-
while (end > start + 1 &&
|
|
234
|
+
while (end > start + 1 && charCodeAt.call(url, end - 1) <= 32) {
|
|
139
235
|
end -= 1;
|
|
140
236
|
}
|
|
141
237
|
|
|
142
238
|
if (
|
|
143
|
-
(
|
|
144
|
-
|
|
145
|
-
(
|
|
146
|
-
|
|
239
|
+
(charCodeAt.call(url, start) === 47 /* '/' */ ||
|
|
240
|
+
charCodeAt.call(url, start) === 92) /* '\' */ &&
|
|
241
|
+
(charCodeAt.call(url, start + 1) === 47 /* '/' */ ||
|
|
242
|
+
charCodeAt.call(url, start + 1) === 92) /* '\' */
|
|
147
243
|
) {
|
|
148
|
-
// Scheme-relative reference ("//host/path").
|
|
244
|
+
// Scheme-relative reference ("//host/path"). Relative to a web page, a
|
|
245
|
+
// special URL, browsers skip any further slashes ("///host").
|
|
149
246
|
start += 2;
|
|
247
|
+
while (
|
|
248
|
+
charCodeAt.call(url, start) === 47 /* '/' */ ||
|
|
249
|
+
charCodeAt.call(url, start) === 92 /* '\' */
|
|
250
|
+
) {
|
|
251
|
+
start += 1;
|
|
252
|
+
}
|
|
150
253
|
} else {
|
|
151
|
-
|
|
254
|
+
// The scheme ends at the first ":/". Most URLs start with "http://" or
|
|
255
|
+
// "https://", so check for those before searching.
|
|
256
|
+
let indexOfProtocol =
|
|
257
|
+
charCodeAt.call(url, start + 4) === 115 /* 's' */
|
|
258
|
+
? start + 5
|
|
259
|
+
: start + 4;
|
|
260
|
+
if (
|
|
261
|
+
charCodeAt.call(url, start) !== 104 /* 'h' */ ||
|
|
262
|
+
charCodeAt.call(url, start + 1) !== 116 /* 't' */ ||
|
|
263
|
+
charCodeAt.call(url, start + 2) !== 116 /* 't' */ ||
|
|
264
|
+
charCodeAt.call(url, start + 3) !== 112 /* 'p' */ ||
|
|
265
|
+
charCodeAt.call(url, indexOfProtocol) !== 58 /* ':' */ ||
|
|
266
|
+
charCodeAt.call(url, indexOfProtocol + 1) !== 47 /* '/' */
|
|
267
|
+
) {
|
|
268
|
+
indexOfProtocol = url.indexOf(':/', start);
|
|
269
|
+
}
|
|
152
270
|
if (indexOfProtocol !== -1) {
|
|
153
271
|
// "scheme://…". Classify the scheme, then position `start` at the host.
|
|
154
272
|
const special = getSpecialScheme(url, start, indexOfProtocol);
|
|
@@ -157,8 +275,8 @@ export default function extractHostname(
|
|
|
157
275
|
// (special-authority-(ignore-)slashes states; '\' acts as '/').
|
|
158
276
|
start = indexOfProtocol + 2;
|
|
159
277
|
while (
|
|
160
|
-
|
|
161
|
-
|
|
278
|
+
charCodeAt.call(url, start) === 47 /* '/' */ ||
|
|
279
|
+
charCodeAt.call(url, start) === 92 /* '\' */
|
|
162
280
|
) {
|
|
163
281
|
start += 1;
|
|
164
282
|
}
|
|
@@ -168,27 +286,26 @@ export default function extractHostname(
|
|
|
168
286
|
start = indexOfProtocol + 1;
|
|
169
287
|
let slashes = 0;
|
|
170
288
|
while (
|
|
171
|
-
(
|
|
289
|
+
(charCodeAt.call(url, start) === 47 ||
|
|
290
|
+
charCodeAt.call(url, start) === 92) &&
|
|
172
291
|
slashes < 2
|
|
173
292
|
) {
|
|
174
293
|
start += 1;
|
|
175
294
|
slashes += 1;
|
|
176
295
|
}
|
|
177
296
|
if (slashes < 2) {
|
|
178
|
-
return
|
|
297
|
+
return noHostname(url, urlIsValidHostname, validate);
|
|
179
298
|
}
|
|
299
|
+
} else if (!isScheme(url, start, start + 1)) {
|
|
300
|
+
// No scheme: one starts with an ASCII letter ("1a://x" is no URL for
|
|
301
|
+
// browsers). Read the input as an authority, as below.
|
|
302
|
+
indexOfProtocol = -1;
|
|
180
303
|
} else {
|
|
181
304
|
// Unknown scheme: validate the WHATWG scheme grammar [A-Za-z0-9+.-];
|
|
182
305
|
// a control char means it was split by a tab/newline (strip + re-parse).
|
|
183
306
|
for (let i = start; i < indexOfProtocol; i += 1) {
|
|
184
|
-
const code =
|
|
185
|
-
if (!(
|
|
186
|
-
((code | 32) >= 97 && (code | 32) <= 122) || // [a-zA-Z] (fold letters only)
|
|
187
|
-
(code >= 48 && code <= 57) || // [0, 9]
|
|
188
|
-
code === 46 || // '.'
|
|
189
|
-
code === 45 || // '-'
|
|
190
|
-
code === 43 // '+'
|
|
191
|
-
)) {
|
|
307
|
+
const code = charCodeAt.call(url, i);
|
|
308
|
+
if (!isSchemeChar(code)) {
|
|
192
309
|
if (
|
|
193
310
|
code === 9 /* tab */ ||
|
|
194
311
|
code === 10 /* LF */ ||
|
|
@@ -200,18 +317,29 @@ export default function extractHostname(
|
|
|
200
317
|
validate,
|
|
201
318
|
);
|
|
202
319
|
}
|
|
203
|
-
// A
|
|
204
|
-
//
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
320
|
+
// A scheme ending at a first ':' (other than a slash-less special
|
|
321
|
+
// one) makes this ':/' part of an opaque path ("foo:bar://x", no
|
|
322
|
+
// host), unless a '/', '\', '?' or '#' first puts it in a path,
|
|
323
|
+
// query or fragment ("a.com/r?u=https://x"). Otherwise no scheme
|
|
324
|
+
// ends here: this ':/' is an empty port, after a slash-less
|
|
325
|
+
// special scheme ("http:a.com:/x") or an authority
|
|
326
|
+
// ("user@host:/x"). Read the input as such below.
|
|
327
|
+
if (
|
|
328
|
+
code === 58 /* ':' */ &&
|
|
329
|
+
getSpecialScheme(url, start, i) !== 1
|
|
330
|
+
) {
|
|
331
|
+
for (let j = i; j < indexOfProtocol; j += 1) {
|
|
332
|
+
const c = charCodeAt.call(url, j);
|
|
333
|
+
if (c === 47 || c === 92 || c === 63 || c === 35) {
|
|
334
|
+
indexOfProtocol = -1;
|
|
335
|
+
break;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
if (indexOfProtocol !== -1) {
|
|
339
|
+
return noHostname(url, urlIsValidHostname, validate);
|
|
210
340
|
}
|
|
211
341
|
}
|
|
212
|
-
|
|
213
|
-
return null;
|
|
214
|
-
}
|
|
342
|
+
indexOfProtocol = -1;
|
|
215
343
|
break;
|
|
216
344
|
}
|
|
217
345
|
}
|
|
@@ -219,17 +347,20 @@ export default function extractHostname(
|
|
|
219
347
|
// A non-special scheme has an authority only after "//" (else it is
|
|
220
348
|
// an opaque path with no host). `indexOf(':/')` already gave the
|
|
221
349
|
// first '/'.
|
|
222
|
-
if (
|
|
350
|
+
if (charCodeAt.call(url, indexOfProtocol + 2) === 47 /* '/' */) {
|
|
223
351
|
isSpecial = false;
|
|
224
352
|
start = indexOfProtocol + 3;
|
|
225
353
|
} else {
|
|
226
|
-
return
|
|
354
|
+
return noHostname(url, urlIsValidHostname, validate);
|
|
227
355
|
}
|
|
228
356
|
}
|
|
229
357
|
}
|
|
230
358
|
}
|
|
231
359
|
|
|
232
|
-
if (
|
|
360
|
+
if (
|
|
361
|
+
indexOfProtocol === -1 &&
|
|
362
|
+
charCodeAt.call(url, start) !== 91 /* '[' */
|
|
363
|
+
) {
|
|
233
364
|
// Cold path: no "scheme://" prefix, and not a bare IPv6 literal (whose
|
|
234
365
|
// first ':' would otherwise look like a scheme separator; "[…]" falls
|
|
235
366
|
// through to the ipv6 handling below). May be a bare host, a host:port,
|
|
@@ -237,7 +368,7 @@ export default function extractHostname(
|
|
|
237
368
|
// URI ("mailto:", "tel:", "urn:…").
|
|
238
369
|
let indexOfColon = -1;
|
|
239
370
|
for (let i = start; i < end; i += 1) {
|
|
240
|
-
const code =
|
|
371
|
+
const code = charCodeAt.call(url, i);
|
|
241
372
|
if (code === 9 || code === 10 || code === 13) {
|
|
242
373
|
return extractHostname(
|
|
243
374
|
url.replace(CONTROL_CHARS, ''),
|
|
@@ -257,9 +388,15 @@ export default function extractHostname(
|
|
|
257
388
|
if (indexOfColon !== -1) {
|
|
258
389
|
// An '@' before the next delimiter => the ':' is userinfo, not a
|
|
259
390
|
// scheme ("user:pass@host", "mailto:a@b"): keep the whole authority.
|
|
391
|
+
// Not after "file:", which has no userinfo and no host without "//".
|
|
392
|
+
const special = getSpecialScheme(url, start, indexOfColon);
|
|
260
393
|
let hasIdentifier = false;
|
|
261
|
-
for (
|
|
262
|
-
|
|
394
|
+
for (
|
|
395
|
+
let i = special === 2 ? end : indexOfColon + 1;
|
|
396
|
+
i < end;
|
|
397
|
+
i += 1
|
|
398
|
+
) {
|
|
399
|
+
const code = charCodeAt.call(url, i);
|
|
263
400
|
if (code === 47 || code === 92 || code === 63 || code === 35) {
|
|
264
401
|
break;
|
|
265
402
|
}
|
|
@@ -272,10 +409,11 @@ export default function extractHostname(
|
|
|
272
409
|
if (!hasIdentifier) {
|
|
273
410
|
// All-digits after ':' => a bare "host:port" (tldts accepts
|
|
274
411
|
// hostnames too); keep `start` and let the port handling trim it.
|
|
275
|
-
|
|
412
|
+
// Not after a special scheme though: "http:8080" is a URL.
|
|
413
|
+
let allDigits = special === 0;
|
|
276
414
|
let i = indexOfColon + 1;
|
|
277
415
|
for (; i < end; i += 1) {
|
|
278
|
-
const code =
|
|
416
|
+
const code = charCodeAt.call(url, i);
|
|
279
417
|
if (code === 47 || code === 92 || code === 63 || code === 35) {
|
|
280
418
|
break;
|
|
281
419
|
}
|
|
@@ -289,7 +427,6 @@ export default function extractHostname(
|
|
|
289
427
|
}
|
|
290
428
|
|
|
291
429
|
if (!allDigits) {
|
|
292
|
-
const special = getSpecialScheme(url, start, indexOfColon);
|
|
293
430
|
if (special === 0) {
|
|
294
431
|
// No "scheme://" on the cold path and not a special scheme.
|
|
295
432
|
// A second ':' before the host's end marks a bare, unbracketed
|
|
@@ -298,7 +435,7 @@ export default function extractHostname(
|
|
|
298
435
|
// with no host ("mailto:x", "foo:bar").
|
|
299
436
|
let isBareIpv6 = false;
|
|
300
437
|
for (let j = indexOfColon + 1; j < end; j += 1) {
|
|
301
|
-
const code =
|
|
438
|
+
const code = charCodeAt.call(url, j);
|
|
302
439
|
if (
|
|
303
440
|
code === 47 ||
|
|
304
441
|
code === 92 ||
|
|
@@ -312,8 +449,20 @@ export default function extractHostname(
|
|
|
312
449
|
break;
|
|
313
450
|
}
|
|
314
451
|
}
|
|
315
|
-
|
|
316
|
-
|
|
452
|
+
// Not a scheme either ("@host:", "a_b:/x"): an authority, if
|
|
453
|
+
// its ':' starts an empty port (digits were read above).
|
|
454
|
+
// Browsers have no host for "a_b:x".
|
|
455
|
+
const next = charCodeAt.call(url, i);
|
|
456
|
+
if (
|
|
457
|
+
!isBareIpv6 &&
|
|
458
|
+
(isScheme(url, start, indexOfColon) ||
|
|
459
|
+
(i < end &&
|
|
460
|
+
next !== 47 &&
|
|
461
|
+
next !== 92 &&
|
|
462
|
+
next !== 63 &&
|
|
463
|
+
next !== 35))
|
|
464
|
+
) {
|
|
465
|
+
return noHostname(url, urlIsValidHostname, validate);
|
|
317
466
|
}
|
|
318
467
|
} else {
|
|
319
468
|
start = indexOfColon + 1;
|
|
@@ -321,20 +470,20 @@ export default function extractHostname(
|
|
|
321
470
|
// file (e.g. "file:\\host"): host only between "//" and next slash.
|
|
322
471
|
let slashes = 0;
|
|
323
472
|
while (
|
|
324
|
-
(
|
|
325
|
-
|
|
473
|
+
(charCodeAt.call(url, start) === 47 ||
|
|
474
|
+
charCodeAt.call(url, start) === 92) &&
|
|
326
475
|
slashes < 2
|
|
327
476
|
) {
|
|
328
477
|
start += 1;
|
|
329
478
|
slashes += 1;
|
|
330
479
|
}
|
|
331
480
|
if (slashes < 2) {
|
|
332
|
-
return
|
|
481
|
+
return noHostname(url, urlIsValidHostname, validate);
|
|
333
482
|
}
|
|
334
483
|
} else {
|
|
335
484
|
while (
|
|
336
|
-
|
|
337
|
-
|
|
485
|
+
charCodeAt.call(url, start) === 47 ||
|
|
486
|
+
charCodeAt.call(url, start) === 92
|
|
338
487
|
) {
|
|
339
488
|
start += 1;
|
|
340
489
|
}
|
|
@@ -347,8 +496,8 @@ export default function extractHostname(
|
|
|
347
496
|
}
|
|
348
497
|
|
|
349
498
|
// Find the host's end: first '/', '?' or '#' (and '\' for special URLs,
|
|
350
|
-
// which WHATWG treats like '/'). Track the last '@'
|
|
351
|
-
//
|
|
499
|
+
// which WHATWG treats like '/'). Track the last '@' and ':' for userinfo
|
|
500
|
+
// and port, plus the first ':' of the host (reset at each
|
|
352
501
|
// '@') to tell a bare IPv6 (>= 2 colons) from a host:port (exactly one);
|
|
353
502
|
// flag uppercase and a stray tab/newline. The loop is split on `code < 64`
|
|
354
503
|
// so common host characters take fewer comparisons.
|
|
@@ -359,7 +508,6 @@ export default function extractHostname(
|
|
|
359
508
|
// brackets, control or trailing dot); those cases clear it / are rejected by
|
|
360
509
|
// the guard below, falling back to `isValidHostname`.
|
|
361
510
|
let indexOfIdentifier = -1;
|
|
362
|
-
let indexOfClosingBracket = -1;
|
|
363
511
|
let indexOfPort = -1;
|
|
364
512
|
let indexOfFirstColon = -1;
|
|
365
513
|
let hasControl = false;
|
|
@@ -368,7 +516,7 @@ export default function extractHostname(
|
|
|
368
516
|
let vLastCode = -1;
|
|
369
517
|
if (validate && start < end) {
|
|
370
518
|
// First-char rule: must be a valid host char, '.', or '_' (NOT '-').
|
|
371
|
-
const c0 =
|
|
519
|
+
const c0 = charCodeAt.call(url, start);
|
|
372
520
|
if (
|
|
373
521
|
// Keep these parens: they scope /*@__INLINE__*/ for terser.
|
|
374
522
|
// prettier-ignore
|
|
@@ -385,7 +533,7 @@ export default function extractHostname(
|
|
|
385
533
|
}
|
|
386
534
|
}
|
|
387
535
|
for (let i = start; i < end; i += 1) {
|
|
388
|
-
const code: number =
|
|
536
|
+
const code: number = charCodeAt.call(url, i);
|
|
389
537
|
if (code < 64) {
|
|
390
538
|
if (code === 47 || code === 35 || code === 63) {
|
|
391
539
|
end = i;
|
|
@@ -420,8 +568,6 @@ export default function extractHostname(
|
|
|
420
568
|
} else if (code === 64 /* '@' */) {
|
|
421
569
|
indexOfIdentifier = i;
|
|
422
570
|
indexOfFirstColon = -1; // colons before '@' are userinfo, not the host
|
|
423
|
-
} else if (code === 93 /* ']' */) {
|
|
424
|
-
indexOfClosingBracket = i;
|
|
425
571
|
} else if (code >= 65 && code <= 90) {
|
|
426
572
|
hasUpper = true;
|
|
427
573
|
} else if (validate && !(/*@__INLINE__*/ isValidHostnameChar(code))) {
|
|
@@ -451,10 +597,19 @@ export default function extractHostname(
|
|
|
451
597
|
start = indexOfIdentifier + 1;
|
|
452
598
|
}
|
|
453
599
|
|
|
454
|
-
if (
|
|
455
|
-
// ipv6 address: return what is between the brackets, or null if
|
|
456
|
-
|
|
457
|
-
|
|
600
|
+
if (charCodeAt.call(url, start) === 91 /* '[' */) {
|
|
601
|
+
// ipv6 address: return what is between the brackets, or null if they are
|
|
602
|
+
// unclosed or followed by anything but a port ("[::1]x"). `parseImpl`
|
|
603
|
+
// checks that they hold an IPv6 address (see `extractedBracketedHost`).
|
|
604
|
+
const closingBracket = url.indexOf(']', start);
|
|
605
|
+
if (
|
|
606
|
+
closingBracket !== -1 &&
|
|
607
|
+
closingBracket < end &&
|
|
608
|
+
(closingBracket + 1 === end ||
|
|
609
|
+
charCodeAt.call(url, closingBracket + 1) === 58) /* ':' */
|
|
610
|
+
) {
|
|
611
|
+
extractedBracketedHost = true;
|
|
612
|
+
return url.slice(start + 1, closingBracket).toLowerCase();
|
|
458
613
|
}
|
|
459
614
|
return null;
|
|
460
615
|
} else if (
|
|
@@ -469,10 +624,11 @@ export default function extractHostname(
|
|
|
469
624
|
end = indexOfPort; // trim ':port'
|
|
470
625
|
}
|
|
471
626
|
|
|
472
|
-
// Empty authority ("http://", "file:///path", "//")
|
|
473
|
-
//
|
|
627
|
+
// Empty authority ("http://", "file:///path", "//"), unless a tab or
|
|
628
|
+
// newline hides one ("/\t/host"); only reachable here via extraction — a
|
|
629
|
+
// bare valid hostname never lands here.
|
|
474
630
|
if (start >= end) {
|
|
475
|
-
return
|
|
631
|
+
return noHostname(url, urlIsValidHostname, validate);
|
|
476
632
|
}
|
|
477
633
|
|
|
478
634
|
// Publish the inline-validation verdict — but only for a "simple" authority,
|
|
@@ -491,24 +647,30 @@ export default function extractHostname(
|
|
|
491
647
|
vValid &&
|
|
492
648
|
indexOfIdentifier === -1 &&
|
|
493
649
|
indexOfPort === -1 &&
|
|
494
|
-
|
|
495
|
-
url.charCodeAt(end - 1) !== 46 /* no trailing dot */ &&
|
|
650
|
+
charCodeAt.call(url, end - 1) !== 46 /* no trailing dot */ &&
|
|
496
651
|
end - start <= 255 && // total length
|
|
497
652
|
end - vLastDot - 1 <= 63 && // last label length
|
|
498
653
|
vLastCode !== 45 /* last char not '-' */
|
|
499
654
|
) {
|
|
500
655
|
extractedHostnameValidated = true;
|
|
501
656
|
}
|
|
657
|
+
failedValidation = validate && !vValid;
|
|
502
658
|
}
|
|
503
659
|
|
|
504
660
|
// Trim trailing dots
|
|
505
|
-
while (end > start + 1 &&
|
|
661
|
+
while (end > start + 1 && charCodeAt.call(url, end - 1) === 46 /* '.' */) {
|
|
506
662
|
end -= 1;
|
|
507
663
|
}
|
|
508
664
|
|
|
509
665
|
const hostname: string =
|
|
510
666
|
start !== 0 || end !== url.length ? url.slice(start, end) : url;
|
|
511
667
|
|
|
668
|
+
// Browsers percent-decode the host of a special URL ("%74racker.com" is
|
|
669
|
+
// tracker.com). Only when validating: otherwise the host is kept as written.
|
|
670
|
+
if (failedValidation && isSpecial && hostname.includes('%')) {
|
|
671
|
+
return decodeHostname(hostname);
|
|
672
|
+
}
|
|
673
|
+
|
|
512
674
|
if (hasUpper) {
|
|
513
675
|
return hostname.toLowerCase();
|
|
514
676
|
}
|
package/src/factory.ts
CHANGED
|
@@ -8,6 +8,7 @@ import getDomain from './domain';
|
|
|
8
8
|
import getDomainWithoutSuffix from './domain-without-suffix';
|
|
9
9
|
import extractHostname, {
|
|
10
10
|
extractedHostnameValidated,
|
|
11
|
+
extractedBracketedHost,
|
|
11
12
|
} from './extract-hostname';
|
|
12
13
|
import isIp from './is-ip';
|
|
13
14
|
import isSpecialUse from './is-special-use';
|
|
@@ -20,9 +21,10 @@ import getSubdomain from './subdomain';
|
|
|
20
21
|
export interface IResult {
|
|
21
22
|
// `hostname` is either a registered name (including but not limited to a
|
|
22
23
|
// hostname), or an IP address, directly extracted from the input URL. IPv4
|
|
23
|
-
// addresses are
|
|
24
|
-
// surrounding brackets; both bracketed (in URLs, e.g.
|
|
25
|
-
// bare unbracketed (e.g. `2a01:e35::1`) IPv6 literals
|
|
24
|
+
// addresses are not canonicalized (`0x7f.1` stays `0x7f.1`). IPv6 is
|
|
25
|
+
// returned without its surrounding brackets; both bracketed (in URLs, e.g.
|
|
26
|
+
// `http://[::1]/`) and bare unbracketed (e.g. `2a01:e35::1`) IPv6 literals
|
|
27
|
+
// are accepted.
|
|
26
28
|
hostname: string | null;
|
|
27
29
|
|
|
28
30
|
// Is `hostname` an IP? (IPv4 or IPv6)
|
|
@@ -137,12 +139,42 @@ export function parseImpl(
|
|
|
137
139
|
result.hostname = extractHostname(url, false, options.validateHostname);
|
|
138
140
|
}
|
|
139
141
|
|
|
142
|
+
// A host in brackets is an IPv6 address, or the URL is invalid, as browsers
|
|
143
|
+
// read it: "http://[1.2.3.4]/" and "http://[a.com]/" have no hostname. An
|
|
144
|
+
// IPv4 address has no ':', every IPv6 address has one.
|
|
145
|
+
if (
|
|
146
|
+
options.extractHostname &&
|
|
147
|
+
extractedBracketedHost &&
|
|
148
|
+
result.hostname !== null
|
|
149
|
+
) {
|
|
150
|
+
if (isIp(result.hostname) !== true || !result.hostname.includes(':')) {
|
|
151
|
+
result.hostname = null;
|
|
152
|
+
return result;
|
|
153
|
+
}
|
|
154
|
+
if (options.detectIp) {
|
|
155
|
+
result.isIp = true;
|
|
156
|
+
return result;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
140
160
|
// Check if `hostname` is a valid ip address
|
|
141
161
|
if (options.detectIp && result.hostname !== null) {
|
|
142
|
-
|
|
162
|
+
const isIpAddress = isIp(result.hostname);
|
|
163
|
+
result.isIp = isIpAddress === true;
|
|
143
164
|
if (result.isIp) {
|
|
144
165
|
return result;
|
|
145
166
|
}
|
|
167
|
+
|
|
168
|
+
// `null`: the host ends in a number, as an IPv4 address, without being
|
|
169
|
+
// one. It is not a valid host then, and never a domain name.
|
|
170
|
+
if (
|
|
171
|
+
isIpAddress === null &&
|
|
172
|
+
options.validateHostname &&
|
|
173
|
+
options.extractHostname
|
|
174
|
+
) {
|
|
175
|
+
result.hostname = null;
|
|
176
|
+
return result;
|
|
177
|
+
}
|
|
146
178
|
}
|
|
147
179
|
|
|
148
180
|
// Perform hostname validation if enabled. If hostname is not valid, no need to
|