This repository has been archived by the owner on Feb 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 12b1b52
Showing
11 changed files
with
251 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
lib-cov | ||
coverage.html | ||
*.seed | ||
*.log | ||
*.csv | ||
*.dat | ||
*.out | ||
*.pid | ||
*.gz | ||
|
||
pids | ||
logs | ||
results | ||
|
||
node_modules | ||
npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
test/ | ||
coverage.html | ||
lib-cov/ | ||
Makefile | ||
.travis.yml | ||
logo.png |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
language: node_js | ||
node_js: | ||
- 0.9 | ||
- 0.8 | ||
- 0.6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
This software is licensed under the MIT License. | ||
|
||
Copyright (C) 2012 by fengmk2 <[email protected]> | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
TESTS = test/*.test.js | ||
REPORTER = spec | ||
TIMEOUT = 2500 | ||
JSCOVERAGE = ./node_modules/jscover/bin/jscover | ||
|
||
install: | ||
@npm install | ||
|
||
test: install | ||
@NODE_ENV=test ./node_modules/mocha/bin/mocha \ | ||
--reporter $(REPORTER) \ | ||
--timeout $(TIMEOUT) \ | ||
$(TESTS) | ||
|
||
test-cov: install lib-cov | ||
@CONNECT_RT_COV=1 $(MAKE) test REPORTER=dot | ||
@CONNECT_RT_COV=1 $(MAKE) test REPORTER=html-cov > coverage.html | ||
|
||
lib-cov: | ||
@rm -rf $@ | ||
@$(JSCOVERAGE) lib $@ | ||
|
||
.PHONY: test-cov test lib-cov install |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
connect-rt [](http://travis-ci.org/fengmk2/connect-rt) | ||
======= | ||
|
||
 | ||
|
||
Description | ||
|
||
* jscoverage: [100%](http://fengmk2.github.com/coverage/connect-rt.html) | ||
|
||
## Install | ||
|
||
```bash | ||
$ npm install connect-rt | ||
``` | ||
|
||
## Usage | ||
|
||
```js | ||
var connect = require('connect'); | ||
var rt = require('connect-rt'); | ||
|
||
var app = connect( | ||
rt(), | ||
function (req, res, next) { | ||
res.end('hello world'); | ||
} | ||
); | ||
``` | ||
|
||
## microtime benchmark | ||
|
||
Please see https://gist.github.com/4345606 | ||
|
||
```bash | ||
$ node timer.js | ||
|
||
Date.now() x 4,290,064 ops/sec ±3.71% (84 runs sampled) | ||
microtime.now() x 2,613,316 ops/sec ±3.58% (89 runs sampled) | ||
process.uptime() x 311,993 ops/sec ±3.29% (84 runs sampled) | ||
process.hrtime() x 678,040 ops/sec ±2.89% (89 runs sampled) | ||
Fastest is Date.now() | ||
``` | ||
|
||
## License | ||
|
||
(The MIT License) | ||
|
||
Copyright (c) 2012 fengmk2 <[email protected]> | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = process.env.CONNECT_RT_COV ? require('./lib-cov/connect-rt') : require('./lib/connect-rt'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/*! | ||
* connect-rt - lib/connect-rt.js | ||
* Copyright(c) 2012 fengmk2 <[email protected]> | ||
* MIT Licensed | ||
*/ | ||
|
||
"use strict"; | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var microtime = require('microtime'); | ||
|
||
/** | ||
* Reponse time: | ||
* | ||
* Adds the `X-Response-Time` header displaying the response | ||
* duration in milliseconds. | ||
* | ||
* @see https://github.com/senchalabs/connect/blob/master/lib/middleware/responseTime.js | ||
* @return {Function(req, res, next)} | ||
* @api public | ||
*/ | ||
|
||
module.exports = function responseTime() { | ||
return function (req, res, next) { | ||
var start = microtime.now(); | ||
|
||
if (res._responseTime) { | ||
return next(); | ||
} | ||
res._responseTime = true; | ||
|
||
res.on('header', function () { | ||
var duration = microtime.now() - start; | ||
res.setHeader('X-Response-Time', (duration / 1000) + 'ms'); | ||
}); | ||
|
||
next(); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"name": "connect-rt", | ||
"version": "0.0.1", | ||
"description": "connect response time middleware", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "make test" | ||
}, | ||
"dependencies": { | ||
"microtime": ">=0.3.3" | ||
}, | ||
"devDependencies": { | ||
"connect": "*", | ||
"should": "*", | ||
"jscover": "*", | ||
"supertest": "*", | ||
"mocha": "*" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/fengmk2/connect-rt.git" | ||
}, | ||
"keywords": [ | ||
"connect-rt", | ||
"response-time", | ||
"rt", | ||
"responseTime", | ||
"connect", | ||
"middleware", | ||
"microtime" | ||
], | ||
"author": "fengmk2 <[email protected]>", | ||
"license": "MIT" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/*! | ||
* connect-rt - test/connect-rt.test.js | ||
* Copyright(c) 2012 fengmk2 <[email protected]> | ||
* MIT Licensed | ||
*/ | ||
|
||
"use strict"; | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var rt = require('../'); | ||
var should = require('should'); | ||
var connect = require('connect'); | ||
var request = require('supertest'); | ||
|
||
describe('connect-rt.test.js', function () { | ||
var app = connect( | ||
rt(), | ||
rt(), // should work | ||
function (req, res, next) { | ||
setTimeout(function () { | ||
res.end(req.url); | ||
}, 2000 * Math.random()); | ||
} | ||
); | ||
|
||
it('should contain X-Response-time', function (done) { | ||
request(app) | ||
.get('/foo') | ||
.expect('X-Response-time', /\d{1,3}\.\d{1,3}ms/) | ||
.expect(200, done); | ||
}); | ||
|
||
}); |