// Based off of `zstd` https://deno.land/x/zstd_wasm@0.0.20/deno/zstd.ts?source by @bokuweb import Module from './zstd.deno.js'; const initialized = (() => new Promise((resolve) => { Module.onRuntimeInitialized = resolve; }))(); let initWASM: Uint8Array; export const getWASM = async () => { if (initWASM) return initWASM; const { source } = await import("./zstd.encoded.wasm.ts"); initWASM = await source(); return initWASM; }; let initResolved = false; /** * Initialize Zstd from either a precompiled module or the portable embedded bytes. * * Emscripten checks `Module.instantiateWasm` before its normal * `WebAssembly.instantiate(bytes, imports)` path. Cloudflare supplies a compiled * `WebAssembly.Module`, so instantiate it directly and hand the instance back to * Emscripten without compiling WASM at request time. */ export const init = async (wasmModule?: WebAssembly.Module) => { if (initResolved) return; if (wasmModule) { Module["instantiateWasm"] = ( imports: WebAssembly.Imports, receiveInstance: (instance: WebAssembly.Instance, module: WebAssembly.Module) => void, ) => { const instance = new WebAssembly.Instance(wasmModule, imports); receiveInstance(instance, wasmModule); return instance.exports; }; Module["init"](wasmModule); } else { const bytes = await getWASM(); Module["init"](bytes); } await initialized; initResolved = true; }; export const isError = async (code: number): Promise => { await init(); const _isError = Module["_ZSTD_isError"]; return _isError(code); }; const compressBound = async (size: number): Promise => { await init(); const bound = Module["_ZSTD_compressBound"]; return bound(size); }; export const createCCtx = async (): Promise => { await init(); return Module["_ZSTD_createCCtx"](); }; export const freeCCtx = async (cctx: number) => { await init(); return Module["_ZSTD_freeCCtx"](cctx); }; export const compressUsingDict = async ( cctx: number, buf: ArrayBuffer, dict: ArrayBuffer, level?: number ) => { await init(); const bound = await compressBound(buf.byteLength); const malloc = Module["_malloc"]; const compressed = malloc(bound); const src = malloc(buf.byteLength); Module.HEAP8.set(buf, src); const pdict = malloc(dict.byteLength); Module.HEAP8.set(dict, pdict); const free = Module["_free"]; try { const _compress = Module["_ZSTD_compress_usingDict"]; const sizeOrError = _compress( cctx, compressed, bound, src, buf.byteLength, pdict, dict.byteLength, level ?? 3 ); if (await isError(sizeOrError)) { throw new Error(`Failed to compress with code ${sizeOrError}`); } const data = new Uint8Array( Module.HEAPU8.buffer, compressed, sizeOrError ).slice(); free(compressed, bound); free(src, buf.byteLength); free(pdict, dict.byteLength); return data; } catch (e) { free(compressed, bound); free(src, buf.byteLength); free(pdict, dict.byteLength); throw e; } }; export const compress = async (buf: BufferSource, level?: number) => { await init(); const bound = await compressBound(buf.byteLength); const malloc = Module["_malloc"]; const compressed = malloc(bound); const src = malloc(buf.byteLength); Module.HEAP8.set(buf, src); const free = Module["_free"]; try { const _compress = Module["_ZSTD_compress"]; const sizeOrError = _compress( compressed, bound, src, buf.byteLength, level ?? 3 ); if (await isError(sizeOrError)) { throw new Error(`Failed to compress with code ${sizeOrError}`); } const data = new Uint8Array( Module.HEAPU8.buffer, compressed, sizeOrError ).slice(); free(compressed, bound); free(src, buf.byteLength); return data; } catch (e) { free(compressed, bound); free(src, buf.byteLength); throw e; } }; const getFrameContentSize = (src: number, size: number): number => { const getSize = Module["_ZSTD_getFrameContentSize"]; return getSize(src, size); }; export const createDCtx = async (): Promise => { await init(); return Module["_ZSTD_createDCtx"](); }; export const freeDCtx = async (dctx: number) => { await init(); return Module["_ZSTD_freeDCtx"](dctx); }; export const decompressUsingDict = async ( dctx: number, buf: ArrayBuffer, dict: ArrayBuffer, opts: DecompressOption = { defaultHeapSize: 1024 * 1024 } ): Promise => { await init(); const malloc = Module["_malloc"]; const src = malloc(buf.byteLength); Module.HEAP8.set(buf, src); const pdict = malloc(dict.byteLength); Module.HEAP8.set(dict, pdict); const contentSize = getFrameContentSize(src, buf.byteLength); const size = contentSize === -1 ? opts.defaultHeapSize : contentSize; const free = Module["_free"]; const heap = malloc(size); try { const _decompress = Module["_ZSTD_decompress_usingDict"]; const sizeOrError = _decompress( dctx, heap, size, src, buf.byteLength, pdict, dict.byteLength ); if (await isError(sizeOrError)) { throw new Error(`Failed to compress with code ${sizeOrError}`); } const data = new Uint8Array( Module.HEAPU8.buffer, heap, sizeOrError ).slice(); free(heap, size); free(src, buf.byteLength); free(pdict, dict.byteLength); return data; } catch (e) { free(heap, size); free(src, buf.byteLength); free(pdict, dict.byteLength); throw e; } }; export type DecompressOption = { defaultHeapSize?: number; }; export const decompress = async ( buf: BufferSource, opts: DecompressOption = { defaultHeapSize: 1024 * 1024 } ): Promise => { await init(); const malloc = Module["_malloc"]; const src = malloc(buf.byteLength); Module.HEAP8.set(buf, src); const contentSize = getFrameContentSize(src, buf.byteLength); const size = contentSize === -1 ? opts.defaultHeapSize : contentSize; const free = Module["_free"]; const heap = malloc(size); try { const _decompress = Module["_ZSTD_decompress"]; const sizeOrError = _decompress(heap, size, src, buf.byteLength); if (await isError(sizeOrError)) { throw new Error(`Failed to compress with code ${sizeOrError}`); } const data = new Uint8Array( Module.HEAPU8.buffer, heap, sizeOrError ).slice(); free(heap, size); free(src, buf.byteLength); return data; } catch (e) { free(heap, size); free(src, buf.byteLength); throw e; } };