-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathurl.mjs
504 lines (418 loc) · 16.4 KB
/
url.mjs
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
import * as l from './lang.mjs'
import * as s from './str.mjs'
/*
Reference: https://en.wikipedia.org/wiki/Uniform_Resource_Identifier
JS doesn't support multiline regexes. To read and edit this, manually reformat
into multiline, then combine back into one line.
*/
export const RE_URL = /^(?:(?<scheme>[A-Za-z][\w+.-]*):(?:(?<slash>[/][/])(?:(?<username>[^\s/?#@:]*)(?::(?<password>[^\s/?#@]*))?@)?(?<hostname>(?:\[[:\p{Hex_Digit}]*\]|[^\s/?#:]*))(?::(?<port>\d*))?)?)?(?<pathname>[^\s?#]*)(?:[?](?<query>[^\s#]*))?(?:[#](?<hash>[^\s]*))?$/u
export const RE_SCHEME = /^[A-Za-z][\w+.-]*$/
export const RE_SLASH = /^[/]*$/
export const RE_PROTOCOL = /^(?:(?<scheme>[A-Za-z][\w+.-]*):(?<slash>[/][/])?)?$/
export const RE_USERNAME = /^[^\s/?#@:]*$/
export const RE_PASSWORD = /^[^\s/?#@]*$/
export const RE_HOSTNAME = /^(?:\[[:\p{Hex_Digit}]*\]|[^\s/?#:]*)$/u
export const RE_PORT = /^\d*$/
export const RE_HOST = /^(?<hostname>(?:\[[:\p{Hex_Digit}]*\]|[^\s/?#:]*))(?::(?<port>\d*))?$/u
export const RE_ORIGIN = /^(?:(?<scheme>[A-Za-z][\w+.-]*):(?:(?<slash>[/][/])(?:(?<username>[^\s/?#@:]*)(?::(?<password>[^\s/?#@]*))?@)?(?<hostname>(?:\[[:\p{Hex_Digit}]*\]|[^\s/?#:]*))(?::(?<port>\d*))?)?)?$/u
export const RE_PATHNAME = /^[^\s?#]*$/
export const RE_HASH = /^\S*$/
export function query(val) {return new Query(val)}
export function toQuery(val) {return l.toInst(val, Query)}
export function url(val) {return new Url(val)}
export function toUrl(val) {return l.toInst(val, Url)}
export function toUrlOpt(val) {return l.toInstOpt(val, Url)}
export function urlJoin(val, ...vals) {return Url.join(val, ...vals)}
export class Query extends s.StrMap {
mut(val) {
if (l.isStr(val)) return this.mutFromStr(val)
return super.mut(val)
}
mutFromStr(src) {
const found = new Set()
for (const pair of s.split(unSearch(src, this.constructor.name), `&`)) {
const ind = pair.indexOf(`=`)
const key = ind >= 0 ? this.dec(pair.slice(0, ind)) : pair
const val = ind >= 0 ? this.dec(pair.slice(ind + 1)) : ``
if (found.has(key)) this.append(key, val)
else this.set(key, val), found.add(key)
}
return this
}
dec(val) {return queryDec(val)}
enc(val) {return queryEnc(val)}
toURLSearchParams() {
const out = new URLSearchParams()
for (let [key, val] of this.entries()) for (val of val) out.append(key, val)
return out
}
// TODO consider another method that ALWAYS prepends `?`.
toStringFull() {return toSearchFull(this.toString())}
toString() {
let out = ``
for (let [key, val] of super.entries()) {
key = this.enc(key)
for (val of val) out += `${out && `&`}${key}=${this.enc(val)}`
}
return out
}
toJSON() {return this.toString() || null}
}
/*
Our lexicon is a mixture of IETF standard and JS URL standard lexicons.
They disagree on some terms.
Reference: https://en.wikipedia.org/wiki/Uniform_Resource_Identifier
*/
export class Url extends l.Emp {
constructor(val) {
super()
this[schemeKey] = ``
this[slashKey] = ``
this[usernameKey] = ``
this[passwordKey] = ``
this[hostnameKey] = ``
this[portKey] = ``
this[pathnameKey] = ``
this[queryKey] = ``
this[hashKey] = ``
if (!isEmpty(val)) this.reset(val)
}
get scheme() {return this[schemeKey]}
set scheme(val) {if (!(this[schemeKey] = toScheme(val))) this.slash = ``}
get slash() {return this[slashKey]}
set slash(val) {
if (!(this[slashKey] = toSlash(val))) {
this[usernameKey] = ``
this[passwordKey] = ``
this[hostnameKey] = ``
this[portKey] = ``
}
}
get username() {return this[usernameKey]}
set username(val) {this[usernameKey] = toUsername(val, this[slashKey])}
get password() {return this[passwordKey]}
set password(val) {this[passwordKey] = toPassword(val, this[slashKey])}
get hostname() {return this[hostnameKey]}
set hostname(val) {this[hostnameKey] = toHostname(val, this[slashKey])}
get port() {return this[portKey]}
set port(val) {this[portKey] = toPort(val, this[slashKey])}
get pathname() {return this[pathnameKey]}
set pathname(val) {this[pathnameKey] = toPathname(val)}
get search() {return this[queryKey].toString()}
/*
This doesn't simply take and set a string, because we must normalize the input
for URL compatibility, by encoding any characters that aren't allowed to
occur in a query in a valid URL. This allows users to provide "invalid" query
strings which are converted to valid strings for URL encoding. This is
implemented for consistency and compatibility with the standard URL API,
which supports this feature. Unfortunately, the standard APIs do not expose
the algorithm. We approximate this, by always decoding the input.
*/
set search(val) {this.query = l.laxStr(val)}
get query() {
let val = this[queryKey]
const cls = this.Query
if (!l.isInst(val, cls)) this[queryKey] = val = new cls(val)
return val
}
set query(val) {
if (l.isNil(val) || l.isStr(val) && !val) this[queryKey] = ``
else this.query.reset(val)
}
get searchParams() {return this.query}
set searchParams(val) {this.query = val}
get hash() {return this[hashKey]}
set hash(val) {this[hashKey] = unHash(toHash(val))}
get protocol() {return this.schemeFull() + this.slash}
set protocol(val) {
const gro = reqGroups(val, RE_PROTOCOL, `protocol`)
this.slash = gro.slash
this.scheme = gro.scheme
}
get host() {return s.optSuf(this.hostname, this.portFull())}
set host(val) {
if (val && !this[slashKey]) throw errSlash(`host`)
const gro = reqGroups(val, RE_HOST, `host`)
this[hostnameKey] = l.laxStr(gro.hostname)
this[portKey] = l.laxStr(gro.port)
}
get origin() {return s.optPre(this.host, this.protocol)}
set origin(val) {
const gro = reqGroups(val, RE_ORIGIN, `origin`)
this[hostnameKey] = l.laxStr(gro.hostname)
this[portKey] = l.laxStr(gro.port)
this.username = l.laxStr(gro.username)
this.password = l.laxStr(gro.password)
this.slash = l.laxStr(gro.slash)
this.scheme = l.laxStr(gro.scheme)
}
get href() {return this.clean() + this.searchFull() + this.hashFull()}
set href(val) {this.reset(val)}
setScheme(val) {return this.scheme = val, this}
setSlash(val) {return this.slash = val, this}
setUsername(val) {return this.username = val, this}
setPassword(val) {return this.password = val, this}
setHostname(val) {return this.hostname = val, this}
setPort(val) {return this.port = val, this}
setPathname(val) {return this.pathname = val, this}
setSearch(val) {return this.search = val, this}
setSearchParams(val) {return this.searchParams = val, this}
setQuery(val) {return this.query = val, this}
setHash(val) {return this.hash = val, this}
setHashExact(val) {return this[hashKey] = toHash(val), this}
setProtocol(val) {return this.protocol = val, this}
setHost(val) {return this.host = val, this}
setOrigin(val) {return this.origin = val, this}
setHref(val) {return this.href = val, this}
withScheme(val) {return this.clone().setScheme(val)}
withSlash(val) {return this.clone().setSlash(val)}
withUsername(val) {return this.clone().setUsername(val)}
withPassword(val) {return this.clone().setPassword(val)}
withHostname(val) {return this.clone().setHostname(val)}
withPort(val) {return this.clone().setPort(val)}
withPathname(val) {return this.clone().setPathname(val)}
withSearch(val) {return this.withoutQuery().setSearch(val)}
withSearchParams(val) {return this.withQuery(val)}
withQuery(val) {return this.withoutQuery().setQuery(val)}
withHash(val) {return this.clone().setHash(val)}
withHashExact(val) {return this.clone().setHashExact(val)}
withProtocol(val) {return this.clone().setProtocol(val)}
withHost(val) {return this.clone().setHost(val)}
withOrigin(val) {return this.clone().setOrigin(val)}
withHref(val) {return this.clone().setHref(val)}
withoutQuery() {
const val = this[queryKey]
this[queryKey] = ``
try {return this.clone()}
finally {this[queryKey] = val}
}
schemeFull() {return s.optSuf(this[schemeKey], `:`)}
portFull() {return s.maybePre(this[portKey], `:`)}
pathnameFull() {return s.optPre(this[pathnameKey], `/`) || `/`}
searchFull() {return toSearchFull(this.search)}
hashFull() {return s.maybePre(this[hashKey], `#`)}
base() {return `${this.protocol}${this.authFull()}${this.host}`}
hostPath() {return s.inter(this.host, `/`, this.pathname)}
auth() {return this.username + s.maybePre(this.password, `:`)}
authFull() {return s.optSuf(this.auth(), `@`)}
rel() {return this.pathname + this.searchFull() + this.hashFull()}
/*
Very similar to `.origin` but includes auth and pathname. This omits only
query and hash. See https://en.wikipedia.org/wiki/Clean_URL which seems to
describe the same concept.
*/
clean() {return this.protocol + this.authFull() + this.hostPath()}
withPath(...val) {return this.clone().setPath(...val)}
setPath(...val) {return this.setPathname().addPath(...val)}
addPath(...val) {return val.forEach(this.addSeg, this), this}
addSeg(seg) {
const val = l.renderLax(seg)
if (!val) throw SyntaxError(`invalid empty URL segment ${l.show(seg)}`)
this[pathnameKey] = s.inter(this[pathnameKey], `/`, val)
return this
}
setPathOpt(...val) {return val.forEach(this.setSegOpt, this), this}
addPathOpt(...val) {return val.forEach(this.addSegOpt, this), this}
addSegOpt(seg) {
this[pathnameKey] = s.inter(this[pathnameKey], `/`, l.renderLax(seg))
return this
}
queryMut(val) {return this.query.mut(val), this}
querySet(key, val) {return this.query.set(key, val), this}
// TODO: consider supporting `window.Location` for better performance.
// Benchmark first. Avoid code bloat.
reset(val) {
if (l.isNil(val)) return this.clear()
if (l.isStr(val)) return this.resetFromStr(val)
if (isURL(val)) return this.resetFromURL(val)
if (isUrl(val)) return this.resetFromUrl(val)
if (isUrlLike(val)) return this.resetFromStr(val.href)
throw l.errConvInst(val, this)
}
resetFromStr(val) {
const gro = urlParse(val)
this[schemeKey] = l.laxStr(gro.scheme)
this[slashKey] = l.laxStr(gro.slash)
this[usernameKey] = l.laxStr(gro.username)
this[passwordKey] = l.laxStr(gro.password)
this[hostnameKey] = l.laxStr(gro.hostname)
this[portKey] = l.laxStr(gro.port)
this[pathnameKey] = l.laxStr(gro.pathname)
this[queryKey] = l.laxStr(gro.query)
this[hashKey] = l.laxStr(gro.hash)
return this
}
resetFromURL(val) {
l.req(val, isURL)
this[schemeKey] = s.stripSuf(val.protocol, `:`)
this[slashKey] = val.href.startsWith(val.protocol + `//`) ? `//` : ``
this[usernameKey] = val.username
this[passwordKey] = val.password
this[hostnameKey] = val.hostname
this[portKey] = val.port
this[pathnameKey] = val.pathname
this.query.reset(val.searchParams)
this[hashKey] = unHash(val.hash)
return this
}
resetFromUrl(val) {
l.req(val, isUrl)
this[schemeKey] = val[schemeKey]
this[slashKey] = val[slashKey]
this[usernameKey] = val[usernameKey]
this[passwordKey] = val[passwordKey]
this[hostnameKey] = val[hostnameKey]
this[portKey] = val[portKey]
this[pathnameKey] = val[pathnameKey]
this.query = val[queryKey]
this[hashKey] = val[hashKey]
return this
}
clear() {
this[schemeKey] = ``
this[slashKey] = ``
this[usernameKey] = ``
this[passwordKey] = ``
this[hostnameKey] = ``
this[portKey] = ``
this[pathnameKey] = ``
this[queryKey] = ``
this[hashKey] = ``
}
clone() {return new this.constructor(this)}
toURL() {return new this.URL(this.href)}
toString() {return this.href}
toJSON() {return this.toString() || null}
valueOf() {return this.href}
get Query() {return Query}
static join(val, ...vals) {return new this(val).addPath(...vals)}
}
export function loc(val) {return new Loc(val)}
export function toLoc(val) {return l.toInst(val, Loc)}
/*
Short for "location". Variant of `Url` with awareness of DOM APIs. Uses both
`window.location` and `window.history`, providing various shortcuts for
manipulating location and history.
Additional properties are symbolic for consistency with `Url`.
Getters and setters also perform type checking.
*/
export class Loc extends Url {
constructor(val) {
super()
this[stateKey] = undefined
this[titleKey] = ``
this.reset(val)
}
get state() {return this[stateKey]}
set state(val) {this[stateKey] = val}
get title() {return this[titleKey]}
set title(val) {this[titleKey] = l.laxStr(val)}
withState(val) {return this.clone().setState(val)}
setState(val) {return this.state = val, this}
withTitle(val) {return this.clone().setTitle(val)}
setTitle(val) {return this.title = val, this}
push() {
this.history.pushState(this.state, this.title, this)
return this
}
replace() {
this.history.replaceState(this.state, this.title, this)
return this
}
reload() {
this.location.href = this
return this
}
// Allows `new Loc(loc)` and `loc.clone()`.
resetFromUrl(val) {
super.resetFromUrl(val)
this.state = val.state
this.title = val.title
return this
}
eq(val) {
return (
!!l.optInst(val, Loc) &&
l.is(this.state, val.state) &&
l.is(this.title, val.title) &&
l.is(this.href, val.href)
)
}
get history() {return this.constructor.history}
get location() {return this.constructor.location}
static get history() {return globalThis.history}
static get location() {return globalThis.location}
// Note: at the time of writing, browsers don't store the title anywhere.
static current() {
return new this(this.location).setState(this.history.state)
}
}
export const stateKey = Symbol.for(`state`)
export const titleKey = Symbol.for(`title`)
export const schemeKey = Symbol.for(`scheme`)
export const slashKey = Symbol.for(`slash`)
export const usernameKey = Symbol.for(`username`)
export const passwordKey = Symbol.for(`password`)
export const hostnameKey = Symbol.for(`hostname`)
export const portKey = Symbol.for(`port`)
export const pathnameKey = Symbol.for(`pathname`)
export const queryKey = Symbol.for(`query`)
export const hashKey = Symbol.for(`hash`)
export function urlParse(val) {return reqGroups(val, RE_URL, `url`)}
// TODO consider moving to `str.mjs`. Might rename.
function reqGroups(val, reg, msg) {
const mat = l.laxStr(val).match(reg)
return l.convSynt(mat && mat.groups, val, msg)
}
function errSlash(msg) {return SyntaxError(`${msg} is forbidden in URL without protocol double slash`)}
function unHash(val) {return s.stripPre(val, `#`)}
function toScheme(val) {return toStrWith(val, RE_SCHEME, `scheme`)}
function toSlash(val) {return toStrWith(val, RE_SLASH, `slash`)}
function toUsername(val, slash) {return toStrWithSlash(val, RE_USERNAME, `username`, slash)}
function toPassword(val, slash) {return toStrWithSlash(val, RE_PASSWORD, `password`, slash)}
function toHostname(val, slash) {return toStrWithSlash(val, RE_HOSTNAME, `hostname`, slash)}
function toPort(val, slash) {return reqSlash(encodePort(val), slash, `port`)}
function toPathname(val) {return toStrWith(val, RE_PATHNAME, `pathname`)}
function toHash(val) {return toStrWith(val, RE_HASH, `hash`)}
function encodePort(val) {
if (l.isNat(val)) return val.toString()
return toStrWith(val, RE_PORT, `port`)
}
function unSearch(val, msg) {
if (l.isStr(val)) {
if (val.includes(`#`)) throw l.errSynt(val, msg)
return s.stripPre(val, `?`)
}
throw l.errConv(val, msg)
}
function toSearchFull(val) {return s.maybePre(val, `?`)}
function toStrWith(val, reg, msg) {
if (l.isNil(val)) return ``
if (l.isStr(val)) {
if (val && !reg.test(val)) throw l.errSynt(val, msg)
return val
}
throw l.errConv(val, msg)
}
function toStrWithSlash(val, reg, msg, slash) {
return reqSlash(toStrWith(val, reg, msg), slash, msg)
}
function reqSlash(val, slash, msg) {
if (val && !slash) throw errSlash(msg)
return val
}
function isEmpty(val) {return l.isNil(val) || val === ``}
function isURL(val) {return l.isInst(val, URL)}
function isUrl(val) {return l.isInst(val, Url)}
function isUrlLike(val) {return l.isStruct(val) && `href` in val}
// Needs optimization. This is currently our bottleneck.
export function queryDec(val) {
if (val.includes(`+`)) val = val.replace(/[+]/g, ` `)
return decodeURIComponent(val)
}
// Needs optimization. This is currently one of our bottlenecks.
export function queryEnc(val) {
val = encodeURIComponent(val)
if (val.includes(`%20`)) val = val.replace(/%20/g, `+`)
return val
}