Skip to content

Commit

Permalink
fixup! fixup! fixup! chore(test): improve log output on error
Browse files Browse the repository at this point in the history
  • Loading branch information
mmarchini committed Jul 15, 2023
1 parent 7835151 commit 93576d4
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions lib/internal/inspector-client.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const net = require('net');
const http = require('http');
const { EventEmitter } = require('events');

Expand All @@ -20,6 +21,35 @@ function getWebSocketPath(host, port) {
});
}

function timeout(time) {
return new Promise((resolve) => {
setTimeout(resolve, time);
});
}

function waitForConnection (host, port) {
return new Promise((resolve, reject) => {
const socket = net.createConnection({ host, port }, () => { socket.close(); resolve() });
socket.on('error', reject);
});
}

async function isInspectorOpen(host, port) {
// make number of retries configurable?
let retries = 5;
let portOpen = false;
do {
try {
await waitForConnection(host, port);
portOpen = true;
} catch {
// make timeout between attempts configurable?
await timeout(75);
}
} while (!portOpen && retries-- > 0);
return portOpen;
}

function handleMessages(data) {
const { id, method, result, params} = JSON.parse(data);
if (id) {
Expand All @@ -44,6 +74,7 @@ class Client extends EventEmitter {

async connect() {
const url = await getWebSocketPath(this.host, this.port);
await isInspectorOpen();
this._ws = new WebSocket(url);
this._ws.on('message', handleMessages.bind(this));
return new Promise((resolve) => {
Expand Down

0 comments on commit 93576d4

Please sign in to comment.