server-timing
Advanced tools
Comparing version 2.1.1 to 3.0.0
@@ -21,2 +21,6 @@ const express = require('express') | ||
app.use((req, res, next) => { | ||
res.startTime('test', 'endtime is automatically called') | ||
next() | ||
}) | ||
app.use((req, res, next) => { | ||
res.send('Open DevTools and See Network tab') | ||
@@ -23,0 +27,0 @@ }) |
26
index.js
@@ -6,7 +6,8 @@ 'use strict' | ||
module.exports = function serverTiming(options) { | ||
module.exports = function serverTiming (options) { | ||
const opts = Object.assign({ | ||
total: true, | ||
enabled: true | ||
}, options); | ||
enabled: true, | ||
autoEnd: true | ||
}, options) | ||
return (_, res, next) => { | ||
@@ -29,2 +30,8 @@ const headers = [] | ||
const timeSec = (diff[0] * 1E3) + (diff[1] * 1e-6) | ||
if (opts.autoEnd) { | ||
const keys = timer.keys() | ||
for (const key of keys) { | ||
res.endTime(key) | ||
} | ||
} | ||
headers.push(`total; dur=${timeSec}; desc="Total Response Time"`) | ||
@@ -36,2 +43,3 @@ } | ||
const existingHeaders = res.getHeader('Server-Timing') | ||
res.setHeader('Server-Timing', [].concat(existingHeaders || []).concat(headers).join(', ')) | ||
@@ -46,3 +54,3 @@ } | ||
function setMetric(headers) { | ||
function setMetric (headers) { | ||
return (name, value, description) => { | ||
@@ -56,4 +64,4 @@ if (typeof name !== 'string') { | ||
const metric = typeof description !== 'string' || !description ? | ||
`${name}; dur=${value}` : `${name}; dur=${value}; desc="${description}"` | ||
const metric = typeof description !== 'string' || !description | ||
? `${name}; dur=${value}` : `${name}; dur=${value}; desc="${description}"` | ||
@@ -64,3 +72,3 @@ headers.push(metric) | ||
function startTime(timer) { | ||
function startTime (timer) { | ||
return (name, description) => { | ||
@@ -75,3 +83,3 @@ if (typeof name !== 'string') { | ||
function endTime(timer, res) { | ||
function endTime (timer, res) { | ||
return (name) => { | ||
@@ -88,2 +96,2 @@ if (typeof name !== 'string') { | ||
} | ||
} | ||
} |
{ | ||
"name": "server-timing", | ||
"version": "2.1.1", | ||
"version": "3.0.0", | ||
"description": "This module can add `ServerTiming` Header to http response, and be able to use express middleware", | ||
@@ -33,4 +33,4 @@ "main": "index.js", | ||
"assert-stream": "1.1.1", | ||
"coveralls": "3.0.6", | ||
"eater": "3.2.0", | ||
"coveralls": "3.0.9", | ||
"eater": "4.0.1", | ||
"espower-loader": "1.2.2", | ||
@@ -37,0 +37,0 @@ "express": "4.17.1", |
@@ -34,2 +34,7 @@ # server-timing | ||
app.use((req, res, next) => { | ||
// you can see test end time response | ||
res.startTime('test', 'forget to call endTime'); | ||
next(); | ||
}); | ||
app.use((req, res, next) => { | ||
// All timings should be in milliseconds (s). See issue #9 (https://github.com/yosuke-furukawa/server-timing/issues/9). | ||
@@ -50,4 +55,5 @@ res.setMetric('db', 100.0, 'Database metric'); | ||
- options.total: boolean, default `true` | ||
- options.enabled: boolean, default `true` | ||
- options.total: boolean, default `true`, add total response time | ||
- options.enabled: boolean, default `true`, enable server timing header | ||
- options.autoEnd: boolean, default `true` automatically endTime is called if timer is not finished. | ||
@@ -54,0 +60,0 @@ # Result |
@@ -65,2 +65,1 @@ 'use strict' | ||
}) | ||
@@ -76,1 +76,18 @@ 'use strict' | ||
test('express stop automatic timer', () => { | ||
const app = express() | ||
app.use(serverTiming()) | ||
app.use((req, res, next) => { | ||
res.startTime('hello', 'hello') | ||
res.send('hello') | ||
}) | ||
const server = app.listen(0, () => { | ||
http.get(`http://localhost:${server.address().port}/`, mustCall((res) => { | ||
const assertStream = new AssertStream() | ||
assertStream.expect('hello') | ||
res.pipe(assertStream) | ||
assert(/hello; dur=.*; desc="hello", total; dur=.*; desc="Total Response Time"/.test(res.headers['server-timing'])) | ||
server.close() | ||
})) | ||
}) | ||
}) |
@@ -117,2 +117,19 @@ 'use strict' | ||
}) | ||
}) | ||
}) | ||
test('success: stop automatically timer', () => { | ||
const server = http.createServer((req, res) => { | ||
serverTiming({})(req, res) | ||
res.startTime('foo', 'foo') | ||
res.end('hello') | ||
}).listen(0, () => { | ||
http.get(`http://localhost:${server.address().port}/`, mustCall((res) => { | ||
const assertStream = new AssertStream() | ||
assertStream.expect('hello') | ||
res.pipe(assertStream) | ||
assert(res.headers['server-timing']) | ||
console.log(res.headers) | ||
server.close() | ||
})) | ||
}) | ||
}) |
@@ -7,2 +7,3 @@ 'use strict' | ||
} | ||
time (name, description) { | ||
@@ -15,2 +16,3 @@ this._times.set(name, { | ||
} | ||
timeEnd (name) { | ||
@@ -27,7 +29,12 @@ const timeObj = this._times.get(name) | ||
} | ||
clear () { | ||
this._times.clear() | ||
} | ||
keys () { | ||
return this._times.keys() | ||
} | ||
} | ||
module.exports = Timer |
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
19557
507
61