-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathuri.ts
More file actions
202 lines (176 loc) · 5.66 KB
/
uri.ts
File metadata and controls
202 lines (176 loc) · 5.66 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
// URI parser based on RFC 3986
// We can't use the standard `URL` object, because we want to support relative `file:` URLs like
// `file:relative/path/database.db`, which are not correct according to RFC 8089, which standardizes the
// `file` scheme.
import { LibsqlError } from "./api.js";
export interface Uri {
scheme: string;
authority: Authority | undefined;
path: string;
query: Query | undefined;
fragment: string | undefined;
}
export interface HierPart {
authority: Authority | undefined;
path: string;
}
export interface Authority {
host: string;
port: number | undefined;
userinfo: Userinfo | undefined;
}
export interface Userinfo {
username: string;
password: string | undefined;
}
export interface Query {
pairs: Array<KeyValue>;
}
export interface KeyValue {
key: string;
value: string;
}
export function parseUri(text: string): Uri {
const match = URI_RE.exec(text);
if (match === null) {
throw new LibsqlError(
`The URL '${text}' is not in a valid format`,
"URL_INVALID",
);
}
const groups = match.groups!;
const scheme = groups["scheme"]!;
const authority =
groups["authority"] !== undefined
? parseAuthority(groups["authority"])
: undefined;
const path = percentDecode(groups["path"]!);
const query =
groups["query"] !== undefined ? parseQuery(groups["query"]) : undefined;
const fragment =
groups["fragment"] !== undefined
? percentDecode(groups["fragment"])
: undefined;
return { scheme, authority, path, query, fragment };
}
const URI_RE = (() => {
const SCHEME = "(?<scheme>[A-Za-z][A-Za-z.+-]*)";
const AUTHORITY = "(?<authority>[^/?#]*)";
const PATH = "(?<path>[^?#]*)";
const QUERY = "(?<query>[^#]*)";
const FRAGMENT = "(?<fragment>.*)";
return new RegExp(
`^${SCHEME}:(//${AUTHORITY})?${PATH}(\\?${QUERY})?(#${FRAGMENT})?$`,
"su",
);
})();
function parseAuthority(text: string): Authority {
const match = AUTHORITY_RE.exec(text);
if (match === null) {
throw new LibsqlError(
"The authority part of the URL is not in a valid format",
"URL_INVALID",
);
}
const groups = match.groups!;
const host = percentDecode(groups["host_br"] ?? groups["host"]);
const port = groups["port"] ? parseInt(groups["port"], 10) : undefined;
const userinfo =
groups["username"] !== undefined
? {
username: percentDecode(groups["username"]),
password:
groups["password"] !== undefined
? percentDecode(groups["password"])
: undefined,
}
: undefined;
return { host, port, userinfo };
}
const AUTHORITY_RE = (() => {
return new RegExp(
`^((?<username>[^:]*)(:(?<password>.*))?@)?((?<host>[^:\\[\\]]*)|(\\[(?<host_br>[^\\[\\]]*)\\]))(:(?<port>[0-9]*))?$`,
"su",
);
})();
// Query string is parsed as application/x-www-form-urlencoded according to the Web URL standard:
// https://url.spec.whatwg.org/#urlencoded-parsing
function parseQuery(text: string): Query {
const sequences = text.split("&");
const pairs = [];
for (const sequence of sequences) {
if (sequence === "") {
continue;
}
let key: string;
let value: string;
const splitIdx = sequence.indexOf("=");
if (splitIdx < 0) {
key = sequence;
value = "";
} else {
key = sequence.substring(0, splitIdx);
value = sequence.substring(splitIdx + 1);
}
pairs.push({
key: percentDecode(key.replaceAll("+", " ")),
value: percentDecode(value.replaceAll("+", " ")),
});
}
return { pairs };
}
function percentDecode(text: string): string {
try {
return decodeURIComponent(text);
} catch (e) {
if (e instanceof URIError) {
throw new LibsqlError(
`URL component has invalid percent encoding: ${e}`,
"URL_INVALID",
undefined,
undefined,
e,
);
}
throw e;
}
}
export function encodeBaseUrl(
scheme: string,
authority: Authority | undefined,
path: string,
): URL {
if (authority === undefined) {
throw new LibsqlError(
`URL with scheme ${JSON.stringify(scheme + ":")} requires authority (the "//" part)`,
"URL_INVALID",
);
}
const schemeText = `${scheme}:`;
const hostText = encodeHost(authority.host);
const portText = encodePort(authority.port);
const userinfoText = encodeUserinfo(authority.userinfo);
const authorityText = `//${userinfoText}${hostText}${portText}`;
let pathText = path.split("/").map(encodeURIComponent).join("/");
if (pathText !== "" && !pathText.startsWith("/")) {
pathText = "/" + pathText;
}
return new URL(`${schemeText}${authorityText}${pathText}`);
}
function encodeHost(host: string): string {
return host.includes(":") ? `[${encodeURI(host)}]` : encodeURI(host);
}
function encodePort(port: number | undefined): string {
return port !== undefined ? `:${port}` : "";
}
function encodeUserinfo(userinfo: Userinfo | undefined): string {
if (userinfo === undefined) {
return "";
}
const usernameText = encodeURIComponent(userinfo.username);
const passwordText =
userinfo.password !== undefined
? `:${encodeURIComponent(userinfo.password)}`
: "";
return `${usernameText}${passwordText}@`;
}