Skip to content

Commit

Permalink
Merge 2d662c7 into 01a8633
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Jul 23, 2021
2 parents 01a8633 + 2d662c7 commit 34fe1f8
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ function setupOnExit (stream) {
})
} else {
const fn = autoEnd.bind(null, stream)
process.on('exit', fn)
process.once('beforeExit', fn)
process.once('exit', fn)

stream.on('close', function () {
process.removeListener('beforeExit', fn)
process.removeListener('exit', fn)
})
}
Expand All @@ -47,7 +49,11 @@ function buildStream (filename, workerData, workerOpts) {
}

function autoEnd (stream) {
stream.ref()
stream.end()
stream.once('close', function () {
stream.unref()
})
}

function transport (fullOptions) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
"fast-redact": "^3.0.0",
"fast-safe-stringify": "^2.0.8",
"get-caller-file": "^2.0.5",
"on-exit-leak-free": "^0.1.0",
"on-exit-leak-free": "^0.2.0",
"pino-abstract-transport": "^0.2.0",
"pino-std-serializers": "^4.0.0",
"json-stringify-safe": "^5.0.1",
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/transport-main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict'

const { join } = require('path')
const pino = require('../..')
const transport = pino.transport({
target: join(__dirname, 'transport-worker.js')
})
const logger = pino(transport)
logger.info('Hello')
13 changes: 13 additions & 0 deletions test/fixtures/transport-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict'

const { Writable } = require('stream')
module.exports = (options) => {
const myTransportStream = new Writable({
autoDestroy: true,
write (chunk, enc, cb) {
console.log(chunk.toString())
cb()
}
})
return myTransportStream
}
14 changes: 14 additions & 0 deletions test/transport.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const { isWin, watchFileCreated } = require('./helper')
const pino = require('../')
const url = require('url')
const strip = require('strip-ansi')
const execa = require('execa')
const writer = require('flush-write-stream')

const { pid } = process
const hostname = os.hostname()
Expand Down Expand Up @@ -366,3 +368,15 @@ test('pino.transport with target #pino/pretty', async ({ match, teardown }) => {
const actual = await readFile(destination, 'utf8')
match(strip(actual), /\[.*\] INFO.*hello/)
})

test('stdout in worker', async ({ not }) => {
let actual = ''
const child = execa(process.argv[0], [join(__dirname, 'fixtures', 'transport-main.js')])

child.stdout.pipe(writer((s, enc, cb) => {
actual += s
cb()
}))
await once(child, 'close')
not(strip(actual).match(/Hello/), null)
})

0 comments on commit 34fe1f8

Please sign in to comment.