Skip to content

Commit

Permalink
Add disableHttpServer option
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Jun 6, 2022
1 parent ce4b247 commit 5e4210e
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
8 changes: 8 additions & 0 deletions base-server/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ export interface BaseServerOptions {
* By default it cleans `Bearer [^\s"]+`.
*/
cleanFromLog?: RegExp

/**
* Disable health check endpoint, control HTTP API, {@link Server#http}.
*
* The server will process only WebSocket connection and ignore all other
* HTTP request (so they can be processed by other HTTP server).
*/
disableHttpServer?: boolean
}

export interface AuthenticatorOptions<Headers extends object> {
Expand Down
5 changes: 5 additions & 0 deletions base-server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,11 @@ export class BaseServer {
}

http(listener) {
if (this.options.disableHttpServer) {
throw new Error(
'`server.http()` can not be called when `disableHttpServer` enabled'
)
}
this.httpListener = listener
}

Expand Down
11 changes: 11 additions & 0 deletions bind-control-server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ function isValidBody(body) {
}

export function bindControlServer(app, custom) {
if (app.options.disableHttpServer) {
if (app.options.controlSecret) {
let err = new Error(
'`controlSecret` can be set together with `disableHttpServer` option'
)
err.logux = true
throw err
}
return
}

let masks = app.options.controlMask.split(/,\s*/).map(i => ip.cidrSubnet(i))
app.httpServer.on('request', async (req, res) => {
let urlString = req.url
Expand Down
53 changes: 49 additions & 4 deletions bind-control-server/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ afterEach(async () => {
await app.destroy()
})

function createReporter(opts = {}): {
function createReporter(opts: Partial<BaseServerOptions> = {}): {
names: string[]
reports: [string, object][]
app: BaseServer
Expand Down Expand Up @@ -104,15 +104,36 @@ it('has hello page', async () => {
expect(response.body).toContain('<svg ')
})

it('disables HTTP on request', async () => {
app = createServer({ disableHttpServer: true })
await app.listen()
let response = false
let req = http.request(
{
method: 'GET',
host: '127.0.0.1',
port: app.options.port,
path: '/health'
},
() => {
response = true
}
)
req.on('error', () => {})
await delay(100)
expect(response).toBe(false)
req.destroy()
})

it('has health check', async () => {
app = createServer({ controlSecret: 'secret', backend: 'http://localhost/' })
app = createServer()
await app.listen()
let response = await request('GET', '/health')
expect(response.body).toContain('OK')
})

it('has health check without control server', async () => {
app = createServer()
it('has health check with control server', async () => {
app = createServer({ controlSecret: 'secret', backend: 'http://localhost/' })
await app.listen()
let response = await request('GET', '/health')
expect(response.body).toContain('OK')
Expand Down Expand Up @@ -275,3 +296,27 @@ it('allows to set custom HTTP processor', async () => {
let response = await request('GET', '/test')
expect(response.body).toEqual('test /test')
})

it('does not allow to set custom HTTP processor on disabled HTTP', async () => {
app = createServer({ disableHttpServer: true })
expect(() => {
app.http((req, res) => {
res.statusCode = 200
res.end(`test ${req.url}`)
})
}).toThrow(/can not be called when `disableHttpServer` enabled/)
})

it('does not allow to have control secret on disabled HTTP', async () => {
app = createServer({ disableHttpServer: true, controlSecret: 'x' })
let err: Error | undefined
try {
await app.listen()
} catch (e) {
if (e instanceof Error) err = e
}
expect(err?.message).toBe(
'`controlSecret` can be set together with `disableHttpServer` option'
)
})

0 comments on commit 5e4210e

Please sign in to comment.