@prosopo/load-balancer 2.6.17 → 2.8.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,207 @@
1
1
  # @prosopo/load-balancer
2
2
 
3
+ ## 2.8.0
4
+ ### Minor Changes
5
+
6
+ - 04d0f1a: weighted random provider selection
7
+
8
+ ### Patch Changes
9
+
10
+ - Updated dependencies [1e3a838]
11
+ - @prosopo/config@3.1.20
12
+ - @prosopo/common@3.1.20
13
+ - @prosopo/types@3.5.3
14
+
15
+ ## 2.7.12
16
+ ### Patch Changes
17
+
18
+ - 5659b24: Release 3.4.4
19
+ - Updated dependencies [f912439]
20
+ - Updated dependencies [5659b24]
21
+ - @prosopo/common@3.1.19
22
+ - @prosopo/types@3.5.2
23
+ - @prosopo/util@3.1.4
24
+ - @prosopo/config@3.1.19
25
+
26
+ ## 2.7.11
27
+ ### Patch Changes
28
+
29
+ - 50c4120: Release 3.4.3
30
+ - Updated dependencies [52cd544]
31
+ - Updated dependencies [b117ba3]
32
+ - Updated dependencies [50c4120]
33
+ - @prosopo/types@3.5.1
34
+ - @prosopo/common@3.1.18
35
+ - @prosopo/util@3.1.3
36
+ - @prosopo/config@3.1.18
37
+
38
+ ## 2.7.10
39
+ ### Patch Changes
40
+
41
+ - 618703f: Release 3.4.2
42
+ - Updated dependencies [618703f]
43
+ - Updated dependencies [e20ad6b]
44
+ - @prosopo/common@3.1.17
45
+ - @prosopo/types@3.5.0
46
+ - @prosopo/util@3.1.2
47
+ - @prosopo/config@3.1.17
48
+
49
+ ## 2.7.9
50
+ ### Patch Changes
51
+
52
+ - 11303d9: Release 3.4.0
53
+ - 18cb28b: Release 3.4.1
54
+ - Updated dependencies [11303d9]
55
+ - Updated dependencies [18cb28b]
56
+ - Updated dependencies [11303d9]
57
+ - @prosopo/common@3.1.16
58
+ - @prosopo/types@3.4.1
59
+ - @prosopo/util@3.1.1
60
+ - @prosopo/config@3.1.16
61
+
62
+ ## 2.7.8
63
+ ### Patch Changes
64
+
65
+ - f3f7aec: Release 3.4.0
66
+ - Updated dependencies [f3f7aec]
67
+ - Updated dependencies [6768f14]
68
+ - @prosopo/common@3.1.15
69
+ - @prosopo/types@3.4.0
70
+ - @prosopo/util@3.1.0
71
+ - @prosopo/config@3.1.15
72
+
73
+ ## 2.7.7
74
+ ### Patch Changes
75
+
76
+ - Release 3.3.1
77
+ - 0824221: Release 3.2.4
78
+ - Updated dependencies [97edf3f]
79
+ - Updated dependencies
80
+ - Updated dependencies [0824221]
81
+ - @prosopo/types@3.3.0
82
+ - @prosopo/common@3.1.14
83
+ - @prosopo/util@3.0.17
84
+ - @prosopo/config@3.1.14
85
+
86
+ ## 2.7.6
87
+ ### Patch Changes
88
+
89
+ - 008d112: Release 3.3.0
90
+ - Updated dependencies [509be28]
91
+ - Updated dependencies [008d112]
92
+ - @prosopo/types@3.2.1
93
+ - @prosopo/common@3.1.13
94
+ - @prosopo/util@3.0.16
95
+ - @prosopo/config@3.1.13
96
+
97
+ ## 2.7.5
98
+ ### Patch Changes
99
+
100
+ - 0824221: Release 3.2.4
101
+ - Updated dependencies [cf48565]
102
+ - Updated dependencies [0824221]
103
+ - @prosopo/types@3.2.0
104
+ - @prosopo/common@3.1.12
105
+ - @prosopo/util@3.0.15
106
+ - @prosopo/config@3.1.12
107
+
108
+ ## 2.7.4
109
+ ### Patch Changes
110
+
111
+ - 1a23649: Release 3.2.3
112
+ - Updated dependencies [0d1a33e]
113
+ - Updated dependencies [0d1a33e]
114
+ - Updated dependencies [1a23649]
115
+ - @prosopo/types@3.1.4
116
+ - @prosopo/common@3.1.11
117
+ - @prosopo/util@3.0.14
118
+ - @prosopo/config@3.1.11
119
+
120
+ ## 2.7.3
121
+ ### Patch Changes
122
+
123
+ - 657a827: Release 3.2.2
124
+ - Updated dependencies [657a827]
125
+ - @prosopo/common@3.1.10
126
+ - @prosopo/types@3.1.3
127
+ - @prosopo/util@3.0.13
128
+ - @prosopo/config@3.1.10
129
+
130
+ ## 2.7.2
131
+ ### Patch Changes
132
+
133
+ - 4440947: fix type-only tsc compilation
134
+ - 7bdaca6: Release 3.2.1
135
+ - Updated dependencies [4440947]
136
+ - Updated dependencies [7bdaca6]
137
+ - Updated dependencies [809b984]
138
+ - Updated dependencies [1249ce0]
139
+ - Updated dependencies [809b984]
140
+ - @prosopo/common@3.1.9
141
+ - @prosopo/types@3.1.2
142
+ - @prosopo/util@3.0.12
143
+ - @prosopo/config@3.1.9
144
+
145
+ ## 2.7.1
146
+ ### Patch Changes
147
+
148
+ - 1f980c4: Fix types mismatch in decryption
149
+ - 6fe8570: Release 3.2.0
150
+ - Updated dependencies [1f980c4]
151
+ - Updated dependencies [6fe8570]
152
+ - @prosopo/types@3.1.1
153
+ - @prosopo/common@3.1.8
154
+ - @prosopo/util@3.0.11
155
+ - @prosopo/config@3.1.8
156
+
157
+ ## 2.7.0
158
+ ### Minor Changes
159
+
160
+ - 8bdc7f0: Using detector to select provider
161
+
162
+ ### Patch Changes
163
+
164
+ - f304be9: Release 3.1.13
165
+ - Updated dependencies [f304be9]
166
+ - Updated dependencies [8bdc7f0]
167
+ - @prosopo/common@3.1.7
168
+ - @prosopo/types@3.1.0
169
+ - @prosopo/util@3.0.10
170
+ - @prosopo/config@3.1.7
171
+
172
+ ## 2.6.21
173
+ ### Patch Changes
174
+
175
+ - Updated dependencies [9eed772]
176
+ - @prosopo/config@3.1.6
177
+ - @prosopo/common@3.1.6
178
+ - @prosopo/types@3.0.10
179
+
180
+ ## 2.6.20
181
+ ### Patch Changes
182
+
183
+ - 6960643: lint detect missing and unneccessary imports
184
+ - Updated dependencies [6960643]
185
+ - @prosopo/common@3.1.5
186
+ - @prosopo/types@3.0.9
187
+
188
+ ## 2.6.19
189
+ ### Patch Changes
190
+
191
+ - Updated dependencies [30e7d4d]
192
+ - @prosopo/config@3.1.5
193
+ - @prosopo/common@3.1.4
194
+ - @prosopo/types@3.0.8
195
+
196
+ ## 2.6.18
197
+ ### Patch Changes
198
+
199
+ - Updated dependencies [44ffda2]
200
+ - Updated dependencies [a49b538]
201
+ - @prosopo/config@3.1.4
202
+ - @prosopo/common@3.1.3
203
+ - @prosopo/types@3.0.7
204
+
3
205
  ## 2.6.17
