From 9a80ea1f9ff9f63eaaac8c82daf67b82efdad6dc Mon Sep 17 00:00:00 2001 From: Manuel Spigolon Date: Thu, 1 Dec 2022 21:16:51 +0100 Subject: [PATCH] fix: server config deep merge (#568) * chore: workflow lint * fix: config deep merge * Update package.json --- .github/workflows/ci.yml | 1 + examples/plugin-with-logger.js | 19 +++++++++++++++++++ package.json | 3 ++- start.js | 16 +++++++++++----- test/helper.test.js | 26 ++++++++++++++++++++++++++ 5 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 examples/plugin-with-logger.js diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d948dae4..b38f49a3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,4 +14,5 @@ jobs: test: uses: fastify/workflows/.github/workflows/plugins-ci.yml@v3 with: + lint: true auto-merge-exclude: 'help-me' diff --git a/examples/plugin-with-logger.js b/examples/plugin-with-logger.js new file mode 100644 index 00000000..10d342b1 --- /dev/null +++ b/examples/plugin-with-logger.js @@ -0,0 +1,19 @@ +'use strict' + +module.exports = function (fastify, options, next) { + fastify.get('/', function (req, reply) { + reply.send({ hello: 'world' }) + }) + next() +} + +module.exports.options = { + logger: { + redact: { + censor: '***', + paths: [ + 'foo' + ] + } + } +} diff --git a/package.json b/package.json index a5efbe83..cbda60bc 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "lint:fix": "standard --fix", "unit": "tap \"test/**/*.test.{js,ts}\" \"templates/**/*.test.{js,ts}\" --timeout 300", "pretest": "xcopy /e /k /i . \"..\\node_modules\\fastify-cli\" || rsync -r --exclude=node_modules ./ node_modules/fastify-cli || echo 'this is fine'", - "test": "npm run lint && npm run unit && npm run test:typescript", + "test": "npm run unit && npm run test:typescript", "test:typescript": "tsd templates/plugin && tsc --project templates/app-ts/tsconfig.json && del-cli templates/app-ts/dist" }, "keywords": [ @@ -42,6 +42,7 @@ ] }, "dependencies": { + "@fastify/deepmerge": "^1.2.0", "chalk": "^4.1.2", "chokidar": "^3.5.2", "close-with-grace": "^1.1.0", diff --git a/start.js b/start.js index 2562efc2..9d0116bd 100755 --- a/start.js +++ b/start.js @@ -5,6 +5,10 @@ require('dotenv').config() const isDocker = require('is-docker') const closeWithGrace = require('close-with-grace') +const deepmerge = require('@fastify/deepmerge')({ + cloneProtoObject (obj) { return obj } +}) + const listenAddressDocker = '0.0.0.0' const watch = require('./lib/watch') const parseArgs = require('./args') @@ -99,7 +103,7 @@ async function runFastify (args, additionalOptions, serverOptions) { const defaultLogger = { level: opts.logLevel } - const options = { + let options = { logger: logger || defaultLogger, pluginTimeout: opts.pluginTimeout @@ -127,12 +131,14 @@ async function runFastify (args, additionalOptions, serverOptions) { } if (serverOptions) { - Object.assign(options, serverOptions) + options = deepmerge(options, serverOptions) + } + + if (opts.options && file.options) { + options = deepmerge(options, file.options) } - const fastify = Fastify( - opts.options ? Object.assign(options, file.options) : options - ) + const fastify = Fastify(options) if (opts.prefix) { opts.pluginOptions.prefix = opts.prefix diff --git a/test/helper.test.js b/test/helper.test.js index 7150f19e..6eb8613a 100644 --- a/test/helper.test.js +++ b/test/helper.test.js @@ -124,3 +124,29 @@ test('should start fastify with custom logger configuration', async t => { t.same(lines.length, 1) t.same(app.log.level, 'warn') }) + +test('should merge the CLI and FILE configs', async t => { + const argv = ['./examples/plugin-with-logger.js', '--options'] + + const lines = [] + const dest = new stream.Writable({ + write: function (chunk, enc, cb) { + lines.push(JSON.parse(chunk)) + cb() + } + }) + + const app = await helper.listen(argv, {}, { + logger: { + level: 'warn', + stream: dest + } + }) + t.teardown(() => app.close()) + app.log.info('test') + t.same(lines.length, 0) + app.log.warn({ foo: 'test' }) + t.same(app.log.level, 'warn') + t.same(lines.length, 1) + t.same(lines[0].foo, '***') +})