Comparing version 8.0.1 to 8.1.0
@@ -16,2 +16,3 @@ 'use strict'; | ||
}; | ||
const NS_PER_SEC = 1e9; | ||
@@ -25,3 +26,4 @@ | ||
localStatePassThrough: false, // Pass cookies defined by the server upstream | ||
maxSockets: Infinity | ||
maxSockets: Infinity, | ||
downstreamResponseTime: false | ||
}; | ||
@@ -151,2 +153,6 @@ | ||
let downstreamStartTime; | ||
if (settings.downstreamResponseTime) { | ||
downstreamStartTime = process.hrtime(); | ||
} | ||
const promise = Wreck.request(request.method, uri, options); | ||
@@ -158,6 +164,14 @@ | ||
let downstreamResponseTime; | ||
try { | ||
res = await promise; | ||
if (settings.downstreamResponseTime) { | ||
downstreamResponseTime = process.hrtime(downstreamStartTime); | ||
} | ||
} | ||
catch (error) { | ||
if (settings.downstreamResponseTime) { | ||
downstreamResponseTime = process.hrtime(downstreamStartTime); | ||
request.log(['h2o2', 'error'], { downstreamResponseTime: downstreamResponseTime[0] * NS_PER_SEC + downstreamResponseTime[1] }); | ||
} | ||
if (settings.onResponse) { | ||
@@ -170,2 +184,7 @@ return settings.onResponse.call(bind, error, res, request, h, settings, ttl); | ||
if (settings.downstreamResponseTime) { | ||
downstreamResponseTime = process.hrtime(downstreamStartTime); | ||
request.log(['h2o2', 'success'], { downstreamResponseTime: downstreamResponseTime[0] * NS_PER_SEC + downstreamResponseTime[1] }); | ||
} | ||
if (settings._upstreamTtl) { | ||
@@ -172,0 +191,0 @@ const cacheControlHeader = res.headers['cache-control']; |
{ | ||
"name": "h2o2", | ||
"description": "Proxy handler plugin for hapi.js", | ||
"version": "8.0.1", | ||
"version": "8.1.0", | ||
"repository": "git://github.com/hapijs/h2o2", | ||
@@ -6,0 +6,0 @@ "main": "lib/index.js", |
@@ -28,4 +28,4 @@ # h2o2 | ||
await server.start(); | ||
console.log(`Server started at: ${server.info.uri}`); | ||
console.log(`Server started at: ${server.info.uri}`); | ||
} | ||
@@ -81,2 +81,3 @@ catch(e) { | ||
The possible values depend on your installation of OpenSSL. Read the official OpenSSL docs for possible [TLS_CIPHERS](https://www.openssl.org/docs/man1.0.2/apps/ciphers.html#CIPHER-LIST-FORMAT). | ||
* `downstreamResponseTime` - logs the time spent processing the downstream request using [process.hrtime](https://nodejs.org/api/process.html#process_process_hrtime_time). Defaults to `false`. | ||
@@ -138,3 +139,3 @@ ## Usage | ||
### Custom `uri` template values | ||
When using the `uri` option, there are optional **default** template values that can be injected from the incoming `request`: | ||
@@ -146,3 +147,3 @@ | ||
* `{path}` | ||
```javascript | ||
@@ -176,3 +177,3 @@ server.route({ | ||
### Using the `mapUri` and `onResponse` options | ||
@@ -179,0 +180,0 @@ |
@@ -1716,3 +1716,2 @@ 'use strict'; | ||
const upstream = Hapi.server({ tls: tlsOptions }); | ||
@@ -1755,2 +1754,87 @@ upstream.route({ | ||
}); | ||
it('adds downstreamResponseTime to the response when downstreamResponseTime is set to true on success', async () => { | ||
const upstream = Hapi.server(); | ||
upstream.route({ | ||
method: 'GET', | ||
path: '/', | ||
handler: function (request, h) { | ||
return h.response('ok'); | ||
} | ||
}); | ||
await upstream.start(); | ||
const plugin = { | ||
register: H2o2.register, | ||
pkg: H2o2.pkg | ||
}; | ||
const options = { downstreamResponseTime: true }; | ||
const server = Hapi.server(); | ||
await server.register({ | ||
plugin, | ||
options | ||
}); | ||
server.route({ | ||
method: 'GET', | ||
path: '/', | ||
handler: function (request, h) { | ||
return h.proxy({ host: 'localhost', port: upstream.info.port, xforward: true, passThrough: true }); | ||
} | ||
}); | ||
server.events.on('request', (request, event, tags) => { | ||
expect(Object.keys(event.data)).to.equal(['downstreamResponseTime']); | ||
expect(tags).to.equal({ h2o2: true, success: true }); | ||
}); | ||
const res = await server.inject('/'); | ||
expect(res.statusCode).to.equal(200); | ||
await upstream.stop(); | ||
}); | ||
it('adds downstreamResponseTime to the response when downstreamResponseTime is set to true on error', async () => { | ||
const failureResponse = function (err, res, request, h, settings, ttl) { | ||
expect(h.response).to.exist(); | ||
throw err; | ||
}; | ||
const dummy = Hapi.server(); | ||
await dummy.start(); | ||
const dummyPort = dummy.info.port; | ||
await dummy.stop(Hoek.ignore); | ||
const plugin = { | ||
register: H2o2.register, | ||
pkg: H2o2.pkg | ||
}; | ||
const options = { downstreamResponseTime: true }; | ||
const server = Hapi.server(); | ||
await server.register({ | ||
plugin, | ||
options | ||
}); | ||
server.route({ method: 'GET', path: '/failureResponse', handler: { proxy: { host: 'localhost', port: dummyPort, onResponse: failureResponse } }, config: { cache: { expiresIn: 500 } } }); | ||
let firstEvent = true; | ||
server.events.on('request', (request, event, tags) => { | ||
if (firstEvent) { | ||
firstEvent = false; | ||
expect(Object.keys(event.data)).to.equal(['downstreamResponseTime']); | ||
expect(tags).to.equal({ h2o2: true, error: true }); | ||
} | ||
}); | ||
const res = await server.inject('/failureResponse'); | ||
expect(res.statusCode).to.equal(502); | ||
}); | ||
}); |
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
85671
1579
205