sonic-ws 1.3.2 → 1.3.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/dist/index.d.ts +0 -4
- package/dist/version.d.ts +1 -5
- package/dist/version.js +23 -4
- package/dist/ws/Connection.d.ts +0 -4
- package/dist/ws/Connection.js +124 -4
- package/dist/ws/PacketProcessor.d.ts +0 -4
- package/dist/ws/PacketProcessor.js +50 -4
- package/dist/ws/client/core/ClientCore.d.ts +0 -4
- package/dist/ws/client/core/ClientCore.js +249 -4
- package/dist/ws/client/node/ClientNode.d.ts +0 -4
- package/dist/ws/client/node/ClientNode.js +34 -4
- package/dist/ws/debug/DebugServer.d.ts +0 -4
- package/dist/ws/debug/DebugServer.js +605 -4
- package/dist/ws/packets/PacketProcessors.d.ts +0 -4
- package/dist/ws/packets/PacketProcessors.js +315 -4
- package/dist/ws/packets/PacketType.d.ts +0 -4
- package/dist/ws/packets/PacketType.js +59 -4
- package/dist/ws/packets/Packets.d.ts +0 -4
- package/dist/ws/packets/Packets.js +275 -4
- package/dist/ws/server/SonicWSConnection.d.ts +0 -4
- package/dist/ws/server/SonicWSConnection.js +296 -4
- package/dist/ws/server/SonicWSServer.d.ts +1 -5
- package/dist/ws/server/SonicWSServer.js +305 -4
- package/dist/ws/util/BufferUtil.d.ts +0 -4
- package/dist/ws/util/BufferUtil.js +40 -4
- package/dist/ws/util/StringUtil.d.ts +0 -4
- package/dist/ws/util/StringUtil.js +49 -4
- package/dist/ws/util/enums/EnumHandler.d.ts +0 -4
- package/dist/ws/util/enums/EnumHandler.js +58 -4
- package/dist/ws/util/enums/EnumType.d.ts +0 -4
- package/dist/ws/util/enums/EnumType.js +70 -4
- package/dist/ws/util/packets/BatchHelper.d.ts +0 -4
- package/dist/ws/util/packets/BatchHelper.js +83 -4
- package/dist/ws/util/packets/CompressionUtil.d.ts +5 -9
- package/dist/ws/util/packets/CompressionUtil.js +275 -4
- package/dist/ws/util/packets/HashUtil.d.ts +0 -4
- package/dist/ws/util/packets/HashUtil.js +120 -4
- package/dist/ws/util/packets/JSONUtil.d.ts +0 -4
- package/dist/ws/util/packets/JSONUtil.js +177 -4
- package/dist/ws/util/packets/PacketHolder.d.ts +0 -4
- package/dist/ws/util/packets/PacketHolder.js +124 -4
- package/dist/ws/util/packets/PacketUtils.d.ts +0 -4
- package/dist/ws/util/packets/PacketUtils.js +281 -4
- package/dist/ws/util/packets/RateHandler.d.ts +0 -4
- package/dist/ws/util/packets/RateHandler.js +64 -4
- package/package.json +3 -3
|
@@ -1,6 +1,85 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
2
3
|
* Copyright 2026 Lily (liwybloc)
|
|
3
|
-
*
|
|
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.
|
|
4
16
|
*/
|
|
5
|
-
|
|
6
|
-
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.BatchHelper = void 0;
|
|
19
|
+
const BufferUtil_1 = require("../BufferUtil");
|
|
20
|
+
const CompressionUtil_1 = require("./CompressionUtil");
|
|
21
|
+
/** @internal */
|
|
22
|
+
class BatchHelper {
|
|
23
|
+
batchInfo = {};
|
|
24
|
+
batchTimeouts = {};
|
|
25
|
+
batchedData = {};
|
|
26
|
+
conn;
|
|
27
|
+
registerSendPackets(packetHolder, conn) {
|
|
28
|
+
this.conn = conn;
|
|
29
|
+
packetHolder.getTags().forEach(tag => {
|
|
30
|
+
const packet = packetHolder.getPacket(tag);
|
|
31
|
+
if (packet.dataBatching == 0)
|
|
32
|
+
return;
|
|
33
|
+
const code = packetHolder.getKey(tag);
|
|
34
|
+
this.initiateBatch(code, packet.dataBatching, packet.gzipCompression);
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
initiateBatch(code, time, compressed) {
|
|
38
|
+
this.batchedData[code] = [];
|
|
39
|
+
this.batchInfo[code] = [time, compressed];
|
|
40
|
+
}
|
|
41
|
+
startBatch(code) {
|
|
42
|
+
const [time, compressed] = this.batchInfo[code];
|
|
43
|
+
this.batchTimeouts[code] = this.conn.setInterval(async () => {
|
|
44
|
+
if (this.batchedData[code].length == 0)
|
|
45
|
+
return;
|
|
46
|
+
const data = new Uint8Array(this.batchedData[code]);
|
|
47
|
+
this.conn.raw_send((0, BufferUtil_1.toPacketBuffer)(code, compressed ? await (0, CompressionUtil_1.compressGzip)(data) : data));
|
|
48
|
+
this.batchedData[code] = [];
|
|
49
|
+
delete this.batchTimeouts[code];
|
|
50
|
+
}, time);
|
|
51
|
+
}
|
|
52
|
+
batchPacket(code, data) {
|
|
53
|
+
const batch = this.batchedData[code];
|
|
54
|
+
batch.push(...(0, CompressionUtil_1.convertVarInt)(data.length));
|
|
55
|
+
data.forEach(val => batch.push(val));
|
|
56
|
+
if (!this.batchTimeouts[code])
|
|
57
|
+
this.startBatch(code);
|
|
58
|
+
}
|
|
59
|
+
static async unravelBatch(packet, _data, socket) {
|
|
60
|
+
const data = packet.gzipCompression ? await (0, CompressionUtil_1.decompressGzip)(_data) : _data;
|
|
61
|
+
const result = [];
|
|
62
|
+
for (let i = 0; i < data.length;) {
|
|
63
|
+
// must be >0 for it to apply
|
|
64
|
+
if (packet.maxBatchSize > 0 && result.length > packet.maxBatchSize)
|
|
65
|
+
return "Too big of batch";
|
|
66
|
+
// read batch length
|
|
67
|
+
const [off, varint] = (0, CompressionUtil_1.readVarInt)(data, i);
|
|
68
|
+
i = off;
|
|
69
|
+
// if it goes oob it's invalid
|
|
70
|
+
if (i + varint > data.length)
|
|
71
|
+
return "Tampered batch length";
|
|
72
|
+
// read sector
|
|
73
|
+
const sect = data.slice(i, i += varint);
|
|
74
|
+
// call the packets listeners
|
|
75
|
+
const listen = await packet.listen(sect, socket);
|
|
76
|
+
// if invalid, return that
|
|
77
|
+
if (typeof listen == 'string')
|
|
78
|
+
return "Batched packet: " + listen;
|
|
79
|
+
// store result
|
|
80
|
+
result.push([listen[0], !packet.dontSpread]);
|
|
81
|
+
}
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
exports.BatchHelper = BatchHelper;
|
|
@@ -1,16 +1,12 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright 2026 Lily (liwybloc)
|
|
3
|
-
* Licensed under the Apache License, Version 2.0.
|
|
4
|
-
*/
|
|
5
1
|
export declare const MAX_BYTE = 255;
|
|
6
2
|
export declare const NEGATIVE_BYTE = 127;
|
|
7
3
|
export declare const MAX_SHORT = 65535;
|
|
8
4
|
export declare const SHORT_CC_OVERFLOW: number;
|
|
9
|
-
export declare const UVARINT_OVERFLOW: number, VARINT_CHAIN_FLAG = 128,
|
|
10
|
-
NEGATIVE_VARINT: number,
|
|
11
|
-
VARINT_OVERFLOW: number,
|
|
12
|
-
MAX_VSECT_SIZE = 7,
|
|
13
|
-
MAX_UVARINT: number,
|
|
5
|
+
export declare const UVARINT_OVERFLOW: number, VARINT_CHAIN_FLAG = 128, // flag for chaining
|
|
6
|
+
NEGATIVE_VARINT: number, // splitting usable range in half for signed
|
|
7
|
+
VARINT_OVERFLOW: number, // for varint to overflow with negative; is 64
|
|
8
|
+
MAX_VSECT_SIZE = 7, // max continues
|
|
9
|
+
MAX_UVARINT: number, // max value from this, subtract one for overflow
|
|
14
10
|
MAX_VARINT: number;
|
|
15
11
|
export declare const ONE_EIGHT: number, ONE_FOURTH: number;
|
|
16
12
|
export declare const EMPTY_UINT8: Uint8Array<ArrayBuffer>;
|
|
@@ -1,6 +1,277 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
2
3
|
* Copyright 2026 Lily (liwybloc)
|
|
3
|
-
*
|
|
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.
|
|
4
16
|
*/
|
|
5
|
-
|
|
6
|
-
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.bitsToBytes = exports.bytesToBits = exports.decompressBools = exports.compressBools = exports.EMPTY_UINT8 = exports.ONE_FOURTH = exports.ONE_EIGHT = exports.MAX_VARINT = exports.MAX_UVARINT = exports.MAX_VSECT_SIZE = exports.VARINT_OVERFLOW = exports.NEGATIVE_VARINT = exports.VARINT_CHAIN_FLAG = exports.UVARINT_OVERFLOW = exports.SHORT_CC_OVERFLOW = exports.MAX_SHORT = exports.NEGATIVE_BYTE = exports.MAX_BYTE = void 0;
|
|
19
|
+
exports.fromShort = fromShort;
|
|
20
|
+
exports.toShort = toShort;
|
|
21
|
+
exports.toByte = toByte;
|
|
22
|
+
exports.convertFloat = convertFloat;
|
|
23
|
+
exports.deconvertFloat = deconvertFloat;
|
|
24
|
+
exports.convertDouble = convertDouble;
|
|
25
|
+
exports.deconvertDouble = deconvertDouble;
|
|
26
|
+
exports.mapZigZag = mapZigZag;
|
|
27
|
+
exports.demapZigZag = demapZigZag;
|
|
28
|
+
exports.demapShort_ZZ = demapShort_ZZ;
|
|
29
|
+
exports.mapShort_ZZ = mapShort_ZZ;
|
|
30
|
+
exports.convertVarInt = convertVarInt;
|
|
31
|
+
exports.readVarInt = readVarInt;
|
|
32
|
+
exports.deconvertVarInts = deconvertVarInts;
|
|
33
|
+
exports.compressGzip = compressGzip;
|
|
34
|
+
exports.decompressGzip = decompressGzip;
|
|
35
|
+
exports.bytesToHex = bytesToHex;
|
|
36
|
+
exports.hexToBytes = hexToBytes;
|
|
37
|
+
const BufferUtil_1 = require("../BufferUtil");
|
|
38
|
+
// this shit is so complex so i commented it...
|
|
39
|
+
// the highest 8-bit
|
|
40
|
+
exports.MAX_BYTE = 0xFF;
|
|
41
|
+
// we split the usable range in half to separate positive and negative encodings
|
|
42
|
+
exports.NEGATIVE_BYTE = 0x7F;
|
|
43
|
+
// the highest 16-bit
|
|
44
|
+
exports.MAX_SHORT = 0xFFFF;
|
|
45
|
+
// overflow for shorts in construction
|
|
46
|
+
exports.SHORT_CC_OVERFLOW = exports.MAX_BYTE + 1;
|
|
47
|
+
// for varint to overflow; is 128
|
|
48
|
+
exports.UVARINT_OVERFLOW = exports.NEGATIVE_BYTE + 1, exports.VARINT_CHAIN_FLAG = 0x80, exports.NEGATIVE_VARINT = Math.floor(exports.NEGATIVE_BYTE / 2), exports.VARINT_OVERFLOW = exports.NEGATIVE_VARINT + 1, exports.MAX_VSECT_SIZE = 7, exports.MAX_UVARINT = (exports.UVARINT_OVERFLOW ** exports.MAX_VSECT_SIZE) - 1, exports.MAX_VARINT = Math.floor(exports.MAX_UVARINT / 2); // max for negatives
|
|
49
|
+
// constants
|
|
50
|
+
exports.ONE_EIGHT = 1 / 8, exports.ONE_FOURTH = 1 / 4;
|
|
51
|
+
exports.EMPTY_UINT8 = new Uint8Array([]);
|
|
52
|
+
const TWO_POWS = [];
|
|
53
|
+
const twoPow = (num) => TWO_POWS[num] ??= Math.pow(2, num);
|
|
54
|
+
// reconstruction
|
|
55
|
+
function fromShort(short) {
|
|
56
|
+
// convert to number
|
|
57
|
+
return short[0] * exports.SHORT_CC_OVERFLOW + short[1];
|
|
58
|
+
}
|
|
59
|
+
// checks, conversion
|
|
60
|
+
function toShort(n) {
|
|
61
|
+
// no nan/infinity
|
|
62
|
+
if (!isFinite(n))
|
|
63
|
+
throw new Error("Can only use real numbers in shorts: " + n);
|
|
64
|
+
// limit check
|
|
65
|
+
if (n > exports.MAX_SHORT || n < 0)
|
|
66
|
+
throw new Error(`Short Numbers must be within range 0 and ${exports.MAX_SHORT}`);
|
|
67
|
+
// how many times it passes SHORT_OVERFLOW and the remainder
|
|
68
|
+
return [Math.floor(n / exports.SHORT_CC_OVERFLOW), n % exports.SHORT_CC_OVERFLOW];
|
|
69
|
+
}
|
|
70
|
+
// checks
|
|
71
|
+
function toByte(n) {
|
|
72
|
+
// no nan/infinity
|
|
73
|
+
if (!isFinite(n))
|
|
74
|
+
throw new Error("Can only use real numbers in bytes: " + n);
|
|
75
|
+
;
|
|
76
|
+
// limit check
|
|
77
|
+
if (n > exports.MAX_BYTE || n < -exports.MAX_BYTE - 1)
|
|
78
|
+
throw new Error(`Byte Numbers must be within range -${exports.MAX_BYTE + 1} and ${exports.MAX_BYTE}: ${n}`);
|
|
79
|
+
return n;
|
|
80
|
+
}
|
|
81
|
+
// boolean stuff
|
|
82
|
+
const compressBools = (array) => array.reduce((byte, val, i) => byte | (val << (7 - i)), 0);
|
|
83
|
+
exports.compressBools = compressBools;
|
|
84
|
+
const decompressBools = (byte) => [...Array(8)].map((_, i) => (byte & (1 << (7 - i))) !== 0);
|
|
85
|
+
exports.decompressBools = decompressBools;
|
|
86
|
+
// IEEE-754 single-precision float codec
|
|
87
|
+
const MAN_BITS = 23;
|
|
88
|
+
function parseBin(str) {
|
|
89
|
+
return parseInt(str, 2);
|
|
90
|
+
}
|
|
91
|
+
function parseMan(bin, isNormal) {
|
|
92
|
+
const mantissaInt = parseBin(bin);
|
|
93
|
+
let fraction = 0;
|
|
94
|
+
for (let i = 0; i < MAN_BITS; i++) {
|
|
95
|
+
if (mantissaInt & (1 << (MAN_BITS - i - 1))) {
|
|
96
|
+
fraction += twoPow(-(i + 1));
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return (isNormal ? 1 : 0) + fraction;
|
|
100
|
+
}
|
|
101
|
+
// constants for floating point numbers
|
|
102
|
+
const FLOAT_EXPSIZE = 8, FLOAT_FRACTSIZE = 23;
|
|
103
|
+
const DOUBLE_EXPSIZE = 11, DOUBLE_FRACTSIZE = 52;
|
|
104
|
+
const FLOAT_SPECIAL = 0xFF, DOUBLE_SPECIAL = 0x7FF;
|
|
105
|
+
// assembles floating point values into 4 bytes
|
|
106
|
+
function assembleFloatingPoint(expSize, fractSize, sign, exponent, mantissa) {
|
|
107
|
+
const bin = sign.toString(2) + exponent.toString(2).padStart(expSize, "0") + mantissa.toString(2).padStart(fractSize, "0");
|
|
108
|
+
return (0, BufferUtil_1.splitArray)(Array.from(bin), 8).map((x) => parseBin(x.join("")));
|
|
109
|
+
}
|
|
110
|
+
function assembleSingleFloat(sign, exponent, mantissa) {
|
|
111
|
+
return assembleFloatingPoint(FLOAT_EXPSIZE, FLOAT_FRACTSIZE, sign, exponent, mantissa);
|
|
112
|
+
}
|
|
113
|
+
function assembleDoubleFloat(sign, exponent, mantissa) {
|
|
114
|
+
return assembleFloatingPoint(DOUBLE_EXPSIZE, DOUBLE_FRACTSIZE, sign, exponent, mantissa);
|
|
115
|
+
}
|
|
116
|
+
// https://stackoverflow.com/questions/3096646/how-to-convert-a-floating-point-number-to-its-binary-representation-ieee-754-i
|
|
117
|
+
// edited for clarity
|
|
118
|
+
function convertFloat(flt) {
|
|
119
|
+
if (isNaN(flt)) // Special case: NaN
|
|
120
|
+
return assembleSingleFloat(0, FLOAT_SPECIAL, 1); // Mantissa is nonzero for NaN
|
|
121
|
+
const sign = (flt < 0) ? 1 : 0;
|
|
122
|
+
flt = Math.abs(flt);
|
|
123
|
+
if (flt == 0.0) // Special case: +-0
|
|
124
|
+
return assembleSingleFloat(sign, 0, 0);
|
|
125
|
+
const exponent = Math.floor(Math.log(flt) / Math.LN2);
|
|
126
|
+
if (exponent > 127 || exponent < -126) // Special case: +-Infinity (and huge numbers)
|
|
127
|
+
return assembleSingleFloat(sign, 0xFF, 0); // Mantissa is zero for +-Infinity
|
|
128
|
+
const mantissa = flt / twoPow(exponent);
|
|
129
|
+
const roundMan = Math.round((mantissa - 1) * twoPow(23));
|
|
130
|
+
return assembleSingleFloat(sign, exponent + 127, roundMan & 0x7FFFFF);
|
|
131
|
+
}
|
|
132
|
+
function deconvertFloat(bytes) {
|
|
133
|
+
const bin = bytes.map(x => x.toString(2).padStart(8, "0")).join("");
|
|
134
|
+
// bit 1 = sign
|
|
135
|
+
// bits 2-9 = exponent
|
|
136
|
+
// bits 10-32 = mantissa
|
|
137
|
+
const sign = parseBin(bin[0]);
|
|
138
|
+
const rawExp = parseBin(bin.slice(1, 9));
|
|
139
|
+
const exp = rawExp == 0 ? -126 : rawExp - 127;
|
|
140
|
+
const man = parseMan(bin.slice(9, 32), rawExp != 0); // whether to add the implicit 1
|
|
141
|
+
let result;
|
|
142
|
+
if (rawExp == FLOAT_SPECIAL) {
|
|
143
|
+
result = man == 0 ? Infinity : NaN;
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
result = man * twoPow(exp);
|
|
147
|
+
}
|
|
148
|
+
return sign == 1 ? -result : result;
|
|
149
|
+
}
|
|
150
|
+
// https://stackoverflow.com/questions/72659156/convert-double-to-integer-mantissa-and-exponents
|
|
151
|
+
// turned to javascript
|
|
152
|
+
function convertDouble(double) {
|
|
153
|
+
if (isNaN(double))
|
|
154
|
+
return assembleDoubleFloat(0, DOUBLE_SPECIAL, 1); // nonzero for nan. sign doesnt effect anything
|
|
155
|
+
const sign = double < 0 ? 1 : 0;
|
|
156
|
+
if (!isFinite(double))
|
|
157
|
+
return assembleDoubleFloat(sign, DOUBLE_SPECIAL, 0); // zero for infinity
|
|
158
|
+
double = Math.abs(double);
|
|
159
|
+
let exponent, significand;
|
|
160
|
+
if (double == 0) {
|
|
161
|
+
exponent = 0;
|
|
162
|
+
significand = 0;
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
exponent = Math.floor(Math.log2(double)) - 51;
|
|
166
|
+
significand = Math.floor(double / twoPow(exponent));
|
|
167
|
+
}
|
|
168
|
+
return assembleDoubleFloat(sign, exponent + 1023, significand);
|
|
169
|
+
}
|
|
170
|
+
function deconvertDouble(bytes) {
|
|
171
|
+
const bin = bytes.map(x => x.toString(2).padStart(8, "0")).join("");
|
|
172
|
+
// bit 1 = sign
|
|
173
|
+
// bits 2-12 = exponent
|
|
174
|
+
// bits 13-64 = mantissa
|
|
175
|
+
const sign = parseBin(bin[0]);
|
|
176
|
+
const rawExp = parseBin(bin.slice(1, 12));
|
|
177
|
+
const exp = rawExp == 0 ? -1022 : rawExp - 1023;
|
|
178
|
+
const man = parseBin(bin.slice(12, 64));
|
|
179
|
+
let result;
|
|
180
|
+
if (rawExp == DOUBLE_SPECIAL) {
|
|
181
|
+
result = man == 0 ? Infinity : NaN;
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
result = man * twoPow(exp);
|
|
185
|
+
}
|
|
186
|
+
return sign == 1 ? -result : result;
|
|
187
|
+
}
|
|
188
|
+
// zig_zag
|
|
189
|
+
function mapZigZag(n) {
|
|
190
|
+
return ((n << 1) ^ (n >> 31));
|
|
191
|
+
}
|
|
192
|
+
function demapZigZag(n) {
|
|
193
|
+
return (n >>> 1) ^ -((n & 1));
|
|
194
|
+
}
|
|
195
|
+
function demapShort_ZZ(short) {
|
|
196
|
+
return demapZigZag(fromShort(short));
|
|
197
|
+
}
|
|
198
|
+
function mapShort_ZZ(short) {
|
|
199
|
+
return toShort(mapZigZag(short));
|
|
200
|
+
}
|
|
201
|
+
// yeah!
|
|
202
|
+
function convertVarInt(num) {
|
|
203
|
+
if (num > exports.MAX_UVARINT || num < 0)
|
|
204
|
+
throw new Error(`Variable Ints must be within range 0 and ${exports.MAX_VARINT}: ${num}`);
|
|
205
|
+
if (num === 0)
|
|
206
|
+
return [0];
|
|
207
|
+
const result = [];
|
|
208
|
+
while (num > 0) {
|
|
209
|
+
let byte = num & 0x7F; // take 7 bits
|
|
210
|
+
num >>>= 7;
|
|
211
|
+
if (num > 0)
|
|
212
|
+
byte |= exports.VARINT_CHAIN_FLAG;
|
|
213
|
+
result.push(byte);
|
|
214
|
+
}
|
|
215
|
+
return result;
|
|
216
|
+
}
|
|
217
|
+
function readVarInt(arr, off) {
|
|
218
|
+
let num = 0;
|
|
219
|
+
let shift = 0;
|
|
220
|
+
let byte;
|
|
221
|
+
do {
|
|
222
|
+
byte = arr[off++];
|
|
223
|
+
num += (byte & ~exports.VARINT_CHAIN_FLAG) << shift;
|
|
224
|
+
shift += 7;
|
|
225
|
+
} while ((byte & exports.VARINT_CHAIN_FLAG) !== 0);
|
|
226
|
+
return [off, num];
|
|
227
|
+
}
|
|
228
|
+
function deconvertVarInts(arr) {
|
|
229
|
+
let res = [];
|
|
230
|
+
let i = 0;
|
|
231
|
+
while (i < arr.length) {
|
|
232
|
+
const [off, varint] = readVarInt(arr, i);
|
|
233
|
+
res.push(varint);
|
|
234
|
+
i = off;
|
|
235
|
+
}
|
|
236
|
+
return res;
|
|
237
|
+
}
|
|
238
|
+
const bytesToBits = (bytes) => Array.from(bytes).map(b => b.toString(2).padStart(8, '0')).join('');
|
|
239
|
+
exports.bytesToBits = bytesToBits;
|
|
240
|
+
const bitsToBytes = (bits) => new Uint8Array(bits.match(/.{1,8}/g)?.map(b => parseInt(b.padEnd(8, '0'), 2)) ?? []);
|
|
241
|
+
exports.bitsToBytes = bitsToBytes;
|
|
242
|
+
const gzipError = "Your browser is too old to support compression. Please update!";
|
|
243
|
+
async function compressGzip(data, ident = "") {
|
|
244
|
+
if (typeof CompressionStream === "undefined") {
|
|
245
|
+
if (typeof window !== "undefined")
|
|
246
|
+
window.alert(gzipError);
|
|
247
|
+
throw new Error(gzipError);
|
|
248
|
+
}
|
|
249
|
+
const stream = new Blob([data]).stream().pipeThrough(new CompressionStream("deflate-raw"));
|
|
250
|
+
const buffer = await new Response(stream).arrayBuffer();
|
|
251
|
+
if (data.length <= buffer.byteLength && ident != "") {
|
|
252
|
+
console.warn("WARN: Packet '" + ident + "' is small, and compressing it makes the size bigger!");
|
|
253
|
+
}
|
|
254
|
+
return new Uint8Array(buffer);
|
|
255
|
+
}
|
|
256
|
+
async function decompressGzip(data) {
|
|
257
|
+
if (typeof DecompressionStream === "undefined") {
|
|
258
|
+
if (typeof window !== "undefined")
|
|
259
|
+
window.alert(gzipError);
|
|
260
|
+
throw new Error(gzipError);
|
|
261
|
+
}
|
|
262
|
+
const stream = new Blob([data]).stream().pipeThrough(new DecompressionStream("deflate-raw"));
|
|
263
|
+
const buffer = await new Response(stream).arrayBuffer();
|
|
264
|
+
return new Uint8Array(buffer);
|
|
265
|
+
}
|
|
266
|
+
function bytesToHex(bytes) {
|
|
267
|
+
return Array.from(bytes, b => b.toString(16).padStart(2, '0')).join('');
|
|
268
|
+
}
|
|
269
|
+
;
|
|
270
|
+
function hexToBytes(hex) {
|
|
271
|
+
const bytes = new Uint8Array(hex.length / 2);
|
|
272
|
+
for (let i = 0; i < hex.length; i += 2) {
|
|
273
|
+
bytes[i / 2] = parseInt(hex.substring(i, i + 2), 16);
|
|
274
|
+
}
|
|
275
|
+
return bytes;
|
|
276
|
+
}
|
|
277
|
+
;
|
|
@@ -1,6 +1,122 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
2
3
|
* Copyright 2026 Lily (liwybloc)
|
|
3
|
-
*
|
|
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.
|
|
4
16
|
*/
|
|
5
|
-
|
|
6
|
-
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.setHashFunc = setHashFunc;
|
|
19
|
+
exports.hashValue = hashValue;
|
|
20
|
+
const HASH_INIT_64 = 14695981039346656037n;
|
|
21
|
+
const HASH_PRIME_64 = 1099511628211n;
|
|
22
|
+
const MASK_64 = (1n << 64n) - 1n;
|
|
23
|
+
const hashValue64 = (value) => {
|
|
24
|
+
let hash = HASH_INIT_64;
|
|
25
|
+
const walk = (v) => {
|
|
26
|
+
if (v === null) {
|
|
27
|
+
hash ^= 0n;
|
|
28
|
+
hash = (hash * HASH_PRIME_64) & MASK_64;
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
const t = typeof v;
|
|
32
|
+
if (t === "number") {
|
|
33
|
+
hash ^= BigInt(Math.trunc(v));
|
|
34
|
+
hash = (hash * HASH_PRIME_64) & MASK_64;
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
if (t === "string") {
|
|
38
|
+
for (let i = 0; i < v.length; i++) {
|
|
39
|
+
hash ^= BigInt(v.charCodeAt(i));
|
|
40
|
+
hash = (hash * HASH_PRIME_64) & MASK_64;
|
|
41
|
+
}
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
if (t === "boolean") {
|
|
45
|
+
hash ^= v ? 1n : 0n;
|
|
46
|
+
hash = (hash * HASH_PRIME_64) & MASK_64;
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
if (Array.isArray(v)) {
|
|
50
|
+
for (let i = 0; i < v.length; i++) {
|
|
51
|
+
walk(v[i]);
|
|
52
|
+
}
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (t === "object") {
|
|
56
|
+
const keys = Object.keys(v).sort();
|
|
57
|
+
for (let i = 0; i < keys.length; i++) {
|
|
58
|
+
const k = keys[i];
|
|
59
|
+
for (let j = 0; j < k.length; j++) {
|
|
60
|
+
hash ^= BigInt(k.charCodeAt(j));
|
|
61
|
+
hash = (hash * HASH_PRIME_64) & MASK_64;
|
|
62
|
+
}
|
|
63
|
+
walk(v[k]);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
walk(value);
|
|
68
|
+
return hash;
|
|
69
|
+
};
|
|
70
|
+
const HASH_INIT = 2166136261;
|
|
71
|
+
const hashValue32 = (value) => {
|
|
72
|
+
let hash = HASH_INIT;
|
|
73
|
+
const walk = (v) => {
|
|
74
|
+
if (v === null) {
|
|
75
|
+
hash ^= 0;
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
const t = typeof v;
|
|
79
|
+
if (t === "number") {
|
|
80
|
+
hash ^= v | 0;
|
|
81
|
+
hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
if (t === "string") {
|
|
85
|
+
for (let i = 0; i < v.length; i++) {
|
|
86
|
+
hash ^= v.charCodeAt(i);
|
|
87
|
+
hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
|
|
88
|
+
}
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
if (t === "boolean") {
|
|
92
|
+
hash ^= v ? 1 : 0;
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
if (Array.isArray(v)) {
|
|
96
|
+
for (let i = 0; i < v.length; i++) {
|
|
97
|
+
walk(v[i]);
|
|
98
|
+
}
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
if (t === "object") {
|
|
102
|
+
const keys = Object.keys(v).sort();
|
|
103
|
+
for (let i = 0; i < keys.length; i++) {
|
|
104
|
+
const k = keys[i];
|
|
105
|
+
for (let j = 0; j < k.length; j++) {
|
|
106
|
+
hash ^= k.charCodeAt(j);
|
|
107
|
+
hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
|
|
108
|
+
}
|
|
109
|
+
walk(v[k]);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
walk(value);
|
|
114
|
+
return hash >>> 0;
|
|
115
|
+
};
|
|
116
|
+
let hashFunc = hashValue64;
|
|
117
|
+
function setHashFunc(use64Bit) {
|
|
118
|
+
hashFunc = use64Bit ? hashValue64 : hashValue32;
|
|
119
|
+
}
|
|
120
|
+
function hashValue(value) {
|
|
121
|
+
return hashFunc(value);
|
|
122
|
+
}
|