sonic-ws 1.3.0 → 1.3.2

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 (49) hide show
  1. package/README.md +50 -44
  2. package/dist/index.d.ts +4 -0
  3. package/dist/version.d.ts +5 -1
  4. package/dist/version.js +4 -23
  5. package/dist/ws/Connection.d.ts +52 -2
  6. package/dist/ws/Connection.js +4 -48
  7. package/dist/ws/PacketProcessor.d.ts +11 -3
  8. package/dist/ws/PacketProcessor.js +4 -17
  9. package/dist/ws/client/core/ClientCore.d.ts +17 -35
  10. package/dist/ws/client/core/ClientCore.js +4 -328
  11. package/dist/ws/client/node/ClientNode.d.ts +6 -2
  12. package/dist/ws/client/node/ClientNode.js +4 -34
  13. package/dist/ws/debug/DebugServer.d.ts +11 -0
  14. package/dist/ws/debug/DebugServer.js +6 -0
  15. package/dist/ws/packets/PacketProcessors.d.ts +6 -2
  16. package/dist/ws/packets/PacketProcessors.js +4 -314
  17. package/dist/ws/packets/PacketType.d.ts +4 -0
  18. package/dist/ws/packets/PacketType.js +4 -59
  19. package/dist/ws/packets/Packets.d.ts +12 -9
  20. package/dist/ws/packets/Packets.js +4 -313
  21. package/dist/ws/server/SonicWSConnection.d.ts +8 -35
  22. package/dist/ws/server/SonicWSConnection.js +4 -421
  23. package/dist/ws/server/SonicWSServer.d.ts +11 -10
  24. package/dist/ws/server/SonicWSServer.js +3 -906
  25. package/dist/ws/util/BufferUtil.d.ts +7 -2
  26. package/dist/ws/util/BufferUtil.js +4 -43
  27. package/dist/ws/util/StringUtil.d.ts +7 -4
  28. package/dist/ws/util/StringUtil.js +4 -64
  29. package/dist/ws/util/enums/EnumHandler.d.ts +4 -0
  30. package/dist/ws/util/enums/EnumHandler.js +4 -58
  31. package/dist/ws/util/enums/EnumType.d.ts +4 -12
  32. package/dist/ws/util/enums/EnumType.js +4 -69
  33. package/dist/ws/util/packets/BatchHelper.d.ts +5 -15
  34. package/dist/ws/util/packets/BatchHelper.js +4 -77
  35. package/dist/ws/util/packets/CompressionUtil.d.ts +17 -26
  36. package/dist/ws/util/packets/CompressionUtil.js +4 -533
  37. package/dist/ws/util/packets/HashUtil.d.ts +4 -0
  38. package/dist/ws/util/packets/HashUtil.js +4 -120
  39. package/dist/ws/util/packets/JSONUtil.d.ts +6 -0
  40. package/dist/ws/util/packets/JSONUtil.js +6 -0
  41. package/dist/ws/util/packets/PacketHolder.d.ts +3 -62
  42. package/dist/ws/util/packets/PacketHolder.js +4 -123
  43. package/dist/ws/util/packets/PacketUtils.d.ts +4 -17
  44. package/dist/ws/util/packets/PacketUtils.js +4 -272
  45. package/dist/ws/util/packets/RateHandler.d.ts +5 -14
  46. package/dist/ws/util/packets/RateHandler.js +4 -63
  47. package/package.json +4 -2
  48. package/dist/ws/util/ArrayUtil.d.ts +0 -1
  49. package/dist/ws/util/ArrayUtil.js +0 -24
