diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d60e305 --- /dev/null +++ b/.gitignore @@ -0,0 +1,68 @@ +# Compiled source # +################### +*.com +*.class +*.dll +*.exe +*.o +*.so + +# Packages # +############ +# it's better to unpack these files and commit the raw source +# git has its own built in compression methods +*.7z +*.dmg +*.gz +*.iso +*.jar +*.rar +*.tar +*.zip + +# Logs and databases # +###################### +*.log +*.sql +*.sqlite + +# OS generated files # +###################### +.DS_Store* +ehthumbs.db +Icon? +Thumbs.db + +# Node.js # +########### +lib-cov +*.seed +*.log +*.csv +*.dat +*.out +*.pid +*.gz + +pids +logs +results + +node_modules +npm-debug.log + +# Git # +####### +*.orig +*.BASE.* +*.BACKUP.* +*.LOCAL.* +*.REMOTE.* + +# Components # +############## + +/build +/components +/landing/components +.elasticbeanstalk/ diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..30d74d2 --- /dev/null +++ b/.npmignore @@ -0,0 +1 @@ +test \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..bd17c55 --- /dev/null +++ b/Makefile @@ -0,0 +1,9 @@ +BIN = ./node_modules/.bin/ + +test: + @$(BIN)mocha \ + --require should \ + --reporter spec \ + --bail + +.PHONY: test \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..77606ef --- /dev/null +++ b/README.md @@ -0,0 +1,23 @@ +# Response Time + +Response time middleware extracted from connect. + +Usage: + +```js +var responseTime = require('response-time'); + +app.use(responseTime()); +``` + +## License + +The MIT License (MIT) + +Copyright (c) 2014 Jonathan Ong me@jongleberry.com + +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. \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..cacaba4 --- /dev/null +++ b/index.js @@ -0,0 +1,34 @@ + +/*! + * Connect - responseTime + * Copyright(c) 2011 TJ Holowaychuk + * MIT Licensed + */ + +/** + * Reponse time: + * + * Adds the `X-Response-Time` header displaying the response + * duration in milliseconds. + * + * @return {Function} + * @api public + */ + +module.exports = function responseTime(){ + return function(req, res, next){ + next = next || noop; + if (res._responseTime) return next(); + var writeHead = res.writeHead; + var start = Date.now(); + res._responseTime = true; + res.writeHead = function(){ + var duration = Date.now() - start; + res.setHeader('X-Response-Time', duration + 'ms'); + writeHead.apply(res, arguments); + }; + next(); + }; +}; + +function noop() {}; \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..13699bf --- /dev/null +++ b/package.json @@ -0,0 +1,28 @@ +{ + "name": "response-time", + "description": "X-Response-Time header for node.js", + "version": "1.0.0", + "author": { + "name": "Jonathan Ong", + "email": "me@jongleberry.com", + "url": "http://jongleberry.com", + "twitter": "https://twitter.com/jongleberry" + }, + "license": "MIT", + "repository": { + "type": "git", + "url": "https://github.com/expressjs/response-time.git" + }, + "bugs": { + "mail": "me@jongleberry.com", + "url": "https://github.com/expressjs/response-time/issues" + }, + "devDependencies": { + "mocha": "*", + "should": "*", + "supertest": "*" + }, + "scripts": { + "test": "make test" + } +} diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..56abecc --- /dev/null +++ b/test/test.js @@ -0,0 +1,22 @@ +var http = require('http'); +var request = require('supertest'); + +var responseTime = require('..')(); + +describe('Response Time', function () { + it('should set the response time header', function (done) { + var server = http.createServer(function (req, res) { + responseTime(req, res); + setTimeout(function () { + res.statusCode = 204; + res.end(); + }, 10) + }) + + request(server) + .get('/') + .expect(204) + .expect('X-Response-Time', /1[0-9]ms/) + .end(done); + }) +}) \ No newline at end of file