sonic-ws 1.1.1 → 1.2.1-wtf
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 +9 -4
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/ws/client/core/ClientCore.d.ts +2 -1
- package/dist/ws/client/core/ClientCore.js +24 -15
- package/dist/ws/packets/PacketProcessors.d.ts +4 -10
- package/dist/ws/packets/PacketProcessors.js +173 -125
- package/dist/ws/packets/PacketType.d.ts +7 -1
- package/dist/ws/packets/PacketType.js +6 -0
- package/dist/ws/packets/Packets.d.ts +12 -6
- package/dist/ws/packets/Packets.js +41 -40
- package/dist/ws/server/SonicWSConnection.d.ts +1 -1
- package/dist/ws/server/SonicWSConnection.js +15 -5
- package/dist/ws/server/SonicWSServer.d.ts +4 -2
- package/dist/ws/server/SonicWSServer.js +20 -17
- package/dist/ws/util/packets/BatchHelper.d.ts +1 -1
- package/dist/ws/util/packets/BatchHelper.js +2 -2
- package/dist/ws/util/packets/CompressionUtil.d.ts +7 -0
- package/dist/ws/util/packets/CompressionUtil.js +202 -1
- package/dist/ws/util/packets/PacketHolder.d.ts +1 -1
- package/dist/ws/util/packets/PacketHolder.js +1 -1
- package/dist/ws/util/packets/PacketUtils.d.ts +10 -1
- package/dist/ws/util/packets/PacketUtils.js +35 -12
- package/package.json +2 -1
|
@@ -7,7 +7,6 @@ type ImpactType<T extends (PacketType | readonly PacketType[]), K> = T extends P
|
|
|
7
7
|
export declare class Packet<T extends (PacketType | readonly PacketType[])> {
|
|
8
8
|
defaultEnabled: boolean;
|
|
9
9
|
readonly tag: string;
|
|
10
|
-
readonly async: boolean;
|
|
11
10
|
readonly maxSize: number;
|
|
12
11
|
readonly minSize: number;
|
|
13
12
|
readonly type: T;
|
|
@@ -19,17 +18,22 @@ export declare class Packet<T extends (PacketType | readonly PacketType[])> {
|
|
|
19
18
|
readonly dontSpread: boolean;
|
|
20
19
|
readonly autoFlatten: boolean;
|
|
21
20
|
readonly rateLimit: number;
|
|
21
|
+
readonly async: boolean;
|
|
22
|
+
readonly rereference: boolean;
|
|
23
|
+
readonly gzipCompression: boolean;
|
|
22
24
|
readonly object: boolean;
|
|
23
25
|
readonly client: boolean;
|
|
24
26
|
private receiveProcessor;
|
|
25
27
|
private sendProcessor;
|
|
26
28
|
private validator;
|
|
27
29
|
processReceive: (data: Uint8Array, validationResult: any) => any;
|
|
28
|
-
processSend: (data: any[]) => Uint8Array
|
|
29
|
-
validate: (data: Uint8Array) => boolean
|
|
30
|
+
processSend: (data: any[]) => Promise<Uint8Array>;
|
|
31
|
+
validate: (data: Uint8Array) => Promise<[Uint8Array, boolean]>;
|
|
30
32
|
customValidator: ((socket: SonicWSConnection, ...values: any[]) => boolean) | null;
|
|
33
|
+
lastReceived: Record<number, any>;
|
|
34
|
+
lastSent: Record<number, any>;
|
|
31
35
|
constructor(tag: string, schema: PacketSchema<T>, customValidator: ValidatorFunction, enabled: boolean, client: boolean);
|
|
32
|
-
listen(value: Uint8Array, socket: SonicWSConnection | null): [processed: any, flatten: boolean] | string
|
|
36
|
+
listen(value: Uint8Array, socket: SonicWSConnection | null): Promise<[processed: any, flatten: boolean] | string>;
|
|
33
37
|
serialize(): number[];
|
|
34
38
|
private static readVarInts;
|
|
35
39
|
static deserialize(data: Uint8Array, offset: number, client: boolean): [packet: Packet<any>, offset: number];
|
|
@@ -46,10 +50,12 @@ export declare class PacketSchema<T extends (PacketType | readonly PacketType[])
|
|
|
46
50
|
dontSpread: boolean;
|
|
47
51
|
autoFlatten: boolean;
|
|
48
52
|
async: boolean;
|
|
53
|
+
rereference: boolean;
|
|
54
|
+
gzipCompression: boolean;
|
|
49
55
|
object: boolean;
|
|
50
56
|
constructor(object: boolean);
|
|
51
57
|
testObject(): this is PacketSchema<PacketType[]>;
|
|
52
|
-
static single<T extends PacketType | EnumPackage>(type: T, dataMax: number, dataMin: number, dontSpread: boolean, dataBatching: number, maxBatchSize: number, rateLimit: number, async: boolean): PacketSchema<ConvertType<T>>;
|
|
53
|
-
static object<T extends readonly (PacketType | EnumPackage)[]>(types: T, dataMaxes: number[], dataMins: number[], dontSpread: boolean, autoFlatten: boolean, dataBatching: number, maxBatchSize: number, rateLimit: number, async: boolean): PacketSchema<ConvertType<T[number]>[]>;
|
|
58
|
+
static single<T extends PacketType | EnumPackage>(type: T, dataMax: number, dataMin: number, dontSpread: boolean, dataBatching: number, maxBatchSize: number, rateLimit: number, async: boolean, gzipCompression: boolean, rereference: boolean): PacketSchema<ConvertType<T>>;
|
|
59
|
+
static object<T extends readonly (PacketType | EnumPackage)[]>(types: T, dataMaxes: number[], dataMins: number[], dontSpread: boolean, autoFlatten: boolean, dataBatching: number, maxBatchSize: number, rateLimit: number, async: boolean, gzipCompression: boolean): PacketSchema<ConvertType<T[number]>[]>;
|
|
54
60
|
}
|
|
55
61
|
export {};
|
|
@@ -27,7 +27,6 @@ const StringUtil_1 = require("../util/StringUtil");
|
|
|
27
27
|
class Packet {
|
|
28
28
|
defaultEnabled;
|
|
29
29
|
tag;
|
|
30
|
-
async;
|
|
31
30
|
maxSize;
|
|
32
31
|
minSize;
|
|
33
32
|
type;
|
|
@@ -39,6 +38,9 @@ class Packet {
|
|
|
39
38
|
dontSpread;
|
|
40
39
|
autoFlatten;
|
|
41
40
|
rateLimit;
|
|
41
|
+
async;
|
|
42
|
+
rereference;
|
|
43
|
+
gzipCompression;
|
|
42
44
|
object;
|
|
43
45
|
client;
|
|
44
46
|
receiveProcessor;
|
|
@@ -48,6 +50,8 @@ class Packet {
|
|
|
48
50
|
processSend;
|
|
49
51
|
validate;
|
|
50
52
|
customValidator;
|
|
53
|
+
lastReceived = {};
|
|
54
|
+
lastSent = {};
|
|
51
55
|
constructor(tag, schema, customValidator, enabled, client) {
|
|
52
56
|
this.tag = tag;
|
|
53
57
|
this.defaultEnabled = enabled;
|
|
@@ -57,8 +61,10 @@ class Packet {
|
|
|
57
61
|
this.rateLimit = schema.rateLimit;
|
|
58
62
|
this.dontSpread = schema.dontSpread;
|
|
59
63
|
this.autoFlatten = schema.autoFlatten;
|
|
64
|
+
this.rereference = schema.rereference;
|
|
60
65
|
this.dataBatching = schema.dataBatching;
|
|
61
66
|
this.maxBatchSize = client ? Infinity : schema.maxBatchSize;
|
|
67
|
+
this.gzipCompression = schema.gzipCompression;
|
|
62
68
|
this.object = schema.object;
|
|
63
69
|
if (schema.testObject()) {
|
|
64
70
|
this.type = schema.type;
|
|
@@ -70,8 +76,8 @@ class Packet {
|
|
|
70
76
|
this.minSize = this.type.length;
|
|
71
77
|
// trst me bro..
|
|
72
78
|
this.receiveProcessor = (0, PacketProcessors_1.createObjReceiveProcessor)(this);
|
|
73
|
-
this.validator = (0, PacketProcessors_1.createObjValidator)(this);
|
|
74
79
|
this.sendProcessor = (0, PacketProcessors_1.createObjSendProcessor)(this);
|
|
80
|
+
this.validator = (0, PacketProcessors_1.createObjValidator)(this);
|
|
75
81
|
}
|
|
76
82
|
else {
|
|
77
83
|
this.type = schema.type;
|
|
@@ -82,22 +88,22 @@ class Packet {
|
|
|
82
88
|
// @ts-expect-error
|
|
83
89
|
this.receiveProcessor = (0, PacketProcessors_1.createReceiveProcessor)(this.type, this.enumData, this.dataMax);
|
|
84
90
|
// @ts-expect-error
|
|
85
|
-
this.
|
|
91
|
+
this.sendProcessor = (0, PacketProcessors_1.createSendProcessor)(this.type, this.gzipCompression, this.rereference);
|
|
86
92
|
// @ts-expect-error
|
|
87
|
-
this.
|
|
93
|
+
this.validator = (0, PacketProcessors_1.createValidator)(this.type, this.dataMax, this.dataMin, this, this.gzipCompression, this.rereference);
|
|
88
94
|
}
|
|
89
95
|
this.processReceive = (data, validationResult) => this.receiveProcessor(data, validationResult, 0);
|
|
90
|
-
this.processSend = (data) => new Uint8Array(this.sendProcessor(data));
|
|
96
|
+
this.processSend = async (data) => new Uint8Array(await this.sendProcessor(tag, data));
|
|
91
97
|
this.validate = (data) => this.validator(data, 0);
|
|
92
98
|
this.customValidator = customValidator;
|
|
93
99
|
}
|
|
94
|
-
listen(value, socket) {
|
|
100
|
+
async listen(value, socket) {
|
|
95
101
|
try {
|
|
96
|
-
const validationResult = this.validate(value);
|
|
102
|
+
const [dcData, validationResult] = await this.validate(value);
|
|
97
103
|
// holy shit i used === to fix another bug
|
|
98
104
|
if (!this.client && validationResult === false)
|
|
99
105
|
return "Invalid packet";
|
|
100
|
-
const processed = this.processReceive(
|
|
106
|
+
const processed = this.processReceive(dcData, validationResult);
|
|
101
107
|
const useableData = this.autoFlatten ? (0, PacketUtils_1.UnFlattenData)(processed) : processed;
|
|
102
108
|
if (this.customValidator != null) {
|
|
103
109
|
if (!this.dontSpread) {
|
|
@@ -120,8 +126,7 @@ class Packet {
|
|
|
120
126
|
// shared values for both
|
|
121
127
|
const sharedData = [
|
|
122
128
|
this.tag.length, ...(0, StringUtil_1.processCharCodes)(this.tag),
|
|
123
|
-
this.dontSpread
|
|
124
|
-
this.async ? 1 : 0,
|
|
129
|
+
(0, CompressionUtil_1.compressBools)([this.dontSpread, this.async, this.object, this.autoFlatten, this.gzipCompression, this.rereference]),
|
|
125
130
|
this.dataBatching,
|
|
126
131
|
this.enumData.length, ...this.enumData.map(x => x.serialize()).flat(),
|
|
127
132
|
];
|
|
@@ -129,7 +134,6 @@ class Packet {
|
|
|
129
134
|
if (!this.object) {
|
|
130
135
|
return [
|
|
131
136
|
...sharedData, // shared
|
|
132
|
-
0, // dummy byte flag for consistent deserialization; becomes -1 to indicate single
|
|
133
137
|
...(0, CompressionUtil_1.convertVarInt)(this.dataMax), // the data max
|
|
134
138
|
...(0, CompressionUtil_1.convertVarInt)(this.dataMin), // the data min
|
|
135
139
|
this.type,
|
|
@@ -138,12 +142,10 @@ class Packet {
|
|
|
138
142
|
// object packet
|
|
139
143
|
return [
|
|
140
144
|
...sharedData,
|
|
141
|
-
this.maxSize
|
|
142
|
-
this.autoFlatten ? 1 : 0, // auto flatten flag
|
|
145
|
+
this.maxSize, // size
|
|
143
146
|
...this.dataMax.map(CompressionUtil_1.convertVarInt).flat(), // all data maxes, serialized
|
|
144
147
|
...this.dataMin.map(CompressionUtil_1.convertVarInt).flat(), // all data mins, serialized
|
|
145
|
-
...this.type, // all types
|
|
146
|
-
this.tag.length, // tag length, offset by 1 for NULL
|
|
148
|
+
...this.type, // all types
|
|
147
149
|
];
|
|
148
150
|
}
|
|
149
151
|
static readVarInts(data, offset, size) {
|
|
@@ -161,10 +163,8 @@ class Packet {
|
|
|
161
163
|
const tagLength = data[offset++];
|
|
162
164
|
// read tag as it's up 1, and add offset
|
|
163
165
|
const tag = (0, BufferUtil_1.as8String)(data.slice(offset, offset += tagLength));
|
|
164
|
-
// then read
|
|
165
|
-
const dontSpread = data[offset++]
|
|
166
|
-
// read async
|
|
167
|
-
const async = data[offset++] == 1;
|
|
166
|
+
// then read dontSpread and async
|
|
167
|
+
const [dontSpread, async, isObject, autoFlatten, gzipCompression, rereference] = (0, CompressionUtil_1.decompressBools)(data[offset++]);
|
|
168
168
|
// read batching, up 1
|
|
169
169
|
const dataBatching = data[offset++];
|
|
170
170
|
// read enum length, up 1
|
|
@@ -191,13 +191,10 @@ class Packet {
|
|
|
191
191
|
// define the enum with the values
|
|
192
192
|
enums.push((0, EnumHandler_1.DefineEnum)(enumTag, values));
|
|
193
193
|
}
|
|
194
|
-
// read type count; prob should change sometime
|
|
195
|
-
const size = data[offset++] - 1;
|
|
196
194
|
// objects
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
const autoFlatten = data[offset++] == 1;
|
|
195
|
+
if (isObject) {
|
|
196
|
+
// read size
|
|
197
|
+
const size = data[offset++];
|
|
201
198
|
// read var ints for the datamaxes
|
|
202
199
|
const [dataMaxes, o1] = this.readVarInts(data, offset, size);
|
|
203
200
|
offset = o1;
|
|
@@ -210,11 +207,11 @@ class Packet {
|
|
|
210
207
|
let index = 0;
|
|
211
208
|
const finalTypes = types.map(x => x == PacketType_1.PacketType.ENUMS ? enums[index++] : x); // convert enums to their enum packages
|
|
212
209
|
// make schema
|
|
213
|
-
const schema = PacketSchema.object(finalTypes, dataMaxes, dataMins, dontSpread, autoFlatten, dataBatching, -1, -1, async);
|
|
210
|
+
const schema = PacketSchema.object(finalTypes, dataMaxes, dataMins, dontSpread, autoFlatten, dataBatching, -1, -1, async, gzipCompression);
|
|
214
211
|
return [
|
|
215
212
|
new Packet(tag, schema, null, false, client),
|
|
216
213
|
// +1 to go next
|
|
217
|
-
(offset - beginningOffset)
|
|
214
|
+
(offset - beginningOffset),
|
|
218
215
|
];
|
|
219
216
|
}
|
|
220
217
|
// single packet
|
|
@@ -224,16 +221,15 @@ class Packet {
|
|
|
224
221
|
// read varint for datamin
|
|
225
222
|
const [o2, dataMin] = (0, CompressionUtil_1.readVarInt)(data, offset);
|
|
226
223
|
offset = o2;
|
|
227
|
-
// read type
|
|
228
|
-
const type = data[offset];
|
|
224
|
+
// read type
|
|
225
|
+
const type = data[offset++];
|
|
229
226
|
// do enum stuff
|
|
230
227
|
const finalType = type == PacketType_1.PacketType.ENUMS ? enums[0] : type; // convert enum to enum package
|
|
231
228
|
// make schema
|
|
232
|
-
const schema = PacketSchema.single(finalType, dataMax, dataMin, dontSpread, dataBatching, -1, -1, async);
|
|
229
|
+
const schema = PacketSchema.single(finalType, dataMax, dataMin, dontSpread, dataBatching, -1, -1, async, gzipCompression, rereference);
|
|
233
230
|
return [
|
|
234
231
|
new Packet(tag, schema, null, false, client),
|
|
235
|
-
|
|
236
|
-
(offset - beginningOffset) + 1,
|
|
232
|
+
(offset - beginningOffset),
|
|
237
233
|
];
|
|
238
234
|
}
|
|
239
235
|
static deserializeAll(data, client) {
|
|
@@ -259,6 +255,8 @@ class PacketSchema {
|
|
|
259
255
|
dontSpread = false;
|
|
260
256
|
autoFlatten = false;
|
|
261
257
|
async = false;
|
|
258
|
+
rereference = false;
|
|
259
|
+
gzipCompression = false;
|
|
262
260
|
object;
|
|
263
261
|
constructor(object) {
|
|
264
262
|
this.object = object;
|
|
@@ -266,7 +264,7 @@ class PacketSchema {
|
|
|
266
264
|
testObject() {
|
|
267
265
|
return this.object;
|
|
268
266
|
}
|
|
269
|
-
static single(type, dataMax, dataMin, dontSpread, dataBatching, maxBatchSize, rateLimit, async) {
|
|
267
|
+
static single(type, dataMax, dataMin, dontSpread, dataBatching, maxBatchSize, rateLimit, async, gzipCompression, rereference) {
|
|
270
268
|
const schema = new PacketSchema(false);
|
|
271
269
|
if (typeof type == 'number') {
|
|
272
270
|
schema.type = type;
|
|
@@ -277,16 +275,18 @@ class PacketSchema {
|
|
|
277
275
|
schema.type = PacketType_1.PacketType.ENUMS;
|
|
278
276
|
schema.enumData = [type];
|
|
279
277
|
}
|
|
280
|
-
schema.
|
|
278
|
+
schema.async = async;
|
|
281
279
|
schema.dataMin = dataMin;
|
|
280
|
+
schema.dataMax = dataMax;
|
|
281
|
+
schema.rateLimit = rateLimit;
|
|
282
282
|
schema.dontSpread = dontSpread;
|
|
283
|
+
schema.rereference = rereference;
|
|
283
284
|
schema.dataBatching = dataBatching;
|
|
284
285
|
schema.maxBatchSize = maxBatchSize;
|
|
285
|
-
schema.
|
|
286
|
-
schema.async = async;
|
|
286
|
+
schema.gzipCompression = gzipCompression;
|
|
287
287
|
return schema;
|
|
288
288
|
}
|
|
289
|
-
static object(types, dataMaxes, dataMins, dontSpread, autoFlatten, dataBatching, maxBatchSize, rateLimit, async) {
|
|
289
|
+
static object(types, dataMaxes, dataMins, dontSpread, autoFlatten, dataBatching, maxBatchSize, rateLimit, async, gzipCompression) {
|
|
290
290
|
if (types.length != dataMaxes.length || types.length != dataMins.length)
|
|
291
291
|
throw new Error("There is an inbalance between the amount of types, data maxes, and data mins!");
|
|
292
292
|
const schema = new PacketSchema(true);
|
|
@@ -300,14 +300,15 @@ class PacketSchema {
|
|
|
300
300
|
schema.enumData.push(type);
|
|
301
301
|
}
|
|
302
302
|
});
|
|
303
|
-
schema.
|
|
303
|
+
schema.async = async;
|
|
304
304
|
schema.dataMin = dataMins;
|
|
305
|
+
schema.dataMax = dataMaxes;
|
|
306
|
+
schema.rateLimit = rateLimit;
|
|
305
307
|
schema.dontSpread = dontSpread;
|
|
306
308
|
schema.autoFlatten = autoFlatten;
|
|
307
309
|
schema.dataBatching = dataBatching;
|
|
308
310
|
schema.maxBatchSize = maxBatchSize;
|
|
309
|
-
schema.
|
|
310
|
-
schema.async = async;
|
|
311
|
+
schema.gzipCompression = gzipCompression;
|
|
311
312
|
return schema;
|
|
312
313
|
}
|
|
313
314
|
}
|
|
@@ -67,7 +67,7 @@ export declare class SonicWSConnection implements Connection {
|
|
|
67
67
|
* @param tag The tag to send
|
|
68
68
|
* @param values The values to send
|
|
69
69
|
*/
|
|
70
|
-
send(tag: string, ...values: any[]): void
|
|
70
|
+
send(tag: string, ...values: any[]): Promise<void>;
|
|
71
71
|
/**
|
|
72
72
|
* Broadcasts a packet to all other users connected
|
|
73
73
|
* @param tag The tag to send
|
|
@@ -92,6 +92,7 @@ class SonicWSConnection {
|
|
|
92
92
|
for (const tag of host.clientPackets.getTags()) {
|
|
93
93
|
this.listeners[tag] = [];
|
|
94
94
|
const pack = host.clientPackets.getPacket(tag);
|
|
95
|
+
pack.lastReceived[this.id] = undefined;
|
|
95
96
|
this.enabledPackets[tag] = pack.defaultEnabled;
|
|
96
97
|
this.asyncMap[tag] = pack.async;
|
|
97
98
|
if (pack.async)
|
|
@@ -121,6 +122,9 @@ class SonicWSConnection {
|
|
|
121
122
|
if (shouldCall)
|
|
122
123
|
callback(true);
|
|
123
124
|
}
|
|
125
|
+
for (const packet of host.clientPackets.getPackets()) {
|
|
126
|
+
delete packet.lastReceived[this.id];
|
|
127
|
+
}
|
|
124
128
|
});
|
|
125
129
|
}
|
|
126
130
|
parseData(event) {
|
|
@@ -209,17 +213,23 @@ class SonicWSConnection {
|
|
|
209
213
|
else
|
|
210
214
|
this.listenLock = false;
|
|
211
215
|
}
|
|
212
|
-
messageHandler(data) {
|
|
216
|
+
async messageHandler(data) {
|
|
213
217
|
if (data == null)
|
|
214
218
|
return;
|
|
215
219
|
const [tag, value] = data;
|
|
216
220
|
const packet = this.host.clientPackets.getPacket(tag);
|
|
221
|
+
if (packet.rereference && value.length == 0) {
|
|
222
|
+
if (packet.lastReceived[this.id] === undefined)
|
|
223
|
+
return this.invalidPacket("No previous value to rereference");
|
|
224
|
+
this.listenPacket(packet.lastReceived[this.id], tag);
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
217
227
|
if (packet.dataBatching == 0) {
|
|
218
|
-
const res = packet.listen(value, this);
|
|
228
|
+
const res = packet.lastReceived[this.id] = await packet.listen(value, this);
|
|
219
229
|
this.listenPacket(res, tag);
|
|
220
230
|
return;
|
|
221
231
|
}
|
|
222
|
-
const batchData = BatchHelper_1.BatchHelper.unravelBatch(packet, value, this);
|
|
232
|
+
const batchData = await BatchHelper_1.BatchHelper.unravelBatch(packet, value, this);
|
|
223
233
|
if (typeof batchData == 'string')
|
|
224
234
|
return this.invalidPacket(batchData);
|
|
225
235
|
for (const data of batchData) {
|
|
@@ -281,8 +291,8 @@ class SonicWSConnection {
|
|
|
281
291
|
* @param tag The tag to send
|
|
282
292
|
* @param values The values to send
|
|
283
293
|
*/
|
|
284
|
-
send(tag, ...values) {
|
|
285
|
-
this.send_processed(...(0, PacketUtils_1.processPacket)(this.host.serverPackets, tag, values));
|
|
294
|
+
async send(tag, ...values) {
|
|
295
|
+
this.send_processed(...await (0, PacketUtils_1.processPacket)(this.host.serverPackets, tag, values, this.id));
|
|
286
296
|
}
|
|
287
297
|
/**
|
|
288
298
|
* Broadcasts a packet to all other users connected
|
|
@@ -13,6 +13,8 @@ export type SonicServerOptions = {
|
|
|
13
13
|
readonly serverPackets?: PacketTypings;
|
|
14
14
|
/** Default WS Options */
|
|
15
15
|
readonly websocketOptions?: WS.ServerOptions;
|
|
16
|
+
/** If it should check for updates; defaults to true. */
|
|
17
|
+
readonly checkForUpdates?: boolean;
|
|
16
18
|
};
|
|
17
19
|
export type PacketTypings = readonly Packet<PacketType | readonly PacketType[]>[];
|
|
18
20
|
export declare class SonicWSServer {
|
|
@@ -98,14 +100,14 @@ export declare class SonicWSServer {
|
|
|
98
100
|
* @param packetTag Packet tag to send
|
|
99
101
|
* @param values Values to send
|
|
100
102
|
*/
|
|
101
|
-
broadcastTagged(tag: string, packetTag: string, ...values: any): void
|
|
103
|
+
broadcastTagged(tag: string, packetTag: string, ...values: any): Promise<void>;
|
|
102
104
|
/**
|
|
103
105
|
* Broadcasts a packet to all users connected, but with a filter
|
|
104
106
|
* @param tag The tag to send
|
|
105
107
|
* @param filter The filter for who to send to
|
|
106
108
|
* @param values The values to send
|
|
107
109
|
*/
|
|
108
|
-
broadcastFiltered(tag: string, filter: (socket: SonicWSConnection) => boolean, ...values: any): void
|
|
110
|
+
broadcastFiltered(tag: string, filter: (socket: SonicWSConnection) => boolean, ...values: any): Promise<void>;
|
|
109
111
|
/**
|
|
110
112
|
* Broadcasts a packet to all users connected
|
|
111
113
|
* @param tag The tag to send
|
|
@@ -83,10 +83,11 @@ class SonicWSServer {
|
|
|
83
83
|
const s_serverPackets = this.serverPackets.serialize();
|
|
84
84
|
const serverData = [...version_1.SERVER_SUFFIX_NUMS, version_1.VERSION];
|
|
85
85
|
const keyData = [...(0, CompressionUtil_1.convertVarInt)(s_clientPackets.length), ...s_clientPackets, ...s_serverPackets];
|
|
86
|
-
this.wss.on('connection', (socket) => {
|
|
86
|
+
this.wss.on('connection', async (socket) => {
|
|
87
87
|
const sonicConnection = new SonicWSConnection_1.SonicWSConnection(socket, this, this.generateSocketID(), this.handshakePacket, this.clientRateLimit, this.serverRateLimit);
|
|
88
88
|
// send tags to the client so it doesn't have to hard code them in
|
|
89
|
-
|
|
89
|
+
const data = new Uint8Array([...(0, CompressionUtil_1.convertVarInt)(sonicConnection.id), ...keyData]);
|
|
90
|
+
socket.send([...serverData, ...await (0, CompressionUtil_1.compressGzip)(data)]);
|
|
90
91
|
this.connections.push(sonicConnection);
|
|
91
92
|
this.connectionMap[sonicConnection.id] = sonicConnection;
|
|
92
93
|
this.connectListeners.forEach(l => l(sonicConnection));
|
|
@@ -101,17 +102,19 @@ class SonicWSServer {
|
|
|
101
102
|
}
|
|
102
103
|
});
|
|
103
104
|
});
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
105
|
+
if (settings.checkForUpdates ?? true) {
|
|
106
|
+
fetch('https://raw.githubusercontent.com/liwybloc/sonic-ws/refs/heads/main/release/version')
|
|
107
|
+
.then((res) => res.text())
|
|
108
|
+
.then((ver) => {
|
|
109
|
+
if (parseInt(ver) > version_1.VERSION) {
|
|
110
|
+
console.warn(`SonicWS is currently running outdated! (current: ${version_1.VERSION}, latest: ${ver}) Update with "npm update sonic-ws"`);
|
|
111
|
+
}
|
|
112
|
+
})
|
|
113
|
+
.catch((err) => {
|
|
114
|
+
console.error(err);
|
|
115
|
+
console.warn(`Could not check SonicWS version.`);
|
|
116
|
+
});
|
|
117
|
+
}
|
|
115
118
|
}
|
|
116
119
|
generateSocketID() {
|
|
117
120
|
if (this.availableIds.length == 0)
|
|
@@ -213,10 +216,10 @@ class SonicWSServer {
|
|
|
213
216
|
* @param packetTag Packet tag to send
|
|
214
217
|
* @param values Values to send
|
|
215
218
|
*/
|
|
216
|
-
broadcastTagged(tag, packetTag, ...values) {
|
|
219
|
+
async broadcastTagged(tag, packetTag, ...values) {
|
|
217
220
|
if (!this.tagsInv.has(tag))
|
|
218
221
|
return;
|
|
219
|
-
const data = (0, PacketUtils_1.processPacket)(this.serverPackets, packetTag, values);
|
|
222
|
+
const data = await (0, PacketUtils_1.processPacket)(this.serverPackets, packetTag, values, -1);
|
|
220
223
|
this.tagsInv.get(tag).forEach(conn => conn.send_processed(...data));
|
|
221
224
|
}
|
|
222
225
|
/**
|
|
@@ -225,8 +228,8 @@ class SonicWSServer {
|
|
|
225
228
|
* @param filter The filter for who to send to
|
|
226
229
|
* @param values The values to send
|
|
227
230
|
*/
|
|
228
|
-
broadcastFiltered(tag, filter, ...values) {
|
|
229
|
-
const data = (0, PacketUtils_1.processPacket)(this.serverPackets, tag, values);
|
|
231
|
+
async broadcastFiltered(tag, filter, ...values) {
|
|
232
|
+
const data = await (0, PacketUtils_1.processPacket)(this.serverPackets, tag, values, -1);
|
|
230
233
|
this.connections.filter(filter).forEach(conn => conn.send_processed(...data));
|
|
231
234
|
}
|
|
232
235
|
/**
|
|
@@ -11,5 +11,5 @@ export declare class BatchHelper {
|
|
|
11
11
|
private initiateBatch;
|
|
12
12
|
private startBatch;
|
|
13
13
|
batchPacket(code: number, data: Uint8Array): void;
|
|
14
|
-
static unravelBatch(packet: Packet<any>, data: Uint8Array, socket: SonicWSConnection | null): any[] | string
|
|
14
|
+
static unravelBatch(packet: Packet<any>, data: Uint8Array, socket: SonicWSConnection | null): Promise<any[] | string>;
|
|
15
15
|
}
|
|
@@ -51,7 +51,7 @@ class BatchHelper {
|
|
|
51
51
|
if (!this.batchTimeouts[code])
|
|
52
52
|
this.startBatch(code);
|
|
53
53
|
}
|
|
54
|
-
static unravelBatch(packet, data, socket) {
|
|
54
|
+
static async unravelBatch(packet, data, socket) {
|
|
55
55
|
const result = [];
|
|
56
56
|
for (let i = 0; i < data.length;) {
|
|
57
57
|
// must be >0 for it to apply
|
|
@@ -66,7 +66,7 @@ class BatchHelper {
|
|
|
66
66
|
// read sector
|
|
67
67
|
const sect = data.slice(i, i += varint);
|
|
68
68
|
// call the packets listeners
|
|
69
|
-
const listen = packet.listen(sect, socket);
|
|
69
|
+
const listen = await packet.listen(sect, socket);
|
|
70
70
|
// if invalid, return that
|
|
71
71
|
if (typeof listen == 'string')
|
|
72
72
|
return "Batched packet: " + listen;
|
|
@@ -15,6 +15,7 @@ export declare const MAX_UVARINT: number;
|
|
|
15
15
|
export declare const MAX_VARINT: number;
|
|
16
16
|
export declare const ONE_EIGHT: number;
|
|
17
17
|
export declare const ONE_FOURTH: number;
|
|
18
|
+
export declare const EMPTY_UINT8: Uint8Array<ArrayBuffer>;
|
|
18
19
|
export declare const varIntOverflowPow: (num: number) => number;
|
|
19
20
|
export type SHORT_BITS = [high: number, low: number];
|
|
20
21
|
export declare function fromShort(short: SHORT_BITS): number;
|
|
@@ -39,3 +40,9 @@ export declare function bytesToBits(bytes: ArrayLike<number>): string;
|
|
|
39
40
|
export declare function bitsToBytes(bitString: string): Uint8Array;
|
|
40
41
|
export declare function encodeHuffman(text: string): Uint8Array;
|
|
41
42
|
export declare function decodeHuffman(bits: string): string;
|
|
43
|
+
export declare function compressGzip(data: Uint8Array<ArrayBuffer>, ident?: string): Promise<Uint8Array>;
|
|
44
|
+
export declare function decompressGzip(data: Uint8Array): Promise<Uint8Array>;
|
|
45
|
+
export declare const compressJSON: (value: any) => Uint8Array<ArrayBuffer>;
|
|
46
|
+
export declare const decompressJSON: (bytes: Uint8Array) => any;
|
|
47
|
+
export declare function bytesToHex(bytes: Uint8Array): string;
|
|
48
|
+
export declare function hexToBytes(hex: string): Uint8Array<ArrayBuffer>;
|