1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
|
/**
* Created by keithpk on 12/2/16.
*/
import { isNothing, Nothing, Opt } from "@jet/environment/types/optional";
import { isDefinedNonNullNonEmpty, isNullOrEmpty } from "./server-data";
export type Query = {
[key: string]: string | undefined;
};
export type URLComponent = "protocol" | "username" | "password" | "host" | "port" | "pathname" | "query" | "hash";
const protocolRegex = /^([a-z][a-z0-9.+-]*:)(\/\/)?([\S\s]*)/i;
const queryParamRegex = /([^=?&]+)=?([^&]*)/g;
const componentOrder: URLComponent[] = ["hash", "query", "pathname", "host"];
type URLSplitStyle = "prefix" | "suffix";
type URLSplitResult = {
result?: string;
remainder: string;
};
function splitUrlComponent(input: string, marker: string, style: URLSplitStyle): URLSplitResult {
const index = input.indexOf(marker);
let result;
let remainder = input;
if (index !== -1) {
const prefix = input.slice(0, index);
const suffix = input.slice(index + marker.length, input.length);
if (style === "prefix") {
result = prefix;
remainder = suffix;
} else {
result = suffix;
remainder = prefix;
}
}
// log("Token: " + marker + " String: " + input, " Result: " + result + " Remainder: " + remainder)
return {
result: result,
remainder: remainder,
};
}
export class URL {
protocol?: Opt<string>;
username: string;
password: string;
host?: Opt<string>;
port: string;
pathname?: Opt<string>;
query?: Query = {};
hash?: string;
constructor(url?: string) {
if (isNullOrEmpty(url)) {
return;
}
// Split the protocol from the rest of the urls
let remainder = url;
const match = protocolRegex.exec(url);
if (match != null) {
// Pull out the protocol
let protocol = match[1];
if (protocol) {
protocol = protocol.split(":")[0];
}
this.protocol = protocol;
// Save the remainder
remainder = match[3];
}
// Then match each component in a specific order
let parse: URLSplitResult = { remainder: remainder, result: undefined };
for (const component of componentOrder) {
if (!parse.remainder) {
break;
}
switch (component) {
case "hash": {
parse = splitUrlComponent(parse.remainder, "#", "suffix");
this.hash = parse.result;
break;
}
case "query": {
parse = splitUrlComponent(parse.remainder, "?", "suffix");
if (isDefinedNonNullNonEmpty(parse.result)) {
this.query = URL.queryFromString(parse.result);
}
break;
}
case "pathname": {
parse = splitUrlComponent(parse.remainder, "/", "suffix");
if (isDefinedNonNullNonEmpty(parse.result)) {
// Replace the initial /, since paths require it
this.pathname = "/" + parse.result;
}
break;
}
case "host": {
if (parse.remainder) {
const authorityParse = splitUrlComponent(parse.remainder, "@", "prefix");
const userInfo = authorityParse.result;
const hostPort = authorityParse.remainder;
if (isDefinedNonNullNonEmpty(userInfo)) {
const userInfoSplit = userInfo.split(":");
this.username = decodeURIComponent(userInfoSplit[0]);
this.password = decodeURIComponent(userInfoSplit[1]);
}
if (hostPort) {
const hostPortSplit = hostPort.split(":");
this.host = hostPortSplit[0];
this.port = hostPortSplit[1];
}
}
break;
}
default: {
throw new Error("Unhandled case!");
}
}
}
}
set(component: URLComponent, value: string | Query): URL {
if (isNullOrEmpty(value)) {
return this;
}
if (component === "query") {
if (typeof value === "string") {
value = URL.queryFromString(value);
}
}
switch (component) {
// Exhaustive match to make sure TS property minifiers and other
// transformer plugins do not break this code.
case "protocol":
this.protocol = value as string;
break;
case "username":
this.username = value as string;
break;
case "password":
this.password = value as string;
break;
case "port":
this.port = value as string;
break;
case "pathname":
this.pathname = value as string;
break;
case "query":
this.query = value as Query;
break;
case "hash":
this.hash = value as string;
break;
default:
// The fallback for component which is not a property of URL object.
this[component] = value as string;
break;
}
return this;
}
private get(component: URLComponent): string | Query | Nothing {
switch (component) {
// Exhaustive match to make sure TS property minifiers and other
// transformer plugins do not break this code.
case "protocol":
return this.protocol;
case "username":
return this.username;
case "password":
return this.password;
case "port":
return this.port;
case "pathname":
return this.pathname;
case "query":
return this.query;
case "hash":
return this.hash;
default:
// The fallback for component which is not a property of URL object.
return this[component];
}
}
append(component: URLComponent, value: string | Query): URL {
const existingValue = this.get(component);
let newValue;
if (component === "query") {
if (typeof value === "string") {
value = URL.queryFromString(value);
}
if (typeof existingValue === "string") {
newValue = { existingValue, ...value };
} else {
newValue = { ...existingValue, ...value };
}
} else {
let existingValueString = existingValue as string;
if (!existingValueString) {
existingValueString = "";
}
let newValueString = existingValueString;
if (component === "pathname") {
const pathLength = existingValueString.length;
if (!pathLength || existingValueString[pathLength - 1] !== "/") {
newValueString += "/";
}
}
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands, @typescript-eslint/no-base-to-string
newValueString += value;
newValue = newValueString;
}
return this.set(component, newValue);
}
param(key: string, value?: string): URL {
if (!key) {
return this;
}
if (this.query == null) {
this.query = {};
}
this.query[key] = value;
return this;
}
removeParam(key: string): URL {
if (!key || this.query == null) {
return this;
}
if (this.query[key] !== undefined) {
delete this.query[key];
}
return this;
}
/**
* Push a new string value onto the path for this url
* @returns URL this object with the updated path.
*/
path(value: string): URL {
return this.append("pathname", value);
}
pathExtension(): Opt<string> {
// Extract path extension if one exists
if (isNothing(this.pathname)) {
return null;
}
const lastFilenameComponents = this.pathname
.split("/")
.filter((item) => item.length > 0) // Remove any double or trailing slashes
.pop()
?.split(".");
if (lastFilenameComponents === undefined) {
return null;
}
if (
lastFilenameComponents.filter((part) => {
return part !== "";
}).length < 2 // Remove any empty parts (e.g. .ssh_config -> ["ssh_config"])
) {
return null;
}
return lastFilenameComponents.pop();
}
/**
* Returns the path components of the URL
* @returns An array of non-empty path components from `urls`.
*/
pathComponents(): string[] {
if (isNullOrEmpty(this.pathname)) {
return [];
}
return this.pathname.split("/").filter((component) => component.length > 0);
}
/**
* Returns the last path component from this url, updating the url to not include this path component
* @returns String the last path component from this url.
*/
popPathComponent(): string | null {
if (isNullOrEmpty(this.pathname)) {
return null;
}
const lastPathComponent = this.pathname.slice(this.pathname.lastIndexOf("/") + 1);
if (lastPathComponent.length === 0) {
return null;
}
this.pathname = this.pathname.slice(0, this.pathname.lastIndexOf("/"));
return lastPathComponent;
}
/**
* Same as toString
*
* @returns {string} A string representation of the URL
*/
build(): string {
return this.toString();
}
/**
* Converts the URL to a string
*
* @returns {string} A string representation of the URL
*/
toString(): string {
let url = "";
if (isDefinedNonNullNonEmpty(this.protocol)) {
url += this.protocol + "://";
}
if (this.username) {
url += encodeURIComponent(this.username);
if (this.password) {
url += ":" + encodeURIComponent(this.password);
}
url += "@";
}
if (isDefinedNonNullNonEmpty(this.host)) {
url += this.host;
if (this.port) {
url += ":" + this.port;
}
}
if (isDefinedNonNullNonEmpty(this.pathname)) {
url += this.pathname;
/// Trim off trailing path separators when we have a valid path
/// We don't do this unless pathname has elements otherwise we will trim the `://`
if (url.endsWith("/") && this.pathname.length > 0) {
url = url.slice(0, -1);
}
}
if (this.query != null && Object.keys(this.query).length > 0) {
url += "?" + URL.toQueryString(this.query);
}
if (isDefinedNonNullNonEmpty(this.hash)) {
url += "#" + this.hash;
}
return url;
}
// ----------------
// Static API
// ----------------
/**
* Converts a string into a query dictionary
* @param query The string to parse
* @returns The query dictionary containing the key-value pairs in the query string
*/
static queryFromString(query: string): Query {
const result = {};
let parseResult = queryParamRegex.exec(query);
while (parseResult != null) {
const key = decodeURIComponent(parseResult[1]);
const value = decodeURIComponent(parseResult[2]);
result[key] = value;
parseResult = queryParamRegex.exec(query);
}
return result;
}
/**
* Converts a query dictionary into a query string
*
* @param query The query dictionary
* @returns {string} The string representation of the query dictionary
*/
static toQueryString(query: Query) {
let queryString = "";
let first = true;
for (const key of Object.keys(query)) {
if (!first) {
queryString += "&";
}
first = false;
queryString += encodeURIComponent(key);
const value = query[key];
if (isDefinedNonNullNonEmpty(value) && value.length) {
queryString += "=" + encodeURIComponent(value);
}
}
return queryString;
}
/**
* Convenience method to instantiate a URL from a string
* @param url The URL string to parse
* @returns {URL} The new URL object representing the URL
*/
static from(url: string): URL {
return new URL(url);
}
/**
* Convenience method to instantiate a URL from numerous (optional) components
* @param protocol The protocol type
* @param host The host name
* @param path The path
* @param query The query
* @param hash The hash
* @returns {URL} The new URL object representing the URL
*/
static fromComponents(
protocol?: Opt<string>,
host?: Opt<string>,
path?: Opt<string>,
query?: Query,
hash?: string,
): URL {
const url = new URL();
url.protocol = protocol;
url.host = host;
url.pathname = path;
url.query = query;
url.hash = hash;
return url;
}
}
|