Socket
Socket
Sign inDemoInstall

token-dealer

Package Overview
Dependencies
3
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.1 to 1.0.2

20

index.js

@@ -54,3 +54,3 @@ 'use strict';

function dealToken(tokens, fn, options, errors) {
function dealToken(tokens, fn, options) {
const chosen = chooseToken(tokens, options);

@@ -66,3 +66,2 @@

usage: chosen.overallUsage,
errors,
}));

@@ -72,14 +71,16 @@ }

return new Promise((resolve) => setTimeout(resolve, waitTime))
.then(() => dealToken(tokens, fn, options, errors));
.then(() => dealToken(tokens, fn, options));
}
chosen.usage.pending += 1;
let retryOnFailure = false;
return Promise.resolve()
.then(() => {
return fn(chosen.token, (reset, failed) => {
retryOnFailure = !!failed;
return fn(chosen.token, (reset, retry) => {
chosen.usage.exhausted = true;
chosen.usage.reset = reset;
if (retry) {
throw Object.assign(new Error('Token is exhausted, retrying..'), { code: 'ETOKENSEXHAUSTED' });
}
});

@@ -93,5 +94,4 @@ })

if (retryOnFailure) {
errors.push(err);
return dealToken(tokens, fn, options, errors);
if (err && err.code === 'ETOKENSEXHAUSTED') {
return dealToken(tokens, fn, options);
}

@@ -117,3 +117,3 @@

return dealToken(tokens, fn, options, []);
return dealToken(tokens, fn, options);
}

@@ -120,0 +120,0 @@

{
"name": "token-dealer",
"version": "1.0.1",
"version": "1.0.2",
"description": "Circumvent API rate limits by having several API tokens and let the dealer manage and give them to you",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -34,3 +34,3 @@ # token-dealer

Basically the only thing you must do is call `exhaust(reset, [failed])` whenever you know that the token may not be used again until `reset` (timestamp in ms). Additionally, you may pass `failed=true` if the operation you were trying to do with the token failed because its rate limit was reached. If the promise is rejected and `exhaust` was called with `failed=true`, `fn` will be called again but with a different token.
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 with the token failed because the token was exhausted, causing `fn` to be called again with another token.

@@ -50,4 +50,6 @@ Here's an example from a request to the [GitHub API](https://developer.github.com/v3/#rate-limiting) using [got](https://www.npmjs.com/package/got):

const handleRateLimit = (response, err) => {
if (response.headers['x-ratelimit-remaining'] === '0') {
exhaust(Number(response.headers['x-ratelimit-reset']) * 1000, err && err.statusCode === 403);
const headers = response.headers;
if (headers['x-ratelimit-remaining'] === '0') {
exhaust(Number(headers['x-ratelimit-reset']) * 1000, err && err.statusCode === 403);
}

@@ -61,6 +63,6 @@ };

.then((response) => {
handleRateLimit(response);
handleResponse(response);
return response;
}, (err) => {
err.response && handleRateLimit(err.response, err);
err.response && handleResponse(err.response, err);
throw err;

@@ -71,2 +73,4 @@ });

// ...
}, (err) => {
// If all tokens are exhausted, err.code will be 'EALLTOKENSEXHAUSTED'
});

@@ -73,0 +77,0 @@ ```

@@ -49,3 +49,2 @@ 'use strict';

exhaust(Date.now() + 2000, true);
throw new Error('foo');
}

@@ -164,3 +163,2 @@ });

exhaust(resetTimestamps[resetTimestamps.length - 1], true);
throw new Error('foo');
});

@@ -178,7 +176,2 @@ }, { lru })

});
expect(err.errors.length).to.equal(2);
err.errors.forEach((err) => {
expect(err).to.be.an.instanceOf(Error);
expect(err.message).to.equal('foo');
});
})

@@ -194,3 +187,2 @@ // Should give A followed by B and then fail

exhaust(resetTimestamps[resetTimestamps.length - 1], true);
throw new Error('foo');
});

@@ -207,3 +199,3 @@ }, { lru, wait: false });

it('should not redeal tokens if exhaust is called with fail != true', () => {
it('should not re-deal tokens if exhaust is called with fail != true', () => {
const tokens = ['A', 'B'];

@@ -210,0 +202,0 @@ const suppliedTokens = [];

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc