Skip to content

Commit

Permalink
add diagnostics_channel publish at initializaiton time
Browse files Browse the repository at this point in the history
  • Loading branch information
bengl committed Aug 26, 2021
1 parent 6ded7ff commit d460b26
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
8 changes: 8 additions & 0 deletions fastify.js
Expand Up @@ -399,6 +399,14 @@ function fastify (options) {
// Delay configuring clientError handler so that it can access fastify state.
server.on('clientError', options.clientErrorHandler.bind(fastify))

try {
const dc = require('diagnostics_channel')
dc.channel('fastify:initialized').publish(fastify)
} catch {
// This only happens if `diagnostics_channel` isn't available, i.e. earlier
// versions of Node.js. In that event, we don't care, so ignore the error.
}

return fastify

function throwIfAlreadyStarted (msg) {
Expand Down
29 changes: 29 additions & 0 deletions test/diagnostics-channel.test.js
@@ -0,0 +1,29 @@
'use strict'

const t = require('tap')
const test = t.test
let dc
try {
dc = require('diagnostics_channel')
} catch {
// Not available in this version of Node.js
}

test('diagnostics_channel', t => {
if (!dc) {
t.plan(1)
t.skip('diagnostics_channel is not available')
return
}
t.plan(2)

let fastifyInHook

const channel = dc.channel('fastify:initialized')
channel.subscribe(function (fastify) {
t.pass('message passed to channel')
fastifyInHook = fastify
})
const fastify = require('../fastify')()
t.equal(fastifyInHook, fastify)
})

0 comments on commit d460b26

Please sign in to comment.