@oozcitak/url 0.0.7 → 1.0.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.
Files changed (47) hide show
  1. package/README.md +14 -0
  2. package/lib/URLAlgorithm.d.ts +267 -321
  3. package/lib/URLAlgorithm.js +2217 -2184
  4. package/lib/URLAlgorithm.js.map +1 -1
  5. package/lib/URLImpl.d.ts +22 -14
  6. package/lib/URLImpl.js +24 -25
  7. package/lib/URLImpl.js.map +1 -1
  8. package/lib/URLSearchParamsImpl.d.ts +0 -2
  9. package/lib/URLSearchParamsImpl.js +4 -5
  10. package/lib/URLSearchParamsImpl.js.map +1 -1
  11. package/lib/index.d.ts +2 -4
  12. package/lib/index.js +4 -15
  13. package/lib/index.js.map +1 -1
  14. package/package.json +5 -5
  15. package/lib/IDNAMappingTable.d.ts +0 -41
  16. package/lib/IDNAMappingTable.js +0 -8688
  17. package/lib/IDNAMappingTable.js.map +0 -1
  18. package/lib/TreeMap.d.ts +0 -39
  19. package/lib/TreeMap.js +0 -105
  20. package/lib/TreeMap.js.map +0 -1
  21. package/lib/algorithm/URLAlgorithm.d.ts +0 -321
  22. package/lib/algorithm/URLAlgorithm.js +0 -2552
  23. package/lib/algorithm/URLAlgorithm.js.map +0 -1
  24. package/lib/algorithm/index.d.ts +0 -1
  25. package/lib/algorithm/index.js +0 -5
  26. package/lib/algorithm/index.js.map +0 -1
  27. package/lib/interfacesInternal.d.ts +0 -22
  28. package/lib/interfacesInternal.js +0 -3
  29. package/lib/interfacesInternal.js.map +0 -1
  30. package/lib/url/URLImpl.d.ts +0 -45
  31. package/lib/url/URLImpl.js +0 -329
  32. package/lib/url/URLImpl.js.map +0 -1
  33. package/lib/url/URLSearchParamsImpl.d.ts +0 -40
  34. package/lib/url/URLSearchParamsImpl.js +0 -184
  35. package/lib/url/URLSearchParamsImpl.js.map +0 -1
  36. package/lib/url/index.d.ts +0 -4
  37. package/lib/url/index.js +0 -16
  38. package/lib/url/index.js.map +0 -1
  39. package/lib/url/interfaces.d.ts +0 -195
  40. package/lib/url/interfaces.js +0 -31
  41. package/lib/url/interfaces.js.map +0 -1
  42. package/lib/util/StringWalker.d.ts +0 -48
  43. package/lib/util/StringWalker.js +0 -92
  44. package/lib/util/StringWalker.js.map +0 -1
  45. package/lib/util/index.d.ts +0 -59
  46. package/lib/util/index.js +0 -170
  47. package/lib/util/index.js.map +0 -1
