@bakit/gateway 2.1.5 → 2.1.7
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 +17 -9
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -34,7 +34,7 @@ var ZLIB_FLUSH = Buffer.from([0, 0, 255, 255]), DEFAULT_SHARD_OPTIONS = {
|
|
|
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, zlibBuffer, sessionId, lastSequence, lastHeartbeatSent = -1, lastHeartbeatAcknowledged = -1, missedHeartbeats = 0, reconnectTimeout, heartbeatTimeout, heartbeatInterval, self = attachEventBus({
|
|
37
|
+
let resolvedOptions = { ...DEFAULT_SHARD_OPTIONS, ...options }, state = ShardState.Idle, strategy = ShardStrategy.Unknown, ws, resumeGatewayURL, inflater, inflateBuffer, zlibBuffer, sessionId, lastSequence, lastHeartbeatSent = -1, lastHeartbeatAcknowledged = -1, missedHeartbeats = 0, reconnectTimeout, heartbeatTimeout, heartbeatInterval, self = attachEventBus({
|
|
38
38
|
send,
|
|
39
39
|
connect,
|
|
40
40
|
disconnect,
|
|
@@ -60,21 +60,29 @@ function createShard(options) {
|
|
|
60
60
|
let { gateway } = resolvedOptions, baseURL = isResumable() && resumeGatewayURL ? resumeGatewayURL : gateway.baseURL, url = new URL(baseURL);
|
|
61
61
|
url.searchParams.set("v", gateway.version.toString()), url.searchParams.set("encoding", "json"), url.searchParams.set("compress", "zlib-stream"), ws = new WebSocket(url.toString(), {
|
|
62
62
|
perMessageDeflate: false
|
|
63
|
-
}), zlibBuffer = Buffer.alloc(0), inflater = createInflate({
|
|
63
|
+
}), inflateBuffer = Buffer.alloc(0), zlibBuffer = Buffer.alloc(0), inflater = createInflate({
|
|
64
64
|
flush: constants.Z_SYNC_FLUSH
|
|
65
65
|
}), inflater.on("data", (chunk) => {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
66
|
+
if (inflateBuffer) {
|
|
67
|
+
if (inflateBuffer = Buffer.concat([inflateBuffer, chunk]), inflateBuffer.length > 10 * 1024 * 1024) {
|
|
68
|
+
self.emit("error", new Error("Inflate buffer overflow")), ws?.terminate();
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
try {
|
|
72
|
+
let text = inflateBuffer.toString("utf8"), payload = JSON.parse(text);
|
|
73
|
+
inflateBuffer = Buffer.alloc(0), handlePayload(payload);
|
|
74
|
+
} catch (err) {
|
|
75
|
+
if (err instanceof SyntaxError)
|
|
76
|
+
return;
|
|
77
|
+
inflateBuffer = Buffer.alloc(0), self.emit("error", err);
|
|
78
|
+
}
|
|
71
79
|
}
|
|
72
80
|
}), inflater.on("error", (err) => {
|
|
73
81
|
self.emit("error", err), ws?.terminate();
|
|
74
82
|
}), ws.on("message", onMessage), ws.on("close", onClose), ws.on("error", (err) => self.emit("error", err));
|
|
75
83
|
}
|
|
76
84
|
function cleanup() {
|
|
77
|
-
heartbeatInterval && (clearInterval(heartbeatInterval), heartbeatInterval = void 0), heartbeatTimeout && (clearTimeout(heartbeatTimeout), heartbeatTimeout = void 0), reconnectTimeout && (clearTimeout(reconnectTimeout), reconnectTimeout = void 0), inflater && (inflater.destroy(), inflater = void 0),
|
|
85
|
+
heartbeatInterval && (clearInterval(heartbeatInterval), heartbeatInterval = void 0), heartbeatTimeout && (clearTimeout(heartbeatTimeout), heartbeatTimeout = void 0), reconnectTimeout && (clearTimeout(reconnectTimeout), reconnectTimeout = void 0), inflater && (inflater.destroy(), inflater = void 0), inflateBuffer = void 0, zlibBuffer = void 0, ws && (ws.readyState !== WebSocket.CLOSED && ws.terminate(), ws.removeAllListeners(), ws = void 0), missedHeartbeats = 0, lastHeartbeatSent = -1, lastHeartbeatAcknowledged = -1;
|
|
78
86
|
}
|
|
79
87
|
function connect() {
|
|
80
88
|
return new Promise((resolve) => {
|
|
@@ -98,7 +106,7 @@ function createShard(options) {
|
|
|
98
106
|
ws?.terminate();
|
|
99
107
|
return;
|
|
100
108
|
}
|
|
101
|
-
zlibBuffer = Buffer.concat([zlibBuffer, data]), zlibBuffer.subarray(zlibBuffer.length - 4).equals(ZLIB_FLUSH) && (inflater?.write(zlibBuffer), zlibBuffer = Buffer.alloc(0));
|
|
109
|
+
zlibBuffer = Buffer.concat([zlibBuffer, data]), !(zlibBuffer.length < 4 || !zlibBuffer.subarray(zlibBuffer.length - 4).equals(ZLIB_FLUSH)) && (inflater?.write(zlibBuffer), zlibBuffer = Buffer.alloc(0));
|
|
102
110
|
}
|
|
103
111
|
}
|
|
104
112
|
function onClose(code) {
|