4
206
  ### Patch Changes
5
207
 
package/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # @prosopo/load-balancer
2
+
3
+ Provider load balancer for Procaptcha
4
+
5
+ ## Provider Selection Config
6
+
7
+ Each provider in the json has the following properties:
8
+
9
+ ```typescript
10
+ {
11
+ address: string; // Provider account address
12
+ url: string; // Provider API endpoint
13
+ datasetId: string; // Dataset identifier
14
+ weight?: number; // Optional weight (1-100, defaults to 1)
15
+ }
16
+ ```
17
+
18
+ ### Weight Field
19
+
20
+ The `weight` field controls how often a provider is selected:
21
+
22
+ - **Range**: 1-100 (values outside this range are automatically coerced)
23
+ - **Default**: 1 (if not specified)
24
+ - **Type**: Integer (decimal values are rounded)
25
+ - **Effect**: Higher weight = more traffic
26
+
27
+ #### Examples
28
+
29
+ ```json
30
+ {
31
+ "provider1": {
32
+ "address": "...",
33
+ "url": "https://provider1.example.com",
34
+ "datasetId": "...",
35
+ "weight": 1
36
+ },
37
+ "provider2": {
38
+ "address": "...",
39
+ "url": "https://provider2.example.com",
40
+ "datasetId": "...",
41
+ "weight": 3
42
+ }
43
+ }
44
+ ```
45
+
46
+ In this example:
47
+ - Total weight = 4
48
+ - Provider 1 receives ~25% of traffic
49
+ - Provider 2 receives ~75% of traffic
@@ -0,0 +1,52 @@
1
+ import { ProsopoEnvError } from "@prosopo/common";
2
+ import { z } from "zod";
3
+ const HardcodedProviderSchema = z.object({
4
+ address: z.string(),
5
+ url: z.string(),
6
+ datasetId: z.string(),
7
+ weight: z.number().optional().default(1).transform((val) => {
8
+ const weight = Math.round(val);
9
+ return Math.max(1, Math.min(100, weight));
10
+ })
11
+ });
12
+ const convertHostedProvider = (provider) => {
13
+ const providers = Object.values(provider).map(
14
+ (p) => HardcodedProviderSchema.parse(p)
15
+ );
16
+ return providers.sort((a, b) => a.url.localeCompare(b.url));
17
+ };
18
+ const loadBalancer = async (environment) => {
19
+ if (environment === "production") {
20
+ const providers = await fetch(
21
+ "https://provider-list.prosopo.io/",
22
+ {
23
+ method: "GET",
24
+ mode: "cors"
25
+ }
26
+ ).then((res) => res.json());
27
+ return convertHostedProvider(providers);
28
+ }
29
+ if (environment === "staging") {
30
+ const providers = await fetch(
31
+ "https://provider-list.prosopo.io/staging.json",
32
+ { method: "GET", mode: "cors" }
33
+ ).then((res) => res.json());
34
+ return convertHostedProvider(providers);
35
+ }
36
+ if (environment === "development") {
37
+ return [
38
+ {
39
+ address: "5EjTA28bKSbFPPyMbUjNtArxyqjwq38r1BapVmLZShaqEedV",
40
+ url: "http://localhost:9229",
41
+ datasetId: "0x9f460e81ac9c71b486f796a21bb36e2263694756a6621134d110da217fd3ef25",
42
+ weight: 1
43
+ }
44
+ ];
45
+ }
46
+ throw new ProsopoEnvError("CONFIG.UNKNOWN_ENVIRONMENT", {
47
+ context: { environment }
48
+ });
49
+ };
50
+ export {
51
+ loadBalancer
52
+ };
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const common = require("@prosopo/common");
4
+ const zod = require("zod");
5
+ const HardcodedProviderSchema = zod.z.object({
6
+ address: zod.z.string(),
7
+ url: zod.z.string(),
8
+ datasetId: zod.z.string(),
9
+ weight: zod.z.number().optional().default(1).transform((val) => {
10
+ const weight = Math.round(val);
11
+ return Math.max(1, Math.min(100, weight));
12
+ })
13
+ });
14
+ const convertHostedProvider = (provider) => {
15
+ const providers = Object.values(provider).map(
16
+ (p) => HardcodedProviderSchema.parse(p)
17
+ );
18
+ return providers.sort((a, b) => a.url.localeCompare(b.url));
19
+ };
20
+ const loadBalancer = async (environment) => {
21
+ if (environment === "production") {
22
+ const providers = await fetch(
23
+ "https://provider-list.prosopo.io/",
24
+ {
25
+ method: "GET",
26
+ mode: "cors"
27
+ }
28
+ ).then((res) => res.json());
29
+ return convertHostedProvider(providers);
30
+ }
31
+ if (environment === "staging") {
32
+ const providers = await fetch(
33
+ "https://provider-list.prosopo.io/staging.json",
34
+ { method: "GET", mode: "cors" }
35
+ ).then((res) => res.json());
36
+ return convertHostedProvider(providers);
37
+ }
38
+ if (environment === "development") {
39
+ return [
40
+ {
41
+ address: "5EjTA28bKSbFPPyMbUjNtArxyqjwq38r1BapVmLZShaqEedV",
42
+ url: "http://localhost:9229",
43
+ datasetId: "0x9f460e81ac9c71b486f796a21bb36e2263694756a6621134d110da217fd3ef25",
44
+ weight: 1
45
+ }
46
+ ];
47
+ }
48
+ throw new common.ProsopoEnvError("CONFIG.UNKNOWN_ENVIRONMENT", {
49
+ context: { environment }
50
+ });
51
+ };
52
+ exports.loadBalancer = loadBalancer;
@@ -1,38 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const common = require("@prosopo/common");
4
- const convertHostedProvider = (provider) => {
5
- return Object.values(provider);
6
- };
7
- const loadBalancer = async (environment) => {
8
- if (environment === "production") {
9
- const providers = await fetch(
10
- "https://provider-list.prosopo.io/",
11
- {
12
- method: "GET",
13
- mode: "cors"
14
- }
15
- ).then((res) => res.json());
16
- return convertHostedProvider(providers);
17
- }
18
- if (environment === "staging") {
19
- const providers = await fetch(
20
- "https://provider-list.prosopo.io/staging.json",
21
- { method: "GET", mode: "cors" }
22
- ).then((res) => res.json());
23
- return convertHostedProvider(providers);
24
- }
25
- if (environment === "development") {
26
- return [
27
- {
28
- address: "5EjTA28bKSbFPPyMbUjNtArxyqjwq38r1BapVmLZShaqEedV",
29
- url: "http://localhost:9229",
30
- datasetId: "0x9f460e81ac9c71b486f796a21bb36e2263694756a6621134d110da217fd3ef25"
31
- }
32
- ];
33
- }
34
- throw new common.ProsopoEnvError("CONFIG.UNKNOWN_ENVIRONMENT", {
35
- context: { environment }
36
- });
37
- };
38
- exports.loadBalancer = loadBalancer;
3
+ const providers = require("./providers.cjs");
4
+ const balancer = require("./balancer.cjs");
5
+ exports._resetCache = providers._resetCache;
6
+ exports.getRandomActiveProvider = providers.getRandomActiveProvider;
7
+ exports.selectWeightedProvider = providers.selectWeightedProvider;
8
+ exports.loadBalancer = balancer.loadBalancer;
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ require("./index.cjs");
4
+ const balancer = require("./balancer.cjs");
5
+ let cachedProviders = [];
6
+ function _resetCache() {
7
+ cachedProviders = [];
8
+ }
9
+ function selectWeightedProvider(providers, entropy) {
10
+ if (providers.length === 0) {
11
+ throw new Error("No providers available");
12
+ }
13
+ const totalWeight = providers.reduce((sum, p) => sum + p.weight, 0);
14
+ const randomValue = entropy % totalWeight;
15
+ let cumulativeWeight = 0;
16
+ for (const provider of providers) {
17
+ cumulativeWeight += provider.weight;
18
+ if (randomValue < cumulativeWeight) {
19
+ return provider;
20
+ }
21
+ }
22
+ const selectedProvider = providers[providers.length - 1];
23
+ if (!selectedProvider) {
24
+ throw new Error("No providers available");
25
+ }
26
+ return selectedProvider;
27
+ }
28
+ const getRandomActiveProvider = async (env, entropy) => {
29
+ if (cachedProviders.length === 0) {
30
+ cachedProviders = await balancer.loadBalancer(env);
31
+ }
32
+ const randomProviderObj = selectWeightedProvider(cachedProviders, entropy);
33
+ return {
34
+ providerAccount: randomProviderObj.address,
35
+ provider: {
36
+ url: randomProviderObj.url,
37
+ datasetId: randomProviderObj.datasetId
38
+ }
39
+ };
40
+ };
41
+ exports._resetCache = _resetCache;
42
+ exports.getRandomActiveProvider = getRandomActiveProvider;
43
+ exports.selectWeightedProvider = selectWeightedProvider;
package/dist/index.js CHANGED
@@ -1,38 +1,8 @@
1
- import { ProsopoEnvError } from "@prosopo/common";
2
- const convertHostedProvider = (provider) => {
3
- return Object.values(provider);
4
- };
5
- const loadBalancer = async (environment) => {
6
- if (environment === "production") {
7
- const providers = await fetch(
8
- "https://provider-list.prosopo.io/",
9
- {
10
- method: "GET",
11
- mode: "cors"
12
- }
13
- ).then((res) => res.json());
14
- return convertHostedProvider(providers);
15
- }
16
- if (environment === "staging") {
17
- const providers = await fetch(
18
- "https://provider-list.prosopo.io/staging.json",
19
- { method: "GET", mode: "cors" }
20
- ).then((res) => res.json());
21
- return convertHostedProvider(providers);
22
- }
23
- if (environment === "development") {
24
- return [
25
- {
26
- address: "5EjTA28bKSbFPPyMbUjNtArxyqjwq38r1BapVmLZShaqEedV",
27
- url: "http://localhost:9229",
28
- datasetId: "0x9f460e81ac9c71b486f796a21bb36e2263694756a6621134d110da217fd3ef25"
29
- }
30
- ];
31
- }
32
- throw new ProsopoEnvError("CONFIG.UNKNOWN_ENVIRONMENT", {
33
- context: { environment }
34
- });
35
- };
1
+ import { _resetCache, getRandomActiveProvider, selectWeightedProvider } from "./providers.js";
2
+ import { loadBalancer } from "./balancer.js";
36
3
  export {
37
- loadBalancer
4
+ _resetCache,
5
+ getRandomActiveProvider,
6
+ loadBalancer,
7
+ selectWeightedProvider
38
8
  };
