Sha256: 6690759222266a3e62d09ba99caebeff4e960c09bd4dcdec24a5327ea6d7d528

Contents?: true

Size: 1.43 KB

Versions: 17

Compression:

Stored size: 1.43 KB

Contents

/*!
 * connect-timeout
 * Copyright(c) 2014 Jonathan Ong
 * Copyright(c) 2014 Douglas Christopher Wilson
 * MIT Licensed
 */

/**
 * Module dependencies.
 */

var debug = require('debug')('connect:timeout');
var ms = require('ms');
var onHeaders = require('on-headers');

/**
 * Timeout:
 *
 * See README.md for documentation.
 *
 * @param {Number} time
 * @param {Object} options
 * @return {Function} middleware
 * @api public
 */

module.exports = function timeout(time, options) {
  options = options || {};

  time = typeof time === 'string'
    ? ms(time)
    : Number(time || 5000);

  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', time);
    }, time);

    if (respond) {
      req.on('timeout', onTimeout(time, next));
    }

    req.clearTimeout = function(){
      clearTimeout(id);
    };

    req.socket.destroy = function(){
      clearTimeout(id);
      destroy.call(this);
    };

    req.timedout = false;

    onHeaders(res, function(){
      clearTimeout(id);
    });

    next();
  };
};

function generateTimeoutError(){
  var err = new Error('Response timeout');
  err.code = 'ETIMEDOUT';
  err.status = 503;
  return err;
}

function onTimeout(time, cb){
  return function(){
    var err = generateTimeoutError();
    err.timeout = time;
    cb(err);
  };
}

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
hooch-0.4.2 jasmine/node_modules/karma/node_modules/connect/node_modules/connect-timeout/index.js
hooch-0.4.1 jasmine/node_modules/karma/node_modules/connect/node_modules/connect-timeout/index.js
hooch-0.4.0 jasmine/node_modules/karma/node_modules/connect/node_modules/connect-timeout/index.js
hooch-0.3.0 jasmine/node_modules/karma/node_modules/connect/node_modules/connect-timeout/index.js
hooch-0.2.1 jasmine/node_modules/karma/node_modules/connect/node_modules/connect-timeout/index.js
hooch-0.2.0 jasmine/node_modules/karma/node_modules/connect/node_modules/connect-timeout/index.js
hooch-0.1.0 jasmine/node_modules/karma/node_modules/connect/node_modules/connect-timeout/index.js
hooch-0.0.8 jasmine/node_modules/karma/node_modules/connect/node_modules/connect-timeout/index.js
hooch-0.0.7 jasmine/node_modules/karma/node_modules/connect/node_modules/connect-timeout/index.js
hooch-0.0.6 jasmine/node_modules/karma/node_modules/connect/node_modules/connect-timeout/index.js
entangled-0.0.16 spec/dummy/public/node_modules/karma/node_modules/connect/node_modules/connect-timeout/index.js
entangled-0.0.15 spec/dummy/public/node_modules/karma/node_modules/connect/node_modules/connect-timeout/index.js
entangled-0.0.14 spec/dummy/public/node_modules/karma/node_modules/connect/node_modules/connect-timeout/index.js
entangled-0.0.13 spec/dummy/public/node_modules/karma/node_modules/connect/node_modules/connect-timeout/index.js
entangled-0.0.12 spec/dummy/public/node_modules/karma/node_modules/connect/node_modules/connect-timeout/index.js
entangled-0.0.11 spec/dummy/public/node_modules/karma/node_modules/connect/node_modules/connect-timeout/index.js
entangled-0.0.10 spec/dummy/public/node_modules/karma/node_modules/connect/node_modules/connect-timeout/index.js