File tree Expand file tree Collapse file tree 3 files changed +37
-0
lines changed Expand file tree Collapse file tree 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')
66const os = require ( 'node:os' )
77
88const { kState, kOptions, kServerBindings } = require ( './symbols' )
9+ const { FSTWRN003 } = require ( './warnings' )
910const { onListenHookRunner } = require ( './hooks' )
1011const {
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 ) {
Original file line number Diff line number Diff 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
1314const 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+
2028const FSTSEC001 = createWarning ( {
2129 name : 'FastifySecurity' ,
2230 code : 'FSTSEC001' ,
@@ -26,5 +34,6 @@ const FSTSEC001 = createWarning({
2634
2735module . exports = {
2836 FSTWRN001 ,
37+ FSTWRN003 ,
2938 FSTSEC001
3039}
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ const { test } = require('node:test')
44const net = require ( 'node:net' )
55const Fastify = require ( '../fastify' )
66const { once } = require ( 'node:events' )
7+ const { FSTWRN003 } = require ( '../lib/warnings.js' )
78
89function 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+ } )
You can’t perform that action at this time.
0 commit comments