Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rrweb] Fix hookSetter maximum call stack exceeded #1313

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions packages/rrweb/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,24 @@ export function hookSetter<T>(
isRevoked?: boolean,
win = window,
): hookResetter {
const isAlreadyHooked = Object.prototype.hasOwnProperty.call(
target,
'__hooked__' + String(key),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should have some indication on the DOM property to indicate it originates with rrweb

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have window.__rrMutationObserver elsewhere, so maybe just __rrHooked would be good?

);

// If already hooked, avoid re-hooking.
// eslint-disable-next-line @typescript-eslint/no-empty-function
if (isAlreadyHooked && !isRevoked) return () => {};

const original = win.Object.getOwnPropertyDescriptor(target, key);

win.Object.defineProperty(
target,
key,
isRevoked
? d
: {
set(value) {
// put hooked setter into event loop to avoid of set latency
setTimeout(() => {
d.set!.call(this, value);
}, 0);
Expand All @@ -122,7 +131,21 @@ export function hookSetter<T>(
},
},
);
return () => hookSetter(target, key, original || {}, true);

if (!isRevoked) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
(target as any)['__hooked__' + String(key)] = true;
} else {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
delete (target as any)['__hooked__' + String(key)];
}

return () => {
if (isRevoked) return; // Ensure that you can't reset multiple times
win.Object.defineProperty(target, key, original || {});
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
delete (target as any)['__hooked__' + String(key)];
};
}

// copy from https://github.com/getsentry/sentry-javascript/blob/b2109071975af8bf0316d3b5b38f519bdaf5dc15/packages/utils/src/object.ts
Expand Down