From 51958f9df7f0287c6334edbb82b8bd99ffd842c7 Mon Sep 17 00:00:00 2001 From: Nicolas Froidure Date: Tue, 20 Feb 2024 13:12:58 +0100 Subject: [PATCH] refactor(@whook/http-server): group HTTP server options and add new ones fix #165 --- packages/whook-example/src/index.test.ts | 60 ++++---------------- packages/whook-graphiql/src/index.test.ts | 33 +---------- packages/whook-http-server/API.md | 5 +- packages/whook-http-server/README.md | 5 +- packages/whook-http-server/src/index.test.ts | 8 ++- packages/whook-http-server/src/index.ts | 60 ++++++++++++-------- packages/whook-swagger-ui/src/index.test.ts | 44 ++------------ packages/whook/src/index.ts | 1 + 8 files changed, 64 insertions(+), 152 deletions(-) diff --git a/packages/whook-example/src/index.test.ts b/packages/whook-example/src/index.test.ts index d6e973a9..f66637a1 100644 --- a/packages/whook-example/src/index.test.ts +++ b/packages/whook-example/src/index.test.ts @@ -291,6 +291,18 @@ describe('runServer', () => { [ "🚫 - Service "ENCODERS" not found in "/home/whoiam/projects/whook/packages/whook-example/src/services/ENCODERS".", ], + [ + "🚫 - Service "HTTP_SERVER_OPTIONS" not found in "/home/whoiam/projects/whook/dist/services/HTTP_SERVER_OPTIONS".", + ], + [ + "🚫 - Service "HTTP_SERVER_OPTIONS" not found in "/home/whoiam/projects/whook/packages/whook-authorization/dist/services/HTTP_SERVER_OPTIONS".", + ], + [ + "🚫 - Service "HTTP_SERVER_OPTIONS" not found in "/home/whoiam/projects/whook/packages/whook-cors/dist/services/HTTP_SERVER_OPTIONS".", + ], + [ + "🚫 - Service "HTTP_SERVER_OPTIONS" not found in "/home/whoiam/projects/whook/packages/whook-example/src/services/HTTP_SERVER_OPTIONS".", + ], [ "🚫 - Service "IGNORED_FILES_PREFIXES" not found in "/home/whoiam/projects/whook/dist/services/IGNORED_FILES_PREFIXES".", ], @@ -327,18 +339,6 @@ describe('runServer', () => { [ "🚫 - Service "JWT_SECRET_ENV_NAME" not found in "/home/whoiam/projects/whook/packages/whook-example/src/services/JWT_SECRET_ENV_NAME".", ], - [ - "🚫 - Service "KEEP_ALIVE_TIMEOUT" not found in "/home/whoiam/projects/whook/dist/services/KEEP_ALIVE_TIMEOUT".", - ], - [ - "🚫 - Service "KEEP_ALIVE_TIMEOUT" not found in "/home/whoiam/projects/whook/packages/whook-authorization/dist/services/KEEP_ALIVE_TIMEOUT".", - ], - [ - "🚫 - Service "KEEP_ALIVE_TIMEOUT" not found in "/home/whoiam/projects/whook/packages/whook-cors/dist/services/KEEP_ALIVE_TIMEOUT".", - ], - [ - "🚫 - Service "KEEP_ALIVE_TIMEOUT" not found in "/home/whoiam/projects/whook/packages/whook-example/src/services/KEEP_ALIVE_TIMEOUT".", - ], [ "🚫 - Service "MAX_CLEAR_RATIO" not found in "/home/whoiam/projects/whook/dist/services/MAX_CLEAR_RATIO".", ], @@ -351,30 +351,6 @@ describe('runServer', () => { [ "🚫 - Service "MAX_CLEAR_RATIO" not found in "/home/whoiam/projects/whook/packages/whook-example/src/services/MAX_CLEAR_RATIO".", ], - [ - "🚫 - Service "MAX_CONNECTIONS" not found in "/home/whoiam/projects/whook/dist/services/MAX_CONNECTIONS".", - ], - [ - "🚫 - Service "MAX_CONNECTIONS" not found in "/home/whoiam/projects/whook/packages/whook-authorization/dist/services/MAX_CONNECTIONS".", - ], - [ - "🚫 - Service "MAX_CONNECTIONS" not found in "/home/whoiam/projects/whook/packages/whook-cors/dist/services/MAX_CONNECTIONS".", - ], - [ - "🚫 - Service "MAX_CONNECTIONS" not found in "/home/whoiam/projects/whook/packages/whook-example/src/services/MAX_CONNECTIONS".", - ], - [ - "🚫 - Service "MAX_HEADERS_COUNT" not found in "/home/whoiam/projects/whook/dist/services/MAX_HEADERS_COUNT".", - ], - [ - "🚫 - Service "MAX_HEADERS_COUNT" not found in "/home/whoiam/projects/whook/packages/whook-authorization/dist/services/MAX_HEADERS_COUNT".", - ], - [ - "🚫 - Service "MAX_HEADERS_COUNT" not found in "/home/whoiam/projects/whook/packages/whook-cors/dist/services/MAX_HEADERS_COUNT".", - ], - [ - "🚫 - Service "MAX_HEADERS_COUNT" not found in "/home/whoiam/projects/whook/packages/whook-example/src/services/MAX_HEADERS_COUNT".", - ], [ "🚫 - Service "PARSERS" not found in "/home/whoiam/projects/whook/dist/services/PARSERS".", ], @@ -435,18 +411,6 @@ describe('runServer', () => { [ "🚫 - Service "SIGNALS" not found in "/home/whoiam/projects/whook/packages/whook-example/src/services/SIGNALS".", ], - [ - "🚫 - Service "SOCKET_TIMEOUT" not found in "/home/whoiam/projects/whook/dist/services/SOCKET_TIMEOUT".", - ], - [ - "🚫 - Service "SOCKET_TIMEOUT" not found in "/home/whoiam/projects/whook/packages/whook-authorization/dist/services/SOCKET_TIMEOUT".", - ], - [ - "🚫 - Service "SOCKET_TIMEOUT" not found in "/home/whoiam/projects/whook/packages/whook-cors/dist/services/SOCKET_TIMEOUT".", - ], - [ - "🚫 - Service "SOCKET_TIMEOUT" not found in "/home/whoiam/projects/whook/packages/whook-example/src/services/SOCKET_TIMEOUT".", - ], [ "🚫 - Service "STRINGIFYERS" not found in "/home/whoiam/projects/whook/dist/services/STRINGIFYERS".", ], diff --git a/packages/whook-graphiql/src/index.test.ts b/packages/whook-graphiql/src/index.test.ts index b6b9801b..70f44063 100644 --- a/packages/whook-graphiql/src/index.test.ts +++ b/packages/whook-graphiql/src/index.test.ts @@ -153,15 +153,6 @@ describe('wrapHTTPRouterWithGraphIQL', () => { }).toMatchInlineSnapshot(` { "autoloaderCalls": [ - [ - "KEEP_ALIVE_TIMEOUT", - ], - [ - "SOCKET_TIMEOUT", - ], - [ - "MAX_CONNECTIONS", - ], [ "BUFFER_LIMIT", ], @@ -220,7 +211,7 @@ describe('wrapHTTPRouterWithGraphIQL', () => { "SIGNALS", ], [ - "MAX_HEADERS_COUNT", + "HTTP_SERVER_OPTIONS", ], ], "debugCalls": [ @@ -346,15 +337,6 @@ describe('wrapHTTPRouterWithGraphIQL', () => { }).toMatchInlineSnapshot(` { "autoloaderCalls": [ - [ - "KEEP_ALIVE_TIMEOUT", - ], - [ - "SOCKET_TIMEOUT", - ], - [ - "MAX_CONNECTIONS", - ], [ "BUFFER_LIMIT", ], @@ -413,7 +395,7 @@ describe('wrapHTTPRouterWithGraphIQL', () => { "SIGNALS", ], [ - "MAX_HEADERS_COUNT", + "HTTP_SERVER_OPTIONS", ], ], "data": " @@ -623,15 +605,6 @@ describe('wrapHTTPRouterWithGraphIQL', () => { }).toMatchInlineSnapshot(` { "autoloaderCalls": [ - [ - "KEEP_ALIVE_TIMEOUT", - ], - [ - "SOCKET_TIMEOUT", - ], - [ - "MAX_CONNECTIONS", - ], [ "BUFFER_LIMIT", ], @@ -690,7 +663,7 @@ describe('wrapHTTPRouterWithGraphIQL', () => { "SIGNALS", ], [ - "MAX_HEADERS_COUNT", + "HTTP_SERVER_OPTIONS", ], ], "debugCalls": [ diff --git a/packages/whook-http-server/API.md b/packages/whook-http-server/API.md index 1890794e..47ca693b 100644 --- a/packages/whook-http-server/API.md +++ b/packages/whook-http-server/API.md @@ -28,12 +28,9 @@ Initialize an HTTP server | services | Object | | The services the server depends on | | [services.ENV] | Object | | The process environment variables | | services.ENV.DESTROY_SOCKETS | String | | Whether the server sockets whould be destroyed or if the server should wait while sockets are kept alive | +| [services.HTTP_SERVER_OPTIONS] | Object | | See https://nodejs.org/docs/latest/api/http.html#class-httpserver | | services.HOST | String | | The server host | | services.PORT | Number | | The server port | -| [services.MAX_HEADERS_COUNT] | Number | | The https://nodejs.org/api/http.html#http_server_maxheaderscount | -| [services.KEEP_ALIVE_TIMEOUT] | Number | | See https://nodejs.org/api/http.html#http_server_keepalivetimeout | -| [services.MAX_CONNECTIONS] | Number | | See https://nodejs.org/api/net.html#net_server_maxconnections | -| [services.SOCKET_TIMEOUT] | Number | | See https://nodejs.org/api/http.html#http_server_timeout | | services.httpRouter | function | | The function to run with the req/res tuple | | [services.log] | function | noop | A logging function | diff --git a/packages/whook-http-server/README.md b/packages/whook-http-server/README.md index c91845be..346594bd 100644 --- a/packages/whook-http-server/README.md +++ b/packages/whook-http-server/README.md @@ -57,12 +57,9 @@ Initialize an HTTP server | services | Object | | The services the server depends on | | [services.ENV] | Object | | The process environment variables | | services.ENV.DESTROY_SOCKETS | String | | Whether the server sockets whould be destroyed or if the server should wait while sockets are kept alive | +| [services.HTTP_SERVER_OPTIONS] | Object | | See https://nodejs.org/docs/latest/api/http.html#class-httpserver | | services.HOST | String | | The server host | | services.PORT | Number | | The server port | -| [services.MAX_HEADERS_COUNT] | Number | | The https://nodejs.org/api/http.html#http_server_maxheaderscount | -| [services.KEEP_ALIVE_TIMEOUT] | Number | | See https://nodejs.org/api/http.html#http_server_keepalivetimeout | -| [services.MAX_CONNECTIONS] | Number | | See https://nodejs.org/api/net.html#net_server_maxconnections | -| [services.SOCKET_TIMEOUT] | Number | | See https://nodejs.org/api/http.html#http_server_timeout | | services.httpRouter | function | | The function to run with the req/res tuple | | [services.log] | function | noop | A logging function | diff --git a/packages/whook-http-server/src/index.test.ts b/packages/whook-http-server/src/index.test.ts index c876bbaf..09713054 100644 --- a/packages/whook-http-server/src/index.test.ts +++ b/packages/whook-http-server/src/index.test.ts @@ -56,7 +56,9 @@ describe('initHTTPServer', () => { ENV: { DESTROY_SOCKETS: '1', }, - MAX_CONNECTIONS: 4, + HTTP_SERVER_OPTIONS: { + maxConnections: 4, + }, HOST, PORT, httpRouter, @@ -84,7 +86,9 @@ describe('initHTTPServer', () => { ENV: { DESTROY_SOCKETS: '1', }, - MAX_CONNECTIONS: 4, + HTTP_SERVER_OPTIONS: { + maxConnections: 4, + }, HOST, PORT, httpRouter, diff --git a/packages/whook-http-server/src/index.ts b/packages/whook-http-server/src/index.ts index fc424ea0..0f280d65 100644 --- a/packages/whook-http-server/src/index.ts +++ b/packages/whook-http-server/src/index.ts @@ -10,13 +10,20 @@ import type { Socket } from 'net'; export type WhookHTTPServerEnv = { DESTROY_SOCKETS?: string; }; +export type WhookHTTPServerOptions = Pick< + http.Server, + | 'timeout' + | 'headersTimeout' + | 'requestTimeout' + | 'keepAliveTimeout' + | 'maxConnections' + | 'maxHeadersCount' + | 'maxRequestsPerSocket' +>; export type WhookHTTPServerConfig = { HOST?: string; PORT?: number; - MAX_HEADERS_COUNT?: number; - KEEP_ALIVE_TIMEOUT?: number; - SOCKET_TIMEOUT?: number; - MAX_CONNECTIONS?: number; + HTTP_SERVER_OPTIONS?: Partial; }; export type WhookHTTPServerDependencies = WhookHTTPServerConfig & { ENV?: WhookHTTPServerEnv; @@ -33,6 +40,15 @@ function noop() { } const DEFAULT_ENV = {}; +const DEFAULT_HTTP_SERVER_OPTIONS: WhookHTTPServerOptions = { + maxHeadersCount: 800, + requestTimeout: ms('5m'), + headersTimeout: ms('1m'), + maxRequestsPerSocket: 0, + timeout: ms('2m'), + keepAliveTimeout: ms('5m'), + maxConnections: 0, +}; export default name('httpServer', autoProvider(initHTTPServer)); @@ -47,18 +63,12 @@ export default name('httpServer', autoProvider(initHTTPServer)); * @param {String} services.ENV.DESTROY_SOCKETS * Whether the server sockets whould be destroyed or if the * server should wait while sockets are kept alive + * @param {Object} [services.HTTP_SERVER_OPTIONS] + * See https://nodejs.org/docs/latest/api/http.html#class-httpserver * @param {String} services.HOST * The server host * @param {Number} services.PORT * The server port - * @param {Number} [services.MAX_HEADERS_COUNT] - * The https://nodejs.org/api/http.html#http_server_maxheaderscount - * @param {Number} [services.KEEP_ALIVE_TIMEOUT] - * See https://nodejs.org/api/http.html#http_server_keepalivetimeout - * @param {Number} [services.MAX_CONNECTIONS] - * See https://nodejs.org/api/net.html#net_server_maxconnections - * @param {Number} [services.SOCKET_TIMEOUT] - * See https://nodejs.org/api/http.html#http_server_timeout * @param {Function} services.httpRouter * The function to run with the req/res tuple * @param {Function} [services.log=noop] @@ -69,15 +79,17 @@ export default name('httpServer', autoProvider(initHTTPServer)); */ async function initHTTPServer({ ENV = DEFAULT_ENV, + HTTP_SERVER_OPTIONS = DEFAULT_HTTP_SERVER_OPTIONS, HOST, PORT, - MAX_HEADERS_COUNT = 800, - KEEP_ALIVE_TIMEOUT = ms('5m'), - SOCKET_TIMEOUT = ms('2m'), - MAX_CONNECTIONS = Infinity, httpRouter, log = noop, }: WhookHTTPServerDependencies): Promise { + const FINAL_HTTP_SERVER_OPTIONS: WhookHTTPServerOptions = { + ...DEFAULT_HTTP_SERVER_OPTIONS, + ...HTTP_SERVER_OPTIONS, + }; + const sockets: Set = ENV.DESTROY_SOCKETS ? new Set() : (undefined as unknown as Set); @@ -97,14 +109,14 @@ async function initHTTPServer({ ); }); - httpServer.timeout = SOCKET_TIMEOUT; - httpServer.keepAliveTimeout = KEEP_ALIVE_TIMEOUT; - httpServer.maxHeadersCount = MAX_HEADERS_COUNT; - httpServer.maxConnections = MAX_CONNECTIONS; - - if ('undefined' !== typeof MAX_CONNECTIONS) { - httpServer.maxConnections = MAX_CONNECTIONS; - } + httpServer.maxHeadersCount = FINAL_HTTP_SERVER_OPTIONS.maxHeadersCount; + httpServer.requestTimeout = FINAL_HTTP_SERVER_OPTIONS.requestTimeout; + httpServer.headersTimeout = FINAL_HTTP_SERVER_OPTIONS.headersTimeout; + httpServer.maxRequestsPerSocket = + FINAL_HTTP_SERVER_OPTIONS.maxRequestsPerSocket; + httpServer.timeout = FINAL_HTTP_SERVER_OPTIONS.timeout; + httpServer.keepAliveTimeout = FINAL_HTTP_SERVER_OPTIONS.keepAliveTimeout; + httpServer.maxConnections = FINAL_HTTP_SERVER_OPTIONS.maxConnections; if (ENV.DESTROY_SOCKETS) { httpServer.on('connection', (socket) => { diff --git a/packages/whook-swagger-ui/src/index.test.ts b/packages/whook-swagger-ui/src/index.test.ts index c6b9b186..72ca9b40 100644 --- a/packages/whook-swagger-ui/src/index.test.ts +++ b/packages/whook-swagger-ui/src/index.test.ts @@ -127,15 +127,6 @@ describe('wrapHTTPRouterWithSwaggerUI', () => { }).toMatchInlineSnapshot(` { "autoloaderCalls": [ - [ - "KEEP_ALIVE_TIMEOUT", - ], - [ - "SOCKET_TIMEOUT", - ], - [ - "MAX_CONNECTIONS", - ], [ "BUFFER_LIMIT", ], @@ -194,7 +185,7 @@ describe('wrapHTTPRouterWithSwaggerUI', () => { "SIGNALS", ], [ - "MAX_HEADERS_COUNT", + "HTTP_SERVER_OPTIONS", ], ], "debugCalls": [ @@ -324,15 +315,6 @@ describe('wrapHTTPRouterWithSwaggerUI', () => { }).toMatchInlineSnapshot(` { "autoloaderCalls": [ - [ - "KEEP_ALIVE_TIMEOUT", - ], - [ - "SOCKET_TIMEOUT", - ], - [ - "MAX_CONNECTIONS", - ], [ "BUFFER_LIMIT", ], @@ -391,7 +373,7 @@ describe('wrapHTTPRouterWithSwaggerUI', () => { "SIGNALS", ], [ - "MAX_HEADERS_COUNT", + "HTTP_SERVER_OPTIONS", ], ], "debugCalls": [ @@ -546,15 +528,6 @@ window.onload = function() { }).toMatchInlineSnapshot(` { "autoloaderCalls": [ - [ - "KEEP_ALIVE_TIMEOUT", - ], - [ - "SOCKET_TIMEOUT", - ], - [ - "MAX_CONNECTIONS", - ], [ "BUFFER_LIMIT", ], @@ -610,7 +583,7 @@ window.onload = function() { "SIGNALS", ], [ - "MAX_HEADERS_COUNT", + "HTTP_SERVER_OPTIONS", ], ], "debugCalls": [ @@ -708,15 +681,6 @@ window.onload = function() { }).toMatchInlineSnapshot(` { "autoloaderCalls": [ - [ - "KEEP_ALIVE_TIMEOUT", - ], - [ - "SOCKET_TIMEOUT", - ], - [ - "MAX_CONNECTIONS", - ], [ "BUFFER_LIMIT", ], @@ -775,7 +739,7 @@ window.onload = function() { "SIGNALS", ], [ - "MAX_HEADERS_COUNT", + "HTTP_SERVER_OPTIONS", ], ], "debugCalls": [ diff --git a/packages/whook/src/index.ts b/packages/whook/src/index.ts index b2586e32..e9e2aebc 100644 --- a/packages/whook/src/index.ts +++ b/packages/whook/src/index.ts @@ -75,6 +75,7 @@ export type { export type { WhookPort, WhookPortEnv } from './services/PORT.js'; export type { WhookHTTPServerEnv, + WhookHTTPServerOptions, WhookHTTPServerConfig, WhookHTTPServerDependencies, WhookHTTPServerService,