Repository: pinojs/pino (js)

Issue:
v8.3.0 throws "SonicBoom supports only file descriptors and files"
After upgrading from 8.2.0 to 8.3.0 my very basic usage of `pino` is now throwing:

```
Error: SonicBoom supports only file descriptors and files
 ❯ new SonicBoom node_modules/sonic-boom/index.js:121:11
 ❯ buildSafeSonicBoom node_modules/pino/lib/tools.js:214:18
 ❯ normalizeArgs node_modules/pino/lib/tools.js:308:18
 ❯ Proxy.pino node_modules/pino/pino.js:84:28
```

My code is:

```js
import pino from 'pino';
const logger = pino();
export default logger;
```

Rolling back to 8.2.0 makes the problem go away. I looked at the diff between 8.2.0 and 8.3.0 and I can't figure out what's wrong. Could something in the `sonic-boom` package upgrade be causing this?

Pull request:
Do not assume process.stdout has a file descriptor
Signed-off-by: Matteo Collina <hello@matteocollina.com>

Fixes #1502 

Patch:
diff --git a/lib/tools.js b/lib/tools.js
index 530c5a5..147be2c 100644
--- a/lib/tools.js
+++ b/lib/tools.js
@@ -305,7 +305,9 @@ function createArgsNormalizer (defaultOptions) {
     if (enabled === false) opts.level = 'silent'
     if (!stream) {
       if (!hasBeenTampered(process.stdout)) {
-        stream = buildSafeSonicBoom({ fd: process.stdout.fd })
+        // If process.stdout.fd is undefined, it means that we are running
+        // in a worker thread. Let's assume we are logging to file descriptor 1.
+        stream = buildSafeSonicBoom({ fd: process.stdout.fd || 1 })
       } else {
         stream = process.stdout
       }


Test patch:
diff --git a/test/stdout-protection.test.js b/test/stdout-protection.test.js
index c37e008..ec27341 100644
--- a/test/stdout-protection.test.js
+++ b/test/stdout-protection.test.js
@@ -23,3 +23,10 @@ test('do not use SonicBoom is someone has passed process.stdout to pino', async
   const logger = pino(process.stdout)
   equal(logger[pino.symbols.streamSym], process.stdout)
 })
+
+test('do not crash if process.stdout has no fd', async ({ teardown }) => {
+  const fd = process.stdout.fd
+  delete process.stdout.fd
+  teardown(function () { process.stdout.fd = fd })
+  pino()
+})
diff --git a/test/transport/core.test.js b/test/transport/core.test.js
index 5b3f429..dcfff30 100644
--- a/test/transport/core.test.js
+++ b/test/transport/core.test.js
@@ -12,8 +12,10 @@ const strip = require('strip-ansi')
 const execa = require('execa')
 const writer = require('flush-write-stream')
 const rimraf = require('rimraf')
+const { promisify } = require('util')
 const { tmpdir } = os
 
+const immediate = promisify(setImmediate)
 const pid = process.pid
 const hostname = os.hostname()
 
@@ -398,6 +400,7 @@ test('stdout in worker', async ({ not }) => {
     cb()
   }))
   await once(child, 'close')
+  await immediate()
   not(strip(actual).match(/Hello/), null)
 })
 
@@ -410,6 +413,7 @@ test('log and exit on ready', async ({ not }) => {
     cb()
   }))
   await once(child, 'close')
+  await immediate()
   not(strip(actual).match(/Hello/), null)
 })