@@ -1,2552 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const util_1 = require("../util");
4
- const interfaces_1 = require("../url/interfaces");
5
- const infra_1 = require("@oozcitak/infra");
6
- const uts46_1 = require("@oozcitak/uts46");
7
- /**
8
- * Represents algorithms to manipulate URLs.
9
- */
10
- class URLAlgorithm {
11
- /**
12
- * Initializes a new `URLAlgorithm`.
13
- *
14
- * @param validationErrorCallback - a callback function to be called when a
15
- * validation error occurs
16
- */
17
- constructor(validationErrorCallback) {
18
- /**
19
- * Default ports for a special URL scheme.
20
- */
21
- this._defaultPorts = {
22
- "ftp": 21,
23
- "file": null,
24
- "http": 80,
25
- "https": 443,
26
- "ws": 80,
27
- "wss": 443
28
- };
29
- /**
30
- * The C0 control percent-encode set are the C0 controls and all code points
31
- * greater than U+007E (~).
32
- */
33
- this._c0ControlPercentEncodeSet = /[\0-\x1F\x7F-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]/;
34
- /**
35
- * The fragment percent-encode set is the C0 control percent-encode set and
36
- * U+0020 SPACE, U+0022 ("), U+003C (<), U+003E (>), and U+0060 (`).
37
- */
38
- this._fragmentPercentEncodeSet = /[ "<>`]|[\0-\x1F\x7F-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]/;
39
- /**
40
- * The path percent-encode set is the fragment percent-encode set and
41
- * U+0023 (#), U+003F (?), U+007B ({), and U+007D (}).
42
- */
43
- this._pathPercentEncodeSet = /[ "<>`#?{}]|[\0-\x1F\x7F-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]/;
44
- /**
45
- * The userinfo percent-encode set is the path percent-encode set and
46
- * U+002F (/), U+003A (:), U+003B (;), U+003D (=), U+0040 (@), U+005B ([),
47
- * U+005C (\), U+005D (]), U+005E (^), and U+007C (|).
48
- */
49
- this._userInfoPercentEncodeSet = /[ "<>`#?{}/:;=@\[\]\\\^\|]|[\0-\x1F\x7F-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]/;
50
- /**
51
- * The URL code points are ASCII alphanumeric, U+0021 (!), U+0024 ($),
52
- * U+0026 (&), U+0027 ('), U+0028 LEFT PARENTHESIS, U+0029 RIGHT PARENTHESIS,
53
- * U+002A (*), U+002B (+), U+002C (,), U+002D (-), U+002E (.), U+002F (/),
54
- * U+003A (:), U+003B (;), U+003D (=), U+003F (?), U+0040 (@), U+005F (_),
55
- * U+007E (~), and code points in the range U+00A0 to U+10FFFD, inclusive,
56
- * excluding surrogates and noncharacters.
57
- */
58
- this._urlCodePoints = /[0-9A-Za-z!\$&-\/:;=\?@_~\xA0-\uD7FF\uE000-\uFDCF\uFDF0-\uFFFD]|[\uD800-\uD83E\uD840-\uD87E\uD880-\uD8BE\uD8C0-\uD8FE\uD900-\uD93E\uD940-\uD97E\uD980-\uD9BE\uD9C0-\uD9FE\uDA00-\uDA3E\uDA40-\uDA7E\uDA80-\uDABE\uDAC0-\uDAFE\uDB00-\uDB3E\uDB40-\uDB7E\uDB80-\uDBBE\uDBC0-\uDBFE][\uDC00-\uDFFF]|[\uD83F\uD87F\uD8BF\uD8FF\uD93F\uD97F\uD9BF\uD9FF\uDA3F\uDA7F\uDABF\uDAFF\uDB3F\uDB7F\uDBBF\uDBFF][\uDC00-\uDFFD]/;
59
- /**
60
- * A forbidden host code point is U+0000 NULL, U+0009 TAB, U+000A LF,
61
- * U+000D CR, U+0020 SPACE, U+0023 (#), U+0025 (%), U+002F (/), U+003A (:),
62
- * U+003F (?), U+0040 (@), U+005B ([), U+005C (\), or U+005D (]).
63
- */
64
- this._forbiddenHostCodePoint = /[\0\t\f\r #%/:?@\[\\\]]/g;
65
- this._validationErrorCallback = validationErrorCallback;
66
- }
67
- /**
68
- * Generates a validation error.
69
- *
70
- * @param message - error message
71
- */
72
- validationError(message) {
73
- if (this._validationErrorCallback !== undefined) {
74
- this._validationErrorCallback.call(this, "Validation Error: " + message);
75
- }
76
- }
77
- /**
78
- * Creates a new URL.
79
- */
80
- newURL() {
81
- return {
82
- scheme: '',
83
- username: '',
84
- password: '',
85
- host: null,
86
- port: null,
87
- path: [],
88
- query: null,
89
- fragment: null,
90
- _cannotBeABaseURLFlag: false,
91
- _blobURLEntry: null
92
- };
93
- }
94
- /**
95
- * Determines if the scheme is a special scheme.
96
- *
97
- * @param scheme - a scheme
98
- */
99
- isSpecialScheme(scheme) {
100
- return (scheme in this._defaultPorts);
101
- }
102
- /**
103
- * Determines if the URL has a special scheme.
104
- *
105
- * @param url - an URL
106
- */
107
- isSpecial(url) {
108
- return this.isSpecialScheme(url.scheme);
109
- }
110
- /**
111
- * Returns the default port for a special scheme.
112
- *
113
- * @param scheme - a scheme
114
- */
115
- defaultPort(scheme) {
116
- return this._defaultPorts[scheme] || null;
117
- }
118
- /**
119
- * Determines if the URL has credentials.
120
- *
121
- * @param url - an URL
122
- */
123
- includesCredentials(url) {
124
- return url.username !== '' || url.password !== '';
125
- }
126
- /**
127
- * Determines if an URL cannot have credentials.
128
- *
129
- * @param url - an URL
130
- */
131
- cannotHaveAUsernamePasswordPort(url) {
132
- /**
133
- * A URL cannot have a username/password/port if its host is null or the
134
- * empty string, its cannot-be-a-base-URL flag is set, or its scheme is
135
- * "file".
136
- */
137
- return (url.host === null || url.host === "" || url._cannotBeABaseURLFlag ||
138
- url.scheme === "file");
139
- }
140
- /**
141
- * Serializes an URL into a string.
142
- *
143
- * @param url - an URL
144
- */
145
- urlSerializer(url, excludeFragmentFlag = false) {
146
- /**
147
- * 1. Let output be url’s scheme and U+003A (:) concatenated.
148
- */
149
- let output = url.scheme + ':';
150
- /**
151
- * 2. If url’s host is non-null:
152
- */
153
- if (url.host !== null) {
154
- /**
155
- * 2.1. Append "//" to output.
156
- */
157
- output += '//';
158
- /**
159
- * 2.2. If url includes credentials, then:
160
- */
161
- if (this.includesCredentials(url)) {
162
- /**
163
- * 2.2.1. Append url’s username to output.
164
- * 2.2.2. If url’s password is not the empty string, then append U+003A (:),
165
- * followed by url’s password, to output.
166
- * 2.2.3. Append U+0040 (@) to output.
167
- */
168
- output += url.username;
169
- if (url.password !== '') {
170
- output += ':' + url.password;
171
- }
172
- output += '@';
173
- }
174
- /**
175
- * 2.3. Append url’s host, serialized, to output.
176
- * 2.4. If url’s port is non-null, append U+003A (:) followed by url’s port,
177
- * serialized, to output.
178
- */
179
- output += this.hostSerializer(url.host);
180
- if (url.port !== null) {
181
- output += ':' + url.port;
182
- }
183
- }
184
- else if (url.host === null && url.scheme === "file") {
185
- /**
186
- * 3. Otherwise, if url’s host is null and url’s scheme is "file", append "//" to output.
187
- */
188
- output += '//';
189
- }
190
- /**
191
- * 4. If url’s cannot-be-a-base-URL flag is set, append url’s path[0] to
192
- * output.
193
- * 5. Otherwise, then for each string in url’s path, append U+002F (/)
194
- * followed by the string to output.
195
- */
196
- if (url._cannotBeABaseURLFlag) {
197
- output += url.path[0];
198
- }
199
- else {
200
- for (const str of url.path) {
201
- output += '/' + str;
202
- }
203
- }
204
- /**
205
- * 6. If url’s query is non-null, append U+003F (?), followed by url’s
206
- * query, to output.
207
- * 7. If the exclude fragment flag is unset and url’s fragment is non-null,
208
- * append U+0023 (#), followed by url’s fragment, to output.
209
- * 8. Return output.
210
- */
211
- if (url.query !== null) {
212
- output += '?' + url.query;
213
- }
214
- if (!excludeFragmentFlag && url.fragment !== null) {
215
- output += '#' + url.fragment;
216
- }
217
- return output;
218
- }
219
- /**
220
- * Serializes a host into a string.
221
- *
222
- * @param host - a host
223
- */
224
- hostSerializer(host) {
225
- /**
226
- * 1. If host is an IPv4 address, return the result of running the IPv4
227
- * serializer on host.
228
- * 2. Otherwise, if host is an IPv6 address, return U+005B ([), followed
229
- * by the result of running the IPv6 serializer on host, followed by
230
- * U+005D (]).
231
- * 3. Otherwise, host is a domain, opaque host, or empty host, return host.
232
- */
233
- if (util_1.isNumber(host)) {
234
- return this.iPv4Serializer(host);
235
- }
236
- else if (util_1.isArray(host)) {
237
- return '[' + this.iPv6Serializer(host) + ']';
238
- }
239
- else {
240
- return host;
241
- }
242
- }
243
- /**
244
- * Serializes an IPv4 address into a string.
245
- *
246
- * @param address - an IPv4 address
247
- */
248
- iPv4Serializer(address) {
249
- /**
250
- * 1. Let output be the empty string.
251
- * 2. Let n be the value of address.
252
- * 3. For each i in the range 1 to 4, inclusive:
253
- * 3.1. Prepend n % 256, serialized, to output.
254
- * 3.2. If i is not 4, then prepend U+002E (.) to output.
255
- * 3.3. Set n to floor(n / 256).
256
- * 4. Return output.
257
- */
258
- let output = "";
259
- let n = address;
260
- for (let i = 1; i <= 4; i++) {
261
- output = (n % 256).toString() + output;
262
- if (i !== 4) {
263
- output = '.' + output;
264
- }
265
- n = Math.floor(n / 256);
266
- }
267
- return output;
268
- }
269
- /**
270
- * Serializes an IPv6 address into a string.
271
- *
272
- * @param address - an IPv6 address represented as a list of eight numbers
273
- */
274
- iPv6Serializer(address) {
275
- /**
276
- * 1. Let output be the empty string.
277
- * 2. Let compress be an index to the first IPv6 piece in the first longest
278
- * sequences of address’s IPv6 pieces that are 0.
279
- * In 0:f:0:0:f:f:0:0 it would point to the second 0.
280
- * 3. If there is no sequence of address’s IPv6 pieces that are 0 that is
281
- * longer than 1, then set compress to null.
282
- */
283
- let output = "";
284
- let compress = null;
285
- let lastIndex = -1;
286
- let count = 0;
287
- let lastCount = 0;
288
- for (let i = 0; i < 8; i++) {
289
- if (address[i] !== 0)
290
- continue;
291
- count = 1;
292
- for (let j = i + 1; j < 8; j++) {
293
- if (address[j] !== 0)
294
- break;
295
- count++;
296
- continue;
297
- }
298
- if (count > lastCount) {
299
- lastCount = count;
300
- lastIndex = i;
301
- }
302
- }
303
- if (lastCount > 1)
304
- compress = lastIndex;
305
- /**
306
- * 4. Let ignore0 be false.
307
- * 5. For each pieceIndex in the range 0 to 7, inclusive:
308
- */
309
- let ignore0 = false;
310
- for (let pieceIndex = 0; pieceIndex < 8; pieceIndex++) {
311
- /**
312
- * 5.1. If ignore0 is true and address[pieceIndex] is 0, then continue.
313
- * 5.2. Otherwise, if ignore0 is true, set ignore0 to false.
314
- * 5.3. If compress is pieceIndex, then:
315
- */
316
- if (ignore0 && address[pieceIndex] === 0)
317
- continue;
318
- if (ignore0)
319
- ignore0 = false;
320
- if (compress === pieceIndex) {
321
- /**
322
- * 5.3.1. Let separator be "::" if pieceIndex is 0, and U+003A (:) otherwise.
323
- * 5.3.2. Append separator to output.
324
- * 5.3.3. Set ignore0 to true and continue.
325
- */
326
- output += (pieceIndex === 0 ? '::' : ':');
327
- ignore0 = true;
328
- continue;
329
- }
330
- /**
331
- * 5.4. Append address[pieceIndex], represented as the shortest possible
332
- * lowercase hexadecimal number, to output.
333
- * 5.5. If pieceIndex is not 7, then append U+003A (:) to output.
334
- */
335
- output += address[pieceIndex].toString(16);
336
- if (pieceIndex !== 7)
337
- output += ':';
338
- }
339
- /**
340
- * 6. Return output.
341
- */
342
- return output;
343
- }
344
- /**
345
- * Parses an URL string.
346
- *
347
- * @param input - input string
348
- * @param baseURL - base URL
349
- * @param encodingOverride - encoding override
350
- */
351
- urlParser(input, baseURL, encodingOverride) {
352
- /**
353
- * 1. Let url be the result of running the basic URL parser on input with
354
- * base, and encoding override as provided.
355
- * 2. If url is failure, return failure.
356
- * 3. If url’s scheme is not "blob", return url.
357
- * 4. Set url’s blob URL entry to the result of resolving the blob URL url,
358
- * if that did not return failure, and null otherwise.
359
- * 5. Return url.
360
- */
361
- const url = this.basicURLParser(input, baseURL, encodingOverride);
362
- if (url === null)
363
- return null;
364
- if (url.scheme !== "blob")
365
- return url;
366
- const entry = this.resolveABlobURL(url);
367
- if (entry !== null) {
368
- url._blobURLEntry = entry;
369
- }
370
- else {
371
- url._blobURLEntry = null;
372
- }
373
- return url;
374
- }
375
- /**
376
- * Parses an URL string.
377
- *
378
- * @param input - input string
379
- * @param baseURL - base URL
380
- * @param encodingOverride - encoding override
381
- */
382
- basicURLParser(input, baseURL, encodingOverride, url, stateOverride) {
383
- /**
384
- * 1. If url is not given:
385
- * 1.1. Set url to a new URL.
386
- * 1.2. If input contains any leading or trailing C0 control or space,
387
- * validation error.
388
- * 1.3. Remove any leading and trailing C0 control or space from input.
389
- */
390
- if (url === undefined) {
391
- url = this.newURL();
392
- // leading
393
- const leadingControlOrSpace = /^[\u0000-\u001F\u0020]+/;
394
- const trailingControlOrSpace = /[\u0000-\u001F\u0020]+$/;
395
- if (leadingControlOrSpace.test(input) || trailingControlOrSpace.test(input)) {
396
- this.validationError("Input string contains leading or trailing control characters or space.");
397
- }
398
- input = input.replace(leadingControlOrSpace, '');
399
- input = input.replace(trailingControlOrSpace, '');
400
- }
401
- /**
402
- * 2. If input contains any ASCII tab or newline, validation error.
403
- * 3. Remove all ASCII tab or newline from input.
404
- */
405
- const tabOrNewline = /[\u0009\u000A\u000D]/g;
406
- if (tabOrNewline.test(input)) {
407
- this.validationError("Input string contains tab or newline characters.");
408
- }
409
- input = input.replace(tabOrNewline, '');
410
- /**
411
- * 4. Let state be state override if given, or scheme start state otherwise.
412
- * 5. If base is not given, set it to null.
413
- * 6. Let encoding be UTF-8.
414
- * 7. If encoding override is given, set encoding to the result of getting
415
- * an output encoding from encoding override.
416
- */
417
- let state = (stateOverride === undefined ? interfaces_1.ParserState.SchemeStart : stateOverride);
418
- if (baseURL === undefined)
419
- baseURL = null;
420
- let encoding = (encodingOverride === undefined ||
421
- encodingOverride === "replacement" || encodingOverride === "UTF-16BE" ||
422
- encodingOverride === "UTF-16LE" ? "UTF-8" : encodingOverride);
423
- /**
424
- * 8. Let buffer be the empty string.
425
- * 9. Let the @ flag, [] flag, and passwordTokenSeenFlag be unset.
426
- * 10. Let pointer be a pointer to first code point in input.
427
- */
428
- let buffer = "";
429
- let atFlag = false;
430
- let arrayFlag = false;
431
- let passwordTokenSeenFlag = false;
432
- const EOF = "";
433
- const walker = new util_1.StringWalker(input);
434
- /**
435
- * 11. Keep running the following state machine by switching on state. If
436
- * after a run pointer points to the EOF code point, go to the next step.
437
- * Otherwise, increase pointer by one and continue with the state machine.
438
- */
439
- while (true) {
440
- switch (state) {
441
- case interfaces_1.ParserState.SchemeStart:
442
- /**
443
- * 1. If c is an ASCII alpha, append c, lowercased, to buffer, and set
444
- * state to scheme state.
445
- * 2. Otherwise, if state override is not given, set state to no scheme
446
- * state, and decrease pointer by one.
447
- * 3. Otherwise, validation error, return failure.
448
- */
449
- if (infra_1.codePoint.ASCIIAlpha.test(walker.c())) {
450
- buffer += walker.c().toLowerCase();
451
- state = interfaces_1.ParserState.Scheme;
452
- }
453
- else if (stateOverride === undefined) {
454
- state = interfaces_1.ParserState.NoScheme;
455
- walker.pointer--;
456
- }
457
- else {
458
- this.validationError("Invalid scheme start character.");
459
- return null;
460
- }
461
- break;
462
- case interfaces_1.ParserState.Scheme:
463
- /**
464
- * 1. If c is an ASCII alphanumeric, U+002B (+), U+002D (-), or U+002E
465
- * (.), append c, lowercased, to buffer.
466
- */
467
- if (infra_1.codePoint.ASCIIAlphanumeric.test(walker.c()) ||
468
- walker.c() === '+' || walker.c() === '-' || walker.c() === '.') {
469
- buffer += walker.c().toLowerCase();
470
- }
471
- else if (walker.c() === ':') {
472
- /**
473
- * 2. Otherwise, if c is U+003A (:), then:
474
- * 2.1. If state override is given, then:
475
- * 2.1.1. If url’s scheme is a special scheme and buffer is not a
476
- * special scheme, then return.
477
- * 2.1.2. If url’s scheme is not a special scheme and buffer is a
478
- * special scheme, then return.
479
- * 2.1.3. If url includes credentials or has a non-null port, and
480
- * buffer is "file", then return.
481
- * 2.1.4. If url’s scheme is "file" and its host is an empty host or
482
- * null, then return.
483
- */
484
- if (stateOverride !== undefined) {
485
- if (this.isSpecialScheme(url.scheme) && !this.isSpecialScheme(buffer))
486
- return url;
487
- if (!this.isSpecialScheme(url.scheme) && this.isSpecialScheme(buffer))
488
- return url;
489
- if ((this.includesCredentials(url) || url.port !== null) && buffer === "file")
490
- return url;
491
- if (url.scheme === "file" && (url.host === "" || url.host === null))
492
- return url;
493
- }
494
- /**
495
- * 2.2. Set url’s scheme to buffer.
496
- */
497
- url.scheme = buffer;
498
- /**
499
- * 2.3. If state override is given, then:
500
- * 2.3.1. If url’s port is url’s scheme’s default port, then set
501
- * url’s port to null.
502
- * 2.3.2. Return.
503
- */
504
- if (stateOverride !== undefined) {
505
- if (url.port === this.defaultPort(url.scheme)) {
506
- url.port = null;
507
- }
508
- return url;
509
- }
510
- /**
511
- * 2.4. Set buffer to the empty string.
512
- */
513
- buffer = "";
514
- if (url.scheme === "file") {
515
- /**
516
- * 2.5. If url’s scheme is "file", then:
517
- * 2.5.1. If remaining does not start with "//", validation error.
518
- * 2.5.2. Set state to file state.
519
- */
520
- if (!walker.remaining().startsWith("//")) {
521
- this.validationError("Invalid file URL scheme, '//' expected.");
522
- }
523
- state = interfaces_1.ParserState.File;
524
- }
525
- else if (this.isSpecial(url) && baseURL !== null && baseURL.scheme === url.scheme) {
526
- /**
527
- * 2.6. Otherwise, if url is special, base is non-null, and base’s
528
- * scheme is equal to url’s scheme, set state to special relative
529
- * or authority state.
530
- */
531
- state = interfaces_1.ParserState.SpecialRelativeOrAuthority;
532
- }
533
- else if (this.isSpecial(url)) {
534
- /**
535
- * 2.7. Otherwise, if url is special, set state to special
536
- * authority slashes state.
537
- */
538
- state = interfaces_1.ParserState.SpecialAuthoritySlashes;
539
- }
540
- else if (walker.remaining().startsWith("/")) {
541
- /**
542
- * 2.8. Otherwise, if remaining starts with an U+002F (/), set state
543
- * to path or authority state and increase pointer by one.
544
- */
545
- state = interfaces_1.ParserState.PathOrAuthority;
546
- walker.pointer++;
547
- }
548
- else {
549
- /**
550
- * 2.9. Otherwise, set url’s cannot-be-a-base-URL flag, append an
551
- * empty string to url’s path, and set state to
552
- * cannot-be-a-base-URL path state.
553
- */
554
- url._cannotBeABaseURLFlag = true;
555
- url.path.push("");
556
- state = interfaces_1.ParserState.CannotBeABaseURLPath;
557
- }
558
- }
559
- else if (stateOverride === undefined) {
560
- /**
561
- * 3. Otherwise, if state override is not given, set buffer to the
562
- * empty string, state to no scheme state, and start over (from the
563
- * first code point in input).
564
- */
565
- buffer = "";
566
- state = interfaces_1.ParserState.NoScheme;
567
- walker.pointer = 0;
568
- continue;
569
- }
570
- else {
571
- /**
572
- * 4. Otherwise, validation error, return failure.
573
- */
574
- this.validationError("Invalid input string.");
575
- return null;
576
- }
577
- break;
578
- case interfaces_1.ParserState.NoScheme:
579
- /**
580
- * 1. If base is null, or base’s cannot-be-a-base-URL flag is set
581
- * and c is not U+0023 (#), validation error, return failure.
582
- * 2. Otherwise, if base’s cannot-be-a-base-URL flag is set and
583
- * c is U+0023 (#), set url’s scheme to base’s scheme, url’s path to
584
- * a copy of base’s path, url’s query to base’s query, url’s
585
- * fragment to the empty string, set url’s cannot-be-a-base-URL
586
- * flag, and set state to fragment state.
587
- * 3. Otherwise, if base’s scheme is not "file", set state to
588
- * relative state and decrease pointer by one.
589
- * 4. Otherwise, set state to file state and decrease pointer by one.
590
- */
591
- if (baseURL === null || (baseURL._cannotBeABaseURLFlag && walker.c() !== '#')) {
592
- this.validationError("Invalid input string.");
593
- return null;
594
- }
595
- else if (baseURL._cannotBeABaseURLFlag && walker.c() === '#') {
596
- url.scheme = baseURL.scheme;
597
- url.path = infra_1.list.clone(baseURL.path);
598
- url.query = baseURL.query;
599
- url.fragment = "";
600
- url._cannotBeABaseURLFlag = true;
601
- state = interfaces_1.ParserState.Fragment;
602
- }
603
- else if (baseURL.scheme !== "file") {
604
- state = interfaces_1.ParserState.Relative;
605
- walker.pointer--;
606
- }
607
- else {
608
- state = interfaces_1.ParserState.File;
609
- walker.pointer--;
610
- }
611
- break;
612
- case interfaces_1.ParserState.SpecialRelativeOrAuthority:
613
- /**
614
- * If c is U+002F (/) and remaining starts with U+002F (/), then set
615
- * state to special authority ignore slashes state and increase
616
- * pointer by one.
617
- * Otherwise, validation error, set state to relative state and
618
- * decrease pointer by one.
619
- */
620
- if (walker.c() === '/' && walker.remaining().startsWith('/')) {
621
- state = interfaces_1.ParserState.SpecialAuthorityIgnoreSlashes;
622
- walker.pointer++;
623
- }
624
- else {
625
- this.validationError("Invalid input string.");
626
- state = interfaces_1.ParserState.Relative;
627
- walker.pointer--;
628
- }
629
- break;
630
- case interfaces_1.ParserState.PathOrAuthority:
631
- /**
632
- * If c is U+002F (/), then set state to authority state.
633
- * Otherwise, set state to path state, and decrease pointer by one.
634
- */
635
- if (walker.c() === '/') {
636
- state = interfaces_1.ParserState.Authority;
637
- }
638
- else {
639
- state = interfaces_1.ParserState.Path;
640
- walker.pointer--;
641
- }
642
- break;
643
- case interfaces_1.ParserState.Relative:
644
- /**
645
- * Set url’s scheme to base’s scheme, and then, switching on c:
646
- */
647
- if (baseURL === null) {
648
- throw new Error("Invalid parser state. Base URL is null.");
649
- }
650
- url.scheme = baseURL.scheme;
651
- switch (walker.c()) {
652
- case EOF: // EOF
653
- /**
654
- * Set url’s username to base’s username, url’s password to base’s
655
- * password, url’s host to base’s host, url’s port to base’s port,
656
- * url’s path to a copy of base’s path, and url’s query to base’s
657
- * query.
658
- */
659
- url.username = baseURL.username;
660
- url.password = baseURL.password;
661
- url.host = baseURL.host;
662
- url.port = baseURL.port;
663
- url.path = infra_1.list.clone(baseURL.path);
664
- url.query = baseURL.query;
665
- break;
666
- case '/':
667
- /**
668
- * Set state to relative slash state.
669
- */
670
- state = interfaces_1.ParserState.RelativeSlash;
671
- break;
672
- case '?':
673
- /**
674
- * Set url’s username to base’s username, url’s password to base’s
675
- * password, url’s host to base’s host, url’s port to base’s port,
676
- * url’s path to a copy of base’s path, url’s query to the empty
677
- * string, and state to query state.
678
- */
679
- url.username = baseURL.username;
680
- url.password = baseURL.password;
681
- url.host = baseURL.host;
682
- url.port = baseURL.port;
683
- url.path = infra_1.list.clone(baseURL.path);
684
- url.query = "";
685
- state = interfaces_1.ParserState.Query;
686
- break;
687
- case '#':
688
- /**
689
- * Set url’s username to base’s username, url’s password to base’s
690
- * password, url’s host to base’s host, url’s port to base’s port,
691
- * url’s path to a copy of base’s path, url’s query to base’s
692
- * query, url’s fragment to the empty string, and state to
693
- * fragment state.
694
- */
695
- url.username = baseURL.username;
696
- url.password = baseURL.password;
697
- url.host = baseURL.host;
698
- url.port = baseURL.port;
699
- url.path = infra_1.list.clone(baseURL.path);
700
- url.query = baseURL.query;
701
- url.fragment = "";
702
- state = interfaces_1.ParserState.Fragment;
703
- break;
704
- default:
705
- /**
706
- * If url is special and c is U+005C (\), validation error,
707
- * set state to relative slash state.
708
- * Otherwise, run these steps:
709
- * 1. Set url’s username to base’s username, url’s password to
710
- * base’s password, url’s host to base’s host, url’s port to
711
- * base’s port, url’s path to a copy of base’s path, and then
712
- * remove url’s path’s last item, if any.
713
- * 2. Set state to path state, and decrease pointer by one.
714
- */
715
- if (this.isSpecial(url) && walker.c() === '\\') {
716
- this.validationError("Invalid input string.");
717
- state = interfaces_1.ParserState.RelativeSlash;
718
- }
719
- else {
720
- url.username = baseURL.username;
721
- url.password = baseURL.password;
722
- url.host = baseURL.host;
723
- url.port = baseURL.port;
724
- url.path = infra_1.list.clone(baseURL.path);
725
- if (url.path.length !== 0)
726
- url.path.splice(url.path.length - 1, 1);
727
- state = interfaces_1.ParserState.Path;
728
- walker.pointer--;
729
- }
730
- break;
731
- }
732
- break;
733
- case interfaces_1.ParserState.RelativeSlash:
734
- /**
735
- * 1. If url is special and c is U+002F (/) or U+005C (\), then:
736
- * 1.1. If c is U+005C (\), validation error.
737
- * 1.2. Set state to special authority ignore slashes state.
738
- * 2. Otherwise, if c is U+002F (/), then set state to authority state.
739
- * 3. Otherwise, set url’s username to base’s username, url’s password
740
- * to base’s password, url’s host to base’s host, url’s port to base’s
741
- * port, state to path state, and then, decrease pointer by one.
742
- */
743
- if (this.isSpecial(url) && (walker.c() === '/' || walker.c() === '\\')) {
744
- if (walker.c() === '\\') {
745
- this.validationError("Invalid input string.");
746
- }
747
- state = interfaces_1.ParserState.SpecialAuthorityIgnoreSlashes;
748
- }
749
- else if (walker.c() === '/') {
750
- state = interfaces_1.ParserState.Authority;
751
- }
752
- else {
753
- if (baseURL === null) {
754
- throw new Error("Invalid parser state. Base URL is null.");
755
- }
756
- url.username = baseURL.username;
757
- url.password = baseURL.password;
758
- url.host = baseURL.host;
759
- url.port = baseURL.port;
760
- state = interfaces_1.ParserState.Path;
761
- walker.pointer--;
762
- }
763
- break;
764
- case interfaces_1.ParserState.SpecialAuthoritySlashes:
765
- /**
766
- * If c is U+002F (/) and remaining starts with U+002F (/), then set
767
- * state to special authority ignore slashes state and increase
768
- * pointer by one.
769
- * Otherwise, validation error, set state to special authority ignore
770
- * slashes state, and decrease pointer by one.
771
- */
772
- if (walker.c() === '/' && walker.remaining().startsWith('/')) {
773
- state = interfaces_1.ParserState.SpecialAuthorityIgnoreSlashes;
774
- walker.pointer++;
775
- }
776
- else {
777
- this.validationError("Expected '//'.");
778
- state = interfaces_1.ParserState.SpecialAuthorityIgnoreSlashes;
779
- walker.pointer--;
780
- }
781
- break;
782
- case interfaces_1.ParserState.SpecialAuthorityIgnoreSlashes:
783
- /**
784
- * If c is neither U+002F (/) nor U+005C (\), then set state to
785
- * authority state and decrease pointer by one.
786
- * Otherwise, validation error.
787
- */
788
- if (walker.c() !== '/' && walker.c() !== '\\') {
789
- state = interfaces_1.ParserState.Authority;
790
- walker.pointer--;
791
- }
792
- else {
793
- this.validationError("Unexpected '/' or '\\'.");
794
- }
795
- break;
796
- case interfaces_1.ParserState.Authority:
797
- /**
798
- * 1. If c is U+0040 (@), then:
799
- */
800
- if (walker.c() === '@') {
801
- /**
802
- * 1.1. Validation error.
803
- * 1.2. If the @ flag is set, prepend "%40" to buffer.
804
- * 1.3. Set the @ flag.
805
- * 1.4. For each codePoint in buffer:
806
- */
807
- this.validationError("Unexpected '@'.");
808
- if (atFlag)
809
- buffer = '%40' + buffer;
810
- atFlag = true;
811
- for (const codePoint of buffer) {
812
- /**
813
- * 1.4.1. If codePoint is U+003A (:) and passwordTokenSeenFlag is
814
- * unset, then set passwordTokenSeenFlag and continue.
815
- * 1.4.2. Let encodedCodePoints be the result of running UTF-8
816
- * percent encode codePoint using the userinfo percent-encode set.
817
- * 1.4.3. If passwordTokenSeenFlag is set, then append
818
- * encodedCodePoints to url’s password.
819
- * 1.4.4. Otherwise, append encodedCodePoints to url’s username.
820
- */
821
- if (codePoint === ':' && !passwordTokenSeenFlag) {
822
- passwordTokenSeenFlag = true;
823
- continue;
824
- }
825
- const encodedCodePoints = this.utf8PercentEncode(codePoint, this._userInfoPercentEncodeSet);
826
- if (passwordTokenSeenFlag) {
827
- url.password += encodedCodePoints;
828
- }
829
- else {
830
- url.username += encodedCodePoints;
831
- }
832
- }
833
- /**
834
- * 1.5. Set buffer to the empty string.
835
- */
836
- buffer = "";
837
- }
838
- else if (walker.c() === EOF || walker.c() === '/' || walker.c() === '?' || walker.c() === '#' ||
839
- (this.isSpecial(url) && walker.c() === '\\')) {
840
- /**
841
- * 2. Otherwise, if one of the following is true
842
- * - c is the EOF code point, U+002F (/), U+003F (?), or U+0023 (#)
843
- * - url is special and c is U+005C (\)
844
- * then:
845
- * 2.1. If @ flag is set and buffer is the empty string, validation
846
- * error, return failure.
847
- * 2.2. Decrease pointer by the number of code points in buffer plus
848
- * one, set buffer to the empty string, and set state to host state.
849
- */
850
- if (atFlag && buffer === "") {
851
- this.validationError("Invalid input string.");
852
- return null;
853
- }
854
- walker.pointer -= (buffer.length + 1);
855
- buffer = "";
856
- state = interfaces_1.ParserState.Host;
857
- }
858
- else {
859
- /**
860
- * 3. Otherwise, append c to buffer.
861
- */
862
- buffer += walker.c();
863
- }
864
- break;
865
- case interfaces_1.ParserState.Host:
866
- case interfaces_1.ParserState.Hostname:
867
- if (stateOverride !== undefined && url.scheme === "file") {
868
- /**
869
- * 1. If state override is given and url’s scheme is "file", then
870
- * decrease pointer by one and set state to file host state.
871
- */
872
- walker.pointer--;
873
- state = interfaces_1.ParserState.FileHost;
874
- }
875
- else if (walker.c() === ':' && !arrayFlag) {
876
- /**
877
- * 2. Otherwise, if c is U+003A (:) and the [] flag is unset, then:
878
- * 2.1. If buffer is the empty string, validation error, return
879
- * failure.
880
- * 2.2. Let host be the result of host parsing buffer with url is
881
- * not special.
882
- * 2.3. If host is failure, then return failure.
883
- * 2.4. Set url’s host to host, buffer to the empty string, and
884
- * state to port state.
885
- * 2.5. If state override is given and state override is hostname
886
- * state, then return.
887
- */
888
- if (buffer === "") {
889
- this.validationError("Invalid input string.");
890
- return null;
891
- }
892
- const host = this.hostParser(buffer, !this.isSpecial(url));
893
- if (host === null)
894
- return null;
895
- url.host = host;
896
- buffer = "";
897
- state = interfaces_1.ParserState.Port;
898
- if (stateOverride === interfaces_1.ParserState.Hostname)
899
- return url;
900
- }
901
- else if (walker.c() === EOF || walker.c() === '/' || walker.c() === '?' || walker.c() === '#' ||
902
- (this.isSpecial(url) && walker.c() === '\\')) {
903
- /**
904
- * 3. Otherwise, if one of the following is true
905
- * - c is the EOF code point, U+002F (/), U+003F (?), or U+0023 (#)
906
- * - url is special and c is U+005C (\)
907
- * then decrease pointer by one, and then:
908
- * 3.1. If url is special and buffer is the empty string, validation
909
- * error, return failure.
910
- * 3.2. Otherwise, if state override is given, buffer is the empty
911
- * string, and either url includes credentials or url’s port is
912
- * non-null, validation error, return.
913
- * 3.3. Let host be the result of host parsing buffer with url is
914
- * not special.
915
- * 3.4. If host is failure, then return failure.
916
- * 3.5. Set url’s host to host, buffer to the empty string, and
917
- * state to path start state.
918
- * 3.6. If state override is given, then return.
919
- */
920
- walker.pointer--;
921
- if (this.isSpecial(url) && buffer === "") {
922
- this.validationError("Invalid input string.");
923
- return null;
924
- }
925
- else if (stateOverride !== undefined && buffer === "" &&
926
- (this.includesCredentials(url) || url.port !== null)) {
927
- this.validationError("Invalid input string.");
928
- return url;
929
- }
930
- const host = this.hostParser(buffer, !this.isSpecial(url));
931
- if (host === null)
932
- return null;
933
- url.host = host;
934
- buffer = "";
935
- state = interfaces_1.ParserState.PathStart;
936
- if (stateOverride !== undefined)
937
- return url;
938
- }
939
- else {
940
- /**
941
- * 4. Otherwise:
942
- * 4.1. If c is U+005B ([), then set the [] flag.
943
- * 4.2. If c is U+005D (]), then unset the [] flag.
944
- * 4.3. Append c to buffer.
945
- */
946
- if (walker.c() === '[')
947
- arrayFlag = true;
948
- if (walker.c() === ']')
949
- arrayFlag = false;
950
- buffer += walker.c();
951
- }
952
- break;
953
- case interfaces_1.ParserState.Port:
954
- if (infra_1.codePoint.ASCIIDigit.test(walker.c())) {
955
- /**
956
- * 1. If c is an ASCII digit, append c to buffer.
957
- */
958
- buffer += walker.c();
959
- }
960
- else if (walker.c() === EOF || walker.c() === '/' || walker.c() === '?' || walker.c() === '#' ||
961
- (this.isSpecial(url) && walker.c() === '\\') || stateOverride) {
962
- /**
963
- * 2. Otherwise, if one of the following is true
964
- * - c is the EOF code point, U+002F (/), U+003F (?), or U+0023 (#)
965
- * - url is special and c is U+005C (\)
966
- * - state override is given
967
- * then:
968
- */
969
- if (buffer !== "") {
970
- /**
971
- * 2.1. If buffer is not the empty string, then:
972
- * 2.1.1. Let port be the mathematical integer value that is
973
- * represented by buffer in radix-10 using ASCII digits for digits
974
- * with values 0 through 9.
975
- * 2.1.2. If port is greater than 2**16 − 1, validation error,
976
- * return failure.
977
- * 2.1.3. Set url’s port to null, if port is url’s scheme’s default
978
- * port, and to port otherwise.
979
- * 2.1.4. Set buffer to the empty string.
980
- */
981
- if (buffer !== "") {
982
- const port = parseInt(buffer, 10);
983
- if (port > Math.pow(2, 16) - 1) {
984
- this.validationError("Invalid port number.");
985
- return null;
986
- }
987
- url.port = (port === this.defaultPort(url.scheme) ? null : port);
988
- buffer = "";
989
- }
990
- }
991
- /**
992
- * 2.2. If state override is given, then return.
993
- * 2.3. Set state to path start state, and decrease pointer by one.
994
- */
995
- if (stateOverride !== undefined) {
996
- return url;
997
- }
998
- state = interfaces_1.ParserState.PathStart;
999
- walker.pointer--;
1000
- }
1001
- else {
1002
- /**
1003
- * 3. Otherwise, validation error, return failure.
1004
- */
1005
- this.validationError("Invalid input string.");
1006
- return null;
1007
- }
1008
- break;
1009
- case interfaces_1.ParserState.File:
1010
- /**
1011
- * 1. Set url’s scheme to "file".
1012
- */
1013
- url.scheme = "file";
1014
- if (walker.c() === '/' || walker.c() === '\\') {
1015
- /**
1016
- * 2. If c is U+002F (/) or U+005C (\), then:
1017
- * 2.1. If c is U+005C (\), validation error.
1018
- * 2.2. Set state to file slash state.
1019
- */
1020
- if (walker.c() === '\\') {
1021
- this.validationError("Invalid input string.");
1022
- }
1023
- state = interfaces_1.ParserState.FileSlash;
1024
- }
1025
- else if (baseURL !== null && baseURL.scheme === "file") {
1026
- /**
1027
- * 3. Otherwise, if base is non-null and base’s scheme is "file",
1028
- * switch on c:
1029
- */
1030
- switch (walker.c()) {
1031
- case EOF:
1032
- /**
1033
- * Set url’s host to base’s host, url’s path to a copy of base’s
1034
- * path, and url’s query to base’s query.
1035
- */
1036
- url.host = baseURL.host;
1037
- url.path = infra_1.list.clone(baseURL.path);
1038
- url.query = baseURL.query;
1039
- break;
1040
- case '?':
1041
- /**
1042
- * Set url’s host to base’s host, url’s path to a copy of base’s
1043
- * path, url’s query to the empty string, and state to query
1044
- * state.
1045
- */
1046
- url.host = baseURL.host;
1047
- url.path = infra_1.list.clone(baseURL.path);
1048
- url.query = "";
1049
- state = interfaces_1.ParserState.Query;
1050
- break;
1051
- case '#':
1052
- /**
1053
- * Set url’s host to base’s host, url’s path to a copy of base’s
1054
- * path, url’s query to base’s query, url’s fragment to the
1055
- * empty string, and state to fragment state.
1056
- */
1057
- url.host = baseURL.host;
1058
- url.path = infra_1.list.clone(baseURL.path);
1059
- url.query = baseURL.query;
1060
- url.fragment = "";
1061
- state = interfaces_1.ParserState.Fragment;
1062
- break;
1063
- default:
1064
- /**
1065
- * 1. If the substring from pointer in input does not start
1066
- * with a Windows drive letter, then set url’s host to base’s
1067
- * host, url’s path to a copy of base’s path, and then shorten
1068
- * url’s path.
1069
- * _Note:_ This is a (platform-independent) Windows drive letter
1070
- * quirk.
1071
- * 2. Otherwise, validation error.
1072
- * 3. Set state to path state, and decrease pointer by one.
1073
- */
1074
- if (!this.startsWithAWindowsDriveLetter(walker.substring())) {
1075
- url.host = baseURL.host;
1076
- url.path = infra_1.list.clone(baseURL.path);
1077
- this.shorten(url);
1078
- }
1079
- else {
1080
- this.validationError("Unexpected windows drive letter in input string.");
1081
- }
1082
- state = interfaces_1.ParserState.Path;
1083
- walker.pointer--;
1084
- break;
1085
- }
1086
- }
1087
- else {
1088
- /**
1089
- * 4. Otherwise, set state to path state, and decrease pointer by
1090
- * one.
1091
- */
1092
- state = interfaces_1.ParserState.Path;
1093
- walker.pointer--;
1094
- }
1095
- break;
1096
- case interfaces_1.ParserState.FileSlash:
1097
- if (walker.c() === '/' || walker.c() === '\\') {
1098
- /**
1099
- * 1. If c is U+002F (/) or U+005C (\), then:
1100
- * 1.1. If c is U+005C (\), validation error.
1101
- * 1.2. Set state to file host state.
1102
- */
1103
- if (walker.c() === '\\') {
1104
- this.validationError("Invalid input string.");
1105
- }
1106
- state = interfaces_1.ParserState.FileHost;
1107
- }
1108
- else {
1109
- /**
1110
- * 2. Otherwise:
1111
- * 2.1. If base is non-null, base’s scheme is "file", and the
1112
- * substring from pointer in input does not start with a Windows
1113
- * drive letter, then:
1114
- * 2.1.1. If base’s path[0] is a normalized Windows drive letter,
1115
- * then append base’s path[0] to url’s path.
1116
- * _Note:_ This is a (platform-independent) Windows drive letter
1117
- * quirk. Both url’s and base’s host are null under these conditions
1118
- * and therefore not copied.
1119
- * 2.1.2. Otherwise, set url’s host to base’s host.
1120
- * 2.2. Set state to path state, and decrease pointer by one.
1121
- */
1122
- if (baseURL !== null && baseURL.scheme === "file" &&
1123
- !this.startsWithAWindowsDriveLetter(walker.substring())) {
1124
- if (this.isNormalizedWindowsDriveLetter(baseURL.path[0])) {
1125
- url.path.push(baseURL.path[0]);
1126
- }
1127
- else {
1128
- url.host = baseURL.host;
1129
- }
1130
- }
1131
- state = interfaces_1.ParserState.Path;
1132
- walker.pointer--;
1133
- }
1134
- break;
1135
- case interfaces_1.ParserState.FileHost:
1136
- if (walker.c() === EOF || walker.c() === '/' || walker.c() === '\\' ||
1137
- walker.c() === '?' || walker.c() === '#') {
1138
- /**
1139
- * 1. If c is the EOF code point, U+002F (/), U+005C (\), U+003F (?),
1140
- * or U+0023 (#), then decrease pointer by one and then:
1141
- */
1142
- walker.pointer--;
1143
- if (stateOverride === undefined && this.isWindowsDriveLetter(buffer)) {
1144
- /**
1145
- * 1.1. If state override is not given and buffer is a Windows drive
1146
- * letter, validation error, set state to path state.
1147
- * _Note:_ This is a (platform-independent) Windows drive letter
1148
- * quirk. buffer is not reset here and instead used in the path state.
1149
- */
1150
- this.validationError("Unexpected windows drive letter in input string.");
1151
- state = interfaces_1.ParserState.Path;
1152
- }
1153
- else if (buffer === "") {
1154
- /**
1155
- * 1.2. Otherwise, if buffer is the empty string, then:
1156
- * 1.2.1. Set url’s host to the empty string.
1157
- * 1.2.2. If state override is given, then return.
1158
- * 1.2.3. Set state to path start state.
1159
- */
1160
- url.host = "";
1161
- if (stateOverride !== undefined)
1162
- return url;
1163
- state = interfaces_1.ParserState.PathStart;
1164
- }
1165
- else {
1166
- /**
1167
- * 1.3. Otherwise, run these steps:
1168
- * 1.3.1. Let host be the result of host parsing buffer with url
1169
- * is not special.
1170
- * 1.3.2. If host is failure, then return failure.
1171
- * 1.3.3. If host is "localhost", then set host to the empty
1172
- * string.
1173
- * 1.3.4. Set url’s host to host.
1174
- * 1.3.5. If state override is given, then return.
1175
- * 1.3.6. Set buffer to the empty string and state to path start
1176
- * state.
1177
- */
1178
- let host = this.hostParser(buffer, !this.isSpecial(url));
1179
- if (host === null)
1180
- return null;
1181
- if (host === "localhost")
1182
- host = "";
1183
- url.host = host;
1184
- if (stateOverride !== undefined)
1185
- return url;
1186
- buffer = "";
1187
- state = interfaces_1.ParserState.PathStart;
1188
- }
1189
- }
1190
- else {
1191
- /**
1192
- * 2. Otherwise, append c to buffer.
1193
- */
1194
- buffer += walker.c();
1195
- }
1196
- break;
1197
- case interfaces_1.ParserState.PathStart:
1198
- if (this.isSpecial(url)) {
1199
- /**
1200
- * 1. If url is special, then:
1201
- * 1.1. If c is U+005C (\), validation error.
1202
- * 1.2. Set state to path state.
1203
- * 1.3. If c is neither U+002F (/) nor U+005C (\), then decrease
1204
- * pointer by one.
1205
- */
1206
- if (walker.c() === '\\') {
1207
- this.validationError("Invalid input string.");
1208
- }
1209
- state = interfaces_1.ParserState.Path;
1210
- if (walker.c() !== '/' && walker.c() !== '\\')
1211
- walker.pointer--;
1212
- }
1213
- else if (stateOverride === undefined && walker.c() === '?') {
1214
- /**
1215
- * 2. Otherwise, if state override is not given and c is U+003F (?),
1216
- * set url’s query to the empty string and state to query state.
1217
- */
1218
- url.query = "";
1219
- state = interfaces_1.ParserState.Query;
1220
- }
1221
- else if (stateOverride === undefined && walker.c() === '#') {
1222
- /**
1223
- * 3. Otherwise, if state override is not given and c is U+0023 (#),
1224
- * set url’s fragment to the empty string and state to fragment
1225
- * state.
1226
- */
1227
- url.fragment = "";
1228
- state = interfaces_1.ParserState.Fragment;
1229
- }
1230
- else if (walker.c() !== EOF) {
1231
- /**
1232
- * 4. Otherwise, if c is not the EOF code point:
1233
- * 4.1. Set state to path state.
1234
- * 4.2. If c is not U+002F (/), then decrease pointer by one.
1235
- */
1236
- state = interfaces_1.ParserState.Path;
1237
- if (walker.c() !== '/')
1238
- walker.pointer--;
1239
- }
1240
- break;
1241
- case interfaces_1.ParserState.Path:
1242
- if ((walker.c() === EOF || walker.c() === '/') ||
1243
- (this.isSpecial(url) && walker.c() === '\\') ||
1244
- (stateOverride === undefined && (walker.c() === '?' || walker.c() === '#'))) {
1245
- /**
1246
- * 1. If one of the following is true
1247
- * - c is the EOF code point or U+002F (/)
1248
- * - url is special and c is U+005C (\)
1249
- * - state override is not given and c is U+003F (?) or U+0023 (#)
1250
- * then:
1251
- */
1252
- if (this.isSpecial(url) && walker.c() === '\\') {
1253
- /**
1254
- * 1.1 If url is special and c is U+005C (\), validation error.
1255
- */
1256
- this.validationError("Invalid input string.");
1257
- }
1258
- if (this.isDoubleDotPathSegment(buffer)) {
1259
- /**
1260
- * 1.2. If buffer is a double-dot path segment, shorten url’s path,
1261
- * and then if neither c is U+002F (/), nor url is special and c is
1262
- * U+005C (\), append the empty string to url’s path.
1263
- */
1264
- this.shorten(url);
1265
- if (walker.c() !== '/' && !(this.isSpecial(url) && walker.c() === '\\')) {
1266
- url.path.push("");
1267
- }
1268
- }
1269
- else if (this.isSingleDotPathSegment(buffer) && walker.c() !== '/' &&
1270
- !(this.isSpecial(url) && walker.c() === '\\')) {
1271
- /**
1272
- * 1.3. Otherwise, if buffer is a single-dot path segment and if
1273
- * neither c is U+002F (/), nor url is special and c is U+005C (\),
1274
- * append the empty string to url’s path.
1275
- */
1276
- url.path.push("");
1277
- }
1278
- else if (!this.isSingleDotPathSegment(buffer)) {
1279
- /**
1280
- * 1.4. Otherwise, if buffer is not a single-dot path segment, then:
1281
- */
1282
- if (url.scheme === "file" && url.path.length === 0 &&
1283
- this.isWindowsDriveLetter(buffer)) {
1284
- /**
1285
- * 1.4.1. If url’s scheme is "file", url’s path is empty, and
1286
- * buffer is a Windows drive letter, then:
1287
- * 1.4.1.1. If url’s host is neither the empty string nor null,
1288
- * validation error, set url’s host to the empty string.
1289
- * 1.4.1.2. Replace the second code point in buffer with U+003A (:).
1290
- * _Note:_ This is a (platform-independent) Windows drive letter quirk.
1291
- */
1292
- if (url.host !== null && url.host !== "") {
1293
- this.validationError("Invalid input string.");
1294
- url.host = "";
1295
- }
1296
- const bufferCodePoints = Array.from(buffer);
1297
- buffer = bufferCodePoints.slice(0, 1) + ':' + bufferCodePoints.slice(2);
1298
- }
1299
- /**
1300
- * 1.4.2. Append buffer to url’s path.
1301
- */
1302
- url.path.push(buffer);
1303
- }
1304
- /**
1305
- * 1.5. Set buffer to the empty string.
1306
- */
1307
- buffer = "";
1308
- /**
1309
- * 1.6. If url’s scheme is "file" and c is the EOF code point,
1310
- * U+003F (?), or U+0023 (#), then while url’s path’s size is
1311
- * greater than 1 and url’s path[0] is the empty string, validation
1312
- * error, remove the first item from url’s path.
1313
- */
1314
- if (url.scheme === "file" && (walker.c() === EOF || walker.c() === '?' || walker.c() === '#')) {
1315
- while (url.path.length > 1 && url.path[0] === "") {
1316
- this.validationError("Invalid input string.");
1317
- url.path.splice(0, 1);
1318
- }
1319
- }
1320
- /**
1321
- * 1.7. If c is U+003F (?), then set url’s query to the empty string
1322
- * and state to query state.
1323
- * 1.8. If c is U+0023 (#), then set url’s fragment to the empty
1324
- * string and state to fragment state.
1325
- */
1326
- if (walker.c() === '?') {
1327
- url.query = "";
1328
- state = interfaces_1.ParserState.Query;
1329
- }
1330
- if (walker.c() === '#') {
1331
- url.fragment = "";
1332
- state = interfaces_1.ParserState.Fragment;
1333
- }
1334
- }
1335
- else {
1336
- /**
1337
- * 2. Otherwise, run these steps:
1338
- * 2.1. If c is not a URL code point and not U+0025 (%), validation
1339
- * error.
1340
- * 2.2. If c is U+0025 (%) and remaining does not start with two
1341
- * ASCII hex digits, validation error.
1342
- * 2.3. UTF-8 percent encode c using the path percent-encode set,
1343
- * and append the result to buffer.
1344
- */
1345
- if (!this._urlCodePoints.test(walker.c()) && walker.c() !== '%') {
1346
- this.validationError("Character is not a URL code point or a percent encoded character.");
1347
- }
1348
- if (walker.c() === '%' && !/^[0-9a-fA-F][0-9a-fA-F]/.test(walker.remaining())) {
1349
- this.validationError("Percent encoded character must be followed by two hex digits.");
1350
- }
1351
- buffer += this.utf8PercentEncode(walker.c(), this._pathPercentEncodeSet);
1352
- }
1353
- break;
1354
- case interfaces_1.ParserState.CannotBeABaseURLPath:
1355
- /**
1356
- * 1. If c is U+003F (?), then set url’s query to the empty string and
1357
- * state to query state.
1358
- * 2. Otherwise, if c is U+0023 (#), then set url’s fragment to the
1359
- * empty string and state to fragment state.
1360
- * 3. Otherwise:
1361
- * 3.1. If c is not the EOF code point, not a URL code point, and not
1362
- * U+0025 (%), validation error.
1363
- * 3.2. If c is U+0025 (%) and remaining does not start with two ASCII
1364
- * hex digits, validation error.
1365
- * 3.3. If c is not the EOF code point, UTF-8 percent encode c using
1366
- * the C0 control percent-encode set, and append the result to url’s
1367
- * path[0].
1368
- */
1369
- if (walker.c() === '?') {
1370
- url.query = "";
1371
- state = interfaces_1.ParserState.Query;
1372
- }
1373
- else if (walker.c() === '#') {
1374
- url.fragment = "";
1375
- state = interfaces_1.ParserState.Fragment;
1376
- }
1377
- else {
1378
- if (walker.c() !== EOF && !this._urlCodePoints.test(walker.c()) && walker.c() !== '%') {
1379
- this.validationError("Character is not a URL code point or a percent encoded character.");
1380
- }
1381
- if (walker.c() === '%' && !/^[0-9a-fA-F][0-9a-fA-F]/.test(walker.remaining())) {
1382
- this.validationError("Percent encoded character must be followed by two hex digits.");
1383
- }
1384
- if (walker.c() !== EOF) {
1385
- url.path[0] += this.utf8PercentEncode(walker.c(), this._c0ControlPercentEncodeSet);
1386
- }
1387
- }
1388
- break;
1389
- case interfaces_1.ParserState.Query:
1390
- /**
1391
- * 1. If encoding is not UTF-8 and one of the following is true
1392
- * - url is not special
1393
- * - url’s scheme is "ws" or "wss"
1394
- * then set encoding to UTF-8.
1395
- */
1396
- if (encoding !== "UTF-8" && (!this.isSpecial(url) ||
1397
- url.scheme === "ws" || url.scheme === "wss")) {
1398
- encoding = "UTF-8";
1399
- }
1400
- if (stateOverride === undefined && walker.c() === '#') {
1401
- /**
1402
- * 2. If state override is not given and c is U+0023 (#), then set
1403
- * url’s fragment to the empty string and state to fragment state.
1404
- */
1405
- url.fragment = "";
1406
- state = interfaces_1.ParserState.Fragment;
1407
- }
1408
- else if (walker.c() !== EOF) {
1409
- /**
1410
- * 3. Otherwise, if c is not the EOF code point:
1411
- * 3.1. If c is not a URL code point and not U+0025 (%), validation
1412
- * error.
1413
- */
1414
- if (!this._urlCodePoints.test(walker.c()) && walker.c() !== '%') {
1415
- this.validationError("Character is not a URL code point or a percent encoded character.");
1416
- }
1417
- /**
1418
- * 3.2. If c is U+0025 (%) and remaining does not start with two
1419
- * ASCII hex digits, validation error.
1420
- */
1421
- if (walker.c() === '%' && !/^[0-9a-fA-F][0-9a-fA-F]/.test(walker.remaining())) {
1422
- this.validationError("Percent encoded character must be followed by two hex digits.");
1423
- }
1424
- /**
1425
- * 3.3. Let bytes be the result of encoding c using encoding.
1426
- */
1427
- if (encoding.toUpperCase() !== "UTF-8") {
1428
- throw new Error("Only UTF-8 encoding is supported.");
1429
- }
1430
- let bytes = util_1.utf8Encode(walker.c());
1431
- /**
1432
- * 3.4. If bytes starts with `&#` and ends with 0x3B (;), then:
1433
- */
1434
- if (bytes.length >= 3 && bytes[0] === 38 && bytes[1] === 35 &&
1435
- bytes[bytes.length - 1] === 59) {
1436
- /**
1437
- * 3.4.1. Replace `&#` at the start of bytes with `%26%23`.
1438
- * 3.4.2. Replace 0x3B (;) at the end of bytes with `%3B`.
1439
- * 3.4.4. Append bytes, isomorphic decoded, to url’s query.
1440
- * _Note:_ This can happen when encoding code points using a
1441
- * non-UTF-8 encoding.
1442
- */
1443
- bytes = bytes.subarray(2, bytes.length - 1);
1444
- url.query += "%26%23" + infra_1.byteSequence.isomorphicDecode(bytes) + "%3B";
1445
- }
1446
- else {
1447
- /**
1448
- * 3.5. Otherwise, for each byte in bytes:
1449
- * 3.5.1. If one of the following is true
1450
- * - byte is less than 0x21 (!)
1451
- * - byte is greater than 0x7E (~)
1452
- * - byte is 0x22 ("), 0x23 (#), 0x3C (<), or 0x3E (>)
1453
- * - byte is 0x27 (') and url is special
1454
- * then append byte, percent encoded, to url’s query.
1455
- * 3.5.2. Otherwise, append a code point whose value is byte to
1456
- * url’s query.
1457
- */
1458
- for (const byte of bytes) {
1459
- if (byte < 0x21 || byte > 0x7E || byte === 0x22 ||
1460
- byte === 0x23 || byte === 0x3C || byte === 0x3E ||
1461
- (byte === 0x27 && this.isSpecial(url))) {
1462
- url.query += this.percentEncode(byte);
1463
- }
1464
- else {
1465
- url.query += String.fromCharCode(byte);
1466
- }
1467
- }
1468
- }
1469
- }
1470
- break;
1471
- case interfaces_1.ParserState.Fragment:
1472
- /**
1473
- * Switching on c:
1474
- * - The EOF code point
1475
- * Do nothing.
1476
- * - U+0000 NULL
1477
- * Validation error.
1478
- * - Otherwise
1479
- * 1. If c is not a URL code point and not U+0025 (%), validation
1480
- * error.
1481
- * 2. If c is U+0025 (%) and remaining does not start with two ASCII
1482
- * hex digits, validation error.
1483
- * 3. UTF-8 percent encode c using the fragment percent-encode set and
1484
- * append the result to url’s fragment.
1485
- */
1486
- if (walker.c() === EOF) {
1487
- //
1488
- }
1489
- else if (walker.c() === "\u0000") {
1490
- this.validationError("NULL character in input string.");
1491
- }
1492
- else {
1493
- if (!this._urlCodePoints.test(walker.c()) && walker.c() !== '%') {
1494
- this.validationError("Unexpected character in fragment string.");
1495
- }
1496
- if (walker.c() === '%' && !/^[A-Za-z0-9][A-Za-z0-9]/.test(walker.remaining())) {
1497
- this.validationError("Unexpected character in fragment string.");
1498
- }
1499
- url.fragment += this.utf8PercentEncode(walker.c(), this._fragmentPercentEncodeSet);
1500
- }
1501
- break;
1502
- }
1503
- if (walker.eof)
1504
- break;
1505
- else
1506
- walker.pointer++;
1507
- }
1508
- /**
1509
- * 12. Return url.
1510
- */
1511
- return url;
1512
- }
1513
- /**
1514
- * Sets a URL's username.
1515
- *
1516
- * @param url - a URL
1517
- * @param username - username string
1518
- */
1519
- setTheUsername(url, username) {
1520
- /**
1521
- * 1. Set url’s username to the empty string.
1522
- * 2. For each code point in username, UTF-8 percent encode it using the
1523
- * userinfo percent-encode set, and append the result to url’s username.
1524
- */
1525
- let result = "";
1526
- for (const codePoint of username) {
1527
- result += this.utf8PercentEncode(codePoint, this._userInfoPercentEncodeSet);
1528
- }
1529
- url.username = result;
1530
- }
1531
- /**
1532
- * Sets a URL's password.
1533
- *
1534
- * @param url - a URL
1535
- * @param username - password string
1536
- */
1537
- setThePassword(url, password) {
1538
- /**
1539
- * 1. Set url’s password to the empty string.
1540
- * 2. For each code point in password, UTF-8 percent encode it using the
1541
- * userinfo percent-encode set, and append the result to url’s password.
1542
- */
1543
- let result = "";
1544
- for (const codePoint of password) {
1545
- result += this.utf8PercentEncode(codePoint, this._userInfoPercentEncodeSet);
1546
- }
1547
- url.password = result;
1548
- }
1549
- /**
1550
- * Determines if the string represents a single dot path.
1551
- *
1552
- * @param str - a string
1553
- */
1554
- isSingleDotPathSegment(str) {
1555
- return str === '.' || str.toLowerCase() === "%2e";
1556
- }
1557
- /**
1558
- * Determines if the string represents a double dot path.
1559
- *
1560
- * @param str - a string
1561
- */
1562
- isDoubleDotPathSegment(str) {
1563
- const lowerStr = str.toLowerCase();
1564
- return lowerStr === ".." || lowerStr === ".%2e" ||
1565
- lowerStr === "%2e." || lowerStr === "%2e%2e";
1566
- }
1567
- /**
1568
- * Shorten's URL's path.
1569
- *
1570
- * @param url - an URL
1571
- */
1572
- shorten(url) {
1573
- /**
1574
- * 1. Let path be url’s path.
1575
- * 2. If path is empty, then return.
1576
- * 3. If url’s scheme is "file", path’s size is 1, and path[0] is a
1577
- * normalized Windows drive letter, then return.
1578
- * 4. Remove path’s last item.
1579
- */
1580
- const path = url.path;
1581
- if (path.length === 0)
1582
- return;
1583
- if (url.scheme === "file" && path.length === 1 &&
1584
- this.isNormalizedWindowsDriveLetter(path[0]))
1585
- return;
1586
- url.path.splice(url.path.length - 1, 1);
1587
- }
1588
- /**
1589
- * Determines if a string is a normalized Windows drive letter.
1590
- *
1591
- * @param str - a string
1592
- */
1593
- isNormalizedWindowsDriveLetter(str) {
1594
- /**
1595
- * A normalized Windows drive letter is a Windows drive letter of which the
1596
- * second code point is U+003A (:).
1597
- */
1598
- return str.length >= 2 && infra_1.codePoint.ASCIIAlpha.test(str[0]) &&
1599
- str[1] === ':';
1600
- }
1601
- /**
1602
- * Determines if a string is a Windows drive letter.
1603
- *
1604
- * @param str - a string
1605
- */
1606
- isWindowsDriveLetter(str) {
1607
- /**
1608
- * A Windows drive letter is two code points, of which the first is an ASCII
1609
- * alpha and the second is either U+003A (:) or U+007C (|).
1610
- */
1611
- return str.length >= 2 && infra_1.codePoint.ASCIIAlpha.test(str[0]) &&
1612
- (str[1] === ':' || str[1] === '|');
1613
- }
1614
- /**
1615
- * Determines if a string starts with a Windows drive letter.
1616
- *
1617
- * @param str - a string
1618
- */
1619
- startsWithAWindowsDriveLetter(str) {
1620
- /**
1621
- * A string starts with a Windows drive letter if all of the following are
1622
- * true:
1623
- * - its length is greater than or equal to 2
1624
- * - its first two code points are a Windows drive letter
1625
- * - its length is 2 or its third code point is U+002F (/), U+005C (\),
1626
- * U+003F (?), or U+0023 (#).
1627
- */
1628
- return str.length >= 2 && this.isWindowsDriveLetter(str) &&
1629
- (str.length === 2 || (str[2] === '/' || str[2] === '\\' ||
1630
- str[2] === '?' || str[2] === '#'));
1631
- }
1632
- /**
1633
- * Parses a host string.
1634
- *
1635
- * @param input - input string
1636
- * @param isNotSpecial - `true` if the source URL is not special; otherwise
1637
- * `false`.
1638
- */
1639
- hostParser(input, isNotSpecial = false) {
1640
- /**
1641
- * 1. If isNotSpecial is not given, then set isNotSpecial to false.
1642
- * 2. If input starts with U+005B ([), then:
1643
- * 2.1. If input does not end with U+005D (]), validation error, return
1644
- * failure.
1645
- * 2.2. Return the result of IPv6 parsing input with its leading U+005B ([)
1646
- * and trailing U+005D (]) removed.
1647
- */
1648
- if (input.startsWith('[')) {
1649
- if (!input.endsWith(']')) {
1650
- this.validationError("Expected ']' after '['.");
1651
- return null;
1652
- }
1653
- return this.iPv6Parser(input.substring(1, input.length - 1));
1654
- }
1655
- /**
1656
- * 3. If isNotSpecial is true, then return the result of opaque-host parsing
1657
- * input.
1658
- */
1659
- if (isNotSpecial) {
1660
- return this.opaqueHostParser(input);
1661
- }
1662
- /**
1663
- * 4. Let domain be the result of running UTF-8 decode without BOM on the
1664
- * string percent decoding of input.
1665
- * _Note:_ Alternatively UTF-8 decode without BOM or fail can be used,
1666
- * coupled with an early return for failure, as domain to ASCII fails
1667
- * on U+FFFD REPLACEMENT CHARACTER.
1668
- */
1669
- const domain = util_1.utf8Decode(this.stringPercentDecode(input));
1670
- /**
1671
- * 5. Let asciiDomain be the result of running domain to ASCII on domain.
1672
- * 6. If asciiDomain is failure, validation error, return failure.
1673
- * 7. If asciiDomain contains a forbidden host code point, validation error,
1674
- * return failure.
1675
- */
1676
- const asciiDomain = this.domainToASCII(domain);
1677
- if (asciiDomain === null) {
1678
- this.validationError("Invalid domain.");
1679
- return null;
1680
- }
1681
- if (this._forbiddenHostCodePoint.test(asciiDomain)) {
1682
- this.validationError("Invalid domain.");
1683
- return null;
1684
- }
1685
- /**
1686
- * 8. Let ipv4Host be the result of IPv4 parsing asciiDomain.
1687
- * 9. If ipv4Host is an IPv4 address or failure, return ipv4Host.
1688
- * 10. Return asciiDomain.
1689
- */
1690
- const ipv4Host = this.iPv4Parser(asciiDomain);
1691
- if (ipv4Host === null || util_1.isNumber(ipv4Host))
1692
- return ipv4Host;
1693
- return asciiDomain;
1694
- }
1695
- /**
1696
- * Parses a string containing an IP v4 address.
1697
- *
1698
- * @param input - input string
1699
- * @param isNotSpecial - `true` if the source URL is not special; otherwise
1700
- * `false`.
1701
- */
1702
- iPv4NumberParser(input, validationErrorFlag = { value: false }) {
1703
- /**
1704
- * 1. Let R be 10.
1705
- */
1706
- let R = 10;
1707
- if (input.startsWith("0x") || input.startsWith("0X")) {
1708
- /**
1709
- * 2. If input contains at least two code points and the first two code
1710
- * points are either "0x" or "0X", then:
1711
- * 2.1. Set validationErrorFlag.
1712
- * 2.2. Remove the first two code points from input.
1713
- * 2.3. Set R to 16.
1714
- */
1715
- validationErrorFlag.value = true;
1716
- input = input.substr(2);
1717
- R = 16;
1718
- }
1719
- else if (input.length >= 2 && input[0] === '0') {
1720
- /**
1721
- * 3. Otherwise, if input contains at least two code points and the first
1722
- * code point is U+0030 (0), then:
1723
- * 3.1. Set validationErrorFlag.
1724
- * 3.2. Remove the first code point from input.
1725
- * 3.3. Set R to 8.
1726
- */
1727
- validationErrorFlag.value = true;
1728
- input = input.substr(1);
1729
- R = 8;
1730
- }
1731
- /**
1732
- * 4. If input is the empty string, then return zero.
1733
- * 5. If input contains a code point that is not a radix-R digit, then
1734
- * return failure.
1735
- */
1736
- if (input === "")
1737
- return 0;
1738
- const radixRDigits = (R === 10 ? /^[0-9]+$/ : (R === 16 ? /^[0-9A-Fa-f]+$/ : /^[0-7]+$/));
1739
- if (!radixRDigits.test(input))
1740
- return null;
1741
- /**
1742
- * 6. Return the mathematical integer value that is represented by input in
1743
- * radix-R notation, using ASCII hex digits for digits with values
1744
- * 0 through 15.
1745
- */
1746
- return parseInt(input, R);
1747
- }
1748
- /**
1749
- * Parses a string containing an IP v4 address.
1750
- *
1751
- * @param input - input string
1752
- */
1753
- iPv4Parser(input) {
1754
- /**
1755
- * 1. Let validationErrorFlag be unset.
1756
- * 2. Let parts be input split on U+002E (.).
1757
- */
1758
- const validationErrorFlag = { value: false };
1759
- const parts = input.split('.');
1760
- /**
1761
- * 3. If the last item in parts is the empty string, then:
1762
- * 3.1. Set validationErrorFlag.
1763
- * 3.2. If parts has more than one item, then remove the last item from
1764
- * parts.
1765
- */
1766
- if (parts[parts.length - 1] === "") {
1767
- validationErrorFlag.value = true;
1768
- if (parts.length > 1)
1769
- parts.pop();
1770
- }
1771
- /**
1772
- * 4. If parts has more than four items, return input.
1773
- */
1774
- if (parts.length > 4)
1775
- return input;
1776
- /**
1777
- * 5. Let numbers be the empty list.
1778
- * 6. For each part in parts:
1779
- * 6.1. If part is the empty string, return input.
1780
- * 6.2. Let n be the result of parsing part using validationErrorFlag.
1781
- * 6.3. If n is failure, return input.
1782
- * 6.4. Append n to numbers.
1783
- */
1784
- const numbers = [];
1785
- for (const part of parts) {
1786
- if (part === "")
1787
- return input;
1788
- const n = this.iPv4NumberParser(part, validationErrorFlag);
1789
- if (n === null)
1790
- return input;
1791
- numbers.push(n);
1792
- }
1793
- /**
1794
- * 7. If validationErrorFlag is set, validation error.
1795
- * 8. If any item in numbers is greater than 255, validation error.
1796
- * 9. If any but the last item in numbers is greater than 255, return
1797
- * failure.
1798
- * 10. If the last item in numbers is greater than or equal to
1799
- * 256**(5 − the number of items in numbers), validation error, return failure.
1800
- */
1801
- if (validationErrorFlag.value)
1802
- this.validationError("Invalid IP v4 address.");
1803
- for (let i = 0; i < numbers.length; i++) {
1804
- const item = numbers[i];
1805
- if (item > 255) {
1806
- this.validationError("Invalid IP v4 address.");
1807
- if (i < numbers.length - 1)
1808
- return null;
1809
- }
1810
- }
1811
- if (numbers[numbers.length - 1] >= Math.pow(256, 5 - numbers.length)) {
1812
- this.validationError("Invalid IP v4 address.");
1813
- return null;
1814
- }
1815
- /**
1816
- * 11. Let ipv4 be the last item in numbers.
1817
- * 12. Remove the last item from numbers.
1818
- */
1819
- let ipv4 = numbers[numbers.length - 1];
1820
- numbers.pop();
1821
- /**
1822
- * 13. Let counter be zero.
1823
- * 14. For each n in numbers:
1824
- * 14.2. Increment ipv4 by n × 256**(3 − counter).
1825
- * 14.2. Increment counter by 1.
1826
- */
1827
- let counter = 0;
1828
- for (const n of numbers) {
1829
- ipv4 += n * Math.pow(256, 3 - counter);
1830
- counter++;
1831
- }
1832
- /**
1833
- * 15. Return ipv4.
1834
- */
1835
- return ipv4;
1836
- }
1837
- /**
1838
- * Parses a string containing an IP v6 address.
1839
- *
1840
- * @param input - input string
1841
- */
1842
- iPv6Parser(input) {
1843
- /**
1844
- * 1. Let address be a new IPv6 address whose IPv6 pieces are all 0.
1845
- * 2. Let pieceIndex be 0.
1846
- * 3. Let compress be null.
1847
- * 4. Let pointer be a pointer into input, initially 0 (pointing to the
1848
- * first code point).
1849
- */
1850
- const EOF = "";
1851
- const address = [0, 0, 0, 0, 0, 0, 0, 0];
1852
- let pieceIndex = 0;
1853
- let compress = null;
1854
- const walker = new util_1.StringWalker(input);
1855
- /**
1856
- * 5. If c is U+003A (:), then:
1857
- * 5.1. If remaining does not start with U+003A (:), validation error,
1858
- * return failure.
1859
- * 5.2. Increase pointer by 2.
1860
- * 5.3. Increase pieceIndex by 1 and then set compress to pieceIndex.
1861
- */
1862
- if (walker.c() === ':') {
1863
- if (!walker.remaining().startsWith(':')) {
1864
- this.validationError("Invalid IP v6 address.");
1865
- return null;
1866
- }
1867
- walker.pointer += 2;
1868
- pieceIndex += 1;
1869
- compress = pieceIndex;
1870
- }
1871
- /**
1872
- * 6. While c is not the EOF code point:
1873
- */
1874
- while (walker.c() !== EOF) {
1875
- /**
1876
- * 6.1. If pieceIndex is 8, validation error, return failure.
1877
- */
1878
- if (pieceIndex === 8) {
1879
- this.validationError("Invalid IP v6 address.");
1880
- return null;
1881
- }
1882
- /**
1883
- * 6.2. If c is U+003A (:), then:
1884
- * 6.2.1. If compress is non-null, validation error, return failure.
1885
- * 6.2.2. Increase pointer and pieceIndex by 1, set compress to pieceIndex,
1886
- * and then continue.
1887
- */
1888
- if (walker.c() === ':') {
1889
- if (compress !== null) {
1890
- this.validationError("Invalid IP v6 address.");
1891
- return null;
1892
- }
1893
- walker.pointer++;
1894
- pieceIndex++;
1895
- compress = pieceIndex;
1896
- continue;
1897
- }
1898
- /**
1899
- * 6.3. Let value and length be 0.
1900
- * 6.4. While length is less than 4 and c is an ASCII hex digit, set value
1901
- * to value × 0x10 + c interpreted as hexadecimal number, and increase
1902
- * pointer and length by 1.
1903
- */
1904
- let value = 0;
1905
- let length = 0;
1906
- while (length < 4 && infra_1.codePoint.ASCIIHexDigit.test(walker.c())) {
1907
- value = value * 0x10 + parseInt(walker.c(), 16);
1908
- walker.pointer++;
1909
- length++;
1910
- }
1911
- /**
1912
- * 6.5. If c is U+002E (.), then:
1913
- */
1914
- if (walker.c() === '.') {
1915
- /**
1916
- * 6.5.1. If length is 0, validation error, return failure.
1917
- * 6.5.2. Decrease pointer by length.
1918
- * 6.5.3. If pieceIndex is greater than 6, validation error, return
1919
- * failure.
1920
- * 6.5.4. Let numbersSeen be 0.
1921
- */
1922
- if (length === 0) {
1923
- this.validationError("Invalid IP v6 address.");
1924
- return null;
1925
- }
1926
- walker.pointer -= length;
1927
- if (pieceIndex > 6) {
1928
- this.validationError("Invalid IP v6 address.");
1929
- return null;
1930
- }
1931
- let numbersSeen = 0;
1932
- /**
1933
- * 6.5.5. While c is not the EOF code point:
1934
- */
1935
- while (walker.c() !== EOF) {
1936
- /**
1937
- * 6.5.5.1. Let ipv4Piece be null.
1938
- */
1939
- let ipv4Piece = null;
1940
- /**
1941
- * 6.5.5.2. If numbersSeen is greater than 0, then:
1942
- * 6.5.5.2.1. If c is a U+002E (.) and numbersSeen is less than 4, then
1943
- * increase pointer by 1.
1944
- * 6.5.5.2.1. Otherwise, validation error, return failure.
1945
- */
1946
- if (numbersSeen > 0) {
1947
- if (walker.c() === '.' && numbersSeen < 4) {
1948
- walker.pointer++;
1949
- }
1950
- else {
1951
- this.validationError("Invalid IP v6 address.");
1952
- return null;
1953
- }
1954
- }
1955
- /**
1956
- * 6.5.5.3. If c is not an ASCII digit, validation error, return
1957
- * failure.
1958
- */
1959
- if (!infra_1.codePoint.ASCIIDigit.test(walker.c())) {
1960
- this.validationError("Invalid IP v6 address.");
1961
- return null;
1962
- }
1963
- /**
1964
- * 6.5.5.4. While c is an ASCII digit:
1965
- */
1966
- while (infra_1.codePoint.ASCIIDigit.test(walker.c())) {
1967
- /**
1968
- * 6.5.5.4.1. Let number be c interpreted as decimal number.
1969
- */
1970
- const number = parseInt(walker.c(), 10);
1971
- /**
1972
- * 6.5.5.4.2. If ipv4Piece is null, then set ipv4Piece to number.
1973
- * Otherwise, if ipv4Piece is 0, validation error, return failure.
1974
- * Otherwise, set ipv4Piece to ipv4Piece × 10 + number.
1975
- */
1976
- if (ipv4Piece === null) {
1977
- ipv4Piece = number;
1978
- }
1979
- else if (ipv4Piece === 0) {
1980
- this.validationError("Invalid IP v6 address.");
1981
- return null;
1982
- }
1983
- else {
1984
- ipv4Piece = ipv4Piece * 10 + number;
1985
- }
1986
- /**
1987
- * 6.5.5.4.3. If ipv4Piece is greater than 255, validation error, return failure.
1988
- * 6.5.5.4.4. Increase pointer by 1.
1989
- */
1990
- if (ipv4Piece > 255) {
1991
- this.validationError("Invalid IP v6 address.");
1992
- return null;
1993
- }
1994
- walker.pointer++;
1995
- }
1996
- /**
1997
- * 6.5.5.5. Set address[pieceIndex] to address[pieceIndex] × 0x100 + ipv4Piece.
1998
- * 6.5.5.6. Increase numbersSeen by 1.
1999
- * 6.5.5.7. If numbersSeen is 2 or 4, then increase pieceIndex by 1.
2000
- */
2001
- if (ipv4Piece === null) {
2002
- this.validationError("Invalid IP v6 address.");
2003
- return null;
2004
- }
2005
- address[pieceIndex] = address[pieceIndex] * 0x100 + ipv4Piece;
2006
- numbersSeen++;
2007
- if (numbersSeen === 2 || numbersSeen === 4)
2008
- pieceIndex++;
2009
- }
2010
- /**
2011
- * 6.5.6. If numbersSeen is not 4, validation error, return failure.
2012
- */
2013
- if (numbersSeen !== 4) {
2014
- this.validationError("Invalid IP v6 address.");
2015
- return null;
2016
- }
2017
- /**
2018
- * 6.5.7. Break.
2019
- */
2020
- break;
2021
- }
2022
- else if (walker.c() === ':') {
2023
- /**
2024
- * 6.6. Otherwise, if c is U+003A (:):
2025
- * 6.6.1. Increase pointer by 1.
2026
- * 6.6.2. If c is the EOF code point, validation error, return failure.
2027
- */
2028
- walker.pointer++;
2029
- if (walker.c() === EOF) {
2030
- this.validationError("Invalid IP v6 address.");
2031
- return null;
2032
- }
2033
- }
2034
- else if (walker.c() !== EOF) {
2035
- /**
2036
- * 6.7. Otherwise, if c is not the EOF code point, validation error,
2037
- * return failure.
2038
- */
2039
- this.validationError("Invalid IP v6 address.");
2040
- return null;
2041
- }
2042
- /**
2043
- * 6.8. Set address[pieceIndex] to value.
2044
- * 6.9. Increase pieceIndex by 1.
2045
- */
2046
- address[pieceIndex] = value;
2047
- pieceIndex++;
2048
- }
2049
- /**
2050
- * 7. If compress is non-null, then:
2051
- * 7.1. Let swaps be pieceIndex − compress.
2052
- * 7.2. Set pieceIndex to 7.
2053
- * 7.3. While pieceIndex is not 0 and swaps is greater than 0, swap
2054
- * address[pieceIndex] with address[compress + swaps − 1], and then decrease
2055
- * both pieceIndex and swaps by 1.
2056
- */
2057
- if (compress !== null) {
2058
- let swaps = pieceIndex - compress;
2059
- pieceIndex = 7;
2060
- while (pieceIndex !== 0 && swaps > 0) {
2061
- [address[pieceIndex], address[compress + swaps - 1]] =
2062
- [address[compress + swaps - 1], address[pieceIndex]];
2063
- pieceIndex--;
2064
- swaps--;
2065
- }
2066
- }
2067
- else if (compress === null && pieceIndex !== 8) {
2068
- /**
2069
- * 8. Otherwise, if compress is null and pieceIndex is not 8,
2070
- * validation error, return failure.
2071
- */
2072
- this.validationError("Invalid IP v6 address.");
2073
- return null;
2074
- }
2075
- /**
2076
- * 9. Return address.
2077
- */
2078
- return address;
2079
- }
2080
- /**
2081
- * Parses an opaque host string.
2082
- *
2083
- * @param input - a string
2084
- */
2085
- opaqueHostParser(input) {
2086
- /**
2087
- * 1. If input contains a forbidden host code point excluding U+0025 (%),
2088
- * validation error, return failure.
2089
- * 2. Let output be the empty string.
2090
- * 3. For each code point in input, UTF-8 percent encode it using the C0
2091
- * control percent-encode set, and append the result to output.
2092
- * 4. Return output.
2093
- */
2094
- const forbiddenChars = /[\x00\t\f\r #/:?@\[\\\]]/;
2095
- if (forbiddenChars.test(input)) {
2096
- this.validationError("Invalid host string.");
2097
- return null;
2098
- }
2099
- let output = "";
2100
- for (const codePoint of input) {
2101
- output += this.utf8PercentEncode(codePoint, this._c0ControlPercentEncodeSet);
2102
- }
2103
- return output;
2104
- }
2105
- /**
2106
- * Resolves a Blob URL from the user agent's Blob URL store.
2107
- * This function is not implemented.
2108
- * See: https://w3c.github.io/FileAPI/#blob-url-resolve
2109
- *
2110
- * @param url - an url
2111
- */
2112
- resolveABlobURL(url) {
2113
- return null;
2114
- }
2115
- /**
2116
- * Percent encodes a byte.
2117
- *
2118
- * @param value - a byte
2119
- */
2120
- percentEncode(value) {
2121
- /**
2122
- * To percent encode a byte into a percent-encoded byte, return a string
2123
- * consisting of U+0025 (%), followed by two ASCII upper hex digits
2124
- * representing byte.
2125
- */
2126
- return '%' + ('00' + value.toString(16).toUpperCase()).slice(-2);
2127
- }
2128
- /**
2129
- * Percent decodes a byte sequence input.
2130
- *
2131
- * @param input - a byte sequence
2132
- */
2133
- percentDecode(input) {
2134
- const isHexDigit = (byte) => {
2135
- return (byte >= 0x30 && byte <= 0x39) || (byte >= 0x41 && byte <= 0x46) ||
2136
- (byte >= 0x61 && byte <= 0x66);
2137
- };
2138
- /**
2139
- * 1. Let output be an empty byte sequence.
2140
- * 2. For each byte byte in input:
2141
- */
2142
- const output = new Uint8Array(input.length);
2143
- let n = 0;
2144
- for (let i = 0; i < input.length; i++) {
2145
- const byte = input[i];
2146
- /**
2147
- * 2.1. If byte is not 0x25 (%), then append byte to output.
2148
- * 2.2. Otherwise, if byte is 0x25 (%) and the next two bytes after byte
2149
- * in input are not in the ranges 0x30 (0) to 0x39 (9), 0x41 (A)
2150
- * to 0x46 (F), and 0x61 (a) to 0x66 (f), all inclusive, append byte
2151
- * to output.
2152
- * 2.3. Otherwise:
2153
- * 2.3.1. Let bytePoint be the two bytes after byte in input, decoded,
2154
- * and then interpreted as hexadecimal number.
2155
- * 2.3.2. Append a byte whose value is bytePoint to output.
2156
- * 2.3.3. Skip the next two bytes in input.
2157
- */
2158
- if (byte !== 0x25) {
2159
- output[n] = byte;
2160
- n++;
2161
- }
2162
- else if (byte === 0x25 && i >= input.length - 2) {
2163
- output[n] = byte;
2164
- n++;
2165
- }
2166
- else if (byte === 0x25 && (!isHexDigit(input[i + 1]) || !isHexDigit(input[i + 2]))) {
2167
- output[n] = byte;
2168
- n++;
2169
- }
2170
- else {
2171
- const bytePoint = parseInt(util_1.utf8Decode(Uint8Array.of(input[i + 1], input[i + 2])), 16);
2172
- output[n] = bytePoint;
2173
- n++;
2174
- i += 2;
2175
- }
2176
- }
2177
- return output.subarray(0, n);
2178
- }
2179
- /**
2180
- * String percent decodes a string.
2181
- *
2182
- * @param input - a string
2183
- */
2184
- stringPercentDecode(input) {
2185
- /**
2186
- * 1. Let bytes be the UTF-8 encoding of input.
2187
- * 2. Return the percent decoding of bytes.
2188
- */
2189
- return this.percentDecode(util_1.utf8Encode(input));
2190
- }
2191
- /**
2192
- * UTF-8 percent encodes a code point, using a percent encode set.
2193
- *
2194
- * @param codePoint - a code point
2195
- * @param percentEncodeSet - a percent encode set
2196
- */
2197
- utf8PercentEncode(codePoint, percentEncodeSet) {
2198
- /**
2199
- * 1. If codePoint is not in percentEncodeSet, then return codePoint.
2200
- * 2. Let bytes be the result of running UTF-8 encode on codePoint.
2201
- * 3. Percent encode each byte in bytes, and then return the results
2202
- * concatenated, in the same order.
2203
- */
2204
- if (!percentEncodeSet.test(codePoint))
2205
- return codePoint;
2206
- const bytes = util_1.utf8Encode(codePoint);
2207
- let result = "";
2208
- for (const byte of bytes) {
2209
- result += this.percentEncode(byte);
2210
- }
2211
- return result;
2212
- }
2213
- /**
2214
- * Determines if two hosts are considered equal.
2215
- *
2216
- * @param hostA - a host
2217
- * @param hostB - a host
2218
- */
2219
- hostEquals(hostA, hostB) {
2220
- return hostA === hostB;
2221
- }
2222
- /**
2223
- * Determines if two URLs are considered equal.
2224
- *
2225
- * @param urlA - a URL
2226
- * @param urlB - a URL
2227
- * @param excludeFragmentsFlag - whether to ignore fragments while comparing
2228
- */
2229
- urlEquals(urlA, urlB, excludeFragmentsFlag = false) {
2230
- /**
2231
- * 1. Let serializedA be the result of serializing A, with the exclude
2232
- * fragment flag set if the exclude fragments flag is set.
2233
- * 2. Let serializedB be the result of serializing B, with the exclude
2234
- * fragment flag set if the exclude fragments flag is set.
2235
- * 3. Return true if serializedA is serializedB, and false otherwise.
2236
- */
2237
- return this.urlSerializer(urlA, excludeFragmentsFlag) ===
2238
- this.urlSerializer(urlB, excludeFragmentsFlag);
2239
- }
2240
- /**
2241
- * Parses an `application/x-www-form-urlencoded` string.
2242
- *
2243
- * @param input - a string
2244
- */
2245
- urlEncodedStringParser(input) {
2246
- /**
2247
- * The application/x-www-form-urlencoded string parser takes a string input,
2248
- * UTF-8 encodes it, and then returns the result of
2249
- * application/x-www-form-urlencoded parsing it.
2250
- */
2251
- return this.urlEncodedParser(util_1.utf8Encode(input));
2252
- }
2253
- /**
2254
- * Parses `application/x-www-form-urlencoded` bytes.
2255
- *
2256
- * @param input - a byte sequence
2257
- */
2258
- urlEncodedParser(input) {
2259
- /**
2260
- * 1. Let sequences be the result of splitting input on 0x26 (&).
2261
- */
2262
- const sequences = [];
2263
- let currentSequence = [];
2264
- for (const byte of input) {
2265
- if (byte === 0x26) {
2266
- sequences.push(Uint8Array.from(currentSequence));
2267
- currentSequence = [];
2268
- }
2269
- else {
2270
- currentSequence.push(byte);
2271
- }
2272
- }
2273
- if (currentSequence.length !== 0) {
2274
- sequences.push(Uint8Array.from(currentSequence));
2275
- }
2276
- /**
2277
- * 2. Let output be an initially empty list of name-value tuples where both name and value hold a string.
2278
- */
2279
- const output = [];
2280
- /**
2281
- * 3. For each byte sequence bytes in sequences:
2282
- */
2283
- for (const bytes of sequences) {
2284
- /**
2285
- * 3.1. If bytes is the empty byte sequence, then continue.
2286
- */
2287
- if (bytes.length === 0)
2288
- continue;
2289
- /**
2290
- * 3.2. If bytes contains a 0x3D (=), then let name be the bytes from the
2291
- * start of bytes up to but excluding its first 0x3D (=), and let value be
2292
- * the bytes, if any, after the first 0x3D (=) up to the end of bytes.
2293
- * If 0x3D (=) is the first byte, then name will be the empty byte
2294
- * sequence. If it is the last, then value will be the empty byte sequence.
2295
- * 3.3. Otherwise, let name have the value of bytes and let value be the
2296
- * empty byte sequence.
2297
- */
2298
- const index = bytes.indexOf(0x3D);
2299
- const name = (index !== -1 ? bytes.slice(0, index) : bytes);
2300
- const value = (index !== -1 ? bytes.slice(index + 1) : new Uint8Array());
2301
- /**
2302
- * 3.4. Replace any 0x2B (+) in name and value with 0x20 (SP).
2303
- */
2304
- for (let i = 0; i < name.length; i++)
2305
- if (name[i] === 0x2B)
2306
- name[i] = 0x20;
2307
- for (let i = 0; i < value.length; i++)
2308
- if (value[i] === 0x2B)
2309
- value[i] = 0x20;
2310
- /**
2311
- * 3.5. Let nameString and valueString be the result of running UTF-8
2312
- * decode without BOM on the percent decoding of name and value,
2313
- * respectively.
2314
- */
2315
- const nameString = util_1.utf8Decode(name);
2316
- const valueString = util_1.utf8Decode(value);
2317
- /**
2318
- * 3.6. Append (nameString, valueString) to output.
2319
- */
2320
- output.push([nameString, valueString]);
2321
- }
2322
- /**
2323
- * 4. Return output.
2324
- */
2325
- return output;
2326
- }
2327
- /**
2328
- * Serializes `application/x-www-form-urlencoded` bytes.
2329
- *
2330
- * @param input - a byte sequence
2331
- */
2332
- urlEncodedByteSerializer(input) {
2333
- /**
2334
- * 1. Let output be the empty string.
2335
- * 2. For each byte in input, depending on byte:
2336
- * 0x20 (SP)
2337
- * Append U+002B (+) to output.
2338
- *
2339
- * 0x2A (*)
2340
- * 0x2D (-)
2341
- * 0x2E (.)
2342
- * 0x30 (0) to 0x39 (9)
2343
- * 0x41 (A) to 0x5A (Z)
2344
- * 0x5F (_)
2345
- * 0x61 (a) to 0x7A (z)
2346
- * Append a code point whose value is byte to output.
2347
- *
2348
- * Otherwise
2349
- * Append byte, percent encoded, to output.
2350
- * 3. Return output.
2351
- */
2352
- let output = "";
2353
- for (const byte of input) {
2354
- if (byte === 0x20) {
2355
- output += '+';
2356
- }
2357
- else if (byte === 0x2A || byte === 0x2D || byte === 0x2E ||
2358
- (byte >= 0x30 && byte <= 0x39) || (byte >= 0x41 && byte <= 0x5A) ||
2359
- byte === 0x5F || (byte >= 0x61 && byte <= 0x7A)) {
2360
- output += String.fromCodePoint(byte);
2361
- }
2362
- else {
2363
- output += this.percentEncode(byte);
2364
- }
2365
- }
2366
- return output;
2367
- }
2368
- /**
2369
- * Serializes `application/x-www-form-urlencoded` tuples.
2370
- *
2371
- * @param input - input tuple of name/value pairs
2372
- * @param encodingOverride: encoding override
2373
- */
2374
- urlEncodedSerializer(tuples, encodingOverride) {
2375
- /**
2376
- * 1. Let encoding be UTF-8.
2377
- * 2. If encoding override is given, set encoding to the result of getting
2378
- * an output encoding from encoding override.
2379
- */
2380
- const encoding = (encodingOverride === undefined ||
2381
- encodingOverride === "replacement" || encodingOverride === "UTF-16BE" ||
2382
- encodingOverride === "UTF-16LE" ? "UTF-8" : encodingOverride);
2383
- if (encoding.toUpperCase() !== "UTF-8") {
2384
- throw new Error("Only UTF-8 encoding is supported.");
2385
- }
2386
- /**
2387
- * 3. Let output be the empty string.
2388
- */
2389
- let output = "";
2390
- /**
2391
- * 4. For each tuple in tuples:
2392
- */
2393
- for (const tuple of tuples) {
2394
- /**
2395
- * 4.1. Let name be the result of serializing the result of encoding
2396
- * tuple’s name, using encoding.
2397
- */
2398
- const name = this.urlEncodedByteSerializer(util_1.utf8Encode(tuple[0]));
2399
- /**
2400
- * 4.2. Let value be tuple’s value.
2401
- */
2402
- let value = tuple[1];
2403
- /**
2404
- * TODO:
2405
- * 4.3. If value is a file, then set value to value’s filename.
2406
- */
2407
- /**
2408
- * 4.4. Set value to the result of serializing the result of encoding
2409
- * value, using encoding.
2410
- */
2411
- value = this.urlEncodedByteSerializer(util_1.utf8Encode(value));
2412
- /**
2413
- * 4.5. If tuple is not the first pair in tuples, then append U+0026 (&)
2414
- * to output.
2415
- */
2416
- if (output !== "")
2417
- output += '&';
2418
- /**
2419
- * 4.6. Append name, followed by U+003D (=), followed by value, to output.
2420
- */
2421
- output += name + '=' + value;
2422
- }
2423
- /**
2424
- * 5. Return output.
2425
- */
2426
- return output;
2427
- }
2428
- /**
2429
- * Returns a URL's origin.
2430
- *
2431
- * @param url - a URL
2432
- */
2433
- origin(url) {
2434
- /**
2435
- * A URL’s origin is the origin returned by running these steps, switching
2436
- * on URL’s scheme:
2437
- * "blob"
2438
- * 1. If URL’s blob URL entry is non-null, then return URL’s blob URL
2439
- * entry’s environment’s origin.
2440
- * 2. Let url be the result of parsing URL’s path[0].
2441
- * 3. Return a new opaque origin, if url is failure, and url’s origin
2442
- * otherwise.
2443
- * "ftp"
2444
- * "http"
2445
- * "https"
2446
- * "ws"
2447
- * "wss"
2448
- * Return a tuple consisting of URL’s scheme, URL’s host, URL’s port, and
2449
- * null.
2450
- * "file"
2451
- * Unfortunate as it is, this is left as an exercise to the reader. When in
2452
- * doubt, return a new opaque origin.
2453
- * Otherwise
2454
- * Return a new opaque origin.
2455
- */
2456
- switch (url.scheme) {
2457
- case "blob":
2458
- if (url._blobURLEntry !== null) {
2459
- // TODO: return URL’s blob URL entry’s environment’s origin.
2460
- }
2461
- const parsedURL = this.basicURLParser(url.path[0]);
2462
- if (parsedURL === null)
2463
- return interfaces_1.OpaqueOrigin;
2464
- else
2465
- return this.origin(parsedURL);
2466
- case "ftp":
2467
- case "http":
2468
- case "https":
2469
- case "ws":
2470
- case "wss":
2471
- return [url.scheme, url.host === null ? "" : url.host, url.port, null];
2472
- case "file":
2473
- return interfaces_1.OpaqueOrigin;
2474
- default:
2475
- return interfaces_1.OpaqueOrigin;
2476
- }
2477
- }
2478
- /**
2479
- * Converts a domain string to ASCII.
2480
- *
2481
- * @param domain - a domain string
2482
- */
2483
- domainToASCII(domain, beStrict = false) {
2484
- /**
2485
- * 1. If beStrict is not given, set it to false.
2486
- * 2. Let result be the result of running Unicode ToASCII with domain_name
2487
- * set to domain, UseSTD3ASCIIRules set to beStrict, CheckHyphens set to
2488
- * false, CheckBidi set to true, CheckJoiners set to true,
2489
- * Transitional_Processing set to false, and VerifyDnsLength set to beStrict.
2490
- * 3. If result is a failure value, validation error, return failure.
2491
- * 4. Return result.
2492
- */
2493
- const result = uts46_1.toASCII(domain, { useSTD3ASCIIRules: beStrict,
2494
- checkHyphens: false, checkBidi: true, checkJoiners: true,
2495
- transitionalProcessing: false, verifyDnsLength: beStrict });
2496
- if (result === null) {
2497
- this.validationError("Invalid domain name.");
2498
- return null;
2499
- }
2500
- return result;
2501
- }
2502
- /**
2503
- * Converts a domain string to Unicode.
2504
- *
2505
- * @param domain - a domain string
2506
- */
2507
- domainToUnicode(domain, beStrict = false) {
2508
- /**
2509
- * 1. Let result be the result of running Unicode ToUnicode with domain_name
2510
- * set to domain, CheckHyphens set to false, CheckBidi set to true,
2511
- * CheckJoiners set to true, UseSTD3ASCIIRules set to false, and
2512
- * Transitional_Processing set to false.
2513
- * 2. Signify validation errors for any returned errors, and then,
2514
- * return result.
2515
- */
2516
- const output = { errors: false };
2517
- const result = uts46_1.toUnicode(domain, { checkHyphens: false, checkBidi: true,
2518
- checkJoiners: true, useSTD3ASCIIRules: false,
2519
- transitionalProcessing: false }, output);
2520
- if (output.errors) {
2521
- this.validationError("Invalid domain name.");
2522
- }
2523
- return result;
2524
- }
2525
- /**
2526
- * Serializes an origin.
2527
- * This function is from the HTML spec:
2528
- * https://html.spec.whatwg.org/#ascii-serialisation-of-an-origin
2529
- *
2530
- * @param origin - an origin
2531
- */
2532
- asciiSerializationOfAnOrigin(origin) {
2533
- /**
2534
- * 1. If origin is an opaque origin, then return "null".
2535
- * 2. Otherwise, let result be origin's scheme.
2536
- * 3. Append "://" to result.
2537
- * 4. Append origin's host, serialized, to result.
2538
- * 5. If origin's port is non-null, append a U+003A COLON character (:),
2539
- * and origin's port, serialized, to result.
2540
- * 6. Return result.
2541
- */
2542
- if (origin[0] === "" && origin[1] === "" && origin[2] === null && origin[3] === null) {
2543
- return "null";
2544
- }
2545
- let result = origin[0] + "://" + this.hostSerializer(origin[1]);
2546
- if (origin[2] !== null)
2547
- result += ":" + origin[2].toString();
2548
- return result;
2549
- }
2550
- }
2551
- exports.URLAlgorithm = URLAlgorithm;
2552
- //# sourceMappingURL=URLAlgorithm.js.map