-
Notifications
You must be signed in to change notification settings - Fork 5
/
token.js
78 lines (67 loc) · 1.95 KB
/
token.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
'use strict';
/**
* A representation of a single API/OAuth token.
*
* @constructor
* @param {String} OAuth The OAuth token.
* @param {String} prefix Authorization prefix.
* @param {Object} options Optional options.
* @api public
*/
function Token(OAuth, prefix, options) {
if (!this) return new Token(OAuth, prefix, options);
prefix = prefix || 'token ';
options = options || {};
options.timeout = 'timeout' in options ? options.timeout : 50;
this.cooldown = false;
this.ratelimit = Infinity;
this.ratereset = Infinity;
this.remaining = Infinity;
this.timeout = options.timout;
this.authorization = prefix + OAuth;
}
/**
* Checks if the token is available for consumption.
*
* @returns {Boolean}
* @api public
*/
Token.prototype.available = function available() {
var reset = this.ratereset >= 0 && (Date.now() >= (this.ratereset * 1000));
//
// We're in our cool down phase. We temporarily disable this token to ensure
// that other tokens can be rolled in.
//
if (this.cooldown) {
if ((this.cooldown + this.timeout) > Date.now()) return false;
this.cooldown = false;
}
//
// This token should be reset by the server, so we can attempt to reset the
// `remaining` api calls to the original rate limit.
//
if (reset && this.ratelimit >= 0 && this.ratelimit !== Infinity) {
this.remaining = this.ratelimit;
}
return this.ratelimit === Infinity // First use, unknown state.
|| this.remaining > 0 // We still tokens remaining.
|| reset; // Rate limit has reset.
};
/**
* Token has been returned to the pool.
*
* @param {Mana} mana The mana instance that used the taken
* @returns {Token}
* @api public
*/
Token.prototype.returned = function returned(mana) {
this.remaining = mana.remaining;
this.ratereset = mana.ratereset;
this.ratelimit = mana.ratelimit;
this.cooldown = Date.now();
return this;
};
//
// Expose the module.
//
module.exports = Token;