Skip to content

Commit

Permalink
Added custom binding support
Browse files Browse the repository at this point in the history
  • Loading branch information
ninadbstack committed Nov 28, 2023
1 parent f5924b0 commit 8c65a65
Show file tree
Hide file tree
Showing 2 changed files with 44 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';
}

// 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://${this.host}`;
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, this.host, handle).once('error', handle);
});
}

Expand Down
37 changes: 37 additions & 0 deletions packages/core/test/unit/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,43 @@ describe('Unit / Server', () => {
await new Promise(r => setImmediate(setImmediate, r));
});

describe('#host', () => {
beforeEach(async () => {
await server.listen(9000);
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 33 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 37 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 51 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')).toBeRejected();

Check failure on line 55 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 8c65a65

Please sign in to comment.