-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
99 lines (82 loc) · 2.4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
'use strict'
const onHeaders = require('on-headers')
const Timer = require('./timer')
module.exports = function serverTiming (options) {
const opts = Object.assign({
name: 'total',
description: 'Total Response Time',
total: true,
enabled: true,
autoEnd: true,
precision: +Infinity
}, options)
return (req, res, next) => {
const headers = []
const timer = new Timer()
if (res.setMetric) {
throw new Error('res.setMetric already exists.')
}
const startAt = process.hrtime()
res.setMetric = setMetric(headers, opts)
res.startTime = startTime(timer)
res.endTime = endTime(timer, res)
onHeaders(res, () => {
if (opts.autoEnd) {
const keys = timer.keys()
for (const key of keys) {
res.endTime(key)
}
}
if (opts.total) {
const diff = process.hrtime(startAt)
const timeSec = (diff[0] * 1E3) + (diff[1] * 1e-6)
res.setMetric(opts.name, timeSec, opts.description)
}
timer.clear()
const enabled = typeof opts.enabled === 'function'
? opts.enabled(req, res) : opts.enabled
if (enabled) {
const existingHeaders = res.getHeader('Server-Timing')
res.setHeader('Server-Timing', [].concat(existingHeaders || []).concat(headers))
}
})
if (typeof next === 'function') {
next()
}
}
}
function setMetric (headers, opts) {
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')
}
const dur = Number.isFinite(opts.precision)
? value.toFixed(opts.precision) : value
const metric = typeof description !== 'string' || !description
? `${name}; dur=${dur}` : `${name}; dur=${dur}; desc="${description}"`
headers.push(metric)
}
}
function startTime (timer) {
return (name, description) => {
if (typeof name !== 'string') {
return console.warn('1st argument name is not string')
}
timer.time(name, description)
}
}
function endTime (timer, res) {
return (name) => {
if (typeof name !== 'string') {
return console.warn('1st argument name is not string')
}
const obj = timer.timeEnd(name)
if (!obj) {
return
}
res.setMetric(obj.name, obj.value, obj.description)
}
}