@bakit/gateway 2.1.8 → 2.1.9
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 +34 -39
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -7,7 +7,7 @@ import { fork } from 'child_process';
|
|
|
7
7
|
import '@bakit/rest';
|
|
8
8
|
|
|
9
9
|
// src/lib/shard.ts
|
|
10
|
-
var
|
|
10
|
+
var DEFAULT_SHARD_OPTIONS = {
|
|
11
11
|
gateway: {
|
|
12
12
|
baseURL: "wss://gateway.discord.gg",
|
|
13
13
|
version: 10
|
|
@@ -34,7 +34,7 @@ var INCOMPLETE_JSON_REGEX = /unexpected end|unterminated|unexpected eof|end of j
|
|
|
34
34
|
Shutdown: 4
|
|
35
35
|
};
|
|
36
36
|
function createShard(options) {
|
|
37
|
-
let resolvedOptions = { ...DEFAULT_SHARD_OPTIONS, ...options }, state = ShardState.Idle, strategy = ShardStrategy.Unknown, ws, resumeGatewayURL, inflater,
|
|
37
|
+
let resolvedOptions = { ...DEFAULT_SHARD_OPTIONS, ...options }, textDecoder = new TextDecoder(), state = ShardState.Idle, strategy = ShardStrategy.Unknown, ws, resumeGatewayURL, inflater, decompressBuffer = [], sessionId, lastSequence, lastHeartbeatSent = -1, lastHeartbeatAcknowledged = -1, missedHeartbeats = 0, reconnectTimeout, heartbeatTimeout, heartbeatInterval, self = attachEventBus({
|
|
38
38
|
send,
|
|
39
39
|
connect,
|
|
40
40
|
disconnect,
|
|
@@ -66,11 +66,8 @@ function createShard(options) {
|
|
|
66
66
|
self.emit("error", err), ws?.terminate();
|
|
67
67
|
}), ws.on("message", onMessage), ws.on("close", onClose), ws.on("error", (err) => self.emit("error", err));
|
|
68
68
|
}
|
|
69
|
-
function isJSONIncomplete(err) {
|
|
70
|
-
return err instanceof Error && INCOMPLETE_JSON_REGEX.test(err.message);
|
|
71
|
-
}
|
|
72
69
|
function cleanup() {
|
|
73
|
-
heartbeatInterval && (clearInterval(heartbeatInterval), heartbeatInterval = void 0), heartbeatTimeout && (clearTimeout(heartbeatTimeout), heartbeatTimeout = void 0), reconnectTimeout && (clearTimeout(reconnectTimeout), reconnectTimeout = void 0), inflater && (inflater.destroy(), inflater = void 0),
|
|
70
|
+
heartbeatInterval && (clearInterval(heartbeatInterval), heartbeatInterval = void 0), heartbeatTimeout && (clearTimeout(heartbeatTimeout), heartbeatTimeout = void 0), reconnectTimeout && (clearTimeout(reconnectTimeout), reconnectTimeout = void 0), inflater && (inflater.destroy(), inflater = void 0), ws && (ws.readyState !== WebSocket.CLOSED && ws.terminate(), ws.removeAllListeners(), ws = void 0), decompressBuffer = [], missedHeartbeats = 0, lastHeartbeatSent = -1, lastHeartbeatAcknowledged = -1;
|
|
74
71
|
}
|
|
75
72
|
function connect() {
|
|
76
73
|
return new Promise((resolve) => {
|
|
@@ -89,48 +86,46 @@ function createShard(options) {
|
|
|
89
86
|
});
|
|
90
87
|
}
|
|
91
88
|
function onMessage(data) {
|
|
92
|
-
if (!
|
|
93
|
-
if (data.length > 4 * 1024 * 1024) {
|
|
94
|
-
self.emit("error", new Error(`Compressed payload too large: ${data.length} bytes`)), ws?.terminate();
|
|
95
|
-
return;
|
|
96
|
-
}
|
|
89
|
+
if (!inflater) {
|
|
97
90
|
try {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
91
|
+
let text = data.toString(), payload = JSON.parse(text);
|
|
92
|
+
handlePayload(payload);
|
|
93
|
+
} catch (error) {
|
|
94
|
+
self.emit("error", error);
|
|
101
95
|
}
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
function onInflate(chunk) {
|
|
105
|
-
if (inflateBuffer = Buffer.concat([inflateBuffer, chunk]), inflateBuffer.length > 8 * 1024 * 1024) {
|
|
106
|
-
self.emit("error", new Error("Decompressed buffer overflow (8MB limit)")), ws?.terminate();
|
|
107
96
|
return;
|
|
108
97
|
}
|
|
109
|
-
let
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
98
|
+
let buffer;
|
|
99
|
+
Buffer.isBuffer(data) ? buffer = data : Array.isArray(data) ? buffer = Buffer.concat(data) : data instanceof ArrayBuffer ? buffer = Buffer.from(data) : buffer = Buffer.from(String(data));
|
|
100
|
+
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;
|
|
101
|
+
inflater.write(buffer, (writeError) => {
|
|
102
|
+
if (writeError) {
|
|
103
|
+
self.emit("error", writeError);
|
|
114
104
|
return;
|
|
115
105
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
106
|
+
hasSyncFlush && inflater?.flush(constants.Z_SYNC_FLUSH);
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
function onInflate(chunk) {
|
|
110
|
+
decompressBuffer.push(chunk);
|
|
111
|
+
try {
|
|
112
|
+
let fullBuffer = Buffer.concat(decompressBuffer), text = textDecoder.decode(fullBuffer), payload = JSON.parse(text);
|
|
113
|
+
handlePayload(payload), decompressBuffer = [];
|
|
114
|
+
} catch (error) {
|
|
115
|
+
if (error instanceof SyntaxError) {
|
|
116
|
+
let fullBuffer = Buffer.concat(decompressBuffer), text = textDecoder.decode(fullBuffer);
|
|
117
|
+
if (text.includes("{") && !isValidJSON(text))
|
|
126
118
|
return;
|
|
127
|
-
|
|
128
|
-
inflateBuffer = inflateBuffer.subarray(start);
|
|
129
|
-
return;
|
|
119
|
+
self.emit("error", error), decompressBuffer = [];
|
|
130
120
|
}
|
|
131
|
-
start = newlinePos + 1;
|
|
132
121
|
}
|
|
133
|
-
|
|
122
|
+
}
|
|
123
|
+
function isValidJSON(str) {
|
|
124
|
+
try {
|
|
125
|
+
return JSON.parse(str), !0;
|
|
126
|
+
} catch {
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
134
129
|
}
|
|
135
130
|
function onClose(code) {
|
|
136
131
|
if (cleanup(), state = ShardState.Disconnected, self.emit("disconnect", code), strategy === ShardStrategy.Shutdown) {
|