Skip to content

Commit

Permalink
feat: partially compress replay data
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra committed Sep 24, 2024
1 parent 7173f75 commit 5b87425
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 37 deletions.
52 changes: 16 additions & 36 deletions src/__tests__/extensions/replay/sessionrecording.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1988,6 +1988,13 @@ describe('SessionRecording', () => {
})

describe('when compression is active', () => {
const captureOptions = {
_batchKey: 'recordings',
_noTruncate: true,
_url: 'https://test.com/s/',
skip_client_rate_limiting: true,
}

beforeEach(() => {
posthog.config.session_recording.compress_events = true
sessionRecording.afterDecideResponse(makeDecideResponse({ sessionRecording: { endpoint: '/s/' } }))
Expand All @@ -2004,19 +2011,15 @@ describe('SessionRecording', () => {
$snapshot_data: [
{
data: expect.any(String),
cv: '2024-10',
type: 2,
},
],
$session_id: sessionId,
$snapshot_bytes: expect.any(Number),
$window_id: 'windowId',
},
{
_batchKey: 'recordings',
_noTruncate: true,
_url: 'https://test.com/s/',
skip_client_rate_limiting: true,
}
captureOptions
)
})

Expand All @@ -2029,6 +2032,7 @@ describe('SessionRecording', () => {
{
$snapshot_data: [
{
cv: '2024-10',
data: {
adds: expect.any(String),
texts: expect.any(String),
Expand All @@ -2044,12 +2048,7 @@ describe('SessionRecording', () => {
$snapshot_bytes: expect.any(Number),
$window_id: 'windowId',
},
{
_batchKey: 'recordings',
_noTruncate: true,
_url: 'https://test.com/s/',
skip_client_rate_limiting: true,
}
captureOptions
)
})

Expand All @@ -2071,19 +2070,15 @@ describe('SessionRecording', () => {
source: 8,
styleId: 1,
},
cv: '2024-10',
type: 3,
},
],
$session_id: sessionId,
$snapshot_bytes: expect.any(Number),
$window_id: 'windowId',
},
{
_batchKey: 'recordings',
_noTruncate: true,
_url: 'https://test.com/s/',
skip_client_rate_limiting: true,
}
captureOptions
)
})

Expand All @@ -2100,12 +2095,7 @@ describe('SessionRecording', () => {
$snapshot_bytes: 86,
$window_id: 'windowId',
},
{
_batchKey: 'recordings',
_noTruncate: true,
_url: 'https://test.com/s/',
skip_client_rate_limiting: true,
}
captureOptions
)
})

Expand All @@ -2129,12 +2119,7 @@ describe('SessionRecording', () => {
$snapshot_bytes: 58,
$window_id: 'windowId',
},
{
_batchKey: 'recordings',
_noTruncate: true,
_url: 'https://test.com/s/',
skip_client_rate_limiting: true,
}
captureOptions
)
})

Expand All @@ -2157,12 +2142,7 @@ describe('SessionRecording', () => {
$snapshot_bytes: 69,
$window_id: 'windowId',
},
{
_batchKey: 'recordings',
_noTruncate: true,
_url: 'https://test.com/s/',
skip_client_rate_limiting: true,
}
captureOptions
)
})
})
Expand Down
7 changes: 6 additions & 1 deletion src/extensions/replay/sessionrecording.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,12 @@ export type compressedEvent =
export type compressedEventWithTime = compressedEvent & {
timestamp: number
delay?: number
// marker for compression version
cv: '2024-10'
}

function gzipToString(data: unknown): string {
return strFromU8(gzipSync(strToU8(JSON.stringify(data))))
return strFromU8(gzipSync(strToU8(JSON.stringify(data))), true)
}

// rrweb's packer takes an event and returns a string or the reverse on unpact,
Expand All @@ -140,11 +142,13 @@ function compressEvent(event: eventWithTime, ph: PostHog): eventWithTime | compr
return {
...event,
data: gzipToString(event.data),
cv: '2024-10',
}
}
if (event.type === EventType.IncrementalSnapshot && event.data.source === IncrementalSource.Mutation) {
return {
...event,
cv: '2024-10',
data: {
...event.data,
texts: gzipToString(event.data.texts),
Expand All @@ -157,6 +161,7 @@ function compressEvent(event: eventWithTime, ph: PostHog): eventWithTime | compr
if (event.type === EventType.IncrementalSnapshot && event.data.source === IncrementalSource.StyleSheetRule) {
return {
...event,
cv: '2024-10',
data: {
...event.data,
adds: gzipToString(event.data.adds),
Expand Down

0 comments on commit 5b87425

Please sign in to comment.