oak-domain 3.0.4 → 4.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (220) hide show
  1. package/lib/actions/action.d.ts +16 -16
  2. package/lib/actions/action.js +17 -17
  3. package/lib/actions/relation.d.ts +5 -5
  4. package/lib/actions/relation.js +25 -25
  5. package/lib/base-app-domain/ActionAuth/Schema.d.ts +186 -186
  6. package/lib/base-app-domain/ActionAuth/Storage.js +39 -39
  7. package/lib/base-app-domain/ActionDefDict.d.ts +8 -8
  8. package/lib/base-app-domain/ActionDefDict.js +9 -9
  9. package/lib/base-app-domain/EntityDict.d.ts +28 -28
  10. package/lib/base-app-domain/I18n/Schema.d.ts +129 -129
  11. package/lib/base-app-domain/I18n/Schema.js +2 -2
  12. package/lib/base-app-domain/I18n/Storage.d.ts +3 -3
  13. package/lib/base-app-domain/I18n/Storage.js +59 -59
  14. package/lib/base-app-domain/Modi/Action.d.ts +10 -10
  15. package/lib/base-app-domain/Modi/Action.js +14 -14
  16. package/lib/base-app-domain/Modi/Schema.d.ts +136 -136
  17. package/lib/base-app-domain/Modi/Storage.js +63 -63
  18. package/lib/base-app-domain/ModiEntity/Schema.d.ts +384 -384
  19. package/lib/base-app-domain/ModiEntity/Storage.js +30 -30
  20. package/lib/base-app-domain/Oper/Schema.d.ts +153 -153
  21. package/lib/base-app-domain/Oper/Storage.js +38 -38
  22. package/lib/base-app-domain/OperEntity/Schema.d.ts +373 -373
  23. package/lib/base-app-domain/OperEntity/Storage.js +30 -30
  24. package/lib/base-app-domain/Path/Schema.d.ts +149 -149
  25. package/lib/base-app-domain/Path/Schema.js +2 -2
  26. package/lib/base-app-domain/Path/Storage.d.ts +3 -3
  27. package/lib/base-app-domain/Path/Storage.js +54 -54
  28. package/lib/base-app-domain/Relation/Schema.d.ts +188 -188
  29. package/lib/base-app-domain/Relation/Storage.js +54 -54
  30. package/lib/base-app-domain/Relation.d.ts +2 -2
  31. package/lib/base-app-domain/Relation.js +4 -4
  32. package/lib/base-app-domain/RelationAuth/Schema.d.ts +214 -214
  33. package/lib/base-app-domain/RelationAuth/Storage.js +44 -44
  34. package/lib/base-app-domain/Storage.js +31 -31
  35. package/lib/base-app-domain/User/Action.d.ts +10 -10
  36. package/lib/base-app-domain/User/Action.js +12 -12
  37. package/lib/base-app-domain/User/Schema.d.ts +210 -210
  38. package/lib/base-app-domain/User/Storage.js +33 -33
  39. package/lib/base-app-domain/UserEntityClaim/Schema.d.ts +264 -264
  40. package/lib/base-app-domain/UserEntityClaim/Schema.js +2 -2
  41. package/lib/base-app-domain/UserEntityClaim/Storage.d.ts +3 -3
  42. package/lib/base-app-domain/UserEntityClaim/Storage.js +37 -37
  43. package/lib/base-app-domain/UserEntityGrant/Action.d.ts +5 -5
  44. package/lib/base-app-domain/UserEntityGrant/Action.js +5 -5
  45. package/lib/base-app-domain/UserEntityGrant/Schema.d.ts +131 -131
  46. package/lib/base-app-domain/UserEntityGrant/Storage.js +25 -25
  47. package/lib/base-app-domain/UserRelation/Schema.d.ts +208 -208
  48. package/lib/base-app-domain/UserRelation/Storage.js +56 -56
  49. package/lib/base-app-domain/_SubQuery.d.ts +142 -142
  50. package/lib/base-app-domain/index.d.ts +4 -4
  51. package/lib/base-app-domain/index.js +7 -7
  52. package/lib/checkers/index.d.ts +5 -5
  53. package/lib/checkers/index.js +13 -13
  54. package/lib/compiler/entities.d.ts +2 -0
  55. package/lib/compiler/entities.js +7 -0
  56. package/lib/compiler/env.d.ts +13 -14
  57. package/lib/compiler/env.js +45 -48
  58. package/lib/compiler/localeBuilder.d.ts +27 -22
  59. package/lib/compiler/localeBuilder.js +184 -173
  60. package/lib/compiler/schemalBuilder.d.ts +27 -27
  61. package/lib/compiler/schemalBuilder.js +3569 -3569
  62. package/lib/entities/ActionAuth.d.ts +10 -10
  63. package/lib/entities/ActionAuth.js +31 -31
  64. package/lib/entities/I18n.d.ts +9 -9
  65. package/lib/entities/I18n.js +36 -37
  66. package/lib/entities/Modi.js +49 -49
  67. package/lib/entities/ModiEntity.js +18 -18
  68. package/lib/entities/Oper.js +21 -21
  69. package/lib/entities/OperEntity.js +18 -18
  70. package/lib/entities/Path.d.ts +8 -8
  71. package/lib/entities/Path.js +35 -35
  72. package/lib/entities/Relation.d.ts +8 -8
  73. package/lib/entities/Relation.js +35 -35
  74. package/lib/entities/RelationAuth.d.ts +8 -8
  75. package/lib/entities/RelationAuth.js +34 -34
  76. package/lib/entities/User.js +31 -31
  77. package/lib/entities/UserEntityClaim.d.ts +13 -13
  78. package/lib/entities/UserEntityClaim.js +17 -17
  79. package/lib/entities/UserEntityGrant.d.ts +9 -9
  80. package/lib/entities/UserEntityGrant.js +15 -15
  81. package/lib/entities/UserRelation.d.ts +10 -10
  82. package/lib/entities/UserRelation.js +38 -38
  83. package/lib/index.d.ts +23 -23
  84. package/lib/index.js +37 -37
  85. package/lib/store/AsyncRowStore.d.ts +65 -59
  86. package/lib/store/AsyncRowStore.js +176 -116
  87. package/lib/store/CascadeStore.d.ts +106 -103
  88. package/lib/store/CascadeStore.js +1740 -1662
  89. package/lib/store/RelationAuth.d.ts +96 -96
  90. package/lib/store/RelationAuth.js +1336 -1334
  91. package/lib/store/SyncRowStore.d.ts +29 -29
  92. package/lib/store/SyncRowStore.js +50 -50
  93. package/lib/store/TriggerExecutor.d.ts +41 -38
  94. package/lib/store/TriggerExecutor.js +468 -459
  95. package/lib/store/actionAuth.d.ts +4 -4
  96. package/lib/store/actionAuth.js +25 -25
  97. package/lib/store/actionDef.d.ts +10 -10
  98. package/lib/store/actionDef.js +278 -278
  99. package/lib/store/checker.d.ts +26 -26
  100. package/lib/store/checker.js +487 -487
  101. package/lib/store/filter.d.ts +85 -122
  102. package/lib/store/filter.js +1652 -1623
  103. package/lib/store/modi.d.ts +13 -13
  104. package/lib/store/modi.js +254 -254
  105. package/lib/store/relation.d.ts +12 -12
  106. package/lib/store/relation.js +67 -67
  107. package/lib/timers/oper.d.ts +18 -18
  108. package/lib/timers/oper.js +57 -57
  109. package/lib/timers/vaccum.d.ts +20 -20
  110. package/lib/timers/vaccum.js +111 -111
  111. package/lib/triggers/index.d.ts +5 -5
  112. package/lib/triggers/index.js +8 -8
  113. package/lib/types/Action.d.ts +20 -20
  114. package/lib/types/AppLoader.d.ts +17 -17
  115. package/lib/types/AppLoader.js +10 -10
  116. package/lib/types/Auth.d.ts +70 -70
  117. package/lib/types/Cluster.d.ts +5 -0
  118. package/lib/types/Cluster.js +2 -0
  119. package/lib/types/Connector.d.ts +38 -38
  120. package/lib/types/Connector.js +2 -2
  121. package/lib/types/DataType.d.ts +25 -25
  122. package/lib/types/DataType.js +6 -6
  123. package/lib/types/Demand.d.ts +89 -88
  124. package/lib/types/Demand.js +10 -10
  125. package/lib/types/Endpoint.d.ts +11 -11
  126. package/lib/types/Entity.d.ts +209 -202
  127. package/lib/types/Entity.js +15 -15
  128. package/lib/types/EntityDesc.d.ts +9 -9
  129. package/lib/types/EntityDesc.js +2 -2
  130. package/lib/types/Environment.d.ts +90 -57
  131. package/lib/types/Environment.js +2 -2
  132. package/lib/types/Exception.d.ts +147 -147
  133. package/lib/types/Exception.js +406 -406
  134. package/lib/types/Expression.d.ts +163 -163
  135. package/lib/types/Expression.js +397 -394
  136. package/lib/types/Geo.d.ts +18 -18
  137. package/lib/types/Locale.d.ts +25 -25
  138. package/lib/types/Logger.d.ts +6 -6
  139. package/lib/types/Polyfill.d.ts +24 -24
  140. package/lib/types/Port.d.ts +18 -18
  141. package/lib/types/RowStore.d.ts +19 -18
  142. package/lib/types/RowStore.js +33 -33
  143. package/lib/types/Storage.d.ts +58 -58
  144. package/lib/types/Style.d.ts +11 -11
  145. package/lib/types/Timer.d.ts +20 -14
  146. package/lib/types/Trigger.d.ts +118 -124
  147. package/lib/types/Trigger.js +35 -58
  148. package/lib/types/Watcher.d.ts +19 -19
  149. package/lib/types/index.d.ts +26 -25
  150. package/lib/types/index.js +29 -28
  151. package/lib/types/schema/DataTypes.d.ts +34 -34
  152. package/lib/utils/SimpleConnector.d.ts +64 -64
  153. package/lib/utils/SimpleConnector.js +206 -206
  154. package/lib/utils/assert.d.ts +5 -5
  155. package/lib/utils/assert.js +9 -9
  156. package/lib/utils/concurrent.d.ts +15 -15
  157. package/lib/utils/concurrent.js +63 -63
  158. package/lib/utils/date.js +18 -18
  159. package/lib/utils/{url.d.ts → domain.d.ts} +1 -1
  160. package/lib/utils/{url.js → domain.js} +11 -13
  161. package/lib/utils/geo.js +24 -24
  162. package/lib/utils/lodash.d.ts +24 -22
  163. package/lib/utils/lodash.js +46 -42
  164. package/lib/utils/mask.js +34 -34
  165. package/lib/utils/money.d.ts +6 -6
  166. package/lib/utils/money.js +51 -51
  167. package/lib/utils/projection.d.ts +4 -4
  168. package/lib/utils/projection.js +15 -15
  169. package/lib/utils/random/random.js +19 -19
  170. package/lib/utils/random/random.mp.js +14 -14
  171. package/lib/utils/random/random.native.d.ts +1 -0
  172. package/lib/utils/random/random.native.js +14 -0
  173. package/lib/utils/random/random.web.js +11 -11
  174. package/lib/utils/string.d.ts +28 -28
  175. package/lib/utils/string.js +69 -69
  176. package/lib/utils/url/index.d.ts +7 -0
  177. package/lib/utils/url/index.js +8 -0
  178. package/lib/utils/url/index.mp.d.ts +3 -0
  179. package/lib/utils/url/index.mp.js +8 -0
  180. package/lib/utils/url/index.native.d.ts +12 -0
  181. package/lib/utils/url/index.native.js +7 -0
  182. package/lib/utils/url/index.web.d.ts +12 -0
  183. package/lib/utils/url/index.web.js +7 -0
  184. package/lib/utils/url/whatwg-url/index.d.ts +3 -0
  185. package/lib/utils/url/whatwg-url/index.js +16 -0
  186. package/lib/utils/url/whatwg-url/lib/URL-impl.d.ts +20 -0
  187. package/lib/utils/url/whatwg-url/lib/URL-impl.js +175 -0
  188. package/lib/utils/url/whatwg-url/lib/URL.d.ts +1 -0
  189. package/lib/utils/url/whatwg-url/lib/URL.js +298 -0
  190. package/lib/utils/url/whatwg-url/lib/URLSearchParams-impl.d.ts +19 -0
  191. package/lib/utils/url/whatwg-url/lib/URLSearchParams-impl.js +126 -0
  192. package/lib/utils/url/whatwg-url/lib/URLSearchParams.d.ts +1 -0
  193. package/lib/utils/url/whatwg-url/lib/URLSearchParams.js +426 -0
  194. package/lib/utils/url/whatwg-url/lib/infra.d.ts +12 -0
  195. package/lib/utils/url/whatwg-url/lib/infra.js +25 -0
  196. package/lib/utils/url/whatwg-url/lib/url-state-machine.d.ts +24 -0
  197. package/lib/utils/url/whatwg-url/lib/url-state-machine.js +1238 -0
  198. package/lib/utils/url/whatwg-url/lib/urlencoded.d.ts +15 -0
  199. package/lib/utils/url/whatwg-url/lib/urlencoded.js +162 -0
  200. package/lib/utils/url/whatwg-url/lib/utils.d.ts +37 -0
  201. package/lib/utils/url/whatwg-url/lib/utils.js +101 -0
  202. package/lib/utils/uuid.d.ts +17 -17
  203. package/lib/utils/uuid.js +218 -218
  204. package/lib/utils/validator.d.ts +26 -26
  205. package/lib/utils/validator.js +131 -131
  206. package/lib/utils/version.js +21 -21
  207. package/package.json +50 -49
  208. package/src/entities/ActionAuth.ts +41 -41
  209. package/src/entities/I18n.ts +45 -46
  210. package/src/entities/Modi.ts +69 -69
  211. package/src/entities/ModiEntity.ts +26 -26
  212. package/src/entities/Oper.ts +32 -32
  213. package/src/entities/OperEntity.ts +27 -27
  214. package/src/entities/Path.ts +43 -43
  215. package/src/entities/Relation.ts +43 -43
  216. package/src/entities/RelationAuth.ts +44 -44
  217. package/src/entities/User.ts +48 -48
  218. package/src/entities/UserEntityClaim.ts +29 -29
  219. package/src/entities/UserEntityGrant.ts +24 -24
  220. package/src/entities/UserRelation.ts +50 -50
