-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.ts
488 lines (422 loc) · 10.9 KB
/
index.ts
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
type Dict<T> = Record<string, T>;
type PathPiece = string | number | Array<string | number>;
function lastItem<T>(arr: Array<T>): T {
return arr[arr.length-1]
}
interface Parsed {
auth: string;
username: string;
password: string;
host: string;
hostname: string;
hash: string | null;
search: string;
pathname: string;
port: number | null; // some protocols have no port, like file://
path: string;
query: string;
protocol: string;
}
function urlParse(href: string) {
if (!href.includes("://")) {
if (href.charAt(0) === "/") {
href = "http://localhost/"
}
href = "http://" + href
}
const url = new URL(href)
const colonFreeProtocol = url.protocol.substring(0, url.protocol.length -1)
const hash = (url.hash && url.hash.charAt(0)) === "#" ? url.hash.substring(1) : url.hash
const result: Parsed = {
protocol: colonFreeProtocol,
auth: `${url.username}:${url.password}`,
username: url.username,
password: url.password,
host: url.host,
hostname: url.hostname,
hash,
search: url.search,
pathname: url.pathname,
port: url.port ? parseInt(url.port, 10) : null,
path: (url.pathname || "") + (url.search || ""),
query: url.search ? url.search.slice(1) : url.search,
}
return result
}
const getDefaults = function () {
const defaultUrl = "http://localhost:80"
const defaults = urlParse(defaultUrl)
return defaults
}
export class UrlGrey {
#url: string
_parsed: Parsed | null
constructor(url: string) {
this.#url = url
this._parsed = null
}
parsed() {
if (this._parsed) {
return this._parsed
}
this._parsed = urlParse(this.#url)
const defaults = getDefaults()
this._parsed.protocol = this._parsed.protocol || defaults.protocol
return this._parsed
}
getExtendedPath() {
let href = this.getPath()
href += this.getQueryString() ? "?" + this.getQueryString() : ""
href += this.getHash() ? "#" + this.getHash() : ""
return href
}
extendedPath(url: string) {
const clone = this.clone()
const p = urlParse(`http://localhost/${url}`)
clone.parsed().hash = p.hash
clone.parsed().query = p.query
return clone.path(p.pathname)
}
port(num: number | string | null) {
if (this.getProtocol() === "file") {
throw new Error("file urls don't have ports")
}
const clone = this.clone()
clone.parsed().port = parseInt(`${num}`, 10)
return clone
}
getPort() {
// getter
const output = this.parsed().port
if (output) return output
switch (this.getProtocol()) {
case "http":
return 80
case "https":
return 443
default:
return null
}
}
getQuery() {
return qsParse(this.parsed().query)
}
clearQuery () {
return this.queryString("")
}
query(mergeObject: Dict<string | number | null>) {
// read the object out
const oldQuery = qsParse(this.parsed().query)
for (const [k, v] of Object.entries(mergeObject)) {
if (v === null) {
delete oldQuery[k]
} else {
oldQuery[k] = `${v}`
}
}
const newString = new URLSearchParams(oldQuery).toString()
return this.queryString(newString)
}
getRawQuery() {
const qObj: Dict<string> = {}
if (this.getQueryString().length === 0) {
return qObj
}
return this.getQueryString()
.split("&")
.reduce(function (obj, pair) {
const pieces = pair.split("=")
const key = pieces[0]
const val = pieces[1]
obj[key] = val
return obj
}, qObj)
}
rawQuery(mergeObject: null | false | Dict<string | number | null>) {
if (mergeObject === null || mergeObject === false) {
return this.queryString("")
}
// read the object out
const oldQuery = qsParse(this.parsed().query)
for (const [k, v] of Object.entries(mergeObject)) {
if (v === null) {
delete oldQuery[k]
} else {
oldQuery[k] = `${v}`
}
}
const pairs = []
for (const [k, v] of Object.entries(oldQuery)) {
pairs.push(k + "=" + v)
}
const newString = pairs.join("&")
return this.queryString(newString)
}
getProtocol(): string {
return this.parsed().protocol
}
protocol(str: string) {
const clone = this.clone()
clone.parsed().protocol = str
return clone
}
getUsername(): string {
return this.parsed().username
}
username(str: string) {
const clone = this.clone()
clone.parsed().username = str
return clone
}
getPassword(): string {
return this.parsed().password
}
password(str: string) {
const clone = this.clone()
clone.parsed().password = str
return clone
}
getHostname(): string {
return this.parsed().hostname
}
hostname(str: string) {
const clone = this.clone()
clone.parsed().hostname = str
return clone
}
getHash(): string | null {
return this.parsed().hash
}
hash(str: string) {
const clone = this.clone()
clone.parsed().hash = str
return clone
}
getPath(): string {
return this.parsed().pathname
}
path(input: PathPiece | PathPiece[]) {
let args = Array.isArray(input) ? input.flat(5) : [input]
let str = args.join("/")
str = str.replace(/\/+/g, "/") // remove double slashes
str = str.replace(/\/$/, "") // remove all trailing slashes
args = str.split("/")
for (let i = 0; i < args.length; i++) {
args[i] = this.encode(`${args[i]}`)
}
str = args.join("/")
if (str[0] !== "/") {
str = "/" + str
}
const clone = this.clone()
clone.parsed().pathname = str
return clone
}
getQueryString(): string {
return this.parsed().query
}
queryString(str: string) {
const clone = this.clone()
clone.parsed().query = str
return clone
}
getRawPath() {
return this.parsed().pathname
}
rawPath(input: PathPiece | PathPiece[]) {
const args = Array.isArray(input) ? input.flat(5) : [input]
let str = args.join("/")
str = str.replace(/\/+/g, "/") // remove double slashes
str = str.replace(/\/$/, "") // remove all trailing slashes
if (str[0] !== "/") {
str = "/" + str
}
this.parsed().pathname = str
return this
}
encode(str: string) {
try {
return encodeURIComponent(str)
} catch (ex) {
return new URLSearchParams({"": str}).toString().slice(1)
}
}
decode(str: string) {
return decode(str)
}
clone() {
return new UrlGrey(this.toString())
}
getParent() {
const pieces = this.getPath().split("/")
let popped = pieces.pop()
if (popped === "") {
// ignore trailing slash
popped = pieces.pop()
}
if (!popped) {
throw new Error("The current path has no parent path")
}
return this.clearQuery().hash("").path(pieces.join("/"))
}
getRawChild() {
const pieces = this.getPath().split("/")
let last = lastItem(pieces)
if (pieces.length > 1 && last === "") {
// ignore trailing slashes
pieces.pop()
last = lastItem(pieces)
}
return last
}
rawChild(suffixes: PathPiece | PathPiece[]) {
return this.clearQuery()
.hash("")
.rawPath([this.getPath(), suffixes].flat(5))
}
getChild() {
const pieces = pathPieces(this.getPath())
let last = lastItem(pieces)
if (pieces.length > 1 && last === "") {
// ignore trailing slashes
pieces.pop()
last = lastItem(pieces)
}
return last
}
child(suffixes: PathPiece | PathPiece[]) {
return this.clearQuery()
.hash("")
.path([this.getPath(), suffixes].flat(5))
}
toJSON() {
return this.toString()
}
toString() {
const p = this.parsed()
let retval = p.protocol + "://"
if (this.getProtocol() !== "file") {
const userinfo = p.username + ":" + p.password
if (userinfo !== ":") {
retval += userinfo + "@"
}
retval += p.hostname
const port = portString(this)
if (port) {
retval += ":" + port
}
}
retval += this.getPath() === "/" ? "" : this.getPath()
const qs = p.query
if (qs) {
retval += "?" + qs
}
if (p.hash) {
retval += "#" + p.hash
}
return retval
}
}
const pathPieces = function (path: string) {
const pieces = path.split("/")
for (let i = 0; i < pieces.length; i++) {
pieces[i] = decode(pieces[i])
}
return pieces
}
const decode = function (str: string) {
try {
return decodeURIComponent(str)
} catch (ex) {
const val = new URLSearchParams(`=${str}`).get("")
if (!val && val !== "") {
throw new Error(`decode() input could not be decoded: ${str}`)
}
return val
}
}
const portString = function (o: UrlGrey): string {
const port = o.getPort()
if (o.getProtocol() === "https") {
if (port === 443) {
return ""
}
}
if (o.getProtocol() === "http") {
if (port === 80) {
return ""
}
}
if (port || port === 0) return `${port}`
return ""
}
const qsParse = (qsStr: string) => {
const params = new URLSearchParams(qsStr)
const retval: Dict<string> = {}
for (const param of params) {
retval[param[0]] = param[1]
}
return retval
}
/*
UrlGrey.prototype.absolute = function(path){
if (path[0] == '/'){
path = path.substring(1);
}
var parsed = urlParse(path);
if (!!parsed.protocol){ // if it's already absolute, just return it
return path;
}
return this._protocol + "://" + this._host + '/' + path;
};
// TODO make this interpolate vars into the url. both sinatra style and url-tempates
// TODO name this:
UrlGrey.prototype.get = function(nameOrPath, varDict){
if (!!nameOrPath){
if (!!varDict){
return this.absolute(this._router.getUrl(nameOrPath, varDict));
}
return this.absolute(this._router.getUrl(nameOrPath));
}
return this.url;
};*/
/*
// TODO needs to take a template as an input
UrlGrey.prototype.param = function(key, defaultValue){
var value = this.params()[key];
if (!!value) {
return value;
}
return defaultValue;
};
// TODO extract params, given a template?
// TODO needs to take a template as an input
UrlGrey.prototype.params = function(inUrl){
if (!!inUrl){
return this._router.pathVariables(inUrl);
}
if (!!this._params){
return this._params;
}
return this._router.pathVariables(this.url);
};
*/
// TODO relative() // takes an absolutepath and returns a relative one
// TODO absolute() // takes a relative path and returns an absolute one.
export default function urlgrey(url: string) {
return new UrlGrey(url);
}
urlgrey.UrlGrey = UrlGrey;
// function addPropertyGetterSetter(propertyName: string, methodName?: string) {
// if (!methodName) {
// methodName = propertyName;
// }
// UrlGrey.prototype[methodName] = function (str) {
// if (!str && str !== "") {
// return this.parsed()[propertyName];
// }
// var obj = new UrlGrey(this.toString());
// obj.parsed()[propertyName] = str;
// return obj;
// };
// }