Skip to content

Commit 04f920f

Browse files
committed
fix(console): JSON.stringify cannot serialize cyclical structures and event pooling issue
1 parent f8a3446 commit 04f920f

1 file changed

Lines changed: 40 additions & 21 deletions

File tree

src/interceptors/ConsoleInterceptor.ts

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,69 +20,88 @@ export default class ConsoleInterceptor extends Interceptor<ConsoleHandlers> {
2020
callback: null,
2121
};
2222

23+
private isSyntheticEvent(event: any): boolean {
24+
return (
25+
event !== null &&
26+
typeof event === 'object' &&
27+
'nativeEvent' in event &&
28+
'currentTarget' in event &&
29+
'target' in event &&
30+
typeof event.preventDefault === 'function'
31+
);
32+
}
33+
34+
private snapshotSyntheticEvent(event: any): Record<string, unknown> {
35+
try {
36+
return JSON.parse(JSON.stringify(event));
37+
} catch {
38+
return {
39+
'[SyntheticEvent]':
40+
'Could not snapshot event. Seem like this event has circular references. You can extract specific properties from the event object to see the details (e.g., event.target.value)',
41+
};
42+
}
43+
}
44+
45+
private callback(type: string, args: any[]) {
46+
const snapshotArgs = args.map(arg =>
47+
this.isSyntheticEvent(arg) ? this.snapshotSyntheticEvent(arg) : arg,
48+
);
49+
this.handlers.callback?.(type, snapshotArgs);
50+
}
51+
2352
@frozen
2453
enableInterception(): void {
2554
if (this.isInterceptorEnabled) return;
2655

27-
const callback = this.handlers.callback;
56+
const callback = this.callback.bind(this);
2857

2958
console.error = function (...args) {
30-
callback?.('error', args);
31-
59+
callback('error', args);
3260
originalConsoleError.call(this, ...args);
3361
};
3462

3563
console.info = function (...args) {
36-
callback?.('info', args);
37-
64+
callback('info', args);
3865
originalConsoleInfo.call(this, ...args);
3966
};
4067

4168
console.log = function (...args) {
42-
callback?.('log', args);
43-
69+
callback('log', args);
4470
originalConsoleLog.call(this, ...args);
4571
};
4672

4773
console.warn = function (...args) {
48-
callback?.('warn', args);
49-
74+
callback('warn', args);
5075
originalConsoleWarn.call(this, ...args);
5176
};
5277

5378
console.trace = function (...args) {
54-
callback?.('trace', args);
55-
79+
callback('trace', args);
5680
originalConsoleTrace.call(this, ...args);
5781
};
5882

5983
console.debug = function (...args) {
60-
callback?.('debug', args);
61-
84+
callback('debug', args);
6285
originalConsoleDebug.call(this, ...args);
6386
};
6487

6588
console.table = function (...args: Parameters<typeof originalConsoleTable>) {
66-
callback?.('table', args);
67-
89+
callback('table', args);
6890
originalConsoleTable.call(this, ...args);
6991
};
7092

7193
console.groupCollapsed = function (...args) {
72-
callback?.('groupCollapsed', args);
73-
94+
callback('groupCollapsed', args);
7495
originalConsoleGroupCollapsed.call(this, ...args);
7596
};
7697

7798
console.groupEnd = function (...args) {
78-
callback?.('groupEnd', args);
79-
99+
callback('groupEnd', args);
80100
originalConsoleGroupEnd.call(this, ...args);
81101
};
82102

83103
console.group = function (...args) {
84-
callback?.('group', args);
85-
104+
callback('group', args);
86105
originalConsoleGroup.call(this, ...args);
87106
};
88107

0 commit comments

Comments
 (0)