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.
Files changed (46) hide show
  1. package/dist/index.d.ts +0 -4
  2. package/dist/version.d.ts +1 -5
  3. package/dist/version.js +23 -4
  4. package/dist/ws/Connection.d.ts +0 -4
  5. package/dist/ws/Connection.js +124 -4
  6. package/dist/ws/PacketProcessor.d.ts +0 -4
  7. package/dist/ws/PacketProcessor.js +50 -4
  8. package/dist/ws/client/core/ClientCore.d.ts +0 -4
  9. package/dist/ws/client/core/ClientCore.js +249 -4
  10. package/dist/ws/client/node/ClientNode.d.ts +0 -4
  11. package/dist/ws/client/node/ClientNode.js +34 -4
  12. package/dist/ws/debug/DebugServer.d.ts +0 -4
  13. package/dist/ws/debug/DebugServer.js +605 -4
  14. package/dist/ws/packets/PacketProcessors.d.ts +0 -4
  15. package/dist/ws/packets/PacketProcessors.js +315 -4
  16. package/dist/ws/packets/PacketType.d.ts +0 -4
  17. package/dist/ws/packets/PacketType.js +59 -4
  18. package/dist/ws/packets/Packets.d.ts +0 -4
  19. package/dist/ws/packets/Packets.js +275 -4
  20. package/dist/ws/server/SonicWSConnection.d.ts +0 -4
  21. package/dist/ws/server/SonicWSConnection.js +296 -4
  22. package/dist/ws/server/SonicWSServer.d.ts +1 -5
  23. package/dist/ws/server/SonicWSServer.js +305 -4
  24. package/dist/ws/util/BufferUtil.d.ts +0 -4
  25. package/dist/ws/util/BufferUtil.js +40 -4
  26. package/dist/ws/util/StringUtil.d.ts +0 -4
  27. package/dist/ws/util/StringUtil.js +49 -4
  28. package/dist/ws/util/enums/EnumHandler.d.ts +0 -4
  29. package/dist/ws/util/enums/EnumHandler.js +58 -4
  30. package/dist/ws/util/enums/EnumType.d.ts +0 -4
  31. package/dist/ws/util/enums/EnumType.js +70 -4
  32. package/dist/ws/util/packets/BatchHelper.d.ts +0 -4
  33. package/dist/ws/util/packets/BatchHelper.js +83 -4
  34. package/dist/ws/util/packets/CompressionUtil.d.ts +5 -9
  35. package/dist/ws/util/packets/CompressionUtil.js +275 -4
  36. package/dist/ws/util/packets/HashUtil.d.ts +0 -4
  37. package/dist/ws/util/packets/HashUtil.js +120 -4
  38. package/dist/ws/util/packets/JSONUtil.d.ts +0 -4
  39. package/dist/ws/util/packets/JSONUtil.js +177 -4
  40. package/dist/ws/util/packets/PacketHolder.d.ts +0 -4
  41. package/dist/ws/util/packets/PacketHolder.js +124 -4
  42. package/dist/ws/util/packets/PacketUtils.d.ts +0 -4
  43. package/dist/ws/util/packets/PacketUtils.js +281 -4
  44. package/dist/ws/util/packets/RateHandler.d.ts +0 -4
  45. package/dist/ws/util/packets/RateHandler.js +64 -4
  46. package/package.json +3 -3
