Comparing version 7.1.0 to 7.2.0
@@ -21,6 +21,9 @@ 'use strict'; | ||
jsonRegex: /^application\/[a-z.+-]*json$/, | ||
shallowOptions: ['agent', 'payload', 'downstreamRes', 'beforeRedirect', 'redirected'] | ||
shallowOptions: ['agent', 'payload', 'downstreamRes', 'beforeRedirect', 'redirected'], | ||
emitSymbol: Symbol.for('wreck') | ||
}; | ||
process[internals.emitSymbol] = process[internals.emitSymbol] || new Events.EventEmitter(); | ||
// new instance is exported as module.exports | ||
@@ -30,3 +33,4 @@ | ||
Events.EventEmitter.call(this); | ||
// Use a single emitter instance for events | ||
Object.assign(this, process[internals.emitSymbol]); | ||
@@ -135,2 +139,4 @@ this.agents = { | ||
this.emit('request', uri, options); | ||
const start = Date.now(); | ||
@@ -140,30 +146,6 @@ const req = client.request(uri); | ||
let shadow = null; // A copy of the streamed request payload when redirects are enabled | ||
let onResponse; | ||
let onError; | ||
let timeoutId; | ||
// Register handlers | ||
const onError = (err) => { | ||
const finish = (err, res) => { | ||
if (!callback || err) { | ||
req.abort(); | ||
} | ||
req.removeListener('response', onResponse); | ||
req.removeListener('error', onError); | ||
req.on('error', Hoek.ignore); | ||
clearTimeout(timeoutId); | ||
this.emit('response', err, req, res, start, uri); | ||
if (callback) { | ||
return callback(err, res); | ||
} | ||
}; | ||
const finishOnce = Hoek.once(finish); | ||
onError = (err) => { | ||
err.trace = _trace; | ||
@@ -175,3 +157,3 @@ return finishOnce(Boom.badGateway('Client request error', err)); | ||
onResponse = (res) => { | ||
const onResponse = (res) => { | ||
@@ -223,2 +205,23 @@ // Pass-through response | ||
// Register handlers | ||
const finish = (err, res) => { | ||
if (!callback || err) { | ||
req.abort(); | ||
} | ||
req.removeListener('response', onResponse); | ||
req.removeListener('error', onError); | ||
req.on('error', Hoek.ignore); | ||
clearTimeout(timeoutId); | ||
this.emit('response', err, req, res, start, uri); | ||
if (callback) { | ||
return callback(err, res); | ||
} | ||
}; | ||
const finishOnce = Hoek.once(finish); | ||
req.once('response', onResponse); | ||
@@ -225,0 +228,0 @@ |
{ | ||
"name": "wreck", | ||
"description": "HTTP Client Utilities", | ||
"version": "7.1.0", | ||
"version": "7.2.0", | ||
"repository": "git://github.com/hapijs/wreck", | ||
@@ -21,3 +21,4 @@ "main": "lib/index", | ||
"code": "2.x.x", | ||
"lab": "10.x.x" | ||
"lab": "10.x.x", | ||
"require-reload": "0.2.x" | ||
}, | ||
@@ -24,0 +25,0 @@ "scripts": { |
@@ -249,6 +249,21 @@ ![wreck Logo](https://raw.github.com/hapijs/wreck/master/images/wreck.png) | ||
#### `request` | ||
The request event is emitted just before *wreck* makes a request. The | ||
handler should accept the following arguments `(uri, options)` where: | ||
- `uri` - the result of `Url.parse(uri)`. This will provide information about the resource requested. Also includes the headers and method. | ||
- `options` - the options passed into the request function. This will include | ||
a payload if there is one. | ||
Since the `request` event executes on a global event handler, you can intercept | ||
and decorate a request before its sent. | ||
#### `response` | ||
The response event is always emitted for any request that *wreck* makes. The handler should accept the following | ||
arguments `(error, request, response, start, uri)` where: | ||
The response event is always emitted for any request that *wreck* makes. The | ||
handler should accept the following arguments `(error, request, response, start, | ||
uri)` where: | ||
- `error` - a Boom error | ||
@@ -261,6 +276,23 @@ - `request` - the raw `ClientHttp` request object | ||
This event is useful for logging all requests that go through *wreck*. | ||
The error and response arguments can be undefined depending on if an error occurs. Please be aware that if multiple | ||
modules are depending on the same cached *wreck* module that this event can fire for each request made across all | ||
modules. The start argument is the timestamp when the request was started. This can be useful for determining how long | ||
it takes *wreck* to get a response back and processed. | ||
This event is useful for logging all requests that go through *wreck*. The error | ||
and response arguments can be undefined depending on if an error occurs. Please | ||
be aware that if multiple modules are depending on the same cached *wreck* | ||
module that this event can fire for each request made across all modules. The | ||
start argument is the timestamp when the request was started. This can be | ||
useful for determining how long it takes *wreck* to get a response back and | ||
processed. | ||
The `EventEmitter` is attached to the `process` object under a `Symbol` with the | ||
value of `'wreck'`. Therefore, if you want to capture a wreck event, after wreck | ||
has been loaded, but in a module that doesn't require wreck, you can handle | ||
events in the following way: | ||
```js | ||
const symbol = Symbol.for('wreck'); | ||
process[symbol].on('response', (err) => { | ||
if (err) { | ||
console.error(err); | ||
} | ||
}); | ||
``` |
Sorry, the diff of this file is too big to display
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
142270
2070
297
3