forked from FGRibreau/node-request-retry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
146 lines (120 loc) · 4.05 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
'use strict';
/*
* Request
*
* Copyright(c) 2014 Francois-Guillaume Ribreau <[email protected]>
* MIT Licensed
*
*/
var when = require('when');
var request = require('request');
var _ = require('fg-lodash');
var RetryStrategies = require('./strategies');
var debug = require('debug')('requestretry');
var DEFAULTS = {
maxAttempts: 5, // try 5 times
retryDelay: 5000, // wait for 5s before trying again
fullResponse: true, // resolve promise with the full response object
promiseFactory: defaultPromiseFactory // Function to use a different promise implementation library
};
// Default promise factory which use bluebird
function defaultPromiseFactory(resolver) {
return when.promise(resolver);
}
/**
* It calls the promiseFactory function passing it the resolver for the promise
*
* @param {Object} requestInstance - The Request Retry instance
* @param {Function} promiseFactoryFn - The Request Retry instance
* @return {Object} - The promise instance
*/
function makePromise(requestInstance, promiseFactoryFn) {
// Resolver function wich assigns the promise (resolve, reject) functions
// to the requestInstance
function Resolver(resolve, reject) {
this._resolve = resolve;
this._reject = reject;
}
return promiseFactoryFn(Resolver.bind(requestInstance));
}
function Request(options, f, retryConfig) {
this.maxAttempts = retryConfig.maxAttempts;
this.retryDelay = retryConfig.retryDelay;
this.fullResponse = retryConfig.fullResponse;
this.attempts = 0;
/**
* Option object
* @type {Object}
*/
this.options = options;
/**
* Return true if the request should be retried
* @type {Function} (err, response) -> Boolean
*/
this.retryStrategy = _.isFunction(options.retryStrategy) ? options.retryStrategy : RetryStrategies.HTTPOrNetworkError;
this._timeout = null;
this._req = null;
this._callback = _.isFunction(f) ? _.once(f) : null;
// create the promise only when no callback was provided
if (!this._callback) {
this._promise = makePromise(this, retryConfig.promiseFactory);
}
this.reply = function requestRetryReply(err, response, body) {
if (this._callback) {
return this._callback(err, response, body);
}
if (err) {
return this._reject(err);
}
// resolve with the full response or just the body
response = this.fullResponse ? response : body;
this._resolve(response);
};
}
Request.request = request;
Request.prototype._tryUntilFail = function () {
this.maxAttempts--;
this.attempts++;
this._req = Request.request(this.options, function (err, response, body) {
if (response) {
response.attempts = this.attempts;
}
if (this.retryStrategy(err, response) && this.maxAttempts > 0) {
debug('retrying request - retries: ' + this.attempts);
this._timeout = setTimeout(this._tryUntilFail.bind(this), this.retryDelay);
return;
}
this.reply(err, response, body);
}.bind(this));
};
Request.prototype.abort = function () {
if (this._req) {
this._req.abort();
}
clearTimeout(this._timeout);
this.reply(new Error('Aborted'));
};
// expose request methods from RequestRetry
['end', 'on', 'emit', 'once', 'setMaxListeners', 'start', 'removeListener', 'pipe', 'write'].forEach(function (requestMethod) {
Request.prototype[requestMethod] = function exposedRequestMethod () {
return this._req[requestMethod].apply(this._req, arguments);
};
});
// expose promise methods
['then', 'catch', 'finally', 'fail', 'done'].forEach(function (promiseMethod) {
Request.prototype[promiseMethod] = function exposedPromiseMethod () {
if (this._callback) {
throw new Error('A callback was provided but waiting a promise, use only one pattern');
}
return this._promise[promiseMethod].apply(this._promise, arguments);
};
});
function Factory(options, f) {
var retryConfig = _(options || {}).defaults(DEFAULTS).pick(Object.keys(DEFAULTS)).value();
var req = new Request(options, f, retryConfig);
req._tryUntilFail();
return req;
}
module.exports = Factory;
Factory.Request = Request;
Factory.RetryStrategies = RetryStrategies;