Skip to content

Commit 030b172

Browse files
committed
chore: Add fallback port to devtools server
1 parent 90f954a commit 030b172

1 file changed

Lines changed: 34 additions & 5 deletions

File tree

‎devtools/test_dashboard/server.mjs‎

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { once } from 'node:events';
12
import http from 'node:http';
23
import { build, context } from 'esbuild';
34
import minimist from 'minimist';
@@ -7,7 +8,16 @@ import constants from '../../tasks/util/constants.js';
78
import { createMocksList, getMockFiles, readFiles, saveMockListToFile } from '../dashboard_utilities.mjs';
89

910
var args = minimist(process.argv.slice(2), {});
10-
var PORT = args.port || 3000;
11+
// An explicit --port is used as given. Without one, try PORT_ATTEMPTS consecutive ports from DEFAULT_PORT.
12+
// This can be passed using the npm script as follows: npm start -- --port XXXX.
13+
const DEFAULT_PORT = 3000;
14+
const PORT_ATTEMPTS = 3;
15+
const portIsExplicit = args.port !== undefined;
16+
const firstPort = portIsExplicit ? Number(args.port) : DEFAULT_PORT;
17+
if (portIsExplicit && !(/^\d+$/.test(String(args.port)) && firstPort >= 1 && firstPort <= 65535)) {
18+
console.error(`Invalid --port value "${args.port}". Pass an integer from 1 to 65535.`);
19+
process.exit(1);
20+
}
1121
var strict = args.strict;
1222
var mathjax3 = args.mathjax3;
1323
var mathjax3chtml = args.mathjax3chtml;
@@ -21,11 +31,11 @@ await getMockFiles().then(readFiles).then(createMocksList).then(saveMockListToFi
2131
build(devtoolsConfig);
2232

2333
var ctx = await context(localDevConfig);
24-
devServer();
34+
await devServer();
2535
console.log('watching esbuild...');
2636
await ctx.watch();
2737

28-
function devServer() {
38+
async function devServer() {
2939
const staticFilesHandler = sirv(constants.pathToRoot, {
3040
dev: true,
3141
maxAge: 0,
@@ -58,7 +68,26 @@ function devServer() {
5868
});
5969

6070
// Start the server up!
61-
server.listen(PORT);
71+
const attempts = portIsExplicit ? 1 : PORT_ATTEMPTS;
72+
let port;
73+
for (let i = 0; i < attempts && port === undefined; i++) {
74+
const candidate = firstPort + i;
75+
try {
76+
server.listen(candidate);
77+
await once(server, 'listening');
78+
port = candidate;
79+
} catch (err) {
80+
if (err.code !== 'EADDRINUSE') throw err;
81+
console.warn(`Port ${candidate} is in use.`);
82+
if (i + 1 < attempts) console.warn(`Trying port ${candidate + 1}.`);
83+
}
84+
}
85+
if (port === undefined) {
86+
console.error(
87+
'All attempted ports are in use. Stop a process on one of those ports or pass a free port with --port.'
88+
);
89+
process.exit(1);
90+
}
6291

6392
let indexName = 'index';
6493
if (mathjax3) indexName += '-mathjax3';
@@ -67,6 +96,6 @@ function devServer() {
6796
indexName += '.html';
6897

6998
// open up browser window
70-
const serverUrl = `http://localhost:${PORT}/devtools/test_dashboard/${indexName}${strict ? '?strict=true' : ''}`;
99+
const serverUrl = `http://localhost:${port}/devtools/test_dashboard/${indexName}${strict ? '?strict=true' : ''}`;
71100
console.log(` \x1b[1m\x1b[32m➜\x1b[0m \x1b[1mLocal:\x1b[0m \x1b[1m\x1b[36m${serverUrl}\x1b[0m\n`);
72101
}

0 commit comments

Comments
 (0)