Clone HTTP Response Stream

2.0.0 · active · verified Tue Apr 21

The `clone-response` package provides a utility function to duplicate a Node.js HTTP response stream, effectively creating an unconsumed copy. This is particularly useful in scenarios where a response stream needs to be processed in multiple asynchronous contexts or passed to different consumers without fully draining the original stream for all parties. The package ensures that all properties and methods from the original `IncomingMessage` are copied to the new stream, creating a complete duplicate. As of version 2.0.0, it is a pure ESM module and requires Node.js 14 or higher. The package maintains a stable release cadence, primarily updating for Node.js compatibility and modern JavaScript module standards. Its core differentiator is the reliable duplication of a response stream's state and data, enabling flexible handling of HTTP responses without complex stream buffering or re-fetching.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to clone an HTTP response stream immediately after receiving it, allowing the original to be piped to `process.stdout` while the cloned stream remains available for later asynchronous consumption.

import http from 'node:http';
import cloneResponse from 'clone-response';

http.get('http://example.com', response => {
	const clonedResponse = cloneResponse(response);
	response.pipe(process.stdout);

	setImmediate(() => {
		// The response stream has already been consumed by the time this executes,
		// however the cloned response stream is still available.
		// In a real application, you would consume clonedResponse here.
		console.log('Cloned response is still available for consumption.');
		// Example: clonedResponse.on('data', chunk => console.log('Cloned data:', chunk.toString()));
	});
});

view raw JSON →