Decompression caching and preview links
All checks were successful
Publish Library / Build NPM Project (push) Successful in 10s
Publish Library / Tag Version (push) Successful in 8s

This commit is contained in:
2026-08-22 16:52:07 -04:00
parent aedb117002
commit ffdca81c78
6 changed files with 206 additions and 54 deletions

32
src/decompress-worker.js Normal file
View File

@@ -0,0 +1,32 @@
'use strict';
import {parentPort} from 'node:worker_threads';
import {decompress as lzmaDecompress} from 'lzma1';
import {ZstdCodec} from 'zstd-codec';
let zstdStreamingPromise = null;
function getZstd() {
if (!zstdStreamingPromise) {
zstdStreamingPromise = new Promise(resolve => ZstdCodec.run(zstd => resolve(new zstd.Streaming())));
}
return zstdStreamingPromise;
}
/** Rebuilds a full 13-byte "alone" LZMA header from libzim's truncated 5-byte one (unknown size). */
function toLzmaAloneStream(body) {
const header = Buffer.concat([body.subarray(0, 5), Buffer.alloc(8, 0xff)]);
return Buffer.concat([header, body.subarray(5)]);
}
parentPort.on('message', async ({id, compType, body}) => {
try {
const buf = Buffer.from(body);
const data = compType === 4
? Buffer.from(lzmaDecompress(toLzmaAloneStream(buf)))
: Buffer.from((await getZstd()).decompress(new Uint8Array(buf)));
parentPort.postMessage({id, data}, [data.buffer]);
} catch (e) {
parentPort.postMessage({id, error: e.message});
}
});