binary-packet 1.0.2 → 1.0.4
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.d.mts +25 -4
- package/dist/index.d.ts +25 -4
- package/dist/index.js +30 -3
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +30 -3
- package/dist/index.mjs.map +1 -1
- package/package.json +6 -3
package/dist/index.d.mts
CHANGED
|
@@ -51,10 +51,27 @@ declare class BinaryPacket<T extends Definition> {
|
|
|
51
51
|
* @throws RangeError If packetId is negative, floating-point, or greater than 255.
|
|
52
52
|
*/
|
|
53
53
|
static define<T extends Definition>(packetId: number, definition?: T): BinaryPacket<T>;
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
54
|
+
/**
|
|
55
|
+
* Reads just the packetId from the given Buffer. \
|
|
56
|
+
* This method practically just reads the uint8 at offset `byteOffset` (default: 0). \
|
|
57
|
+
* Useful if the receiving side receives multiple types of packets.
|
|
58
|
+
*/
|
|
59
|
+
static readPacketIdNodeBuffer(buffer: Buffer, byteOffset?: number): number;
|
|
60
|
+
/**
|
|
61
|
+
* Reads just the packetId from the given DataView. \
|
|
62
|
+
* This method practically just reads the uint8 at offset `byteOffset` (default: 0). \
|
|
63
|
+
* Useful if the receiving side receives multiple types of packets.
|
|
64
|
+
*/
|
|
65
|
+
static readPacketIdDataView(dataview: DataView, byteOffset?: number): number;
|
|
66
|
+
/**
|
|
67
|
+
* Reads just the packetId from the given ArrayBuffer. \
|
|
68
|
+
* This method practically just reads the uint8 at offset `byteOffset`. \
|
|
69
|
+
* Useful if the receiving side receives multiple types of packets.
|
|
70
|
+
*
|
|
71
|
+
* NOTE: Due to security issues, the `byteOffset` argument cannot be defaulted and must be provided by the user. \
|
|
72
|
+
* NOTE: For more information read the `readArrayBuffer` method documentation.
|
|
73
|
+
*/
|
|
74
|
+
static readPacketIdArrayBuffer(arraybuffer: ArrayBuffer, byteOffset: number): number;
|
|
58
75
|
/**
|
|
59
76
|
* Reads/deserializes from the given Buffer. \
|
|
60
77
|
* Method available ONLY on NodeJS and Bun.
|
|
@@ -116,6 +133,10 @@ declare class BinaryPacket<T extends Definition> {
|
|
|
116
133
|
byteLength: number;
|
|
117
134
|
byteOffset: number;
|
|
118
135
|
};
|
|
136
|
+
private readonly entries;
|
|
137
|
+
readonly canFastWrite: boolean;
|
|
138
|
+
readonly minimumByteLength: number;
|
|
139
|
+
private constructor();
|
|
119
140
|
private read;
|
|
120
141
|
private write;
|
|
121
142
|
private fastWrite;
|
package/dist/index.d.ts
CHANGED
|
@@ -51,10 +51,27 @@ declare class BinaryPacket<T extends Definition> {
|
|
|
51
51
|
* @throws RangeError If packetId is negative, floating-point, or greater than 255.
|
|
52
52
|
*/
|
|
53
53
|
static define<T extends Definition>(packetId: number, definition?: T): BinaryPacket<T>;
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
54
|
+
/**
|
|
55
|
+
* Reads just the packetId from the given Buffer. \
|
|
56
|
+
* This method practically just reads the uint8 at offset `byteOffset` (default: 0). \
|
|
57
|
+
* Useful if the receiving side receives multiple types of packets.
|
|
58
|
+
*/
|
|
59
|
+
static readPacketIdNodeBuffer(buffer: Buffer, byteOffset?: number): number;
|
|
60
|
+
/**
|
|
61
|
+
* Reads just the packetId from the given DataView. \
|
|
62
|
+
* This method practically just reads the uint8 at offset `byteOffset` (default: 0). \
|
|
63
|
+
* Useful if the receiving side receives multiple types of packets.
|
|
64
|
+
*/
|
|
65
|
+
static readPacketIdDataView(dataview: DataView, byteOffset?: number): number;
|
|
66
|
+
/**
|
|
67
|
+
* Reads just the packetId from the given ArrayBuffer. \
|
|
68
|
+
* This method practically just reads the uint8 at offset `byteOffset`. \
|
|
69
|
+
* Useful if the receiving side receives multiple types of packets.
|
|
70
|
+
*
|
|
71
|
+
* NOTE: Due to security issues, the `byteOffset` argument cannot be defaulted and must be provided by the user. \
|
|
72
|
+
* NOTE: For more information read the `readArrayBuffer` method documentation.
|
|
73
|
+
*/
|
|
74
|
+
static readPacketIdArrayBuffer(arraybuffer: ArrayBuffer, byteOffset: number): number;
|
|
58
75
|
/**
|
|
59
76
|
* Reads/deserializes from the given Buffer. \
|
|
60
77
|
* Method available ONLY on NodeJS and Bun.
|
|
@@ -116,6 +133,10 @@ declare class BinaryPacket<T extends Definition> {
|
|
|
116
133
|
byteLength: number;
|
|
117
134
|
byteOffset: number;
|
|
118
135
|
};
|
|
136
|
+
private readonly entries;
|
|
137
|
+
readonly canFastWrite: boolean;
|
|
138
|
+
readonly minimumByteLength: number;
|
|
139
|
+
private constructor();
|
|
119
140
|
private read;
|
|
120
141
|
private write;
|
|
121
142
|
private fastWrite;
|
package/dist/index.js
CHANGED
|
@@ -83,9 +83,33 @@ var BinaryPacket = class _BinaryPacket {
|
|
|
83
83
|
}
|
|
84
84
|
return new _BinaryPacket(packetId, definition);
|
|
85
85
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
86
|
+
/**
|
|
87
|
+
* Reads just the packetId from the given Buffer. \
|
|
88
|
+
* This method practically just reads the uint8 at offset `byteOffset` (default: 0). \
|
|
89
|
+
* Useful if the receiving side receives multiple types of packets.
|
|
90
|
+
*/
|
|
91
|
+
static readPacketIdNodeBuffer(buffer, byteOffset = 0) {
|
|
92
|
+
return buffer.readUint8(byteOffset);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Reads just the packetId from the given DataView. \
|
|
96
|
+
* This method practically just reads the uint8 at offset `byteOffset` (default: 0). \
|
|
97
|
+
* Useful if the receiving side receives multiple types of packets.
|
|
98
|
+
*/
|
|
99
|
+
static readPacketIdDataView(dataview, byteOffset = 0) {
|
|
100
|
+
return dataview.getUint8(byteOffset);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Reads just the packetId from the given ArrayBuffer. \
|
|
104
|
+
* This method practically just reads the uint8 at offset `byteOffset`. \
|
|
105
|
+
* Useful if the receiving side receives multiple types of packets.
|
|
106
|
+
*
|
|
107
|
+
* NOTE: Due to security issues, the `byteOffset` argument cannot be defaulted and must be provided by the user. \
|
|
108
|
+
* NOTE: For more information read the `readArrayBuffer` method documentation.
|
|
109
|
+
*/
|
|
110
|
+
static readPacketIdArrayBuffer(arraybuffer, byteOffset) {
|
|
111
|
+
return new Uint8Array(arraybuffer, byteOffset, 1)[0];
|
|
112
|
+
}
|
|
89
113
|
/**
|
|
90
114
|
* Reads/deserializes from the given Buffer. \
|
|
91
115
|
* Method available ONLY on NodeJS and Bun.
|
|
@@ -158,6 +182,9 @@ var BinaryPacket = class _BinaryPacket {
|
|
|
158
182
|
const buf = hasNodeBuffers ? this.writeNodeBuffer(dataOut) : this.writeDataView(dataOut);
|
|
159
183
|
return { buffer: buf.buffer, byteLength: buf.byteLength, byteOffset: buf.byteOffset };
|
|
160
184
|
}
|
|
185
|
+
entries;
|
|
186
|
+
canFastWrite;
|
|
187
|
+
minimumByteLength;
|
|
161
188
|
read(dataIn, offsetPointer, byteLength, readFunctions) {
|
|
162
189
|
if (byteLength + offsetPointer.offset < this.minimumByteLength) {
|
|
163
190
|
throw new Error(
|
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 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,EAAA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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 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 /**\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 = 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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B,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"]}
|
package/dist/index.mjs
CHANGED
|
@@ -55,9 +55,33 @@ var BinaryPacket = class _BinaryPacket {
|
|
|
55
55
|
}
|
|
56
56
|
return new _BinaryPacket(packetId, definition);
|
|
57
57
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
58
|
+
/**
|
|
59
|
+
* Reads just the packetId from the given Buffer. \
|
|
60
|
+
* This method practically just reads the uint8 at offset `byteOffset` (default: 0). \
|
|
61
|
+
* Useful if the receiving side receives multiple types of packets.
|
|
62
|
+
*/
|
|
63
|
+
static readPacketIdNodeBuffer(buffer, byteOffset = 0) {
|
|
64
|
+
return buffer.readUint8(byteOffset);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Reads just the packetId from the given DataView. \
|
|
68
|
+
* This method practically just reads the uint8 at offset `byteOffset` (default: 0). \
|
|
69
|
+
* Useful if the receiving side receives multiple types of packets.
|
|
70
|
+
*/
|
|
71
|
+
static readPacketIdDataView(dataview, byteOffset = 0) {
|
|
72
|
+
return dataview.getUint8(byteOffset);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Reads just the packetId from the given ArrayBuffer. \
|
|
76
|
+
* This method practically just reads the uint8 at offset `byteOffset`. \
|
|
77
|
+
* Useful if the receiving side receives multiple types of packets.
|
|
78
|
+
*
|
|
79
|
+
* NOTE: Due to security issues, the `byteOffset` argument cannot be defaulted and must be provided by the user. \
|
|
80
|
+
* NOTE: For more information read the `readArrayBuffer` method documentation.
|
|
81
|
+
*/
|
|
82
|
+
static readPacketIdArrayBuffer(arraybuffer, byteOffset) {
|
|
83
|
+
return new Uint8Array(arraybuffer, byteOffset, 1)[0];
|
|
84
|
+
}
|
|
61
85
|
/**
|
|
62
86
|
* Reads/deserializes from the given Buffer. \
|
|
63
87
|
* Method available ONLY on NodeJS and Bun.
|
|
@@ -130,6 +154,9 @@ var BinaryPacket = class _BinaryPacket {
|
|
|
130
154
|
const buf = hasNodeBuffers ? this.writeNodeBuffer(dataOut) : this.writeDataView(dataOut);
|
|
131
155
|
return { buffer: buf.buffer, byteLength: buf.byteLength, byteOffset: buf.byteOffset };
|
|
132
156
|
}
|
|
157
|
+
entries;
|
|
158
|
+
canFastWrite;
|
|
159
|
+
minimumByteLength;
|
|
133
160
|
read(dataIn, offsetPointer, byteLength, readFunctions) {
|
|
134
161
|
if (byteLength + offsetPointer.offset < this.minimumByteLength) {
|
|
135
162
|
throw new Error(
|
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 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"],"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,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;;;ACpBO,IAAW,QAAX,kBAAWA,WAAX;AAKL,EAAAA,cAAA,oBAAiB,KAAjB;AAMA,EAAAA,cAAA;AAMA,EAAAA,cAAA;AAMA,EAAAA,cAAA;AAMA,EA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AI,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 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 /**\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 = 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"],"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,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;;;ACpBO,IAAW,QAAX,kBAAWA,WAAX;AAKL,EAAAA,cAAA,oBAAiB,KAAjB;AAMA,EAAAA,cAAA;AAMA,EAAAA,cAAA;AAMA,EAAAA,cAAA;AAMA,EAAAA,cAAA;AAMA,EAAAA,cAAA;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AK,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"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "binary-packet",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
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",
|
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
"scripts": {
|
|
12
12
|
"build": "tsup",
|
|
13
13
|
"test": "node -r ts-node/register/transpile-only src/tests/reads.test.ts && node -r ts-node/register/transpile-only src/tests/writes.test.ts",
|
|
14
|
-
"benchmark": "node -r ts-node/register/transpile-only src/tests/benchmark.test.ts"
|
|
14
|
+
"benchmark": "node -r ts-node/register/transpile-only src/tests/benchmark.test.ts",
|
|
15
|
+
"lint": "eslint"
|
|
15
16
|
},
|
|
16
17
|
"keywords": [
|
|
17
18
|
"binary-packet",
|
|
@@ -45,13 +46,15 @@
|
|
|
45
46
|
"license": "Apache-2.0",
|
|
46
47
|
"devDependencies": {
|
|
47
48
|
"colors": "^1.4.0",
|
|
49
|
+
"eslint": "^9.12.0",
|
|
48
50
|
"msgpackr": "^1.11.0",
|
|
49
51
|
"prettier": "^3.3.3",
|
|
50
52
|
"prettier-plugin-organize-imports": "^4.1.0",
|
|
51
53
|
"restructure": "^3.0.2",
|
|
52
54
|
"ts-node": "^10.9.2",
|
|
53
55
|
"tsup": "^8.3.0",
|
|
54
|
-
"typescript": "^5.6.2"
|
|
56
|
+
"typescript": "^5.6.2",
|
|
57
|
+
"typescript-eslint": "^8.8.1"
|
|
55
58
|
},
|
|
56
59
|
"engines": {
|
|
57
60
|
"node": ">=16"
|