@bakit/gateway 2.1.4 → 2.1.6
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 +16 -10
- 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,27 @@ 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
|
+
err instanceof SyntaxError || (inflateBuffer = Buffer.alloc(0)), self.emit("error", err);
|
|
76
|
+
}
|
|
71
77
|
}
|
|
72
78
|
}), inflater.on("error", (err) => {
|
|
73
79
|
self.emit("error", err), ws?.terminate();
|
|
74
80
|
}), ws.on("message", onMessage), ws.on("close", onClose), ws.on("error", (err) => self.emit("error", err));
|
|
75
81
|
}
|
|
76
82
|
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),
|
|
83
|
+
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
84
|
}
|
|
79
85
|
function connect() {
|
|
80
86
|
return new Promise((resolve) => {
|
|
@@ -98,7 +104,7 @@ function createShard(options) {
|
|
|
98
104
|
ws?.terminate();
|
|
99
105
|
return;
|
|
100
106
|
}
|
|
101
|
-
zlibBuffer = Buffer.concat([zlibBuffer, data]), zlibBuffer.subarray(zlibBuffer.length - 4).equals(ZLIB_FLUSH) && (inflater?.write(zlibBuffer), zlibBuffer = Buffer.alloc(0));
|
|
107
|
+
zlibBuffer = Buffer.concat([zlibBuffer, data]), !(zlibBuffer.length < 4 || !zlibBuffer.subarray(zlibBuffer.length - 4).equals(ZLIB_FLUSH)) && (inflater?.write(zlibBuffer), zlibBuffer = Buffer.alloc(0));
|
|
102
108
|
}
|
|
103
109
|
}
|
|
104
110
|
function onClose(code) {
|
|
@@ -298,7 +304,7 @@ function bindWorkerToProcess(worker) {
|
|
|
298
304
|
});
|
|
299
305
|
});
|
|
300
306
|
function send(type, payload) {
|
|
301
|
-
process.send
|
|
307
|
+
process.send && process.connected && process.send({ type, ...payload });
|
|
302
308
|
}
|
|
303
309
|
process.on("SIGINT", () => {
|
|
304
310
|
}), process.on("SIGTERM", async () => {
|