Skip to content

Commit

Permalink
wait for destination to close
Browse files Browse the repository at this point in the history
Signed-off-by: Matteo Collina <[email protected]>
  • Loading branch information
mcollina committed Oct 15, 2024
1 parent a2738a5 commit cda2758
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
12 changes: 9 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ function prettyFactory (options) {
*/
function build (opts = {}) {
let pretty = prettyFactory(opts)
let destination
return abstractTransport(function (source) {
source.on('message', function pinoConfigListener (message) {
if (!message || message.code !== 'PINO_CONFIG') return
Expand All @@ -151,8 +152,6 @@ function build (opts = {}) {
}
})

let destination

if (typeof opts.destination === 'object' && typeof opts.destination.write === 'function') {
destination = opts.destination
} else {
Expand All @@ -170,7 +169,14 @@ function build (opts = {}) {

pump(source, stream, destination, () => {})
return stream
}, { parse: 'lines' })
}, {
parse: 'lines',
close (err, cb) {
destination.on('close', () => {
cb(err)
})
}
})
}

module.exports = build
Expand Down
32 changes: 32 additions & 0 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,38 @@ test('basic prettifier tests', (t) => {
t.equal(closeCalled, false)
})

t.test('wait for close event from destination', (t) => {
t.plan(2)
const destination = pino.destination({ minLength: 4096, sync: true })
const prettyDestination = pinoPretty({ destination, colorize: false })
const log = pino(prettyDestination)
log.info('this message has been buffered')
const chunks = []
const { close, writeSync } = fs
fs.close = new Proxy(close, {
apply: (target, self, args) => {
}
})
fs.writeSync = new Proxy(writeSync, {
apply: (target, self, args) => {
chunks.push(args[1])
return args[1].length
}
})
t.teardown(() => {
Object.assign(fs, { close, writeSync })
})
let destinationClosed = false
destination.on('close', () => {
destinationClosed = true
})
prettyDestination.on('close', () => {
t.match(chunks.join(''), /INFO .+: this message has been buffered/)
t.equal(destinationClosed, true)
})
prettyDestination.end()
})

t.test('stream usage', async (t) => {
t.plan(1)
const tmpDir = path.join(__dirname, '.tmp_' + Date.now())
Expand Down

0 comments on commit cda2758

Please sign in to comment.