Skip to content

Commit 0439dd1

Browse files
authored
feat: add listen async callback warning (fastify#6011)
1 parent 40ea7d8 commit 0439dd1

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

lib/server.js

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const dns = require('node:dns')
66
const os = require('node:os')
77

88
const { kState, kOptions, kServerBindings } = require('./symbols')
9+
const { FSTWRN003 } = require('./warnings')
910
const { onListenHookRunner } = require('./hooks')
1011
const {
1112
FST_ERR_HTTP2_INVALID_VERSION,
@@ -29,6 +30,10 @@ function createServer (options, httpHandler) {
2930
cb = undefined
3031
) {
3132
if (typeof cb === 'function') {
33+
if (cb.constructor.name === 'AsyncFunction') {
34+
FSTWRN003('listen method')
35+
}
36+
3237
listenOptions.cb = cb
3338
}
3439
if (listenOptions.signal) {

lib/warnings.js

+9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const { createWarning } = require('process-warning')
88
* - FSTSEC001
99
*
1010
* Deprecation Codes FSTDEP001 - FSTDEP021 were used by v4 and MUST NOT not be reused.
11+
* Warning Codes FSTWRN001 - FSTWRN002 were used by v4 and MUST NOT not be reused.
1112
*/
1213

1314
const FSTWRN001 = createWarning({
@@ -17,6 +18,13 @@ const FSTWRN001 = createWarning({
1718
unlimited: true
1819
})
1920

21+
const FSTWRN003 = createWarning({
22+
name: 'FastifyWarning',
23+
code: 'FSTWRN003',
24+
message: 'The %s mixes async and callback styles that may lead to unhandled rejections. Please use only one of them.',
25+
unlimited: true
26+
})
27+
2028
const FSTSEC001 = createWarning({
2129
name: 'FastifySecurity',
2230
code: 'FSTSEC001',
@@ -26,5 +34,6 @@ const FSTSEC001 = createWarning({
2634

2735
module.exports = {
2836
FSTWRN001,
37+
FSTWRN003,
2938
FSTSEC001
3039
}

test/listen.5.test.js

+23
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { test } = require('node:test')
44
const net = require('node:net')
55
const Fastify = require('../fastify')
66
const { once } = require('node:events')
7+
const { FSTWRN003 } = require('../lib/warnings.js')
78

89
function createDeferredPromise () {
910
const promise = {}
@@ -97,3 +98,25 @@ test('same port conflict and success should not fire callback multiple times - p
9798
await fastify.listen()
9899
await fastify.close()
99100
})
101+
102+
test('should emit a warning when using async callback', (t, done) => {
103+
t.plan(2)
104+
105+
process.on('warning', onWarning)
106+
function onWarning (warning) {
107+
t.assert.strictEqual(warning.name, 'FastifyWarning')
108+
t.assert.strictEqual(warning.code, FSTWRN003.code)
109+
}
110+
111+
const fastify = Fastify()
112+
113+
t.after(async () => {
114+
await fastify.close()
115+
process.removeListener('warning', onWarning)
116+
FSTWRN003.emitted = false
117+
})
118+
119+
fastify.listen({ port: 0 }, async function doNotUseAsyncCallback () {
120+
done()
121+
})
122+
})

0 commit comments

Comments
 (0)