Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(replay): read zstd-compressed cv payloads in the player alongside gzip (#68545)

Robbie7bb94f4c58d2
diff --git a/common/replay-shared/package.json b/common/replay-shared/package.json
index 9b80550f03e9..8dcc2d10d02f 100644
--- a/common/replay-shared/package.json
+++ b/common/replay-shared/package.json
@@ -12,6 +12,7 @@
},
"dependencies": {
"fflate": "^0.8.2",
+ "fzstd": "^0.1.1",
"hls.js": "^1.5.7",
"posthog-js": "catalog:"
},
diff --git a/common/replay-shared/src/snapshot-processing/decompress.test.ts b/common/replay-shared/src/snapshot-processing/decompress.test.ts
new file mode 100644
index 000000000000..70c29dc6ba7a
--- /dev/null
+++ b/common/replay-shared/src/snapshot-processing/decompress.test.ts
@@ -0,0 +1,66 @@
+import { gzipSync, strFromU8, strToU8 } from 'fflate'
+import { zstdCompressSync } from 'node:zlib'
+
+import { ReplayTelemetry } from '../telemetry'
+import { decompressEvent } from './decompress'
+import { clearThrottle } from './throttle-capturing'
+
+const FULL_SNAPSHOT_DATA = { node: { id: 1, tag: 'div' }, initialOffset: { left: 0, top: 16 } }
+const TEXTS = [{ id: 5, value: 'hello' }]
+
+const toWire = (bytes: Uint8Array): string => strFromU8(bytes, true)
+const gzipWire = (value: unknown): string => toWire(gzipSync(strToU8(JSON.stringify(value))))
+// Fixtures compress via node:zlib (tests run in node); shipped code stays on decompress-only fzstd.
+const zstdWire = (value: unknown): string => toWire(zstdCompressSync(strToU8(JSON.stringify(value))))
+
+describe('decompressEvent', () => {
+ let telemetry: ReplayTelemetry
+
+ beforeEach(() => {
+ clearThrottle()
+ telemetry = { capture: jest.fn(), captureException: jest.fn() }
+ })
+
+ it.each([
+ ['gzip (SDK capture format)', gzipWire(FULL_SNAPSHOT_DATA)],
+ ['zstd (ml-mirror native anonymizer format)', zstdWire(FULL_SNAPSHOT_DATA)],
+ ])('decompresses a full snapshot compressed as %s', (_label: string, wire: string) => {
+ const ev = { cv: '2024-10', type: 2, timestamp: 1, data: wire }
+ const result = decompressEvent(ev, 'session-1', telemetry) as any
+ expect(result.data).toEqual(FULL_SNAPSHOT_DATA)
+ expect(telemetry.captureException).not.toHaveBeenCalled()
+ })
+
+ it('decompresses a mutation mixing zstd and gzip fields in one event', () => {
+ // The anonymizer re-emits only the fields it scrubs, so post-scrub events carry zstd
+ // `texts`/`adds` next to untouched gzip `removes`/`attributes`.
+ const ev = {
+ cv: '2024-10',
+ type: 3,
+ timestamp: 1,
+ data: {
+ source: 0,
+ texts: zstdWire(TEXTS),
+ adds: zstdWire([]),
+ attributes: gzipWire([{ id: 9, attributes: { class: 'a' } }]),
+ removes: gzipWire([{ parentId: 1, id: 2 }]),
+ },
+ }
+ const result = decompressEvent(ev, 'session-2', telemetry) as any
+ expect(result.data).toEqual({
+ source: 0,
+ texts: TEXTS,
+ adds: [],
+ attributes: [{ id: 9, attributes: { class: 'a' } }],
+ removes: [{ parentId: 1, id: 2 }],
+ })
+ expect(telemetry.captureException).not.toHaveBeenCalled()
+ })
+
+ it('passes the event through and reports when the payload is neither gzip nor zstd', () => {
+ const ev = { cv: '2024-10', type: 2, timestamp: 1, data: 'not a compressed frame' }
+ const result = decompressEvent(ev, 'session-3', telemetry)
+ expect(result).toBe(ev)
+ expect(telemetry.captureException).toHaveBeenCalledTimes(1)
+ })
+})
diff --git a/common/replay-shared/src/snapshot-processing/decompress.ts b/common/replay-shared/src/snapshot-processing/decompress.ts
index 59717890764c..6decf9997157 100644
--- a/common/replay-shared/src/snapshot-processing/decompress.ts
+++ b/common/replay-shared/src/snapshot-processing/decompress.ts
@@ -1,4 +1,5 @@
import { gunzipSync, strFromU8, strToU8 } from 'fflate'
+import { decompress as zstdDecompressSync } from 'fzstd'
import { EventType, IncrementalSource } from 'posthog-js/rrweb-types'
import { ReplayTelemetry } from '../telemetry'
@@ -15,11 +16,26 @@ function isCompressedEvent(ev: unknown): ev is CompressedEventWithTime {
return typeof ev === 'object' && ev !== null && 'cv' in ev
}
+// dispatch to either gzip or zstd based on their standard magic bytes prefix
+function decompressSync(raw: Uint8Array): Uint8Array {
+ if (raw[0] === 0x1f && raw[1] === 0x8b) {
+ return gunzipSync(raw)
+ }
+ if (raw[0] === 0x28 && raw[1] === 0xb5 && raw[2] === 0x2f && raw[3] === 0xfd) {
+ return zstdDecompressSync(raw)
+ }
+ throw new Error(
+ `cv payload is neither gzip nor zstd (leading bytes ${Array.from(raw.slice(0, 4), (b) =>
+ b.toString(16).padStart(2, '0')
+ ).join(' ')})`
+ )
+}
+
function unzip(compressedStr: string | undefined): any {
if (!compressedStr) {
return undefined
}
- return JSON.parse(strFromU8(gunzipSync(strToU8(compressedStr, true))))
+ return JSON.parse(strFromU8(decompressSync(strToU8(compressedStr, true))))
}
export function decompressEvent(ev: unknown, sessionRecordingId: string, telemetry: ReplayTelemetry): unknown {
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index e17b9059f973..5280d352bb54 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -379,6 +379,9 @@ importers:
fflate:
specifier: ^0.8.2
version: 0.8.2
+ fzstd:
+ specifier: ^0.1.1
+ version: 0.1.1
hls.js:
specifier: ^1.5.7
version: 1.5.15
@@ -15782,6 +15785,9 @@ packages:
resolution: {integrity: sha512-plz8RVjfcDedTGfVngWH1jmJvBvAwi1v2jecfDerbEnMcmOYUEEwKFTHbNoCiYyzaK2Ws8lABkTCcRSqCY1q4w==}
engines: {node: '>=10'}
+ resolution: {integrity: sha512-dkuVSOKKwh3eas5VkJy1AW1vFpet8TA/fGmVA5krThl8YcOVE/8ZIoEA1+U1vEn5ckxxhLirSdY837azmbaNHA==}
+
resolution: {integrity: sha512-gSaYYIO1Y3wUtdfHmjDUZ8LWaxJQpiavzbF5Kq53akSzvmVg0RfyOcFDbO1KJ/KCGRFz2qG+lS81F0nkr7cRJA==}
engines: {node: '>=10'}
@@ -36795,6 +36801,8 @@ snapshots:
+
dependencies:
abort-controller: 3.0.0