[Pkg-javascript-commits] [node-connect-timeout] 01/01: Imported Upstream version 1.1.0

Leo Iannacone l3on-guest at moszumanska.debian.org
Sat Jun 14 16:46:02 UTC 2014


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

l3on-guest pushed a commit to annotated tag upstream/1.1.0
in repository node-connect-timeout.

commit 5d3fca6ea1cdc4654e862d0e1c9523b50d1bc968
Author: Leo Iannacone <l3on at ubuntu.com>
Date:   Sat Jun 14 18:30:41 2014 +0200

    Imported Upstream version 1.1.0
---
 .npmignore   |   2 +
 .travis.yml  |   9 ++++
 History.md   |  17 ++++++
 README.md    | 134 +++++++++++++++++++++++++++++++++++++++++++++
 index.js     |  76 ++++++++++++++++++++++++++
 package.json |  35 ++++++++++++
 test/test.js | 174 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 447 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..1e3e05b
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,9 @@
+language: node_js
+node_js:
+  - "0.8"
+  - "0.10"
+  - "0.11"
+matrix:
+  allow_failures:
+    - node_js: "0.11"
+  fast_finish: true
\ No newline at end of file
diff --git a/History.md b/History.md
new file mode 100644
index 0000000..eba8b9c
--- /dev/null
+++ b/History.md
@@ -0,0 +1,17 @@
+1.1.0 / 2014-04-29
+==================
+
+  * Add `req.timedout` property
+  * Add `respond` option to constructor
+
+1.0.1 / 2014-04-28
+==================
+
+  * Clear timer on socket destroy
+  * Compatible with node.js 0.8
+  * deps: debug at 0.8.1
+
+1.0.0 / 2014-03-05
+==================
+
+  * Genesis from `connect`
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..d9b18b9
--- /dev/null
+++ b/README.md
@@ -0,0 +1,134 @@
+# connect-timeout [![Build Status](https://travis-ci.org/expressjs/timeout.svg)](https://travis-ci.org/expressjs/timeout) [![NPM version](https://badge.fury.io/js/connect-timeout.svg)](http://badge.fury.io/js/connect-timeout)
+
+Times out the request in `ms`, defaulting to `5000`.
+
+## Install
+
+    npm install connect-timeout
+
+## API
+
+**NOTE** This module is not recommend as a "top-level" middleware (i.e. do not recommend for use as `app.use(timeout(5000))`).
+
+### timeout(ms, options)
+
+Returns middleware that times out in `ms` milliseconds. On timeout, `req` will emit `"timeout"`.
+
+#### options
+
+* `respond` - If `true`, the timeout error is passed to `next()` so that you may customize the response behavior. This error has a `.timeout` property as well as `.status == 503`. This defaults to `true`.
+
+### req.clearTimeout()
+
+Clears the timeout on the request.
+
+### req.timedout
+
+`true` if timeout fired; `false` otherwise.
+
+## Examples
+
+### as top-level middleware
+
+```javascript
+var express = require('express');
+var timeout = require('connect-timeout');
+
+// example of using this top-level; note the use of haltOnTimedout
+// after every middleware; it will stop the request flow on a timeout
+var app = express();
+app.use(timeout(5000));
+app.use(bodyParser());
+app.use(haltOnTimedout);
+app.use(cookieParser());
+app.use(haltOnTimedout);
+
+// Add your routes here, etc.
+
+function haltOnTimedout(req, res, next){
+  if (!req.timedout) next();
+}
+
+app.listen(3000);
+```
+
+### express 3.x
+
+```javascript
+var express = require('express');
+var bodyParser = require('body-parser');
+var timeout = require('connect-timeout');
+
+var app = express();
+app.post('/save', timeout(5000), bodyParser.json(), haltOnTimedout, function(req, res, next){
+  savePost(req.body, function(err, id){
+    if (err) return next(err);
+    if (req.timedout) return;
+    res.send('saved as id ' + id);
+  });
+});
+
+function haltOnTimedout(req, res, next){
+  if (!req.timedout) next();
+}
+
+function savePost(post, cb){
+  setTimeout(function(){
+    cb(null, ((Math.random()* 40000) >>> 0));
+  }, (Math.random()* 7000) >>> 0));
+}
+
+app.listen(3000);
+```
+
+### connect 2.x
+
+```javascript
+var connect = require('connect');
+var timeout = require('connect-timeout');
+
+var app = require('connect');
+app.use('/save', timeout(5000), connect.json(), haltOnTimedout, function(req, res, next){
+  savePost(req.body, function(err, id){
+    if (err) return next(err);
+    if (req.timedout) return;
+    res.send('saved as id ' + id);
+  });
+});
+
+function haltOnTimedout(req, res, next){
+  if (!req.timedout) next();
+}
+
+function savePost(post, cb){
+  setTimeout(function(){
+    cb(null, ((Math.random()* 40000) >>> 0));
+  }, (Math.random()* 7000) >>> 0));
+}
+
+app.listen(3000);
+```
+
+## 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.
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..b8279cf
--- /dev/null
+++ b/index.js
@@ -0,0 +1,76 @@
+/*!
+ * Expressjs | Connect - timeout
+ * Ported from https://github.com/LearnBoost/connect-timeout
+ * MIT Licensed
+ */
+
+/**
+ * Module dependencies.
+ */
+
+var debug = require('debug')('connect:timeout');
+
+/**
+ * Timeout:
+ *
+ * See README.md for documentation.
+ *
+ * @param {Number} ms
+ * @param {Object} options
+ * @return {Function} middleware
+ * @api public
+ */
+
+module.exports = function timeout(ms, options) {
+  ms = ms || 5000;
+  options = options || {};
+
+  var respond = !('respond' in options) || options.respond === true;
+
+  return function(req, res, next) {
+    var destroy = req.socket.destroy;
+    var id = setTimeout(function(){
+      req.timedout = true;
+      req.emit('timeout', ms);
+    }, ms);
+
+    if (respond) {
+      req.on('timeout', onTimeout(ms, next));
+    }
+
+    req.clearTimeout = function(){
+      clearTimeout(id);
+    };
+
+    req.socket.destroy = function(){
+      clearTimeout(id);
+      destroy.call(this);
+    };
+
+    req.timedout = false;
+
+    var writeHead = res.writeHead;
+    res.writeHead = function(){
+      clearTimeout(id);
+      writeHead.apply(res, arguments);
+    }
+
+    next();
+  };
+};
+
+function generateTimeoutError(){
+  var err = new Error('Response timeout');
+  err.code = 'ETIMEDOUT';
+  err.status = 503;
+  return err;
+}
+
+function onTimeout(ms, cb){
+  return function(){
+    if (this._header) return debug('response started, cannot timeout');
+    var err = generateTimeoutError();
+    err.timeout = ms;
+    cb(err);
+  };
+}
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..d731aa4
--- /dev/null
+++ b/package.json
@@ -0,0 +1,35 @@
+{
+  "name": "connect-timeout",
+  "description": "timeout middleware",
+  "version": "1.1.0",
+  "author": {
+    "name": "Jonathan Ong",
+    "email": "me at jongleberry.com",
+    "url": "http://jongleberry.com",
+    "twitter": "https://twitter.com/jongleberry"
+  },
+  "license": "MIT",
+  "repository": {
+    "type": "git",
+    "url": "https://github.com/expressjs/timeout.git"
+  },
+  "bugs": {
+    "mail": "me at jongleberry.com",
+    "url": "https://github.com/expressjs/timeout/issues"
+  },
+  "dependencies": {
+    "debug": "0.8.1"
+  },
+  "devDependencies": {
+    "mocha": ">= 1.18.0 < 2.0",
+    "should": ">= 3.3.0 < 4.0",
+    "supertest": "*",
+    "connect": "*"
+  },
+  "engines": {
+    "node": ">= 0.8"
+  },
+  "scripts": {
+    "test": "mocha --reporter spec --require should"
+  }
+}
diff --git a/test/test.js b/test/test.js
new file mode 100644
index 0000000..0cb7690
--- /dev/null
+++ b/test/test.js
@@ -0,0 +1,174 @@
+
+process.env.NODE_ENV = 'test';
+
+var connect = require('connect');
+var request = require('supertest');
+var timeout = require('..');
+
+var app = connect()
+  .use(timeout(300))
+  .use(function(req, res){
+    res.end('Hello');
+  });
+
+describe('timeout()', function(){
+  describe('when below the timeout', function(){
+    it('should do nothing', function(done){
+      request(app.listen())
+      .get('/')
+      .expect('Hello', done);
+    })
+  })
+
+  describe('when above the timeout', function(){
+    describe('with no response made', function(){
+      it('should respond with 503 Request timeout', function(done){
+        var app = connect()
+          .use(timeout(300))
+          .use(function(req, res){
+            setTimeout(function(){
+              req.timedout.should.be.true;
+              res.end('Hello');
+            }, 400);
+          });
+
+        request(app.listen())
+        .get('/')
+        .expect(503, done);
+      })
+
+      it('should pass the error to next()', function(done){
+        var app = connect()
+          .use(timeout(300))
+          .use(function(req, res){
+            setTimeout(function(){
+              req.timedout.should.be.true;
+              res.end('Hello');
+            }, 400);
+          })
+          .use(function(err, req, res, next){
+            res.statusCode = err.status;
+            res.end('timeout of ' + err.timeout + 'ms exceeded');
+          });
+
+        request(app.listen())
+        .get('/')
+        .expect('timeout of 300ms exceeded', done);
+      })
+    })
+
+    describe('with a partial response', function(){
+      it('should do nothing', function(done){
+        var app = connect()
+          .use(timeout(300))
+          .use(function(req, res){
+            res.write('Hello');
+            setTimeout(function(){
+              req.timedout.should.be.false;
+              res.end(' World');
+            }, 400);
+          });
+
+        request(app.listen())
+        .get('/')
+        .expect('Hello World', done);
+      })
+    })
+  })
+
+  describe('options', function(){
+    it('can disable auto response', function(done){
+      var app = connect()
+        .use(timeout(300, {respond: false}))
+        .use(function(req, res){
+          setTimeout(function(){
+            res.end('Timedout ' + req.timedout);
+          }, 400);
+        });
+
+      request(app.listen())
+      .get('/')
+      .expect('Timedout true', done);
+    })
+  })
+
+  describe('req.clearTimeout()', function(){
+    it('should revert this behavior', function(done){
+      var app = connect()
+        .use(timeout(300))
+        .use(function(req, res){
+          req.clearTimeout();
+          setTimeout(function(){
+            req.timedout.should.be.false;
+            res.end('Hello');
+          }, 400);
+        });
+
+      request(app.listen())
+      .get('/')
+      .expect('Hello', done);
+    })
+  })
+
+  describe('destroy()', function(){
+    it('req should clear timer', function(done){
+      var app = connect()
+        .use(timeout(100))
+        .use(function(req, res){
+          req.destroy();
+          setTimeout(function(){
+            error.code.should.equal('ECONNRESET');
+            req.timedout.should.be.false;
+            done();
+          }, 200);
+        });
+      var error;
+
+      request(app.listen())
+      .get('/')
+      .end(function(err){
+        error = err
+      });
+    })
+
+    it('res should clear timer', function(done){
+      var app = connect()
+        .use(timeout(100))
+        .use(function(req, res){
+          res.destroy();
+          setTimeout(function(){
+            error.code.should.equal('ECONNRESET');
+            req.timedout.should.be.false;
+            done();
+          }, 200);
+        });
+      var error;
+
+      request(app.listen())
+      .get('/')
+      .end(function(err){
+        error = err
+      });
+    })
+
+    it('socket should clear timer', function(done){
+      var app = connect()
+        .use(timeout(100))
+        .use(function(req, res){
+          req.socket.destroy();
+          setTimeout(function(){
+            error.code.should.equal('ECONNRESET');
+            req.timedout.should.be.false;
+            done();
+          }, 200);
+        });
+      var error;
+
+      request(app.listen())
+      .get('/')
+      .end(function(err){
+        error = err
+      });
+    })
+  })
+})

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



More information about the Pkg-javascript-commits mailing list