Handle fragmented replay events in recording and playback
This commit is contained in:
1 parent
4987e0ca01
commit
15038b09d9
6 files changed
+345
-34
No files matched your search
@@ -0,0 +1,84 @@
|
||||
import { expect, test } from 'vitest';
|
||||
import {
|
||||
getReplayEventCount,
|
||||
hasReplayFullSnapshot,
|
||||
REPLAY_EVENT_FRAGMENT_TYPE,
|
||||
restoreReplayEventFragments,
|
||||
} from './replay';
|
||||
|
||||
test('hasReplayFullSnapshot returns true when a full snapshot is present', () => {
|
||||
expect(hasReplayFullSnapshot([{ type: 4 }, { type: 2 }, { type: 3 }])).toBe(true);
|
||||
});
|
||||
|
||||
test('hasReplayFullSnapshot returns false when only incremental events are present', () => {
|
||||
expect(hasReplayFullSnapshot([{ type: 4 }, { type: 3 }, { type: 3 }])).toBe(false);
|
||||
});
|
||||
|
||||
test('hasReplayFullSnapshot handles missing events', () => {
|
||||
expect(hasReplayFullSnapshot(null)).toBe(false);
|
||||
expect(hasReplayFullSnapshot(undefined)).toBe(false);
|
||||
});
|
||||
|
||||
test('restoreReplayEventFragments restores fragmented events', () => {
|
||||
const fullSnapshot = {
|
||||
type: 2,
|
||||
timestamp: 1781553116151,
|
||||
data: {
|
||||
node: {
|
||||
type: 0,
|
||||
childNodes: [{ type: 2, tagName: 'html' }],
|
||||
},
|
||||
},
|
||||
};
|
||||
const serialized = JSON.stringify(fullSnapshot);
|
||||
const splitAt = Math.floor(serialized.length / 2);
|
||||
|
||||
const events = restoreReplayEventFragments([
|
||||
{ type: 4, timestamp: 1781553116150 },
|
||||
{
|
||||
type: REPLAY_EVENT_FRAGMENT_TYPE,
|
||||
timestamp: fullSnapshot.timestamp,
|
||||
data: {
|
||||
id: 'snapshot-1',
|
||||
index: 0,
|
||||
total: 2,
|
||||
value: serialized.slice(0, splitAt),
|
||||
},
|
||||
},
|
||||
{
|
||||
type: REPLAY_EVENT_FRAGMENT_TYPE,
|
||||
timestamp: fullSnapshot.timestamp,
|
||||
data: {
|
||||
id: 'snapshot-1',
|
||||
index: 1,
|
||||
total: 2,
|
||||
value: serialized.slice(splitAt),
|
||||
},
|
||||
},
|
||||
{ type: 3, timestamp: 1781553116160 },
|
||||
]);
|
||||
|
||||
expect(events).toEqual([
|
||||
{ type: 4, timestamp: 1781553116150 },
|
||||
fullSnapshot,
|
||||
{ type: 3, timestamp: 1781553116160 },
|
||||
]);
|
||||
expect(hasReplayFullSnapshot(events)).toBe(true);
|
||||
});
|
||||
|
||||
test('getReplayEventCount counts a fragment group as one event', () => {
|
||||
expect(
|
||||
getReplayEventCount([
|
||||
{ type: 4 },
|
||||
{
|
||||
type: REPLAY_EVENT_FRAGMENT_TYPE,
|
||||
data: { id: 'snapshot-1', index: 0, total: 2, value: '{' },
|
||||
},
|
||||
{
|
||||
type: REPLAY_EVENT_FRAGMENT_TYPE,
|
||||
data: { id: 'snapshot-1', index: 1, total: 2, value: '}' },
|
||||
},
|
||||
{ type: 3 },
|
||||
]),
|
||||
).toBe(3);
|
||||
});
|
||||
@@ -0,0 +1,108 @@
|
||||
export const RRWEB_EVENT_TYPE = {
|
||||
FullSnapshot: 2,
|
||||
} as const;
|
||||
|
||||
export const REPLAY_EVENT_FRAGMENT_TYPE = 'umami:rrweb-event-fragment';
|
||||
|
||||
interface ReplayEventFragment {
|
||||
type: typeof REPLAY_EVENT_FRAGMENT_TYPE;
|
||||
timestamp?: number;
|
||||
data: {
|
||||
id: string;
|
||||
index: number;
|
||||
total: number;
|
||||
value: string;
|
||||
};
|
||||
}
|
||||
|
||||
export function isReplayEventFragment(event: any): event is ReplayEventFragment {
|
||||
const { data } = event || {};
|
||||
|
||||
return (
|
||||
event?.type === REPLAY_EVENT_FRAGMENT_TYPE &&
|
||||
typeof data?.id === 'string' &&
|
||||
Number.isInteger(data?.index) &&
|
||||
Number.isInteger(data?.total) &&
|
||||
data.index >= 0 &&
|
||||
data.index < data.total &&
|
||||
typeof data?.value === 'string'
|
||||
);
|
||||
}
|
||||
|
||||
export function getReplayEventCount(events: any[] | null | undefined) {
|
||||
if (!Array.isArray(events)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return events.reduce((count, event) => {
|
||||
if (isReplayEventFragment(event)) {
|
||||
return count + (event.data.index === 0 ? 1 : 0);
|
||||
}
|
||||
|
||||
return count + 1;
|
||||
}, 0);
|
||||
}
|
||||
|
||||
export function restoreReplayEventFragments(events: any[] | null | undefined) {
|
||||
if (!Array.isArray(events)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const restored: any[] = [];
|
||||
const pending = new Map<
|
||||
string,
|
||||
{
|
||||
total: number;
|
||||
values: Map<number, string>;
|
||||
received: number;
|
||||
}
|
||||
>();
|
||||
|
||||
for (const event of events) {
|
||||
if (!isReplayEventFragment(event)) {
|
||||
restored.push(event);
|
||||
continue;
|
||||
}
|
||||
|
||||
const { id, index, total, value } = event.data;
|
||||
let fragment = pending.get(id);
|
||||
|
||||
if (!fragment || fragment.total !== total) {
|
||||
fragment = {
|
||||
total,
|
||||
values: new Map(),
|
||||
received: 0,
|
||||
};
|
||||
pending.set(id, fragment);
|
||||
}
|
||||
|
||||
if (!fragment.values.has(index)) {
|
||||
fragment.values.set(index, value);
|
||||
fragment.received += 1;
|
||||
}
|
||||
|
||||
if (fragment.received === fragment.total) {
|
||||
pending.delete(id);
|
||||
|
||||
try {
|
||||
let serialized = '';
|
||||
|
||||
for (let i = 0; i < fragment.total; i++) {
|
||||
serialized += fragment.values.get(i) || '';
|
||||
}
|
||||
|
||||
restored.push(JSON.parse(serialized));
|
||||
} catch {
|
||||
// Ignore malformed fragment groups. A partial replay is better than failing the whole response.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return restored;
|
||||
}
|
||||
|
||||
export function hasReplayFullSnapshot(events: any[] | null | undefined) {
|
||||
return (
|
||||
Array.isArray(events) && events.some(event => event?.type === RRWEB_EVENT_TYPE.FullSnapshot)
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user