import { HexclaveAssertionError } from "./errors"; /** * Ensures a Uint8Array is backed by a regular ArrayBuffer (not SharedArrayBuffer). * * TypeScript 5.7+ made typed arrays generic over their buffer type. Bare `Uint8Array` * defaults to `Uint8Array`, which includes SharedArrayBuffer. Web Crypto * APIs require `BufferSource` which only accepts `ArrayBufferView`. This * function narrows the type using an instanceof guard, creating a same-buffer view * (zero-copy) when the buffer is already an ArrayBuffer. */ export function toArrayBufferBacked(arr: Uint8Array): Uint8Array { if (arr.buffer instanceof SharedArrayBuffer) { throw new HexclaveAssertionError("SharedArrayBuffer-backed Uint8Arrays are not supported in this context"); } return new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength); }