binary-packet 1.0.0

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 ADDED
@@ -0,0 +1,385 @@
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
+ });
27
+ module.exports = __toCommonJS(src_exports);
28
+
29
+ // src/buffers.ts
30
+ var hasNodeBuffers = typeof Buffer === "function";
31
+ function growDataView(dataview, newByteLength) {
32
+ const resizedBuffer = new ArrayBuffer(newByteLength);
33
+ const amountToCopy = Math.min(dataview.byteLength, resizedBuffer.byteLength);
34
+ let length = Math.trunc(amountToCopy / 8);
35
+ new Float64Array(resizedBuffer, 0, length).set(new Float64Array(dataview.buffer, 0, length));
36
+ const offset = length * 8;
37
+ length = amountToCopy - offset;
38
+ new Uint8Array(resizedBuffer, offset, length).set(new Uint8Array(dataview.buffer, offset, length));
39
+ return new DataView(resizedBuffer);
40
+ }
41
+ function growNodeBuffer(buffer, newByteLength) {
42
+ const newBuffer = Buffer.allocUnsafe(newByteLength);
43
+ buffer.copy(newBuffer);
44
+ return newBuffer;
45
+ }
46
+
47
+ // src/index.ts
48
+ var Field = /* @__PURE__ */ ((Field2) => {
49
+ Field2[Field2["UNSIGNED_INT_8"] = 0] = "UNSIGNED_INT_8";
50
+ Field2[Field2["UNSIGNED_INT_16"] = 1] = "UNSIGNED_INT_16";
51
+ Field2[Field2["UNSIGNED_INT_32"] = 2] = "UNSIGNED_INT_32";
52
+ Field2[Field2["INT_8"] = 3] = "INT_8";
53
+ Field2[Field2["INT_16"] = 4] = "INT_16";
54
+ Field2[Field2["INT_32"] = 5] = "INT_32";
55
+ Field2[Field2["FLOAT_32"] = 6] = "FLOAT_32";
56
+ Field2[Field2["FLOAT_64"] = 7] = "FLOAT_64";
57
+ return Field2;
58
+ })(Field || {});
59
+ function FieldArray(item) {
60
+ return [item];
61
+ }
62
+ var BinaryPacket = class _BinaryPacket {
63
+ constructor(packetId, definition) {
64
+ this.packetId = packetId;
65
+ this.entries = definition ? sortEntries(definition) : [];
66
+ const inspection = inspectEntries(this.entries);
67
+ this.minimumByteLength = inspection.minimumByteLength;
68
+ this.canFastWrite = inspection.canFastWrite;
69
+ }
70
+ /**
71
+ * Defines a new binary packet. \
72
+ * Make sure that every `packetId` is unique.
73
+ * @throws RangeError If packetId is negative, floating-point, or greater than 255.
74
+ */
75
+ static define(packetId, definition) {
76
+ if (packetId < 0 || !Number.isFinite(packetId)) {
77
+ throw new RangeError("Packet IDs must be positive integers.");
78
+ }
79
+ if (packetId > 255) {
80
+ throw new RangeError(
81
+ "Packet IDs greater than 255 are not supported. Do you REALLY need more than 255 different kinds of packets?"
82
+ );
83
+ }
84
+ return new _BinaryPacket(packetId, definition);
85
+ }
86
+ entries;
87
+ canFastWrite;
88
+ minimumByteLength;
89
+ /**
90
+ * Reads/deserializes from the given Buffer. \
91
+ * Method available ONLY on NodeJS and Bun.
92
+ *
93
+ * If possible, always prefer reading using this method, as it is much faster than the other ones.
94
+ *
95
+ * NOTE: if you have an ArrayBuffer do not bother wrapping it into a node Buffer yourself. \
96
+ * NOTE: if you have an ArrayBuffer use the appropriate `readArrayBuffer`.
97
+ */
98
+ readNodeBuffer(dataIn, offsetPointer = { offset: 0 }, byteLength = dataIn.byteLength) {
99
+ return this.read(dataIn, offsetPointer, byteLength, GET_FUNCTION_BUF);
100
+ }
101
+ /**
102
+ * Reads/deserializes from the given DataView.
103
+ *
104
+ * NOTE: if you have an ArrayBuffer do not bother wrapping it into a DataView yourself. \
105
+ * NOTE: if you have an ArrayBuffer use the appropriate `readArrayBuffer`.
106
+ */
107
+ readDataView(dataIn, offsetPointer = { offset: 0 }, byteLength = dataIn.byteLength) {
108
+ return this.read(dataIn, offsetPointer, byteLength, GET_FUNCTION);
109
+ }
110
+ /**
111
+ * Reads/deserializes from the given ArrayBuffer. \
112
+ * WARNING: this method is practically a HACK.
113
+ *
114
+ * When using this method both the `byteOffset` and `byteLength` are REQUIRED and cannot be defaulted. \
115
+ * This is to prevent serious bugs and security issues. \
116
+ * That is because often raw ArrayBuffers come from a pre-allocated buffer pool and do not start at byteOffset 0.
117
+ *
118
+ * NOTE: if you have a node Buffer do not bother wrapping it into an ArrayBuffer yourself. \
119
+ * NOTE: if you have a node Buffer use the appropriate `readNodeBuffer` as it is much faster and less error prone.
120
+ */
121
+ readArrayBuffer(dataIn, byteOffset, byteLength) {
122
+ return this.read(
123
+ hasNodeBuffers ? Buffer.from(dataIn, byteOffset, byteLength) : new DataView(dataIn, byteOffset, byteLength),
124
+ { offset: 0 },
125
+ // The underlying buffer has already been offsetted
126
+ byteLength,
127
+ hasNodeBuffers ? GET_FUNCTION_BUF : GET_FUNCTION
128
+ );
129
+ }
130
+ /**
131
+ * Writes/serializes the given object into a Buffer. \
132
+ * Method available ONLY on NodeJS and Bun.
133
+ *
134
+ * If possible, always prefer writing using this method, as it is much faster than the other ones.
135
+ */
136
+ writeNodeBuffer(dataOut) {
137
+ const buffer = Buffer.allocUnsafe(this.minimumByteLength);
138
+ return this.write(buffer, dataOut, { offset: 0 }, SET_FUNCTION_BUF, growNodeBuffer);
139
+ }
140
+ /**
141
+ * Writes/serializes the given object into a DataView. \
142
+ */
143
+ writeDataView(dataOut) {
144
+ const dataview = new DataView(new ArrayBuffer(this.minimumByteLength));
145
+ return this.write(dataview, dataOut, { offset: 0 }, SET_FUNCTION, growDataView);
146
+ }
147
+ /**
148
+ * Writes/serializes the given object into an ArrayBuffer. \
149
+ * This method is just a wrapper around either `writeNodeBuffer` or `writeDataView`. \
150
+ *
151
+ * This method works with JavaScript standard raw ArrayBuffer(s) and, as such, is very error prone: \
152
+ * Make sure you're using the returned byteLength and byteOffset fields in the read counterpart. \
153
+ *
154
+ * Always consider whether is possible to use directly `writeNodeBuffer` or `writeDataView` instead of `writeArrayBuffer`. \
155
+ * For more information read the `readArrayBuffer` documentation.
156
+ */
157
+ writeArrayBuffer(dataOut) {
158
+ const buf = hasNodeBuffers ? this.writeNodeBuffer(dataOut) : this.writeDataView(dataOut);
159
+ return { buffer: buf.buffer, byteLength: buf.byteLength, byteOffset: buf.byteOffset };
160
+ }
161
+ read(dataIn, offsetPointer, byteLength, readFunctions) {
162
+ if (byteLength + offsetPointer.offset < this.minimumByteLength) {
163
+ throw new Error(
164
+ `There is no space available to fit a packet of type ${this.packetId} at offset ${offsetPointer.offset}`
165
+ );
166
+ }
167
+ if (readFunctions[0 /* UNSIGNED_INT_8 */](dataIn, offsetPointer.offset) !== this.packetId) {
168
+ throw new Error(
169
+ `Data at offset ${offsetPointer.offset} is not a packet of type ${this.packetId}`
170
+ );
171
+ }
172
+ offsetPointer.offset += 1;
173
+ const result = {};
174
+ for (const [name, def] of this.entries) {
175
+ if (Array.isArray(def)) {
176
+ const length = readFunctions[0 /* UNSIGNED_INT_8 */](dataIn, offsetPointer.offset++);
177
+ const array = Array(length);
178
+ const itemType = def[0];
179
+ if (typeof itemType === "object") {
180
+ for (let i = 0; i < length; ++i) {
181
+ array[i] = itemType.read(dataIn, offsetPointer, byteLength, readFunctions);
182
+ }
183
+ } else {
184
+ const itemSize = BYTE_SIZE[itemType];
185
+ for (let i = 0; i < length; ++i) {
186
+ array[i] = readFunctions[itemType](dataIn, offsetPointer.offset);
187
+ offsetPointer.offset += itemSize;
188
+ }
189
+ }
190
+ result[name] = array;
191
+ } else if (typeof def === "object") {
192
+ result[name] = def.read(dataIn, offsetPointer, byteLength, readFunctions);
193
+ } else {
194
+ result[name] = readFunctions[def](dataIn, offsetPointer.offset);
195
+ offsetPointer.offset += BYTE_SIZE[def];
196
+ }
197
+ }
198
+ return result;
199
+ }
200
+ write(buffer, dataOut, offsetPointer, writeFunctions, growBufferFunction) {
201
+ writeFunctions[0 /* UNSIGNED_INT_8 */](buffer, this.packetId, offsetPointer.offset);
202
+ offsetPointer.offset += 1;
203
+ if (this.canFastWrite) {
204
+ this.fastWrite(buffer, dataOut, offsetPointer, writeFunctions);
205
+ return buffer;
206
+ } else {
207
+ return this.slowWrite(
208
+ buffer,
209
+ dataOut,
210
+ offsetPointer,
211
+ this.minimumByteLength,
212
+ this.minimumByteLength,
213
+ writeFunctions,
214
+ growBufferFunction
215
+ );
216
+ }
217
+ }
218
+ fastWrite(buffer, dataOut, offsetPointer, writeFunctions) {
219
+ for (const [name, def] of this.entries) {
220
+ if (typeof def === "object") {
221
+ ;
222
+ def.fastWrite(
223
+ buffer,
224
+ dataOut[name],
225
+ offsetPointer,
226
+ writeFunctions
227
+ );
228
+ } else {
229
+ writeFunctions[def](buffer, dataOut[name], offsetPointer.offset);
230
+ offsetPointer.offset += BYTE_SIZE[def];
231
+ }
232
+ }
233
+ }
234
+ /**
235
+ * The slow writing path tries writing data into the buffer as fast as the fast writing path does. \
236
+ * But, if a non-empty array is encountered, the buffer needs to grow, slightly reducing performance.
237
+ */
238
+ slowWrite(buffer, dataOut, offsetPointer, byteLength, maxByteLength, writeFunctions, growBufferFunction) {
239
+ for (const [name, def] of this.entries) {
240
+ const data = dataOut[name];
241
+ if (Array.isArray(def)) {
242
+ const length = data.length;
243
+ writeFunctions[0 /* UNSIGNED_INT_8 */](buffer, length, offsetPointer.offset);
244
+ offsetPointer.offset += 1;
245
+ if (length > 0) {
246
+ const itemType = def[0];
247
+ if (typeof itemType === "object") {
248
+ const neededBytesForElements = length * itemType.minimumByteLength;
249
+ byteLength += neededBytesForElements;
250
+ maxByteLength += neededBytesForElements;
251
+ if (buffer.byteLength < maxByteLength) {
252
+ buffer = growBufferFunction(buffer, maxByteLength);
253
+ }
254
+ for (const object of data) {
255
+ writeFunctions[0 /* UNSIGNED_INT_8 */](
256
+ buffer,
257
+ itemType.packetId,
258
+ offsetPointer.offset
259
+ );
260
+ offsetPointer.offset += 1;
261
+ buffer = itemType.slowWrite(
262
+ buffer,
263
+ object,
264
+ offsetPointer,
265
+ byteLength,
266
+ maxByteLength,
267
+ writeFunctions,
268
+ growBufferFunction
269
+ );
270
+ byteLength = offsetPointer.offset;
271
+ maxByteLength = buffer.byteLength;
272
+ }
273
+ } else {
274
+ const itemSize = BYTE_SIZE[itemType];
275
+ const neededBytesForElements = length * itemSize;
276
+ byteLength += neededBytesForElements;
277
+ maxByteLength += neededBytesForElements;
278
+ if (buffer.byteLength < maxByteLength) {
279
+ buffer = growBufferFunction(buffer, maxByteLength);
280
+ }
281
+ for (const number of data) {
282
+ writeFunctions[itemType](buffer, number, offsetPointer.offset);
283
+ offsetPointer.offset += itemSize;
284
+ }
285
+ }
286
+ }
287
+ } else if (typeof def === "object") {
288
+ writeFunctions[0 /* UNSIGNED_INT_8 */](buffer, def.packetId, offsetPointer.offset);
289
+ offsetPointer.offset += 1;
290
+ buffer = def.slowWrite(
291
+ buffer,
292
+ data,
293
+ offsetPointer,
294
+ byteLength,
295
+ maxByteLength,
296
+ writeFunctions,
297
+ growBufferFunction
298
+ );
299
+ byteLength = offsetPointer.offset;
300
+ maxByteLength = buffer.byteLength;
301
+ } else {
302
+ writeFunctions[def](buffer, data, offsetPointer.offset);
303
+ offsetPointer.offset += BYTE_SIZE[def];
304
+ }
305
+ }
306
+ return buffer;
307
+ }
308
+ };
309
+ function sortEntries(definition) {
310
+ return Object.entries(definition).sort(
311
+ ([fieldName1], [fieldName2]) => fieldName1.localeCompare(fieldName2)
312
+ );
313
+ }
314
+ function inspectEntries(entries) {
315
+ let minimumByteLength = 1;
316
+ let canFastWrite = true;
317
+ for (const [, type] of entries) {
318
+ if (Array.isArray(type)) {
319
+ minimumByteLength += 1;
320
+ canFastWrite = false;
321
+ } else if (type instanceof BinaryPacket) {
322
+ minimumByteLength += type.minimumByteLength;
323
+ canFastWrite &&= type.canFastWrite;
324
+ } else {
325
+ minimumByteLength += BYTE_SIZE[type];
326
+ }
327
+ }
328
+ return { minimumByteLength, canFastWrite };
329
+ }
330
+ var BYTE_SIZE = Array(8);
331
+ BYTE_SIZE[0 /* UNSIGNED_INT_8 */] = 1;
332
+ BYTE_SIZE[3 /* INT_8 */] = 1;
333
+ BYTE_SIZE[1 /* UNSIGNED_INT_16 */] = 2;
334
+ BYTE_SIZE[4 /* INT_16 */] = 2;
335
+ BYTE_SIZE[2 /* UNSIGNED_INT_32 */] = 4;
336
+ BYTE_SIZE[5 /* INT_32 */] = 4;
337
+ BYTE_SIZE[6 /* FLOAT_32 */] = 4;
338
+ BYTE_SIZE[7 /* FLOAT_64 */] = 8;
339
+ var GET_FUNCTION = Array(8);
340
+ GET_FUNCTION[0 /* UNSIGNED_INT_8 */] = (view, offset) => view.getUint8(offset);
341
+ GET_FUNCTION[3 /* INT_8 */] = (view, offset) => view.getInt8(offset);
342
+ GET_FUNCTION[1 /* UNSIGNED_INT_16 */] = (view, offset, le) => view.getUint16(offset, le);
343
+ GET_FUNCTION[4 /* INT_16 */] = (view, offset, le) => view.getInt16(offset, le);
344
+ GET_FUNCTION[2 /* UNSIGNED_INT_32 */] = (view, offset, le) => view.getUint32(offset, le);
345
+ GET_FUNCTION[5 /* INT_32 */] = (view, offset, le) => view.getInt32(offset, le);
346
+ GET_FUNCTION[6 /* FLOAT_32 */] = (view, offset, le) => view.getFloat32(offset, le);
347
+ GET_FUNCTION[7 /* FLOAT_64 */] = (view, offset, le) => view.getFloat64(offset, le);
348
+ var SET_FUNCTION = Array(8);
349
+ SET_FUNCTION[0 /* UNSIGNED_INT_8 */] = (view, value, offset) => view.setUint8(offset, value);
350
+ SET_FUNCTION[3 /* INT_8 */] = (view, value, offset) => view.setInt8(offset, value);
351
+ SET_FUNCTION[1 /* UNSIGNED_INT_16 */] = (view, value, offset) => view.setUint16(offset, value);
352
+ SET_FUNCTION[4 /* INT_16 */] = (view, value, offset) => view.setInt16(offset, value);
353
+ SET_FUNCTION[2 /* UNSIGNED_INT_32 */] = (view, value, offset) => view.setUint32(offset, value);
354
+ SET_FUNCTION[5 /* INT_32 */] = (view, value, offset) => view.setInt32(offset, value);
355
+ SET_FUNCTION[6 /* FLOAT_32 */] = (view, value, offset) => view.setFloat32(offset, value);
356
+ SET_FUNCTION[7 /* FLOAT_64 */] = (view, value, offset) => view.setFloat64(offset, value);
357
+ var SET_FUNCTION_BUF = Array(8);
358
+ if (hasNodeBuffers) {
359
+ SET_FUNCTION_BUF[0 /* UNSIGNED_INT_8 */] = (view, value, offset) => view.writeUint8(value, offset);
360
+ SET_FUNCTION_BUF[3 /* INT_8 */] = (view, value, offset) => view.writeInt8(value, offset);
361
+ SET_FUNCTION_BUF[1 /* UNSIGNED_INT_16 */] = (view, value, offset) => view.writeUint16LE(value, offset);
362
+ SET_FUNCTION_BUF[4 /* INT_16 */] = (view, value, offset) => view.writeInt16LE(value, offset);
363
+ SET_FUNCTION_BUF[2 /* UNSIGNED_INT_32 */] = (view, value, offset) => view.writeUint32LE(value, offset);
364
+ SET_FUNCTION_BUF[5 /* INT_32 */] = (view, value, offset) => view.writeInt32LE(value, offset);
365
+ SET_FUNCTION_BUF[6 /* FLOAT_32 */] = (view, value, offset) => view.writeFloatLE(value, offset);
366
+ SET_FUNCTION_BUF[7 /* FLOAT_64 */] = (view, value, offset) => view.writeDoubleLE(value, offset);
367
+ }
368
+ var GET_FUNCTION_BUF = Array(8);
369
+ if (hasNodeBuffers) {
370
+ GET_FUNCTION_BUF[0 /* UNSIGNED_INT_8 */] = (view, offset) => view.readUint8(offset);
371
+ GET_FUNCTION_BUF[3 /* INT_8 */] = (view, offset) => view.readInt8(offset);
372
+ GET_FUNCTION_BUF[1 /* UNSIGNED_INT_16 */] = (view, offset) => view.readUint16LE(offset);
373
+ GET_FUNCTION_BUF[4 /* INT_16 */] = (view, offset) => view.readInt16LE(offset);
374
+ GET_FUNCTION_BUF[2 /* UNSIGNED_INT_32 */] = (view, offset) => view.readUint32LE(offset);
375
+ GET_FUNCTION_BUF[5 /* INT_32 */] = (view, offset) => view.readInt32LE(offset);
376
+ GET_FUNCTION_BUF[6 /* FLOAT_32 */] = (view, offset) => view.readFloatLE(offset);
377
+ GET_FUNCTION_BUF[7 /* FLOAT_64 */] = (view, offset) => view.readDoubleLE(offset);
378
+ }
379
+ // Annotate the CommonJS export names for ESM import in node:
380
+ 0 && (module.exports = {
381
+ BinaryPacket,
382
+ Field,
383
+ FieldArray
384
+ });
385
+ //# sourceMappingURL=index.js.map
@@ -0,0 +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 an array of a certain type. \\\r\n * As of now, only arrays with at most 256 elements are supported.\r\n */\r\nexport function FieldArray<T extends Field | BinaryPacket<Definition>>(item: T) {\r\n return [item]\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 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 /**\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 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 = readFunctions[Field.UNSIGNED_INT_8](dataIn as any, offsetPointer.offset++)\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 result[name] = array\r\n } else if (typeof def === 'object') {\r\n // Single \"subpacket\"\r\n result[name] = def.read(dataIn, offsetPointer, byteLength, readFunctions)\r\n } else {\r\n // Single primitive (number)\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 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 (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 as BinaryPacket<Definition>).fastWrite(\r\n buffer,\r\n dataOut[name] as ToJson<Definition>,\r\n offsetPointer,\r\n writeFunctions\r\n )\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 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 const length = (data as any[]).length\r\n\r\n writeFunctions[Field.UNSIGNED_INT_8](buffer as any, length, offsetPointer.offset)\r\n offsetPointer.offset += 1\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 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 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 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 // 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) // equivalent to { cells: [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]: Field | Field[] | BinaryPacket<Definition> | BinaryPacket<Definition>[]\r\n}\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 ReadonlyArray<infer Item>\r\n ? Item extends BinaryPacket<infer BPDef>\r\n ? ToJson<BPDef>[]\r\n : number[]\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 // Adding 1 byte to serialize the array length\r\n minimumByteLength += 1\r\n canFastWrite = false\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)\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: ((view: DataView, offset: number, littleEndian?: boolean) => number)[] =\r\n Array(8)\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, le) => view.getUint16(offset, le)\r\nGET_FUNCTION[Field.INT_16] = (view, offset, le) => view.getInt16(offset, le)\r\n\r\nGET_FUNCTION[Field.UNSIGNED_INT_32] = (view, offset, le) => view.getUint32(offset, le)\r\nGET_FUNCTION[Field.INT_32] = (view, offset, le) => view.getInt32(offset, le)\r\nGET_FUNCTION[Field.FLOAT_32] = (view, offset, le) => view.getFloat32(offset, le)\r\n\r\nGET_FUNCTION[Field.FLOAT_64] = (view, offset, le) => view.getFloat64(offset, le)\r\n\r\nconst SET_FUNCTION: ((view: DataView, value: number, offset: number) => void)[] = Array(8)\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: ((nodeBuffer: Buffer, value: number, offset: number) => void)[] = Array(8)\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: ((nodeBuffer: Buffer, offset: number) => number)[] = Array(8)\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;;;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;AAEjG,SAAO,IAAI,SAAS,aAAa;AACnC;AAEO,SAAS,eAAe,QAAgB,eAAuB;AACpE,QAAM,YAAY,OAAO,YAAY,aAAa;AAClD,SAAO,KAAK,SAAS;AACrB,SAAO;AACT;;;ADpBO,IAAW,QAAX,kBAAWA,WAAX;AAKL,EAAAA,cAAA,oBAAiB,KAAjB;AAMA,EAAAA,cAAA;AAMA,EAAAA,cAAA;AAMA,EAAAA,cAAA;AAMA,EAAAA,cAAA;AAMA,EAAAA,cAAA;AAKA,EAAAA,cAAA;AAKA,EAAAA,cAAA;AA7CgB,SAAAA;AAAA,GAAA;AA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