This repository has been archived by the owner on Sep 19, 2023. It is now read-only.
/
ServerLocation.ts
146 lines (108 loc) · 2.93 KB
/
ServerLocation.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
import { ParsedHref, parseHref } from './parseHref'
const HTTPS_PROTOCOL = 'https:'
const HTTPS_DEFAULT_PORT = '443'
const HTTP_DEFAULT_PORT = '80'
/**
* An in-memory implementation of `Location`.
* @name ServerLocation
*/
export class ServerLocation implements Location {
get ancestorOrigins(): DOMStringList {
return ([] as any) as DOMStringList
}
get hash(): string {
return parseValue('hash', this)
}
set hash(value: string) {
const hash = value.startsWith('#') ? value : '#' + value
replace('hash', hash, this)
}
get host(): string {
return parseValue('host', this)
}
set host(value: string) {
replace('host', value, this)
}
get hostname(): string {
return parseValue('hostname', this)
}
set hostname(value: string) {
replace('hostname', value, this)
}
get pathname(): string {
return parseValue('pathname', this)
}
set pathname(value: string) {
replace('pathname', value, this)
}
get port(): string {
const { href } = this
const { port, protocol } = parseHref(href)
if (port) {
return port
}
return protocol === HTTPS_PROTOCOL ? HTTPS_DEFAULT_PORT : HTTP_DEFAULT_PORT
}
set port(value: string) {
replace('port', value, this)
}
get protocol(): string {
return parseValue('protocol', this) || 'http:'
}
set protocol(value: string) {
replace('protocol', value, this)
}
get search(): string {
return parseValue('search', this)
}
set search(value: string) {
const search = value.startsWith('?') ? value : '?' + value
replace('search', search, this)
}
get origin(): string {
return this.protocol + '//' + this.host
}
public href!: string
private history!: History
constructor(href: string) {
this.replace(href)
}
public assign(url: string): void {
this.replace(url)
if (this.history) {
this.history.pushState(null, '', this.href)
}
}
// Does not have defined behavior outside of browser
// tslint:disable-next-line:no-empty
public reload(): void {}
public replace(url: string): void {
// tslint:disable-next-line:prefer-const
let { href, host, relative } = parseHref(url)
if (this.host && !host) {
href = this.host + href
}
const { protocol } = parseHref(href)
if (href !== relative && this.protocol && !protocol) {
href = this.protocol + '//' + href
}
this.href = href
}
public toString(): string {
return this.href
}
// ServerLocation Specific
public setHistory(history: History) {
this.history = history
return this
}
}
function replace(key: keyof ParsedHref, value: string, location: ServerLocation) {
const { href } = location
const currentValue = parseHref(href)[key]
const updateHref = href.replace(currentValue, value)
location.replace(updateHref)
}
function parseValue(key: keyof ParsedHref, location: ServerLocation): string {
return parseHref(location.href)[key] as string
}