-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathrateLimiting.js
More file actions
207 lines (189 loc) · 6.97 KB
/
Copy pathrateLimiting.js
File metadata and controls
207 lines (189 loc) · 6.97 KB
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
/**
* @typedef {Object} ParsedRateLimits
* @property {number} shortTermUsage
* @property {number} shortTermLimit
* @property {number} longTermUsage
* @property {number} longTermLimit
* @property {number} readShortTermUsage
* @property {number} readShortTermLimit
* @property {number} readLongTermUsage
* @property {number} readLongTermLimit
*/
/**
* In-memory rate limit state for Strava API (short/long and read limits), including methods.
* @typedef {Object} RateLimitState
* @property {Date} requestTime
* @property {number} shortTermLimit
* @property {number} longTermLimit
* @property {number} shortTermUsage
* @property {number} longTermUsage
* @property {number} readShortTermLimit
* @property {number} readLongTermLimit
* @property {number} readShortTermUsage
* @property {number} readLongTermUsage
* @property {function(): boolean} exceeded
* @property {function(): number} fractionReached
* @property {function(): boolean} readExceeded
* @property {function(): number} readFractionReached
* @property {function({ [x: string]: string|string[]|undefined }): ParsedRateLimits|null} parseRateLimits
* @property {function(this: RateLimitState, { [x: string]: string|string[]|undefined }): ParsedRateLimits|null} updateRateLimits
* @property {function(this: RateLimitState): void} clear
*/
var RateLimit = /** @type {RateLimitState} */ ({
requestTime: new Date(),
shortTermLimit: 0,
longTermLimit: 0,
shortTermUsage: 0,
longTermUsage: 0,
readShortTermLimit: 0,
readLongTermLimit: 0,
readShortTermUsage: 0,
readLongTermUsage: 0
})
/**
* Returns whether the most recent request exceeded the (write) rate limit.
* Call as `strava.rateLimiting.exceeded()`.
* @this {RateLimitState}
* @returns {boolean} True if short-term or long-term limit was reached.
*/
RateLimit.exceeded = function () {
if (this.shortTermLimit > 0 && this.shortTermUsage >= this.shortTermLimit) {
return true
}
if (this.longTermLimit > 0 && this.longTermUsage >= this.longTermLimit) {
return true
}
return false
}
/**
* Returns the current fraction of overall rate used, the greater of short and long term.
* Call as `strava.rateLimiting.fractionReached()`.
* @this {RateLimitState}
* @returns {number} Fraction in [0, 1+] (0 if no limits set).
*/
RateLimit.fractionReached = function () {
var shortLimitFraction = this.shortTermLimit > 0
? this.shortTermUsage / this.shortTermLimit
: 0
var longLimitFraction = this.longTermLimit > 0
? this.longTermUsage / this.longTermLimit
: 0
if (shortLimitFraction > longLimitFraction) {
return shortLimitFraction
} else {
return longLimitFraction
}
}
/**
* Returns whether the most recent request exceeded the read rate limit.
* Call as `strava.rateLimiting.readExceeded()`.
* @this {RateLimitState}
* @returns {boolean} True if read short-term or long-term limit was reached.
*/
RateLimit.readExceeded = function () {
if (this.readShortTermLimit > 0 && this.readShortTermUsage >= this.readShortTermLimit) {
return true
}
if (this.readLongTermLimit > 0 && this.readLongTermUsage >= this.readLongTermLimit) {
return true
}
return false
}
/**
* Returns the current fraction of read rate used, the greater of short and long term.
* Call as `strava.rateLimiting.readFractionReached()`.
* @this {RateLimitState}
* @returns {number} Fraction in [0, 1+] (0 if no limits set).
*/
RateLimit.readFractionReached = function () {
var shortLimitFraction = this.readShortTermLimit > 0
? this.readShortTermUsage / this.readShortTermLimit
: 0
var longLimitFraction = this.readLongTermLimit > 0
? this.readLongTermUsage / this.readLongTermLimit
: 0
if (shortLimitFraction > longLimitFraction) {
return shortLimitFraction
} else {
return longLimitFraction
}
}
/**
* Parses Strava rate-limit response headers into numeric usage/limit fields.
* @param {Object.<string, string|string[]|undefined>} headers - Response headers (e.g. from axios); must include x-ratelimit-limit, x-ratelimit-usage, x-readratelimit-limit, x-readratelimit-usage.
* @returns {ParsedRateLimits|null} Parsed limits object or null if headers are missing/invalid.
*/
RateLimit.parseRateLimits = function (headers) {
if (!headers || !headers['x-ratelimit-limit'] || !headers['x-ratelimit-usage'] ||
!headers['x-readratelimit-limit'] || !headers['x-readratelimit-usage']) {
return null
}
var limit = /** @type {string} */ (headers['x-ratelimit-limit']).split(',')
var usage = /** @type {string} */ (headers['x-ratelimit-usage']).split(',')
var readLimit = /** @type {string} */ (headers['x-readratelimit-limit']).split(',')
var readUsage = /** @type {string} */ (headers['x-readratelimit-usage']).split(',')
var radix = 10
return /** @type {ParsedRateLimits} */ ({
shortTermUsage: parseInt(usage[0], radix),
shortTermLimit: parseInt(limit[0], radix),
longTermUsage: parseInt(usage[1], radix),
longTermLimit: parseInt(limit[1], radix),
readShortTermUsage: parseInt(readUsage[0], radix),
readShortTermLimit: parseInt(readLimit[0], radix),
readLongTermUsage: parseInt(readUsage[1], radix),
readLongTermLimit: parseInt(readLimit[1], radix)
})
}
/**
* Updates internal rate limit state from response headers.
* @this {RateLimitState}
* @param {Object.<string, string|string[]|undefined>} headers - Response headers containing rate limit fields.
* @returns {ParsedRateLimits|null} Parsed limits object if headers were valid, otherwise null.
*/
RateLimit.updateRateLimits = /** @type {RateLimitState['updateRateLimits']} */ (
/**
* @this {RateLimitState}
*/
function (headers) {
var newLimits = this.parseRateLimits(headers)
if (newLimits) {
this.requestTime = new Date()
this.shortTermLimit =
!isNaN(newLimits.shortTermLimit) ? newLimits.shortTermLimit : 0
this.shortTermUsage =
!isNaN(newLimits.shortTermUsage) ? newLimits.shortTermUsage : 0
this.longTermLimit =
!isNaN(newLimits.longTermLimit) ? newLimits.longTermLimit : 0
this.longTermUsage =
!isNaN(newLimits.longTermUsage) ? newLimits.longTermUsage : 0
this.readShortTermLimit =
!isNaN(newLimits.readShortTermLimit) ? newLimits.readShortTermLimit : 0
this.readShortTermUsage =
!isNaN(newLimits.readShortTermUsage) ? newLimits.readShortTermUsage : 0
this.readLongTermLimit =
!isNaN(newLimits.readLongTermLimit) ? newLimits.readLongTermLimit : 0
this.readLongTermUsage =
!isNaN(newLimits.readLongTermUsage) ? newLimits.readLongTermUsage : 0
} else {
this.clear()
}
return newLimits
}
)
/**
* Resets all rate limit counters and timestamps to zero.
* @this {RateLimitState}
* @returns {void}
*/
RateLimit.clear = function () {
this.requestTime = new Date()
this.shortTermLimit = 0
this.longTermLimit = 0
this.shortTermUsage = 0
this.longTermUsage = 0
this.readShortTermLimit = 0
this.readLongTermLimit = 0
this.readShortTermUsage = 0
this.readLongTermUsage = 0
}
module.exports = RateLimit