{"library":"mimic-response","title":"Mimic Node.js HTTP Response Stream Properties","description":"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.","language":"javascript","status":"active","last_verified":"Tue Apr 21","install":{"commands":["npm install mimic-response"],"cli":null},"imports":["import mimicResponse from 'mimic-response';","import mimicResponse from 'mimic-response';","import type mimicResponse from 'mimic-response';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { PassThrough as PassThroughStream } from 'node:stream';\nimport mimicResponse from 'mimic-response';\n\nfunction getHttpResponseStream() {\n  // Simulate an http.IncomingMessage with relevant properties\n  const response = new PassThroughStream();\n  Object.defineProperty(response, 'statusCode', { value: 200, configurable: true });\n  Object.defineProperty(response, 'statusMessage', { value: 'OK', configurable: true });\n  Object.defineProperty(response, 'headers', { value: { 'content-type': 'application/json' }, configurable: true });\n  Object.defineProperty(response, 'rawHeaders', { value: ['Content-Type', 'application/json'], configurable: true });\n  Object.defineProperty(response, 'trailers', { value: {}, configurable: true });\n  Object.defineProperty(response, 'rawTrailers', { value: [], configurable: true });\n  return response;\n}\n\nconst responseStream = getHttpResponseStream();\nconst myStream = new PassThroughStream();\n\nmimicResponse(responseStream, myStream);\n\nconsole.log('Original status code:', responseStream.statusCode);\nconsole.log('Mimicked status code:', myStream.statusCode);\n\n// Demonstrating the 'destroy' caveat\nconst myStreamWithDestroy = new PassThroughStream({\n  destroy(error, callback) {\n    console.log('myStreamWithDestroy destroy called with error:', error ? error.message : 'none');\n    responseStream.destroy(error); // Ensure the source stream's destroy is also called\n    callback(error);\n  }\n});\n\nmimicResponse(responseStream, myStreamWithDestroy);\n// Simulate an error causing the stream to destroy\nmyStreamWithDestroy.destroy(new Error('Simulated stream error'));","lang":"typescript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}