Skip to content

Commit

Permalink
fix: server config deep merge (#568)
Browse files Browse the repository at this point in the history
* chore: workflow lint

* fix: config deep merge

* Update package.json
  • Loading branch information
Eomm authored Dec 1, 2022
1 parent c32b2e3 commit 9a80ea1
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 6 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ jobs:
test:
uses: fastify/workflows/.github/workflows/plugins-ci.yml@v3
with:
lint: true
auto-merge-exclude: 'help-me'
19 changes: 19 additions & 0 deletions examples/plugin-with-logger.js
Original file line number Diff line number Diff line change
@@ -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'
]
}
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down Expand Up @@ -42,6 +42,7 @@
]
},
"dependencies": {
"@fastify/deepmerge": "^1.2.0",
"chalk": "^4.1.2",
"chokidar": "^3.5.2",
"close-with-grace": "^1.1.0",
Expand Down
16 changes: 11 additions & 5 deletions start.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions test/helper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '***')
})

0 comments on commit 9a80ea1

Please sign in to comment.