This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
index.ts
327 lines (285 loc) · 8.82 KB
/
index.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import WebSocket from "isomorphic-ws";
// must polyfill AbortController to use axios >=0.20.0, <=0.27.2 on node <= v14.x
import "../polyfill";
import axios from "axios";
import {
base64ToJson,
jsonToBase64,
Message
} from "@truffle/dashboard-message-bus-common";
import debugModule from "debug";
import { MessageBusConnectionError } from "../errors";
import {
DashboardMessageBusConnectionEvents,
DashboardMessageBusConnectionOptions,
SocketEventHandlerMap
} from "./types";
import { TypedEmitter } from "tiny-typed-emitter";
import { tracked } from "@truffle/promise-tracker";
import delay from "delay";
const debug = debugModule("dashboard-message-bus-client:connection");
const debugMessage = debugModule("dashboard-message-bus-client:message");
type HandlerFactory<T> = (
resolve: (result: T) => void,
reject: (err?: any) => void
) => SocketEventHandlerMap;
export class DashboardMessageBusConnection extends TypedEmitter<DashboardMessageBusConnectionEvents> {
private _connectionType: "publish" | "subscribe";
private _socket: WebSocket | undefined;
private _host: string;
private _port: number;
private _publishPort: number | undefined;
private _subscribePort: number | undefined;
private _connecting: boolean;
constructor({
host,
port,
publishPort,
subscribePort,
connectionType: socketType
}: DashboardMessageBusConnectionOptions) {
super();
this._host = host;
this._port = port;
this._publishPort = publishPort;
this._subscribePort = subscribePort;
this._connectionType = socketType;
}
get isConnected() {
return this._socket && this._socket.readyState === WebSocket.OPEN;
}
get isConnecting() {
return this._socket && this._socket.readyState === WebSocket.CONNECTING;
}
get isClosing() {
return this._socket && this._socket.readyState === WebSocket.CLOSING;
}
async connect(): Promise<void> {
if (this._socket) {
switch (this._socket.readyState) {
case WebSocket.CONNECTING:
debug(
"connect: %s already attempting to connect (readyState switch)",
this._connectionType
);
await delay(10);
return this.connect();
case WebSocket.OPEN:
// we're already connected, just return
debug("connect: %s already connected", this._connectionType);
return;
case WebSocket.CLOSING:
case WebSocket.CLOSED:
debug(
"connect: %s was previously connected but has been closed",
this._connectionType
);
// already closed or on our way there, so we'll just recreate it in a
// moment
delete this._socket;
}
}
try {
if (this._connecting) {
debug(
"connect: %s already attempting to connect (_connecting flag)",
this._connectionType
);
await delay(10);
return this.connect();
}
this._connecting = true;
const port = await this._getMessageBusPort();
const url = `ws://${this._host}:${port}`;
debug(
"connect: %s is attempting to connect to %s",
this._connectionType,
url
);
this._socket = new WebSocket(url);
this._socket?.addEventListener(
"message",
((event: WebSocket.MessageEvent) => {
if (typeof event.data !== "string") {
event.data = event.data.toString();
}
const message = base64ToJson(event.data);
debugMessage(
"%s connection received message %o",
this._connectionType,
message
);
this.emit("message", message);
}).bind(this)
);
// connecting
// we now have a socket that's in the process of opening, so return a
// promise that resolves when it opens, or fails to open
const connectPromise = this._createEventWrapperPromise<void>(
(resolve, reject) => {
return {
open: () => {
debug(
"connect: %s connection succeeded to url %s",
this._connectionType,
this._socket?.url
);
if (this._connectionType === "subscribe") {
this._socket?.send("ready");
}
resolve();
this._connecting = false;
},
error: (event: WebSocket.ErrorEvent) => {
debug(
"connect: %s connection to url %s failed due to error %s",
this._connectionType,
this._socket?.url,
event.error
);
reject(
new MessageBusConnectionError({
message: event.error.message,
cause: event.error
})
);
this._connecting = false;
},
close: (event: WebSocket.CloseEvent) => {
debug(
"connect: %s connection to url %s closed before successfully connecting due to code %s and reason %s",
this._connectionType,
this._socket?.url,
event.code,
event.reason
);
reject(
new MessageBusConnectionError({
message: `Socket connection closed with code '${event.code}' and reason '${event.reason}'`
})
);
this._connecting = false;
}
};
}
);
let timedout = false;
await Promise.race([
connectPromise,
async () => {
await delay(350);
timedout = true;
}
]);
if (timedout) {
debug(
"connect: %s connection to url %s timed out",
this._connectionType,
url
);
}
} catch {
this._connecting = false;
}
}
async send(message: Message): Promise<void>;
async send(data: string): Promise<void>;
@tracked
async send(dataOrMessage: string | Message): Promise<void> {
const encodedMessage =
typeof dataOrMessage === "string"
? dataOrMessage
: jsonToBase64(dataOrMessage);
await this.connect();
debug(
"send: %s connection sending %o",
this._connectionType,
base64ToJson(encodedMessage)
);
this._socket?.send(encodedMessage);
}
async close(): Promise<void> {
if (!this._socket) {
return;
}
if (this._socket.readyState <= WebSocket.CLOSING) {
const promise = this._createEventWrapperPromise<void>(
(resolve, reject) => {
return {
error: (event: WebSocket.ErrorEvent) => {
reject(event.error);
},
close: () => {
debug("%s connection closed", this._connectionType);
resolve();
}
};
}
);
this._socket.close();
return promise;
}
}
private async _getMessageBusPort(): Promise<number> {
if (this._connectionType === "subscribe" && this._subscribePort) {
return this._subscribePort;
}
if (this._connectionType === "publish" && this._publishPort) {
return this._publishPort;
}
// otherwise, fetch it from the server
try {
debug(
"_getMessageBusPort: %s connection attempting to fetch ports",
this._connectionType
);
const { data } = await axios.get(
`http://${this._host}:${this._port}/ports`,
{
timeout: 350
}
);
const port =
this._connectionType === "subscribe"
? data.subscribePort
: data.publishPort;
debug(
"_getMessageBusPort: %s connection will use port %s",
this._connectionType,
port
);
return port;
} catch (err) {
debug(
"_getMessageBusPort: failed fetching ports for %s connection due to error %s",
this._connectionType,
err
);
throw err;
}
}
private _createEventWrapperPromise<T>(
handlerFactory: HandlerFactory<T>
): Promise<T> {
return new Promise<T>(
((resolve: (result: T) => void, reject: (err?: any) => void) => {
this._registerEventHandlers(handlerFactory.call(this, resolve, reject));
}).bind(this)
);
}
private _registerEventHandlers(handlers: SocketEventHandlerMap) {
let wrappedHandlers: SocketEventHandlerMap = {};
for (const eventType in handlers) {
wrappedHandlers[eventType] = ((...args: any[]) => {
handlers[eventType].call(this, ...args);
this._cleanUpEventHandlers(wrappedHandlers);
}).bind(this);
this._socket?.addEventListener(eventType, wrappedHandlers[eventType]);
}
}
private _cleanUpEventHandlers(handlers: SocketEventHandlerMap) {
for (const eventType in handlers) {
this._socket?.removeEventListener(eventType, handlers[eventType]);
}
}
}