Skip to content

Commit a2abb83

Browse files
committed
safe-stringify package
1 parent a4fb9d8 commit a2abb83

File tree

3 files changed

+34
-53
lines changed

3 files changed

+34
-53
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"dependencies": {
5050
"@hawk.so/types": "^0.1.20",
5151
"error-stack-parser": "^2.1.4",
52+
"safe-stringify": "^1.1.1",
5253
"vite-plugin-dts": "^4.2.4"
5354
}
5455
}

src/addons/consoleCatcher.ts

Lines changed: 28 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
22
* @file Module for intercepting console logs with stack trace capture
33
*/
4-
5-
import type { ConsoleLogEvent } from '@hawk.so/types';
4+
import safeStringify from "safe-stringify";
5+
import type { ConsoleLogEvent } from "@hawk.so/types";
66

77
const createConsoleCatcher = (): {
88
initConsoleCatcher: () => void;
@@ -13,41 +13,20 @@ const createConsoleCatcher = (): {
1313
const consoleOutput: ConsoleLogEvent[] = [];
1414
let isInitialized = false;
1515

16-
/**
17-
* Safely stringifies objects handling circular references
18-
*/
19-
const safeStringify = (obj: unknown): string => {
20-
const seen = new WeakSet();
21-
22-
return JSON.stringify(obj, (key, value) => {
23-
24-
if (typeof value === 'object' && value !== null) {
25-
26-
if (seen.has(value)) {
27-
return '[Circular]';
28-
}
29-
seen.add(value);
30-
}
31-
32-
return value;
33-
});
34-
};
35-
3616
/**
3717
* Formats console arguments handling %c directives
3818
*/
3919
const formatConsoleArgs = (
4020
args: unknown[]
4121
): { message: string; styles: string[] } => {
42-
if (args.length === 0) return { message: '', styles: [] };
22+
if (args.length === 0) return { message: "", styles: [] };
4323

4424
const firstArg = args[0];
45-
if (typeof firstArg !== 'string' || !firstArg.includes('%c')) {
46-
25+
if (typeof firstArg !== "string" || !firstArg.includes("%c")) {
4726
return {
4827
message: args
49-
.map((arg) => (typeof arg === 'string' ? arg : safeStringify(arg)))
50-
.join(' '),
28+
.map((arg) => (typeof arg === "string" ? arg : safeStringify(arg)))
29+
.join(" "),
5130
styles: [],
5231
};
5332
}
@@ -62,20 +41,20 @@ const createConsoleCatcher = (): {
6241
for (let i = 1; i < args.length; i++) {
6342
const arg = args[i];
6443

65-
if (typeof arg === 'string' && message.indexOf('%c', styleIndex) !== -1) {
44+
if (typeof arg === "string" && message.indexOf("%c", styleIndex) !== -1) {
6645
styles.push(arg);
67-
styleIndex = message.indexOf('%c', styleIndex) + 2;
46+
styleIndex = message.indexOf("%c", styleIndex) + 2;
6847
}
6948
}
7049

7150
// Add remaining arguments that aren't styles
7251
const remainingArgs = args
7352
.slice(styles.length + 1)
74-
.map((arg) => (typeof arg === 'string' ? arg : safeStringify(arg)))
75-
.join(' ');
53+
.map((arg) => (typeof arg === "string" ? arg : safeStringify(arg)))
54+
.join(" ");
7655

7756
return {
78-
message: message + (remainingArgs ? ' ' + remainingArgs : ''),
57+
message: message + (remainingArgs ? " " + remainingArgs : ""),
7958
styles,
8059
};
8160
};
@@ -90,28 +69,26 @@ const createConsoleCatcher = (): {
9069
const createConsoleEventFromError = (
9170
event: ErrorEvent | PromiseRejectionEvent
9271
): ConsoleLogEvent => {
93-
9472
if (event instanceof ErrorEvent) {
95-
9673
return {
97-
method: 'error',
74+
method: "error",
9875
timestamp: new Date(),
99-
type: event.error?.name || 'Error',
76+
type: event.error?.name || "Error",
10077
message: event.error?.message || event.message,
101-
stack: event.error?.stack || '',
78+
stack: event.error?.stack || "",
10279
fileLine: event.filename
10380
? `${event.filename}:${event.lineno}:${event.colno}`
104-
: '',
81+
: "",
10582
};
10683
}
10784

10885
return {
109-
method: 'error',
86+
method: "error",
11087
timestamp: new Date(),
111-
type: 'UnhandledRejection',
88+
type: "UnhandledRejection",
11289
message: event.reason?.message || String(event.reason),
113-
stack: event.reason?.stack || '',
114-
fileLine: '',
90+
stack: event.reason?.stack || "",
91+
fileLine: "",
11592
};
11693
};
11794

@@ -123,24 +100,23 @@ const createConsoleCatcher = (): {
123100

124101
isInitialized = true;
125102
const consoleMethods: string[] = [
126-
'log',
127-
'warn',
128-
'error',
129-
'info',
130-
'debug',
103+
"log",
104+
"warn",
105+
"error",
106+
"info",
107+
"debug",
131108
];
132109

133110
consoleMethods.forEach((method) => {
134-
if (typeof window.console[method] !== 'function') {
111+
if (typeof window.console[method] !== "function") {
135112
return;
136113
}
137114

138115
const oldFunction = window.console[method].bind(window.console);
139116

140117
window.console[method] = function (...args: unknown[]): void {
141118
const stack =
142-
new Error().stack?.split('\n').slice(2)
143-
.join('\n') || '';
119+
new Error().stack?.split("\n").slice(2).join("\n") || "";
144120
const { message, styles } = formatConsoleArgs(args);
145121

146122
const logEvent: ConsoleLogEvent = {
@@ -149,7 +125,7 @@ const createConsoleCatcher = (): {
149125
type: method,
150126
message,
151127
stack,
152-
fileLine: stack.split('\n')[0]?.trim(),
128+
fileLine: stack.split("\n")[0]?.trim(),
153129
styles,
154130
};
155131

@@ -166,8 +142,7 @@ const createConsoleCatcher = (): {
166142
},
167143

168144
getConsoleLogStack(): ConsoleLogEvent[] {
169-
170-
return [ ...consoleOutput ];
145+
return [...consoleOutput];
171146
},
172147
};
173148
};

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2619,6 +2619,11 @@ safe-regex-test@^1.0.3:
26192619
es-errors "^1.3.0"
26202620
is-regex "^1.1.4"
26212621

2622+
safe-stringify@^1.1.1:
2623+
version "1.1.1"
2624+
resolved "https://registry.yarnpkg.com/safe-stringify/-/safe-stringify-1.1.1.tgz#f4240f506d041f58374d6106e2a5850f6b1ce576"
2625+
integrity sha512-YSzQLuwp06fuvJD1h6+vVNFYZoXmDs5UUNPUbTvQK7Ap+L0qD4Vp+sN434C+pdS3prVVlUfQdNeiEIgxox/kUQ==
2626+
26222627
semver@^6.3.1:
26232628
version "6.3.1"
26242629
resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.1.tgz#556d2ef8689146e46dcea4bfdd095f3434dffcb4"

0 commit comments

Comments
 (0)