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.0.0 to 0.1.0

5

package.json
{
"name": "clone-response",
"version": "0.0.0",
"version": "0.1.0",
"description": "Clone a Node.js response object",

@@ -35,6 +35,9 @@ "main": "src/index.js",

"coveralls": "^2.13.1",
"create-test-server": "^0.1.0",
"eslint-config-xo-lukechilds": "^1.0.0",
"get-stream": "^3.0.0",
"nyc": "^10.3.2",
"rfpify": "^1.0.0",
"xo": "^0.19.0"
}
}

41

src/index.js
'use strict';
const cloneResponse = () => {
const PassThrough = require('stream').PassThrough;
// We define these manually to ensure they're always copied
// even if they would move up the prototype chain
// https://nodejs.org/api/http.html#http_class_http_incomingmessage
const knownProps = [
'destroy',
'setTimeout',
'socket',
'headers',
'trailers',
'rawHeaders',
'statusCode',
'httpVersion',
'httpVersionMinor',
'httpVersionMajor',
'rawTrailers',
'statusMessage'
];
const copyProps = (fromStream, toStream) => {
const toProps = Object.keys(toStream);
const fromProps = new Set(Object.keys(fromStream).concat(knownProps));
for (const prop of fromProps) {
// Don't overwrite existing properties
if (toProps.indexOf(prop) !== -1) {
continue;
}
toStream[prop] = typeof prop === 'function' ? fromStream[prop].bind(fromStream) : fromStream[prop];
}
};
const cloneResponse = response => {
const clone = new PassThrough();
copyProps(response, clone);
return response.pipe(clone);
};
cloneResponse.knownProps = knownProps;
module.exports = cloneResponse;

@@ -0,6 +1,55 @@

import http from 'http';
import test from 'ava';
import rfpify from 'rfpify';
import getStream from 'get-stream';
import createTestServer from 'create-test-server';
import cloneResponse from '../';
let s;
const responseText = 'Hi!';
test.before(async () => {
s = await createTestServer();
s.get('/', (req, res) => {
res.send(responseText);
});
});
test('cloneResponse is a function', t => {
t.is(typeof cloneResponse, 'function');
});
test('streaming a response twice should fail', async t => {
const response = await rfpify(http.get)(s.url + '/');
const firstStream = await getStream(response);
const secondStream = await getStream(response);
t.is(firstStream, responseText);
t.is(secondStream, '');
});
test('streaming multiple cloned responses', async t => {
const response = await rfpify(http.get)(s.url + '/');
const clonedResponse = cloneResponse(response);
const firstStream = await getStream(response);
const clonedStream = await getStream(clonedResponse);
t.is(firstStream, responseText);
t.is(clonedStream, responseText);
});
test('known properties are copied over', async t => {
const response = await rfpify(http.get)(s.url + '/');
const clonedResponse = cloneResponse(response);
cloneResponse.knownProps.forEach(prop => t.is(clonedResponse[prop], response[prop]));
});
test('custom properties are copied over', async t => {
const response = await rfpify(http.get)(s.url + '/');
response.foo = 'bar';
const clonedResponse = cloneResponse(response);
t.is(clonedResponse.foo, 'bar');
});

Sorry, the diff of this file is not supported yet

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