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