@@ -0,0 +1,1238 @@
1
+ "use strict";
2
+ // @ts-nocheck
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.parseURL = exports.serializeInteger = exports.cannotHaveAUsernamePasswordPort = exports.serializeHost = exports.setThePassword = exports.setTheUsername = exports.basicURLParse = exports.serializeURLOrigin = exports.serializeURL = void 0;
5
+ const tslib_1 = require("tslib");
6
+ const buffer_1 = require("buffer");
7
+ const infra_1 = tslib_1.__importDefault(require("./infra"));
8
+ const urlencoded_1 = tslib_1.__importDefault(require("./urlencoded"));
9
+ const specialSchemes = {
10
+ ftp: 21,
11
+ file: null,
12
+ http: 80,
13
+ https: 443,
14
+ ws: 80,
15
+ wss: 443,
16
+ };
17
+ const failure = Symbol('failure');
18
+ function countSymbols(str) {
19
+ return urlencoded_1.default.ucs2decode(str).length;
20
+ }
21
+ function at(input, idx) {
22
+ const c = input[idx];
23
+ return isNaN(c) ? undefined : String.fromCodePoint(c);
24
+ }
25
+ function isSingleDot(buffer) {
26
+ return buffer === '.' || buffer.toLowerCase() === '%2e';
27
+ }
28
+ function isDoubleDot(buffer) {
29
+ buffer = buffer.toLowerCase();
30
+ return (buffer === '..' ||
31
+ buffer === '%2e.' ||
32
+ buffer === '.%2e' ||
33
+ buffer === '%2e%2e');
34
+ }
35
+ function isWindowsDriveLetterCodePoints(cp1, cp2) {
36
+ return infra_1.default.isASCIIAlpha(cp1) && (cp2 === 58 || cp2 === 124);
37
+ }
38
+ function isWindowsDriveLetterString(string) {
39
+ return (string.length === 2 &&
40
+ infra_1.default.isASCIIAlpha(string.codePointAt(0)) &&
41
+ (string[1] === ':' || string[1] === '|'));
42
+ }
43
+ function isNormalizedWindowsDriveLetterString(string) {
44
+ return (string.length === 2 &&
45
+ infra_1.default.isASCIIAlpha(string.codePointAt(0)) &&
46
+ string[1] === ':');
47
+ }
48
+ function containsForbiddenHostCodePoint(string) {
49
+ return (string.search(/\u0000|\u0009|\u000A|\u000D|\u0020|#|%|\/|:|\?|@|\[|\\|\]/) !== -1);
50
+ }
51
+ function containsForbiddenHostCodePointExcludingPercent(string) {
52
+ return (string.search(/\u0000|\u0009|\u000A|\u000D|\u0020|#|\/|:|\?|@|\[|\\|\]/) !== -1);
53
+ }
54
+ function isSpecialScheme(scheme) {
55
+ return specialSchemes[scheme] !== undefined;
56
+ }
57
+ function isSpecial(url) {
58
+ return isSpecialScheme(url.scheme);
59
+ }
60
+ function isNotSpecial(url) {
61
+ return !isSpecialScheme(url.scheme);
62
+ }
63
+ function defaultPort(scheme) {
64
+ return specialSchemes[scheme];
65
+ }
66
+ function utf8PercentEncode(c) {
67
+ const buf = buffer_1.Buffer.from(c);
68
+ let str = '';
69
+ for (let i = 0; i < buf.length; ++i) {
70
+ str += urlencoded_1.default.percentEncode(buf[i]);
71
+ }
72
+ return str;
73
+ }
74
+ function isC0ControlPercentEncode(c) {
75
+ return c <= 0x1f || c > 0x7e;
76
+ }
77
+ const extraUserinfoPercentEncodeSet = new Set([
78
+ 47, 58, 59, 61, 64, 91, 92, 93, 94, 124,
79
+ ]);
80
+ function isUserinfoPercentEncode(c) {
81
+ return isPathPercentEncode(c) || extraUserinfoPercentEncodeSet.has(c);
82
+ }
83
+ const extraFragmentPercentEncodeSet = new Set([32, 34, 60, 62, 96]);
84
+ function isFragmentPercentEncode(c) {
85
+ return isC0ControlPercentEncode(c) || extraFragmentPercentEncodeSet.has(c);
86
+ }
87
+ const extraPathPercentEncodeSet = new Set([35, 63, 123, 125]);
88
+ function isPathPercentEncode(c) {
89
+ return isFragmentPercentEncode(c) || extraPathPercentEncodeSet.has(c);
90
+ }
91
+ function percentEncodeChar(c, encodeSetPredicate) {
92
+ const cStr = String.fromCodePoint(c);
93
+ if (encodeSetPredicate(c)) {
94
+ return utf8PercentEncode(cStr);
95
+ }
96
+ return cStr;
97
+ }
98
+ function parseIPv4Number(input) {
99
+ let R = 10;
100
+ if (input.length >= 2 &&
101
+ input.charAt(0) === '0' &&
102
+ input.charAt(1).toLowerCase() === 'x') {
103
+ input = input.substring(2);
104
+ R = 16;
105
+ }
106
+ else if (input.length >= 2 && input.charAt(0) === '0') {
107
+ input = input.substring(1);
108
+ R = 8;
109
+ }
110
+ if (input === '') {
111
+ return 0;
112
+ }
113
+ let regex = /[^0-7]/;
114
+ if (R === 10) {
115
+ regex = /[^0-9]/;
116
+ }
117
+ if (R === 16) {
118
+ regex = /[^0-9A-Fa-f]/;
119
+ }
120
+ if (regex.test(input)) {
121
+ return failure;
122
+ }
123
+ return parseInt(input, R);
124
+ }
125
+ function parseIPv4(input) {
126
+ const parts = input.split('.');
127
+ if (parts[parts.length - 1] === '') {
128
+ if (parts.length > 1) {
129
+ parts.pop();
130
+ }
131
+ }
132
+ if (parts.length > 4) {
133
+ return input;
134
+ }
135
+ const numbers = [];
136
+ for (const part of parts) {
137
+ if (part === '') {
138
+ return input;
139
+ }
140
+ const n = parseIPv4Number(part);
141
+ if (n === failure) {
142
+ return input;
143
+ }
144
+ numbers.push(n);
145
+ }
146
+ for (let i = 0; i < numbers.length - 1; ++i) {
147
+ if (numbers[i] > 255) {
148
+ return failure;
149
+ }
150
+ }
151
+ if (numbers[numbers.length - 1] >= Math.pow(256, 5 - numbers.length)) {
152
+ return failure;
153
+ }
154
+ let ipv4 = numbers.pop();
155
+ let counter = 0;
156
+ for (const n of numbers) {
157
+ ipv4 += n * Math.pow(256, 3 - counter);
158
+ ++counter;
159
+ }
160
+ return ipv4;
161
+ }
162
+ function serializeIPv4(address) {
163
+ let output = '';
164
+ let n = address;
165
+ for (let i = 1; i <= 4; ++i) {
166
+ output = String(n % 256) + output;
167
+ if (i !== 4) {
168
+ output = '.' + output;
169
+ }
170
+ n = Math.floor(n / 256);
171
+ }
172
+ return output;
173
+ }
174
+ function parseIPv6(input) {
175
+ const address = [0, 0, 0, 0, 0, 0, 0, 0];
176
+ let pieceIndex = 0;
177
+ let compress = null;
178
+ let pointer = 0;
179
+ input = urlencoded_1.default.ucs2decode(input);
180
+ if (input[pointer] === 58) {
181
+ if (input[pointer + 1] !== 58) {
182
+ return failure;
183
+ }
184
+ pointer += 2;
185
+ ++pieceIndex;
186
+ compress = pieceIndex;
187
+ }
188
+ while (pointer < input.length) {
189
+ if (pieceIndex === 8) {
190
+ return failure;
191
+ }
192
+ if (input[pointer] === 58) {
193
+ if (compress !== null) {
194
+ return failure;
195
+ }
196
+ ++pointer;
197
+ ++pieceIndex;
198
+ compress = pieceIndex;
199
+ continue;
200
+ }
201
+ let value = 0;
202
+ let length = 0;
203
+ while (length < 4 && infra_1.default.isASCIIHex(input[pointer])) {
204
+ value = value * 0x10 + parseInt(at(input, pointer), 16);
205
+ ++pointer;
206
+ ++length;
207
+ }
208
+ if (input[pointer] === 46) {
209
+ if (length === 0) {
210
+ return failure;
211
+ }
212
+ pointer -= length;
213
+ if (pieceIndex > 6) {
214
+ return failure;
215
+ }
216
+ let numbersSeen = 0;
217
+ while (input[pointer] !== undefined) {
218
+ let ipv4Piece = null;
219
+ if (numbersSeen > 0) {
220
+ if (input[pointer] === 46 && numbersSeen < 4) {
221
+ ++pointer;
222
+ }
223
+ else {
224
+ return failure;
225
+ }
226
+ }
227
+ if (!infra_1.default.isASCIIDigit(input[pointer])) {
228
+ return failure;
229
+ }
230
+ while (infra_1.default.isASCIIDigit(input[pointer])) {
231
+ const number = parseInt(at(input, pointer));
232
+ if (ipv4Piece === null) {
233
+ ipv4Piece = number;
234
+ }
235
+ else if (ipv4Piece === 0) {
236
+ return failure;
237
+ }
238
+ else {
239
+ ipv4Piece = ipv4Piece * 10 + number;
240
+ }
241
+ if (ipv4Piece > 255) {
242
+ return failure;
243
+ }
244
+ ++pointer;
245
+ }
246
+ address[pieceIndex] = address[pieceIndex] * 0x100 + ipv4Piece;
247
+ ++numbersSeen;
248
+ if (numbersSeen === 2 || numbersSeen === 4) {
249
+ ++pieceIndex;
250
+ }
251
+ }
252
+ if (numbersSeen !== 4) {
253
+ return failure;
254
+ }
255
+ break;
256
+ }
257
+ else if (input[pointer] === 58) {
258
+ ++pointer;
259
+ if (input[pointer] === undefined) {
260
+ return failure;
261
+ }
262
+ }
263
+ else if (input[pointer] !== undefined) {
264
+ return failure;
265
+ }
266
+ address[pieceIndex] = value;
267
+ ++pieceIndex;
268
+ }
269
+ if (compress !== null) {
270
+ let swaps = pieceIndex - compress;
271
+ pieceIndex = 7;
272
+ while (pieceIndex !== 0 && swaps > 0) {
273
+ const temp = address[compress + swaps - 1];
274
+ address[compress + swaps - 1] = address[pieceIndex];
275
+ address[pieceIndex] = temp;
276
+ --pieceIndex;
277
+ --swaps;
278
+ }
279
+ }
280
+ else if (compress === null && pieceIndex !== 8) {
281
+ return failure;
282
+ }
283
+ return address;
284
+ }
285
+ function serializeIPv6(address) {
286
+ let output = '';
287
+ const seqResult = findLongestZeroSequence(address);
288
+ const compress = seqResult.idx;
289
+ let ignore0 = false;
290
+ for (let pieceIndex = 0; pieceIndex <= 7; ++pieceIndex) {
291
+ if (ignore0 && address[pieceIndex] === 0) {
292
+ continue;
293
+ }
294
+ else if (ignore0) {
295
+ ignore0 = false;
296
+ }
297
+ if (compress === pieceIndex) {
298
+ const separator = pieceIndex === 0 ? '::' : ':';
299
+ output += separator;
300
+ ignore0 = true;
301
+ continue;
302
+ }
303
+ output += address[pieceIndex].toString(16);
304
+ if (pieceIndex !== 7) {
305
+ output += ':';
306
+ }
307
+ }
308
+ return output;
309
+ }
310
+ function parseHost(input, isNotSpecialArg = false) {
311
+ if (input[0] === '[') {
312
+ if (input[input.length - 1] !== ']') {
313
+ return failure;
314
+ }
315
+ return parseIPv6(input.substring(1, input.length - 1));
316
+ }
317
+ if (isNotSpecialArg) {
318
+ return parseOpaqueHost(input);
319
+ }
320
+ const domain = urlencoded_1.default.percentDecode(buffer_1.Buffer.from(input)).toString();
321
+ const asciiDomain = domainToASCII(domain);
322
+ if (asciiDomain === failure) {
323
+ return failure;
324
+ }
325
+ if (containsForbiddenHostCodePoint(asciiDomain)) {
326
+ return failure;
327
+ }
328
+ const ipv4Host = parseIPv4(asciiDomain);
329
+ if (typeof ipv4Host === 'number' || ipv4Host === failure) {
330
+ return ipv4Host;
331
+ }
332
+ return asciiDomain;
333
+ }
334
+ function parseOpaqueHost(input) {
335
+ if (containsForbiddenHostCodePointExcludingPercent(input)) {
336
+ return failure;
337
+ }
338
+ let output = '';
339
+ const decoded = urlencoded_1.default.ucs2decode(input);
340
+ for (let i = 0; i < decoded.length; ++i) {
341
+ output += percentEncodeChar(decoded[i], isC0ControlPercentEncode);
342
+ }
343
+ return output;
344
+ }
345
+ function findLongestZeroSequence(arr) {
346
+ let maxIdx = null;
347
+ let maxLen = 1; // only find elements > 1
348
+ let currStart = null;
349
+ let currLen = 0;
350
+ for (let i = 0; i < arr.length; ++i) {
351
+ if (arr[i] !== 0) {
352
+ if (currLen > maxLen) {
353
+ maxIdx = currStart;
354
+ maxLen = currLen;
355
+ }
356
+ currStart = null;
357
+ currLen = 0;
358
+ }
359
+ else {
360
+ if (currStart === null) {
361
+ currStart = i;
362
+ }
363
+ ++currLen;
364
+ }
365
+ }
366
+ // if trailing zeros
367
+ if (currLen > maxLen) {
368
+ maxIdx = currStart;
369
+ maxLen = currLen;
370
+ }
371
+ return {
372
+ idx: maxIdx,
373
+ len: maxLen,
374
+ };
375
+ }
376
+ function serializeHost(host) {
377
+ if (typeof host === 'number') {
378
+ return serializeIPv4(host);
379
+ }
380
+ // IPv6 serializer
381
+ if (host instanceof Array) {
382
+ return '[' + serializeIPv6(host) + ']';
383
+ }
384
+ return host;
385
+ }
386
+ function domainToASCII(domain) {
387
+ const result = domain;
388
+ if (result === null) {
389
+ return failure;
390
+ }
391
+ return result;
392
+ }
393
+ function trimControlChars(url) {
394
+ return url.replace(/^[\u0000-\u001F\u0020]+|[\u0000-\u001F\u0020]+$/g, '');
395
+ }
396
+ function trimTabAndNewline(url) {
397
+ return url.replace(/\u0009|\u000A|\u000D/g, '');
398
+ }
399
+ function shortenPath(url) {
400
+ const { path } = url;
401
+ if (path.length === 0) {
402
+ return;
403
+ }
404
+ if (url.scheme === 'file' &&
405
+ path.length === 1 &&
406
+ isNormalizedWindowsDriveLetter(path[0])) {
407
+ return;
408
+ }
409
+ path.pop();
410
+ }
411
+ function includesCredentials(url) {
412
+ return url.username !== '' || url.password !== '';
413
+ }
414
+ function cannotHaveAUsernamePasswordPort(url) {
415
+ return (url.host === null ||
416
+ url.host === '' ||
417
+ url.cannotBeABaseURL ||
418
+ url.scheme === 'file');
419
+ }
420
+ function isNormalizedWindowsDriveLetter(string) {
421
+ return /^[A-Za-z]:$/.test(string);
422
+ }
423
+ function URLStateMachine(input, base, encodingOverride, url, stateOverride) {
424
+ this.pointer = 0;
425
+ this.input = input;
426
+ this.base = base || null;
427
+ this.encodingOverride = encodingOverride || 'utf-8';
428
+ this.stateOverride = stateOverride;
429
+ this.url = url;
430
+ this.failure = false;
431
+ this.parseError = false;
432
+ if (!this.url) {
433
+ this.url = {
434
+ scheme: '',
435
+ username: '',
436
+ password: '',
437
+ host: null,
438
+ port: null,
439
+ path: [],
440
+ query: null,
441
+ fragment: null,
442
+ cannotBeABaseURL: false,
443
+ };
444
+ const res = trimControlChars(this.input);
445
+ if (res !== this.input) {
446
+ this.parseError = true;
447
+ }
448
+ this.input = res;
449
+ }
450
+ const res = trimTabAndNewline(this.input);
451
+ if (res !== this.input) {
452
+ this.parseError = true;
453
+ }
454
+ this.input = res;
455
+ this.state = stateOverride || 'scheme start';
456
+ this.buffer = '';
457
+ this.atFlag = false;
458
+ this.arrFlag = false;
459
+ this.passwordTokenSeenFlag = false;
460
+ this.input = urlencoded_1.default.ucs2decode(this.input);
461
+ for (; this.pointer <= this.input.length; ++this.pointer) {
462
+ const c = this.input[this.pointer];
463
+ const cStr = isNaN(c) ? undefined : String.fromCodePoint(c);
464
+ // exec state machine
465
+ const ret = this['parse ' + this.state](c, cStr);
466
+ if (!ret) {
467
+ break; // terminate algorithm
468
+ }
469
+ else if (ret === failure) {
470
+ this.failure = true;
471
+ break;
472
+ }
473
+ }
474
+ }
475
+ URLStateMachine.prototype['parse scheme start'] = function parseSchemeStart(c, cStr) {
476
+ if (infra_1.default.isASCIIAlpha(c)) {
477
+ this.buffer += cStr.toLowerCase();
478
+ this.state = 'scheme';
479
+ }
480
+ else if (!this.stateOverride) {
481
+ this.state = 'no scheme';
482
+ --this.pointer;
483
+ }
484
+ else {
485
+ this.parseError = true;
486
+ return failure;
487
+ }
488
+ return true;
489
+ };
490
+ URLStateMachine.prototype['parse scheme'] = function parseScheme(c, cStr) {
491
+ if (infra_1.default.isASCIIAlphanumeric(c) || c === 43 || c === 45 || c === 46) {
492
+ this.buffer += cStr.toLowerCase();
493
+ }
494
+ else if (c === 58) {
495
+ if (this.stateOverride) {
496
+ if (isSpecial(this.url) && !isSpecialScheme(this.buffer)) {
497
+ return false;
498
+ }
499
+ if (!isSpecial(this.url) && isSpecialScheme(this.buffer)) {
500
+ return false;
501
+ }
502
+ if ((includesCredentials(this.url) || this.url.port !== null) &&
503
+ this.buffer === 'file') {
504
+ return false;
505
+ }
506
+ if (this.url.scheme === 'file' &&
507
+ (this.url.host === '' || this.url.host === null)) {
508
+ return false;
509
+ }
510
+ }
511
+ this.url.scheme = this.buffer;
512
+ if (this.stateOverride) {
513
+ if (this.url.port === defaultPort(this.url.scheme)) {
514
+ this.url.port = null;
515
+ }
516
+ return false;
517
+ }
518
+ this.buffer = '';
519
+ if (this.url.scheme === 'file') {
520
+ if (this.input[this.pointer + 1] !== 47 ||
521
+ this.input[this.pointer + 2] !== 47) {
522
+ this.parseError = true;
523
+ }
524
+ this.state = 'file';
525
+ }
526
+ else if (isSpecial(this.url) &&
527
+ this.base !== null &&
528
+ this.base.scheme === this.url.scheme) {
529
+ this.state = 'special relative or authority';
530
+ }
531
+ else if (isSpecial(this.url)) {
532
+ this.state = 'special authority slashes';
533
+ }
534
+ else if (this.input[this.pointer + 1] === 47) {
535
+ this.state = 'path or authority';
536
+ ++this.pointer;
537
+ }
538
+ else {
539
+ this.url.cannotBeABaseURL = true;
540
+ this.url.path.push('');
541
+ this.state = 'cannot-be-a-base-URL path';
542
+ }
543
+ }
544
+ else if (!this.stateOverride) {
545
+ this.buffer = '';
546
+ this.state = 'no scheme';
547
+ this.pointer = -1;
548
+ }
549
+ else {
550
+ this.parseError = true;
551
+ return failure;
552
+ }
553
+ return true;
554
+ };
555
+ URLStateMachine.prototype['parse no scheme'] = function parseNoScheme(c) {
556
+ if (this.base === null || (this.base.cannotBeABaseURL && c !== 35)) {
557
+ return failure;
558
+ }
559
+ else if (this.base.cannotBeABaseURL && c === 35) {
560
+ this.url.scheme = this.base.scheme;
561
+ this.url.path = this.base.path.slice();
562
+ this.url.query = this.base.query;
563
+ this.url.fragment = '';
564
+ this.url.cannotBeABaseURL = true;
565
+ this.state = 'fragment';
566
+ }
567
+ else if (this.base.scheme === 'file') {
568
+ this.state = 'file';
569
+ --this.pointer;
570
+ }
571
+ else {
572
+ this.state = 'relative';
573
+ --this.pointer;
574
+ }
575
+ return true;
576
+ };
577
+ URLStateMachine.prototype['parse special relative or authority'] =
578
+ function parseSpecialRelativeOrAuthority(c) {
579
+ if (c === 47 && this.input[this.pointer + 1] === 47) {
580
+ this.state = 'special authority ignore slashes';
581
+ ++this.pointer;
582
+ }
583
+ else {
584
+ this.parseError = true;
585
+ this.state = 'relative';
586
+ --this.pointer;
587
+ }
588
+ return true;
589
+ };
590
+ URLStateMachine.prototype['parse path or authority'] =
591
+ function parsePathOrAuthority(c) {
592
+ if (c === 47) {
593
+ this.state = 'authority';
594
+ }
595
+ else {
596
+ this.state = 'path';
597
+ --this.pointer;
598
+ }
599
+ return true;
600
+ };
601
+ URLStateMachine.prototype['parse relative'] = function parseRelative(c) {
602
+ this.url.scheme = this.base.scheme;
603
+ if (isNaN(c)) {
604
+ this.url.username = this.base.username;
605
+ this.url.password = this.base.password;
606
+ this.url.host = this.base.host;
607
+ this.url.port = this.base.port;
608
+ this.url.path = this.base.path.slice();
609
+ this.url.query = this.base.query;
610
+ }
611
+ else if (c === 47) {
612
+ this.state = 'relative slash';
613
+ }
614
+ else if (c === 63) {
615
+ this.url.username = this.base.username;
616
+ this.url.password = this.base.password;
617
+ this.url.host = this.base.host;
618
+ this.url.port = this.base.port;
619
+ this.url.path = this.base.path.slice();
620
+ this.url.query = '';
621
+ this.state = 'query';
622
+ }
623
+ else if (c === 35) {
624
+ this.url.username = this.base.username;
625
+ this.url.password = this.base.password;
626
+ this.url.host = this.base.host;
627
+ this.url.port = this.base.port;
628
+ this.url.path = this.base.path.slice();
629
+ this.url.query = this.base.query;
630
+ this.url.fragment = '';
631
+ this.state = 'fragment';
632
+ }
633
+ else if (isSpecial(this.url) && c === 92) {
634
+ this.parseError = true;
635
+ this.state = 'relative slash';
636
+ }
637
+ else {
638
+ this.url.username = this.base.username;
639
+ this.url.password = this.base.password;
640
+ this.url.host = this.base.host;
641
+ this.url.port = this.base.port;
642
+ this.url.path = this.base.path.slice(0, this.base.path.length - 1);
643
+ this.state = 'path';
644
+ --this.pointer;
645
+ }
646
+ return true;
647
+ };
648
+ URLStateMachine.prototype['parse relative slash'] = function parseRelativeSlash(c) {
649
+ if (isSpecial(this.url) && (c === 47 || c === 92)) {
650
+ if (c === 92) {
651
+ this.parseError = true;
652
+ }
653
+ this.state = 'special authority ignore slashes';
654
+ }
655
+ else if (c === 47) {
656
+ this.state = 'authority';
657
+ }
658
+ else {
659
+ this.url.username = this.base.username;
660
+ this.url.password = this.base.password;
661
+ this.url.host = this.base.host;
662
+ this.url.port = this.base.port;
663
+ this.state = 'path';
664
+ --this.pointer;
665
+ }
666
+ return true;
667
+ };
668
+ URLStateMachine.prototype['parse special authority slashes'] =
669
+ function parseSpecialAuthoritySlashes(c) {
670
+ if (c === 47 && this.input[this.pointer + 1] === 47) {
671
+ this.state = 'special authority ignore slashes';
672
+ ++this.pointer;
673
+ }
674
+ else {
675
+ this.parseError = true;
676
+ this.state = 'special authority ignore slashes';
677
+ --this.pointer;
678
+ }
679
+ return true;
680
+ };
681
+ URLStateMachine.prototype['parse special authority ignore slashes'] =
682
+ function parseSpecialAuthorityIgnoreSlashes(c) {
683
+ if (c !== 47 && c !== 92) {
684
+ this.state = 'authority';
685
+ --this.pointer;
686
+ }
687
+ else {
688
+ this.parseError = true;
689
+ }
690
+ return true;
691
+ };
692
+ URLStateMachine.prototype['parse authority'] = function parseAuthority(c, cStr) {
693
+ if (c === 64) {
694
+ this.parseError = true;
695
+ if (this.atFlag) {
696
+ this.buffer = '%40' + this.buffer;
697
+ }
698
+ this.atFlag = true;
699
+ // careful, this is based on buffer and has its own pointer (this.pointer != pointer) and inner chars
700
+ const len = countSymbols(this.buffer);
701
+ for (let pointer = 0; pointer < len; ++pointer) {
702
+ const codePoint = this.buffer.codePointAt(pointer);
703
+ if (codePoint === 58 && !this.passwordTokenSeenFlag) {
704
+ this.passwordTokenSeenFlag = true;
705
+ continue;
706
+ }
707
+ const encodedCodePoints = percentEncodeChar(codePoint, isUserinfoPercentEncode);
708
+ if (this.passwordTokenSeenFlag) {
709
+ this.url.password += encodedCodePoints;
710
+ }
711
+ else {
712
+ this.url.username += encodedCodePoints;
713
+ }
714
+ }
715
+ this.buffer = '';
716
+ }
717
+ else if (isNaN(c) ||
718
+ c === 47 ||
719
+ c === 63 ||
720
+ c === 35 ||
721
+ (isSpecial(this.url) && c === 92)) {
722
+ if (this.atFlag && this.buffer === '') {
723
+ this.parseError = true;
724
+ return failure;
725
+ }
726
+ this.pointer -= countSymbols(this.buffer) + 1;
727
+ this.buffer = '';
728
+ this.state = 'host';
729
+ }
730
+ else {
731
+ this.buffer += cStr;
732
+ }
733
+ return true;
734
+ };
735
+ URLStateMachine.prototype['parse hostname'] = URLStateMachine.prototype['parse host'] = function parseHostName(c, cStr) {
736
+ if (this.stateOverride && this.url.scheme === 'file') {
737
+ --this.pointer;
738
+ this.state = 'file host';
739
+ }
740
+ else if (c === 58 && !this.arrFlag) {
741
+ if (this.buffer === '') {
742
+ this.parseError = true;
743
+ return failure;
744
+ }
745
+ const host = parseHost(this.buffer, isNotSpecial(this.url));
746
+ if (host === failure) {
747
+ return failure;
748
+ }
749
+ this.url.host = host;
750
+ this.buffer = '';
751
+ this.state = 'port';
752
+ if (this.stateOverride === 'hostname') {
753
+ return false;
754
+ }
755
+ }
756
+ else if (isNaN(c) ||
757
+ c === 47 ||
758
+ c === 63 ||
759
+ c === 35 ||
760
+ (isSpecial(this.url) && c === 92)) {
761
+ --this.pointer;
762
+ if (isSpecial(this.url) && this.buffer === '') {
763
+ this.parseError = true;
764
+ return failure;
765
+ }
766
+ else if (this.stateOverride &&
767
+ this.buffer === '' &&
768
+ (includesCredentials(this.url) || this.url.port !== null)) {
769
+ this.parseError = true;
770
+ return false;
771
+ }
772
+ const host = parseHost(this.buffer, isNotSpecial(this.url));
773
+ if (host === failure) {
774
+ return failure;
775
+ }
776
+ this.url.host = host;
777
+ this.buffer = '';
778
+ this.state = 'path start';
779
+ if (this.stateOverride) {
780
+ return false;
781
+ }
782
+ }
783
+ else {
784
+ if (c === 91) {
785
+ this.arrFlag = true;
786
+ }
787
+ else if (c === 93) {
788
+ this.arrFlag = false;
789
+ }
790
+ this.buffer += cStr;
791
+ }
792
+ return true;
793
+ };
794
+ URLStateMachine.prototype['parse port'] = function parsePort(c, cStr) {
795
+ if (infra_1.default.isASCIIDigit(c)) {
796
+ this.buffer += cStr;
797
+ }
798
+ else if (isNaN(c) ||
799
+ c === 47 ||
800
+ c === 63 ||
801
+ c === 35 ||
802
+ (isSpecial(this.url) && c === 92) ||
803
+ this.stateOverride) {
804
+ if (this.buffer !== '') {
805
+ const port = parseInt(this.buffer);
806
+ if (port > Math.pow(2, 16) - 1) {
807
+ this.parseError = true;
808
+ return failure;
809
+ }
810
+ this.url.port = port === defaultPort(this.url.scheme) ? null : port;
811
+ this.buffer = '';
812
+ }
813
+ if (this.stateOverride) {
814
+ return false;
815
+ }
816
+ this.state = 'path start';
817
+ --this.pointer;
818
+ }
819
+ else {
820
+ this.parseError = true;
821
+ return failure;
822
+ }
823
+ return true;
824
+ };
825
+ const fileOtherwiseCodePoints = new Set([47, 92, 63, 35]);
826
+ function startsWithWindowsDriveLetter(input, pointer) {
827
+ const length = input.length - pointer;
828
+ return (length >= 2 &&
829
+ isWindowsDriveLetterCodePoints(input[pointer], input[pointer + 1]) &&
830
+ (length === 2 || fileOtherwiseCodePoints.has(input[pointer + 2])));
831
+ }
832
+ URLStateMachine.prototype['parse file'] = function parseFile(c) {
833
+ this.url.scheme = 'file';
834
+ if (c === 47 || c === 92) {
835
+ if (c === 92) {
836
+ this.parseError = true;
837
+ }
838
+ this.state = 'file slash';
839
+ }
840
+ else if (this.base !== null && this.base.scheme === 'file') {
841
+ if (isNaN(c)) {
842
+ this.url.host = this.base.host;
843
+ this.url.path = this.base.path.slice();
844
+ this.url.query = this.base.query;
845
+ }
846
+ else if (c === 63) {
847
+ this.url.host = this.base.host;
848
+ this.url.path = this.base.path.slice();
849
+ this.url.query = '';
850
+ this.state = 'query';
851
+ }
852
+ else if (c === 35) {
853
+ this.url.host = this.base.host;
854
+ this.url.path = this.base.path.slice();
855
+ this.url.query = this.base.query;
856
+ this.url.fragment = '';
857
+ this.state = 'fragment';
858
+ }
859
+ else {
860
+ if (!startsWithWindowsDriveLetter(this.input, this.pointer)) {
861
+ this.url.host = this.base.host;
862
+ this.url.path = this.base.path.slice();
863
+ shortenPath(this.url);
864
+ }
865
+ else {
866
+ this.parseError = true;
867
+ }
868
+ this.state = 'path';
869
+ --this.pointer;
870
+ }
871
+ }
872
+ else {
873
+ this.state = 'path';
874
+ --this.pointer;
875
+ }
876
+ return true;
877
+ };
878
+ URLStateMachine.prototype['parse file slash'] = function parseFileSlash(c) {
879
+ if (c === 47 || c === 92) {
880
+ if (c === 92) {
881
+ this.parseError = true;
882
+ }
883
+ this.state = 'file host';
884
+ }
885
+ else {
886
+ if (this.base !== null &&
887
+ this.base.scheme === 'file' &&
888
+ !startsWithWindowsDriveLetter(this.input, this.pointer)) {
889
+ if (isNormalizedWindowsDriveLetterString(this.base.path[0])) {
890
+ this.url.path.push(this.base.path[0]);
891
+ }
892
+ else {
893
+ this.url.host = this.base.host;
894
+ }
895
+ }
896
+ this.state = 'path';
897
+ --this.pointer;
898
+ }
899
+ return true;
900
+ };
901
+ URLStateMachine.prototype['parse file host'] = function parseFileHost(c, cStr) {
902
+ if (isNaN(c) || c === 47 || c === 92 || c === 63 || c === 35) {
903
+ --this.pointer;
904
+ if (!this.stateOverride && isWindowsDriveLetterString(this.buffer)) {
905
+ this.parseError = true;
906
+ this.state = 'path';
907
+ }
908
+ else if (this.buffer === '') {
909
+ this.url.host = '';
910
+ if (this.stateOverride) {
911
+ return false;
912
+ }
913
+ this.state = 'path start';
914
+ }
915
+ else {
916
+ let host = parseHost(this.buffer, isNotSpecial(this.url));
917
+ if (host === failure) {
918
+ return failure;
919
+ }
920
+ if (host === 'localhost') {
921
+ host = '';
922
+ }
923
+ this.url.host = host;
924
+ if (this.stateOverride) {
925
+ return false;
926
+ }
927
+ this.buffer = '';
928
+ this.state = 'path start';
929
+ }
930
+ }
931
+ else {
932
+ this.buffer += cStr;
933
+ }
934
+ return true;
935
+ };
936
+ URLStateMachine.prototype['parse path start'] = function parsePathStart(c) {
937
+ if (isSpecial(this.url)) {
938
+ if (c === 92) {
939
+ this.parseError = true;
940
+ }
941
+ this.state = 'path';
942
+ if (c !== 47 && c !== 92) {
943
+ --this.pointer;
944
+ }
945
+ }
946
+ else if (!this.stateOverride && c === 63) {
947
+ this.url.query = '';
948
+ this.state = 'query';
949
+ }
950
+ else if (!this.stateOverride && c === 35) {
951
+ this.url.fragment = '';
952
+ this.state = 'fragment';
953
+ }
954
+ else if (c !== undefined) {
955
+ this.state = 'path';
956
+ if (c !== 47) {
957
+ --this.pointer;
958
+ }
959
+ }
960
+ return true;
961
+ };
962
+ URLStateMachine.prototype['parse path'] = function parsePath(c) {
963
+ if (isNaN(c) ||
964
+ c === 47 ||
965
+ (isSpecial(this.url) && c === 92) ||
966
+ (!this.stateOverride && (c === 63 || c === 35))) {
967
+ if (isSpecial(this.url) && c === 92) {
968
+ this.parseError = true;
969
+ }
970
+ if (isDoubleDot(this.buffer)) {
971
+ shortenPath(this.url);
972
+ if (c !== 47 && !(isSpecial(this.url) && c === 92)) {
973
+ this.url.path.push('');
974
+ }
975
+ }
976
+ else if (isSingleDot(this.buffer) &&
977
+ c !== 47 &&
978
+ !(isSpecial(this.url) && c === 92)) {
979
+ this.url.path.push('');
980
+ }
981
+ else if (!isSingleDot(this.buffer)) {
982
+ if (this.url.scheme === 'file' &&
983
+ this.url.path.length === 0 &&
984
+ isWindowsDriveLetterString(this.buffer)) {
985
+ if (this.url.host !== '' && this.url.host !== null) {
986
+ this.parseError = true;
987
+ this.url.host = '';
988
+ }
989
+ this.buffer = this.buffer[0] + ':';
990
+ }
991
+ this.url.path.push(this.buffer);
992
+ }
993
+ this.buffer = '';
994
+ if (this.url.scheme === 'file' &&
995
+ (c === undefined || c === 63 || c === 35)) {
996
+ while (this.url.path.length > 1 && this.url.path[0] === '') {
997
+ this.parseError = true;
998
+ this.url.path.shift();
999
+ }
1000
+ }
1001
+ if (c === 63) {
1002
+ this.url.query = '';
1003
+ this.state = 'query';
1004
+ }
1005
+ if (c === 35) {
1006
+ this.url.fragment = '';
1007
+ this.state = 'fragment';
1008
+ }
1009
+ }
1010
+ else {
1011
+ // TODO: If c is not a URL code point and not "%", parse error.
1012
+ if (c === 37 &&
1013
+ (!infra_1.default.isASCIIHex(this.input[this.pointer + 1]) ||
1014
+ !infra_1.default.isASCIIHex(this.input[this.pointer + 2]))) {
1015
+ this.parseError = true;
1016
+ }
1017
+ this.buffer += percentEncodeChar(c, isPathPercentEncode);
1018
+ }
1019
+ return true;
1020
+ };
1021
+ URLStateMachine.prototype['parse cannot-be-a-base-URL path'] =
1022
+ function parseCannotBeABaseURLPath(c) {
1023
+ if (c === 63) {
1024
+ this.url.query = '';
1025
+ this.state = 'query';
1026
+ }
1027
+ else if (c === 35) {
1028
+ this.url.fragment = '';
1029
+ this.state = 'fragment';
1030
+ }
1031
+ else {
1032
+ // TODO: Add: not a URL code point
1033
+ if (!isNaN(c) && c !== 37) {
1034
+ this.parseError = true;
1035
+ }
1036
+ if (c === 37 &&
1037
+ (!infra_1.default.isASCIIHex(this.input[this.pointer + 1]) ||
1038
+ !infra_1.default.isASCIIHex(this.input[this.pointer + 2]))) {
1039
+ this.parseError = true;
1040
+ }
1041
+ if (!isNaN(c)) {
1042
+ this.url.path[0] += percentEncodeChar(c, isC0ControlPercentEncode);
1043
+ }
1044
+ }
1045
+ return true;
1046
+ };
1047
+ URLStateMachine.prototype['parse query'] = function parseQuery(c, cStr) {
1048
+ if (isNaN(c) || (!this.stateOverride && c === 35)) {
1049
+ if (!isSpecial(this.url) ||
1050
+ this.url.scheme === 'ws' ||
1051
+ this.url.scheme === 'wss') {
1052
+ this.encodingOverride = 'utf-8';
1053
+ }
1054
+ const buffer = buffer_1.Buffer.from(this.buffer); // TODO: Use encoding override instead
1055
+ for (let i = 0; i < buffer.length; ++i) {
1056
+ if (buffer[i] < 0x21 ||
1057
+ buffer[i] > 0x7e ||
1058
+ buffer[i] === 0x22 ||
1059
+ buffer[i] === 0x23 ||
1060
+ buffer[i] === 0x3c ||
1061
+ buffer[i] === 0x3e ||
1062
+ (buffer[i] === 0x27 && isSpecial(this.url))) {
1063
+ this.url.query += urlencoded_1.default.percentEncode(buffer[i]);
1064
+ }
1065
+ else {
1066
+ this.url.query += String.fromCodePoint(buffer[i]);
1067
+ }
1068
+ }
1069
+ this.buffer = '';
1070
+ if (c === 35) {
1071
+ this.url.fragment = '';
1072
+ this.state = 'fragment';
1073
+ }
1074
+ }
1075
+ else {
1076
+ // TODO: If c is not a URL code point and not "%", parse error.
1077
+ if (c === 37 &&
1078
+ (!infra_1.default.isASCIIHex(this.input[this.pointer + 1]) ||
1079
+ !infra_1.default.isASCIIHex(this.input[this.pointer + 2]))) {
1080
+ this.parseError = true;
1081
+ }
1082
+ this.buffer += cStr;
1083
+ }
1084
+ return true;
1085
+ };
1086
+ URLStateMachine.prototype['parse fragment'] = function parseFragment(c) {
1087
+ if (isNaN(c)) {
1088
+ // do nothing
1089
+ }
1090
+ else if (c === 0x0) {
1091
+ this.parseError = true;
1092
+ }
1093
+ else {
1094
+ // TODO: If c is not a URL code point and not "%", parse error.
1095
+ if (c === 37 &&
1096
+ (!infra_1.default.isASCIIHex(this.input[this.pointer + 1]) ||
1097
+ !infra_1.default.isASCIIHex(this.input[this.pointer + 2]))) {
1098
+ this.parseError = true;
1099
+ }
1100
+ this.url.fragment += percentEncodeChar(c, isFragmentPercentEncode);
1101
+ }
1102
+ return true;
1103
+ };
1104
+ function serializeURL(url, excludeFragment) {
1105
+ let output = url.scheme + ':';
1106
+ if (url.host !== null) {
1107
+ output += '//';
1108
+ if (url.username !== '' || url.password !== '') {
1109
+ output += url.username;
1110
+ if (url.password !== '') {
1111
+ output += ':' + url.password;
1112
+ }
1113
+ output += '@';
1114
+ }
1115
+ output += serializeHost(url.host);
1116
+ if (url.port !== null) {
1117
+ output += ':' + url.port;
1118
+ }
1119
+ }
1120
+ else if (url.host === null && url.scheme === 'file') {
1121
+ output += '//';
1122
+ }
1123
+ if (url.cannotBeABaseURL) {
1124
+ output += url.path[0];
1125
+ }
1126
+ else {
1127
+ for (const string of url.path) {
1128
+ output += '/' + string;
1129
+ }
1130
+ }
1131
+ if (url.query !== null) {
1132
+ output += '?' + url.query;
1133
+ }
1134
+ if (!excludeFragment && url.fragment !== null) {
1135
+ output += '#' + url.fragment;
1136
+ }
1137
+ return output;
1138
+ }
1139
+ function serializeOrigin(tuple) {
1140
+ let result = tuple.scheme + '://';
1141
+ result += serializeHost(tuple.host);
1142
+ if (tuple.port !== null) {
1143
+ result += ':' + tuple.port;
1144
+ }
1145
+ return result;
1146
+ }
1147
+ exports.serializeURL = serializeURL;
1148
+ const serializeURLOrigin = function (url) {
1149
+ // https://url.spec.whatwg.org/#concept-url-origin
1150
+ switch (url.scheme) {
1151
+ case 'blob':
1152
+ try {
1153
+ return module.exports.serializeURLOrigin(module.exports.parseURL(url.path[0]));
1154
+ }
1155
+ catch (e) {
1156
+ // serializing an opaque origin returns "null"
1157
+ return 'null';
1158
+ }
1159
+ case 'ftp':
1160
+ case 'http':
1161
+ case 'https':
1162
+ case 'ws':
1163
+ case 'wss':
1164
+ return serializeOrigin({
1165
+ scheme: url.scheme,
1166
+ host: url.host,
1167
+ port: url.port,
1168
+ });
1169
+ case 'file':
1170
+ // The spec says:
1171
+ // > Unfortunate as it is, this is left as an exercise to the reader. When in doubt, return a new opaque origin.
1172
+ // Browsers tested so far:
1173
+ // - Chrome says "file://", but treats file: URLs as cross-origin for most (all?) purposes; see e.g.
1174
+ // https://bugs.chromium.org/p/chromium/issues/detail?id=37586
1175
+ // - Firefox says "null", but treats file: URLs as same-origin sometimes based on directory stuff; see
1176
+ // https://developer.mozilla.org/en-US/docs/Archive/Misc_top_level/Same-origin_policy_for_file:_URIs
1177
+ return 'null';
1178
+ default:
1179
+ // serializing an opaque origin returns "null"
1180
+ return 'null';
1181
+ }
1182
+ };
1183
+ exports.serializeURLOrigin = serializeURLOrigin;
1184
+ const basicURLParse = function (input, options) {
1185
+ if (options === undefined) {
1186
+ options = {};
1187
+ }
1188
+ const usm = new URLStateMachine(input, options.baseURL, options.encodingOverride, options.url, options.stateOverride);
1189
+ if (usm.failure) {
1190
+ return null;
1191
+ }
1192
+ return usm.url;
1193
+ };
1194
+ exports.basicURLParse = basicURLParse;
1195
+ const setTheUsername = function (url, username) {
1196
+ url.username = '';
1197
+ const decoded = urlencoded_1.default.ucs2decode(username);
1198
+ for (let i = 0; i < decoded.length; ++i) {
1199
+ url.username += percentEncodeChar(decoded[i], isUserinfoPercentEncode);
1200
+ }
1201
+ };
1202
+ exports.setTheUsername = setTheUsername;
1203
+ const setThePassword = function (url, password) {
1204
+ url.password = '';
1205
+ const decoded = urlencoded_1.default.ucs2decode(password);
1206
+ for (let i = 0; i < decoded.length; ++i) {
1207
+ url.password += percentEncodeChar(decoded[i], isUserinfoPercentEncode);
1208
+ }
1209
+ };
1210
+ exports.setThePassword = setThePassword;
1211
+ exports.serializeHost = serializeHost;
1212
+ exports.cannotHaveAUsernamePasswordPort = cannotHaveAUsernamePasswordPort;
1213
+ const serializeInteger = function (integer) {
1214
+ return String(integer);
1215
+ };
1216
+ exports.serializeInteger = serializeInteger;
1217
+ const parseURL = function (input, options) {
1218
+ if (options === undefined) {
1219
+ options = {};
1220
+ }
1221
+ // We don't handle blobs, so this just delegates:
1222
+ return module.exports.basicURLParse(input, {
1223
+ baseURL: options.baseURL,
1224
+ encodingOverride: options.encodingOverride,
1225
+ });
1226
+ };
1227
+ exports.parseURL = parseURL;
1228
+ exports.default = {
1229
+ parseURL: exports.parseURL,
1230
+ serializeInteger: exports.serializeInteger,
1231
+ serializeHost,
1232
+ cannotHaveAUsernamePasswordPort,
1233
+ setThePassword: exports.setThePassword,
1234
+ setTheUsername: exports.setTheUsername,
1235
+ basicURLParse: exports.basicURLParse,
1236
+ serializeURLOrigin: exports.serializeURLOrigin,
1237
+ serializeURL,
1238
+ };