const { pipeline } = require('node:stream');

pipeline(upstreamResponse, clientResponse, (error) => {
  if (error) console.error('Proxy response failed:', error);
});

Use this body path after target selection, the upstream request and downstream status/header preparation. Node streams normally supply Buffers; do not accumulate them into a string for images or other unchanged binary content.

pipeline coordinates backpressure and propagates failures. It may destroy the destination on error, so do not assume another HTTP error response can be sent afterward. This fragment is not a full proxy server.

For pass-through bytes, preserve relevant representation metadata such as media type and content encoding while handling hop-specific fields through forwarding rules. A comparison of gzip bytes and matching response headers shows the encoded-versus-decoded representation choice.

Deliberate HTML rewriting needs another path: recognize its media type, handle compression and charset, transform text and generate metadata for the new bytes. The old Content-Length cannot describe a changed body.

Buffer.concat can preserve binary content when full buffering is required, but stores the entire body in memory. A pass-through image stream does not need that cost.

متعلقہ مضامین