UNPKG

8.28 kBJavaScriptView Raw
1import BottleneckLight from 'bottleneck/light';
2
3const VERSION = "3.0.0";
4
5const noop = () => Promise.resolve();
6// @ts-ignore
7function wrapRequest(state, request, options) {
8 return state.retryLimiter.schedule(doRequest, state, request, options);
9}
10// @ts-ignore
11async function doRequest(state, request, options) {
12 const isWrite = options.method !== "GET" && options.method !== "HEAD";
13 const isSearch = options.method === "GET" && options.url.startsWith("/search/");
14 const isGraphQL = options.url.startsWith("/graphql");
15 const retryCount = ~~options.request.retryCount;
16 const jobOptions = retryCount > 0 ? { priority: 0, weight: 0 } : {};
17 if (state.clustering) {
18 // Remove a job from Redis if it has not completed or failed within 60s
19 // Examples: Node process terminated, client disconnected, etc.
20 // @ts-ignore
21 jobOptions.expiration = 1000 * 60;
22 }
23 // Guarantee at least 1000ms between writes
24 // GraphQL can also trigger writes
25 if (isWrite || isGraphQL) {
26 await state.write.key(state.id).schedule(jobOptions, noop);
27 }
28 // Guarantee at least 3000ms between requests that trigger notifications
29 if (isWrite && state.triggersNotification(options.url)) {
30 await state.notifications.key(state.id).schedule(jobOptions, noop);
31 }
32 // Guarantee at least 2000ms between search requests
33 if (isSearch) {
34 await state.search.key(state.id).schedule(jobOptions, noop);
35 }
36 const req = state.global.key(state.id).schedule(jobOptions, request, options);
37 if (isGraphQL) {
38 const res = await req;
39 if (res.data.errors != null &&
40 // @ts-ignore
41 res.data.errors.some(error => error.type === "RATE_LIMITED")) {
42 const error = Object.assign(new Error("GraphQL Rate Limit Exceeded"), {
43 headers: res.headers,
44 data: res.data
45 });
46 throw error;
47 }
48 }
49 return req;
50}
51
52const PATHS = [
53 "/orgs/:org/invitations",
54 "/repos/:owner/:repo/collaborators/:username",
55 "/repos/:owner/:repo/commits/:commit_sha/comments",
56 "/repos/:owner/:repo/issues",
57 "/repos/:owner/:repo/issues/:issue_number/comments",
58 "/repos/:owner/:repo/pulls",
59 "/repos/:owner/:repo/pulls/:pull_number/comments",
60 "/repos/:owner/:repo/pulls/:pull_number/comments/:comment_id/replies",
61 "/repos/:owner/:repo/pulls/:pull_number/merge",
62 "/repos/:owner/:repo/pulls/:pull_number/requested_reviewers",
63 "/repos/:owner/:repo/pulls/:pull_number/reviews",
64 "/repos/:owner/:repo/releases",
65 "/teams/:team_id/discussions",
66 "/teams/:team_id/discussions/:discussion_number/comments"
67];
68
69// @ts-ignore
70function routeMatcher(paths) {
71 // EXAMPLE. For the following paths:
72 /* [
73 "/orgs/:org/invitations",
74 "/repos/:owner/:repo/collaborators/:username"
75 ] */
76 // @ts-ignore
77 const regexes = paths.map(path => path
78 .split("/")
79 // @ts-ignore
80 .map(c => (c.startsWith(":") ? "(?:.+?)" : c))
81 .join("/"));
82 // 'regexes' would contain:
83 /* [
84 '/orgs/(?:.+?)/invitations',
85 '/repos/(?:.+?)/(?:.+?)/collaborators/(?:.+?)'
86 ] */
87 // @ts-ignore
88 const regex = `^(?:${regexes.map(r => `(?:${r})`).join("|")})[^/]*$`;
89 // 'regex' would contain:
90 /*
91 ^(?:(?:\/orgs\/(?:.+?)\/invitations)|(?:\/repos\/(?:.+?)\/(?:.+?)\/collaborators\/(?:.+?)))[^\/]*$
92
93 It may look scary, but paste it into https://www.debuggex.com/
94 and it will make a lot more sense!
95 */
96 return new RegExp(regex, "i");
97}
98
99// @ts-ignore
100// Workaround to allow tests to directly access the triggersNotification function.
101const regex = routeMatcher(PATHS);
102const triggersNotification = regex.test.bind(regex);
103const groups = {};
104// @ts-ignore
105const createGroups = function (Bottleneck, common) {
106 // @ts-ignore
107 groups.global = new Bottleneck.Group({
108 id: "octokit-global",
109 maxConcurrent: 10,
110 ...common
111 });
112 // @ts-ignore
113 groups.search = new Bottleneck.Group({
114 id: "octokit-search",
115 maxConcurrent: 1,
116 minTime: 2000,
117 ...common
118 });
119 // @ts-ignore
120 groups.write = new Bottleneck.Group({
121 id: "octokit-write",
122 maxConcurrent: 1,
123 minTime: 1000,
124 ...common
125 });
126 // @ts-ignore
127 groups.notifications = new Bottleneck.Group({
128 id: "octokit-notifications",
129 maxConcurrent: 1,
130 minTime: 3000,
131 ...common
132 });
133};
134function throttling(octokit, octokitOptions = {}) {
135 const { enabled = true, Bottleneck = BottleneckLight, id = "no-id", timeout = 1000 * 60 * 2, // Redis TTL: 2 minutes
136 connection
137 // @ts-ignore
138 } = octokitOptions.throttle || {};
139 if (!enabled) {
140 return;
141 }
142 const common = { connection, timeout };
143 // @ts-ignore
144 if (groups.global == null) {
145 createGroups(Bottleneck, common);
146 }
147 const state = Object.assign({
148 clustering: connection != null,
149 triggersNotification,
150 minimumAbuseRetryAfter: 5,
151 retryAfterBaseValue: 1000,
152 retryLimiter: new Bottleneck(),
153 id,
154 ...groups
155 },
156 // @ts-ignore
157 octokitOptions.throttle);
158 if (typeof state.onAbuseLimit !== "function" ||
159 typeof state.onRateLimit !== "function") {
160 throw new Error(`octokit/plugin-throttling error:
161 You must pass the onAbuseLimit and onRateLimit error handlers.
162 See https://github.com/octokit/rest.js#throttling
163
164 const octokit = new Octokit({
165 throttle: {
166 onAbuseLimit: (retryAfter, options) => {/* ... */},
167 onRateLimit: (retryAfter, options) => {/* ... */}
168 }
169 })
170 `);
171 }
172 const events = {};
173 const emitter = new Bottleneck.Events(events);
174 // @ts-ignore
175 events.on("abuse-limit", state.onAbuseLimit);
176 // @ts-ignore
177 events.on("rate-limit", state.onRateLimit);
178 // @ts-ignore
179 events.on("error", e => console.warn("Error in throttling-plugin limit handler", e));
180 // @ts-ignore
181 state.retryLimiter.on("failed", async function (error, info) {
182 const options = info.args[info.args.length - 1];
183 const isGraphQL = options.url.startsWith("/graphql");
184 if (!(isGraphQL || error.status === 403)) {
185 return;
186 }
187 const retryCount = ~~options.request.retryCount;
188 options.request.retryCount = retryCount;
189 const { wantRetry, retryAfter } = await (async function () {
190 if (/\babuse\b/i.test(error.message)) {
191 // The user has hit the abuse rate limit. (REST only)
192 // https://developer.github.com/v3/#abuse-rate-limits
193 // The Retry-After header can sometimes be blank when hitting an abuse limit,
194 // but is always present after 2-3s, so make sure to set `retryAfter` to at least 5s by default.
195 const retryAfter = Math.max(~~error.headers["retry-after"], state.minimumAbuseRetryAfter);
196 const wantRetry = await emitter.trigger("abuse-limit", retryAfter, options);
197 return { wantRetry, retryAfter };
198 }
199 if (error.headers != null &&
200 error.headers["x-ratelimit-remaining"] === "0") {
201 // The user has used all their allowed calls for the current time period (REST and GraphQL)
202 // https://developer.github.com/v3/#rate-limiting
203 const rateLimitReset = new Date(~~error.headers["x-ratelimit-reset"] * 1000).getTime();
204 const retryAfter = Math.max(Math.ceil((rateLimitReset - Date.now()) / 1000), 0);
205 const wantRetry = await emitter.trigger("rate-limit", retryAfter, options);
206 return { wantRetry, retryAfter };
207 }
208 return {};
209 })();
210 if (wantRetry) {
211 options.request.retryCount++;
212 // @ts-ignore
213 return retryAfter * state.retryAfterBaseValue;
214 }
215 });
216 octokit.hook.wrap("request", wrapRequest.bind(null, state));
217}
218throttling.VERSION = VERSION;
219throttling.triggersNotification = triggersNotification;
220
221export { throttling };
222//# sourceMappingURL=index.js.map