Skip to content

Commit

Permalink
fix: allow system to generate random port when desired (#319) (#328)
Browse files Browse the repository at this point in the history
Co-authored-by: Justin Beckwith <beckwith@google.com>
  • Loading branch information
rrthomas and JustinBeckwith authored Oct 5, 2021
1 parent 02f9af6 commit 7b9d493
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {EventEmitter} from 'events';
import {URL} from 'url';
import {AddressInfo} from 'net';
import * as http from 'http';
import * as path from 'path';
import {Readable} from 'stream';
Expand Down Expand Up @@ -81,13 +82,17 @@ export class LinkChecker extends EventEmitter {
let server: http.Server | undefined;
const hasHttpPaths = options.path.find(x => x.startsWith('http'));
if (!hasHttpPaths) {
const port = options.port || 5000 + Math.round(Math.random() * 1000);
let port = options.port;
server = await startWebServer({
root: options.serverRoot!,
port,
markdown: options.markdown,
directoryListing: options.directoryListing,
});
if (port === undefined) {
const addr = server.address() as AddressInfo;
port = addr.port;
}
for (let i = 0; i < options.path.length; i++) {
if (options.path[i].startsWith('/')) {
options.path[i] = options.path[i].slice(1);
Expand Down
9 changes: 7 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {AddressInfo} from 'net';
import * as http from 'http';
import * as path from 'path';
import * as fs from 'fs';
Expand All @@ -16,7 +17,7 @@ export interface WebServerOptions {
// The local path that should be mounted as a static web server
root: string;
// The port on which to start the local web server
port: number;
port?: number;
// If markdown should be automatically compiled and served
markdown?: boolean;
// Should directories automatically serve an inde page
Expand All @@ -33,8 +34,12 @@ export async function startWebServer(options: WebServerOptions) {
return new Promise<http.Server>((resolve, reject) => {
const server = http
.createServer((req, res) => handleRequest(req, res, root, options))
.listen(options.port, () => resolve(server))
.listen(options.port || 0, () => resolve(server))
.on('error', reject);
if (!options.port) {
const addr = server.address() as AddressInfo;
options.port = addr.port;
}
enableDestroy(server);
});
}
Expand Down
7 changes: 4 additions & 3 deletions test/test.server.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import * as assert from 'assert';
import {describe, it, before, after} from 'mocha';
import {startWebServer} from '../src/server';
import {AddressInfo} from 'net';
import {Server} from 'http';
import {request} from 'gaxios';
import * as fs from 'fs';

describe('server', () => {
let server: Server;
const port = 5000 + Math.round(Math.random() * 1000);
const rootUrl = `http://localhost:${port}`;
let rootUrl: string;
const contents = fs.readFileSync('test/fixtures/server/index.html', 'utf-8');
before(async () => {
server = await startWebServer({
port,
directoryListing: true,
markdown: true,
root: 'test/fixtures/server',
});
const addr = server.address() as AddressInfo;
rootUrl = `http://localhost:${addr.port}`;
});
after(() => server.destroy());

Expand Down

0 comments on commit 7b9d493

Please sign in to comment.