-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
index.js
432 lines (432 loc) · 17.4 KB
/
index.js
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
* Copyright (c) 2020. MeLike2D All Rights Reserved.
* Neo is licensed under the MIT License.
* See the LICENSE file in the project root for more details.
*/ "use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "Shard", {
enumerable: true,
get: function() {
return Shard;
}
});
const _class_private_field_get = require("@swc/helpers/_/_class_private_field_get");
const _class_private_field_init = require("@swc/helpers/_/_class_private_field_init");
const _class_private_field_set = require("@swc/helpers/_/_class_private_field_set");
const _interop_require_default = require("@swc/helpers/_/_interop_require_default");
const _ws = /*#__PURE__*/ _interop_require_default._(require("ws"));
const _utils = require("../../utils");
const _connection = require("./connection");
const _serialization = require("./serialization");
const _compression = require("./compression");
const connectionStates = Object.keys(_ws.default);
var /**
* The serialization handler.
* @type {Serialization}
*/ _serialization1 = /*#__PURE__*/ new WeakMap(), /**
* The compression handler.
* @type {Compression}
*/ _compression1 = /*#__PURE__*/ new WeakMap(), /**
* The current sequence.
* @type {number}
*/ _seq = /*#__PURE__*/ new WeakMap(), /**
* The shard sequence when the websocket last closed.
* @type {number}
*/ _closingSeq = /*#__PURE__*/ new WeakMap(), /**
* The rate-limit bucket.
* @type {Bucket}
*/ _bucket = /*#__PURE__*/ new WeakMap(), /**
* The rate-limit bucket for presence updates.
* @type {Bucket}
*/ _presenceBucket = /*#__PURE__*/ new WeakMap(), /**
* The current connection.
* @type {WebSocket}
*/ _ws1 = /*#__PURE__*/ new WeakMap(), /**
* Packets that are waiting to be sent.
* @type {DiscordPacket[]}
*/ _queue = /*#__PURE__*/ new WeakMap();
class Shard extends _utils.Emitter {
/**
* The current sequence
* @type {number}
*/ get seq() {
return _class_private_field_get._(this, _seq);
}
/**
* The closing sequence.
* @type {number}
*/ get closingSeq() {
return _class_private_field_get._(this, _closingSeq);
}
/**
* The client instance.
* @type {Client}
*/ get client() {
return this.manager.client;
}
/**
* Whether this shard is connected to the gateway or not.
* @type {boolean}
*/ get connected() {
return _class_private_field_get._(this, _ws1) && _class_private_field_get._(this, _ws1).readyState === _ws.default.OPEN;
}
/**
* Send a new packet to the discord gateway.
* @param {DiscordPacket} data The packet to send.
* @param {boolean} [prioritized=false] Whether to prioritize this packet.
*/ send(data, prioritized = false) {
if (this.connected) {
let _i = 0, _w = 1;
const func = ()=>{
if (++_i < _w) {
return;
}
const encoded = _class_private_field_get._(this, _serialization1).encode(data);
_class_private_field_get._(this, _ws1).send(encoded);
};
if (data.op === _utils.GatewayOp.PRESENCE_UPDATE) {
++_w;
_class_private_field_get._(this, _presenceBucket).queue(func, prioritized);
}
return _class_private_field_get._(this, _bucket).queue(func, prioritized);
}
_class_private_field_get._(this, _queue)[prioritized ? "unshift" : "push"](data);
}
/**
* Destroys this shard.
* @param {ShardDestroyOptions} [options={}]
*/ destroy({ code = 1000, emit = true, log = true, reset = false } = {}) {
if (log) {
this._debug(`Destroying; Code = ${code}, Resetting? = ${reset ? "yes" : "no"}`);
}
// (Step 0) Reset some of the session/heartbeat stuff.
this.heartbeat.reset();
this.session.clearHelloTimeout();
// (Step 1) Close the websocket connection.
if (_class_private_field_get._(this, _ws1)) {
if (_class_private_field_get._(this, _ws1).readyState === _ws.default.OPEN) {
_class_private_field_get._(this, _ws1).close(code);
} else {
this._debug(`Ws State: ${connectionStates[_class_private_field_get._(this, _ws1).readyState]}`);
this._cleanupConnection();
try {
_class_private_field_get._(this, _ws1).close(code);
} catch {
// no-op
}
if (emit) {
/**
* Emitted whenever the shard was destroyed.
* @event Shard#destroyed
*/ this.emit(_utils.ShardEvent.DESTROYED);
}
}
} else if (emit) {
this.emit(_utils.ShardEvent.DESTROYED);
}
// (Step 3) Make the websocket connection prop undefined.
_class_private_field_set._(this, _ws1, undefined);
// (Step 4) Set the shard status to disconnected.
this.status = _utils.Status.DISCONNECTED;
// (Step 5) Cache ze current sequence!
if (_class_private_field_get._(this, _seq) !== -1) {
_class_private_field_set._(this, _closingSeq, _class_private_field_get._(this, _seq));
}
// (Step 6) Reset the shit.
if (reset) {
_class_private_field_set._(this, _seq, -1);
this.session.reset();
}
// (Step 7) Reset ze bucket!
_class_private_field_set._(this, _bucket, new _utils.Bucket(120, 6e4));
}
connect() {
/* Step 0 - Check if a connection already exists. If so identify the session. */ if (this.connected) {
this._debug("A connection is already present, attempting to identify.");
this.session.identify();
return;
}
/* Step 1 - If a socket is already present, destroy it. */ if (_class_private_field_get._(this, _ws1)) {
this._debug("A connection is already present, cleaning up...");
this.destroy({
emit: false
});
}
/* Step 2 - Setup serialization and compression. */ const qs = new URLSearchParams();
qs.append("v", this.manager.options.version.toString());
// Step 2.1 - Serialization
const encoding = this.manager.options.useEtf ? "etf" : "json";
qs.append("encoding", encoding);
_class_private_field_set._(this, _serialization1, _serialization.Serialization.create(encoding));
// Step 2.2 - Compression
if (this.manager.compression) {
_class_private_field_set._(this, _compression1, _compression.Compression.create(this.manager.compression).on("data", (buffer)=>this._packet(buffer)).on("error", (error)=>this.emit(_utils.ShardEvent.ERROR, error)).on("debug", (message)=>this._debug(message)));
qs.append("compress", "zlib-stream");
}
/* Step 5 - Set the status and wait for the hello op code. */ this.status = this.status === _utils.Status.DISCONNECTED ? _utils.Status.RECONNECTING : _utils.Status.CONNECTING;
this.session.setHelloTimeout();
/**
* The timestamp in which this shard connected.
* @type {number}
*/ this.connectedAt = Date.now();
/* Step 6 - Connect to the gateway. */ const uri = this.manager.gatewayUrl.replace(/\/*$/m, "");
_class_private_field_set._(this, _ws1, new _ws.default(`${uri}/?${qs}`));
/* Step 7 - Attach the listeners. */ _class_private_field_get._(this, _ws1).onopen = this._open.bind(this);
_class_private_field_get._(this, _ws1).onclose = this._close.bind(this);
_class_private_field_get._(this, _ws1).onerror = this._error.bind(this);
_class_private_field_get._(this, _ws1).onmessage = this._message.bind(this);
}
_packet(raw) {
/** @type {DiscordPacket} */ let pak;
try {
pak = _class_private_field_get._(this, _serialization1).decode(raw);
this.manager.emit(_utils.ClientEvent.RAW_PACKET, pak, this);
} catch (e) {
this.manager.client.emit(_utils.ClientEvent.SHARD_ERROR, e, this);
return;
}
switch(pak.t){
case "READY":
/**
* Emitted whenever the Shard receives the READY payload.
* @event Shard#ready
*/ this.emit(_utils.ShardEvent.READY);
this.session.id = pak.d.session_id;
this.expectedGuilds = new Set(pak.d.guilds.map((g)=>g.id));
this.status = _utils.Status.WAITING_FOR_GUILDS;
this.heartbeat.acked = true;
this.heartbeat.new("ready");
break;
case "RESUMED":
/**
* Emitted when a shards connection has been resumed.
* @event Shard#resumed
*/ this.emit(_utils.ShardEvent.RESUMED);
this.status = _utils.Status.READY;
this.heartbeat.acked = true;
this.heartbeat.new("resumed");
break;
}
if (pak.s !== null) {
if (_class_private_field_get._(this, _seq) !== -1 && pak.s > _class_private_field_get._(this, _seq) + 1) {
this._debug(`Non-consecutive sequence [${_class_private_field_get._(this, _seq)} => ${pak.s}]`);
}
_class_private_field_set._(this, _seq, pak.s);
}
switch(pak.op){
case _utils.GatewayOp.HELLO:
this.heartbeat.delay = pak.d.heartbeat_interval;
this.session.hello();
break;
case _utils.GatewayOp.RECONNECT:
this._debug("Gateway asked us to reconnect.");
this.destroy({
code: 4000
});
break;
case _utils.GatewayOp.INVALID_SESSION:
this._debug(`Invalid Session: Resumable => ${pak.d}`);
if (pak.d) {
this.session.resume();
break;
}
_class_private_field_set._(this, _seq, -1);
this.session.reset();
this.status = _utils.Status.RECONNECTING;
this.emit(_utils.ShardEvent.INVALID_SESSION);
break;
case _utils.GatewayOp.HEARTBEAT:
this.heartbeat.new("requested");
break;
case _utils.GatewayOp.HEARTBEAT_ACK:
this.heartbeat.ack();
break;
default:
if (this.status === _utils.Status.WAITING_FOR_GUILDS && pak.t === "GUILD_CREATE") {
this.expectedGuilds.delete(pak.d.id);
this._checkReady();
}
}
}
_checkReady() {
if (this._readyTimeout) {
_utils.Timers.clearTimeout(this._readyTimeout);
delete this._readyTimeout;
}
if (!this.expectedGuilds.size) {
this._debug("Received all guilds. Marking as full-ready.");
this.status = _utils.Status.READY;
return this.emit(_utils.ShardEvent.FULL_READY);
}
/**
* The ready timeout.
* @type {NodeJS.Timeout}
* @private
*/ this._readyTimeout = _utils.Timers.setTimeout(()=>{
this._debug("Didn't receive any more guilds within 15 seconds.");
this.status = _utils.Status.READY;
delete this._readyTimeout;
this.emit(_utils.ShardEvent.FULL_READY, this.expectedGuilds);
}, 15e3);
}
/**
* Called whenever the websocket opens.
* @private
*/ _open() {
var _class_private_field_get1;
this.status = _utils.Status.HANDSHAKING;
this._debug(`Connected. ${(_class_private_field_get1 = _class_private_field_get._(this, _ws1)) === null || _class_private_field_get1 === void 0 ? void 0 : _class_private_field_get1.url} in ${Date.now() - this.connectedAt}`);
if (_class_private_field_get._(this, _queue).length) {
this._debug(`${_class_private_field_get._(this, _queue).length} packets waiting... sending all now.`);
while(_class_private_field_get._(this, _queue).length){
const pk = _class_private_field_get._(this, _queue).shift();
if (!pk) {
break;
}
this.send(pk);
}
}
}
/**
* Called whenever the websocket encounters an error.
* @param {WebSocket.ErrorEvent} event
* @private
*/ _error(event) {
const error = event.error ? event.error : event;
if (error) {
this.manager.client.emit(_utils.ClientEvent.SHARD_ERROR, error, this);
}
}
/**
* Called whenever the websocket closes.
* @param {WebSocket.CloseEvent} evt
* @private
*/ _close(evt) {
var _ref;
const reason = (_ref = evt.reason || _utils.GatewayCloseCode[evt.code]) !== null && _ref !== void 0 ? _ref : "unknown";
this._debug(`Closed; Code = ${evt.code}, Clean? = ${evt.wasClean}, Reason = ${reason}`);
if (_class_private_field_get._(this, _seq) !== -1) {
_class_private_field_set._(this, _closingSeq, _class_private_field_get._(this, _seq));
}
_class_private_field_set._(this, _seq, -1);
this.heartbeat.reset();
this.session.clearHelloTimeout();
if (_class_private_field_get._(this, _ws1)) {
this._cleanupConnection();
}
this.status = _utils.Status.DISCONNECTED;
/**
* Emitted whenever the shard's websocket closes.
* @event Shard#close
* @param {WebSocket.CloseEvent} event The received event
*/ this.emit(_utils.ShardEvent.CLOSE, evt);
}
/**
* Called whenever the websocket receives a message.
* @param {WebSocket.MessageEvent} evt
* @private
*/ _message(evt) {
return _class_private_field_get._(this, _compression1) ? _class_private_field_get._(this, _compression1).add(evt.data) : this._packet(evt.data);
}
/**
* Cleans up the WebSocket connection listeners.
* @private
*/ _cleanupConnection() {
_class_private_field_get._(this, _ws1).onopen = _class_private_field_get._(this, _ws1).onclose = _class_private_field_get._(this, _ws1).onerror = _class_private_field_get._(this, _ws1).onmessage = null;
}
/**
* Used for debugging shard stuff.
* @param {string} message The debug message.
* @private
*/ _debug(message) {
return this.manager._debug(message, this);
}
/**
* @param {ShardManager} manager The shard manager.
* @param {number} id The ID of this shard.
*/ constructor(manager, id){
super();
_class_private_field_init._(this, _serialization1, {
writable: true,
value: void 0
});
_class_private_field_init._(this, _compression1, {
writable: true,
value: void 0
});
_class_private_field_init._(this, _seq, {
writable: true,
value: void 0
});
_class_private_field_init._(this, _closingSeq, {
writable: true,
value: void 0
});
_class_private_field_init._(this, _bucket, {
writable: true,
value: void 0
});
_class_private_field_init._(this, _presenceBucket, {
writable: true,
value: void 0
});
_class_private_field_init._(this, _ws1, {
writable: true,
value: void 0
});
_class_private_field_init._(this, _queue, {
writable: true,
value: void 0
});
/**
* The sharding manager.
* @type {ShardManager}
*/ this.manager = manager;
/**
* The ID of this shard.
* @type {number}
*/ this.id = id;
/**
* The session stuff for this shard.
* @type {Session}
*/ this.session = new _connection.Session(this);
/**
* The heartbeat stuff for this shard.
* @type {Heartbeat}
*/ this.heartbeat = new _connection.Heartbeat(this);
/**
* The latency of this shard.
* @type {number}
*/ this.latency = 0;
/**
* The status of this shard.
* @type {Status}
*/ this.status = _utils.Status.IDLE;
/**
* Guilds that are expected to be received.
* @type {Set<string>}
*/ this.expectedGuilds = new Set();
// Private shit
_class_private_field_set._(this, _seq, -1);
_class_private_field_set._(this, _closingSeq, 0);
_class_private_field_set._(this, _queue, []);
_class_private_field_set._(this, _bucket, new _utils.Bucket(120, 6e4, {
reservedTokens: 5
}));
_class_private_field_set._(this, _presenceBucket, new _utils.Bucket(5, 6e4));
}
} /**
* @typedef {Object} DiscordPacket
* @property {number} [op]
* @property {*} [d]
* @property {number | null} [s]
* @property {string} [t]
*/ /**
* @typedef {Object} ShardDestroyOptions
* @property {boolean} [reset=false] Whether to reset the shard.
* @property {boolean} [emit=true] Whether to emit the "destroyed" event.
* @property {boolean} [log=false] Whether to emit a debug log.
* @property {number} [code=1000] The code to use.
*/