sonic-ws 1.3.0 → 1.3.2
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/README.md +50 -44
- package/dist/index.d.ts +4 -0
- package/dist/version.d.ts +5 -1
- package/dist/version.js +4 -23
- package/dist/ws/Connection.d.ts +52 -2
- package/dist/ws/Connection.js +4 -48
- package/dist/ws/PacketProcessor.d.ts +11 -3
- package/dist/ws/PacketProcessor.js +4 -17
- package/dist/ws/client/core/ClientCore.d.ts +17 -35
- package/dist/ws/client/core/ClientCore.js +4 -328
- package/dist/ws/client/node/ClientNode.d.ts +6 -2
- package/dist/ws/client/node/ClientNode.js +4 -34
- package/dist/ws/debug/DebugServer.d.ts +11 -0
- package/dist/ws/debug/DebugServer.js +6 -0
- package/dist/ws/packets/PacketProcessors.d.ts +6 -2
- package/dist/ws/packets/PacketProcessors.js +4 -314
- package/dist/ws/packets/PacketType.d.ts +4 -0
- package/dist/ws/packets/PacketType.js +4 -59
- package/dist/ws/packets/Packets.d.ts +12 -9
- package/dist/ws/packets/Packets.js +4 -313
- package/dist/ws/server/SonicWSConnection.d.ts +8 -35
- package/dist/ws/server/SonicWSConnection.js +4 -421
- package/dist/ws/server/SonicWSServer.d.ts +11 -10
- package/dist/ws/server/SonicWSServer.js +3 -906
- package/dist/ws/util/BufferUtil.d.ts +7 -2
- package/dist/ws/util/BufferUtil.js +4 -43
- package/dist/ws/util/StringUtil.d.ts +7 -4
- package/dist/ws/util/StringUtil.js +4 -64
- package/dist/ws/util/enums/EnumHandler.d.ts +4 -0
- package/dist/ws/util/enums/EnumHandler.js +4 -58
- package/dist/ws/util/enums/EnumType.d.ts +4 -12
- package/dist/ws/util/enums/EnumType.js +4 -69
- package/dist/ws/util/packets/BatchHelper.d.ts +5 -15
- package/dist/ws/util/packets/BatchHelper.js +4 -77
- package/dist/ws/util/packets/CompressionUtil.d.ts +17 -26
- package/dist/ws/util/packets/CompressionUtil.js +4 -533
- package/dist/ws/util/packets/HashUtil.d.ts +4 -0
- package/dist/ws/util/packets/HashUtil.js +4 -120
- package/dist/ws/util/packets/JSONUtil.d.ts +6 -0
- package/dist/ws/util/packets/JSONUtil.js +6 -0
- package/dist/ws/util/packets/PacketHolder.d.ts +3 -62
- package/dist/ws/util/packets/PacketHolder.js +4 -123
- package/dist/ws/util/packets/PacketUtils.d.ts +4 -17
- package/dist/ws/util/packets/PacketUtils.js +4 -272
- package/dist/ws/util/packets/RateHandler.d.ts +5 -14
- package/dist/ws/util/packets/RateHandler.js +4 -63
- package/package.json +4 -2
- package/dist/ws/util/ArrayUtil.d.ts +0 -1
- package/dist/ws/util/ArrayUtil.js +0 -24
|
@@ -1,315 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
/*
|
|
1
|
+
/**
|
|
3
2
|
* Copyright 2026 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.
|
|
3
|
+
* Licensed under the Apache License, Version 2.0.
|
|
16
4
|
*/
|
|
17
|
-
|
|
18
|
-
exports.PacketSchema = exports.Packet =
|
|
19
|
-
const EnumHandler_1 = require("../util/enums/EnumHandler");
|
|
20
|
-
const EnumType_1 = require("../util/enums/EnumType");
|
|
21
|
-
const CompressionUtil_1 = require("../util/packets/CompressionUtil");
|
|
22
|
-
const PacketUtils_1 = require("../util/packets/PacketUtils");
|
|
23
|
-
const PacketProcessors_1 = require("./PacketProcessors");
|
|
24
|
-
const PacketType_1 = require("./PacketType");
|
|
25
|
-
const BufferUtil_1 = require("../util/BufferUtil");
|
|
26
|
-
const StringUtil_1 = require("../util/StringUtil");
|
|
27
|
-
class Packet {
|
|
28
|
-
defaultEnabled;
|
|
29
|
-
tag;
|
|
30
|
-
maxSize;
|
|
31
|
-
minSize;
|
|
32
|
-
type;
|
|
33
|
-
enumData;
|
|
34
|
-
dataMax;
|
|
35
|
-
dataMin;
|
|
36
|
-
dataBatching;
|
|
37
|
-
maxBatchSize;
|
|
38
|
-
dontSpread;
|
|
39
|
-
autoFlatten;
|
|
40
|
-
rateLimit;
|
|
41
|
-
async;
|
|
42
|
-
rereference;
|
|
43
|
-
gzipCompression;
|
|
44
|
-
object;
|
|
45
|
-
client;
|
|
46
|
-
receiveProcessor;
|
|
47
|
-
sendProcessor;
|
|
48
|
-
validator;
|
|
49
|
-
processReceive;
|
|
50
|
-
processSend;
|
|
51
|
-
validate;
|
|
52
|
-
customValidator;
|
|
53
|
-
lastReceived = {};
|
|
54
|
-
lastSent = {};
|
|
55
|
-
constructor(tag, schema, customValidator, enabled, client) {
|
|
56
|
-
this.tag = tag;
|
|
57
|
-
this.defaultEnabled = enabled;
|
|
58
|
-
this.client = client;
|
|
59
|
-
this.async = schema.async;
|
|
60
|
-
this.enumData = schema.enumData;
|
|
61
|
-
this.rateLimit = schema.rateLimit;
|
|
62
|
-
this.dontSpread = schema.dontSpread;
|
|
63
|
-
this.autoFlatten = schema.autoFlatten;
|
|
64
|
-
this.rereference = schema.rereference;
|
|
65
|
-
this.dataBatching = schema.dataBatching;
|
|
66
|
-
this.maxBatchSize = client ? Infinity : schema.maxBatchSize;
|
|
67
|
-
this.gzipCompression = schema.gzipCompression;
|
|
68
|
-
this.object = schema.object;
|
|
69
|
-
if (schema.testObject()) {
|
|
70
|
-
this.type = schema.type;
|
|
71
|
-
this.dataMax = schema.dataMax;
|
|
72
|
-
this.dataMin = schema.dataMin;
|
|
73
|
-
// @ts-expect-error
|
|
74
|
-
this.maxSize = this.type.length;
|
|
75
|
-
// @ts-expect-error
|
|
76
|
-
this.minSize = this.type.length;
|
|
77
|
-
// trst me bro..
|
|
78
|
-
this.receiveProcessor = (0, PacketProcessors_1.createObjReceiveProcessor)(this);
|
|
79
|
-
this.sendProcessor = (0, PacketProcessors_1.createObjSendProcessor)(this);
|
|
80
|
-
this.validator = (0, PacketProcessors_1.createObjValidator)(this);
|
|
81
|
-
}
|
|
82
|
-
else {
|
|
83
|
-
this.type = schema.type;
|
|
84
|
-
this.dataMax = schema.dataMax;
|
|
85
|
-
this.dataMin = schema.dataMin;
|
|
86
|
-
this.maxSize = this.dataMax;
|
|
87
|
-
this.minSize = this.dataMin;
|
|
88
|
-
// @ts-expect-error
|
|
89
|
-
this.receiveProcessor = (0, PacketProcessors_1.createReceiveProcessor)(this.type, this.enumData, this.dataMax);
|
|
90
|
-
// @ts-expect-error
|
|
91
|
-
this.sendProcessor = (0, PacketProcessors_1.createSendProcessor)(this.type, this.gzipCompression, this.rereference);
|
|
92
|
-
// @ts-expect-error
|
|
93
|
-
this.validator = (0, PacketProcessors_1.createValidator)(this.type, this.dataMax, this.dataMin, this, this.gzipCompression, this.rereference);
|
|
94
|
-
}
|
|
95
|
-
this.processReceive = (data, validationResult) => this.receiveProcessor(data, validationResult, 0);
|
|
96
|
-
this.processSend = async (data) => new Uint8Array(await this.sendProcessor(tag, data));
|
|
97
|
-
this.validate = (data) => this.validator(data, 0);
|
|
98
|
-
this.customValidator = customValidator;
|
|
99
|
-
}
|
|
100
|
-
async listen(value, socket) {
|
|
101
|
-
try {
|
|
102
|
-
const [dcData, validationResult] = await this.validate(value);
|
|
103
|
-
// holy shit i used === to fix another bug
|
|
104
|
-
if (!this.client && validationResult === false)
|
|
105
|
-
return "Invalid packet";
|
|
106
|
-
const processed = this.processReceive(dcData, validationResult);
|
|
107
|
-
const useableData = this.autoFlatten ? (0, PacketUtils_1.UnFlattenData)(processed) : processed;
|
|
108
|
-
if (this.customValidator != null) {
|
|
109
|
-
if (!this.dontSpread) {
|
|
110
|
-
if (!this.customValidator(socket, ...useableData))
|
|
111
|
-
return "Didn't pass custom validator";
|
|
112
|
-
}
|
|
113
|
-
else {
|
|
114
|
-
if (!this.customValidator(socket, useableData))
|
|
115
|
-
return "Didn't pass custom validator";
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
return [useableData, !this.dontSpread];
|
|
119
|
-
}
|
|
120
|
-
catch (err) {
|
|
121
|
-
console.error("There was an error processing the packet! This is probably my fault... report at https://github.com/liwybloc/sonic-ws", err);
|
|
122
|
-
return "Error: " + err;
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
serialize() {
|
|
126
|
-
// shared values for both
|
|
127
|
-
const sharedData = [
|
|
128
|
-
this.tag.length, ...(0, StringUtil_1.processCharCodes)(this.tag),
|
|
129
|
-
(0, CompressionUtil_1.compressBools)([this.dontSpread, this.async, this.object, this.autoFlatten, this.gzipCompression, this.rereference]),
|
|
130
|
-
this.dataBatching,
|
|
131
|
-
this.enumData.length, ...this.enumData.map(x => x.serialize()).flat(),
|
|
132
|
-
];
|
|
133
|
-
// single-value packet (not an object schema)
|
|
134
|
-
if (!this.object) {
|
|
135
|
-
return [
|
|
136
|
-
...sharedData, // shared
|
|
137
|
-
...(0, CompressionUtil_1.convertVarInt)(this.dataMax), // the data max
|
|
138
|
-
...(0, CompressionUtil_1.convertVarInt)(this.dataMin), // the data min
|
|
139
|
-
this.type,
|
|
140
|
-
];
|
|
141
|
-
}
|
|
142
|
-
// object packet
|
|
143
|
-
return [
|
|
144
|
-
...sharedData,
|
|
145
|
-
this.maxSize, // size
|
|
146
|
-
...this.dataMax.map(CompressionUtil_1.convertVarInt).flat(), // all data maxes, serialized
|
|
147
|
-
...this.dataMin.map(CompressionUtil_1.convertVarInt).flat(), // all data mins, serialized
|
|
148
|
-
...this.type, // all types
|
|
149
|
-
];
|
|
150
|
-
}
|
|
151
|
-
static readVarInts(data, offset, size) {
|
|
152
|
-
const res = [];
|
|
153
|
-
for (let i = 0; i < size; i++) {
|
|
154
|
-
const [off, varint] = (0, CompressionUtil_1.readVarInt)(data, offset);
|
|
155
|
-
offset = off;
|
|
156
|
-
res.push(varint);
|
|
157
|
-
}
|
|
158
|
-
return [res, offset];
|
|
159
|
-
}
|
|
160
|
-
static deserialize(data, offset, client) {
|
|
161
|
-
const beginningOffset = offset;
|
|
162
|
-
// read length, go up 1
|
|
163
|
-
const tagLength = data[offset++];
|
|
164
|
-
// read tag as it's up 1, and add offset
|
|
165
|
-
const tag = (0, BufferUtil_1.as8String)(data.slice(offset, offset += tagLength));
|
|
166
|
-
// then read dontSpread and async
|
|
167
|
-
const [dontSpread, async, isObject, autoFlatten, gzipCompression, rereference] = (0, CompressionUtil_1.decompressBools)(data[offset++]);
|
|
168
|
-
// read batching, up 1
|
|
169
|
-
const dataBatching = data[offset++];
|
|
170
|
-
// read enum length, up 1
|
|
171
|
-
const enumLength = data[offset++];
|
|
172
|
-
const enums = [];
|
|
173
|
-
for (let i = 0; i < enumLength; i++) {
|
|
174
|
-
// read tag length, go up 1
|
|
175
|
-
const enumTagLength = data[offset++];
|
|
176
|
-
// up 1 so read offset -> offset += tag length, to add tag length and skip over it
|
|
177
|
-
const enumTag = (0, BufferUtil_1.as8String)(data.slice(offset, offset += enumTagLength));
|
|
178
|
-
// read amount of values
|
|
179
|
-
const valueCount = data[offset++];
|
|
180
|
-
const values = [];
|
|
181
|
-
for (let j = 0; j < valueCount; j++) {
|
|
182
|
-
// read the length of the value, go up 1
|
|
183
|
-
const valueLength = data[offset++];
|
|
184
|
-
// then read the type of value, up 1
|
|
185
|
-
const valueType = data[offset++];
|
|
186
|
-
// now can just read the values, increase offset for later use
|
|
187
|
-
const value = (0, BufferUtil_1.as8String)(data.slice(offset, offset += valueLength));
|
|
188
|
-
// process it
|
|
189
|
-
values.push(EnumType_1.TYPE_CONVERSION_MAP[valueType](value));
|
|
190
|
-
}
|
|
191
|
-
// define the enum with the values
|
|
192
|
-
enums.push((0, EnumHandler_1.DefineEnum)(enumTag, values));
|
|
193
|
-
}
|
|
194
|
-
// objects
|
|
195
|
-
if (isObject) {
|
|
196
|
-
// read size
|
|
197
|
-
const size = data[offset++];
|
|
198
|
-
// read var ints for the datamaxes
|
|
199
|
-
const [dataMaxes, o1] = this.readVarInts(data, offset, size);
|
|
200
|
-
offset = o1;
|
|
201
|
-
// read var ints for the datamins
|
|
202
|
-
const [dataMins, o2] = this.readVarInts(data, offset, size);
|
|
203
|
-
offset = o2;
|
|
204
|
-
// get types, skip past size since there'll be size of these
|
|
205
|
-
const types = Array.from(data.slice(offset, offset += size));
|
|
206
|
-
// convert any enums into their indexed form for best bandwidth
|
|
207
|
-
let index = 0;
|
|
208
|
-
const finalTypes = types.map(x => x == PacketType_1.PacketType.ENUMS ? enums[index++] : x); // convert enums to their enum packages
|
|
209
|
-
// make schema
|
|
210
|
-
const schema = PacketSchema.object(finalTypes, dataMaxes, dataMins, dontSpread, autoFlatten, dataBatching, -1, -1, async, gzipCompression);
|
|
211
|
-
return [
|
|
212
|
-
new Packet(tag, schema, null, false, client),
|
|
213
|
-
// +1 to go next
|
|
214
|
-
(offset - beginningOffset),
|
|
215
|
-
];
|
|
216
|
-
}
|
|
217
|
-
// single packet
|
|
218
|
-
// read varint for datamax
|
|
219
|
-
const [o1, dataMax] = (0, CompressionUtil_1.readVarInt)(data, offset);
|
|
220
|
-
offset = o1;
|
|
221
|
-
// read varint for datamin
|
|
222
|
-
const [o2, dataMin] = (0, CompressionUtil_1.readVarInt)(data, offset);
|
|
223
|
-
offset = o2;
|
|
224
|
-
// read type
|
|
225
|
-
const type = data[offset++];
|
|
226
|
-
// do enum stuff
|
|
227
|
-
const finalType = type == PacketType_1.PacketType.ENUMS ? enums[0] : type; // convert enum to enum package
|
|
228
|
-
// make schema
|
|
229
|
-
const schema = PacketSchema.single(finalType, dataMax, dataMin, dontSpread, dataBatching, -1, -1, async, gzipCompression, rereference);
|
|
230
|
-
return [
|
|
231
|
-
new Packet(tag, schema, null, false, client),
|
|
232
|
-
(offset - beginningOffset),
|
|
233
|
-
];
|
|
234
|
-
}
|
|
235
|
-
static deserializeAll(data, client) {
|
|
236
|
-
const arr = [];
|
|
237
|
-
let offset = 0;
|
|
238
|
-
while (offset < data.length) {
|
|
239
|
-
const [packet, len] = this.deserialize(data, offset, client);
|
|
240
|
-
arr.push(packet);
|
|
241
|
-
offset += len;
|
|
242
|
-
}
|
|
243
|
-
return arr;
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
exports.Packet = Packet;
|
|
247
|
-
class PacketSchema {
|
|
248
|
-
type;
|
|
249
|
-
dataMax;
|
|
250
|
-
dataMin;
|
|
251
|
-
dataBatching = 0;
|
|
252
|
-
maxBatchSize = 10;
|
|
253
|
-
rateLimit = 0;
|
|
254
|
-
enumData = [];
|
|
255
|
-
dontSpread = false;
|
|
256
|
-
autoFlatten = false;
|
|
257
|
-
async = false;
|
|
258
|
-
rereference = false;
|
|
259
|
-
gzipCompression = false;
|
|
260
|
-
object;
|
|
261
|
-
constructor(object) {
|
|
262
|
-
this.object = object;
|
|
263
|
-
}
|
|
264
|
-
testObject() {
|
|
265
|
-
return this.object;
|
|
266
|
-
}
|
|
267
|
-
static single(type, dataMax, dataMin, dontSpread, dataBatching, maxBatchSize, rateLimit, async, gzipCompression, rereference) {
|
|
268
|
-
const schema = new PacketSchema(false);
|
|
269
|
-
if (typeof type == 'number') {
|
|
270
|
-
schema.type = type;
|
|
271
|
-
if (type == PacketType_1.PacketType.NONE)
|
|
272
|
-
dataMax = dataMin = 0; // remove garbage data issues
|
|
273
|
-
}
|
|
274
|
-
else {
|
|
275
|
-
schema.type = PacketType_1.PacketType.ENUMS;
|
|
276
|
-
schema.enumData = [type];
|
|
277
|
-
}
|
|
278
|
-
schema.async = async;
|
|
279
|
-
schema.dataMin = dataMin;
|
|
280
|
-
schema.dataMax = dataMax;
|
|
281
|
-
schema.rateLimit = rateLimit;
|
|
282
|
-
schema.dontSpread = dontSpread;
|
|
283
|
-
schema.rereference = rereference;
|
|
284
|
-
schema.dataBatching = dataBatching;
|
|
285
|
-
schema.maxBatchSize = maxBatchSize;
|
|
286
|
-
schema.gzipCompression = gzipCompression;
|
|
287
|
-
return schema;
|
|
288
|
-
}
|
|
289
|
-
static object(types, dataMaxes, dataMins, dontSpread, autoFlatten, dataBatching, maxBatchSize, rateLimit, async, gzipCompression) {
|
|
290
|
-
if (types.length != dataMaxes.length || types.length != dataMins.length)
|
|
291
|
-
throw new Error("There is an inbalance between the amount of types, data maxes, and data mins!");
|
|
292
|
-
const schema = new PacketSchema(true);
|
|
293
|
-
schema.type = [];
|
|
294
|
-
types.forEach(type => {
|
|
295
|
-
if (typeof type == 'number') {
|
|
296
|
-
schema.type.push(type);
|
|
297
|
-
}
|
|
298
|
-
else {
|
|
299
|
-
schema.type.push(PacketType_1.PacketType.ENUMS);
|
|
300
|
-
schema.enumData.push(type);
|
|
301
|
-
}
|
|
302
|
-
});
|
|
303
|
-
schema.async = async;
|
|
304
|
-
schema.dataMin = dataMins;
|
|
305
|
-
schema.dataMax = dataMaxes;
|
|
306
|
-
schema.rateLimit = rateLimit;
|
|
307
|
-
schema.dontSpread = dontSpread;
|
|
308
|
-
schema.autoFlatten = autoFlatten;
|
|
309
|
-
schema.dataBatching = dataBatching;
|
|
310
|
-
schema.maxBatchSize = maxBatchSize;
|
|
311
|
-
schema.gzipCompression = gzipCompression;
|
|
312
|
-
return schema;
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
exports.PacketSchema = PacketSchema;
|
|
5
|
+
|
|
6
|
+
Object.defineProperty(exports,"__esModule",{value:!0}),exports.PacketSchema=exports.Packet=void 0;const t=require("../util/enums/EnumHandler"),e=require("../util/enums/EnumType"),a=require("../util/packets/CompressionUtil"),s=require("../util/packets/PacketUtils"),i=require("./PacketProcessors"),r=require("./PacketType"),n=require("../util/StringUtil");class o{defaultEnabled;tag;maxSize;minSize;type;enumData;dataMax;dataMin;dataBatching;maxBatchSize;dontSpread;autoFlatten;rateLimit;async;rereference;gzipCompression;object;client;receiveProcessor;sendProcessor;validator;processReceive;processSend;validate;customValidator;lastReceived={};lastSent={};constructor(t,e,a,s,n){if(this.tag=t,this.defaultEnabled=s,this.client=n,this.async=e.async,this.enumData=e.enumData,this.rateLimit=e.rateLimit,this.dontSpread=e.dontSpread,this.autoFlatten=e.autoFlatten,this.rereference=e.rereference,this.dataBatching=e.dataBatching,this.maxBatchSize=n?1/0:e.maxBatchSize,this.gzipCompression=e.gzipCompression,this.object=e.object,this.type=e.type,this.dataMax=e.dataMax,this.dataMin=e.dataMin,e.testObject(this)){this.maxSize=this.minSize=this.type.length;for(let t=0;t<this.type.length;t++)this.type[t]==r.PacketType.NONE&&(this.dataMax[t]=this.dataMin[t]=0);this.receiveProcessor=(0,i.createObjReceiveProcessor)(this),this.sendProcessor=(0,i.createObjSendProcessor)(this),this.validator=(0,i.createObjValidator)(this)}else this.maxSize=this.dataMax,this.minSize=this.dataMin,this.type==r.PacketType.NONE&&(this.dataMax=this.dataMin=0),this.receiveProcessor=(0,i.createReceiveProcessor)(this.type,this.enumData,this.dataMax),this.sendProcessor=(0,i.createSendProcessor)(this.type,this.gzipCompression,0!=this.dataBatching),this.validator=(0,i.createValidator)(this.type,this.dataMax,this.dataMin,this,this.gzipCompression);this.processReceive=(t,e)=>this.receiveProcessor(t,e,0),this.processSend=async e=>new Uint8Array(await this.sendProcessor(t,e)),this.validate=t=>this.validator(t,0),this.customValidator=a}async listen(t,e){try{const[a,i]=await this.validate(t);if(!this.client&&!1===i)return"Invalid packet";const r=this.processReceive(a,i),n=this.autoFlatten?(0,s.UnFlattenData)(r):r;if(null!=this.customValidator)if(this.dontSpread){if(!this.customValidator(e,n))return"Didn't pass custom validator"}else if(!this.customValidator(e,...n))return"Didn't pass custom validator";return[n,!this.dontSpread]}catch(t){return console.error("There was an error processing the packet! This is probably my fault... report at https://github.com/liwybloc/sonic-ws",t),"Error: "+t}}serialize(){const t=[this.tag.length,...(0,n.processCharCodes)(this.tag),(0,a.compressBools)([this.dontSpread,this.async,this.object,this.autoFlatten,this.gzipCompression,this.rereference]),this.dataBatching,this.enumData.length,...this.enumData.map(t=>t.serialize()).flat()];return this.object?[...t,this.maxSize,...this.dataMax.map(a.convertVarInt).flat(),...this.dataMin.map(a.convertVarInt).flat(),...this.type]:[...t,...(0,a.convertVarInt)(this.dataMax),...(0,a.convertVarInt)(this.dataMin),this.type]}static readVarInts(t,e,s){const i=[];for(let r=0;r<s;r++){const[s,r]=(0,a.readVarInt)(t,e);e=s,i.push(r)}return[i,e]}static deserialize(s,i,c){const d=i,l=s[i++],p=(0,n.as8String)(s.slice(i,i+=l)),[u,m,y,S,g,P]=(0,a.decompressBools)(s[i++]),f=s[i++],M=s[i++],v=[];for(let a=0;a<M;a++){const a=s[i++],r=(0,n.as8String)(s.slice(i,i+=a)),o=s[i++],c=[];for(let t=0;t<o;t++){const t=s[i++],a=s[i++],r=(0,n.as8String)(s.slice(i,i+=t));c.push(e.TYPE_CONVERSION_MAP[a](r))}v.push((0,t.DefineEnum)(r,c))}if(y){const t=s[i++],[e,a]=this.readVarInts(s,i,t);i=a;const[n,l]=this.readVarInts(s,i,t);i=l;const y=Array.from(s.slice(i,i+=t));let P=0;const M=y.map(t=>t==r.PacketType.ENUMS?v[P++]:t),x=new h(!0,M,m,n,e,-1,u,S,!1,f,-1,g);return[new o(p,x,null,!1,c),i-d]}const[x,z]=(0,a.readVarInt)(s,i);i=x;const[b,V]=(0,a.readVarInt)(s,i);i=b;const k=s[i++],j=k==r.PacketType.ENUMS?v[0]:k,B=new h(!1,j,m,V,z,-1,u,!1,P,f,-1,g);return[new o(p,B,null,!1,c),i-d]}static deserializeAll(t,e){const a=[];let s=0;for(;s<t.length;){const[i,r]=this.deserialize(t,s,e);a.push(i),s+=r}return a}}exports.Packet=o;const c=(t,a)=>t instanceof e.EnumPackage?(a.push(t),r.PacketType.ENUMS):t;class h{type;enumData=[];dataMax;dataMin;dataBatching;maxBatchSize;rateLimit;dontSpread=!1;autoFlatten=!1;async=!1;rereference=!1;gzipCompression=!1;object;constructor(t,e,a,s,i,r,n,o,h,d,l,p){this.object=t,this.async=a,this.dataMin=s,this.dataMax=i,this.rateLimit=r,this.dontSpread=n,this.autoFlatten=o,this.rereference=h,this.dataBatching=d,this.maxBatchSize=l,this.gzipCompression=p,this.type=t?e.map(t=>c(t,this.enumData)):c(e,this.enumData)}testObject(t){return this.object}}exports.PacketSchema=h;
|
|
@@ -1,30 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 Lily (liwybloc)
|
|
3
|
+
* Licensed under the Apache License, Version 2.0.
|
|
4
|
+
*/
|
|
1
5
|
import * as WS from 'ws';
|
|
2
|
-
import { SonicWSServer } from
|
|
3
|
-
import { Packet } from
|
|
4
|
-
import { Connection } from
|
|
5
|
-
|
|
6
|
-
export declare class SonicWSConnection implements Connection {
|
|
7
|
-
/** Raw 'ws' library socket */
|
|
8
|
-
socket: WS.WebSocket;
|
|
6
|
+
import { SonicWSServer } from "./SonicWSServer";
|
|
7
|
+
import { Packet } from "../packets/Packets";
|
|
8
|
+
import { Connection } from "../Connection";
|
|
9
|
+
export declare class SonicWSConnection extends Connection<WS.WebSocket, Buffer> {
|
|
9
10
|
private host;
|
|
10
|
-
private listeners;
|
|
11
11
|
private print;
|
|
12
|
-
private name;
|
|
13
12
|
private handshakePacket;
|
|
14
13
|
private handshakeLambda;
|
|
15
14
|
private messageLambda;
|
|
16
15
|
private handshakedMessageLambda;
|
|
17
|
-
private batcher;
|
|
18
16
|
private rater;
|
|
19
17
|
private enabledPackets;
|
|
20
18
|
/** If the packet handshake has been completed; `wss.requireHandshake(packet)` */
|
|
21
19
|
handshakeComplete: boolean;
|
|
22
|
-
/** The index of the connection; unique for all connected, recycles old disconnected ids. Should be safe for INTS_C unless you have more than 27,647 connected at once. */
|
|
23
|
-
id: number;
|
|
24
|
-
_timers: Record<number, [number, (closed: boolean) => void, boolean]>;
|
|
25
20
|
private asyncMap;
|
|
26
21
|
private asyncData;
|
|
27
|
-
private closed;
|
|
28
22
|
constructor(socket: WS.WebSocket, host: SonicWSServer, id: number, handshakePacket: string | null, clientRateLimit: number, serverRateLimit: number);
|
|
29
23
|
private parseData;
|
|
30
24
|
private handshakeHandler;
|
|
@@ -34,9 +28,6 @@ export declare class SonicWSConnection implements Connection {
|
|
|
34
28
|
private packetQueue;
|
|
35
29
|
private listenPacket;
|
|
36
30
|
private messageHandler;
|
|
37
|
-
private middlewares;
|
|
38
|
-
addMiddleware(middleware: ConnectionMiddleware): void;
|
|
39
|
-
callMiddleware<K extends FuncKeys<ConnectionMiddleware> & keyof ConnectionMiddleware>(method: K, ...values: Parameters<NonNullable<Extract<ConnectionMiddleware[K], (...args: any[]) => any>>>): Promise<boolean>;
|
|
40
31
|
/**
|
|
41
32
|
* Enables a packet for the client.
|
|
42
33
|
* @param tag The tag of the packet
|
|
@@ -47,11 +38,6 @@ export declare class SonicWSConnection implements Connection {
|
|
|
47
38
|
* @param tag The tag of the packet
|
|
48
39
|
*/
|
|
49
40
|
disablePacket(tag: string): void;
|
|
50
|
-
/**
|
|
51
|
-
* Checks if the connection is closed
|
|
52
|
-
* @returns If it's closed or not
|
|
53
|
-
*/
|
|
54
|
-
isClosed(): boolean;
|
|
55
41
|
/**
|
|
56
42
|
* Listens for when the connection closes
|
|
57
43
|
* @param listener Called when it closes
|
|
@@ -91,23 +77,10 @@ export declare class SonicWSConnection implements Connection {
|
|
|
91
77
|
*/
|
|
92
78
|
togglePrint(): void;
|
|
93
79
|
raw_send(data: Uint8Array): void;
|
|
94
|
-
close(code?: number, reason?: string | Buffer): void;
|
|
95
|
-
setTimeout(call: () => void, time: number, callOnClose?: boolean): number;
|
|
96
|
-
setInterval(call: () => void, time: number, callOnClose?: boolean): number;
|
|
97
|
-
clearTimeout(id: number): void;
|
|
98
|
-
clearInterval(id: number): void;
|
|
99
80
|
/**
|
|
100
81
|
* Tags the socket with a key
|
|
101
82
|
* @param tag The tag to add
|
|
102
83
|
* @param replace If it should replace a previous tag; defaults to true. If using false, you can add multiple tags.
|
|
103
84
|
*/
|
|
104
85
|
tag(tag: string, replace?: boolean): void;
|
|
105
|
-
/**
|
|
106
|
-
* Sets the name of this connection for the debug menu; good for setting e.g. usernames on games
|
|
107
|
-
*/
|
|
108
|
-
setName(name: string): Promise<void>;
|
|
109
|
-
/**
|
|
110
|
-
* @returns Name of the socket, defaults to Socket [ID] unless set with setName()
|
|
111
|
-
*/
|
|
112
|
-
getName(): string;
|
|
113
86
|
}
|