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.
- package/README.md +19 -11
- 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 +19 -0
- package/dist/ws/Connection.js +17 -0
- package/dist/ws/client/core/ClientCore.d.ts +27 -9
- package/dist/ws/client/core/ClientCore.js +162 -64
- package/dist/ws/client/node/ClientNode.js +2 -2
- package/dist/ws/packets/PacketProcessors.d.ts +11 -6
- package/dist/ws/packets/PacketProcessors.js +233 -175
- package/dist/ws/packets/PacketType.d.ts +30 -16
- package/dist/ws/packets/PacketType.js +31 -17
- package/dist/ws/packets/Packets.d.ts +15 -14
- package/dist/ws/packets/Packets.js +117 -99
- package/dist/ws/server/SonicWSConnection.d.ts +30 -8
- package/dist/ws/server/SonicWSConnection.js +67 -25
- package/dist/ws/server/SonicWSServer.d.ts +22 -0
- package/dist/ws/server/SonicWSServer.js +50 -8
- package/dist/ws/util/ArrayUtil.js +1 -1
- package/dist/ws/util/BufferUtil.d.ts +4 -0
- package/dist/ws/util/BufferUtil.js +40 -0
- 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 +4 -7
- package/dist/ws/util/enums/EnumType.d.ts +3 -2
- package/dist/ws/util/enums/EnumType.js +21 -10
- package/dist/ws/util/packets/BatchHelper.d.ts +10 -6
- package/dist/ws/util/packets/BatchHelper.js +28 -16
- package/dist/ws/util/packets/CompressionUtil.d.ts +44 -0
- package/dist/ws/util/packets/CompressionUtil.js +321 -0
- package/dist/ws/util/packets/PacketHolder.d.ts +5 -10
- package/dist/ws/util/packets/PacketHolder.js +25 -13
- package/dist/ws/util/packets/PacketUtils.d.ts +11 -8
- package/dist/ws/util/packets/PacketUtils.js +31 -26
- package/dist/ws/util/packets/RateHandler.js +16 -1
- package/package.json +2 -2
- package/dist/ws/util/packets/CodePointUtil.d.ts +0 -22
- package/dist/ws/util/packets/CodePointUtil.js +0 -220
|
@@ -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.
|
|
@@ -16,42 +16,54 @@
|
|
|
16
16
|
*/
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
18
|
exports.BatchHelper = void 0;
|
|
19
|
+
var BufferUtil_1 = require("../BufferUtil");
|
|
20
|
+
var CompressionUtil_1 = require("./CompressionUtil");
|
|
19
21
|
var BatchHelper = /** @class */ (function () {
|
|
20
22
|
function BatchHelper() {
|
|
23
|
+
this.batchTimes = {};
|
|
24
|
+
this.batchTimeouts = {};
|
|
21
25
|
this.batchedData = {};
|
|
22
26
|
}
|
|
23
|
-
BatchHelper.prototype.registerSendPackets = function (packetHolder,
|
|
27
|
+
BatchHelper.prototype.registerSendPackets = function (packetHolder, conn) {
|
|
24
28
|
var _this = this;
|
|
29
|
+
this.conn = conn;
|
|
25
30
|
packetHolder.getTags().forEach(function (tag) {
|
|
26
31
|
var packet = packetHolder.getPacket(tag);
|
|
27
32
|
if (packet.dataBatching == 0)
|
|
28
33
|
return;
|
|
29
|
-
var code = packetHolder.
|
|
30
|
-
_this.initiateBatch(code, packet.dataBatching
|
|
34
|
+
var code = packetHolder.getKey(tag);
|
|
35
|
+
_this.initiateBatch(code, packet.dataBatching);
|
|
31
36
|
});
|
|
32
37
|
};
|
|
33
|
-
BatchHelper.prototype.initiateBatch = function (code, time
|
|
34
|
-
var _this = this;
|
|
38
|
+
BatchHelper.prototype.initiateBatch = function (code, time) {
|
|
35
39
|
this.batchedData[code] = [];
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
+
this.batchTimes[code] = time;
|
|
41
|
+
};
|
|
42
|
+
BatchHelper.prototype.startBatch = function (code) {
|
|
43
|
+
var _this = this;
|
|
44
|
+
this.batchTimeouts[code] = this.conn.setTimeout(function () {
|
|
45
|
+
_this.conn.raw_send((0, BufferUtil_1.toPacketBuffer)(code, _this.batchedData[code]));
|
|
40
46
|
_this.batchedData[code] = [];
|
|
41
|
-
|
|
47
|
+
delete _this.batchTimeouts[code];
|
|
48
|
+
}, this.batchTimes[code]);
|
|
42
49
|
};
|
|
43
50
|
BatchHelper.prototype.batchPacket = function (code, data) {
|
|
44
|
-
this.batchedData[code]
|
|
51
|
+
var batch = this.batchedData[code];
|
|
52
|
+
batch.push.apply(batch, (0, CompressionUtil_1.convertVarInt)(data.length, false));
|
|
53
|
+
data.forEach(function (val) { return batch.push(val); });
|
|
54
|
+
if (!this.batchTimeouts[code])
|
|
55
|
+
this.startBatch(code);
|
|
45
56
|
};
|
|
46
57
|
BatchHelper.unravelBatch = function (packet, data, socket) {
|
|
47
58
|
var result = [];
|
|
48
59
|
for (var i = 0; i < data.length;) {
|
|
49
|
-
if (result.length > packet.maxBatchSize)
|
|
60
|
+
if (packet.maxBatchSize != 0 && result.length > packet.maxBatchSize)
|
|
50
61
|
return "Too big of batch";
|
|
51
|
-
var
|
|
52
|
-
|
|
62
|
+
var _a = (0, CompressionUtil_1.readVarInt)(data, i, false), off = _a[0], varint = _a[1];
|
|
63
|
+
i = off;
|
|
64
|
+
if (i + varint > data.length)
|
|
53
65
|
return "Tampered batch length";
|
|
54
|
-
var sect = data.
|
|
66
|
+
var sect = data.slice(i, i += varint);
|
|
55
67
|
var listen = packet.listen(sect, socket);
|
|
56
68
|
if (typeof listen == 'string')
|
|
57
69
|
return "Batched packet: " + listen;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export declare const MAX_BYTE = 255;
|
|
2
|
+
export declare const NEGATIVE_BYTE: number;
|
|
3
|
+
export declare const BYTE_OVERFLOW: number;
|
|
4
|
+
export declare const MAX_SHORT = 65535;
|
|
5
|
+
export declare const NEGATIVE_SHORT: number;
|
|
6
|
+
export declare const SHORT_CC_OVERFLOW: number;
|
|
7
|
+
export declare const SHORT_OVERFLOW: number;
|
|
8
|
+
export declare const MAX_INT_D: number;
|
|
9
|
+
export declare const UVARINT_OVERFLOW: number;
|
|
10
|
+
export declare const VARINT_CHAIN_FLAG = 128;
|
|
11
|
+
export declare const NEGATIVE_VARINT: number;
|
|
12
|
+
export declare const VARINT_OVERFLOW: number;
|
|
13
|
+
export declare const MAX_VSECT_SIZE = 7;
|
|
14
|
+
export declare const MAX_UVARINT: number;
|
|
15
|
+
export declare const MAX_VARINT: number;
|
|
16
|
+
export declare const byteOverflowPow: (num: number) => number;
|
|
17
|
+
export declare const uvarIntOverflowPow: (num: number) => number;
|
|
18
|
+
export declare const varIntOverflowPow: (num: number) => number;
|
|
19
|
+
export type SHORT_BITS = [high: number, low: number];
|
|
20
|
+
export declare function fromShort(short: SHORT_BITS): number;
|
|
21
|
+
export declare function toShort(n: number, signed: boolean): SHORT_BITS;
|
|
22
|
+
export declare function fromSignedShort(short: SHORT_BITS): number;
|
|
23
|
+
export declare function toSignedShort(number: number): SHORT_BITS;
|
|
24
|
+
export declare function toByte(n: number, signed: boolean): number;
|
|
25
|
+
export declare function fromSignedByte(point: number): number;
|
|
26
|
+
export declare function toSignedByte(number: number): number;
|
|
27
|
+
export declare function sectorSize(number: number, pow: any): number;
|
|
28
|
+
export declare const MAX_DSECT_SIZE: number;
|
|
29
|
+
export declare function convertBase(number: number, chars: number, single: any, neg: number, overflow: number, overflowFunc: any): number[];
|
|
30
|
+
export declare function convertBytePows(number: number, chars: number): number[];
|
|
31
|
+
export declare function deconvertBytePows(codes: any): any;
|
|
32
|
+
export declare const compressBools: (array: boolean[]) => number;
|
|
33
|
+
export declare const decompressBools: (byte: number) => boolean[];
|
|
34
|
+
export declare function convertFloat(flt: number): number[];
|
|
35
|
+
export declare function deconvertFloat(str: number[]): number;
|
|
36
|
+
export declare function mapZigZag(n: number): number;
|
|
37
|
+
export declare function demapZigZag(n: number): number;
|
|
38
|
+
export declare function demapShort_ZZ(short: SHORT_BITS): number;
|
|
39
|
+
export declare function mapShort_ZZ(short: number): SHORT_BITS;
|
|
40
|
+
export declare function toSignedVarInt(n: number): number;
|
|
41
|
+
export declare function fromSignedVarInt(n: number, signed: boolean): number;
|
|
42
|
+
export declare function convertVarInt(num: number, signed: boolean): number[];
|
|
43
|
+
export declare function readVarInt(arr: number[] | Uint8Array, off: number, signed: boolean): [offset: number, number: number];
|
|
44
|
+
export declare function deconvertVarInts(arr: Uint8Array | number[], signed: boolean): number[];
|
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright 2025 Lily (liwybloc)
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
18
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
19
|
+
if (ar || !(i in from)) {
|
|
20
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
21
|
+
ar[i] = from[i];
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
25
|
+
};
|
|
26
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
27
|
+
exports.decompressBools = exports.compressBools = exports.MAX_DSECT_SIZE = exports.varIntOverflowPow = exports.uvarIntOverflowPow = exports.byteOverflowPow = exports.MAX_VARINT = exports.MAX_UVARINT = exports.MAX_VSECT_SIZE = exports.VARINT_OVERFLOW = exports.NEGATIVE_VARINT = exports.VARINT_CHAIN_FLAG = exports.UVARINT_OVERFLOW = exports.MAX_INT_D = exports.SHORT_OVERFLOW = exports.SHORT_CC_OVERFLOW = exports.NEGATIVE_SHORT = exports.MAX_SHORT = exports.BYTE_OVERFLOW = exports.NEGATIVE_BYTE = exports.MAX_BYTE = void 0;
|
|
28
|
+
exports.fromShort = fromShort;
|
|
29
|
+
exports.toShort = toShort;
|
|
30
|
+
exports.fromSignedShort = fromSignedShort;
|
|
31
|
+
exports.toSignedShort = toSignedShort;
|
|
32
|
+
exports.toByte = toByte;
|
|
33
|
+
exports.fromSignedByte = fromSignedByte;
|
|
34
|
+
exports.toSignedByte = toSignedByte;
|
|
35
|
+
exports.sectorSize = sectorSize;
|
|
36
|
+
exports.convertBase = convertBase;
|
|
37
|
+
exports.convertBytePows = convertBytePows;
|
|
38
|
+
exports.deconvertBytePows = deconvertBytePows;
|
|
39
|
+
exports.convertFloat = convertFloat;
|
|
40
|
+
exports.deconvertFloat = deconvertFloat;
|
|
41
|
+
exports.mapZigZag = mapZigZag;
|
|
42
|
+
exports.demapZigZag = demapZigZag;
|
|
43
|
+
exports.demapShort_ZZ = demapShort_ZZ;
|
|
44
|
+
exports.mapShort_ZZ = mapShort_ZZ;
|
|
45
|
+
exports.toSignedVarInt = toSignedVarInt;
|
|
46
|
+
exports.fromSignedVarInt = fromSignedVarInt;
|
|
47
|
+
exports.convertVarInt = convertVarInt;
|
|
48
|
+
exports.readVarInt = readVarInt;
|
|
49
|
+
exports.deconvertVarInts = deconvertVarInts;
|
|
50
|
+
var ArrayUtil_1 = require("../ArrayUtil");
|
|
51
|
+
// this shit is so complex so i commented it...
|
|
52
|
+
// the highest 8-bit
|
|
53
|
+
exports.MAX_BYTE = 0xFF;
|
|
54
|
+
// we split the usable range in half to separate positive and negative encodings
|
|
55
|
+
exports.NEGATIVE_BYTE = Math.floor(exports.MAX_BYTE / 2);
|
|
56
|
+
// overflow is used as our "base" in a positional number system (like base 10, but very large)
|
|
57
|
+
// we use this to reduce the number of characters needed to represent large numbers
|
|
58
|
+
exports.BYTE_OVERFLOW = exports.NEGATIVE_BYTE + 1;
|
|
59
|
+
// the highest16-bit
|
|
60
|
+
exports.MAX_SHORT = 0xFFFF;
|
|
61
|
+
// we split the usable range in half to separate positive and negative encodings
|
|
62
|
+
exports.NEGATIVE_SHORT = Math.floor(exports.MAX_SHORT / 2);
|
|
63
|
+
// overflow for shorts in construction
|
|
64
|
+
exports.SHORT_CC_OVERFLOW = exports.MAX_BYTE + 1;
|
|
65
|
+
// overflow for shorts base
|
|
66
|
+
exports.SHORT_OVERFLOW = exports.MAX_SHORT + 1;
|
|
67
|
+
// highest number INT_D can optimally support
|
|
68
|
+
exports.MAX_INT_D = Number.MAX_SAFE_INTEGER;
|
|
69
|
+
// for varint to overflow; is 128
|
|
70
|
+
exports.UVARINT_OVERFLOW = exports.NEGATIVE_BYTE + 1;
|
|
71
|
+
// flag for chaining
|
|
72
|
+
exports.VARINT_CHAIN_FLAG = 0x80;
|
|
73
|
+
// splitting usable range in half for signed
|
|
74
|
+
exports.NEGATIVE_VARINT = Math.floor(exports.NEGATIVE_BYTE / 2);
|
|
75
|
+
// for varint to overflow with negative; is 64
|
|
76
|
+
exports.VARINT_OVERFLOW = exports.NEGATIVE_VARINT + 1;
|
|
77
|
+
// max continues
|
|
78
|
+
exports.MAX_VSECT_SIZE = 7;
|
|
79
|
+
// max value from this, subtract one for overflow
|
|
80
|
+
exports.MAX_UVARINT = (Math.pow(exports.UVARINT_OVERFLOW, exports.MAX_VSECT_SIZE)) - 1;
|
|
81
|
+
// max for negatives
|
|
82
|
+
exports.MAX_VARINT = Math.floor(exports.MAX_UVARINT / 2);
|
|
83
|
+
// precompute powers
|
|
84
|
+
var BYTE_OVERFLOW_POWS = [];
|
|
85
|
+
var byteOverflowPow = function (num) { var _a; return (_a = BYTE_OVERFLOW_POWS[num]) !== null && _a !== void 0 ? _a : (BYTE_OVERFLOW_POWS[num] = Math.pow(exports.BYTE_OVERFLOW, num)); };
|
|
86
|
+
exports.byteOverflowPow = byteOverflowPow;
|
|
87
|
+
var UVARINT_OVERFLOW_POWS = [];
|
|
88
|
+
var uvarIntOverflowPow = function (num) { var _a; return (_a = UVARINT_OVERFLOW_POWS[num]) !== null && _a !== void 0 ? _a : (UVARINT_OVERFLOW_POWS[num] = Math.pow(exports.UVARINT_OVERFLOW, num)); };
|
|
89
|
+
exports.uvarIntOverflowPow = uvarIntOverflowPow;
|
|
90
|
+
var VARINT_OVERFLOW_POWS = [];
|
|
91
|
+
var varIntOverflowPow = function (num) { var _a; return (_a = VARINT_OVERFLOW_POWS[num]) !== null && _a !== void 0 ? _a : (VARINT_OVERFLOW_POWS[num] = Math.pow(exports.VARINT_OVERFLOW, num)); };
|
|
92
|
+
exports.varIntOverflowPow = varIntOverflowPow;
|
|
93
|
+
var TWO_POWS = [];
|
|
94
|
+
var twoPow = function (num) { var _a; return (_a = TWO_POWS[num]) !== null && _a !== void 0 ? _a : (TWO_POWS[num] = Math.pow(2, num)); };
|
|
95
|
+
for (var i = 0; i <= 3; i++) {
|
|
96
|
+
(0, exports.byteOverflowPow)(i);
|
|
97
|
+
(0, exports.uvarIntOverflowPow)(i);
|
|
98
|
+
(0, exports.varIntOverflowPow)(i);
|
|
99
|
+
twoPow(i);
|
|
100
|
+
}
|
|
101
|
+
// reconstruction
|
|
102
|
+
function fromShort(short) {
|
|
103
|
+
// convert to number
|
|
104
|
+
return short[0] * exports.SHORT_CC_OVERFLOW + short[1];
|
|
105
|
+
}
|
|
106
|
+
// checks, conversion
|
|
107
|
+
function toShort(n, signed) {
|
|
108
|
+
// no nan/infinity
|
|
109
|
+
if (!isFinite(n))
|
|
110
|
+
throw new Error("Can only use real numbers in shorts: " + n);
|
|
111
|
+
// limit check
|
|
112
|
+
var lim = signed ? exports.NEGATIVE_SHORT : exports.MAX_SHORT;
|
|
113
|
+
var min = signed ? -exports.NEGATIVE_SHORT - 1 : 0;
|
|
114
|
+
if (n > lim || n < min)
|
|
115
|
+
throw new Error("".concat(signed ? "Signed " : " ", "Short Numbers must be within range ").concat(min, " and ").concat(lim));
|
|
116
|
+
// how many times it passes SHORT_OVERFLOW and the remainder
|
|
117
|
+
return [Math.floor(n / exports.SHORT_CC_OVERFLOW), n % exports.SHORT_CC_OVERFLOW];
|
|
118
|
+
}
|
|
119
|
+
// this converts an encoded code point back to a signed number
|
|
120
|
+
function fromSignedShort(short) {
|
|
121
|
+
// convert to number
|
|
122
|
+
var point = fromShort(short);
|
|
123
|
+
// if the number is below NEGATIVE_SHORT, it's a positive number and can be returned directly
|
|
124
|
+
// if it's above or equal to NEGATIVE_SHORT, it was originally negative, so we reverse the offset
|
|
125
|
+
return point <= exports.NEGATIVE_SHORT ? point : -point + exports.NEGATIVE_SHORT;
|
|
126
|
+
}
|
|
127
|
+
// this converts a signed number into a non-negative integer that fits in a short
|
|
128
|
+
function toSignedShort(number) {
|
|
129
|
+
// positive numbers are returned as-is
|
|
130
|
+
// negative numbers are made positive and offset above NEGATIVE_SHORT to mark them
|
|
131
|
+
// ugh fix this shit
|
|
132
|
+
return toShort(number < 0 ? -number + exports.NEGATIVE_SHORT : number, false);
|
|
133
|
+
}
|
|
134
|
+
// checks
|
|
135
|
+
function toByte(n, signed) {
|
|
136
|
+
// no nan/infinity
|
|
137
|
+
if (!isFinite(n))
|
|
138
|
+
throw new Error("Can only use real numbers in bytes: " + n);
|
|
139
|
+
;
|
|
140
|
+
// limit check
|
|
141
|
+
var lim = signed ? exports.NEGATIVE_BYTE : exports.MAX_BYTE;
|
|
142
|
+
if (n > lim || n < -lim - 1)
|
|
143
|
+
throw new Error("".concat(signed ? "Signed " : " ", "Byte Numbers must be within range -").concat(lim + 1, " and ").concat(lim, ": ").concat(n));
|
|
144
|
+
return n;
|
|
145
|
+
}
|
|
146
|
+
// this converts a byte back to a signed number
|
|
147
|
+
function fromSignedByte(point) {
|
|
148
|
+
// if the number is below NEGATIVE_BYTE, it's a positive number and can be returned directly
|
|
149
|
+
// if it's above or equal to NEGATIVE_BYTE, it was originally negative, so we reverse the offset
|
|
150
|
+
return point <= exports.NEGATIVE_BYTE ? point : -point + exports.NEGATIVE_BYTE;
|
|
151
|
+
}
|
|
152
|
+
// this converts a signed number into a non-negative integer that fits in a byte
|
|
153
|
+
function toSignedByte(number) {
|
|
154
|
+
// positive numbers are returned as-is
|
|
155
|
+
// negative numbers are made positive and offset above NEGATIVE_BYTE to mark them
|
|
156
|
+
number = toByte(number, true);
|
|
157
|
+
return number < 0 ? -number + exports.NEGATIVE_BYTE : number;
|
|
158
|
+
}
|
|
159
|
+
// calculate how many characters (digits) are needed to store this number in OVERFLOW base
|
|
160
|
+
function sectorSize(number, pow) {
|
|
161
|
+
number = Math.abs(number);
|
|
162
|
+
// iterative system because it's faster than log
|
|
163
|
+
var count = 1;
|
|
164
|
+
// i like my code 𝑓𝑟𝑒𝑎𝑘𝑦
|
|
165
|
+
for (var num = pow(1); number >= num; num = pow(++count))
|
|
166
|
+
;
|
|
167
|
+
return count;
|
|
168
|
+
}
|
|
169
|
+
exports.MAX_DSECT_SIZE = sectorSize(exports.MAX_INT_D, exports.byteOverflowPow);
|
|
170
|
+
// encodes a number into a safe array using a large base (OVERFLOW)
|
|
171
|
+
function convertBase(number, chars, single, neg, overflow, overflowFunc) {
|
|
172
|
+
// no nan/infinity
|
|
173
|
+
if (!isFinite(number))
|
|
174
|
+
throw new Error("Cannot use a non-finite number: " + number);
|
|
175
|
+
// zero is just null
|
|
176
|
+
if (number == 0)
|
|
177
|
+
return Array.from({ length: chars }).map(function () { return 0; });
|
|
178
|
+
// any 1 char will just be INT_C anyway
|
|
179
|
+
if (chars == 1)
|
|
180
|
+
return [single(number)];
|
|
181
|
+
// store the sign and work with the absolute value
|
|
182
|
+
var negative = number < 0;
|
|
183
|
+
number = Math.abs(number);
|
|
184
|
+
// limit range
|
|
185
|
+
if (number > exports.MAX_INT_D)
|
|
186
|
+
throw new Error("Non-float numbers must be within range -".concat(exports.MAX_INT_D.toLocaleString(), " and ").concat(exports.MAX_INT_D.toLocaleString(), ": ").concat(number));
|
|
187
|
+
var result = [];
|
|
188
|
+
// for each character except the last, extract the digit at that position
|
|
189
|
+
// this is similar to how base conversion works: divide by base^position
|
|
190
|
+
var posPowerAmt = chars - 1;
|
|
191
|
+
for (var i = 0; i < posPowerAmt; i++) {
|
|
192
|
+
var power = overflowFunc(posPowerAmt - i);
|
|
193
|
+
var based = Math.floor(number / power);
|
|
194
|
+
result.push(based);
|
|
195
|
+
// remove it from the number so it doesnt effect future iterations
|
|
196
|
+
number -= based * power;
|
|
197
|
+
}
|
|
198
|
+
// the last digit is just the remainder
|
|
199
|
+
result.push(number % overflow);
|
|
200
|
+
// if the number was negative, we offset each character to indicate the sign
|
|
201
|
+
// we only offset non-zero digits to avoid collisions with the null character
|
|
202
|
+
var bits = negative ? result.map(function (part) { return part > 0 ? part + neg : part; })
|
|
203
|
+
: result;
|
|
204
|
+
return bits;
|
|
205
|
+
}
|
|
206
|
+
// im sleepy go aways..
|
|
207
|
+
function convertBytePows(number, chars) {
|
|
208
|
+
return convertBase(number, chars, toSignedByte, exports.NEGATIVE_BYTE, exports.BYTE_OVERFLOW, exports.byteOverflowPow);
|
|
209
|
+
}
|
|
210
|
+
// decodes a string created by convertINT_D back into the original signed integer
|
|
211
|
+
function deconvertBytePows(codes) {
|
|
212
|
+
if (codes.length == 0)
|
|
213
|
+
return fromSignedByte(codes[0]);
|
|
214
|
+
// for each code point in the string, reverse the sign encoding if necessary,
|
|
215
|
+
// multiply by the positional weight based on its place (most-significant-digit first)
|
|
216
|
+
return codes.reduce(function (c, n, i, arr) { return c + fromSignedByte(n) * (0, exports.byteOverflowPow)(arr.length - i - 1); }, 0);
|
|
217
|
+
}
|
|
218
|
+
// boolean stuff
|
|
219
|
+
var compressBools = function (array) { return array.reduce(function (byte, val, i) { return byte | (val << (7 - i)); }, 0); };
|
|
220
|
+
exports.compressBools = compressBools;
|
|
221
|
+
var decompressBools = function (byte) { return __spreadArray([], Array(8), true).map(function (_, i) { return (byte & (1 << (7 - i))) !== 0; }); };
|
|
222
|
+
exports.decompressBools = decompressBools;
|
|
223
|
+
// IEEE-754 single-precision float codec
|
|
224
|
+
var MAN_BITS = 23;
|
|
225
|
+
function parseBin(str) {
|
|
226
|
+
return Number("0b" + str);
|
|
227
|
+
}
|
|
228
|
+
function parseMan(bin, isNormal) {
|
|
229
|
+
var mantissaInt = parseBin(bin);
|
|
230
|
+
var fraction = 0;
|
|
231
|
+
for (var i = 0; i < MAN_BITS; i++) {
|
|
232
|
+
if (mantissaInt & (1 << (MAN_BITS - i - 1))) {
|
|
233
|
+
fraction += twoPow(-(i + 1));
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return (isNormal ? 1 : 0) + fraction;
|
|
237
|
+
}
|
|
238
|
+
function assembleFloat(sign, exponent, mantissa) {
|
|
239
|
+
var bin = sign.toString(2) + exponent.toString(2).padStart(8, "0") + mantissa.toString(2).padStart(23, "0");
|
|
240
|
+
return (0, ArrayUtil_1.splitArray)(bin, 8).map(function (x) { return parseBin(x); });
|
|
241
|
+
}
|
|
242
|
+
// https://stackoverflow.com/questions/3096646/how-to-convert-a-floating-point-number-to-its-binary-representation-ieee-754-i
|
|
243
|
+
// edited for clarity
|
|
244
|
+
function convertFloat(flt) {
|
|
245
|
+
if (isNaN(flt)) // Special case: NaN
|
|
246
|
+
return assembleFloat(0, 0xFF, 0x1337); // Mantissa is nonzero for NaN
|
|
247
|
+
var sign = (flt < 0) ? 1 : 0;
|
|
248
|
+
flt = Math.abs(flt);
|
|
249
|
+
if (flt == 0.0) // Special case: +-0
|
|
250
|
+
return assembleFloat(sign, 0, 0);
|
|
251
|
+
var exponent = Math.floor(Math.log(flt) / Math.LN2);
|
|
252
|
+
if (exponent > 127 || exponent < -126) // Special case: +-Infinity (and huge numbers)
|
|
253
|
+
return assembleFloat(sign, 0xFF, 0); // Mantissa is zero for +-Infinity
|
|
254
|
+
var mantissa = flt / twoPow(exponent);
|
|
255
|
+
var roundMan = Math.round((mantissa - 1) * twoPow(23));
|
|
256
|
+
return assembleFloat(sign, exponent + 127, roundMan & 0x7FFFFF);
|
|
257
|
+
}
|
|
258
|
+
function deconvertFloat(str) {
|
|
259
|
+
var bin = str.map(function (x) { return x.toString(2).padStart(8, "0"); }).join("");
|
|
260
|
+
var sign = parseBin(bin[0]);
|
|
261
|
+
var rawExp = parseBin(bin.slice(1, 9));
|
|
262
|
+
var exp = rawExp === 0 ? -126 : rawExp - 127;
|
|
263
|
+
var man = parseMan(bin.slice(9, 32), rawExp !== 0); // whether to add the implicit 1
|
|
264
|
+
return (sign == 0 ? 1 : -1) * man * twoPow(exp);
|
|
265
|
+
}
|
|
266
|
+
// zig_zag
|
|
267
|
+
function mapZigZag(n) {
|
|
268
|
+
return (n << 1) // shifts left (multiply by 2 to get into zigzag)
|
|
269
|
+
^
|
|
270
|
+
(n >> 15); // then xor the sign away
|
|
271
|
+
}
|
|
272
|
+
function demapZigZag(n) {
|
|
273
|
+
return (n >>> 1) // shifts right unsigned to remove the sign & divide by 2
|
|
274
|
+
^
|
|
275
|
+
-(n & 1); // flips bits to give negative back
|
|
276
|
+
}
|
|
277
|
+
function demapShort_ZZ(short) {
|
|
278
|
+
return demapZigZag(fromShort(short));
|
|
279
|
+
}
|
|
280
|
+
function mapShort_ZZ(short) {
|
|
281
|
+
return toShort(mapZigZag(short), false);
|
|
282
|
+
}
|
|
283
|
+
// yeah!
|
|
284
|
+
function toSignedVarInt(n) {
|
|
285
|
+
return n < 0 ? n | exports.VARINT_OVERFLOW : n;
|
|
286
|
+
}
|
|
287
|
+
function fromSignedVarInt(n, signed) {
|
|
288
|
+
return signed && (n & exports.VARINT_OVERFLOW) != 0 ? -(n ^ exports.VARINT_OVERFLOW) : n;
|
|
289
|
+
}
|
|
290
|
+
function convertSVarInt(num, min, max, overflowFunc, overflow, signed) {
|
|
291
|
+
if (num > max || num < min)
|
|
292
|
+
throw new Error("".concat(signed ? "Signed " : "", "Variable Ints must be within range ").concat(min, " and ").concat(max, ": ").concat(num));
|
|
293
|
+
var chars = sectorSize(num, overflowFunc);
|
|
294
|
+
return convertBase(num, chars, toSignedVarInt, exports.VARINT_OVERFLOW, overflow, overflowFunc).map(function (x, i) { return i == 0 ? x : x | exports.VARINT_CHAIN_FLAG; }).reverse();
|
|
295
|
+
}
|
|
296
|
+
function convertVarInt(num, signed) {
|
|
297
|
+
return signed ? convertSVarInt(num, -exports.MAX_UVARINT - 1, exports.MAX_UVARINT, exports.varIntOverflowPow, exports.VARINT_OVERFLOW, true)
|
|
298
|
+
: convertSVarInt(num, 0, exports.MAX_UVARINT, exports.uvarIntOverflowPow, exports.UVARINT_OVERFLOW, false);
|
|
299
|
+
}
|
|
300
|
+
function readVarInt(arr, off, signed) {
|
|
301
|
+
var num = [];
|
|
302
|
+
var cont;
|
|
303
|
+
do {
|
|
304
|
+
var part = arr[off++];
|
|
305
|
+
cont = (part & exports.VARINT_CHAIN_FLAG) != 0;
|
|
306
|
+
num.push(cont ? part ^ exports.VARINT_CHAIN_FLAG : part);
|
|
307
|
+
} while (cont);
|
|
308
|
+
var func = signed ? exports.varIntOverflowPow : exports.uvarIntOverflowPow;
|
|
309
|
+
var number = num.reduce(function (p, c, i) { return p + fromSignedVarInt(c, signed) * func(i); }, 0);
|
|
310
|
+
return [off, number];
|
|
311
|
+
}
|
|
312
|
+
function deconvertVarInts(arr, signed) {
|
|
313
|
+
var res = [];
|
|
314
|
+
var i = 0;
|
|
315
|
+
while (i < arr.length) {
|
|
316
|
+
var _a = readVarInt(arr, i, signed), off = _a[0], varint = _a[1];
|
|
317
|
+
res.push(varint);
|
|
318
|
+
i = off;
|
|
319
|
+
}
|
|
320
|
+
return res;
|
|
321
|
+
}
|
|
@@ -29,17 +29,12 @@ export declare class PacketHolder {
|
|
|
29
29
|
* Returns the numeric key for a given tag
|
|
30
30
|
* @param tag The packet tag
|
|
31
31
|
*/
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Returns the character representation of a tag's key
|
|
35
|
-
* @param tag The packet tag
|
|
36
|
-
*/
|
|
37
|
-
getChar(tag: string): string;
|
|
32
|
+
getKey(tag: string): number;
|
|
38
33
|
/**
|
|
39
34
|
* Returns the tag associated with a given character key
|
|
40
35
|
* @param key A 1-character string key
|
|
41
36
|
*/
|
|
42
|
-
getTag(key:
|
|
37
|
+
getTag(key: number): string;
|
|
43
38
|
/**
|
|
44
39
|
* Returns the packet instance associated with a tag
|
|
45
40
|
* @param tag The packet tag
|
|
@@ -47,9 +42,9 @@ export declare class PacketHolder {
|
|
|
47
42
|
getPacket(tag: string): Packet;
|
|
48
43
|
/**
|
|
49
44
|
* Checks if a given character key exists
|
|
50
|
-
* @param key A
|
|
45
|
+
* @param key A string index
|
|
51
46
|
*/
|
|
52
|
-
|
|
47
|
+
hasKey(key: number): boolean;
|
|
53
48
|
/**
|
|
54
49
|
* Checks if a tag has been assigned a key
|
|
55
50
|
* @param tag The packet tag
|
|
@@ -64,5 +59,5 @@ export declare class PacketHolder {
|
|
|
64
59
|
/** Returns the list of all registered packets */
|
|
65
60
|
getPackets(): Packet[];
|
|
66
61
|
/** Serializes all registered packets into a string */
|
|
67
|
-
serialize():
|
|
62
|
+
serialize(): number[];
|
|
68
63
|
}
|
|
@@ -1,4 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright 2025 Lily (liwybloc)
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
2
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
18
|
exports.PacketHolder = void 0;
|
|
4
19
|
/**
|
|
@@ -39,36 +54,33 @@ var PacketHolder = /** @class */ (function () {
|
|
|
39
54
|
* Returns the numeric key for a given tag
|
|
40
55
|
* @param tag The packet tag
|
|
41
56
|
*/
|
|
42
|
-
PacketHolder.prototype.
|
|
57
|
+
PacketHolder.prototype.getKey = function (tag) {
|
|
58
|
+
if (!(tag in this.keys))
|
|
59
|
+
throw new Error("Not a valid tag: ".concat(tag));
|
|
43
60
|
return this.keys[tag];
|
|
44
61
|
};
|
|
45
|
-
/**
|
|
46
|
-
* Returns the character representation of a tag's key
|
|
47
|
-
* @param tag The packet tag
|
|
48
|
-
*/
|
|
49
|
-
PacketHolder.prototype.getChar = function (tag) {
|
|
50
|
-
return String.fromCharCode(this.get(tag));
|
|
51
|
-
};
|
|
52
62
|
/**
|
|
53
63
|
* Returns the tag associated with a given character key
|
|
54
64
|
* @param key A 1-character string key
|
|
55
65
|
*/
|
|
56
66
|
PacketHolder.prototype.getTag = function (key) {
|
|
57
|
-
return this.tags[key
|
|
67
|
+
return this.tags[key];
|
|
58
68
|
};
|
|
59
69
|
/**
|
|
60
70
|
* Returns the packet instance associated with a tag
|
|
61
71
|
* @param tag The packet tag
|
|
62
72
|
*/
|
|
63
73
|
PacketHolder.prototype.getPacket = function (tag) {
|
|
74
|
+
if (!(tag in this.packetMap))
|
|
75
|
+
throw new Error("Unknown packet tag: " + tag);
|
|
64
76
|
return this.packetMap[tag];
|
|
65
77
|
};
|
|
66
78
|
/**
|
|
67
79
|
* Checks if a given character key exists
|
|
68
|
-
* @param key A
|
|
80
|
+
* @param key A string index
|
|
69
81
|
*/
|
|
70
|
-
PacketHolder.prototype.
|
|
71
|
-
return key
|
|
82
|
+
PacketHolder.prototype.hasKey = function (key) {
|
|
83
|
+
return key in this.tags;
|
|
72
84
|
};
|
|
73
85
|
/**
|
|
74
86
|
* Checks if a tag has been assigned a key
|
|
@@ -95,7 +107,7 @@ var PacketHolder = /** @class */ (function () {
|
|
|
95
107
|
};
|
|
96
108
|
/** Serializes all registered packets into a string */
|
|
97
109
|
PacketHolder.prototype.serialize = function () {
|
|
98
|
-
return this.packets.map(function (p) { return p.serialize(); }).
|
|
110
|
+
return this.packets.map(function (p) { return p.serialize(); }).flat();
|
|
99
111
|
};
|
|
100
112
|
return PacketHolder;
|
|
101
113
|
}());
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { PacketHolder } from "./PacketHolder";
|
|
2
|
-
import { Packet } from "../../packets/Packets";
|
|
2
|
+
import { Packet, ValidatorFunction } from "../../packets/Packets";
|
|
3
3
|
import { PacketType } from "../../packets/PacketType";
|
|
4
4
|
import { EnumPackage } from "../enums/EnumType";
|
|
5
|
-
import { SonicWSConnection } from "../../server/SonicWSConnection";
|
|
6
5
|
/**
|
|
7
6
|
* Processes and verifies values into a sendable format
|
|
8
7
|
* @param packets Packet holder
|
|
@@ -10,7 +9,7 @@ import { SonicWSConnection } from "../../server/SonicWSConnection";
|
|
|
10
9
|
* @param values The values
|
|
11
10
|
* @returns The indexed code, the data, and the packet schema
|
|
12
11
|
*/
|
|
13
|
-
export declare function processPacket(packets: PacketHolder, tag: string, values: any[]): [code:
|
|
12
|
+
export declare function processPacket(packets: PacketHolder, tag: string, values: any[]): [code: number, data: number[], packet: Packet];
|
|
14
13
|
/**
|
|
15
14
|
* Calls the listener for a packet with error callback
|
|
16
15
|
* @param listened The listened data
|
|
@@ -36,12 +35,18 @@ export type SharedPacketSettings = {
|
|
|
36
35
|
* Each batched packet is counted towards the rate limit.
|
|
37
36
|
*/
|
|
38
37
|
dataBatching?: number;
|
|
39
|
-
/** If data batching is on, this will limit the amount of packets that can be batched into one (only effects the client). Defaults to 10. */
|
|
38
|
+
/** If data batching is on, this will limit the amount of packets that can be batched into one (only effects the client). Defaults to 10. 0 for unlimited. */
|
|
40
39
|
maxBatchSize?: number;
|
|
41
|
-
/** The amount of times this packet can be sent every second, or 0 for infinite */
|
|
40
|
+
/** The amount of times this packet can be sent every second, or 0 for infinite. */
|
|
42
41
|
rateLimit?: number;
|
|
42
|
+
/**
|
|
43
|
+
* If the packet should be enabled by default. Defaults to true. Will kick the client if they send a disabled packet. Does not effect server.
|
|
44
|
+
*
|
|
45
|
+
* Changeable with socket.enablePacket("tag") | socket.disablePacket("tag") (you can also do wss.enablePacket/disablePacket).
|
|
46
|
+
*/
|
|
47
|
+
enabled?: boolean;
|
|
43
48
|
/** A validation function that is called whenever data is received. Return true for success, return false to kick socket. */
|
|
44
|
-
validator?:
|
|
49
|
+
validator?: ValidatorFunction;
|
|
45
50
|
};
|
|
46
51
|
/** Settings for single-typed packets */
|
|
47
52
|
export type SinglePacketSettings = SharedPacketSettings & {
|
|
@@ -62,8 +67,6 @@ export type MultiPacketSettings = SharedPacketSettings & {
|
|
|
62
67
|
dataMins?: number[] | number;
|
|
63
68
|
/** Will automatically run FlattenData() and UnFlattenData() on values; this will optimize [[x,y,z],[x,y,z]...] for wire transfer */
|
|
64
69
|
autoFlatten?: boolean;
|
|
65
|
-
/** If the packet will have values larger than 55,296 then this will allow up to 3,057,647,616 length for some minor bandwidth cost. */
|
|
66
|
-
largePacket?: boolean;
|
|
67
70
|
};
|
|
68
71
|
/** Settings for single-typed enum packets */
|
|
69
72
|
export type EnumPacketSettings = SharedPacketSettings & {
|