-
Notifications
You must be signed in to change notification settings - Fork 51
/
deno_transport.ts
298 lines (271 loc) · 7.8 KB
/
deno_transport.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
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
288
289
290
291
292
293
294
295
296
297
298
/*
* Copyright 2020-2024 The NATS Authors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
Deferred,
deferred,
TlsOptions,
} from "../nats-base-client/internal_mod.ts";
import { writeAll } from "https://deno.land/std@0.221.0/io/write_all.ts";
import {
checkOptions,
checkUnsupportedOption,
ConnectionOptions,
DataBuffer,
Empty,
ErrorCode,
extractProtocolMessage,
INFO,
NatsError,
render,
ServerInfo,
Transport,
} from "../nats-base-client/internal_mod.ts";
const VERSION = "1.28.2";
const LANG = "nats.deno";
const ReadBufferSize = 1024 * 256;
export class DenoTransport implements Transport {
version: string = VERSION;
lang: string = LANG;
closeError?: Error;
private options!: ConnectionOptions;
private buf: Uint8Array;
private encrypted = false;
private done = false;
private closedNotification: Deferred<void | Error> = deferred();
private conn!: Deno.TcpConn | Deno.TlsConn;
private frames: Array<Uint8Array>;
private pendingWrite: Promise<void> | null;
constructor() {
this.buf = new Uint8Array(ReadBufferSize);
this.frames = [];
this.pendingWrite = null;
}
async connect(
hp: { hostname: string; port: number; tlsName: string },
options: ConnectionOptions,
) {
this.options = options;
const { tls } = this.options;
const { handshakeFirst } = tls || {};
try {
if (handshakeFirst === true) {
const opts = await this.loadTlsOptions(hp.hostname);
const ctls = {
caCerts: opts.caCerts,
hostname: hp.hostname,
port: hp.port,
};
this.conn = await Deno.connectTls(ctls);
this.encrypted = true;
// do nothing yet.
} else {
this.conn = await Deno.connect(hp);
}
const info = await this.peekInfo();
checkOptions(info, this.options);
const { tls_required: tlsRequired, tls_available: tlsAvailable } = info;
const desired = tlsAvailable === true && options.tls !== null;
if (!handshakeFirst && (tlsRequired || desired)) {
const tlsn = hp.tlsName ? hp.tlsName : hp.hostname;
await this.startTLS(tlsn);
}
} catch (err) {
this.conn?.close();
throw err.name === "ConnectionRefused"
? NatsError.errorForCode(ErrorCode.ConnectionRefused)
: err;
}
}
get isClosed(): boolean {
return this.done;
}
async peekInfo(): Promise<ServerInfo> {
const inbound = new DataBuffer();
let pm: string;
while (true) {
const c = await this.conn.read(this.buf);
if (c === null) {
// EOF
throw new Error("socket closed while expecting INFO");
} else if (c) {
const frame = this.buf.subarray(0, c);
if (this.options.debug) {
console.info(`> ${render(frame)}`);
}
inbound.fill(frame);
const raw = inbound.peek();
pm = extractProtocolMessage(raw);
if (pm !== "") {
break;
}
}
}
// reset the buffer to previously read, so the client
// can validate the info matches the connection
this.buf = new Uint8Array(inbound.drain());
// expecting the info protocol
const m = INFO.exec(pm);
if (!m) {
throw new Error("unexpected response from server");
}
return JSON.parse(m[1]) as ServerInfo;
}
async loadTlsOptions(hostname: string): Promise<Deno.StartTlsOptions> {
const tls = this.options && this.options.tls
? this.options.tls
: {} as TlsOptions;
// these options are not available in Deno
checkUnsupportedOption("tls.ca", tls.ca);
checkUnsupportedOption("tls.cert", tls.cert);
checkUnsupportedOption("tls.certFile", tls.certFile);
checkUnsupportedOption("tls.key", tls.key);
checkUnsupportedOption("tls.keyFile", tls.keyFile);
const sto = { hostname } as Deno.StartTlsOptions;
if (tls.caFile) {
const ca = await Deno.readTextFile(tls.caFile);
sto.caCerts = [ca];
}
return sto;
}
async startTLS(hostname: string): Promise<void> {
const sto = await (this.loadTlsOptions(hostname));
this.conn = await Deno.startTls(
//@ts-ignore: just the conn
this.conn as Deno.TcpConn,
sto,
);
// this is necessary because the startTls process doesn't
// reject a bad certificate, however the next write will.
// to identify this as the error, we force it
await this.conn.write(Empty);
this.encrypted = true;
}
async *[Symbol.asyncIterator](): AsyncIterableIterator<Uint8Array> {
let reason: Error | undefined;
// yield what we initially read
yield this.buf;
while (!this.done) {
try {
this.buf = new Uint8Array(ReadBufferSize);
const c = await this.conn.read(this.buf);
if (c === null) {
break;
}
if (c) {
const frame = this.buf.subarray(0, c);
if (this.options.debug) {
console.info(`> ${render(frame)}`);
}
yield frame;
}
} catch (err) {
reason = err;
break;
}
}
this._closed(reason).then().catch();
}
maybeWriteFrame(): void {
if (this.pendingWrite) {
return;
}
const frame = this.frames.shift();
if (!frame) {
return;
}
this.pendingWrite = writeAll(this.conn, frame).then(() => {
this.pendingWrite = null;
this.maybeWriteFrame();
}).then(() => {
if (this.options.debug) {
console.info(`< ${render(frame)}`);
}
}).catch((err) => {
if (this.options.debug) {
console.error(`!!! ${render(frame)}: ${err}`);
}
});
}
send(frame: Uint8Array): void {
// if we are closed, don't do anything
if (this.done) {
return;
}
// push the frame
this.frames.push(frame);
this.maybeWriteFrame();
}
isEncrypted(): boolean {
return this.encrypted;
}
close(err?: Error): Promise<void> {
return this._closed(err, false);
}
disconnect() {
this._closed(undefined, true)
.then().catch();
}
async _closed(err?: Error, internal = true): Promise<void> {
if (this.done) return;
this.done = true;
this.closeError = err;
if (!err && internal) {
try {
// this is a noop but gives us a place to hang
// a close and ensure that we sent all before closing
// we wait for the operation to fail or succeed
this.frames.push(Empty);
this.maybeWriteFrame();
if (this.pendingWrite) {
await this.pendingWrite;
}
} catch (err) {
if (this.options.debug) {
console.log("transport close terminated with an error", err);
}
}
}
try {
this.conn?.close();
} catch (_err) {
// ignored
}
if (internal) {
this.closedNotification.resolve(err);
}
}
closed(): Promise<void | Error> {
return this.closedNotification;
}
discard() {
// ignored - this is not required, as there's no throttling
}
}
export async function denoResolveHost(s: string): Promise<string[]> {
const a = Deno.resolveDns(s, "A");
const aaaa = Deno.resolveDns(s, "AAAA");
const ips: string[] = [];
const w = await Promise.allSettled([a, aaaa]);
if (w[0].status === "fulfilled") {
ips.push(...w[0].value);
}
if (w[1].status === "fulfilled") {
ips.push(...w[1].value);
}
if (ips.length === 0) {
ips.push(s);
}
return ips;
}