-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
77 lines (67 loc) · 1.98 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
/**
* Store of the APIs.
* @type {Map<any, any>}
*/
const apis = new Map();
/**
* Build a new Rlac.
* @param {Object} api API to be proxified
* @param {String} apiId a unique ID
* @param {Number} timeLimit time limit
* @param {Number} quantityLimit amount limit
* @returns {Object}
*/
export function rlac(api, apiId, timeLimit, quantityLimit) {
if (!api || !apiId) {
throw new Error('An API is needed');
}
if (apis.get(apiId)) {
return apis.get(apiId);
}
if (!timeLimit || !quantityLimit) {
throw new Error('Rate limits needed');
}
let currentCalled = 0;
let totalCalled = 0;
// The resetter
setInterval(() => currentCalled = 0, timeLimit);
// Creating the proxy to call any method of the api
const theProxy = new Proxy(api, {
get(target, prop, receiver) {
const value = target[prop];
if (value instanceof Function) {
return async function proxified(...args) {
return rateLimitedAPICaller(() => value.apply(this === receiver ? target : this, args));
}
}
return Reflect.get(target, prop);
},
});
apis.set(apiId, theProxy);
/**
* Using an async function to get data when ready.
* @param {Function} cb the callback which will be called when ready
* @return {Promise} the promise to wait for
*/
async function rateLimitedAPICaller(cb) {
await waitForYourTurn();
totalCalled += 1;
return cb();
}
/**
* Wait until the limit is not exceeded anymore.
* @return {Promise} to resolve when ready
*/
async function waitForYourTurn() {
const poll = (resolve) => {
if (currentCalled < quantityLimit) {
currentCalled += 1;
resolve();
} else {
setTimeout(() => poll(resolve), 400);
}
}
return new Promise(poll);
}
return theProxy;
}