From e79ebc242993c1ee5083efc0e9b0bc9ebbe5c999 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 + pnpm-lock.yaml | 7 +++++++ src/utils/compressionUtils.js | 18 ++++++++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 src/utils/compressionUtils.js diff --git a/package.json b/package.json index 8519e11d..504d6b1b 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "linkify-html": "4.1.1", "linkifyjs": "4.1.1", "mux.js": "6.3.0", + "pako": "2.1.0", "qrcode": "^1.5.3", "shaka-player": "4.4.1", "stream-browserify": "3.0.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5375c102..4ecea5c9 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -38,6 +38,9 @@ dependencies: mux.js: specifier: 6.3.0 version: 6.3.0 + pako: + specifier: 2.1.0 + version: 2.1.0 qrcode: specifier: ^1.5.3 version: 1.5.3 @@ -4177,6 +4180,10 @@ packages: engines: {node: '>=6'} dev: false + /pako@2.1.0: + resolution: {integrity: sha512-w+eufiZ1WuJYgPXbV/PO3NCMEc3xqylkKHzp8bxp1uW4qaSNQUkwmLLEc3kKsfz8lpV1F8Ht3U1Cm+9Srog2ug==} + dev: false + /parent-module@1.0.1: resolution: {integrity: sha512-GQ2EWRpQV8/o+Aw8YqtfZZPfNRWZYkbidE9k5rpl/hC3vtHHBfGm2Ifi6qWV+coDGkrUKZAxE3Lot5kcsRlh+g==} engines: {node: '>=6'} diff --git a/src/utils/compressionUtils.js b/src/utils/compressionUtils.js new file mode 100644 index 00000000..5f482c2e --- /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); + } +};