axios-rate-limit 1.3.0 → 1.3.2

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/README.md CHANGED
@@ -2,14 +2,19 @@
2
2
 
3
3
  [![npm version](https://img.shields.io/npm/v/axios-rate-limit.svg?style=flat-square)](https://www.npmjs.com/package/axios-rate-limit)
4
4
  [![npm downloads](https://img.shields.io/npm/dt/axios-rate-limit.svg?style=flat-square)](https://www.npmjs.com/package/axios-rate-limit)
5
- [![Build Status](https://img.shields.io/travis/aishek/axios-rate-limit.svg?style=flat-square)](https://travis-ci.org/aishek/axios-rate-limit)
5
+ [![npm bundle size](https://img.shields.io/bundlephobia/minzip/axios-rate-limit?style=flat-square)](https://bundlephobia.com/package/axios-rate-limit@latest)
6
+ [![build status](https://img.shields.io/github/actions/workflow/status/aishek/axios-rate-limit/node.js.yml
7
+ )](https://github.com/aishek/axios-rate-limit/actions?query=branch%3Amaster++)
8
+ [![code coverage](https://img.shields.io/coveralls/aishek/axios-rate-limit.svg?style=flat-square)](https://coveralls.io/r/aishek/axios-rate-limit)
9
+ [![install size](https://img.shields.io/badge/dynamic/json?url=https://packagephobia.com/v2/api.json?p=axios-rate-limit&query=$.install.pretty&label=install%20size&style=flat-square)](https://packagephobia.now.sh/result?p=axios-rate-limit)
10
+ [![known vulnerabilities](https://snyk.io/test/npm/axios-rate-limit/badge.svg)](https://snyk.io/test/npm/axios-rate-limit)
6
11
 
7
- A rate limit for axios: set how many requests per interval should perform immediately, other will be delayed automatically.
12
+ A rate limit for [Axios](https://www.npmjs.com/package/axios): set how many requests per interval should perform immediately, other will be delayed automatically.
8
13
 
9
14
  ## Installing
10
15
 
11
16
  ```bash
12
- yarn add axios-rate-limit
17
+ npm install axios-rate-limit
13
18
  ```
14
19
 
15
20
  ## Usage
@@ -33,6 +38,6 @@ http.getMaxRPS() // 3
33
38
  http.setRateLimitOptions({ maxRequests: 6, perMilliseconds: 150 }) // same options as constructor
34
39
  ```
35
40
 
36
- ## A bit of advertising :-)
41
+ ## Alternatives
37
42
 
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
43
+ Consider using Axios built-in [rate-limiting](https://www.npmjs.com/package/axios#user-content--rate-limiting) functionality.
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.3.0",
4
+ "version": "1.3.2",
5
5
  "license": "MIT",
6
6
  "bugs": {
7
7
  "url": "https://github.com/aishek/axios-rate-limit/issues"
@@ -12,7 +12,7 @@
12
12
  },
13
13
  "homepage": "https://github.com/aishek/axios-rate-limit#readme",
14
14
  "devDependencies": {
15
- "@logux/eslint-config": "^28.1.0",
15
+ "@logux/eslint-config": "28.1.0",
16
16
  "axios": "^0.18.0",
17
17
  "eslint": "^5.16.0",
18
18
  "eslint-ci": "^1.0.0",
@@ -28,8 +28,7 @@
28
28
  "husky": "^1.3.1",
29
29
  "jest": "^24.7.1",
30
30
  "lint-staged": "^8.1.5",
31
- "sinon": "^7.3.2",
32
- "yaspeller-ci": "^1.0.0"
31
+ "sinon": "^7.3.2"
33
32
  },
34
33
  "peerDependencies": {
35
34
  "axios": "*"
@@ -41,10 +40,9 @@
41
40
  "typings/index.d.ts"
42
41
  ],
43
42
  "scripts": {
44
- "watch": "yarn jest --watch",
45
- "test": "jest --forceExit --runInBand --coverage --detectOpenHandles && yarn lint && yarn spell",
46
- "lint": "eslint-ci src/*.js __tests__/*.js",
47
- "spell": "yaspeller-ci *.md"
43
+ "watch": "jest --watch",
44
+ "test": "jest --forceExit --runInBand --coverage --detectOpenHandles",
45
+ "lint": "eslint-ci src/*.js __tests__/*.js"
48
46
  },
49
47
  "jest": {
50
48
  "verbose": true,
@@ -81,7 +79,6 @@
81
79
  }
82
80
  },
83
81
  "lint-staged": {
84
- "*.md": "yaspeller-ci",
85
82
  "*.js": "eslint"
86
83
  },
87
84
  "keywords": [
@@ -89,27 +86,5 @@
89
86
  "rate",
90
87
  "limit"
91
88
  ],
92
- "author": "Alexandr Borisov (aishek@gmail.com)",
93
- "yaspeller": {
94
- "lang": "en",
95
- "dictionary": [
96
- "Cifronomika",
97
- "axios",
98
- "Versioning",
99
- "jsdoc",
100
- "github",
101
- "aishek",
102
- "mcuppi",
103
- "com",
104
- "NodeJS",
105
- "TypeScript",
106
- "Unreleased",
107
- "unref",
108
- "maxRPS",
109
- "setMaxRPS",
110
- "getMaxRPS",
111
- "setRateLimitOptions",
112
- "lodash"
113
- ]
114
- }
89
+ "author": "Alexandr Borisov (aishek@gmail.com)"
115
90
  }
package/src/index.js CHANGED
@@ -49,9 +49,34 @@ AxiosRateLimit.prototype.enable = function (axios) {
49
49
  )
50
50
  }
51
51
 
52
+ /*
53
+ * from axios library (dispatchRequest.js:11)
54
+ * @param config
55
+ */
56
+ function throwIfCancellationRequested (config) {
57
+ if (config.cancelToken) {
58
+ config.cancelToken.throwIfRequested()
59
+ }
60
+ }
61
+
52
62
  AxiosRateLimit.prototype.handleRequest = function (request) {
53
- return new Promise(function (resolve) {
54
- this.push({ resolve: function () { resolve(request) } })
63
+ return new Promise(function (resolve, reject) {
64
+ this.push({
65
+ /*
66
+ * rejects a cancelled request and returns request has been resolved or not
67
+ * @returns {boolean}
68
+ */
69
+ resolve: function () {
70
+ try {
71
+ throwIfCancellationRequested(request)
72
+ } catch (error) {
73
+ reject(error)
74
+ return false
75
+ }
76
+ resolve(request)
77
+ return true
78
+ }
79
+ })
55
80
  }.bind(this))
56
81
  }
57
82
 
@@ -80,7 +105,7 @@ AxiosRateLimit.prototype.shift = function () {
80
105
  }
81
106
 
82
107
  var queued = this.queue.shift()
83
- queued.resolve()
108
+ var resolved = queued.resolve()
84
109
 
85
110
  if (this.timeslotRequests === 0) {
86
111
  this.timeoutId = setTimeout(function () {
@@ -93,6 +118,11 @@ AxiosRateLimit.prototype.shift = function () {
93
118
  }
94
119
  }
95
120
 
121
+ if (!resolved) {
122
+ this.shift() // rejected request --> shift another request
123
+ return
124
+ }
125
+
96
126
  this.timeslotRequests += 1
97
127
  }
98
128
 
@@ -1,9 +1,9 @@
1
1
  import { AxiosInstance } from 'axios';
2
2
 
3
3
  export interface RateLimitedAxiosInstance extends AxiosInstance {
4
- getMaxRPS(): number,
5
- setMaxRPS(rps:number): void,
6
- setRateLimitOptions(options: rateLimitOptions): void,
4
+ getMaxRPS: () => number,
5
+ setMaxRPS: (rps: number) => void,
6
+ setRateLimitOptions: (options: rateLimitOptions) => void,
7
7
  // enable(axios: any): void,
8
8
  // handleRequest(request:any):any,
9
9
  // handleResponse(response: any): any,
@@ -12,7 +12,11 @@ export interface RateLimitedAxiosInstance extends AxiosInstance {
12
12
  // shift():any
13
13
  }
14
14
 
15
- type rateLimitOptions = { maxRequests?: number, perMilliseconds?: number, maxRPS?:number };
15
+ export type rateLimitOptions = {
16
+ maxRequests?: number,
17
+ perMilliseconds?: number,
18
+ maxRPS?: number
19
+ };
16
20
 
17
21
  /**
18
22
  * Apply rate limit to axios instance.
@@ -33,7 +37,7 @@ type rateLimitOptions = { maxRequests?: number, perMilliseconds?: number, maxRPS
33
37
  * http.getMaxRPS() // 3
34
38
  * http.setRateLimitOptions({ maxRequests: 6, perMilliseconds: 150 }) // same options as constructor
35
39
  *
36
- * @param {Object} axios axios instance
40
+ * @param {Object} axiosInstance axios instance
37
41
  * @param {Object} options options for rate limit, available for live update
38
42
  * @param {Number} options.maxRequests max requests to perform concurrently in given amount of time.
39
43
  * @param {Number} options.perMilliseconds amount of time to limit concurrent requests.
package/CHANGELOG.md DELETED
@@ -1,44 +0,0 @@
1
- # Change Log
2
- This project adheres to [Semantic Versioning](http://semver.org/).
3
-
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
20
- * Fixed NodeJS delayed shutdown (see https://github.com/aishek/axios-rate-limit/pull/13/files)
21
-
22
- ## 1.1.1
23
- * Fixed TypeScript typings bundle (see https://github.com/aishek/axios-rate-limit/pull/12/files)
24
-
25
- ## 1.1.0
26
- * Added TypeScript typings (see https://github.com/aishek/axios-rate-limit/pull/9)
27
-
28
- ## 1.0.1
29
- * Updated internals (see https://github.com/aishek/axios-rate-limit/pull/5, https://github.com/aishek/axios-rate-limit/pull/7, https://github.com/aishek/axios-rate-limit/pull/8)
30
-
31
- ## 1.0.0
32
- * Changed exports to classic NodeJS modules (see https://github.com/aishek/axios-rate-limit/issues/3)
33
-
34
- ## 0.0.4
35
- * Add errors re-throw by @mcuppi (see https://github.com/aishek/axios-rate-limit/issues/1)
36
-
37
- ## 0.0.3
38
- * Add jsdoc.
39
-
40
- ## 0.0.2
41
- * Fix default import.
42
-
43
- ## 0.0.1
44
- * Initial release.