Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
yosuke-furukawa committed Dec 17, 2019
1 parent cd3389f commit 737f4ae
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
20 changes: 10 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
const onHeaders = require('on-headers')
const Timer = require('./timer')

module.exports = function serverTiming(options) {
module.exports = function serverTiming (options) {
const opts = Object.assign({
total: true,
enabled: true,
autoEnd: true,
}, options);
autoEnd: true
}, options)
return (_, res, next) => {
const headers = []
const timer = new Timer()
Expand All @@ -27,9 +27,9 @@ module.exports = function serverTiming(options) {
const diff = process.hrtime(startAt)
const timeSec = (diff[0] * 1E3) + (diff[1] * 1e-6)
if (opts.autoEnd) {
const keys = timer.keys();
const keys = timer.keys()
for (const key of keys) {
res.endTime(key);
res.endTime(key)
}
}
headers.push(`total; dur=${timeSec}; desc="Total Response Time"`)
Expand All @@ -48,7 +48,7 @@ module.exports = function serverTiming(options) {
}
}

function setMetric(headers) {
function setMetric (headers) {
return (name, value, description) => {
if (typeof name !== 'string') {
return console.warn('1st argument name is not string')
Expand All @@ -57,14 +57,14 @@ function setMetric(headers) {
return console.warn('2nd argument value is not number')
}

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}"`

headers.push(metric)
}
}

function startTime(timer) {
function startTime (timer) {
return (name, description) => {
if (typeof name !== 'string') {
return console.warn('1st argument name is not string')
Expand All @@ -74,7 +74,7 @@ function startTime(timer) {
}
}

function endTime(timer, res) {
function endTime (timer, res) {
return (name) => {
if (typeof name !== 'string') {
return console.warn('1st argument name is not string')
Expand Down
1 change: 0 additions & 1 deletion test/express-test-set-timer.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,3 @@ test('express use startTime/endTime multiple', () => {
http.get(`http://localhost:${server.address().port}/`, mustCall(checkFunc))
})
})

4 changes: 2 additions & 2 deletions test/http-basic-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ test('success: stop automatically timer', () => {
assertStream.expect('hello')
res.pipe(assertStream)
assert(res.headers['server-timing'])
console.log(res.headers);
console.log(res.headers)
server.close()
}))
})
});
})
6 changes: 5 additions & 1 deletion timer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ class Timer {
constructor () {
this._times = new Map()
}

time (name, description) {
this._times.set(name, {
name: name,
description: description,
start: process.hrtime()
})
}

timeEnd (name) {
const timeObj = this._times.get(name)
if (!timeObj) {
Expand All @@ -22,11 +24,13 @@ class Timer {
this._times.delete(name)
return timeObj
}

clear () {
this._times.clear()
}

keys () {
return this._times.keys();
return this._times.keys()
}
}

Expand Down

0 comments on commit 737f4ae

Please sign in to comment.