redgun-security 1.4.1 → 1.4.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/package.json +1 -1
- package/src/utils/fetch.js +17 -0
package/package.json
CHANGED
package/src/utils/fetch.js
CHANGED
|
@@ -1,7 +1,24 @@
|
|
|
1
1
|
import https from 'https';
|
|
2
2
|
import http from 'http';
|
|
3
3
|
|
|
4
|
+
const RATE_LIMIT = 5;
|
|
5
|
+
let lastRequestTime = 0;
|
|
6
|
+
|
|
7
|
+
async function waitForSlot() {
|
|
8
|
+
const now = Date.now();
|
|
9
|
+
const minInterval = 1000 / RATE_LIMIT;
|
|
10
|
+
const timeSinceLast = now - lastRequestTime;
|
|
11
|
+
|
|
12
|
+
if (timeSinceLast < minInterval) {
|
|
13
|
+
await new Promise((r) => setTimeout(r, minInterval - timeSinceLast));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
lastRequestTime = Date.now();
|
|
17
|
+
}
|
|
18
|
+
|
|
4
19
|
export async function fetchWithTimeout(url, options = {}, timeout = 10000) {
|
|
20
|
+
await waitForSlot();
|
|
21
|
+
|
|
5
22
|
const controller = new AbortController();
|
|
6
23
|
const timer = setTimeout(() => controller.abort(), timeout);
|
|
7
24
|
|