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

fix: angular wrapped mutationobserver detection #1597

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/moody-experts-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rrweb/record": patch
---

correctly detect when angular has wrapped mutation observer"
25 changes: 24 additions & 1 deletion packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,25 @@ const testableMethods = {

const untaintedBasePrototype: Partial<BasePrototypeCache> = {};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const isFunction = (x: unknown): x is (...args: any[]) => any => {
return typeof x === 'function';
};

/*
When angular patches things they pass the above `isNativeFunction` check
That then causes performance issues
because angular's change detection
doesn't like sharing a mutation observer
*/
export const isAngularZonePatchedFunction = (x: unknown): boolean => {
if (!isFunction(x)) {
return false;
}
const prototypeKeys = Object.getOwnPropertyNames(x.prototype || {});
return prototypeKeys.some((key) => key.indexOf('__zone'));
};

export function getUntaintedPrototype<T extends keyof BasePrototypeCache>(
key: T,
): BasePrototypeCache[T] {
Expand Down Expand Up @@ -63,7 +82,11 @@ export function getUntaintedPrototype<T extends keyof BasePrototypeCache>(
),
);

if (isUntaintedAccessors && isUntaintedMethods) {
if (
isUntaintedAccessors &&
isUntaintedMethods &&
!isAngularZonePatchedFunction(defaultObj)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

considered only checking this for the known "bad" case of MutationObserver but it's a fast enough check to always be sure

) {
untaintedBasePrototype[key] = defaultObj.prototype as BasePrototypeCache[T];
return defaultObj.prototype as BasePrototypeCache[T];
}
Expand Down
Loading