From b6895a86d904407cba0c713194296527842acd71 Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Fri, 3 Feb 2023 16:45:13 +0000 Subject: [PATCH] Add helper utility to compress data. --- package.json | 1 + src/utils/compressionUtils.js | 18 ++++++++++++++++++ yarn.lock | 5 +++++ 3 files changed, 24 insertions(+) create mode 100644 src/utils/compressionUtils.js diff --git a/package.json b/package.json index 08f28340d3..b989e7a219 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "hotkeys-js": "3.10.1", "javascript-time-ago": "2.5.9", "mux.js": "6.2.0", + "pako": "2.1.0", "shaka-player": "4.3.4", "stream-browserify": "3.0.0", "vue": "3.2.47", diff --git a/src/utils/compressionUtils.js b/src/utils/compressionUtils.js new file mode 100644 index 0000000000..5f482c2ece --- /dev/null +++ b/src/utils/compressionUtils.js @@ -0,0 +1,18 @@ +export const compressGzip = async data => { + // Firefox does not support CompressionStream yet + if (typeof CompressionStream !== "undefined") { + let bytes = new TextEncoder().encode(data); + // eslint-disable-next-line no-undef + const cs = new CompressionStream("gzip"); + const writer = cs.writable.getWriter(); + writer.write(bytes); + writer.close(); + const compAb = await new Response(cs.readable).arrayBuffer(); + bytes = new Uint8Array(compAb); + + return bytes; + } else { + const pako = require("pako"); + return pako.gzip(data); + } +}; diff --git a/yarn.lock b/yarn.lock index 375e73f8aa..1c2ee32ae3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4011,6 +4011,11 @@ p-locate@^5.0.0: dependencies: p-limit "^3.0.2" +pako@2.1.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/pako/-/pako-2.1.0.tgz#266cc37f98c7d883545d11335c00fbd4062c9a86" + integrity sha512-w+eufiZ1WuJYgPXbV/PO3NCMEc3xqylkKHzp8bxp1uW4qaSNQUkwmLLEc3kKsfz8lpV1F8Ht3U1Cm+9Srog2ug== + parent-module@^1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/parent-module/-/parent-module-1.0.1.tgz#691d2709e78c79fae3a156622452d00762caaaa2"