axios-rate-limit 1.1.2 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -1
- package/README.md +10 -2
- package/package.json +8 -2
- package/src/index.js +50 -15
- package/typings/index.d.ts +40 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,7 +1,22 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
This project adheres to [Semantic Versioning](http://semver.org/).
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
## 1.3.0
|
|
5
|
+
* Add TypeScript typings (see https://github.com/aishek/axios-rate-limit/pull/23)
|
|
6
|
+
* Upgrade handlebars to 4.5.3
|
|
7
|
+
* Upgrade acorn to 5.7.4
|
|
8
|
+
* Upgrade lodash to 4.17.19
|
|
9
|
+
|
|
10
|
+
## 1.2.1
|
|
11
|
+
* Fix TypeScript imports (see https://github.com/aishek/axios-rate-limit/pull/19/files)
|
|
12
|
+
|
|
13
|
+
## 1.2.0
|
|
14
|
+
* Add maxRPS options, getMaxRPS, setMaxRPS, setRateLimitOptions methods
|
|
15
|
+
|
|
16
|
+
## 1.1.3
|
|
17
|
+
* Fixed ref/unref timeout behaviour for NodeJS (see https://github.com/aishek/axios-rate-limit/issues/16)
|
|
18
|
+
|
|
19
|
+
## 1.1.2
|
|
5
20
|
* Fixed NodeJS delayed shutdown (see https://github.com/aishek/axios-rate-limit/pull/13/files)
|
|
6
21
|
|
|
7
22
|
## 1.1.1
|
package/README.md
CHANGED
|
@@ -19,12 +19,20 @@ import axios from 'axios';
|
|
|
19
19
|
import rateLimit from 'axios-rate-limit';
|
|
20
20
|
|
|
21
21
|
// sets max 2 requests per 1 second, other will be delayed
|
|
22
|
-
|
|
22
|
+
// note maxRPS is a shorthand for perMilliseconds: 1000, and it takes precedence
|
|
23
|
+
// if specified both with maxRequests and perMilliseconds
|
|
24
|
+
const http = rateLimit(axios.create(), { maxRequests: 2, perMilliseconds: 1000, maxRPS: 2 })
|
|
25
|
+
http.getMaxRPS() // 2
|
|
23
26
|
http.get('https://example.com/api/v1/users.json?page=1') // will perform immediately
|
|
24
27
|
http.get('https://example.com/api/v1/users.json?page=2') // will perform immediately
|
|
25
28
|
http.get('https://example.com/api/v1/users.json?page=3') // will perform after 1 second from the first one
|
|
29
|
+
|
|
30
|
+
// options hot-reloading also available
|
|
31
|
+
http.setMaxRPS(3)
|
|
32
|
+
http.getMaxRPS() // 3
|
|
33
|
+
http.setRateLimitOptions({ maxRequests: 6, perMilliseconds: 150 }) // same options as constructor
|
|
26
34
|
```
|
|
27
35
|
|
|
28
36
|
## A bit of advertising :-)
|
|
29
37
|
|
|
30
|
-
Since 2010
|
|
38
|
+
Since 2010 run my own software development company [Cifronomika](http://cifronomika.com/). We doing Ruby on Rails and JavaScript development. Feel free to contact
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "axios-rate-limit",
|
|
3
3
|
"description": "Rate limit for axios.",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.3.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"bugs": {
|
|
7
7
|
"url": "https://github.com/aishek/axios-rate-limit/issues"
|
|
@@ -103,7 +103,13 @@
|
|
|
103
103
|
"com",
|
|
104
104
|
"NodeJS",
|
|
105
105
|
"TypeScript",
|
|
106
|
-
"Unreleased"
|
|
106
|
+
"Unreleased",
|
|
107
|
+
"unref",
|
|
108
|
+
"maxRPS",
|
|
109
|
+
"setMaxRPS",
|
|
110
|
+
"getMaxRPS",
|
|
111
|
+
"setRateLimitOptions",
|
|
112
|
+
"lodash"
|
|
107
113
|
]
|
|
108
114
|
}
|
|
109
115
|
}
|
package/src/index.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
function AxiosRateLimit (
|
|
2
|
-
this.maxRequests = options.maxRequests
|
|
3
|
-
this.perMilliseconds = options.perMilliseconds
|
|
4
|
-
|
|
1
|
+
function AxiosRateLimit (axios) {
|
|
5
2
|
this.queue = []
|
|
6
3
|
this.timeslotRequests = 0
|
|
7
4
|
|
|
@@ -13,7 +10,28 @@ function AxiosRateLimit (options) {
|
|
|
13
10
|
this.handleRequest = this.handleRequest.bind(this)
|
|
14
11
|
this.handleResponse = this.handleResponse.bind(this)
|
|
15
12
|
|
|
16
|
-
this.enable(
|
|
13
|
+
this.enable(axios)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
AxiosRateLimit.prototype.getMaxRPS = function () {
|
|
17
|
+
var perSeconds = (this.perMilliseconds / 1000)
|
|
18
|
+
return this.maxRequests / perSeconds
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
AxiosRateLimit.prototype.setMaxRPS = function (rps) {
|
|
22
|
+
this.setRateLimitOptions({
|
|
23
|
+
maxRequests: rps,
|
|
24
|
+
perMilliseconds: 1000
|
|
25
|
+
})
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
AxiosRateLimit.prototype.setRateLimitOptions = function (options) {
|
|
29
|
+
if (options.maxRPS) {
|
|
30
|
+
this.setMaxRPS(options.maxRPS)
|
|
31
|
+
} else {
|
|
32
|
+
this.perMilliseconds = options.perMilliseconds
|
|
33
|
+
this.maxRequests = options.maxRequests
|
|
34
|
+
}
|
|
17
35
|
}
|
|
18
36
|
|
|
19
37
|
AxiosRateLimit.prototype.enable = function (axios) {
|
|
@@ -53,19 +71,28 @@ AxiosRateLimit.prototype.shiftInitial = function () {
|
|
|
53
71
|
|
|
54
72
|
AxiosRateLimit.prototype.shift = function () {
|
|
55
73
|
if (!this.queue.length) return
|
|
56
|
-
if (this.timeslotRequests === this.maxRequests)
|
|
74
|
+
if (this.timeslotRequests === this.maxRequests) {
|
|
75
|
+
if (this.timeoutId && typeof this.timeoutId.ref === 'function') {
|
|
76
|
+
this.timeoutId.ref()
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return
|
|
80
|
+
}
|
|
57
81
|
|
|
58
82
|
var queued = this.queue.shift()
|
|
59
83
|
queued.resolve()
|
|
60
84
|
|
|
61
85
|
if (this.timeslotRequests === 0) {
|
|
62
|
-
|
|
86
|
+
this.timeoutId = setTimeout(function () {
|
|
63
87
|
this.timeslotRequests = 0
|
|
64
88
|
this.shift()
|
|
65
89
|
}.bind(this), this.perMilliseconds)
|
|
66
90
|
|
|
67
|
-
if (typeof timeoutId.unref === 'function')
|
|
91
|
+
if (typeof this.timeoutId.unref === 'function') {
|
|
92
|
+
if (this.queue.length === 0) this.timeoutId.unref()
|
|
93
|
+
}
|
|
68
94
|
}
|
|
95
|
+
|
|
69
96
|
this.timeslotRequests += 1
|
|
70
97
|
}
|
|
71
98
|
|
|
@@ -77,23 +104,31 @@ AxiosRateLimit.prototype.shift = function () {
|
|
|
77
104
|
* import rateLimit from 'axios-rate-limit';
|
|
78
105
|
*
|
|
79
106
|
* // sets max 2 requests per 1 second, other will be delayed
|
|
80
|
-
*
|
|
107
|
+
* // note maxRPS is a shorthand for perMilliseconds: 1000, and it takes precedence
|
|
108
|
+
* // if specified both with maxRequests and perMilliseconds
|
|
109
|
+
* const http = rateLimit(axios.create(), { maxRequests: 2, perMilliseconds: 1000, maxRPS: 2 })
|
|
110
|
+
* http.getMaxRPS() // 2
|
|
81
111
|
* http.get('https://example.com/api/v1/users.json?page=1') // will perform immediately
|
|
82
112
|
* http.get('https://example.com/api/v1/users.json?page=2') // will perform immediately
|
|
83
113
|
* http.get('https://example.com/api/v1/users.json?page=3') // will perform after 1 second from the first one
|
|
114
|
+
* http.setMaxRPS(3)
|
|
115
|
+
* http.getMaxRPS() // 3
|
|
116
|
+
* http.setRateLimitOptions({ maxRequests: 6, perMilliseconds: 150 }) // same options as constructor
|
|
84
117
|
*
|
|
85
118
|
* @param {Object} axios axios instance
|
|
86
|
-
* @param {Object} options options for rate limit
|
|
119
|
+
* @param {Object} options options for rate limit, available for live update
|
|
87
120
|
* @param {Number} options.maxRequests max requests to perform concurrently in given amount of time.
|
|
88
121
|
* @param {Number} options.perMilliseconds amount of time to limit concurrent requests.
|
|
89
122
|
* @returns {Object} axios instance with interceptors added
|
|
90
123
|
*/
|
|
91
124
|
function axiosRateLimit (axios, options) {
|
|
92
|
-
new AxiosRateLimit(
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
125
|
+
var rateLimitInstance = new AxiosRateLimit(axios)
|
|
126
|
+
rateLimitInstance.setRateLimitOptions(options)
|
|
127
|
+
|
|
128
|
+
axios.getMaxRPS = AxiosRateLimit.prototype.getMaxRPS.bind(rateLimitInstance)
|
|
129
|
+
axios.setMaxRPS = AxiosRateLimit.prototype.setMaxRPS.bind(rateLimitInstance)
|
|
130
|
+
axios.setRateLimitOptions = AxiosRateLimit.prototype.setRateLimitOptions
|
|
131
|
+
.bind(rateLimitInstance)
|
|
97
132
|
|
|
98
133
|
return axios
|
|
99
134
|
}
|
package/typings/index.d.ts
CHANGED
|
@@ -1,10 +1,45 @@
|
|
|
1
1
|
import { AxiosInstance } from 'axios';
|
|
2
2
|
|
|
3
|
-
interface RateLimitedAxiosInstance extends AxiosInstance {
|
|
3
|
+
export interface RateLimitedAxiosInstance extends AxiosInstance {
|
|
4
|
+
getMaxRPS(): number,
|
|
5
|
+
setMaxRPS(rps:number): void,
|
|
6
|
+
setRateLimitOptions(options: rateLimitOptions): void,
|
|
7
|
+
// enable(axios: any): void,
|
|
8
|
+
// handleRequest(request:any):any,
|
|
9
|
+
// handleResponse(response: any): any,
|
|
10
|
+
// push(requestHandler:any):any,
|
|
11
|
+
// shiftInitial():any,
|
|
12
|
+
// shift():any
|
|
13
|
+
}
|
|
4
14
|
|
|
5
|
-
|
|
15
|
+
type rateLimitOptions = { maxRequests?: number, perMilliseconds?: number, maxRPS?:number };
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Apply rate limit to axios instance.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* import axios from 'axios';
|
|
22
|
+
* import rateLimit from 'axios-rate-limit';
|
|
23
|
+
*
|
|
24
|
+
* // sets max 2 requests per 1 second, other will be delayed
|
|
25
|
+
* // note maxRPS is a shorthand for perMilliseconds: 1000, and it takes precedence
|
|
26
|
+
* // if specified both with maxRequests and perMilliseconds
|
|
27
|
+
* const http = rateLimit(axios.create(), { maxRequests: 2, perMilliseconds: 1000, maxRPS: 2 })
|
|
28
|
+
* http.getMaxRPS() // 2
|
|
29
|
+
* http.get('https://example.com/api/v1/users.json?page=1') // will perform immediately
|
|
30
|
+
* http.get('https://example.com/api/v1/users.json?page=2') // will perform immediately
|
|
31
|
+
* http.get('https://example.com/api/v1/users.json?page=3') // will perform after 1 second from the first one
|
|
32
|
+
* http.setMaxRPS(3)
|
|
33
|
+
* http.getMaxRPS() // 3
|
|
34
|
+
* http.setRateLimitOptions({ maxRequests: 6, perMilliseconds: 150 }) // same options as constructor
|
|
35
|
+
*
|
|
36
|
+
* @param {Object} axios axios instance
|
|
37
|
+
* @param {Object} options options for rate limit, available for live update
|
|
38
|
+
* @param {Number} options.maxRequests max requests to perform concurrently in given amount of time.
|
|
39
|
+
* @param {Number} options.perMilliseconds amount of time to limit concurrent requests.
|
|
40
|
+
* @returns {Object} axios instance with interceptors added
|
|
41
|
+
*/
|
|
42
|
+
export default function axiosRateLimit(
|
|
6
43
|
axiosInstance: AxiosInstance,
|
|
7
|
-
options:
|
|
44
|
+
options: rateLimitOptions
|
|
8
45
|
): RateLimitedAxiosInstance;
|
|
9
|
-
|
|
10
|
-
export default axiosRateLimit;
|