server-timing
Advanced tools
Comparing version 1.0.0 to 1.0.1
const express = require('express') | ||
const path = require('path') | ||
const app = express() | ||
@@ -7,13 +6,12 @@ const serverTiming = require('../.') | ||
//app.use(express.static(path.join(__dirname, 'public'))) | ||
app.use(serverTiming()) | ||
app.use((req, res, next) => { | ||
res.setMetric('db', 100.0, "Database metric"); | ||
res.setMetric('api', 200.0, "HTTP/API metric"); | ||
res.setMetric('cache', 300.0, "cache metric"); | ||
next(); | ||
}); | ||
res.setMetric('db', 100.0, 'Database metric') | ||
res.setMetric('api', 200.0, 'HTTP/API metric') | ||
res.setMetric('cache', 300.0, 'cache metric') | ||
next() | ||
}) | ||
app.use((req, res, next) => { | ||
res.send('hello') | ||
}); | ||
}) | ||
@@ -20,0 +18,0 @@ app.listen(PORT, () => { |
30
index.js
'use strict' | ||
const onHeaders = require('on-headers') | ||
const headers = [] | ||
module.exports = function serverTiming (options) { | ||
const headers = [] | ||
const opts = options || { total: true } | ||
@@ -15,3 +15,3 @@ return (_, res, next) => { | ||
res.setMetric = setMetric | ||
res.setMetric = setMetric(headers) | ||
@@ -32,17 +32,19 @@ onHeaders(res, () => { | ||
function setMetric (name, value, description) { | ||
if (typeof name !== 'string') { | ||
return console.warn('1st argument name is not string') | ||
} | ||
if (typeof value !== 'number') { | ||
return console.warn('2nd argument value is not number') | ||
} | ||
function setMetric (headers) { | ||
return (name, value, description) => { | ||
if (typeof name !== 'string') { | ||
return console.warn('1st argument name is not string') | ||
} | ||
if (typeof value !== 'number') { | ||
return console.warn('2nd argument value is not number') | ||
} | ||
let metric = `${name}=${value}` | ||
let metric = `${name}=${value}` | ||
if (typeof description === 'string') { | ||
metric += `; "${description}"` | ||
if (typeof description === 'string') { | ||
metric += `; "${description}"` | ||
} | ||
headers.push(metric) | ||
} | ||
headers.push(metric) | ||
} |
{ | ||
"name": "server-timing", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "This module can add `ServerTiming` Header to http response, and be able to use express middleware", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "eater", | ||
"test": "eater --require .loader.js", | ||
"lint": "standard" | ||
@@ -32,6 +32,8 @@ }, | ||
"eater": "^3.2.0", | ||
"espower-loader": "^1.2.0", | ||
"express": "^4.14.1", | ||
"must-call": "^1.0.0", | ||
"power-assert": "^1.4.2", | ||
"standard": "^8.6.0" | ||
} | ||
} |
@@ -45,1 +45,39 @@ 'use strict' | ||
}) | ||
test('http request twice more server timing response', () => { | ||
let count = 0 | ||
const server = http.createServer((req, res) => { | ||
serverTiming()(req, res) | ||
if (count === 0) { | ||
res.setMetric('foo', 100.0) | ||
res.setMetric('bar', 10.0, 'Bar is not Foo') | ||
res.setMetric('baz', 0) | ||
res.end('hello') | ||
} | ||
if (count === 1) { | ||
res.setMetric('test', 0.10, 'Test') | ||
res.end('world') | ||
} | ||
count++ | ||
}).listen(0, () => { | ||
http.get(`http://localhost:${server.address().port}/`, mustCall((res) => { | ||
const assertStream = new AssertStream() | ||
assertStream.expect('hello') | ||
res.pipe(assertStream) | ||
const timingHeader = res.headers['server-timing'] | ||
assert(/total=.*; "Total Response Time"/.test(timingHeader)) | ||
assert(/foo=100, bar=10; "Bar is not Foo", baz=0/.test(timingHeader)) | ||
http.get(`http://localhost:${server.address().port}/`, mustCall((res) => { | ||
const assertStream = new AssertStream() | ||
assertStream.expect('world') | ||
res.pipe(assertStream) | ||
const timingHeader = res.headers['server-timing'] | ||
assert(/total=.*; "Total Response Time"/.test(timingHeader)) | ||
assert(/test=0.1; "Test"/.test(timingHeader)) | ||
server.close() | ||
})) | ||
})) | ||
}) | ||
}) |
7648
9
179
7