Registry / http-networking / mimic-response

mimic-response

JSON →
library4.0.0jsnpmunverified

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.

npm install mimic-response
INSTALL
IMPORT
SIG · MIMIC-RESPONSE
M
mimic-response
http-networkingjavascriptv4.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

mimicResponse
import mimicResponse from 'mimic-response';
const mimicResponse = require('mimic-response');
The `mimic-response` package is pure ESM since v4.0.0. CommonJS users must either migrate their project to ESM or use dynamic import with `await import('mimic-response')`.
mimicResponse
import mimicResponse from 'mimic-response';
import { mimicResponse } from 'mimic-response';
`mimicResponse` is the default export of the package and should not be imported as a named export.
mimicResponse (type)
import type mimicResponse from 'mimic-response';
import type { mimicResponse } from 'mimic-response';
The package includes TypeScript definitions. As `mimicResponse` is a default export, its type import should follow the default import syntax.

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'));
Debug
Known issues
breakingVersion 4.0.0 of `mimic-response` is a pure ESM package. This means it can only be imported using `import` statements in ES modules, or dynamically with `await import()` in CommonJS. Direct `require()` calls are no longer supported.
fix
Migrate your project to use ES modules by adding `"type": "module"` to your `package.json` and using `import` statements, or ensure your consuming code is already an ES module. For existing CommonJS projects that cannot migrate, use `await import('mimic-response')` for dynamic loading.
affects: >=4.0.0
breakingVersion 4.0.0 increased the minimum Node.js requirement to `^12.20.0 || ^14.13.1 || >=16.0.0`.
fix
Ensure your Node.js environment meets the minimum version requirement. Upgrade Node.js if necessary.
affects: >=4.0.0
breakingVersion 3.0.0 increased the minimum Node.js requirement to Node.js 10.
fix
Ensure your Node.js environment is at least version 10.x. Upgrade Node.js if necessary, or downgrade `mimic-response` to a v2.x version if strict Node.js 8.x compatibility is required.
affects: >=3.0.0 <4.0.0
breakingVersion 2.0.0 increased the minimum Node.js requirement to Node.js 8.
fix
Ensure your Node.js environment is at least version 8.x. Upgrade Node.js if necessary, or use `mimic-response` v1.x for older Node.js compatibility.
affects: >=2.0.0 <3.0.0
gotchaThe `destroy(error)` function of the source stream (`from`) is not automatically proxied to the target stream (`to`). If you need to propagate destroy calls, you must manually implement the `destroy` method on your target stream and call `from.destroy(error)` within it.
fix
When creating your target stream, provide a `destroy` method in its constructor options that explicitly calls the `destroy` method of the `from` stream, as shown in the package's API documentation example.
affects: >=1.0.0
Errors
Common errors & fixes
Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/node_modules/mimic-response/index.js from /your/project/index.js not supported.
Attempting to use `require()` to import `mimic-response` version 4.0.0 or higher in a CommonJS module.
fix
Refactor your consuming code to use `import` syntax within an ES module (by adding `"type": "module"` to `package.json` or using `.mjs` files), or use dynamic `import` like `const mimicResponse = (await import('mimic-response')).default;`.
SyntaxError: Cannot use import statement outside a module
Using `import` syntax for `mimic-response` v4.0.0+ in a Node.js file that is not treated as an ES module.
fix
Add `"type": "module"` to your `package.json` file, or rename your file to have a `.mjs` extension. Ensure your Node.js version is compatible (Node.js 12.20.0 or higher for v4.x).
TypeError: Cannot read properties of undefined (reading 'statusCode') OR TypeError: mimicResponse is not a function
Incorrectly attempting to import `mimicResponse` as a named export (e.g., `import { mimicResponse } from 'mimic-response';`) instead of a default export.
fix
Use the correct default import syntax: `import mimicResponse from 'mimic-response';`.
TypeError: myStream.destroy is not a function (when calling myStream.destroy())
Calling `destroy()` on a target stream (`myStream`) without it having a custom `destroy` method, specifically when you intend for the destroy call to be propagated to the source stream.
fix
Implement a `destroy` method in the constructor options of your target stream (e.g., `new PassThroughStream({ destroy(error, callback) { /* ... */ } })`) that also calls `from.destroy(error)` if propagation is desired.
Upgrade
Version history
4.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
mimic-response — npm install mimic-response · libregistry