reuse fixed size DataViews

This commit is contained in:
2025-01-22 23:10:42 -06:00
parent c33a2dfd33
commit 7d7b92678b
3 changed files with 3 additions and 3 deletions
+1 -1
View File
@@ -1,3 +1,4 @@
const header = new DataView(new ArrayBuffer(4));
export default (id: number, chunkData: Uint8Array, hasSubchunks: boolean): Uint8Array => {
if (!Number.isInteger(id)) {
throw new Error('chunk id must be an integer');
@@ -11,7 +12,6 @@ export default (id: number, chunkData: Uint8Array, hasSubchunks: boolean): Uint8
throw new Error('chunkData can not be greater than 32767 bytes');
}
const header = new DataView(new ArrayBuffer(4));
header.setUint16(0, id, true);
const hasSubChunksBit = (hasSubchunks ? 1 : 0) << 15;
header.setUint16(2, hasSubChunksBit + chunkData.length, true);
@@ -1,7 +1,7 @@
import chunk from '../chunk';
const buf = new DataView(new ArrayBuffer(8));
export default (timestamp: bigint): Uint8Array => {
const buf = new DataView(new ArrayBuffer(8));
buf.setBigUint64(0, BigInt(timestamp), true);
return chunk(0x0006, new Uint8Array(buf.buffer), false);
};
+1 -1
View File
@@ -1,5 +1,6 @@
import chunk from './chunk';
const packetHeader = new DataView(new ArrayBuffer(12));
export default (
timestamp: bigint,
versionHigh: number,
@@ -33,7 +34,6 @@ export default (
throw new Error('frame packet count must be >= 0 and <= 255');
}
const packetHeader = new DataView(new ArrayBuffer(12));
packetHeader.setBigUint64(0, BigInt(timestamp), true);
packetHeader.setUint8(8, versionHigh);
packetHeader.setUint8(9, versionLow);