@hackthedev/dsync-ratelimit 1.0.1 → 1.0.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/.github/workflows/publish.yml +43 -0
- package/README.md +14 -2
- package/bun.lock +2 -2
- package/index.mjs +32 -0
- package/package.json +3 -3
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
name: Publish to npm
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches:
|
|
6
|
+
- main
|
|
7
|
+
|
|
8
|
+
permissions:
|
|
9
|
+
contents: write
|
|
10
|
+
|
|
11
|
+
jobs:
|
|
12
|
+
publish:
|
|
13
|
+
runs-on: ubuntu-latest
|
|
14
|
+
|
|
15
|
+
steps:
|
|
16
|
+
- uses: actions/checkout@v4
|
|
17
|
+
with:
|
|
18
|
+
persist-credentials: true
|
|
19
|
+
|
|
20
|
+
- name: Skip version bump commits
|
|
21
|
+
run: |
|
|
22
|
+
if git log -1 --pretty=%B | grep -q "chore: bump version"; then
|
|
23
|
+
echo "Version bump commit detected, skipping."
|
|
24
|
+
exit 0
|
|
25
|
+
fi
|
|
26
|
+
|
|
27
|
+
- uses: actions/setup-node@v4
|
|
28
|
+
with:
|
|
29
|
+
node-version: 20
|
|
30
|
+
registry-url: https://registry.npmjs.org/
|
|
31
|
+
|
|
32
|
+
- run: npm ci
|
|
33
|
+
|
|
34
|
+
- run: |
|
|
35
|
+
git config user.name "github-actions"
|
|
36
|
+
git config user.email "actions@github.com"
|
|
37
|
+
npm version patch -m "chore: bump version %s"
|
|
38
|
+
git push
|
|
39
|
+
|
|
40
|
+
- run: npm publish --access public
|
|
41
|
+
env:
|
|
42
|
+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
|
43
|
+
|
package/README.md
CHANGED
|
@@ -87,11 +87,23 @@ io.on("connection", (socket) => {
|
|
|
87
87
|
});
|
|
88
88
|
```
|
|
89
89
|
|
|
90
|
+
------
|
|
90
91
|
|
|
92
|
+
## Consensus
|
|
91
93
|
|
|
94
|
+
A `calculateConsensus` function has been added to possibly set dynamic rate limits.
|
|
92
95
|
|
|
93
|
-
|
|
94
|
-
|
|
96
|
+
```js
|
|
97
|
+
// exmaple
|
|
98
|
+
const votes = [
|
|
99
|
+
["userA", 12, 1],
|
|
100
|
+
["userB", 13, 1],
|
|
101
|
+
["userC", 11, 1],
|
|
102
|
+
["userD", 48, 1], // spike
|
|
103
|
+
];
|
|
104
|
+
|
|
105
|
+
const { value, result, disqualified } = calculateConsensus(votes, 10);
|
|
106
|
+
```
|
|
95
107
|
|
|
96
108
|
|
|
97
109
|
|
package/bun.lock
CHANGED
|
@@ -11,8 +11,8 @@
|
|
|
11
11
|
},
|
|
12
12
|
},
|
|
13
13
|
"packages": {
|
|
14
|
-
"@hackthedev/arraytools": ["@hackthedev/arraytools@1.0.
|
|
14
|
+
"@hackthedev/arraytools": ["@hackthedev/arraytools@1.0.2", "", {}, "sha512-rFr3L60vAqp6bs+ur2W3VqaavOzoyQxDFqGYU/R+ocgZ63Hh4nat7HRZOVqZ6BVHC/UuC4Q/Y8+BiX9i3xakkg=="],
|
|
15
15
|
|
|
16
|
-
"@hackthedev/dsync-ipsec": ["@hackthedev/dsync-ipsec@1.0.
|
|
16
|
+
"@hackthedev/dsync-ipsec": ["@hackthedev/dsync-ipsec@1.0.12", "", {}, "sha512-aCPQu6Cypqa6kmIpGjmqVPsoRwaYWbtPBvhPkZjOPu/FI054DItr3gtcqCE8+sULJSzNwAo2Ol2NZP2HEWucEg=="],
|
|
17
17
|
}
|
|
18
18
|
}
|
package/index.mjs
CHANGED
|
@@ -24,6 +24,38 @@ export default class dSyncRateLimit {
|
|
|
24
24
|
return Date.now();
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
+
export function calculateConsensus(votes, tolerance = null) {
|
|
28
|
+
if (!votes || votes.length === 0) {
|
|
29
|
+
return { value: null, result: [], disqualified: [] };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// get median
|
|
33
|
+
const values = votes.map(v => v[1]).sort((a, b) => a - b);
|
|
34
|
+
const median = values.length % 2 === 0
|
|
35
|
+
? (values[values.length / 2 - 1] + values[values.length / 2]) / 2
|
|
36
|
+
: values[Math.floor(values.length / 2)];
|
|
37
|
+
|
|
38
|
+
let sumWeighted = 0;
|
|
39
|
+
let sumWeights = 0;
|
|
40
|
+
|
|
41
|
+
const result = [];
|
|
42
|
+
const disqualified = [];
|
|
43
|
+
|
|
44
|
+
for (const [host, reportedValue, weight] of votes) {
|
|
45
|
+
if (tolerance !== null && Math.abs(reportedValue - median) > tolerance) {
|
|
46
|
+
disqualified.push([host, reportedValue, weight]);
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
result.push([host, reportedValue, weight]);
|
|
50
|
+
sumWeighted += reportedValue * weight;
|
|
51
|
+
sumWeights += weight;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const value = sumWeights > 0 ? parseFloat((sumWeighted / sumWeights).toFixed(2)) : null;
|
|
55
|
+
|
|
56
|
+
return { value, result, disqualified };
|
|
57
|
+
}
|
|
58
|
+
|
|
27
59
|
check(key, limit, blockUntilDate = null) {
|
|
28
60
|
const now = Date.now();
|
|
29
61
|
let rec = this.store.get(key);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hackthedev/dsync-ratelimit",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "This small library is part of the dSync concept and will handle rate limits by either defining and using custom, generic rate limits or by using an express middleware to enforce rate limits.",
|
|
5
5
|
"homepage": "https://github.com/NETWORK-Z-Dev/dsync-ratelimit#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@hackthedev/arraytools": "^1.0.
|
|
21
|
-
"@hackthedev/dsync-ipsec": "^1.0.
|
|
20
|
+
"@hackthedev/arraytools": "^1.0.2",
|
|
21
|
+
"@hackthedev/dsync-ipsec": "^1.0.12"
|
|
22
22
|
}
|
|
23
23
|
}
|