From 0887d850000b995518108eca01fe817987567097 Mon Sep 17 00:00:00 2001 From: Luca Rainone Date: Fri, 27 Sep 2024 20:36:19 +0200 Subject: [PATCH 1/2] feat: pass forceCloseConnections option from cli --- README.md | 45 +++++++++++++++++++------------------- args.js | 4 +++- start.js | 4 ++++ test/args.test.js | 15 ++++++++++++- test/data/custom-config.js | 1 + 5 files changed, 45 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 21657bee..5305868d 100644 --- a/README.md +++ b/README.md @@ -149,28 +149,29 @@ If your `package.json` does not have `"type": "module"`, use `.mjs` for the exte #### Options You can pass the following options via CLI arguments. You can also use `--config` or `-c` flag to pass a configuration file that exports all the properties listed below in camelCase convention. In case of collision (i.e., An argument existing in both the configuration file and as a command-line argument, the command-line argument is given the priority). Every option has a corresponding environment variable: -| Description | Short command | Full command | Environment variable | -| --------------------------------------------------------------------------------------------------------------------------------------- | ------------- | ------------------ | ------------------------ | -| Path to configuration file that can be used to manage the options listed below | `-c` | `--config` | `FASTIFY_CONFIG or CONFIG` | -| Port to listen on (default to 3000) | `-p` | `--port` | `FASTIFY_PORT or PORT` | -| Address to listen on | `-a` | `--address` | `FASTIFY_ADDRESS` | -| Socket to listen on | `-s` | `--socket` | `FASTIFY_SOCKET` | -| Module to preload | `-r` | `--require` | `FASTIFY_REQUIRE` | -| ES Module to preload | `-i` | `--import` | `FASTIFY_IMPORT` | -| Log level (default to fatal) | `-l` | `--log-level` | `FASTIFY_LOG_LEVEL` | -| Path to logging configuration module to use | `-L` | `--logging-module` | `FASTIFY_LOGGING_MODULE` | -| Start Fastify app in debug mode with nodejs inspector | `-d` | `--debug` | `FASTIFY_DEBUG` | -| Set the inspector port (default: 9320) | `-I` | `--debug-port` | `FASTIFY_DEBUG_PORT` | -| Set the inspector host to listen on (default: loopback address or `0.0.0.0` inside Docker or Kubernetes) | | `--debug-host` | `FASTIFY_DEBUG_HOST` | -| Prints pretty logs | `-P` | `--pretty-logs` | `FASTIFY_PRETTY_LOGS` | -| Watch process.cwd() directory for changes, recursively; when that happens, the process will auto reload | `-w` | `--watch` | `FASTIFY_WATCH` | -| Ignore changes to the specified files or directories when watch is enabled. (e.g. `--ignore-watch='node_modules .git logs/error.log'` ) | | `--ignore-watch` | `FASTIFY_IGNORE_WATCH` | -| Prints events triggered by watch listener (useful to debug unexpected reload when using `--watch` ) | `-V` | `--verbose-watch` | `FASTIFY_VERBOSE_WATCH` | -| Use custom options | `-o` | `--options` | `FASTIFY_OPTIONS` | -| Set the prefix | `-x` | `--prefix` | `FASTIFY_PREFIX` | -| Set the plugin timeout | `-T` | `--plugin-timeout` | `FASTIFY_PLUGIN_TIMEOUT` | -| Defines the maximum payload, in bytes,
that the server is allowed to accept | | `--body-limit` | `FASTIFY_BODY_LIMIT` | -| Set the maximum ms delay before forcefully closing pending requests after receiving SIGTERM or SIGINT signals; and uncaughtException or unhandledRejection errors (default: 500) | `-g` | `--close-grace-delay` | `FASTIFY_CLOSE_GRACE_DELAY` | +| Description | Short command | Full command | Environment variable | +|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------|-----------------------------|-----------------------------------| +| Path to configuration file that can be used to manage the options listed below | `-c` | `--config` | `FASTIFY_CONFIG or CONFIG` | +| Port to listen on (default to 3000) | `-p` | `--port` | `FASTIFY_PORT or PORT` | +| Address to listen on | `-a` | `--address` | `FASTIFY_ADDRESS` | +| Socket to listen on | `-s` | `--socket` | `FASTIFY_SOCKET` | +| Module to preload | `-r` | `--require` | `FASTIFY_REQUIRE` | +| ES Module to preload | `-i` | `--import` | `FASTIFY_IMPORT` | +| Log level (default to fatal) | `-l` | `--log-level` | `FASTIFY_LOG_LEVEL` | +| Path to logging configuration module to use | `-L` | `--logging-module` | `FASTIFY_LOGGING_MODULE` | +| Start Fastify app in debug mode with nodejs inspector | `-d` | `--debug` | `FASTIFY_DEBUG` | +| Set the inspector port (default: 9320) | `-I` | `--debug-port` | `FASTIFY_DEBUG_PORT` | +| Set the inspector host to listen on (default: loopback address or `0.0.0.0` inside Docker or Kubernetes) | | `--debug-host` | `FASTIFY_DEBUG_HOST` | +| Prints pretty logs | `-P` | `--pretty-logs` | `FASTIFY_PRETTY_LOGS` | +| Watch process.cwd() directory for changes, recursively; when that happens, the process will auto reload | `-w` | `--watch` | `FASTIFY_WATCH` | +| Ignore changes to the specified files or directories when watch is enabled. (e.g. `--ignore-watch='node_modules .git logs/error.log'` ) | | `--ignore-watch` | `FASTIFY_IGNORE_WATCH` | +| Prints events triggered by watch listener (useful to debug unexpected reload when using `--watch` ) | `-V` | `--verbose-watch` | `FASTIFY_VERBOSE_WATCH` | +| Use custom options | `-o` | `--options` | `FASTIFY_OPTIONS` | +| Set the prefix | `-x` | `--prefix` | `FASTIFY_PREFIX` | +| Set the plugin timeout | `-T` | `--plugin-timeout` | `FASTIFY_PLUGIN_TIMEOUT` | +| Set forceCloseConnections option on fastify instance | `-f` | `--force-close-connections` | `FASTIFY_FORCE_CLOSE_CONNECTIONS` | +| Defines the maximum payload, in bytes,
that the server is allowed to accept | | `--body-limit` | `FASTIFY_BODY_LIMIT` | +| Set the maximum ms delay before forcefully closing pending requests after receiving SIGTERM or SIGINT signals; and uncaughtException or unhandledRejection errors (default: 500) | `-g` | `--close-grace-delay` | `FASTIFY_CLOSE_GRACE_DELAY` | By default `fastify-cli` runs [`dotenv`](https://www.npmjs.com/package/dotenv), so it will load all the env variables stored in `.env` in your current working directory. diff --git a/args.js b/args.js index 1f842bc2..05633152 100644 --- a/args.js +++ b/args.js @@ -29,7 +29,7 @@ module.exports = function parseArgs (args) { }, number: ['port', 'inspect-port', 'body-limit', 'plugin-timeout', 'close-grace-delay'], string: ['log-level', 'address', 'socket', 'prefix', 'ignore-watch', 'logging-module', 'debug-host', 'lang', 'require', 'import', 'config', 'method'], - boolean: ['pretty-logs', 'options', 'watch', 'verbose-watch', 'debug', 'standardlint', 'common-prefix', 'include-hooks'], + boolean: ['pretty-logs', 'options', 'watch', 'verbose-watch', 'debug', 'standardlint', 'common-prefix', 'include-hooks', 'force-close-connections'], envPrefix: 'FASTIFY_', alias: { port: ['p'], @@ -47,6 +47,7 @@ module.exports = function parseArgs (args) { 'log-level': ['l'], 'pretty-logs': ['P'], 'plugin-timeout': ['T'], + 'force-close-connections': ['f'], 'close-grace-delay': ['g'], 'logging-module': ['L'], 'verbose-watch': ['V'] @@ -73,6 +74,7 @@ module.exports = function parseArgs (args) { port: parsedArgs.port, bodyLimit: parsedArgs.bodyLimit, pluginTimeout: parsedArgs.pluginTimeout, + forceCloseConnections: parsedArgs.forceCloseConnections, closeGraceDelay: parsedArgs.closeGraceDelay, pluginOptions, prettyLogs: parsedArgs.prettyLogs, diff --git a/start.js b/start.js index e359369e..f0775e11 100755 --- a/start.js +++ b/start.js @@ -139,6 +139,10 @@ async function runFastify (args, additionalOptions, serverOptions) { options.bodyLimit = opts.bodyLimit } + if (opts.forceCloseConnections) { + options.forceCloseConnections = opts.forceCloseConnections + } + if (opts.prettyLogs) { options.logger.transport = { target: 'pino-pretty' diff --git a/test/args.test.js b/test/args.test.js index 5d024a83..c128e22a 100644 --- a/test/args.test.js +++ b/test/args.test.js @@ -21,6 +21,7 @@ test('should parse args correctly', t => { '--options', 'true', '--prefix', 'FASTIFY_', '--plugin-timeout', '500', + '--force-close-connections', 'true', '--close-grace-delay', '30000', '--body-limit', '5242880', '--debug', 'true', @@ -47,6 +48,7 @@ test('should parse args correctly', t => { logLevel: 'info', prefix: 'FASTIFY_', pluginTimeout: 500, + forceCloseConnections: true, closeGraceDelay: 30000, pluginOptions: {}, bodyLimit: 5242880, @@ -78,6 +80,7 @@ test('should parse args with = assignment correctly', t => { '--options=true', '--prefix=FASTIFY_', '--plugin-timeout=500', + '--force-close-connections=true', '--close-grace-delay=30000', '--body-limit=5242880', '--debug=true', @@ -104,6 +107,7 @@ test('should parse args with = assignment correctly', t => { logLevel: 'info', prefix: 'FASTIFY_', pluginTimeout: 500, + forceCloseConnections: true, closeGraceDelay: 30000, pluginOptions: {}, bodyLimit: 5242880, @@ -135,6 +139,7 @@ test('should parse env vars correctly', t => { process.env.FASTIFY_PREFIX = 'FASTIFY_' process.env.FASTIFY_BODY_LIMIT = '5242880' process.env.FASTIFY_PLUGIN_TIMEOUT = '500' + process.env.FASTIFY_FORCE_CLOSE_CONNECTIONS = 'true' process.env.FASTIFY_CLOSE_GRACE_DELAY = '30000' process.env.FASTIFY_DEBUG = 'true' process.env.FASTIFY_DEBUG_PORT = '1111' @@ -156,6 +161,7 @@ test('should parse env vars correctly', t => { delete process.env.FASTIFY_PREFIX delete process.env.FASTIFY_BODY_LIMIT delete process.env.FASTIFY_PLUGIN_TIMEOUT + delete process.env.FASTIFY_FORCE_CLOSE_CONNECTIONS delete process.env.FASTIFY_CLOSE_GRACE_DELAY delete process.env.FASTIFY_DEBUG delete process.env.FASTIFY_DEBUG_PORT @@ -181,6 +187,7 @@ test('should parse env vars correctly', t => { require: './require-module.js', import: './import-module.js', pluginTimeout: 500, + forceCloseConnections: true, closeGraceDelay: 30000, pluginOptions: {}, debug: true, @@ -195,7 +202,7 @@ test('should parse env vars correctly', t => { }) test('should respect default values', t => { - t.plan(14) + t.plan(15) const argv = [ 'app.js' @@ -211,6 +218,7 @@ test('should respect default values', t => { t.equal(parsedArgs.verboseWatch, false) t.equal(parsedArgs.logLevel, 'fatal') t.equal(parsedArgs.pluginTimeout, 10000) + t.equal(parsedArgs.forceCloseConnections, undefined) t.equal(parsedArgs.closeGraceDelay, 500) t.equal(parsedArgs.debug, false) t.equal(parsedArgs.debugPort, 9320) @@ -236,6 +244,7 @@ test('should parse custom plugin options', t => { '--options', 'true', '--prefix', 'FASTIFY_', '--plugin-timeout', '500', + '--force-close-connections', 'true', '--close-grace-delay', '30000', '--body-limit', '5242880', '--debug', 'true', @@ -269,6 +278,7 @@ test('should parse custom plugin options', t => { logLevel: 'info', prefix: 'FASTIFY_', pluginTimeout: 500, + forceCloseConnections: true, closeGraceDelay: 30000, pluginOptions: { a: true, @@ -303,6 +313,7 @@ test('should parse config file correctly and prefer config values over default o port: 5000, bodyLimit: undefined, pluginTimeout: 9000, + forceCloseConnections: true, closeGraceDelay: 1000, pluginOptions: {}, prettyLogs: true, @@ -335,6 +346,7 @@ test('should prefer command line args over config file options', t => { '--port', '4000', '--debugPort', '9320', '--plugin-timeout', '10000', + '--force-close-connections', 'false', '--close-grace-delay', '30000', 'app.js' ] @@ -346,6 +358,7 @@ test('should prefer command line args over config file options', t => { port: 4000, bodyLimit: undefined, pluginTimeout: 10000, + forceCloseConnections: false, closeGraceDelay: 30000, pluginOptions: {}, prettyLogs: true, diff --git a/test/data/custom-config.js b/test/data/custom-config.js index 744e471c..8195022d 100644 --- a/test/data/custom-config.js +++ b/test/data/custom-config.js @@ -8,5 +8,6 @@ module.exports = { prettyLogs: true, debugPort: 4000, pluginTimeout: 9 * 1000, + forceCloseConnections: true, closeGraceDelay: 1000 } From e9804394b71eaa73196c7ac376332280a382eba5 Mon Sep 17 00:00:00 2001 From: Luca Rainone Date: Sat, 28 Sep 2024 09:21:10 +0200 Subject: [PATCH 2/2] update: restore format of table --- README.md | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 5305868d..a8e83eba 100644 --- a/README.md +++ b/README.md @@ -149,29 +149,29 @@ If your `package.json` does not have `"type": "module"`, use `.mjs` for the exte #### Options You can pass the following options via CLI arguments. You can also use `--config` or `-c` flag to pass a configuration file that exports all the properties listed below in camelCase convention. In case of collision (i.e., An argument existing in both the configuration file and as a command-line argument, the command-line argument is given the priority). Every option has a corresponding environment variable: -| Description | Short command | Full command | Environment variable | -|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------|-----------------------------|-----------------------------------| -| Path to configuration file that can be used to manage the options listed below | `-c` | `--config` | `FASTIFY_CONFIG or CONFIG` | -| Port to listen on (default to 3000) | `-p` | `--port` | `FASTIFY_PORT or PORT` | -| Address to listen on | `-a` | `--address` | `FASTIFY_ADDRESS` | -| Socket to listen on | `-s` | `--socket` | `FASTIFY_SOCKET` | -| Module to preload | `-r` | `--require` | `FASTIFY_REQUIRE` | -| ES Module to preload | `-i` | `--import` | `FASTIFY_IMPORT` | -| Log level (default to fatal) | `-l` | `--log-level` | `FASTIFY_LOG_LEVEL` | -| Path to logging configuration module to use | `-L` | `--logging-module` | `FASTIFY_LOGGING_MODULE` | -| Start Fastify app in debug mode with nodejs inspector | `-d` | `--debug` | `FASTIFY_DEBUG` | -| Set the inspector port (default: 9320) | `-I` | `--debug-port` | `FASTIFY_DEBUG_PORT` | -| Set the inspector host to listen on (default: loopback address or `0.0.0.0` inside Docker or Kubernetes) | | `--debug-host` | `FASTIFY_DEBUG_HOST` | -| Prints pretty logs | `-P` | `--pretty-logs` | `FASTIFY_PRETTY_LOGS` | -| Watch process.cwd() directory for changes, recursively; when that happens, the process will auto reload | `-w` | `--watch` | `FASTIFY_WATCH` | -| Ignore changes to the specified files or directories when watch is enabled. (e.g. `--ignore-watch='node_modules .git logs/error.log'` ) | | `--ignore-watch` | `FASTIFY_IGNORE_WATCH` | -| Prints events triggered by watch listener (useful to debug unexpected reload when using `--watch` ) | `-V` | `--verbose-watch` | `FASTIFY_VERBOSE_WATCH` | -| Use custom options | `-o` | `--options` | `FASTIFY_OPTIONS` | -| Set the prefix | `-x` | `--prefix` | `FASTIFY_PREFIX` | -| Set the plugin timeout | `-T` | `--plugin-timeout` | `FASTIFY_PLUGIN_TIMEOUT` | -| Set forceCloseConnections option on fastify instance | `-f` | `--force-close-connections` | `FASTIFY_FORCE_CLOSE_CONNECTIONS` | -| Defines the maximum payload, in bytes,
that the server is allowed to accept | | `--body-limit` | `FASTIFY_BODY_LIMIT` | -| Set the maximum ms delay before forcefully closing pending requests after receiving SIGTERM or SIGINT signals; and uncaughtException or unhandledRejection errors (default: 500) | `-g` | `--close-grace-delay` | `FASTIFY_CLOSE_GRACE_DELAY` | +| Description | Short command | Full command | Environment variable | +| --------------------------------------------------------------------------------------------------------------------------------------- | ------------- | ------------------ | ------------------------ | +| Path to configuration file that can be used to manage the options listed below | `-c` | `--config` | `FASTIFY_CONFIG or CONFIG` | +| Port to listen on (default to 3000) | `-p` | `--port` | `FASTIFY_PORT or PORT` | +| Address to listen on | `-a` | `--address` | `FASTIFY_ADDRESS` | +| Socket to listen on | `-s` | `--socket` | `FASTIFY_SOCKET` | +| Module to preload | `-r` | `--require` | `FASTIFY_REQUIRE` | +| ES Module to preload | `-i` | `--import` | `FASTIFY_IMPORT` | +| Log level (default to fatal) | `-l` | `--log-level` | `FASTIFY_LOG_LEVEL` | +| Path to logging configuration module to use | `-L` | `--logging-module` | `FASTIFY_LOGGING_MODULE` | +| Start Fastify app in debug mode with nodejs inspector | `-d` | `--debug` | `FASTIFY_DEBUG` | +| Set the inspector port (default: 9320) | `-I` | `--debug-port` | `FASTIFY_DEBUG_PORT` | +| Set the inspector host to listen on (default: loopback address or `0.0.0.0` inside Docker or Kubernetes) | | `--debug-host` | `FASTIFY_DEBUG_HOST` | +| Prints pretty logs | `-P` | `--pretty-logs` | `FASTIFY_PRETTY_LOGS` | +| Watch process.cwd() directory for changes, recursively; when that happens, the process will auto reload | `-w` | `--watch` | `FASTIFY_WATCH` | +| Ignore changes to the specified files or directories when watch is enabled. (e.g. `--ignore-watch='node_modules .git logs/error.log'` ) | | `--ignore-watch` | `FASTIFY_IGNORE_WATCH` | +| Prints events triggered by watch listener (useful to debug unexpected reload when using `--watch` ) | `-V` | `--verbose-watch` | `FASTIFY_VERBOSE_WATCH` | +| Use custom options | `-o` | `--options` | `FASTIFY_OPTIONS` | +| Set the prefix | `-x` | `--prefix` | `FASTIFY_PREFIX` | +| Set the plugin timeout | `-T` | `--plugin-timeout` | `FASTIFY_PLUGIN_TIMEOUT` | +| Set forceCloseConnections option on fastify instance | `-f` | `--force-close-connections` | `FASTIFY_FORCE_CLOSE_CONNECTIONS` | +| Defines the maximum payload, in bytes,
that the server is allowed to accept | | `--body-limit` | `FASTIFY_BODY_LIMIT` | +| Set the maximum ms delay before forcefully closing pending requests after receiving SIGTERM or SIGINT signals; and uncaughtException or unhandledRejection errors (default: 500) | `-g` | `--close-grace-delay` | `FASTIFY_CLOSE_GRACE_DELAY` | By default `fastify-cli` runs [`dotenv`](https://www.npmjs.com/package/dotenv), so it will load all the env variables stored in `.env` in your current working directory.