@@ -34,6 +34,9 @@ import { serveStatic } from '@hono/node-server/serve-static';
3434import { createServer as createHttpsServer } from 'node:https' ;
3535import type { IncomingMessage , ServerResponse } from 'node:http' ;
3636import type { Socket } from 'node:net' ;
37+
38+ /** The responses one open connection still owes, for the drain in {@link HonoHttpServer.close}. */
39+ type OwedResponses = Set < ServerResponse > ;
3740import { matchesRoutePattern } from './route-pattern' ;
3841import { readRequestBody , type RequestBodyReading , type RequestBodyRefusal } from './request-body' ;
3942// The ADR-0112 wire vocabulary, read as DATA rather than restated: `ErrorCode`
@@ -591,7 +594,7 @@ export class HonoHttpServer implements IHttpServer {
591594 * closes, so a response that will never be written (one pipelined behind
592595 * another on a connection that died) is not held past its connection.
593596 */
594- private inFlightResponses = new Map < Socket , Set < ServerResponse > > ( ) ;
597+ private inFlightResponses = new Map < Socket , OwedResponses > ( ) ;
595598 /**
596599 * Every `(method, pattern)` pair registered through this server, kept so
597600 * the `notFound` handler can answer "the path exists but the method is
@@ -1891,16 +1894,7 @@ export class HonoHttpServer implements IHttpServer {
18911894 // `req.socket`, not `res.socket`: a pipelined response waiting its turn
18921895 // has no socket yet.
18931896 const socket = req . socket ;
1894- let pending = this . inFlightResponses . get ( socket ) ;
1895- if ( ! pending ) {
1896- const owed = new Set < ServerResponse > ( ) ;
1897- pending = owed ;
1898- this . inFlightResponses . set ( socket , owed ) ;
1899- socket . once ( 'close' , ( ) => {
1900- if ( this . inFlightResponses . get ( socket ) === owed ) this . inFlightResponses . delete ( socket ) ;
1901- } ) ;
1902- }
1903- const owed = pending ;
1897+ const owed : OwedResponses = this . responsesOwedOn ( socket ) ;
19041898 owed . add ( res ) ;
19051899 res . once ( 'close' , ( ) => {
19061900 owed . delete ( res ) ;
@@ -1910,6 +1904,18 @@ export class HonoHttpServer implements IHttpServer {
19101904 } ) ;
19111905 }
19121906
1907+ /** The {@link inFlightResponses} entry for one connection, made on its first request and dropped when it closes. */
1908+ private responsesOwedOn ( socket : Socket ) : OwedResponses {
1909+ const known : OwedResponses | undefined = this . inFlightResponses . get ( socket ) ;
1910+ if ( known ) return known ;
1911+ const created : OwedResponses = new Set ( ) ;
1912+ this . inFlightResponses . set ( socket , created ) ;
1913+ socket . once ( 'close' , ( ) => {
1914+ if ( this . inFlightResponses . get ( socket ) === created ) this . inFlightResponses . delete ( socket ) ;
1915+ } ) ;
1916+ return created ;
1917+ }
1918+
19131919 getPort ( ) {
19141920 return this . listeningPort || this . port ;
19151921 }
0 commit comments