1 line
43 KiB
Plaintext
1 line
43 KiB
Plaintext
|
|
{"version":3,"file":"zstddec.cjs","sources":["../../src/libs/zstddec.ts"],"sourcesContent":["interface DecoderExports {\n memory: Uint8Array\n\n ZSTD_findDecompressedSize: (compressedPtr: number, compressedSize: number) => BigInt\n ZSTD_decompress: (\n uncompressedPtr: number,\n uncompressedSize: number,\n compressedPtr: number,\n compressedSize: number,\n ) => number\n malloc: (ptr: number) => number\n free: (ptr: number) => void\n}\n\nlet init: Promise<void>\nlet instance: { exports: DecoderExports }\nlet heap: Uint8Array\n\nconst IMPORT_OBJECT = {\n env: {\n emscripten_notify_memory_growth: function (index: number): void {\n heap = new Uint8Array(instance.exports.memory.buffer)\n },\n },\n}\n\n/**\n * ZSTD (Zstandard) decoder.\n */\nexport class ZSTDDecoder {\n init(): Promise<void> {\n if (init) return init\n\n if (typeof fetch !== 'undefined') {\n // Web.\n\n init = fetch('data:application/wasm;base64,' + wasm)\n .then((response) => response.arrayBuffer())\n .then((arrayBuffer) => WebAssembly.instantiate(arrayBuffer, IMPORT_OBJECT))\n .then(this._init)\n } else {\n // Node.js.\n\n init = WebAssembly.instantiate(Buffer.from(wasm, 'base64'), IMPORT_OBJECT).then(this._init)\n }\n\n return init\n }\n\n _init(result: WebAssembly.WebAssemblyInstantiatedSource): void {\n instance = (result.instance as unknown) as { exports: DecoderExports }\n\n IMPORT_OBJECT.env.emscripten_notify_memory_growth(0) // initialize heap.\n }\n\n decode(array: Uint8Array, uncompressedSize = 0): Uint8Array {\n if (!instance) throw new Error(`ZSTDDecoder: Await .init() before decoding.`)\n\n // Write compressed data into WASM memory.\n const compressedSize = array.byteLength\n const compressedPtr = instance.exports.malloc(compressedSize)\n heap.set(array, compressedPtr)\n\n // Decompress into WASM memory.\n uncompressedSize =\n uncompressedSize || Number(instance.exports.ZSTD_findDecompressedSize(compressedPtr, compressedSize))\n const uncompressedPtr = instance.exports.malloc(uncompressedSize)\n const actualSize = instance.exports.ZSTD_decompress(\n uncompressedPtr,\n uncompressedSize,\n compressedPtr,\n compressedSize,\n )\n\n // Read decompressed data and free WASM memory.\n const dec = heap.slice(uncompressedPtr, uncompressedPtr + actualSize)\n instance.exports.free(compressedPtr)\n instance.exports.free(uncompressedPtr)\n\n return dec\n }\n}\n\n/**\n * BSD License\n *\n * For Zstandard software\n *\n * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without modification,\n * are permitted provided that the following conditions are met:\n *\n * * Redistributions of source code must retain the above copyright notice, this\n * list of conditions and the following disclaimer.\n *\n * * Redistributions in binary form must reproduce the above copyright notice,\n * this list of conditions and the following disclaimer in the documentation\n * and/or other materials provided with the distribution.\n *\n * * Neither the name Facebook nor the names of its contributors may be used to\n * endorse or promote products derived from this software without specific\n * prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND\n * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\n * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\n * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR\n * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\n * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\n * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON\n * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISIN
|