@jsprismarine/binaryutils 0.20.0-unstable-20261110112121 → 0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -2
- package/dist/BinaryStream.d.ts +22 -0
- package/dist/BinaryStream.d.ts.map +1 -1
- package/dist/BinaryStream.js +32 -0
- package/dist/BinaryStream.js.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
# @jsprismarine/binaryutils
|
|
2
2
|
|
|
3
|
-
## 0.20.0
|
|
3
|
+
## 0.20.0
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#2944](https://github.com/JSPrismarine/JSPrismarine/pull/2944) [`f030c63`](https://github.com/JSPrismarine/JSPrismarine/commit/f030c63e5c6269288d8dc576bddde160eda3cc74) Thanks [@filiphsps](https://github.com/filiphsps)! - `BinaryStream` reads and writes varuint32-length-prefixed values: `readLengthPrefixed` and `writeLengthPrefixed` for bytes, and `readVarString` and `writeVarString` for UTF-8 strings, the form Bedrock writes strings in. `NetworkBinaryStream` in `@jsprismarine/protocol` now inherits them instead of defining its own, so a package that needs Bedrock strings without the protocol package can use them.
|
|
6
8
|
|
|
7
9
|
## 0.19.2
|
|
8
10
|
|
package/dist/BinaryStream.d.ts
CHANGED
|
@@ -59,6 +59,28 @@ export declare class BinaryStream {
|
|
|
59
59
|
* @param value - The string to encode.
|
|
60
60
|
*/
|
|
61
61
|
writeUTF8String(value: string): void;
|
|
62
|
+
/**
|
|
63
|
+
* Reads a varuint32 byte length, then that many bytes.
|
|
64
|
+
* @returns The bytes, a view into the stream's buffer.
|
|
65
|
+
*/
|
|
66
|
+
readLengthPrefixed(): Buffer;
|
|
67
|
+
/**
|
|
68
|
+
* Writes a varuint32 byte length, then the bytes.
|
|
69
|
+
* @param buf - The bytes to append.
|
|
70
|
+
*/
|
|
71
|
+
writeLengthPrefixed(buf: Uint8Array): void;
|
|
72
|
+
/**
|
|
73
|
+
* Reads a string: a varuint32 byte length, then UTF-8 bytes, the form Bedrock writes strings in.
|
|
74
|
+
* Named apart from `readString` because RakNet's packets read theirs with a u16 length.
|
|
75
|
+
* {@link https://mojang.github.io/bedrock-protocol-docs/1.26.51/primitives/#string | The docs' string primitive}
|
|
76
|
+
* @returns The read string.
|
|
77
|
+
*/
|
|
78
|
+
readVarString(): string;
|
|
79
|
+
/**
|
|
80
|
+
* Writes a string as a varuint32 byte length, then UTF-8 bytes.
|
|
81
|
+
* @param str - The string to write.
|
|
82
|
+
*/
|
|
83
|
+
writeVarString(str: string): void;
|
|
62
84
|
/**
|
|
63
85
|
* Reads an unsigned byte (0 to 255).
|
|
64
86
|
* @returns {number}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BinaryStream.d.ts","sources":["../src/BinaryStream.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,qBAAa,YAAY;IACrB,OAAO,CAAC,WAAW,CAAuB;IAC1C,OAAO,CAAC,UAAU,CAAuB;IACzC,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,UAAU,CAAK;IACvB,OAAO,CAAC,aAAa,CAAK;IAE1B;;;;;;;OAOG;IACH,YAAmB,MAAM,CAAC,EAAE,MAAM,EAAE,MAAM,GAAE,MAAU,EAAE,eAAe,GAAE,MAAU,EAMlF;IAED;;;OAGG;IACI,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAG/B;IAED;;;OAGG;IACI,KAAK,CAAC,GAAG,EAAE,UAAU,GAAG,IAAI,CASlC;IAED;;;OAGG;IACI,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAU1C;IAED;;;OAGG;IACI,QAAQ,IAAI,MAAM,CAGxB;IAED;;;OAGG;IACI,SAAS,CAAC,CAAC,EAAE,MAAM,GAAG,IAAI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S,IAAI,MAAM,CAEzB;IAED;;;OAGG;IACI,aAAa,IAAI,MAAM,GAAG,IAAI,CAEpC;IAED;;;;;;;;;;;OAWG;IACI,cAAc,IAAI,MAAM,CAE9B;IAED;;;;;OAKG;IACI,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAElC;IAED;;;;OAIG;IACI,aAAa,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,SAAI,GAAG,IAAI,CAGlD;IAED;;;;OAIG;IACI,cAAc,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,SAAI,GAAG,IAAI,CAInD;IAED;;;;;OAKG;IACI,KAAK,IAAI,IAAI,CAInB;IAED;;;;;;;OAOG;IACI,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAI9B;IAED;;;OAGG;IACI,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAGvC;IAED;;;OAGG;IACI,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAGxC;IAED;;;OAGG;IACI,YAAY,IAAI,MAAM,CAE5B;IAED;;;OAGG;IACI,aAAa,IAAI,MAAM,CAE7B;IAED;;;OAGG;IACH,OAAO,CAAC,gBAAgB;IAkBxB;;;;;OAKG;IACH,OAAO,CAAC,iBAAiB;CAa5B;AAGD,eAAe,YAAY,CAAC"}
|
package/dist/BinaryStream.js
CHANGED
|
@@ -80,6 +80,38 @@ var BinaryStream = class {
|
|
|
80
80
|
this.writeIndex += bytesWritten;
|
|
81
81
|
}
|
|
82
82
|
/**
|
|
83
|
+
* Reads a varuint32 byte length, then that many bytes.
|
|
84
|
+
* @returns The bytes, a view into the stream's buffer.
|
|
85
|
+
*/
|
|
86
|
+
readLengthPrefixed() {
|
|
87
|
+
return this.read(this.readUnsignedVarInt());
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Writes a varuint32 byte length, then the bytes.
|
|
91
|
+
* @param buf - The bytes to append.
|
|
92
|
+
*/
|
|
93
|
+
writeLengthPrefixed(buf) {
|
|
94
|
+
this.writeUnsignedVarInt(buf.byteLength);
|
|
95
|
+
this.write(buf);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Reads a string: a varuint32 byte length, then UTF-8 bytes, the form Bedrock writes strings in.
|
|
99
|
+
* Named apart from `readString` because RakNet's packets read theirs with a u16 length.
|
|
100
|
+
* {@link https://mojang.github.io/bedrock-protocol-docs/1.26.51/primitives/#string | The docs' string primitive}
|
|
101
|
+
* @returns The read string.
|
|
102
|
+
*/
|
|
103
|
+
readVarString() {
|
|
104
|
+
return this.readLengthPrefixed().toString("utf-8");
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Writes a string as a varuint32 byte length, then UTF-8 bytes.
|
|
108
|
+
* @param str - The string to write.
|
|
109
|
+
*/
|
|
110
|
+
writeVarString(str) {
|
|
111
|
+
this.writeUnsignedVarInt(Buffer.byteLength(str, "utf-8"));
|
|
112
|
+
this.writeUTF8String(str);
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
83
115
|
* Reads an unsigned byte (0 to 255).
|
|
84
116
|
* @returns {number}
|
|
85
117
|
*/
|
package/dist/BinaryStream.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BinaryStream.js","names":[],"sources":["../src/BinaryStream.ts"],"sourcesContent":["import assert from 'assert';\n\n/**\n * A high-performance binary stream for reading and writing binary data.\n *\n * Unlike Node.js native buffers which have fixed size, BinaryStream uses dynamic\n * buffers that grow automatically during write operations. This eliminates the need\n * for manual resizing or inefficient `Buffer.concat()` calls.\n *\n * @example Reading data\n * ```typescript\n * const stream = new BinaryStream(buffer);\n * const value = stream.readInt();\n * ```\n *\n * @example Writing data\n * ```typescript\n * const stream = new BinaryStream();\n * stream.writeInt(42);\n * stream.write(Buffer.from('Hello')); // Buffer grows automatically\n * const buffer = stream.getWriteBuffer();\n * ```\n *\n * @example Encoding many packets without allocating\n * ```typescript\n * const stream = new BinaryStream(undefined, 0, 4096);\n * for (const packet of queue) {\n * stream.resetWrite();\n * packet.encode(stream);\n * offset = stream.copyInto(arena, offset);\n * }\n * ```\n */\nexport class BinaryStream {\n private writeBuffer: Buffer | null = null;\n private readBuffer: Buffer | null = null;\n private readIndex: number;\n private writeIndex = 0;\n private writeCapacity = 0;\n\n /**\n * Creates a new BinaryStream instance.\n * @param {Buffer|null|undefined} buffer - The array or Buffer containing binary data.\n * @param {number} offset - The initial pointer position.\n * @param {number} initialCapacity - Pre-allocates the write buffer to this size. Use it when\n * the encoded size is known up front: it skips the 256 -> 512 -> 1024 ... growth ladder, and\n * with {@link resetWrite} it lets a long-lived encoder run without allocating at all.\n */\n public constructor(buffer?: Buffer, offset: number = 0, initialCapacity: number = 0) {\n this.readBuffer = buffer ?? null; // Keep this instance for reading\n this.readIndex = offset;\n if (initialCapacity > 0) {\n this.reserve(initialCapacity);\n }\n }\n\n /**\n * Reads a slice of buffer by the given length.\n * @param {number} len\n */\n public read(len: number): Buffer {\n this.doReadAssertions(len);\n return this.readBuffer!.subarray(this.readIndex, (this.readIndex += len));\n }\n\n /**\n * Appends a buffer to the main buffer.\n * @param {Buffer|Uint8Array} buf\n */\n public write(buf: Uint8Array): void {\n const len = buf.byteLength;\n this.ensureCapacity(len);\n // This used to call `buf.copy(...)`, which only exists on Buffer, so passing the plain\n // Uint8Array this signature advertises threw `TypeError: buf.copy is not a function`.\n // `set` accepts both, is spec-required to handle overlapping ranges, and is cheaper\n // for the small payloads (UUIDs, short blobs) that dominate packet encoding.\n this.writeBuffer!.set(buf, this.writeIndex);\n this.writeIndex += len;\n }\n\n /**\n * Writes a UTF-8 string directly into the stream's backing buffer.\n * @param value - The string to encode.\n */\n public writeUTF8String(value: string): void {\n const byteLength = Buffer.byteLength(value, 'utf8');\n if (byteLength === 0) {\n return;\n }\n\n this.ensureCapacity(byteLength);\n // Reserve by encoded byte length, then let Buffer encode directly into that space.\n const bytesWritten = this.writeBuffer!.write(value, this.writeIndex, byteLength, 'utf8');\n this.writeIndex += bytesWritten;\n }\n\n /**\n * Reads an unsigned byte (0 to 255).\n * @returns {number}\n */\n public readByte(): number {\n this.doReadAssertions(1);\n return this.readBuffer![this.readIndex++]!;\n }\n\n /**\n * Writes an unsigned byte (0 to 255).\n * @param {number} v\n */\n public writeByte(v: number): void {\n this.ensureCapacity(1);\n this.writeBuffer![this.writeIndex++] = v & 0xff;\n }\n\n /**\n * Reads a signed byte (-128 to 127).\n * @returns {number}\n */\n public readSignedByte(): number {\n this.doReadAssertions(1);\n return (this.readBuffer![this.readIndex++]! << 24) >> 24;\n }\n\n /**\n * Writes a signed byte (-128 to 127).\n * @param {number} v\n */\n public writeSignedByte(v: number): void {\n this.ensureCapacity(1);\n this.writeBuffer![this.writeIndex++] = (v < 0 ? 0xff + v + 1 : v) & 0xff;\n }\n\n /**\n * Reads a boolean (true or false).\n * @returns {boolean}\n */\n public readBoolean(): boolean {\n this.doReadAssertions(1);\n return this.readBuffer![this.readIndex++] !== 0;\n }\n\n /**\n * Writes a boolean (true or false).\n * @param {boolean} v\n */\n public writeBoolean(v: boolean): void {\n this.ensureCapacity(1);\n this.writeBuffer![this.writeIndex++] = v ? 1 : 0;\n }\n\n /**\n * Reads a 16 bit (2 bytes) signed big-endian number.\n * @returns {number}\n */\n public readShort(): number {\n this.doReadAssertions(2);\n return this.readBuffer!.readInt16BE(this.addOffset(2));\n }\n\n /**\n * Writes a 16 bit (2 bytes) signed big-endian number.\n * @param {number} v\n */\n public writeShort(v: number): void {\n this.doWriteAssertions(v, -32_768, 32_767);\n this.ensureCapacity(2);\n this.writeBuffer![this.writeIndex++] = (v >> 8) & 0xff;\n this.writeBuffer![this.writeIndex++] = v & 0xff;\n }\n\n /**\n * Reads a 16 bit (2 bytes) signed little-endian number.\n * @returns {number}\n */\n public readShortLE(): number {\n this.doReadAssertions(2);\n return this.readBuffer!.readInt16LE(this.addOffset(2));\n }\n\n /**\n * Writes a 16 bit (2 bytes) signed little-endian number.\n * @param {number} v\n */\n public writeShortLE(v: number): void {\n this.doWriteAssertions(v, -32_768, 32_767);\n this.ensureCapacity(2);\n this.writeBuffer![this.writeIndex++] = v & 0xff;\n this.writeBuffer![this.writeIndex++] = (v >> 8) & 0xff;\n }\n\n /**\n * Reads a 16 bit (2 bytes) unsigned big-endian number.\n * @returns {number}\n */\n public readUnsignedShort(): number {\n this.doReadAssertions(2);\n return this.readBuffer!.readUInt16BE(this.addOffset(2));\n }\n\n /**\n * Writes a 16 bit (2 bytes) unsigned big-endian number.\n * @param {number} v\n */\n public writeUnsignedShort(v: number): void {\n this.doWriteAssertions(v, 0, 65_535);\n this.ensureCapacity(2);\n this.writeBuffer![this.writeIndex++] = (v >>> 8) & 0xff;\n this.writeBuffer![this.writeIndex++] = v & 0xff;\n }\n\n /**\n * Reads a 16 bit (2 bytes) unsigned little-endian number.\n * @returns {number}\n */\n public readUnsignedShortLE(): number {\n this.doReadAssertions(2);\n return this.readBuffer!.readUInt16LE(this.addOffset(2));\n }\n\n /**\n * Writes a 16 bit (2 bytes) unsigned little-endian number.\n * @param {number} v\n */\n public writeUnsignedShortLE(v: number): void {\n this.doWriteAssertions(v, 0, 65_535);\n this.ensureCapacity(2);\n this.writeBuffer![this.writeIndex++] = v & 0xff;\n this.writeBuffer![this.writeIndex++] = (v >>> 8) & 0xff;\n }\n\n /**\n * Reads a 24 bit (3 bytes) signed big-endian number.\n * @returns {number}\n */\n public readTriad(): number {\n this.doReadAssertions(3);\n return this.readBuffer!.readIntBE(this.addOffset(3), 3);\n }\n\n /**\n * Writes a 24 bit (3 bytes) signed big-endian number.\n * @param {number} v\n */\n public writeTriad(v: number): void {\n this.doWriteAssertions(v, -8_388_608, 8_388_607);\n this.ensureCapacity(3);\n this.writeBuffer![this.writeIndex++] = (v >> 16) & 0xff;\n this.writeBuffer![this.writeIndex++] = (v >> 8) & 0xff;\n this.writeBuffer![this.writeIndex++] = v & 0xff;\n }\n\n /**\n * Reads a 24 bit (3 bytes) little-endian number.\n * @returns {number}\n */\n public readTriadLE(): number {\n this.doReadAssertions(3);\n return this.readBuffer!.readIntLE(this.addOffset(3), 3);\n }\n\n /**\n * Writes a 24 bit (3 bytes) signed little-endian number.\n * @param {number} v\n */\n public writeTriadLE(v: number): void {\n this.doWriteAssertions(v, -8_388_608, 8_388_607);\n this.ensureCapacity(3);\n this.writeBuffer![this.writeIndex++] = v & 0xff;\n this.writeBuffer![this.writeIndex++] = (v >> 8) & 0xff;\n this.writeBuffer![this.writeIndex++] = (v >> 16) & 0xff;\n }\n\n /**\n * Reads a 24 bit (3 bytes) unsigned big-endian number.\n * @returns {number}\n */\n public readUnsignedTriad(): number {\n this.doReadAssertions(3);\n return this.readBuffer!.readUIntBE(this.addOffset(3), 3);\n }\n\n /**\n * Writes a 24 bit (3 bytes) unsigned big-endian number.\n * @param {number} v\n */\n public writeUnsignedTriad(v: number): void {\n this.doWriteAssertions(v, 0, 16_777_215);\n this.ensureCapacity(3);\n this.writeBuffer![this.writeIndex++] = (v >>> 16) & 0xff;\n this.writeBuffer![this.writeIndex++] = (v >>> 8) & 0xff;\n this.writeBuffer![this.writeIndex++] = v & 0xff;\n }\n\n /**\n * Reads a 24 bit (3 bytes) unsigned little-endian number.\n * @returns {number}\n */\n public readUnsignedTriadLE(): number {\n this.doReadAssertions(3);\n return this.readBuffer!.readUIntLE(this.addOffset(3), 3);\n }\n\n /**\n * Writes a 24 bit (3 bytes) unsigned little-endian number.\n * @param {number} v\n */\n public writeUnsignedTriadLE(v: number): void {\n this.doWriteAssertions(v, 0, 16_777_215);\n this.ensureCapacity(3);\n this.writeBuffer![this.writeIndex++] = v & 0xff;\n this.writeBuffer![this.writeIndex++] = (v >>> 8) & 0xff;\n this.writeBuffer![this.writeIndex++] = (v >>> 16) & 0xff;\n }\n\n /**\n * Reads a 32 bit (4 bytes) big-endian signed number.\n * @returns {number}\n */\n public readInt(): number {\n this.doReadAssertions(4);\n return this.readBuffer!.readInt32BE(this.addOffset(4));\n }\n\n /**\n * Writes a 32 bit (4 bytes) big-endian signed number.\n * @param {number} v\n */\n public writeInt(v: number): void {\n this.doWriteAssertions(v, -2_147_483_648, 2_147_483_647);\n this.ensureCapacity(4);\n this.writeBuffer!.writeInt32BE(v, this.writeIndex);\n this.writeIndex += 4;\n }\n\n /**\n * Reads a 32 bit (4 bytes) signed number.\n * @returns {number}\n */\n public readIntLE(): number {\n this.doReadAssertions(4);\n return this.readBuffer!.readIntLE(this.addOffset(4), 4);\n }\n\n /**\n * Writes a 32 bit (4 bytes) little-endian signed number.\n * @param {number} v\n */\n public writeIntLE(v: number) {\n this.doWriteAssertions(v, -2_147_483_648, 2_147_483_647);\n this.ensureCapacity(4);\n this.writeBuffer!.writeInt32LE(v, this.writeIndex);\n this.writeIndex += 4;\n }\n\n /**\n * Reads a 32 bit (4 bytes) big-endian unsigned number.\n * @returns {number}\n */\n public readUnsignedInt(): number {\n this.doReadAssertions(4);\n return this.readBuffer!.readUInt32BE(this.addOffset(4));\n }\n\n /**\n * Writes a 32 bit (4 bytes) big-endian unsigned number.\n * @param {number} v\n */\n public writeUnsignedInt(v: number): void {\n this.doWriteAssertions(v, 0, 4_294_967_295);\n this.ensureCapacity(4);\n this.writeBuffer!.writeUInt32BE(v, this.writeIndex);\n this.writeIndex += 4;\n }\n\n /**\n * Reads a 32 bit (4 bytes) little-endian unsigned number.\n * @returns {number}\n */\n public readUnsignedIntLE(): number {\n this.doReadAssertions(4);\n return this.readBuffer!.readUInt32LE(this.addOffset(4));\n }\n\n /**\n * Writes a 32 bit (4 bytes) little-endian unsigned number.\n * @param {number} v\n */\n public writeUnsignedIntLE(v: number): void {\n this.doWriteAssertions(v, 0, 4_294_967_295);\n this.ensureCapacity(4);\n this.writeBuffer!.writeUInt32LE(v, this.writeIndex);\n this.writeIndex += 4;\n }\n\n /**\n * Returns a 32 bit (4 bytes) big-endian flating point number.\n * @returns {number}\n */\n public readFloat(): number {\n this.doReadAssertions(4);\n return this.readBuffer!.readFloatBE(this.addOffset(4));\n }\n\n /**\n * Writes a 32 bit (4 bytes) big-endian floating point number.\n * @param {number} v\n */\n public writeFloat(v: number): void {\n this.doWriteAssertions(v, -3.4028234663852886e38, +3.4028234663852886e38);\n this.ensureCapacity(4);\n this.writeBuffer!.writeFloatBE(v, this.writeIndex);\n this.writeIndex += 4;\n }\n\n /**\n * Returns a 32 bit (4 bytes) big-endian flating point number.\n * @returns {number}\n */\n public readFloatLE(): number {\n this.doReadAssertions(4);\n return this.readBuffer!.readFloatLE(this.addOffset(4));\n }\n\n /**\n * Writes a 32 bit (4 bytes) little-endian floating point number.\n * @param {number} v\n */\n public writeFloatLE(v: number): void {\n this.doWriteAssertions(v, -3.4028234663852886e38, +3.4028234663852886e38);\n this.ensureCapacity(4);\n this.writeBuffer!.writeFloatLE(v, this.writeIndex);\n this.writeIndex += 4;\n }\n\n /**\n * Returns a 64 bit (8 bytes) big-endian flating point number.\n * @returns {number}\n */\n public readDouble(): number {\n this.doReadAssertions(8);\n return this.readBuffer!.readDoubleBE(this.addOffset(8));\n }\n\n /**\n * Writes a 64 bit (8 bytes) big-endian floating point number.\n * @param {number} v\n */\n public writeDouble(v: number): void {\n this.doWriteAssertions(v, -1.7976931348623157e308, +1.7976931348623157e308);\n this.ensureCapacity(8);\n this.writeBuffer!.writeDoubleBE(v, this.writeIndex);\n this.writeIndex += 8;\n }\n\n /**\n * Returns a 64 bit (8 bytes) little-endian flating point number.\n * @returns {number}\n */\n public readDoubleLE(): number {\n this.doReadAssertions(8);\n return this.readBuffer!.readDoubleLE(this.addOffset(8));\n }\n\n /**\n * Writes a 64 bit (8 bytes) little-endian floating point number.\n * @param {number} v\n */\n public writeDoubleLE(v: number): void {\n this.doWriteAssertions(v, -1.7976931348623157e308, +1.7976931348623157e308);\n this.ensureCapacity(8);\n this.writeBuffer!.writeDoubleLE(v, this.writeIndex);\n this.writeIndex += 8;\n }\n\n /**\n * Returns a 64 bit (8 bytes) signed big-endian number.\n * @returns {bigint}\n */\n public readLong(): bigint {\n this.doReadAssertions(8);\n return this.readBuffer!.readBigInt64BE(this.addOffset(8));\n }\n\n /**\n * Writes a 64 bit (8 bytes) signed big-endian number.\n * @param {bigint} v\n */\n public writeLong(v: bigint): void {\n this.ensureCapacity(8);\n this.writeBuffer!.writeBigInt64BE(v, this.writeIndex);\n this.writeIndex += 8;\n }\n\n /**\n * Returns a 64 bit (8 bytes) signed little-endian number.\n * @returns {bigint}\n */\n public readLongLE(): bigint {\n this.doReadAssertions(8);\n return this.readBuffer!.readBigInt64LE(this.addOffset(8));\n }\n\n /**\n * Writes a 64 bit (8 bytes) signed little-endian number.\n * @param {bigint} v\n */\n public writeLongLE(v: bigint): void {\n this.ensureCapacity(8);\n this.writeBuffer!.writeBigInt64LE(v, this.writeIndex);\n this.writeIndex += 8;\n }\n\n /**\n * Returns a 64 bit (8 bytes) unsigned big-endian number.\n * @returns {bigint}\n */\n public readUnsignedLong(): bigint {\n this.doReadAssertions(8);\n return this.readBuffer!.readBigUInt64BE(this.addOffset(8));\n }\n\n /**\n * Writes a 64 bit (8 bytes) unsigned big-endian number.\n * @param {bigint} v\n */\n public writeUnsignedLong(v: bigint): void {\n this.ensureCapacity(8);\n this.writeBuffer!.writeBigUInt64BE(v, this.writeIndex);\n this.writeIndex += 8;\n }\n\n /**\n * Returns a 64 bit (8 bytes) unsigned little-endian number.\n * @returns {bigint}\n */\n public readUnsignedLongLE(): bigint {\n this.doReadAssertions(8);\n return this.readBuffer!.readBigUInt64LE(this.addOffset(8));\n }\n\n /**\n * Writes a 64 bit (8 bytes) unsigned little-endian number.\n * @param {bigint} v\n */\n public writeUnsignedLongLE(v: bigint): void {\n this.ensureCapacity(8);\n this.writeBuffer!.writeBigUInt64LE(v, this.writeIndex);\n this.writeIndex += 8;\n }\n\n /**\n * Reads a 32 bit (4 bytes) zigzag-encoded number.\n * @returns {number}\n */\n public readVarInt(): number {\n const raw = this.readUnsignedVarInt();\n // The previous expression was `(((raw << 63) >> 63) ^ raw) >> 1 ^ (raw & (1 << 63))`,\n // a Java port that relied on JS shift counts being taken mod 32. It was verified\n // correct over 7.2M values, but this is the same function written legibly.\n return (raw >>> 1) ^ -(raw & 1);\n }\n\n /**\n * Writes a 32 bit (4 bytes) zigzag-encoded number.\n * @param {number} v\n */\n public writeVarInt(v: number): void {\n // The `v = (v << 32) >> 32` that used to sit here was a no-op: JS shift counts are\n // taken mod 32, so it shifted by 0. The `<< 1` below already coerces to int32.\n return this.writeUnsignedVarInt((v << 1) ^ (v >> 31));\n }\n\n /**\n * Reads a 32 bit unsigned number.\n * @returns {number}\n */\n public readUnsignedVarInt(): number {\n const buf = this.readBuffer;\n if (buf === null) {\n assert.fail('Reading on empty buffer!');\n }\n const len = buf.byteLength;\n let value = 0;\n for (let i = 0; i <= 28; i += 7) {\n // A numeric compare rather than `typeof buf[idx] === 'undefined'`: the latter is an\n // out-of-bounds element load, which makes V8 discard the optimized code for this\n // function the first time a stream is read to its end.\n if (this.readIndex >= len) {\n throw new Error('No bytes left in buffer');\n }\n const b = buf[this.readIndex++]!;\n value |= (b & 0x7f) << i;\n\n if ((b & 0x80) === 0) {\n if (i === 28 && (b & 0x70) !== 0) {\n // Bits above the 32nd used to be shifted off and discarded, so an\n // over-long encoding such as `ff ff ff ff 7f` silently decoded to the\n // same value as the canonical `ff ff ff ff 0f`.\n throw new Error('VarInt overflows 32 bits!');\n }\n // `>>> 0` because the accumulator above is int32: without it every value\n // from 2^31 up came back negative, so writeUnsignedVarInt(0xffffffff)\n // round-tripped to -1.\n return value >>> 0;\n }\n }\n\n throw new Error('VarInt did not terminate after 5 bytes!');\n }\n\n /**\n * Writes a 32 bit unsigned number with variable-length.\n * @param {number} v\n */\n public writeUnsignedVarInt(v: number): void {\n // One capacity check for the whole varint instead of one per byte (via writeByte),\n // with the buffer and cursor hoisted into locals so each byte is a plain store.\n this.ensureCapacity(5);\n const buf = this.writeBuffer!;\n let i = this.writeIndex;\n while ((v & 0xffffff80) !== 0) {\n buf[i++] = (v & 0x7f) | 0x80;\n v >>>= 7;\n }\n buf[i++] = v & 0x7f;\n this.writeIndex = i;\n }\n\n /**\n * Reads a 64 bit zigzag-encoded variable-length number.\n * @returns {bigint}\n */\n public readVarLong(): bigint {\n const raw = this.readUnsignedVarLong();\n // The zigzag sign XOR was missing, so this used to return `raw >> 1n`: every negative\n // value decoded wrong (-20n came back as 19n, -1n as 0n) and anything at or above\n // 2^54 collapsed to 0n. Positive small values were unaffected, which is why the\n // shipped test suite never caught it.\n return (raw >> 1n) ^ -(raw & 1n);\n }\n\n /**\n * Writes a 64 bit unsigned zigzag-encoded number.\n * @param {bigint} v\n */\n public writeVarLong(v: bigint) {\n const n = BigInt.asIntN(64, v);\n const zigzag = (n << 1n) ^ (n >> 63n);\n // Zigzag values in uint32 range have identical LEB128 bytes and avoid the BigInt split.\n if (zigzag >= 0n && zigzag <= 0xffff_ffffn) {\n return this.writeUnsignedVarInt(Number(zigzag));\n }\n return this.writeUnsignedVarLong(zigzag);\n }\n\n /**\n * Reads a 64 bit unsigned variable-length number.\n * @returns {bigint}\n */\n public readUnsignedVarLong(): bigint {\n const buf = this.readBuffer;\n if (buf === null) {\n throw new Error('Buffer is write only!');\n }\n const len = buf.byteLength;\n // The accumulator is kept in two int32 halves and materialised as a single BigInt at\n // the end. The previous version allocated five BigInts per decoded byte\n // (BigInt(b), the mask, BigInt(i), the shift and the or).\n let lo = 0;\n let hi = 0;\n for (let i = 0; i <= 63; i += 7) {\n if (this.readIndex >= len) {\n throw new Error('No bytes left in buffer');\n }\n const b = buf[this.readIndex++]!;\n if (i < 28) {\n lo |= (b & 0x7f) << i;\n } else if (i === 28) {\n lo = (lo | ((b & 0x0f) << 28)) >>> 0;\n hi = (b & 0x7f) >>> 4;\n } else {\n hi |= (b & 0x7f) << (i - 32);\n }\n\n if ((b & 0x80) === 0) {\n if (i === 63 && (b & 0x7e) !== 0) {\n throw new Error('VarLong overflows 64 bits!');\n }\n return (BigInt(hi >>> 0) << 32n) | BigInt(lo >>> 0);\n }\n }\n\n throw new Error('VarLong did not terminate after 10 bytes!');\n }\n\n /**\n * Writes a 64 bit unsigned variable-length number.\n * @param {bigint} v\n */\n public writeUnsignedVarLong(v: bigint) {\n // The value is split into two uint32 halves once, then the shift/mask loop runs\n // entirely in int32 space. The previous version did three BigInt allocations plus a\n // Number() conversion per emitted byte, and computed `v >> 7n` twice per byte.\n //\n // It was also wrong: `Number(v | 0x80n)` converted to a double *before* writeByte's\n // `& 0xff` mask, so above 2^54 the low bits were already rounded away.\n // writeUnsignedVarLong(9007199254741119n) emitted a leading 0x00 - a terminator -\n // and the value read back as 0n. Negative inputs emitted ten unterminated bytes;\n // they are now well-defined as their two's complement, matching protobuf.\n // Number is exact for uint32 values, and both writers emit the same unsigned LEB128.\n if (v >= 0n && v <= 0xffff_ffffn) {\n return this.writeUnsignedVarInt(Number(v));\n }\n\n this.ensureCapacity(10);\n const buf = this.writeBuffer!;\n let i = this.writeIndex;\n const u = BigInt.asUintN(64, v);\n let lo = Number(u & 0xffffffffn) >>> 0;\n let hi = Number(u >> 32n) >>> 0;\n while (hi !== 0 || lo > 0x7f) {\n buf[i++] = (lo & 0x7f) | 0x80;\n lo = ((lo >>> 7) | (hi << 25)) >>> 0;\n hi >>>= 7;\n }\n buf[i++] = lo;\n this.writeIndex = i;\n }\n\n /**\n * Ensures write buffer has enough capacity, grows if needed.\n * @param {number} needed\n */\n private ensureCapacity(needed: number): void {\n const required = this.writeIndex + needed;\n if (required > this.writeCapacity) {\n // Initial allocation or growth\n const newCapacity =\n this.writeCapacity === 0\n ? Math.max(256, required) // Initial: 256 or required size\n : Math.max(required, this.writeCapacity << 1); // Growth: 2x\n const newBuffer = Buffer.allocUnsafe(newCapacity);\n\n // Copy existing data if any\n if (this.writeBuffer !== null && this.writeIndex > 0) {\n this.writeBuffer!.copy(newBuffer, 0, 0, this.writeIndex);\n }\n\n this.writeBuffer = newBuffer;\n this.writeCapacity = newCapacity;\n }\n }\n\n /**\n * Grows the write buffer so that at least `capacity` bytes can be written without any\n * further allocation, and returns this stream.\n *\n * Pre-sizing skips the 256 -> 512 -> 1024 ... growth ladder, each step of which copies the\n * whole payload; a 3 KB packet starting from the 256-byte floor performs five allocations\n * and memcpy's 1.25x its own size before it is done.\n * @param {number} capacity\n */\n public reserve(capacity: number): this {\n if (capacity > this.writeCapacity) {\n const newBuffer = Buffer.allocUnsafe(capacity);\n if (this.writeBuffer !== null && this.writeIndex > 0) {\n this.writeBuffer.copy(newBuffer, 0, 0, this.writeIndex);\n }\n this.writeBuffer = newBuffer;\n this.writeCapacity = capacity;\n }\n return this;\n }\n\n /**\n * Rewinds the write cursor while keeping the allocated capacity, so a long-lived encoder\n * reaches a steady state where it never allocates again. This is the intended way to reuse\n * a stream for many packets.\n *\n * Note the aliasing contract, which it shares with {@link clear} and {@link reuse}: any\n * buffer previously handed out by {@link getWriteBuffer} is a *view* over this stream's\n * memory and is invalidated by this call. Use {@link copyOut} or {@link copyInto} for\n * output you intend to keep.\n */\n public resetWrite(): void {\n this.writeIndex = 0;\n }\n\n /**\n * Returns the encoded bytes as an exactly-sized, standalone Buffer.\n *\n * Unlike {@link getWriteBuffer} the result does not alias this stream, so it survives\n * {@link resetWrite}, and it does not pin a pool chunk: because `Buffer.allocUnsafe` is\n * pool-backed (`Buffer.poolSize` is 64 KB on current Node), holding on to a handful of\n * small slices from {@link getWriteBuffer} can keep megabytes alive.\n * @returns {Buffer}\n */\n public copyOut(): Buffer {\n const out = Buffer.allocUnsafeSlow(this.writeIndex);\n if (this.writeIndex > 0) {\n this.writeBuffer!.copy(out, 0, 0, this.writeIndex);\n }\n return out;\n }\n\n /**\n * Copies the encoded bytes into a caller-owned buffer at `offset` and returns the offset\n * just past them. Lets many packets be encoded into one arena with no per-packet\n * allocation at all.\n * @param {Buffer} target\n * @param {number} offset\n * @returns {number} the new offset\n */\n public copyInto(target: Buffer, offset: number = 0): number {\n if (this.writeIndex > 0) {\n this.writeBuffer!.copy(target, offset, 0, this.writeIndex);\n }\n return offset + this.writeIndex;\n }\n\n /**\n * Increases the write offset by the given length.\n * @param {number} length\n */\n private addOffset(length: number): number {\n return (this.readIndex += length) - length;\n }\n\n /**\n * Returns whatever or not the read offset is at end of line.\n * @returns {number}\n */\n public feof(): boolean {\n const buf = this.readBuffer;\n if (buf === null) throw new Error('Buffer is write only!');\n // A numeric compare rather than `typeof buf[i] === 'undefined'`. The latter is an\n // out-of-bounds element load, and V8 bails out of the optimized code for this\n // function the first time it happens - which is every packet decoded to its end.\n return this.readIndex >= buf.byteLength;\n }\n\n /**\n * Reads the remaining bytes and returns the buffer slice.\n * @returns {Buffer}\n */\n public readRemaining(): Buffer {\n if (!this.readBuffer) throw new Error('Buffer is write only!');\n const buf = this.readBuffer!.subarray(this.readIndex);\n this.readIndex = this.readBuffer.byteLength;\n return buf;\n }\n\n /**\n * Skips len bytes on the buffer.\n * @param {number} len\n */\n public skip(len: number): void {\n assert(Number.isInteger(len), 'Cannot skip a float amount of bytes');\n this.readIndex += len;\n }\n\n /**\n * Returns the encoded buffer.\n *\n * Beware: when a read buffer is present this returns *that*, discarding everything\n * written. On a stream used for both reading and writing it never returns your output.\n * @returns {Buffer}\n * @deprecated See {@link getReadBuffer} and {@link getWriteBuffer}.\n */\n public getBuffer(): Buffer {\n return this.readBuffer !== null ? this.readBuffer : this.writeBuffer!.subarray(0, this.writeIndex);\n }\n\n /**\n * Returns the read buffer if available.\n * @returns {Buffer}\n */\n public getReadBuffer(): Buffer | null {\n return this.readBuffer;\n }\n\n /**\n * Returns the encoded bytes as a *view* over this stream's internal buffer.\n *\n * The view is only valid until the next write: {@link resetWrite}, {@link clear} and\n * {@link reuse} all rewind the cursor without reallocating, so a previously returned\n * buffer is silently overwritten by the next packet. If you queue the result - a send\n * queue, a resend window - use {@link copyOut} or {@link copyInto} instead.\n *\n * It also keeps the whole pooled 64 KB chunk it was carved from alive; retaining a\n * scattered handful of small packets this way can pin megabytes.\n * @returns {Buffer}\n */\n public getWriteBuffer(): Buffer {\n return this.writeBuffer!.subarray(0, this.writeIndex);\n }\n\n /**\n * Sets the buffer for reading.\n * make sure to reset the reading index!\n * @param buf - The new Buffer.\n * @deprecated See {@link setReadBuffer} and {@link setWriteBuffer}.\n */\n public setBuffer(buf: Buffer): void {\n this.readBuffer = buf;\n }\n\n /**\n * Sets the buffer for reading.\n * @param buf - The new Buffer.\n * @param rIndex - The new read index (default: 0, pass -1 to keep current).\n */\n public setReadBuffer(buf: Buffer, rIndex = 0): void {\n this.readBuffer = buf;\n if (rIndex >= 0) this.readIndex = rIndex;\n }\n\n /**\n * Sets the buffer for writing.\n * @param buf - The new Buffer.\n * @param wIndex - The new write index (default: 0, pass -1 to keep current).\n */\n public setWriteBuffer(buf: Buffer, wIndex = 0): void {\n this.writeBuffer = buf;\n if (wIndex >= 0) this.writeIndex = wIndex;\n this.writeCapacity = buf.byteLength;\n }\n\n /**\n * Clears the whole BinaryStream instance.\n *\n * The write buffer and its capacity are kept, so any view previously returned by\n * {@link getWriteBuffer} is invalidated. See {@link resetWrite}.\n */\n public clear(): void {\n this.readBuffer = null;\n this.readIndex = 0;\n this.writeIndex = 0;\n }\n\n /**\n * Conventional method to reuse the stream\n * without having to create a new BinaryStream instance.\n *\n * The write buffer and its capacity are kept, so any view previously returned by\n * {@link getWriteBuffer} is invalidated. See {@link resetWrite}.\n * @param buf - The new buffer instance.\n */\n public reuse(buf: Buffer): void {\n this.readBuffer = buf;\n this.readIndex = 0;\n this.writeIndex = 0;\n }\n\n /**\n * Sets the reading index.\n * @param index - The new read index.\n */\n public setReadIndex(index: number): void {\n assert(index >= 0, 'Index must be non-negative');\n this.readIndex = index;\n }\n\n /**\n * Sets the new writing index.\n * @param index - The new write index.\n */\n public setWriteIndex(index: number): void {\n assert(index >= 0, 'Index must be non-negative');\n this.writeIndex = index;\n }\n\n /**\n * Retuns the read index.\n * @returns {number}\n */\n public getReadIndex(): number {\n return this.readIndex;\n }\n\n /**\n * Returns the write index.\n * @returns {number}\n */\n public getWriteIndex(): number {\n return this.writeIndex;\n }\n\n /**\n * Do read assertions, check if the read buffer is null.\n * @param {number} byteLength\n */\n private doReadAssertions(byteLength: number): void {\n const buf = this.readBuffer;\n if (buf === null) {\n assert.fail('Cannot read without buffer data!');\n }\n // This used to compare the *total* buffer length against the requested length and\n // ignore readIndex entirely, so it caught almost nothing: read(4) at offset 998 of a\n // 1000-byte buffer returned 2 bytes with no error, readBoolean past the end returned\n // true, and the fixed-width readers blew up with an ERR_OUT_OF_RANGE raised from\n // inside Buffer rather than an AssertionError raised here.\n if (this.readIndex + byteLength > buf.byteLength) {\n assert.fail(\n `Cannot read ${byteLength} byte(s) at offset ${this.readIndex}: ` +\n `only ${Math.max(0, buf.byteLength - this.readIndex)} left in buffer`\n );\n }\n }\n\n /**\n * Do read assertions, check if the read buffer is null.\n * @param {number|bigint} num\n * @param {number|bigint} minVal\n * @param {number|bigint} maxVal\n */\n private doWriteAssertions(num: number, minVal: number, maxVal: number): void {\n // Argument expressions are evaluated *before* the call, so the template literal used\n // to be built on every successful write - sixteen write methods route through here.\n // For writeFloat/writeDouble the bounds are doubles, so each call ran three\n // double-to-string conversions and threw the result away. Building the message only\n // on failure is worth ~2.3x on a mixed packet-encoding workload.\n //\n // `!(a && b)` rather than `a < min || a > max`: the latter silently accepts NaN,\n // which this method has always rejected.\n if (!(num >= minVal && num <= maxVal)) {\n assert.fail(`Value out of bounds: value=${num}, min=${minVal}, max=${maxVal}`);\n }\n }\n}\n\n// Default export for backward compatibility\nexport default BinaryStream;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiCA,IAAa,eAAb,MAA0B;CACtB,cAAqC;CACrC,aAAoC;CACpC;CACA,aAAqB;CACrB,gBAAwB;;;;;;;;;CAUxB,YAAmB,QAAiB,SAAiB,GAAG,kBAA0B,GAAG;EACjF,KAAK,aAAa,UAAU;EAC5B,KAAK,YAAY;EACjB,IAAI,kBAAkB,GAClB,KAAK,QAAQ,eAAe;CAEpC;;;;;CAMA,KAAY,KAAqB;EAC7B,KAAK,iBA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,KAAK,YAAY;CACrB;;;;;CAMA,cAAqB,OAAqB;EACtC,OAAO,SAAS,GAAG,4BAA4B;EAC/C,KAAK,aAAa;CACtB;;;;;CAMA,eAA8B;EAC1B,OAAO,KAAK;CAChB;;;;;CAMA,gBAA+B;EAC3B,OAAO,KAAK;CAChB;;;;;CAMA,iBAAyB,YAA0B;EAC/C,MAAM,MAAM,KAAK;EACjB,IAAI,QAAQ,MACR,OAAO,KAAK,kCAAkC;EAOlD,IAAI,KAAK,YAAY,aAAa,IAAI,YAClC,OAAO,KACH,eAAe,WAAW,qBAAqB,KAAK,UAAU,SAClD,KAAK,IAAI,GAAG,IAAI,aAAa,KAAK,SAAS,EAAE,gBAC7D;CAER;;;;;;;CAQA,kBAA0B,KAAa,QAAgB,QAAsB;EASzE,IAAI,EAAE,OAAO,UAAU,OAAO,SAC1B,OAAO,KAAK,8BAA8B,IAAI,QAAQ,OAAO,QAAQ,QAAQ;CAErF;AACJ"}
|
|
1
|
+
{"version":3,"file":"BinaryStream.js","names":[],"sources":["../src/BinaryStream.ts"],"sourcesContent":["import assert from 'assert';\n\n/**\n * A high-performance binary stream for reading and writing binary data.\n *\n * Unlike Node.js native buffers which have fixed size, BinaryStream uses dynamic\n * buffers that grow automatically during write operations. This eliminates the need\n * for manual resizing or inefficient `Buffer.concat()` calls.\n *\n * @example Reading data\n * ```typescript\n * const stream = new BinaryStream(buffer);\n * const value = stream.readInt();\n * ```\n *\n * @example Writing data\n * ```typescript\n * const stream = new BinaryStream();\n * stream.writeInt(42);\n * stream.write(Buffer.from('Hello')); // Buffer grows automatically\n * const buffer = stream.getWriteBuffer();\n * ```\n *\n * @example Encoding many packets without allocating\n * ```typescript\n * const stream = new BinaryStream(undefined, 0, 4096);\n * for (const packet of queue) {\n * stream.resetWrite();\n * packet.encode(stream);\n * offset = stream.copyInto(arena, offset);\n * }\n * ```\n */\nexport class BinaryStream {\n private writeBuffer: Buffer | null = null;\n private readBuffer: Buffer | null = null;\n private readIndex: number;\n private writeIndex = 0;\n private writeCapacity = 0;\n\n /**\n * Creates a new BinaryStream instance.\n * @param {Buffer|null|undefined} buffer - The array or Buffer containing binary data.\n * @param {number} offset - The initial pointer position.\n * @param {number} initialCapacity - Pre-allocates the write buffer to this size. Use it when\n * the encoded size is known up front: it skips the 256 -> 512 -> 1024 ... growth ladder, and\n * with {@link resetWrite} it lets a long-lived encoder run without allocating at all.\n */\n public constructor(buffer?: Buffer, offset: number = 0, initialCapacity: number = 0) {\n this.readBuffer = buffer ?? null; // Keep this instance for reading\n this.readIndex = offset;\n if (initialCapacity > 0) {\n this.reserve(initialCapacity);\n }\n }\n\n /**\n * Reads a slice of buffer by the given length.\n * @param {number} len\n */\n public read(len: number): Buffer {\n this.doReadAssertions(len);\n return this.readBuffer!.subarray(this.readIndex, (this.readIndex += len));\n }\n\n /**\n * Appends a buffer to the main buffer.\n * @param {Buffer|Uint8Array} buf\n */\n public write(buf: Uint8Array): void {\n const len = buf.byteLength;\n this.ensureCapacity(len);\n // This used to call `buf.copy(...)`, which only exists on Buffer, so passing the plain\n // Uint8Array this signature advertises threw `TypeError: buf.copy is not a function`.\n // `set` accepts both, is spec-required to handle overlapping ranges, and is cheaper\n // for the small payloads (UUIDs, short blobs) that dominate packet encoding.\n this.writeBuffer!.set(buf, this.writeIndex);\n this.writeIndex += len;\n }\n\n /**\n * Writes a UTF-8 string directly into the stream's backing buffer.\n * @param value - The string to encode.\n */\n public writeUTF8String(value: string): void {\n const byteLength = Buffer.byteLength(value, 'utf8');\n if (byteLength === 0) {\n return;\n }\n\n this.ensureCapacity(byteLength);\n // Reserve by encoded byte length, then let Buffer encode directly into that space.\n const bytesWritten = this.writeBuffer!.write(value, this.writeIndex, byteLength, 'utf8');\n this.writeIndex += bytesWritten;\n }\n\n /**\n * Reads a varuint32 byte length, then that many bytes.\n * @returns The bytes, a view into the stream's buffer.\n */\n public readLengthPrefixed(): Buffer {\n return this.read(this.readUnsignedVarInt());\n }\n\n /**\n * Writes a varuint32 byte length, then the bytes.\n * @param buf - The bytes to append.\n */\n public writeLengthPrefixed(buf: Uint8Array): void {\n this.writeUnsignedVarInt(buf.byteLength);\n this.write(buf);\n }\n\n /**\n * Reads a string: a varuint32 byte length, then UTF-8 bytes, the form Bedrock writes strings in.\n * Named apart from `readString` because RakNet's packets read theirs with a u16 length.\n * {@link https://mojang.github.io/bedrock-protocol-docs/1.26.51/primitives/#string | The docs' string primitive}\n * @returns The read string.\n */\n public readVarString(): string {\n return this.readLengthPrefixed().toString('utf-8');\n }\n\n /**\n * Writes a string as a varuint32 byte length, then UTF-8 bytes.\n * @param str - The string to write.\n */\n public writeVarString(str: string): void {\n this.writeUnsignedVarInt(Buffer.byteLength(str, 'utf-8'));\n this.writeUTF8String(str);\n }\n\n /**\n * Reads an unsigned byte (0 to 255).\n * @returns {number}\n */\n public readByte(): number {\n this.doReadAssertions(1);\n return this.readBuffer![this.readIndex++]!;\n }\n\n /**\n * Writes an unsigned byte (0 to 255).\n * @param {number} v\n */\n public writeByte(v: number): void {\n this.ensureCapacity(1);\n this.writeBuffer![this.writeIndex++] = v & 0xff;\n }\n\n /**\n * Reads a signed byte (-128 to 127).\n * @returns {number}\n */\n public readSignedByte(): number {\n this.doReadAssertions(1);\n return (this.readBuffer![this.readIndex++]! << 24) >> 24;\n }\n\n /**\n * Writes a signed byte (-128 to 127).\n * @param {number} v\n */\n public writeSignedByte(v: number): void {\n this.ensureCapacity(1);\n this.writeBuffer![this.writeIndex++] = (v < 0 ? 0xff + v + 1 : v) & 0xff;\n }\n\n /**\n * Reads a boolean (true or false).\n * @returns {boolean}\n */\n public readBoolean(): boolean {\n this.doReadAssertions(1);\n return this.readBuffer![this.readIndex++] !== 0;\n }\n\n /**\n * Writes a boolean (true or false).\n * @param {boolean} v\n */\n public writeBoolean(v: boolean): void {\n this.ensureCapacity(1);\n this.writeBuffer![this.writeIndex++] = v ? 1 : 0;\n }\n\n /**\n * Reads a 16 bit (2 bytes) signed big-endian number.\n * @returns {number}\n */\n public readShort(): number {\n this.doReadAssertions(2);\n return this.readBuffer!.readInt16BE(this.addOffset(2));\n }\n\n /**\n * Writes a 16 bit (2 bytes) signed big-endian number.\n * @param {number} v\n */\n public writeShort(v: number): void {\n this.doWriteAssertions(v, -32_768, 32_767);\n this.ensureCapacity(2);\n this.writeBuffer![this.writeIndex++] = (v >> 8) & 0xff;\n this.writeBuffer![this.writeIndex++] = v & 0xff;\n }\n\n /**\n * Reads a 16 bit (2 bytes) signed little-endian number.\n * @returns {number}\n */\n public readShortLE(): number {\n this.doReadAssertions(2);\n return this.readBuffer!.readInt16LE(this.addOffset(2));\n }\n\n /**\n * Writes a 16 bit (2 bytes) signed little-endian number.\n * @param {number} v\n */\n public writeShortLE(v: number): void {\n this.doWriteAssertions(v, -32_768, 32_767);\n this.ensureCapacity(2);\n this.writeBuffer![this.writeIndex++] = v & 0xff;\n this.writeBuffer![this.writeIndex++] = (v >> 8) & 0xff;\n }\n\n /**\n * Reads a 16 bit (2 bytes) unsigned big-endian number.\n * @returns {number}\n */\n public readUnsignedShort(): number {\n this.doReadAssertions(2);\n return this.readBuffer!.readUInt16BE(this.addOffset(2));\n }\n\n /**\n * Writes a 16 bit (2 bytes) unsigned big-endian number.\n * @param {number} v\n */\n public writeUnsignedShort(v: number): void {\n this.doWriteAssertions(v, 0, 65_535);\n this.ensureCapacity(2);\n this.writeBuffer![this.writeIndex++] = (v >>> 8) & 0xff;\n this.writeBuffer![this.writeIndex++] = v & 0xff;\n }\n\n /**\n * Reads a 16 bit (2 bytes) unsigned little-endian number.\n * @returns {number}\n */\n public readUnsignedShortLE(): number {\n this.doReadAssertions(2);\n return this.readBuffer!.readUInt16LE(this.addOffset(2));\n }\n\n /**\n * Writes a 16 bit (2 bytes) unsigned little-endian number.\n * @param {number} v\n */\n public writeUnsignedShortLE(v: number): void {\n this.doWriteAssertions(v, 0, 65_535);\n this.ensureCapacity(2);\n this.writeBuffer![this.writeIndex++] = v & 0xff;\n this.writeBuffer![this.writeIndex++] = (v >>> 8) & 0xff;\n }\n\n /**\n * Reads a 24 bit (3 bytes) signed big-endian number.\n * @returns {number}\n */\n public readTriad(): number {\n this.doReadAssertions(3);\n return this.readBuffer!.readIntBE(this.addOffset(3), 3);\n }\n\n /**\n * Writes a 24 bit (3 bytes) signed big-endian number.\n * @param {number} v\n */\n public writeTriad(v: number): void {\n this.doWriteAssertions(v, -8_388_608, 8_388_607);\n this.ensureCapacity(3);\n this.writeBuffer![this.writeIndex++] = (v >> 16) & 0xff;\n this.writeBuffer![this.writeIndex++] = (v >> 8) & 0xff;\n this.writeBuffer![this.writeIndex++] = v & 0xff;\n }\n\n /**\n * Reads a 24 bit (3 bytes) little-endian number.\n * @returns {number}\n */\n public readTriadLE(): number {\n this.doReadAssertions(3);\n return this.readBuffer!.readIntLE(this.addOffset(3), 3);\n }\n\n /**\n * Writes a 24 bit (3 bytes) signed little-endian number.\n * @param {number} v\n */\n public writeTriadLE(v: number): void {\n this.doWriteAssertions(v, -8_388_608, 8_388_607);\n this.ensureCapacity(3);\n this.writeBuffer![this.writeIndex++] = v & 0xff;\n this.writeBuffer![this.writeIndex++] = (v >> 8) & 0xff;\n this.writeBuffer![this.writeIndex++] = (v >> 16) & 0xff;\n }\n\n /**\n * Reads a 24 bit (3 bytes) unsigned big-endian number.\n * @returns {number}\n */\n public readUnsignedTriad(): number {\n this.doReadAssertions(3);\n return this.readBuffer!.readUIntBE(this.addOffset(3), 3);\n }\n\n /**\n * Writes a 24 bit (3 bytes) unsigned big-endian number.\n * @param {number} v\n */\n public writeUnsignedTriad(v: number): void {\n this.doWriteAssertions(v, 0, 16_777_215);\n this.ensureCapacity(3);\n this.writeBuffer![this.writeIndex++] = (v >>> 16) & 0xff;\n this.writeBuffer![this.writeIndex++] = (v >>> 8) & 0xff;\n this.writeBuffer![this.writeIndex++] = v & 0xff;\n }\n\n /**\n * Reads a 24 bit (3 bytes) unsigned little-endian number.\n * @returns {number}\n */\n public readUnsignedTriadLE(): number {\n this.doReadAssertions(3);\n return this.readBuffer!.readUIntLE(this.addOffset(3), 3);\n }\n\n /**\n * Writes a 24 bit (3 bytes) unsigned little-endian number.\n * @param {number} v\n */\n public writeUnsignedTriadLE(v: number): void {\n this.doWriteAssertions(v, 0, 16_777_215);\n this.ensureCapacity(3);\n this.writeBuffer![this.writeIndex++] = v & 0xff;\n this.writeBuffer![this.writeIndex++] = (v >>> 8) & 0xff;\n this.writeBuffer![this.writeIndex++] = (v >>> 16) & 0xff;\n }\n\n /**\n * Reads a 32 bit (4 bytes) big-endian signed number.\n * @returns {number}\n */\n public readInt(): number {\n this.doReadAssertions(4);\n return this.readBuffer!.readInt32BE(this.addOffset(4));\n }\n\n /**\n * Writes a 32 bit (4 bytes) big-endian signed number.\n * @param {number} v\n */\n public writeInt(v: number): void {\n this.doWriteAssertions(v, -2_147_483_648, 2_147_483_647);\n this.ensureCapacity(4);\n this.writeBuffer!.writeInt32BE(v, this.writeIndex);\n this.writeIndex += 4;\n }\n\n /**\n * Reads a 32 bit (4 bytes) signed number.\n * @returns {number}\n */\n public readIntLE(): number {\n this.doReadAssertions(4);\n return this.readBuffer!.readIntLE(this.addOffset(4), 4);\n }\n\n /**\n * Writes a 32 bit (4 bytes) little-endian signed number.\n * @param {number} v\n */\n public writeIntLE(v: number) {\n this.doWriteAssertions(v, -2_147_483_648, 2_147_483_647);\n this.ensureCapacity(4);\n this.writeBuffer!.writeInt32LE(v, this.writeIndex);\n this.writeIndex += 4;\n }\n\n /**\n * Reads a 32 bit (4 bytes) big-endian unsigned number.\n * @returns {number}\n */\n public readUnsignedInt(): number {\n this.doReadAssertions(4);\n return this.readBuffer!.readUInt32BE(this.addOffset(4));\n }\n\n /**\n * Writes a 32 bit (4 bytes) big-endian unsigned number.\n * @param {number} v\n */\n public writeUnsignedInt(v: number): void {\n this.doWriteAssertions(v, 0, 4_294_967_295);\n this.ensureCapacity(4);\n this.writeBuffer!.writeUInt32BE(v, this.writeIndex);\n this.writeIndex += 4;\n }\n\n /**\n * Reads a 32 bit (4 bytes) little-endian unsigned number.\n * @returns {number}\n */\n public readUnsignedIntLE(): number {\n this.doReadAssertions(4);\n return this.readBuffer!.readUInt32LE(this.addOffset(4));\n }\n\n /**\n * Writes a 32 bit (4 bytes) little-endian unsigned number.\n * @param {number} v\n */\n public writeUnsignedIntLE(v: number): void {\n this.doWriteAssertions(v, 0, 4_294_967_295);\n this.ensureCapacity(4);\n this.writeBuffer!.writeUInt32LE(v, this.writeIndex);\n this.writeIndex += 4;\n }\n\n /**\n * Returns a 32 bit (4 bytes) big-endian flating point number.\n * @returns {number}\n */\n public readFloat(): number {\n this.doReadAssertions(4);\n return this.readBuffer!.readFloatBE(this.addOffset(4));\n }\n\n /**\n * Writes a 32 bit (4 bytes) big-endian floating point number.\n * @param {number} v\n */\n public writeFloat(v: number): void {\n this.doWriteAssertions(v, -3.4028234663852886e38, +3.4028234663852886e38);\n this.ensureCapacity(4);\n this.writeBuffer!.writeFloatBE(v, this.writeIndex);\n this.writeIndex += 4;\n }\n\n /**\n * Returns a 32 bit (4 bytes) big-endian flating point number.\n * @returns {number}\n */\n public readFloatLE(): number {\n this.doReadAssertions(4);\n return this.readBuffer!.readFloatLE(this.addOffset(4));\n }\n\n /**\n * Writes a 32 bit (4 bytes) little-endian floating point number.\n * @param {number} v\n */\n public writeFloatLE(v: number): void {\n this.doWriteAssertions(v, -3.4028234663852886e38, +3.4028234663852886e38);\n this.ensureCapacity(4);\n this.writeBuffer!.writeFloatLE(v, this.writeIndex);\n this.writeIndex += 4;\n }\n\n /**\n * Returns a 64 bit (8 bytes) big-endian flating point number.\n * @returns {number}\n */\n public readDouble(): number {\n this.doReadAssertions(8);\n return this.readBuffer!.readDoubleBE(this.addOffset(8));\n }\n\n /**\n * Writes a 64 bit (8 bytes) big-endian floating point number.\n * @param {number} v\n */\n public writeDouble(v: number): void {\n this.doWriteAssertions(v, -1.7976931348623157e308, +1.7976931348623157e308);\n this.ensureCapacity(8);\n this.writeBuffer!.writeDoubleBE(v, this.writeIndex);\n this.writeIndex += 8;\n }\n\n /**\n * Returns a 64 bit (8 bytes) little-endian flating point number.\n * @returns {number}\n */\n public readDoubleLE(): number {\n this.doReadAssertions(8);\n return this.readBuffer!.readDoubleLE(this.addOffset(8));\n }\n\n /**\n * Writes a 64 bit (8 bytes) little-endian floating point number.\n * @param {number} v\n */\n public writeDoubleLE(v: number): void {\n this.doWriteAssertions(v, -1.7976931348623157e308, +1.7976931348623157e308);\n this.ensureCapacity(8);\n this.writeBuffer!.writeDoubleLE(v, this.writeIndex);\n this.writeIndex += 8;\n }\n\n /**\n * Returns a 64 bit (8 bytes) signed big-endian number.\n * @returns {bigint}\n */\n public readLong(): bigint {\n this.doReadAssertions(8);\n return this.readBuffer!.readBigInt64BE(this.addOffset(8));\n }\n\n /**\n * Writes a 64 bit (8 bytes) signed big-endian number.\n * @param {bigint} v\n */\n public writeLong(v: bigint): void {\n this.ensureCapacity(8);\n this.writeBuffer!.writeBigInt64BE(v, this.writeIndex);\n this.writeIndex += 8;\n }\n\n /**\n * Returns a 64 bit (8 bytes) signed little-endian number.\n * @returns {bigint}\n */\n public readLongLE(): bigint {\n this.doReadAssertions(8);\n return this.readBuffer!.readBigInt64LE(this.addOffset(8));\n }\n\n /**\n * Writes a 64 bit (8 bytes) signed little-endian number.\n * @param {bigint} v\n */\n public writeLongLE(v: bigint): void {\n this.ensureCapacity(8);\n this.writeBuffer!.writeBigInt64LE(v, this.writeIndex);\n this.writeIndex += 8;\n }\n\n /**\n * Returns a 64 bit (8 bytes) unsigned big-endian number.\n * @returns {bigint}\n */\n public readUnsignedLong(): bigint {\n this.doReadAssertions(8);\n return this.readBuffer!.readBigUInt64BE(this.addOffset(8));\n }\n\n /**\n * Writes a 64 bit (8 bytes) unsigned big-endian number.\n * @param {bigint} v\n */\n public writeUnsignedLong(v: bigint): void {\n this.ensureCapacity(8);\n this.writeBuffer!.writeBigUInt64BE(v, this.writeIndex);\n this.writeIndex += 8;\n }\n\n /**\n * Returns a 64 bit (8 bytes) unsigned little-endian number.\n * @returns {bigint}\n */\n public readUnsignedLongLE(): bigint {\n this.doReadAssertions(8);\n return this.readBuffer!.readBigUInt64LE(this.addOffset(8));\n }\n\n /**\n * Writes a 64 bit (8 bytes) unsigned little-endian number.\n * @param {bigint} v\n */\n public writeUnsignedLongLE(v: bigint): void {\n this.ensureCapacity(8);\n this.writeBuffer!.writeBigUInt64LE(v, this.writeIndex);\n this.writeIndex += 8;\n }\n\n /**\n * Reads a 32 bit (4 bytes) zigzag-encoded number.\n * @returns {number}\n */\n public readVarInt(): number {\n const raw = this.readUnsignedVarInt();\n // The previous expression was `(((raw << 63) >> 63) ^ raw) >> 1 ^ (raw & (1 << 63))`,\n // a Java port that relied on JS shift counts being taken mod 32. It was verified\n // correct over 7.2M values, but this is the same function written legibly.\n return (raw >>> 1) ^ -(raw & 1);\n }\n\n /**\n * Writes a 32 bit (4 bytes) zigzag-encoded number.\n * @param {number} v\n */\n public writeVarInt(v: number): void {\n // The `v = (v << 32) >> 32` that used to sit here was a no-op: JS shift counts are\n // taken mod 32, so it shifted by 0. The `<< 1` below already coerces to int32.\n return this.writeUnsignedVarInt((v << 1) ^ (v >> 31));\n }\n\n /**\n * Reads a 32 bit unsigned number.\n * @returns {number}\n */\n public readUnsignedVarInt(): number {\n const buf = this.readBuffer;\n if (buf === null) {\n assert.fail('Reading on empty buffer!');\n }\n const len = buf.byteLength;\n let value = 0;\n for (let i = 0; i <= 28; i += 7) {\n // A numeric compare rather than `typeof buf[idx] === 'undefined'`: the latter is an\n // out-of-bounds element load, which makes V8 discard the optimized code for this\n // function the first time a stream is read to its end.\n if (this.readIndex >= len) {\n throw new Error('No bytes left in buffer');\n }\n const b = buf[this.readIndex++]!;\n value |= (b & 0x7f) << i;\n\n if ((b & 0x80) === 0) {\n if (i === 28 && (b & 0x70) !== 0) {\n // Bits above the 32nd used to be shifted off and discarded, so an\n // over-long encoding such as `ff ff ff ff 7f` silently decoded to the\n // same value as the canonical `ff ff ff ff 0f`.\n throw new Error('VarInt overflows 32 bits!');\n }\n // `>>> 0` because the accumulator above is int32: without it every value\n // from 2^31 up came back negative, so writeUnsignedVarInt(0xffffffff)\n // round-tripped to -1.\n return value >>> 0;\n }\n }\n\n throw new Error('VarInt did not terminate after 5 bytes!');\n }\n\n /**\n * Writes a 32 bit unsigned number with variable-length.\n * @param {number} v\n */\n public writeUnsignedVarInt(v: number): void {\n // One capacity check for the whole varint instead of one per byte (via writeByte),\n // with the buffer and cursor hoisted into locals so each byte is a plain store.\n this.ensureCapacity(5);\n const buf = this.writeBuffer!;\n let i = this.writeIndex;\n while ((v & 0xffffff80) !== 0) {\n buf[i++] = (v & 0x7f) | 0x80;\n v >>>= 7;\n }\n buf[i++] = v & 0x7f;\n this.writeIndex = i;\n }\n\n /**\n * Reads a 64 bit zigzag-encoded variable-length number.\n * @returns {bigint}\n */\n public readVarLong(): bigint {\n const raw = this.readUnsignedVarLong();\n // The zigzag sign XOR was missing, so this used to return `raw >> 1n`: every negative\n // value decoded wrong (-20n came back as 19n, -1n as 0n) and anything at or above\n // 2^54 collapsed to 0n. Positive small values were unaffected, which is why the\n // shipped test suite never caught it.\n return (raw >> 1n) ^ -(raw & 1n);\n }\n\n /**\n * Writes a 64 bit unsigned zigzag-encoded number.\n * @param {bigint} v\n */\n public writeVarLong(v: bigint) {\n const n = BigInt.asIntN(64, v);\n const zigzag = (n << 1n) ^ (n >> 63n);\n // Zigzag values in uint32 range have identical LEB128 bytes and avoid the BigInt split.\n if (zigzag >= 0n && zigzag <= 0xffff_ffffn) {\n return this.writeUnsignedVarInt(Number(zigzag));\n }\n return this.writeUnsignedVarLong(zigzag);\n }\n\n /**\n * Reads a 64 bit unsigned variable-length number.\n * @returns {bigint}\n */\n public readUnsignedVarLong(): bigint {\n const buf = this.readBuffer;\n if (buf === null) {\n throw new Error('Buffer is write only!');\n }\n const len = buf.byteLength;\n // The accumulator is kept in two int32 halves and materialised as a single BigInt at\n // the end. The previous version allocated five BigInts per decoded byte\n // (BigInt(b), the mask, BigInt(i), the shift and the or).\n let lo = 0;\n let hi = 0;\n for (let i = 0; i <= 63; i += 7) {\n if (this.readIndex >= len) {\n throw new Error('No bytes left in buffer');\n }\n const b = buf[this.readIndex++]!;\n if (i < 28) {\n lo |= (b & 0x7f) << i;\n } else if (i === 28) {\n lo = (lo | ((b & 0x0f) << 28)) >>> 0;\n hi = (b & 0x7f) >>> 4;\n } else {\n hi |= (b & 0x7f) << (i - 32);\n }\n\n if ((b & 0x80) === 0) {\n if (i === 63 && (b & 0x7e) !== 0) {\n throw new Error('VarLong overflows 64 bits!');\n }\n return (BigInt(hi >>> 0) << 32n) | BigInt(lo >>> 0);\n }\n }\n\n throw new Error('VarLong did not terminate after 10 bytes!');\n }\n\n /**\n * Writes a 64 bit unsigned variable-length number.\n * @param {bigint} v\n */\n public writeUnsignedVarLong(v: bigint) {\n // The value is split into two uint32 halves once, then the shift/mask loop runs\n // entirely in int32 space. The previous version did three BigInt allocations plus a\n // Number() conversion per emitted byte, and computed `v >> 7n` twice per byte.\n //\n // It was also wrong: `Number(v | 0x80n)` converted to a double *before* writeByte's\n // `& 0xff` mask, so above 2^54 the low bits were already rounded away.\n // writeUnsignedVarLong(9007199254741119n) emitted a leading 0x00 - a terminator -\n // and the value read back as 0n. Negative inputs emitted ten unterminated bytes;\n // they are now well-defined as their two's complement, matching protobuf.\n // Number is exact for uint32 values, and both writers emit the same unsigned LEB128.\n if (v >= 0n && v <= 0xffff_ffffn) {\n return this.writeUnsignedVarInt(Number(v));\n }\n\n this.ensureCapacity(10);\n const buf = this.writeBuffer!;\n let i = this.writeIndex;\n const u = BigInt.asUintN(64, v);\n let lo = Number(u & 0xffffffffn) >>> 0;\n let hi = Number(u >> 32n) >>> 0;\n while (hi !== 0 || lo > 0x7f) {\n buf[i++] = (lo & 0x7f) | 0x80;\n lo = ((lo >>> 7) | (hi << 25)) >>> 0;\n hi >>>= 7;\n }\n buf[i++] = lo;\n this.writeIndex = i;\n }\n\n /**\n * Ensures write buffer has enough capacity, grows if needed.\n * @param {number} needed\n */\n private ensureCapacity(needed: number): void {\n const required = this.writeIndex + needed;\n if (required > this.writeCapacity) {\n // Initial allocation or growth\n const newCapacity =\n this.writeCapacity === 0\n ? Math.max(256, required) // Initial: 256 or required size\n : Math.max(required, this.writeCapacity << 1); // Growth: 2x\n const newBuffer = Buffer.allocUnsafe(newCapacity);\n\n // Copy existing data if any\n if (this.writeBuffer !== null && this.writeIndex > 0) {\n this.writeBuffer!.copy(newBuffer, 0, 0, this.writeIndex);\n }\n\n this.writeBuffer = newBuffer;\n this.writeCapacity = newCapacity;\n }\n }\n\n /**\n * Grows the write buffer so that at least `capacity` bytes can be written without any\n * further allocation, and returns this stream.\n *\n * Pre-sizing skips the 256 -> 512 -> 1024 ... growth ladder, each step of which copies the\n * whole payload; a 3 KB packet starting from the 256-byte floor performs five allocations\n * and memcpy's 1.25x its own size before it is done.\n * @param {number} capacity\n */\n public reserve(capacity: number): this {\n if (capacity > this.writeCapacity) {\n const newBuffer = Buffer.allocUnsafe(capacity);\n if (this.writeBuffer !== null && this.writeIndex > 0) {\n this.writeBuffer.copy(newBuffer, 0, 0, this.writeIndex);\n }\n this.writeBuffer = newBuffer;\n this.writeCapacity = capacity;\n }\n return this;\n }\n\n /**\n * Rewinds the write cursor while keeping the allocated capacity, so a long-lived encoder\n * reaches a steady state where it never allocates again. This is the intended way to reuse\n * a stream for many packets.\n *\n * Note the aliasing contract, which it shares with {@link clear} and {@link reuse}: any\n * buffer previously handed out by {@link getWriteBuffer} is a *view* over this stream's\n * memory and is invalidated by this call. Use {@link copyOut} or {@link copyInto} for\n * output you intend to keep.\n */\n public resetWrite(): void {\n this.writeIndex = 0;\n }\n\n /**\n * Returns the encoded bytes as an exactly-sized, standalone Buffer.\n *\n * Unlike {@link getWriteBuffer} the result does not alias this stream, so it survives\n * {@link resetWrite}, and it does not pin a pool chunk: because `Buffer.allocUnsafe` is\n * pool-backed (`Buffer.poolSize` is 64 KB on current Node), holding on to a handful of\n * small slices from {@link getWriteBuffer} can keep megabytes alive.\n * @returns {Buffer}\n */\n public copyOut(): Buffer {\n const out = Buffer.allocUnsafeSlow(this.writeIndex);\n if (this.writeIndex > 0) {\n this.writeBuffer!.copy(out, 0, 0, this.writeIndex);\n }\n return out;\n }\n\n /**\n * Copies the encoded bytes into a caller-owned buffer at `offset` and returns the offset\n * just past them. Lets many packets be encoded into one arena with no per-packet\n * allocation at all.\n * @param {Buffer} target\n * @param {number} offset\n * @returns {number} the new offset\n */\n public copyInto(target: Buffer, offset: number = 0): number {\n if (this.writeIndex > 0) {\n this.writeBuffer!.copy(target, offset, 0, this.writeIndex);\n }\n return offset + this.writeIndex;\n }\n\n /**\n * Increases the write offset by the given length.\n * @param {number} length\n */\n private addOffset(length: number): number {\n return (this.readIndex += length) - length;\n }\n\n /**\n * Returns whatever or not the read offset is at end of line.\n * @returns {number}\n */\n public feof(): boolean {\n const buf = this.readBuffer;\n if (buf === null) throw new Error('Buffer is write only!');\n // A numeric compare rather than `typeof buf[i] === 'undefined'`. The latter is an\n // out-of-bounds element load, and V8 bails out of the optimized code for this\n // function the first time it happens - which is every packet decoded to its end.\n return this.readIndex >= buf.byteLength;\n }\n\n /**\n * Reads the remaining bytes and returns the buffer slice.\n * @returns {Buffer}\n */\n public readRemaining(): Buffer {\n if (!this.readBuffer) throw new Error('Buffer is write only!');\n const buf = this.readBuffer!.subarray(this.readIndex);\n this.readIndex = this.readBuffer.byteLength;\n return buf;\n }\n\n /**\n * Skips len bytes on the buffer.\n * @param {number} len\n */\n public skip(len: number): void {\n assert(Number.isInteger(len), 'Cannot skip a float amount of bytes');\n this.readIndex += len;\n }\n\n /**\n * Returns the encoded buffer.\n *\n * Beware: when a read buffer is present this returns *that*, discarding everything\n * written. On a stream used for both reading and writing it never returns your output.\n * @returns {Buffer}\n * @deprecated See {@link getReadBuffer} and {@link getWriteBuffer}.\n */\n public getBuffer(): Buffer {\n return this.readBuffer !== null ? this.readBuffer : this.writeBuffer!.subarray(0, this.writeIndex);\n }\n\n /**\n * Returns the read buffer if available.\n * @returns {Buffer}\n */\n public getReadBuffer(): Buffer | null {\n return this.readBuffer;\n }\n\n /**\n * Returns the encoded bytes as a *view* over this stream's internal buffer.\n *\n * The view is only valid until the next write: {@link resetWrite}, {@link clear} and\n * {@link reuse} all rewind the cursor without reallocating, so a previously returned\n * buffer is silently overwritten by the next packet. If you queue the result - a send\n * queue, a resend window - use {@link copyOut} or {@link copyInto} instead.\n *\n * It also keeps the whole pooled 64 KB chunk it was carved from alive; retaining a\n * scattered handful of small packets this way can pin megabytes.\n * @returns {Buffer}\n */\n public getWriteBuffer(): Buffer {\n return this.writeBuffer!.subarray(0, this.writeIndex);\n }\n\n /**\n * Sets the buffer for reading.\n * make sure to reset the reading index!\n * @param buf - The new Buffer.\n * @deprecated See {@link setReadBuffer} and {@link setWriteBuffer}.\n */\n public setBuffer(buf: Buffer): void {\n this.readBuffer = buf;\n }\n\n /**\n * Sets the buffer for reading.\n * @param buf - The new Buffer.\n * @param rIndex - The new read index (default: 0, pass -1 to keep current).\n */\n public setReadBuffer(buf: Buffer, rIndex = 0): void {\n this.readBuffer = buf;\n if (rIndex >= 0) this.readIndex = rIndex;\n }\n\n /**\n * Sets the buffer for writing.\n * @param buf - The new Buffer.\n * @param wIndex - The new write index (default: 0, pass -1 to keep current).\n */\n public setWriteBuffer(buf: Buffer, wIndex = 0): void {\n this.writeBuffer = buf;\n if (wIndex >= 0) this.writeIndex = wIndex;\n this.writeCapacity = buf.byteLength;\n }\n\n /**\n * Clears the whole BinaryStream instance.\n *\n * The write buffer and its capacity are kept, so any view previously returned by\n * {@link getWriteBuffer} is invalidated. See {@link resetWrite}.\n */\n public clear(): void {\n this.readBuffer = null;\n this.readIndex = 0;\n this.writeIndex = 0;\n }\n\n /**\n * Conventional method to reuse the stream\n * without having to create a new BinaryStream instance.\n *\n * The write buffer and its capacity are kept, so any view previously returned by\n * {@link getWriteBuffer} is invalidated. See {@link resetWrite}.\n * @param buf - The new buffer instance.\n */\n public reuse(buf: Buffer): void {\n this.readBuffer = buf;\n this.readIndex = 0;\n this.writeIndex = 0;\n }\n\n /**\n * Sets the reading index.\n * @param index - The new read index.\n */\n public setReadIndex(index: number): void {\n assert(index >= 0, 'Index must be non-negative');\n this.readIndex = index;\n }\n\n /**\n * Sets the new writing index.\n * @param index - The new write index.\n */\n public setWriteIndex(index: number): void {\n assert(index >= 0, 'Index must be non-negative');\n this.writeIndex = index;\n }\n\n /**\n * Retuns the read index.\n * @returns {number}\n */\n public getReadIndex(): number {\n return this.readIndex;\n }\n\n /**\n * Returns the write index.\n * @returns {number}\n */\n public getWriteIndex(): number {\n return this.writeIndex;\n }\n\n /**\n * Do read assertions, check if the read buffer is null.\n * @param {number} byteLength\n */\n private doReadAssertions(byteLength: number): void {\n const buf = this.readBuffer;\n if (buf === null) {\n assert.fail('Cannot read without buffer data!');\n }\n // This used to compare the *total* buffer length against the requested length and\n // ignore readIndex entirely, so it caught almost nothing: read(4) at offset 998 of a\n // 1000-byte buffer returned 2 bytes with no error, readBoolean past the end returned\n // true, and the fixed-width readers blew up with an ERR_OUT_OF_RANGE raised from\n // inside Buffer rather than an AssertionError raised here.\n if (this.readIndex + byteLength > buf.byteLength) {\n assert.fail(\n `Cannot read ${byteLength} byte(s) at offset ${this.readIndex}: ` +\n `only ${Math.max(0, buf.byteLength - this.readIndex)} left in buffer`\n );\n }\n }\n\n /**\n * Do read assertions, check if the read buffer is null.\n * @param {number|bigint} num\n * @param {number|bigint} minVal\n * @param {number|bigint} maxVal\n */\n private doWriteAssertions(num: number, minVal: number, maxVal: number): void {\n // Argument expressions are evaluated *before* the call, so the template literal used\n // to be built on every successful write - sixteen write methods route through here.\n // For writeFloat/writeDouble the bounds are doubles, so each call ran three\n // double-to-string conversions and threw the result away. Building the message only\n // on failure is worth ~2.3x on a mixed packet-encoding workload.\n //\n // `!(a && b)` rather than `a < min || a > max`: the latter silently accepts NaN,\n // which this method has always rejected.\n if (!(num >= minVal && num <= maxVal)) {\n assert.fail(`Value out of bounds: value=${num}, min=${minVal}, max=${maxVal}`);\n }\n }\n}\n\n// Default export for backward compatibility\nexport default BinaryStream;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiCA,IAAa,eAAb,MAA0B;CACtB,cAAqC;CACrC,aAAoC;CACpC;CACA,aAAqB;CACrB,gBAAwB;;;;;;;;;CAUxB,YAAmB,QAAiB,SAAiB,GAAG,kBAA0B,GAAG;EACjF,KAAK,aAAa,UAAU;EAC5B,KAAK,YAAY;EACjB,IAAI,kBAAkB,GAClB,KAAK,QAAQ,eAAe;CAEpC;;;;;CAMA,KAAY,KAAqB;EAC7B,KAAK,iBAAiB,GAAG;EACzB,OAAO,KAAK,WAAY,SAAS,KAAK,WAAY,KAAK,aAAa,GAAI;CAC5E;;;;;CAMA,MAAa,KAAuB;EAChC,MAAM,MAAM,IAAI;EAChB,KAAK,eAAe,GAAG;EAKvB,KAAK,YAAa,IAAI,KAAK,KAAK,UAAU;EAC1C,KAAK,cAAc;CACvB;;;;;CAMA,gBAAuB,OAAqB;EACxC,MAAM,aAAa,OAAO,WAAW,OAAO,MAAM;EAClD,IAAI,eAAe,GACf;EAGJ,KAAK,eAAe,UAAU;EAE9B,MAAM,eAAe,KAAK,YAAa,MAAM,OAAO,KAAK,YAAY,YAAY,MAAM;EACvF,KAAK,cAAc;CACvB;;;;;CAMA,qBAAoC;EAChC,OAAO,KAAK,KAAK,KAAK,mBAAmB,CAAC;CAC9C;;;;;CAMA,oBAA2B,KAAuB;EAC9C,KAAK,oBAAoB,IAAI,UAAU;EACvC,KAAK,MAAM,GAAG;CAClB;;;;;;;CAQA,gBAA+B;EAC3B,OAAO,KAAK,mBAAmB,CAAC,CAAC,SAAS,OAAO;CACrD;;;;;CAMA,eAAsB,KAAmB;EACrC,KAAK,oBAAoB,OAAO,WAAW,KAAK,OAAO,CAAC;EACxD,KAAK,gBAAgB,GAAG;CAC5B;;;;;CAMA,WA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