-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
334 lines (315 loc) · 12.1 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
'use strict';
const util = require("util");
const req = require("request-promise-native");
([ 'emptyConfig', 'defaultConfig', 'championGGConfig' ].forEach(
config => RiotApi[config] = require('./' + config + '.json')));
const DEBUG = typeof global.it === 'function';
const delayPromise = delay => new Promise(resolve => setTimeout(resolve, delay));
/** `RiotApi(key [, config])` or `RiotApi(config)` with `config.key` set. */
function RiotApi(key, config = {}) {
if (!(this instanceof RiotApi)) return new RiotApi(...arguments);
this.config = JSON.parse(JSON.stringify(RiotApi.defaultConfig));
if (key instanceof Object)
config = key;
else
this.config.key = key;
Object.assign(this.config, config);
this.regions = {};
this.hasRegions = this.config.prefix.includes('%s');
}
RiotApi.prototype.get = function() {
if (!this.hasRegions)
return this._getRegion(null).get(this.config.prefix, ...arguments);
let [ region, ...rest ] = arguments;
rest.unshift(util.format(this.config.prefix, region));
return this._getRegion(region).get(...rest);
};
/**
* Limits requests to FACTOR fraction of normal rate limits, allowing multiple
* instances to be used across multiple processes/machines.
* This can be called at any time.
*/
RiotApi.prototype.setDistFactor = function(factor) {
if (factor <= 0 || factor > 1)
throw new Error("Factor must be greater than zero and non-greater than one.");
if (this.config.distFactor === factor)
return;
this.config.distFactor = factor;
Object.values(this.regions).forEach(r => r.updateDistFactor());
};
RiotApi.prototype._getRegion = function(region) {
if (this.regions[region])
return this.regions[region];
return (this.regions[region] = new Region(this.config));
};
/** RateLimits for a region. One app limit and any number of method limits. */
function Region(config) {
this.config = config;
this.appLimit = new RateLimit(this.config.rateLimitTypeApplication, 1, this.config);
this.methodLimits = {};
this.concurrentSema = new Semaphore(this.config.maxConcurrent);
}
Region.prototype.get = function() {
let prefix = arguments[0];
let target = arguments[1];
let suffix = this.config.endpoints;
for (let path of target.split('.')) {
suffix = suffix[path];
if (!suffix) throw new Error(util.format('Missing path "%s" in "%s".', path, target));
}
let qs = {};
let args = Array.prototype.slice.call(arguments, 2);
if (typeof args[args.length - 1] === 'object') // If last obj is query string, pop it off.
qs = args.pop();
if (suffix.split('%s').length - 1 !== args.length)
throw new Error(util.format('Wrong number of path arguments: "%j", for path "%s".', args, suffix));
if (this.config.collapseQueryArrays) {
qs = JSON.parse(JSON.stringify(qs)); // Clone object so we can modify without confusion.
Object.entries(qs).forEach(([ key, val ]) => qs[key] = Array.isArray(val) ? val.join(',') : val);
}
suffix = util.format(suffix, ...args.map(arg => encodeURIComponent(arg)));
let uri = prefix + suffix;
let rateLimits = [ this.appLimit ];
if (this.config.rateLimitTypeMethod) // Also method limit if applicable.
rateLimits.push(this._getMethodLimit(target));
let retries = 0;
let fn = () => {
let delay = RateLimit.getAllOrDelay(rateLimits);
if (delay >= 0)
return delayPromise(delay).then(fn);
let reqConfig = {
uri, qs,
forever: true, // keep-alive.
simple: false,
resolveWithFullResponse: true,
qsStringifyOptions: { arrayFormat: { indices: false }},
timeout: this.config.timeout,
};
if (this.config.keyHeader)
reqConfig.headers = { [this.config.keyHeader]: this.config.key };
else
qs[this.config.keyQueryParam] = this.config.key;
return req(reqConfig).then(res => {
rateLimits.forEach(rl => rl.onResponse(res));
if (400 === res.statusCode)
throw new Error('Bad request, responded with: ' + res.body + '\nurl:' + reqConfig.uri);
if ([ 204, 404, 422 ].includes(res.statusCode))
return null;
if (429 === res.statusCode || 500 <= res.statusCode) {
if (retries >= this.config.retries)
throw new Error('Failed after ' + retries + ' retries with code ' + res.statusCode + '.');
retries++;
return fn();
}
return JSON.parse(res.body);
}, err => {
if (err.error && [ 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ETIMEDOUT' ].includes(err.error.code)
&& retries < this.config.retries) { // Retry connection resets/timeouts.
retries++;
return fn();
}
throw new Error('Failed after ' + retries + ' retries due to: ' + err.message);
});
};
return new Promise((resolve, reject) => {
this.concurrentSema.acquire()
.then(fn)
.then(out => {
this.concurrentSema.release();
resolve(out);
}, err => {
this.concurrentSema.release();
reject(err);
});
});
};
Region.prototype.updateDistFactor = function() {
this.appLimit.setDistFactor(this.config.distFactor);
Object.values(this.methodLimits).forEach(rl => rl.setDistFactor(this.config.distFactor));
};
Region.prototype._getMethodLimit = function(method) {
if (this.methodLimits[method])
return this.methodLimits[method];
return (this.methodLimits[method] = new RateLimit(this.config.rateLimitTypeMethod, 1, this.config));
};
/** Rate limit. A collection of token buckets, updated when needed. */
function RateLimit(type, distFactor, config) {
this.config = config;
this.type = type;
this.buckets = this.config.defaultBuckets.map(b => new TokenBucket(b.timespan, b.limit, b));
this.retryAfter = 0;
this.distFactor = distFactor;
}
RateLimit.prototype.retryDelay = function() {
let now = Date.now();
return now > this.retryAfter ? -1 : this.retryAfter - now;
};
RateLimit.prototype.onResponse = function(res) {
if (429 === res.statusCode) {
let type = res.headers[this.config.headerLimitType] || this.config.defaultLimitType;
if (!type)
throw new Error('Response missing type.');
if (this.type.name === type.toLowerCase()) {
let retryAfter = +res.headers[this.config.headerRetryAfter];
if (Number.isNaN(retryAfter))
throw new Error('Response 429 missing retry-after header.');
this.retryAfter = Date.now() + retryAfter * 1000 + 500;
}
}
const limitHeader = res.headers[this.type.headerLimit];
const countHeader = res.headers[this.type.headerCount];
if (this._bucketsNeedUpdate(limitHeader, countHeader))
this.buckets = RateLimit._getBucketsFromHeaders(limitHeader, countHeader);
};
RateLimit.prototype.setDistFactor = function(factor) {
this.distFactor = factor;
this.buckets.forEach(b => b.setDistFactor(factor));
};
RateLimit.prototype._bucketsNeedUpdate = function(limitHeader, countHeader) {
if (!limitHeader || !countHeader)
return false;
let limits = this.buckets.map(b => b.toLimitString()).join(',');
return limitHeader !== limits;
};
RateLimit._getBucketsFromHeaders = function(limitHeader, countHeader) {
// Limits: "20000:10,1200000:600"
// Counts: "7:10,58:600"
let limits = limitHeader.split(',');
let counts = countHeader.split(',');
if (limits.length !== counts.length)
throw new Error('Limit and count headers do not match: ' + limitHeader + ', ' + countHeader + '.');
return limits
.map((limit, i) => {
let count = counts[i];
let [limitVal, limitSpan] = limit.split(':').map(Number);
let [countVal, countSpan] = count.split(':').map(Number);
limitSpan *= 1000;
countSpan *= 1000;
if (limitSpan != countSpan)
throw new Error('Limit span and count span do not match: ' + limitSpan + ', ' + countSpan + '.');
let bucket = new TokenBucket(limitSpan, limitVal, { distFactor: this.distFactor });
bucket.getTokens(countVal);
return bucket;
});
};
RateLimit.getAllOrDelay = function(rateLimits) {
let delay = rateLimits
.map(r => r.retryDelay())
.reduce((a, b) => Math.max(a, b), -1);
if (delay >= 0)
return delay; // Techincally the delay could be more but whatev.
let allBuckets = [].concat.apply([], rateLimits.map(rl => rl.buckets));
return TokenBucket.getAllOrDelay(allBuckets);
};
/** Token bucket. Represents a single "100:60", AKA a 100 tokens per 60 seconds pair. */
function TokenBucket(timespan, limit, { distFactor = 1, factor = 20, spread = 500 / timespan, now = Date.now } = {}) {
this.now = now;
this.timespan = timespan;
// this.givenLimit is the limit given to the constructor, this.limit is the
// functional limit, accounting for this.distFactor.
this.givenLimit = limit;
this.factor = factor;
this.spread = spread;
this.timespanIndex = Math.ceil(timespan / factor);
this.total = 0;
this.time = -1;
this.buffer = new Array(this.factor + 1).fill(0);
this.setDistFactor(distFactor);
}
TokenBucket.prototype.setDistFactor = function(distFactor) {
this.limit = this.givenLimit * distFactor;
// TODO: this math is ugly and basically wrong
this.limitPerIndex = Math.floor(this.givenLimit / this.spread / this.factor) * distFactor;
if (this.limitPerIndex * this.factor < this.limit) // TODO: hack to fix math above
this.limitPerIndex = Math.ceil(this.limit / this.factor);
};
/** Returns delay in milliseconds or -1 if token available. */
TokenBucket.prototype.getDelay = function() {
let index = this._update();
if (this.total < this.limit) {
if (this.buffer[index] >= this.limitPerIndex)
return this._getTimeToBucket(1);
return -1;
}
// check how soon into the future old buckets will be zeroed, making requests available.
let i = 1;
for (; i < this.buffer.length; i++) {
if (this.buffer[(index + i) % this.buffer.length] > 0)
break;
}
return this._getTimeToBucket(i);
}
TokenBucket.prototype.getTokens = function(n) {
let index = this._update();
this.buffer[index] += n;
this.total += n;
return this.total <= this.limit && this.buffer[index] <= this.limitPerIndex;
};
TokenBucket.prototype.toLimitString = function() {
return this.givenLimit + ':' + (this.timespan / 1000);
};
TokenBucket.prototype._update = function() {
if (this.time < 0) {
this.time = this.now();
return this._getIndex(this.time);
}
let index = this._getIndex(this.time);
let length = this._getLength(this.time, (this.time = this.now()));
if (length < 0)
throw new Error('Negative length.');
if (length == 0)
return index;
if (length >= this.buffer.length) {
this.buffer.fill(0);
this.total = 0;
return index;
}
for (let i = 0; i < length; i++) {
index++;
index %= this.buffer.length;
this.total -= this.buffer[index];
this.buffer[index] = 0;
}
if (this._getIndex(this.time) != index)
throw new Error('Get index time wrong:' + this._getIndex(this.time) + ', ' + index + '.');
return index;
};
TokenBucket.prototype._getIndex = function(ts) {
return Math.floor((ts / this.timespanIndex) % this.buffer.length);
};
TokenBucket.prototype._getLength = function(start, end) {
return Math.floor(end / this.timespanIndex) - Math.floor(start / this.timespanIndex);
};
TokenBucket.prototype._getTimeToBucket = function(n) {
return n * this.timespanIndex - (this.time % this.timespanIndex);
};
TokenBucket.getAllOrDelay = function(tokenBuckets) {
let delay = tokenBuckets
.map(b => b.getDelay())
.reduce((a, b) => Math.max(a, b), -1);
if (delay >= 0)
return delay;
tokenBuckets.forEach(b => b.getTokens(1));
return -1;
};
function Semaphore(permits) {
this._permits = permits;
this._queue = [];
}
Semaphore.prototype.acquire = function() {
return new Promise(resolve => {
if (this._permits) {
this._permits--;
resolve();
}
else
this._queue.push(resolve);
});
};
Semaphore.prototype.release = function() {
const resolve = this._queue.shift();
(resolve ? resolve() : this._permits++);
};
module.exports = RiotApi;
if (DEBUG)
Object.assign(module.exports, { delayPromise, TokenBucket, Semaphore });