@@ -1,6 +1,85 @@
1
- /**
1
+ "use strict";
2
+ /*
2
3
  * Copyright 2026 Lily (liwybloc)
3
- * Licensed under the Apache License, Version 2.0.
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
- Object.defineProperty(exports,"__esModule",{value:!0}),exports.BatchHelper=void 0;const t=require("../BufferUtil"),e=require("./CompressionUtil");exports.BatchHelper=class{batchInfo={};batchTimeouts={};batchedData={};conn;registerSendPackets(t,e){this.conn=e,t.getTags().forEach(e=>{const a=t.getPacket(e);if(0==a.dataBatching)return;const s=t.getKey(e);this.initiateBatch(s,a.dataBatching,a.gzipCompression)})}initiateBatch(t,e,a){this.batchedData[t]=[],this.batchInfo[t]=[e,a]}startBatch(a){const[s,c]=this.batchInfo[a];this.batchTimeouts[a]=this.conn.setInterval(async()=>{if(0==this.batchedData[a].length)return;const s=new Uint8Array(this.batchedData[a]);this.conn.raw_send((0,t.toPacketBuffer)(a,c?await(0,e.compressGzip)(s):s)),this.batchedData[a]=[],delete this.batchTimeouts[a]},s)}batchPacket(t,a){const s=this.batchedData[t];s.push(...(0,e.convertVarInt)(a.length)),a.forEach(t=>s.push(t)),this.batchTimeouts[t]||this.startBatch(t)}static async unravelBatch(t,a,s){const c=t.gzipCompression?await(0,e.decompressGzip)(a):a,i=[];for(let a=0;a<c.length;){if(t.maxBatchSize>0&&i.length>t.maxBatchSize)return"Too big of batch";const[h,n]=(0,e.readVarInt)(c,a);if(a=h,a+n>c.length)return"Tampered batch length";const r=c.slice(a,a+=n),o=await t.listen(r,s);if("string"==typeof o)return"Batched packet: "+o;i.push([o[0],!t.dontSpread])}return i}};
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
- * Licensed under the Apache License, Version 2.0.
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
- Object.defineProperty(exports,"__esModule",{value:!0}),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,exports.fromShort=o,exports.toShort=n,exports.toByte=function(t){if(!isFinite(t))throw new Error("Can only use real numbers in bytes: "+t);if(t>exports.MAX_BYTE||t<-exports.MAX_BYTE-1)throw new Error(`Byte Numbers must be within range -${exports.MAX_BYTE+1} and ${exports.MAX_BYTE}: ${t}`);return t},exports.convertFloat=function(t){if(isNaN(t))return T(0,A,1);const r=t<0?1:0;if(0==(t=Math.abs(t)))return T(r,0,0);const o=Math.floor(Math.log(t)/Math.LN2);if(o>127||o<-126)return T(r,255,0);const n=t/e(o),s=Math.round((n-1)*e(23));return T(r,o+127,8388607&s)},exports.deconvertFloat=function(t){const r=t.map(t=>t.toString(2).padStart(8,"0")).join(""),o=p(r[0]),n=p(r.slice(1,9)),i=0==n?-126:n-127,a=function(t,r){const o=p(t);let n=0;for(let t=0;t<s;t++)o&1<<s-t-1&&(n+=e(-(t+1)));return(r?1:0)+n}(r.slice(9,32),0!=n);let u;u=n==A?0==a?1/0:NaN:a*e(i);return 1==o?-u:u},exports.convertDouble=function(t){if(isNaN(t))return _(0,c,1);const r=t<0?1:0;if(!isFinite(t))return _(r,c,0);let o,n;0==(t=Math.abs(t))?(o=0,n=0):(o=Math.floor(Math.log2(t))-51,n=Math.floor(t/e(o)));return _(r,o+1023,n)},exports.deconvertDouble=function(t){const r=t.map(t=>t.toString(2).padStart(8,"0")).join(""),o=p(r[0]),n=p(r.slice(1,12)),s=0==n?-1022:n-1023,i=p(r.slice(12,64));let a;a=n==c?0==i?1/0:NaN:i*e(s);return 1==o?-a:a},exports.mapZigZag=E,exports.demapZigZag=l,exports.demapShort_ZZ=function(t){return l(o(t))},exports.mapShort_ZZ=function(t){return n(E(t))},exports.convertVarInt=function(t){if(t>exports.MAX_UVARINT||t<0)throw new Error(`Variable Ints must be within range 0 and ${exports.MAX_VARINT}: ${t}`);if(0===t)return[0];const r=[];for(;t>0;){let e=127&t;(t>>>=7)>0&&(e|=exports.VARINT_CHAIN_FLAG),r.push(e)}return r},exports.readVarInt=N,exports.deconvertVarInts=function(t){let r=[],e=0;for(;e<t.length;){const[o,n]=N(t,e);r.push(n),e=o}return r},exports.compressGzip=async function(t,r=""){if("undefined"==typeof CompressionStream)throw"undefined"!=typeof window&&window.alert(I),new Error(I);const e=new Blob([t]).stream().pipeThrough(new CompressionStream("deflate-raw")),o=await new Response(e).arrayBuffer();t.length<=o.byteLength&&""!=r&&console.warn("WARN: Packet '"+r+"' is small, and compressing it makes the size bigger!");return new Uint8Array(o)},exports.decompressGzip=async function(t){if("undefined"==typeof DecompressionStream)throw"undefined"!=typeof window&&window.alert(I),new Error(I);const r=new Blob([t]).stream().pipeThrough(new DecompressionStream("deflate-raw")),e=await new Response(r).arrayBuffer();return new Uint8Array(e)},exports.bytesToHex=function(t){return Array.from(t,t=>t.toString(16).padStart(2,"0")).join("")},exports.hexToBytes=function(t){const r=new Uint8Array(t.length/2);for(let e=0;e<t.length;e+=2)r[e/2]=parseInt(t.substring(e,e+2),16);return r};const t=require("../BufferUtil");exports.MAX_BYTE=255,exports.NEGATIVE_BYTE=127,exports.MAX_SHORT=65535,exports.SHORT_CC_OVERFLOW=exports.MAX_BYTE+1,exports.UVARINT_OVERFLOW=exports.NEGATIVE_BYTE+1,exports.VARINT_CHAIN_FLAG=128,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),exports.ONE_EIGHT=1/8,exports.ONE_FOURTH=1/4,exports.EMPTY_UINT8=new Uint8Array([]);const r=[],e=t=>r[t]??=Math.pow(2,t);function o(t){return t[0]*exports.SHORT_CC_OVERFLOW+t[1]}function n(t){if(!isFinite(t))throw new Error("Can only use real numbers in shorts: "+t);if(t>exports.MAX_SHORT||t<0)throw new Error(`Short Numbers must be within range 0 and ${exports.MAX_SHORT}`);return[Math.floor(t/exports.SHORT_CC_OVERFLOW),t%exports.SHORT_CC_OVERFLOW]}exports.compressBools=t=>t.reduce((t,r,e)=>t|r<<7-e,0);exports.decompressBools=t=>[...Array(8)].map((r,e)=>!!(t&1<<7-e));const s=23;function p(t){return parseInt(t,2)}const i=8,a=23,u=11,x=52,A=255,c=2047;function f(r,e,o,n,s){const i=o.toString(2)+n.toString(2).padStart(r,"0")+s.toString(2).padStart(e,"0");return(0,t.splitArray)(Array.from(i),8).map(t=>p(t.join("")))}function T(t,r,e){return f(i,a,t,r,e)}function _(t,r,e){return f(u,x,t,r,e)}function E(t){return t<<1^t>>31}function l(t){return t>>>1^-(1&t)}function N(t,r){let e,o=0,n=0;do{e=t[r++],o+=(e&~exports.VARINT_CHAIN_FLAG)<<n,n+=7}while(0!==(e&exports.VARINT_CHAIN_FLAG));return[r,o]}exports.bytesToBits=t=>Array.from(t).map(t=>t.toString(2).padStart(8,"0")).join("");exports.bitsToBytes=t=>new Uint8Array(t.match(/.{1,8}/g)?.map(t=>parseInt(t.padEnd(8,"0"),2))??[]);const I="Your browser is too old to support compression. Please update!";
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,2 @@
1
- /**
2
- * Copyright 2026 Lily (liwybloc)
3
- * Licensed under the Apache License, Version 2.0.
4
- */
5
1
  export declare function setHashFunc(use64Bit: boolean): void;
