Skip to content

Commit 7a9ae2c

Browse files
committed
Minor fixes for detecting free PIO Home port // Issue #19
1 parent df114c8 commit 7a9ae2c

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

src/home.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@ import ws from 'ws';
2020

2121
const SERVER_LAUNCH_TIMEOUT = 30; // 30 seconds
2222
const SERVER_AUTOSHUTDOWN_TIMEOUT = 3600; // 1 hour
23-
const HTTP_HOST = '127.0.0.1';
2423
const HTTP_PORT_BEGIN = 8010;
2524
const HTTP_PORT_END = 8050;
2625
const SESSION_ID = crypto
2726
.createHash('sha1')
2827
.update(crypto.randomBytes(512))
2928
.digest('hex');
29+
let _HTTP_HOST = '127.0.0.1';
3030
let _HTTP_PORT = 0;
31-
let _HTTP_HOST = HTTP_HOST;
3231
let _IDECMDS_LISTENER_STATUS = 0;
3332

3433
export function constructServerUrl({
@@ -123,7 +122,7 @@ async function isPortUsed(host, port) {
123122
async function findFreePort() {
124123
let port = HTTP_PORT_BEGIN;
125124
while (port < HTTP_PORT_END) {
126-
if (!(await isPortUsed(HTTP_HOST, port))) {
125+
if (!(await isPortUsed(_HTTP_HOST, port))) {
127126
return port;
128127
}
129128
port++;
@@ -162,7 +161,9 @@ async function _ensureServerStarted(options = {}) {
162161
if (_HTTP_PORT === 0) {
163162
_HTTP_PORT = options.port || (await findFreePort());
164163
}
165-
_HTTP_HOST = options.host || HTTP_HOST;
164+
if (options.host) {
165+
_HTTP_HOST = options.host;
166+
}
166167
if (!(await isServerStarted())) {
167168
await new Promise((resolve, reject) => {
168169
runPIOCommand(
@@ -185,14 +186,16 @@ async function _ensureServerStarted(options = {}) {
185186
}
186187
}
187188
);
188-
tcpPortUsed.waitUntilUsed(_HTTP_PORT, _HTTP_HOST, 500, SERVER_LAUNCH_TIMEOUT * 1000).then(
189-
() => {
190-
resolve(true);
191-
},
192-
(err) => {
193-
reject(new Error('Could not start PIO Home server: ' + err.toString()));
194-
}
195-
);
189+
tcpPortUsed
190+
.waitUntilUsedOnHost(_HTTP_PORT, _HTTP_HOST, 500, SERVER_LAUNCH_TIMEOUT * 1000)
191+
.then(
192+
() => {
193+
resolve(true);
194+
},
195+
(err) => {
196+
reject(new Error('Could not start PIO Home server: ' + err.toString()));
197+
}
198+
);
196199
});
197200
}
198201
if (options.onIDECommand) {

0 commit comments

Comments
 (0)