Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

clone-response

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

clone-response - npm Package Compare versions

Comparing version 0.2.2 to 1.0.0

10

package.json
{
"name": "clone-response",
"version": "0.2.2",
"description": "Clone a Node.js response object",
"version": "1.0.0",
"description": "Clone a Node.js HTTP response stream",
"main": "src/index.js",

@@ -21,3 +21,5 @@ "scripts": {

"copy",
"response"
"response",
"HTTP",
"stream"
],

@@ -36,3 +38,3 @@ "author": "Luke Childs <lukechilds123@gmail.com> (http://lukechilds.co.uk)",

"coveralls": "^2.13.1",
"create-test-server": "^0.1.0",
"create-test-server": "^1.1.1",
"eslint-config-xo-lukechilds": "^1.0.0",

@@ -39,0 +41,0 @@ "get-stream": "^3.0.0",

46

README.md
# clone-response
> Clone a Node.js response object
> Clone a Node.js HTTP response stream

@@ -9,2 +9,6 @@ [![Build Status](https://travis-ci.org/lukechilds/clone-response.svg?branch=master)](https://travis-ci.org/lukechilds/clone-response)

Returns a new stream and copies over all properties and methods from the original response giving you a complete duplicate.
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.
## Install

@@ -16,4 +20,44 @@

## Usage
```js
const http = require('http');
const cloneResponse = require('clone-response');
http.get('http://example.com', response => {
const clonedResponse = cloneResponse(response);
response.pipe(process.stdout);
setImmediate(() => {
// The response stream has already been consumed by the time this executes,
// however the cloned response stream is still available.
doSomethingWithResponse(clonedResponse);
});
});
```
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:
```js
const clone1 = cloneResponse(response);
const clone2 = cloneResponse(response);
// response can still be consumed in this tick but cannot be consumed if passed
// into any async callbacks. clone1 and clone2 can be passed around and be
// consumed in the future.
```
## API
### cloneResponse(response)
Returns a clone of the passed in response.
#### response
Type: `stream`
A [Node.js HTTP response stream](https://nodejs.org/api/http.html#http_class_http_incomingmessage) to clone.
## License
MIT © Luke Childs

@@ -7,2 +7,6 @@ 'use strict';

const cloneResponse = response => {
if (!(response && response.pipe)) {
throw new TypeError('Parameter `response` must be a response stream.');
}
const clone = new PassThrough();

@@ -9,0 +13,0 @@ mimicResponse(response, clone);

import http from 'http';
import { PassThrough } from 'stream';
import test from 'ava';

@@ -25,2 +26,14 @@ import pify from 'pify';

test('returns a new PassThrough stream', async t => {
const response = await get(s.url + '/');
const clonedResponse = cloneResponse(response);
t.true(clonedResponse instanceof PassThrough);
});
test('throws TypeError if response isn\'t passed in', t => {
const error = t.throws(() => cloneResponse());
t.is(error.message, 'Parameter `response` must be a response stream.');
});
test('streaming a response twice should fail', async t => {

@@ -62,1 +75,5 @@ const response = await get(s.url + '/');

});
test.after('cleanup', async () => {
await s.close();
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc