/** * Thin wrapper around @bokuweb/zstd-wasm that defers WASM load until the * first compress/decompress call and memoises the init promise. * * The WASM binary lives alongside the package's ESM entrypoint, so Vite's * default asset handling picks it up. The extension's CSP already allows * `wasm-unsafe-eval` for the existing hash-wasm Argon2id module. */ import { compress as zstdCompress, decompress as zstdDecompress, init as zstdInit, } from "@bokuweb/zstd-wasm"; let initPromise: Promise | null = null; function ensureInit(): Promise { if (!initPromise) { initPromise = zstdInit().catch((err) => { initPromise = null; throw err; }); } return initPromise; } /** Compress bytes with zstd at the given level (default 19 per spec ยง4.1). */ export async function compressZstd(bytes: Uint8Array, level = 19): Promise { await ensureInit(); return zstdCompress(bytes, level); } /** Decompress a zstd frame. Throws if the frame is malformed. */ export async function decompressZstd(bytes: Uint8Array): Promise { await ensureInit(); return zstdDecompress(bytes); }