-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.mjs
More file actions
287 lines (227 loc) · 7.52 KB
/
index.mjs
File metadata and controls
287 lines (227 loc) · 7.52 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
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
import hasUnpairedSurrogates from './internal/has-unpaired-surrogates.mjs';
import indexOfAny from './internal/index-of-any.mjs';
import uriDecodeNoNul from './internal/uri-decode-no-nul.mjs';
import OPTIONS from './internal/options.mjs';
// src/interfaces/libpq/fe-connect.c:5568: parse_connection_string
const parseConnectionString = connectionString => {
if (typeof connectionString !== 'string') {
throw new TypeError('Connection string must be a string');
}
// libpq normally parses a NUL-terminated string, so it doesn’t have the equivalent of this situation
if (connectionString.includes('\0')) {
throw new Error('Connection string can’t contain NUL characters');
}
// libpq normally parses a bytestring, but we have a string of UTF-16 text and intend to parse it like libpq would its UTF-8 encoding, so make sure the UTF-16 is valid and can be represented in UTF-8
if (hasUnpairedSurrogates(connectionString)) {
throw new Error('Connection string can’t contain unpaired surrogates');
}
const uriPrefixLength = getUriPrefixLength(connectionString);
return uriPrefixLength === 0
? parseKeyValue(connectionString)
: parseUri(connectionString, uriPrefixLength);
};
// src/interfaces/libpq/fe-connect.c:5588: uri_prefix_length
const getUriPrefixLength = connectionString => {
// src/interfaces/libpq/fe-connect.c:371: uri_designator, short_uri_designator
const prefix = /^postgres(?:ql)?:\/\//.exec(connectionString);
return prefix === null ? 0 : prefix[0].length;
};
const splitCredentials = uriCredentials => {
const i = uriCredentials.indexOf(':');
let encodedUser;
let encodedPassword;
if (i === -1) {
encodedUser = uriCredentials;
encodedPassword = '';
} else {
encodedUser = uriCredentials.substring(0, i);
encodedPassword = uriCredentials.substring(i + 1);
}
return {
user: encodedUser === '' ? null : uriDecodeNoNul(encodedUser),
password: encodedPassword === '' ? null : uriDecodeNoNul(encodedPassword),
};
};
// src/interfaces/libpq/fe-connect.c:191: PQconninfoOptions
const createConnInfo = () => ({
service: null,
user: null,
password: null,
passfile: null,
channel_binding: null,
connect_timeout: null,
dbname: null,
host: null,
hostaddr: null,
port: null,
client_encoding: null,
options: null,
application_name: null,
fallback_application_name: null,
keepalives: null,
keepalives_idle: null,
keepalives_interval: null,
keepalives_count: null,
tcp_user_timeout: null,
sslmode: null,
sslcompression: null,
sslcert: null,
sslkey: null,
sslpassword: null,
sslrootcert: null,
sslcrl: null,
sslcrldir: null,
sslsni: null,
requirepeer: null,
ssl_min_protocol_version: null,
ssl_max_protocol_version: null,
gssencmode: null,
krbsrvname: null,
gsslib: null,
replication: null,
target_session_attrs: null,
});
const parseUri = (connectionString, uriPrefixLength) => {
const result = createConnInfo();
// If credentials exist, parse them and start looking for netlocs after them
// src/interfaces/libpq/fe-connect.c:6168
const credentialsEnd = indexOfAny(connectionString, '@/', uriPrefixLength);
let netlocStart;
if (credentialsEnd !== -1 && connectionString.charAt(credentialsEnd) === '@') {
netlocStart = credentialsEnd + 1;
const credentials = splitCredentials(connectionString.substring(uriPrefixLength, credentialsEnd));
result.user = credentials.user;
result.password = credentials.password;
} else {
netlocStart = uriPrefixLength;
}
let hosts = '';
let ports = '';
let netlocEnd;
for (;;) {
let host;
let hostEnd;
if (netlocStart < connectionString.length && connectionString.charAt(netlocStart) === '[') {
// IPv6 address
const ipv6End = connectionString.indexOf(']', netlocStart + 1);
if (ipv6End === -1) {
throw new Error('Connection string IPv6 address missing closing square bracket');
}
if (ipv6End === netlocStart + 1) {
throw new Error('Connection string IPv6 address can’t be empty');
}
host = connectionString.substring(netlocStart + 1, ipv6End);
hostEnd = ipv6End + 1;
} else {
hostEnd = indexOfAny(connectionString, ':/?,', netlocStart);
if (hostEnd === -1) {
hostEnd = connectionString.length;
}
host = connectionString.substring(netlocStart, hostEnd);
}
let separator =
hostEnd === connectionString.length
? null
: connectionString.charAt(hostEnd);
if (![null, ':', '/', '?', ','].includes(separator)) {
throw new Error('Connection string host followed by unexpected character: ' + separator);
}
hosts += host;
if (separator === ':') {
let portEnd = indexOfAny(connectionString, '/?,', hostEnd + 1);
if (portEnd === -1) {
portEnd = connectionString.length;
}
ports += connectionString.substring(hostEnd + 1, portEnd);
hostEnd = portEnd;
separator =
hostEnd === connectionString.length
? null
: connectionString.charAt(hostEnd);
}
if (separator !== ',') {
netlocEnd = hostEnd;
break;
}
hosts += ',';
ports += ',';
netlocStart = hostEnd + 1;
}
if (hosts !== '') {
result.host = uriDecodeNoNul(hosts);
}
if (ports !== '') {
result.port = uriDecodeNoNul(ports);
}
let dbnameEnd;
if (netlocEnd < connectionString.length && connectionString.charAt(netlocEnd) === '/') {
dbnameEnd = connectionString.indexOf('?', netlocEnd + 1);
if (dbnameEnd === -1) {
dbnameEnd = connectionString.length;
}
if (dbnameEnd !== netlocEnd + 1) {
result.dbname = uriDecodeNoNul(connectionString.substring(netlocEnd + 1, dbnameEnd));
}
} else {
dbnameEnd = netlocEnd;
}
if (dbnameEnd + 1 < connectionString.length) {
parseUriParams(connectionString.substring(dbnameEnd + 1), result);
}
return result;
};
// Parses a non-empty string of URI parameters.
// src/interfaces/libpq/fe-connect.c:6366: conninfo_uri_parse_params
const parseUriParams = (paramsString, result) => {
const pairs = paramsString.split('&');
for (const pair of pairs) {
const pairParts = pair.split('=');
if (pairParts.length !== 2) {
throw new Error('Connection string parameter has invalid separators: ' + pair);
}
const [encodedKey, encodedValue] = pairParts;
let key = uriDecodeNoNul(encodedKey);
let value = uriDecodeNoNul(encodedValue);
// src/interfaces/libpq/fe-connect.c:6437: Special keyword handling for improved JDBC compatibility
if (key === 'ssl' && value === 'true') {
key = 'sslmode';
value = 'require';
}
store(key, value, result);
}
};
// src/interfaces/libpq/fe-connect.c:6614: conninfo_storeval
const store = (key, value, result) => {
if (key === 'requiressl') {
key = 'sslmode';
value = value.startsWith('1') ? 'require' : 'prefer';
}
if (!OPTIONS.has(key)) {
throw new Error('Connection string parameter unknown: ' + key);
}
result[key] = value;
};
const unescapeValue = escapedValue =>
escapedValue.replace(/\\(.?)/g, '$1');
const parseKeyValue = connectionString => {
const result = createConnInfo();
const pair = /[ \f\n\r\t\v]*(?:(?:([^= \f\n\r\t\v]+)[ \f\n\r\t\v]*)?=[ \f\n\r\t\v]*((?!')(?:[^ \f\n\r\t\v\\]|\\[^]?)+|'(?:[^'\\]|\\[^])*('?)|)|([^ \f\n\r\t\v]+))/y;
for (let match; (match = pair.exec(connectionString)) !== null;) {
if (match[4] !== undefined) {
throw new Error(`Connection string missing “=” after “${match[4]}”`);
}
const key = match[1] ?? '';
let escapedValue;
if (match[3] === undefined) {
escapedValue = match[2];
} else {
if (match[3] === '') {
throw new Error('Connection string missing closing quote');
}
escapedValue = match[2].slice(1, -1);
}
store(key, unescapeValue(escapedValue), result);
}
return result;
};
export default parseConnectionString;