1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2025-12-11 09:11:50 +00:00
tobspr_shapez.io/src/js/webworkers/compression.ts
Даниїл Григор'єв 82dae1158e
Re-implement compression workers and abstraction
Implement DefaultCompression class along with a generic interface to
facilitate easy to use compression in a background thread, and make use
of this class in Storage implementation by default.
2025-06-10 16:46:28 +03:00

21 lines
622 B
TypeScript

/// <reference lib="WebWorker" />
import { encode } from "@msgpack/msgpack";
async function compress(data: unknown): Promise<Uint8Array> {
const input = new Blob([encode(data)]).stream();
const gzip = new CompressionStream("gzip");
const response = new Response(input.pipeThrough(gzip));
return new Uint8Array(await response.arrayBuffer());
}
self.addEventListener("message", async ev => {
try {
const result = await compress(ev.data);
self.postMessage({ result, error: null }, [result.buffer]);
} catch (err) {
self.postMessage({ result: null, error: err });
}
});