UNPKG

2.28 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
7var Bottleneck = _interopDefault(require('bottleneck/light'));
8
9// @ts-ignore
10async function errorRequest(octokit, state, error, options) {
11 if (!error.request || !error.request.request) {
12 // address https://github.com/octokit/plugin-retry.js/issues/8
13 throw error;
14 } // retry all >= 400 && not doNotRetry
15
16
17 if (error.status >= 400 && !state.doNotRetry.includes(error.status)) {
18 const retries = options.request.retries != null ? options.request.retries : state.retries;
19 const retryAfter = Math.pow((options.request.retryCount || 0) + 1, 2);
20 throw octokit.retry.retryRequest(error, retries, retryAfter);
21 } // Maybe eventually there will be more cases here
22
23
24 throw error;
25}
26
27// @ts-ignore
28
29async function wrapRequest(state, request, options) {
30 const limiter = new Bottleneck(); // @ts-ignore
31
32 limiter.on("failed", function (error, info) {
33 const maxRetries = ~~error.request.request.retries;
34 const after = ~~error.request.request.retryAfter;
35 options.request.retryCount = info.retryCount + 1;
36
37 if (maxRetries > info.retryCount) {
38 // Returning a number instructs the limiter to retry
39 // the request after that number of milliseconds have passed
40 return after * state.retryAfterBaseValue;
41 }
42 });
43 return limiter.schedule(request, options);
44}
45
46const VERSION = "3.0.7";
47function retry(octokit, octokitOptions = {}) {
48 const state = Object.assign({
49 enabled: true,
50 retryAfterBaseValue: 1000,
51 doNotRetry: [400, 401, 403, 404, 422],
52 retries: 3
53 }, octokitOptions.retry);
54 octokit.retry = {
55 retryRequest: (error, retries, retryAfter) => {
56 error.request.request = Object.assign({}, error.request.request, {
57 retries: retries,
58 retryAfter: retryAfter
59 });
60 return error;
61 }
62 };
63
64 if (!state.enabled) {
65 return;
66 }
67
68 octokit.hook.error("request", errorRequest.bind(null, octokit, state));
69 octokit.hook.wrap("request", wrapRequest.bind(null, state));
70}
71retry.VERSION = VERSION;
72
73exports.VERSION = VERSION;
74exports.retry = retry;
75//# sourceMappingURL=index.js.map