do gzip
This commit is contained in:
@@ -245,11 +245,15 @@ class RPCProxy {
|
|||||||
try {
|
try {
|
||||||
// Create fresh client for this request
|
// Create fresh client for this request
|
||||||
const client = this.createClient(this.streamEndpoint);
|
const client = this.createClient(this.streamEndpoint);
|
||||||
|
|
||||||
|
// Get the original Accept-Encoding from the client request
|
||||||
|
const acceptEncoding = res.req.headers['accept-encoding'] || 'identity';
|
||||||
|
|
||||||
const response = await client.post('/', requestBody, {
|
const response = await client.post('/', requestBody, {
|
||||||
responseType: 'stream',
|
responseType: 'stream',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Accept-Encoding': 'identity', // Request uncompressed responses
|
'Accept-Encoding': acceptEncoding, // Forward client's encoding preference
|
||||||
},
|
},
|
||||||
validateStatus: (status) => true, // Don't throw on any status
|
validateStatus: (status) => true, // Don't throw on any status
|
||||||
});
|
});
|
||||||
@@ -274,9 +278,8 @@ class RPCProxy {
|
|||||||
res.setHeader('Keep-Alive', `timeout=${Math.floor(config.requestTimeout / 1000)}`);
|
res.setHeader('Keep-Alive', `timeout=${Math.floor(config.requestTimeout / 1000)}`);
|
||||||
|
|
||||||
Object.entries(response.headers).forEach(([key, value]) => {
|
Object.entries(response.headers).forEach(([key, value]) => {
|
||||||
// Remove content-encoding since we're requesting uncompressed
|
|
||||||
// Don't override Connection header we just set
|
// Don't override Connection header we just set
|
||||||
if (key.toLowerCase() !== 'content-encoding' && key.toLowerCase() !== 'connection') {
|
if (key.toLowerCase() !== 'connection') {
|
||||||
res.setHeader(key, value);
|
res.setHeader(key, value);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -297,9 +300,10 @@ class RPCProxy {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Capture and stream the response
|
// Capture and stream the response
|
||||||
|
const chunks = [];
|
||||||
response.data.on('data', (chunk) => {
|
response.data.on('data', (chunk) => {
|
||||||
// Always capture data for comparison purposes
|
// Always capture raw chunks for comparison
|
||||||
responseData += chunk.toString();
|
chunks.push(chunk);
|
||||||
|
|
||||||
// Only write to client if still connected
|
// Only write to client if still connected
|
||||||
if (!isClientClosed() && !res.writableEnded) {
|
if (!isClientClosed() && !res.writableEnded) {
|
||||||
@@ -330,19 +334,25 @@ class RPCProxy {
|
|||||||
|
|
||||||
const totalTime = Date.now() - startTime;
|
const totalTime = Date.now() - startTime;
|
||||||
|
|
||||||
|
// Combine chunks and convert to string for logging
|
||||||
|
const rawData = Buffer.concat(chunks);
|
||||||
|
responseData = rawData.toString('utf8');
|
||||||
|
|
||||||
logger.info({
|
logger.info({
|
||||||
requestId,
|
requestId,
|
||||||
endpoint: 'stream',
|
endpoint: 'stream',
|
||||||
totalTimeMs: totalTime,
|
totalTimeMs: totalTime,
|
||||||
responseSize: responseData.length,
|
responseSize: rawData.length,
|
||||||
|
contentEncoding: response.headers['content-encoding'],
|
||||||
clientClosed: isClientClosed(),
|
clientClosed: isClientClosed(),
|
||||||
}, 'Stream response completed');
|
}, 'Stream response completed');
|
||||||
|
|
||||||
resolve({
|
resolve({
|
||||||
statusCode,
|
statusCode,
|
||||||
data: responseData,
|
data: responseData,
|
||||||
size: responseData.length,
|
size: rawData.length,
|
||||||
latency: totalTime,
|
latency: totalTime,
|
||||||
|
contentEncoding: response.headers['content-encoding'],
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -386,7 +396,7 @@ class RPCProxy {
|
|||||||
const response = await client.post('/', requestBody, {
|
const response = await client.post('/', requestBody, {
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Accept-Encoding': 'identity', // Request uncompressed responses
|
'Accept-Encoding': 'gzip, deflate', // Accept compressed responses for comparison
|
||||||
},
|
},
|
||||||
validateStatus: (status) => true, // Don't throw on any status
|
validateStatus: (status) => true, // Don't throw on any status
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user