Skip to content

Commit

Permalink
fixed traffic stats for chain
Browse files Browse the repository at this point in the history
  • Loading branch information
peonone committed Feb 13, 2025
1 parent 697e542 commit bbe7cee
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import type { EventEmitter } from 'events';
import http from 'http';
import https from 'https';
import type { URL } from 'url';

import type { Socket } from './socket';
import { badGatewayStatusCodes, createCustomStatusHttpResponse, errorCodeToStatusCode } from './statuses';
import { countTargetBytes } from './utils/count_target_bytes';
import { countTargetBytes, SocketPreviousStats } from './utils/count_target_bytes';
import { getBasicAuthorizationHeader } from './utils/get_basic';

interface Options {
Expand Down Expand Up @@ -85,9 +84,15 @@ export const chain = (
const fn = proxy.protocol === 'https:' ? https.request : http.request;
const client = fn(proxy.origin, options as unknown as http.ClientRequestArgs);

client.on('connect', (response, targetSocket, clientHead) => {
client.once('socket', (targetSocket: Socket & SocketPreviousStats) => {
// socket can be re-used by multiple requests (HTTP keep alive)
// (even in multiple Server objects)
targetSocket.previousBytesRead = targetSocket.bytesRead;
targetSocket.previousBytesWritten = targetSocket.bytesWritten;
countTargetBytes(sourceSocket, targetSocket);
});

client.on('connect', (response, targetSocket, clientHead) => {
if (sourceSocket.readyState !== 'open') {
// Sanity check, should never reach.
targetSocket.destroy();
Expand Down

0 comments on commit bbe7cee

Please sign in to comment.