Skip to content

Commit

Permalink
refactor(@whook/http-server): group HTTP server options and add new ones
Browse files Browse the repository at this point in the history
fix #165
  • Loading branch information
nfroidure committed Feb 20, 2024
1 parent 66afe54 commit 51958f9
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 152 deletions.
60 changes: 12 additions & 48 deletions packages/whook-example/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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".",
],
Expand Down Expand Up @@ -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".",
],
Expand All @@ -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".",
],
Expand Down Expand Up @@ -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".",
],
Expand Down
33 changes: 3 additions & 30 deletions packages/whook-graphiql/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,6 @@ describe('wrapHTTPRouterWithGraphIQL', () => {
}).toMatchInlineSnapshot(`
{
"autoloaderCalls": [
[
"KEEP_ALIVE_TIMEOUT",
],
[
"SOCKET_TIMEOUT",
],
[
"MAX_CONNECTIONS",
],
[
"BUFFER_LIMIT",
],
Expand Down Expand Up @@ -220,7 +211,7 @@ describe('wrapHTTPRouterWithGraphIQL', () => {
"SIGNALS",
],
[
"MAX_HEADERS_COUNT",
"HTTP_SERVER_OPTIONS",
],
],
"debugCalls": [
Expand Down Expand Up @@ -346,15 +337,6 @@ describe('wrapHTTPRouterWithGraphIQL', () => {
}).toMatchInlineSnapshot(`
{
"autoloaderCalls": [
[
"KEEP_ALIVE_TIMEOUT",
],
[
"SOCKET_TIMEOUT",
],
[
"MAX_CONNECTIONS",
],
[
"BUFFER_LIMIT",
],
Expand Down Expand Up @@ -413,7 +395,7 @@ describe('wrapHTTPRouterWithGraphIQL', () => {
"SIGNALS",
],
[
"MAX_HEADERS_COUNT",
"HTTP_SERVER_OPTIONS",
],
],
"data": "
Expand Down Expand Up @@ -623,15 +605,6 @@ describe('wrapHTTPRouterWithGraphIQL', () => {
}).toMatchInlineSnapshot(`
{
"autoloaderCalls": [
[
"KEEP_ALIVE_TIMEOUT",
],
[
"SOCKET_TIMEOUT",
],
[
"MAX_CONNECTIONS",
],
[
"BUFFER_LIMIT",
],
Expand Down Expand Up @@ -690,7 +663,7 @@ describe('wrapHTTPRouterWithGraphIQL', () => {
"SIGNALS",
],
[
"MAX_HEADERS_COUNT",
"HTTP_SERVER_OPTIONS",
],
],
"debugCalls": [
Expand Down
5 changes: 1 addition & 4 deletions packages/whook-http-server/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,9 @@ Initialize an HTTP server
| services | <code>Object</code> | | The services the server depends on |
| [services.ENV] | <code>Object</code> | | The process environment variables |
| services.ENV.DESTROY_SOCKETS | <code>String</code> | | Whether the server sockets whould be destroyed or if the server should wait while sockets are kept alive |
| [services.HTTP_SERVER_OPTIONS] | <code>Object</code> | | See https://nodejs.org/docs/latest/api/http.html#class-httpserver |
| services.HOST | <code>String</code> | | The server host |
| services.PORT | <code>Number</code> | | The server port |
| [services.MAX_HEADERS_COUNT] | <code>Number</code> | | The https://nodejs.org/api/http.html#http_server_maxheaderscount |
| [services.KEEP_ALIVE_TIMEOUT] | <code>Number</code> | | See https://nodejs.org/api/http.html#http_server_keepalivetimeout |
| [services.MAX_CONNECTIONS] | <code>Number</code> | | See https://nodejs.org/api/net.html#net_server_maxconnections |
| [services.SOCKET_TIMEOUT] | <code>Number</code> | | See https://nodejs.org/api/http.html#http_server_timeout |
| services.httpRouter | <code>function</code> | | The function to run with the req/res tuple |
| [services.log] | <code>function</code> | <code>noop</code> | A logging function |

Expand Down
5 changes: 1 addition & 4 deletions packages/whook-http-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,9 @@ Initialize an HTTP server
| services | <code>Object</code> | | The services the server depends on |
| [services.ENV] | <code>Object</code> | | The process environment variables |
| services.ENV.DESTROY_SOCKETS | <code>String</code> | | Whether the server sockets whould be destroyed or if the server should wait while sockets are kept alive |
| [services.HTTP_SERVER_OPTIONS] | <code>Object</code> | | See https://nodejs.org/docs/latest/api/http.html#class-httpserver |
| services.HOST | <code>String</code> | | The server host |
| services.PORT | <code>Number</code> | | The server port |
| [services.MAX_HEADERS_COUNT] | <code>Number</code> | | The https://nodejs.org/api/http.html#http_server_maxheaderscount |
| [services.KEEP_ALIVE_TIMEOUT] | <code>Number</code> | | See https://nodejs.org/api/http.html#http_server_keepalivetimeout |
| [services.MAX_CONNECTIONS] | <code>Number</code> | | See https://nodejs.org/api/net.html#net_server_maxconnections |
| [services.SOCKET_TIMEOUT] | <code>Number</code> | | See https://nodejs.org/api/http.html#http_server_timeout |
| services.httpRouter | <code>function</code> | | The function to run with the req/res tuple |
| [services.log] | <code>function</code> | <code>noop</code> | A logging function |

Expand Down
8 changes: 6 additions & 2 deletions packages/whook-http-server/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ describe('initHTTPServer', () => {
ENV: {
DESTROY_SOCKETS: '1',
},
MAX_CONNECTIONS: 4,
HTTP_SERVER_OPTIONS: {
maxConnections: 4,
},
HOST,
PORT,
httpRouter,
Expand Down Expand Up @@ -84,7 +86,9 @@ describe('initHTTPServer', () => {
ENV: {
DESTROY_SOCKETS: '1',
},
MAX_CONNECTIONS: 4,
HTTP_SERVER_OPTIONS: {
maxConnections: 4,
},
HOST,
PORT,
httpRouter,
Expand Down
60 changes: 36 additions & 24 deletions packages/whook-http-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<WhookHTTPServerOptions>;
};
export type WhookHTTPServerDependencies = WhookHTTPServerConfig & {
ENV?: WhookHTTPServerEnv;
Expand All @@ -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));

Expand All @@ -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]
Expand All @@ -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<WhookHTTPServerProvider> {
const FINAL_HTTP_SERVER_OPTIONS: WhookHTTPServerOptions = {
...DEFAULT_HTTP_SERVER_OPTIONS,
...HTTP_SERVER_OPTIONS,
};

const sockets: Set<Socket> = ENV.DESTROY_SOCKETS
? new Set()
: (undefined as unknown as Set<Socket>);
Expand All @@ -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) => {
Expand Down
Loading

0 comments on commit 51958f9

Please sign in to comment.