clone-response
Advanced tools
+5
-3
| { | ||
| "name": "clone-response", | ||
| "version": "0.2.0", | ||
| "version": "0.2.1", | ||
| "description": "Clone a Node.js response object", | ||
@@ -31,3 +31,5 @@ "main": "src/index.js", | ||
| "homepage": "https://github.com/lukechilds/clone-response", | ||
| "dependencies": {}, | ||
| "dependencies": { | ||
| "mimic-response": "^1.0.0" | ||
| }, | ||
| "devDependencies": { | ||
@@ -40,5 +42,5 @@ "ava": "^0.19.1", | ||
| "nyc": "^10.3.2", | ||
| "rfpify": "^1.0.0", | ||
| "pify": "^3.0.0", | ||
| "xo": "^0.19.0" | ||
| } | ||
| } |
+2
-35
| 'use strict'; | ||
| const PassThrough = require('stream').PassThrough; | ||
| const mimicResponse = require('mimic-response'); | ||
| // 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 cloneResponse = response => { | ||
| const clone = new PassThrough(); | ||
| copyProps(response, clone); | ||
| mimicResponse(response, clone); | ||
@@ -44,4 +13,2 @@ return response.pipe(clone); | ||
| cloneResponse.knownProps = knownProps; | ||
| module.exports = cloneResponse; |
| import http from 'http'; | ||
| import test from 'ava'; | ||
| import rfpify from 'rfpify'; | ||
| import pify from 'pify'; | ||
| import getStream from 'get-stream'; | ||
@@ -8,2 +8,4 @@ import createTestServer from 'create-test-server'; | ||
| const get = pify(http.get, { errorFirst: false }); | ||
| let s; | ||
@@ -25,3 +27,3 @@ const responseText = 'Hi!'; | ||
| test('streaming a response twice should fail', async t => { | ||
| const response = await rfpify(http.get)(s.url + '/'); | ||
| const response = await get(s.url + '/'); | ||
| const firstStream = await getStream(response); | ||
@@ -35,3 +37,3 @@ const secondStream = await getStream(response); | ||
| test('streaming multiple cloned responses succeeds', async t => { | ||
| const response = await rfpify(http.get)(s.url + '/'); | ||
| const response = await get(s.url + '/'); | ||
| const clonedResponse = cloneResponse(response); | ||
@@ -45,11 +47,4 @@ const firstStream = await getStream(response); | ||
| 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.true(typeof clonedResponse[prop] !== 'undefined')); | ||
| }); | ||
| test('custom properties are copied over', async t => { | ||
| const response = await rfpify(http.get)(s.url + '/'); | ||
| const response = await get(s.url + '/'); | ||
| response.foo = 'bar'; | ||
@@ -62,4 +57,4 @@ const clonedResponse = cloneResponse(response); | ||
| test('function methods are bound to the original response instance', async t => { | ||
| const response = await rfpify(http.get)(s.url + '/'); | ||
| response.getThis = function () { | ||
| const response = await get(s.url + '/'); | ||
| response.getContext = function () { | ||
| return this; | ||
@@ -69,3 +64,3 @@ }; | ||
| t.is(response.getThis(), clonedResponse.getThis()); | ||
| t.is(response.getContext(), clonedResponse.getContext()); | ||
| }); |
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
0
-100%5752
-14.9%1
Infinity%56
-36.36%+ Added
+ Added