Skip to content

Commit 03df862

Browse files
committed
fixed traffic stats for chain
1 parent 697e542 commit 03df862

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/chain.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { URL } from 'url';
77

88
import type { Socket } from './socket';
99
import { badGatewayStatusCodes, createCustomStatusHttpResponse, errorCodeToStatusCode } from './statuses';
10+
import type { SocketPreviousStats } from './utils/count_target_bytes';
1011
import { countTargetBytes } from './utils/count_target_bytes';
1112
import { getBasicAuthorizationHeader } from './utils/get_basic';
1213

@@ -85,9 +86,15 @@ export const chain = (
8586
const fn = proxy.protocol === 'https:' ? https.request : http.request;
8687
const client = fn(proxy.origin, options as unknown as http.ClientRequestArgs);
8788

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;
8994
countTargetBytes(sourceSocket, targetSocket);
95+
});
9096

97+
client.on('connect', (response, targetSocket, clientHead) => {
9198
if (sourceSocket.readyState !== 'open') {
9299
// Sanity check, should never reach.
93100
targetSocket.destroy();

0 commit comments

Comments
 (0)