binary-packet 1.0.5 → 1.0.6
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.js +1 -446
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -416
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,447 +1,2 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var src_exports = {};
|
|
22
|
-
__export(src_exports, {
|
|
23
|
-
BinaryPacket: () => BinaryPacket,
|
|
24
|
-
Field: () => Field,
|
|
25
|
-
FieldArray: () => FieldArray,
|
|
26
|
-
FieldFixedArray: () => FieldFixedArray
|
|
27
|
-
});
|
|
28
|
-
module.exports = __toCommonJS(src_exports);
|
|
29
|
-
|
|
30
|
-
// src/buffers.ts
|
|
31
|
-
var hasNodeBuffers = typeof Buffer === "function";
|
|
32
|
-
function growDataView(dataview, newByteLength) {
|
|
33
|
-
const resizedBuffer = new ArrayBuffer(newByteLength);
|
|
34
|
-
const amountToCopy = Math.min(dataview.byteLength, resizedBuffer.byteLength);
|
|
35
|
-
let length = Math.trunc(amountToCopy / 8);
|
|
36
|
-
new Float64Array(resizedBuffer, 0, length).set(new Float64Array(dataview.buffer, 0, length));
|
|
37
|
-
const offset = length * 8;
|
|
38
|
-
length = amountToCopy - offset;
|
|
39
|
-
new Uint8Array(resizedBuffer, offset, length).set(new Uint8Array(dataview.buffer, offset, length));
|
|
40
|
-
return new DataView(resizedBuffer);
|
|
41
|
-
}
|
|
42
|
-
function growNodeBuffer(buffer, newByteLength) {
|
|
43
|
-
const newBuffer = Buffer.allocUnsafe(newByteLength);
|
|
44
|
-
buffer.copy(newBuffer);
|
|
45
|
-
return newBuffer;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// src/index.ts
|
|
49
|
-
var Field = /* @__PURE__ */ ((Field2) => {
|
|
50
|
-
Field2[Field2["UNSIGNED_INT_8"] = 0] = "UNSIGNED_INT_8";
|
|
51
|
-
Field2[Field2["UNSIGNED_INT_16"] = 1] = "UNSIGNED_INT_16";
|
|
52
|
-
Field2[Field2["UNSIGNED_INT_32"] = 2] = "UNSIGNED_INT_32";
|
|
53
|
-
Field2[Field2["INT_8"] = 3] = "INT_8";
|
|
54
|
-
Field2[Field2["INT_16"] = 4] = "INT_16";
|
|
55
|
-
Field2[Field2["INT_32"] = 5] = "INT_32";
|
|
56
|
-
Field2[Field2["FLOAT_32"] = 6] = "FLOAT_32";
|
|
57
|
-
Field2[Field2["FLOAT_64"] = 7] = "FLOAT_64";
|
|
58
|
-
return Field2;
|
|
59
|
-
})(Field || {});
|
|
60
|
-
function FieldArray(item) {
|
|
61
|
-
return [item];
|
|
62
|
-
}
|
|
63
|
-
function FieldFixedArray(item, length) {
|
|
64
|
-
if (length < 0 || !Number.isFinite(length)) {
|
|
65
|
-
throw new RangeError("Length of a FixedArray must be a positive integer.");
|
|
66
|
-
}
|
|
67
|
-
return [item, length];
|
|
68
|
-
}
|
|
69
|
-
var BinaryPacket = class _BinaryPacket {
|
|
70
|
-
constructor(packetId, definition) {
|
|
71
|
-
this.packetId = packetId;
|
|
72
|
-
this.entries = definition ? sortEntries(definition) : [];
|
|
73
|
-
const inspection = inspectEntries(this.entries);
|
|
74
|
-
this.minimumByteLength = inspection.minimumByteLength;
|
|
75
|
-
this.canFastWrite = inspection.canFastWrite;
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* Defines a new binary packet. \
|
|
79
|
-
* Make sure that every `packetId` is unique.
|
|
80
|
-
* @throws RangeError If packetId is negative, floating-point, or greater than 255.
|
|
81
|
-
*/
|
|
82
|
-
static define(packetId, definition) {
|
|
83
|
-
if (packetId < 0 || !Number.isFinite(packetId)) {
|
|
84
|
-
throw new RangeError("Packet IDs must be positive integers.");
|
|
85
|
-
}
|
|
86
|
-
if (packetId > 255) {
|
|
87
|
-
throw new RangeError(
|
|
88
|
-
"Packet IDs greater than 255 are not supported. Do you REALLY need more than 255 different kinds of packets?"
|
|
89
|
-
);
|
|
90
|
-
}
|
|
91
|
-
return new _BinaryPacket(packetId, definition);
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* Reads just the packetId from the given Buffer. \
|
|
95
|
-
* This method practically just reads the uint8 at offset `byteOffset` (default: 0). \
|
|
96
|
-
* Useful if the receiving side receives multiple types of packets.
|
|
97
|
-
*/
|
|
98
|
-
static readPacketIdNodeBuffer(buffer, byteOffset = 0) {
|
|
99
|
-
return buffer.readUint8(byteOffset);
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Reads just the packetId from the given DataView. \
|
|
103
|
-
* This method practically just reads the uint8 at offset `byteOffset` (default: 0). \
|
|
104
|
-
* Useful if the receiving side receives multiple types of packets.
|
|
105
|
-
*/
|
|
106
|
-
static readPacketIdDataView(dataview, byteOffset = 0) {
|
|
107
|
-
return dataview.getUint8(byteOffset);
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* Reads just the packetId from the given ArrayBuffer. \
|
|
111
|
-
* This method practically just reads the uint8 at offset `byteOffset`. \
|
|
112
|
-
* Useful if the receiving side receives multiple types of packets.
|
|
113
|
-
*
|
|
114
|
-
* NOTE: Due to security issues, the `byteOffset` argument cannot be defaulted and must be provided by the user. \
|
|
115
|
-
* NOTE: For more information read the `readArrayBuffer` method documentation.
|
|
116
|
-
*/
|
|
117
|
-
static readPacketIdArrayBuffer(arraybuffer, byteOffset) {
|
|
118
|
-
return new Uint8Array(arraybuffer, byteOffset, 1)[0];
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* Reads/deserializes from the given Buffer. \
|
|
122
|
-
* Method available ONLY on NodeJS and Bun.
|
|
123
|
-
*
|
|
124
|
-
* If possible, always prefer reading using this method, as it is much faster than the other ones.
|
|
125
|
-
*
|
|
126
|
-
* NOTE: if you have an ArrayBuffer do not bother wrapping it into a node Buffer yourself. \
|
|
127
|
-
* NOTE: if you have an ArrayBuffer use the appropriate `readArrayBuffer`.
|
|
128
|
-
*/
|
|
129
|
-
readNodeBuffer(dataIn, offsetPointer = { offset: 0 }, byteLength = dataIn.byteLength) {
|
|
130
|
-
return this.read(dataIn, offsetPointer, byteLength, GET_FUNCTION_BUF);
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Reads/deserializes from the given DataView.
|
|
134
|
-
*
|
|
135
|
-
* NOTE: if you have an ArrayBuffer do not bother wrapping it into a DataView yourself. \
|
|
136
|
-
* NOTE: if you have an ArrayBuffer use the appropriate `readArrayBuffer`.
|
|
137
|
-
*/
|
|
138
|
-
readDataView(dataIn, offsetPointer = { offset: 0 }, byteLength = dataIn.byteLength) {
|
|
139
|
-
return this.read(dataIn, offsetPointer, byteLength, GET_FUNCTION);
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* Reads/deserializes from the given ArrayBuffer. \
|
|
143
|
-
* WARNING: this method is practically a HACK.
|
|
144
|
-
*
|
|
145
|
-
* When using this method both the `byteOffset` and `byteLength` are REQUIRED and cannot be defaulted. \
|
|
146
|
-
* This is to prevent serious bugs and security issues. \
|
|
147
|
-
* That is because often raw ArrayBuffers come from a pre-allocated buffer pool and do not start at byteOffset 0.
|
|
148
|
-
*
|
|
149
|
-
* NOTE: if you have a node Buffer do not bother wrapping it into an ArrayBuffer yourself. \
|
|
150
|
-
* NOTE: if you have a node Buffer use the appropriate `readNodeBuffer` as it is much faster and less error prone.
|
|
151
|
-
*/
|
|
152
|
-
readArrayBuffer(dataIn, byteOffset, byteLength) {
|
|
153
|
-
return this.read(
|
|
154
|
-
hasNodeBuffers ? Buffer.from(dataIn, byteOffset, byteLength) : new DataView(dataIn, byteOffset, byteLength),
|
|
155
|
-
{ offset: 0 },
|
|
156
|
-
// The underlying buffer has already been offsetted
|
|
157
|
-
byteLength,
|
|
158
|
-
hasNodeBuffers ? GET_FUNCTION_BUF : GET_FUNCTION
|
|
159
|
-
);
|
|
160
|
-
}
|
|
161
|
-
/**
|
|
162
|
-
* Writes/serializes the given object into a Buffer. \
|
|
163
|
-
* Method available ONLY on NodeJS and Bun.
|
|
164
|
-
*
|
|
165
|
-
* If possible, always prefer writing using this method, as it is much faster than the other ones.
|
|
166
|
-
*/
|
|
167
|
-
writeNodeBuffer(dataOut) {
|
|
168
|
-
const buffer = Buffer.allocUnsafe(this.minimumByteLength);
|
|
169
|
-
return this.write(buffer, dataOut, { offset: 0 }, SET_FUNCTION_BUF, growNodeBuffer);
|
|
170
|
-
}
|
|
171
|
-
/**
|
|
172
|
-
* Writes/serializes the given object into a DataView. \
|
|
173
|
-
*/
|
|
174
|
-
writeDataView(dataOut) {
|
|
175
|
-
const dataview = new DataView(new ArrayBuffer(this.minimumByteLength));
|
|
176
|
-
return this.write(dataview, dataOut, { offset: 0 }, SET_FUNCTION, growDataView);
|
|
177
|
-
}
|
|
178
|
-
/**
|
|
179
|
-
* Writes/serializes the given object into an ArrayBuffer. \
|
|
180
|
-
* This method is just a wrapper around either `writeNodeBuffer` or `writeDataView`. \
|
|
181
|
-
*
|
|
182
|
-
* This method works with JavaScript standard raw ArrayBuffer(s) and, as such, is very error prone: \
|
|
183
|
-
* Make sure you're using the returned byteLength and byteOffset fields in the read counterpart. \
|
|
184
|
-
*
|
|
185
|
-
* Always consider whether is possible to use directly `writeNodeBuffer` or `writeDataView` instead of `writeArrayBuffer`. \
|
|
186
|
-
* For more information read the `readArrayBuffer` documentation.
|
|
187
|
-
*/
|
|
188
|
-
writeArrayBuffer(dataOut) {
|
|
189
|
-
const buf = hasNodeBuffers ? this.writeNodeBuffer(dataOut) : this.writeDataView(dataOut);
|
|
190
|
-
return { buffer: buf.buffer, byteLength: buf.byteLength, byteOffset: buf.byteOffset };
|
|
191
|
-
}
|
|
192
|
-
entries;
|
|
193
|
-
canFastWrite;
|
|
194
|
-
minimumByteLength;
|
|
195
|
-
read(dataIn, offsetPointer, byteLength, readFunctions) {
|
|
196
|
-
if (byteLength + offsetPointer.offset < this.minimumByteLength) {
|
|
197
|
-
throw new Error(
|
|
198
|
-
`There is no space available to fit a packet of type ${this.packetId} at offset ${offsetPointer.offset}`
|
|
199
|
-
);
|
|
200
|
-
}
|
|
201
|
-
if (readFunctions[0 /* UNSIGNED_INT_8 */](dataIn, offsetPointer.offset) !== this.packetId) {
|
|
202
|
-
throw new Error(
|
|
203
|
-
`Data at offset ${offsetPointer.offset} is not a packet of type ${this.packetId}`
|
|
204
|
-
);
|
|
205
|
-
}
|
|
206
|
-
offsetPointer.offset += 1;
|
|
207
|
-
const result = {};
|
|
208
|
-
for (const [name, def] of this.entries) {
|
|
209
|
-
if (Array.isArray(def)) {
|
|
210
|
-
const length = (
|
|
211
|
-
// def[1] is the length of a statically-sized array, if undefined: must read the length from the buffer as it means it's a dynamically-sized array
|
|
212
|
-
def[1] ?? readFunctions[0 /* UNSIGNED_INT_8 */](dataIn, offsetPointer.offset++)
|
|
213
|
-
);
|
|
214
|
-
const array = Array(length);
|
|
215
|
-
const itemType = def[0];
|
|
216
|
-
if (typeof itemType === "object") {
|
|
217
|
-
for (let i = 0; i < length; ++i) {
|
|
218
|
-
array[i] = itemType.read(dataIn, offsetPointer, byteLength, readFunctions);
|
|
219
|
-
}
|
|
220
|
-
} else {
|
|
221
|
-
const itemSize = BYTE_SIZE[itemType];
|
|
222
|
-
for (let i = 0; i < length; ++i) {
|
|
223
|
-
array[i] = readFunctions[itemType](dataIn, offsetPointer.offset);
|
|
224
|
-
offsetPointer.offset += itemSize;
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
result[name] = array;
|
|
228
|
-
} else if (typeof def === "object") {
|
|
229
|
-
result[name] = def.read(dataIn, offsetPointer, byteLength, readFunctions);
|
|
230
|
-
} else {
|
|
231
|
-
result[name] = readFunctions[def](dataIn, offsetPointer.offset);
|
|
232
|
-
offsetPointer.offset += BYTE_SIZE[def];
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
return result;
|
|
236
|
-
}
|
|
237
|
-
write(buffer, dataOut, offsetPointer, writeFunctions, growBufferFunction) {
|
|
238
|
-
writeFunctions[0 /* UNSIGNED_INT_8 */](buffer, this.packetId, offsetPointer.offset);
|
|
239
|
-
offsetPointer.offset += 1;
|
|
240
|
-
if (this.canFastWrite) {
|
|
241
|
-
this.fastWrite(buffer, dataOut, offsetPointer, writeFunctions);
|
|
242
|
-
return buffer;
|
|
243
|
-
} else {
|
|
244
|
-
return this.slowWrite(
|
|
245
|
-
buffer,
|
|
246
|
-
dataOut,
|
|
247
|
-
offsetPointer,
|
|
248
|
-
this.minimumByteLength,
|
|
249
|
-
this.minimumByteLength,
|
|
250
|
-
writeFunctions,
|
|
251
|
-
growBufferFunction
|
|
252
|
-
);
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
/**
|
|
256
|
-
* Fast write does not support writing dynamically-sized arrays.
|
|
257
|
-
*/
|
|
258
|
-
fastWrite(buffer, dataOut, offsetPointer, writeFunctions) {
|
|
259
|
-
for (const [name, def] of this.entries) {
|
|
260
|
-
if (Array.isArray(def)) {
|
|
261
|
-
const itemType = def[0];
|
|
262
|
-
const length = def[1];
|
|
263
|
-
const data = dataOut[name];
|
|
264
|
-
if (typeof itemType === "object") {
|
|
265
|
-
for (let i = 0; i < length; ++i) {
|
|
266
|
-
itemType.fastWrite(buffer, data[i], offsetPointer, writeFunctions);
|
|
267
|
-
}
|
|
268
|
-
} else {
|
|
269
|
-
const itemSize = BYTE_SIZE[itemType];
|
|
270
|
-
for (let i = 0; i < length; ++i) {
|
|
271
|
-
writeFunctions[itemType](buffer, data[i], offsetPointer.offset);
|
|
272
|
-
offsetPointer.offset += itemSize;
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
} else if (typeof def === "object") {
|
|
276
|
-
def.fastWrite(buffer, dataOut[name], offsetPointer, writeFunctions);
|
|
277
|
-
} else {
|
|
278
|
-
writeFunctions[def](buffer, dataOut[name], offsetPointer.offset);
|
|
279
|
-
offsetPointer.offset += BYTE_SIZE[def];
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
/**
|
|
284
|
-
* The slow writing path tries writing data into the buffer as fast as the fast writing path does. \
|
|
285
|
-
* But, if a non-empty dynamically-sized array is encountered, the buffer needs to grow, slightly reducing performance.
|
|
286
|
-
*/
|
|
287
|
-
slowWrite(buffer, dataOut, offsetPointer, byteLength, maxByteLength, writeFunctions, growBufferFunction) {
|
|
288
|
-
for (const [name, def] of this.entries) {
|
|
289
|
-
const data = dataOut[name];
|
|
290
|
-
if (Array.isArray(def)) {
|
|
291
|
-
const length = data.length;
|
|
292
|
-
const isDynamicArray = def[1] === void 0;
|
|
293
|
-
if (isDynamicArray) {
|
|
294
|
-
writeFunctions[0 /* UNSIGNED_INT_8 */](buffer, length, offsetPointer.offset);
|
|
295
|
-
offsetPointer.offset += 1;
|
|
296
|
-
}
|
|
297
|
-
if (length > 0) {
|
|
298
|
-
const itemType = def[0];
|
|
299
|
-
if (typeof itemType === "object") {
|
|
300
|
-
if (isDynamicArray) {
|
|
301
|
-
const neededBytesForElements = length * itemType.minimumByteLength;
|
|
302
|
-
byteLength += neededBytesForElements;
|
|
303
|
-
maxByteLength += neededBytesForElements;
|
|
304
|
-
if (buffer.byteLength < maxByteLength) {
|
|
305
|
-
buffer = growBufferFunction(buffer, maxByteLength);
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
for (const object of data) {
|
|
309
|
-
writeFunctions[0 /* UNSIGNED_INT_8 */](
|
|
310
|
-
buffer,
|
|
311
|
-
itemType.packetId,
|
|
312
|
-
offsetPointer.offset
|
|
313
|
-
);
|
|
314
|
-
offsetPointer.offset += 1;
|
|
315
|
-
buffer = itemType.slowWrite(
|
|
316
|
-
buffer,
|
|
317
|
-
object,
|
|
318
|
-
offsetPointer,
|
|
319
|
-
byteLength,
|
|
320
|
-
maxByteLength,
|
|
321
|
-
writeFunctions,
|
|
322
|
-
growBufferFunction
|
|
323
|
-
);
|
|
324
|
-
byteLength = offsetPointer.offset;
|
|
325
|
-
maxByteLength = buffer.byteLength;
|
|
326
|
-
}
|
|
327
|
-
} else {
|
|
328
|
-
const itemSize = BYTE_SIZE[itemType];
|
|
329
|
-
if (isDynamicArray) {
|
|
330
|
-
const neededBytesForElements = length * itemSize;
|
|
331
|
-
byteLength += neededBytesForElements;
|
|
332
|
-
maxByteLength += neededBytesForElements;
|
|
333
|
-
if (buffer.byteLength < maxByteLength) {
|
|
334
|
-
buffer = growBufferFunction(buffer, maxByteLength);
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
for (const number of data) {
|
|
338
|
-
writeFunctions[itemType](buffer, number, offsetPointer.offset);
|
|
339
|
-
offsetPointer.offset += itemSize;
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
} else if (typeof def === "object") {
|
|
344
|
-
writeFunctions[0 /* UNSIGNED_INT_8 */](buffer, def.packetId, offsetPointer.offset);
|
|
345
|
-
offsetPointer.offset += 1;
|
|
346
|
-
buffer = def.slowWrite(
|
|
347
|
-
buffer,
|
|
348
|
-
data,
|
|
349
|
-
offsetPointer,
|
|
350
|
-
byteLength,
|
|
351
|
-
maxByteLength,
|
|
352
|
-
writeFunctions,
|
|
353
|
-
growBufferFunction
|
|
354
|
-
);
|
|
355
|
-
byteLength = offsetPointer.offset;
|
|
356
|
-
maxByteLength = buffer.byteLength;
|
|
357
|
-
} else {
|
|
358
|
-
writeFunctions[def](buffer, data, offsetPointer.offset);
|
|
359
|
-
offsetPointer.offset += BYTE_SIZE[def];
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
return buffer;
|
|
363
|
-
}
|
|
364
|
-
};
|
|
365
|
-
function sortEntries(definition) {
|
|
366
|
-
return Object.entries(definition).sort(
|
|
367
|
-
([fieldName1], [fieldName2]) => fieldName1.localeCompare(fieldName2)
|
|
368
|
-
);
|
|
369
|
-
}
|
|
370
|
-
function inspectEntries(entries) {
|
|
371
|
-
let minimumByteLength = 1;
|
|
372
|
-
let canFastWrite = true;
|
|
373
|
-
for (const [, type] of entries) {
|
|
374
|
-
if (Array.isArray(type)) {
|
|
375
|
-
if (type.length === 2) {
|
|
376
|
-
const itemSize = typeof type[0] === "object" ? type[0].minimumByteLength : BYTE_SIZE[type[0]];
|
|
377
|
-
minimumByteLength += type[1] * itemSize;
|
|
378
|
-
} else {
|
|
379
|
-
minimumByteLength += 1;
|
|
380
|
-
canFastWrite = false;
|
|
381
|
-
}
|
|
382
|
-
} else if (type instanceof BinaryPacket) {
|
|
383
|
-
minimumByteLength += type.minimumByteLength;
|
|
384
|
-
canFastWrite &&= type.canFastWrite;
|
|
385
|
-
} else {
|
|
386
|
-
minimumByteLength += BYTE_SIZE[type];
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
return { minimumByteLength, canFastWrite };
|
|
390
|
-
}
|
|
391
|
-
var BYTE_SIZE = Array(8);
|
|
392
|
-
BYTE_SIZE[0 /* UNSIGNED_INT_8 */] = 1;
|
|
393
|
-
BYTE_SIZE[3 /* INT_8 */] = 1;
|
|
394
|
-
BYTE_SIZE[1 /* UNSIGNED_INT_16 */] = 2;
|
|
395
|
-
BYTE_SIZE[4 /* INT_16 */] = 2;
|
|
396
|
-
BYTE_SIZE[2 /* UNSIGNED_INT_32 */] = 4;
|
|
397
|
-
BYTE_SIZE[5 /* INT_32 */] = 4;
|
|
398
|
-
BYTE_SIZE[6 /* FLOAT_32 */] = 4;
|
|
399
|
-
BYTE_SIZE[7 /* FLOAT_64 */] = 8;
|
|
400
|
-
var GET_FUNCTION = Array(8);
|
|
401
|
-
GET_FUNCTION[0 /* UNSIGNED_INT_8 */] = (view, offset) => view.getUint8(offset);
|
|
402
|
-
GET_FUNCTION[3 /* INT_8 */] = (view, offset) => view.getInt8(offset);
|
|
403
|
-
GET_FUNCTION[1 /* UNSIGNED_INT_16 */] = (view, offset) => view.getUint16(offset);
|
|
404
|
-
GET_FUNCTION[4 /* INT_16 */] = (view, offset) => view.getInt16(offset);
|
|
405
|
-
GET_FUNCTION[2 /* UNSIGNED_INT_32 */] = (view, offset) => view.getUint32(offset);
|
|
406
|
-
GET_FUNCTION[5 /* INT_32 */] = (view, offset) => view.getInt32(offset);
|
|
407
|
-
GET_FUNCTION[6 /* FLOAT_32 */] = (view, offset) => view.getFloat32(offset);
|
|
408
|
-
GET_FUNCTION[7 /* FLOAT_64 */] = (view, offset) => view.getFloat64(offset);
|
|
409
|
-
var SET_FUNCTION = Array(8);
|
|
410
|
-
SET_FUNCTION[0 /* UNSIGNED_INT_8 */] = (view, value, offset) => view.setUint8(offset, value);
|
|
411
|
-
SET_FUNCTION[3 /* INT_8 */] = (view, value, offset) => view.setInt8(offset, value);
|
|
412
|
-
SET_FUNCTION[1 /* UNSIGNED_INT_16 */] = (view, value, offset) => view.setUint16(offset, value);
|
|
413
|
-
SET_FUNCTION[4 /* INT_16 */] = (view, value, offset) => view.setInt16(offset, value);
|
|
414
|
-
SET_FUNCTION[2 /* UNSIGNED_INT_32 */] = (view, value, offset) => view.setUint32(offset, value);
|
|
415
|
-
SET_FUNCTION[5 /* INT_32 */] = (view, value, offset) => view.setInt32(offset, value);
|
|
416
|
-
SET_FUNCTION[6 /* FLOAT_32 */] = (view, value, offset) => view.setFloat32(offset, value);
|
|
417
|
-
SET_FUNCTION[7 /* FLOAT_64 */] = (view, value, offset) => view.setFloat64(offset, value);
|
|
418
|
-
var SET_FUNCTION_BUF = Array(8);
|
|
419
|
-
if (hasNodeBuffers) {
|
|
420
|
-
SET_FUNCTION_BUF[0 /* UNSIGNED_INT_8 */] = (view, value, offset) => view.writeUint8(value, offset);
|
|
421
|
-
SET_FUNCTION_BUF[3 /* INT_8 */] = (view, value, offset) => view.writeInt8(value, offset);
|
|
422
|
-
SET_FUNCTION_BUF[1 /* UNSIGNED_INT_16 */] = (view, value, offset) => view.writeUint16LE(value, offset);
|
|
423
|
-
SET_FUNCTION_BUF[4 /* INT_16 */] = (view, value, offset) => view.writeInt16LE(value, offset);
|
|
424
|
-
SET_FUNCTION_BUF[2 /* UNSIGNED_INT_32 */] = (view, value, offset) => view.writeUint32LE(value, offset);
|
|
425
|
-
SET_FUNCTION_BUF[5 /* INT_32 */] = (view, value, offset) => view.writeInt32LE(value, offset);
|
|
426
|
-
SET_FUNCTION_BUF[6 /* FLOAT_32 */] = (view, value, offset) => view.writeFloatLE(value, offset);
|
|
427
|
-
SET_FUNCTION_BUF[7 /* FLOAT_64 */] = (view, value, offset) => view.writeDoubleLE(value, offset);
|
|
428
|
-
}
|
|
429
|
-
var GET_FUNCTION_BUF = Array(8);
|
|
430
|
-
if (hasNodeBuffers) {
|
|
431
|
-
GET_FUNCTION_BUF[0 /* UNSIGNED_INT_8 */] = (view, offset) => view.readUint8(offset);
|
|
432
|
-
GET_FUNCTION_BUF[3 /* INT_8 */] = (view, offset) => view.readInt8(offset);
|
|
433
|
-
GET_FUNCTION_BUF[1 /* UNSIGNED_INT_16 */] = (view, offset) => view.readUint16LE(offset);
|
|
434
|
-
GET_FUNCTION_BUF[4 /* INT_16 */] = (view, offset) => view.readInt16LE(offset);
|
|
435
|
-
GET_FUNCTION_BUF[2 /* UNSIGNED_INT_32 */] = (view, offset) => view.readUint32LE(offset);
|
|
436
|
-
GET_FUNCTION_BUF[5 /* INT_32 */] = (view, offset) => view.readInt32LE(offset);
|
|
437
|
-
GET_FUNCTION_BUF[6 /* FLOAT_32 */] = (view, offset) => view.readFloatLE(offset);
|
|
438
|
-
GET_FUNCTION_BUF[7 /* FLOAT_64 */] = (view, offset) => view.readDoubleLE(offset);
|
|
439
|
-
}
|
|
440
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
441
|
-
0 && (module.exports = {
|
|
442
|
-
BinaryPacket,
|
|
443
|
-
Field,
|
|
444
|
-
FieldArray,
|
|
445
|
-
FieldFixedArray
|
|
446
|
-
});
|
|
1
|
+
"use strict";var E=Object.defineProperty;var A=Object.getOwnPropertyDescriptor;var b=Object.getOwnPropertyNames;var L=Object.prototype.hasOwnProperty;var g=(n,e)=>{for(var t in e)E(n,t,{get:e[t],enumerable:!0})},S=(n,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of b(e))!L.call(n,i)&&i!==t&&E(n,i,{get:()=>e[i],enumerable:!(r=A(e,i))||r.enumerable});return n};var G=n=>S(E({},"__esModule",{value:!0}),n);var v={};g(v,{BinaryPacket:()=>p,Field:()=>w,FieldArray:()=>O,FieldFixedArray:()=>k});module.exports=G(v);var B=typeof Buffer=="function";function U(n,e){let t=new ArrayBuffer(e),r=Math.min(n.byteLength,t.byteLength),i=Math.trunc(r/8);new Float64Array(t,0,i).set(new Float64Array(n.buffer,0,i));let s=i*8;return i=r-s,new Uint8Array(t,s,i).set(new Uint8Array(n.buffer,s,i)),new DataView(t)}function h(n,e){let t=Buffer.allocUnsafe(e);return n.copy(t),t}var w=(a=>(a[a.UNSIGNED_INT_8=0]="UNSIGNED_INT_8",a[a.UNSIGNED_INT_16=1]="UNSIGNED_INT_16",a[a.UNSIGNED_INT_32=2]="UNSIGNED_INT_32",a[a.INT_8=3]="INT_8",a[a.INT_16=4]="INT_16",a[a.INT_32=5]="INT_32",a[a.FLOAT_32=6]="FLOAT_32",a[a.FLOAT_64=7]="FLOAT_64",a))(w||{});function O(n){return[n]}function k(n,e){if(e<0||!Number.isFinite(e))throw new RangeError("Length of a FixedArray must be a positive integer.");return[n,e]}var p=class n{constructor(e,t){this.packetId=e;this.entries=t?J(t):[];let r=V(this.entries);this.minimumByteLength=r.minimumByteLength,this.canFastWrite=r.canFastWrite}static define(e,t){if(e<0||!Number.isFinite(e))throw new RangeError("Packet IDs must be positive integers.");if(e>255)throw new RangeError("Packet IDs greater than 255 are not supported. Do you REALLY need more than 255 different kinds of packets?");return new n(e,t)}static readPacketIdNodeBuffer(e,t=0){return e.readUint8(t)}static readPacketIdDataView(e,t=0){return e.getUint8(t)}static readPacketIdArrayBuffer(e,t){return new Uint8Array(e,t,1)[0]}readNodeBuffer(e,t={offset:0},r=e.byteLength){return this.read(e,t,r,l)}readDataView(e,t={offset:0},r=e.byteLength){return this.read(e,t,r,m)}readArrayBuffer(e,t,r){return this.read(B?Buffer.from(e,t,r):new DataView(e,t,r),{offset:0},r,B?l:m)}writeNodeBuffer(e){let t=Buffer.allocUnsafe(this.minimumByteLength);return this.write(t,e,{offset:0},d,h)}writeDataView(e){let t=new DataView(new ArrayBuffer(this.minimumByteLength));return this.write(t,e,{offset:0},I,U)}writeArrayBuffer(e){let t=B?this.writeNodeBuffer(e):this.writeDataView(e);return{buffer:t.buffer,byteLength:t.byteLength,byteOffset:t.byteOffset}}entries;canFastWrite;minimumByteLength;read(e,t,r,i){if(r+t.offset<this.minimumByteLength)throw new Error(`There is no space available to fit a packet of type ${this.packetId} at offset ${t.offset}`);if(i[0](e,t.offset)!==this.packetId)throw new Error(`Data at offset ${t.offset} is not a packet of type ${this.packetId}`);t.offset+=1;let s={};for(let[f,o]of this.entries)if(Array.isArray(o)){let a=o[1]??i[0](e,t.offset++),y=Array(a),T=o[0];if(typeof T=="object")for(let u=0;u<a;++u)y[u]=T.read(e,t,r,i);else{let u=N[T];for(let _=0;_<a;++_)y[_]=i[T](e,t.offset),t.offset+=u}s[f]=y}else typeof o=="object"?s[f]=o.read(e,t,r,i):(s[f]=i[o](e,t.offset),t.offset+=N[o]);return s}write(e,t,r,i,s){return i[0](e,this.packetId,r.offset),r.offset+=1,this.canFastWrite?(this.fastWrite(e,t,r,i),e):this.slowWrite(e,t,r,this.minimumByteLength,this.minimumByteLength,i,s)}fastWrite(e,t,r,i){for(let[s,f]of this.entries)if(Array.isArray(f)){let o=f[0],a=f[1],y=t[s];if(typeof o=="object")for(let T=0;T<a;++T)o.fastWrite(e,y[T],r,i);else{let T=N[o];for(let u=0;u<a;++u)i[o](e,y[u],r.offset),r.offset+=T}}else typeof f=="object"?f.fastWrite(e,t[s],r,i):(i[f](e,t[s],r.offset),r.offset+=N[f])}slowWrite(e,t,r,i,s,f,o){for(let[a,y]of this.entries){let T=t[a];if(Array.isArray(y)){let u=T.length,_=y[1]===void 0;if(_&&(f[0](e,u,r.offset),r.offset+=1),u>0){let D=y[0];if(typeof D=="object"){if(_){let c=u*D.minimumByteLength;i+=c,s+=c,e.byteLength<s&&(e=o(e,s))}for(let c of T)f[0](e,D.packetId,r.offset),r.offset+=1,e=D.slowWrite(e,c,r,i,s,f,o),i=r.offset,s=e.byteLength}else{let c=N[D];if(_){let F=u*c;i+=F,s+=F,e.byteLength<s&&(e=o(e,s))}for(let F of T)f[D](e,F,r.offset),r.offset+=c}}}else typeof y=="object"?(f[0](e,y.packetId,r.offset),r.offset+=1,e=y.slowWrite(e,T,r,i,s,f,o),i=r.offset,s=e.byteLength):(f[y](e,T,r.offset),r.offset+=N[y])}return e}};function J(n){return Object.entries(n).sort(([e],[t])=>e.localeCompare(t))}function V(n){let e=1,t=!0;for(let[,r]of n)if(Array.isArray(r))if(r.length===2){let i=typeof r[0]=="object"?r[0].minimumByteLength:N[r[0]];e+=r[1]*i}else e+=1,t=!1;else r instanceof p?(e+=r.minimumByteLength,t&&=r.canFastWrite):e+=N[r];return{minimumByteLength:e,canFastWrite:t}}var N=Array(8);N[0]=1;N[3]=1;N[1]=2;N[4]=2;N[2]=4;N[5]=4;N[6]=4;N[7]=8;var m=Array(8);m[0]=(n,e)=>n.getUint8(e);m[3]=(n,e)=>n.getInt8(e);m[1]=(n,e)=>n.getUint16(e);m[4]=(n,e)=>n.getInt16(e);m[2]=(n,e)=>n.getUint32(e);m[5]=(n,e)=>n.getInt32(e);m[6]=(n,e)=>n.getFloat32(e);m[7]=(n,e)=>n.getFloat64(e);var I=Array(8);I[0]=(n,e,t)=>n.setUint8(t,e);I[3]=(n,e,t)=>n.setInt8(t,e);I[1]=(n,e,t)=>n.setUint16(t,e);I[4]=(n,e,t)=>n.setInt16(t,e);I[2]=(n,e,t)=>n.setUint32(t,e);I[5]=(n,e,t)=>n.setInt32(t,e);I[6]=(n,e,t)=>n.setFloat32(t,e);I[7]=(n,e,t)=>n.setFloat64(t,e);var d=Array(8);B&&(d[0]=(n,e,t)=>n.writeUint8(e,t),d[3]=(n,e,t)=>n.writeInt8(e,t),d[1]=(n,e,t)=>n.writeUint16LE(e,t),d[4]=(n,e,t)=>n.writeInt16LE(e,t),d[2]=(n,e,t)=>n.writeUint32LE(e,t),d[5]=(n,e,t)=>n.writeInt32LE(e,t),d[6]=(n,e,t)=>n.writeFloatLE(e,t),d[7]=(n,e,t)=>n.writeDoubleLE(e,t));var l=Array(8);B&&(l[0]=(n,e)=>n.readUint8(e),l[3]=(n,e)=>n.readInt8(e),l[1]=(n,e)=>n.readUint16LE(e),l[4]=(n,e)=>n.readInt16LE(e),l[2]=(n,e)=>n.readUint32LE(e),l[5]=(n,e)=>n.readInt32LE(e),l[6]=(n,e)=>n.readFloatLE(e),l[7]=(n,e)=>n.readDoubleLE(e));0&&(module.exports={BinaryPacket,Field,FieldArray,FieldFixedArray});
|
|
447
2
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/buffers.ts"],"sourcesContent":["import { growDataView, growNodeBuffer, hasNodeBuffers } from './buffers'\r\n\r\nexport const enum Field {\r\n /**\r\n * Defines a 1 byte (8 bits) unsigned integer field. \\\r\n * (Range: 0 - 255)\r\n */\r\n UNSIGNED_INT_8 = 0,\r\n\r\n /**\r\n * Defines a 2 bytes (16 bits) unsigned integer field. \\\r\n * (Range: 0 - 65535)\r\n */\r\n UNSIGNED_INT_16,\r\n\r\n /**\r\n * Defines a 4 bytes (32 bits) unsigned integer field. \\\r\n * (Range: 0 - 4294967295)\r\n */\r\n UNSIGNED_INT_32,\r\n\r\n /**\r\n * Defines a 1 byte (8 bits) signed integer field. \\\r\n * (Range: -128 - 127)\r\n */\r\n INT_8,\r\n\r\n /**\r\n * Defines a 2 bytes (16 bits) signed integer field. \\\r\n * (Range: -32768 - 32767)\r\n */\r\n INT_16,\r\n\r\n /**\r\n * Defines a 4 bytes (32 bits) signed integer field. \\\r\n * (Range: -2147483648 - 2147483647)\r\n */\r\n INT_32,\r\n\r\n /**\r\n * Defines a 4 bytes (32 bits) floating-point field. \\\r\n */\r\n FLOAT_32,\r\n\r\n /**\r\n * Defines a 8 bytes (64 bits) floating-point field. \\\r\n */\r\n FLOAT_64\r\n}\r\n\r\n/**\r\n * Defines a dynamically-sized array with elements of a certain type. \\\r\n * Dynamically-sized arrays are useful when a packet's field is an array of a non pre-defined length. \\\r\n * Although, this makes dynamically-sized arrays more memory expensive as the internal buffer needs to be grown accordingly.\r\n *\r\n * NOTE: If an array will ALWAYS have the same length, prefer using the `FieldFixedArray` type, for both better performance and memory efficiency. \\\r\n * NOTE: As of now, dynamic arrays can have at most 256 elements.\r\n */\r\nexport function FieldArray<T extends Field | BinaryPacket<Definition>>(item: T): [itemType: T] {\r\n return [item]\r\n}\r\n\r\n/**\r\n * Defines a statically-sized array with elements of a certain type. \\\r\n * Fixed arrays are useful when a packet's field is an array of a pre-defined length. \\\r\n * Fixed arrays much more memory efficient and performant than non-fixed ones.\r\n *\r\n * NOTE: If an array will not always have the same length, use the `FieldArray` type.\r\n */\r\nexport function FieldFixedArray<T extends Field | BinaryPacket<Definition>, Length extends number>(\r\n item: T,\r\n length: Length\r\n): [itemType: T, length: Length] {\r\n if (length < 0 || !Number.isFinite(length)) {\r\n throw new RangeError('Length of a FixedArray must be a positive integer.')\r\n }\r\n\r\n return [item, length]\r\n}\r\n\r\nexport class BinaryPacket<T extends Definition> {\r\n /**\r\n * Defines a new binary packet. \\\r\n * Make sure that every `packetId` is unique.\r\n * @throws RangeError If packetId is negative, floating-point, or greater than 255.\r\n */\r\n static define<T extends Definition>(packetId: number, definition?: T) {\r\n if (packetId < 0 || !Number.isFinite(packetId)) {\r\n throw new RangeError('Packet IDs must be positive integers.')\r\n }\r\n\r\n if (packetId > 255) {\r\n throw new RangeError(\r\n 'Packet IDs greater than 255 are not supported. Do you REALLY need more than 255 different kinds of packets?'\r\n )\r\n }\r\n\r\n return new BinaryPacket(packetId, definition)\r\n }\r\n\r\n /**\r\n * Reads just the packetId from the given Buffer. \\\r\n * This method practically just reads the uint8 at offset `byteOffset` (default: 0). \\\r\n * Useful if the receiving side receives multiple types of packets.\r\n */\r\n static readPacketIdNodeBuffer(buffer: Buffer, byteOffset = 0) {\r\n return buffer.readUint8(byteOffset)\r\n }\r\n\r\n /**\r\n * Reads just the packetId from the given DataView. \\\r\n * This method practically just reads the uint8 at offset `byteOffset` (default: 0). \\\r\n * Useful if the receiving side receives multiple types of packets.\r\n */\r\n static readPacketIdDataView(dataview: DataView, byteOffset = 0) {\r\n return dataview.getUint8(byteOffset)\r\n }\r\n\r\n /**\r\n * Reads just the packetId from the given ArrayBuffer. \\\r\n * This method practically just reads the uint8 at offset `byteOffset`. \\\r\n * Useful if the receiving side receives multiple types of packets.\r\n *\r\n * NOTE: Due to security issues, the `byteOffset` argument cannot be defaulted and must be provided by the user. \\\r\n * NOTE: For more information read the `readArrayBuffer` method documentation.\r\n */\r\n static readPacketIdArrayBuffer(arraybuffer: ArrayBuffer, byteOffset: number) {\r\n return new Uint8Array(arraybuffer, byteOffset, 1)[0]\r\n }\r\n\r\n /**\r\n * Reads/deserializes from the given Buffer. \\\r\n * Method available ONLY on NodeJS and Bun.\r\n *\r\n * If possible, always prefer reading using this method, as it is much faster than the other ones.\r\n *\r\n * NOTE: if you have an ArrayBuffer do not bother wrapping it into a node Buffer yourself. \\\r\n * NOTE: if you have an ArrayBuffer use the appropriate `readArrayBuffer`.\r\n */\r\n readNodeBuffer(\r\n dataIn: Buffer,\r\n offsetPointer = { offset: 0 },\r\n byteLength = dataIn.byteLength\r\n ): ToJson<T> {\r\n return this.read(dataIn, offsetPointer, byteLength, GET_FUNCTION_BUF)\r\n }\r\n\r\n /**\r\n * Reads/deserializes from the given DataView.\r\n *\r\n * NOTE: if you have an ArrayBuffer do not bother wrapping it into a DataView yourself. \\\r\n * NOTE: if you have an ArrayBuffer use the appropriate `readArrayBuffer`.\r\n */\r\n readDataView(\r\n dataIn: DataView,\r\n offsetPointer = { offset: 0 },\r\n byteLength = dataIn.byteLength\r\n ): ToJson<T> {\r\n return this.read(dataIn, offsetPointer, byteLength, GET_FUNCTION)\r\n }\r\n\r\n /**\r\n * Reads/deserializes from the given ArrayBuffer. \\\r\n * WARNING: this method is practically a HACK.\r\n *\r\n * When using this method both the `byteOffset` and `byteLength` are REQUIRED and cannot be defaulted. \\\r\n * This is to prevent serious bugs and security issues. \\\r\n * That is because often raw ArrayBuffers come from a pre-allocated buffer pool and do not start at byteOffset 0.\r\n *\r\n * NOTE: if you have a node Buffer do not bother wrapping it into an ArrayBuffer yourself. \\\r\n * NOTE: if you have a node Buffer use the appropriate `readNodeBuffer` as it is much faster and less error prone.\r\n */\r\n readArrayBuffer(\r\n dataIn: ArrayBuffer & { buffer?: undefined },\r\n byteOffset: number,\r\n byteLength: number\r\n ) {\r\n return this.read(\r\n hasNodeBuffers\r\n ? Buffer.from(dataIn, byteOffset, byteLength)\r\n : new DataView(dataIn, byteOffset, byteLength),\r\n { offset: 0 }, // The underlying buffer has already been offsetted\r\n byteLength,\r\n hasNodeBuffers ? GET_FUNCTION_BUF : GET_FUNCTION\r\n )\r\n }\r\n\r\n /**\r\n * Writes/serializes the given object into a Buffer. \\\r\n * Method available ONLY on NodeJS and Bun.\r\n *\r\n * If possible, always prefer writing using this method, as it is much faster than the other ones.\r\n */\r\n writeNodeBuffer(dataOut: ToJson<T>) {\r\n const buffer = Buffer.allocUnsafe(this.minimumByteLength)\r\n return this.write(buffer, dataOut, { offset: 0 }, SET_FUNCTION_BUF, growNodeBuffer)\r\n }\r\n\r\n /**\r\n * Writes/serializes the given object into a DataView. \\\r\n */\r\n writeDataView(dataOut: ToJson<T>) {\r\n const dataview = new DataView(new ArrayBuffer(this.minimumByteLength))\r\n return this.write(dataview, dataOut, { offset: 0 }, SET_FUNCTION, growDataView)\r\n }\r\n\r\n /**\r\n * Writes/serializes the given object into an ArrayBuffer. \\\r\n * This method is just a wrapper around either `writeNodeBuffer` or `writeDataView`. \\\r\n *\r\n * This method works with JavaScript standard raw ArrayBuffer(s) and, as such, is very error prone: \\\r\n * Make sure you're using the returned byteLength and byteOffset fields in the read counterpart. \\\r\n *\r\n * Always consider whether is possible to use directly `writeNodeBuffer` or `writeDataView` instead of `writeArrayBuffer`. \\\r\n * For more information read the `readArrayBuffer` documentation.\r\n */\r\n writeArrayBuffer(dataOut: ToJson<T>) {\r\n const buf = hasNodeBuffers ? this.writeNodeBuffer(dataOut) : this.writeDataView(dataOut)\r\n return { buffer: buf.buffer, byteLength: buf.byteLength, byteOffset: buf.byteOffset }\r\n }\r\n\r\n private readonly entries: Entries\r\n readonly canFastWrite: boolean\r\n readonly minimumByteLength: number\r\n\r\n private constructor(\r\n private readonly packetId: number,\r\n definition?: T\r\n ) {\r\n this.entries = definition ? sortEntries(definition) : []\r\n const inspection = inspectEntries(this.entries)\r\n\r\n this.minimumByteLength = inspection.minimumByteLength\r\n this.canFastWrite = inspection.canFastWrite\r\n }\r\n\r\n private read(\r\n dataIn: DataView | Buffer,\r\n offsetPointer: { offset: number },\r\n byteLength: number,\r\n readFunctions: typeof GET_FUNCTION | typeof GET_FUNCTION_BUF\r\n ): ToJson<T> {\r\n if (byteLength + offsetPointer.offset < this.minimumByteLength) {\r\n throw new Error(\r\n `There is no space available to fit a packet of type ${this.packetId} at offset ${offsetPointer.offset}`\r\n )\r\n }\r\n\r\n if (\r\n readFunctions[Field.UNSIGNED_INT_8](dataIn as any, offsetPointer.offset) !== this.packetId\r\n ) {\r\n throw new Error(\r\n `Data at offset ${offsetPointer.offset} is not a packet of type ${this.packetId}`\r\n )\r\n }\r\n\r\n offsetPointer.offset += 1\r\n const result: any = {}\r\n\r\n for (const [name, def] of this.entries) {\r\n if (Array.isArray(def)) {\r\n const length =\r\n // def[1] is the length of a statically-sized array, if undefined: must read the length from the buffer as it means it's a dynamically-sized array\r\n def[1] ?? readFunctions[Field.UNSIGNED_INT_8](dataIn as any, offsetPointer.offset++)\r\n\r\n const array = Array(length)\r\n\r\n const itemType = def[0]\r\n\r\n if (typeof itemType === 'object') {\r\n // Array of \"subpackets\"\r\n for (let i = 0; i < length; ++i) {\r\n array[i] = itemType.read(dataIn, offsetPointer, byteLength, readFunctions)\r\n }\r\n } else {\r\n // Array of primitives (numbers)\r\n const itemSize = BYTE_SIZE[itemType]\r\n\r\n // It seems like looping over each element is actually much faster than using TypedArrays bulk copy.\r\n // TODO: properly benchmark with various array sizes to see if it's actually the case.\r\n for (let i = 0; i < length; ++i) {\r\n array[i] = readFunctions[itemType](dataIn as any, offsetPointer.offset)\r\n offsetPointer.offset += itemSize\r\n }\r\n }\r\n\r\n // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access\r\n result[name] = array\r\n } else if (typeof def === 'object') {\r\n // Single \"subpacket\"\r\n // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access\r\n result[name] = def.read(dataIn, offsetPointer, byteLength, readFunctions)\r\n } else {\r\n // Single primitive (number)\r\n // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access\r\n result[name] = readFunctions[def](dataIn as any, offsetPointer.offset)\r\n offsetPointer.offset += BYTE_SIZE[def]\r\n }\r\n }\r\n\r\n return result as ToJson<T>\r\n }\r\n\r\n private write<Buf extends DataView | Buffer>(\r\n buffer: Buf,\r\n dataOut: ToJson<T>,\r\n offsetPointer: { offset: number },\r\n writeFunctions: typeof SET_FUNCTION | typeof SET_FUNCTION_BUF,\r\n growBufferFunction: (buffer: Buf, newByteLength: number) => Buf\r\n ): Buf {\r\n writeFunctions[Field.UNSIGNED_INT_8](buffer as any, this.packetId, offsetPointer.offset)\r\n offsetPointer.offset += 1\r\n\r\n if (this.canFastWrite) {\r\n // If there are no arrays, the minimumByteLength always equals to the full needed byteLength.\r\n // So we can take the fast path, since we know beforehand that the buffer isn't going to grow.\r\n this.fastWrite(buffer, dataOut, offsetPointer, writeFunctions)\r\n return buffer\r\n } else {\r\n // If non-empty arrays are encountered, the buffer must grow.\r\n // If every array is empty, the speed of this path is comparable to the fast path.\r\n return this.slowWrite(\r\n buffer,\r\n dataOut,\r\n offsetPointer,\r\n this.minimumByteLength,\r\n this.minimumByteLength,\r\n writeFunctions,\r\n growBufferFunction\r\n )\r\n }\r\n }\r\n\r\n /**\r\n * Fast write does not support writing dynamically-sized arrays.\r\n */\r\n private fastWrite<Buf extends DataView | Buffer>(\r\n buffer: Buf,\r\n dataOut: ToJson<T>,\r\n offsetPointer: { offset: number },\r\n writeFunctions: typeof SET_FUNCTION | typeof SET_FUNCTION_BUF\r\n ) {\r\n for (const [name, def] of this.entries) {\r\n if (Array.isArray(def)) {\r\n // Statically-sized array\r\n const itemType = def[0]\r\n const length = def[1]!\r\n const data = dataOut[name] as any[]\r\n\r\n if (typeof itemType === 'object') {\r\n for (let i = 0; i < length; ++i) {\r\n itemType.fastWrite(buffer, data[i] as ToJson<Definition>, offsetPointer, writeFunctions)\r\n }\r\n } else {\r\n const itemSize = BYTE_SIZE[itemType]\r\n\r\n for (let i = 0; i < length; ++i) {\r\n writeFunctions[itemType](buffer as any, data[i] as number, offsetPointer.offset)\r\n offsetPointer.offset += itemSize\r\n }\r\n }\r\n } else if (typeof def === 'object') {\r\n // Single \"subpacket\"\r\n // In fastWrite there cannot be arrays, but the cast is needed because TypeScript can't possibly know that.\r\n def.fastWrite(buffer, dataOut[name] as ToJson<Definition>, offsetPointer, writeFunctions)\r\n } else {\r\n // Single primitive (number)\r\n writeFunctions[def](buffer as any, dataOut[name] as number, offsetPointer.offset)\r\n offsetPointer.offset += BYTE_SIZE[def]\r\n }\r\n }\r\n }\r\n\r\n /**\r\n * The slow writing path tries writing data into the buffer as fast as the fast writing path does. \\\r\n * But, if a non-empty dynamically-sized array is encountered, the buffer needs to grow, slightly reducing performance.\r\n */\r\n private slowWrite<Buf extends DataView | Buffer>(\r\n buffer: Buf,\r\n dataOut: ToJson<T>,\r\n offsetPointer: { offset: number },\r\n byteLength: number,\r\n maxByteLength: number,\r\n writeFunctions: typeof SET_FUNCTION | typeof SET_FUNCTION_BUF,\r\n growBufferFunction: (buffer: Buf, newByteLength: number) => Buf\r\n ): Buf {\r\n for (const [name, def] of this.entries) {\r\n const data = dataOut[name]\r\n\r\n if (Array.isArray(def)) {\r\n // Could be both an array of just numbers or \"subpackets\"\r\n\r\n const length = (data as any[]).length\r\n const isDynamicArray = def[1] === undefined\r\n\r\n // Check if it is a dynamically-sized array, if it is, the length of the array must be serialized in the buffer before its elements\r\n // Explicitly check for undefined and not falsy values because it could be a statically-sized array of 0 elements.\r\n if (isDynamicArray) {\r\n writeFunctions[Field.UNSIGNED_INT_8](buffer as any, length, offsetPointer.offset)\r\n offsetPointer.offset += 1\r\n }\r\n\r\n if (length > 0) {\r\n const itemType = def[0]\r\n\r\n if (typeof itemType === 'object') {\r\n // Array of \"subpackets\"\r\n\r\n if (isDynamicArray) {\r\n const neededBytesForElements = length * itemType.minimumByteLength\r\n\r\n byteLength += neededBytesForElements\r\n maxByteLength += neededBytesForElements\r\n\r\n if (buffer.byteLength < maxByteLength) {\r\n buffer = growBufferFunction(buffer, maxByteLength)\r\n }\r\n }\r\n\r\n for (const object of data as unknown as ToJson<Definition>[]) {\r\n writeFunctions[Field.UNSIGNED_INT_8](\r\n buffer as any,\r\n itemType.packetId,\r\n offsetPointer.offset\r\n )\r\n\r\n offsetPointer.offset += 1\r\n\r\n buffer = itemType.slowWrite(\r\n buffer,\r\n object,\r\n offsetPointer,\r\n byteLength,\r\n maxByteLength,\r\n writeFunctions,\r\n growBufferFunction\r\n )\r\n\r\n byteLength = offsetPointer.offset\r\n maxByteLength = buffer.byteLength\r\n }\r\n } else {\r\n // Array of primitives (numbers)\r\n const itemSize = BYTE_SIZE[itemType]\r\n\r\n if (isDynamicArray) {\r\n const neededBytesForElements = length * itemSize\r\n\r\n byteLength += neededBytesForElements\r\n maxByteLength += neededBytesForElements\r\n\r\n if (buffer.byteLength < maxByteLength) {\r\n buffer = growBufferFunction(buffer, maxByteLength)\r\n }\r\n }\r\n\r\n // It seems like looping over each element is actually much faster than using TypedArrays bulk copy.\r\n // TODO: properly benchmark with various array sizes to see if it's actually the case.\r\n for (const number of data as number[]) {\r\n writeFunctions[itemType](buffer as any, number, offsetPointer.offset)\r\n offsetPointer.offset += itemSize\r\n }\r\n }\r\n }\r\n } else if (typeof def === 'object') {\r\n // Single \"subpacket\"\r\n writeFunctions[Field.UNSIGNED_INT_8](buffer as any, def.packetId, offsetPointer.offset)\r\n offsetPointer.offset += 1\r\n\r\n buffer = def.slowWrite(\r\n buffer,\r\n data as ToJson<Definition>,\r\n offsetPointer,\r\n byteLength,\r\n maxByteLength,\r\n writeFunctions,\r\n growBufferFunction\r\n )\r\n\r\n byteLength = offsetPointer.offset\r\n maxByteLength = buffer.byteLength\r\n } else {\r\n // Single primitive (number)\r\n writeFunctions[def](buffer as any, data as number, offsetPointer.offset)\r\n offsetPointer.offset += BYTE_SIZE[def]\r\n }\r\n }\r\n\r\n return buffer\r\n }\r\n}\r\n\r\n/**\r\n * BinaryPacket definition: \\\r\n * Any packet can be defined through a \"schema\" object explaining its fields names and types.\r\n *\r\n * @example\r\n * // Imagine we have a game board where each cell is a square and is one unit big.\r\n * // A cell can be then defined by its X and Y coordinates.\r\n * // For simplicity, let's say there cannot be more than 256 cells, so we can use 8 bits for each coordinate.\r\n * const Cell = {\r\n * x: Field.UNSIGNED_INT_8,\r\n * y: Field.UNSIGNED_INT_8\r\n * }\r\n *\r\n * // When done with the cell definition we can create its BinaryPacket writer/reader.\r\n * // NOTE: each BinaryPacket needs an unique ID, for identification purposes and error checking.\r\n * const CellPacket = BinaryPacket.define(0, Cell)\r\n *\r\n * // Let's now make the definition of the whole game board.\r\n * // You can also specify arrays of both \"primitive\" fields and other BinaryPackets.\r\n * const Board = {\r\n * numPlayers: Field.UNSIGNED_INT_8,\r\n * cells: FieldArray(CellPacket)\r\n * }\r\n *\r\n * // When done with the board definition we can create its BinaryPacket writer/reader.\r\n * // NOTE: each BinaryPacket needs an unique ID, for identification purposes and error checking.\r\n * const BoardPacket = BinaryPacket.define(1, Board)\r\n *\r\n * // And use it.\r\n * const buffer = BoardPacket.writeNodeBuffer({\r\n * numPlayers: 1,\r\n * cells: [\r\n * { x: 0, y: 0 },\r\n * { x: 1, y: 1 }\r\n * ]\r\n * })\r\n *\r\n * // sendTheBufferOver(buffer)\r\n * // ...\r\n * // const buffer = receiveTheBuffer()\r\n * const board = BoardPacket.readNodeBuffer(buffer)\r\n * // ...\r\n */\r\nexport type Definition = {\r\n [fieldName: string]: MaybeArray<Field> | MaybeArray<BinaryPacket<Definition>>\r\n}\r\n\r\ntype MaybeArray<T> = T | [itemType: T] | [itemType: T, length: number]\r\n\r\n/**\r\n * Meta-type that converts a `Definition` schema to the type of the actual JavaScript object that will be written into a packet or read from. \\\r\n */\r\ntype ToJson<T extends Definition> = {\r\n [K in keyof T]: T[K] extends [infer Item]\r\n ? Item extends BinaryPacket<infer BPDef>\r\n ? ToJson<BPDef>[]\r\n : number[]\r\n : T[K] extends [infer Item, infer Length]\r\n ? Item extends BinaryPacket<infer BPDef>\r\n ? ToJson<BPDef>[] & { length: Length }\r\n : number[] & { length: Length }\r\n : T[K] extends BinaryPacket<infer BPDef>\r\n ? ToJson<BPDef>\r\n : number\r\n}\r\n\r\n/**\r\n * In a JavaScript object, the order of its keys is not strictly defined: sort them by field name. \\\r\n * Thus, we cannot trust iterating over an object keys: we MUST iterate over its entries array. \\\r\n * This is important to make sure that whoever shares BinaryPacket definitions can correctly write/read packets independently of their JS engines.\r\n */\r\nfunction sortEntries(definition: Definition) {\r\n return Object.entries(definition).sort(([fieldName1], [fieldName2]) =>\r\n fieldName1.localeCompare(fieldName2)\r\n )\r\n}\r\n\r\ntype Entries = ReturnType<typeof sortEntries>\r\n\r\n/**\r\n * Helper function that \"inspects\" the entries of a BinaryPacket definition\r\n * and returns useful \"stats\" needed for writing and reading buffers.\r\n *\r\n * This function is ever called only once per BinaryPacket definition.\r\n */\r\nfunction inspectEntries(entries: Entries) {\r\n // The PacketID is already 1 byte, that's why we aren't starting from 0.\r\n let minimumByteLength = 1\r\n let canFastWrite = true\r\n\r\n for (const [, type] of entries) {\r\n if (Array.isArray(type)) {\r\n if (type.length === 2) {\r\n // Statically-sized array\r\n const itemSize =\r\n typeof type[0] === 'object' ? type[0].minimumByteLength : BYTE_SIZE[type[0]]\r\n\r\n minimumByteLength += type[1] * itemSize\r\n } else {\r\n // Dynamically-sized array\r\n // Adding 1 byte to serialize the array length\r\n minimumByteLength += 1\r\n canFastWrite = false\r\n }\r\n } else if (type instanceof BinaryPacket) {\r\n minimumByteLength += type.minimumByteLength\r\n canFastWrite &&= type.canFastWrite\r\n } else {\r\n minimumByteLength += BYTE_SIZE[type]\r\n }\r\n }\r\n\r\n return { minimumByteLength, canFastWrite }\r\n}\r\n\r\n//////////////////////////////////////////////\r\n// The logic here is practically over //\r\n// Here below there are needed constants //\r\n// that map a field-type to a functionality //\r\n//////////////////////////////////////////////\r\n\r\nconst BYTE_SIZE = Array(8) as number[]\r\n\r\nBYTE_SIZE[Field.UNSIGNED_INT_8] = 1\r\nBYTE_SIZE[Field.INT_8] = 1\r\n\r\nBYTE_SIZE[Field.UNSIGNED_INT_16] = 2\r\nBYTE_SIZE[Field.INT_16] = 2\r\n\r\nBYTE_SIZE[Field.UNSIGNED_INT_32] = 4\r\nBYTE_SIZE[Field.INT_32] = 4\r\nBYTE_SIZE[Field.FLOAT_32] = 4\r\n\r\nBYTE_SIZE[Field.FLOAT_64] = 8\r\n\r\nconst GET_FUNCTION = Array(8) as ((view: DataView, offset: number) => number)[]\r\n\r\nGET_FUNCTION[Field.UNSIGNED_INT_8] = (view, offset) => view.getUint8(offset)\r\nGET_FUNCTION[Field.INT_8] = (view, offset) => view.getInt8(offset)\r\n\r\nGET_FUNCTION[Field.UNSIGNED_INT_16] = (view, offset) => view.getUint16(offset)\r\nGET_FUNCTION[Field.INT_16] = (view, offset) => view.getInt16(offset)\r\n\r\nGET_FUNCTION[Field.UNSIGNED_INT_32] = (view, offset) => view.getUint32(offset)\r\nGET_FUNCTION[Field.INT_32] = (view, offset) => view.getInt32(offset)\r\nGET_FUNCTION[Field.FLOAT_32] = (view, offset) => view.getFloat32(offset)\r\n\r\nGET_FUNCTION[Field.FLOAT_64] = (view, offset) => view.getFloat64(offset)\r\n\r\nconst SET_FUNCTION = Array(8) as ((view: DataView, value: number, offset: number) => void)[]\r\n\r\nSET_FUNCTION[Field.UNSIGNED_INT_8] = (view, value, offset) => view.setUint8(offset, value)\r\nSET_FUNCTION[Field.INT_8] = (view, value, offset) => view.setInt8(offset, value)\r\n\r\nSET_FUNCTION[Field.UNSIGNED_INT_16] = (view, value, offset) => view.setUint16(offset, value)\r\nSET_FUNCTION[Field.INT_16] = (view, value, offset) => view.setInt16(offset, value)\r\n\r\nSET_FUNCTION[Field.UNSIGNED_INT_32] = (view, value, offset) => view.setUint32(offset, value)\r\nSET_FUNCTION[Field.INT_32] = (view, value, offset) => view.setInt32(offset, value)\r\nSET_FUNCTION[Field.FLOAT_32] = (view, value, offset) => view.setFloat32(offset, value)\r\n\r\nSET_FUNCTION[Field.FLOAT_64] = (view, value, offset) => view.setFloat64(offset, value)\r\n\r\nconst SET_FUNCTION_BUF = Array(8) as ((nodeBuffer: Buffer, value: number, offset: number) => void)[]\r\n\r\nif (hasNodeBuffers) {\r\n SET_FUNCTION_BUF[Field.UNSIGNED_INT_8] = (view, value, offset) => view.writeUint8(value, offset)\r\n SET_FUNCTION_BUF[Field.INT_8] = (view, value, offset) => view.writeInt8(value, offset)\r\n\r\n SET_FUNCTION_BUF[Field.UNSIGNED_INT_16] = (view, value, offset) =>\r\n view.writeUint16LE(value, offset)\r\n SET_FUNCTION_BUF[Field.INT_16] = (view, value, offset) => view.writeInt16LE(value, offset)\r\n\r\n SET_FUNCTION_BUF[Field.UNSIGNED_INT_32] = (view, value, offset) =>\r\n view.writeUint32LE(value, offset)\r\n SET_FUNCTION_BUF[Field.INT_32] = (view, value, offset) => view.writeInt32LE(value, offset)\r\n SET_FUNCTION_BUF[Field.FLOAT_32] = (view, value, offset) => view.writeFloatLE(value, offset)\r\n\r\n SET_FUNCTION_BUF[Field.FLOAT_64] = (view, value, offset) => view.writeDoubleLE(value, offset)\r\n}\r\n\r\nconst GET_FUNCTION_BUF = Array(8) as ((nodeBuffer: Buffer, offset: number) => number)[]\r\n\r\nif (hasNodeBuffers) {\r\n GET_FUNCTION_BUF[Field.UNSIGNED_INT_8] = (view, offset) => view.readUint8(offset)\r\n GET_FUNCTION_BUF[Field.INT_8] = (view, offset) => view.readInt8(offset)\r\n\r\n GET_FUNCTION_BUF[Field.UNSIGNED_INT_16] = (view, offset) => view.readUint16LE(offset)\r\n GET_FUNCTION_BUF[Field.INT_16] = (view, offset) => view.readInt16LE(offset)\r\n\r\n GET_FUNCTION_BUF[Field.UNSIGNED_INT_32] = (view, offset) => view.readUint32LE(offset)\r\n GET_FUNCTION_BUF[Field.INT_32] = (view, offset) => view.readInt32LE(offset)\r\n GET_FUNCTION_BUF[Field.FLOAT_32] = (view, offset) => view.readFloatLE(offset)\r\n\r\n GET_FUNCTION_BUF[Field.FLOAT_64] = (view, offset) => view.readDoubleLE(offset)\r\n}\r\n","export const hasNodeBuffers = typeof Buffer === 'function'\r\n\r\nexport function growDataView(dataview: DataView, newByteLength: number) {\r\n const resizedBuffer = new ArrayBuffer(newByteLength)\r\n const amountToCopy = Math.min(dataview.byteLength, resizedBuffer.byteLength)\r\n\r\n // Treat the buffer as if it was a Float64Array so we can copy 8 bytes at a time, to finish faster\r\n let length = Math.trunc(amountToCopy / 8)\r\n new Float64Array(resizedBuffer, 0, length).set(new Float64Array(dataview.buffer, 0, length))\r\n\r\n // Copy the remaining up to 7 bytes\r\n const offset = length * 8\r\n length = amountToCopy - offset\r\n new Uint8Array(resizedBuffer, offset, length).set(new Uint8Array(dataview.buffer, offset, length))\r\n\r\n return new DataView(resizedBuffer)\r\n}\r\n\r\nexport function growNodeBuffer(buffer: Buffer, newByteLength: number) {\r\n const newBuffer = Buffer.allocUnsafe(newByteLength)\r\n buffer.copy(newBuffer)\r\n return newBuffer\r\n}\r\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAO,IAAM,iBAAiB,OAAO,WAAW;AAEzC,SAAS,aAAa,UAAoB,eAAuB;AACtE,QAAM,gBAAgB,IAAI,YAAY,aAAa;AACnD,QAAM,eAAe,KAAK,IAAI,SAAS,YAAY,cAAc,UAAU;AAG3E,MAAI,SAAS,KAAK,MAAM,eAAe,CAAC;AACxC,MAAI,aAAa,eAAe,GAAG,MAAM,EAAE,IAAI,IAAI,aAAa,SAAS,QAAQ,GAAG,MAAM,CAAC;AAG3F,QAAM,SAAS,SAAS;AACxB,WAAS,eAAe;AACxB,MAAI,WAAW,eAAe,QAAQ,MAAM,EAAE,IAAI,IAAI,WAAW,SAAS,QAAQ,QAAQ,MAAM,CAAC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MAAM,WAAW,KAAK,UAAU,MAAM;AAChF,mBAAiB,aAAW,IAAI,CAAC,MAAM,WAAW,KAAK,SAAS,MAAM;AAEtE,mBAAiB,uBAAqB,IAAI,CAAC,MAAM,WAAW,KAAK,aAAa,MAAM;AACpF,mBAAiB,cAAY,IAAI,CAAC,MAAM,WAAW,KAAK,YAAY,MAAM;AAE1E,mBAAiB,uBAAqB,IAAI,CAAC,MAAM,WAAW,KAAK,aAAa,MAAM;AACpF,mBAAiB,cAAY,IAAI,CAAC,MAAM,WAAW,KAAK,YAAY,MAAM;AAC1E,mBAAiB,gBAAc,IAAI,CAAC,MAAM,WAAW,KAAK,YAAY,MAAM;AAE5E,mBAAiB,gBAAc,IAAI,CAAC,MAAM,WAAW,KAAK,aAAa,MAAM;AAC/E;","names":["Field"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/buffers.ts"],"sourcesContent":["import { growDataView, growNodeBuffer, hasNodeBuffers } from './buffers'\r\n\r\nexport const enum Field {\r\n /**\r\n * Defines a 1 byte (8 bits) unsigned integer field. \\\r\n * (Range: 0 - 255)\r\n */\r\n UNSIGNED_INT_8 = 0,\r\n\r\n /**\r\n * Defines a 2 bytes (16 bits) unsigned integer field. \\\r\n * (Range: 0 - 65535)\r\n */\r\n UNSIGNED_INT_16,\r\n\r\n /**\r\n * Defines a 4 bytes (32 bits) unsigned integer field. \\\r\n * (Range: 0 - 4294967295)\r\n */\r\n UNSIGNED_INT_32,\r\n\r\n /**\r\n * Defines a 1 byte (8 bits) signed integer field. \\\r\n * (Range: -128 - 127)\r\n */\r\n INT_8,\r\n\r\n /**\r\n * Defines a 2 bytes (16 bits) signed integer field. \\\r\n * (Range: -32768 - 32767)\r\n */\r\n INT_16,\r\n\r\n /**\r\n * Defines a 4 bytes (32 bits) signed integer field. \\\r\n * (Range: -2147483648 - 2147483647)\r\n */\r\n INT_32,\r\n\r\n /**\r\n * Defines a 4 bytes (32 bits) floating-point field. \\\r\n */\r\n FLOAT_32,\r\n\r\n /**\r\n * Defines a 8 bytes (64 bits) floating-point field. \\\r\n */\r\n FLOAT_64\r\n}\r\n\r\n/**\r\n * Defines a dynamically-sized array with elements of a certain type. \\\r\n * Dynamically-sized arrays are useful when a packet's field is an array of a non pre-defined length. \\\r\n * Although, this makes dynamically-sized arrays more memory expensive as the internal buffer needs to be grown accordingly.\r\n *\r\n * NOTE: If an array will ALWAYS have the same length, prefer using the `FieldFixedArray` type, for both better performance and memory efficiency. \\\r\n * NOTE: As of now, dynamic arrays can have at most 256 elements.\r\n */\r\nexport function FieldArray<T extends Field | BinaryPacket<Definition>>(item: T): [itemType: T] {\r\n return [item]\r\n}\r\n\r\n/**\r\n * Defines a statically-sized array with elements of a certain type. \\\r\n * Fixed arrays are useful when a packet's field is an array of a pre-defined length. \\\r\n * Fixed arrays much more memory efficient and performant than non-fixed ones.\r\n *\r\n * NOTE: If an array will not always have the same length, use the `FieldArray` type.\r\n */\r\nexport function FieldFixedArray<T extends Field | BinaryPacket<Definition>, Length extends number>(\r\n item: T,\r\n length: Length\r\n): [itemType: T, length: Length] {\r\n if (length < 0 || !Number.isFinite(length)) {\r\n throw new RangeError('Length of a FixedArray must be a positive integer.')\r\n }\r\n\r\n return [item, length]\r\n}\r\n\r\nexport class BinaryPacket<T extends Definition> {\r\n /**\r\n * Defines a new binary packet. \\\r\n * Make sure that every `packetId` is unique.\r\n * @throws RangeError If packetId is negative, floating-point, or greater than 255.\r\n */\r\n static define<T extends Definition>(packetId: number, definition?: T) {\r\n if (packetId < 0 || !Number.isFinite(packetId)) {\r\n throw new RangeError('Packet IDs must be positive integers.')\r\n }\r\n\r\n if (packetId > 255) {\r\n throw new RangeError(\r\n 'Packet IDs greater than 255 are not supported. Do you REALLY need more than 255 different kinds of packets?'\r\n )\r\n }\r\n\r\n return new BinaryPacket(packetId, definition)\r\n }\r\n\r\n /**\r\n * Reads just the packetId from the given Buffer. \\\r\n * This method practically just reads the uint8 at offset `byteOffset` (default: 0). \\\r\n * Useful if the receiving side receives multiple types of packets.\r\n */\r\n static readPacketIdNodeBuffer(buffer: Buffer, byteOffset = 0) {\r\n return buffer.readUint8(byteOffset)\r\n }\r\n\r\n /**\r\n * Reads just the packetId from the given DataView. \\\r\n * This method practically just reads the uint8 at offset `byteOffset` (default: 0). \\\r\n * Useful if the receiving side receives multiple types of packets.\r\n */\r\n static readPacketIdDataView(dataview: DataView, byteOffset = 0) {\r\n return dataview.getUint8(byteOffset)\r\n }\r\n\r\n /**\r\n * Reads just the packetId from the given ArrayBuffer. \\\r\n * This method practically just reads the uint8 at offset `byteOffset`. \\\r\n * Useful if the receiving side receives multiple types of packets.\r\n *\r\n * NOTE: Due to security issues, the `byteOffset` argument cannot be defaulted and must be provided by the user. \\\r\n * NOTE: For more information read the `readArrayBuffer` method documentation.\r\n */\r\n static readPacketIdArrayBuffer(arraybuffer: ArrayBuffer, byteOffset: number) {\r\n return new Uint8Array(arraybuffer, byteOffset, 1)[0]\r\n }\r\n\r\n /**\r\n * Reads/deserializes from the given Buffer. \\\r\n * Method available ONLY on NodeJS and Bun.\r\n *\r\n * If possible, always prefer reading using this method, as it is much faster than the other ones.\r\n *\r\n * NOTE: if you have an ArrayBuffer do not bother wrapping it into a node Buffer yourself. \\\r\n * NOTE: if you have an ArrayBuffer use the appropriate `readArrayBuffer`.\r\n */\r\n readNodeBuffer(\r\n dataIn: Buffer,\r\n offsetPointer = { offset: 0 },\r\n byteLength = dataIn.byteLength\r\n ): ToJson<T> {\r\n return this.read(dataIn, offsetPointer, byteLength, GET_FUNCTION_BUF)\r\n }\r\n\r\n /**\r\n * Reads/deserializes from the given DataView.\r\n *\r\n * NOTE: if you have an ArrayBuffer do not bother wrapping it into a DataView yourself. \\\r\n * NOTE: if you have an ArrayBuffer use the appropriate `readArrayBuffer`.\r\n */\r\n readDataView(\r\n dataIn: DataView,\r\n offsetPointer = { offset: 0 },\r\n byteLength = dataIn.byteLength\r\n ): ToJson<T> {\r\n return this.read(dataIn, offsetPointer, byteLength, GET_FUNCTION)\r\n }\r\n\r\n /**\r\n * Reads/deserializes from the given ArrayBuffer. \\\r\n * WARNING: this method is practically a HACK.\r\n *\r\n * When using this method both the `byteOffset` and `byteLength` are REQUIRED and cannot be defaulted. \\\r\n * This is to prevent serious bugs and security issues. \\\r\n * That is because often raw ArrayBuffers come from a pre-allocated buffer pool and do not start at byteOffset 0.\r\n *\r\n * NOTE: if you have a node Buffer do not bother wrapping it into an ArrayBuffer yourself. \\\r\n * NOTE: if you have a node Buffer use the appropriate `readNodeBuffer` as it is much faster and less error prone.\r\n */\r\n readArrayBuffer(\r\n dataIn: ArrayBuffer & { buffer?: undefined },\r\n byteOffset: number,\r\n byteLength: number\r\n ) {\r\n return this.read(\r\n hasNodeBuffers\r\n ? Buffer.from(dataIn, byteOffset, byteLength)\r\n : new DataView(dataIn, byteOffset, byteLength),\r\n { offset: 0 }, // The underlying buffer has already been offsetted\r\n byteLength,\r\n hasNodeBuffers ? GET_FUNCTION_BUF : GET_FUNCTION\r\n )\r\n }\r\n\r\n /**\r\n * Writes/serializes the given object into a Buffer. \\\r\n * Method available ONLY on NodeJS and Bun.\r\n *\r\n * If possible, always prefer writing using this method, as it is much faster than the other ones.\r\n */\r\n writeNodeBuffer(dataOut: ToJson<T>) {\r\n const buffer = Buffer.allocUnsafe(this.minimumByteLength)\r\n return this.write(buffer, dataOut, { offset: 0 }, SET_FUNCTION_BUF, growNodeBuffer)\r\n }\r\n\r\n /**\r\n * Writes/serializes the given object into a DataView. \\\r\n */\r\n writeDataView(dataOut: ToJson<T>) {\r\n const dataview = new DataView(new ArrayBuffer(this.minimumByteLength))\r\n return this.write(dataview, dataOut, { offset: 0 }, SET_FUNCTION, growDataView)\r\n }\r\n\r\n /**\r\n * Writes/serializes the given object into an ArrayBuffer. \\\r\n * This method is just a wrapper around either `writeNodeBuffer` or `writeDataView`. \\\r\n *\r\n * This method works with JavaScript standard raw ArrayBuffer(s) and, as such, is very error prone: \\\r\n * Make sure you're using the returned byteLength and byteOffset fields in the read counterpart. \\\r\n *\r\n * Always consider whether is possible to use directly `writeNodeBuffer` or `writeDataView` instead of `writeArrayBuffer`. \\\r\n * For more information read the `readArrayBuffer` documentation.\r\n */\r\n writeArrayBuffer(dataOut: ToJson<T>) {\r\n const buf = hasNodeBuffers ? this.writeNodeBuffer(dataOut) : this.writeDataView(dataOut)\r\n return { buffer: buf.buffer, byteLength: buf.byteLength, byteOffset: buf.byteOffset }\r\n }\r\n\r\n private readonly entries: Entries\r\n readonly canFastWrite: boolean\r\n readonly minimumByteLength: number\r\n\r\n private constructor(\r\n private readonly packetId: number,\r\n definition?: T\r\n ) {\r\n this.entries = definition ? sortEntries(definition) : []\r\n const inspection = inspectEntries(this.entries)\r\n\r\n this.minimumByteLength = inspection.minimumByteLength\r\n this.canFastWrite = inspection.canFastWrite\r\n }\r\n\r\n private read(\r\n dataIn: DataView | Buffer,\r\n offsetPointer: { offset: number },\r\n byteLength: number,\r\n readFunctions: typeof GET_FUNCTION | typeof GET_FUNCTION_BUF\r\n ): ToJson<T> {\r\n if (byteLength + offsetPointer.offset < this.minimumByteLength) {\r\n throw new Error(\r\n `There is no space available to fit a packet of type ${this.packetId} at offset ${offsetPointer.offset}`\r\n )\r\n }\r\n\r\n if (\r\n readFunctions[Field.UNSIGNED_INT_8](dataIn as any, offsetPointer.offset) !== this.packetId\r\n ) {\r\n throw new Error(\r\n `Data at offset ${offsetPointer.offset} is not a packet of type ${this.packetId}`\r\n )\r\n }\r\n\r\n offsetPointer.offset += 1\r\n const result: any = {}\r\n\r\n for (const [name, def] of this.entries) {\r\n if (Array.isArray(def)) {\r\n const length =\r\n // def[1] is the length of a statically-sized array, if undefined: must read the length from the buffer as it means it's a dynamically-sized array\r\n def[1] ?? readFunctions[Field.UNSIGNED_INT_8](dataIn as any, offsetPointer.offset++)\r\n\r\n const array = Array(length)\r\n\r\n const itemType = def[0]\r\n\r\n if (typeof itemType === 'object') {\r\n // Array of \"subpackets\"\r\n for (let i = 0; i < length; ++i) {\r\n array[i] = itemType.read(dataIn, offsetPointer, byteLength, readFunctions)\r\n }\r\n } else {\r\n // Array of primitives (numbers)\r\n const itemSize = BYTE_SIZE[itemType]\r\n\r\n // It seems like looping over each element is actually much faster than using TypedArrays bulk copy.\r\n // TODO: properly benchmark with various array sizes to see if it's actually the case.\r\n for (let i = 0; i < length; ++i) {\r\n array[i] = readFunctions[itemType](dataIn as any, offsetPointer.offset)\r\n offsetPointer.offset += itemSize\r\n }\r\n }\r\n\r\n // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access\r\n result[name] = array\r\n } else if (typeof def === 'object') {\r\n // Single \"subpacket\"\r\n // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access\r\n result[name] = def.read(dataIn, offsetPointer, byteLength, readFunctions)\r\n } else {\r\n // Single primitive (number)\r\n // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access\r\n result[name] = readFunctions[def](dataIn as any, offsetPointer.offset)\r\n offsetPointer.offset += BYTE_SIZE[def]\r\n }\r\n }\r\n\r\n return result as ToJson<T>\r\n }\r\n\r\n private write<Buf extends DataView | Buffer>(\r\n buffer: Buf,\r\n dataOut: ToJson<T>,\r\n offsetPointer: { offset: number },\r\n writeFunctions: typeof SET_FUNCTION | typeof SET_FUNCTION_BUF,\r\n growBufferFunction: (buffer: Buf, newByteLength: number) => Buf\r\n ): Buf {\r\n writeFunctions[Field.UNSIGNED_INT_8](buffer as any, this.packetId, offsetPointer.offset)\r\n offsetPointer.offset += 1\r\n\r\n if (this.canFastWrite) {\r\n // If there are no arrays, the minimumByteLength always equals to the full needed byteLength.\r\n // So we can take the fast path, since we know beforehand that the buffer isn't going to grow.\r\n this.fastWrite(buffer, dataOut, offsetPointer, writeFunctions)\r\n return buffer\r\n } else {\r\n // If non-empty arrays are encountered, the buffer must grow.\r\n // If every array is empty, the speed of this path is comparable to the fast path.\r\n return this.slowWrite(\r\n buffer,\r\n dataOut,\r\n offsetPointer,\r\n this.minimumByteLength,\r\n this.minimumByteLength,\r\n writeFunctions,\r\n growBufferFunction\r\n )\r\n }\r\n }\r\n\r\n /**\r\n * Fast write does not support writing dynamically-sized arrays.\r\n */\r\n private fastWrite<Buf extends DataView | Buffer>(\r\n buffer: Buf,\r\n dataOut: ToJson<T>,\r\n offsetPointer: { offset: number },\r\n writeFunctions: typeof SET_FUNCTION | typeof SET_FUNCTION_BUF\r\n ) {\r\n for (const [name, def] of this.entries) {\r\n if (Array.isArray(def)) {\r\n // Statically-sized array\r\n const itemType = def[0]\r\n const length = def[1]!\r\n const data = dataOut[name] as any[]\r\n\r\n if (typeof itemType === 'object') {\r\n for (let i = 0; i < length; ++i) {\r\n itemType.fastWrite(buffer, data[i] as ToJson<Definition>, offsetPointer, writeFunctions)\r\n }\r\n } else {\r\n const itemSize = BYTE_SIZE[itemType]\r\n\r\n for (let i = 0; i < length; ++i) {\r\n writeFunctions[itemType](buffer as any, data[i] as number, offsetPointer.offset)\r\n offsetPointer.offset += itemSize\r\n }\r\n }\r\n } else if (typeof def === 'object') {\r\n // Single \"subpacket\"\r\n // In fastWrite there cannot be arrays, but the cast is needed because TypeScript can't possibly know that.\r\n def.fastWrite(buffer, dataOut[name] as ToJson<Definition>, offsetPointer, writeFunctions)\r\n } else {\r\n // Single primitive (number)\r\n writeFunctions[def](buffer as any, dataOut[name] as number, offsetPointer.offset)\r\n offsetPointer.offset += BYTE_SIZE[def]\r\n }\r\n }\r\n }\r\n\r\n /**\r\n * The slow writing path tries writing data into the buffer as fast as the fast writing path does. \\\r\n * But, if a non-empty dynamically-sized array is encountered, the buffer needs to grow, slightly reducing performance.\r\n */\r\n private slowWrite<Buf extends DataView | Buffer>(\r\n buffer: Buf,\r\n dataOut: ToJson<T>,\r\n offsetPointer: { offset: number },\r\n byteLength: number,\r\n maxByteLength: number,\r\n writeFunctions: typeof SET_FUNCTION | typeof SET_FUNCTION_BUF,\r\n growBufferFunction: (buffer: Buf, newByteLength: number) => Buf\r\n ): Buf {\r\n for (const [name, def] of this.entries) {\r\n const data = dataOut[name]\r\n\r\n if (Array.isArray(def)) {\r\n // Could be both an array of just numbers or \"subpackets\"\r\n\r\n const length = (data as any[]).length\r\n const isDynamicArray = def[1] === undefined\r\n\r\n // Check if it is a dynamically-sized array, if it is, the length of the array must be serialized in the buffer before its elements\r\n // Explicitly check for undefined and not falsy values because it could be a statically-sized array of 0 elements.\r\n if (isDynamicArray) {\r\n writeFunctions[Field.UNSIGNED_INT_8](buffer as any, length, offsetPointer.offset)\r\n offsetPointer.offset += 1\r\n }\r\n\r\n if (length > 0) {\r\n const itemType = def[0]\r\n\r\n if (typeof itemType === 'object') {\r\n // Array of \"subpackets\"\r\n\r\n if (isDynamicArray) {\r\n const neededBytesForElements = length * itemType.minimumByteLength\r\n\r\n byteLength += neededBytesForElements\r\n maxByteLength += neededBytesForElements\r\n\r\n if (buffer.byteLength < maxByteLength) {\r\n buffer = growBufferFunction(buffer, maxByteLength)\r\n }\r\n }\r\n\r\n for (const object of data as unknown as ToJson<Definition>[]) {\r\n writeFunctions[Field.UNSIGNED_INT_8](\r\n buffer as any,\r\n itemType.packetId,\r\n offsetPointer.offset\r\n )\r\n\r\n offsetPointer.offset += 1\r\n\r\n buffer = itemType.slowWrite(\r\n buffer,\r\n object,\r\n offsetPointer,\r\n byteLength,\r\n maxByteLength,\r\n writeFunctions,\r\n growBufferFunction\r\n )\r\n\r\n byteLength = offsetPointer.offset\r\n maxByteLength = buffer.byteLength\r\n }\r\n } else {\r\n // Array of primitives (numbers)\r\n const itemSize = BYTE_SIZE[itemType]\r\n\r\n if (isDynamicArray) {\r\n const neededBytesForElements = length * itemSize\r\n\r\n byteLength += neededBytesForElements\r\n maxByteLength += neededBytesForElements\r\n\r\n if (buffer.byteLength < maxByteLength) {\r\n buffer = growBufferFunction(buffer, maxByteLength)\r\n }\r\n }\r\n\r\n // It seems like looping over each element is actually much faster than using TypedArrays bulk copy.\r\n // TODO: properly benchmark with various array sizes to see if it's actually the case.\r\n for (const number of data as number[]) {\r\n writeFunctions[itemType](buffer as any, number, offsetPointer.offset)\r\n offsetPointer.offset += itemSize\r\n }\r\n }\r\n }\r\n } else if (typeof def === 'object') {\r\n // Single \"subpacket\"\r\n writeFunctions[Field.UNSIGNED_INT_8](buffer as any, def.packetId, offsetPointer.offset)\r\n offsetPointer.offset += 1\r\n\r\n buffer = def.slowWrite(\r\n buffer,\r\n data as ToJson<Definition>,\r\n offsetPointer,\r\n byteLength,\r\n maxByteLength,\r\n writeFunctions,\r\n growBufferFunction\r\n )\r\n\r\n byteLength = offsetPointer.offset\r\n maxByteLength = buffer.byteLength\r\n } else {\r\n // Single primitive (number)\r\n writeFunctions[def](buffer as any, data as number, offsetPointer.offset)\r\n offsetPointer.offset += BYTE_SIZE[def]\r\n }\r\n }\r\n\r\n return buffer\r\n }\r\n}\r\n\r\n/**\r\n * BinaryPacket definition: \\\r\n * Any packet can be defined through a \"schema\" object explaining its fields names and types.\r\n *\r\n * @example\r\n * // Imagine we have a game board where each cell is a square and is one unit big.\r\n * // A cell can be then defined by its X and Y coordinates.\r\n * // For simplicity, let's say there cannot be more than 256 cells, so we can use 8 bits for each coordinate.\r\n * const Cell = {\r\n * x: Field.UNSIGNED_INT_8,\r\n * y: Field.UNSIGNED_INT_8\r\n * }\r\n *\r\n * // When done with the cell definition we can create its BinaryPacket writer/reader.\r\n * // NOTE: each BinaryPacket needs an unique ID, for identification purposes and error checking.\r\n * const CellPacket = BinaryPacket.define(0, Cell)\r\n *\r\n * // Let's now make the definition of the whole game board.\r\n * // You can also specify arrays of both \"primitive\" fields and other BinaryPackets.\r\n * const Board = {\r\n * numPlayers: Field.UNSIGNED_INT_8,\r\n * cells: FieldArray(CellPacket)\r\n * }\r\n *\r\n * // When done with the board definition we can create its BinaryPacket writer/reader.\r\n * // NOTE: each BinaryPacket needs an unique ID, for identification purposes and error checking.\r\n * const BoardPacket = BinaryPacket.define(1, Board)\r\n *\r\n * // And use it.\r\n * const buffer = BoardPacket.writeNodeBuffer({\r\n * numPlayers: 1,\r\n * cells: [\r\n * { x: 0, y: 0 },\r\n * { x: 1, y: 1 }\r\n * ]\r\n * })\r\n *\r\n * // sendTheBufferOver(buffer)\r\n * // ...\r\n * // const buffer = receiveTheBuffer()\r\n * const board = BoardPacket.readNodeBuffer(buffer)\r\n * // ...\r\n */\r\nexport type Definition = {\r\n [fieldName: string]: MaybeArray<Field> | MaybeArray<BinaryPacket<Definition>>\r\n}\r\n\r\ntype MaybeArray<T> = T | [itemType: T] | [itemType: T, length: number]\r\n\r\n/**\r\n * Meta-type that converts a `Definition` schema to the type of the actual JavaScript object that will be written into a packet or read from. \\\r\n */\r\ntype ToJson<T extends Definition> = {\r\n [K in keyof T]: T[K] extends [infer Item]\r\n ? Item extends BinaryPacket<infer BPDef>\r\n ? ToJson<BPDef>[]\r\n : number[]\r\n : T[K] extends [infer Item, infer Length]\r\n ? Item extends BinaryPacket<infer BPDef>\r\n ? ToJson<BPDef>[] & { length: Length }\r\n : number[] & { length: Length }\r\n : T[K] extends BinaryPacket<infer BPDef>\r\n ? ToJson<BPDef>\r\n : number\r\n}\r\n\r\n/**\r\n * In a JavaScript object, the order of its keys is not strictly defined: sort them by field name. \\\r\n * Thus, we cannot trust iterating over an object keys: we MUST iterate over its entries array. \\\r\n * This is important to make sure that whoever shares BinaryPacket definitions can correctly write/read packets independently of their JS engines.\r\n */\r\nfunction sortEntries(definition: Definition) {\r\n return Object.entries(definition).sort(([fieldName1], [fieldName2]) =>\r\n fieldName1.localeCompare(fieldName2)\r\n )\r\n}\r\n\r\ntype Entries = ReturnType<typeof sortEntries>\r\n\r\n/**\r\n * Helper function that \"inspects\" the entries of a BinaryPacket definition\r\n * and returns useful \"stats\" needed for writing and reading buffers.\r\n *\r\n * This function is ever called only once per BinaryPacket definition.\r\n */\r\nfunction inspectEntries(entries: Entries) {\r\n // The PacketID is already 1 byte, that's why we aren't starting from 0.\r\n let minimumByteLength = 1\r\n let canFastWrite = true\r\n\r\n for (const [, type] of entries) {\r\n if (Array.isArray(type)) {\r\n if (type.length === 2) {\r\n // Statically-sized array\r\n const itemSize =\r\n typeof type[0] === 'object' ? type[0].minimumByteLength : BYTE_SIZE[type[0]]\r\n\r\n minimumByteLength += type[1] * itemSize\r\n } else {\r\n // Dynamically-sized array\r\n // Adding 1 byte to serialize the array length\r\n minimumByteLength += 1\r\n canFastWrite = false\r\n }\r\n } else if (type instanceof BinaryPacket) {\r\n minimumByteLength += type.minimumByteLength\r\n canFastWrite &&= type.canFastWrite\r\n } else {\r\n minimumByteLength += BYTE_SIZE[type]\r\n }\r\n }\r\n\r\n return { minimumByteLength, canFastWrite }\r\n}\r\n\r\n//////////////////////////////////////////////\r\n// The logic here is practically over //\r\n// Here below there are needed constants //\r\n// that map a field-type to a functionality //\r\n//////////////////////////////////////////////\r\n\r\nconst BYTE_SIZE = Array(8) as number[]\r\n\r\nBYTE_SIZE[Field.UNSIGNED_INT_8] = 1\r\nBYTE_SIZE[Field.INT_8] = 1\r\n\r\nBYTE_SIZE[Field.UNSIGNED_INT_16] = 2\r\nBYTE_SIZE[Field.INT_16] = 2\r\n\r\nBYTE_SIZE[Field.UNSIGNED_INT_32] = 4\r\nBYTE_SIZE[Field.INT_32] = 4\r\nBYTE_SIZE[Field.FLOAT_32] = 4\r\n\r\nBYTE_SIZE[Field.FLOAT_64] = 8\r\n\r\nconst GET_FUNCTION = Array(8) as ((view: DataView, offset: number) => number)[]\r\n\r\nGET_FUNCTION[Field.UNSIGNED_INT_8] = (view, offset) => view.getUint8(offset)\r\nGET_FUNCTION[Field.INT_8] = (view, offset) => view.getInt8(offset)\r\n\r\nGET_FUNCTION[Field.UNSIGNED_INT_16] = (view, offset) => view.getUint16(offset)\r\nGET_FUNCTION[Field.INT_16] = (view, offset) => view.getInt16(offset)\r\n\r\nGET_FUNCTION[Field.UNSIGNED_INT_32] = (view, offset) => view.getUint32(offset)\r\nGET_FUNCTION[Field.INT_32] = (view, offset) => view.getInt32(offset)\r\nGET_FUNCTION[Field.FLOAT_32] = (view, offset) => view.getFloat32(offset)\r\n\r\nGET_FUNCTION[Field.FLOAT_64] = (view, offset) => view.getFloat64(offset)\r\n\r\nconst SET_FUNCTION = Array(8) as ((view: DataView, value: number, offset: number) => void)[]\r\n\r\nSET_FUNCTION[Field.UNSIGNED_INT_8] = (view, value, offset) => view.setUint8(offset, value)\r\nSET_FUNCTION[Field.INT_8] = (view, value, offset) => view.setInt8(offset, value)\r\n\r\nSET_FUNCTION[Field.UNSIGNED_INT_16] = (view, value, offset) => view.setUint16(offset, value)\r\nSET_FUNCTION[Field.INT_16] = (view, value, offset) => view.setInt16(offset, value)\r\n\r\nSET_FUNCTION[Field.UNSIGNED_INT_32] = (view, value, offset) => view.setUint32(offset, value)\r\nSET_FUNCTION[Field.INT_32] = (view, value, offset) => view.setInt32(offset, value)\r\nSET_FUNCTION[Field.FLOAT_32] = (view, value, offset) => view.setFloat32(offset, value)\r\n\r\nSET_FUNCTION[Field.FLOAT_64] = (view, value, offset) => view.setFloat64(offset, value)\r\n\r\nconst SET_FUNCTION_BUF = Array(8) as ((nodeBuffer: Buffer, value: number, offset: number) => void)[]\r\n\r\nif (hasNodeBuffers) {\r\n SET_FUNCTION_BUF[Field.UNSIGNED_INT_8] = (view, value, offset) => view.writeUint8(value, offset)\r\n SET_FUNCTION_BUF[Field.INT_8] = (view, value, offset) => view.writeInt8(value, offset)\r\n\r\n SET_FUNCTION_BUF[Field.UNSIGNED_INT_16] = (view, value, offset) =>\r\n view.writeUint16LE(value, offset)\r\n SET_FUNCTION_BUF[Field.INT_16] = (view, value, offset) => view.writeInt16LE(value, offset)\r\n\r\n SET_FUNCTION_BUF[Field.UNSIGNED_INT_32] = (view, value, offset) =>\r\n view.writeUint32LE(value, offset)\r\n SET_FUNCTION_BUF[Field.INT_32] = (view, value, offset) => view.writeInt32LE(value, offset)\r\n SET_FUNCTION_BUF[Field.FLOAT_32] = (view, value, offset) => view.writeFloatLE(value, offset)\r\n\r\n SET_FUNCTION_BUF[Field.FLOAT_64] = (view, value, offset) => view.writeDoubleLE(value, offset)\r\n}\r\n\r\nconst GET_FUNCTION_BUF = Array(8) as ((nodeBuffer: Buffer, offset: number) => number)[]\r\n\r\nif (hasNodeBuffers) {\r\n GET_FUNCTION_BUF[Field.UNSIGNED_INT_8] = (view, offset) => view.readUint8(offset)\r\n GET_FUNCTION_BUF[Field.INT_8] = (view, offset) => view.readInt8(offset)\r\n\r\n GET_FUNCTION_BUF[Field.UNSIGNED_INT_16] = (view, offset) => view.readUint16LE(offset)\r\n GET_FUNCTION_BUF[Field.INT_16] = (view, offset) => view.readInt16LE(offset)\r\n\r\n GET_FUNCTION_BUF[Field.UNSIGNED_INT_32] = (view, offset) => view.readUint32LE(offset)\r\n GET_FUNCTION_BUF[Field.INT_32] = (view, offset) => view.readInt32LE(offset)\r\n GET_FUNCTION_BUF[Field.FLOAT_32] = (view, offset) => view.readFloatLE(offset)\r\n\r\n GET_FUNCTION_BUF[Field.FLOAT_64] = (view, offset) => view.readDoubleLE(offset)\r\n}\r\n","export const hasNodeBuffers = typeof Buffer === 'function'\r\n\r\nexport function growDataView(dataview: DataView, newByteLength: number) {\r\n const resizedBuffer = new ArrayBuffer(newByteLength)\r\n const amountToCopy = Math.min(dataview.byteLength, resizedBuffer.byteLength)\r\n\r\n // Treat the buffer as if it was a Float64Array so we can copy 8 bytes at a time, to finish faster\r\n let length = Math.trunc(amountToCopy / 8)\r\n new Float64Array(resizedBuffer, 0, length).set(new Float64Array(dataview.buffer, 0, length))\r\n\r\n // Copy the remaining up to 7 bytes\r\n const offset = length * 8\r\n length = amountToCopy - offset\r\n new Uint8Array(resizedBuffer, offset, length).set(new Uint8Array(dataview.buffer, offset, length))\r\n\r\n return new DataView(resizedBuffer)\r\n}\r\n\r\nexport function growNodeBuffer(buffer: Buffer, newByteLength: number) {\r\n const newBuffer = Buffer.allocUnsafe(newByteLength)\r\n buffer.copy(newBuffer)\r\n return newBuffer\r\n}\r\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD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Packet","Field","FieldArray","FieldFixedArray","__toCommonJS","hasNodeBuffers","growDataView","dataview","newByteLength","resizedBuffer","amountToCopy","length","offset","growNodeBuffer","buffer","newBuffer","Field","FieldArray","item","FieldFixedArray","length","BinaryPacket","_BinaryPacket","packetId","definition","sortEntries","inspection","inspectEntries","buffer","byteOffset","dataview","arraybuffer","dataIn","offsetPointer","byteLength","GET_FUNCTION_BUF","GET_FUNCTION","hasNodeBuffers","dataOut","SET_FUNCTION_BUF","growNodeBuffer","SET_FUNCTION","growDataView","buf","readFunctions","result","name","def","array","itemType","i","itemSize","BYTE_SIZE","writeFunctions","growBufferFunction","data","maxByteLength","isDynamicArray","neededBytesForElements","object","number","fieldName1","fieldName2","entries","minimumByteLength","canFastWrite","type","view","offset","value"]}
|
package/dist/index.mjs
CHANGED
|
@@ -1,417 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
var hasNodeBuffers = typeof Buffer === "function";
|
|
3
|
-
function growDataView(dataview, newByteLength) {
|
|
4
|
-
const resizedBuffer = new ArrayBuffer(newByteLength);
|
|
5
|
-
const amountToCopy = Math.min(dataview.byteLength, resizedBuffer.byteLength);
|
|
6
|
-
let length = Math.trunc(amountToCopy / 8);
|
|
7
|
-
new Float64Array(resizedBuffer, 0, length).set(new Float64Array(dataview.buffer, 0, length));
|
|
8
|
-
const offset = length * 8;
|
|
9
|
-
length = amountToCopy - offset;
|
|
10
|
-
new Uint8Array(resizedBuffer, offset, length).set(new Uint8Array(dataview.buffer, offset, length));
|
|
11
|
-
return new DataView(resizedBuffer);
|
|
12
|
-
}
|
|
13
|
-
function growNodeBuffer(buffer, newByteLength) {
|
|
14
|
-
const newBuffer = Buffer.allocUnsafe(newByteLength);
|
|
15
|
-
buffer.copy(newBuffer);
|
|
16
|
-
return newBuffer;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
// src/index.ts
|
|
20
|
-
var Field = /* @__PURE__ */ ((Field2) => {
|
|
21
|
-
Field2[Field2["UNSIGNED_INT_8"] = 0] = "UNSIGNED_INT_8";
|
|
22
|
-
Field2[Field2["UNSIGNED_INT_16"] = 1] = "UNSIGNED_INT_16";
|
|
23
|
-
Field2[Field2["UNSIGNED_INT_32"] = 2] = "UNSIGNED_INT_32";
|
|
24
|
-
Field2[Field2["INT_8"] = 3] = "INT_8";
|
|
25
|
-
Field2[Field2["INT_16"] = 4] = "INT_16";
|
|
26
|
-
Field2[Field2["INT_32"] = 5] = "INT_32";
|
|
27
|
-
Field2[Field2["FLOAT_32"] = 6] = "FLOAT_32";
|
|
28
|
-
Field2[Field2["FLOAT_64"] = 7] = "FLOAT_64";
|
|
29
|
-
return Field2;
|
|
30
|
-
})(Field || {});
|
|
31
|
-
function FieldArray(item) {
|
|
32
|
-
return [item];
|
|
33
|
-
}
|
|
34
|
-
function FieldFixedArray(item, length) {
|
|
35
|
-
if (length < 0 || !Number.isFinite(length)) {
|
|
36
|
-
throw new RangeError("Length of a FixedArray must be a positive integer.");
|
|
37
|
-
}
|
|
38
|
-
return [item, length];
|
|
39
|
-
}
|
|
40
|
-
var BinaryPacket = class _BinaryPacket {
|
|
41
|
-
constructor(packetId, definition) {
|
|
42
|
-
this.packetId = packetId;
|
|
43
|
-
this.entries = definition ? sortEntries(definition) : [];
|
|
44
|
-
const inspection = inspectEntries(this.entries);
|
|
45
|
-
this.minimumByteLength = inspection.minimumByteLength;
|
|
46
|
-
this.canFastWrite = inspection.canFastWrite;
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Defines a new binary packet. \
|
|
50
|
-
* Make sure that every `packetId` is unique.
|
|
51
|
-
* @throws RangeError If packetId is negative, floating-point, or greater than 255.
|
|
52
|
-
*/
|
|
53
|
-
static define(packetId, definition) {
|
|
54
|
-
if (packetId < 0 || !Number.isFinite(packetId)) {
|
|
55
|
-
throw new RangeError("Packet IDs must be positive integers.");
|
|
56
|
-
}
|
|
57
|
-
if (packetId > 255) {
|
|
58
|
-
throw new RangeError(
|
|
59
|
-
"Packet IDs greater than 255 are not supported. Do you REALLY need more than 255 different kinds of packets?"
|
|
60
|
-
);
|
|
61
|
-
}
|
|
62
|
-
return new _BinaryPacket(packetId, definition);
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Reads just the packetId from the given Buffer. \
|
|
66
|
-
* This method practically just reads the uint8 at offset `byteOffset` (default: 0). \
|
|
67
|
-
* Useful if the receiving side receives multiple types of packets.
|
|
68
|
-
*/
|
|
69
|
-
static readPacketIdNodeBuffer(buffer, byteOffset = 0) {
|
|
70
|
-
return buffer.readUint8(byteOffset);
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Reads just the packetId from the given DataView. \
|
|
74
|
-
* This method practically just reads the uint8 at offset `byteOffset` (default: 0). \
|
|
75
|
-
* Useful if the receiving side receives multiple types of packets.
|
|
76
|
-
*/
|
|
77
|
-
static readPacketIdDataView(dataview, byteOffset = 0) {
|
|
78
|
-
return dataview.getUint8(byteOffset);
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* Reads just the packetId from the given ArrayBuffer. \
|
|
82
|
-
* This method practically just reads the uint8 at offset `byteOffset`. \
|
|
83
|
-
* Useful if the receiving side receives multiple types of packets.
|
|
84
|
-
*
|
|
85
|
-
* NOTE: Due to security issues, the `byteOffset` argument cannot be defaulted and must be provided by the user. \
|
|
86
|
-
* NOTE: For more information read the `readArrayBuffer` method documentation.
|
|
87
|
-
*/
|
|
88
|
-
static readPacketIdArrayBuffer(arraybuffer, byteOffset) {
|
|
89
|
-
return new Uint8Array(arraybuffer, byteOffset, 1)[0];
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Reads/deserializes from the given Buffer. \
|
|
93
|
-
* Method available ONLY on NodeJS and Bun.
|
|
94
|
-
*
|
|
95
|
-
* If possible, always prefer reading using this method, as it is much faster than the other ones.
|
|
96
|
-
*
|
|
97
|
-
* NOTE: if you have an ArrayBuffer do not bother wrapping it into a node Buffer yourself. \
|
|
98
|
-
* NOTE: if you have an ArrayBuffer use the appropriate `readArrayBuffer`.
|
|
99
|
-
*/
|
|
100
|
-
readNodeBuffer(dataIn, offsetPointer = { offset: 0 }, byteLength = dataIn.byteLength) {
|
|
101
|
-
return this.read(dataIn, offsetPointer, byteLength, GET_FUNCTION_BUF);
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Reads/deserializes from the given DataView.
|
|
105
|
-
*
|
|
106
|
-
* NOTE: if you have an ArrayBuffer do not bother wrapping it into a DataView yourself. \
|
|
107
|
-
* NOTE: if you have an ArrayBuffer use the appropriate `readArrayBuffer`.
|
|
108
|
-
*/
|
|
109
|
-
readDataView(dataIn, offsetPointer = { offset: 0 }, byteLength = dataIn.byteLength) {
|
|
110
|
-
return this.read(dataIn, offsetPointer, byteLength, GET_FUNCTION);
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Reads/deserializes from the given ArrayBuffer. \
|
|
114
|
-
* WARNING: this method is practically a HACK.
|
|
115
|
-
*
|
|
116
|
-
* When using this method both the `byteOffset` and `byteLength` are REQUIRED and cannot be defaulted. \
|
|
117
|
-
* This is to prevent serious bugs and security issues. \
|
|
118
|
-
* That is because often raw ArrayBuffers come from a pre-allocated buffer pool and do not start at byteOffset 0.
|
|
119
|
-
*
|
|
120
|
-
* NOTE: if you have a node Buffer do not bother wrapping it into an ArrayBuffer yourself. \
|
|
121
|
-
* NOTE: if you have a node Buffer use the appropriate `readNodeBuffer` as it is much faster and less error prone.
|
|
122
|
-
*/
|
|
123
|
-
readArrayBuffer(dataIn, byteOffset, byteLength) {
|
|
124
|
-
return this.read(
|
|
125
|
-
hasNodeBuffers ? Buffer.from(dataIn, byteOffset, byteLength) : new DataView(dataIn, byteOffset, byteLength),
|
|
126
|
-
{ offset: 0 },
|
|
127
|
-
// The underlying buffer has already been offsetted
|
|
128
|
-
byteLength,
|
|
129
|
-
hasNodeBuffers ? GET_FUNCTION_BUF : GET_FUNCTION
|
|
130
|
-
);
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Writes/serializes the given object into a Buffer. \
|
|
134
|
-
* Method available ONLY on NodeJS and Bun.
|
|
135
|
-
*
|
|
136
|
-
* If possible, always prefer writing using this method, as it is much faster than the other ones.
|
|
137
|
-
*/
|
|
138
|
-
writeNodeBuffer(dataOut) {
|
|
139
|
-
const buffer = Buffer.allocUnsafe(this.minimumByteLength);
|
|
140
|
-
return this.write(buffer, dataOut, { offset: 0 }, SET_FUNCTION_BUF, growNodeBuffer);
|
|
141
|
-
}
|
|
142
|
-
/**
|
|
143
|
-
* Writes/serializes the given object into a DataView. \
|
|
144
|
-
*/
|
|
145
|
-
writeDataView(dataOut) {
|
|
146
|
-
const dataview = new DataView(new ArrayBuffer(this.minimumByteLength));
|
|
147
|
-
return this.write(dataview, dataOut, { offset: 0 }, SET_FUNCTION, growDataView);
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Writes/serializes the given object into an ArrayBuffer. \
|
|
151
|
-
* This method is just a wrapper around either `writeNodeBuffer` or `writeDataView`. \
|
|
152
|
-
*
|
|
153
|
-
* This method works with JavaScript standard raw ArrayBuffer(s) and, as such, is very error prone: \
|
|
154
|
-
* Make sure you're using the returned byteLength and byteOffset fields in the read counterpart. \
|
|
155
|
-
*
|
|
156
|
-
* Always consider whether is possible to use directly `writeNodeBuffer` or `writeDataView` instead of `writeArrayBuffer`. \
|
|
157
|
-
* For more information read the `readArrayBuffer` documentation.
|
|
158
|
-
*/
|
|
159
|
-
writeArrayBuffer(dataOut) {
|
|
160
|
-
const buf = hasNodeBuffers ? this.writeNodeBuffer(dataOut) : this.writeDataView(dataOut);
|
|
161
|
-
return { buffer: buf.buffer, byteLength: buf.byteLength, byteOffset: buf.byteOffset };
|
|
162
|
-
}
|
|
163
|
-
entries;
|
|
164
|
-
canFastWrite;
|
|
165
|
-
minimumByteLength;
|
|
166
|
-
read(dataIn, offsetPointer, byteLength, readFunctions) {
|
|
167
|
-
if (byteLength + offsetPointer.offset < this.minimumByteLength) {
|
|
168
|
-
throw new Error(
|
|
169
|
-
`There is no space available to fit a packet of type ${this.packetId} at offset ${offsetPointer.offset}`
|
|
170
|
-
);
|
|
171
|
-
}
|
|
172
|
-
if (readFunctions[0 /* UNSIGNED_INT_8 */](dataIn, offsetPointer.offset) !== this.packetId) {
|
|
173
|
-
throw new Error(
|
|
174
|
-
`Data at offset ${offsetPointer.offset} is not a packet of type ${this.packetId}`
|
|
175
|
-
);
|
|
176
|
-
}
|
|
177
|
-
offsetPointer.offset += 1;
|
|
178
|
-
const result = {};
|
|
179
|
-
for (const [name, def] of this.entries) {
|
|
180
|
-
if (Array.isArray(def)) {
|
|
181
|
-
const length = (
|
|
182
|
-
// def[1] is the length of a statically-sized array, if undefined: must read the length from the buffer as it means it's a dynamically-sized array
|
|
183
|
-
def[1] ?? readFunctions[0 /* UNSIGNED_INT_8 */](dataIn, offsetPointer.offset++)
|
|
184
|
-
);
|
|
185
|
-
const array = Array(length);
|
|
186
|
-
const itemType = def[0];
|
|
187
|
-
if (typeof itemType === "object") {
|
|
188
|
-
for (let i = 0; i < length; ++i) {
|
|
189
|
-
array[i] = itemType.read(dataIn, offsetPointer, byteLength, readFunctions);
|
|
190
|
-
}
|
|
191
|
-
} else {
|
|
192
|
-
const itemSize = BYTE_SIZE[itemType];
|
|
193
|
-
for (let i = 0; i < length; ++i) {
|
|
194
|
-
array[i] = readFunctions[itemType](dataIn, offsetPointer.offset);
|
|
195
|
-
offsetPointer.offset += itemSize;
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
result[name] = array;
|
|
199
|
-
} else if (typeof def === "object") {
|
|
200
|
-
result[name] = def.read(dataIn, offsetPointer, byteLength, readFunctions);
|
|
201
|
-
} else {
|
|
202
|
-
result[name] = readFunctions[def](dataIn, offsetPointer.offset);
|
|
203
|
-
offsetPointer.offset += BYTE_SIZE[def];
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
return result;
|
|
207
|
-
}
|
|
208
|
-
write(buffer, dataOut, offsetPointer, writeFunctions, growBufferFunction) {
|
|
209
|
-
writeFunctions[0 /* UNSIGNED_INT_8 */](buffer, this.packetId, offsetPointer.offset);
|
|
210
|
-
offsetPointer.offset += 1;
|
|
211
|
-
if (this.canFastWrite) {
|
|
212
|
-
this.fastWrite(buffer, dataOut, offsetPointer, writeFunctions);
|
|
213
|
-
return buffer;
|
|
214
|
-
} else {
|
|
215
|
-
return this.slowWrite(
|
|
216
|
-
buffer,
|
|
217
|
-
dataOut,
|
|
218
|
-
offsetPointer,
|
|
219
|
-
this.minimumByteLength,
|
|
220
|
-
this.minimumByteLength,
|
|
221
|
-
writeFunctions,
|
|
222
|
-
growBufferFunction
|
|
223
|
-
);
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
/**
|
|
227
|
-
* Fast write does not support writing dynamically-sized arrays.
|
|
228
|
-
*/
|
|
229
|
-
fastWrite(buffer, dataOut, offsetPointer, writeFunctions) {
|
|
230
|
-
for (const [name, def] of this.entries) {
|
|
231
|
-
if (Array.isArray(def)) {
|
|
232
|
-
const itemType = def[0];
|
|
233
|
-
const length = def[1];
|
|
234
|
-
const data = dataOut[name];
|
|
235
|
-
if (typeof itemType === "object") {
|
|
236
|
-
for (let i = 0; i < length; ++i) {
|
|
237
|
-
itemType.fastWrite(buffer, data[i], offsetPointer, writeFunctions);
|
|
238
|
-
}
|
|
239
|
-
} else {
|
|
240
|
-
const itemSize = BYTE_SIZE[itemType];
|
|
241
|
-
for (let i = 0; i < length; ++i) {
|
|
242
|
-
writeFunctions[itemType](buffer, data[i], offsetPointer.offset);
|
|
243
|
-
offsetPointer.offset += itemSize;
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
} else if (typeof def === "object") {
|
|
247
|
-
def.fastWrite(buffer, dataOut[name], offsetPointer, writeFunctions);
|
|
248
|
-
} else {
|
|
249
|
-
writeFunctions[def](buffer, dataOut[name], offsetPointer.offset);
|
|
250
|
-
offsetPointer.offset += BYTE_SIZE[def];
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
/**
|
|
255
|
-
* The slow writing path tries writing data into the buffer as fast as the fast writing path does. \
|
|
256
|
-
* But, if a non-empty dynamically-sized array is encountered, the buffer needs to grow, slightly reducing performance.
|
|
257
|
-
*/
|
|
258
|
-
slowWrite(buffer, dataOut, offsetPointer, byteLength, maxByteLength, writeFunctions, growBufferFunction) {
|
|
259
|
-
for (const [name, def] of this.entries) {
|
|
260
|
-
const data = dataOut[name];
|
|
261
|
-
if (Array.isArray(def)) {
|
|
262
|
-
const length = data.length;
|
|
263
|
-
const isDynamicArray = def[1] === void 0;
|
|
264
|
-
if (isDynamicArray) {
|
|
265
|
-
writeFunctions[0 /* UNSIGNED_INT_8 */](buffer, length, offsetPointer.offset);
|
|
266
|
-
offsetPointer.offset += 1;
|
|
267
|
-
}
|
|
268
|
-
if (length > 0) {
|
|
269
|
-
const itemType = def[0];
|
|
270
|
-
if (typeof itemType === "object") {
|
|
271
|
-
if (isDynamicArray) {
|
|
272
|
-
const neededBytesForElements = length * itemType.minimumByteLength;
|
|
273
|
-
byteLength += neededBytesForElements;
|
|
274
|
-
maxByteLength += neededBytesForElements;
|
|
275
|
-
if (buffer.byteLength < maxByteLength) {
|
|
276
|
-
buffer = growBufferFunction(buffer, maxByteLength);
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
for (const object of data) {
|
|
280
|
-
writeFunctions[0 /* UNSIGNED_INT_8 */](
|
|
281
|
-
buffer,
|
|
282
|
-
itemType.packetId,
|
|
283
|
-
offsetPointer.offset
|
|
284
|
-
);
|
|
285
|
-
offsetPointer.offset += 1;
|
|
286
|
-
buffer = itemType.slowWrite(
|
|
287
|
-
buffer,
|
|
288
|
-
object,
|
|
289
|
-
offsetPointer,
|
|
290
|
-
byteLength,
|
|
291
|
-
maxByteLength,
|
|
292
|
-
writeFunctions,
|
|
293
|
-
growBufferFunction
|
|
294
|
-
);
|
|
295
|
-
byteLength = offsetPointer.offset;
|
|
296
|
-
maxByteLength = buffer.byteLength;
|
|
297
|
-
}
|
|
298
|
-
} else {
|
|
299
|
-
const itemSize = BYTE_SIZE[itemType];
|
|
300
|
-
if (isDynamicArray) {
|
|
301
|
-
const neededBytesForElements = length * itemSize;
|
|
302
|
-
byteLength += neededBytesForElements;
|
|
303
|
-
maxByteLength += neededBytesForElements;
|
|
304
|
-
if (buffer.byteLength < maxByteLength) {
|
|
305
|
-
buffer = growBufferFunction(buffer, maxByteLength);
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
for (const number of data) {
|
|
309
|
-
writeFunctions[itemType](buffer, number, offsetPointer.offset);
|
|
310
|
-
offsetPointer.offset += itemSize;
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
} else if (typeof def === "object") {
|
|
315
|
-
writeFunctions[0 /* UNSIGNED_INT_8 */](buffer, def.packetId, offsetPointer.offset);
|
|
316
|
-
offsetPointer.offset += 1;
|
|
317
|
-
buffer = def.slowWrite(
|
|
318
|
-
buffer,
|
|
319
|
-
data,
|
|
320
|
-
offsetPointer,
|
|
321
|
-
byteLength,
|
|
322
|
-
maxByteLength,
|
|
323
|
-
writeFunctions,
|
|
324
|
-
growBufferFunction
|
|
325
|
-
);
|
|
326
|
-
byteLength = offsetPointer.offset;
|
|
327
|
-
maxByteLength = buffer.byteLength;
|
|
328
|
-
} else {
|
|
329
|
-
writeFunctions[def](buffer, data, offsetPointer.offset);
|
|
330
|
-
offsetPointer.offset += BYTE_SIZE[def];
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
return buffer;
|
|
334
|
-
}
|
|
335
|
-
};
|
|
336
|
-
function sortEntries(definition) {
|
|
337
|
-
return Object.entries(definition).sort(
|
|
338
|
-
([fieldName1], [fieldName2]) => fieldName1.localeCompare(fieldName2)
|
|
339
|
-
);
|
|
340
|
-
}
|
|
341
|
-
function inspectEntries(entries) {
|
|
342
|
-
let minimumByteLength = 1;
|
|
343
|
-
let canFastWrite = true;
|
|
344
|
-
for (const [, type] of entries) {
|
|
345
|
-
if (Array.isArray(type)) {
|
|
346
|
-
if (type.length === 2) {
|
|
347
|
-
const itemSize = typeof type[0] === "object" ? type[0].minimumByteLength : BYTE_SIZE[type[0]];
|
|
348
|
-
minimumByteLength += type[1] * itemSize;
|
|
349
|
-
} else {
|
|
350
|
-
minimumByteLength += 1;
|
|
351
|
-
canFastWrite = false;
|
|
352
|
-
}
|
|
353
|
-
} else if (type instanceof BinaryPacket) {
|
|
354
|
-
minimumByteLength += type.minimumByteLength;
|
|
355
|
-
canFastWrite &&= type.canFastWrite;
|
|
356
|
-
} else {
|
|
357
|
-
minimumByteLength += BYTE_SIZE[type];
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
return { minimumByteLength, canFastWrite };
|
|
361
|
-
}
|
|
362
|
-
var BYTE_SIZE = Array(8);
|
|
363
|
-
BYTE_SIZE[0 /* UNSIGNED_INT_8 */] = 1;
|
|
364
|
-
BYTE_SIZE[3 /* INT_8 */] = 1;
|
|
365
|
-
BYTE_SIZE[1 /* UNSIGNED_INT_16 */] = 2;
|
|
366
|
-
BYTE_SIZE[4 /* INT_16 */] = 2;
|
|
367
|
-
BYTE_SIZE[2 /* UNSIGNED_INT_32 */] = 4;
|
|
368
|
-
BYTE_SIZE[5 /* INT_32 */] = 4;
|
|
369
|
-
BYTE_SIZE[6 /* FLOAT_32 */] = 4;
|
|
370
|
-
BYTE_SIZE[7 /* FLOAT_64 */] = 8;
|
|
371
|
-
var GET_FUNCTION = Array(8);
|
|
372
|
-
GET_FUNCTION[0 /* UNSIGNED_INT_8 */] = (view, offset) => view.getUint8(offset);
|
|
373
|
-
GET_FUNCTION[3 /* INT_8 */] = (view, offset) => view.getInt8(offset);
|
|
374
|
-
GET_FUNCTION[1 /* UNSIGNED_INT_16 */] = (view, offset) => view.getUint16(offset);
|
|
375
|
-
GET_FUNCTION[4 /* INT_16 */] = (view, offset) => view.getInt16(offset);
|
|
376
|
-
GET_FUNCTION[2 /* UNSIGNED_INT_32 */] = (view, offset) => view.getUint32(offset);
|
|
377
|
-
GET_FUNCTION[5 /* INT_32 */] = (view, offset) => view.getInt32(offset);
|
|
378
|
-
GET_FUNCTION[6 /* FLOAT_32 */] = (view, offset) => view.getFloat32(offset);
|
|
379
|
-
GET_FUNCTION[7 /* FLOAT_64 */] = (view, offset) => view.getFloat64(offset);
|
|
380
|
-
var SET_FUNCTION = Array(8);
|
|
381
|
-
SET_FUNCTION[0 /* UNSIGNED_INT_8 */] = (view, value, offset) => view.setUint8(offset, value);
|
|
382
|
-
SET_FUNCTION[3 /* INT_8 */] = (view, value, offset) => view.setInt8(offset, value);
|
|
383
|
-
SET_FUNCTION[1 /* UNSIGNED_INT_16 */] = (view, value, offset) => view.setUint16(offset, value);
|
|
384
|
-
SET_FUNCTION[4 /* INT_16 */] = (view, value, offset) => view.setInt16(offset, value);
|
|
385
|
-
SET_FUNCTION[2 /* UNSIGNED_INT_32 */] = (view, value, offset) => view.setUint32(offset, value);
|
|
386
|
-
SET_FUNCTION[5 /* INT_32 */] = (view, value, offset) => view.setInt32(offset, value);
|
|
387
|
-
SET_FUNCTION[6 /* FLOAT_32 */] = (view, value, offset) => view.setFloat32(offset, value);
|
|
388
|
-
SET_FUNCTION[7 /* FLOAT_64 */] = (view, value, offset) => view.setFloat64(offset, value);
|
|
389
|
-
var SET_FUNCTION_BUF = Array(8);
|
|
390
|
-
if (hasNodeBuffers) {
|
|
391
|
-
SET_FUNCTION_BUF[0 /* UNSIGNED_INT_8 */] = (view, value, offset) => view.writeUint8(value, offset);
|
|
392
|
-
SET_FUNCTION_BUF[3 /* INT_8 */] = (view, value, offset) => view.writeInt8(value, offset);
|
|
393
|
-
SET_FUNCTION_BUF[1 /* UNSIGNED_INT_16 */] = (view, value, offset) => view.writeUint16LE(value, offset);
|
|
394
|
-
SET_FUNCTION_BUF[4 /* INT_16 */] = (view, value, offset) => view.writeInt16LE(value, offset);
|
|
395
|
-
SET_FUNCTION_BUF[2 /* UNSIGNED_INT_32 */] = (view, value, offset) => view.writeUint32LE(value, offset);
|
|
396
|
-
SET_FUNCTION_BUF[5 /* INT_32 */] = (view, value, offset) => view.writeInt32LE(value, offset);
|
|
397
|
-
SET_FUNCTION_BUF[6 /* FLOAT_32 */] = (view, value, offset) => view.writeFloatLE(value, offset);
|
|
398
|
-
SET_FUNCTION_BUF[7 /* FLOAT_64 */] = (view, value, offset) => view.writeDoubleLE(value, offset);
|
|
399
|
-
}
|
|
400
|
-
var GET_FUNCTION_BUF = Array(8);
|
|
401
|
-
if (hasNodeBuffers) {
|
|
402
|
-
GET_FUNCTION_BUF[0 /* UNSIGNED_INT_8 */] = (view, offset) => view.readUint8(offset);
|
|
403
|
-
GET_FUNCTION_BUF[3 /* INT_8 */] = (view, offset) => view.readInt8(offset);
|
|
404
|
-
GET_FUNCTION_BUF[1 /* UNSIGNED_INT_16 */] = (view, offset) => view.readUint16LE(offset);
|
|
405
|
-
GET_FUNCTION_BUF[4 /* INT_16 */] = (view, offset) => view.readInt16LE(offset);
|
|
406
|
-
GET_FUNCTION_BUF[2 /* UNSIGNED_INT_32 */] = (view, offset) => view.readUint32LE(offset);
|
|
407
|
-
GET_FUNCTION_BUF[5 /* INT_32 */] = (view, offset) => view.readInt32LE(offset);
|
|
408
|
-
GET_FUNCTION_BUF[6 /* FLOAT_32 */] = (view, offset) => view.readFloatLE(offset);
|
|
409
|
-
GET_FUNCTION_BUF[7 /* FLOAT_64 */] = (view, offset) => view.readDoubleLE(offset);
|
|
410
|
-
}
|
|
411
|
-
export {
|
|
412
|
-
BinaryPacket,
|
|
413
|
-
Field,
|
|
414
|
-
FieldArray,
|
|
415
|
-
FieldFixedArray
|
|
416
|
-
};
|
|
1
|
+
var B=typeof Buffer=="function";function E(n,e){let t=new ArrayBuffer(e),r=Math.min(n.byteLength,t.byteLength),i=Math.trunc(r/8);new Float64Array(t,0,i).set(new Float64Array(n.buffer,0,i));let s=i*8;return i=r-s,new Uint8Array(t,s,i).set(new Uint8Array(n.buffer,s,i)),new DataView(t)}function U(n,e){let t=Buffer.allocUnsafe(e);return n.copy(t),t}var h=(a=>(a[a.UNSIGNED_INT_8=0]="UNSIGNED_INT_8",a[a.UNSIGNED_INT_16=1]="UNSIGNED_INT_16",a[a.UNSIGNED_INT_32=2]="UNSIGNED_INT_32",a[a.INT_8=3]="INT_8",a[a.INT_16=4]="INT_16",a[a.INT_32=5]="INT_32",a[a.FLOAT_32=6]="FLOAT_32",a[a.FLOAT_64=7]="FLOAT_64",a))(h||{});function g(n){return[n]}function S(n,e){if(e<0||!Number.isFinite(e))throw new RangeError("Length of a FixedArray must be a positive integer.");return[n,e]}var p=class n{constructor(e,t){this.packetId=e;this.entries=t?w(t):[];let r=A(this.entries);this.minimumByteLength=r.minimumByteLength,this.canFastWrite=r.canFastWrite}static define(e,t){if(e<0||!Number.isFinite(e))throw new RangeError("Packet IDs must be positive integers.");if(e>255)throw new RangeError("Packet IDs greater than 255 are not supported. Do you REALLY need more than 255 different kinds of packets?");return new n(e,t)}static readPacketIdNodeBuffer(e,t=0){return e.readUint8(t)}static readPacketIdDataView(e,t=0){return e.getUint8(t)}static readPacketIdArrayBuffer(e,t){return new Uint8Array(e,t,1)[0]}readNodeBuffer(e,t={offset:0},r=e.byteLength){return this.read(e,t,r,l)}readDataView(e,t={offset:0},r=e.byteLength){return this.read(e,t,r,m)}readArrayBuffer(e,t,r){return this.read(B?Buffer.from(e,t,r):new DataView(e,t,r),{offset:0},r,B?l:m)}writeNodeBuffer(e){let t=Buffer.allocUnsafe(this.minimumByteLength);return this.write(t,e,{offset:0},d,U)}writeDataView(e){let t=new DataView(new ArrayBuffer(this.minimumByteLength));return this.write(t,e,{offset:0},I,E)}writeArrayBuffer(e){let t=B?this.writeNodeBuffer(e):this.writeDataView(e);return{buffer:t.buffer,byteLength:t.byteLength,byteOffset:t.byteOffset}}entries;canFastWrite;minimumByteLength;read(e,t,r,i){if(r+t.offset<this.minimumByteLength)throw new Error(`There is no space available to fit a packet of type ${this.packetId} at offset ${t.offset}`);if(i[0](e,t.offset)!==this.packetId)throw new Error(`Data at offset ${t.offset} is not a packet of type ${this.packetId}`);t.offset+=1;let s={};for(let[f,o]of this.entries)if(Array.isArray(o)){let a=o[1]??i[0](e,t.offset++),y=Array(a),T=o[0];if(typeof T=="object")for(let u=0;u<a;++u)y[u]=T.read(e,t,r,i);else{let u=N[T];for(let _=0;_<a;++_)y[_]=i[T](e,t.offset),t.offset+=u}s[f]=y}else typeof o=="object"?s[f]=o.read(e,t,r,i):(s[f]=i[o](e,t.offset),t.offset+=N[o]);return s}write(e,t,r,i,s){return i[0](e,this.packetId,r.offset),r.offset+=1,this.canFastWrite?(this.fastWrite(e,t,r,i),e):this.slowWrite(e,t,r,this.minimumByteLength,this.minimumByteLength,i,s)}fastWrite(e,t,r,i){for(let[s,f]of this.entries)if(Array.isArray(f)){let o=f[0],a=f[1],y=t[s];if(typeof o=="object")for(let T=0;T<a;++T)o.fastWrite(e,y[T],r,i);else{let T=N[o];for(let u=0;u<a;++u)i[o](e,y[u],r.offset),r.offset+=T}}else typeof f=="object"?f.fastWrite(e,t[s],r,i):(i[f](e,t[s],r.offset),r.offset+=N[f])}slowWrite(e,t,r,i,s,f,o){for(let[a,y]of this.entries){let T=t[a];if(Array.isArray(y)){let u=T.length,_=y[1]===void 0;if(_&&(f[0](e,u,r.offset),r.offset+=1),u>0){let D=y[0];if(typeof D=="object"){if(_){let c=u*D.minimumByteLength;i+=c,s+=c,e.byteLength<s&&(e=o(e,s))}for(let c of T)f[0](e,D.packetId,r.offset),r.offset+=1,e=D.slowWrite(e,c,r,i,s,f,o),i=r.offset,s=e.byteLength}else{let c=N[D];if(_){let F=u*c;i+=F,s+=F,e.byteLength<s&&(e=o(e,s))}for(let F of T)f[D](e,F,r.offset),r.offset+=c}}}else typeof y=="object"?(f[0](e,y.packetId,r.offset),r.offset+=1,e=y.slowWrite(e,T,r,i,s,f,o),i=r.offset,s=e.byteLength):(f[y](e,T,r.offset),r.offset+=N[y])}return e}};function w(n){return Object.entries(n).sort(([e],[t])=>e.localeCompare(t))}function A(n){let e=1,t=!0;for(let[,r]of n)if(Array.isArray(r))if(r.length===2){let i=typeof r[0]=="object"?r[0].minimumByteLength:N[r[0]];e+=r[1]*i}else e+=1,t=!1;else r instanceof p?(e+=r.minimumByteLength,t&&=r.canFastWrite):e+=N[r];return{minimumByteLength:e,canFastWrite:t}}var N=Array(8);N[0]=1;N[3]=1;N[1]=2;N[4]=2;N[2]=4;N[5]=4;N[6]=4;N[7]=8;var m=Array(8);m[0]=(n,e)=>n.getUint8(e);m[3]=(n,e)=>n.getInt8(e);m[1]=(n,e)=>n.getUint16(e);m[4]=(n,e)=>n.getInt16(e);m[2]=(n,e)=>n.getUint32(e);m[5]=(n,e)=>n.getInt32(e);m[6]=(n,e)=>n.getFloat32(e);m[7]=(n,e)=>n.getFloat64(e);var I=Array(8);I[0]=(n,e,t)=>n.setUint8(t,e);I[3]=(n,e,t)=>n.setInt8(t,e);I[1]=(n,e,t)=>n.setUint16(t,e);I[4]=(n,e,t)=>n.setInt16(t,e);I[2]=(n,e,t)=>n.setUint32(t,e);I[5]=(n,e,t)=>n.setInt32(t,e);I[6]=(n,e,t)=>n.setFloat32(t,e);I[7]=(n,e,t)=>n.setFloat64(t,e);var d=Array(8);B&&(d[0]=(n,e,t)=>n.writeUint8(e,t),d[3]=(n,e,t)=>n.writeInt8(e,t),d[1]=(n,e,t)=>n.writeUint16LE(e,t),d[4]=(n,e,t)=>n.writeInt16LE(e,t),d[2]=(n,e,t)=>n.writeUint32LE(e,t),d[5]=(n,e,t)=>n.writeInt32LE(e,t),d[6]=(n,e,t)=>n.writeFloatLE(e,t),d[7]=(n,e,t)=>n.writeDoubleLE(e,t));var l=Array(8);B&&(l[0]=(n,e)=>n.readUint8(e),l[3]=(n,e)=>n.readInt8(e),l[1]=(n,e)=>n.readUint16LE(e),l[4]=(n,e)=>n.readInt16LE(e),l[2]=(n,e)=>n.readUint32LE(e),l[5]=(n,e)=>n.readInt32LE(e),l[6]=(n,e)=>n.readFloatLE(e),l[7]=(n,e)=>n.readDoubleLE(e));export{p as BinaryPacket,h as Field,g as FieldArray,S as FieldFixedArray};
|
|
417
2
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/buffers.ts","../src/index.ts"],"sourcesContent":["export const hasNodeBuffers = typeof Buffer === 'function'\r\n\r\nexport function growDataView(dataview: DataView, newByteLength: number) {\r\n const resizedBuffer = new ArrayBuffer(newByteLength)\r\n const amountToCopy = Math.min(dataview.byteLength, resizedBuffer.byteLength)\r\n\r\n // Treat the buffer as if it was a Float64Array so we can copy 8 bytes at a time, to finish faster\r\n let length = Math.trunc(amountToCopy / 8)\r\n new Float64Array(resizedBuffer, 0, length).set(new Float64Array(dataview.buffer, 0, length))\r\n\r\n // Copy the remaining up to 7 bytes\r\n const offset = length * 8\r\n length = amountToCopy - offset\r\n new Uint8Array(resizedBuffer, offset, length).set(new Uint8Array(dataview.buffer, offset, length))\r\n\r\n return new DataView(resizedBuffer)\r\n}\r\n\r\nexport function growNodeBuffer(buffer: Buffer, newByteLength: number) {\r\n const newBuffer = Buffer.allocUnsafe(newByteLength)\r\n buffer.copy(newBuffer)\r\n return newBuffer\r\n}\r\n","import { growDataView, growNodeBuffer, hasNodeBuffers } from './buffers'\r\n\r\nexport const enum Field {\r\n /**\r\n * Defines a 1 byte (8 bits) unsigned integer field. \\\r\n * (Range: 0 - 255)\r\n */\r\n UNSIGNED_INT_8 = 0,\r\n\r\n /**\r\n * Defines a 2 bytes (16 bits) unsigned integer field. \\\r\n * (Range: 0 - 65535)\r\n */\r\n UNSIGNED_INT_16,\r\n\r\n /**\r\n * Defines a 4 bytes (32 bits) unsigned integer field. \\\r\n * (Range: 0 - 4294967295)\r\n */\r\n UNSIGNED_INT_32,\r\n\r\n /**\r\n * Defines a 1 byte (8 bits) signed integer field. \\\r\n * (Range: -128 - 127)\r\n */\r\n INT_8,\r\n\r\n /**\r\n * Defines a 2 bytes (16 bits) signed integer field. \\\r\n * (Range: -32768 - 32767)\r\n */\r\n INT_16,\r\n\r\n /**\r\n * Defines a 4 bytes (32 bits) signed integer field. \\\r\n * (Range: -2147483648 - 2147483647)\r\n */\r\n INT_32,\r\n\r\n /**\r\n * Defines a 4 bytes (32 bits) floating-point field. \\\r\n */\r\n FLOAT_32,\r\n\r\n /**\r\n * Defines a 8 bytes (64 bits) floating-point field. \\\r\n */\r\n FLOAT_64\r\n}\r\n\r\n/**\r\n * Defines a dynamically-sized array with elements of a certain type. \\\r\n * Dynamically-sized arrays are useful when a packet's field is an array of a non pre-defined length. \\\r\n * Although, this makes dynamically-sized arrays more memory expensive as the internal buffer needs to be grown accordingly.\r\n *\r\n * NOTE: If an array will ALWAYS have the same length, prefer using the `FieldFixedArray` type, for both better performance and memory efficiency. \\\r\n * NOTE: As of now, dynamic arrays can have at most 256 elements.\r\n */\r\nexport function FieldArray<T extends Field | BinaryPacket<Definition>>(item: T): [itemType: T] {\r\n return [item]\r\n}\r\n\r\n/**\r\n * Defines a statically-sized array with elements of a certain type. \\\r\n * Fixed arrays are useful when a packet's field is an array of a pre-defined length. \\\r\n * Fixed arrays much more memory efficient and performant than non-fixed ones.\r\n *\r\n * NOTE: If an array will not always have the same length, use the `FieldArray` type.\r\n */\r\nexport function FieldFixedArray<T extends Field | BinaryPacket<Definition>, Length extends number>(\r\n item: T,\r\n length: Length\r\n): [itemType: T, length: Length] {\r\n if (length < 0 || !Number.isFinite(length)) {\r\n throw new RangeError('Length of a FixedArray must be a positive integer.')\r\n }\r\n\r\n return [item, length]\r\n}\r\n\r\nexport class BinaryPacket<T extends Definition> {\r\n /**\r\n * Defines a new binary packet. \\\r\n * Make sure that every `packetId` is unique.\r\n * @throws RangeError If packetId is negative, floating-point, or greater than 255.\r\n */\r\n static define<T extends Definition>(packetId: number, definition?: T) {\r\n if (packetId < 0 || !Number.isFinite(packetId)) {\r\n throw new RangeError('Packet IDs must be positive integers.')\r\n }\r\n\r\n if (packetId > 255) {\r\n throw new RangeError(\r\n 'Packet IDs greater than 255 are not supported. Do you REALLY need more than 255 different kinds of packets?'\r\n )\r\n }\r\n\r\n return new BinaryPacket(packetId, definition)\r\n }\r\n\r\n /**\r\n * Reads just the packetId from the given Buffer. \\\r\n * This method practically just reads the uint8 at offset `byteOffset` (default: 0). \\\r\n * Useful if the receiving side receives multiple types of packets.\r\n */\r\n static readPacketIdNodeBuffer(buffer: Buffer, byteOffset = 0) {\r\n return buffer.readUint8(byteOffset)\r\n }\r\n\r\n /**\r\n * Reads just the packetId from the given DataView. \\\r\n * This method practically just reads the uint8 at offset `byteOffset` (default: 0). \\\r\n * Useful if the receiving side receives multiple types of packets.\r\n */\r\n static readPacketIdDataView(dataview: DataView, byteOffset = 0) {\r\n return dataview.getUint8(byteOffset)\r\n }\r\n\r\n /**\r\n * Reads just the packetId from the given ArrayBuffer. \\\r\n * This method practically just reads the uint8 at offset `byteOffset`. \\\r\n * Useful if the receiving side receives multiple types of packets.\r\n *\r\n * NOTE: Due to security issues, the `byteOffset` argument cannot be defaulted and must be provided by the user. \\\r\n * NOTE: For more information read the `readArrayBuffer` method documentation.\r\n */\r\n static readPacketIdArrayBuffer(arraybuffer: ArrayBuffer, byteOffset: number) {\r\n return new Uint8Array(arraybuffer, byteOffset, 1)[0]\r\n }\r\n\r\n /**\r\n * Reads/deserializes from the given Buffer. \\\r\n * Method available ONLY on NodeJS and Bun.\r\n *\r\n * If possible, always prefer reading using this method, as it is much faster than the other ones.\r\n *\r\n * NOTE: if you have an ArrayBuffer do not bother wrapping it into a node Buffer yourself. \\\r\n * NOTE: if you have an ArrayBuffer use the appropriate `readArrayBuffer`.\r\n */\r\n readNodeBuffer(\r\n dataIn: Buffer,\r\n offsetPointer = { offset: 0 },\r\n byteLength = dataIn.byteLength\r\n ): ToJson<T> {\r\n return this.read(dataIn, offsetPointer, byteLength, GET_FUNCTION_BUF)\r\n }\r\n\r\n /**\r\n * Reads/deserializes from the given DataView.\r\n *\r\n * NOTE: if you have an ArrayBuffer do not bother wrapping it into a DataView yourself. \\\r\n * NOTE: if you have an ArrayBuffer use the appropriate `readArrayBuffer`.\r\n */\r\n readDataView(\r\n dataIn: DataView,\r\n offsetPointer = { offset: 0 },\r\n byteLength = dataIn.byteLength\r\n ): ToJson<T> {\r\n return this.read(dataIn, offsetPointer, byteLength, GET_FUNCTION)\r\n }\r\n\r\n /**\r\n * Reads/deserializes from the given ArrayBuffer. \\\r\n * WARNING: this method is practically a HACK.\r\n *\r\n * When using this method both the `byteOffset` and `byteLength` are REQUIRED and cannot be defaulted. \\\r\n * This is to prevent serious bugs and security issues. \\\r\n * That is because often raw ArrayBuffers come from a pre-allocated buffer pool and do not start at byteOffset 0.\r\n *\r\n * NOTE: if you have a node Buffer do not bother wrapping it into an ArrayBuffer yourself. \\\r\n * NOTE: if you have a node Buffer use the appropriate `readNodeBuffer` as it is much faster and less error prone.\r\n */\r\n readArrayBuffer(\r\n dataIn: ArrayBuffer & { buffer?: undefined },\r\n byteOffset: number,\r\n byteLength: number\r\n ) {\r\n return this.read(\r\n hasNodeBuffers\r\n ? Buffer.from(dataIn, byteOffset, byteLength)\r\n : new DataView(dataIn, byteOffset, byteLength),\r\n { offset: 0 }, // The underlying buffer has already been offsetted\r\n byteLength,\r\n hasNodeBuffers ? GET_FUNCTION_BUF : GET_FUNCTION\r\n )\r\n }\r\n\r\n /**\r\n * Writes/serializes the given object into a Buffer. \\\r\n * Method available ONLY on NodeJS and Bun.\r\n *\r\n * If possible, always prefer writing using this method, as it is much faster than the other ones.\r\n */\r\n writeNodeBuffer(dataOut: ToJson<T>) {\r\n const buffer = Buffer.allocUnsafe(this.minimumByteLength)\r\n return this.write(buffer, dataOut, { offset: 0 }, SET_FUNCTION_BUF, growNodeBuffer)\r\n }\r\n\r\n /**\r\n * Writes/serializes the given object into a DataView. \\\r\n */\r\n writeDataView(dataOut: ToJson<T>) {\r\n const dataview = new DataView(new ArrayBuffer(this.minimumByteLength))\r\n return this.write(dataview, dataOut, { offset: 0 }, SET_FUNCTION, growDataView)\r\n }\r\n\r\n /**\r\n * Writes/serializes the given object into an ArrayBuffer. \\\r\n * This method is just a wrapper around either `writeNodeBuffer` or `writeDataView`. \\\r\n *\r\n * This method works with JavaScript standard raw ArrayBuffer(s) and, as such, is very error prone: \\\r\n * Make sure you're using the returned byteLength and byteOffset fields in the read counterpart. \\\r\n *\r\n * Always consider whether is possible to use directly `writeNodeBuffer` or `writeDataView` instead of `writeArrayBuffer`. \\\r\n * For more information read the `readArrayBuffer` documentation.\r\n */\r\n writeArrayBuffer(dataOut: ToJson<T>) {\r\n const buf = hasNodeBuffers ? this.writeNodeBuffer(dataOut) : this.writeDataView(dataOut)\r\n return { buffer: buf.buffer, byteLength: buf.byteLength, byteOffset: buf.byteOffset }\r\n }\r\n\r\n private readonly entries: Entries\r\n readonly canFastWrite: boolean\r\n readonly minimumByteLength: number\r\n\r\n private constructor(\r\n private readonly packetId: number,\r\n definition?: T\r\n ) {\r\n this.entries = definition ? sortEntries(definition) : []\r\n const inspection = inspectEntries(this.entries)\r\n\r\n this.minimumByteLength = inspection.minimumByteLength\r\n this.canFastWrite = inspection.canFastWrite\r\n }\r\n\r\n private read(\r\n dataIn: DataView | Buffer,\r\n offsetPointer: { offset: number },\r\n byteLength: number,\r\n readFunctions: typeof GET_FUNCTION | typeof GET_FUNCTION_BUF\r\n ): ToJson<T> {\r\n if (byteLength + offsetPointer.offset < this.minimumByteLength) {\r\n throw new Error(\r\n `There is no space available to fit a packet of type ${this.packetId} at offset ${offsetPointer.offset}`\r\n )\r\n }\r\n\r\n if (\r\n readFunctions[Field.UNSIGNED_INT_8](dataIn as any, offsetPointer.offset) !== this.packetId\r\n ) {\r\n throw new Error(\r\n `Data at offset ${offsetPointer.offset} is not a packet of type ${this.packetId}`\r\n )\r\n }\r\n\r\n offsetPointer.offset += 1\r\n const result: any = {}\r\n\r\n for (const [name, def] of this.entries) {\r\n if (Array.isArray(def)) {\r\n const length =\r\n // def[1] is the length of a statically-sized array, if undefined: must read the length from the buffer as it means it's a dynamically-sized array\r\n def[1] ?? readFunctions[Field.UNSIGNED_INT_8](dataIn as any, offsetPointer.offset++)\r\n\r\n const array = Array(length)\r\n\r\n const itemType = def[0]\r\n\r\n if (typeof itemType === 'object') {\r\n // Array of \"subpackets\"\r\n for (let i = 0; i < length; ++i) {\r\n array[i] = itemType.read(dataIn, offsetPointer, byteLength, readFunctions)\r\n }\r\n } else {\r\n // Array of primitives (numbers)\r\n const itemSize = BYTE_SIZE[itemType]\r\n\r\n // It seems like looping over each element is actually much faster than using TypedArrays bulk copy.\r\n // TODO: properly benchmark with various array sizes to see if it's actually the case.\r\n for (let i = 0; i < length; ++i) {\r\n array[i] = readFunctions[itemType](dataIn as any, offsetPointer.offset)\r\n offsetPointer.offset += itemSize\r\n }\r\n }\r\n\r\n // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access\r\n result[name] = array\r\n } else if (typeof def === 'object') {\r\n // Single \"subpacket\"\r\n // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access\r\n result[name] = def.read(dataIn, offsetPointer, byteLength, readFunctions)\r\n } else {\r\n // Single primitive (number)\r\n // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access\r\n result[name] = readFunctions[def](dataIn as any, offsetPointer.offset)\r\n offsetPointer.offset += BYTE_SIZE[def]\r\n }\r\n }\r\n\r\n return result as ToJson<T>\r\n }\r\n\r\n private write<Buf extends DataView | Buffer>(\r\n buffer: Buf,\r\n dataOut: ToJson<T>,\r\n offsetPointer: { offset: number },\r\n writeFunctions: typeof SET_FUNCTION | typeof SET_FUNCTION_BUF,\r\n growBufferFunction: (buffer: Buf, newByteLength: number) => Buf\r\n ): Buf {\r\n writeFunctions[Field.UNSIGNED_INT_8](buffer as any, this.packetId, offsetPointer.offset)\r\n offsetPointer.offset += 1\r\n\r\n if (this.canFastWrite) {\r\n // If there are no arrays, the minimumByteLength always equals to the full needed byteLength.\r\n // So we can take the fast path, since we know beforehand that the buffer isn't going to grow.\r\n this.fastWrite(buffer, dataOut, offsetPointer, writeFunctions)\r\n return buffer\r\n } else {\r\n // If non-empty arrays are encountered, the buffer must grow.\r\n // If every array is empty, the speed of this path is comparable to the fast path.\r\n return this.slowWrite(\r\n buffer,\r\n dataOut,\r\n offsetPointer,\r\n this.minimumByteLength,\r\n this.minimumByteLength,\r\n writeFunctions,\r\n growBufferFunction\r\n )\r\n }\r\n }\r\n\r\n /**\r\n * Fast write does not support writing dynamically-sized arrays.\r\n */\r\n private fastWrite<Buf extends DataView | Buffer>(\r\n buffer: Buf,\r\n dataOut: ToJson<T>,\r\n offsetPointer: { offset: number },\r\n writeFunctions: typeof SET_FUNCTION | typeof SET_FUNCTION_BUF\r\n ) {\r\n for (const [name, def] of this.entries) {\r\n if (Array.isArray(def)) {\r\n // Statically-sized array\r\n const itemType = def[0]\r\n const length = def[1]!\r\n const data = dataOut[name] as any[]\r\n\r\n if (typeof itemType === 'object') {\r\n for (let i = 0; i < length; ++i) {\r\n itemType.fastWrite(buffer, data[i] as ToJson<Definition>, offsetPointer, writeFunctions)\r\n }\r\n } else {\r\n const itemSize = BYTE_SIZE[itemType]\r\n\r\n for (let i = 0; i < length; ++i) {\r\n writeFunctions[itemType](buffer as any, data[i] as number, offsetPointer.offset)\r\n offsetPointer.offset += itemSize\r\n }\r\n }\r\n } else if (typeof def === 'object') {\r\n // Single \"subpacket\"\r\n // In fastWrite there cannot be arrays, but the cast is needed because TypeScript can't possibly know that.\r\n def.fastWrite(buffer, dataOut[name] as ToJson<Definition>, offsetPointer, writeFunctions)\r\n } else {\r\n // Single primitive (number)\r\n writeFunctions[def](buffer as any, dataOut[name] as number, offsetPointer.offset)\r\n offsetPointer.offset += BYTE_SIZE[def]\r\n }\r\n }\r\n }\r\n\r\n /**\r\n * The slow writing path tries writing data into the buffer as fast as the fast writing path does. \\\r\n * But, if a non-empty dynamically-sized array is encountered, the buffer needs to grow, slightly reducing performance.\r\n */\r\n private slowWrite<Buf extends DataView | Buffer>(\r\n buffer: Buf,\r\n dataOut: ToJson<T>,\r\n offsetPointer: { offset: number },\r\n byteLength: number,\r\n maxByteLength: number,\r\n writeFunctions: typeof SET_FUNCTION | typeof SET_FUNCTION_BUF,\r\n growBufferFunction: (buffer: Buf, newByteLength: number) => Buf\r\n ): Buf {\r\n for (const [name, def] of this.entries) {\r\n const data = dataOut[name]\r\n\r\n if (Array.isArray(def)) {\r\n // Could be both an array of just numbers or \"subpackets\"\r\n\r\n const length = (data as any[]).length\r\n const isDynamicArray = def[1] === undefined\r\n\r\n // Check if it is a dynamically-sized array, if it is, the length of the array must be serialized in the buffer before its elements\r\n // Explicitly check for undefined and not falsy values because it could be a statically-sized array of 0 elements.\r\n if (isDynamicArray) {\r\n writeFunctions[Field.UNSIGNED_INT_8](buffer as any, length, offsetPointer.offset)\r\n offsetPointer.offset += 1\r\n }\r\n\r\n if (length > 0) {\r\n const itemType = def[0]\r\n\r\n if (typeof itemType === 'object') {\r\n // Array of \"subpackets\"\r\n\r\n if (isDynamicArray) {\r\n const neededBytesForElements = length * itemType.minimumByteLength\r\n\r\n byteLength += neededBytesForElements\r\n maxByteLength += neededBytesForElements\r\n\r\n if (buffer.byteLength < maxByteLength) {\r\n buffer = growBufferFunction(buffer, maxByteLength)\r\n }\r\n }\r\n\r\n for (const object of data as unknown as ToJson<Definition>[]) {\r\n writeFunctions[Field.UNSIGNED_INT_8](\r\n buffer as any,\r\n itemType.packetId,\r\n offsetPointer.offset\r\n )\r\n\r\n offsetPointer.offset += 1\r\n\r\n buffer = itemType.slowWrite(\r\n buffer,\r\n object,\r\n offsetPointer,\r\n byteLength,\r\n maxByteLength,\r\n writeFunctions,\r\n growBufferFunction\r\n )\r\n\r\n byteLength = offsetPointer.offset\r\n maxByteLength = buffer.byteLength\r\n }\r\n } else {\r\n // Array of primitives (numbers)\r\n const itemSize = BYTE_SIZE[itemType]\r\n\r\n if (isDynamicArray) {\r\n const neededBytesForElements = length * itemSize\r\n\r\n byteLength += neededBytesForElements\r\n maxByteLength += neededBytesForElements\r\n\r\n if (buffer.byteLength < maxByteLength) {\r\n buffer = growBufferFunction(buffer, maxByteLength)\r\n }\r\n }\r\n\r\n // It seems like looping over each element is actually much faster than using TypedArrays bulk copy.\r\n // TODO: properly benchmark with various array sizes to see if it's actually the case.\r\n for (const number of data as number[]) {\r\n writeFunctions[itemType](buffer as any, number, offsetPointer.offset)\r\n offsetPointer.offset += itemSize\r\n }\r\n }\r\n }\r\n } else if (typeof def === 'object') {\r\n // Single \"subpacket\"\r\n writeFunctions[Field.UNSIGNED_INT_8](buffer as any, def.packetId, offsetPointer.offset)\r\n offsetPointer.offset += 1\r\n\r\n buffer = def.slowWrite(\r\n buffer,\r\n data as ToJson<Definition>,\r\n offsetPointer,\r\n byteLength,\r\n maxByteLength,\r\n writeFunctions,\r\n growBufferFunction\r\n )\r\n\r\n byteLength = offsetPointer.offset\r\n maxByteLength = buffer.byteLength\r\n } else {\r\n // Single primitive (number)\r\n writeFunctions[def](buffer as any, data as number, offsetPointer.offset)\r\n offsetPointer.offset += BYTE_SIZE[def]\r\n }\r\n }\r\n\r\n return buffer\r\n }\r\n}\r\n\r\n/**\r\n * BinaryPacket definition: \\\r\n * Any packet can be defined through a \"schema\" object explaining its fields names and types.\r\n *\r\n * @example\r\n * // Imagine we have a game board where each cell is a square and is one unit big.\r\n * // A cell can be then defined by its X and Y coordinates.\r\n * // For simplicity, let's say there cannot be more than 256 cells, so we can use 8 bits for each coordinate.\r\n * const Cell = {\r\n * x: Field.UNSIGNED_INT_8,\r\n * y: Field.UNSIGNED_INT_8\r\n * }\r\n *\r\n * // When done with the cell definition we can create its BinaryPacket writer/reader.\r\n * // NOTE: each BinaryPacket needs an unique ID, for identification purposes and error checking.\r\n * const CellPacket = BinaryPacket.define(0, Cell)\r\n *\r\n * // Let's now make the definition of the whole game board.\r\n * // You can also specify arrays of both \"primitive\" fields and other BinaryPackets.\r\n * const Board = {\r\n * numPlayers: Field.UNSIGNED_INT_8,\r\n * cells: FieldArray(CellPacket)\r\n * }\r\n *\r\n * // When done with the board definition we can create its BinaryPacket writer/reader.\r\n * // NOTE: each BinaryPacket needs an unique ID, for identification purposes and error checking.\r\n * const BoardPacket = BinaryPacket.define(1, Board)\r\n *\r\n * // And use it.\r\n * const buffer = BoardPacket.writeNodeBuffer({\r\n * numPlayers: 1,\r\n * cells: [\r\n * { x: 0, y: 0 },\r\n * { x: 1, y: 1 }\r\n * ]\r\n * })\r\n *\r\n * // sendTheBufferOver(buffer)\r\n * // ...\r\n * // const buffer = receiveTheBuffer()\r\n * const board = BoardPacket.readNodeBuffer(buffer)\r\n * // ...\r\n */\r\nexport type Definition = {\r\n [fieldName: string]: MaybeArray<Field> | MaybeArray<BinaryPacket<Definition>>\r\n}\r\n\r\ntype MaybeArray<T> = T | [itemType: T] | [itemType: T, length: number]\r\n\r\n/**\r\n * Meta-type that converts a `Definition` schema to the type of the actual JavaScript object that will be written into a packet or read from. \\\r\n */\r\ntype ToJson<T extends Definition> = {\r\n [K in keyof T]: T[K] extends [infer Item]\r\n ? Item extends BinaryPacket<infer BPDef>\r\n ? ToJson<BPDef>[]\r\n : number[]\r\n : T[K] extends [infer Item, infer Length]\r\n ? Item extends BinaryPacket<infer BPDef>\r\n ? ToJson<BPDef>[] & { length: Length }\r\n : number[] & { length: Length }\r\n : T[K] extends BinaryPacket<infer BPDef>\r\n ? ToJson<BPDef>\r\n : number\r\n}\r\n\r\n/**\r\n * In a JavaScript object, the order of its keys is not strictly defined: sort them by field name. \\\r\n * Thus, we cannot trust iterating over an object keys: we MUST iterate over its entries array. \\\r\n * This is important to make sure that whoever shares BinaryPacket definitions can correctly write/read packets independently of their JS engines.\r\n */\r\nfunction sortEntries(definition: Definition) {\r\n return Object.entries(definition).sort(([fieldName1], [fieldName2]) =>\r\n fieldName1.localeCompare(fieldName2)\r\n )\r\n}\r\n\r\ntype Entries = ReturnType<typeof sortEntries>\r\n\r\n/**\r\n * Helper function that \"inspects\" the entries of a BinaryPacket definition\r\n * and returns useful \"stats\" needed for writing and reading buffers.\r\n *\r\n * This function is ever called only once per BinaryPacket definition.\r\n */\r\nfunction inspectEntries(entries: Entries) {\r\n // The PacketID is already 1 byte, that's why we aren't starting from 0.\r\n let minimumByteLength = 1\r\n let canFastWrite = true\r\n\r\n for (const [, type] of entries) {\r\n if (Array.isArray(type)) {\r\n if (type.length === 2) {\r\n // Statically-sized array\r\n const itemSize =\r\n typeof type[0] === 'object' ? type[0].minimumByteLength : BYTE_SIZE[type[0]]\r\n\r\n minimumByteLength += type[1] * itemSize\r\n } else {\r\n // Dynamically-sized array\r\n // Adding 1 byte to serialize the array length\r\n minimumByteLength += 1\r\n canFastWrite = false\r\n }\r\n } else if (type instanceof BinaryPacket) {\r\n minimumByteLength += type.minimumByteLength\r\n canFastWrite &&= type.canFastWrite\r\n } else {\r\n minimumByteLength += BYTE_SIZE[type]\r\n }\r\n }\r\n\r\n return { minimumByteLength, canFastWrite }\r\n}\r\n\r\n//////////////////////////////////////////////\r\n// The logic here is practically over //\r\n// Here below there are needed constants //\r\n// that map a field-type to a functionality //\r\n//////////////////////////////////////////////\r\n\r\nconst BYTE_SIZE = Array(8) as number[]\r\n\r\nBYTE_SIZE[Field.UNSIGNED_INT_8] = 1\r\nBYTE_SIZE[Field.INT_8] = 1\r\n\r\nBYTE_SIZE[Field.UNSIGNED_INT_16] = 2\r\nBYTE_SIZE[Field.INT_16] = 2\r\n\r\nBYTE_SIZE[Field.UNSIGNED_INT_32] = 4\r\nBYTE_SIZE[Field.INT_32] = 4\r\nBYTE_SIZE[Field.FLOAT_32] = 4\r\n\r\nBYTE_SIZE[Field.FLOAT_64] = 8\r\n\r\nconst GET_FUNCTION = Array(8) as ((view: DataView, offset: number) => number)[]\r\n\r\nGET_FUNCTION[Field.UNSIGNED_INT_8] = (view, offset) => view.getUint8(offset)\r\nGET_FUNCTION[Field.INT_8] = (view, offset) => view.getInt8(offset)\r\n\r\nGET_FUNCTION[Field.UNSIGNED_INT_16] = (view, offset) => view.getUint16(offset)\r\nGET_FUNCTION[Field.INT_16] = (view, offset) => view.getInt16(offset)\r\n\r\nGET_FUNCTION[Field.UNSIGNED_INT_32] = (view, offset) => view.getUint32(offset)\r\nGET_FUNCTION[Field.INT_32] = (view, offset) => view.getInt32(offset)\r\nGET_FUNCTION[Field.FLOAT_32] = (view, offset) => view.getFloat32(offset)\r\n\r\nGET_FUNCTION[Field.FLOAT_64] = (view, offset) => view.getFloat64(offset)\r\n\r\nconst SET_FUNCTION = Array(8) as ((view: DataView, value: number, offset: number) => void)[]\r\n\r\nSET_FUNCTION[Field.UNSIGNED_INT_8] = (view, value, offset) => view.setUint8(offset, value)\r\nSET_FUNCTION[Field.INT_8] = (view, value, offset) => view.setInt8(offset, value)\r\n\r\nSET_FUNCTION[Field.UNSIGNED_INT_16] = (view, value, offset) => view.setUint16(offset, value)\r\nSET_FUNCTION[Field.INT_16] = (view, value, offset) => view.setInt16(offset, value)\r\n\r\nSET_FUNCTION[Field.UNSIGNED_INT_32] = (view, value, offset) => view.setUint32(offset, value)\r\nSET_FUNCTION[Field.INT_32] = (view, value, offset) => view.setInt32(offset, value)\r\nSET_FUNCTION[Field.FLOAT_32] = (view, value, offset) => view.setFloat32(offset, value)\r\n\r\nSET_FUNCTION[Field.FLOAT_64] = (view, value, offset) => view.setFloat64(offset, value)\r\n\r\nconst SET_FUNCTION_BUF = Array(8) as ((nodeBuffer: Buffer, value: number, offset: number) => void)[]\r\n\r\nif (hasNodeBuffers) {\r\n SET_FUNCTION_BUF[Field.UNSIGNED_INT_8] = (view, value, offset) => view.writeUint8(value, offset)\r\n SET_FUNCTION_BUF[Field.INT_8] = (view, value, offset) => view.writeInt8(value, offset)\r\n\r\n SET_FUNCTION_BUF[Field.UNSIGNED_INT_16] = (view, value, offset) =>\r\n view.writeUint16LE(value, offset)\r\n SET_FUNCTION_BUF[Field.INT_16] = (view, value, offset) => view.writeInt16LE(value, offset)\r\n\r\n SET_FUNCTION_BUF[Field.UNSIGNED_INT_32] = (view, value, offset) =>\r\n view.writeUint32LE(value, offset)\r\n SET_FUNCTION_BUF[Field.INT_32] = (view, value, offset) => view.writeInt32LE(value, offset)\r\n SET_FUNCTION_BUF[Field.FLOAT_32] = (view, value, offset) => view.writeFloatLE(value, offset)\r\n\r\n SET_FUNCTION_BUF[Field.FLOAT_64] = (view, value, offset) => view.writeDoubleLE(value, offset)\r\n}\r\n\r\nconst GET_FUNCTION_BUF = Array(8) as ((nodeBuffer: Buffer, offset: number) => number)[]\r\n\r\nif (hasNodeBuffers) {\r\n GET_FUNCTION_BUF[Field.UNSIGNED_INT_8] = (view, offset) => view.readUint8(offset)\r\n GET_FUNCTION_BUF[Field.INT_8] = (view, offset) => view.readInt8(offset)\r\n\r\n GET_FUNCTION_BUF[Field.UNSIGNED_INT_16] = (view, offset) => view.readUint16LE(offset)\r\n GET_FUNCTION_BUF[Field.INT_16] = (view, offset) => view.readInt16LE(offset)\r\n\r\n GET_FUNCTION_BUF[Field.UNSIGNED_INT_32] = (view, offset) => view.readUint32LE(offset)\r\n GET_FUNCTION_BUF[Field.INT_32] = (view, offset) => view.readInt32LE(offset)\r\n GET_FUNCTION_BUF[Field.FLOAT_32] = (view, offset) => view.readFloatLE(offset)\r\n\r\n GET_FUNCTION_BUF[Field.FLOAT_64] = (view, offset) => view.readDoubleLE(offset)\r\n}\r\n"],"mappings":";AAAO,IAAM,iBAAiB,OAAO,WAAW;AAEzC,SAAS,aAAa,UAAoB,eAAuB;AACtE,QAAM,gBAAgB,IAAI,YAAY,aAAa;AACnD,QAAM,eAAe,KAAK,IAAI,SAAS,YAAY,cAAc,UAAU;AAG3E,MAAI,SAAS,KAAK,MAAM,eAAe,CAAC;AACxC,MAAI,aAAa,eAAe,GAAG,MAAM,EAAE,IAAI,IAAI,aAAa,SAAS,QAAQ,GAAG,MAAM,CAAC;AAG3F,QAAM,SA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KAAK,cAAc,OAAO,MAAM;AAC9F;AAEA,IAAM,mBAAmB,MAAM,CAAC;AAEhC,IAAI,gBAAgB;AAClB,mBAAiB,sBAAoB,IAAI,CAAC,MAAM,WAAW,KAAK,UAAU,MAAM;AAChF,mBAAiB,aAAW,IAAI,CAAC,MAAM,WAAW,KAAK,SAAS,MAAM;AAEtE,mBAAiB,uBAAqB,IAAI,CAAC,MAAM,WAAW,KAAK,aAAa,MAAM;AACpF,mBAAiB,cAAY,IAAI,CAAC,MAAM,WAAW,KAAK,YAAY,MAAM;AAE1E,mBAAiB,uBAAqB,IAAI,CAAC,MAAM,WAAW,KAAK,aAAa,MAAM;AACpF,mBAAiB,cAAY,IAAI,CAAC,MAAM,WAAW,KAAK,YAAY,MAAM;AAC1E,mBAAiB,gBAAc,IAAI,CAAC,MAAM,WAAW,KAAK,YAAY,MAAM;AAE5E,mBAAiB,gBAAc,IAAI,CAAC,MAAM,WAAW,KAAK,aAAa,MAAM;AAC/E;","names":["Field"]}
|
|
1
|
+
{"version":3,"sources":["../src/buffers.ts","../src/index.ts"],"sourcesContent":["export const hasNodeBuffers = typeof Buffer === 'function'\r\n\r\nexport function growDataView(dataview: DataView, newByteLength: number) {\r\n const resizedBuffer = new ArrayBuffer(newByteLength)\r\n const amountToCopy = Math.min(dataview.byteLength, resizedBuffer.byteLength)\r\n\r\n // Treat the buffer as if it was a Float64Array so we can copy 8 bytes at a time, to finish faster\r\n let length = Math.trunc(amountToCopy / 8)\r\n new Float64Array(resizedBuffer, 0, length).set(new Float64Array(dataview.buffer, 0, length))\r\n\r\n // Copy the remaining up to 7 bytes\r\n const offset = length * 8\r\n length = amountToCopy - offset\r\n new Uint8Array(resizedBuffer, offset, length).set(new Uint8Array(dataview.buffer, offset, length))\r\n\r\n return new DataView(resizedBuffer)\r\n}\r\n\r\nexport function growNodeBuffer(buffer: Buffer, newByteLength: number) {\r\n const newBuffer = Buffer.allocUnsafe(newByteLength)\r\n buffer.copy(newBuffer)\r\n return newBuffer\r\n}\r\n","import { growDataView, growNodeBuffer, hasNodeBuffers } from './buffers'\r\n\r\nexport const enum Field {\r\n /**\r\n * Defines a 1 byte (8 bits) unsigned integer field. \\\r\n * (Range: 0 - 255)\r\n */\r\n UNSIGNED_INT_8 = 0,\r\n\r\n /**\r\n * Defines a 2 bytes (16 bits) unsigned integer field. \\\r\n * (Range: 0 - 65535)\r\n */\r\n UNSIGNED_INT_16,\r\n\r\n /**\r\n * Defines a 4 bytes (32 bits) unsigned integer field. \\\r\n * (Range: 0 - 4294967295)\r\n */\r\n UNSIGNED_INT_32,\r\n\r\n /**\r\n * Defines a 1 byte (8 bits) signed integer field. \\\r\n * (Range: -128 - 127)\r\n */\r\n INT_8,\r\n\r\n /**\r\n * Defines a 2 bytes (16 bits) signed integer field. \\\r\n * (Range: -32768 - 32767)\r\n */\r\n INT_16,\r\n\r\n /**\r\n * Defines a 4 bytes (32 bits) signed integer field. \\\r\n * (Range: -2147483648 - 2147483647)\r\n */\r\n INT_32,\r\n\r\n /**\r\n * Defines a 4 bytes (32 bits) floating-point field. \\\r\n */\r\n FLOAT_32,\r\n\r\n /**\r\n * Defines a 8 bytes (64 bits) floating-point field. \\\r\n */\r\n FLOAT_64\r\n}\r\n\r\n/**\r\n * Defines a dynamically-sized array with elements of a certain type. \\\r\n * Dynamically-sized arrays are useful when a packet's field is an array of a non pre-defined length. \\\r\n * Although, this makes dynamically-sized arrays more memory expensive as the internal buffer needs to be grown accordingly.\r\n *\r\n * NOTE: If an array will ALWAYS have the same length, prefer using the `FieldFixedArray` type, for both better performance and memory efficiency. \\\r\n * NOTE: As of now, dynamic arrays can have at most 256 elements.\r\n */\r\nexport function FieldArray<T extends Field | BinaryPacket<Definition>>(item: T): [itemType: T] {\r\n return [item]\r\n}\r\n\r\n/**\r\n * Defines a statically-sized array with elements of a certain type. \\\r\n * Fixed arrays are useful when a packet's field is an array of a pre-defined length. \\\r\n * Fixed arrays much more memory efficient and performant than non-fixed ones.\r\n *\r\n * NOTE: If an array will not always have the same length, use the `FieldArray` type.\r\n */\r\nexport function FieldFixedArray<T extends Field | BinaryPacket<Definition>, Length extends number>(\r\n item: T,\r\n length: Length\r\n): [itemType: T, length: Length] {\r\n if (length < 0 || !Number.isFinite(length)) {\r\n throw new RangeError('Length of a FixedArray must be a positive integer.')\r\n }\r\n\r\n return [item, length]\r\n}\r\n\r\nexport class BinaryPacket<T extends Definition> {\r\n /**\r\n * Defines a new binary packet. \\\r\n * Make sure that every `packetId` is unique.\r\n * @throws RangeError If packetId is negative, floating-point, or greater than 255.\r\n */\r\n static define<T extends Definition>(packetId: number, definition?: T) {\r\n if (packetId < 0 || !Number.isFinite(packetId)) {\r\n throw new RangeError('Packet IDs must be positive integers.')\r\n }\r\n\r\n if (packetId > 255) {\r\n throw new RangeError(\r\n 'Packet IDs greater than 255 are not supported. Do you REALLY need more than 255 different kinds of packets?'\r\n )\r\n }\r\n\r\n return new BinaryPacket(packetId, definition)\r\n }\r\n\r\n /**\r\n * Reads just the packetId from the given Buffer. \\\r\n * This method practically just reads the uint8 at offset `byteOffset` (default: 0). \\\r\n * Useful if the receiving side receives multiple types of packets.\r\n */\r\n static readPacketIdNodeBuffer(buffer: Buffer, byteOffset = 0) {\r\n return buffer.readUint8(byteOffset)\r\n }\r\n\r\n /**\r\n * Reads just the packetId from the given DataView. \\\r\n * This method practically just reads the uint8 at offset `byteOffset` (default: 0). \\\r\n * Useful if the receiving side receives multiple types of packets.\r\n */\r\n static readPacketIdDataView(dataview: DataView, byteOffset = 0) {\r\n return dataview.getUint8(byteOffset)\r\n }\r\n\r\n /**\r\n * Reads just the packetId from the given ArrayBuffer. \\\r\n * This method practically just reads the uint8 at offset `byteOffset`. \\\r\n * Useful if the receiving side receives multiple types of packets.\r\n *\r\n * NOTE: Due to security issues, the `byteOffset` argument cannot be defaulted and must be provided by the user. \\\r\n * NOTE: For more information read the `readArrayBuffer` method documentation.\r\n */\r\n static readPacketIdArrayBuffer(arraybuffer: ArrayBuffer, byteOffset: number) {\r\n return new Uint8Array(arraybuffer, byteOffset, 1)[0]\r\n }\r\n\r\n /**\r\n * Reads/deserializes from the given Buffer. \\\r\n * Method available ONLY on NodeJS and Bun.\r\n *\r\n * If possible, always prefer reading using this method, as it is much faster than the other ones.\r\n *\r\n * NOTE: if you have an ArrayBuffer do not bother wrapping it into a node Buffer yourself. \\\r\n * NOTE: if you have an ArrayBuffer use the appropriate `readArrayBuffer`.\r\n */\r\n readNodeBuffer(\r\n dataIn: Buffer,\r\n offsetPointer = { offset: 0 },\r\n byteLength = dataIn.byteLength\r\n ): ToJson<T> {\r\n return this.read(dataIn, offsetPointer, byteLength, GET_FUNCTION_BUF)\r\n }\r\n\r\n /**\r\n * Reads/deserializes from the given DataView.\r\n *\r\n * NOTE: if you have an ArrayBuffer do not bother wrapping it into a DataView yourself. \\\r\n * NOTE: if you have an ArrayBuffer use the appropriate `readArrayBuffer`.\r\n */\r\n readDataView(\r\n dataIn: DataView,\r\n offsetPointer = { offset: 0 },\r\n byteLength = dataIn.byteLength\r\n ): ToJson<T> {\r\n return this.read(dataIn, offsetPointer, byteLength, GET_FUNCTION)\r\n }\r\n\r\n /**\r\n * Reads/deserializes from the given ArrayBuffer. \\\r\n * WARNING: this method is practically a HACK.\r\n *\r\n * When using this method both the `byteOffset` and `byteLength` are REQUIRED and cannot be defaulted. \\\r\n * This is to prevent serious bugs and security issues. \\\r\n * That is because often raw ArrayBuffers come from a pre-allocated buffer pool and do not start at byteOffset 0.\r\n *\r\n * NOTE: if you have a node Buffer do not bother wrapping it into an ArrayBuffer yourself. \\\r\n * NOTE: if you have a node Buffer use the appropriate `readNodeBuffer` as it is much faster and less error prone.\r\n */\r\n readArrayBuffer(\r\n dataIn: ArrayBuffer & { buffer?: undefined },\r\n byteOffset: number,\r\n byteLength: number\r\n ) {\r\n return this.read(\r\n hasNodeBuffers\r\n ? Buffer.from(dataIn, byteOffset, byteLength)\r\n : new DataView(dataIn, byteOffset, byteLength),\r\n { offset: 0 }, // The underlying buffer has already been offsetted\r\n byteLength,\r\n hasNodeBuffers ? GET_FUNCTION_BUF : GET_FUNCTION\r\n )\r\n }\r\n\r\n /**\r\n * Writes/serializes the given object into a Buffer. \\\r\n * Method available ONLY on NodeJS and Bun.\r\n *\r\n * If possible, always prefer writing using this method, as it is much faster than the other ones.\r\n */\r\n writeNodeBuffer(dataOut: ToJson<T>) {\r\n const buffer = Buffer.allocUnsafe(this.minimumByteLength)\r\n return this.write(buffer, dataOut, { offset: 0 }, SET_FUNCTION_BUF, growNodeBuffer)\r\n }\r\n\r\n /**\r\n * Writes/serializes the given object into a DataView. \\\r\n */\r\n writeDataView(dataOut: ToJson<T>) {\r\n const dataview = new DataView(new ArrayBuffer(this.minimumByteLength))\r\n return this.write(dataview, dataOut, { offset: 0 }, SET_FUNCTION, growDataView)\r\n }\r\n\r\n /**\r\n * Writes/serializes the given object into an ArrayBuffer. \\\r\n * This method is just a wrapper around either `writeNodeBuffer` or `writeDataView`. \\\r\n *\r\n * This method works with JavaScript standard raw ArrayBuffer(s) and, as such, is very error prone: \\\r\n * Make sure you're using the returned byteLength and byteOffset fields in the read counterpart. \\\r\n *\r\n * Always consider whether is possible to use directly `writeNodeBuffer` or `writeDataView` instead of `writeArrayBuffer`. \\\r\n * For more information read the `readArrayBuffer` documentation.\r\n */\r\n writeArrayBuffer(dataOut: ToJson<T>) {\r\n const buf = hasNodeBuffers ? this.writeNodeBuffer(dataOut) : this.writeDataView(dataOut)\r\n return { buffer: buf.buffer, byteLength: buf.byteLength, byteOffset: buf.byteOffset }\r\n }\r\n\r\n private readonly entries: Entries\r\n readonly canFastWrite: boolean\r\n readonly minimumByteLength: number\r\n\r\n private constructor(\r\n private readonly packetId: number,\r\n definition?: T\r\n ) {\r\n this.entries = definition ? sortEntries(definition) : []\r\n const inspection = inspectEntries(this.entries)\r\n\r\n this.minimumByteLength = inspection.minimumByteLength\r\n this.canFastWrite = inspection.canFastWrite\r\n }\r\n\r\n private read(\r\n dataIn: DataView | Buffer,\r\n offsetPointer: { offset: number },\r\n byteLength: number,\r\n readFunctions: typeof GET_FUNCTION | typeof GET_FUNCTION_BUF\r\n ): ToJson<T> {\r\n if (byteLength + offsetPointer.offset < this.minimumByteLength) {\r\n throw new Error(\r\n `There is no space available to fit a packet of type ${this.packetId} at offset ${offsetPointer.offset}`\r\n )\r\n }\r\n\r\n if (\r\n readFunctions[Field.UNSIGNED_INT_8](dataIn as any, offsetPointer.offset) !== this.packetId\r\n ) {\r\n throw new Error(\r\n `Data at offset ${offsetPointer.offset} is not a packet of type ${this.packetId}`\r\n )\r\n }\r\n\r\n offsetPointer.offset += 1\r\n const result: any = {}\r\n\r\n for (const [name, def] of this.entries) {\r\n if (Array.isArray(def)) {\r\n const length =\r\n // def[1] is the length of a statically-sized array, if undefined: must read the length from the buffer as it means it's a dynamically-sized array\r\n def[1] ?? readFunctions[Field.UNSIGNED_INT_8](dataIn as any, offsetPointer.offset++)\r\n\r\n const array = Array(length)\r\n\r\n const itemType = def[0]\r\n\r\n if (typeof itemType === 'object') {\r\n // Array of \"subpackets\"\r\n for (let i = 0; i < length; ++i) {\r\n array[i] = itemType.read(dataIn, offsetPointer, byteLength, readFunctions)\r\n }\r\n } else {\r\n // Array of primitives (numbers)\r\n const itemSize = BYTE_SIZE[itemType]\r\n\r\n // It seems like looping over each element is actually much faster than using TypedArrays bulk copy.\r\n // TODO: properly benchmark with various array sizes to see if it's actually the case.\r\n for (let i = 0; i < length; ++i) {\r\n array[i] = readFunctions[itemType](dataIn as any, offsetPointer.offset)\r\n offsetPointer.offset += itemSize\r\n }\r\n }\r\n\r\n // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access\r\n result[name] = array\r\n } else if (typeof def === 'object') {\r\n // Single \"subpacket\"\r\n // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access\r\n result[name] = def.read(dataIn, offsetPointer, byteLength, readFunctions)\r\n } else {\r\n // Single primitive (number)\r\n // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access\r\n result[name] = readFunctions[def](dataIn as any, offsetPointer.offset)\r\n offsetPointer.offset += BYTE_SIZE[def]\r\n }\r\n }\r\n\r\n return result as ToJson<T>\r\n }\r\n\r\n private write<Buf extends DataView | Buffer>(\r\n buffer: Buf,\r\n dataOut: ToJson<T>,\r\n offsetPointer: { offset: number },\r\n writeFunctions: typeof SET_FUNCTION | typeof SET_FUNCTION_BUF,\r\n growBufferFunction: (buffer: Buf, newByteLength: number) => Buf\r\n ): Buf {\r\n writeFunctions[Field.UNSIGNED_INT_8](buffer as any, this.packetId, offsetPointer.offset)\r\n offsetPointer.offset += 1\r\n\r\n if (this.canFastWrite) {\r\n // If there are no arrays, the minimumByteLength always equals to the full needed byteLength.\r\n // So we can take the fast path, since we know beforehand that the buffer isn't going to grow.\r\n this.fastWrite(buffer, dataOut, offsetPointer, writeFunctions)\r\n return buffer\r\n } else {\r\n // If non-empty arrays are encountered, the buffer must grow.\r\n // If every array is empty, the speed of this path is comparable to the fast path.\r\n return this.slowWrite(\r\n buffer,\r\n dataOut,\r\n offsetPointer,\r\n this.minimumByteLength,\r\n this.minimumByteLength,\r\n writeFunctions,\r\n growBufferFunction\r\n )\r\n }\r\n }\r\n\r\n /**\r\n * Fast write does not support writing dynamically-sized arrays.\r\n */\r\n private fastWrite<Buf extends DataView | Buffer>(\r\n buffer: Buf,\r\n dataOut: ToJson<T>,\r\n offsetPointer: { offset: number },\r\n writeFunctions: typeof SET_FUNCTION | typeof SET_FUNCTION_BUF\r\n ) {\r\n for (const [name, def] of this.entries) {\r\n if (Array.isArray(def)) {\r\n // Statically-sized array\r\n const itemType = def[0]\r\n const length = def[1]!\r\n const data = dataOut[name] as any[]\r\n\r\n if (typeof itemType === 'object') {\r\n for (let i = 0; i < length; ++i) {\r\n itemType.fastWrite(buffer, data[i] as ToJson<Definition>, offsetPointer, writeFunctions)\r\n }\r\n } else {\r\n const itemSize = BYTE_SIZE[itemType]\r\n\r\n for (let i = 0; i < length; ++i) {\r\n writeFunctions[itemType](buffer as any, data[i] as number, offsetPointer.offset)\r\n offsetPointer.offset += itemSize\r\n }\r\n }\r\n } else if (typeof def === 'object') {\r\n // Single \"subpacket\"\r\n // In fastWrite there cannot be arrays, but the cast is needed because TypeScript can't possibly know that.\r\n def.fastWrite(buffer, dataOut[name] as ToJson<Definition>, offsetPointer, writeFunctions)\r\n } else {\r\n // Single primitive (number)\r\n writeFunctions[def](buffer as any, dataOut[name] as number, offsetPointer.offset)\r\n offsetPointer.offset += BYTE_SIZE[def]\r\n }\r\n }\r\n }\r\n\r\n /**\r\n * The slow writing path tries writing data into the buffer as fast as the fast writing path does. \\\r\n * But, if a non-empty dynamically-sized array is encountered, the buffer needs to grow, slightly reducing performance.\r\n */\r\n private slowWrite<Buf extends DataView | Buffer>(\r\n buffer: Buf,\r\n dataOut: ToJson<T>,\r\n offsetPointer: { offset: number },\r\n byteLength: number,\r\n maxByteLength: number,\r\n writeFunctions: typeof SET_FUNCTION | typeof SET_FUNCTION_BUF,\r\n growBufferFunction: (buffer: Buf, newByteLength: number) => Buf\r\n ): Buf {\r\n for (const [name, def] of this.entries) {\r\n const data = dataOut[name]\r\n\r\n if (Array.isArray(def)) {\r\n // Could be both an array of just numbers or \"subpackets\"\r\n\r\n const length = (data as any[]).length\r\n const isDynamicArray = def[1] === undefined\r\n\r\n // Check if it is a dynamically-sized array, if it is, the length of the array must be serialized in the buffer before its elements\r\n // Explicitly check for undefined and not falsy values because it could be a statically-sized array of 0 elements.\r\n if (isDynamicArray) {\r\n writeFunctions[Field.UNSIGNED_INT_8](buffer as any, length, offsetPointer.offset)\r\n offsetPointer.offset += 1\r\n }\r\n\r\n if (length > 0) {\r\n const itemType = def[0]\r\n\r\n if (typeof itemType === 'object') {\r\n // Array of \"subpackets\"\r\n\r\n if (isDynamicArray) {\r\n const neededBytesForElements = length * itemType.minimumByteLength\r\n\r\n byteLength += neededBytesForElements\r\n maxByteLength += neededBytesForElements\r\n\r\n if (buffer.byteLength < maxByteLength) {\r\n buffer = growBufferFunction(buffer, maxByteLength)\r\n }\r\n }\r\n\r\n for (const object of data as unknown as ToJson<Definition>[]) {\r\n writeFunctions[Field.UNSIGNED_INT_8](\r\n buffer as any,\r\n itemType.packetId,\r\n offsetPointer.offset\r\n )\r\n\r\n offsetPointer.offset += 1\r\n\r\n buffer = itemType.slowWrite(\r\n buffer,\r\n object,\r\n offsetPointer,\r\n byteLength,\r\n maxByteLength,\r\n writeFunctions,\r\n growBufferFunction\r\n )\r\n\r\n byteLength = offsetPointer.offset\r\n maxByteLength = buffer.byteLength\r\n }\r\n } else {\r\n // Array of primitives (numbers)\r\n const itemSize = BYTE_SIZE[itemType]\r\n\r\n if (isDynamicArray) {\r\n const neededBytesForElements = length * itemSize\r\n\r\n byteLength += neededBytesForElements\r\n maxByteLength += neededBytesForElements\r\n\r\n if (buffer.byteLength < maxByteLength) {\r\n buffer = growBufferFunction(buffer, maxByteLength)\r\n }\r\n }\r\n\r\n // It seems like looping over each element is actually much faster than using TypedArrays bulk copy.\r\n // TODO: properly benchmark with various array sizes to see if it's actually the case.\r\n for (const number of data as number[]) {\r\n writeFunctions[itemType](buffer as any, number, offsetPointer.offset)\r\n offsetPointer.offset += itemSize\r\n }\r\n }\r\n }\r\n } else if (typeof def === 'object') {\r\n // Single \"subpacket\"\r\n writeFunctions[Field.UNSIGNED_INT_8](buffer as any, def.packetId, offsetPointer.offset)\r\n offsetPointer.offset += 1\r\n\r\n buffer = def.slowWrite(\r\n buffer,\r\n data as ToJson<Definition>,\r\n offsetPointer,\r\n byteLength,\r\n maxByteLength,\r\n writeFunctions,\r\n growBufferFunction\r\n )\r\n\r\n byteLength = offsetPointer.offset\r\n maxByteLength = buffer.byteLength\r\n } else {\r\n // Single primitive (number)\r\n writeFunctions[def](buffer as any, data as number, offsetPointer.offset)\r\n offsetPointer.offset += BYTE_SIZE[def]\r\n }\r\n }\r\n\r\n return buffer\r\n }\r\n}\r\n\r\n/**\r\n * BinaryPacket definition: \\\r\n * Any packet can be defined through a \"schema\" object explaining its fields names and types.\r\n *\r\n * @example\r\n * // Imagine we have a game board where each cell is a square and is one unit big.\r\n * // A cell can be then defined by its X and Y coordinates.\r\n * // For simplicity, let's say there cannot be more than 256 cells, so we can use 8 bits for each coordinate.\r\n * const Cell = {\r\n * x: Field.UNSIGNED_INT_8,\r\n * y: Field.UNSIGNED_INT_8\r\n * }\r\n *\r\n * // When done with the cell definition we can create its BinaryPacket writer/reader.\r\n * // NOTE: each BinaryPacket needs an unique ID, for identification purposes and error checking.\r\n * const CellPacket = BinaryPacket.define(0, Cell)\r\n *\r\n * // Let's now make the definition of the whole game board.\r\n * // You can also specify arrays of both \"primitive\" fields and other BinaryPackets.\r\n * const Board = {\r\n * numPlayers: Field.UNSIGNED_INT_8,\r\n * cells: FieldArray(CellPacket)\r\n * }\r\n *\r\n * // When done with the board definition we can create its BinaryPacket writer/reader.\r\n * // NOTE: each BinaryPacket needs an unique ID, for identification purposes and error checking.\r\n * const BoardPacket = BinaryPacket.define(1, Board)\r\n *\r\n * // And use it.\r\n * const buffer = BoardPacket.writeNodeBuffer({\r\n * numPlayers: 1,\r\n * cells: [\r\n * { x: 0, y: 0 },\r\n * { x: 1, y: 1 }\r\n * ]\r\n * })\r\n *\r\n * // sendTheBufferOver(buffer)\r\n * // ...\r\n * // const buffer = receiveTheBuffer()\r\n * const board = BoardPacket.readNodeBuffer(buffer)\r\n * // ...\r\n */\r\nexport type Definition = {\r\n [fieldName: string]: MaybeArray<Field> | MaybeArray<BinaryPacket<Definition>>\r\n}\r\n\r\ntype MaybeArray<T> = T | [itemType: T] | [itemType: T, length: number]\r\n\r\n/**\r\n * Meta-type that converts a `Definition` schema to the type of the actual JavaScript object that will be written into a packet or read from. \\\r\n */\r\ntype ToJson<T extends Definition> = {\r\n [K in keyof T]: T[K] extends [infer Item]\r\n ? Item extends BinaryPacket<infer BPDef>\r\n ? ToJson<BPDef>[]\r\n : number[]\r\n : T[K] extends [infer Item, infer Length]\r\n ? Item extends BinaryPacket<infer BPDef>\r\n ? ToJson<BPDef>[] & { length: Length }\r\n : number[] & { length: Length }\r\n : T[K] extends BinaryPacket<infer BPDef>\r\n ? ToJson<BPDef>\r\n : number\r\n}\r\n\r\n/**\r\n * In a JavaScript object, the order of its keys is not strictly defined: sort them by field name. \\\r\n * Thus, we cannot trust iterating over an object keys: we MUST iterate over its entries array. \\\r\n * This is important to make sure that whoever shares BinaryPacket definitions can correctly write/read packets independently of their JS engines.\r\n */\r\nfunction sortEntries(definition: Definition) {\r\n return Object.entries(definition).sort(([fieldName1], [fieldName2]) =>\r\n fieldName1.localeCompare(fieldName2)\r\n )\r\n}\r\n\r\ntype Entries = ReturnType<typeof sortEntries>\r\n\r\n/**\r\n * Helper function that \"inspects\" the entries of a BinaryPacket definition\r\n * and returns useful \"stats\" needed for writing and reading buffers.\r\n *\r\n * This function is ever called only once per BinaryPacket definition.\r\n */\r\nfunction inspectEntries(entries: Entries) {\r\n // The PacketID is already 1 byte, that's why we aren't starting from 0.\r\n let minimumByteLength = 1\r\n let canFastWrite = true\r\n\r\n for (const [, type] of entries) {\r\n if (Array.isArray(type)) {\r\n if (type.length === 2) {\r\n // Statically-sized array\r\n const itemSize =\r\n typeof type[0] === 'object' ? type[0].minimumByteLength : BYTE_SIZE[type[0]]\r\n\r\n minimumByteLength += type[1] * itemSize\r\n } else {\r\n // Dynamically-sized array\r\n // Adding 1 byte to serialize the array length\r\n minimumByteLength += 1\r\n canFastWrite = false\r\n }\r\n } else if (type instanceof BinaryPacket) {\r\n minimumByteLength += type.minimumByteLength\r\n canFastWrite &&= type.canFastWrite\r\n } else {\r\n minimumByteLength += BYTE_SIZE[type]\r\n }\r\n }\r\n\r\n return { minimumByteLength, canFastWrite }\r\n}\r\n\r\n//////////////////////////////////////////////\r\n// The logic here is practically over //\r\n// Here below there are needed constants //\r\n// that map a field-type to a functionality //\r\n//////////////////////////////////////////////\r\n\r\nconst BYTE_SIZE = Array(8) as number[]\r\n\r\nBYTE_SIZE[Field.UNSIGNED_INT_8] = 1\r\nBYTE_SIZE[Field.INT_8] = 1\r\n\r\nBYTE_SIZE[Field.UNSIGNED_INT_16] = 2\r\nBYTE_SIZE[Field.INT_16] = 2\r\n\r\nBYTE_SIZE[Field.UNSIGNED_INT_32] = 4\r\nBYTE_SIZE[Field.INT_32] = 4\r\nBYTE_SIZE[Field.FLOAT_32] = 4\r\n\r\nBYTE_SIZE[Field.FLOAT_64] = 8\r\n\r\nconst GET_FUNCTION = Array(8) as ((view: DataView, offset: number) => number)[]\r\n\r\nGET_FUNCTION[Field.UNSIGNED_INT_8] = (view, offset) => view.getUint8(offset)\r\nGET_FUNCTION[Field.INT_8] = (view, offset) => view.getInt8(offset)\r\n\r\nGET_FUNCTION[Field.UNSIGNED_INT_16] = (view, offset) => view.getUint16(offset)\r\nGET_FUNCTION[Field.INT_16] = (view, offset) => view.getInt16(offset)\r\n\r\nGET_FUNCTION[Field.UNSIGNED_INT_32] = (view, offset) => view.getUint32(offset)\r\nGET_FUNCTION[Field.INT_32] = (view, offset) => view.getInt32(offset)\r\nGET_FUNCTION[Field.FLOAT_32] = (view, offset) => view.getFloat32(offset)\r\n\r\nGET_FUNCTION[Field.FLOAT_64] = (view, offset) => view.getFloat64(offset)\r\n\r\nconst SET_FUNCTION = Array(8) as ((view: DataView, value: number, offset: number) => void)[]\r\n\r\nSET_FUNCTION[Field.UNSIGNED_INT_8] = (view, value, offset) => view.setUint8(offset, value)\r\nSET_FUNCTION[Field.INT_8] = (view, value, offset) => view.setInt8(offset, value)\r\n\r\nSET_FUNCTION[Field.UNSIGNED_INT_16] = (view, value, offset) => view.setUint16(offset, value)\r\nSET_FUNCTION[Field.INT_16] = (view, value, offset) => view.setInt16(offset, value)\r\n\r\nSET_FUNCTION[Field.UNSIGNED_INT_32] = (view, value, offset) => view.setUint32(offset, value)\r\nSET_FUNCTION[Field.INT_32] = (view, value, offset) => view.setInt32(offset, value)\r\nSET_FUNCTION[Field.FLOAT_32] = (view, value, offset) => view.setFloat32(offset, value)\r\n\r\nSET_FUNCTION[Field.FLOAT_64] = (view, value, offset) => view.setFloat64(offset, value)\r\n\r\nconst SET_FUNCTION_BUF = Array(8) as ((nodeBuffer: Buffer, value: number, offset: number) => void)[]\r\n\r\nif (hasNodeBuffers) {\r\n SET_FUNCTION_BUF[Field.UNSIGNED_INT_8] = (view, value, offset) => view.writeUint8(value, offset)\r\n SET_FUNCTION_BUF[Field.INT_8] = (view, value, offset) => view.writeInt8(value, offset)\r\n\r\n SET_FUNCTION_BUF[Field.UNSIGNED_INT_16] = (view, value, offset) =>\r\n view.writeUint16LE(value, offset)\r\n SET_FUNCTION_BUF[Field.INT_16] = (view, value, offset) => view.writeInt16LE(value, offset)\r\n\r\n SET_FUNCTION_BUF[Field.UNSIGNED_INT_32] = (view, value, offset) =>\r\n view.writeUint32LE(value, offset)\r\n SET_FUNCTION_BUF[Field.INT_32] = (view, value, offset) => view.writeInt32LE(value, offset)\r\n SET_FUNCTION_BUF[Field.FLOAT_32] = (view, value, offset) => view.writeFloatLE(value, offset)\r\n\r\n SET_FUNCTION_BUF[Field.FLOAT_64] = (view, value, offset) => view.writeDoubleLE(value, offset)\r\n}\r\n\r\nconst GET_FUNCTION_BUF = Array(8) as ((nodeBuffer: Buffer, offset: number) => number)[]\r\n\r\nif (hasNodeBuffers) {\r\n GET_FUNCTION_BUF[Field.UNSIGNED_INT_8] = (view, offset) => view.readUint8(offset)\r\n GET_FUNCTION_BUF[Field.INT_8] = (view, offset) => view.readInt8(offset)\r\n\r\n GET_FUNCTION_BUF[Field.UNSIGNED_INT_16] = (view, offset) => view.readUint16LE(offset)\r\n GET_FUNCTION_BUF[Field.INT_16] = (view, offset) => view.readInt16LE(offset)\r\n\r\n GET_FUNCTION_BUF[Field.UNSIGNED_INT_32] = (view, offset) => view.readUint32LE(offset)\r\n GET_FUNCTION_BUF[Field.INT_32] = (view, offset) => view.readInt32LE(offset)\r\n GET_FUNCTION_BUF[Field.FLOAT_32] = (view, offset) => view.readFloatLE(offset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eBuffers","growDataView","dataview","newByteLength","resizedBuffer","amountToCopy","length","offset","growNodeBuffer","buffer","newBuffer","Field","FieldArray","item","FieldFixedArray","length","BinaryPacket","_BinaryPacket","packetId","definition","sortEntries","inspection","inspectEntries","buffer","byteOffset","dataview","arraybuffer","dataIn","offsetPointer","byteLength","GET_FUNCTION_BUF","GET_FUNCTION","hasNodeBuffers","dataOut","SET_FUNCTION_BUF","growNodeBuffer","SET_FUNCTION","growDataView","buf","readFunctions","result","name","def","array","itemType","i","itemSize","BYTE_SIZE","writeFunctions","growBufferFunction","data","maxByteLength","isDynamicArray","neededBytesForElements","object","number","fieldName1","fieldName2","entries","minimumByteLength","canFastWrite","type","view","offset","value"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "binary-packet",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"description": "Lightweight and hyper-fast, zero-dependencies, TypeScript-first, schema-based binary packets serialization and deserialization library",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|