arbiter-sdk 1.0.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.
@@ -0,0 +1,39 @@
1
+ import axios from "axios";
2
+ import { ENDPOINTS } from "./endpoints.js";
3
+
4
+ export default class ArbiterClient{
5
+ constructor(){
6
+ this.timeout = 2000;
7
+ this.primary = ENDPOINTS.primary;
8
+ this.secondary = ENDPOINTS.secondary;
9
+
10
+ this.primaryClient = axios.create({
11
+ baseURL: this.primary,
12
+ timeout: this.timeout,
13
+ });
14
+
15
+ this.secondaryClient = axios.create({
16
+ baseURL: this.secondary,
17
+ timeout: this.timeout,
18
+ });
19
+ }
20
+
21
+ async decide(payload){
22
+ try {
23
+ const res = await this.primaryClient.post("/decide", payload);
24
+ return res.data;
25
+ } catch (error) {
26
+ if(!this.secondary){
27
+ throw error;
28
+ }
29
+
30
+ try {
31
+ const res = await this.secondaryClient.post("/decide", payload);
32
+ return res.data;
33
+ }catch (error) {
34
+ throw new Error("Both primary and secondary endpoints failed");
35
+ }
36
+
37
+ }
38
+ }
39
+ }
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # Arbiter Node.js SDK
2
+
3
+ Official Node.js SDK for Arbiter a developer-first traffic protection service.
Binary file
package/endpoints.js ADDED
@@ -0,0 +1,4 @@
1
+ export const ENDPOINTS = {
2
+ primary: "https://arbiter-service.onrender.com",
3
+ secondary: "http://54.172.120.251"
4
+ };
package/index.js ADDED
@@ -0,0 +1,46 @@
1
+ import ArbiterClient from "./ArbiterClient.js";
2
+
3
+ export function createArbiterClient(config) {
4
+ const client = new ArbiterClient();
5
+ const defaultAlgorithm = config.defaultAlgorithm || "leaky-bucket";
6
+ const globalWhitelist = (config.whitelist || []).map(normalizeKey);
7
+ const globalBlacklist = (config.blacklist || []).map(normalizeKey);
8
+
9
+
10
+ return {
11
+ async protect({key, rule}){
12
+ const ruleConfig = config.rules[rule];
13
+
14
+ if(!ruleConfig){
15
+ throw new Error(`Rule not found: ${rule}`);
16
+ }
17
+
18
+ const algorithm = ruleConfig.algorithm || defaultAlgorithm;
19
+
20
+ const policy = {
21
+ whitelist: (ruleConfig.policy?.whitelist || globalWhitelist).map(normalizeKey),
22
+ blacklist: (ruleConfig.policy?.blacklist || globalBlacklist).map(normalizeKey),
23
+ }
24
+
25
+ const abuse = ruleConfig.abuse || null;
26
+
27
+ return client.decide({
28
+ key: normalizeKey(key),
29
+ rule:{
30
+ limit: ruleConfig.limit,
31
+ window: ruleConfig.window,
32
+ algorithm,
33
+ },
34
+ policy,
35
+ abuse
36
+ });
37
+ }
38
+ };
39
+ }
40
+
41
+ function normalizeKey(key) {
42
+ if (typeof key === "string" && key.startsWith("::ffff:")) {
43
+ return key.replace("::ffff:", "");
44
+ }
45
+ return key;
46
+ }
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "arbiter-sdk",
3
+ "version": "1.0.0",
4
+ "description": "Official Node.js SDK for the Arbiter traffic protection service",
5
+ "type": "module",
6
+ "main": "index.js",
7
+ "exports": {
8
+ ".": "./index.js"
9
+ },
10
+ "keywords": [
11
+ "nodejs",
12
+ "node-sdk",
13
+ "rate-limiting",
14
+ "api-security",
15
+ "traffic-control",
16
+ "redis"
17
+ ],
18
+ "author": "Guna Sai",
19
+ "license": "MIT",
20
+ "dependencies": {
21
+ "axios": "^1.13.4"
22
+ }
23
+ }
package/test.js ADDED
@@ -0,0 +1,16 @@
1
+ import { createArbiterClient } from "./index.js";
2
+
3
+ const ab = createArbiterClient({
4
+ rules:{
5
+ login:{
6
+ limit: 5,
7
+ window: 10
8
+ }
9
+ }
10
+ });
11
+
12
+ for(let i=0;i<10;i++){
13
+ const res = await ab.protect({key: "user1", rule: "login"});
14
+ console.log(res);
15
+ await new Promise(r => setTimeout(r, 500));
16
+ }