sonic-ws 1.0.0-rc.8 → 1.0.3
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/LICENSE +190 -209
- package/README.md +11 -11
- package/dist/index.d.ts +0 -0
- package/dist/index.js +1 -1
- package/dist/version.d.ts +5 -3
- package/dist/version.js +8 -5
- package/dist/ws/Connection.d.ts +44 -0
- package/dist/ws/Connection.js +17 -0
- package/dist/ws/client/core/ClientCore.d.ts +9 -17
- package/dist/ws/client/core/ClientCore.js +122 -159
- package/dist/ws/client/node/ClientNode.d.ts +0 -0
- package/dist/ws/client/node/ClientNode.js +8 -25
- package/dist/ws/packets/PacketProcessors.d.ts +11 -7
- package/dist/ws/packets/PacketProcessors.js +218 -177
- package/dist/ws/packets/PacketType.d.ts +20 -16
- package/dist/ws/packets/PacketType.js +21 -17
- package/dist/ws/packets/Packets.d.ts +8 -10
- package/dist/ws/packets/Packets.js +186 -161
- package/dist/ws/server/SonicWSConnection.d.ts +7 -21
- package/dist/ws/server/SonicWSConnection.js +117 -148
- package/dist/ws/server/SonicWSServer.d.ts +0 -0
- package/dist/ws/server/SonicWSServer.js +79 -98
- package/dist/ws/util/ArrayUtil.d.ts +0 -0
- package/dist/ws/util/ArrayUtil.js +3 -3
- package/dist/ws/util/BufferUtil.d.ts +3 -0
- package/dist/ws/util/BufferUtil.js +19 -3
- package/dist/ws/util/StringUtil.d.ts +6 -0
- package/dist/ws/util/StringUtil.js +66 -0
- package/dist/ws/util/enums/EnumHandler.d.ts +2 -3
- package/dist/ws/util/enums/EnumHandler.js +17 -17
- package/dist/ws/util/enums/EnumType.d.ts +1 -1
- package/dist/ws/util/enums/EnumType.js +28 -24
- package/dist/ws/util/packets/BatchHelper.d.ts +9 -5
- package/dist/ws/util/packets/BatchHelper.js +50 -39
- package/dist/ws/util/packets/CompressionUtil.d.ts +22 -15
- package/dist/ws/util/packets/CompressionUtil.js +179 -141
- package/dist/ws/util/packets/PacketHolder.d.ts +1 -6
- package/dist/ws/util/packets/PacketHolder.js +43 -41
- package/dist/ws/util/packets/PacketUtils.d.ts +2 -6
- package/dist/ws/util/packets/PacketUtils.js +71 -70
- package/dist/ws/util/packets/RateHandler.d.ts +0 -0
- package/dist/ws/util/packets/RateHandler.js +24 -26
- package/package.json +6 -6
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/*
|
|
3
|
-
* Copyright 2025 Lily (
|
|
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,201 +14,242 @@
|
|
|
14
14
|
* See the License for the specific language governing permissions and
|
|
15
15
|
* limitations under the License.
|
|
16
16
|
*/
|
|
17
|
-
var _a, _b, _c;
|
|
18
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
-
exports.
|
|
18
|
+
exports.createValidator = createValidator;
|
|
19
|
+
exports.createReceiveProcessor = createReceiveProcessor;
|
|
20
|
+
exports.createSendProcessor = createSendProcessor;
|
|
20
21
|
exports.createObjSendProcessor = createObjSendProcessor;
|
|
21
22
|
exports.createObjReceiveProcessor = createObjReceiveProcessor;
|
|
22
23
|
exports.createObjValidator = createObjValidator;
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
return true;
|
|
40
|
-
};
|
|
41
|
-
var BYTE_LEN = function (data, cap, min) { return data.length >= min && data.length <= cap; };
|
|
42
|
-
var SHORT_LEN = function (data, cap, min) { return data.length >= min * 2 && data.length <= cap * 2 && data.length % 2 == 0; };
|
|
43
|
-
// todo, instead of big array make this a function that creates functions, then i can include pre-defined data like Math.floor(min/8) and stuff
|
|
44
|
-
exports.PacketValidityProcessors = (_a = {},
|
|
45
|
-
_a[PacketType_1.PacketType.NONE] = function (data) { return data.length == 0; },
|
|
46
|
-
_a[PacketType_1.PacketType.RAW] = function () { return true; },
|
|
47
|
-
_a[PacketType_1.PacketType.STRINGS] = LEN_DELIMIT,
|
|
48
|
-
_a[PacketType_1.PacketType.ENUMS] = function (data, cap, min, packet, index) {
|
|
49
|
-
if (data.length < min || data.length > cap || index >= packet.enumData.length)
|
|
24
|
+
const ArrayUtil_1 = require("../util/ArrayUtil");
|
|
25
|
+
const CompressionUtil_1 = require("../util/packets/CompressionUtil");
|
|
26
|
+
const PacketType_1 = require("./PacketType");
|
|
27
|
+
const BufferUtil_1 = require("../util/BufferUtil");
|
|
28
|
+
const StringUtil_1 = require("../util/StringUtil");
|
|
29
|
+
function BYTE_LEN(cap, min) {
|
|
30
|
+
return (data) => data.length >= min && data.length <= cap;
|
|
31
|
+
}
|
|
32
|
+
function SHORT_LEN(cap, min) {
|
|
33
|
+
min *= 2;
|
|
34
|
+
cap *= 2;
|
|
35
|
+
return (data) => data.length >= min && data.length <= cap && data.length % 2 == 0;
|
|
36
|
+
}
|
|
37
|
+
function VARINT_VERIF(cap, min) {
|
|
38
|
+
return (data) => {
|
|
39
|
+
if (data.length == 0)
|
|
50
40
|
return false;
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
41
|
+
let sectors = 0, i = 0, computed = [];
|
|
42
|
+
while (i < data.length) {
|
|
43
|
+
const [off, varint] = (0, CompressionUtil_1.readVarInt)(data, i);
|
|
44
|
+
i = off;
|
|
45
|
+
computed.push(varint);
|
|
46
|
+
if (++sectors > cap)
|
|
54
47
|
return false;
|
|
55
48
|
}
|
|
56
|
-
|
|
57
|
-
},
|
|
58
|
-
_a[PacketType_1.PacketType.BYTES] = BYTE_LEN,
|
|
59
|
-
_a[PacketType_1.PacketType.UBYTES] = BYTE_LEN,
|
|
60
|
-
_a[PacketType_1.PacketType.BYTES_ZZ] = BYTE_LEN,
|
|
61
|
-
_a[PacketType_1.PacketType.SHORTS] = SHORT_LEN,
|
|
62
|
-
_a[PacketType_1.PacketType.USHORTS] = SHORT_LEN,
|
|
63
|
-
_a[PacketType_1.PacketType.SHORTS_ZZ] = SHORT_LEN,
|
|
64
|
-
_a[PacketType_1.PacketType.INTEGERS_D] = function (raw, cap, min) {
|
|
65
|
-
if (raw.length == 0)
|
|
66
|
-
return false;
|
|
67
|
-
var dataLength = raw.length - 1, sectSize = raw[0];
|
|
68
|
-
if (dataLength % sectSize != 0)
|
|
69
|
-
return false;
|
|
70
|
-
var valueAmount = dataLength / sectSize;
|
|
71
|
-
if (valueAmount < min || valueAmount > cap)
|
|
49
|
+
if (sectors < min)
|
|
72
50
|
return false;
|
|
73
|
-
return
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
51
|
+
return computed;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
;
|
|
55
|
+
function createValidator(type, dataCap, dataMin, packet) {
|
|
56
|
+
switch (type) {
|
|
57
|
+
case PacketType_1.PacketType.NONE: return (data) => data.length == 0;
|
|
58
|
+
case PacketType_1.PacketType.RAW: return () => undefined;
|
|
59
|
+
case PacketType_1.PacketType.ENUMS: return (data, index) => {
|
|
60
|
+
if (data.length < dataMin || data.length > dataCap || index >= packet.enumData.length)
|
|
80
61
|
return false;
|
|
81
|
-
|
|
82
|
-
|
|
62
|
+
console.log("~~ENUM~~", packet, packet.enumData, index);
|
|
63
|
+
const pkg = packet.enumData[index];
|
|
64
|
+
for (let i = 0; i < data.length; i++) {
|
|
65
|
+
if (pkg.values.length <= data[i])
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
case PacketType_1.PacketType.BYTES: return BYTE_LEN(dataCap, dataMin);
|
|
70
|
+
case PacketType_1.PacketType.UBYTES: return BYTE_LEN(dataCap, dataMin);
|
|
71
|
+
case PacketType_1.PacketType.SHORTS: return SHORT_LEN(dataCap, dataMin);
|
|
72
|
+
case PacketType_1.PacketType.USHORTS: return SHORT_LEN(dataCap, dataMin);
|
|
73
|
+
case PacketType_1.PacketType.VARINT: return VARINT_VERIF(dataCap, dataMin);
|
|
74
|
+
case PacketType_1.PacketType.UVARINT: return VARINT_VERIF(dataCap, dataMin);
|
|
75
|
+
case PacketType_1.PacketType.DELTAS: return VARINT_VERIF(dataCap, dataMin);
|
|
76
|
+
case PacketType_1.PacketType.FLOATS: return (data) => {
|
|
77
|
+
if (data.length % 4 !== 0)
|
|
83
78
|
return false;
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
return
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
function
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
79
|
+
const sectors = data.length * CompressionUtil_1.ONE_FOURTH;
|
|
80
|
+
if (sectors > dataCap || sectors < dataMin)
|
|
81
|
+
return false;
|
|
82
|
+
return undefined;
|
|
83
|
+
};
|
|
84
|
+
case PacketType_1.PacketType.DOUBLES: return (data) => {
|
|
85
|
+
if (data.length % 8 !== 0)
|
|
86
|
+
return false;
|
|
87
|
+
const sectors = data.length * CompressionUtil_1.ONE_EIGHT;
|
|
88
|
+
if (sectors > dataCap || sectors < dataMin)
|
|
89
|
+
return false;
|
|
90
|
+
return undefined;
|
|
91
|
+
};
|
|
92
|
+
case PacketType_1.PacketType.BOOLEANS:
|
|
93
|
+
{
|
|
94
|
+
const min = Math.floor(dataMin * CompressionUtil_1.ONE_EIGHT);
|
|
95
|
+
const cap = Math.floor(dataCap * CompressionUtil_1.ONE_EIGHT);
|
|
96
|
+
return (data) => data.length >= min && data.length <= cap;
|
|
97
|
+
}
|
|
98
|
+
;
|
|
99
|
+
case PacketType_1.PacketType.STRINGS_ASCII: return (data) => {
|
|
100
|
+
let sectors = 0, index = 0, computed = [];
|
|
101
|
+
while (index < data.length) {
|
|
102
|
+
sectors++;
|
|
103
|
+
if (sectors > dataCap)
|
|
104
|
+
return false;
|
|
105
|
+
const [off, varint] = (0, CompressionUtil_1.readVarInt)(data, index);
|
|
106
|
+
index = off + varint;
|
|
107
|
+
computed.push(varint);
|
|
108
|
+
if (index > data.length)
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
if (sectors < dataMin)
|
|
112
|
+
return false;
|
|
113
|
+
return computed;
|
|
114
|
+
};
|
|
115
|
+
case PacketType_1.PacketType.STRINGS_UTF16: return (data) => {
|
|
116
|
+
let sectors = 0, index = 0, computed = [];
|
|
117
|
+
while (index < data.length) {
|
|
118
|
+
sectors++;
|
|
119
|
+
if (sectors > dataCap)
|
|
120
|
+
return false;
|
|
121
|
+
const [off, varint] = (0, CompressionUtil_1.readVarInt)(data, index);
|
|
122
|
+
index = off + varint * 2;
|
|
123
|
+
computed.push(varint);
|
|
124
|
+
if (index > data.length)
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
if (sectors < dataMin)
|
|
128
|
+
return false;
|
|
129
|
+
return computed;
|
|
130
|
+
};
|
|
131
|
+
default: throw new Error("Unknown type: " + type);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
function createReceiveProcessor(type, enumData, cap) {
|
|
135
|
+
switch (type) {
|
|
136
|
+
case PacketType_1.PacketType.NONE: return () => undefined;
|
|
137
|
+
case PacketType_1.PacketType.RAW: return (data) => data;
|
|
138
|
+
case PacketType_1.PacketType.BYTES: return (data) => Array.from(data).map(CompressionUtil_1.demapZigZag);
|
|
139
|
+
case PacketType_1.PacketType.UBYTES: return (data) => Array.from(data);
|
|
140
|
+
case PacketType_1.PacketType.SHORTS: return (data) => (0, BufferUtil_1.splitBuffer)(data, 2).map(v => (0, CompressionUtil_1.demapShort_ZZ)(v));
|
|
141
|
+
case PacketType_1.PacketType.USHORTS: return (data) => (0, BufferUtil_1.splitBuffer)(data, 2).map(v => (0, CompressionUtil_1.fromShort)(v));
|
|
142
|
+
case PacketType_1.PacketType.VARINT: return (_, computed) => computed.map(CompressionUtil_1.demapZigZag);
|
|
143
|
+
case PacketType_1.PacketType.UVARINT: return (_, computed) => computed;
|
|
144
|
+
case PacketType_1.PacketType.DELTAS: return (_, computed) => computed.map((x, i) => computed[i] = (computed[i - 1] || 0) + (0, CompressionUtil_1.demapZigZag)(x));
|
|
145
|
+
case PacketType_1.PacketType.FLOATS: return (data) => (0, BufferUtil_1.splitBuffer)(data, 4).map(CompressionUtil_1.deconvertFloat);
|
|
146
|
+
case PacketType_1.PacketType.DOUBLES: return (data) => (0, BufferUtil_1.splitBuffer)(data, 8).map(CompressionUtil_1.deconvertDouble);
|
|
147
|
+
case PacketType_1.PacketType.BOOLEANS: return (data) => Array.from(data).map(d => (0, CompressionUtil_1.decompressBools)(d)).flat().splice(0, cap);
|
|
148
|
+
case PacketType_1.PacketType.ENUMS: return (data, _, index) => {
|
|
149
|
+
const pkg = enumData[index];
|
|
150
|
+
return Array.from(data).map(code => pkg.values[code]);
|
|
151
|
+
};
|
|
152
|
+
case PacketType_1.PacketType.STRINGS_ASCII: return (data, computed) => {
|
|
153
|
+
let off = 0;
|
|
154
|
+
return computed.map(len => (0, BufferUtil_1.as8String)(data.subarray(++off, off += len)));
|
|
155
|
+
};
|
|
156
|
+
case PacketType_1.PacketType.STRINGS_UTF16: return (data, computed) => {
|
|
157
|
+
let off = 0;
|
|
158
|
+
return computed.map(len => (0, BufferUtil_1.as16String)(data.subarray(++off, off += len * 2)));
|
|
159
|
+
};
|
|
160
|
+
default: throw new Error("Unknown type: " + type);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
/** Creates a function that processes a packet type */
|
|
164
|
+
function createSendProcessor(type) {
|
|
165
|
+
switch (type) {
|
|
166
|
+
case PacketType_1.PacketType.NONE: return () => [];
|
|
167
|
+
case PacketType_1.PacketType.RAW: return (data) => Array.from(data);
|
|
168
|
+
case PacketType_1.PacketType.ENUMS: return (enums) => enums;
|
|
169
|
+
case PacketType_1.PacketType.BYTES: return (numbers) => numbers.map(CompressionUtil_1.mapZigZag);
|
|
170
|
+
case PacketType_1.PacketType.UBYTES: return (numbers) => numbers.map(CompressionUtil_1.toByte);
|
|
171
|
+
case PacketType_1.PacketType.SHORTS: return (numbers) => numbers.map(CompressionUtil_1.mapShort_ZZ).flat();
|
|
172
|
+
case PacketType_1.PacketType.USHORTS: return (numbers) => numbers.map(CompressionUtil_1.toShort).flat();
|
|
173
|
+
case PacketType_1.PacketType.VARINT: return (numbers) => numbers.map(n => (0, CompressionUtil_1.convertVarInt)((0, CompressionUtil_1.mapZigZag)(n))).flat();
|
|
174
|
+
case PacketType_1.PacketType.UVARINT: return (numbers) => numbers.map(CompressionUtil_1.convertVarInt).flat();
|
|
175
|
+
case PacketType_1.PacketType.DELTAS: return (numbers) => numbers.map((n, i) => (0, CompressionUtil_1.convertVarInt)((0, CompressionUtil_1.mapZigZag)(n - (numbers[i - 1] || 0)))).flat();
|
|
176
|
+
case PacketType_1.PacketType.FLOATS: return (singles) => singles.map(CompressionUtil_1.convertFloat).flat();
|
|
177
|
+
case PacketType_1.PacketType.DOUBLES: return (doubles) => doubles.map(CompressionUtil_1.convertDouble).flat();
|
|
178
|
+
case PacketType_1.PacketType.BOOLEANS: return (bools) => (0, ArrayUtil_1.splitArray)(bools, 8).map((bools) => (0, CompressionUtil_1.compressBools)(bools)).flat();
|
|
179
|
+
case PacketType_1.PacketType.STRINGS_ASCII: return (strings) => {
|
|
180
|
+
const res = [];
|
|
181
|
+
for (const v of strings) {
|
|
182
|
+
const string = String(v);
|
|
183
|
+
res.push(...(0, CompressionUtil_1.convertVarInt)(string.length));
|
|
184
|
+
const codes = (0, StringUtil_1.processCharCodes)(string);
|
|
185
|
+
const highCode = codes.find(x => x > CompressionUtil_1.MAX_BYTE);
|
|
186
|
+
if (highCode)
|
|
187
|
+
throw new Error(`Cannot store code ${highCode} (${String.fromCharCode(highCode)}) in a UTF-8 String! Use STRINGS_UTF16.`);
|
|
188
|
+
codes.map(c => res.push(c));
|
|
189
|
+
}
|
|
190
|
+
return res;
|
|
191
|
+
};
|
|
192
|
+
case PacketType_1.PacketType.STRINGS_UTF16: return (strings) => {
|
|
193
|
+
const res = [];
|
|
194
|
+
for (const v of strings) {
|
|
195
|
+
const string = String(v);
|
|
196
|
+
res.push(...(0, CompressionUtil_1.convertVarInt)(string.length));
|
|
197
|
+
(0, StringUtil_1.processCharCodes)(string).map(c => res.push(...(0, StringUtil_1.splitCodePoint)(c).map(p => (0, CompressionUtil_1.toShort)(p)).flat()));
|
|
198
|
+
}
|
|
199
|
+
return res;
|
|
200
|
+
};
|
|
201
|
+
default: throw new Error("Unknown type: " + type);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
function createObjSendProcessor(packet) {
|
|
205
|
+
const types = packet.type;
|
|
206
|
+
const size = types.length;
|
|
207
|
+
const processors = types.map(t => createSendProcessor(t));
|
|
208
|
+
return (data) => {
|
|
209
|
+
let result = [];
|
|
210
|
+
for (let i = 0; i < size; i++) {
|
|
211
|
+
const sectorData = data[i];
|
|
212
|
+
const d = processors[i](Array.isArray(sectorData) ? sectorData : [sectorData]);
|
|
213
|
+
if (d.length > CompressionUtil_1.MAX_UVARINT)
|
|
214
|
+
throw new Error(`Cannot send ${d.length}/${CompressionUtil_1.MAX_UVARINT} bytes of data!`);
|
|
215
|
+
result.push(...(0, CompressionUtil_1.convertVarInt)(d.length));
|
|
216
|
+
d.forEach(val => result.push(val));
|
|
181
217
|
}
|
|
182
218
|
return result;
|
|
183
219
|
};
|
|
184
220
|
}
|
|
185
|
-
function createObjReceiveProcessor(
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
221
|
+
function createObjReceiveProcessor(packet) {
|
|
222
|
+
const types = packet.type, dataMaxes = packet.dataMax;
|
|
223
|
+
const processors = types.map((t, i) => createReceiveProcessor(t, packet.enumData, dataMaxes[i]));
|
|
224
|
+
return (data, validationResult) => {
|
|
225
|
+
let index = 0, enums = 0, result = [];
|
|
226
|
+
while (index < data.length) {
|
|
227
|
+
const [off, sectorLength] = (0, CompressionUtil_1.readVarInt)(data, index);
|
|
228
|
+
index = off;
|
|
229
|
+
const sector = data.subarray(index, index += sectorLength);
|
|
230
|
+
result.push(processors[result.length](sector, validationResult[result.length], types[result.length] == PacketType_1.PacketType.ENUMS ? enums++ : 0));
|
|
194
231
|
}
|
|
195
232
|
return result;
|
|
196
233
|
};
|
|
197
234
|
}
|
|
198
|
-
function createObjValidator(
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
if (
|
|
205
|
-
return false;
|
|
206
|
-
|
|
207
|
-
|
|
235
|
+
function createObjValidator(packet) {
|
|
236
|
+
const types = packet.type, dataMaxes = packet.dataMax, dataMins = packet.dataMin;
|
|
237
|
+
const validators = types.map((t, i) => createValidator(t, dataMaxes[i], dataMins[i], packet));
|
|
238
|
+
return (data) => {
|
|
239
|
+
let index = 0, enums = 0, computedData = [];
|
|
240
|
+
while (index < data.length) {
|
|
241
|
+
if (computedData.length > types.length)
|
|
242
|
+
return false; // only types amount of values
|
|
243
|
+
const [off, sectorLength] = (0, CompressionUtil_1.readVarInt)(data, index);
|
|
244
|
+
index = off;
|
|
245
|
+
if (sectorLength + index > data.length)
|
|
208
246
|
return false;
|
|
209
|
-
|
|
210
|
-
|
|
247
|
+
const sector = data.subarray(index, index += sectorLength);
|
|
248
|
+
const result = validators[computedData.length](sector, types[computedData.length] == PacketType_1.PacketType.ENUMS ? enums++ : 0);
|
|
249
|
+
if (result === false)
|
|
250
|
+
return false; // chat i used === to fix a bug !!!!
|
|
251
|
+
computedData.push(result);
|
|
211
252
|
}
|
|
212
|
-
return
|
|
253
|
+
return computedData;
|
|
213
254
|
};
|
|
214
255
|
}
|
|
@@ -4,28 +4,32 @@ export declare enum PacketType {
|
|
|
4
4
|
NONE = 0,
|
|
5
5
|
/** Raw data */
|
|
6
6
|
RAW = 1,
|
|
7
|
-
/**
|
|
7
|
+
/** 8 byte string data (0-255 codes) */
|
|
8
|
+
STRINGS_ASCII = 2,
|
|
9
|
+
/** Code point UTF16 data; up to 0x10FFFF */
|
|
10
|
+
STRINGS_UTF16 = 3,
|
|
11
|
+
/** Strings; defaults to ASCII (0-255 codes). Use STRINGS_UTF16 for more codes. */
|
|
8
12
|
STRINGS = 2,
|
|
9
13
|
/** Constant primitive data; strings, numbers, booleans, null, undefined */
|
|
10
|
-
ENUMS =
|
|
11
|
-
/** One or more bytes; -128 to 127 */
|
|
12
|
-
BYTES =
|
|
14
|
+
ENUMS = 4,
|
|
15
|
+
/** One or more bytes; -128 to 127 | zig-zag encoded */
|
|
16
|
+
BYTES = 5,
|
|
13
17
|
/** One or more bytes; 0 to 255 */
|
|
14
|
-
UBYTES =
|
|
15
|
-
/** One or more
|
|
16
|
-
BYTES_ZZ = 6,
|
|
17
|
-
/** One or more shorts; -32,768 to 32,767 */
|
|
18
|
+
UBYTES = 6,
|
|
19
|
+
/** One or more shorts; -32,768 to 32,767 | zig-zag encoded */
|
|
18
20
|
SHORTS = 7,
|
|
19
21
|
/** One or more shorts; 0 to 65,535 */
|
|
20
22
|
USHORTS = 8,
|
|
21
|
-
/** One or more
|
|
22
|
-
|
|
23
|
-
/** One or more integers up to
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
|
|
23
|
+
/** One or more integers between -281,474,976,710,656 and 281,474,976,710,655 | zig-zag encoded */
|
|
24
|
+
VARINT = 9,
|
|
25
|
+
/** One or more integers up to 562,949,953,421,311. */
|
|
26
|
+
UVARINT = 10,
|
|
27
|
+
/** Var ints that use deltas; each value will show the difference from the last value. Good for close numbers */
|
|
28
|
+
DELTAS = 11,
|
|
27
29
|
/** One or more single precision floating point numbers. Only up to 7 digits of accuracy. */
|
|
28
|
-
|
|
30
|
+
FLOATS = 12,
|
|
31
|
+
/** One or more double precision floating point numbers. */
|
|
32
|
+
DOUBLES = 13,
|
|
29
33
|
/** One or more true/false */
|
|
30
|
-
BOOLEANS =
|
|
34
|
+
BOOLEANS = 14
|
|
31
35
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/*
|
|
3
|
-
* Copyright 2025 Lily (
|
|
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.
|
|
@@ -24,28 +24,32 @@ var PacketType;
|
|
|
24
24
|
PacketType[PacketType["NONE"] = 0] = "NONE";
|
|
25
25
|
/** Raw data */
|
|
26
26
|
PacketType[PacketType["RAW"] = 1] = "RAW";
|
|
27
|
-
/**
|
|
27
|
+
/** 8 byte string data (0-255 codes) */
|
|
28
|
+
PacketType[PacketType["STRINGS_ASCII"] = 2] = "STRINGS_ASCII";
|
|
29
|
+
/** Code point UTF16 data; up to 0x10FFFF */
|
|
30
|
+
PacketType[PacketType["STRINGS_UTF16"] = 3] = "STRINGS_UTF16";
|
|
31
|
+
/** Strings; defaults to ASCII (0-255 codes). Use STRINGS_UTF16 for more codes. */
|
|
28
32
|
PacketType[PacketType["STRINGS"] = 2] = "STRINGS";
|
|
29
33
|
/** Constant primitive data; strings, numbers, booleans, null, undefined */
|
|
30
|
-
PacketType[PacketType["ENUMS"] =
|
|
31
|
-
/** One or more bytes; -128 to 127 */
|
|
32
|
-
PacketType[PacketType["BYTES"] =
|
|
34
|
+
PacketType[PacketType["ENUMS"] = 4] = "ENUMS";
|
|
35
|
+
/** One or more bytes; -128 to 127 | zig-zag encoded */
|
|
36
|
+
PacketType[PacketType["BYTES"] = 5] = "BYTES";
|
|
33
37
|
/** One or more bytes; 0 to 255 */
|
|
34
|
-
PacketType[PacketType["UBYTES"] =
|
|
35
|
-
/** One or more
|
|
36
|
-
PacketType[PacketType["BYTES_ZZ"] = 6] = "BYTES_ZZ";
|
|
37
|
-
/** One or more shorts; -32,768 to 32,767 */
|
|
38
|
+
PacketType[PacketType["UBYTES"] = 6] = "UBYTES";
|
|
39
|
+
/** One or more shorts; -32,768 to 32,767 | zig-zag encoded */
|
|
38
40
|
PacketType[PacketType["SHORTS"] = 7] = "SHORTS";
|
|
39
41
|
/** One or more shorts; 0 to 65,535 */
|
|
40
42
|
PacketType[PacketType["USHORTS"] = 8] = "USHORTS";
|
|
41
|
-
/** One or more
|
|
42
|
-
PacketType[PacketType["
|
|
43
|
-
/** One or more integers up to
|
|
44
|
-
PacketType[PacketType["
|
|
45
|
-
/**
|
|
46
|
-
PacketType[PacketType["
|
|
43
|
+
/** One or more integers between -281,474,976,710,656 and 281,474,976,710,655 | zig-zag encoded */
|
|
44
|
+
PacketType[PacketType["VARINT"] = 9] = "VARINT";
|
|
45
|
+
/** One or more integers up to 562,949,953,421,311. */
|
|
46
|
+
PacketType[PacketType["UVARINT"] = 10] = "UVARINT";
|
|
47
|
+
/** Var ints that use deltas; each value will show the difference from the last value. Good for close numbers */
|
|
48
|
+
PacketType[PacketType["DELTAS"] = 11] = "DELTAS";
|
|
47
49
|
/** One or more single precision floating point numbers. Only up to 7 digits of accuracy. */
|
|
48
|
-
PacketType[PacketType["
|
|
50
|
+
PacketType[PacketType["FLOATS"] = 12] = "FLOATS";
|
|
51
|
+
/** One or more double precision floating point numbers. */
|
|
52
|
+
PacketType[PacketType["DOUBLES"] = 13] = "DOUBLES";
|
|
49
53
|
/** One or more true/false */
|
|
50
|
-
PacketType[PacketType["BOOLEANS"] =
|
|
54
|
+
PacketType[PacketType["BOOLEANS"] = 14] = "BOOLEANS";
|
|
51
55
|
})(PacketType || (exports.PacketType = PacketType = {}));
|
|
@@ -11,27 +11,26 @@ export declare class Packet {
|
|
|
11
11
|
enumData: EnumPackage[];
|
|
12
12
|
dataMax: number | number[];
|
|
13
13
|
dataMin: number | number[];
|
|
14
|
-
packetDelimitSize: number;
|
|
15
14
|
dataBatching: number;
|
|
16
15
|
maxBatchSize: number;
|
|
17
16
|
dontSpread: boolean;
|
|
18
17
|
autoFlatten: boolean;
|
|
19
18
|
rateLimit: number;
|
|
20
19
|
object: boolean;
|
|
20
|
+
client: boolean;
|
|
21
21
|
private receiveProcessor;
|
|
22
22
|
private sendProcessor;
|
|
23
|
-
private
|
|
24
|
-
processReceive: (data: Uint8Array) => any;
|
|
23
|
+
private validator;
|
|
24
|
+
processReceive: (data: Uint8Array, validationResult: any) => any;
|
|
25
25
|
processSend: (data: any[]) => number[];
|
|
26
26
|
validate: (data: Uint8Array) => boolean;
|
|
27
27
|
customValidator: ((socket: SonicWSConnection, ...values: any[]) => boolean) | null;
|
|
28
28
|
constructor(tag: string, schema: PacketSchema, customValidator: ValidatorFunction, enabled: boolean, client: boolean);
|
|
29
29
|
listen(value: Uint8Array, socket: SonicWSConnection | null): [processed: any, flatten: boolean] | string;
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
static
|
|
34
|
-
static deserializeAll(text: string, client: boolean): Packet[];
|
|
30
|
+
serialize(): number[];
|
|
31
|
+
private static readVarInts;
|
|
32
|
+
static deserialize(data: Uint8Array, offset: number, client: boolean): [packet: Packet, offset: number];
|
|
33
|
+
static deserializeAll(data: Uint8Array, client: boolean): Packet[];
|
|
35
34
|
}
|
|
36
35
|
export declare class PacketSchema {
|
|
37
36
|
types: PacketType[];
|
|
@@ -40,7 +39,6 @@ export declare class PacketSchema {
|
|
|
40
39
|
type: PacketType;
|
|
41
40
|
dataMax: number;
|
|
42
41
|
dataMin: number;
|
|
43
|
-
packetDelimitSize: number;
|
|
44
42
|
dataBatching: number;
|
|
45
43
|
maxBatchSize: number;
|
|
46
44
|
rateLimit: number;
|
|
@@ -50,5 +48,5 @@ export declare class PacketSchema {
|
|
|
50
48
|
object: boolean;
|
|
51
49
|
constructor(object: boolean);
|
|
52
50
|
static single(type: PacketType | EnumPackage, dataMax: number, dataMin: number, dontSpread: boolean, dataBatching: number, maxBatchSize: number, rateLimit: number): PacketSchema;
|
|
53
|
-
static object(types: (PacketType | EnumPackage)[], dataMaxes: number[], dataMins: number[], dontSpread: boolean, autoFlatten: boolean,
|
|
51
|
+
static object(types: (PacketType | EnumPackage)[], dataMaxes: number[], dataMins: number[], dontSpread: boolean, autoFlatten: boolean, dataBatching: number, maxBatchSize: number, rateLimit: number): PacketSchema;
|
|
54
52
|
}
|