Skip to content

Commit

Permalink
debugger: enable linter on internal/inspector/inspect_client
Browse files Browse the repository at this point in the history
PR-URL: nodejs#38417
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
aduh95 committed Jul 19, 2021
1 parent fbabe59 commit 26fb6b9
Showing 1 changed file with 29 additions and 23 deletions.
52 changes: 29 additions & 23 deletions lib/internal/inspector/inspect_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,28 @@
* IN THE SOFTWARE.
*/

// TODO(trott): enable ESLint
/* eslint-disable */
// TODO(aduh95): use errors exported by the internal/errors module
/* eslint-disable no-restricted-syntax */

'use strict';

const {
ArrayPrototypePush,
Error,
ErrorCaptureStackTrace,
FunctionPrototypeBind,
JSONParse,
JSONStringify,
ObjectKeys,
Promise,
} = primordials;

const Buffer = require('buffer').Buffer;
const { EventEmitter } = require('events');
const http = require('http');
const URL = require('url');
const util = require('util');

const debuglog = util.debuglog('inspect');
const debuglog = require('internal/util/debuglog').debuglog('inspect');

const kOpCodeText = 0x1;
const kOpCodeClose = 0x8;
Expand All @@ -49,14 +60,10 @@ const kTwoBytePayloadLengthField = 126;
const kEightBytePayloadLengthField = 127;
const kMaskingKeyWidthInBytes = 4;

function isEmpty(obj) {
return Object.keys(obj).length === 0;
}

function unpackError({ code, message, data }) {
const err = new Error(`${message} - ${data}`);
err.code = code;
Error.captureStackTrace(err, unpackError);
ErrorCaptureStackTrace(err, unpackError);
return err;
}

Expand Down Expand Up @@ -169,7 +176,7 @@ function decodeFrameHybi17(data) {
class Client extends EventEmitter {
constructor() {
super();
this.handleChunk = this._handleChunk.bind(this);
this.handleChunk = FunctionPrototypeBind(this._handleChunk, this);

this._port = undefined;
this._host = undefined;
Expand Down Expand Up @@ -202,7 +209,7 @@ class Client extends EventEmitter {
}
let payload;
try {
payload = JSON.parse(payloadStr);
payload = JSONParse(payloadStr);
} catch (parseError) {
parseError.string = payloadStr;
throw parseError;
Expand Down Expand Up @@ -247,9 +254,9 @@ class Client extends EventEmitter {
const data = { id: ++this._lastId, method, params };
this._pending[data.id] = (error, result) => {
if (error) reject(unpackError(error));
else resolve(isEmpty(result) ? undefined : result);
else resolve(ObjectKeys(result).length ? result : undefined);
};
const json = JSON.stringify(data);
const json = JSONStringify(data);
debuglog('> %s', json);
this._socket.write(encodeFrameHybi17(Buffer.from(json)));
});
Expand All @@ -273,15 +280,15 @@ class Client extends EventEmitter {
return;
}
try {
resolve(JSON.parse(resBody));
} catch (parseError) {
resolve(JSONParse(resBody));
} catch {
reject(new Error(`Response didn't contain JSON: ${resBody}`));

}
}

httpRes.on('error', reject);
httpRes.on('data', (chunk) => chunks.push(chunk));
httpRes.on('data', (chunk) => ArrayPrototypePush(chunks, chunk));
httpRes.on('end', parseChunks);
}

Expand All @@ -290,17 +297,16 @@ class Client extends EventEmitter {
});
}

connect(port, host) {
async connect(port, host) {
this._port = port;
this._host = host;
return this._discoverWebsocketPath()
.then((urlPath) => this._connectWebsocket(urlPath));
const urlPath = await this._discoverWebsocketPath();
return this._connectWebsocket(urlPath);
}

_discoverWebsocketPath() {
return this._fetchJSON('/json')
.then(({ 0: { webSocketDebuggerUrl } }) =>
URL.parse(webSocketDebuggerUrl).path);
async _discoverWebsocketPath() {
const { 0: { webSocketDebuggerUrl } } = await this._fetchJSON('/json');
return URL.parse(webSocketDebuggerUrl).path;
}

_connectWebsocket(urlPath) {
Expand Down

0 comments on commit 26fb6b9

Please sign in to comment.