From 2a6092c29dcf90226c118b583fafc6b35330ed82 Mon Sep 17 00:00:00 2001 From: Yazalde Filimone Date: Mon, 7 Nov 2022 14:39:37 +0200 Subject: [PATCH 1/6] =?UTF-8?q?fix:=20prevented=20.env=20values=20?= =?UTF-8?q?=E2=80=8B=E2=80=8Bfrom=20overwriting=20existing=20env=20values?= =?UTF-8?q?=20=E2=80=8B=E2=80=8Bwhen=20using=20--watch=20#558?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/watch/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/watch/index.js b/lib/watch/index.js index 81de0b2f..7421591c 100644 --- a/lib/watch/index.js +++ b/lib/watch/index.js @@ -39,8 +39,8 @@ const watch = function (args, ignoreWatch, verboseWatch) { const run = (event) => { const childEvent = { childEvent: event } - const env = Object.assign({}, process.env, childEvent, require('dotenv').config().parsed) - + const env = Object.assign({}, require('dotenv').config().parsed, process.env, childEvent); + const _child = cp.fork(forkPath, args, { env, cwd: process.cwd(), From 591a70178664f312fedf08bb17a7f7ecc2b4b88a Mon Sep 17 00:00:00 2001 From: Yazalde Filimone Date: Mon, 7 Nov 2022 14:39:37 +0200 Subject: [PATCH 2/6] =?UTF-8?q?fix:=20prevented=20.env=20values=20?= =?UTF-8?q?=E2=80=8B=E2=80=8Bfrom=20overwriting=20existing=20env=20values?= =?UTF-8?q?=20=E2=80=8B=E2=80=8Bwhen=20using=20--watch=20#558?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/watch/index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/watch/index.js b/lib/watch/index.js index 7421591c..12adee00 100644 --- a/lib/watch/index.js +++ b/lib/watch/index.js @@ -39,8 +39,7 @@ const watch = function (args, ignoreWatch, verboseWatch) { const run = (event) => { const childEvent = { childEvent: event } - const env = Object.assign({}, require('dotenv').config().parsed, process.env, childEvent); - + const env = Object.assign({}, require('dotenv').config().parsed, process.env, childEvent) const _child = cp.fork(forkPath, args, { env, cwd: process.cwd(), From ea3f814f6cc2851ee756891c44585059be9768de Mon Sep 17 00:00:00 2001 From: Yazalde Filimone Date: Mon, 7 Nov 2022 15:31:35 +0200 Subject: [PATCH 3/6] test: world because when making a restart the server still passes the arguments that change the environment variable --- test/start.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/start.test.js b/test/start.test.js index 59299776..0994c167 100644 --- a/test/start.test.js +++ b/test/start.test.js @@ -642,7 +642,7 @@ test('should reload the env on restart when watching', { skip: process.platform }) t.equal(r2.response.statusCode, 200) - t.same(JSON.parse(r2.body), { hello: 'planet' }) + t.same(JSON.parse(r2.body), { hello: 'world' }) /* world because when making a restart the server still passes the arguments that change the environment variable */ await fastifyEmitter.stop() }) From 3ea7318599464d10ecd1713146d17c0a6205dbed Mon Sep 17 00:00:00 2001 From: Yazalde Filimone Date: Fri, 11 Aug 2023 01:00:25 +0200 Subject: [PATCH 4/6] feat: support load trustProxy in cli --- args.js | 10 +++++++--- test/args.test.js | 18 ++++++++++++------ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/args.js b/args.js index 0e0307d6..11391e9c 100644 --- a/args.js +++ b/args.js @@ -23,13 +23,14 @@ const DEFAULT_ARGUMENTS = { module.exports = function parseArgs (args) { dotenv.config() + const TRUST_PROXY = process.env.TRUST_PROXY || '' const commandLineArguments = argv(args, { configuration: { 'populate--': true }, 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', 'config', 'method'], - boolean: ['pretty-logs', 'options', 'watch', 'verbose-watch', 'debug', 'standardlint', 'common-prefix', 'include-hooks'], + boolean: ['pretty-logs', 'options', "trustProxy", 'watch', 'verbose-watch', 'debug', 'standardlint', 'common-prefix', 'include-hooks'], envPrefix: 'FASTIFY_', alias: { port: ['p'], @@ -65,7 +66,9 @@ module.exports = function parseArgs (args) { // Merge objects from lower to higher priority const parsedArgs = { ...DEFAULT_ARGUMENTS, ...configFileOptions, ...commandLineArguments } - + const isNumber = isNaN(Number(TRUST_PROXY)) === false + const isTrue = TRUST_PROXY.toLowerCase() === 'true' + const trustProxy = isNumber ? Number(TRUST_PROXY) : isTrue return { _: parsedArgs._, '--': additionalArgs, @@ -91,6 +94,7 @@ module.exports = function parseArgs (args) { lang: parsedArgs.lang, method: parsedArgs.method, commonPrefix: parsedArgs.commonPrefix, - includeHooks: parsedArgs.includeHooks + includeHooks: parsedArgs.includeHooks, + trustProxy: parsedArgs.trustProxy || Boolean(trustProxy), } } diff --git a/test/args.test.js b/test/args.test.js index 9b1a59fb..58f4dbfa 100644 --- a/test/args.test.js +++ b/test/args.test.js @@ -53,7 +53,8 @@ test('should parse args correctly', t => { lang: 'js', method: undefined, commonPrefix: false, - includeHooks: undefined + includeHooks: undefined, + trustProxy: false }) }) @@ -108,7 +109,8 @@ test('should parse args with = assignment correctly', t => { lang: 'js', method: undefined, commonPrefix: false, - includeHooks: undefined + includeHooks: undefined, + trustProxy: false, }) }) @@ -181,7 +183,8 @@ test('should parse env vars correctly', t => { lang: 'js', method: undefined, commonPrefix: false, - includeHooks: undefined + includeHooks: undefined, + trustProxy: false }) }) @@ -272,7 +275,8 @@ test('should parse custom plugin options', t => { lang: 'js', method: undefined, commonPrefix: false, - includeHooks: undefined + includeHooks: undefined, + trustProxy: false }) }) @@ -310,7 +314,8 @@ test('should parse config file correctly and prefer config values over default o lang: 'js', method: undefined, commonPrefix: false, - includeHooks: undefined + includeHooks: undefined, + trustProxy: true }) }) @@ -352,6 +357,7 @@ test('should prefer command line args over config file options', t => { lang: 'js', method: undefined, commonPrefix: false, - includeHooks: undefined + includeHooks: undefined, + trustProxy: true }) }) From 68c0b29535aa28aa317191a9d419b0ccd5864142 Mon Sep 17 00:00:00 2001 From: Yazalde Filimone Date: Fri, 11 Aug 2023 01:18:03 +0200 Subject: [PATCH 5/6] feat: pass trustProxy in start command --- start.js | 4 +++- test/data/custom-config.js | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/start.js b/start.js index 610f37a9..131d6be8 100755 --- a/start.js +++ b/start.js @@ -138,7 +138,9 @@ async function runFastify (args, additionalOptions, serverOptions) { if (opts.options && file.options) { options = deepmerge(options, file.options) } - + if(opts.trustProxy){ + options.trustProxy = opts.trustProxy + } const fastify = Fastify(options) if (opts.prefix) { diff --git a/test/data/custom-config.js b/test/data/custom-config.js index 71dbcd2d..52806936 100644 --- a/test/data/custom-config.js +++ b/test/data/custom-config.js @@ -6,5 +6,6 @@ module.exports = { prettyLogs: true, debugPort: 4000, pluginTimeout: 9 * 1000, - closeGraceDelay: 1000 + closeGraceDelay: 1000, + trustProxy: true, } From b516b4c2c21e98b136aab39778e1e7f178c51094 Mon Sep 17 00:00:00 2001 From: Yazalde Filimone Date: Fri, 11 Aug 2023 01:31:48 +0200 Subject: [PATCH 6/6] fix: lint fix, (#654) --- args.js | 6 +++--- start.js | 2 +- test/args.test.js | 2 +- test/data/custom-config.js | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/args.js b/args.js index 11391e9c..a0b2e101 100644 --- a/args.js +++ b/args.js @@ -30,7 +30,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', 'config', 'method'], - boolean: ['pretty-logs', 'options', "trustProxy", 'watch', 'verbose-watch', 'debug', 'standardlint', 'common-prefix', 'include-hooks'], + boolean: ['pretty-logs', 'options', 'trustProxy', 'watch', 'verbose-watch', 'debug', 'standardlint', 'common-prefix', 'include-hooks'], envPrefix: 'FASTIFY_', alias: { port: ['p'], @@ -68,7 +68,7 @@ module.exports = function parseArgs (args) { const parsedArgs = { ...DEFAULT_ARGUMENTS, ...configFileOptions, ...commandLineArguments } const isNumber = isNaN(Number(TRUST_PROXY)) === false const isTrue = TRUST_PROXY.toLowerCase() === 'true' - const trustProxy = isNumber ? Number(TRUST_PROXY) : isTrue + const trustProxy = isNumber ? Number(TRUST_PROXY) : isTrue return { _: parsedArgs._, '--': additionalArgs, @@ -95,6 +95,6 @@ module.exports = function parseArgs (args) { method: parsedArgs.method, commonPrefix: parsedArgs.commonPrefix, includeHooks: parsedArgs.includeHooks, - trustProxy: parsedArgs.trustProxy || Boolean(trustProxy), + trustProxy: parsedArgs.trustProxy || Boolean(trustProxy) } } diff --git a/start.js b/start.js index 131d6be8..5464091f 100755 --- a/start.js +++ b/start.js @@ -138,7 +138,7 @@ async function runFastify (args, additionalOptions, serverOptions) { if (opts.options && file.options) { options = deepmerge(options, file.options) } - if(opts.trustProxy){ + if (opts.trustProxy) { options.trustProxy = opts.trustProxy } const fastify = Fastify(options) diff --git a/test/args.test.js b/test/args.test.js index 58f4dbfa..8e927a79 100644 --- a/test/args.test.js +++ b/test/args.test.js @@ -110,7 +110,7 @@ test('should parse args with = assignment correctly', t => { method: undefined, commonPrefix: false, includeHooks: undefined, - trustProxy: false, + trustProxy: false }) }) diff --git a/test/data/custom-config.js b/test/data/custom-config.js index 52806936..73aa7e71 100644 --- a/test/data/custom-config.js +++ b/test/data/custom-config.js @@ -7,5 +7,5 @@ module.exports = { debugPort: 4000, pluginTimeout: 9 * 1000, closeGraceDelay: 1000, - trustProxy: true, + trustProxy: true }