Mimic Node.js HTTP Response Stream Properties

4.0.0 · active · verified Tue Apr 21

The `mimic-response` library is a utility designed to replicate properties and events from a Node.js `http.IncomingMessage` (an HTTP response stream) onto any generic Node.js stream. This is particularly useful in scenarios like proxying or transforming HTTP responses where the downstream stream needs to inherit metadata such as `statusCode`, `statusMessage`, and `headers` without copying the actual data payload. The current stable version is `4.0.0`. Historically, new major versions have been released roughly annually, typically coinciding with updated Node.js LTS requirements and ecosystem shifts like the move to pure ESM. Its key differentiator lies in its specific focus on mirroring *response stream properties* rather than content, providing a lightweight way to maintain HTTP context across stream transformations. It stands apart from related tools like `mimic-fn` (for functions) or broader stream cloning utilities by targeting the specific metadata of an HTTP response.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to use `mimicResponse` to transfer HTTP response metadata like `statusCode` and `headers` from a source stream (simulated `IncomingMessage`) to a target `PassThroughStream`, and also illustrates the important `destroy` proxying caveat.

import { PassThrough as PassThroughStream } from 'node:stream';
import mimicResponse from 'mimic-response';

function getHttpResponseStream() {
  // Simulate an http.IncomingMessage with relevant properties
  const response = new PassThroughStream();
  Object.defineProperty(response, 'statusCode', { value: 200, configurable: true });
  Object.defineProperty(response, 'statusMessage', { value: 'OK', configurable: true });
  Object.defineProperty(response, 'headers', { value: { 'content-type': 'application/json' }, configurable: true });
  Object.defineProperty(response, 'rawHeaders', { value: ['Content-Type', 'application/json'], configurable: true });
  Object.defineProperty(response, 'trailers', { value: {}, configurable: true });
  Object.defineProperty(response, 'rawTrailers', { value: [], configurable: true });
  return response;
}

const responseStream = getHttpResponseStream();
const myStream = new PassThroughStream();

mimicResponse(responseStream, myStream);

console.log('Original status code:', responseStream.statusCode);
console.log('Mimicked status code:', myStream.statusCode);

// Demonstrating the 'destroy' caveat
const myStreamWithDestroy = new PassThroughStream({
  destroy(error, callback) {
    console.log('myStreamWithDestroy destroy called with error:', error ? error.message : 'none');
    responseStream.destroy(error); // Ensure the source stream's destroy is also called
    callback(error);
  }
});

mimicResponse(responseStream, myStreamWithDestroy);
// Simulate an error causing the stream to destroy
myStreamWithDestroy.destroy(new Error('Simulated stream error'));

view raw JSON →