1 | # clone-response
|
2 |
|
3 | > Clone a Node.js HTTP response stream
|
4 |
|
5 | [![Build Status](https://travis-ci.org/lukechilds/clone-response.svg?branch=master)](https://travis-ci.org/lukechilds/clone-response)
|
6 | [![Coverage Status](https://coveralls.io/repos/github/lukechilds/clone-response/badge.svg?branch=master)](https://coveralls.io/github/lukechilds/clone-response?branch=master)
|
7 | [![npm](https://img.shields.io/npm/dm/clone-response.svg)](https://www.npmjs.com/package/clone-response)
|
8 | [![npm](https://img.shields.io/npm/v/clone-response.svg)](https://www.npmjs.com/package/clone-response)
|
9 |
|
10 | Returns a new stream and copies over all properties and methods from the original response giving you a complete duplicate.
|
11 |
|
12 | This is useful in situations where you need to consume the response stream but also want to pass an unconsumed stream somewhere else to be consumed later.
|
13 |
|
14 | ## Install
|
15 |
|
16 | ```shell
|
17 | npm install --save clone-response
|
18 | ```
|
19 |
|
20 | ## Usage
|
21 |
|
22 | ```js
|
23 | const http = require('http');
|
24 | const cloneResponse = require('clone-response');
|
25 |
|
26 | http.get('http://example.com', response => {
|
27 | const clonedResponse = cloneResponse(response);
|
28 | response.pipe(process.stdout);
|
29 |
|
30 | setImmediate(() => {
|
31 | // The response stream has already been consumed by the time this executes,
|
32 | // however the cloned response stream is still available.
|
33 | doSomethingWithResponse(clonedResponse);
|
34 | });
|
35 | });
|
36 | ```
|
37 |
|
38 | Please bear in mind that the process of cloning a stream consumes it. However, you can consume a stream multiple times in the same tick, therefore allowing you to create multiple clones. e.g:
|
39 |
|
40 | ```js
|
41 | const clone1 = cloneResponse(response);
|
42 | const clone2 = cloneResponse(response);
|
43 | // response can still be consumed in this tick but cannot be consumed if passed
|
44 | // into any async callbacks. clone1 and clone2 can be passed around and be
|
45 | // consumed in the future.
|
46 | ```
|
47 |
|
48 | ## API
|
49 |
|
50 | ### cloneResponse(response)
|
51 |
|
52 | Returns a clone of the passed in response.
|
53 |
|
54 | #### response
|
55 |
|
56 | Type: `stream`
|
57 |
|
58 | A [Node.js HTTP response stream](https://nodejs.org/api/http.html#http_class_http_incomingmessage) to clone.
|
59 |
|
60 | ## License
|
61 |
|
62 | MIT © Luke Childs
|