@@ -0,0 +1,43 @@
1
+ import "./index.js";
2
+ import { loadBalancer } from "./balancer.js";
3
+ let cachedProviders = [];
4
+ function _resetCache() {
5
+ cachedProviders = [];
6
+ }
7
+ function selectWeightedProvider(providers, entropy) {
8
+ if (providers.length === 0) {
9
+ throw new Error("No providers available");
10
+ }
11
+ const totalWeight = providers.reduce((sum, p) => sum + p.weight, 0);
12
+ const randomValue = entropy % totalWeight;
13
+ let cumulativeWeight = 0;
14
+ for (const provider of providers) {
15
+ cumulativeWeight += provider.weight;
16
+ if (randomValue < cumulativeWeight) {
17
+ return provider;
18
+ }
19
+ }
20
+ const selectedProvider = providers[providers.length - 1];
21
+ if (!selectedProvider) {
22
+ throw new Error("No providers available");
23
+ }
24
+ return selectedProvider;
25
+ }
26
+ const getRandomActiveProvider = async (env, entropy) => {
27
+ if (cachedProviders.length === 0) {
28
+ cachedProviders = await loadBalancer(env);
29
+ }
30
+ const randomProviderObj = selectWeightedProvider(cachedProviders, entropy);
31
+ return {
32
+ providerAccount: randomProviderObj.address,
33
+ provider: {
34
+ url: randomProviderObj.url,
35
+ datasetId: randomProviderObj.datasetId
36
+ }
37
+ };
38
+ };
39
+ export {
40
+ _resetCache,
41
+ getRandomActiveProvider,
42
+ selectWeightedProvider
43
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prosopo/load-balancer",
3
- "version": "2.6.17",
3
+ "version": "2.8.0",
4
4
  "description": "Provider load balancer",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -21,7 +21,8 @@
21
21
  "build": "NODE_ENV=${NODE_ENV:-development}; vite build --config vite.esm.config.ts --mode $NODE_ENV",
22
22
  "build:tsc": "tsc --build --verbose",
23
23
  "build:cjs": "NODE_ENV=${NODE_ENV:-development}; vite build --config vite.cjs.config.ts --mode $NODE_ENV",
24
- "typecheck": "tsc --build --declaration --emitDeclarationOnly"
24
+ "test": "NODE_ENV=${NODE_ENV:-test}; npx vitest run --config ./vite.test.config.ts --mode $NODE_ENV",
25
+ "typecheck": "tsc --project tsconfig.types.json"
25
26
  },
