Skip to content

Commit

Permalink
Correctly select the correct file when using the transport option (#1147
Browse files Browse the repository at this point in the history
)
  • Loading branch information
mcollina authored Oct 7, 2021
1 parent 34ddfb9 commit e9eb267
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ function autoEnd (stream, eventName) {
}

function createArgsNormalizer (defaultOptions) {
return function normalizeArgs (instance, opts = {}, stream) {
return function normalizeArgs (instance, caller, opts = {}, stream) {
// support stream as a string
if (typeof opts === 'string') {
stream = buildSafeSonicBoom({ dest: opts, sync: true })
Expand All @@ -404,7 +404,7 @@ function createArgsNormalizer (defaultOptions) {
stream = opts
opts = null
} else if (opts.transport) {
stream = transport(opts.transport)
stream = transport(opts.transport, caller)
opts = null
}
opts = Object.assign({}, defaultOptions, opts)
Expand Down
3 changes: 1 addition & 2 deletions lib/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,9 @@ function autoEnd (stream) {
})
}

function transport (fullOptions) {
function transport (fullOptions, callerFile = caller()) {
const { pipeline, targets, options = {}, worker = {} } = fullOptions
// This function call MUST stay in the top-level function of this module
const callerFile = caller()
const callerRequire = createRequire(callerFile)

let target = fullOptions.target
Expand Down
3 changes: 2 additions & 1 deletion pino.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/* eslint no-prototype-builtins: 0 */
const os = require('os')
const stdSerializers = require('pino-std-serializers')
const caller = require('get-caller-file')
const redaction = require('./lib/redaction')
const time = require('./lib/time')
const proto = require('./lib/proto')
Expand Down Expand Up @@ -75,7 +76,7 @@ const serializers = Object.assign(Object.create(null), stdSerializers)

function pino (...args) {
const instance = {}
const { opts, stream } = normalize(instance, ...args)
const { opts, stream } = normalize(instance, caller(), ...args)
const {
redact,
crlf,
Expand Down
61 changes: 58 additions & 3 deletions test/transport/module-link.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,31 @@

const os = require('os')
const { join } = require('path')
const { readFile, symlink, unlink } = require('fs').promises
const { readFile, symlink, unlink, mkdir, writeFile } = require('fs').promises
const { test } = require('tap')
const { isWin, isYarnPnp, watchFileCreated } = require('../helper')
const { once } = require('events')
const execa = require('execa')
const pino = require('../../')

const { pid } = process
const hostname = os.hostname()

async function installTransportModule () {
async function installTransportModule (target) {
if (isYarnPnp) {
return
}
try {
await uninstallTransportModule()
} catch {}

if (!target) {
target = join(__dirname, '..', '..')
}

await symlink(
join(__dirname, '..', 'fixtures', 'transport'),
join(__dirname, '..', '..', 'node_modules', 'transport')
join(target, 'node_modules', 'transport')
)
}

Expand Down Expand Up @@ -91,3 +98,51 @@ test('pino.transport with package as a target', { skip: isWin }, async ({ same,
msg: 'hello'
})
})

// TODO make this test pass on Windows
test('pino({ transport })', { skip: isWin || isYarnPnp }, async ({ same, teardown }) => {
const folder = join(
os.tmpdir(),
'_' + Math.random().toString(36).substr(2, 9)
)

const destination = join(folder, 'output')

await mkdir(join(folder, 'node_modules'), { recursive: true })

// Link pino
await symlink(
join(__dirname, '..', '..'),
join(folder, 'node_modules', 'pino')
)

await installTransportModule(folder)

const toRun = join(folder, 'index.js')

const toRunContent = `
const pino = require('pino')
const logger = pino({
transport: {
target: 'transport',
options: { destination: '${destination}' }
}
})
logger.info('hello')
`

await writeFile(toRun, toRunContent)

const child = execa(process.argv[0], [toRun])

await once(child, 'close')

const result = JSON.parse(await readFile(destination))
delete result.time
same(result, {
pid: child.pid,
hostname,
level: 30,
msg: 'hello'
})
})

0 comments on commit e9eb267

Please sign in to comment.