Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
39d82f7
commit c51e5f8
Showing
4 changed files
with
10 additions
and
43 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters