clone-response
Advanced tools
Comparing version 0.0.0 to 0.1.0
{ | ||
"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" | ||
} | ||
} |
'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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
6431
80
8