Skip to content

Commit

Permalink
feat: default to gzip compression instead of base64 (#1355)
Browse files Browse the repository at this point in the history
  • Loading branch information
frankh authored Aug 14, 2024
1 parent 19ca9dd commit a84dfe5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
7 changes: 7 additions & 0 deletions functional_tests/mock-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ResponseComposition, rest } from 'msw'
import { setupServer } from 'msw/lib/node'
import { RestContext } from 'msw'
import { RestRequest } from 'msw'
import { decompressSync, strFromU8 } from 'fflate'

// the request bodies in a store that we can inspect within tests.
const capturedRequests: { '/e/': any[]; '/engage/': any[]; '/decide/': any[] } = {
Expand All @@ -18,8 +19,14 @@ const handleRequest = (group: string) => (req: RestRequest, res: ResponseComposi
if (typeof body === 'string') {
try {
const b64Encoded = req.url.href.includes('compression=base64')
const gzipCompressed = req.url.href.includes('compression=gzip-js')
if (b64Encoded) {
body = JSON.parse(Buffer.from(decodeURIComponent(body.split('=')[1]), 'base64').toString())
} else if (gzipCompressed) {
const data = new Uint8Array(req._body)
const decoded = strFromU8(decompressSync(data))
console.log(decoded)
body = JSON.parse(decoded)
} else {
body = JSON.parse(decodeURIComponent(body.split('=')[1]))
}
Expand Down
2 changes: 1 addition & 1 deletion src/posthog-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ export class PostHog {
logger.error('[posthog] on_xhr_error is deprecated. Use on_request_error instead')
}

this.compression = config.disable_compression ? undefined : Compression.Base64
this.compression = config.disable_compression ? undefined : Compression.GZipJS

this.persistence = new PostHogPersistence(this.config)
this.sessionPersistence =
Expand Down

0 comments on commit a84dfe5

Please sign in to comment.