@@ -133,6 +133,7 @@ const {
133133const { isUint8Array } = require ( 'internal/util/types' ) ;
134134const { queueMicrotask } = require ( 'internal/process/task_queues' ) ;
135135const {
136+ deprecate,
136137 guessHandleType,
137138 isWindows,
138139 kEmptyObject,
@@ -2357,7 +2358,27 @@ function setupListenHandle(address, port, addressType, backlog, fd, flags) {
23572358 this ) ;
23582359}
23592360
2360- Server . prototype . _listen2 = setupListenHandle ; // legacy alias
2361+ // Legacy alias for `setupListenHandle`, kept around only because it is an
2362+ // undocumented monkeypatch point. Nothing in core calls it unless it has been
2363+ // overridden, see `callSetupListenHandle`.
2364+ const legacyListen2 = deprecate (
2365+ setupListenHandle ,
2366+ 'Server.prototype._listen2 is deprecated. Use Server.prototype.listen() instead.' ,
2367+ 'DEP0208' ) ;
2368+ Server . prototype . _listen2 = legacyListen2 ;
2369+
2370+ // Set up the listen handle, going through `_listen2` when userland replaced it
2371+ // so that the monkeypatch keeps taking effect (DEP0208). Servers that did not
2372+ // touch `_listen2` must not trigger the deprecation warning.
2373+ function callSetupListenHandle ( server , address , port , addressType , backlog ,
2374+ fd , flags ) {
2375+ if ( server . _listen2 !== legacyListen2 ) {
2376+ server . _listen2 ( address , port , addressType , backlog , fd , flags ) ;
2377+ return ;
2378+ }
2379+ FunctionPrototypeCall ( setupListenHandle , server , address , port , addressType ,
2380+ backlog , fd , flags ) ;
2381+ }
23612382
23622383// A listening TCP Server can be transferred to another thread, which moves the
23632384// underlying listening socket (and its pending accept queue) to that thread's
@@ -2431,9 +2452,8 @@ function listenInCluster(server, address, port, addressType,
24312452
24322453 if ( cluster . isPrimary || exclusive ) {
24332454 // Will create a new handle
2434- // _listen2 sets up the listened handle, it is still named like this
2435- // to avoid breaking code that wraps this method
2436- server . _listen2 ( address , port , addressType , backlog , fd , flags ) ;
2455+ callSetupListenHandle ( server , address , port , addressType , backlog , fd ,
2456+ flags ) ;
24372457 return ;
24382458 }
24392459
@@ -2467,9 +2487,8 @@ function listenInCluster(server, address, port, addressType,
24672487 }
24682488 // Reuse primary's server handle
24692489 server . _handle = handle ;
2470- // _listen2 sets up the listened handle, it is still named like this
2471- // to avoid breaking code that wraps this method
2472- server . _listen2 ( address , port , addressType , backlog , fd , flags ) ;
2490+ callSetupListenHandle ( server , address , port , addressType , backlog , fd ,
2491+ flags ) ;
24732492 }
24742493}
24752494
0 commit comments