Skip to content
This repository has been archived by the owner on Feb 1, 2023. It is now read-only.

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 20, 2012
0 parents commit 12b1b52
Show file tree
Hide file tree
Showing 11 changed files with 251 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .gitignore
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
6 changes: 6 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
test/
coverage.html
lib-cov/
Makefile
.travis.yml
logo.png
5 changes: 5 additions & 0 deletions .travis.yml
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
21 changes: 21 additions & 0 deletions LICENSE.txt
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.
23 changes: 23 additions & 0 deletions Makefile
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
67 changes: 67 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
connect-rt [![Build Status](https://secure.travis-ci.org/fengmk2/connect-rt.png)](http://travis-ci.org/fengmk2/connect-rt)
=======

![logo](https://raw.github.com/fengmk2/connect-rt/master/logo.png)

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 &lt;[email protected]&gt;

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.
1 change: 1 addition & 0 deletions index.js
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');
42 changes: 42 additions & 0 deletions lib/connect-rt.js
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();
};
};
Binary file added logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions package.json
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"
}
36 changes: 36 additions & 0 deletions test/connect-rt.test.js
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);
});

});

0 comments on commit 12b1b52

Please sign in to comment.