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 0cfe18c
Showing
9 changed files
with
257 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,7 @@ | ||
root = true | ||
|
||
[*] | ||
end_of_line = lf | ||
insert_final_newline = true | ||
indent_style = space | ||
indent_size = 4 |
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,2 @@ | ||
/node_modules | ||
/npm-debug.* |
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,63 @@ | ||
{ | ||
"predef": [ | ||
"console", | ||
"require", | ||
"define", | ||
"describe", | ||
"it", | ||
"before", | ||
"beforeEach", | ||
"after", | ||
"afterEach" | ||
], | ||
|
||
"node": true, | ||
"devel": true, | ||
|
||
"bitwise": true, | ||
"curly": true, | ||
"eqeqeq": false, | ||
"forin": false, | ||
"immed": true, | ||
"latedef": false, | ||
"newcap": true, | ||
"noarg": true, | ||
"noempty": false, | ||
"nonew": true, | ||
"plusplus": false, | ||
"regexp": true, | ||
"undef": true, | ||
"unused": true, | ||
"quotmark": "single", | ||
"strict": true, | ||
"trailing": true, | ||
|
||
"asi": false, | ||
"boss": false, | ||
"debug": false, | ||
"eqnull": true, | ||
"es5": false, | ||
"esnext": false, | ||
"evil": false, | ||
"expr": false, | ||
"funcscope": false, | ||
"globalstrict": false, | ||
"iterator": false, | ||
"lastsemic": false, | ||
"laxbreak": false, | ||
"laxcomma": false, | ||
"loopfunc": true, | ||
"multistr": false, | ||
"onecase": true, | ||
"regexdash": false, | ||
"scripturl": false, | ||
"smarttabs": false, | ||
"shadow": false, | ||
"sub": false, | ||
"supernew": false, | ||
"validthis": false, | ||
|
||
"nomen": false, | ||
"onevar": false, | ||
"white": true | ||
} |
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 @@ | ||
language: node_js | ||
node_js: | ||
- "0.10" | ||
- "0.8" |
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,19 @@ | ||
Copyright (c) 2012 IndigoUnited | ||
|
||
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,45 @@ | ||
# request-replay [![Build Status](https://secure.travis-ci.org/IndigoUnited/node-request-replay.png)](http://travis-ci.org/IndigoUnited/node-request-replay.png) | ||
|
||
Replays a [request](https://github.com/mikeal/request) when a network error occurs using the [retry](https://github.com/felixge/node-retry) module. | ||
|
||
|
||
## Installation | ||
|
||
`$ npm install request-replay` | ||
|
||
|
||
## Usage | ||
|
||
```js | ||
var fs = require('fs'); | ||
var request = require('request'); | ||
var replay = require('request-replay'); | ||
|
||
// Note that the options argument is optional | ||
// Accepts the same options the retry module does and an additional | ||
// errorCodes array that default to ['EADDRINFO', 'ETIMEDOUT', 'ECONNRESET'] | ||
replay(request('http://google.com/doodle.png'), {}}) | ||
.on('error', function (err) { | ||
// Do something with err | ||
}) | ||
.pipe(fs.createWriteStream('doodle.png')) | ||
.on('error', function (err) { | ||
// Do something with err | ||
}) | ||
.on('close', function (err) { | ||
// Saved to doogle.png! | ||
}) | ||
``` | ||
|
||
Note that the default retry options are modified to be more appropriate for requests: | ||
|
||
* `retries`: The maximum amount of times to retry the operation. Default is `5`. | ||
* `factor`: The exponential factor to use. Default is `2`. | ||
* `minTimeout`: The amount of time before starting the first retry. Default is `2000`. | ||
* `maxTimeout`: The maximum amount of time between two retries. Default is `35000`. | ||
* `randomize`: Randomizes the timeouts by multiplying with a factor between `1` to `2`. Default is `false`. | ||
|
||
|
||
## License | ||
|
||
Released under the [MIT License](http://www.opensource.org/licenses/mit-license.php). |
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,60 @@ | ||
'use strict'; | ||
|
||
var retry = require('retry'); | ||
var deepExtend = require('deep-extend'); | ||
|
||
var errorCodes = [ | ||
'EADDRINFO', | ||
'ETIMEDOUT', | ||
'ECONNRESET' | ||
]; | ||
|
||
function requestReplay(request, options) { | ||
var originalEmit = request.emit; | ||
var operation; | ||
var attempts = 0; | ||
|
||
options = deepExtend({ | ||
errorCodes: errorCodes, | ||
retries: 5, | ||
factor: 3, | ||
minTimeout: 2000, | ||
maxTimeout: 35000 | ||
}, options || {}); | ||
|
||
options.errorCodes = options.errorCodes || errorCodes; | ||
|
||
operation = retry.operation(options); | ||
operation.attempt(function () { | ||
if (attempts) { | ||
request.start(); | ||
} | ||
|
||
attempts++; | ||
}); | ||
|
||
request.emit = function (name, error) { | ||
// If not an error, pass-through | ||
if (name !== 'error') { | ||
return originalEmit.apply(this, arguments); | ||
} | ||
|
||
// If not a retry error code, pass-through | ||
if (options.errorCodes.indexOf(error.code) === -1) { | ||
return originalEmit.call(this, name, error); | ||
} | ||
|
||
// Retry | ||
if (operation.retry(error)) { | ||
return 0; | ||
} | ||
|
||
// No more retries available, error out | ||
error.replays = attempts - 1; | ||
return originalEmit.call(this, name, error); | ||
}; | ||
|
||
return request; | ||
} | ||
|
||
module.exports = requestReplay; |
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,35 @@ | ||
{ | ||
"name": "request-replay", | ||
"version": "0.1.0", | ||
"description": "Replays a request when a network error occurs", | ||
"main": "index.js", | ||
"dependencies": { | ||
"retry": "~0.6.0", | ||
"deep-extend": "~0.2.5" | ||
}, | ||
"devDependencies": { | ||
"mocha": "~1.12.0", | ||
"expect.js": "~0.2.0", | ||
"nock": "~0.22.0", | ||
"request": "~2" | ||
}, | ||
"scripts": { | ||
"test": "mocha -R spec" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/IndigoUnited/node-request-replay" | ||
}, | ||
"bugs": { | ||
"url": "http://github.com/IndigoUnited/node-request-replay/issues" | ||
}, | ||
"keywords": [ | ||
"retry", | ||
"replay", | ||
"request", | ||
"mikeal", | ||
"network" | ||
], | ||
"author": "IndigoUnited <hello@indigounited.com> (http://indigounited.com)", | ||
"license": "MIT" | ||
} |
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,22 @@ | ||
'use strict'; | ||
|
||
var request = require('request'); | ||
var expect = require('expect.js'); | ||
var replay = require('../'); | ||
|
||
describe('request-replay', function () { | ||
it('should retry on network error', function (next) { | ||
var stream; | ||
var error; | ||
|
||
error = new Error('foo'); | ||
error.code = 'ECONNRESET'; | ||
|
||
stream = replay(request.get('http://somedomainthatwillneverexistforsure.com:8089', function (error) { | ||
expect(error).to.be.an(Error); | ||
expect(error.code).to.equal('ENOTFOUND'); | ||
expect(error.replays).to.equal(5); | ||
next(); | ||
}), { errorCodes: ['ENOTFOUND'], factor: 1, minTimeout: 10, maxTimeout: 10 }); | ||
}); | ||
}); |