Repository: pinojs/pino (js)

Issue:
pino 7 - error on flush
I get this error:

```
^C/Users/mspigolon/workspace/_experiments/fastify-scripts/node_modules/thread-stream/index.js:307
      cb()
      ^

TypeError: cb is not a function
    at /Users/mspigolon/workspace/_experiments/fastify-scripts/node_modules/thread-stream/index.js:307:7
    at wait (/Users/mspigolon/workspace/_experiments/fastify-scripts/node_modules/thread-stream/lib/wait.js:9:5)
```

For this script:

```
const pino = require('pino')

const transport = pino.transport({
  target: require('path').join(__dirname, './tran.js')
})
const logger = pino(transport)

setInterval(() => {
  logger.info('hello world')
}, 1000)

process.on('SIGINT', () => { logger.flush() })
```

`tran.js` file (from transport documentation):
```js
const { Writable } = require( 'stream')
module.exports = (options) => {
  const myTransportStream = new Writable({
    write (chunk, enc, cb) {
    // apply a transform and send to stdout
    console.log(chunk.toString().toUpperCase())
    cb()
    }
  })
  return myTransportStream
}
```

Or this transport produces the same error:

```js
const build = require('pino-abstract-transport')

module.exports = function (opts) {
  return build(function (source) {
    source.on('data', function (obj) {
      console.log(obj)
    })
  })
}
```

It seems related to this module:
https://github.com/pinojs/thread-stream


Pull request:
fix: flush thread-stream missing callback
Fixes #1136 

Currently, `.flush` will be either `sonic-beam` stream or `thread-stream` stream.
`sonic-beam` stream do not require `callback` for `.flush`. But, `thread-stream` does.

I just put a `noop` callback inside the function to prevent throwing for `thread-stream`.

Patch:
diff --git a/file.js b/file.js
index dde0fae..477fbd5 100644
--- a/file.js
+++ b/file.js
@@ -1,7 +1,7 @@
 'use strict'
 
 const { Transform, pipeline } = require('stream')
-const pino = require('../pino')
+const pino = require('./pino')
 const { once } = require('events')
 
 module.exports = async function (opts = {}) {
diff --git a/lib/proto.js b/lib/proto.js
index a4c5619..ae00e38 100644
--- a/lib/proto.js
+++ b/lib/proto.js
@@ -190,7 +190,9 @@ function write (_obj, msg, num) {
   stream.write(s)
 }
 
+function noop () {}
+
 function flush () {
   const stream = this[streamSym]
-  if ('flush' in stream) stream.flush()
+  if ('flush' in stream) stream.flush(noop)
 }


Test patch:
diff --git a/test/fixtures/console-transport.js b/test/fixtures/console-transport.js
new file mode 100644
index 0000000..cb7dbab
--- /dev/null
+++ b/test/fixtures/console-transport.js
@@ -0,0 +1,12 @@
+const { Writable } = require('stream')
+
+module.exports = (options) => {
+  const myTransportStream = new Writable({
+    write (chunk, enc, cb) {
+      // apply a transform and send to stdout
+      console.log(chunk.toString().toUpperCase())
+      cb()
+    }
+  })
+  return myTransportStream
+}
diff --git a/test/syncfalse.test.js b/test/syncfalse.test.js
index cf16988..dcf2f7a 100644
--- a/test/syncfalse.test.js
+++ b/test/syncfalse.test.js
@@ -116,3 +116,13 @@ test('flush does nothing with sync true (default)', async () => {
   const instance = require('..')()
   instance.flush()
 })
+
+test('thread-stream async flush', async () => {
+  const pino = require('..')
+  const transport = pino.transport({
+    target: join(__dirname, 'fixtures', 'console-transport.js')
+  })
+  const instance = pino(transport)
+  instance.info('hello')
+  instance.flush()
+})
diff --git a/test/targets.test.js b/test/targets.test.js
index aa2bb8e..03cf6ce 100644
--- a/test/targets.test.js
+++ b/test/targets.test.js
@@ -8,7 +8,7 @@ test('file-target mocked', async function ({ equal, same, plan, pass }) {
   plan(1)
   let ret
   const fileTarget = proxyquire('../file', {
-    '../pino': {
+    './pino': {
       destination (opts) {
         same(opts, { dest: 1, sync: false })