Skip to content

Commit

Permalink
Allow specifying hostname for Hapi servers.
Browse files Browse the repository at this point in the history
And specify it as "localhost" for the servers that are started by the integration tests.

Some of the integration tests were failing for me because:
- My laptop is configured with both IPv4 and IPv6 for localhost (and non-localhost, too, but that's not relevant here).
- Hapi binds to 0.0.0.0 by default ([API docs](https://hapi.dev/api/?v=20.2.2#-serveroptionsaddress), [code reference](https://github.com/hapijs/hapi/blob/b8ba0adc7c3255995cb56a9a740c4f9750b80e6b/lib/core.js#L339)).
- That's an IPv4 address so the server will only accept connections over IPv4 and not IPv6.
- Our integration test config specifies `localhost` when creating the clients for connecting to these servers.
- `localhost` can resolve to an IPv6 address.
- Node before v17 sorted IP addresses from the name resolver so IPv4 addresses were first but Node v17 and newer returns them in the order returned by the resolver ([GitHub comment](nodejs/node#40537 (comment)), [relevant PR](nodejs/node#39987), it's the first "Other Notable Changes" in [the v17.0.0 release notes](https://nodejs.org/en/blog/release/v17.0.0/#other-notable-changes)). Apparently on my laptop an IPv6 address is returned before an IPv4 address.

Alternatively I could have changed the test config so clients connect to 127.0.0.1. I like this change because:
- It's a good idea to allow users to specify the address to bind to.
- Now the test servers will bind to localhost instead of 0.0.0.0 and it's nice to avoid accepting outside connections when possible.
  • Loading branch information
markdoliner-doma committed Jul 6, 2022
1 parent b4a4751 commit 0051bd7
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ export async function createServer(
* Creates Hapi server with thrift endpoint.
*/
const server: Hapi.Server = await createThriftServer({
address: HAPI_CALC_SERVER_STRICT_CONFIG.hostName,
port: HAPI_CALC_SERVER_STRICT_CONFIG.port,
path: HAPI_CALC_SERVER_STRICT_CONFIG.path,
thriftOptions: {
Expand Down
1 change: 1 addition & 0 deletions packages/thrift-integration/src/hapi-add-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export async function createServer(sampleRate = 0): Promise<Hapi.Server> {
* Creates Hapi server with thrift endpoint.
*/
const server: Hapi.Server = await createThriftServer({
address: ADD_SERVER_CONFIG.hostName,
port: ADD_SERVER_CONFIG.port,
path: ADD_SERVER_CONFIG.path,
thriftOptions: {
Expand Down
1 change: 1 addition & 0 deletions packages/thrift-integration/src/hapi-calculator-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ export async function createServer(
* Creates Hapi server with thrift endpoint.
*/
const server: Hapi.Server = await createThriftServer({
address: HAPI_CALC_SERVER_CONFIG.hostName,
port: HAPI_CALC_SERVER_CONFIG.port,
path: HAPI_CALC_SERVER_CONFIG.path,
thriftOptions: {
Expand Down
1 change: 1 addition & 0 deletions packages/thrift-server-hapi/src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export function createThriftServer<TProcessor extends IThriftProcessor<Hapi.Requ
options: ICreateHapiServerOptions<TProcessor>
): Promise<Hapi.Server> {
const server = new Hapi.Server({
address: options.address,
port: options.port,
debug: { request: ['error'] },
})
Expand Down
3 changes: 2 additions & 1 deletion packages/thrift-server-hapi/src/main/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ export interface IHapiPluginOptions<TProcessor extends IThriftProcessor<Hapi.Req

export interface ICreateHapiServerOptions<TProcessor extends IThriftProcessor<Hapi.Request>>
extends IHapiPluginOptions<TProcessor> {
port: number
address?: Hapi.ServerOptions['address']
port?: Hapi.ServerOptions['port']
}

export type ThriftHapiPlugin = Hapi.Plugin<void>

0 comments on commit 0051bd7

Please sign in to comment.