mirror of
https://github.com/tobspr/shapez.io.git
synced 2025-12-11 09:11:50 +00:00
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.
21 lines
622 B
TypeScript
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 });
|
|
}
|
|
});
|