Skip to content

Commit 8408b18

Browse files
committed
fix(rrweb): WIP: refactor sideeffects from rrweb/record
1 parent 6cee8d0 commit 8408b18

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

packages/rrweb/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@
5858
"dist",
5959
"package.json"
6060
],
61-
"sideEffects": false,
61+
"sideEffects": [
62+
"./src/record/clean-array-from.ts"
63+
],
6264
"author": "yanzhen@smartx.com",
6365
"license": "MIT",
6466
"bugs": {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Multiple tools (i.e. MooTools, Prototype.js) override Array.from and drop support for the 2nd parameter
2+
// Try to pull a clean implementation from a newly created iframe
3+
try {
4+
if (Array.from([1], (x) => x * 2)[0] !== 2) {
5+
const cleanFrame = document.createElement('iframe');
6+
document.body.appendChild(cleanFrame);
7+
// eslint-disable-next-line @typescript-eslint/unbound-method -- Array.from is static and doesn't rely on binding
8+
Array.from = cleanFrame.contentWindow?.Array.from || Array.from;
9+
document.body.removeChild(cleanFrame);
10+
}
11+
} catch (err) {
12+
console.debug('Unable to override Array.from', err);
13+
}
14+
15+
export {};

packages/rrweb/src/record/index.ts

+9-16
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
MaskInputOptions,
44
SlimDOMOptions,
55
createMirror,
6+
type Mirror,
67
} from '@sentry-internal/rrweb-snapshot';
78
import { initObservers, mutationBuffers } from './observer';
89
import {
@@ -52,6 +53,8 @@ import {
5253
registerErrorHandler,
5354
unregisterErrorHandler,
5455
} from './error-handler';
56+
import './clean-array-from';
57+
5558
export type { CanvasManagerConstructorOptions } from './observers/canvas/canvas-manager';
5659

5760
declare global {
@@ -67,21 +70,6 @@ let _wrappedEmit:
6770
| ((e: eventWithTime, isCheckout?: boolean) => void);
6871
let _takeFullSnapshot: undefined | ((isCheckout?: boolean) => void);
6972

70-
// Multiple tools (i.e. MooTools, Prototype.js) override Array.from and drop support for the 2nd parameter
71-
// Try to pull a clean implementation from a newly created iframe
72-
try {
73-
if (Array.from([1], (x) => x * 2)[0] !== 2) {
74-
const cleanFrame = document.createElement('iframe');
75-
document.body.appendChild(cleanFrame);
76-
// eslint-disable-next-line @typescript-eslint/unbound-method -- Array.from is static and doesn't rely on binding
77-
Array.from = cleanFrame.contentWindow?.Array.from || Array.from;
78-
document.body.removeChild(cleanFrame);
79-
}
80-
} catch (err) {
81-
console.debug('Unable to override Array.from', err);
82-
}
83-
84-
const mirror = createMirror();
8573
function record<T = eventWithTime>(
8674
options: recordOptions<T> = {},
8775
): listenerHandler | undefined {
@@ -129,6 +117,7 @@ function record<T = eventWithTime>(
129117
} = options;
130118

131119
registerErrorHandler(errorHandler);
120+
const mirror = createMirror();
132121

133122
const inEmittingFrame = recordCrossOriginIframes
134123
? window.parent === window
@@ -721,9 +710,13 @@ export function takeFullSnapshot(isCheckout?: boolean) {
721710
// record.freezePage is removed because Sentry Session Replay does not use it
722711

723712
// For backwards compatibility - we can eventually remove this when we migrated to using the exported `mirror` & `takeFullSnapshot`
724-
record.mirror = mirror;
713+
// record.mirror = mirror;
725714
record.takeFullSnapshot = takeFullSnapshot;
726715

716+
namespace record {
717+
export var mirror: Mirror;
718+
}
719+
727720
export default record;
728721

729722
function _getCanvasManager(

0 commit comments

Comments
 (0)