-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
168 lines (156 loc) · 4.87 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
var request = require('request');
var concat = require('concat-stream');
var util = require('util');
var once = require('once');
var pump = require('pump');
var ProxyStream = require('./ProxyStream');
module.exports = verbFunc();
module.exports.get = verbFunc('get');
module.exports.head = verbFunc('head');
module.exports.post = verbFunc('post');
module.exports.put = verbFunc('put');
module.exports.patch = verbFunc('patch');
module.exports.del = verbFunc('del');
const RETRIABLE_ERRORS = [
'ECONNRESET',
'ENOTFOUND',
'ESOCKETTIMEDOUT',
'ETIMEDOUT',
'ECONNREFUSED',
'EHOSTUNREACH',
'EPIPE',
'EAI_AGAIN'
];
function noop() {
}
var supportedMethods = ['POST', 'PUT', 'PATCH', 'GET', 'DELETE'];
function verbFunc(verb) {
return function () {
var params = request.initParams.apply(request, arguments);
if (verb) {
params.method = verb === 'del' ? 'DELETE' : verb.toUpperCase();
}
var maxAttempts = params.attempts || 3;
var delay = params.delay || 500;
var logFunction = params.logFunction || noop;
var attempts = 0;
if (!params.timeout) {
throw new Error('request-retry-stream you have to specify a timeout');
}
if (supportedMethods.indexOf(params.method) === -1) {
throw new Error(`request-retry-stream only supports ${supportedMethods.join(', ')} for now. PRs are welcome if you want to add support for other verbs`);
}
var callback = params.callback;
if(callback){
params.callback = function (err, resp) {
if (!shouldRetry(err, resp, attempts)) {
if (err || !/2\d\d/.test(resp && resp.statusCode)) {
//unrecoverable error
err = err || new Error('Error in request ' + ((err && err.message) || 'statusCode: ' + (resp && resp.statusCode)));
err.statusCode = (resp && resp.statusCode);
Object.assign(err, params);
err.requestBody = params.body;
err.attemptsDone = attempts;
err.body = resp && resp.body;
return callback(err);
}
callback.apply(this, arguments);
} else {
attempts++;
logFunction(err || 'request-retry-stream is retrying to perform request');
return setTimeout(() => {
request(params, params.callback);
}, attempts * delay);
}
};
attempts++;
return request(params, params.callback);
}
//no callback, streaming
var stream = new ProxyStream();
makeStreamingRequest();
var originalPipe = stream.pipe;
var destination = null;
stream.pipe = function (dest) {
destination = dest;
return originalPipe.apply(stream, arguments);
};
return stream;
function makeStreamingRequest() {
attempts++;
var potentialStream = new ProxyStream();
var success = false;
var handler = once(function (err, resp) {
if (shouldRetry(err, resp, attempts)) {
potentialStream.destroy(err || new Error('request-retry-stream is retrying this request'));
logFunction(err || 'request-retry-stream is retrying to perform request');
return setTimeout(makeStreamingRequest, attempts * delay);
}
if (err || (!params.passThrough && !/2\d\d/.test(resp && resp.statusCode))) {
//unrecoverable error
if (callback) {
return;
}
var cb = once(returnError);
var concatStream = concat(cb);
return pump(potentialStream, concatStream, cb);
}
//all good
success = true;
Object.keys(resp.headers).forEach(function (key) {
stream.setHeader(key, resp.headers[key]);
});
stream.statusCode = resp.statusCode;
stream.emit('response', resp);
stream.on('data', noop);
return pump(potentialStream, stream);
function returnError(bodyBufferOrError) {
err = err || new Error('Error in request ' + ((err && err.message) || (resp && resp.statusCode)));
err.statusCode = (resp && resp.statusCode);
Object.assign(err, params);
err.requestBody = params.body;
err.attemptsDone = attempts;
if (util.isError(bodyBufferOrError)) {
err.streamError = bodyBufferOrError;
} else {
err.body = bodyBufferOrError.slice(0, 1500).toString(); //max 1500 bytes
}
stream.destroy(err);
}
});
var req = request(params);
req.on('response', function (resp) {
handler(null, resp);
});
req.on('error', handler);
req.pipefilter = function (resp, proxy) {
if (success && destination) {
try {
for (var i in proxy._headers) {
destination.setHeader && destination.setHeader(i, proxy._headers[i]);
}
} catch (ex) {
logFunction(ex);
}
if (stream.pipefilter) {
try {
stream.pipefilter(resp, destination);
} catch (ex) {
logFunction(ex);
}
}
}
};
return pump(req, potentialStream);
}
function shouldRetry(err, resp, attempts) {
var retryableError = false;
if (err) {
retryableError = RETRIABLE_ERRORS.indexOf(err.code) !== -1;
} else {
retryableError = resp && /5\d\d/.test(resp.statusCode);
}
return attempts < maxAttempts && retryableError;
}
};
}