@hocuspocus/extension-throttle 3.4.6-rc.0 → 3.4.6-rc.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 +2 -2
- package/dist/index.js +0 -69
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hocuspocus/extension-throttle",
|
|
3
|
-
"version": "3.4.6-rc.
|
|
3
|
+
"version": "3.4.6-rc.2",
|
|
4
4
|
"description": "hocuspocus throttle extension",
|
|
5
5
|
"homepage": "https://hocuspocus.dev",
|
|
6
6
|
"keywords": [
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"dist"
|
|
29
29
|
],
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@hocuspocus/server": "^3.4.6-rc.
|
|
31
|
+
"@hocuspocus/server": "^3.4.6-rc.2"
|
|
32
32
|
},
|
|
33
33
|
"gitHead": "b3454a4ca289a84ddfb7fa5607a2d4b8d5c37e9d",
|
|
34
34
|
"repository": {
|
package/dist/index.js
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
//#region packages/extension-throttle/src/index.ts
|
|
2
|
-
var Throttle = class {
|
|
3
|
-
/**
|
|
4
|
-
* Constructor
|
|
5
|
-
*/
|
|
6
|
-
constructor(configuration) {
|
|
7
|
-
this.configuration = {
|
|
8
|
-
throttle: 15,
|
|
9
|
-
banTime: 5,
|
|
10
|
-
consideredSeconds: 60,
|
|
11
|
-
cleanupInterval: 90
|
|
12
|
-
};
|
|
13
|
-
this.connectionsByIp = /* @__PURE__ */ new Map();
|
|
14
|
-
this.bannedIps = /* @__PURE__ */ new Map();
|
|
15
|
-
this.configuration = {
|
|
16
|
-
...this.configuration,
|
|
17
|
-
...configuration
|
|
18
|
-
};
|
|
19
|
-
this.cleanupInterval = setInterval(this.clearMaps.bind(this), this.configuration.cleanupInterval * 1e3);
|
|
20
|
-
}
|
|
21
|
-
onDestroy() {
|
|
22
|
-
if (this.cleanupInterval) clearInterval(this.cleanupInterval);
|
|
23
|
-
return Promise.resolve();
|
|
24
|
-
}
|
|
25
|
-
clearMaps() {
|
|
26
|
-
this.connectionsByIp.forEach((value, key) => {
|
|
27
|
-
const filteredValue = value.filter((timestamp) => timestamp + this.configuration.consideredSeconds * 1e3 > Date.now());
|
|
28
|
-
if (filteredValue.length) this.connectionsByIp.set(key, filteredValue);
|
|
29
|
-
else this.connectionsByIp.delete(key);
|
|
30
|
-
});
|
|
31
|
-
this.bannedIps.forEach((value, key) => {
|
|
32
|
-
if (!this.isBanned(key)) this.bannedIps.delete(key);
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
isBanned(ip) {
|
|
36
|
-
const bannedAt = this.bannedIps.get(ip) || 0;
|
|
37
|
-
return Date.now() < bannedAt + this.configuration.banTime * 60 * 1e3;
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Throttle requests
|
|
41
|
-
* @private
|
|
42
|
-
*/
|
|
43
|
-
throttle(ip) {
|
|
44
|
-
if (!this.configuration.throttle) return false;
|
|
45
|
-
if (this.isBanned(ip)) return true;
|
|
46
|
-
this.bannedIps.delete(ip);
|
|
47
|
-
const previousConnections = this.connectionsByIp.get(ip) || [];
|
|
48
|
-
previousConnections.push(Date.now());
|
|
49
|
-
const previousConnectionsInTheConsideredInterval = previousConnections.filter((timestamp) => timestamp + this.configuration.consideredSeconds * 1e3 > Date.now());
|
|
50
|
-
this.connectionsByIp.set(ip, previousConnectionsInTheConsideredInterval);
|
|
51
|
-
if (previousConnectionsInTheConsideredInterval.length > this.configuration.throttle) {
|
|
52
|
-
this.bannedIps.set(ip, Date.now());
|
|
53
|
-
return true;
|
|
54
|
-
}
|
|
55
|
-
return false;
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* onConnect hook
|
|
59
|
-
* @param data
|
|
60
|
-
*/
|
|
61
|
-
onConnect(data) {
|
|
62
|
-
const { request } = data;
|
|
63
|
-
const ip = request.headers["x-real-ip"] || request.headers["x-forwarded-for"] || request.socket.remoteAddress || "";
|
|
64
|
-
return this.throttle(ip) ? Promise.reject() : Promise.resolve();
|
|
65
|
-
}
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
//#endregion
|
|
69
|
-
export { Throttle };
|