Circumvent API rate limits by having several API tokens and let the dealer manage and give them to you.
Several public APIs, such as GitHub and Twitter, have rate limits applied per account. To multiply these rate limits, you must have a farm of tokens associated to multiple accounts, either donated or created by you. This is where token-dealer
comes in, making it easy to
manage these tokens and their usage.
$ npm install token-dealer
Calls fn(token, exhaust)
with the most appropriate token
from tokens
and a exhaust
function that you may call to signal that the token is exhausted.
Basically the only thing you must do is call exhaust(reset, [retry])
whenever you know that the token may not be used again until reset
(timestamp in ms). Additionally, you may retry if the operation you were trying to do failed because the token was exhausted, causing fn
to be called again with another token.
Here's an example from a request to the GitHub API using got:
const tokenDealer = require('tokenDealer');
const got = require('got');
const tokens = [
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb',
];
tokenDealer(tokens, (token, exhaust) => {
const handleRateLimit = (response, err) => {
if (response.headers['x-ratelimit-remaining'] === '0') {
const isRateLimitError = err && err.statusCode === 403 && /rate limit/i.test(response.body.message);
exhaust(Number(response.headers['x-ratelimit-reset']) * 1000, isRateLimitError);
}
};
return got('https://api.github.com/repos/moxystudio/node-cross-spawn', {
json: true,
headers: { Authorization: `token ${token}` },
})
.then((response) => {
handleRateLimit(response);
return response;
}, (err) => {
err.response && handleRateLimit(err.response, err);
throw err;
});
})
.then((response) => {
// ...
}, (err) => {
// If all tokens are exhausted, err.code will be 'EALLTOKENSEXHAUSTED'
});
Available options:
group
: The group associated to the tokens; this effectively groups tokens to prevent conflicts (e.g.github
, defaults todefault
).wait
: True to wait for a token to be free in case all are exhausted (defaults tofalse
); alternatively you may pass a function that will be called with[token, duration]
so that you can decide to wait dynamically.lru
: A custom LRU cache instance to be used internally.onExhausted
: Called with(token, reset)
whenever a token become exhausted (defaults tonull
).
If tokens
is nullish or an empty array, the given token
will be an empty string.
Get the tokens usage for tokens
. The available options are group
and lru
which are the same as tokenDealer()
.
const tokenDealer = require('tokenDealer');
const tokens = [
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb',
];
const usage = tokenDealer.getTokensUsage(tokens);
// `usage` looks like this:
// {
// 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa': {
// exhausted: true, // true if exhausted, false otherwise
// reset: 1455996883369, // the timestamp in which the token will become available again
// inflight: 0, // the number of deliveries (`fn` calls) that still haven't completed
// },
// 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb': {
// // ...
// }
// }
$ npm test
$ npm test -- --watch
during development
Released under the MIT License.