Skip to content

Commit

Permalink
Added support to set network interface to bind
Browse files Browse the repository at this point in the history
  • Loading branch information
ninadbstack committed Nov 28, 2023
1 parent f5924b0 commit e497347
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
9 changes: 7 additions & 2 deletions packages/core/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ export class Server extends http.Server {
});
}

// return host bind address - defaults to 0.0.0.0
get host() {
return process.env.PERCY_SERVER_HOST || '0.0.0.0'

Check failure on line 119 in packages/core/src/server.js

View workflow job for this annotation

GitHub Actions / Lint

Missing semicolon
}

// return the listening port or any default port
get port() {
return super.address()?.port ?? this.#defaultPort;
Expand All @@ -122,7 +127,7 @@ export class Server extends http.Server {
// return a string representation of the server address
address() {
let port = this.port;
let host = 'http://localhost';
let host = `http://${host}`;

Check failure on line 130 in packages/core/src/server.js

View workflow job for this annotation

GitHub Actions / Lint

'host' was used before it was defined
return port ? `${host}:${port}` : host;
}

Expand All @@ -131,7 +136,7 @@ export class Server extends http.Server {
return new Promise((resolve, reject) => {
let handle = err => off() && err ? reject(err) : resolve(this);
let off = () => this.off('error', handle).off('listening', handle);
super.listen(port, handle).once('error', handle);
super.listen(port, host, handle).once('error', handle);

Check failure on line 139 in packages/core/src/server.js

View workflow job for this annotation

GitHub Actions / Lint

'host' is not defined
});
}

Expand Down
38 changes: 38 additions & 0 deletions packages/core/test/unit/server.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { fs, mockfs } from '../helpers/index.js';
import Server from '../../src/server.js';
import { beforeEach } from 'node:test';

describe('Unit / Server', () => {
let server;
Expand All @@ -20,6 +21,43 @@ describe('Unit / Server', () => {
await new Promise(r => setImmediate(setImmediate, r));
});

describe('#host', () => {
beforeEach(async () => {
await server.listen(9000)

Check failure on line 26 in packages/core/test/unit/server.test.js

View workflow job for this annotation

GitHub Actions / Lint

Missing semicolon
server.route('get', '/test/:path', (req, res) => res.text(req.params.path));
});

it('returns the host', async () => {
expect(server.host).toEqual('0.0.0.0');

// test connection via 0.0.0.0
await expectAsync(request('/test/foo', 'GET')).toBeResolvedTo('foo');

Check failure on line 34 in packages/core/test/unit/server.test.js

View workflow job for this annotation

GitHub Actions / Lint

'request' was used before it was defined

// test connection via localhost
let { request } = await import('../helpers/request.js');
await expectAsync(request(new URL(path, 'localhost'), 'GET')).toBeResolvedTo('foo');

Check failure on line 38 in packages/core/test/unit/server.test.js

View workflow job for this annotation

GitHub Actions / Lint

'path' is not defined
});

describe('with PERCY_SERVER_HOST set', () => {
beforeEach(() => {
process.env.PERCY_SERVER_HOST = 'localhost';
});

afterEach(() => {
delete process.env.PERCY_SERVER_HOST;
});

it('uses correct host', async () => {
// test connection via localhost
await expectAsync(request('/test/foo', 'GET')).toBeResolvedTo('foo');

Check failure on line 52 in packages/core/test/unit/server.test.js

View workflow job for this annotation

GitHub Actions / Lint

'request' was used before it was defined

// test connection via 0.0.0.0
let { request } = await import('../helpers/request.js');
await expectAsync(request(new URL(path, '0.0.0.0'), 'GET')).toBeResolvedTo('foo');

Check failure on line 56 in packages/core/test/unit/server.test.js

View workflow job for this annotation

GitHub Actions / Lint

'path' is not defined
});
});
});

describe('#port', () => {
it('returns the provided default port when not listening', () => {
expect(server.port).toEqual(8000);
Expand Down

0 comments on commit e497347

Please sign in to comment.