sonic-ws 1.0.0-rc.7 → 1.0.0

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 (40) hide show
  1. package/README.md +19 -11
  2. package/dist/index.js +1 -1
  3. package/dist/version.d.ts +5 -3
  4. package/dist/version.js +8 -5
  5. package/dist/ws/Connection.d.ts +19 -0
  6. package/dist/ws/Connection.js +17 -0
  7. package/dist/ws/client/core/ClientCore.d.ts +27 -9
  8. package/dist/ws/client/core/ClientCore.js +162 -64
  9. package/dist/ws/client/node/ClientNode.js +2 -2
  10. package/dist/ws/packets/PacketProcessors.d.ts +11 -6
  11. package/dist/ws/packets/PacketProcessors.js +233 -175
  12. package/dist/ws/packets/PacketType.d.ts +30 -16
  13. package/dist/ws/packets/PacketType.js +31 -17
  14. package/dist/ws/packets/Packets.d.ts +15 -14
  15. package/dist/ws/packets/Packets.js +117 -99
  16. package/dist/ws/server/SonicWSConnection.d.ts +30 -8
  17. package/dist/ws/server/SonicWSConnection.js +67 -25
  18. package/dist/ws/server/SonicWSServer.d.ts +22 -0
  19. package/dist/ws/server/SonicWSServer.js +50 -8
  20. package/dist/ws/util/ArrayUtil.js +1 -1
  21. package/dist/ws/util/BufferUtil.d.ts +4 -0
  22. package/dist/ws/util/BufferUtil.js +40 -0
  23. package/dist/ws/util/StringUtil.d.ts +6 -0
  24. package/dist/ws/util/StringUtil.js +66 -0
  25. package/dist/ws/util/enums/EnumHandler.d.ts +2 -3
  26. package/dist/ws/util/enums/EnumHandler.js +4 -7
  27. package/dist/ws/util/enums/EnumType.d.ts +3 -2
  28. package/dist/ws/util/enums/EnumType.js +21 -10
  29. package/dist/ws/util/packets/BatchHelper.d.ts +10 -6
  30. package/dist/ws/util/packets/BatchHelper.js +28 -16
  31. package/dist/ws/util/packets/CompressionUtil.d.ts +44 -0
  32. package/dist/ws/util/packets/CompressionUtil.js +321 -0
  33. package/dist/ws/util/packets/PacketHolder.d.ts +5 -10
  34. package/dist/ws/util/packets/PacketHolder.js +25 -13
  35. package/dist/ws/util/packets/PacketUtils.d.ts +11 -8
  36. package/dist/ws/util/packets/PacketUtils.js +31 -26
  37. package/dist/ws/util/packets/RateHandler.js +16 -1
  38. package/package.json +2 -2
  39. package/dist/ws/util/packets/CodePointUtil.d.ts +0 -22
  40. package/dist/ws/util/packets/CodePointUtil.js +0 -220
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  /*
3
- * Copyright 2025 Lily (cutelittlelily)
3
+ * Copyright 2025 Lily (liwybloc)
4
4
  *
5
5
  * Licensed under the Apache License, Version 2.0 (the "License");
6
6
  * you may not use this file except in compliance with the License.
@@ -59,16 +59,15 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
59
59
  Object.defineProperty(exports, "__esModule", { value: true });
60
60
  exports.SonicWSConnection = void 0;
61
61
  var WS = __importStar(require("ws"));
62
- var CodePointUtil_1 = require("../util/packets/CodePointUtil");
63
62
  var PacketUtils_1 = require("../util/packets/PacketUtils");
64
63
  var BatchHelper_1 = require("../util/packets/BatchHelper");
65
64
  var RateHandler_1 = require("../util/packets/RateHandler");
65
+ var BufferUtil_1 = require("../util/BufferUtil");
66
66
  var CLIENT_RATELIMIT_TAG = "C", SERVER_RATELIMIT_TAG = "S";
67
67
  var SonicWSConnection = /** @class */ (function () {
68
68
  function SonicWSConnection(socket, host, id, handshakePacket, clientRateLimit, serverRateLimit) {
69
69
  var _this = this;
70
70
  this.print = false;
71
- this.timers = [];
72
71
  this.messageLambda = function (data) { return _this.messageHandler(_this.parseData(data)); };
73
72
  this.handshakedMessageLambda = function (data) {
74
73
  var parsed = _this.parseData(data);
@@ -78,17 +77,19 @@ var SonicWSConnection = /** @class */ (function () {
78
77
  return _this.socket.close(4005);
79
78
  _this.messageHandler(parsed);
80
79
  };
80
+ this.enabledPackets = {};
81
81
  /** If the packet handshake has been completed; `wss.requireHandshake(packet)` */
82
82
  this.handshakeComplete = false;
83
+ this.timers = {};
83
84
  this.socket = socket;
84
85
  this.host = host;
85
86
  this.id = id;
86
- this.code = String.fromCharCode(id);
87
87
  this.handshakePacket = handshakePacket;
88
88
  this.listeners = {};
89
- for (var _i = 0, _a = Object.values(host.clientPackets.getTagMap()); _i < _a.length; _i++) {
89
+ for (var _i = 0, _a = host.clientPackets.getTags(); _i < _a.length; _i++) {
90
90
  var key = _a[_i];
91
91
  this.listeners[key] = [];
92
+ this.enabledPackets[key] = host.clientPackets.getPacket(key).defaultEnabled;
92
93
  }
93
94
  this.setInterval = this.setInterval.bind(this);
94
95
  this.batcher = new BatchHelper_1.BatchHelper();
@@ -108,29 +109,37 @@ var SonicWSConnection = /** @class */ (function () {
108
109
  this.socket.addEventListener('message', this.handshakeLambda);
109
110
  }
110
111
  this.socket.on('close', function () {
111
- _this.timers.forEach(clearTimeout);
112
+ Object.values(_this.timers).forEach(clearTimeout);
112
113
  });
113
114
  }
114
115
  SonicWSConnection.prototype.parseData = function (event) {
115
116
  if (this.rater.trigger(CLIENT_RATELIMIT_TAG))
116
117
  return null;
117
- var message = event.data.toString();
118
+ if (!(event.data instanceof Buffer))
119
+ return null;
120
+ var message = new Uint8Array(event.data);
118
121
  if (this.print)
119
- console.log("\u001B[31m\u2B07 \u001B[38;5;245m(".concat(this.id, ",").concat((0, CodePointUtil_1.getStringBytes)(message), ")\u001B[0m ").concat(this.hideNewLines(message)));
120
- if (message.length < 1) {
122
+ console.log("\u001B[31m\u2B07 \u001B[38;5;245m(".concat(this.id, ",").concat(message.byteLength, ")\u001B[0m ").concat(this.hideNewLines(message)));
123
+ if (message.byteLength < 1) {
121
124
  this.socket.close(4001);
122
125
  return null;
123
126
  }
124
127
  var key = message[0];
125
- var value = message.substring(1);
128
+ var value = message.slice(1);
126
129
  // not a key, bye bye
127
- if (!this.host.clientPackets.has(key)) {
130
+ if (!this.host.clientPackets.hasKey(key)) {
128
131
  this.socket.close(4002);
129
132
  return null;
130
133
  }
134
+ var tag = this.host.clientPackets.getTag(key);
135
+ // disabled, bye bye
136
+ if (!this.enabledPackets[tag]) {
137
+ this.socket.close(4006);
138
+ return null;
139
+ }
131
140
  if (this.rater.trigger("client" + key))
132
141
  return null;
133
- return [this.host.clientPackets.getTag(key), value];
142
+ return [tag, value];
134
143
  };
135
144
  SonicWSConnection.prototype.handshakeHandler = function (data) {
136
145
  var parsed = this.parseData(data);
@@ -175,7 +184,7 @@ var SonicWSConnection = /** @class */ (function () {
175
184
  }
176
185
  };
177
186
  SonicWSConnection.prototype.hideNewLines = function (str) {
178
- return str.split("\n").join("");
187
+ return Array.from((0, BufferUtil_1.as8String)(str)).map(function (x) { return x == "\n" ? "☺" : x; }).join("");
179
188
  };
180
189
  /** Sends raw data to the user; will likely fail validity checks if used externally */
181
190
  SonicWSConnection.prototype.raw_send = function (data) {
@@ -184,9 +193,23 @@ var SonicWSConnection = /** @class */ (function () {
184
193
  if (this.rater.trigger(SERVER_RATELIMIT_TAG))
185
194
  return;
186
195
  if (this.print)
187
- console.log("\u001B[32m\u2B06 \u001B[38;5;245m(".concat(this.id, ",").concat((0, CodePointUtil_1.getStringBytes)(data), ")\u001B[0m ").concat(this.hideNewLines(data)));
196
+ console.log("\u001B[32m\u2B06 \u001B[38;5;245m(".concat(this.id, ",").concat(data.byteLength, ")\u001B[0m ").concat(this.hideNewLines(data)));
188
197
  this.socket.send(data);
189
198
  };
199
+ /**
200
+ * Enables a packet for the client.
201
+ * @param tag The tag of the packet
202
+ */
203
+ SonicWSConnection.prototype.enablePacket = function (tag) {
204
+ this.enabledPackets[tag] = true;
205
+ };
206
+ /**
207
+ * Disables a packet for the client.
208
+ * @param tag The tag of the packet
209
+ */
210
+ SonicWSConnection.prototype.disablePacket = function (tag) {
211
+ this.enabledPackets[tag] = false;
212
+ };
190
213
  /**
191
214
  * Checks if the connection is closed
192
215
  * @returns If it's closed or not
@@ -207,8 +230,7 @@ var SonicWSConnection = /** @class */ (function () {
207
230
  * @param listener A function to listen for it
208
231
  */
209
232
  SonicWSConnection.prototype.on = function (tag, listener) {
210
- var code = this.host.clientPackets.getChar(tag);
211
- if (code == null)
233
+ if (!this.host.clientPackets.hasTag(tag))
212
234
  throw new Error("Tag \"".concat(tag, "\" has not been created!"));
213
235
  if (!this.listeners[tag])
214
236
  this.listeners[tag] = [];
@@ -218,10 +240,10 @@ var SonicWSConnection = /** @class */ (function () {
218
240
  * For internal use.
219
241
  */
220
242
  SonicWSConnection.prototype.send_processed = function (code, data, packet) {
221
- if (this.rater.trigger("client" + code))
243
+ if (this.rater.trigger("server" + code))
222
244
  return;
223
245
  if (packet.dataBatching == 0)
224
- this.raw_send(code + data);
246
+ this.raw_send((0, BufferUtil_1.toPacketBuffer)(code, data));
225
247
  else
226
248
  this.batcher.batchPacket(code, data);
227
249
  };
@@ -270,32 +292,52 @@ var SonicWSConnection = /** @class */ (function () {
270
292
  this.print = !this.print;
271
293
  };
272
294
  /**
273
- * Closes the socket
295
+ * Closes the connection
274
296
  */
275
- SonicWSConnection.prototype.close = function (code) {
276
- if (code === void 0) { code = 1000; }
277
- this.socket.close(code);
297
+ SonicWSConnection.prototype.close = function (code, reason) {
298
+ this.socket.close(code, reason);
278
299
  };
279
300
  /**
280
301
  * Sets a timeout that will automatically end when the socket closes
281
302
  * @param call The function to call
282
303
  * @param time The time between now and the call (ms)
304
+ * @returns The timeout id to be used with socket.clearInterval(id)
283
305
  */
284
306
  SonicWSConnection.prototype.setTimeout = function (call, time) {
285
- var timeout = setTimeout(call, time);
286
- this.timers.push(timeout);
307
+ var _this = this;
308
+ var timeout = setTimeout(function () {
309
+ call();
310
+ _this.clearTimeout(timeout);
311
+ }, time);
312
+ this.timers[timeout] = timeout;
287
313
  return timeout;
288
314
  };
289
315
  /**
290
316
  * Sets an interval that will automatically end when the socket closes
291
317
  * @param call The function to call
292
318
  * @param time The time between calls (ms)
319
+ * @returns The interval id to be used with socket.clearInterval(id)
293
320
  */
294
321
  SonicWSConnection.prototype.setInterval = function (call, time) {
295
322
  var interval = setInterval(call, time);
296
- this.timers.push(interval);
323
+ this.timers[interval] = interval;
297
324
  return interval;
298
325
  };
326
+ /**
327
+ * Clears a timeout
328
+ * @param id The timeout id
329
+ */
330
+ SonicWSConnection.prototype.clearTimeout = function (id) {
331
+ clearTimeout(id);
332
+ delete this.timers[id];
333
+ };
334
+ /**
335
+ * Clears an interval
336
+ * @param id The interval id
337
+ */
338
+ SonicWSConnection.prototype.clearInterval = function (id) {
339
+ this.clearTimeout(id);
340
+ };
299
341
  return SonicWSConnection;
300
342
  }());
301
343
  exports.SonicWSConnection = SonicWSConnection;
@@ -25,6 +25,8 @@ export declare class SonicWSServer {
25
25
  private clientRateLimit;
26
26
  private serverRateLimit;
27
27
  private handshakePacket;
28
+ private maxConnections;
29
+ private queueTime;
28
30
  /**
29
31
  * Initializes and hosts a websocket with sonic protocol
30
32
  * Rate limits can be set with wss.setClientRateLimit(x) and wss.setServerRateLimit(x); it is defaulted at 500/second per both
@@ -63,6 +65,16 @@ export declare class SonicWSServer {
63
65
  * @param limit Amount of packets the server can send every second, or 0 for infinite
64
66
  */
65
67
  setServerRateLimit(limit: number): void;
68
+ /**
69
+ * Enables a packet for all current & new clients.
70
+ * @param tag The tag of the packet
71
+ */
72
+ enablePacket(tag: string): void;
73
+ /**
74
+ * Disables a packet for all current & new clients.
75
+ * @param tag The tag of the packet
76
+ */
77
+ disablePacket(tag: string): void;
66
78
  /**
67
79
  * Listens for whenever a client connects
68
80
  * @param runner Called when ready
@@ -91,6 +103,16 @@ export declare class SonicWSServer {
91
103
  * @param values The values to send
92
104
  */
93
105
  broadcast(tag: string, ...values: any): void;
106
+ /**
107
+ * Sets the maximum amount of users that can be connected; will tell about wss.setQueueTime(time). Defaults to unlimited
108
+ * @param amount The amount of users that can be connected
109
+ */
110
+ setMaxConnections(amount: number): void;
111
+ /**
112
+ * Sets the time in milliseconds for queued users to attempt to retry; applies for wss.setMaxConnections(amt). Defaults to 10,000ms
113
+ * @param time The requested reconnect time
114
+ */
115
+ setQueueTimeMs(time: number): void;
94
116
  /**
95
117
  * @returns All users connected to the socket
96
118
  */
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  /*
3
- * Copyright 2025 Lily (cutelittlelily)
3
+ * Copyright 2025 Lily (liwybloc)
4
4
  *
5
5
  * Licensed under the Apache License, Version 2.0 (the "License");
6
6
  * you may not use this file except in compliance with the License.
@@ -65,7 +65,7 @@ var node_fetch_1 = __importDefault(require("node-fetch"));
65
65
  var WS = __importStar(require("ws"));
66
66
  var SonicWSConnection_1 = require("./SonicWSConnection");
67
67
  var PacketHolder_1 = require("../util/packets/PacketHolder");
68
- var CodePointUtil_1 = require("../util/packets/CodePointUtil");
68
+ var CompressionUtil_1 = require("../util/packets/CompressionUtil");
69
69
  var version_1 = require("../../version");
70
70
  var PacketUtils_1 = require("../util/packets/PacketUtils");
71
71
  var SonicWSServer = /** @class */ (function () {
@@ -84,17 +84,25 @@ var SonicWSServer = /** @class */ (function () {
84
84
  this.clientRateLimit = 500;
85
85
  this.serverRateLimit = 500;
86
86
  this.handshakePacket = null;
87
+ this.maxConnections = 0;
88
+ this.queueTime = 10000;
87
89
  var _a = settings.clientPackets, clientPackets = _a === void 0 ? [] : _a, _b = settings.serverPackets, serverPackets = _b === void 0 ? [] : _b, _c = settings.websocketOptions, websocketOptions = _c === void 0 ? {} : _c;
88
90
  this.wss = new WS.WebSocketServer(websocketOptions);
89
91
  this.clientPackets = new PacketHolder_1.PacketHolder(clientPackets);
90
92
  this.serverPackets = new PacketHolder_1.PacketHolder(serverPackets);
91
93
  var s_clientPackets = this.clientPackets.serialize();
92
94
  var s_serverPackets = this.serverPackets.serialize();
93
- var keyData = "SWS" + version_1.VERSION_CHAR + s_clientPackets + CodePointUtil_1.NULL + s_serverPackets;
95
+ var keyData = __spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], version_1.SERVER_SUFFIX_NUMS, true), [version_1.VERSION], false), (0, CompressionUtil_1.convertVarInt)(s_clientPackets.length, false), true), s_clientPackets, true), s_serverPackets, true);
96
+ var retryData = new Uint8Array(version_1.SERVER_SUFFIX_NUMS);
94
97
  this.wss.on('connection', function (socket) {
98
+ if (_this.maxConnections != 0 && _this.connections.length >= _this.maxConnections) {
99
+ socket.send(retryData);
100
+ socket.close(1013);
101
+ return;
102
+ }
95
103
  var sonicConnection = new SonicWSConnection_1.SonicWSConnection(socket, _this, _this.generateSocketID(), _this.handshakePacket, _this.clientRateLimit, _this.serverRateLimit);
96
104
  // send tags to the client so it doesn't have to hard code them in
97
- socket.send(keyData + CodePointUtil_1.NULL + String.fromCharCode(_this.clientRateLimit) + sonicConnection.code);
105
+ socket.send(new Uint8Array(__spreadArray(__spreadArray([], keyData, true), [sonicConnection.id], false)));
98
106
  _this.connections.push(sonicConnection);
99
107
  _this.connectionMap[sonicConnection.id] = sonicConnection;
100
108
  _this.connectListeners.forEach(function (l) { return l(sonicConnection); });
@@ -155,9 +163,9 @@ var SonicWSServer = /** @class */ (function () {
155
163
  */
156
164
  SonicWSServer.prototype.setClientRateLimit = function (limit) {
157
165
  // so that i can store limits in 1 packet
158
- if (limit > CodePointUtil_1.MAX_C) {
166
+ if (limit > CompressionUtil_1.MAX_BYTE) {
159
167
  limit = 0;
160
- console.warn("A rate limit above ".concat(CodePointUtil_1.MAX_C, " is considered infinite."));
168
+ console.warn("A rate limit above ".concat(CompressionUtil_1.MAX_BYTE, " is considered infinite."));
161
169
  }
162
170
  this.clientRateLimit = limit;
163
171
  };
@@ -167,12 +175,28 @@ var SonicWSServer = /** @class */ (function () {
167
175
  */
168
176
  SonicWSServer.prototype.setServerRateLimit = function (limit) {
169
177
  // so that i can store limits in 1 packet
170
- if (limit > CodePointUtil_1.MAX_C) {
178
+ if (limit > CompressionUtil_1.MAX_BYTE) {
171
179
  limit = 0;
172
- console.warn("A rate limit above ".concat(CodePointUtil_1.MAX_C, " is considered infinite."));
180
+ console.warn("A rate limit above ".concat(CompressionUtil_1.MAX_BYTE, " is considered infinite."));
173
181
  }
174
182
  this.serverRateLimit = limit;
175
183
  };
184
+ /**
185
+ * Enables a packet for all current & new clients.
186
+ * @param tag The tag of the packet
187
+ */
188
+ SonicWSServer.prototype.enablePacket = function (tag) {
189
+ this.clientPackets.getPacket(tag).defaultEnabled = true;
190
+ this.connections.forEach(function (socket) { return socket.enablePacket(tag); });
191
+ };
192
+ /**
193
+ * Disables a packet for all current & new clients.
194
+ * @param tag The tag of the packet
195
+ */
196
+ SonicWSServer.prototype.disablePacket = function (tag) {
197
+ this.clientPackets.getPacket(tag).defaultEnabled = false;
198
+ this.connections.forEach(function (socket) { return socket.disablePacket(tag); });
199
+ };
176
200
  /**
177
201
  * Listens for whenever a client connects
178
202
  * @param runner Called when ready
@@ -220,6 +244,24 @@ var SonicWSServer = /** @class */ (function () {
220
244
  }
221
245
  this.broadcastFiltered.apply(this, __spreadArray([tag, function () { return true; }], values, false));
222
246
  };
247
+ /**
248
+ * Sets the maximum amount of users that can be connected; will tell about wss.setQueueTime(time). Defaults to unlimited
249
+ * @param amount The amount of users that can be connected
250
+ */
251
+ SonicWSServer.prototype.setMaxConnections = function (amount) {
252
+ if (amount < 1)
253
+ throw new Error("Max connections must be at least 1: ".concat(amount));
254
+ this.maxConnections = amount;
255
+ };
256
+ /**
257
+ * Sets the time in milliseconds for queued users to attempt to retry; applies for wss.setMaxConnections(amt). Defaults to 10,000ms
258
+ * @param time The requested reconnect time
259
+ */
260
+ SonicWSServer.prototype.setQueueTimeMs = function (time) {
261
+ if (time < 1)
262
+ throw new Error("Queue time must be at least 1ms: ".concat(time, "ms"));
263
+ this.queueTime = time;
264
+ };
223
265
  /**
224
266
  * @returns All users connected to the socket
225
267
  */
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  /*
3
- * Copyright 2025 Lily (cutelittlelily)
3
+ * Copyright 2025 Lily (liwybloc)
4
4
  *
5
5
  * Licensed under the Apache License, Version 2.0 (the "License");
6
6
  * you may not use this file except in compliance with the License.
@@ -0,0 +1,4 @@
1
+ export declare function toPacketBuffer(code: number, data: number[]): Uint8Array;
2
+ export declare function splitBuffer(arr: Uint8Array, x: number): number[][];
3
+ export declare function as8String(data: Uint8Array): string;
4
+ export declare function as16String(data: Uint8Array): string;
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright 2025 Lily (liwybloc)
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.toPacketBuffer = toPacketBuffer;
19
+ exports.splitBuffer = splitBuffer;
20
+ exports.as8String = as8String;
21
+ exports.as16String = as16String;
22
+ var ArrayUtil_1 = require("./ArrayUtil");
23
+ var CompressionUtil_1 = require("./packets/CompressionUtil");
24
+ var StringUtil_1 = require("./StringUtil");
25
+ function toPacketBuffer(code, data) {
26
+ var buffer = new Uint8Array(1 + data.length);
27
+ buffer[0] = code;
28
+ buffer.set(data, 1);
29
+ return buffer;
30
+ }
31
+ function splitBuffer(arr, x) {
32
+ return (0, ArrayUtil_1.splitArray)(Array.from(arr), x);
33
+ }
34
+ function as8String(data) {
35
+ return (0, StringUtil_1.convertCharCodes)(Array.from(data));
36
+ }
37
+ function as16String(data) {
38
+ var codePoints = splitBuffer(data, 2).map(function (short) { return (0, CompressionUtil_1.fromShort)(short); });
39
+ return (0, StringUtil_1.convertCodePoints)(codePoints);
40
+ }
@@ -0,0 +1,6 @@
1
+ export declare const SURROGATE_MIN = 55296, SURROGATE_MAX = 57343, MAX_UTF8 = 65535, MAX_UTF16 = 1114111;
2
+ export declare function processCharCodes(text: string): number[];
3
+ export declare function convertCharCodes(codes: number[]): string;
4
+ export declare function splitCodePoint(codePoint: number): number[];
5
+ export declare function pairToPoint(highSurrogate: number, lowSurrogate: number): number;
6
+ export declare function convertCodePoints(codePoints: number[]): string;
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright 2025 Lily (liwybloc)
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.MAX_UTF16 = exports.MAX_UTF8 = exports.SURROGATE_MAX = exports.SURROGATE_MIN = void 0;
19
+ exports.processCharCodes = processCharCodes;
20
+ exports.convertCharCodes = convertCharCodes;
21
+ exports.splitCodePoint = splitCodePoint;
22
+ exports.pairToPoint = pairToPoint;
23
+ exports.convertCodePoints = convertCodePoints;
24
+ exports.SURROGATE_MIN = 0xD800, exports.SURROGATE_MAX = 0xDFFF, exports.MAX_UTF8 = 0xFFFF, exports.MAX_UTF16 = 0x10FFFF;
25
+ function processCharCodes(text) {
26
+ return Array.from(text, function (char) { return char.codePointAt(0); });
27
+ }
28
+ function convertCharCodes(codes) {
29
+ return String.fromCodePoint.apply(String, codes);
30
+ }
31
+ function isSurrogate(x) {
32
+ return x >= exports.SURROGATE_MIN && x <= exports.SURROGATE_MAX;
33
+ }
34
+ function splitCodePoint(codePoint) {
35
+ if (codePoint <= exports.MAX_UTF8) {
36
+ if (isSurrogate(codePoint))
37
+ throw new Error("Cannot send code point ".concat(codePoint, "; must be out of range ").concat(exports.SURROGATE_MIN, " and ").concat(exports.SURROGATE_MAX));
38
+ return [codePoint];
39
+ }
40
+ if (codePoint > exports.MAX_UTF16)
41
+ throw new Error("Cannot send code ".concat(codePoint));
42
+ codePoint -= 0x10000;
43
+ var highSurrogate = (codePoint >> 10) + 0xD800;
44
+ var lowSurrogate = (codePoint & 0x3FF) + 0xDC00;
45
+ return [highSurrogate, lowSurrogate];
46
+ }
47
+ function pairToPoint(highSurrogate, lowSurrogate) {
48
+ return (highSurrogate - 0xD800) * 0x400 + (lowSurrogate - 0xDC00) + 0x10000;
49
+ }
50
+ function convertCodePoints(codePoints) {
51
+ var result = [];
52
+ for (var i = 0; i < codePoints.length; i++) {
53
+ var x = codePoints[i];
54
+ if (isSurrogate(x)) {
55
+ if (i == codePoints.length - 1)
56
+ throw new Error("Terminated surrogate pair; index ".concat(i, " value ").concat(x));
57
+ var pair = codePoints[++i];
58
+ if (!isSurrogate(pair))
59
+ throw new Error("Terminated surrogate pair; index ".concat(i, " value ").concat(x, " next value ").concat(pair));
60
+ result.push(pairToPoint(x, pair));
61
+ continue;
62
+ }
63
+ result.push(x);
64
+ }
65
+ return convertCharCodes(result);
66
+ }
@@ -1,5 +1,5 @@
1
1
  import { EnumPackage } from "./EnumType";
2
- export declare const MAX_ENUM_SIZE = 128;
2
+ export declare const MAX_ENUM_SIZE = 255;
3
3
  export declare const ENUM_TAG_TO_KEY: Record<string, Record<any, number>>;
4
4
  export declare const ENUM_KEY_TO_TAG: Record<string, Record<number, any>>;
5
5
  /**
@@ -15,6 +15,5 @@ export declare function DefineEnum(tag: string, values: any[]): EnumPackage;
15
15
  * @param value The value to send
16
16
  * @returns A transmittable enum value
17
17
  */
18
- export declare function WrapEnum(tag: string, value: any): string;
18
+ export declare function WrapEnum(tag: string, value: any): number;
19
19
  export declare function fromIndex(tag: string, index: number): string;
20
- export declare function fromEncoded(tag: string, encoded: string): string;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  /*
3
- * Copyright 2025 Lily (cutelittlelily)
3
+ * Copyright 2025 Lily (liwybloc)
4
4
  *
5
5
  * Licensed under the Apache License, Version 2.0 (the "License");
6
6
  * you may not use this file except in compliance with the License.
@@ -19,9 +19,9 @@ exports.ENUM_KEY_TO_TAG = exports.ENUM_TAG_TO_KEY = exports.MAX_ENUM_SIZE = void
19
19
  exports.DefineEnum = DefineEnum;
20
20
  exports.WrapEnum = WrapEnum;
21
21
  exports.fromIndex = fromIndex;
22
- exports.fromEncoded = fromEncoded;
22
+ var CompressionUtil_1 = require("../packets/CompressionUtil");
23
23
  var EnumType_1 = require("./EnumType");
24
- exports.MAX_ENUM_SIZE = 0x80;
24
+ exports.MAX_ENUM_SIZE = CompressionUtil_1.MAX_BYTE;
25
25
  exports.ENUM_TAG_TO_KEY = {};
26
26
  exports.ENUM_KEY_TO_TAG = {};
27
27
  /**
@@ -46,11 +46,8 @@ function DefineEnum(tag, values) {
46
46
  function WrapEnum(tag, value) {
47
47
  if (!(value in exports.ENUM_TAG_TO_KEY[tag]))
48
48
  throw new Error("Value \"".concat(value, "\" does not exist in enum \"").concat(tag, "\""));
49
- return String.fromCharCode(exports.ENUM_TAG_TO_KEY[tag][value]);
49
+ return exports.ENUM_TAG_TO_KEY[tag][value];
50
50
  }
51
51
  function fromIndex(tag, index) {
52
52
  return exports.ENUM_KEY_TO_TAG[tag][index];
53
53
  }
54
- function fromEncoded(tag, encoded) {
55
- return fromIndex(tag, encoded.charCodeAt(0));
56
- }
@@ -1,7 +1,8 @@
1
1
  export declare const TYPE_CONVERSION_MAP: Record<number, (data: string) => string | number | boolean | undefined | null>;
2
+ export type EnumValue = string | number | boolean | undefined | null;
2
3
  export declare class EnumPackage {
3
4
  tag: string;
4
- values: any[];
5
+ values: EnumValue[];
5
6
  constructor(tag: string, values: any[]);
6
- serialize(): string;
7
+ serialize(): number[];
7
8
  }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  /*
3
- * Copyright 2025 Lily (cutelittlelily)
3
+ * Copyright 2025 Lily (liwybloc)
4
4
  *
5
5
  * Licensed under the Apache License, Version 2.0 (the "License");
6
6
  * you may not use this file except in compliance with the License.
@@ -14,8 +14,18 @@
14
14
  * See the License for the specific language governing permissions and
15
15
  * limitations under the License.
16
16
  */
17
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
18
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
19
+ if (ar || !(i in from)) {
20
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
21
+ ar[i] = from[i];
22
+ }
23
+ }
24
+ return to.concat(ar || Array.prototype.slice.call(from));
25
+ };
17
26
  Object.defineProperty(exports, "__esModule", { value: true });
18
27
  exports.EnumPackage = exports.TYPE_CONVERSION_MAP = void 0;
28
+ var StringUtil_1 = require("../StringUtil");
19
29
  var TYPE_INDEX_MAP = {
20
30
  'string': 0,
21
31
  'number': 1,
@@ -42,15 +52,16 @@ var EnumPackage = /** @class */ (function () {
42
52
  this.values = values;
43
53
  }
44
54
  EnumPackage.prototype.serialize = function () {
45
- return String.fromCharCode(this.tag.length + 1) + // tag length
46
- this.tag + // tag
47
- String.fromCharCode(this.values.length + 1) + // value count
48
- this.values.map(function (v) {
49
- return String.fromCharCode(String(v).length + 1) + // value length
50
- String.fromCharCode(getTypedIndex(v) + 1) + // value type
51
- v;
52
- } // value
53
- ).join("");
55
+ var tag = (0, StringUtil_1.processCharCodes)(this.tag);
56
+ return __spreadArray(__spreadArray(__spreadArray([
57
+ this.tag.length
58
+ ], tag, true), [
59
+ this.values.length
60
+ ], false), this.values.map(function (v) { return __spreadArray([
61
+ String(v).length, // value length
62
+ getTypedIndex(v)
63
+ ], (0, StringUtil_1.processCharCodes)(String(v)) // value
64
+ , true); }).flat(), true);
54
65
  };
55
66
  return EnumPackage;
56
67
  }());
@@ -1,11 +1,15 @@
1
- import { SonicWSCore } from "../../client/core/ClientCore";
1
+ import { Connection } from "../../Connection";
2
2
  import { Packet } from "../../packets/Packets";
3
3
  import { SonicWSConnection } from "../../server/SonicWSConnection";
4
4
  import { PacketHolder } from "./PacketHolder";
5
5
  export declare class BatchHelper {
6
- batchedData: Record<string, string[]>;
7
- registerSendPackets(packetHolder: PacketHolder, clazz: SonicWSCore | SonicWSConnection): void;
8
- initiateBatch(code: string, time: number, clazz: SonicWSCore | SonicWSConnection): void;
9
- batchPacket(code: string, data: string): void;
10
- static unravelBatch(packet: Packet, data: string, socket: SonicWSConnection | null): any[] | string;
6
+ private batchTimes;
7
+ private batchTimeouts;
8
+ private batchedData;
9
+ private conn;
10
+ registerSendPackets(packetHolder: PacketHolder, conn: Connection): void;
11
+ private initiateBatch;
12
+ private startBatch;
13
+ batchPacket(code: number, data: number[]): void;
14
+ static unravelBatch(packet: Packet, data: Uint8Array, socket: SonicWSConnection | null): any[] | string;
11
15
  }