4
4
import safeStringify from 'safe-stringify' ;
5
5
import type { ConsoleLogEvent } from '@hawk.so/types' ;
6
6
7
- const createConsoleCatcher = ( ) : {
7
+ /**
8
+ * Creates a console interceptor that captures and formats console output
9
+ */
10
+ function createConsoleCatcher ( ) : {
8
11
initConsoleCatcher : ( ) => void ;
9
12
addErrorEvent : ( event : ErrorEvent | PromiseRejectionEvent ) => void ;
10
13
getConsoleLogStack : ( ) => ConsoleLogEvent [ ] ;
11
- } => {
14
+ } {
12
15
const MAX_LOGS = 20 ;
13
16
const consoleOutput : ConsoleLogEvent [ ] = [ ] ;
14
17
let isInitialized = false ;
15
18
16
19
/**
17
20
* Converts any argument to its string representation
18
21
*
19
- * @param arg - Console arguments
22
+ * @param arg - Value to convert to string
20
23
*/
21
- const stringifyArg = ( arg : unknown ) : string => {
24
+ function stringifyArg ( arg : unknown ) : string {
22
25
if ( typeof arg === 'string' ) {
23
26
return arg ;
24
27
}
@@ -27,19 +30,22 @@ const createConsoleCatcher = (): {
27
30
}
28
31
29
32
return safeStringify ( arg ) ;
30
- } ;
33
+ }
31
34
32
35
/**
33
36
* Formats console arguments handling %c directives
34
37
*
35
- * @param args - Console arguments that may include %c style directives
38
+ * @param args - Console arguments that may include style directives
36
39
*/
37
- const formatConsoleArgs = (
38
- args : unknown [ ]
39
- ) : { message : string ; styles : string [ ] } => {
40
+ function formatConsoleArgs ( args : unknown [ ] ) : {
41
+ message : string ;
42
+ styles : string [ ] ;
43
+ } {
40
44
if ( args . length === 0 ) {
41
- return { message : '' ,
42
- styles : [ ] } ;
45
+ return {
46
+ message : '' ,
47
+ styles : [ ] ,
48
+ } ;
43
49
}
44
50
45
51
const firstArg = args [ 0 ] ;
@@ -77,18 +83,28 @@ const createConsoleCatcher = (): {
77
83
message : message + ( remainingArgs ? ' ' + remainingArgs : '' ) ,
78
84
styles,
79
85
} ;
80
- } ;
86
+ }
81
87
82
- const addToConsoleOutput = ( logEvent : ConsoleLogEvent ) : void => {
88
+ /**
89
+ * Adds a console log event to the output buffer
90
+ *
91
+ * @param logEvent - The console log event to be added to the output buffer
92
+ */
93
+ function addToConsoleOutput ( logEvent : ConsoleLogEvent ) : void {
83
94
if ( consoleOutput . length >= MAX_LOGS ) {
84
95
consoleOutput . shift ( ) ;
85
96
}
86
97
consoleOutput . push ( logEvent ) ;
87
- } ;
98
+ }
88
99
89
- const createConsoleEventFromError = (
100
+ /**
101
+ * Creates a console log event from an error or promise rejection
102
+ *
103
+ * @param event - The error event or promise rejection event to convert
104
+ */
105
+ function createConsoleEventFromError (
90
106
event : ErrorEvent | PromiseRejectionEvent
91
- ) : ConsoleLogEvent => {
107
+ ) : ConsoleLogEvent {
92
108
if ( event instanceof ErrorEvent ) {
93
109
return {
94
110
method : 'error' ,
@@ -110,63 +126,71 @@ const createConsoleCatcher = (): {
110
126
stack : event . reason ?. stack || '' ,
111
127
fileLine : '' ,
112
128
} ;
113
- } ;
129
+ }
114
130
115
- return {
116
- initConsoleCatcher ( ) : void {
117
- if ( isInitialized ) {
131
+ /**
132
+ * Initializes the console interceptor by overriding default console methods
133
+ */
134
+ function initConsoleCatcher ( ) : void {
135
+ if ( isInitialized ) {
136
+ return ;
137
+ }
138
+
139
+ isInitialized = true ;
140
+ const consoleMethods : string [ ] = [ 'log' , 'warn' , 'error' , 'info' , 'debug' ] ;
141
+
142
+ consoleMethods . forEach ( function overrideConsoleMethod ( method ) {
143
+ if ( typeof window . console [ method ] !== 'function' ) {
118
144
return ;
119
145
}
120
146
121
- isInitialized = true ;
122
- const consoleMethods : string [ ] = [
123
- 'log' ,
124
- 'warn' ,
125
- 'error' ,
126
- 'info' ,
127
- 'debug' ,
128
- ] ;
129
-
130
- consoleMethods . forEach ( ( method ) => {
131
- if ( typeof window . console [ method ] !== 'function' ) {
132
- return ;
133
- }
134
-
135
- const oldFunction = window . console [ method ] . bind ( window . console ) ;
136
-
137
- window . console [ method ] = function ( ...args : unknown [ ] ) : void {
138
- const stack =
139
- new Error ( ) . stack ?. split ( '\n' ) . slice ( 2 )
140
- . join ( '\n' ) || '' ;
141
- const { message, styles } = formatConsoleArgs ( args ) ;
142
-
143
- const logEvent : ConsoleLogEvent = {
144
- method,
145
- timestamp : new Date ( ) ,
146
- type : method ,
147
- message,
148
- stack,
149
- fileLine : stack . split ( '\n' ) [ 0 ] ?. trim ( ) ,
150
- styles,
151
- } ;
152
-
153
- addToConsoleOutput ( logEvent ) ;
154
- oldFunction ( ...args ) ;
147
+ const oldFunction = window . console [ method ] . bind ( window . console ) ;
148
+
149
+ window . console [ method ] = function ( ...args : unknown [ ] ) : void {
150
+ const stack = new Error ( ) . stack ?. split ( '\n' ) . slice ( 2 )
151
+ . join ( '\n' ) || '' ;
152
+ const { message, styles } = formatConsoleArgs ( args ) ;
153
+
154
+ const logEvent : ConsoleLogEvent = {
155
+ method,
156
+ timestamp : new Date ( ) ,
157
+ type : method ,
158
+ message,
159
+ stack,
160
+ fileLine : stack . split ( '\n' ) [ 0 ] ?. trim ( ) ,
161
+ styles,
155
162
} ;
156
- } ) ;
157
- } ,
158
163
159
- addErrorEvent ( event : ErrorEvent | PromiseRejectionEvent ) : void {
160
- const logEvent = createConsoleEventFromError ( event ) ;
164
+ addToConsoleOutput ( logEvent ) ;
165
+ oldFunction ( ...args ) ;
166
+ } ;
167
+ } ) ;
168
+ }
161
169
162
- addToConsoleOutput ( logEvent ) ;
163
- } ,
170
+ /**
171
+ * Handles error events by converting them to console log events
172
+ *
173
+ * @param event - The error or promise rejection event to handle
174
+ */
175
+ function addErrorEvent ( event : ErrorEvent | PromiseRejectionEvent ) : void {
176
+ const logEvent = createConsoleEventFromError ( event ) ;
164
177
165
- getConsoleLogStack ( ) : ConsoleLogEvent [ ] {
166
- return [ ...consoleOutput ] ;
167
- } ,
178
+ addToConsoleOutput ( logEvent ) ;
179
+ }
180
+
181
+ /**
182
+ * Returns the current console output buffer
183
+ */
184
+ function getConsoleLogStack ( ) : ConsoleLogEvent [ ] {
185
+ return [ ...consoleOutput ] ;
186
+ }
187
+
188
+ return {
189
+ initConsoleCatcher,
190
+ addErrorEvent,
191
+ getConsoleLogStack,
168
192
} ;
169
- } ;
193
+ }
170
194
171
195
const consoleCatcher = createConsoleCatcher ( ) ;
172
196
0 commit comments