From c37171a61c104282b71347665532ec5bbff37d5a Mon Sep 17 00:00:00 2001 From: simoleo89 Date: Mon, 11 May 2026 21:09:22 +0200 Subject: [PATCH] TS 5.7+ ArrayBuffer drift: cast where ArrayBufferLike leaked MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. 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 (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 which the old ArrayBuffer-only signature rejected). --- packages/communication/src/crypto/WsSessionCrypto.ts | 6 +++--- packages/utils/src/ArrayBufferToBase64.ts | 4 ++-- packages/utils/src/BinaryReader.ts | 4 ++-- packages/utils/src/BinaryWriter.ts | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/communication/src/crypto/WsSessionCrypto.ts b/packages/communication/src/crypto/WsSessionCrypto.ts index e92078c..ed7f814 100644 --- a/packages/communication/src/crypto/WsSessionCrypto.ts +++ b/packages/communication/src/crypto/WsSessionCrypto.ts @@ -38,17 +38,17 @@ export async function deriveAesKey(sharedSecret: ArrayBuffer): Promise +export async function aesGcmEncrypt(key: CryptoKey, nonce: Uint8Array, plaintext: ArrayBuffer): Promise { return window.crypto.subtle.encrypt({ name: 'AES-GCM', iv: nonce, tagLength: GCM_TAG_LEN * 8 }, key, plaintext); } -export async function aesGcmDecrypt(key: CryptoKey, nonce: Uint8Array, ciphertextWithTag: ArrayBuffer): Promise +export async function aesGcmDecrypt(key: CryptoKey, nonce: Uint8Array, ciphertextWithTag: ArrayBuffer): Promise { return window.crypto.subtle.decrypt({ name: 'AES-GCM', iv: nonce, tagLength: GCM_TAG_LEN * 8 }, key, ciphertextWithTag); } -export function randomNonce(): Uint8Array +export function randomNonce(): Uint8Array { const n = new Uint8Array(NONCE_LEN); window.crypto.getRandomValues(n); diff --git a/packages/utils/src/ArrayBufferToBase64.ts b/packages/utils/src/ArrayBufferToBase64.ts index aa8eb74..409bca0 100644 --- a/packages/utils/src/ArrayBufferToBase64.ts +++ b/packages/utils/src/ArrayBufferToBase64.ts @@ -1,8 +1,8 @@ -export const ArrayBufferToBase64 = (buffer: ArrayBuffer) => +export const ArrayBufferToBase64 = (buffer: ArrayBufferLike | Uint8Array) => { let binary = ''; - const bytes = new Uint8Array(buffer); + const bytes = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer); const len = bytes.byteLength; for(let i = 0; i < len; i++) (binary += String.fromCharCode(bytes[i])); diff --git a/packages/utils/src/BinaryReader.ts b/packages/utils/src/BinaryReader.ts index b0c68ac..f42b25f 100644 --- a/packages/utils/src/BinaryReader.ts +++ b/packages/utils/src/BinaryReader.ts @@ -13,7 +13,7 @@ export class BinaryReader implements IBinaryReader public readBytes(length: number): IBinaryReader { - const buffer = new BinaryReader(this._dataView.buffer.slice(this._position, this._position + length)); + const buffer = new BinaryReader(this._dataView.buffer.slice(this._position, this._position + length) as ArrayBuffer); this._position += length; @@ -77,6 +77,6 @@ export class BinaryReader implements IBinaryReader public toArrayBuffer(): ArrayBuffer { - return this._dataView.buffer; + return this._dataView.buffer as ArrayBuffer; } } diff --git a/packages/utils/src/BinaryWriter.ts b/packages/utils/src/BinaryWriter.ts index dd20ef5..554a823 100644 --- a/packages/utils/src/BinaryWriter.ts +++ b/packages/utils/src/BinaryWriter.ts @@ -89,7 +89,7 @@ export class BinaryWriter implements IBinaryWriter public getBuffer(): ArrayBuffer { - return this._buffer.buffer; + return this._buffer.buffer as ArrayBuffer; } public get position(): number