@bakit/gateway 2.1.6 → 2.1.8

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.
Files changed (2) hide show
  1. package/dist/index.js +47 -22
  2. 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 ZLIB_FLUSH = Buffer.from([0, 0, 255, 255]), DEFAULT_SHARD_OPTIONS = {
10
+ var INCOMPLETE_JSON_REGEX = /unexpected end|unterminated|unexpected eof|end of json/i, DEFAULT_SHARD_OPTIONS = {
11
11
  gateway: {
12
12
  baseURL: "wss://gateway.discord.gg",
13
13
  version: 10
@@ -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, inflateBuffer, 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 = Buffer.alloc(0), sessionId, lastSequence, lastHeartbeatSent = -1, lastHeartbeatAcknowledged = -1, missedHeartbeats = 0, reconnectTimeout, heartbeatTimeout, heartbeatInterval, self = attachEventBus({
38
38
  send,
39
39
  connect,
40
40
  disconnect,
@@ -60,27 +60,17 @@ 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
- }), inflateBuffer = Buffer.alloc(0), zlibBuffer = Buffer.alloc(0), inflater = createInflate({
63
+ }), inflater = createInflate({
64
64
  flush: constants.Z_SYNC_FLUSH
65
- }), inflater.on("data", (chunk) => {
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
- }
77
- }
78
- }), inflater.on("error", (err) => {
65
+ }), inflater.on("data", onInflate), inflater.on("error", (err) => {
79
66
  self.emit("error", err), ws?.terminate();
80
67
  }), ws.on("message", onMessage), ws.on("close", onClose), ws.on("error", (err) => self.emit("error", err));
81
68
  }
69
+ function isJSONIncomplete(err) {
70
+ return err instanceof Error && INCOMPLETE_JSON_REGEX.test(err.message);
71
+ }
82
72
  function cleanup() {
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;
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), inflateBuffer = Buffer.alloc(0), ws && (ws.readyState !== WebSocket.CLOSED && ws.terminate(), ws.removeAllListeners(), ws = void 0), missedHeartbeats = 0, lastHeartbeatSent = -1, lastHeartbeatAcknowledged = -1;
84
74
  }
85
75
  function connect() {
86
76
  return new Promise((resolve) => {
@@ -99,13 +89,48 @@ function createShard(options) {
99
89
  });
100
90
  }
101
91
  function onMessage(data) {
102
- if (!(!(data instanceof Buffer) || !zlibBuffer)) {
103
- if (data.length > 8 * 1024 * 1024) {
104
- ws?.terminate();
92
+ if (!(!(data instanceof Buffer) || !inflater)) {
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
+ }
97
+ try {
98
+ inflater.write(data);
99
+ } catch (err) {
100
+ self.emit("error", new Error(`Zlib inflation error: ${err instanceof Error ? err.message : String(err)}`)), ws?.terminate();
101
+ }
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
+ return;
108
+ }
109
+ let start = 0;
110
+ for (; start < inflateBuffer.length; ) {
111
+ let newlinePos = inflateBuffer.indexOf(10, start);
112
+ if (newlinePos === -1) {
113
+ start > 0 && (inflateBuffer = inflateBuffer.subarray(start));
114
+ return;
115
+ }
116
+ let messageSlice = inflateBuffer.subarray(start, newlinePos);
117
+ try {
118
+ let payload = JSON.parse(messageSlice.toString("utf8"));
119
+ handlePayload(payload);
120
+ } catch (err) {
121
+ if (!isJSONIncomplete(err)) {
122
+ self.emit(
123
+ "error",
124
+ new Error(`Failed to parse gateway payload: ${err instanceof Error ? err.message : String(err)}`)
125
+ ), inflateBuffer = Buffer.alloc(0);
126
+ return;
127
+ }
128
+ inflateBuffer = inflateBuffer.subarray(start);
105
129
  return;
106
130
  }
107
- zlibBuffer = Buffer.concat([zlibBuffer, data]), !(zlibBuffer.length < 4 || !zlibBuffer.subarray(zlibBuffer.length - 4).equals(ZLIB_FLUSH)) && (inflater?.write(zlibBuffer), zlibBuffer = Buffer.alloc(0));
131
+ start = newlinePos + 1;
108
132
  }
133
+ inflateBuffer = Buffer.alloc(0);
109
134
  }
110
135
  function onClose(code) {
111
136
  if (cleanup(), state = ShardState.Disconnected, self.emit("disconnect", code), strategy === ShardStrategy.Shutdown) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bakit/gateway",
3
- "version": "2.1.6",
3
+ "version": "2.1.8",
4
4
  "description": "Gateway manager for bakit framework",
5
5
  "type": "module",
6
6
  "exports": {