15
15
// specific language governing permissions and limitations
16
16
// under the License.
17
17
18
+ const { FilterBy } = require ( './filterBy' )
18
19
const { ConsoleLogEntry, JavascriptLogEntry, GenericLogEntry } = require ( './logEntries' )
19
20
20
21
const LOG = {
21
22
TYPE_CONSOLE : 'console' ,
22
- TYPE_JS_LOGS : 'jsLogs' ,
23
- TYPE_JS_EXCEPTION : 'jsException' ,
24
- TYPE_LOG : 'log'
23
+ TYPE_JS_LOGS : 'javascript' ,
25
24
}
26
25
27
26
class LogInspector {
@@ -57,26 +56,36 @@ class LogInspector {
57
56
* @param callback
58
57
* @returns {Promise<void> }
59
58
*/
60
- async onConsoleEntry ( callback ) {
61
- this . ws = await this . bidi . socket
62
- let enabled = ( LOG . TYPE_CONSOLE in this . listener ) || this . logListener ( LOG . TYPE_CONSOLE )
63
- this . listener [ LOG . TYPE_CONSOLE ] . push ( callback )
64
-
65
- if ( enabled ) {
66
- return
59
+ async onConsoleEntry ( callback , filterBy = undefined ) {
60
+ if ( filterBy != undefined && ! ( filterBy instanceof FilterBy ) ) {
61
+ throw Error ( `Pass valid FilterBy object. Received: ${ filterBy } ` )
67
62
}
68
63
69
- this . ws . on ( 'message' , event => {
64
+ this . ws = await this . bidi . socket
65
+
66
+ this . ws . on ( 'message' , ( event ) => {
70
67
const { params } = JSON . parse ( Buffer . from ( event . toString ( ) ) )
71
68
72
69
if ( params ?. type === LOG . TYPE_CONSOLE ) {
73
- let consoleEntry = new ConsoleLogEntry ( params . level , params . text ,
74
- params . timestamp , params . type , params . method , params . realm ,
75
- params . args , params . stackTrace )
76
-
77
- this . listener [ LOG . TYPE_CONSOLE ] . forEach ( listener => {
78
- listener ( consoleEntry )
79
- } )
70
+ let consoleEntry = new ConsoleLogEntry (
71
+ params . level ,
72
+ params . text ,
73
+ params . timestamp ,
74
+ params . type ,
75
+ params . method ,
76
+ params . realm ,
77
+ params . args ,
78
+ params . stackTrace
79
+ )
80
+
81
+ if ( filterBy != undefined ) {
82
+ if ( params ?. level === filterBy . getLevel ( ) ) {
83
+ callback ( consoleEntry )
84
+ }
85
+ return
86
+ }
87
+
88
+ callback ( consoleEntry )
80
89
}
81
90
} )
82
91
}
@@ -86,26 +95,33 @@ class LogInspector {
86
95
* @param callback
87
96
* @returns {Promise<void> }
88
97
*/
89
- async onJavascriptLog ( callback ) {
90
- this . ws = await this . bidi . socket
91
- let enabled = ( LOG . TYPE_JS_LOGS in this . listener ) || this . logListener (
92
- LOG . TYPE_JS_LOGS )
93
- this . listener [ LOG . TYPE_JS_LOGS ] . push ( callback )
94
-
95
- if ( enabled ) {
96
- return
98
+ async onJavascriptLog ( callback , filterBy = undefined ) {
99
+ if ( filterBy != undefined && ! ( filterBy instanceof FilterBy ) ) {
100
+ throw Error ( `Pass valid FilterBy object. Received: ${ filterBy } ` )
97
101
}
98
102
99
- this . ws . on ( 'message' , event => {
100
- const { params } = JSON . parse ( Buffer . from ( event . toString ( ) ) )
101
- if ( ( params ?. type === 'javascript' ) ) {
103
+ this . ws = await this . bidi . socket
102
104
103
- let jsEntry = new JavascriptLogEntry ( params . level , params . text ,
104
- params . timestamp , params . type , params . stackTrace )
105
+ this . ws . on ( 'message' , ( event ) => {
106
+ const { params } = JSON . parse ( Buffer . from ( event . toString ( ) ) )
105
107
106
- this . listener [ LOG . TYPE_JS_LOGS ] . forEach ( listener => {
107
- listener ( jsEntry )
108
- } )
108
+ if ( params ?. type === LOG . TYPE_JS_LOGS ) {
109
+ let jsEntry = new JavascriptLogEntry (
110
+ params . level ,
111
+ params . text ,
112
+ params . timestamp ,
113
+ params . type ,
114
+ params . stackTrace
115
+ )
116
+
117
+ if ( filterBy != undefined ) {
118
+ if ( params ?. level === filterBy . getLevel ( ) ) {
119
+ callback ( jsEntry )
120
+ }
121
+ return
122
+ }
123
+
124
+ callback ( jsEntry )
109
125
}
110
126
} )
111
127
}
@@ -115,24 +131,29 @@ class LogInspector {
115
131
* @param callback
116
132
* @returns {Promise<void> }
117
133
*/
118
- async onJavascriptException ( callback ) {
134
+ async onJavascriptException ( callback ) {
119
135
this . ws = await this . bidi . socket
120
- let enabled = ( LOG . TYPE_JS_EXCEPTION in this . listener ) || this . logListener (
121
- LOG . TYPE_JS_EXCEPTION )
136
+ let enabled =
137
+ LOG . TYPE_JS_EXCEPTION in this . listener ||
138
+ this . logListener ( LOG . TYPE_JS_EXCEPTION )
122
139
this . listener [ LOG . TYPE_JS_EXCEPTION ] . push ( callback )
123
140
124
141
if ( enabled ) {
125
142
return
126
143
}
127
144
128
- this . ws . on ( 'message' , event => {
145
+ this . ws . on ( 'message' , ( event ) => {
129
146
const { params } = JSON . parse ( Buffer . from ( event . toString ( ) ) )
130
- if ( ( params ?. type === 'javascript' ) && ( params ?. level === 'error' ) ) {
131
-
132
- let jsErrorEntry = new JavascriptLogEntry ( params . level , params . text ,
133
- params . timestamp , params . type , params . stackTrace )
134
-
135
- this . listener [ LOG . TYPE_JS_EXCEPTION ] . forEach ( listener => {
147
+ if ( params ?. type === 'javascript' && params ?. level === 'error' ) {
148
+ let jsErrorEntry = new JavascriptLogEntry (
149
+ params . level ,
150
+ params . text ,
151
+ params . timestamp ,
152
+ params . type ,
153
+ params . stackTrace
154
+ )
155
+
156
+ this . listener [ LOG . TYPE_JS_EXCEPTION ] . forEach ( ( listener ) => {
136
157
listener ( jsErrorEntry )
137
158
} )
138
159
}
@@ -144,53 +165,82 @@ class LogInspector {
144
165
* @param callback
145
166
* @returns {Promise<void> }
146
167
*/
147
- async onLog ( callback ) {
148
- this . ws = await this . bidi . socket
149
- let enabled = ( LOG . TYPE_LOG in this . listener ) || this . logListener (
150
- LOG . TYPE_LOG )
151
- this . listener [ LOG . TYPE_LOG ] . push ( callback )
152
-
153
- if ( enabled ) {
154
- return
168
+ async onLog ( callback , filterBy = undefined ) {
169
+ if ( filterBy != undefined && ! ( filterBy instanceof FilterBy ) ) {
170
+ throw Error ( `Pass valid FilterBy object. Received: ${ filterBy } ` )
155
171
}
156
172
157
- this . ws . on ( 'message' , event => {
158
- const { params } = JSON . parse ( Buffer . from ( event . toString ( ) ) )
159
- if ( ( params ?. type === 'javascript' ) ) {
160
-
161
- let jsEntry = new JavascriptLogEntry ( params . level , params . text ,
162
- params . timestamp , params . type , params . stackTrace )
173
+ this . ws = await this . bidi . socket
163
174
164
- this . listener [ LOG . TYPE_LOG ] . forEach ( listener => {
165
- listener ( jsEntry )
166
- } )
175
+ this . ws . on ( 'message' , ( event ) => {
176
+ const { params } = JSON . parse ( Buffer . from ( event . toString ( ) ) )
177
+ if ( params ?. type === 'javascript' ) {
178
+ let jsEntry = new JavascriptLogEntry (
179
+ params . level ,
180
+ params . text ,
181
+ params . timestamp ,
182
+ params . type ,
183
+ params . stackTrace
184
+ )
185
+
186
+ if ( filterBy != undefined ) {
187
+ if ( params ?. level === filterBy . getLevel ( ) ) {
188
+ callback ( jsEntry )
189
+ }
190
+ return
191
+ }
192
+
193
+ callback ( jsEntry )
167
194
return
168
195
}
169
-
170
- if ( ( params ?. type === 'console' ) ) {
171
-
172
- let consoleEntry = new ConsoleLogEntry ( params . level , params . text ,
173
- params . timestamp , params . type , params . method , params . realm ,
174
- params . args , params . stackTrace )
175
-
176
- this . listener [ LOG . TYPE_LOG ] . forEach ( listener => {
177
- listener ( consoleEntry )
178
- } )
196
+
197
+ if ( params ?. type === 'console' ) {
198
+ let consoleEntry = new ConsoleLogEntry (
199
+ params . level ,
200
+ params . text ,
201
+ params . timestamp ,
202
+ params . type ,
203
+ params . method ,
204
+ params . realm ,
205
+ params . args ,
206
+ params . stackTrace
207
+ )
208
+
209
+ if ( filterBy != undefined ) {
210
+ if ( params ?. level === filterBy . getLevel ( ) ) {
211
+ callback ( consoleEntry )
212
+ }
213
+ return
214
+ }
215
+
216
+ callback ( consoleEntry )
179
217
return
180
- }
181
-
182
- if ( params != undefined && ! [ 'console' , 'javascript' ] . includes ( params ?. type ) ) {
183
- let genericEntry = new GenericLogEntry ( params . level , params . text ,
184
- params . timestamp , params . type , params . stackTrace )
218
+ }
185
219
186
- this . listener [ LOG . TYPE_LOG ] . forEach ( listener => {
187
- listener ( genericEntry )
188
- } )
220
+ if (
221
+ params != undefined &&
222
+ ! [ 'console' , 'javascript' ] . includes ( params ?. type )
223
+ ) {
224
+ let genericEntry = new GenericLogEntry (
225
+ params . level ,
226
+ params . text ,
227
+ params . timestamp ,
228
+ params . type ,
229
+ params . stackTrace
230
+ )
231
+
232
+ if ( filterBy != undefined ) {
233
+ if ( params ?. level === filterBy . getLevel ( ) ) {
234
+ callback ( genericEntry )
235
+ }
236
+ return
237
+ }
238
+
239
+ callback ( genericEntry )
189
240
}
190
241
} )
191
242
}
192
243
193
-
194
244
/**
195
245
* Unsubscribe to log event
196
246
* @returns {Promise<void> }
0 commit comments