1
1
'use strict' ;
2
2
3
3
/*
4
- * retriablerequest
4
+ * Request
5
5
*
6
6
* Copyright(c) 2014 Francois-Guillaume Ribreau <npm@fgribreau.com>
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
+ var Cancelable = require ( 'cancelable' ) ;
12
13
13
14
var RETRIABLE_ERRORS = [ 'ECONNRESET' , 'ENOTFOUND' , 'ESOCKETTIMEDOUT' , 'ETIMEDOUT' , 'ECONNREFUSED' ] ;
14
15
@@ -17,26 +18,50 @@ var DEFAULTS = {
17
18
retryDelay : 5000 , // wait for 5s before trying again
18
19
} ;
19
20
20
- function isRetriableRequest ( err , response ) {
21
- // Inspired from https://github.com/geoffreak/request-enhanced/blob/master/src/request-enhanced.coffee#L107
22
- return ( err && _ . contains ( RETRIABLE_ERRORS , err . code ) ) || ( response && 500 <= response . statusCode && response . statusCode < 600 ) ;
21
+ function Request ( options , f , maxAttempts , retryDelay ) {
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
28
}
24
29
25
- function tryUntilFail ( options , f , retryOptions ) {
26
- retryOptions . maxAttempts -- ;
30
+ Request . request = request ;
31
+
32
+ Request . prototype . _tryUntilFail = function ( ) {
33
+ this . maxAttempts -- ;
27
34
28
- request ( options , function ( err , response , body ) {
29
- if ( isRetriableRequest ( err , response ) && retryOptions . maxAttempts >= 0 ) {
30
- return setTimeout ( tryUntilFail . bind ( null , options , f , retryOptions ) , retryOptions . retryDelay ) ;
35
+ this . _req = Request . request ( this . options , function ( err , response , body ) {
36
+ if ( this . _isRetriable ( err , response ) && this . maxAttempts >= 0 ) {
37
+ this . _timeout = setTimeout ( this . _tryUntilFail . bind ( this ) , this . retryDelay ) ;
38
+ return ;
31
39
}
32
40
33
- return f ( err , response , body ) ;
34
- } ) ;
35
- }
41
+ return this . f ( err , response , body ) ;
42
+ } . bind ( this ) ) ;
43
+ } ;
44
+
45
+ Request . prototype . _isRetriable = function ( err , response ) {
46
+ // Inspired from https://github.com/geoffreak/request-enhanced/blob/master/src/request-enhanced.coffee#L107
47
+ return ( err && _ . contains ( RETRIABLE_ERRORS , err . code ) ) || ( response && 500 <= response . statusCode && response . statusCode < 600 ) ;
48
+ } ;
36
49
37
- function RetriableRequest ( options , f ) {
38
- var retryOptions = _ ( options || { } ) . defaults ( DEFAULTS ) . pick ( Object . keys ( DEFAULTS ) ) . value ( ) ;
39
- tryUntilFail ( options , f , retryOptions ) ;
50
+ Request . prototype . abort = function ( ) {
51
+ if ( this . _req ) {
52
+ this . _req . abort ( ) ;
53
+ }
54
+ clearTimeout ( this . _timeout ) ;
55
+ this . f ( new Error ( 'Aborted' ) ) ;
56
+ } ;
57
+
58
+ function Factory ( options , f ) {
59
+ var retry = _ ( options || { } ) . defaults ( DEFAULTS ) . pick ( Object . keys ( DEFAULTS ) ) . value ( ) ;
60
+ var req = new Request ( options , f , retry . maxAttempts , retry . retryDelay ) ;
61
+ req . _tryUntilFail ( ) ;
62
+ return req ;
40
63
}
41
64
42
- module . exports = RetriableRequest ;
65
+ module . exports = Factory ;
66
+
67
+ Factory . Request = Request ;
0 commit comments