Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/server/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import { finalHandler } from './factories/final_handler.js'
import { writeResponse } from './factories/write_response.js'
import { asyncLocalStorage } from '../http_context/local_storage.js'
import { middlewareHandler } from './factories/middleware_handler.js'
import lodash from '@poppinss/utils/lodash'
import { NodeConfig } from '../types/node.js'

/**
* The HTTP server implementation to handle incoming requests and respond using the
Expand Down Expand Up @@ -294,6 +296,18 @@ export class Server {
* Set the HTTP server instance used to listen for requests.
*/
setNodeServer(server: HttpServer | HttpsServer) {
const nodeServerOptions: (keyof NodeConfig)[] = [
'keepAliveTimeout',
'headersTimeout',
'requestTimeout',
'timeout',
]

Object.assign<HttpServer | HttpsServer, Partial<NodeConfig>>(
server,
lodash.pick<NodeConfig>(this.#config, nodeServerOptions)
)

this.#nodeHttpServer = server
}

Expand Down
26 changes: 26 additions & 0 deletions src/types/node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export type NodeConfig = {
/**
* The number of milliseconds of inactivity a server needs to wait for additional incoming data, after
* it has finished writing the last response, before a socket will be destroyed
* @default 5000
*/
keepAliveTimeout?: number

/**
* Limit the amount of time the parser will wait to receive the complete HTTP headers
* @default 60000
*/
headersTimeout?: number

/**
* Sets the timeout value in milliseconds for receiving the entire request from the client
* @default 300000
*/
requestTimeout?: number

/**
* The number of milliseconds of inactivity before a socket is presumed to have timed out
* @default 0
*/
timeout?: number
}
4 changes: 3 additions & 1 deletion src/types/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type { QSParserConfig } from './qs.js'
import type { RequestConfig } from './request.js'
import type { ResponseConfig } from './response.js'
import type { HttpContext } from '../http_context/main.js'
import type { NodeConfig } from './node.js'

/**
* Normalized HTTP error used by the exception
Expand Down Expand Up @@ -83,7 +84,8 @@ export type ErrorHandlerAsAClass = Constructor<ServerErrorHandler>
* Config accepted by the HTTP server
*/
export type ServerConfig = RequestConfig &
ResponseConfig & {
ResponseConfig &
NodeConfig & {
/**
* Whether or not to create an async local storage store for
* the HTTP context.
Expand Down
19 changes: 19 additions & 0 deletions tests/server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,25 @@ test.group('Server', () => {

await supertest(httpServer).get('/').expect(200)
}).waitForDone()

test('pass config to node http server', ({ assert }) => {
const keepAliveTimeout = 61000
const app = new AppFactory().create(BASE_URL, () => {})
const server = new ServerFactory()
.merge({
app,
config: {
keepAliveTimeout,
},
})
.create()
server.use([])

const httpServer = createServer(() => {})
server.setNodeServer(httpServer)

assert.strictEqual(httpServer.keepAliveTimeout, keepAliveTimeout)
})
})

test.group('Server | Response handling', () => {
Expand Down
Loading