Normalize replay events for rrweb-player playback

This commit is contained in:
Mike Cao committed 2026-06-15 20:08:53 -07:00
1 parent a5f78e3e3d
commit 95ece5ba56
7 files changed
+278 -56

No files matched your search

+63 -10
View File
@@ -1,15 +1,78 @@
import { expect, test } from 'vitest';
import {
canReplayEvents,
getReplayEventCount,
getReplayPlayerEvents,
getReplayViewport,
hasReplayableFullSnapshot,
hasReplayFullSnapshot,
hasReplayMeta,
REPLAY_EVENT_FRAGMENT_TYPE,
restoreReplayEventFragments,
} from './replay';
const fullSnapshot = {
type: 2,
timestamp: 1781553116151,
data: {
node: {
type: 0,
id: 1,
childNodes: [{ type: 2, id: 2, tagName: 'html', attributes: {}, childNodes: [] }],
},
initialOffset: { left: 0, top: 0 },
},
};
test('hasReplayFullSnapshot returns true when a full snapshot is present', () => {
expect(hasReplayFullSnapshot([{ type: 4 }, { type: 2 }, { type: 3 }])).toBe(true);
});
test('hasReplayMeta returns true when a meta event is present', () => {
expect(hasReplayMeta([{ type: 4 }, { type: 2 }, { type: 3 }])).toBe(true);
});
test('getReplayViewport returns the recorded viewport dimensions', () => {
expect(getReplayViewport(null)).toBe(null);
expect(getReplayViewport([{ type: 4, data: { width: 0, height: 768 } }])).toBe(null);
expect(getReplayViewport([{ type: 4, data: { width: '1024', height: '1365' } }])).toEqual({
width: 1024,
height: 1365,
});
});
test('hasReplayableFullSnapshot returns true when a usable full snapshot is present', () => {
expect(hasReplayableFullSnapshot([{ type: 4 }, fullSnapshot, { type: 3 }])).toBe(true);
});
test('canReplayEvents requires events that can be normalized with a full snapshot', () => {
expect(canReplayEvents([{ type: 4, timestamp: 1781553116150 }, fullSnapshot])).toBe(true);
expect(canReplayEvents([fullSnapshot])).toBe(true);
expect(canReplayEvents([{ type: 4, timestamp: 1781553116150 }, { type: 2 }])).toBe(false);
expect(canReplayEvents([{ type: 4, timestamp: 1781553116150 }, { type: 3 }])).toBe(false);
expect(canReplayEvents([{ ...fullSnapshot, timestamp: undefined }])).toBe(true);
});
test('getReplayPlayerEvents normalizes events for rrweb-player', () => {
expect(getReplayPlayerEvents(null)).toEqual([]);
expect(getReplayPlayerEvents([{ type: 3, timestamp: 1781553116160 }])).toEqual([]);
expect(getReplayPlayerEvents([fullSnapshot])).toEqual([
fullSnapshot,
{ ...fullSnapshot, timestamp: fullSnapshot.timestamp + 1 },
]);
expect(
getReplayPlayerEvents([
{ type: 0, timestamp: '1781553116149' },
{ ...fullSnapshot, timestamp: undefined },
]),
).toEqual([
{ type: 0, timestamp: 1781553116149 },
{ ...fullSnapshot, timestamp: 1781553116150 },
]);
});
test('hasReplayFullSnapshot returns false when only incremental events are present', () => {
expect(hasReplayFullSnapshot([{ type: 4 }, { type: 3 }, { type: 3 }])).toBe(false);
});
@@ -20,16 +83,6 @@ test('hasReplayFullSnapshot handles missing events', () => {
});
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);
+103
View File
@@ -1,4 +1,5 @@
export const RRWEB_EVENT_TYPE = {
Meta: 4,
FullSnapshot: 2,
} as const;
@@ -106,3 +107,105 @@ export function hasReplayFullSnapshot(events: any[] | null | undefined) {
Array.isArray(events) && events.some(event => event?.type === RRWEB_EVENT_TYPE.FullSnapshot)
);
}
export function hasReplayableFullSnapshot(events: any[] | null | undefined) {
return (
Array.isArray(events) &&
events.some(event => {
const { data } = event || {};
const { node } = data || {};
return (
event?.type === RRWEB_EVENT_TYPE.FullSnapshot &&
Number.isInteger(node?.type) &&
Array.isArray(node?.childNodes)
);
})
);
}
export function hasReplayMeta(events: any[] | null | undefined) {
return Array.isArray(events) && events.some(event => event?.type === RRWEB_EVENT_TYPE.Meta);
}
function getReplayDimension(value: any) {
const dimension = Number(value);
return Number.isFinite(dimension) && dimension > 0 ? dimension : null;
}
export function getReplayViewport(events: any[] | null | undefined) {
if (!Array.isArray(events)) {
return null;
}
const meta = events.find(event => {
const width = getReplayDimension(event?.data?.width);
const height = getReplayDimension(event?.data?.height);
return event?.type === RRWEB_EVENT_TYPE.Meta && width && height;
});
if (!meta) {
return null;
}
return {
width: getReplayDimension(meta.data.width) as number,
height: getReplayDimension(meta.data.height) as number,
};
}
function getReplayTimestamp(event: any) {
const timestamp = Number(event?.timestamp);
return Number.isFinite(timestamp) ? timestamp : null;
}
export function getReplayPlayerEvents(events: any[] | null | undefined) {
if (!Array.isArray(events)) {
return [];
}
const replayEvents = events.filter(
event => event && typeof event === 'object' && event.type !== undefined && event.type !== null,
);
if (!hasReplayableFullSnapshot(replayEvents)) {
return [];
}
const firstTimestamp =
replayEvents.map(getReplayTimestamp).find(timestamp => timestamp !== null) ?? Date.now();
let lastTimestamp = firstTimestamp - 1;
const normalizedEvents = replayEvents.map(event => {
const timestamp = getReplayTimestamp(event);
if (timestamp !== null) {
lastTimestamp = timestamp;
return typeof event.timestamp === 'number' ? event : { ...event, timestamp };
}
lastTimestamp += 1;
return { ...event, timestamp: lastTimestamp };
});
if (normalizedEvents.length === 1) {
return [
normalizedEvents[0],
{
...normalizedEvents[0],
timestamp: normalizedEvents[0].timestamp + 1,
},
];
}
return normalizedEvents;
}
export function canReplayEvents(events: any[] | null | undefined) {
return getReplayPlayerEvents(events).length >= 2;
}