@@ -1,122 +1,6 @@
1
- "use strict";
2
- /*
1
+ /**
3
2
  * Copyright 2026 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.
3
+ * Licensed under the Apache License, Version 2.0.
16
4
  */
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
- }
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;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Copyright 2026 Lily (liwybloc)
3
+ * Licensed under the Apache License, Version 2.0.
4
+ */
5
+ export declare const compressJSON: (value: any) => Uint8Array<ArrayBuffer>;
6
+ export declare const decompressJSON: (bytes: Uint8Array) => any;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Copyright 2026 Lily (liwybloc)
3
+ * Licensed under the Apache License, Version 2.0.
4
+ */
5
+
6
+ Object.defineProperty(exports,"__esModule",{value:!0}),exports.decompressJSON=exports.compressJSON=void 0;const e=require("../BufferUtil"),r=require("./CompressionUtil");var t;!function(e){e[e.NULL=0]="NULL",e[e.BOOL=1]="BOOL",e[e.INT=2]="INT",e[e.FLOAT=3]="FLOAT",e[e.STRING=4]="STRING",e[e.ARRAY=5]="ARRAY",e[e.OBJECT=6]="OBJECT"}(t||(t={}));const s=e=>{const t=(new TextEncoder).encode(e);return[...(0,r.convertVarInt)(t.length),...t]},n=(e,t)=>{const[s,n]=(0,r.readVarInt)(e,t);return{value:(new TextDecoder).decode(e.subarray(s,s+n)),length:s+n-t}};exports.compressJSON=n=>{const o=[],c=[],a=[],u=e=>{if(null===e)a.push(t.NULL);else if("boolean"==typeof e)a.push(t.BOOL),o.push(e);else if(Number.isInteger(e))a.push(t.INT),c.push(...(0,r.convertVarInt)((0,r.mapZigZag)(e)));else if("number"==typeof e)a.push(t.FLOAT),c.push(...(0,r.convertFloat)(e));else if("string"==typeof e)a.push(t.STRING),c.push(...s(e));else if(Array.isArray(e)){a.push(t.ARRAY),c.push(...(0,r.convertVarInt)(e.length));for(const r of e)u(r)}else{if("object"!=typeof e)throw new Error("Unsupported type");{a.push(t.OBJECT);const n=Object.keys(e);c.push(...(0,r.convertVarInt)(n.length));for(const r of n)c.push(...s(r)),u(e[r])}}};u(n);const p=o.length?(0,e.splitArray)(o,8).map(e=>(0,r.compressBools)(e)):[],l=(e=>{let t="";for(const r of e)t+=r.toString(2).padStart(3,"0");return(0,r.bitsToBytes)(t)})(a),h=[...(0,r.convertVarInt)(p.length),...(0,r.convertVarInt)(l.length)];return Uint8Array.from([...h,...p.flat(),...l,...c])};exports.decompressJSON=e=>{let s=0;const[o,c]=(0,r.readVarInt)(e,s);s=o;const[a,u]=(0,r.readVarInt)(e,s);s=a;const p=[];for(let t=0;t<c;t++)p.push(...(0,r.decompressBools)(e[s++]));let l=0;const h=e.subarray(s,s+u);s+=u;const f=((e,t)=>{const s=(0,r.bytesToBits)(e),n=[];for(let e=0;e<t;e++)n.push(parseInt(s.slice(3*e,3*e+3),2));return n})(h,8*h.length/3);let i=0;const d=o=>{if(o>500)throw new Error("JSON array too deep.");const c=f[i++];switch(c){case t.NULL:return null;case t.BOOL:return p[l++];case t.INT:{const[t,n]=(0,r.readVarInt)(e,s);return s=t,(0,r.demapZigZag)(n)}case t.FLOAT:{const t=(0,r.deconvertFloat)(Array.from(e.subarray(s,s+4)));return s+=4,t}case t.STRING:{const{value:r,length:t}=n(e,s);return s+=t,r}case t.ARRAY:{const[t,n]=(0,r.readVarInt)(e,s);s=t;const c=[];for(let e=0;e<n;e++)c.push(d(o+1));return c}case t.OBJECT:{const[t,c]=(0,r.readVarInt)(e,s);s=t;const a={};for(let r=0;r<c;r++){const{value:r,length:t}=n(e,s);s+=t,a[r]=d(o+1)}return a}default:throw new Error(`Unknown type ${c}`)}};return d(0)};
@@ -1,64 +1,5 @@
1
- import { Packet } from "../../packets/Packets";
2
- import { PacketTypings } from "../../server/SonicWSServer";
3
1
  /**
4
- * Holds and maps packets to indexed keys and tags for serialization and lookup
2
+ * Copyright 2026 Lily (liwybloc)
3
+ * Licensed under the Apache License, Version 2.0.
5
4
  */
