-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
WebSocketManager.ts
313 lines (270 loc) · 8.31 KB
/
WebSocketManager.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
/* eslint-disable @typescript-eslint/no-base-to-string */
import { EventEmitter } from "node:events";
import type TypedEmitter from "typed-emitter";
import WebSocket from "ws";
import type { ClientUserData, SkeletonWSPayload, WSEvent, WSEventNames, WSPayload } from "../generated/api-typings";
import { WSOpCodes } from "../generated/api-typings";
import Heartbeater from "./Heartbeater";
export class WebSocketManager {
/**
* The version of the websocket to connect to.
*/
version = this.options.version ?? 1;
/**
* Token used to authenticate requests.
*/
token = this.options.token;
/**
* The websocket connected to guilded.
*/
socket: WebSocket | null = null;
/**
* Whether or not this connection is connected and heartbeating.
*/
isAlive = false;
/**
* The amount of milliseconds the websocket took to respond to the last ping request. This will be zero before the first heartbeat
*/
ping = 0;
/**
* The timestamp in milliseconds of the last ping request.
*/
lastPingedAt = 0;
/**
* The last message id received. Used in the event of resuming connections.
*/
lastMessageId: string | null = null;
/**
* The date since the last initial connection was established.
*/
connectedAt: Date | null = null;
/**
* Emitter in charge of emitting ws gateway related events
*/
emitter = new EventEmitter() as TypedEmitter<WebsocketManagerEvents>;
/**
* Count of how many times a reconnect has been attempted
*/
reconnectAttemptAmount = 0;
/**
* Heartbeating helper
*/
heartbeater: Heartbeater | null = null;
constructor(public readonly options: WebSocketOptions) {}
/**
* The url that will be used to connect. Prioritizes proxy url and if not available uses the default base url for guidled.
*/
get wsURL(): string {
return this.options.proxyURL ?? `wss://www.guilded.gg/websocket/v${this.version}`;
}
get reconnectAttemptExceeded(): boolean {
return this.reconnectAttemptAmount >= (this.options.reconnectAttemptLimit ?? Number.POSITIVE_INFINITY);
}
get shouldReplayMissedEvents(): boolean {
return this.options.replayMissedEvents !== false;
}
get shouldRequestMissedEvents(): boolean {
return this.shouldReplayMissedEvents && this.lastMessageId !== null;
}
connect(): void {
this._debug(`Connecting to Guilded WS Gateway at url ${this.wsURL}.`);
// Since rest supplies custom headers after authorization header, WS will be the same in that regard
const headers: Record<string, string> = {
Authorization: `Bearer ${this.token}`,
...this.options.headers,
};
if (this.shouldRequestMissedEvents) {
this._debug(`Requesting missed events from last message ${this.lastMessageId}.`);
if (this.lastMessageId) {
headers["guilded-last-message-id"] = this.lastMessageId;
}
}
try {
this.socket = new WebSocket(this.wsURL, {
headers,
});
this._debug("Socket created.");
} catch (error) {
this._debug(`Error creating socket ${(error as Error).message}.`);
if (!this.shouldRequestMissedEvents && this.lastMessageId) throw error;
this.lastMessageId = null;
if (error instanceof Error) {
this.emitter.emit("error", "Error connecting to socket", error);
}
this._handleDisconnect({
blockReconnects: false,
});
return;
}
this.socket.on("open", this.onSocketOpen.bind(this));
this.socket.on("ping", this.onSocketPing.bind(this));
this.socket.on("pong", this.onSocketPong.bind(this));
this.socket.on("message", (data) => {
this.emitter.emit("raw", data);
this.onSocketMessage(data.toString());
});
this.socket.on("error", (err) => {
this._debug(`Error received from WS. ${err.message}`);
this.emitter.emit("exit", "Gateway connection closed due to error.");
this._handleDisconnect({
blockReconnects: true,
});
});
this.socket.on("close", (code, reason) => {
this.emitter.emit("exit", "Gateway connection closed.");
this._handleDisconnect({
blockReconnects: false,
});
});
}
destroy(): void {
this._debug("Destroying websocket connection.");
if (!this.socket) {
throw new Error("There is no active connection to destroy.");
}
this.heartbeater?.destroy();
this.heartbeater = null;
this.socket.removeAllListeners();
if (!this.socket.CLOSED && !this.socket.CLOSING) this.socket.close();
this.socket = null;
this.isAlive = false;
}
_handleDisconnect(opts: {
blockReconnects: boolean;
}): void {
this._debug("Received request to disconnect.");
this.destroy();
this._debug(
`Checking if should reconnect.
Reconnect allowed: ${this.options.autoConnectOnErr}.
Reconnect attempt ${this.reconnectAttemptAmount}.
Reconnect attempt limit ${this.options.reconnectAttemptLimit}.`,
);
if ((!opts.blockReconnects && (this.options.autoConnectOnErr ?? true)) || !this.reconnectAttemptExceeded) {
this._debug("Reconnecting.");
this.reconnectAttemptAmount++;
this.connect();
}
}
_debug(str: unknown): boolean {
return this.emitter.emit("debug", `[DEBUG] ${str}`);
}
private onSocketMessage(packet: string): void {
let EVENT_NAME: string;
// biome-ignore lint/suspicious/noExplicitAny: Data can be anything json parseable
let EVENT_DATA: any;
try {
const data = JSON.parse(packet) as SkeletonWSPayload;
EVENT_NAME = data.t;
EVENT_DATA = data;
} catch (error) {
this.emitter.emit("error", "ERROR PARSING WS EVENT", error as Error, packet);
return;
}
this._debug(`Received event ${EVENT_NAME}. ${packet}}`);
// SAVE THE ID IF AVAILABLE. USED FOR RESUMING CONNECTIONS.
if (EVENT_DATA.s) this.lastMessageId = EVENT_DATA.s;
switch (EVENT_DATA.op) {
// Normal event based packets
case WSOpCodes.SUCCESS: {
this.emitter.emit("gatewayEvent", EVENT_NAME as WSEventNames, EVENT_DATA);
break;
}
// Auto handled by ws lib
case WSOpCodes.WELCOME: {
this._debug("Received welcome packet. Setting up heartbeat.");
this.heartbeater = new Heartbeater(this, (EVENT_DATA.d as WSPayload<"_WelcomeMessage">).heartbeatIntervalMs);
this.emitter.emit("ready", (EVENT_DATA.d as WSPayload<"_WelcomeMessage">).user as ClientUserData);
break;
}
case WSOpCodes.RESUME: {
this._debug("Received resume packet.");
this.lastMessageId = null;
break;
}
case WSOpCodes.ERROR: {
this._debug("Received error packet.");
this.emitter.emit(
"error",
"Error received from WS",
new Error(
(
EVENT_DATA.d as {
message: string;
}
).message,
),
);
this.lastMessageId = null;
this._handleDisconnect({
blockReconnects: false,
});
break;
}
default: {
this._debug("Received unknown opcode.");
this.emitter.emit("unknown", "unknown opcode", packet);
break;
}
}
}
private onSocketOpen(): void {
this._debug("Socket has been successfully opened and is ready to receive data.");
this.isAlive = true;
this.connectedAt = new Date();
}
private onSocketPing(): void {
this._debug("Ping request from Guilded received. Responding with a pong.");
this.socket?.pong();
}
private onSocketPong(): void {
this.ping = Date.now() - this.lastPingedAt;
this._debug(`Pong response from Guilded received. Latency: ${this.ping}ms.`);
}
}
export type WebSocketOptions = {
/**
* Whether to try to re-establish connection on error
*/
autoConnectOnErr?: boolean;
/**
* The base url that the websocket will connect to.
*/
proxyURL?: string;
/**
* Limit of how many times a reconnection should be attempted
*/
reconnectAttemptLimit?: number;
/**
* Whether the manager should request missed events on reconnect
*/
replayMissedEvents?: boolean;
/**
* The bot's token.
*/
token: string;
/**
* The version of the websocket to connect to.
*/
version?: 1;
/**
* The additional headers that will be added to WebSocket request upon initial connection.
*/
headers?: Record<string, string>;
};
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export type WebsocketManagerEvents = {
debug(data: unknown): unknown;
error(reason: string, err: Error | null, data?: unknown): unknown;
exit(info: string): unknown;
gatewayEvent(event: keyof WSEvent, data: SkeletonWSPayload): unknown;
raw(data: unknown): unknown;
ready(
user: WSPayload<"_WelcomeMessage">["user"] & {
createdBy: string;
botId: string;
},
): unknown;
reconnect(): unknown;
unknown(reason: string, data: unknown): unknown;
};