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.
@@ -14,207 +14,265 @@
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.PacketSendProcessors = exports.PacketReceiveProcessors = exports.PacketValidityProcessors = void 0;
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
  var ArrayUtil_1 = require("../util/ArrayUtil");
24
- var CodePointUtil_1 = require("../util/packets/CodePointUtil");
25
+ var CompressionUtil_1 = require("../util/packets/CompressionUtil");
25
26
  var PacketType_1 = require("./PacketType");
26
- var LEN_DELIMIT = function (data, cap, min) {
27
- var sectors = 0;
28
- for (var index = 0; index < data.length; index++) {
29
- sectors++;
30
- if (sectors > cap)
31
- return false;
32
- index += data.charCodeAt(index);
33
- if (index + 1 > data.length)
34
- return false;
35
- }
36
- if (sectors < min)
37
- return false;
38
- return true;
39
- };
40
- var INT_C_LEN = function (data, cap, min) { return data.length >= min && data.length <= cap && (0, CodePointUtil_1.processCharCodes)(data).find(function (v) { return v > CodePointUtil_1.MAX_C; }) == null; };
41
- var INT_D_LIKE = function (raw, cap, min, off) {
42
- if (raw.length == 0)
43
- return false;
44
- var dataLength = raw.length - 1, sectSize = raw.charCodeAt(0) + off;
45
- if (dataLength % sectSize != 0)
46
- return false;
47
- var valueAmount = dataLength / sectSize;
48
- if (valueAmount < min || valueAmount > cap)
49
- return false;
50
- return true;
51
- };
52
- // 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
53
- exports.PacketValidityProcessors = (_a = {},
54
- _a[PacketType_1.PacketType.NONE] = function (data) { return data.length == 0; },
55
- _a[PacketType_1.PacketType.RAW] = function () { return true; },
56
- _a[PacketType_1.PacketType.STRINGS] = LEN_DELIMIT,
57
- _a[PacketType_1.PacketType.ENUMS] = function (data, cap, min, packet, index) {
58
- if (data.length < min || data.length > cap || index >= packet.enumData.length)
27
+ var BufferUtil_1 = require("../util/BufferUtil");
28
+ var StringUtil_1 = require("../util/StringUtil");
29
+ function BYTE_LEN(cap, min) {
30
+ return function (data) { return data.length >= min && data.length <= cap; };
31
+ }
32
+ function SHORT_LEN(cap, min) {
33
+ min *= 2;
34
+ cap *= 2;
35
+ return function (data) { return data.length >= min && data.length <= cap && data.length % 2 == 0; };
36
+ }
37
+ function VARINT_VERIF(cap, min, signed) {
38
+ return function (data) {
39
+ if (data.length == 0)
59
40
  return false;
60
- var pkg = packet.enumData[index];
61
- for (var i = 0; i < data.length; i++) {
62
- if (pkg.values.length <= data.charCodeAt(i))
41
+ var sectors = 0, i = 0, computed = [];
42
+ while (i < data.length) {
43
+ var _a = (0, CompressionUtil_1.readVarInt)(data, i, signed), off = _a[0], varint = _a[1];
44
+ i = off;
45
+ computed.push(varint);
46
+ if (++sectors > cap)
63
47
  return false;
64
48
  }
65
- return true;
66
- },
67
- _a[PacketType_1.PacketType.INTS_C] = INT_C_LEN,
68
- _a[PacketType_1.PacketType.UINTS_C] = INT_C_LEN,
69
- _a[PacketType_1.PacketType.ZIG_ZAG] = INT_C_LEN,
70
- _a[PacketType_1.PacketType.INTS_D] = function (raw, cap, min) { return INT_D_LIKE(raw, cap, min, 0); },
71
- _a[PacketType_1.PacketType.INTS_A] = LEN_DELIMIT,
72
- _a[PacketType_1.PacketType.EXPONENTIAL] = function (raw, cap, min) { return INT_D_LIKE(raw, cap, min, 1); },
73
- _a[PacketType_1.PacketType.DECIMALS] = function (data, cap, min) {
74
- var sectors = 0;
75
- for (var i = 0; i < data.length;) {
76
- sectors++;
77
- if (sectors > cap)
49
+ if (sectors < min)
50
+ return false;
51
+ return computed;
52
+ };
53
+ }
54
+ ;
55
+ function createValidator(type, dataCap, dataMin, enumData) {
56
+ switch (type) {
57
+ case PacketType_1.PacketType.NONE: return function (data) { return data.length == 0; };
58
+ case PacketType_1.PacketType.RAW: return function () { return undefined; };
59
+ case PacketType_1.PacketType.ENUMS: return function (data, index) {
60
+ if (data.length < dataMin || data.length > dataCap || index >= enumData.length)
78
61
  return false;
79
- var sectorBits = data.charCodeAt(i++);
80
- var len = sectorBits >> 7;
81
- var len2 = sectorBits & 0x7F;
82
- i += len;
83
- if (i > data.length)
62
+ var pkg = enumData[index];
63
+ for (var i = 0; i < data.length; i++) {
64
+ if (pkg.values.length <= data[i])
65
+ return false;
66
+ }
67
+ return undefined;
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.BYTES_ZZ: return BYTE_LEN(dataCap, dataMin);
72
+ case PacketType_1.PacketType.SHORTS: return SHORT_LEN(dataCap, dataMin);
73
+ case PacketType_1.PacketType.USHORTS: return SHORT_LEN(dataCap, dataMin);
74
+ case PacketType_1.PacketType.SHORTS_ZZ: return SHORT_LEN(dataCap, dataMin);
75
+ case PacketType_1.PacketType.NUMBERS: return function (raw) {
76
+ if (raw.length == 0)
84
77
  return false;
85
- i += len2;
86
- if (i > data.length)
78
+ var sectSize = raw[0];
79
+ if (sectSize > CompressionUtil_1.MAX_DSECT_SIZE)
87
80
  return false;
88
- }
89
- if (sectors < min)
90
- return false;
91
- return true;
92
- },
93
- _a[PacketType_1.PacketType.BOOLEANS] = function (data, cap, min) {
94
- var codes = (0, CodePointUtil_1.processCharCodes)(data);
95
- return codes.length >= Math.floor(min / 8) + 1 && codes.length <= Math.floor(cap / 8) + 1 && codes.find(function (d) { return d > 255; }) == undefined;
96
- },
97
- _a);
98
- exports.PacketReceiveProcessors = (_b = {},
99
- _b[PacketType_1.PacketType.NONE] = function (_) { return ""; },
100
- _b[PacketType_1.PacketType.RAW] = function (data) { return data; },
101
- _b[PacketType_1.PacketType.STRINGS] = function (data) {
102
- var strings = [];
103
- for (var i = 0; i < data.length; i++) {
104
- var stringSize = data.charCodeAt(i);
105
- strings.push(data.substring(i + 1, i + 1 + stringSize));
106
- i += stringSize;
107
- }
108
- return strings;
109
- },
110
- _b[PacketType_1.PacketType.ENUMS] = function (data, _, packet, index) {
111
- var pkg = packet.enumData[index];
112
- return (0, CodePointUtil_1.processCharCodes)(data).map(function (code) { return pkg.values[code]; });
113
- },
114
- _b[PacketType_1.PacketType.INTS_C] = function (data) { return (0, CodePointUtil_1.processCharCodes)(data).map(CodePointUtil_1.fromSignedINT_C); },
115
- _b[PacketType_1.PacketType.UINTS_C] = function (data) { return (0, CodePointUtil_1.processCharCodes)(data); },
116
- _b[PacketType_1.PacketType.ZIG_ZAG] = function (data) { return (0, CodePointUtil_1.processCharCodes)(data).map(CodePointUtil_1.demapZIG_ZAG); },
117
- _b[PacketType_1.PacketType.INTS_D] = function (data) { return (0, ArrayUtil_1.splitArray)(data.substring(1), data.charCodeAt(0)).map(CodePointUtil_1.deconvertINT_D); },
118
- _b[PacketType_1.PacketType.INTS_A] = function (data) {
119
- var numbers = [];
120
- for (var i = 0; i < data.length; i++) {
121
- var sectSize = data.charCodeAt(i);
122
- numbers.push((0, CodePointUtil_1.deconvertINT_D)(data.substring(i + 1, i + 1 + sectSize)));
123
- i += sectSize;
124
- }
125
- return numbers;
126
- },
127
- _b[PacketType_1.PacketType.EXPONENTIAL] = function (data) { return (0, ArrayUtil_1.splitArray)(data.substring(1), data.charCodeAt(0) + 1).map(CodePointUtil_1.deconvertINT_E); },
128
- _b[PacketType_1.PacketType.DECIMALS] = function (data) {
129
- var points = (0, CodePointUtil_1.processCharCodes)(data);
130
- var numbers = [];
131
- for (var i = 0; i < points.length;) {
132
- var sectorBits = points[i++];
133
- var wholeSS = sectorBits >> 7;
134
- var decimalSS = sectorBits & 0x7F;
135
- var whole = (0, CodePointUtil_1.deconvertINT_DCodes)(points.slice(i, i + wholeSS));
136
- i += wholeSS;
137
- var decimal = (0, CodePointUtil_1.deconvertINT_DCodes)(points.slice(i, i + decimalSS));
138
- i += decimalSS;
139
- numbers.push(parseFloat(whole + "." + decimal));
140
- }
141
- return numbers;
142
- },
143
- _b[PacketType_1.PacketType.BOOLEANS] = function (data, cap) { return (0, CodePointUtil_1.processCharCodes)(data).map(function (d) { return (0, CodePointUtil_1.decompressBools)(d); }).flat().splice(0, cap); },
144
- _b);
145
- exports.PacketSendProcessors = (_c = {},
146
- _c[PacketType_1.PacketType.NONE] = function () { return ""; },
147
- _c[PacketType_1.PacketType.RAW] = function (data) { return data.map(String).join(""); },
148
- // todo: try some kind of string compression ig :p
149
- _c[PacketType_1.PacketType.STRINGS] = function (strings) { return strings.map(function (string) { return String.fromCharCode(string.toString().length) + string; }).join(""); },
150
- _c[PacketType_1.PacketType.ENUMS] = function (enums) { return enums.join(""); },
151
- _c[PacketType_1.PacketType.INTS_C] = function (numbers) { return numbers.map(CodePointUtil_1.stringedINT_C).join(""); },
152
- _c[PacketType_1.PacketType.UINTS_C] = function (numbers) { return String.fromCharCode.apply(String, numbers); },
153
- _c[PacketType_1.PacketType.ZIG_ZAG] = function (numbers) { return String.fromCharCode.apply(String, numbers.map(CodePointUtil_1.mapZIG_ZAG)); },
154
- _c[PacketType_1.PacketType.INTS_D] = function (numbers) {
155
- var sectSize = numbers.reduce(function (c, n) { return Math.max(c, (0, CodePointUtil_1.sectorSize)(n)); }, 1);
156
- var sects = numbers.map(function (n) { return (0, CodePointUtil_1.convertINT_D)(n, sectSize); });
157
- return String.fromCharCode(sectSize) + sects.join("");
158
- },
159
- _c[PacketType_1.PacketType.INTS_A] = function (numbers) { return numbers.map(function (v) {
160
- var sectSize = (0, CodePointUtil_1.sectorSize)(v);
161
- return String.fromCharCode(sectSize) + (0, CodePointUtil_1.convertINT_D)(v, sectSize);
162
- }).join(""); },
163
- _c[PacketType_1.PacketType.EXPONENTIAL] = function (numbers) { return (0, CodePointUtil_1.convertINT_Es)(numbers); },
164
- _c[PacketType_1.PacketType.DECIMALS] = function (numbers) { return numbers.map(function (n) {
165
- var split = n.toString().split(".");
166
- var whole = parseFloat(split[0]) || 0;
167
- var decimal = split.length > 1 ? parseFloat(split[1]) || 0 : 0;
168
- var wholeSS = (0, CodePointUtil_1.sectorSize)(whole);
169
- var decimalSS = (0, CodePointUtil_1.sectorSize)(decimal);
170
- var num = (wholeSS << 7) | decimalSS;
171
- return String.fromCharCode(num) + (0, CodePointUtil_1.convertINT_D)(whole, wholeSS) + (0, CodePointUtil_1.convertINT_D)(decimal, decimalSS);
172
- }).join(""); },
173
- _c[PacketType_1.PacketType.BOOLEANS] = function (bools) { return (0, ArrayUtil_1.splitArray)(bools, 7).map(function (bools) { return String.fromCharCode((0, CodePointUtil_1.compressBools)(bools)); }).join(""); },
174
- _c);
175
- // so uhm. it work. sorry-
176
- function createObjSendProcessor(types, packetDelimitSize) {
81
+ var dataLength = raw.length - 1;
82
+ if (dataLength % sectSize != 0)
83
+ return false;
84
+ var valueAmount = dataLength / sectSize;
85
+ if (valueAmount < dataMin || valueAmount > dataCap)
86
+ return false;
87
+ return undefined;
88
+ };
89
+ case PacketType_1.PacketType.VARINT: return VARINT_VERIF(dataCap, dataMin, true);
90
+ case PacketType_1.PacketType.UVARINT: return VARINT_VERIF(dataCap, dataMin, false);
91
+ case PacketType_1.PacketType.VARINT_ZZ: return VARINT_VERIF(dataCap, dataMin, false);
92
+ case PacketType_1.PacketType.DELTAS: return VARINT_VERIF(dataCap, dataMin, false);
93
+ case PacketType_1.PacketType.FLOAT: return function (data) {
94
+ if (data.length % 4 !== 0)
95
+ return false;
96
+ var sectors = data.length / 4;
97
+ if (sectors > dataCap || sectors < dataMin)
98
+ return false;
99
+ return undefined;
100
+ };
101
+ case PacketType_1.PacketType.BOOLEANS:
102
+ {
103
+ var min_1 = Math.floor(dataMin / 8);
104
+ var cap_1 = Math.floor(dataCap / 8);
105
+ return function (data) { return data.length >= min_1 && data.length <= cap_1; };
106
+ }
107
+ ;
108
+ case PacketType_1.PacketType.STRINGS_ASCII: return function (data) {
109
+ var sectors = 0, index = 0, computed = [];
110
+ while (index < data.length) {
111
+ sectors++;
112
+ if (sectors > dataCap)
113
+ return false;
114
+ var _a = (0, CompressionUtil_1.readVarInt)(data, index, false), off = _a[0], varint = _a[1];
115
+ index = off + varint;
116
+ computed.push(varint);
117
+ if (index > data.length)
118
+ return false;
119
+ }
120
+ if (sectors < dataMin)
121
+ return false;
122
+ return computed; // todo
123
+ };
124
+ case PacketType_1.PacketType.STRINGS_UTF16: return function (data) {
125
+ var sectors = 0, index = 0, computed = [];
126
+ while (index < data.length) {
127
+ sectors++;
128
+ if (sectors > dataCap)
129
+ return false;
130
+ var _a = (0, CompressionUtil_1.readVarInt)(data, index, false), off = _a[0], varint = _a[1];
131
+ index = off + varint * 2;
132
+ computed.push(varint);
133
+ if (index > data.length)
134
+ return false;
135
+ }
136
+ if (sectors < dataMin)
137
+ return false;
138
+ return computed; // todo
139
+ };
140
+ default: throw new Error("Unknown type: " + type);
141
+ }
142
+ }
143
+ function createReceiveProcessor(type, enumData, cap) {
144
+ switch (type) {
145
+ case PacketType_1.PacketType.NONE: return function () { return undefined; };
146
+ case PacketType_1.PacketType.RAW: return function (data) { return data; };
147
+ case PacketType_1.PacketType.BYTES: return function (data) { return Array.from(data).map(CompressionUtil_1.fromSignedByte); };
148
+ case PacketType_1.PacketType.UBYTES: return function (data) { return Array.from(data); };
149
+ case PacketType_1.PacketType.BYTES_ZZ: return function (data) { return Array.from(data).map(CompressionUtil_1.demapZigZag); };
150
+ case PacketType_1.PacketType.SHORTS: return function (data) { return (0, BufferUtil_1.splitBuffer)(data, 2).map(function (v) { return (0, CompressionUtil_1.fromSignedShort)(v); }); };
151
+ case PacketType_1.PacketType.USHORTS: return function (data) { return (0, BufferUtil_1.splitBuffer)(data, 2).map(function (v) { return (0, CompressionUtil_1.fromShort)(v); }); };
152
+ case PacketType_1.PacketType.SHORTS_ZZ: return function (data) { return (0, BufferUtil_1.splitBuffer)(data, 2).map(function (v) { return (0, CompressionUtil_1.demapShort_ZZ)(v); }); };
153
+ case PacketType_1.PacketType.VARINT: return function (_, computed) { return computed; };
154
+ case PacketType_1.PacketType.UVARINT: return function (_, computed) { return computed; };
155
+ case PacketType_1.PacketType.VARINT_ZZ: return function (_, computed) { return computed.map(CompressionUtil_1.demapZigZag); };
156
+ case PacketType_1.PacketType.DELTAS: return function (_, computed) { return computed.map(function (x, i) { return computed[i] = (computed[i - 1] || 0) + (0, CompressionUtil_1.demapZigZag)(x); }); };
157
+ case PacketType_1.PacketType.FLOAT: return function (data) { return (0, BufferUtil_1.splitBuffer)(data, 4).map(CompressionUtil_1.deconvertFloat); };
158
+ case PacketType_1.PacketType.BOOLEANS: return function (data) { return Array.from(data).map(function (d) { return (0, CompressionUtil_1.decompressBools)(d); }).flat().splice(0, cap); };
159
+ case PacketType_1.PacketType.NUMBERS: return function (data) { return (0, ArrayUtil_1.splitArray)(data.subarray(1), data[0]).map(CompressionUtil_1.deconvertBytePows); };
160
+ case PacketType_1.PacketType.ENUMS: return function (data, _, index) {
161
+ var pkg = enumData[index];
162
+ return Array.from(data).map(function (code) { return pkg.values[code]; });
163
+ };
164
+ case PacketType_1.PacketType.STRINGS_ASCII: return function (data, computed) {
165
+ var off = 0;
166
+ return computed.map(function (len) { return (0, BufferUtil_1.as8String)(data.subarray(++off, off += len)); });
167
+ };
168
+ case PacketType_1.PacketType.STRINGS_UTF16: return function (data, computed) {
169
+ var off = 0;
170
+ return computed.map(function (len) { return (0, BufferUtil_1.as16String)(data.subarray(++off, off += len * 2)); });
171
+ };
172
+ default: throw new Error("Unknown type: " + type);
173
+ }
174
+ }
175
+ /** Creates a function that processes a packet type */
176
+ function createSendProcessor(type) {
177
+ switch (type) {
178
+ case PacketType_1.PacketType.NONE: return function () { return []; };
179
+ case PacketType_1.PacketType.RAW: return function (data) { return Array.from(data); };
180
+ case PacketType_1.PacketType.ENUMS: return function (enums) { return enums; };
181
+ case PacketType_1.PacketType.BYTES: return function (numbers) { return numbers.map(CompressionUtil_1.toSignedByte); };
182
+ case PacketType_1.PacketType.UBYTES: return function (numbers) { return numbers.map(function (n) { return (0, CompressionUtil_1.toByte)(n, false); }); };
183
+ case PacketType_1.PacketType.BYTES_ZZ: return function (numbers) { return numbers.map(CompressionUtil_1.mapZigZag); };
184
+ case PacketType_1.PacketType.SHORTS: return function (numbers) { return numbers.map(CompressionUtil_1.toSignedShort).flat(); };
185
+ case PacketType_1.PacketType.USHORTS: return function (numbers) { return numbers.map(function (n) { return (0, CompressionUtil_1.toShort)(n, false); }).flat(); };
186
+ case PacketType_1.PacketType.SHORTS_ZZ: return function (numbers) { return numbers.map(CompressionUtil_1.mapShort_ZZ).flat(); };
187
+ case PacketType_1.PacketType.VARINT: return function (numbers) { return numbers.map(function (n) { return (0, CompressionUtil_1.convertVarInt)(n, true); }).flat(); };
188
+ case PacketType_1.PacketType.UVARINT: return function (numbers) { return numbers.map(function (n) { return (0, CompressionUtil_1.convertVarInt)(n, false); }).flat(); };
189
+ case PacketType_1.PacketType.VARINT_ZZ: return function (numbers) { return numbers.map(function (n) { return (0, CompressionUtil_1.convertVarInt)((0, CompressionUtil_1.mapZigZag)(n), false); }).flat(); };
190
+ case PacketType_1.PacketType.DELTAS: return function (numbers) { return numbers.map(function (n, i) { return (0, CompressionUtil_1.convertVarInt)((0, CompressionUtil_1.mapZigZag)(n - (numbers[i - 1] || 0)), false); }).flat(); };
191
+ case PacketType_1.PacketType.FLOAT: return function (floats) { return floats.map(CompressionUtil_1.convertFloat).flat(); };
192
+ case PacketType_1.PacketType.BOOLEANS: return function (bools) { return (0, ArrayUtil_1.splitArray)(bools, 8).map(function (bools) { return (0, CompressionUtil_1.compressBools)(bools); }).flat(); };
193
+ case PacketType_1.PacketType.NUMBERS: return function (numbers) {
194
+ var res = [];
195
+ var sectSize = numbers.reduce(function (c, n) { return Math.max(c, (0, CompressionUtil_1.sectorSize)(n, CompressionUtil_1.byteOverflowPow)); }, 1);
196
+ res.push(sectSize);
197
+ numbers.forEach(function (n) { return (0, CompressionUtil_1.convertBytePows)(n, sectSize).forEach(function (c) { return res.push(c); }); });
198
+ return res;
199
+ };
200
+ case PacketType_1.PacketType.STRINGS_ASCII: return function (strings) {
201
+ var res = [];
202
+ for (var _i = 0, strings_1 = strings; _i < strings_1.length; _i++) {
203
+ var v = strings_1[_i];
204
+ var string = String(v);
205
+ res.push.apply(res, (0, CompressionUtil_1.convertVarInt)(string.length, false));
206
+ var codes = (0, StringUtil_1.processCharCodes)(string);
207
+ var highCode = codes.find(function (x) { return x > CompressionUtil_1.MAX_BYTE; });
208
+ if (highCode)
209
+ throw new Error("Cannot store code ".concat(highCode, " (").concat(String.fromCharCode(highCode), ") in a UTF-8 String! Use STRINGS_UTF16."));
210
+ codes.map(function (c) { return res.push(c); });
211
+ }
212
+ return res;
213
+ };
214
+ case PacketType_1.PacketType.STRINGS_UTF16: return function (strings) {
215
+ var res = [];
216
+ for (var _i = 0, strings_2 = strings; _i < strings_2.length; _i++) {
217
+ var v = strings_2[_i];
218
+ var string = String(v);
219
+ res.push.apply(res, (0, CompressionUtil_1.convertVarInt)(string.length, false));
220
+ (0, StringUtil_1.processCharCodes)(string).map(function (c) { return res.push.apply(res, (0, StringUtil_1.splitCodePoint)(c).map(function (p) { return (0, CompressionUtil_1.toShort)(p, false); }).flat()); });
221
+ }
222
+ return res;
223
+ };
224
+ default: throw new Error("Unknown type: " + type);
225
+ }
226
+ }
227
+ function createObjSendProcessor(packet) {
228
+ var types = packet.type;
177
229
  var size = types.length;
178
- var processors = types.map(function (t) { return exports.PacketSendProcessors[t]; });
230
+ var processors = types.map(function (t) { return createSendProcessor(t); });
179
231
  return function (data) {
180
- var result = "";
232
+ var result = [];
181
233
  for (var i = 0; i < size; i++) {
182
234
  var sectorData = data[i];
183
235
  var d = processors[i](Array.isArray(sectorData) ? sectorData : [sectorData]);
184
- if (d.length > (0, CodePointUtil_1.overflowPow)(packetDelimitSize))
185
- throw new Error("Cannot store ".concat(d.length, " bytes of data! Set largePacket: true on the object!"));
186
- result += (0, CodePointUtil_1.convertINT_D)(d.length, packetDelimitSize) + d;
236
+ if (d.length > CompressionUtil_1.MAX_UVARINT)
237
+ throw new Error("Cannot send ".concat(d.length, "/").concat(CompressionUtil_1.MAX_UVARINT, " bytes of data!"));
238
+ result.push.apply(result, (0, CompressionUtil_1.convertVarInt)(d.length, false));
239
+ d.forEach(function (val) { return result.push(val); });
187
240
  }
188
241
  return result;
189
242
  };
190
243
  }
191
- function createObjReceiveProcessor(types, packetDelimitSize) {
192
- var processors = types.map(function (t) { return exports.PacketReceiveProcessors[t]; });
193
- return function (data, dataCaps, packet) {
194
- var result = [];
195
- var enums = 0;
196
- for (var i = 0; i < data.length;) {
197
- var sectorLength = (0, CodePointUtil_1.deconvertINT_D)(data.slice(i, i += packetDelimitSize));
198
- var sector = data.slice(i, i += sectorLength);
199
- result.push(processors[result.length](sector, dataCaps[result.length], packet, types[result.length] == PacketType_1.PacketType.ENUMS ? enums++ : 0));
244
+ function createObjReceiveProcessor(packet) {
245
+ var types = packet.type, dataMaxes = packet.dataMax;
246
+ var processors = types.map(function (t, i) { return createReceiveProcessor(t, packet.enumData, dataMaxes[i]); });
247
+ return function (data, validationResult) {
248
+ var index = 0, enums = 0, result = [];
249
+ while (index < data.length) {
250
+ var _a = (0, CompressionUtil_1.readVarInt)(data, index, false), off = _a[0], sectorLength = _a[1];
251
+ index = off;
252
+ var sector = data.subarray(index, index += sectorLength);
253
+ result.push(processors[result.length](sector, validationResult[result.length], types[result.length] == PacketType_1.PacketType.ENUMS ? enums++ : 0));
200
254
  }
201
255
  return result;
202
256
  };
203
257
  }
204
- function createObjValidator(types, packetDelimitSize) {
205
- var validators = types.map(function (t) { return exports.PacketValidityProcessors[t]; });
206
- return function (data, dataCaps, dataMins, packet) {
207
- var sectors = 0, enums = 0;
208
- for (var i = 0; i < data.length;) {
209
- var sectorLength = (0, CodePointUtil_1.deconvertINT_D)(data.slice(i, i += packetDelimitSize));
210
- if (sectorLength + i > data.length)
258
+ function createObjValidator(packet) {
259
+ var types = packet.type, dataMaxes = packet.dataMax, dataMins = packet.dataMin;
260
+ var validators = types.map(function (t, i) { return createValidator(t, dataMaxes[i], dataMins[i], packet.enumData); });
261
+ return function (data) {
262
+ var index = 0, enums = 0, computedData = [];
263
+ while (index < data.length) {
264
+ if (computedData.length > types.length)
265
+ return false; // only types amount of values
266
+ var _a = (0, CompressionUtil_1.readVarInt)(data, index, false), off = _a[0], sectorLength = _a[1];
267
+ index = off;
268
+ if (sectorLength + index > data.length)
211
269
  return false;
212
- var sector = data.slice(i, i += sectorLength);
213
- if (!validators[sectors](sector, dataCaps[sectors], dataMins[sectors], packet, types[sectors] == PacketType_1.PacketType.ENUMS ? enums++ : 0))
270
+ var sector = data.subarray(index, index += sectorLength);
271
+ var result = validators[computedData.length](sector, types[computedData.length] == PacketType_1.PacketType.ENUMS ? enums++ : 0);
272
+ if (result == false)
214
273
  return false;
215
- if (++sectors > dataCaps.length)
216
- return false; // caps length is also the amount of values there are
274
+ computedData.push(result);
217
275
  }
218
- return true;
276
+ return computedData;
219
277
  };
220
278
  }
@@ -4,24 +4,38 @@ export declare enum PacketType {
4
4
  NONE = 0,
5
5
  /** Raw data */
6
6
  RAW = 1,
7
- /** String data; use ENUMS if the values are constant */
7
+ /** 8 byte string data (0-255 codes) */
8
+ STRINGS_ASCII = 2,
9
+ /** 16 byte string data (0-65535 codes) */
10
+ STRINGS_UTF16 = 3,
11
+ /** Strings; defaults to UTF8 (0-255 codes) */
8
12
  STRINGS = 2,
9
13
  /** Constant primitive data; strings, numbers, booleans, null, undefined */
10
- ENUMS = 3,
11
- /** One or more integers from -27,648 to 27,647 */
12
- INTS_C = 4,
13
- /** One or more positive integers from 0 to 55,295 */
14
- UINTS_C = 5,
15
- /** One or more integers from -27,648 to 27,647, but zig-zag encoded for small negatives (good for deltas; maps like -1->1, 1->2, -2->3, 2->4, etc.) */
16
- ZIG_ZAG = 6,
14
+ ENUMS = 4,
15
+ /** One or more bytes; -128 to 127 */
16
+ BYTES = 5,
17
+ /** One or more bytes; 0 to 255 */
18
+ UBYTES = 6,
19
+ /** One or more bytes; -128 to 127 | zig-zag encoded for small negatives (good for deltas; maps like -1->1, 1->2, -2->3, 2->4, etc.) */
20
+ BYTES_ZZ = 7,
21
+ /** One or more shorts; -32,768 to 32,767 */
22
+ SHORTS = 8,
23
+ /** One or more shorts; 0 to 65,535 */
24
+ USHORTS = 9,
25
+ /** One or more shorts; -32,768 to 32,767 | zig-zag encoded for small negatives (good for deltas; maps like -1->1, 1->2, -2->3, 2->4, etc.) */
26
+ SHORTS_ZZ = 10,
17
27
  /** One or more integers up to MAX_SAFE_INTEGER | 9,007,199,254,740,991 (or negative). Similar maximum size will produce maximum efficiency */
18
- INTS_D = 7,
19
- /** One or more integers up to MAX_SAFE_INTEGER | 9,007,199,2554,740,991 (or negative). More efficient for differently sized numbers, but worse than INTS_D for similar sized numbers. */
20
- INTS_A = 8,
21
- /** One or more numbers up to MAX_VALUE or 1.7976931348623157e+308. */
22
- EXPONENTIAL = 9,
23
- /** One or more numbers of any size */
24
- DECIMALS = 10,
28
+ NUMBERS = 11,
29
+ /** One or more integers between -281,474,976,710,656 and 281,474,976,710,655. */
30
+ VARINT = 12,
31
+ /** One or more integers up to 562,949,953,421,311. */
32
+ UVARINT = 13,
33
+ /** One or more integers between -281,474,976,710,656 and 281,474,976,710,655 | zig-zag encoded (good for deltas; maps like -1->1, 1->2, -2->3, 2->4, etc.) */
34
+ VARINT_ZZ = 14,
35
+ /** Var ints that use deltas; each value will show the difference from the last value. Good for close numbers */
36
+ DELTAS = 15,
37
+ /** One or more single precision floating point numbers. Only up to 7 digits of accuracy. */
38
+ FLOAT = 16,
25
39
  /** One or more true/false */
26
- BOOLEANS = 11
40
+ BOOLEANS = 17
27
41
  }
@@ -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.
@@ -24,24 +24,38 @@ var PacketType;
24
24
  PacketType[PacketType["NONE"] = 0] = "NONE";
25
25
  /** Raw data */
26
26
  PacketType[PacketType["RAW"] = 1] = "RAW";
27
- /** String data; use ENUMS if the values are constant */
27
+ /** 8 byte string data (0-255 codes) */
28
+ PacketType[PacketType["STRINGS_ASCII"] = 2] = "STRINGS_ASCII";
29
+ /** 16 byte string data (0-65535 codes) */
30
+ PacketType[PacketType["STRINGS_UTF16"] = 3] = "STRINGS_UTF16";
31
+ /** Strings; defaults to UTF8 (0-255 codes) */
28
32
  PacketType[PacketType["STRINGS"] = 2] = "STRINGS";
29
33
  /** Constant primitive data; strings, numbers, booleans, null, undefined */
30
- PacketType[PacketType["ENUMS"] = 3] = "ENUMS";
31
- /** One or more integers from -27,648 to 27,647 */
32
- PacketType[PacketType["INTS_C"] = 4] = "INTS_C";
33
- /** One or more positive integers from 0 to 55,295 */
34
- PacketType[PacketType["UINTS_C"] = 5] = "UINTS_C";
35
- /** One or more integers from -27,648 to 27,647, but zig-zag encoded for small negatives (good for deltas; maps like -1->1, 1->2, -2->3, 2->4, etc.) */
36
- PacketType[PacketType["ZIG_ZAG"] = 6] = "ZIG_ZAG";
34
+ PacketType[PacketType["ENUMS"] = 4] = "ENUMS";
35
+ /** One or more bytes; -128 to 127 */
36
+ PacketType[PacketType["BYTES"] = 5] = "BYTES";
37
+ /** One or more bytes; 0 to 255 */
38
+ PacketType[PacketType["UBYTES"] = 6] = "UBYTES";
39
+ /** One or more bytes; -128 to 127 | zig-zag encoded for small negatives (good for deltas; maps like -1->1, 1->2, -2->3, 2->4, etc.) */
40
+ PacketType[PacketType["BYTES_ZZ"] = 7] = "BYTES_ZZ";
41
+ /** One or more shorts; -32,768 to 32,767 */
42
+ PacketType[PacketType["SHORTS"] = 8] = "SHORTS";
43
+ /** One or more shorts; 0 to 65,535 */
44
+ PacketType[PacketType["USHORTS"] = 9] = "USHORTS";
45
+ /** One or more shorts; -32,768 to 32,767 | zig-zag encoded for small negatives (good for deltas; maps like -1->1, 1->2, -2->3, 2->4, etc.) */
46
+ PacketType[PacketType["SHORTS_ZZ"] = 10] = "SHORTS_ZZ";
37
47
  /** One or more integers up to MAX_SAFE_INTEGER | 9,007,199,254,740,991 (or negative). Similar maximum size will produce maximum efficiency */
38
- PacketType[PacketType["INTS_D"] = 7] = "INTS_D";
39
- /** One or more integers up to MAX_SAFE_INTEGER | 9,007,199,2554,740,991 (or negative). More efficient for differently sized numbers, but worse than INTS_D for similar sized numbers. */
40
- PacketType[PacketType["INTS_A"] = 8] = "INTS_A";
41
- /** One or more numbers up to MAX_VALUE or 1.7976931348623157e+308. */
42
- PacketType[PacketType["EXPONENTIAL"] = 9] = "EXPONENTIAL";
43
- /** One or more numbers of any size */
44
- PacketType[PacketType["DECIMALS"] = 10] = "DECIMALS";
48
+ PacketType[PacketType["NUMBERS"] = 11] = "NUMBERS";
49
+ /** One or more integers between -281,474,976,710,656 and 281,474,976,710,655. */
50
+ PacketType[PacketType["VARINT"] = 12] = "VARINT";
51
+ /** One or more integers up to 562,949,953,421,311. */
52
+ PacketType[PacketType["UVARINT"] = 13] = "UVARINT";
53
+ /** One or more integers between -281,474,976,710,656 and 281,474,976,710,655 | zig-zag encoded (good for deltas; maps like -1->1, 1->2, -2->3, 2->4, etc.) */
54
+ PacketType[PacketType["VARINT_ZZ"] = 14] = "VARINT_ZZ";
55
+ /** Var ints that use deltas; each value will show the difference from the last value. Good for close numbers */
56
+ PacketType[PacketType["DELTAS"] = 15] = "DELTAS";
57
+ /** One or more single precision floating point numbers. Only up to 7 digits of accuracy. */
58
+ PacketType[PacketType["FLOAT"] = 16] = "FLOAT";
45
59
  /** One or more true/false */
46
- PacketType[PacketType["BOOLEANS"] = 11] = "BOOLEANS";
60
+ PacketType[PacketType["BOOLEANS"] = 17] = "BOOLEANS";
47
61
  })(PacketType || (exports.PacketType = PacketType = {}));