File tree 3 files changed +37
-0
lines changed
3 files changed +37
-0
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ const dns = require('node:dns')
6
6
const os = require ( 'node:os' )
7
7
8
8
const { kState, kOptions, kServerBindings } = require ( './symbols' )
9
+ const { FSTWRN003 } = require ( './warnings' )
9
10
const { onListenHookRunner } = require ( './hooks' )
10
11
const {
11
12
FST_ERR_HTTP2_INVALID_VERSION ,
@@ -29,6 +30,10 @@ function createServer (options, httpHandler) {
29
30
cb = undefined
30
31
) {
31
32
if ( typeof cb === 'function' ) {
33
+ if ( cb . constructor . name === 'AsyncFunction' ) {
34
+ FSTWRN003 ( 'listen method' )
35
+ }
36
+
32
37
listenOptions . cb = cb
33
38
}
34
39
if ( listenOptions . signal ) {
Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ const { createWarning } = require('process-warning')
8
8
* - FSTSEC001
9
9
*
10
10
* 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.
11
12
*/
12
13
13
14
const FSTWRN001 = createWarning ( {
@@ -17,6 +18,13 @@ const FSTWRN001 = createWarning({
17
18
unlimited : true
18
19
} )
19
20
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
+
20
28
const FSTSEC001 = createWarning ( {
21
29
name : 'FastifySecurity' ,
22
30
code : 'FSTSEC001' ,
@@ -26,5 +34,6 @@ const FSTSEC001 = createWarning({
26
34
27
35
module . exports = {
28
36
FSTWRN001 ,
37
+ FSTWRN003 ,
29
38
FSTSEC001
30
39
}
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ const { test } = require('node:test')
4
4
const net = require ( 'node:net' )
5
5
const Fastify = require ( '../fastify' )
6
6
const { once } = require ( 'node:events' )
7
+ const { FSTWRN003 } = require ( '../lib/warnings.js' )
7
8
8
9
function createDeferredPromise ( ) {
9
10
const promise = { }
@@ -97,3 +98,25 @@ test('same port conflict and success should not fire callback multiple times - p
97
98
await fastify . listen ( )
98
99
await fastify . close ( )
99
100
} )
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
+ } )
You can’t perform that action at this time.
0 commit comments