26
27
  "repository": {
27
28
  "type": "git",
@@ -34,20 +35,17 @@
34
35
  },
35
36
  "homepage": "https://github.com/prosopo/captcha#readme",
36
37
  "dependencies": {
37
- "@prosopo/common": "3.1.2",
38
- "@prosopo/config": "3.1.3",
39
- "@prosopo/types": "3.0.6",
40
- "@typegoose/auto-increment": "4.13.0",
41
- "axios": "1.10.0",
42
- "esbuild": "0.25.6",
43
- "express": "4.21.2",
44
- "openpgp": "5.11.3",
45
- "webpack-dev-server": "5.2.2"
38
+ "@prosopo/common": "3.1.20",
39
+ "@prosopo/config": "3.1.20",
40
+ "@prosopo/types": "3.5.3",
41
+ "zod": "3.23.8"
46
42
  },
47
43
  "devDependencies": {
44
+ "@types/node": "22.10.2",
48
45
  "@vitest/coverage-v8": "3.0.9",
49
46
  "concurrently": "9.0.1",
50
47
  "del-cli": "6.0.0",
48
+ "dotenv": "17.2.1",
51
49
  "npm-run-all": "4.1.5",
52
50
  "tslib": "2.7.0",
53
51
  "tsx": "4.20.3",
@@ -0,0 +1,33 @@
1
+ // Copyright 2021-2025 Prosopo (UK) Ltd.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ import fs from "node:fs";
16
+ import path from "node:path";
17
+ import { ViteTestConfig } from "@prosopo/config";
18
+ import dotenv from "dotenv";
19
+ process.env.NODE_ENV = "test";
20
+ // if .env.test exists at this level, use it, otherwise use the one at the root
21
+ const envFile = `.env.${process.env.NODE_ENV || "development"}`;
22
+ let envPath = envFile;
23
+ if (fs.existsSync(envFile)) {
24
+ envPath = path.resolve(envFile);
25
+ } else if (fs.existsSync(`../../${envFile}`)) {
26
+ envPath = path.resolve(`../../${envFile}`);
27
+ } else {
28
+ throw new Error(`No ${envFile} file found`);
29
+ }
30
+
31
+ dotenv.config({ path: envPath });
32
+
33
+ export default ViteTestConfig();