Skip to content

Commit 7b9d493

Browse files
rrthomasJustinBeckwith
andauthoredOct 5, 2021
fix: allow system to generate random port when desired (#319) (#328)
Co-authored-by: Justin Beckwith <beckwith@google.com>
1 parent 02f9af6 commit 7b9d493

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed
 

‎src/index.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {EventEmitter} from 'events';
22
import {URL} from 'url';
3+
import {AddressInfo} from 'net';
34
import * as http from 'http';
45
import * as path from 'path';
56
import {Readable} from 'stream';
@@ -81,13 +82,17 @@ export class LinkChecker extends EventEmitter {
8182
let server: http.Server | undefined;
8283
const hasHttpPaths = options.path.find(x => x.startsWith('http'));
8384
if (!hasHttpPaths) {
84-
const port = options.port || 5000 + Math.round(Math.random() * 1000);
85+
let port = options.port;
8586
server = await startWebServer({
8687
root: options.serverRoot!,
8788
port,
8889
markdown: options.markdown,
8990
directoryListing: options.directoryListing,
9091
});
92+
if (port === undefined) {
93+
const addr = server.address() as AddressInfo;
94+
port = addr.port;
95+
}
9196
for (let i = 0; i < options.path.length; i++) {
9297
if (options.path[i].startsWith('/')) {
9398
options.path[i] = options.path[i].slice(1);

‎src/server.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {AddressInfo} from 'net';
12
import * as http from 'http';
23
import * as path from 'path';
34
import * as fs from 'fs';
@@ -16,7 +17,7 @@ export interface WebServerOptions {
1617
// The local path that should be mounted as a static web server
1718
root: string;
1819
// The port on which to start the local web server
19-
port: number;
20+
port?: number;
2021
// If markdown should be automatically compiled and served
2122
markdown?: boolean;
2223
// Should directories automatically serve an inde page
@@ -33,8 +34,12 @@ export async function startWebServer(options: WebServerOptions) {
3334
return new Promise<http.Server>((resolve, reject) => {
3435
const server = http
3536
.createServer((req, res) => handleRequest(req, res, root, options))
36-
.listen(options.port, () => resolve(server))
37+
.listen(options.port || 0, () => resolve(server))
3738
.on('error', reject);
39+
if (!options.port) {
40+
const addr = server.address() as AddressInfo;
41+
options.port = addr.port;
42+
}
3843
enableDestroy(server);
3944
});
4045
}

‎test/test.server.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
import * as assert from 'assert';
22
import {describe, it, before, after} from 'mocha';
33
import {startWebServer} from '../src/server';
4+
import {AddressInfo} from 'net';
45
import {Server} from 'http';
56
import {request} from 'gaxios';
67
import * as fs from 'fs';
78

89
describe('server', () => {
910
let server: Server;
10-
const port = 5000 + Math.round(Math.random() * 1000);
11-
const rootUrl = `http://localhost:${port}`;
11+
let rootUrl: string;
1212
const contents = fs.readFileSync('test/fixtures/server/index.html', 'utf-8');
1313
before(async () => {
1414
server = await startWebServer({
15-
port,
1615
directoryListing: true,
1716
markdown: true,
1817
root: 'test/fixtures/server',
1918
});
19+
const addr = server.address() as AddressInfo;
20+
rootUrl = `http://localhost:${addr.port}`;
2021
});
2122
after(() => server.destroy());
2223

0 commit comments

Comments
 (0)
Please sign in to comment.