Skip to content

Commit

Permalink
Introduce fixed port or host
Browse files Browse the repository at this point in the history
  • Loading branch information
willnode committed Jun 17, 2024
1 parent 7da3c5f commit 36d1ff3
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,32 @@
const { getFreePort, tryConnect } = require('./util');
const { spawn } = require('child_process');
const httpProxy = require('http-proxy');
const { isIPv4, isIPv6 } = require('net');
const http = require('unit-http');

async function init() {
var outPort;
try {
outPort = await getFreePort();
} catch (err) {
throw new Error("Can't get free port");
var port;
var host;
if (process.env.PORT) {
port = parseInt(process.env.PORT);
}
if (!port) {
try {
outPort = await getFreePort();
} catch (err) {
throw new Error("Can't get free port");
}
}
if (process.env.HOST && (isIPv4(process.env.HOST) || isIPv6(process.env.HOST))) {
host = process.env.HOST;
} else {
host = '127.0.0.1';
}

if (process.argv.length > 2) {
const cmd = process.argv[2];
const args = process.argv.slice(3);
const env = { ...process.env, PORT: outPort.toString() };
const env = { ...process.env, PORT: port.toString(), HOST: host };

const child = spawn(cmd, args, { stdio: 'inherit', env, detached: false });

Expand All @@ -38,7 +50,7 @@ async function init() {
});
}

return outPort;
return { port, host };
}


Expand Down Expand Up @@ -91,8 +103,8 @@ async function startProxy(host, port) {

(async function () {
try {
let port = await init();
await startProxy('localhost', port);
let { host, port } = await init();
await startProxy(host, port);
} catch (err) {
console.error(err);
process.exit(1);
Expand Down

0 comments on commit 36d1ff3

Please sign in to comment.