6
- export declare class PacketHolder {
7
- /** Current key index for packet tags */
8
- private key;
9
- /** Maps tags to keys */
10
- private keys;
11
- /** Maps keys to tags */
12
- private tags;
13
- /** Maps tags to packet instances */
14
- private packetMap;
15
- /** List of all packet instances */
16
- private packets;
17
- /**
18
- * Creates a new PacketHolder with an array of packets
19
- * @param packets Array of packets to register
20
- */
21
- constructor(packets?: PacketTypings);
22
- /** Assigns a new unique key to a tag */
23
- createKey(tag: string): void;
24
- /**
25
- * Registers an array of packets and assigns them keys
26
- * @param packets Array of packets to register
27
- */
28
- holdPackets(packets: PacketTypings): void;
29
- /**
30
- * Returns the numeric key for a given tag
31
- * @param tag The packet tag
32
- */
33
- getKey(tag: string): number;
34
- /**
35
- * Returns the tag associated with a given character key
36
- * @param key Key bytre
37
- */
38
- getTag(key: number): string | undefined;
39
- /**
40
- * Returns the packet instance associated with a tag
41
- * @param tag The packet tag
42
- */
43
- getPacket(tag: string): Packet<any>;
44
- /**
45
- * Checks if a given character key exists
46
- * @param key A string index
47
- */
48
- hasKey(key: number): key is keyof typeof this.tags;
49
- /**
50
- * Checks if a tag has been assigned a key
51
- * @param tag The packet tag
52
- */
53
- hasTag(tag: string): boolean;
54
- /** Returns the mapping of tags to keys */
55
- getKeys(): Record<string, number>;
56
- /** Returns the mapping of keys to tags */
57
- getTagMap(): Record<number, string>;
58
- /** Returns an array of all registered tags */
59
- getTags(): string[];
60
- /** Returns the list of all registered packets */
61
- getPackets(): PacketTypings;
62
- /** Serializes all registered packets into a string */
63
- serialize(): number[];
64
- }
5
+ export {};
@@ -1,125 +1,6 @@
1
- "use strict";
2
- /*
3
- * Copyright 2026 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
- Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.PacketHolder = void 0;
19
1
  /**
20
- * Holds and maps packets to indexed keys and tags for serialization and lookup
2
+ * Copyright 2026 Lily (liwybloc)
3
+ * Licensed under the Apache License, Version 2.0.
21
4
  */
22
- class PacketHolder {
23
- /** Current key index for packet tags */
24
- key;
25
- /** Maps tags to keys */
26
- keys;
27
- /** Maps keys to tags */
28
- tags;
29
- /** Maps tags to packet instances */
30
- packetMap;
31
- /** List of all packet instances */
32
- packets;
33
- /**
34
- * Creates a new PacketHolder with an array of packets
35
- * @param packets Array of packets to register
36
- */
37
- constructor(packets) {
38
- // reserves:
39
- // 0 - enum update
40
- this.key = 1;
41
- this.keys = {};
42
- this.tags = {};
43
- this.packetMap = {};
44
- if (!packets)
45
- return;
46
- this.holdPackets(packets);
47
- }
48
- /** Assigns a new unique key to a tag */
49
- createKey(tag) {
50
- this.keys[tag] = this.key;
51
- this.tags[this.key] = tag;
52
- this.key++;
53
- }
54
- /**
55
- * Registers an array of packets and assigns them keys
56
- * @param packets Array of packets to register
57
- */
58
- holdPackets(packets) {
59
- this.packets = packets;
60
- for (const packet of packets)
61
- this.createKey(packet.tag), this.packetMap[packet.tag] = packet;
62
- }
63
- /**
64
- * Returns the numeric key for a given tag
65
- * @param tag The packet tag
66
- */
67
- getKey(tag) {
68
- if (!(tag in this.keys))
69
- throw new Error(`Not a valid tag: ${tag}`);
70
- return this.keys[tag];
71
- }
72
- /**
73
- * Returns the tag associated with a given character key
74
- * @param key Key bytre
75
- */
76
- getTag(key) {
77
- if (!(key in this.tags))
78
- return undefined;
79
- return this.tags[key];
80
- }
81
- /**
82
- * Returns the packet instance associated with a tag
83
- * @param tag The packet tag
84
- */
85
- getPacket(tag) {
86
- if (!(tag in this.packetMap))
87
- throw new Error("Unknown packet tag: " + tag);
88
- return this.packetMap[tag];
89
- }
90
- /**
91
- * Checks if a given character key exists
92
- * @param key A string index
93
- */
94
- hasKey(key) {
95
- return key in this.tags;
96
- }
97
- /**
98
- * Checks if a tag has been assigned a key
99
- * @param tag The packet tag
100
- */
101
- hasTag(tag) {
102
- return tag in this.keys;
103
- }
104
- /** Returns the mapping of tags to keys */
105
- getKeys() {
106
- return this.keys;
107
- }
108
- /** Returns the mapping of keys to tags */
109
- getTagMap() {
110
- return this.tags;
111
- }
112
- /** Returns an array of all registered tags */
113
- getTags() {
114
- return Object.values(this.tags);
115
- }
116
- /** Returns the list of all registered packets */
117
- getPackets() {
118
- return this.packets;
119
- }
120
- /** Serializes all registered packets into a string */
121
- serialize() {
122
- return this.packets.map(p => p.serialize()).flat();
123
- }
124
- }
125
- exports.PacketHolder = PacketHolder;
5
+
6
+ Object.defineProperty(exports,"__esModule",{value:!0}),exports.PacketHolder=void 0;exports.PacketHolder=class{key;keys;tags;packetMap;packets;constructor(t){this.key=1,this.keys={},this.tags={},this.packetMap={},t&&this.holdPackets(t)}createKey(t){this.keys[t]=this.key,this.tags[this.key]=t,this.key++}holdPackets(t){this.packets=t;for(const e of t)this.createKey(e.tag),this.packetMap[e.tag]=e}getKey(t){if(!(t in this.keys))throw new Error(`Not a valid tag: ${t}`);return this.keys[t]}getTag(t){if(t in this.tags)return this.tags[t]}getPacket(t){if(!(t in this.packetMap))throw new Error("Unknown packet tag: "+t);return this.packetMap[t]}hasKey(t){return t in this.tags}hasTag(t){return t in this.keys}getKeys(){return this.keys}getTagMap(){return this.tags}getTags(){return Object.values(this.tags)}getPackets(){return this.packets}serialize(){return this.packets.map(t=>t.serialize()).flat()}};
@@ -1,24 +1,11 @@
1
- import { PacketHolder } from "./PacketHolder";
1
+ /**
2
+ * Copyright 2026 Lily (liwybloc)
3
+ * Licensed under the Apache License, Version 2.0.
4
+ */
2
5
  import { ConvertType, Packet, ValidatorFunction } from "../../packets/Packets";
