1515// specific language governing permissions and limitations
1616// under the License.
1717
18+ const { FilterBy } = require ( './filterBy' )
1819const { ConsoleLogEntry, JavascriptLogEntry, GenericLogEntry } = require ( './logEntries' )
1920
2021const LOG = {
2122 TYPE_CONSOLE : 'console' ,
22- TYPE_JS_LOGS : 'jsLogs' ,
23- TYPE_JS_EXCEPTION : 'jsException' ,
24- TYPE_LOG : 'log'
23+ TYPE_JS_LOGS : 'javascript' ,
2524}
2625
2726class LogInspector {
@@ -57,26 +56,36 @@ class LogInspector {
5756 * @param callback
5857 * @returns {Promise<void> }
5958 */
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 } ` )
6762 }
6863
69- this . ws . on ( 'message' , event => {
64+ this . ws = await this . bidi . socket
65+
66+ this . ws . on ( 'message' , ( event ) => {
7067 const { params } = JSON . parse ( Buffer . from ( event . toString ( ) ) )
7168
7269 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 )
8089 }
8190 } )
8291 }
@@ -86,26 +95,33 @@ class LogInspector {
8695 * @param callback
8796 * @returns {Promise<void> }
8897 */
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 } ` )
97101 }
98102
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
102104
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 ( ) ) )
105107
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 )
109125 }
110126 } )
111127 }
@@ -115,24 +131,29 @@ class LogInspector {
115131 * @param callback
116132 * @returns {Promise<void> }
117133 */
118- async onJavascriptException ( callback ) {
134+ async onJavascriptException ( callback ) {
119135 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 )
122139 this . listener [ LOG . TYPE_JS_EXCEPTION ] . push ( callback )
123140
124141 if ( enabled ) {
125142 return
126143 }
127144
128- this . ws . on ( 'message' , event => {
145+ this . ws . on ( 'message' , ( event ) => {
129146 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 ) => {
136157 listener ( jsErrorEntry )
137158 } )
138159 }
@@ -144,53 +165,82 @@ class LogInspector {
144165 * @param callback
145166 * @returns {Promise<void> }
146167 */
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 } ` )
155171 }
156172
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
163174
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 )
167194 return
168195 }
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 )
179217 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+ }
185219
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 )
189240 }
190241 } )
191242 }
192243
193-
194244 /**
195245 * Unsubscribe to log event
196246 * @returns {Promise<void> }
0 commit comments