pax_global_header00006660000000000000000000000064130127322570014514gustar00rootroot0000000000000052 comment=34c40bc97bcd7e54e13b33f77ee231ca2032de6c
response-time-2.3.2/000077500000000000000000000000001301273225700143125ustar00rootroot00000000000000response-time-2.3.2/.eslintignore000066400000000000000000000000261301273225700170130ustar00rootroot00000000000000coverage
node_modules
response-time-2.3.2/.eslintrc000066400000000000000000000000341301273225700161330ustar00rootroot00000000000000{
"extends": "standard"
}
response-time-2.3.2/.gitignore000066400000000000000000000000461301273225700163020ustar00rootroot00000000000000coverage/
node_modules/
npm-debug.log
response-time-2.3.2/.travis.yml000066400000000000000000000016601301273225700164260ustar00rootroot00000000000000language: node_js
node_js:
- "0.8"
- "0.10"
- "0.12"
- "1.8"
- "2.5"
- "3.3"
- "4.6"
- "5.12"
- "6.9"
- "7.1"
sudo: false
cache:
directories:
- node_modules
before_install:
# Setup Node.js version-specific dependencies
- "test $TRAVIS_NODE_VERSION != '0.8' || npm rm --save-dev istanbul"
- "test $(echo $TRAVIS_NODE_VERSION | cut -d. -f1) -ge 4 || npm rm --save-dev eslint eslint-config-standard eslint-plugin-promise eslint-plugin-standard"
# Update Node.js modules
- "test ! -d node_modules || npm prune"
- "test ! -d node_modules || npm rebuild"
script:
# Run test script, depending on istanbul install
- "test ! -z $(npm -ps ls istanbul) || npm test"
- "test -z $(npm -ps ls istanbul) || npm run-script test-travis"
- "test -z $(npm -ps ls eslint ) || npm run-script lint"
after_script:
- "test -e ./coverage/lcov.info && npm install coveralls@2 && cat ./coverage/lcov.info | coveralls"
response-time-2.3.2/HISTORY.md000066400000000000000000000017211301273225700157760ustar00rootroot000000000000002.3.2 / 2015-11-15
==================
* deps: depd@~1.1.0
- Enable strict mode in more places
- Support web browser loading
* deps: on-headers@~1.0.1
- perf: enable strict mode
* perf: enable strict mode
2.3.1 / 2015-05-14
==================
* deps: depd@~1.0.1
2.3.0 / 2015-02-15
==================
* Add function argument to support recording of response time
2.2.0 / 2014-09-22
==================
* Add `suffix` option
* deps: depd@~1.0.0
2.1.0 / 2014-09-16
==================
* Add `header` option for custom header name
* Change `digits` argument to an `options` argument
2.0.1 / 2014-08-10
==================
* deps: on-headers@~1.0.0
2.0.0 / 2014-05-31
==================
* add `digits` argument
* do not override existing `X-Response-Time` header
* timer not subject to clock drift
* timer resolution down to nanoseconds
* use `on-headers` module
1.0.0 / 2014-02-08
==================
* Genesis from `connect`
response-time-2.3.2/LICENSE000066400000000000000000000022241301273225700153170ustar00rootroot00000000000000(The MIT License)
Copyright (c) 2014 Jonathan Ong
Copyright (c) 2014-2015 Douglas Christopher Wilson
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
response-time-2.3.2/README.md000066400000000000000000000067071301273225700156030ustar00rootroot00000000000000# response-time
[![NPM Version][npm-image]][npm-url]
[![NPM Downloads][downloads-image]][downloads-url]
[![Build Status][travis-image]][travis-url]
[![Test Coverage][coveralls-image]][coveralls-url]
[![Gratipay][gratipay-image]][gratipay-url]
Response time for Node.js servers.
This module creates a middleware that records the response time for
requests in HTTP servers. The "response time" is defined here as the
elapsed time from when a request enters this middleware to when the
headers are written out to the client.
## Installation
```sh
$ npm install response-time
```
## API
```js
var responseTime = require('response-time')
```
### responseTime([options])
Create a middleware that adds a `X-Response-Time` header to responses. If
you don't want to use this module to automatically set a header, please
see the section about [`responseTime(fn)`](#responsetimeoptions).
#### Options
The `responseTime` function accepts an optional `options` object that may
contain any of the following keys:
##### digits
The fixed number of digits to include in the output, which is always in
milliseconds, defaults to `3` (ex: `2.300ms`).
##### header
The name of the header to set, defaults to `X-Response-Time`.
##### suffix
Boolean to indicate if units of measurement suffix should be added to
the output, defaults to `true` (ex: `2.300ms` vs `2.300`).
### responseTime(fn)
Create a new middleware that records the response time of a request and
makes this available to your own function `fn`. The `fn` argument will be
invoked as `fn(req, res, time)`, where `time` is a number in milliseconds.
## Examples
### express/connect
```js
var express = require('express')
var responseTime = require('response-time')
var app = express()
app.use(responseTime())
app.get('/', function (req, res) {
res.send('hello, world!')
})
```
### vanilla http server
```js
var finalhandler = require('finalhandler')
var http = require('http')
var responseTime = require('response-time')
// create "middleware"
var _responseTime = responseTime()
http.createServer(function (req, res) {
var done = finalhandler(req, res)
_responseTime(req, res, function (err) {
if (err) return done(err)
// respond to request
res.setHeader('content-type', 'text/plain')
res.end('hello, world!')
})
})
```
### response time metrics
```js
var express = require('express')
var responseTime = require('response-time')
var StatsD = require('node-statsd')
var app = express()
var stats = new StatsD()
stats.socket.on('error', function (error) {
console.error(error.stack)
})
app.use(responseTime(function (req, res, time) {
var stat = (req.method + req.url).toLowerCase()
.replace(/[:\.]/g, '')
.replace(/\//g, '_')
stats.timing(stat, time)
}))
app.get('/', function (req, res) {
res.send('hello, world!')
})
```
## License
[MIT](LICENSE)
[npm-image]: https://img.shields.io/npm/v/response-time.svg
[npm-url]: https://npmjs.org/package/response-time
[travis-image]: https://img.shields.io/travis/expressjs/response-time/master.svg
[travis-url]: https://travis-ci.org/expressjs/response-time
[coveralls-image]: https://img.shields.io/coveralls/expressjs/response-time/master.svg
[coveralls-url]: https://coveralls.io/r/expressjs/response-time?branch=master
[downloads-image]: https://img.shields.io/npm/dm/response-time.svg
[downloads-url]: https://npmjs.org/package/response-time
[gratipay-image]: https://img.shields.io/gratipay/dougwilson.svg
[gratipay-url]: https://www.gratipay.com/dougwilson/
response-time-2.3.2/index.js000066400000000000000000000034761301273225700157710ustar00rootroot00000000000000/*!
* response-time
* Copyright(c) 2011 TJ Holowaychuk
* Copyright(c) 2014 Jonathan Ong
* Copyright(c) 2014-2015 Douglas Christopher Wilson
* MIT Licensed
*/
'use strict'
/**
* Module dependencies
* @api private
*/
var deprecate = require('depd')('response-time')
var onHeaders = require('on-headers')
/**
* Module exports
*/
module.exports = responseTime
/**
* Reponse time:
*
* Adds the `X-Response-Time` header displaying the response
* duration in milliseconds.
*
* @param {object} [options]
* @param {number} [options.digits=3]
* @return {function}
* @api public
*/
function responseTime (options) {
var opts = options || {}
if (typeof options === 'number') {
// back-compat single number argument
deprecate('number argument: use {digits: ' + JSON.stringify(options) + '} instead')
opts = { digits: options }
}
// get the function to invoke
var fn = typeof opts !== 'function'
? createSetHeader(opts)
: opts
return function responseTime (req, res, next) {
var startAt = process.hrtime()
onHeaders(res, function onHeaders () {
var diff = process.hrtime(startAt)
var time = diff[0] * 1e3 + diff[1] * 1e-6
fn(req, res, time)
})
next()
}
}
/**
* Create function to set respoonse time header.
* @api private
*/
function createSetHeader (options) {
// response time digits
var digits = options.digits !== undefined
? options.digits
: 3
// header name
var header = options.header || 'X-Response-Time'
// display suffix
var suffix = options.suffix !== undefined
? Boolean(options.suffix)
: true
return function setResponseHeader (req, res, time) {
if (res.getHeader(header)) {
return
}
var val = time.toFixed(digits)
if (suffix) {
val += 'ms'
}
res.setHeader(header, val)
}
}
response-time-2.3.2/package.json000066400000000000000000000022471301273225700166050ustar00rootroot00000000000000{
"name": "response-time",
"description": "Response time for Node.js servers",
"version": "2.3.2",
"author": "Jonathan Ong (http://jongleberry.com)",
"contributors": [
"Douglas Christopher Wilson "
],
"license": "MIT",
"keywords": [
"http",
"res",
"response time",
"x-response-time"
],
"repository": "expressjs/response-time",
"dependencies": {
"depd": "~1.1.0",
"on-headers": "~1.0.1"
},
"devDependencies": {
"after": "0.8.2",
"eslint": "3.10.1",
"eslint-config-standard": "6.2.1",
"eslint-plugin-promise": "3.3.2",
"eslint-plugin-standard": "2.0.1",
"istanbul": "0.4.5",
"mocha": "2.5.3",
"supertest": "1.1.0"
},
"files": [
"LICENSE",
"HISTORY.md",
"index.js"
],
"engines": {
"node": ">= 0.8.0"
},
"scripts": {
"lint": "eslint .",
"test": "mocha --reporter spec --bail --check-leaks test/",
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/",
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/"
}
}
response-time-2.3.2/test/000077500000000000000000000000001301273225700152715ustar00rootroot00000000000000response-time-2.3.2/test/.eslintrc000066400000000000000000000000441301273225700171130ustar00rootroot00000000000000{
"env": {
"mocha": true
}
}
response-time-2.3.2/test/test.js000066400000000000000000000102331301273225700166050ustar00rootroot00000000000000
process.env.NO_DEPRECATION = 'response-time'
var after = require('after')
var assert = require('assert')
var http = require('http')
var request = require('supertest')
var responseTime = require('..')
describe('responseTime()', function () {
it('should send X-Response-Time header', function (done) {
var server = createServer()
request(server)
.get('/')
.expect('X-Response-Time', /^[0-9.]+ms$/, done)
})
it('should not override X-Response-Time header', function (done) {
var server = createServer(undefined, function (req, res) {
res.setHeader('X-Response-Time', 'bogus')
})
request(server)
.get('/')
.expect('X-Response-Time', 'bogus', done)
})
describe('with "digits"', function () {
it('should default to 3', function (done) {
var server = createServer()
request(server)
.get('/')
.expect('X-Response-Time', /^[0-9]+\.[0-9]{3}ms$/, done)
})
it('should allow custom digits', function (done) {
var server = createServer(5)
request(server)
.get('/')
.expect('X-Response-Time', /^[0-9]+\.[0-9]{5}ms$/, done)
})
it('should allow no digits', function (done) {
var server = createServer(0)
request(server)
.get('/')
.expect('X-Response-Time', /^[0-9]+ms$/, done)
})
})
})
describe('responseTime(fn)', function () {
it('should call fn with response time', function (done) {
var cb = after(2, done)
var start = process.hrtime()
var server = createServer(function (req, res, time) {
var diff = process.hrtime(start)
var max = diff[0] * 1e3 + diff[1] * 1e-6
assert.equal(req.url, '/')
assert.equal(res.statusCode, 200)
assert.ok(time >= 0)
assert.ok(time <= max)
cb()
})
request(server)
.get('/')
.expect(200, cb)
})
it('should not send X-Response-Time header', function (done) {
var cb = after(2, done)
var server = createServer(function () {
cb()
})
request(server)
.get('/')
.expect(shouldNotHaveHeader('X-Response-Time'))
.expect(200, cb)
})
})
describe('responseTime(options)', function () {
describe('with "digits" option', function () {
it('should default to 3', function (done) {
var server = createServer()
request(server)
.get('/')
.expect('X-Response-Time', /^[0-9]+\.[0-9]{3}ms$/, done)
})
it('should allow custom digits', function (done) {
var server = createServer({ digits: 5 })
request(server)
.get('/')
.expect('X-Response-Time', /^[0-9]+\.[0-9]{5}ms$/, done)
})
it('should allow no digits', function (done) {
var server = createServer({ digits: 0 })
request(server)
.get('/')
.expect('X-Response-Time', /^[0-9]+ms$/, done)
})
})
describe('with "header" option', function () {
it('should default to X-Response-Time', function (done) {
var server = createServer()
request(server)
.get('/')
.expect('X-Response-Time', /^[0-9.]+ms$/, done)
})
it('should allow custom header name', function (done) {
var server = createServer({ header: 'X-Time-Taken' })
request(server)
.get('/')
.expect('X-Time-Taken', /^[0-9.]+ms$/, done)
})
})
describe('with "suffix" option', function () {
it('should default to true', function (done) {
var server = createServer()
request(server)
.get('/')
.expect('X-Response-Time', /^[0-9.]+ms$/, done)
})
it('should allow disabling suffix', function (done) {
var server = createServer({ suffix: false })
request(server)
.get('/')
.expect('X-Response-Time', /^[0-9.]+$/, done)
})
})
})
function createServer (opts, fn) {
var _responseTime = responseTime(opts)
return http.createServer(function (req, res) {
_responseTime(req, res, function (err) {
setTimeout(function () {
fn && fn(req, res)
res.statusCode = err ? (err.status || 500) : 200
res.end(err ? err.message : 'OK')
}, 10)
})
})
}
function shouldNotHaveHeader (header) {
return function (res) {
assert.ok(!(header.toLowerCase() in res.headers), 'should not have header ' + header)
}
}