3
6
  import { PacketType } from "../../packets/PacketType";
4
7
  import { EnumPackage } from "../enums/EnumType";
5
- import { SendQueue } from "../../PacketProcessor";
6
8
  export type ProcessedPacket = [code: number, data: Uint8Array, packet: Packet<any>];
7
- /**
8
- * Processes and verifies values into a sendable format
9
- * @param packets Packet holder
10
- * @param tag The tag of the packet
11
- * @param values The values
12
- * @returns The indexed code, the data, and the packet schema
13
- */
14
- export declare function processPacket(packets: PacketHolder, tag: string, values: any[], sendQueue: SendQueue, id: number, force?: boolean): Promise<ProcessedPacket>;
15
- /**
16
- * Calls the listener for a packet with error callback
17
- * @param listened The listened data
18
- * @param listeners The listeners to run
19
- * @param errorCB The callback if something goes wrong
20
- */
21
- export declare function listenPacket(listened: string | [any[], boolean], listeners: ((...values: any) => void | Promise<void>)[], errorCB: (data: string) => void): Promise<void>;
22
9
  /** Valid packet type */
23
10
  export type ArguableType = PacketType | EnumPackage;
24
11
  /** Shared packet setting types */
@@ -1,274 +1,6 @@
1
- "use strict";
2
- /*
3
- * Copyright 2026 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
- Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.processPacket = processPacket;
19
- exports.listenPacket = listenPacket;
20
- exports.CreatePacket = CreatePacket;
21
- exports.CreateObjPacket = CreateObjPacket;
22
- exports.CreateEnumPacket = CreateEnumPacket;
23
- exports.CreateKeyEffective = CreateKeyEffective;
24
- exports.FlattenData = FlattenData;
25
- exports.UnFlattenData = UnFlattenData;
26
- const Packets_1 = require("../../packets/Packets");
27
- const PacketType_1 = require("../../packets/PacketType");
28
- const EnumType_1 = require("../enums/EnumType");
29
- const CompressionUtil_1 = require("./CompressionUtil");
30
- const HashUtil_1 = require("./HashUtil");
31
- /**
32
- * Processes and verifies values into a sendable format
33
- * @param packets Packet holder
34
- * @param tag The tag of the packet
35
- * @param values The values
36
- * @returns The indexed code, the data, and the packet schema
37
- */
38
- async function processPacket(packets, tag, values, sendQueue, id, force = false) {
39
- const code = packets.getKey(tag);
40
- const packet = packets.getPacket(tag);
41
- return handleQueue(sendQueue, packets, id, tag, values, force, async () => {
42
- if (packet.rereference) {
43
- if (id === -1)
44
- throw new Error("Cannot send a re-referenced packet from the server-wide sender!");
45
- const serialized = (0, HashUtil_1.hashValue)(values);
46
- if (packet.lastSent[id] === serialized) {
47
- return [code, CompressionUtil_1.EMPTY_UINT8, packet];
48
- }
49
- packet.lastSent[id] = serialized;
50
- }
51
- if (packet.autoFlatten) {
52
- values = FlattenData(values[0]);
53
- }
54
- else {
55
- if (values.length > packet.maxSize)
56
- throw new Error(`Packet "${tag}" only allows ${packet.maxSize} values!`);
57
- if (values.length < packet.minSize)
58
- throw new Error(`Packet "${tag}" requires at least ${packet.minSize} values!`);
59
- }
60
- if (!packet.object) {
61
- if (packet.type !== PacketType_1.PacketType.JSON) {
62
- const found = values.find(v => typeof v === 'object' && v != null);
63
- if (found)
64
- console.warn(`Passing an array will result in undefined behavior (${JSON.stringify(found)}). Spread the array with ...arr`);
65
- }
66
- }
67
- else {
68
- values = values.map(x => !Array.isArray(x) ? [x] : x);
69
- if (!packet.autoFlatten) {
70
- const dataMins = packet.dataMin;
71
- const dataMaxes = packet.dataMax;
72
- for (let i = 0; i < dataMins.length; i++) {
73
- if (values[i].length < dataMins[i])
74
- throw new Error(`Section ${i + 1} of packet "${tag}" requires at least ${dataMins[i]} values!`);
75
- if (values[i].length > dataMaxes[i])
76
- throw new Error(`Section ${i + 1} of packet "${tag}" only allows ${dataMaxes[i]} values!`);
77
- }
78
- }
79
- }
80
- const sendData = values.length > 0 ? await packet.processSend(values) : CompressionUtil_1.EMPTY_UINT8;
81
- return [code, sendData, packet];
82
- });
83
- }
84
- async function handleQueue(sendQueue, packets, id, tag, values, force, fn) {
85
- if (sendQueue[0] && !force) {
86
- return new Promise((resolve) => sendQueue[1].push([resolve, tag, values]));
87
- }
88
- sendQueue[0] = true;
89
- const result = await fn();
90
- if (sendQueue[1].length > 0) {
91
- const [resolve, nextTag, nextValues] = sendQueue[1].shift();
92
- queueMicrotask(async () => {
93
- resolve(await processPacket(packets, nextTag, nextValues, sendQueue, id, true));
94
- });
95
- }
96
- else {
97
- sendQueue[0] = false;
98
- }
99
- return result;
100
- }
101
- /**
102
- * Calls the listener for a packet with error callback
103
- * @param listened The listened data
104
- * @param listeners The listeners to run
105
- * @param errorCB The callback if something goes wrong
106
- */
107
- async function listenPacket(listened, listeners, errorCB) {
108
- if (typeof listened === 'string')
109
- return errorCB(listened);
110
- const [processed, flatten] = listened;
111
- try {
112
- if (flatten && Array.isArray(processed)) {
113
- for (const l of listeners) {
114
- await l(...processed);
115
- }
116
- }
117
- else {
118
- for (const l of listeners) {
119
- await l(processed);
120
- }
121
- }
122
- }
123
- catch (err) {
124
- console.error(err);
125
- errorCB(err);
126
- }
127
- }
128
- /** Determines if a type is a invalid packet type */
129
- function isInvalidType(type) {
130
- return (!(typeof type == 'number' && type in PacketType_1.PacketType) && !(type instanceof EnumType_1.EnumPackage)) || type == PacketType_1.PacketType.KEY_EFFECTIVE;
131
- }
132
- const MAX_DATA_MAX = 2048383;
133
- /** Clamps data max between 0 and MAX_DATA_MAX */
134
- function clampDataMax(dataMax) {
135
- if (dataMax < 0) {
136
- console.warn(`Having a data maximum below 0 does not do anything!`);
137
- return 0;
138
- }
139
- // dfkjgsdkfgjk
140
- if (dataMax > MAX_DATA_MAX) {
141
- console.warn(`Only ${MAX_DATA_MAX} values can be sent on a type! Uhh make an issue if you want to send more.`);
142
- return MAX_DATA_MAX;
143
- }
144
- return dataMax;
145
- }
146
- /** Clamps data min between 0 and datamax */
147
- function clampDataMin(dataMin, dataMax) {
148
- if (dataMin < 0) {
149
- console.warn(`Having a data minimum below 0 does not do anything!`);
150
- return 0;
151
- }
152
- // also catches >MAX_DATA_MAX
153
- if (dataMin > dataMax) {
154
- console.warn(`Data minimum can not be higher than the data maximum!`);
155
- return dataMax;
156
- }
157
- return dataMin;
158
- }
159
- /**
160
- * Creates a structure for a simple single-typed packet.
161
- * This packet can be sent and received with the specified tag, type, and data cap.
162
- * @param settings The settings object containing `tag`, `type`, `dataMax`, `dataMin`, `noDataRange`, `dontSpread`, `validator`, `dataBatching`, and/or `maxBatchSize`.
163
- * @returns The constructed packet structure data.
164
- * @throws {Error} If the `type` is invalid.
165
- */
166
- function CreatePacket(settings) {
167
- let { tag, type = PacketType_1.PacketType.NONE, dataMax = 1, dataMin = 1, noDataRange = false, dontSpread = false, validator = null, dataBatching = 0, maxBatchSize = 10, rateLimit = 0, enabled = true, async = false, gzipCompression = type == PacketType_1.PacketType.JSON, rereference = false } = settings;
168
- if (!tag)
169
- throw new Error("Tag not selected!");
170
- if (noDataRange) {
171
- dataMin = rereference ? 1 : 0;
172
- dataMax = MAX_DATA_MAX;
173
- }
174
- else if (dataMin == undefined)
175
- dataMin = type == PacketType_1.PacketType.NONE ? 0 : dataMax;
176
- if (rereference && dataMin == 0)
177
- throw new Error("Rereference cannot be true if the dataMin is 0");
178
- if (isInvalidType(type)) {
179
- throw new Error(`Invalid packet type: ${type}`);
180
- }
181
- const schema = Packets_1.PacketSchema.single(type, clampDataMax(dataMax), clampDataMin(dataMin, dataMax), dontSpread, dataBatching, maxBatchSize, rateLimit, async, gzipCompression, rereference);
182
- return new Packets_1.Packet(tag, schema, validator, enabled, false);
183
- }
184
1
  /**
185
- * Creates a structure for an object (multi-typed) packet.
186
- * This packet allows multiple types and their associated data caps.
187
- * @param settings The settings object containing `tag`, `types`, `dataMaxes`, `dataMins`, `noDataRange`, `dontSpread`, `autoFlatten`, `largePacket`, `validator`, `dataBatching`, and/or `maxBatchSize`.
188
- * @returns The constructed packet structure data.
189
- * @throws {Error} If any type in `types` is invalid.
190
- */
191
- function CreateObjPacket(settings) {
192
- let { tag, types = [], dataMaxes, dataMins, noDataRange = false, dontSpread = false, autoFlatten = false, validator = null, dataBatching = 0, maxBatchSize = 10, rateLimit = 0, enabled = true, async = false, gzipCompression = types && types.includes(PacketType_1.PacketType.JSON) } = settings;
193
- if (!tag)
194
- throw new Error("Tag not selected!");
195
- if (!types || types.length == 0)
196
- throw new Error("Types is set to 0 length");
197
- for (const type of types) {
198
- if (!isInvalidType(type))
199
- continue;
200
- throw new Error(`Invalid packet type in "${tag}" packet: ${type}`);
201
- }
202
- if (noDataRange) {
203
- dataMaxes = Array.from({ length: types.length }).map(() => MAX_DATA_MAX);
204
- dataMins = Array.from({ length: types.length }).map(() => 0);
205
- }
206
- else {
207
- if (dataMaxes == undefined)
208
- dataMaxes = Array.from({ length: types.length }).map(() => 1);
209
- else if (!Array.isArray(dataMaxes))
210
- dataMaxes = Array.from({ length: types.length }).map(() => dataMaxes);
211
- if (dataMins == undefined)
212
- dataMins = Array.from({ length: types.length }).map((_, i) => dataMaxes[i]);
213
- else if (!Array.isArray(dataMins))
214
- dataMins = Array.from({ length: types.length }).map(() => dataMins);
215
- }
216
- const clampedDataMaxes = dataMaxes.map(clampDataMax);
217
- const clampedDataMins = dataMins.map((m, i) => types[i] == PacketType_1.PacketType.NONE ? 0 : clampDataMin(m, clampedDataMaxes[i]));
218
- const schema = Packets_1.PacketSchema.object(types, clampedDataMaxes, clampedDataMins, dontSpread, autoFlatten, dataBatching, maxBatchSize, rateLimit, async, gzipCompression);
219
- return new Packets_1.Packet(tag, schema, validator, enabled, false);
220
- }
221
- /**
222
- * Creates and defines an enum packet. This can be used to create an enum-based packet
223
- * with a specific tag and possible values.
224
- * @param settings The settings object containing `tag`, `enumTag`, `values`, `dataMax`, `dataMin`, `noDataRange`, `dontSpread`, `validator`, `dataBatching`, and/or `maxBatchSize`.
225
- * @returns The constructed packet structure data.
226
- */
227
- function CreateEnumPacket(settings) {
228
- const { tag, enumData, dataMax = 1, dataMin = 0, noDataRange = false, dontSpread = false, validator = null, dataBatching = 0, maxBatchSize = 10, rateLimit = 0, enabled = true, async = false } = settings;
229
- return CreatePacket({
230
- tag: tag,
231
- type: enumData,
232
- dataMax,
233
- dataMin,
234
- noDataRange,
235
- dontSpread,
236
- validator,
237
- dataBatching,
238
- maxBatchSize,
239
- rateLimit,
240
- enabled,
241
- async,
242
- });
243
- }
244
- function CreateKeyEffective(settings) {
245
- const { tag, count = 2, validator = null, async = false } = settings;
246
- if (!tag)
247
- throw new Error("Tag not selected!");
248
- if (count < 2)
249
- throw new Error("Must have at least 2 key consumptions on key effective packet!");
250
- throw new Error("Currently W.I.P.");
251
- }
252
- /**
253
- * Flattens a 2-depth array for efficient wire transfer
254
- * Turns [[x,y,z],[x,y,z]...] to [[x,x...],[y,y...],[z,z...]]
255
- * @param array A 2-depth array of multi-valued
256
- */
257
- function FlattenData(arr) {
258
- if (arr == null)
259
- return [];
260
- const setup = arr[0];
261
- if (setup == null)
262
- return [];
263
- if (!Array.isArray(setup))
264
- throw new Error(`Cannot flatten array: ${arr}`);
265
- return setup.map((_, i) => arr.map(row => row[i])) ?? [];
266
- }
267
- /**
268
- * Unflattens an array into 2-depth; reverse of FlattenData()
269
- * turns [[x,x...],[y,y...],[z,z...]] to [[x,y,z],[x,y,z]...]
270
- * @param array A flattened array
2
+ * Copyright 2026 Lily (liwybloc)
3
+ * Licensed under the Apache License, Version 2.0.
271
4
  */
