@@ -7,6 +7,7 @@ import type { URL } from 'url';
7
7
8
8
import type { Socket } from './socket' ;
9
9
import { badGatewayStatusCodes , createCustomStatusHttpResponse , errorCodeToStatusCode } from './statuses' ;
10
+ import type { SocketPreviousStats } from './utils/count_target_bytes' ;
10
11
import { countTargetBytes } from './utils/count_target_bytes' ;
11
12
import { getBasicAuthorizationHeader } from './utils/get_basic' ;
12
13
@@ -85,9 +86,15 @@ export const chain = (
85
86
const fn = proxy . protocol === 'https:' ? https . request : http . request ;
86
87
const client = fn ( proxy . origin , options as unknown as http . ClientRequestArgs ) ;
87
88
88
- client . on ( 'connect' , ( response , targetSocket , clientHead ) => {
89
+ client . once ( 'socket' , ( targetSocket : Socket & SocketPreviousStats ) => {
90
+ // socket can be re-used by multiple requests (HTTP keep alive)
91
+ // (even in multiple Server objects)
92
+ targetSocket . previousBytesRead = targetSocket . bytesRead ;
93
+ targetSocket . previousBytesWritten = targetSocket . bytesWritten ;
89
94
countTargetBytes ( sourceSocket , targetSocket ) ;
95
+ } ) ;
90
96
97
+ client . on ( 'connect' , ( response , targetSocket , clientHead ) => {
91
98
if ( sourceSocket . readyState !== 'open' ) {
92
99
// Sanity check, should never reach.
93
100
targetSocket . destroy ( ) ;
0 commit comments