Skip to content

Commit ccee5a9

Browse files
authored
dev_server: Serve on localhost by default (#3115)
* dev_server: serve on localhost only by default * Limit characters in route for /out/*/listing.js
1 parent ef5d229 commit ccee5a9

File tree

1 file changed

+41
-16
lines changed

1 file changed

+41
-16
lines changed

src/common/tools/dev_server.ts

+41-16
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@ import { makeListing } from './crawl.js';
1414
// Make sure that makeListing doesn't cache imported spec files. See crawl().
1515
process.env.STANDALONE_DEV_SERVER = '1';
1616

17+
function usage(rc: number): void {
18+
console.error(`\
19+
Usage:
20+
tools/dev_server
21+
tools/dev_server 0.0.0.0
22+
npm start
23+
npm start 0.0.0.0
24+
25+
By default, serves on localhost only. If the argument 0.0.0.0 is passed, serves on all interfaces.
26+
`);
27+
process.exit(rc);
28+
}
29+
1730
const srcDir = path.resolve(__dirname, '../../');
1831

1932
// Import the project's babel.config.js. We'll use the same config for the runtime compiler.
@@ -110,7 +123,7 @@ app.use('/out-wpt', express.static(path.resolve(srcDir, '../out-wpt')));
110123
app.use('/docs/tsdoc', express.static(path.resolve(srcDir, '../docs/tsdoc')));
111124

112125
// Serve a suite's listing.js file by crawling the filesystem for all tests.
113-
app.get('/out/:suite/listing.js', async (req, res, next) => {
126+
app.get('/out/:suite([a-zA-Z0-9_-]+)/listing.js', async (req, res, next) => {
114127
const suite = req.params['suite'];
115128

116129
if (listingCache.has(suite)) {
@@ -162,28 +175,40 @@ app.get('/out/**/*.js', async (req, res, next) => {
162175
}
163176
});
164177

165-
const host = '0.0.0.0';
166-
const port = 8080;
167-
// Find an available port, starting at 8080.
168-
portfinder.getPort({ host, port }, (err, port) => {
169-
if (err) {
170-
throw err;
178+
// Serve everything else (not .js) as static, and directories as directory listings.
179+
app.use('/out', serveIndex(path.resolve(srcDir, '../src')));
180+
app.use('/out', express.static(path.resolve(srcDir, '../src')));
181+
182+
void (async () => {
183+
let host = '127.0.0.1';
184+
if (process.argv.length >= 3) {
185+
if (process.argv.length !== 3) usage(1);
186+
if (process.argv[2] === '0.0.0.0') {
187+
host = '0.0.0.0';
188+
} else {
189+
usage(1);
190+
}
171191
}
192+
193+
console.log(`Finding an available port on ${host}...`);
194+
const kPortFinderStart = 8080;
195+
const port = await portfinder.getPortPromise({ host, port: kPortFinderStart });
196+
172197
watcher.on('ready', () => {
173198
// Listen on the available port.
174199
app.listen(port, host, () => {
175200
console.log('Standalone test runner running at:');
176-
for (const iface of Object.values(os.networkInterfaces())) {
177-
for (const details of iface || []) {
178-
if (details.family === 'IPv4') {
179-
console.log(` http://${details.address}:${port}/standalone/`);
201+
if (host === '0.0.0.0') {
202+
for (const iface of Object.values(os.networkInterfaces())) {
203+
for (const details of iface || []) {
204+
if (details.family === 'IPv4') {
205+
console.log(` http://${details.address}:${port}/standalone/`);
206+
}
180207
}
181208
}
209+
} else {
210+
console.log(` http://${host}:${port}/standalone/`);
182211
}
183212
});
184213
});
185-
});
186-
187-
// Serve everything else (not .js) as static, and directories as directory listings.
188-
app.use('/out', serveIndex(path.resolve(srcDir, '../src')));
189-
app.use('/out', express.static(path.resolve(srcDir, '../src')));
214+
})();

0 commit comments

Comments
 (0)