From 659c5b4b0fe29b9baaa315e84ba95ca053069810 Mon Sep 17 00:00:00 2001 From: nick-delirium Date: Wed, 19 Apr 2023 10:45:20 +0200 Subject: [PATCH] change(player): detect gzip file after decoding --- frontend/app/player/web/network/crypto.ts | 27 +++++++++++++---------- tracker/tracker/src/main/app/index.ts | 2 +- tracker/tracker/src/webworker/index.ts | 5 ++--- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/frontend/app/player/web/network/crypto.ts b/frontend/app/player/web/network/crypto.ts index dbb5d9a04..2bc7f641f 100644 --- a/frontend/app/player/web/network/crypto.ts +++ b/frontend/app/player/web/network/crypto.ts @@ -21,18 +21,21 @@ export function decryptSessionBytes(cypher: Uint8Array, keyString: string): Prom .then(key => crypto.subtle.decrypt({ name: "AES-CBC", iv: iv}, key, cypher)) .then((bArray: ArrayBuffer) => new Uint8Array(bArray)) .then(async (u8Array: Uint8Array) => { - const now = performance.now() - const data = gunzipSync(u8Array) - console.debug( - "Decompression time", - performance.now() - now, - 'size', - Math.floor(u8Array.byteLength/1024), - '->', - Math.floor(data.byteLength/1024), - 'kb' - ) - return data + const isGzip = u8Array[0] === 0x1F && u8Array[1] === 0x8B && u8Array[2] === 0x08; + if (isGzip) { + const now = performance.now() + const data = gunzipSync(u8Array) + console.debug( + "Decompression time", + performance.now() - now, + 'size', + Math.floor(u8Array.byteLength/1024), + '->', + Math.floor(data.byteLength/1024), + 'kb' + ) + return data + } else return u8Array }) //?? TS doesn not catch the `decrypt`` returning type } diff --git a/tracker/tracker/src/main/app/index.ts b/tracker/tracker/src/main/app/index.ts index f70d6bf90..1eb9ec844 100644 --- a/tracker/tracker/src/main/app/index.ts +++ b/tracker/tracker/src/main/app/index.ts @@ -180,7 +180,7 @@ export default class App { } else if (data.type === 'compress') { const batch = data.batch const batchSize = batch.byteLength - if (batchSize > this.compressionThreshold) { + if (batchSize > 10) { gzip(data.batch, { mtime: 0 }, (err, result) => { if (err) console.error(err) // @ts-ignore diff --git a/tracker/tracker/src/webworker/index.ts b/tracker/tracker/src/webworker/index.ts index 699602e20..a1b836502 100644 --- a/tracker/tracker/src/webworker/index.ts +++ b/tracker/tracker/src/webworker/index.ts @@ -102,7 +102,7 @@ self.onmessage = ({ data }: any): any => { if (data.type === 'compressed') { if (!sender) { - console.debug('WebWorker: sender not initialised. Received auth.') + console.debug('WebWorker: sender not initialised. Compressed batch.') initiateRestart() return } @@ -110,7 +110,7 @@ self.onmessage = ({ data }: any): any => { } if (data.type === 'uncompressed') { if (!sender) { - console.debug('WebWorker: sender not initialised. Received auth.') + console.debug('WebWorker: sender not initialised. Uncompressed batch.') initiateRestart() return } @@ -139,7 +139,6 @@ self.onmessage = ({ data }: any): any => { data.pageNo, data.timestamp, data.url, - // onBatch (batch) => sender && sender.push(batch), ) if (sendIntervalID === null) {