Skip to content

Commit fcdd0b9

Browse files
authored
feat: remove lz compression (#654)
We've wanted to remove LZ compression for c2 years. #155 it is 5%+ of the bundle size. Let's remove it compression options are provided by decide, decide returns a harcoded list, and the library prefers gzip if it is present. So this can only negatively affect people using very, very old self-hosted versions and the latest pothog-js. And even then it would mean they would switch from lz64 to base64 so things would carry on working.
1 parent 9f6e0bd commit fcdd0b9

File tree

12 files changed

+64
-606
lines changed

12 files changed

+64
-606
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ coverage
2626
react/dist
2727
test-result.json
2828
yarn-error.log
29-
/.eslintcache
29+
stats.html
30+
.eslintcache

cypress/e2e/capture.cy.js

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
/// <reference types="cypress" />
22
import { version } from '../../package.json'
33

4-
import { getBase64EncodedPayload, getGzipEncodedPayload, getLZStringEncodedPayload } from '../support/compression'
4+
import { getBase64EncodedPayload, getGzipEncodedPayload } from '../support/compression'
55

66
const urlWithVersion = new RegExp(`&ver=${version}`)
77

88
describe('Event capture', () => {
99
given('options', () => ({}))
1010
given('sessionRecording', () => false)
11-
given('supportedCompression', () => ['gzip', 'lz64'])
11+
given('supportedCompression', () => ['gzip-js'])
1212

1313
// :TRICKY: Use a custom start command over beforeEach to deal with given2 not being ready yet.
1414
const start = ({ waitForDecide = true } = {}) => {
@@ -332,47 +332,7 @@ describe('Event capture', () => {
332332
})
333333

334334
describe('decoding the payload', () => {
335-
it('contains the correct headers and payload after an event', () => {
336-
start()
337-
338-
// Pageview will be sent immediately
339-
cy.wait('@capture').should(({ request, url }) => {
340-
expect(request.headers).to.eql({
341-
'Content-Type': 'application/x-www-form-urlencoded',
342-
})
343-
344-
expect(url).to.match(urlWithVersion)
345-
const captures = getBase64EncodedPayload(request)
346-
347-
expect(captures['event']).to.equal('$pageview')
348-
})
349-
350-
cy.get('[data-cy-custom-event-button]').click()
351-
cy.get('[data-cy-custom-event-button]').click()
352-
cy.phCaptures().should('have.length', 5)
353-
cy.phCaptures().should('include', '$autocapture')
354-
cy.phCaptures().should('include', 'custom-event')
355-
356-
cy.wait('@capture').should(({ request, url }) => {
357-
expect(request.headers).to.eql({
358-
'Content-Type': 'application/x-www-form-urlencoded',
359-
})
360-
361-
expect(url).to.match(urlWithVersion)
362-
const captures = getLZStringEncodedPayload(request)
363-
364-
expect(captures.map(({ event }) => event)).to.deep.equal([
365-
'$autocapture',
366-
'custom-event',
367-
'$autocapture',
368-
'custom-event',
369-
])
370-
})
371-
})
372-
373335
describe('gzip-js supported', () => {
374-
given('supportedCompression', () => ['gzip-js'])
375-
376336
it('contains the correct payload after an event', () => {
377337
start()
378338
// Pageview will be sent immediately

cypress/support/compression.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
import * as fflate from 'fflate'
2-
import { LZString } from '../../src/lz-string'
32

43
export function getBase64EncodedPayload(request) {
54
const data = decodeURIComponent(request.body.match(/data=(.*)/)[1])
65
return JSON.parse(Buffer.from(data, 'base64'))
76
}
87

9-
export function getLZStringEncodedPayload(request) {
10-
const data = decodeURIComponent(request.body.match(/data=(.*)&compression=lz64/)[1])
11-
return JSON.parse(LZString.decompressFromBase64(data))
12-
}
13-
148
export async function getGzipEncodedPayload(requestBody) {
159
const data = new Uint8Array(await requestBody.arrayBuffer())
1610
const decoded = fflate.strFromU8(fflate.decompressSync(data))

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
"prettier": "^2.7.1",
8383
"rollup": "^2.77.0",
8484
"rollup-plugin-dts": "^4.2.2",
85+
"rollup-plugin-visualizer": "^5.9.0",
8586
"rrweb": "^1.1.3",
8687
"rrweb2": "npm:[email protected]",
8788
"sinon": "9.0.2",

rollup.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import typescript from '@rollup/plugin-typescript'
55
import dts from 'rollup-plugin-dts'
66
import pkg from './package.json'
77
import terser from '@rollup/plugin-terser'
8+
import { visualizer } from 'rollup-plugin-visualizer'
89

910
const extensions = ['.js', '.jsx', '.ts', '.tsx']
1011
const plugins = [
@@ -17,6 +18,7 @@ const plugins = [
1718
presets: ['@babel/preset-env'],
1819
}),
1920
terser({ toplevel: true }),
21+
visualizer(),
2022
]
2123

2224
export default [

src/__tests__/__snapshots__/compression.js.snap

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,3 @@ Array [
7373
},
7474
]
7575
`;
76-
77-
exports[`compressData() handles lz64 1`] = `
78-
Array [
79-
Object {
80-
"compression": "lz64",
81-
"data": "N4IgNghgTg5gpgfQNZwJ4gFwggIwMa4H6EnFlEWmXlW033WN1MPNusctfvec/99BvYQJFDRE8VLEzJs6XMULl81UrUr1WzTo17t+3QeNHTh8yYtnLN63asPbj+09cv3zz8xABfIA",
82-
},
83-
Object {
84-
"method": "POST",
85-
},
86-
]
87-
`;

src/__tests__/compression.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,18 @@ describe('decideCompression()', () => {
1313
})
1414

1515
it('returns gzip-js if all compressions supported', () => {
16-
given('compressionSupport', () => ({ lz64: true, 'gzip-js': true }))
16+
given('compressionSupport', () => ({
17+
'gzip-js': true,
18+
'a different thing that is either deprecated or new': true,
19+
}))
1720

1821
expect(given.subject).toEqual('gzip-js')
1922
})
2023

21-
it('returns lz64 if supported', () => {
22-
given('compressionSupport', () => ({ lz64: true, 'gzip-js': false }))
24+
it('returns base64 if only unexpected compression is received', () => {
25+
given('compressionSupport', () => ({ 'the new compression that is not supported yet': true }))
2326

24-
expect(given.subject).toEqual('lz64')
27+
expect(given.subject).toEqual('base64')
2528
})
2629
})
2730

@@ -37,12 +40,6 @@ describe('compressData()', () => {
3740
expect(given.subject).toMatchSnapshot()
3841
})
3942

40-
it('handles lz64', () => {
41-
given('compression', () => 'lz64')
42-
43-
expect(given.subject).toMatchSnapshot()
44-
})
45-
4643
it('handles gzip-js', () => {
4744
given('compression', () => 'gzip-js')
4845

@@ -67,7 +64,7 @@ describe('Payload Compression', () => {
6764
_prepare_callback: sandbox.spy((callback) => callback),
6865
_send_request: sandbox.spy((url, params, options, callback) => {
6966
if (url === 'https://test.com/decide/?v=3') {
70-
callback({ config: { enable_collect_everything: true }, supportedCompression: ['lz64'] })
67+
callback({ config: { enable_collect_everything: true }, supportedCompression: ['gzip-js'] })
7168
} else {
7269
throw new Error('Should not get here')
7370
}
@@ -112,7 +109,7 @@ describe('Payload Compression', () => {
112109
it('should save supported compression in instance', () => {
113110
new Decide(lib).call()
114111
autocapture.init(lib)
115-
expect(lib.compression).toEqual({ lz64: true })
112+
expect(lib.compression).toEqual({ 'gzip-js': true })
116113
})
117114
})
118115
})

src/compression.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
import { LZString } from './lz-string'
21
import { gzipSync, strToU8 } from 'fflate'
32
import { _base64Encode } from './utils'
43
import { Compression, CompressionData, XHROptions } from './types'
54

65
export function decideCompression(compressionSupport: Partial<Record<Compression, boolean>>): Compression {
76
if (compressionSupport[Compression.GZipJS]) {
87
return Compression.GZipJS
9-
} else if (compressionSupport[Compression.LZ64]) {
10-
return Compression.LZ64
118
} else {
129
return Compression.Base64
1310
}
@@ -18,9 +15,7 @@ export function compressData(
1815
jsonData: string,
1916
options: XHROptions
2017
): [CompressionData | Uint8Array, XHROptions] {
21-
if (compression === Compression.LZ64) {
22-
return [{ data: LZString.compressToBase64(jsonData), compression: Compression.LZ64 }, options]
23-
} else if (compression === Compression.GZipJS) {
18+
if (compression === Compression.GZipJS) {
2419
// :TRICKY: This returns an UInt8Array. We don't encode this to a string - returning a blob will do this for us.
2520
return [
2621
gzipSync(strToU8(jsonData), { mtime: 0 }),

0 commit comments

Comments
 (0)