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: move patch function into utils to improve bundling #1631

Merged
merged 5 commits into from
Feb 6, 2025
Merged
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
8 changes: 8 additions & 0 deletions .changeset/itchy-tables-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@rrweb/rrweb-plugin-console-record": patch
"@rrweb/record": patch
"rrweb": patch
"@rrweb/utils": patch
---

Move patch function into @rrweb/utils to improve bundling
3 changes: 2 additions & 1 deletion packages/plugins/rrweb-plugin-console-record/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"puppeteer": "^20.9.0"
},
"peerDependencies": {
"rrweb": "^2.0.0-alpha.18"
"rrweb": "^2.0.0-alpha.18",
"@rrweb/utils": "^2.0.0-alpha.18"
}
}
4 changes: 2 additions & 2 deletions packages/plugins/rrweb-plugin-console-record/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { listenerHandler, RecordPlugin, IWindow } from '@rrweb/types';
import { utils } from 'rrweb';
import { patch } from '@rrweb/utils';
import { ErrorStackParser, StackFrame } from './error-stack-parser';
import { stringify } from './stringify';

Expand Down Expand Up @@ -183,7 +183,7 @@ function initLogObserver(
};
}
// replace the logger.{level}. return a restore function
return utils.patch(
return patch(
_logger,
level,
(original: (...args: Array<unknown>) => void) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/record/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
},
"dependencies": {
"@rrweb/types": "^2.0.0-alpha.18",
"rrweb": "^2.0.0-alpha.18"
"rrweb": "^2.0.0-alpha.18",
"@rrweb/utils": "^2.0.0-alpha.18"
},
"browserslist": [
"supports es6-class"
Expand Down
3 changes: 3 additions & 0 deletions packages/record/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
},
{
"path": "../rrweb"
},
{
"path": "../utils"
}
]
}
2 changes: 1 addition & 1 deletion packages/rrweb/src/record/observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
getWindowWidth,
isBlocked,
legacy_isTouchEvent,
patch,
StyleSheetMirror,
nowTimestamp,
} from '../utils';
import { patch } from '@rrweb/utils';
import type { observerParam, MutationBufferParam } from '../types';
import {
IncrementalSource,
Expand Down Expand Up @@ -131,7 +131,7 @@
| IncrementalSource.TouchMove
| IncrementalSource.Drag,
) => {
const totalOffset = Date.now() - timeBaseline!;

Check warning on line 134 in packages/rrweb/src/record/observer.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/rrweb/src/record/observer.ts#L134

[@typescript-eslint/no-non-null-assertion] Forbidden non-null assertion.
mousemoveCb(
positions.map((p) => {
p.timeOffset -= totalOffset;
Expand Down
3 changes: 2 additions & 1 deletion packages/rrweb/src/record/observers/canvas/2d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
type IWindow,
type listenerHandler,
} from '@rrweb/types';
import { hookSetter, isBlocked, patch } from '../../../utils';
import { hookSetter, isBlocked } from '../../../utils';
import { patch } from '@rrweb/utils';
import { serializeArgs } from './serialize-args';

export default function initCanvas2DMutationObserver(
Expand Down
3 changes: 2 additions & 1 deletion packages/rrweb/src/record/observers/canvas/canvas.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { ICanvas } from 'rrweb-snapshot';
import type { blockClass, IWindow, listenerHandler } from '@rrweb/types';
import { isBlocked, patch } from '../../../utils';
import { isBlocked } from '../../../utils';
import { patch } from '@rrweb/utils';

function getNormalizedContextName(contextType: string) {
return contextType === 'experimental-webgl' ? 'webgl' : contextType;
Expand Down
3 changes: 2 additions & 1 deletion packages/rrweb/src/record/observers/canvas/webgl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
type IWindow,
type listenerHandler,
} from '@rrweb/types';
import { hookSetter, isBlocked, patch } from '../../../utils';
import { hookSetter, isBlocked } from '../../../utils';
import { patch } from '@rrweb/utils';
import { saveWebGLVar, serializeArgs } from './serialize-args';

function patchGLPrototype(
Expand Down
4 changes: 2 additions & 2 deletions packages/rrweb/src/record/shadow-dom-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import {
initScrollObserver,
initAdoptedStyleSheetObserver,
} from './observer';
import { patch, inDom } from '../utils';
import { inDom } from '../utils';
import type { Mirror } from 'rrweb-snapshot';
import { isNativeShadowDom } from 'rrweb-snapshot';
import dom from '@rrweb/utils';
import dom, { patch } from '@rrweb/utils';

type BypassOptions = Omit<
MutationBufferParam,
Expand Down
43 changes: 0 additions & 43 deletions packages/rrweb/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
'now you can use replayer.getMirror() to access the mirror instance of a replayer,' +
'\r\n' +
'or you can use record.mirror to access the mirror instance during recording.';
/** @deprecated */

Check warning on line 35 in packages/rrweb/src/utils.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/rrweb/src/utils.ts#L35

[tsdoc/syntax] tsdoc-missing-deprecation-message: The @deprecated block must include a deprecation message, e.g. describing the recommended alternative
export let _mirror: DeprecatedMirror = {
map: {},
getId() {
Expand Down Expand Up @@ -116,7 +116,7 @@
set(value) {
// put hooked setter into event loop to avoid of set latency
setTimeout(() => {
d.set!.call(this, value);

Check warning on line 119 in packages/rrweb/src/utils.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/rrweb/src/utils.ts#L119

[@typescript-eslint/no-non-null-assertion] Forbidden non-null assertion.
}, 0);
if (original && original.set) {
original.set.call(this, value);
Expand All @@ -127,49 +127,6 @@
return () => hookSetter(target, key, original || {}, true);
}

// 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
}
}

// guard against old third party libraries which redefine Date.now
let nowTimestamp = Date.now;

Expand Down
44 changes: 44 additions & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,49 @@
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 },

Check warning on line 227 in packages/utils/src/index.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/utils/src/index.ts#L227

[@typescript-eslint/no-explicit-any] Unexpected any. Specify a different type.
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,
Expand All @@ -235,4 +278,5 @@
querySelector,
querySelectorAll,
mutationObserver: mutationObserverCtor,
patch,
};
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
# yarn lockfile v1


"@ampproject/remapping@^2.2.0", "@ampproject/remapping@^2.2.1":
Expand Down
Loading