This repository was archived by the owner on Dec 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathUri.ts
More file actions
520 lines (429 loc) · 11.8 KB
/
Uri.ts
File metadata and controls
520 lines (429 loc) · 11.8 KB
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/*!
* @author electricessence / https://github.com/electricessence/
* Licensing: MIT https://github.com/electricessence/TypeScript.NET/blob/master/LICENSE.md
* Based on: https://en.wikipedia.org/wiki/Uniform_Resource_Identifier
*/
import {Type} from "../Types";
import {UriComponent} from "./UriComponent";
import {Scheme} from "./Scheme";
import {SchemeValue} from "./SchemeValue";
import {QueryParam} from "./QueryParam";
import {encode, parseToMap, Separator} from "./QueryParams";
import {trim} from "../Text/Utility";
import {Exception} from "../Exception";
import {ArgumentException} from "../Exceptions/ArgumentException";
import {ArgumentOutOfRangeException} from "../Exceptions/ArgumentOutOfRangeException";
import {IUri} from "./IUri";
import {IMap} from "../../IMap";
import {Primitive} from "../Primitive";
import {StringKeyValuePair} from "../KeyValuePair";
import {IEquatable} from "../IEquatable";
import {Action} from "../FunctionTypes";
const VOID0:undefined = void 0;
/**
* Provides an read-only model representation of a uniform resource identifier (URI) and easy access to the parts of the URI.
*
* The read-only model (frozen) is easier for debugging than exposing accessors for each property.
* ICloneable<Uri> is not used to prevent unnecessary copying of values that won't change.
*/
export class Uri implements IUri, IEquatable<IUri>
{
readonly scheme:SchemeValue.Any | null;
readonly userInfo:string | null;
readonly host:string | null;
readonly port:number | null;
readonly path:string | null;
readonly query:string | null;
readonly fragment:string | null;
readonly queryParams:IMap<Primitive|Primitive[]>;//Readonly<IMap<Primitive|Primitive[]>>;
/**
* @param scheme The user name, password, or other user-specific information associated with the specified URI.
* @param userInfo The host component of this instance.
* @param host The port number of this URI.
* @param port The absolute path of the URI.
* @param path The absolute path of the URI.
* @param query Any query information included in the specified URI.
* @param fragment The escaped URI fragment.
*/
constructor(
scheme:SchemeValue.Any|null,
userInfo:string|null,
host:string|null,
port:number|null,
path:string|null,
query?:QueryParam.Convertible,
fragment?:string)
{
const _ = this;
this.scheme = getScheme(scheme) || null;
this.userInfo = userInfo || null;
this.host = host || null;
this.port = getPort(port);
this.authority = _.getAuthority() || null;
this.path = path || null;
if(!Type.isString(query))
query = encode(<UriComponent.Map|StringKeyValuePair<Primitive>[]>query);
this.query = formatQuery(<string>query) || null;
Object.freeze(this.queryParams
= _.query
? parseToMap(_.query)
: {});
this.pathAndQuery = _.getPathAndQuery() || null;
this.fragment = formatFragment(fragment) || null;
// This should validate the uri...
this.absoluteUri = _.getAbsoluteUri();
this.baseUri = _.absoluteUri.replace(/[?#].*/, '');
// Intended to be read-only. Call .toMap() to get a writable copy.
Object.freeze(this);
}
/**
* Compares the values of another IUri via toString comparison.
* @param other
* @returns {boolean}
*/
equals(other:IUri):boolean
{
return this===other || this.absoluteUri==Uri.toString(other);
}
/**
* Parses or clones values from existing Uri values.
* @param uri
* @param defaults
* @returns {Uri}
*/
static from(uri:string|IUri|null|undefined, defaults?:IUri):Uri
{
const u = Type.isString(uri)
? Uri.parse(<string>uri) // Parsing a string should throw errors. Null or undefined simply means empty.
: <IUri>uri;
return new Uri(
u && u.scheme || defaults && <any>defaults.scheme,
u && u.userInfo || defaults && <any>defaults.userInfo,
u && u.host || defaults && <any>defaults.host,
u && Type.isNumber(u.port,true) ? u.port : defaults && <any>defaults.port,
u && u.path || defaults && <any>defaults.path,
u && u.query || defaults && <any>defaults.query,
u && u.fragment || defaults && <any>defaults.fragment
);
}
/**
* Parses a URL into it's components.
* @param url The url to parse.
* @returns {IUri} Will throw an exception if not able to parse.
*/
static parse(url:string):IUri
static parse(url:string, throwIfInvalid:true):IUri
/**
* Parses a URL into it's components.
* @param url The url to parse.
* @param throwIfInvalid Defaults to true.
* @returns {IUri} Returns a map of the values or *null* if invalid and *throwIfInvalid* is <b>false</b>.
*/
static parse(url:string, throwIfInvalid:boolean):IUri|null
static parse(url:string, throwIfInvalid:boolean = true):IUri|null
{
let result:IUri|null = null;
const ex = tryParse(url, (out) => {result = out;});
if(throwIfInvalid && ex) throw ex;
return result;
}
/**
* Parses a URL into it's components.
* @param url The url to parse.
* @param out A delegate to capture the value.
* @returns {boolean} True if valid. False if invalid.
*/
static tryParse(url:string, out:(result:IUri)=>void):boolean
{
return !tryParse(url, out); // return type is Exception.
}
static copyOf(map:IUri):IUri
{
return copyUri(map);
}
copyTo(map:IUri):IUri
{
return copyUri(this, map);
}
updateQuery(query:QueryParam.Convertible):Uri
{
const map = this.toMap();
map.query = <any>query;
return Uri.from(map);
}
/**
* Is provided for sub classes to override this value.
*/
protected getAbsoluteUri():string
{
return uriToString(this);
}
/**
* Is provided for sub classes to override this value.
*/
protected getAuthority():string
{
return getAuthority(this);
}
/**
* Is provided for sub classes to override this value.
*/
protected getPathAndQuery():string
{
return getPathAndQuery(this);
}
/**
* The absolute URI.
*/
absoluteUri:string;
/**
* Gets the Domain Name System (DNS) host name or IP address and the port number for a server.
*/
readonly authority:string | null;
/**
* Gets the path and Query properties separated by a question mark (?).
*/
readonly pathAndQuery:string | null;
/**
* Gets the full path without the query or fragment.
*/
readonly baseUri:string;
/**
* The segments that represent a path.<br/>
* https://msdn.microsoft.com/en-us/library/system.uri.segments%28v=vs.110%29.aspx
*
* <h5><b>Example:</b></h5>
* If the path value equals: ```/tree/node/index.html```<br/>
* The result will be: ```['/','tree/','node/','index.html']```
* @returns {string[]}
*/
get pathSegments():string[]
{
return this.path
&& this.path.match(/^[/]|[^/]*[/]|[^/]+$/g)
|| [];
}
/**
* Creates a writable copy.
* @returns {IUri}
*/
toMap():IUri
{
return this.copyTo({});
}
/**
* @returns {string} The full absolute uri.
*/
toString():string
{
return this.absoluteUri;
}
/**
* Properly converts an existing URI to a string.
* @param uri
* @returns {string}
*/
static toString(uri:IUri):string
{
return uri instanceof <any>Uri
? (<Uri>uri).absoluteUri
: uriToString(uri);
}
/**
* Returns the authority segment of an URI.
* @param uri
* @returns {string}
*/
static getAuthority(uri:IUri):string
{
return getAuthority(uri);
}
}
export enum Fields {
scheme,
userInfo,
host,
port,
path,
query,
fragment
}
Object.freeze(Fields);
function copyUri(from:IUri, to?:IUri)
{
let i = 0, field:string;
if(!to) to = {};
while(field = Fields[i++])
{
const value = (<any>from)[field];
if(value) (<any>to)[field] = value;
}
return to;
}
const SLASH = '/', SLASH2 = '//', QM = Separator.Query, HASH = '#', EMPTY = '', AT = '@';
function getScheme(scheme:SchemeValue.Any|string|null|undefined):SchemeValue.Any|null
{
let s:any = scheme;
if(Type.isString(s))
{
if(!s) return null;
s = trim(s)
.toLowerCase()
.replace(/[^a-z0-9+.-]+$/g, EMPTY);
if(!s) return null;
if(Scheme.isValid(s)) return s;
}
else
{
if(s==null) return s;
}
throw new ArgumentOutOfRangeException('scheme', scheme, 'Invalid scheme.');
}
function getPort(port:number|string|null|undefined):number|null
{
if(port===0) return <number>port;
if(!port) return null;
let p:number;
if(Type.isNumber(port))
{
p = <number>port;
if(p>=0 && isFinite(p))
return p;
}
else if(Type.isString(port) && (p = parseInt(<string>port)) && !isNaN(p))
{
return getPort(p);
}
throw new ArgumentException("port", "invalid value");
}
function getAuthority(uri:IUri):string
{
if(!uri.host)
{
if(uri.userInfo)
throw new ArgumentException('host', 'Cannot include user info when there is no host.');
if(Type.isNumber(uri.port, true))
throw new ArgumentException('host', 'Cannot include a port when there is no host.');
}
/*
* [//[user:password@]host[:port]]
*/
let result = uri.host || EMPTY;
if(result)
{
if(uri.userInfo) result = uri.userInfo + AT + result;
if(!isNaN(<any>(uri.port))) result += ':' + uri.port;
result = SLASH2 + result;
}
return result;
}
function formatQuery(query:string|null|undefined):string|null|undefined
{
return query && ((query.indexOf(QM)!==0 ? QM : EMPTY) + query);
}
function formatFragment(fragment:string|null|undefined):string|null|undefined
{
return fragment && ((fragment.indexOf(HASH)!==0 ? HASH : EMPTY) + fragment);
}
function getPathAndQuery(uri:IUri):string
{
const path = uri.path,
query = uri.query;
return EMPTY
+ (path || EMPTY)
+ (formatQuery(query) || EMPTY);
}
function uriToString(uri:IUri):string
{
// scheme:[//[user:password@]domain[:port]][/]path[?query][#fragment]
// {scheme}{authority}{path}{query}{fragment}
const scheme = getScheme(uri.scheme);
let authority = getAuthority(uri);
const pathAndQuery = getPathAndQuery(uri),
fragment = formatFragment(uri.fragment);
const part1 = EMPTY
+ ((scheme && (scheme + ':')) || EMPTY)
+ (authority || EMPTY);
let part2 = EMPTY
+ (pathAndQuery || EMPTY)
+ (fragment || EMPTY);
if(part1 && part2 && scheme && !authority)
throw new ArgumentException('authority', "Cannot format schemed Uri with missing authority.");
if(part1 && pathAndQuery && pathAndQuery.indexOf(SLASH)!==0)
part2 = SLASH + part2;
return part1 + part2;
}
function tryParse(url:string, out:Action<IUri>):null|Exception
{
if(!url)
return new ArgumentException('url', 'Nothing to parse.');
// Could use a regex here, but well follow some rules instead.
// The intention is to 'gather' the pieces. This isn't validation (yet).
// scheme:[//[user:password@]domain[:port]][/]path[?query][#fragment]
let i:number;
const result:IUri = {};
// Anything after the first # is the fragment.
i = url.indexOf(HASH);
if(i!= -1)
{
result.fragment = url.substring(i + 1) || VOID0;
url = url.substring(0, i);
}
// Anything after the first ? is the query.
i = url.indexOf(QM);
if(i!= -1)
{
result.query = url.substring(i + 1) || VOID0;
url = url.substring(0, i);
}
// Guarantees a separation.
i = url.indexOf(SLASH2);
if(i!= -1)
{
let scheme = trim(url.substring(0, i));
const c = /:$/;
if(!c.test(scheme))
return new ArgumentException('url', 'Scheme was improperly formatted');
scheme = trim(scheme.replace(c, EMPTY));
try
{
result.scheme = getScheme(scheme) || VOID0;
}
catch(ex)
{
return ex;
}
url = url.substring(i + 2);
}
// Find any path information.
i = url.indexOf(SLASH);
if(i!= -1)
{
result.path = url.substring(i);
url = url.substring(0, i);
}
// Separate user info.
i = url.indexOf(AT);
if(i!= -1)
{
result.userInfo = url.substring(0, i) || VOID0;
url = url.substring(i + 1);
}
// Remaining is host and port.
i = url.indexOf(':');
if(i!= -1)
{
const port = parseInt(trim(url.substring(i + 1)));
if(isNaN(port))
return new ArgumentException('url', 'Port was invalid.');
result.port = port;
url = url.substring(0, i);
}
url = trim(url);
if(url)
result.host = url;
out(copyUri(result));
// null is good! (here)
return null;
}
export default Uri;