20
20
* IN THE SOFTWARE.
21
21
*/
22
22
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 */
25
25
26
26
'use strict' ;
27
+
28
+ const {
29
+ ArrayPrototypePush,
30
+ Error,
31
+ ErrorCaptureStackTrace,
32
+ FunctionPrototypeBind,
33
+ JSONParse,
34
+ JSONStringify,
35
+ ObjectKeys,
36
+ Promise,
37
+ } = primordials ;
38
+
27
39
const Buffer = require ( 'buffer' ) . Buffer ;
28
40
const { EventEmitter } = require ( 'events' ) ;
29
41
const http = require ( 'http' ) ;
30
42
const URL = require ( 'url' ) ;
31
- const util = require ( 'util' ) ;
32
43
33
- const debuglog = util . debuglog ( 'inspect' ) ;
44
+ const debuglog = require ( 'internal/ util/debuglog' ) . debuglog ( 'inspect' ) ;
34
45
35
46
const kOpCodeText = 0x1 ;
36
47
const kOpCodeClose = 0x8 ;
@@ -49,14 +60,10 @@ const kTwoBytePayloadLengthField = 126;
49
60
const kEightBytePayloadLengthField = 127 ;
50
61
const kMaskingKeyWidthInBytes = 4 ;
51
62
52
- function isEmpty ( obj ) {
53
- return Object . keys ( obj ) . length === 0 ;
54
- }
55
-
56
63
function unpackError ( { code, message, data } ) {
57
64
const err = new Error ( `${ message } - ${ data } ` ) ;
58
65
err . code = code ;
59
- Error . captureStackTrace ( err , unpackError ) ;
66
+ ErrorCaptureStackTrace ( err , unpackError ) ;
60
67
return err ;
61
68
}
62
69
@@ -169,7 +176,7 @@ function decodeFrameHybi17(data) {
169
176
class Client extends EventEmitter {
170
177
constructor ( ) {
171
178
super ( ) ;
172
- this . handleChunk = this . _handleChunk . bind ( this ) ;
179
+ this . handleChunk = FunctionPrototypeBind ( this . _handleChunk , this ) ;
173
180
174
181
this . _port = undefined ;
175
182
this . _host = undefined ;
@@ -202,7 +209,7 @@ class Client extends EventEmitter {
202
209
}
203
210
let payload ;
204
211
try {
205
- payload = JSON . parse ( payloadStr ) ;
212
+ payload = JSONParse ( payloadStr ) ;
206
213
} catch ( parseError ) {
207
214
parseError . string = payloadStr ;
208
215
throw parseError ;
@@ -247,9 +254,9 @@ class Client extends EventEmitter {
247
254
const data = { id : ++ this . _lastId , method, params } ;
248
255
this . _pending [ data . id ] = ( error , result ) => {
249
256
if ( error ) reject ( unpackError ( error ) ) ;
250
- else resolve ( isEmpty ( result ) ? undefined : result ) ;
257
+ else resolve ( ObjectKeys ( result ) . length ? result : undefined ) ;
251
258
} ;
252
- const json = JSON . stringify ( data ) ;
259
+ const json = JSONStringify ( data ) ;
253
260
debuglog ( '> %s' , json ) ;
254
261
this . _socket . write ( encodeFrameHybi17 ( Buffer . from ( json ) ) ) ;
255
262
} ) ;
@@ -273,15 +280,15 @@ class Client extends EventEmitter {
273
280
return ;
274
281
}
275
282
try {
276
- resolve ( JSON . parse ( resBody ) ) ;
277
- } catch ( parseError ) {
283
+ resolve ( JSONParse ( resBody ) ) ;
284
+ } catch {
278
285
reject ( new Error ( `Response didn't contain JSON: ${ resBody } ` ) ) ;
279
286
280
287
}
281
288
}
282
289
283
290
httpRes . on ( 'error' , reject ) ;
284
- httpRes . on ( 'data' , ( chunk ) => chunks . push ( chunk ) ) ;
291
+ httpRes . on ( 'data' , ( chunk ) => ArrayPrototypePush ( chunks , chunk ) ) ;
285
292
httpRes . on ( 'end' , parseChunks ) ;
286
293
}
287
294
@@ -290,17 +297,16 @@ class Client extends EventEmitter {
290
297
} ) ;
291
298
}
292
299
293
- connect ( port , host ) {
300
+ async connect ( port , host ) {
294
301
this . _port = port ;
295
302
this . _host = host ;
296
- return this . _discoverWebsocketPath ( )
297
- . then ( ( urlPath ) => this . _connectWebsocket ( urlPath ) ) ;
303
+ const urlPath = await this . _discoverWebsocketPath ( ) ;
304
+ return this . _connectWebsocket ( urlPath ) ;
298
305
}
299
306
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 ;
304
310
}
305
311
306
312
_connectWebsocket ( urlPath ) {
0 commit comments