record canvas mutations (#296)
* record canvas mutations close #60, #261 This patch implements the canvas mutation observer. It consists of both the record and the replay side changes. In the record side, we add a `recordCanvas` flag to indicate whether to record canvas elements and the flag defaults to false. Different from our other observers, the canvas observer was disabled by default. Because some applications with heavy canvas usage may emit a lot of data as canvas changed, especially the scenarios that use a lot of `drawImage` API. So the behavior should be audited by users and only record canvas when the flag was set to true. In the replay side, we add a `UNSAFE_replayCanvas` flag to indicate whether to replay canvas mutations. Similar to the `recordCanvas` flag, `UNSAFE_replayCanvas` defaults to false. But unlike the record canvas implementation is stable and safe, the replay canvas implementation is UNSAFE. It's unsafe because we need to add `allow-scripts` to the replay sandbox, which may cause some unexpected script execution. Currently, users should be aware of this implementation detail and enable this feature carefully. * update canvas integration test
This commit is contained in:
39
src/utils.ts
39
src/utils.ts
@@ -124,18 +124,18 @@ export function patch(
|
||||
// tslint:disable-next-line:no-any
|
||||
replacement: (...args: any[]) => any,
|
||||
): () => void {
|
||||
if (!(name in source)) {
|
||||
return () => {};
|
||||
}
|
||||
try {
|
||||
if (!(name in source)) {
|
||||
return () => {};
|
||||
}
|
||||
|
||||
const original = source[name] as () => unknown;
|
||||
const wrapped = replacement(original);
|
||||
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"
|
||||
// tslint:disable-next-line:strict-type-predicates
|
||||
if (typeof wrapped === 'function') {
|
||||
try {
|
||||
// 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"
|
||||
// tslint:disable-next-line:strict-type-predicates
|
||||
if (typeof wrapped === 'function') {
|
||||
wrapped.prototype = wrapped.prototype || {};
|
||||
Object.defineProperties(wrapped, {
|
||||
__rrweb_original__: {
|
||||
@@ -143,17 +143,18 @@ export function patch(
|
||||
value: original,
|
||||
},
|
||||
});
|
||||
} catch {
|
||||
// This can throw if multiple fill happens on a global object like XMLHttpRequest
|
||||
// Fixes https://github.com/getsentry/sentry-javascript/issues/2043
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
source[name] = wrapped;
|
||||
|
||||
return () => {
|
||||
source[name] = original;
|
||||
};
|
||||
}
|
||||
|
||||
export function getWindowHeight(): number {
|
||||
|
||||
Reference in New Issue
Block a user