[Pkg-javascript-commits] [node-response-time] 01/02: Imported Upstream version 2.0.0

Leo Iannacone l3on-guest at moszumanska.debian.org
Tue Jun 17 16:39:11 UTC 2014


This is an automated email from the git hooks/post-receive script.

l3on-guest pushed a commit to branch master
in repository node-response-time.

commit f14cbc2761847739540ed305159705c29693f87a
Author: Leo Iannacone <l3on at ubuntu.com>
Date:   Tue Jun 17 18:26:18 2014 +0200

    Imported Upstream version 2.0.0
---
 .npmignore   |  2 ++
 .travis.yml  | 10 ++++++++++
 HISTORY.md   | 13 +++++++++++++
 README.md    | 39 +++++++++++++++++++++++++++++++++++++++
 index.js     | 44 ++++++++++++++++++++++++++++++++++++++++++++
 package.json | 40 ++++++++++++++++++++++++++++++++++++++++
 test/test.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 199 insertions(+)

diff --git a/.npmignore b/.npmignore
new file mode 100644
index 0000000..ac0bfb9
--- /dev/null
+++ b/.npmignore
@@ -0,0 +1,2 @@
+test/
+.travis.yml
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..bb47c1b
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,10 @@
+language: node_js
+node_js:
+  - "0.8"
+  - "0.10"
+  - "0.11"
+matrix:
+  allow_failures:
+    - node_js: "0.11"
+  fast_finish: true
+script: "npm run-script test-travis"
diff --git a/HISTORY.md b/HISTORY.md
new file mode 100644
index 0000000..80ff2dd
--- /dev/null
+++ b/HISTORY.md
@@ -0,0 +1,13 @@
+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`
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..02a7cbb
--- /dev/null
+++ b/README.md
@@ -0,0 +1,39 @@
+# response-time
+
+[![NPM version](https://badge.fury.io/js/response-time.svg)](https://badge.fury.io/js/response-time)
+[![Build Status](https://travis-ci.org/expressjs/response-time.svg?branch=master)](https://travis-ci.org/expressjs/response-time)
+
+Response time middleware extracted from connect.
+
+## Installation
+
+```sh
+$ npm install response-time
+```
+
+## API
+
+```js
+var responseTime = require('response-time')
+
+// time starts ticking from the moment req goes through the middleware
+app.use(responseTime(5))
+```
+
+### responseTime(digits)
+
+Returns middleware that adds a `X-Response-Time` header to responses.
+
+- `digits` - the fixed number of digits to include. (default: `3`)
+
+## License
+
+The MIT License (MIT)
+
+Copyright (c) 2014 Jonathan Ong me at 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..7d743fc
--- /dev/null
+++ b/index.js
@@ -0,0 +1,44 @@
+
+/*!
+ * Connect - responseTime
+ * Copyright(c) 2011 TJ Holowaychuk
+ * MIT Licensed
+ */
+
+/**
+ * Module dependencies
+ */
+
+var onHeaders = require('on-headers')
+
+/**
+ * Reponse time:
+ *
+ * Adds the `X-Response-Time` header displaying the response
+ * duration in milliseconds.
+ *
+ * @param {number} [digits=3]
+ * @return {function}
+ * @api public
+ */
+
+module.exports = function responseTime(digits) {
+  digits = digits === undefined
+    ? 3
+    : digits
+
+  return function responseTime(req, res, next) {
+    var startAt = process.hrtime()
+
+    onHeaders(res, function () {
+      if (this.getHeader('X-Response-Time')) return;
+
+      var diff = process.hrtime(startAt)
+      var ms = diff[0] * 1e3 + diff[1] * 1e-6
+
+      this.setHeader('X-Response-Time', ms.toFixed(digits) + 'ms')
+    })
+
+    next()
+  }
+}
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..b19085a
--- /dev/null
+++ b/package.json
@@ -0,0 +1,40 @@
+{
+  "name": "response-time",
+  "description": "X-Response-Time header for node.js",
+  "version": "2.0.0",
+  "author": {
+    "name": "Jonathan Ong",
+    "email": "me at jongleberry.com",
+    "url": "http://jongleberry.com",
+    "twitter": "https://twitter.com/jongleberry"
+  },
+  "contributors": [
+    {
+      "name": "Douglas Christopher Wilson",
+      "email": "doug at somethingdoug.com"
+    }
+  ],
+  "license": "MIT",
+  "repository": {
+    "type": "git",
+    "url": "https://github.com/expressjs/response-time.git"
+  },
+  "bugs": {
+    "mail": "me at jongleberry.com",
+    "url": "https://github.com/expressjs/response-time/issues"
+  },
+  "dependencies": {
+    "on-headers": "0.0.0"
+  },
+  "devDependencies": {
+    "mocha": "~1.20.0",
+    "supertest": "~0.13.0"
+  },
+  "engines": {
+    "node": ">= 0.8.0"
+  },
+  "scripts": {
+    "test": "mocha --reporter dot",
+    "test-travis": "mocha --reporter spec"
+  }
+}
diff --git a/test/test.js b/test/test.js
new file mode 100644
index 0000000..977614e
--- /dev/null
+++ b/test/test.js
@@ -0,0 +1,51 @@
+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 no digits', function (done) {
+      var server = createServer(0)
+      request(server)
+      .get('/')
+      .expect('X-Response-Time', /^[0-9]+ms$/, done)
+    })
+  })
+})
+
+function createServer(digits, fn) {
+  var _responseTime = responseTime(digits)
+  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)
+    })
+  })
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-response-time.git



More information about the Pkg-javascript-commits mailing list