6
2
  export declare function hashValue(value: any): bigint | number;
@@ -1,6 +1,122 @@
1
- /**
1
+ "use strict";
2
+ /*
2
3
  * Copyright 2026 Lily (liwybloc)
3
- * Licensed under the Apache License, Version 2.0.
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
- Object.defineProperty(exports,"__esModule",{value:!0}),exports.setHashFunc=function(e){o=e?n:r},exports.hashValue=function(e){return o(e)};const e=1099511628211n,t=(1n<<64n)-1n,n=n=>{let r=14695981039346656037n;const o=n=>{if(null===n)return r^=0n,void(r=r*e&t);const l=typeof n;if("number"===l)return r^=BigInt(Math.trunc(n)),void(r=r*e&t);if("string"!==l){if("boolean"===l)return r^=n?1n:0n,void(r=r*e&t);if(Array.isArray(n))for(let e=0;e<n.length;e++)o(n[e]);else if("object"===l){const l=Object.keys(n).sort();for(let s=0;s<l.length;s++){const i=l[s];for(let n=0;n<i.length;n++)r^=BigInt(i.charCodeAt(n)),r=r*e&t;o(n[i])}}}else for(let o=0;o<n.length;o++)r^=BigInt(n.charCodeAt(o)),r=r*e&t};return o(n),r},r=e=>{let t=2166136261;const n=e=>{if(null===e)return void(t^=0);const r=typeof e;if("number"===r)return t^=0|e,void(t+=(t<<1)+(t<<4)+(t<<7)+(t<<8)+(t<<24));if("string"!==r)if("boolean"!==r){if(Array.isArray(e))for(let t=0;t<e.length;t++)n(e[t]);else if("object"===r){const r=Object.keys(e).sort();for(let o=0;o<r.length;o++){const l=r[o];for(let e=0;e<l.length;e++)t^=l.charCodeAt(e),t+=(t<<1)+(t<<4)+(t<<7)+(t<<8)+(t<<24);n(e[l])}}}else t^=e?1:0;else for(let n=0;n<e.length;n++)t^=e.charCodeAt(n),t+=(t<<1)+(t<<4)+(t<<7)+(t<<8)+(t<<24)};return n(e),t>>>0};let o=n;
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
+ }
@@ -1,6 +1,2 @@
1
- /**
2
- * Copyright 2026 Lily (liwybloc)
3
- * Licensed under the Apache License, Version 2.0.
4
- */
5
1
  export declare const compressJSON: (value: any) => Uint8Array<ArrayBuffer>;
6
2
  export declare const decompressJSON: (bytes: Uint8Array) => any;