fix: move patch function into utils to improve bundling (#1631)

* fix: move patch function into utils to improve bundling

---------

Co-authored-by: pauldambra <pauldambra@users.noreply.github.com>
Co-authored-by: Justin Halsall <Juice10@users.noreply.github.com>
This commit is contained in:
Paul D'Ambra
2026-04-01 12:00:00 +08:00
committed by GitHub
parent 636b02780e
commit bd367c2a73
13 changed files with 71 additions and 54 deletions

View File

@@ -222,6 +222,49 @@ export function mutationObserverCtor(): (typeof MutationObserver)['prototype']['
return getUntaintedPrototype('MutationObserver').constructor;
}
// copy from https://github.com/getsentry/sentry-javascript/blob/b2109071975af8bf0316d3b5b38f519bdaf5dc15/packages/utils/src/object.ts
export function patch(
source: { [key: string]: any },
name: string,
replacement: (...args: unknown[]) => unknown,
): () => void {
try {
if (!(name in source)) {
return () => {
//
};
}
const original = source[name] as () => unknown;
const wrapped = replacement(original);
// Make sure it's a function first, as we need to attach an empty prototype for `defineProperties` to work
// otherwise it'll throw "TypeError: Object.defineProperties called on non-object"
if (typeof wrapped === 'function') {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
wrapped.prototype = wrapped.prototype || {};
Object.defineProperties(wrapped, {
__rrweb_original__: {
enumerable: false,
value: original,
},
});
}
source[name] = wrapped;
return () => {
source[name] = original;
};
} catch {
return () => {
//
};
// This can throw if multiple fill happens on a global object like XMLHttpRequest
// Fixes https://github.com/getsentry/sentry-javascript/issues/2043
}
}
export default {
childNodes,
parentNode,
@@ -235,4 +278,5 @@ export default {
querySelector,
querySelectorAll,
mutationObserver: mutationObserverCtor,
patch,
};