From 4d72bcdb1b19cc7f4539456a39612e00a9e07598 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Fri, 12 Aug 2022 15:52:59 +0200 Subject: [PATCH] Default fd=1 in pino.destination if stdout has no file descriptor (#1517) Signed-off-by: Matteo Collina Signed-off-by: Matteo Collina --- lib/tools.js | 5 +++++ test/stdout-protection.test.js | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/lib/tools.js b/lib/tools.js index 147be2c6d..29bf28107 100644 --- a/lib/tools.js +++ b/lib/tools.js @@ -350,6 +350,11 @@ function normalizeDestFileDescriptor (destination) { if (typeof destination === 'string' && Number.isFinite(fd)) { return fd } + // destination could be undefined if we are in a worker + if (destination === undefined) { + // This is stdout in UNIX systems + return 1 + } return destination } diff --git a/test/stdout-protection.test.js b/test/stdout-protection.test.js index ec27341a2..c10f45cb2 100644 --- a/test/stdout-protection.test.js +++ b/test/stdout-protection.test.js @@ -30,3 +30,10 @@ test('do not crash if process.stdout has no fd', async ({ teardown }) => { teardown(function () { process.stdout.fd = fd }) pino() }) + +test('use fd=1 if process.stdout has no fd in pino.destination() (worker case)', async ({ teardown }) => { + const fd = process.stdout.fd + delete process.stdout.fd + teardown(function () { process.stdout.fd = fd }) + pino.destination() +})