-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathcanvas.ts
121 lines (111 loc) · 3.57 KB
/
canvas.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import type { CanvasManagerInterface, CanvasManagerOptions } from '@sentry-internal/replay';
import { CanvasManager } from '@sentry-internal/rrweb';
import { defineIntegration } from '@sentry/core';
import type { Integration, IntegrationFn } from '@sentry/core';
interface ReplayCanvasIntegration extends Integration {
snapshot: (canvasElement?: HTMLCanvasElement) => Promise<void>;
}
interface ReplayCanvasOptions {
enableManualSnapshot?: boolean;
maxCanvasSize?: [width: number, height: number];
quality: 'low' | 'medium' | 'high';
}
type GetCanvasManager = (options: CanvasManagerOptions) => CanvasManagerInterface;
export interface ReplayCanvasIntegrationOptions {
enableManualSnapshot?: boolean;
maxCanvasSize?: number;
recordCanvas: true;
getCanvasManager: GetCanvasManager;
sampling: {
canvas: number;
};
dataURLOptions: {
type: string;
quality: number;
};
}
const CANVAS_QUALITY = {
low: {
sampling: {
canvas: 1,
},
dataURLOptions: {
type: 'image/webp',
quality: 0.25,
},
},
medium: {
sampling: {
canvas: 2,
},
dataURLOptions: {
type: 'image/webp',
quality: 0.4,
},
},
high: {
sampling: {
canvas: 4,
},
dataURLOptions: {
type: 'image/webp',
quality: 0.5,
},
},
};
const INTEGRATION_NAME = 'ReplayCanvas';
const DEFAULT_MAX_CANVAS_SIZE = 1280;
/** Exported only for type safe tests. */
export const _replayCanvasIntegration = ((options: Partial<ReplayCanvasOptions> = {}) => {
const [maxCanvasWidth, maxCanvasHeight] = options.maxCanvasSize || [];
const _canvasOptions = {
quality: options.quality || 'medium',
enableManualSnapshot: options.enableManualSnapshot,
maxCanvasSize: [
maxCanvasWidth ? Math.min(maxCanvasWidth, DEFAULT_MAX_CANVAS_SIZE) : DEFAULT_MAX_CANVAS_SIZE,
maxCanvasHeight ? Math.min(maxCanvasHeight, DEFAULT_MAX_CANVAS_SIZE) : DEFAULT_MAX_CANVAS_SIZE,
] as [number, number],
};
let canvasManagerResolve: (value: CanvasManager) => void;
const _canvasManager: Promise<CanvasManager> = new Promise(resolve => (canvasManagerResolve = resolve));
return {
name: INTEGRATION_NAME,
getOptions(): ReplayCanvasIntegrationOptions {
const { quality, enableManualSnapshot, maxCanvasSize } = _canvasOptions;
return {
enableManualSnapshot,
recordCanvas: true,
getCanvasManager: (getCanvasManagerOptions: CanvasManagerOptions) => {
const manager = new CanvasManager({
...getCanvasManagerOptions,
enableManualSnapshot,
maxCanvasSize,
errorHandler: (err: unknown) => {
try {
if (typeof err === 'object') {
(err as Error & { __rrweb__?: boolean }).__rrweb__ = true;
}
} catch (error) {
// ignore errors here
// this can happen if the error is frozen or does not allow mutation for other reasons
}
},
});
canvasManagerResolve(manager);
return manager;
},
...(CANVAS_QUALITY[quality || 'medium'] || CANVAS_QUALITY.medium),
};
},
async snapshot(canvasElement?: HTMLCanvasElement) {
const canvasManager = await _canvasManager;
canvasManager.snapshot(canvasElement);
},
};
}) satisfies IntegrationFn<ReplayCanvasIntegration>;
/**
* Add this in addition to `replayIntegration()` to enable canvas recording.
*/
export const replayCanvasIntegration = defineIntegration(
_replayCanvasIntegration,
) as IntegrationFn<ReplayCanvasIntegration>;