Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait for destination to close #533

Merged
merged 6 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,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 @@ -152,8 +153,6 @@ function build (opts = {}) {
}
})

let destination

if (typeof opts.destination === 'object' && typeof opts.destination.write === 'function') {
destination = opts.destination
} else {
Expand All @@ -171,7 +170,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