@bakit/gateway 3.0.1 → 3.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js DELETED
@@ -1,619 +0,0 @@
1
- import EventEmitter from 'events';
2
- import { createInflate, constants } from 'zlib';
3
- import { TextDecoder } from 'util';
4
- import { randomInt, randomUUID } from 'crypto';
5
- import WebSocket from 'ws';
6
- import { GatewayOpcodes, GatewayDispatchEvents, GatewayCloseCodes } from 'discord-api-types/v10';
7
- import { fork } from 'child_process';
8
- import { dirname, resolve } from 'path';
9
- import { fileURLToPath } from 'url';
10
- import { isCommonJS, Collection, Queue } from '@bakit/utils';
11
- import { REST } from '@bakit/rest';
12
-
13
- // src/lib/Shard.ts
14
- var MIN_HEARTBEAT_INTERVAL = 1e3, MAX_HEARTBEAT_INTERVAL = 6e4, SAFE_HEARTBEAT_INTERVAL = 45e3, ShardState = /* @__PURE__ */ ((ShardState2) => (ShardState2[ShardState2.Idle = 0] = "Idle", ShardState2[ShardState2.Connecting = 1] = "Connecting", ShardState2[ShardState2.Ready = 2] = "Ready", ShardState2[ShardState2.Resuming = 3] = "Resuming", ShardState2[ShardState2.Disconnecting = 4] = "Disconnecting", ShardState2[ShardState2.Disconnected = 5] = "Disconnected", ShardState2))(ShardState || {}), ShardStrategy = /* @__PURE__ */ ((ShardStrategy2) => (ShardStrategy2[ShardStrategy2.Resume = 0] = "Resume", ShardStrategy2[ShardStrategy2.Reconnect = 1] = "Reconnect", ShardStrategy2[ShardStrategy2.Shutdown = 2] = "Shutdown", ShardStrategy2))(ShardStrategy || {}), Shard = class extends EventEmitter {
15
- constructor(id, options) {
16
- super();
17
- this.id = id;
18
- this.options = options;
19
- }
20
- #state = 0 /* Idle */;
21
- #ws;
22
- #inflater;
23
- #textDecoder = new TextDecoder();
24
- #decompressBuffer = [];
25
- #sessionId;
26
- #lastSequence;
27
- #resumeGatewayURL;
28
- #lastHeartbeatSent = -1;
29
- #lastHeartbeatAck = -1;
30
- #missedHeartbeats = 0;
31
- #heartbeatInterval;
32
- #heartbeatTimeout;
33
- #reconnectTimeout;
34
- #strategy;
35
- get state() {
36
- return this.#state;
37
- }
38
- get latency() {
39
- return this.#lastHeartbeatSent === -1 || this.#lastHeartbeatAck === -1 ? -1 : this.#lastHeartbeatAck - this.#lastHeartbeatSent;
40
- }
41
- get resumable() {
42
- let hasSessionId = this.#sessionId !== void 0, hasSequence = this.#lastSequence !== void 0;
43
- return this.#strategy === 0 /* Resume */ && hasSequence && hasSessionId;
44
- }
45
- async connect() {
46
- if (this.#state !== 0 /* Idle */ && this.#state !== 5 /* Disconnected */)
47
- throw new Error("Shard already connecting or connected");
48
- return new Promise((resolve2, reject) => {
49
- let cleanup = () => {
50
- this.off("error", onError), this.off("ready", onReady);
51
- }, onReady = () => {
52
- cleanup(), resolve2();
53
- }, onError = (err) => {
54
- cleanup(), reject(err);
55
- };
56
- this.once("ready", onReady), this.once("error", onError), this.#init();
57
- });
58
- }
59
- disconnect(code) {
60
- return new Promise((resolve2) => {
61
- if (this.#state = 4 /* Disconnecting */, this.#strategy = 2 /* Shutdown */, !this.#ws) {
62
- resolve2();
63
- return;
64
- }
65
- this.#ws.once("close", () => {
66
- resolve2();
67
- }), this.#ws.close(code);
68
- });
69
- }
70
- resume() {
71
- this.resumable && (this.#state = 3 /* Resuming */, this.send({
72
- op: GatewayOpcodes.Resume,
73
- d: {
74
- token: this.options.token,
75
- session_id: this.#sessionId,
76
- seq: this.#lastSequence
77
- }
78
- }));
79
- }
80
- identify() {
81
- this.send({
82
- op: GatewayOpcodes.Identify,
83
- d: {
84
- token: this.options.token,
85
- intents: Number(this.options.intents),
86
- properties: {
87
- os: process.platform,
88
- browser: "bakit",
89
- device: "bakit"
90
- },
91
- shard: [this.id, this.options.total]
92
- }
93
- });
94
- }
95
- send(payload) {
96
- this.#ws?.readyState === WebSocket.OPEN && this.#ws.send(JSON.stringify(payload));
97
- }
98
- sendHeartbeat() {
99
- if (this.#lastHeartbeatSent !== -1 && this.#lastHeartbeatAck < this.#lastHeartbeatSent ? this.#missedHeartbeats++ : this.#missedHeartbeats = 0, this.#missedHeartbeats >= 2) {
100
- this.emit("debug", "Missed 2 heartbeats, reconnecting"), this.#ws?.terminate();
101
- return;
102
- }
103
- this.send({
104
- op: GatewayOpcodes.Heartbeat,
105
- d: this.#lastSequence ?? null
106
- }), this.#lastHeartbeatSent = Date.now();
107
- }
108
- #init() {
109
- this.#state = 1 /* Connecting */, this.#strategy ??= 1 /* Reconnect */;
110
- let url = new URL(
111
- this.#strategy === 0 /* Resume */ && this.#resumeGatewayURL ? this.#resumeGatewayURL : this.options.gateway.baseURL
112
- );
113
- url.searchParams.set("v", String(this.options.gateway.version)), url.searchParams.set("encoding", "json"), url.searchParams.set("compress", "zlib-stream"), this.#ws = new WebSocket(url, { perMessageDeflate: false }), this.#inflater = createInflate({ flush: constants.Z_SYNC_FLUSH }), this.#inflater.on("data", (chunk) => this.#onInflate(chunk)), this.#inflater.on("error", (err) => {
114
- this.emit("error", err), this.#ws?.terminate();
115
- }), this.#ws.on("message", (data) => this.#onMessage(data)), this.#ws.on("close", (code) => this.#onClose(code)), this.#ws.on("error", (err) => this.emit("error", err));
116
- }
117
- #onMessage(data) {
118
- if (!this.#inflater) {
119
- try {
120
- let text = data.toString(), payload = JSON.parse(text);
121
- this.#handlePayload(payload);
122
- } catch (error) {
123
- this.emit("error", error);
124
- }
125
- return;
126
- }
127
- let buffer;
128
- Buffer.isBuffer(data) ? buffer = data : Array.isArray(data) ? buffer = Buffer.concat(data) : data instanceof ArrayBuffer ? buffer = Buffer.from(data) : buffer = Buffer.from(String(data));
129
- let hasSyncFlush = buffer.length >= 4 && buffer[buffer.length - 4] === 0 && buffer[buffer.length - 3] === 0 && buffer[buffer.length - 2] === 255 && buffer[buffer.length - 1] === 255;
130
- this.#inflater.write(buffer, (writeError) => {
131
- if (writeError) {
132
- this.emit("error", writeError);
133
- return;
134
- }
135
- hasSyncFlush && this.#inflater?.flush(constants.Z_SYNC_FLUSH);
136
- });
137
- }
138
- #onInflate(chunk) {
139
- this.#decompressBuffer.push(chunk);
140
- let fullBuffer = Buffer.concat(this.#decompressBuffer);
141
- try {
142
- let text = this.#textDecoder.decode(fullBuffer), payload = JSON.parse(text);
143
- this.#handlePayload(payload), this.#decompressBuffer = [];
144
- } catch (error) {
145
- if (error instanceof SyntaxError) {
146
- let text = this.#textDecoder.decode(fullBuffer);
147
- if (text.includes("{") && !isValidJSON(text))
148
- return;
149
- this.emit("error", error), this.#decompressBuffer = [];
150
- }
151
- }
152
- }
153
- #handlePayload(payload) {
154
- switch (this.emit("raw", payload), payload.op) {
155
- case GatewayOpcodes.Dispatch: {
156
- this.#handleDispatch(payload);
157
- break;
158
- }
159
- case GatewayOpcodes.Hello: {
160
- this.#startHeartbeat(payload.d.heartbeat_interval), this.resumable ? this.resume() : this.emit("needIdentify");
161
- break;
162
- }
163
- case GatewayOpcodes.Heartbeat: {
164
- this.sendHeartbeat();
165
- break;
166
- }
167
- case GatewayOpcodes.HeartbeatAck: {
168
- this.#lastHeartbeatAck = Date.now();
169
- break;
170
- }
171
- case GatewayOpcodes.InvalidSession: {
172
- payload.d ? this.#strategy = 0 /* Resume */ : (this.#strategy = 1 /* Reconnect */, this.#sessionId = void 0, this.#lastSequence = void 0, this.#resumeGatewayURL = void 0), this.emit("debug", `Invalid session (resumable=${this.resumable})`), this.#ws?.terminate();
173
- break;
174
- }
175
- case GatewayOpcodes.Reconnect: {
176
- this.#strategy = 0 /* Resume */, this.emit("debug", "Reconnecting to gateway"), this.#ws?.terminate();
177
- break;
178
- }
179
- }
180
- }
181
- #handleDispatch(payload) {
182
- switch (this.#lastSequence = payload.s, this.emit("dispatch", payload), payload.t) {
183
- case GatewayDispatchEvents.Ready: {
184
- let { d: data } = payload;
185
- this.#state = 2 /* Ready */, this.#sessionId = data.session_id, this.#resumeGatewayURL = data.resume_gateway_url, this.emit("ready", data);
186
- break;
187
- }
188
- case GatewayDispatchEvents.Resumed: {
189
- this.#state = 2 /* Ready */, this.#strategy = void 0, this.emit("resume");
190
- break;
191
- }
192
- }
193
- }
194
- #onClose(code) {
195
- if (this.#cleanup(), this.#state = 5 /* Disconnected */, this.emit("disconnect", code), this.#strategy === 2 /* Shutdown */) {
196
- switch (code) {
197
- case GatewayCloseCodes.AuthenticationFailed:
198
- this.emit("error", new Error("Invalid token provided"));
199
- break;
200
- case GatewayCloseCodes.InvalidIntents:
201
- this.emit("error", new Error("Invalid intents provided"));
202
- break;
203
- case GatewayCloseCodes.DisallowedIntents:
204
- this.emit("error", new Error("Disallowed intents provided"));
205
- break;
206
- }
207
- return;
208
- } else this.#strategy || (this.#strategy = this.#getStrategy(code));
209
- (this.#strategy === 1 /* Reconnect */ || this.#strategy === 0 /* Resume */) && this.#scheduleReconnect();
210
- }
211
- #getStrategy(code) {
212
- switch (code) {
213
- case GatewayCloseCodes.AuthenticationFailed:
214
- case GatewayCloseCodes.InvalidIntents:
215
- case GatewayCloseCodes.DisallowedIntents:
216
- return 2 /* Shutdown */;
217
- case GatewayCloseCodes.InvalidSeq:
218
- case GatewayCloseCodes.SessionTimedOut:
219
- return 1 /* Reconnect */;
220
- default:
221
- return 0 /* Resume */;
222
- }
223
- }
224
- #scheduleReconnect(delay = 1e3) {
225
- this.#reconnectTimeout || (this.#reconnectTimeout = setTimeout(() => {
226
- this.#reconnectTimeout = void 0, this.#state = 0 /* Idle */, this.#init();
227
- }, delay));
228
- }
229
- #startHeartbeat(interval) {
230
- this.#heartbeatInterval && (clearInterval(this.#heartbeatInterval), this.#heartbeatInterval = void 0), (interval < MIN_HEARTBEAT_INTERVAL || interval > MAX_HEARTBEAT_INTERVAL) && (interval = SAFE_HEARTBEAT_INTERVAL);
231
- let jitter = randomInt(0, 10) / 100, firstDelay = Math.floor(interval * jitter);
232
- this.emit("debug", `Starting heartbeat (interval=${interval}ms, jitter=${firstDelay}ms)`), this.#heartbeatTimeout = setTimeout(() => {
233
- this.sendHeartbeat(), this.#heartbeatInterval = setInterval(() => this.sendHeartbeat(), interval);
234
- }, firstDelay);
235
- }
236
- #cleanup() {
237
- clearTimeout(this.#reconnectTimeout), clearInterval(this.#heartbeatInterval), clearTimeout(this.#heartbeatTimeout), this.#inflater?.destroy(), this.#inflater = void 0, this.#ws?.removeAllListeners(), this.#ws = void 0, this.#decompressBuffer = [], this.#missedHeartbeats = 0;
238
- }
239
- };
240
- function isValidJSON(str) {
241
- try {
242
- return JSON.parse(str), !0;
243
- } catch {
244
- return false;
245
- }
246
- }
247
- var EVAL_TIMEOUT = 3e4, __filename$1 = fileURLToPath(import.meta.url), __dirname$1 = dirname(__filename$1);
248
- function isDispatchPayload(payload) {
249
- return payload.op === "dispatch";
250
- }
251
- function isIdentifyPayload(payload) {
252
- return payload.op === "identify";
253
- }
254
- function isSendPayload(payload) {
255
- return payload.op === "send";
256
- }
257
- function isEvalRequestPayload(payload) {
258
- return payload.op === "eval";
259
- }
260
- function isEvalResponsePayload(payload) {
261
- return payload.op === "evalResponse";
262
- }
263
- var ClusterProcess = class _ClusterProcess extends EventEmitter {
264
- constructor(manager, id, options = {}) {
265
- super();
266
- this.manager = manager;
267
- this.id = id;
268
- this.setMaxListeners(0);
269
- let entry = resolve(__dirname$1, isCommonJS() ? "cluster.cjs" : "cluster.js");
270
- this.process = fork(entry, {
271
- env: options.env,
272
- execArgv: options.execArgv,
273
- stdio: ["inherit", "inherit", "inherit", "ipc"]
274
- }), this.#bindProcessEvents();
275
- }
276
- process;
277
- #pendingEvals = /* @__PURE__ */ new Map();
278
- get killed() {
279
- return this.process.killed || !this.process.connected;
280
- }
281
- kill(signal = "SIGTERM") {
282
- if (!this.killed) {
283
- for (let [nonce, pending] of this.#pendingEvals)
284
- clearTimeout(pending.timeout), pending.reject(new Error(`Process killed before eval completed (nonce: ${nonce})`));
285
- this.#pendingEvals.clear(), this.process.kill(signal);
286
- }
287
- }
288
- async eval(fn, ctx) {
289
- let nonce = randomUUID();
290
- return new Promise((resolve2, reject) => {
291
- let timeoutId = setTimeout(() => {
292
- this.#pendingEvals.delete(nonce), reject(new Error(`Eval timed out after ${EVAL_TIMEOUT}ms`));
293
- }, EVAL_TIMEOUT);
294
- this.#pendingEvals.set(nonce, {
295
- resolve: resolve2,
296
- reject,
297
- timeout: timeoutId
298
- });
299
- let context;
300
- try {
301
- context = JSON.stringify(ctx ?? null);
302
- } catch {
303
- reject(new Error("Eval context is not serializable"));
304
- return;
305
- }
306
- let script = `(${fn.toString()})(cluster, ${context})`;
307
- this.sendIPC({
308
- op: "eval",
309
- d: { nonce, script }
310
- });
311
- });
312
- }
313
- send(idOrPayload, payload) {
314
- let hasShardId = typeof idOrPayload == "number" && payload !== void 0, shardId = hasShardId ? idOrPayload : void 0, data = hasShardId ? payload : idOrPayload;
315
- this.sendIPC({
316
- op: "send",
317
- d: { shardId, data }
318
- });
319
- }
320
- sendIPC(message) {
321
- if (!(!this.process.connected || this.process.killed))
322
- try {
323
- this.process.send(message, void 0, void 0, (err) => {
324
- err && this.emit("error", err);
325
- });
326
- } catch (err) {
327
- this.emit("error", err);
328
- }
329
- }
330
- identifyShard(id) {
331
- this.sendIPC({
332
- op: "identify",
333
- d: id
334
- });
335
- }
336
- #bindProcessEvents() {
337
- this.process.on("message", (message) => this.#handleIPC(message)), this.process.on("error", (err) => this.emit("error", err)), this.process.on("disconnect", () => this.emit("debug", "Process disconnected")), this.process.on("exit", (code) => {
338
- for (let [nonce, pending] of this.#pendingEvals)
339
- clearTimeout(pending.timeout), pending.reject(new Error(`Process exited (code: ${code}) before eval completed (nonce: ${nonce})`));
340
- this.#pendingEvals.clear();
341
- });
342
- }
343
- #handleIPC(message) {
344
- if (this.#isValidPayload(message)) {
345
- if (isDispatchPayload(message)) {
346
- this.emit(message.t, ...message.d);
347
- return;
348
- }
349
- if (isEvalResponsePayload(message)) {
350
- this.#handleEvalResponse(message);
351
- return;
352
- }
353
- }
354
- }
355
- #handleEvalResponse(payload) {
356
- let pending = this.#pendingEvals.get(payload.d.nonce);
357
- if (!pending) {
358
- this.emit("debug", `Received eval response for unknown nonce: ${payload.d.nonce}`);
359
- return;
360
- }
361
- if (pending.timeout && clearTimeout(pending.timeout), this.#pendingEvals.delete(payload.d.nonce), payload.d.success)
362
- pending.resolve({
363
- success: true,
364
- data: payload.d.result,
365
- cluster: this
366
- });
367
- else {
368
- let error = new Error(payload.d.error ?? "Unknown eval error");
369
- pending.resolve({
370
- success: false,
371
- error,
372
- cluster: this
373
- });
374
- }
375
- }
376
- #isValidPayload(message) {
377
- if (typeof message != "object" || message === null)
378
- return false;
379
- let payload = message;
380
- return payload.op === "dispatch" || payload.op === "identify" || payload.op === "send" || payload.op === "eval" || payload.op === "evalResponse";
381
- }
382
- static bindProcess(cluster) {
383
- let superEmit = cluster.emit.bind(cluster), safeSend = (message) => {
384
- process.connected && process.send?.(message, void 0, void 0, (err) => {
385
- err && cluster.emit("error", err);
386
- });
387
- };
388
- cluster.emit = function(eventName, ...args) {
389
- let result = superEmit(eventName, ...args);
390
- return safeSend({
391
- op: "dispatch",
392
- t: eventName,
393
- d: args
394
- }), result;
395
- };
396
- let messageHandler = async (message) => {
397
- if (isIdentifyPayload(message)) {
398
- cluster.shards.get(message.d)?.identify();
399
- return;
400
- }
401
- if (isSendPayload(message)) {
402
- message.d.shardId !== void 0 ? cluster.send(message.d.shardId, message.d.data) : cluster.send(message.d.data);
403
- return;
404
- }
405
- if (isEvalRequestPayload(message)) {
406
- await _ClusterProcess.#handleEvalRequest(cluster, message, safeSend);
407
- return;
408
- }
409
- };
410
- process.on("message", messageHandler);
411
- }
412
- static async #handleEvalRequest(cluster, payload, safeSend) {
413
- let { nonce, script } = payload.d, executeEval = async () => await new Function("cluster", `return ${script}`)(cluster), timeoutId;
414
- try {
415
- let evalPromise = executeEval(), timeoutPromise = new Promise((_, reject) => {
416
- timeoutId = setTimeout(() => {
417
- reject(new Error(`Eval execution timed out after ${EVAL_TIMEOUT}ms`));
418
- }, EVAL_TIMEOUT);
419
- }), result = await Promise.race([evalPromise, timeoutPromise]);
420
- timeoutId && clearTimeout(timeoutId), safeSend({
421
- op: "evalResponse",
422
- d: {
423
- nonce,
424
- success: !0,
425
- result
426
- }
427
- });
428
- } catch (err) {
429
- timeoutId && clearTimeout(timeoutId), safeSend({
430
- op: "evalResponse",
431
- d: {
432
- nonce,
433
- success: false,
434
- result: void 0,
435
- error: err instanceof Error ? err.message : String(err)
436
- }
437
- });
438
- }
439
- }
440
- };
441
- var Cluster = class extends EventEmitter {
442
- constructor(id, options) {
443
- super();
444
- this.id = id;
445
- this.options = options;
446
- }
447
- shards = new Collection();
448
- #readyCount = 0;
449
- #starting = false;
450
- get size() {
451
- return this.shards.size;
452
- }
453
- get ready() {
454
- return this.#readyCount === this.options.shards.length;
455
- }
456
- async spawn() {
457
- if (!this.#starting) {
458
- this.#starting = true, this.emit("debug", `Spawning ${this.options.shards.length} shards...`);
459
- for (let i of this.options.shards)
460
- await this.#spawnShard(i);
461
- this.emit("debug", "All shards spawned");
462
- }
463
- }
464
- async shutdown(code = 1e3) {
465
- this.emit("debug", "Shutting down cluster...");
466
- let tasks = [];
467
- for (let shard of this.shards.values())
468
- tasks.push(shard.disconnect(code));
469
- await Promise.allSettled(tasks), this.emit("debug", "Cluster shutdown complete");
470
- }
471
- broadcast(fn) {
472
- for (let shard of this.shards.values())
473
- fn(shard);
474
- }
475
- send(idOrPayload, payload) {
476
- let hasId = typeof idOrPayload == "number" && payload !== void 0, shardId = hasId ? idOrPayload : void 0, data = hasId ? payload : idOrPayload;
477
- shardId !== void 0 ? this.shards.get(shardId)?.send(data) : this.broadcast((shard) => shard.send(data));
478
- }
479
- async #spawnShard(id) {
480
- let shard = new Shard(id, {
481
- token: this.options.token,
482
- intents: this.options.intents,
483
- total: this.options.total,
484
- gateway: this.options.gateway
485
- });
486
- this.#bindShardEvents(shard), this.shards.set(id, shard), this.emit("shardAdd", id), await shard.connect();
487
- }
488
- #bindShardEvents(shard) {
489
- let id = shard.id;
490
- shard.on("ready", () => {
491
- this.#readyCount++, this.emit("debug", `Shard ${id} ready`), this.emit("shardReady", id), this.ready && this.emit("ready");
492
- }), shard.on("resume", () => {
493
- this.emit("debug", `Shard ${id} resumed`), this.emit("shardResume", id);
494
- }), shard.on("disconnect", (code) => {
495
- this.emit("debug", `Shard ${id} disconnected (${code})`), this.emit("shardDisconnect", id, code);
496
- }), shard.on("error", (err) => {
497
- this.emit("debug", `Shard ${id} error: ${err.message}`), this.emit("shardError", id, err);
498
- }), shard.on("raw", (payload) => {
499
- this.emit("raw", id, payload);
500
- }), shard.on("dispatch", (payload) => {
501
- this.emit("dispatch", id, payload);
502
- }), shard.on("needIdentify", () => {
503
- this.emit("needIdentify", id);
504
- }), shard.on("debug", (msg) => {
505
- this.emit("debug", `[Shard ${id}] ${msg}`);
506
- });
507
- }
508
- };
509
- var ShardingManager = class extends EventEmitter {
510
- clusters = new Collection();
511
- options;
512
- rest;
513
- #gatewayInfo;
514
- #totalShards = 0;
515
- #readyCount = 0;
516
- #identifyQueue;
517
- constructor(options, rest) {
518
- super(), this.setMaxListeners(0), this.options = {
519
- shardsPerCluster: 5,
520
- totalShards: options.totalShards ?? "auto",
521
- ...options
522
- }, rest || (rest = new REST({ token: this.options.token })), this.rest = rest;
523
- }
524
- get totalClusters() {
525
- let { shardsPerCluster } = this.options;
526
- return shardsPerCluster <= 0 ? 0 : Math.ceil(this.totalShards / shardsPerCluster);
527
- }
528
- get totalShards() {
529
- return this.#totalShards;
530
- }
531
- get ready() {
532
- return this.#readyCount === this.totalClusters;
533
- }
534
- async spawn() {
535
- this.#gatewayInfo = await this.rest.get("/gateway/bot");
536
- let { session_start_limit: limit } = this.#gatewayInfo;
537
- this.#totalShards = typeof this.options.totalShards == "number" ? this.options.totalShards : this.#gatewayInfo.shards, this.#identifyQueue = new Queue({
538
- concurrency: limit.max_concurrency,
539
- intervalCap: limit.max_concurrency,
540
- interval: 5e3
541
- });
542
- let { totalShards, totalClusters } = this;
543
- this.emit("debug", `Spawning ${totalClusters} clusters (${totalShards} total shards)...`);
544
- let promises = [];
545
- for (let i = 0; i < totalClusters; i++)
546
- promises.push(this.#spawnCluster(i));
547
- await Promise.all(promises), this.emit("debug", "All clusters spawned");
548
- }
549
- async kill(signal = "SIGTERM") {
550
- this.emit("debug", "Shutting down all clusters...");
551
- let tasks = [];
552
- for (let cluster of this.clusters.values())
553
- tasks.push(
554
- new Promise((resolve2) => {
555
- cluster.process.once("exit", () => resolve2()), cluster.kill(signal);
556
- })
557
- );
558
- await Promise.all(tasks), this.emit("debug", "All clusters shut down");
559
- }
560
- broadcast(payload) {
561
- for (let cluster of this.clusters.values())
562
- cluster.send(payload);
563
- }
564
- async broadcastEval(fn) {
565
- let promises = this.clusters.map((cluster) => cluster.eval(fn));
566
- return Promise.all(promises);
567
- }
568
- requestIdentify(cluster, shardId) {
569
- this.#identifyQueue?.add(() => cluster.identifyShard(shardId));
570
- }
571
- #getShardIdsForCluster(clusterId) {
572
- let start = clusterId * this.options.shardsPerCluster, end = Math.min(start + this.options.shardsPerCluster, this.totalShards);
573
- return Array.from({ length: end - start }, (_, i) => start + i);
574
- }
575
- async #spawnCluster(id) {
576
- let shardIds = this.#getShardIdsForCluster(id), firstShardId = shardIds[0], lastShardId = shardIds[shardIds.length - 1];
577
- this.emit("debug", `Spawning cluster ${id} (shards ${firstShardId}-${lastShardId})`);
578
- let env = {
579
- ...process.env,
580
- BAKIT_CLUSTER_ID: String(id),
581
- BAKIT_CLUSTER_SHARD_TOTAL: String(this.totalShards),
582
- BAKIT_CLUSTER_SHARD_LIST: JSON.stringify(shardIds),
583
- BAKIT_DISCORD_TOKEN: this.options.token,
584
- BAKIT_DISCORD_INTENTS: String(this.options.intents),
585
- BAKIT_DISCORD_GATEWAY_URL: this.#gatewayInfo?.url,
586
- BAKIT_DISCORD_GATEWAY_VERSION: "10"
587
- }, cluster = new ClusterProcess(this, id, { env });
588
- this.#bindClusterEvents(cluster, id), this.clusters.set(id, cluster), this.emit("clusterCreate", cluster);
589
- }
590
- #bindClusterEvents(cluster, id) {
591
- cluster.on("ready", () => {
592
- this.#readyCount++, this.emit("clusterReady", cluster), this.ready && this.emit("ready");
593
- }), cluster.process.on("exit", (code) => {
594
- this.emit("clusterExit", cluster, code), this.clusters.delete(id), this.#readyCount = Math.max(0, this.#readyCount - 1);
595
- }), cluster.on("error", (err) => {
596
- this.emit("clusterError", cluster, err);
597
- }), cluster.on("debug", (msg) => {
598
- this.emit("debug", `[Cluster ${id}] ${msg}`);
599
- }), cluster.on("dispatch", (shardId, payload) => {
600
- this.emit("dispatch", cluster, shardId, payload);
601
- }), cluster.on("raw", (shardId, payload) => {
602
- this.emit("raw", cluster, shardId, payload);
603
- }), cluster.on("shardAdd", (shardId) => {
604
- this.emit("shardAdd", cluster, shardId);
605
- }), cluster.on("shardReady", (shardId) => {
606
- this.emit("shardReady", cluster, shardId);
607
- }), cluster.on("shardDisconnect", (shardId, code) => {
608
- this.emit("shardDisconnect", cluster, shardId, code);
609
- }), cluster.on("shardResume", (shardId) => {
610
- this.emit("shardResume", cluster, shardId);
611
- }), cluster.on("shardError", (shardId, error) => {
612
- this.emit("shardError", cluster, shardId, error);
613
- }), cluster.on("needIdentify", (shardId) => {
614
- this.requestIdentify(cluster, shardId);
615
- });
616
- }
617
- };
618
-
619
- export { Cluster, ClusterProcess, Shard, ShardState, ShardStrategy, ShardingManager };