sonic-ws 1.3.3 → 2.0.0-fix
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/LICENSE +56 -196
- package/README.md +88 -17
- package/bundled/bundle.js +6 -0
- package/bundled/bundle.wasm +0 -0
- package/dist/{index.d.ts → ts/index.d.ts} +11 -0
- package/dist/{index.js → ts/index.js} +10 -11
- package/dist/ts/native/wrapper.d.ts +64 -0
- package/dist/ts/native/wrapper.js +7 -0
- package/dist/ts/version.d.ts +16 -0
- package/dist/ts/version.js +7 -0
- package/dist/{ws → ts/ws}/Connection.d.ts +10 -0
- package/dist/ts/ws/Connection.js +7 -0
- package/dist/{ws → ts/ws}/PacketProcessor.d.ts +10 -0
- package/dist/ts/ws/PacketProcessor.js +7 -0
- package/dist/{ws → ts/ws}/client/core/ClientCore.d.ts +10 -0
- package/dist/ts/ws/client/core/ClientCore.js +7 -0
- package/dist/{ws → ts/ws}/client/node/ClientNode.d.ts +10 -0
- package/dist/ts/ws/client/node/ClientNode.js +7 -0
- package/dist/ts/ws/debug/DebugServer.d.ts +17 -0
- package/dist/ts/ws/debug/DebugServer.js +7 -0
- package/dist/{ws → ts/ws}/packets/PacketType.d.ts +11 -1
- package/dist/ts/ws/packets/PacketType.js +7 -0
- package/dist/{ws → ts/ws}/packets/Packets.d.ts +10 -3
- package/dist/ts/ws/packets/Packets.js +7 -0
- package/dist/{ws → ts/ws}/server/SonicWSConnection.d.ts +10 -0
- package/dist/ts/ws/server/SonicWSConnection.js +7 -0
- package/dist/{ws → ts/ws}/server/SonicWSServer.d.ts +13 -0
- package/dist/ts/ws/server/SonicWSServer.js +7 -0
- package/dist/{ws → ts/ws}/util/BufferUtil.d.ts +10 -0
- package/dist/ts/ws/util/BufferUtil.js +7 -0
- package/dist/ts/ws/util/StringUtil.d.ts +13 -0
- package/dist/ts/ws/util/StringUtil.js +7 -0
- package/dist/{ws → ts/ws}/util/enums/EnumHandler.d.ts +10 -0
- package/dist/ts/ws/util/enums/EnumHandler.js +7 -0
- package/dist/ts/ws/util/enums/EnumType.d.ts +12 -0
- package/dist/ts/ws/util/enums/EnumType.js +7 -0
- package/dist/ts/ws/util/packets/BatchHelper.d.ts +11 -0
- package/dist/ts/ws/util/packets/BatchHelper.js +7 -0
- package/dist/ts/ws/util/packets/CompressionUtil.d.ts +18 -0
- package/dist/ts/ws/util/packets/CompressionUtil.js +7 -0
- package/dist/ts/ws/util/packets/HashUtil.d.ts +12 -0
- package/dist/ts/ws/util/packets/HashUtil.js +7 -0
- package/dist/ts/ws/util/packets/JSONUtil.d.ts +12 -0
- package/dist/ts/ws/util/packets/JSONUtil.js +7 -0
- package/dist/ts/ws/util/packets/PacketHolder.d.ts +11 -0
- package/dist/ts/ws/util/packets/PacketHolder.js +7 -0
- package/dist/{ws → ts/ws}/util/packets/PacketUtils.d.ts +10 -0
- package/dist/ts/ws/util/packets/PacketUtils.js +7 -0
- package/dist/ts/ws/util/packets/RateHandler.d.ts +11 -0
- package/dist/ts/ws/util/packets/RateHandler.js +7 -0
- package/package.json +14 -10
- package/dist/version.d.ts +0 -6
- package/dist/version.js +0 -25
- package/dist/ws/Connection.js +0 -126
- package/dist/ws/PacketProcessor.js +0 -52
- package/dist/ws/client/core/ClientCore.js +0 -251
- package/dist/ws/client/node/ClientNode.js +0 -36
- package/dist/ws/debug/DebugServer.d.ts +0 -7
- package/dist/ws/debug/DebugServer.js +0 -607
- package/dist/ws/packets/PacketProcessors.d.ts +0 -13
- package/dist/ws/packets/PacketProcessors.js +0 -317
- package/dist/ws/packets/PacketType.js +0 -61
- package/dist/ws/packets/Packets.js +0 -277
- package/dist/ws/server/SonicWSConnection.js +0 -298
- package/dist/ws/server/SonicWSServer.js +0 -307
- package/dist/ws/util/BufferUtil.js +0 -42
- package/dist/ws/util/StringUtil.d.ts +0 -5
- package/dist/ws/util/StringUtil.js +0 -51
- package/dist/ws/util/enums/EnumHandler.js +0 -60
- package/dist/ws/util/enums/EnumType.d.ts +0 -2
- package/dist/ws/util/enums/EnumType.js +0 -72
- package/dist/ws/util/packets/BatchHelper.d.ts +0 -1
- package/dist/ws/util/packets/BatchHelper.js +0 -85
- package/dist/ws/util/packets/CompressionUtil.d.ts +0 -35
- package/dist/ws/util/packets/CompressionUtil.js +0 -277
- package/dist/ws/util/packets/HashUtil.d.ts +0 -2
- package/dist/ws/util/packets/HashUtil.js +0 -122
- package/dist/ws/util/packets/JSONUtil.d.ts +0 -2
- package/dist/ws/util/packets/JSONUtil.js +0 -179
- package/dist/ws/util/packets/PacketHolder.d.ts +0 -1
- package/dist/ws/util/packets/PacketHolder.js +0 -126
- package/dist/ws/util/packets/PacketUtils.js +0 -283
- package/dist/ws/util/packets/RateHandler.d.ts +0 -1
- package/dist/ws/util/packets/RateHandler.js +0 -66
|
@@ -1,122 +0,0 @@
|
|
|
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.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,179 +0,0 @@
|
|
|
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.decompressJSON = exports.compressJSON = void 0;
|
|
19
|
-
const BufferUtil_1 = require("../BufferUtil");
|
|
20
|
-
const CompressionUtil_1 = require("./CompressionUtil");
|
|
21
|
-
var JSONType;
|
|
22
|
-
(function (JSONType) {
|
|
23
|
-
JSONType[JSONType["NULL"] = 0] = "NULL";
|
|
24
|
-
JSONType[JSONType["BOOL"] = 1] = "BOOL";
|
|
25
|
-
JSONType[JSONType["INT"] = 2] = "INT";
|
|
26
|
-
JSONType[JSONType["FLOAT"] = 3] = "FLOAT";
|
|
27
|
-
JSONType[JSONType["STRING"] = 4] = "STRING";
|
|
28
|
-
JSONType[JSONType["ARRAY"] = 5] = "ARRAY";
|
|
29
|
-
JSONType[JSONType["OBJECT"] = 6] = "OBJECT";
|
|
30
|
-
})(JSONType || (JSONType = {}));
|
|
31
|
-
const encodeString = (str) => {
|
|
32
|
-
const encoder = new TextEncoder();
|
|
33
|
-
const data = encoder.encode(str);
|
|
34
|
-
return [...(0, CompressionUtil_1.convertVarInt)(data.length), ...data];
|
|
35
|
-
};
|
|
36
|
-
const decodeString = (bytes, offset) => {
|
|
37
|
-
const [off, len] = (0, CompressionUtil_1.readVarInt)(bytes, offset);
|
|
38
|
-
const decoder = new TextDecoder();
|
|
39
|
-
return { value: decoder.decode(bytes.subarray(off, off + len)), length: off + len - offset };
|
|
40
|
-
};
|
|
41
|
-
// utility: pack 3-bit values into bytes
|
|
42
|
-
const packTypeBits = (types) => {
|
|
43
|
-
let bits = '';
|
|
44
|
-
for (const t of types)
|
|
45
|
-
bits += t.toString(2).padStart(3, '0');
|
|
46
|
-
return (0, CompressionUtil_1.bitsToBytes)(bits);
|
|
47
|
-
};
|
|
48
|
-
// utility: unpack bytes into 3-bit type array
|
|
49
|
-
const unpackTypeBits = (bytes, totalValues) => {
|
|
50
|
-
const bitStr = (0, CompressionUtil_1.bytesToBits)(bytes);
|
|
51
|
-
const types = [];
|
|
52
|
-
for (let i = 0; i < totalValues; i++) {
|
|
53
|
-
types.push(parseInt(bitStr.slice(i * 3, i * 3 + 3), 2));
|
|
54
|
-
}
|
|
55
|
-
return types;
|
|
56
|
-
};
|
|
57
|
-
// main compression
|
|
58
|
-
const compressJSON = (value) => {
|
|
59
|
-
const bools = [];
|
|
60
|
-
const payload = [];
|
|
61
|
-
const typeList = [];
|
|
62
|
-
const encodeValue = (val) => {
|
|
63
|
-
if (val === null) {
|
|
64
|
-
typeList.push(JSONType.NULL);
|
|
65
|
-
}
|
|
66
|
-
else if (typeof val === 'boolean') {
|
|
67
|
-
typeList.push(JSONType.BOOL);
|
|
68
|
-
bools.push(val);
|
|
69
|
-
}
|
|
70
|
-
else if (Number.isInteger(val)) {
|
|
71
|
-
typeList.push(JSONType.INT);
|
|
72
|
-
payload.push(...(0, CompressionUtil_1.convertVarInt)((0, CompressionUtil_1.mapZigZag)(val)));
|
|
73
|
-
}
|
|
74
|
-
else if (typeof val === 'number') {
|
|
75
|
-
typeList.push(JSONType.FLOAT);
|
|
76
|
-
payload.push(...(0, CompressionUtil_1.convertFloat)(val));
|
|
77
|
-
}
|
|
78
|
-
else if (typeof val === 'string') {
|
|
79
|
-
typeList.push(JSONType.STRING);
|
|
80
|
-
payload.push(...encodeString(val));
|
|
81
|
-
}
|
|
82
|
-
else if (Array.isArray(val)) {
|
|
83
|
-
typeList.push(JSONType.ARRAY);
|
|
84
|
-
payload.push(...(0, CompressionUtil_1.convertVarInt)(val.length));
|
|
85
|
-
for (const item of val)
|
|
86
|
-
encodeValue(item);
|
|
87
|
-
}
|
|
88
|
-
else if (typeof val === 'object') {
|
|
89
|
-
typeList.push(JSONType.OBJECT);
|
|
90
|
-
const keys = Object.keys(val);
|
|
91
|
-
payload.push(...(0, CompressionUtil_1.convertVarInt)(keys.length));
|
|
92
|
-
for (const key of keys) {
|
|
93
|
-
payload.push(...encodeString(key));
|
|
94
|
-
encodeValue(val[key]);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
else {
|
|
98
|
-
throw new Error('Unsupported type');
|
|
99
|
-
}
|
|
100
|
-
};
|
|
101
|
-
encodeValue(value);
|
|
102
|
-
// boolean bitmap bytes
|
|
103
|
-
const boolBytes = bools.length
|
|
104
|
-
? (0, BufferUtil_1.splitArray)(bools, 8).map((slice) => (0, CompressionUtil_1.compressBools)(slice))
|
|
105
|
-
: [];
|
|
106
|
-
// type map bytes (3-bit per value)
|
|
107
|
-
const typeBytes = packTypeBits(typeList);
|
|
108
|
-
// prepend lengths of boolBytes and typeBytes as varints
|
|
109
|
-
const header = [...(0, CompressionUtil_1.convertVarInt)(boolBytes.length), ...(0, CompressionUtil_1.convertVarInt)(typeBytes.length)];
|
|
110
|
-
return Uint8Array.from([...header, ...boolBytes.flat(), ...typeBytes, ...payload]);
|
|
111
|
-
};
|
|
112
|
-
exports.compressJSON = compressJSON;
|
|
113
|
-
// decompression
|
|
114
|
-
const decompressJSON = (bytes) => {
|
|
115
|
-
let offset = 0;
|
|
116
|
-
// read lengths
|
|
117
|
-
const [off1, boolByteLen] = (0, CompressionUtil_1.readVarInt)(bytes, offset);
|
|
118
|
-
offset = off1;
|
|
119
|
-
const [off2, typeByteLen] = (0, CompressionUtil_1.readVarInt)(bytes, offset);
|
|
120
|
-
offset = off2;
|
|
121
|
-
// boolean bitmap
|
|
122
|
-
const boolStream = [];
|
|
123
|
-
for (let i = 0; i < boolByteLen; i++) {
|
|
124
|
-
boolStream.push(...(0, CompressionUtil_1.decompressBools)(bytes[offset++]));
|
|
125
|
-
}
|
|
126
|
-
let boolIndex = 0;
|
|
127
|
-
// type map
|
|
128
|
-
const typeBytes = bytes.subarray(offset, offset + typeByteLen);
|
|
129
|
-
offset += typeByteLen;
|
|
130
|
-
const typeList = unpackTypeBits(typeBytes, typeBytes.length * 8 / 3); // overestimate, will only use while decoding
|
|
131
|
-
let typeIndex = 0;
|
|
132
|
-
const decodeValue = (depth) => {
|
|
133
|
-
if (depth > 500)
|
|
134
|
-
throw new Error("JSON array too deep.");
|
|
135
|
-
const type = typeList[typeIndex++];
|
|
136
|
-
switch (type) {
|
|
137
|
-
case JSONType.NULL: return null;
|
|
138
|
-
case JSONType.BOOL: return boolStream[boolIndex++];
|
|
139
|
-
case JSONType.INT: {
|
|
140
|
-
const [off, n] = (0, CompressionUtil_1.readVarInt)(bytes, offset);
|
|
141
|
-
offset = off;
|
|
142
|
-
return (0, CompressionUtil_1.demapZigZag)(n);
|
|
143
|
-
}
|
|
144
|
-
case JSONType.FLOAT: {
|
|
145
|
-
const val = (0, CompressionUtil_1.deconvertFloat)(Array.from(bytes.subarray(offset, offset + 4)));
|
|
146
|
-
offset += 4;
|
|
147
|
-
return val;
|
|
148
|
-
}
|
|
149
|
-
case JSONType.STRING: {
|
|
150
|
-
const { value, length } = decodeString(bytes, offset);
|
|
151
|
-
offset += length;
|
|
152
|
-
return value;
|
|
153
|
-
}
|
|
154
|
-
case JSONType.ARRAY: {
|
|
155
|
-
const [off, len] = (0, CompressionUtil_1.readVarInt)(bytes, offset);
|
|
156
|
-
offset = off;
|
|
157
|
-
const arr = [];
|
|
158
|
-
for (let i = 0; i < len; i++)
|
|
159
|
-
arr.push(decodeValue(depth + 1));
|
|
160
|
-
return arr;
|
|
161
|
-
}
|
|
162
|
-
case JSONType.OBJECT: {
|
|
163
|
-
const [off, numKeys] = (0, CompressionUtil_1.readVarInt)(bytes, offset);
|
|
164
|
-
offset = off;
|
|
165
|
-
const obj = {};
|
|
166
|
-
for (let i = 0; i < numKeys; i++) {
|
|
167
|
-
const { value: key, length: keyLen } = decodeString(bytes, offset);
|
|
168
|
-
offset += keyLen;
|
|
169
|
-
obj[key] = decodeValue(depth + 1);
|
|
170
|
-
}
|
|
171
|
-
return obj;
|
|
172
|
-
}
|
|
173
|
-
default:
|
|
174
|
-
throw new Error(`Unknown type ${type}`);
|
|
175
|
-
}
|
|
176
|
-
};
|
|
177
|
-
return decodeValue(0);
|
|
178
|
-
};
|
|
179
|
-
exports.decompressJSON = decompressJSON;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,126 +0,0 @@
|
|
|
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
|
-
/**
|
|
20
|
-
* Holds and maps packets to indexed keys and tags for serialization and lookup
|
|
21
|
-
* @internal
|
|
22
|
-
*/
|
|
23
|
-
class PacketHolder {
|
|
24
|
-
/** Current key index for packet tags */
|
|
25
|
-
key;
|
|
26
|
-
/** Maps tags to keys */
|
|
27
|
-
keys;
|
|
28
|
-
/** Maps keys to tags */
|
|
29
|
-
tags;
|
|
30
|
-
/** Maps tags to packet instances */
|
|
31
|
-
packetMap;
|
|
32
|
-
/** List of all packet instances */
|
|
33
|
-
packets;
|
|
34
|
-
/**
|
|
35
|
-
* Creates a new PacketHolder with an array of packets
|
|
36
|
-
* @param packets Array of packets to register
|
|
37
|
-
*/
|
|
38
|
-
constructor(packets) {
|
|
39
|
-
// reserves:
|
|
40
|
-
// 0 - enum update
|
|
41
|
-
this.key = 1;
|
|
42
|
-
this.keys = {};
|
|
43
|
-
this.tags = {};
|
|
44
|
-
this.packetMap = {};
|
|
45
|
-
if (!packets)
|
|
46
|
-
return;
|
|
47
|
-
this.holdPackets(packets);
|
|
48
|
-
}
|
|
49
|
-
/** Assigns a new unique key to a tag */
|
|
50
|
-
createKey(tag) {
|
|
51
|
-
this.keys[tag] = this.key;
|
|
52
|
-
this.tags[this.key] = tag;
|
|
53
|
-
this.key++;
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Registers an array of packets and assigns them keys
|
|
57
|
-
* @param packets Array of packets to register
|
|
58
|
-
*/
|
|
59
|
-
holdPackets(packets) {
|
|
60
|
-
this.packets = packets;
|
|
61
|
-
for (const packet of packets)
|
|
62
|
-
this.createKey(packet.tag), this.packetMap[packet.tag] = packet;
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Returns the numeric key for a given tag
|
|
66
|
-
* @param tag The packet tag
|
|
67
|
-
*/
|
|
68
|
-
getKey(tag) {
|
|
69
|
-
if (!(tag in this.keys))
|
|
70
|
-
throw new Error(`Not a valid tag: ${tag}`);
|
|
71
|
-
return this.keys[tag];
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Returns the tag associated with a given character key
|
|
75
|
-
* @param key Key bytre
|
|
76
|
-
*/
|
|
77
|
-
getTag(key) {
|
|
78
|
-
if (!(key in this.tags))
|
|
79
|
-
return undefined;
|
|
80
|
-
return this.tags[key];
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Returns the packet instance associated with a tag
|
|
84
|
-
* @param tag The packet tag
|
|
85
|
-
*/
|
|
86
|
-
getPacket(tag) {
|
|
87
|
-
if (!(tag in this.packetMap))
|
|
88
|
-
throw new Error("Unknown packet tag: " + tag);
|
|
89
|
-
return this.packetMap[tag];
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Checks if a given character key exists
|
|
93
|
-
* @param key A string index
|
|
94
|
-
*/
|
|
95
|
-
hasKey(key) {
|
|
96
|
-
return key in this.tags;
|
|
97
|
-
}
|
|
98
|
-
/**
|
|
99
|
-
* Checks if a tag has been assigned a key
|
|
100
|
-
* @param tag The packet tag
|
|
101
|
-
*/
|
|
102
|
-
hasTag(tag) {
|
|
103
|
-
return tag in this.keys;
|
|
104
|
-
}
|
|
105
|
-
/** Returns the mapping of tags to keys */
|
|
106
|
-
getKeys() {
|
|
107
|
-
return this.keys;
|
|
108
|
-
}
|
|
109
|
-
/** Returns the mapping of keys to tags */
|
|
110
|
-
getTagMap() {
|
|
111
|
-
return this.tags;
|
|
112
|
-
}
|
|
113
|
-
/** Returns an array of all registered tags */
|
|
114
|
-
getTags() {
|
|
115
|
-
return Object.values(this.tags);
|
|
116
|
-
}
|
|
117
|
-
/** Returns the list of all registered packets */
|
|
118
|
-
getPackets() {
|
|
119
|
-
return this.packets;
|
|
120
|
-
}
|
|
121
|
-
/** Serializes all registered packets into a string */
|
|
122
|
-
serialize() {
|
|
123
|
-
return this.packets.map(p => p.serialize()).flat();
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
exports.PacketHolder = PacketHolder;
|