mimic-response
Advanced tools
Comparing version 2.1.0 to 3.0.0
@@ -13,6 +13,6 @@ import {IncomingMessage} from 'http'; | ||
declare function mimicResponse<T extends NodeJS.ReadableStream>( | ||
fromStream: IncomingMessage, | ||
fromStream: IncomingMessage, // eslint-disable-line @typescript-eslint/prefer-readonly-parameter-types | ||
toStream: T, | ||
): IncomingMessage & T; | ||
): T & IncomingMessage; | ||
export = mimicResponse; |
23
index.js
@@ -9,3 +9,2 @@ 'use strict'; | ||
'complete', | ||
'destroy', | ||
'headers', | ||
@@ -29,2 +28,4 @@ 'httpVersion', | ||
const properties = {}; | ||
for (const property of fromProperties) { | ||
@@ -36,6 +37,24 @@ // Don't overwrite existing properties. | ||
toStream[property] = typeof fromStream[property] === 'function' ? fromStream[property].bind(fromStream) : fromStream[property]; | ||
properties[property] = { | ||
get() { | ||
const value = fromStream[property]; | ||
const isFunction = typeof value === 'function'; | ||
return isFunction ? value.bind(fromStream) : value; | ||
}, | ||
set(value) { | ||
fromStream[property] = value; | ||
}, | ||
enumerable: true, | ||
configurable: false | ||
}; | ||
} | ||
Object.defineProperties(toStream, properties); | ||
fromStream.once('aborted', () => { | ||
toStream.emit('aborted'); | ||
}); | ||
return toStream; | ||
}; |
{ | ||
"name": "mimic-response", | ||
"version": "2.1.0", | ||
"version": "3.0.0", | ||
"description": "Mimic a Node.js HTTP response stream", | ||
@@ -14,3 +14,3 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=8" | ||
"node": ">=10" | ||
}, | ||
@@ -35,10 +35,10 @@ "scripts": { | ||
"devDependencies": { | ||
"@sindresorhus/tsconfig": "^0.3.0", | ||
"@types/node": "^12.0.0", | ||
"ava": "^1.1.0", | ||
"@types/node": "^14.0.1", | ||
"ava": "^2.4.0", | ||
"create-test-server": "^2.4.0", | ||
"pify": "^4.0.1", | ||
"tsd": "^0.7.3", | ||
"xo": "^0.24.0" | ||
"p-event": "^4.1.0", | ||
"pify": "^5.0.0", | ||
"tsd": "^0.11.0", | ||
"xo": "^0.30.0" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# mimic-response [![Build Status](https://travis-ci.org/sindresorhus/mimic-response.svg?branch=master)](https://travis-ci.org/sindresorhus/mimic-response) | ||
# mimic-response [![Build Status](https://travis-ci.com/sindresorhus/mimic-response.svg?branch=master)](https://travis-ci.com/sindresorhus/mimic-response) | ||
@@ -30,2 +30,51 @@ > Mimic a [Node.js HTTP response stream](https://nodejs.org/api/http.html#http_class_http_incomingmessage) | ||
**Note #1:** The `from.destroy(error)` function is not proxied. You have to call it manually: | ||
```js | ||
const stream = require('stream'); | ||
const mimicResponse = require('mimic-response'); | ||
const responseStream = getHttpResponseStream(); | ||
const myStream = new stream.PassThrough({ | ||
destroy(error, callback) { | ||
responseStream.destroy(); | ||
callback(error); | ||
} | ||
}); | ||
myStream.destroy(); | ||
``` | ||
Please note that `myStream` and `responseStream` never throws. The error is passed to the request instead. | ||
**Note #2:** The `aborted` and `close` events are not proxied. You have to add them manually: | ||
```js | ||
const stream = require('stream'); | ||
const mimicResponse = require('mimic-response'); | ||
const responseStream = getHttpResponseStream(); | ||
const myStream = new stream.PassThrough({ | ||
destroy(error, callback) { | ||
responseStream.destroy(); | ||
callback(error); | ||
} | ||
}); | ||
responseStream.once('aborted', () => { | ||
myStream.destroy(); | ||
myStream.emit('aborted'); | ||
}); | ||
responseStream.once('closed', () => { | ||
myStream.emit('closed'); | ||
}); | ||
responseStream.pipe(myStream); | ||
``` | ||
#### from | ||
@@ -32,0 +81,0 @@ |
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
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
6145
61
107