Skip to content
This repository was archived by the owner on Feb 1, 2022. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion lib/internal/inspect-protocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
const crypto = require('crypto');
const { EventEmitter } = require('events');
const http = require('http');
const URL = require('url');
const util = require('util');

const debuglog = util.debuglog('inspect');
Expand Down Expand Up @@ -228,7 +229,53 @@ class Client extends EventEmitter {
});
}

_fetchJSON(urlPath) {
return new Promise((resolve, reject) => {
const httpReq = http.get({
host: this._host,
port: this._port,
path: urlPath,
});

const chunks = [];

function onResponse(httpRes) {
function parseChunks() {
const resBody = Buffer.concat(chunks).toString();
if (httpRes.statusCode !== 200) {
reject(new Error(`Unexpected ${httpRes.statusCode}: ${resBody}`));
return;
}
try {
resolve(JSON.parse(resBody));
} catch (parseError) {
reject(new Error(`Response did not contain valid JSON: ${resBody}`));
return;
}
}

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

httpReq.on('error', reject);
httpReq.on('response', onResponse);
});
}

connect() {
return this._discoverWebsocketPath()
.then(urlPath => this._connectWebsocket(urlPath));
}

_discoverWebsocketPath() {
return this._fetchJSON('/json')
.then(([{ webSocketDebuggerUrl }]) =>
URL.parse(webSocketDebuggerUrl).path);
}

_connectWebsocket(urlPath) {
this.reset();

const key1 = crypto.randomBytes(16).toString('base64');
Expand All @@ -237,18 +284,20 @@ class Client extends EventEmitter {
const httpReq = this._http = http.request({
host: this._host,
port: this._port,
path: '/node',
path: urlPath,
headers: {
Connection: 'Upgrade',
Upgrade: 'websocket',
'Sec-WebSocket-Key': key1,
'Sec-WebSocket-Version': '13',
},
});
httpReq.on('error', e => {
this.emit('error', e);
});
httpReq.on('response', httpRes => {
if (httpRes.statusCode >= 400) {
process.stderr.write(`Unexpected HTTP response: ${httpRes.statusCode}\n`);
httpRes.pipe(process.stderr);
} else {
httpRes.pipe(process.stderr);
Expand Down