forked from denodrivers/postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
100 lines (86 loc) · 2.51 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
import { Hash } 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])
);
}
const encoder = new TextEncoder();
function md5(bytes: Uint8Array): string {
return new Hash("md5").digest(bytes).hex();
}
// https://www.postgresql.org/docs/current/protocol-flow.html
// AuthenticationMD5Password
// The actual PasswordMessage can be computed in SQL as:
// concat('md5', md5(concat(md5(concat(password, username)), random-salt))).
// (Keep in mind the md5() function returns its result as a hex string.)
export function hashMd5Password(
username: string,
password: string,
salt: Uint8Array
): string {
const innerHash = md5(encoder.encode(password + username));
const innerBytes = encoder.encode(innerHash);
const outerBuffer = new Uint8Array(innerBytes.length + salt.length);
outerBuffer.set(innerBytes);
outerBuffer.set(salt, innerBytes.length);
const outerHash = md5(outerBuffer);
return "md5" + outerHash;
}
export interface DsnResult {
driver: string;
user: string;
password: string;
host: string;
port: string;
database: string;
params: {
[key: string]: string;
};
}
export function parseDsn(dsn: string): DsnResult {
const url = new URL(dsn);
const params = {};
for (const [key, value] of url.searchParams.entries()) {
params[key] = value;
}
return {
// remove trailing colon
driver: url.protocol.slice(0, url.protocol.length - 1),
user: url.username,
password: url.password,
host: url.hostname,
port: url.port,
// remove leading slash from path
database: url.pathname.slice(1),
params
};
}
export function delay<T>(ms: number, value?: T): Promise<T> {
return new Promise<T>((resolve, reject) => {
setTimeout(() => {
resolve(value);
}, ms);
});
}