-
Notifications
You must be signed in to change notification settings - Fork 92
/
utils.ts
141 lines (122 loc) · 3.21 KB
/
utils.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
import { bold, yellow } from "../deps.ts";
export function readInt16BE(buffer: Uint8Array, offset: number): number {
offset = offset >>> 0;
const val = buffer[offset + 1] | (buffer[offset] << 8);
return val & 0x8000 ? val | 0xffff0000 : val;
}
export function readUInt16BE(buffer: Uint8Array, offset: number): number {
offset = offset >>> 0;
return buffer[offset] | (buffer[offset + 1] << 8);
}
export function readInt32BE(buffer: Uint8Array, offset: number): number {
offset = offset >>> 0;
return (
(buffer[offset] << 24) |
(buffer[offset + 1] << 16) |
(buffer[offset + 2] << 8) |
buffer[offset + 3]
);
}
export function readUInt32BE(buffer: Uint8Array, offset: number): number {
offset = offset >>> 0;
return (
buffer[offset] * 0x1000000 +
((buffer[offset + 1] << 16) |
(buffer[offset + 2] << 8) |
buffer[offset + 3])
);
}
export interface Uri {
driver: string;
host: string;
password: string;
path: string;
params: Record<string, string>;
port: string;
user: string;
}
type ConnectionInfo = {
driver?: string;
user?: string;
password?: string;
full_host?: string;
path?: string;
params?: string;
};
type ParsedHost = {
host?: string;
port?: string;
};
/**
* This function parses valid connection strings according to https://www.postgresql.org/docs/14/libpq-connect.html#LIBPQ-CONNSTRING
*
* The only exception to this rule are multi-host connection strings
*/
export function parseConnectionUri(uri: string): Uri {
const parsed_uri = uri.match(
/(?<driver>\w+):\/{2}((?<user>[^\/?#\s:]+?)?(:(?<password>[^\/?#\s]+)?)?@)?(?<full_host>[^\/?#\s]+)?(\/(?<path>[^?#\s]*))?(\?(?<params>[^#\s]+))?.*/,
);
if (!parsed_uri) throw new Error("Could not parse the provided URL");
let {
driver = "",
full_host = "",
params = "",
password = "",
path = "",
user = "",
}: ConnectionInfo = parsed_uri.groups ?? {};
const parsed_host = full_host.match(
/(?<host>(\[.+\])|(.*?))(:(?<port>[\w]*))?$/,
);
if (!parsed_host) throw new Error(`Could not parse "${full_host}" host`);
let {
host = "",
port = "",
}: ParsedHost = parsed_host.groups ?? {};
try {
if (host) {
host = decodeURIComponent(host);
}
} catch (_e) {
console.error(
bold(yellow("Failed to decode URL host") + "\nDefaulting to raw host"),
);
}
if (port && Number.isNaN(Number(port))) {
throw new Error(`The provided port "${port}" is not a valid number`);
}
try {
if (password) {
password = decodeURIComponent(password);
}
} catch (_e) {
console.error(
bold(
yellow("Failed to decode URL password") +
"\nDefaulting to raw password",
),
);
}
return {
driver,
host,
params: Object.fromEntries(new URLSearchParams(params).entries()),
password,
path,
port,
user,
};
}
export function isTemplateString(
template: unknown,
): template is TemplateStringsArray {
if (!Array.isArray(template)) {
return false;
}
return true;
}
/**
* https://www.postgresql.org/docs/14/runtime-config-connection.html#RUNTIME-CONFIG-CONNECTION-SETTINGS
* unix_socket_directories
*/
export const getSocketName = (port: number) => `.s.PGSQL.${port}`;