UNPKG

2.09 kBMarkdownView Raw
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
10Returns a new stream and copies over all properties and methods from the original response giving you a complete duplicate.
11
12This 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
17npm install --save clone-response
18```
19
20## Usage
21
22```js
23const http = require('http');
24const cloneResponse = require('clone-response');
25
26http.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
38Please 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
41const clone1 = cloneResponse(response);
42const 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
52Returns a clone of the passed in response.
53
54#### response
55
56Type: `stream`
57
58A [Node.js HTTP response stream](https://nodejs.org/api/http.html#http_class_http_incomingmessage) to clone.
59
60## License
61
62MIT © Luke Childs