Files
Nitro_Render_V3/packages/utils/src/BinaryReader.ts
T
simoleo89 c37171a61c TS 5.7+ ArrayBuffer drift: cast where ArrayBufferLike leaked
TypeScript 5.7 split ArrayBuffer / SharedArrayBuffer at the type level
(ArrayBuffer now exposes resizable/transfer/detached etc; SharedArrayBuffer
doesn't), and parametrized the typed-array constructors so plain
Uint8Array became Uint8Array<ArrayBufferLike>.

The renderer never uses SharedArrayBuffer, so this is type-level only —
narrowing back to ArrayBuffer at the boundaries:

- BinaryReader.readBytes() / .toArrayBuffer() return the underlying
  DataView buffer; cast to ArrayBuffer.
- BinaryWriter.getBuffer() same shape.
- WsSessionCrypto.randomNonce() now returns Uint8Array<ArrayBuffer>
  (it's always backed by a plain ArrayBuffer); aesGcmEncrypt/Decrypt
  nonce parameter retyped accordingly so SubtleCrypto.encrypt accepts
  it as BufferSource.
- ArrayBufferToBase64 now accepts Uint8Array | ArrayBufferLike directly
  (pako/inflate hands back Uint8Array<ArrayBuffer> which the old
  ArrayBuffer-only signature rejected).
2026-05-11 21:09:22 +02:00

83 lines
1.6 KiB
TypeScript

import { IBinaryReader } from '@nitrots/api';
export class BinaryReader implements IBinaryReader
{
private _position: number;
private _dataView: DataView;
constructor(buffer: ArrayBuffer)
{
this._position = 0;
this._dataView = new DataView(buffer);
}
public readBytes(length: number): IBinaryReader
{
const buffer = new BinaryReader(this._dataView.buffer.slice(this._position, this._position + length) as ArrayBuffer);
this._position += length;
return buffer;
}
public readByte(): number
{
const byte = this._dataView.getInt8(this._position);
this._position++;
return byte;
}
public readShort(): number
{
const short = this._dataView.getInt16(this._position);
this._position += 2;
return short;
}
public readInt(): number
{
const int = this._dataView.getInt32(this._position);
this._position += 4;
return int;
}
public readFloat(): number
{
const float = this._dataView.getFloat32(this._position);
this._position += 4;
return float;
}
public readDouble(): number
{
const double = this._dataView.getFloat64(this._position);
this._position += 8;
return double;
}
public remaining(): number
{
return this._dataView.byteLength - this._position;
}
public toString(encoding?: string): string
{
return new TextDecoder().decode(this._dataView.buffer);
}
public toArrayBuffer(): ArrayBuffer
{
return this._dataView.buffer as ArrayBuffer;
}
}