Skip to content

Commit

Permalink
Reuse port numbers of previously started server names/IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
svicalifornia committed Jun 24, 2022
1 parent 0d7eba9 commit 542d3ab
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 13 deletions.
Empty file modified src/cli/bin.js
100644 → 100755
Empty file.
37 changes: 34 additions & 3 deletions src/cli/run.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,41 @@
const cp = require("child_process");
const getPort = require("get-port");
const getNewPort = require("get-port");
const http = require("http");
const servers = require("./servers");
const getCmd = require("../get-cmd");
const daemonConf = require("../conf");

const signals = ["SIGINT", "SIGTERM", "SIGHUP"];

function getPort(id) {
return new Promise((resolve, reject) => {
const { host, port } = daemonConf;
const options = {
host,
port,
path: `/_/servers/${id}/port`,
method: "GET"
};
const req = http.request(options, res => {
let body = "";
req.on("error", reject);
res.on("data", function(chunk) {
body += chunk;
});
res.on("end", () => {
if (!res.complete) {
reject(
new Error("HTTP connection terminated before response complete")
);
}
resolve(body ? parseInt(body, 10) : getNewPort());
});
});
req.on("error", reject);
req.end();
});
}

module.exports = {
// For testing purpose, allows stubbing cp.spawnSync
_spawnSync(...args) {
Expand All @@ -26,7 +57,7 @@ module.exports = {
const serverAddress = `http://localhost:${port}`;

process.env.PORT = port;
servers.add(serverAddress, opts);
servers.add(serverAddress, { ...opts, port });

signals.forEach(signal => process.on(signal, cleanAndExit));

Expand All @@ -43,7 +74,7 @@ module.exports = {
if (opts.port) {
startServer(opts.port);
} else {
getPort()
getPort(servers.getId(opts))
.then(startServer)
.catch(err => {
throw err;
Expand Down
21 changes: 12 additions & 9 deletions src/cli/servers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const serversDir = common.serversDir;
module.exports = {
add,
rm,
ls
ls,
getId
};

function isUrl(str) {
Expand All @@ -29,7 +30,9 @@ function domainify(str) {
);
}

function getId(cwd) {
function getId(opts, cwd) {
if (opts.name) return domainify(opts.name);
if (!cwd) cwd = process.cwd();
return domainify(path.basename(cwd));
}

Expand All @@ -41,7 +44,7 @@ function add(param, opts = {}) {
mkdirp.sync(serversDir);

const cwd = opts.dir || process.cwd();
const id = opts.name ? domainify(opts.name) : getId(cwd);
const id = getId(opts, cwd);
const force = opts.force;

const file = getServerFile(id);
Expand Down Expand Up @@ -97,11 +100,12 @@ function add(param, opts = {}) {
}
});
}
}

// Copy port option
if (opts.port) {
conf.env.PORT = opts.port;
}
// Copy port option
if (opts.port) {
if (!conf.env) conf.env = {};
conf.env.PORT = opts.port;
}

const data = JSON.stringify(conf, null, 2);
Expand All @@ -126,8 +130,7 @@ function add(param, opts = {}) {
}

function rm(opts = {}) {
const cwd = process.cwd();
const id = opts.n || getId(cwd);
const id = getId(opts);
const file = getServerFile(id);

console.log(`Remove ${tildify(file)}`);
Expand Down
9 changes: 9 additions & 0 deletions src/daemon/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Group extends EventEmitter {
super();

this._list = {};
this._ports = {};
this._proxy = httpProxy.createProxyServer({
xfwd: true
});
Expand Down Expand Up @@ -64,7 +65,15 @@ class Group extends EventEmitter {
return this._list[id];
}

findPort(id) {
return this._ports[id];
}

add(id, conf) {
if (conf.env && conf.env.PORT) {
this._ports[id] = conf.env.PORT;
}

if (conf.target) {
log(`Add target ${id}`);
this._list[id] = conf;
Expand Down
6 changes: 6 additions & 0 deletions src/daemon/routers/api/servers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ module.exports = group => {
res.json(group.list());
});

router.get("/:id/port", (req, res) => {
const port = group.findPort(req.params.id);
if (port) res.json(port);
return res.end();
});

router.post(
"/:id/start",
group.exists.bind(group),
Expand Down
6 changes: 5 additions & 1 deletion test/cli/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ test("spawn with port", t => {
"should add a target"
);

t.is(servers.add.firstCall.args[1], opts, "should pass options to add");
t.deepEqual(
servers.add.firstCall.args[1],
opts,
"should pass options to add"
);

t.true(servers.rm.called);
t.is(servers.rm.firstCall.args[0], opts, "should use same options to remove");
Expand Down

0 comments on commit 542d3ab

Please sign in to comment.