| 1 | # mimic-response [](https://travis-ci.com/sindresorhus/mimic-response)
|
|---|
| 2 |
|
|---|
| 3 | > Mimic a [Node.js HTTP response stream](https://nodejs.org/api/http.html#http_class_http_incomingmessage)
|
|---|
| 4 |
|
|---|
| 5 | ## Install
|
|---|
| 6 |
|
|---|
| 7 | ```
|
|---|
| 8 | $ npm install mimic-response
|
|---|
| 9 | ```
|
|---|
| 10 |
|
|---|
| 11 | ## Usage
|
|---|
| 12 |
|
|---|
| 13 | ```js
|
|---|
| 14 | const stream = require('stream');
|
|---|
| 15 | const mimicResponse = require('mimic-response');
|
|---|
| 16 |
|
|---|
| 17 | const responseStream = getHttpResponseStream();
|
|---|
| 18 | const myStream = new stream.PassThrough();
|
|---|
| 19 |
|
|---|
| 20 | mimicResponse(responseStream, myStream);
|
|---|
| 21 |
|
|---|
| 22 | console.log(myStream.statusCode);
|
|---|
| 23 | //=> 200
|
|---|
| 24 | ```
|
|---|
| 25 |
|
|---|
| 26 | ## API
|
|---|
| 27 |
|
|---|
| 28 | ### mimicResponse(from, to)
|
|---|
| 29 |
|
|---|
| 30 | **Note #1:** The `from.destroy(error)` function is not proxied. You have to call it manually:
|
|---|
| 31 |
|
|---|
| 32 | ```js
|
|---|
| 33 | const stream = require('stream');
|
|---|
| 34 | const mimicResponse = require('mimic-response');
|
|---|
| 35 |
|
|---|
| 36 | const responseStream = getHttpResponseStream();
|
|---|
| 37 |
|
|---|
| 38 | const myStream = new stream.PassThrough({
|
|---|
| 39 | destroy(error, callback) {
|
|---|
| 40 | responseStream.destroy();
|
|---|
| 41 |
|
|---|
| 42 | callback(error);
|
|---|
| 43 | }
|
|---|
| 44 | });
|
|---|
| 45 |
|
|---|
| 46 | myStream.destroy();
|
|---|
| 47 | ```
|
|---|
| 48 |
|
|---|
| 49 | Please note that `myStream` and `responseStream` never throws. The error is passed to the request instead.
|
|---|
| 50 |
|
|---|
| 51 | #### from
|
|---|
| 52 |
|
|---|
| 53 | Type: `Stream`
|
|---|
| 54 |
|
|---|
| 55 | [Node.js HTTP response stream.](https://nodejs.org/api/http.html#http_class_http_incomingmessage)
|
|---|
| 56 |
|
|---|
| 57 | #### to
|
|---|
| 58 |
|
|---|
| 59 | Type: `Stream`
|
|---|
| 60 |
|
|---|
| 61 | Any stream.
|
|---|
| 62 |
|
|---|
| 63 | ## Related
|
|---|
| 64 |
|
|---|
| 65 | - [mimic-fn](https://github.com/sindresorhus/mimic-fn) - Make a function mimic another one
|
|---|
| 66 | - [clone-response](https://github.com/lukechilds/clone-response) - Clone a Node.js response stream
|
|---|
| 67 |
|
|---|
| 68 | ---
|
|---|
| 69 |
|
|---|
| 70 | <div align="center">
|
|---|
| 71 | <b>
|
|---|
| 72 | <a href="https://tidelift.com/subscription/pkg/npm-mimic-response?utm_source=npm-mimic-response&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
|
|---|
| 73 | </b>
|
|---|
| 74 | <br>
|
|---|
| 75 | <sub>
|
|---|
| 76 | Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
|
|---|
| 77 | </sub>
|
|---|
| 78 | </div>
|
|---|