Skip to content

Commit

Permalink
Use mimic-response
Browse files Browse the repository at this point in the history
  • Loading branch information
lukechilds committed Jun 3, 2017
1 parent 39d82f7 commit c51e5f8
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 43 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -28,7 +28,9 @@
"url": "https://github.com/lukechilds/clone-response/issues"
},
"homepage": "https://github.com/lukechilds/clone-response",
"dependencies": {},
"dependencies": {
"mimic-response": "^1.0.0"
},
"devDependencies": {
"ava": "^0.19.1",
"coveralls": "^2.13.1",
Expand Down
37 changes: 2 additions & 35 deletions src/index.js
@@ -1,46 +1,13 @@
'use strict';

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 fromStream[prop] === 'function' ? fromStream[prop].bind(fromStream) : fromStream[prop];
}
};
const mimicResponse = require('mimic-response');

const cloneResponse = response => {
const clone = new PassThrough();
copyProps(response, clone);
mimicResponse(response, clone);

return response.pipe(clone);
};

cloneResponse.knownProps = knownProps;

module.exports = cloneResponse;
7 changes: 0 additions & 7 deletions test/clone-response.js
Expand Up @@ -41,13 +41,6 @@ test('streaming multiple cloned responses succeeds', async t => {
t.is(clonedStream, responseText);
});

test('known properties are copied over', async t => {
const response = await get(s.url + '/');
const clonedResponse = cloneResponse(response);

cloneResponse.knownProps.forEach(prop => t.true(typeof clonedResponse[prop] !== 'undefined'));
});

test('custom properties are copied over', async t => {
const response = await get(s.url + '/');
response.foo = 'bar';
Expand Down

0 comments on commit c51e5f8

Please sign in to comment.