Skip to content

Commit 561cb3e

Browse files
authoredDec 23, 2021
fix(caller): use calling file name for libraries calling Pino (#1276)
* fix(caller): use calling file name for libraries calling Pino * fix(caller): handle case where stacktrace contains node:internal entries * fix: add node_modules in fixtures to gitignore * refactor(caller): merge two functions
1 parent c40ee5a commit 561cb3e

20 files changed

+129
-5
lines changed
 

‎.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,5 @@ profile-*
5555
# lock files
5656
yarn.lock
5757
package-lock.json
58+
59+
!test/fixtures/eval/node_modules

‎lib/caller.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
'use strict'
22

3+
const fs = require('fs')
4+
35
function noOpPrepareStackTrace (_, stack) {
46
return stack
57
}
68

9+
function isOutsideNodeModules (input) {
10+
return typeof input === 'string' && fs.existsSync(input) && input.indexOf('node_modules') === -1
11+
}
12+
713
module.exports = function getCaller () {
814
const originalPrepare = Error.prepareStackTrace
915
Error.prepareStackTrace = noOpPrepareStackTrace
@@ -14,11 +20,15 @@ module.exports = function getCaller () {
1420
return undefined
1521
}
1622

17-
for (const entry of stack.slice(2)) {
23+
const entries = stack.slice(2)
24+
25+
for (const entry of entries) {
1826
const file = entry ? entry.getFileName() : undefined
1927

20-
if (file && file.indexOf('node_modules') === -1) {
28+
if (isOutsideNodeModules(file)) {
2129
return file
2230
}
2331
}
32+
33+
return entries[0] ? entries[0].getFileName() : undefined
2434
}

‎test/fixtures/eval.js ‎test/fixtures/eval/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable no-eval */
22

33
eval(`
4-
const pino = require('../../')
4+
const pino = require('../../../')
55
66
const logger = pino(
77
pino.transport({

‎test/fixtures/eval/node_modules/14-files.js

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/fixtures/eval/node_modules/2-files.js

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/fixtures/eval/node_modules/file1.js

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/fixtures/eval/node_modules/file10.js

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/fixtures/eval/node_modules/file11.js

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/fixtures/eval/node_modules/file12.js

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/fixtures/eval/node_modules/file13.js

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/fixtures/eval/node_modules/file14.js

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/fixtures/eval/node_modules/file2.js

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/fixtures/eval/node_modules/file3.js

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/fixtures/eval/node_modules/file4.js

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/fixtures/eval/node_modules/file5.js

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/fixtures/eval/node_modules/file6.js

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/fixtures/eval/node_modules/file7.js

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/fixtures/eval/node_modules/file8.js

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/fixtures/eval/node_modules/file9.js

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/transport/caller.test.js

+32-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,38 @@ const execa = require('execa')
77

88
const { once } = require('../helper')
99

10-
test('app using a custom transport', async function (t) {
11-
const evalApp = join(__dirname, '../', '/fixtures/eval')
10+
test('when using a custom transport outside node_modules, the first file outside node_modules should be used', async function (t) {
11+
const evalApp = join(__dirname, '../', '/fixtures/eval/index.js')
12+
const child = execa(process.argv[0], [evalApp])
13+
14+
let actual = ''
15+
child.stdout.pipe(writer((s, enc, cb) => {
16+
actual += s
17+
cb()
18+
}))
19+
20+
await once(child, 'close')
21+
22+
t.match(actual, /done!/)
23+
})
24+
25+
test('when using a custom transport where some files in stacktrace are in the node_modules, the first file outside node_modules should be used', async function (t) {
26+
const evalApp = join(__dirname, '../', '/fixtures/eval/node_modules/2-files.js')
27+
const child = execa(process.argv[0], [evalApp])
28+
29+
let actual = ''
30+
child.stdout.pipe(writer((s, enc, cb) => {
31+
actual += s
32+
cb()
33+
}))
34+
35+
await once(child, 'close')
36+
37+
t.match(actual, /done!/)
38+
})
39+
40+
test('when using a custom transport where all files in stacktrace are in the node_modules, the first file inside node_modules should be used', async function (t) {
41+
const evalApp = join(__dirname, '../', '/fixtures/eval/node_modules/14-files.js')
1242
const child = execa(process.argv[0], [evalApp])
1343

1444
let actual = ''

0 commit comments

Comments
 (0)
Please sign in to comment.