/
Authority.ts
97 lines (74 loc) · 2.41 KB
/
Authority.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
import { Host, parseHost } from './Host';
import { parsePassword, Password } from './Password';
import { parsePort, Port } from './Port';
import { parseUsername, Username } from './Username';
const PATTERN =
/^((?<username>[^:@]+)(:(?<password>[^@]+))?@)?(?<host>[^:]+)(:(?<port>\d+))?$/;
/**
* An authority component of the URI.
* @see URI.authority
*/
export interface Authority {
readonly host: Host;
readonly port?: Port;
readonly username?: Username;
readonly password?: Password;
}
export function parseAuthority(authority: string): Authority {
const components = PATTERN.exec(authority)?.groups;
if (components) {
const { host, port, username, password } = components;
return validate({
host: parseHost(host),
port: port == null ? undefined : parsePort(port),
username: username == null ? undefined : parseUsername(username),
password: password == null ? undefined : parsePassword(password),
});
}
throw new URIError('Invalid authority');
}
export function serializeAuthority(authority: Authority): string {
const { host, port, username, password } = validate(authority);
let result = '';
if (username) {
result += username;
if (password) {
result += ':';
result += password;
}
result += '@';
}
result += host;
if (port) {
result += ':';
result += port;
}
return result;
}
export function withHost(host: Host) {
return (authority: Authority) => validate({ ...authority, host });
}
export function withPort(port: Port) {
return (authority: Authority) => validate({ ...authority, port });
}
export function withoutPort(authority: Authority): Authority {
return validate({ ...authority, port: undefined });
}
export function withUsername(username: Username) {
return (authority: Authority) => validate({ ...authority, username });
}
export function withoutUsername(authority: Authority): Authority {
return validate({ ...authority, username: undefined });
}
export function withPassword(password: Password) {
return (authority: Authority) => validate({ ...authority, password });
}
export function withoutPassword(authority: Authority): Authority {
return validate({ ...authority, password: undefined });
}
function validate(authority: Authority): Authority {
if (!authority.username && authority.password) {
throw new URIError('Username is required when the password is specified');
}
return authority;
}