12345678910import {11 compress as zstdCompress,12 decompress as zstdDecompress,13 init as zstdInit,14} from "@bokuweb/zstd-wasm";15import { IMPORT_LIMITS, importTooLarge } from "../shared/import-limits";1617let initPromise: Promise<void> | null = null;1819function ensureInit(): Promise<void> {20 if (!initPromise) {21 initPromise = zstdInit().catch((err) => {22 initPromise = null;23 throw err;24 });25 }26 return initPromise;27}282930export async function compressZstd(bytes: Uint8Array, level = 19): Promise<Uint8Array> {31 await ensureInit();32 return zstdCompress(bytes, level);33}343536373839404142export async function decompressZstd(bytes: Uint8Array): Promise<Uint8Array> {43 await ensureInit();44 const out = zstdDecompress(bytes);45 if (out.byteLength > IMPORT_LIMITS.MAX_DECOMPRESSED_BYTES) {46 throw importTooLarge(47 `decompressed payload ${out.byteLength} bytes exceeds ${IMPORT_LIMITS.MAX_DECOMPRESSED_BYTES}`,48 );49 }50 return out;51}