Add gzip decompression helper.

This commit is contained in:
Kavin 2023-02-20 15:38:22 +00:00
parent 1265044854
commit 2d7801eb67
No known key found for this signature in database
GPG key ID: 6E4598CA5C92C41F

View file

@ -16,3 +16,23 @@ export const compressGzip = async data => {
return pako.gzip(data);
}
};
export const decompressGzip = async compressedData => {
// Firefox does not support DecompressionStream yet
if (typeof DecompressionStream !== "undefined") {
// eslint-disable-next-line no-undef
const ds = new DecompressionStream("gzip");
const writer = ds.writable.getWriter();
writer.write(compressedData);
writer.close();
const decompAb = await new Response(ds.readable).arrayBuffer();
const bytes = new Uint8Array(decompAb);
return new TextDecoder().decode(bytes);
} else {
const pako = require("pako");
const inflated = pako.inflate(compressedData, { to: "string" });
return inflated;
}
};