1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
6 |
|
7 | var Bottleneck = _interopDefault(require('bottleneck/light'));
|
8 |
|
9 |
|
10 | async function errorRequest(octokit, state, error, options) {
|
11 | if (!error.request || !error.request.request) {
|
12 |
|
13 | throw error;
|
14 | }
|
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 | }
|
22 |
|
23 |
|
24 | throw error;
|
25 | }
|
26 |
|
27 |
|
28 |
|
29 | async function wrapRequest(state, request, options) {
|
30 | const limiter = new Bottleneck();
|
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 |
|
39 |
|
40 | return after * state.retryAfterBaseValue;
|
41 | }
|
42 | });
|
43 | return limiter.schedule(request, options);
|
44 | }
|
45 |
|
46 | const VERSION = "3.0.5";
|
47 | function 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 | }
|
71 | retry.VERSION = VERSION;
|
72 |
|
73 | exports.VERSION = VERSION;
|
74 | exports.retry = retry;
|
75 |
|