forked from expressjs/timeout
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 63edd88
Showing
8 changed files
with
296 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Compiled source # | ||
################### | ||
*.com | ||
*.class | ||
*.dll | ||
*.exe | ||
*.o | ||
*.so | ||
|
||
# Packages # | ||
############ | ||
# it's better to unpack these files and commit the raw source | ||
# git has its own built in compression methods | ||
*.7z | ||
*.dmg | ||
*.gz | ||
*.iso | ||
*.jar | ||
*.rar | ||
*.tar | ||
*.zip | ||
|
||
# Logs and databases # | ||
###################### | ||
*.log | ||
*.sql | ||
*.sqlite | ||
|
||
# OS generated files # | ||
###################### | ||
.DS_Store* | ||
ehthumbs.db | ||
Icon? | ||
Thumbs.db | ||
|
||
# Node.js # | ||
########### | ||
lib-cov | ||
*.seed | ||
*.log | ||
*.csv | ||
*.dat | ||
*.out | ||
*.pid | ||
*.gz | ||
|
||
pids | ||
logs | ||
results | ||
|
||
node_modules | ||
npm-debug.log | ||
|
||
# Git # | ||
####### | ||
*.orig | ||
*.BASE.* | ||
*.BACKUP.* | ||
*.LOCAL.* | ||
*.REMOTE.* | ||
|
||
# Components # | ||
############## | ||
|
||
/build | ||
/components |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
node_js: | ||
- "0.10" | ||
- "0.11" | ||
language: node_js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
test: | ||
@NODE_ENV=test ./node_modules/.bin/mocha \ | ||
--reporter spec \ | ||
--require should | ||
|
||
.PHONY: test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Method Override | ||
|
||
Previously `connect.timeout()`. | ||
|
||
Usage: | ||
|
||
```js | ||
var app = require('connect'); | ||
app.use(require('timeout')(300)) | ||
``` | ||
|
||
## API | ||
|
||
### fn = timeout(ms) | ||
|
||
Returns middleware that times out in `ms` milliseconds. | ||
|
||
## License | ||
|
||
The MIT License (MIT) | ||
|
||
Copyright (c) 2014 Jonathan Ong [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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/*! | ||
* Connect - timeout | ||
* Ported from https://github.com/LearnBoost/connect-timeout | ||
* MIT Licensed | ||
*/ | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var debug = require('debug')('connect:timeout'); | ||
|
||
/** | ||
* Timeout: | ||
* | ||
* Times out the request in `ms`, defaulting to `5000`. The | ||
* method `req.clearTimeout()` is added to revert this behaviour | ||
* programmatically within your application's middleware, routes, etc. | ||
* | ||
* The timeout error is passed to `next()` so that you may customize | ||
* the response behaviour. This error has the `.timeout` property as | ||
* well as `.status == 503`. | ||
* | ||
* @param {Number} ms | ||
* @return {Function} | ||
* @api public | ||
*/ | ||
|
||
module.exports = function timeout(ms) { | ||
ms = ms || 5000; | ||
|
||
return function(req, res, next) { | ||
var id = setTimeout(function(){ | ||
req.emit('timeout', ms); | ||
}, ms); | ||
|
||
req.on('timeout', function(){ | ||
if (res.headersSent) return debug('response started, cannot timeout'); | ||
var err = new Error('Response timeout'); | ||
err.timeout = ms; | ||
err.status = 503; | ||
next(err); | ||
}); | ||
|
||
req.clearTimeout = function(){ | ||
clearTimeout(id); | ||
}; | ||
|
||
var writeHead = res.writeHead; | ||
res.writeHead = function(){ | ||
clearTimeout(id); | ||
writeHead.apply(res, arguments); | ||
} | ||
|
||
next(); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"name": "connect-timeout", | ||
"description": "timeout middleware", | ||
"version": "1.0.0", | ||
"author": { | ||
"name": "Jonathan Ong", | ||
"email": "[email protected]", | ||
"url": "http://jongleberry.com", | ||
"twitter": "https://twitter.com/jongleberry" | ||
}, | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/expressjs/timeout.git" | ||
}, | ||
"bugs": { | ||
"mail": "[email protected]", | ||
"url": "https://github.com/expressjs/timeout/issues" | ||
}, | ||
"dependencies": { | ||
"debug": "*" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^1.17.0", | ||
"should": "^3.0.0", | ||
"supertest": "*", | ||
"connect": "*" | ||
}, | ||
"scripts": { | ||
"test": "make test" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
|
||
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 408 Request timeout', function(done){ | ||
var app = connect() | ||
.use(timeout(300)) | ||
.use(function(req, res){ | ||
setTimeout(function(){ | ||
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(){ | ||
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(){ | ||
res.end(' World'); | ||
}, 400); | ||
}); | ||
|
||
request(app.listen()) | ||
.get('/') | ||
.expect('Hello World', 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(){ | ||
res.end('Hello'); | ||
}, 400); | ||
}); | ||
|
||
request(app.listen()) | ||
.get('/') | ||
.expect('Hello', done); | ||
}) | ||
}) | ||
}) |