Skip to content

Commit cd72d10

Browse files
authored
make getPluginName resilient against too low stackTraceLimit (#209)
1 parent d7c3866 commit cd72d10

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

lib/getPluginName.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ const fileNamePattern = /(\w*(\.\w*)*)\..*/
66
module.exports = function getPluginName (fn) {
77
if (fn.name.length > 0) return fn.name
88

9+
const stackTraceLimit = Error.stackTraceLimit
10+
Error.stackTraceLimit = 10
911
try {
1012
throw new Error('anonymous function')
1113
} catch (e) {
14+
Error.stackTraceLimit = stackTraceLimit
1215
return extractPluginName(e.stack)
1316
}
1417
}

test/test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,29 @@ test('should set anonymous function name to file it was called from with a count
155155
t.end()
156156
})
157157

158+
test('should set function name if Error.stackTraceLimit is set to 0', t => {
159+
const stackTraceLimit = Error.stackTraceLimit = 0
160+
161+
const fp = proxyquire('../plugin.js', { stubs: {} })
162+
163+
const fn = fp((fastify, opts, next) => {
164+
next()
165+
})
166+
167+
t.equal(fn[Symbol.for('plugin-meta')].name, 'test-auto-0')
168+
t.equal(fn[Symbol.for('fastify.display-name')], 'test-auto-0')
169+
170+
const fn2 = fp((fastify, opts, next) => {
171+
next()
172+
})
173+
174+
t.equal(fn2[Symbol.for('plugin-meta')].name, 'test-auto-1')
175+
t.equal(fn2[Symbol.for('fastify.display-name')], 'test-auto-1')
176+
177+
Error.stackTraceLimit = stackTraceLimit
178+
t.end()
179+
})
180+
158181
test('should set display-name to meta name', t => {
159182
t.plan(2)
160183

0 commit comments

Comments
 (0)