Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit 238d431c5254fe90a6bd336bd30087e07a7b5d8b 0 parents
@rauchg rauchg authored
6 .gitmodules
@@ -0,0 +1,6 @@
+[submodule "support/expresso"]
+ path = support/expresso
+ url = git://github.com/visionmedia/expresso.git
+[submodule "support/connect"]
+ path = support/connect
+ url = git://github.com/senchalabs/connect.git
5 Makefile
@@ -0,0 +1,5 @@
+test:
+ @CONNECT_ENV=test ./support/expresso/bin/expresso \
+ -I support/connect/lib
+
+.PHONY: test
31 Readme.md
@@ -0,0 +1,31 @@
+# Connect-timeout middleware
+
+Connect timeout sets a global timeout for responses.
+
+## Usage
+
+ connect.createServer(
+ require('./path/to/connect-timeout')({options})
+ )
+
+### Options
+
+#### Code
+
+Error code to respond with in case of timeout (500)
+
+#### Time
+
+Number of ms after which the timeout is triggered (8000)
+
+## What about long-running requests?
+
+When the timeout is enabled, a `clearTimeout` method is attached to the request.
+
+ function(req, res, next){
+ req.clearTimeout();
+ }
+
+## Author
+
+Guillermo Rauch <guillermo@learnboost.com>
25 index.js
@@ -0,0 +1,25 @@
+module.exports = function responseTimeout(options){
+ options = options || {};
+ options.time = options.time || 8000;
+ options.code = options.code || 500;
+
+ return function responseTimeout(req, res, next){
+ var writeHead = res.writeHead,
+ timer = setTimeout(function(){
+ res.writeHead(options.code);
+ res.end();
+ }, options.time);
+
+ req.clearTimeout = function(){
+ clearTimeout(timer);
+ };
+
+ res.writeHead = function(code, headers){
+ res.writeHead = writeHead;
+ req.clearTimeout();
+ res.writeHead(code, headers);
+ };
+
+ next();
+ }
+};
1  support/connect
@@ -0,0 +1 @@
+Subproject commit 4c030b65b8bc4954f3a2f5a279bff6989bc20a15
1  support/expresso
@@ -0,0 +1 @@
+Subproject commit da10c136e9f7af4de4fb80cfc583b5def9c91c06
42 test/timeout.test.js
@@ -0,0 +1,42 @@
+/**
+ * Module dependencies.
+ */
+
+var connect = require('connect'),
+ timeout = require('../index');
+
+// Test server
+
+var server = connect.createServer(
+ timeout(),
+ function(req, res, next){
+ if (req.url == '/should/timeout'){
+ // we do nothing!
+ } else if (req.url == '/should/not/timeout') {
+ res.writeHead(200);
+ res.end();
+ } else if (req.url == '/should/interrupt/timeout') {
+ req.clearTimeout();
+ setTimeout(function(){
+ res.writeHead(200);
+ res.end();
+ }, 12000); // make sure this timer is > timeout timer
+ }
+ }
+);
+
+// Tests
+
+exports['timeouts'] = function(assert){
+ assert.response(server,
+ { url: '/should/timeout' },
+ { status: 500 });
+
+ assert.response(server,
+ { url: '/should/not/timeout' },
+ { status: 200 });
+
+ assert.response(server,
+ { url: '/should/interrupt/timeout' },
+ { status: 200 });
+};
Please sign in to comment.
Something went wrong with that request. Please try again.