diff --git a/file.js b/file.js index e824b9e36..7968c1945 100644 --- a/file.js +++ b/file.js @@ -4,15 +4,7 @@ const pino = require('./pino') const { once } = require('events') module.exports = async function (opts = {}) { - const destination = pino.destination({ dest: normalizeDest(opts.destination || 1), sync: false }) + const destination = pino.destination({ dest: opts.destination || 1, sync: false }) await once(destination, 'ready') return destination } - -function normalizeDest (destination) { - const fd = Number(destination) - if (typeof destination === 'string' && Number.isFinite(fd)) { - return fd - } - return destination -} diff --git a/lib/tools.js b/lib/tools.js index 8532c8c86..14cce4844 100644 --- a/lib/tools.js +++ b/lib/tools.js @@ -514,6 +514,22 @@ function setMetadataProps (dest, that) { } } +/** + * Convert a string integer file descriptor to a proper native integer + * file descriptor. + * + * @param {string} destination The file descriptor string to attempt to convert. + * + * @returns {Number} + */ +function normalizeDestFileDescriptor (destination) { + const fd = Number(destination) + if (typeof destination === 'string' && Number.isFinite(fd)) { + return fd + } + return destination +} + module.exports = { noop, buildSafeSonicBoom, @@ -524,5 +540,6 @@ module.exports = { createArgsNormalizer, final, stringify, - buildFormatters + buildFormatters, + normalizeDestFileDescriptor } diff --git a/pino.js b/pino.js index 980732281..31330c9c3 100644 --- a/pino.js +++ b/pino.js @@ -15,6 +15,7 @@ const { stringify, buildSafeSonicBoom, buildFormatters, + normalizeDestFileDescriptor, noop } = require('./lib/tools') const { version } = require('./lib/meta') @@ -175,10 +176,10 @@ module.exports = pino module.exports.destination = (dest = process.stdout.fd) => { if (typeof dest === 'object') { - dest.dest = dest.dest || process.stdout.fd + dest.dest = normalizeDestFileDescriptor(dest.dest || process.stdout.fd) return buildSafeSonicBoom(dest) } else { - return buildSafeSonicBoom({ dest, minLength: 0, sync: true }) + return buildSafeSonicBoom({ dest: normalizeDestFileDescriptor(dest), minLength: 0, sync: true }) } }