From 26fb6b90bf6a4f1165d705fff0056f14bb17e5a1 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Mon, 26 Apr 2021 10:10:54 +0200 Subject: [PATCH] debugger: enable linter on `internal/inspector/inspect_client` PR-URL: https://github.com/nodejs/node/pull/38417 Reviewed-By: James M Snell Reviewed-By: Rich Trott --- lib/internal/inspector/inspect_client.js | 52 +++++++++++++----------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/lib/internal/inspector/inspect_client.js b/lib/internal/inspector/inspect_client.js index 831a7fda1cdff1..ba1e9bc30ce5ce 100644 --- a/lib/internal/inspector/inspect_client.js +++ b/lib/internal/inspector/inspect_client.js @@ -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; @@ -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; } @@ -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; @@ -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; @@ -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))); }); @@ -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); } @@ -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) {