272
- function UnFlattenData(arr) {
273
- return arr[0]?.map((_, i) => arr.map(col => col[i])) ?? [];
274
- }
5
+
6
+ Object.defineProperty(exports,"__esModule",{value:!0}),exports.processPacket=o,exports.listenPacket=async function(e,t,a){if("string"==typeof e)return a(e);const[n,r]=e;try{if(r&&Array.isArray(n))for(const e of t)await e(...n);else for(const e of t)await e(n)}catch(e){console.error(e),a(e)}},exports.CreatePacket=u,exports.CreateObjPacket=function(a){let{tag:n,types:r=[],dataMaxes:o,dataMins:u,noDataRange:f=!1,dontSpread:p=!1,autoFlatten:h=!1,validator:y=null,dataBatching:m=0,maxBatchSize:d=10,rateLimit:w=0,enabled:g=!0,async:k=!1,gzipCompression:P=r&&r.includes(t.PacketType.JSON)}=a;if(!n)throw new Error("Tag not selected!");if(!r||0==r.length)throw new Error("Types is set to 0 length");for(const e of r)if(i(e))throw new Error(`Invalid packet type in "${n}" packet: ${e}`);f?(o=Array.from({length:r.length}).map(()=>s),u=Array.from({length:r.length}).map(()=>0)):(null==o?o=Array.from({length:r.length}).map(()=>1):Array.isArray(o)||(o=Array.from({length:r.length}).map(()=>o)),null==u?u=Array.from({length:r.length}).map((e,t)=>o[t]):Array.isArray(u)||(u=Array.from({length:r.length}).map(()=>u)));const E=o.map(c),S=u.map((e,a)=>r[a]==t.PacketType.NONE?0:l(e,E[a])),x=new e.PacketSchema(!0,r,k,S,E,w,p,h,!1,m,d,P);return new e.Packet(n,x,y,g,!1)},exports.CreateEnumPacket=function(e){const{tag:t,enumData:a,dataMax:n=1,dataMin:r=0,noDataRange:o=!1,dontSpread:i=!1,validator:s=null,dataBatching:c=0,maxBatchSize:l=10,rateLimit:f=0,enabled:p=!0,async:h=!1}=e;return u({tag:t,type:a,dataMax:n,dataMin:r,noDataRange:o,dontSpread:i,validator:s,dataBatching:c,maxBatchSize:l,rateLimit:f,enabled:p,async:h})},exports.CreateKeyEffective=function(e){const{tag:t,count:a=2,validator:n=null,async:r=!1}=e;if(!t)throw new Error("Tag not selected!");if(a<2)throw new Error("Must have at least 2 key consumptions on key effective packet!");throw new Error("Currently W.I.P.")},exports.FlattenData=f,exports.UnFlattenData=function(e){return e[0]?.map((t,a)=>e.map(e=>e[a]))??[]};const e=require("../../packets/Packets"),t=require("../../packets/PacketType"),a=require("../enums/EnumType"),n=require("./CompressionUtil"),r=require("./HashUtil");async function o(e,a,i,s,c,l=!1){const u=e.getKey(a),p=e.getPacket(a);return async function(e,t,a,n,r,i,s){if(e[0]&&!i)return new Promise(t=>e[1].push([t,n,r]));e[0]=!0;const c=await s();if(e[1].length>0){const[n,r,i]=e[1].shift();queueMicrotask(async()=>{n(await o(t,r,i,e,a,!0))})}else e[0]=!1;return c}(s,e,c,a,i,l,async()=>{if(p.rereference){if(-1===c)throw new Error("Cannot send a re-referenced packet from the server-wide sender!");const e=(0,r.hashValue)(i);if(p.lastSent[c]===e)return[u,n.EMPTY_UINT8,p];p.lastSent[c]=e}if(p.autoFlatten)i=f(i[0]);else{if(i.length>p.maxSize)throw new Error(`Packet "${a}" only allows ${p.maxSize} values!`);if(i.length<p.minSize)throw new Error(`Packet "${a}" requires at least ${p.minSize} values!`)}if(p.object){if(i=i.map(e=>Array.isArray(e)?e:[e]),!p.autoFlatten){const e=p.dataMin,t=p.dataMax;for(let n=0;n<e.length;n++){if(i[n].length<e[n])throw new Error(`Section ${n+1} of packet "${a}" requires at least ${e[n]} values!`);if(i[n].length>t[n])throw new Error(`Section ${n+1} of packet "${a}" only allows ${t[n]} values!`)}}}else if(p.type!==t.PacketType.JSON){const e=i.find(e=>"object"==typeof e&&null!=e);e&&console.warn(`Passing an array will result in undefined behavior (${JSON.stringify(e)}). Spread the array with ...arr`)}const e=i.length>0?await p.processSend(i):n.EMPTY_UINT8;return[u,e,p]})}function i(e){return!(("number"==typeof e&&e in t.PacketType||e instanceof a.EnumPackage)&&e!=t.PacketType.KEY_EFFECTIVE)}const s=2048383;function c(e){return e<0?(console.warn("Having a data maximum below 0 does not do anything!"),0):e>s?(console.warn(`Only ${s} values can be sent on a type! Uhh make an issue if you want to send more.`),s):e}function l(e,t){return e<0?(console.warn("Having a data minimum below 0 does not do anything!"),0):e>t?(console.warn("Data minimum can not be higher than the data maximum!"),t):e}function u(a){let{tag:n,type:r=t.PacketType.NONE,dataMax:o=1,dataMin:u=1,noDataRange:f=!1,dontSpread:p=!1,validator:h=null,dataBatching:y=0,maxBatchSize:m=10,rateLimit:d=0,enabled:w=!0,async:g=!1,gzipCompression:k=r==t.PacketType.JSON,rereference:P=!1}=a;if(!n)throw new Error("Tag not selected!");if(f?(u=P?1:0,o=s):null==u&&(u=r==t.PacketType.NONE?0:o),P&&0==u)throw new Error("Rereference cannot be true if the dataMin is 0");if(i(r))throw new Error(`Invalid packet type: ${r}`);const E=new e.PacketSchema(!1,r,g,l(u,o),c(o),d,p,!1,P,y,m,k);return new e.Packet(n,E,h,w,!1)}function f(e){if(null==e)return[];const t=e[0];if(null==t)return[];if(!Array.isArray(t))throw new Error(`Cannot flatten array: ${e}`);return t.map((t,a)=>e.map(e=>e[a]))??[]}