Skip to content

Commit

Permalink
feat: Safer initialization of replay. (#490)
Browse files Browse the repository at this point in the history
Check for presence of crypto and randomUUID.
If they are not present we do not capture a session.

Remove the header injection as I do not think we will use it for the
demo.
  • Loading branch information
kinyoklion authored Jun 26, 2024
1 parent a8b0757 commit da288c5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as rrweb from 'rrweb';

import { BrowserTelemetry } from '../../api/BrowserTelemetry';
import { Collector } from '../../api/Collector';
import { sessionId } from './patches';
import { ContinuousCapture } from './SessionReplayOptions';

export default class ContinuousReplay implements Collector {
Expand All @@ -13,26 +12,31 @@ export default class ContinuousReplay implements Collector {
private visibilityHandler: any;
private timerHandle: any;
private index: number = 0;
private readonly sessionId: string = '';

constructor(private readonly options: ContinuousCapture) {
this.visibilityHandler = () => {
this.handleVisibilityChange();
};
constructor(options: ContinuousCapture) {
if (typeof crypto !== undefined && typeof crypto.randomUUID === 'function') {
this.sessionId = crypto.randomUUID();
this.visibilityHandler = () => {
this.handleVisibilityChange();
};

document.addEventListener('visibilitychange', this.visibilityHandler, true);
document.addEventListener('visibilitychange', this.visibilityHandler, true);

this.timerHandle = setInterval(() => {
// If there are only 2 events, then we just have a snapshot.
// We can wait to capture until there is some activity.
// TODO: Figure out a better check.
if (this.buffer.length === 2) {
return;
}
this.recordCapture();
this.restartCapture();
}, options.intervalMs);
this.timerHandle = setInterval(() => {
// If there are only 2 events, then we just have a snapshot.
// We can wait to capture until there is some activity.
// TODO: Figure out a better check.
if (this.buffer.length === 2) {
return;
}
this.recordCapture();
this.restartCapture();
}, options.intervalMs);

this.startCapture();
this.startCapture();
}
// TODO: Should log that we are not going to record sessions.
}

register(telemetry: BrowserTelemetry): void {
Expand All @@ -57,7 +61,7 @@ export default class ContinuousReplay implements Collector {

private recordCapture(): void {
this.telemetry?.captureSession({
id: sessionId,
id: this.sessionId,
events: [...this.buffer],
index: this.index,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import * as rrweb from 'rrweb';
import { BrowserTelemetry } from '../../api/BrowserTelemetry';
import { Collector } from '../../api/Collector';
import ContinuousReplay from './ContinuousReplay';
import applyPatches from './patches';
import RollingReplay from './RollingReplay';
import { ContinuousCapture, RollingCapture, SessionReplayOptions } from './SessionReplayOptions';

Expand All @@ -23,7 +22,6 @@ export default class SessionReplay implements Collector {
impl: Collector;

constructor(options?: SessionReplayOptions) {
applyPatches();
const captureOptions = options?.capture;
if (isContinuousCapture(captureOptions)) {
this.impl = new ContinuousReplay(captureOptions);
Expand Down

This file was deleted.

0 comments on commit da288c5

Please sign in to comment.