Mock HTTP Response Stream
Responselike provides a streamable object designed to mimic a Node.js HTTP response stream (`http.IncomingMessage`). This utility is primarily used for mocking HTTP responses in testing scenarios or for reformatting cached data to be consumed by code expecting a standard HTTP response. The current stable version is 4.0.2. The package is actively maintained by Sindre Sorhus, with releases typically occurring as needed for bug fixes or Node.js version updates, as seen with the recent v4.0.0 and v4.0.1/v4.0.2 updates. Its key differentiator is its focus on accurately replicating the streamable nature and properties of a native HTTP response, making it suitable for integrations where stream-based consumption is expected, unlike simpler object mocks.
Common errors
-
ERR_REQUIRE_ESM
cause Attempting to `require()` responselike in an ESM project, or attempting to `require()` a pure ESM package in a CommonJS context.fixUse `import Response from 'responselike';` instead of `const Response = require('responselike');`. Ensure your project is set up for ESM, potentially by adding `"type": "module"` to your `package.json`. -
TypeError: Cannot read properties of undefined (reading 'pipe')
cause The `body` property passed to the `Response` constructor was not a `Buffer` or was missing, leading to an unstreamable object.fixEnsure the `body` option is explicitly set to a `Buffer.from('your content')` when creating a new `Response` instance. -
TypeError: responselike_1.default is not a constructor
cause Incorrectly importing `Response` as a named export or attempting to use `new` on the module itself when it's a default export.fixCorrect the import statement to `import Response from 'responselike';` (for ESM) or verify your CommonJS wrapper if using older Node.js versions.
Warnings
- breaking Version 4.0.0 and newer require Node.js version 20 or higher.
- breaking Version 3.0.0 introduced breaking changes, requiring Node.js 14 and transitioning the package to pure ESM. CommonJS `require()` is no longer supported.
- breaking Since v3.0.0, the `Response` constructor now accepts a single `options` object instead of multiple positional arguments.
- gotcha When providing headers, keys will be automatically lowercased by the `Response` object to mimic standard HTTP header behavior.
Install
-
npm install responselike -
yarn add responselike -
pnpm add responselike
Imports
- Response
const Response = require('responselike');import Response from 'responselike';
- ResponseOptions
import type { ResponseOptions } from 'responselike';
Quickstart
import Response from 'responselike';
import { Writable } from 'stream';
class TestStream extends Writable {
_write(chunk, encoding, callback) {
console.log(chunk.toString());
callback();
}
}
const response = new Response({
statusCode: 200,
headers: {
'content-type': 'text/plain',
'x-custom-header': 'value'
},
body: Buffer.from('Hello, World! This is a mocked response body.'),
url: 'https://example.com/api/data'
});
console.log(`Status Code: ${response.statusCode}`);
console.log(`URL: ${response.url}`);
console.log('Headers:', response.headers);
const testOutput = new TestStream();
console.log('\nStreaming body content:');
response.pipe(testOutput);