Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

debugger: enable linter on internal/inspector/inspect_client #38417

Merged
merged 1 commit into from
May 3, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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