7
7
* MIT Licensed
8
8
*
9
9
*/
10
- var request = require ( 'request' ) ;
11
- var _ = require ( 'fg-lodash' ) ;
10
+ var request = require ( 'request' ) ;
11
+ var _ = require ( 'fg-lodash' ) ;
12
12
var Cancelable = require ( 'cancelable' ) ;
13
13
14
14
var RETRIABLE_ERRORS = [ 'ECONNRESET' , 'ENOTFOUND' , 'ESOCKETTIMEDOUT' , 'ETIMEDOUT' , 'ECONNREFUSED' , 'EHOSTUNREACH' , 'EPIPE' ] ;
15
15
16
- var DEFAULTS = {
17
- maxAttempts : 5 , // try 5 times
16
+ var DEFAULTS = {
17
+ maxAttempts : 5 , // try 5 times
18
18
retryDelay : 5000 , // wait for 5s before trying again
19
19
} ;
20
20
21
- function Request ( options , f , maxAttempts , retryDelay ) {
21
+ function Request ( options , f , maxAttempts , retryDelay ) {
22
22
this . maxAttempts = maxAttempts ;
23
- this . retryDelay = retryDelay ;
24
- this . options = options ;
25
- this . f = _ . once ( f ) ;
26
- this . _timeout = null ;
27
- this . _req = null ;
23
+ this . retryDelay = retryDelay ;
24
+ this . options = options ;
25
+ this . f = _ . once ( f ) ;
26
+ this . _timeout = null ;
27
+ this . _req = null ;
28
28
29
29
// expose _req methods from Request
30
30
[ 'end' , 'on' , 'emit' , 'once' , 'setMaxListeners' , 'start' , 'removeListener' , 'pipe' ] . forEach ( function ( methodName ) {
@@ -36,11 +36,11 @@ function Request(options, f, maxAttempts, retryDelay) {
36
36
37
37
Request . request = request ;
38
38
39
- Request . prototype . _tryUntilFail = function ( ) {
39
+ Request . prototype . _tryUntilFail = function ( ) {
40
40
this . maxAttempts -- ;
41
41
42
- this . _req = Request . request ( this . options , function ( err , response , body ) {
43
- if ( this . _isRetriable ( err , response ) && this . maxAttempts >= 0 ) {
42
+ this . _req = Request . request ( this . options , function ( err , response , body ) {
43
+ if ( this . _isRetriable ( err , response ) && this . maxAttempts >= 0 ) {
44
44
this . _timeout = setTimeout ( this . _tryUntilFail . bind ( this ) , this . retryDelay ) ;
45
45
return ;
46
46
}
@@ -49,26 +49,26 @@ Request.prototype._tryUntilFail = function() {
49
49
} . bind ( this ) ) ;
50
50
} ;
51
51
52
- Request . prototype . _isRetriable = function ( err , response ) {
52
+ Request . prototype . _isRetriable = function ( err , response ) {
53
53
// Inspired from https://github.com/geoffreak/request-enhanced/blob/master/src/request-enhanced.coffee#L107
54
54
return ( err && _ . contains ( RETRIABLE_ERRORS , err . code ) ) || ( response && 500 <= response . statusCode && response . statusCode < 600 ) ;
55
55
} ;
56
56
57
- Request . prototype . abort = function ( ) {
58
- if ( this . _req ) {
57
+ Request . prototype . abort = function ( ) {
58
+ if ( this . _req ) {
59
59
this . _req . abort ( ) ;
60
60
}
61
61
clearTimeout ( this . _timeout ) ;
62
62
this . f ( new Error ( 'Aborted' ) ) ;
63
63
} ;
64
64
65
- Request . prototype . exposeReqFunction = function ( name ) {
66
- if ( this . _req ) {
65
+ Request . prototype . exposeReqFunction = function ( name ) {
66
+ if ( this . _req ) {
67
67
this . _req [ name ] . apply ( this . _req , Array . prototype . slice . call ( arguments , 1 ) [ 0 ] ) ;
68
68
}
69
69
} ;
70
70
71
- function Factory ( options , f ) {
71
+ function Factory ( options , f ) {
72
72
f = _ . isFunction ( f ) ? f : _ . noop ;
73
73
var retry = _ ( options || { } ) . defaults ( DEFAULTS ) . pick ( Object . keys ( DEFAULTS ) ) . value ( ) ;
74
74
var req = new Request ( options , f , retry . maxAttempts , retry . retryDelay ) ;
@@ -78,4 +78,4 @@ function Factory(options, f) {
78
78
79
79
module . exports = Factory ;
80
80
81
- Factory . Request = Request ;
81
+ Factory . Request = Request ;
0 commit comments