-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnection.ts
177 lines (166 loc) · 5.43 KB
/
connection.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
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
import * as util from "./util.ts";
import Reader from "./reader.ts";
import Writer from "./writer.ts";
import Message from "./message.ts";
import Query from "./query.ts";
interface Config {
user?: string;
password: string;
database?: string;
hostname?: string;
certFile?: string;
port?: number;
}
export default class Connection {
private conn!: Deno.Conn;
private readonly encoding = "utf-8";
private user: string;
private password: string;
private hostname: string;
private database: string;
private certFile: string;
private port: number;
private encoder = new TextEncoder();
private decoder = new TextDecoder();
private activeQuery = new Query();
private processID?: string;
private secretKey?: string;
constructor(config: Config) {
this.user = config.user ?? "postgres";
this.hostname = config.hostname ?? "127.0.0.1";
this.database = config.database ?? "postgres";
this.port = config.port ?? 5432;
this.certFile = config.certFile ?? "";
this.password = config.password;
}
async connect(): Promise<void> {
try {
const { port, hostname } = this;
this.conn = await Deno.connect({ port, hostname });
if (this.certFile) await this.requestSsl();
await this.startup();
} catch (error) {
if (error.name === "ECONNRESET" || error.name === "EPIPE") return;
throw error;
}
}
async startup(): Promise<void> {
const body = {
user: this.user,
database: this.database,
client_encoding: "'utf-8'",
};
const size = Writer.getSize(8, body);
const writer = new Writer(size);
writer.writeInt32(size).writeInt16(3).writeInt16(0).writeBody(body);
await Deno.writeAll(this.conn, writer.buffer);
await this.readMessage();
}
private async readMessage(): Promise<void> {
const buffer = await util.readMsg(this.conn);
const reader = new Reader(buffer!);
let chunk: Uint8Array | null;
while (chunk = reader.read()) {
if (chunk === null || chunk.length === 0) break;
const message = new Message({
chunk,
length: reader.length,
header: reader.header,
});
if (message.name === "error") {
throw new Error(message.message);
}
await this.handleMessage(message);
}
}
async handleMessage(message: Message): Promise<void> {
switch (message.name) {
case "AuthenticationMD5Password":
const md5Password = util.Md5Password(
this.user,
this.password,
message.salt,
);
await this.auth(md5Password);
break;
case "AuthenticationCleartextPassword":
await this.auth(this.password);
break;
case "BackendKeyData":
this.processID = message.processID;
this.secretKey = message.secretKey;
break;
case "AuthenticationOk":
case "ParameterStatus":
break;
case "ReadyForQuery":
return;
case "RowDescription":
await this.activeQuery.handleRowDescription(message);
break;
case "DataRow":
await this.activeQuery.handleDataRow(message);
break;
case "CommandComplete":
await this.activeQuery.handleCommandComplete(message);
return;
case "NoticeResponse":
await this.activeQuery.handleCommandComplete(message);
return;
default:
console.debug(message);
throw new Error("Unknown response");
}
}
async auth(password: string): Promise<void> {
const encodedPassword = this.encoder.encode(password);
const passwordLen = encodedPassword.byteLength;
const size = 1 + 4 + passwordLen + 1; // Byte1('p') + Int32 + String + 1(null terminator)
const writer = new Writer(size);
writer.writeHeader("p").writeInt32(size - 1).write(encodedPassword);
await Deno.writeAll(this.conn, writer.buffer);
await this.readMessage();
}
async query(sql: string): Promise<Query> {
this.activeQuery = new Query();
const encodedSql = this.encoder.encode(sql);
const sqlLen = encodedSql.byteLength;
const size = 1 + 4 + sqlLen + 1; // Byte1('Q') + Int32 + String + 1(null terminator)
const writer = new Writer(size);
writer.writeHeader("Q").writeInt32(size - 1).write(encodedSql);
await Deno.writeAll(this.conn, writer.buffer);
await this.readMessage();
return this.activeQuery;
}
async end(): Promise<void> {
const writer = new Writer(5);
writer.writeHeader("X").writeInt32(4);
await this.conn.write(writer.buffer);
this.conn.close();
}
async requestSsl(): Promise<void> {
// warn: no null terminator
const size = 8;
const writer = new Writer(size);
writer.writeInt32(size).writeInt16(1234).writeInt16(5679);
await Deno.writeAll(this.conn, writer.buffer);
const buffer = await util.readMsg(this.conn);
const responseCode = this.decoder.decode(buffer!);
switch (responseCode) {
case "S": // Server supports SSL connections, continue with a secure connection
break;
case "N": // Server does not support SSL connections
this.end();
throw new Error("Server does not support SSL connections");
default: // Any other response byte, including 'E' (ErrorResponse) indicating a server error
this.end();
throw new Error("There was an error establishing an SSL connection");
}
const { port, hostname, certFile } = this;
this.conn = await Deno.connectTls({
port,
hostname,
certFile,
});
}
}