From 2d7801eb6731bf074c1622dd2270c016405d2cfc Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Mon, 20 Feb 2023 15:38:22 +0000 Subject: [PATCH] Add gzip decompression helper. --- src/utils/compressionUtils.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/utils/compressionUtils.js b/src/utils/compressionUtils.js index 5f482c2e..29f1c88f 100644 --- a/src/utils/compressionUtils.js +++ b/src/utils/compressionUtils.js @@ -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; + } +};