2020 * IN THE SOFTWARE.
2121 */
2222
23- // TODO(trott ): enable ESLint
24- /* eslint-disable */
23+ // TODO(aduh95 ): use errors exported by the internal/errors module
24+ /* eslint-disable no-restricted-syntax */
2525
2626'use strict' ;
27+
28+ const {
29+ ArrayPrototypePush,
30+ Error,
31+ ErrorCaptureStackTrace,
32+ FunctionPrototypeBind,
33+ JSONParse,
34+ JSONStringify,
35+ ObjectKeys,
36+ Promise,
37+ } = primordials ;
38+
2739const Buffer = require ( 'buffer' ) . Buffer ;
2840const { EventEmitter } = require ( 'events' ) ;
2941const http = require ( 'http' ) ;
3042const URL = require ( 'url' ) ;
31- const util = require ( 'util' ) ;
3243
33- const debuglog = util . debuglog ( 'inspect' ) ;
44+ const debuglog = require ( 'internal/ util/debuglog' ) . debuglog ( 'inspect' ) ;
3445
3546const kOpCodeText = 0x1 ;
3647const kOpCodeClose = 0x8 ;
@@ -49,14 +60,10 @@ const kTwoBytePayloadLengthField = 126;
4960const kEightBytePayloadLengthField = 127 ;
5061const kMaskingKeyWidthInBytes = 4 ;
5162
52- function isEmpty ( obj ) {
53- return Object . keys ( obj ) . length === 0 ;
54- }
55-
5663function unpackError ( { code, message, data } ) {
5764 const err = new Error ( `${ message } - ${ data } ` ) ;
5865 err . code = code ;
59- Error . captureStackTrace ( err , unpackError ) ;
66+ ErrorCaptureStackTrace ( err , unpackError ) ;
6067 return err ;
6168}
6269
@@ -169,7 +176,7 @@ function decodeFrameHybi17(data) {
169176class Client extends EventEmitter {
170177 constructor ( ) {
171178 super ( ) ;
172- this . handleChunk = this . _handleChunk . bind ( this ) ;
179+ this . handleChunk = FunctionPrototypeBind ( this . _handleChunk , this ) ;
173180
174181 this . _port = undefined ;
175182 this . _host = undefined ;
@@ -202,7 +209,7 @@ class Client extends EventEmitter {
202209 }
203210 let payload ;
204211 try {
205- payload = JSON . parse ( payloadStr ) ;
212+ payload = JSONParse ( payloadStr ) ;
206213 } catch ( parseError ) {
207214 parseError . string = payloadStr ;
208215 throw parseError ;
@@ -247,9 +254,9 @@ class Client extends EventEmitter {
247254 const data = { id : ++ this . _lastId , method, params } ;
248255 this . _pending [ data . id ] = ( error , result ) => {
249256 if ( error ) reject ( unpackError ( error ) ) ;
250- else resolve ( isEmpty ( result ) ? undefined : result ) ;
257+ else resolve ( ObjectKeys ( result ) . length ? result : undefined ) ;
251258 } ;
252- const json = JSON . stringify ( data ) ;
259+ const json = JSONStringify ( data ) ;
253260 debuglog ( '> %s' , json ) ;
254261 this . _socket . write ( encodeFrameHybi17 ( Buffer . from ( json ) ) ) ;
255262 } ) ;
@@ -273,15 +280,15 @@ class Client extends EventEmitter {
273280 return ;
274281 }
275282 try {
276- resolve ( JSON . parse ( resBody ) ) ;
277- } catch ( parseError ) {
283+ resolve ( JSONParse ( resBody ) ) ;
284+ } catch {
278285 reject ( new Error ( `Response didn't contain JSON: ${ resBody } ` ) ) ;
279286
280287 }
281288 }
282289
283290 httpRes . on ( 'error' , reject ) ;
284- httpRes . on ( 'data' , ( chunk ) => chunks . push ( chunk ) ) ;
291+ httpRes . on ( 'data' , ( chunk ) => ArrayPrototypePush ( chunks , chunk ) ) ;
285292 httpRes . on ( 'end' , parseChunks ) ;
286293 }
287294
@@ -290,17 +297,16 @@ class Client extends EventEmitter {
290297 } ) ;
291298 }
292299
293- connect ( port , host ) {
300+ async connect ( port , host ) {
294301 this . _port = port ;
295302 this . _host = host ;
296- return this . _discoverWebsocketPath ( )
297- . then ( ( urlPath ) => this . _connectWebsocket ( urlPath ) ) ;
303+ const urlPath = await this . _discoverWebsocketPath ( ) ;
304+ return this . _connectWebsocket ( urlPath ) ;
298305 }
299306
300- _discoverWebsocketPath ( ) {
301- return this . _fetchJSON ( '/json' )
302- . then ( ( { 0 : { webSocketDebuggerUrl } } ) =>
303- URL . parse ( webSocketDebuggerUrl ) . path ) ;
307+ async _discoverWebsocketPath ( ) {
308+ const { 0 : { webSocketDebuggerUrl } } = await this . _fetchJSON ( '/json' ) ;
309+ return URL . parse ( webSocketDebuggerUrl ) . path ;
304310 }
305311
306312 _connectWebsocket ( urlPath ) {
0 commit comments