@prosopo/load-balancer 2.8.13 → 2.9.10
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/.turbo/turbo-build$colon$cjs.log +8 -6
- package/.turbo/turbo-build$colon$tsc.log +32 -0
- package/.turbo/turbo-build.log +13 -7
- package/CHANGELOG.md +307 -0
- package/dist/balancer.d.ts +25 -0
- package/dist/balancer.d.ts.map +1 -0
- package/dist/balancer.js +2 -2
- package/dist/balancer.js.map +1 -0
- package/dist/cjs/balancer.cjs +2 -2
- package/dist/cjs/index.cjs +2 -0
- package/dist/cjs/providers.cjs +29 -6
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -2
- package/dist/index.js.map +1 -0
- package/dist/providers.d.ts +8 -0
- package/dist/providers.d.ts.map +1 -0
- package/dist/providers.js +30 -7
- package/dist/providers.js.map +1 -0
- package/dist/tests/providers.unit.test.d.ts +2 -0
- package/dist/tests/providers.unit.test.d.ts.map +1 -0
- package/dist/tests/providers.unit.test.js +224 -0
- package/dist/tests/providers.unit.test.js.map +1 -0
- package/package.json +8 -6
- package/src/balancer.ts +81 -0
- package/src/index.ts +15 -0
- package/src/providers.ts +131 -0
- package/src/tests/providers.unit.test.ts +320 -0
- package/tsconfig.cjs.json +24 -0
- package/tsconfig.json +25 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/tsconfig.types.json +9 -0
- package/vite.cjs.config.ts +1 -1
- package/vite.esm.config.ts +1 -1
- package/vite.test.config.ts +1 -1
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { loadBalancer } from "../index.js";
|
|
3
|
+
import { getRandomActiveProvider, selectWeightedProvider, } from "../providers.js";
|
|
4
|
+
import { _resetCache } from "../providers.js";
|
|
5
|
+
vi.mock("../index.js", () => ({
|
|
6
|
+
loadBalancer: vi.fn(),
|
|
7
|
+
}));
|
|
8
|
+
describe("selectWeightedProvider", () => {
|
|
9
|
+
it("selects provider based on weight distribution", () => {
|
|
10
|
+
const providers = [
|
|
11
|
+
{
|
|
12
|
+
address: "address1",
|
|
13
|
+
url: "url1",
|
|
14
|
+
datasetId: "dataset1",
|
|
15
|
+
weight: 1,
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
address: "address2",
|
|
19
|
+
url: "url2",
|
|
20
|
+
datasetId: "dataset2",
|
|
21
|
+
weight: 3,
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
address: "address3",
|
|
25
|
+
url: "url3",
|
|
26
|
+
datasetId: "dataset3",
|
|
27
|
+
weight: 1,
|
|
28
|
+
},
|
|
29
|
+
];
|
|
30
|
+
expect(selectWeightedProvider(providers, 0).address).toBe("address1");
|
|
31
|
+
expect(selectWeightedProvider(providers, 1).address).toBe("address2");
|
|
32
|
+
expect(selectWeightedProvider(providers, 2).address).toBe("address2");
|
|
33
|
+
expect(selectWeightedProvider(providers, 3).address).toBe("address2");
|
|
34
|
+
expect(selectWeightedProvider(providers, 4).address).toBe("address3");
|
|
35
|
+
expect(selectWeightedProvider(providers, 5).address).toBe("address1");
|
|
36
|
+
expect(selectWeightedProvider(providers, 6).address).toBe("address2");
|
|
37
|
+
});
|
|
38
|
+
it("handles equal weights correctly", () => {
|
|
39
|
+
const providers = [
|
|
40
|
+
{
|
|
41
|
+
address: "address1",
|
|
42
|
+
url: "url1",
|
|
43
|
+
datasetId: "dataset1",
|
|
44
|
+
weight: 1,
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
address: "address2",
|
|
48
|
+
url: "url2",
|
|
49
|
+
datasetId: "dataset2",
|
|
50
|
+
weight: 1,
|
|
51
|
+
},
|
|
52
|
+
];
|
|
53
|
+
expect(selectWeightedProvider(providers, 0).address).toBe("address1");
|
|
54
|
+
expect(selectWeightedProvider(providers, 1).address).toBe("address2");
|
|
55
|
+
expect(selectWeightedProvider(providers, 2).address).toBe("address1");
|
|
56
|
+
expect(selectWeightedProvider(providers, 3).address).toBe("address2");
|
|
57
|
+
});
|
|
58
|
+
it("handles single provider", () => {
|
|
59
|
+
const providers = [
|
|
60
|
+
{
|
|
61
|
+
address: "address1",
|
|
62
|
+
url: "url1",
|
|
63
|
+
datasetId: "dataset1",
|
|
64
|
+
weight: 10,
|
|
65
|
+
},
|
|
66
|
+
];
|
|
67
|
+
expect(selectWeightedProvider(providers, 0).address).toBe("address1");
|
|
68
|
+
expect(selectWeightedProvider(providers, 5).address).toBe("address1");
|
|
69
|
+
expect(selectWeightedProvider(providers, 100).address).toBe("address1");
|
|
70
|
+
});
|
|
71
|
+
it("throws error for empty provider list", () => {
|
|
72
|
+
expect(() => selectWeightedProvider([], 0)).toThrow("No providers available");
|
|
73
|
+
});
|
|
74
|
+
it("heavily weighted provider is selected more often", () => {
|
|
75
|
+
const providers = [
|
|
76
|
+
{
|
|
77
|
+
address: "address1",
|
|
78
|
+
url: "url1",
|
|
79
|
+
datasetId: "dataset1",
|
|
80
|
+
weight: 1,
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
address: "address2",
|
|
84
|
+
url: "url2",
|
|
85
|
+
datasetId: "dataset2",
|
|
86
|
+
weight: 99,
|
|
87
|
+
},
|
|
88
|
+
];
|
|
89
|
+
const selections = { address1: 0, address2: 0 };
|
|
90
|
+
for (let i = 0; i < 100; i++) {
|
|
91
|
+
const selected = selectWeightedProvider(providers, i);
|
|
92
|
+
if (selected.address === "address1") {
|
|
93
|
+
selections.address1++;
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
selections.address2++;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
expect(selections.address1).toBe(1);
|
|
100
|
+
expect(selections.address2).toBe(99);
|
|
101
|
+
});
|
|
102
|
+
it("handles maximum weight value (100)", () => {
|
|
103
|
+
const providers = [
|
|
104
|
+
{
|
|
105
|
+
address: "address1",
|
|
106
|
+
url: "url1",
|
|
107
|
+
datasetId: "dataset1",
|
|
108
|
+
weight: 100,
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
address: "address2",
|
|
112
|
+
url: "url2",
|
|
113
|
+
datasetId: "dataset2",
|
|
114
|
+
weight: 100,
|
|
115
|
+
},
|
|
116
|
+
];
|
|
117
|
+
const selections = { address1: 0, address2: 0 };
|
|
118
|
+
for (let i = 0; i < 200; i++) {
|
|
119
|
+
const selected = selectWeightedProvider(providers, i);
|
|
120
|
+
if (selected.address === "address1") {
|
|
121
|
+
selections.address1++;
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
selections.address2++;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
expect(selections.address1).toBe(100);
|
|
128
|
+
expect(selections.address2).toBe(100);
|
|
129
|
+
});
|
|
130
|
+
it("correctly handles providers without values for weight", () => {
|
|
131
|
+
const providers = [
|
|
132
|
+
{
|
|
133
|
+
address: "address1",
|
|
134
|
+
url: "url1",
|
|
135
|
+
datasetId: "dataset1",
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
address: "address2",
|
|
139
|
+
url: "url2",
|
|
140
|
+
datasetId: "dataset2",
|
|
141
|
+
weight: 3,
|
|
142
|
+
},
|
|
143
|
+
];
|
|
144
|
+
const providersWithDefaults = [
|
|
145
|
+
{ ...providers[0], weight: 1 },
|
|
146
|
+
providers[1],
|
|
147
|
+
];
|
|
148
|
+
const selections = { address1: 0, address2: 0 };
|
|
149
|
+
for (let i = 0; i < 100; i++) {
|
|
150
|
+
const selected = selectWeightedProvider(providersWithDefaults, i);
|
|
151
|
+
if (selected.address === "address1") {
|
|
152
|
+
selections.address1++;
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
selections.address2++;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
expect(selections.address1).toBe(25);
|
|
159
|
+
expect(selections.address2).toBe(75);
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
describe("getRandomActiveProvider", () => {
|
|
163
|
+
beforeEach(() => {
|
|
164
|
+
_resetCache();
|
|
165
|
+
vi.clearAllMocks();
|
|
166
|
+
vi.resetAllMocks();
|
|
167
|
+
vi.resetModules();
|
|
168
|
+
});
|
|
169
|
+
it("returns a random provider when providers list is populated", async () => {
|
|
170
|
+
const mockProviders = [
|
|
171
|
+
{ address: "address1", url: "url1", datasetId: "dataset1", weight: 1 },
|
|
172
|
+
{ address: "address2", url: "url2", datasetId: "dataset2", weight: 1 },
|
|
173
|
+
];
|
|
174
|
+
loadBalancer.mockResolvedValue(mockProviders);
|
|
175
|
+
const result = await getRandomActiveProvider("development", 1);
|
|
176
|
+
expect(result.providerAccount).toBe("address2");
|
|
177
|
+
expect(result.provider.url).toBe("url2");
|
|
178
|
+
expect(result.provider.datasetId).toBe("dataset2");
|
|
179
|
+
});
|
|
180
|
+
it("loads providers only once when called multiple times", async () => {
|
|
181
|
+
const mockProviders = [
|
|
182
|
+
{ address: "address1", url: "url1", datasetId: "dataset1", weight: 1 },
|
|
183
|
+
];
|
|
184
|
+
loadBalancer.mockResolvedValue(mockProviders);
|
|
185
|
+
await getRandomActiveProvider("development", 123);
|
|
186
|
+
await getRandomActiveProvider("development", 456);
|
|
187
|
+
expect(loadBalancer).toHaveBeenCalledTimes(1);
|
|
188
|
+
});
|
|
189
|
+
it("handles empty providers list gracefully", async () => {
|
|
190
|
+
loadBalancer.mockResolvedValue([]);
|
|
191
|
+
await expect(getRandomActiveProvider("development", 123)).rejects.toThrow();
|
|
192
|
+
});
|
|
193
|
+
it("respects provider weights when selecting", async () => {
|
|
194
|
+
const mockProviders = [
|
|
195
|
+
{ address: "address1", url: "url1", datasetId: "dataset1", weight: 1 },
|
|
196
|
+
{ address: "address2", url: "url2", datasetId: "dataset2", weight: 3 },
|
|
197
|
+
];
|
|
198
|
+
loadBalancer.mockResolvedValue(mockProviders);
|
|
199
|
+
const result0 = await getRandomActiveProvider("development", 0);
|
|
200
|
+
expect(result0.providerAccount).toBe("address1");
|
|
201
|
+
_resetCache();
|
|
202
|
+
const result1 = await getRandomActiveProvider("development", 1);
|
|
203
|
+
expect(result1.providerAccount).toBe("address2");
|
|
204
|
+
_resetCache();
|
|
205
|
+
const result2 = await getRandomActiveProvider("development", 2);
|
|
206
|
+
expect(result2.providerAccount).toBe("address2");
|
|
207
|
+
_resetCache();
|
|
208
|
+
const result3 = await getRandomActiveProvider("development", 3);
|
|
209
|
+
expect(result3.providerAccount).toBe("address2");
|
|
210
|
+
});
|
|
211
|
+
it("handles providers with missing weight field (defaults to 1)", async () => {
|
|
212
|
+
const mockProviders = [
|
|
213
|
+
{ address: "address1", url: "url1", datasetId: "dataset1", weight: 1 },
|
|
214
|
+
{ address: "address2", url: "url2", datasetId: "dataset2", weight: 1 },
|
|
215
|
+
];
|
|
216
|
+
loadBalancer.mockResolvedValue(mockProviders);
|
|
217
|
+
const result0 = await getRandomActiveProvider("development", 0);
|
|
218
|
+
expect(result0.providerAccount).toBe("address1");
|
|
219
|
+
_resetCache();
|
|
220
|
+
const result1 = await getRandomActiveProvider("development", 1);
|
|
221
|
+
expect(result1.providerAccount).toBe("address2");
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
//# sourceMappingURL=providers.unit.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"providers.unit.test.js","sourceRoot":"","sources":["../../src/tests/providers.unit.test.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AAC9D,OAAO,EAA0B,YAAY,EAAE,MAAM,aAAa,CAAC;AACnE,OAAO,EACN,uBAAuB,EACvB,sBAAsB,GACtB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAE9C,EAAE,CAAC,IAAI,CAAC,aAAa,EAAE,GAAG,EAAE,CAAC,CAAC;IAC7B,YAAY,EAAE,EAAE,CAAC,EAAE,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C,EAAE;SACtE,CAAC;QACD,YAAoD,CAAC,iBAAiB,CACtE,aAAa,CACb,CAAC;QAGF,MAAM,OAAO,GAAG,MAAM,uBAAuB,CAAC,aAAa,EAAE,CAAC,CAAC,CAAC;QAChE,MAAM,CAAC,OAAO,CAAC,eAAe,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QAEjD,WAAW,EAAE,CAAC;QACd,MAAM,OAAO,GAAG,MAAM,uBAAuB,CAAC,aAAa,EAAE,CAAC,CAAC,CAAC;QAChE,MAAM,CAAC,OAAO,CAAC,eAAe,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IAClD,CAAC,CAAC,CAAC;AACJ,CAAC,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prosopo/load-balancer",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.9.10",
|
|
4
4
|
"description": "Provider load balancer",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -18,7 +18,8 @@
|
|
|
18
18
|
},
|
|
19
19
|
"scripts": {
|
|
20
20
|
"clean": "del-cli --verbose dist tsconfig.tsbuildinfo",
|
|
21
|
-
"build": "
|
|
21
|
+
"build": "npm run build:cross-env -- --mode ${NODE_ENV:-development}",
|
|
22
|
+
"build:cross-env": "vite build --config vite.esm.config.ts",
|
|
22
23
|
"build:tsc": "tsc --build --verbose",
|
|
23
24
|
"build:cjs": "NODE_ENV=${NODE_ENV:-development}; vite build --config vite.cjs.config.ts --mode $NODE_ENV",
|
|
24
25
|
"test": "NODE_ENV=${NODE_ENV:-test}; npx vitest run --config ./vite.test.config.ts --mode $NODE_ENV",
|
|
@@ -26,7 +27,8 @@
|
|
|
26
27
|
},
|
|
27
28
|
"repository": {
|
|
28
29
|
"type": "git",
|
|
29
|
-
"url": "git+
|
|
30
|
+
"url": "git+https://github.com/prosopo/captcha.git",
|
|
31
|
+
"directory": "packages/load-balancer"
|
|
30
32
|
},
|
|
31
33
|
"author": "Prosopo Limited",
|
|
32
34
|
"license": "Apache-2.0",
|
|
@@ -35,12 +37,12 @@
|
|
|
35
37
|
},
|
|
36
38
|
"homepage": "https://github.com/prosopo/captcha#readme",
|
|
37
39
|
"dependencies": {
|
|
38
|
-
"@prosopo/common": "3.1.
|
|
39
|
-
"@prosopo/types": "3.
|
|
40
|
+
"@prosopo/common": "3.1.38",
|
|
41
|
+
"@prosopo/types": "4.3.0",
|
|
40
42
|
"zod": "3.23.8"
|
|
41
43
|
},
|
|
42
44
|
"devDependencies": {
|
|
43
|
-
"@prosopo/config": "3.1
|
|
45
|
+
"@prosopo/config": "3.3.1",
|
|
44
46
|
"@types/node": "22.10.2",
|
|
45
47
|
"@vitest/coverage-v8": "3.2.4",
|
|
46
48
|
"concurrently": "9.0.1",
|
package/src/balancer.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// Copyright 2021-2026 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
|
+
import { ProsopoEnvError } from "@prosopo/common";
|
|
15
|
+
import type { EnvironmentTypes } from "@prosopo/types";
|
|
16
|
+
import { z } from "zod";
|
|
17
|
+
|
|
18
|
+
const HardcodedProviderSchema = z.object({
|
|
19
|
+
address: z.string(),
|
|
20
|
+
url: z.string(),
|
|
21
|
+
datasetId: z.string(),
|
|
22
|
+
weight: z
|
|
23
|
+
.number()
|
|
24
|
+
.optional()
|
|
25
|
+
.default(1)
|
|
26
|
+
.transform((val) => {
|
|
27
|
+
// weight coerced to int 1-100
|
|
28
|
+
const weight = Math.round(val);
|
|
29
|
+
return Math.max(1, Math.min(100, weight));
|
|
30
|
+
}),
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
export type HardcodedProvider = z.infer<typeof HardcodedProviderSchema>;
|
|
34
|
+
|
|
35
|
+
type hostedProviders = Record<string, unknown>;
|
|
36
|
+
|
|
37
|
+
export const convertHostedProvider = (
|
|
38
|
+
provider: hostedProviders,
|
|
39
|
+
): HardcodedProvider[] => {
|
|
40
|
+
const providers = Object.values(provider).map((p) =>
|
|
41
|
+
HardcodedProviderSchema.parse(p),
|
|
42
|
+
);
|
|
43
|
+
return providers.sort((a, b) => a.url.localeCompare(b.url));
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export const getLoadBalancerUrl = (environment: EnvironmentTypes): string => {
|
|
47
|
+
if (environment === "production") {
|
|
48
|
+
return "https://provider-list.prosopo.io/";
|
|
49
|
+
}
|
|
50
|
+
if (environment === "staging") {
|
|
51
|
+
return "https://provider-list.prosopo.io/staging.json";
|
|
52
|
+
}
|
|
53
|
+
throw new ProsopoEnvError("CONFIG.UNKNOWN_ENVIRONMENT", {
|
|
54
|
+
context: { environment },
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export const loadBalancer = async (
|
|
59
|
+
environment: EnvironmentTypes,
|
|
60
|
+
): Promise<HardcodedProvider[]> => {
|
|
61
|
+
if (environment === "development") {
|
|
62
|
+
return [
|
|
63
|
+
{
|
|
64
|
+
address: "5EjTA28bKSbFPPyMbUjNtArxyqjwq38r1BapVmLZShaqEedV",
|
|
65
|
+
url: "https://localhost:9229",
|
|
66
|
+
datasetId:
|
|
67
|
+
"0x7984714b92d61fd92fd6a7bc9b56b729481470bcc771c19c382ec679acf02e67",
|
|
68
|
+
weight: 1,
|
|
69
|
+
},
|
|
70
|
+
];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const providers: hostedProviders = await fetch(
|
|
74
|
+
getLoadBalancerUrl(environment),
|
|
75
|
+
{
|
|
76
|
+
method: "GET",
|
|
77
|
+
mode: "cors",
|
|
78
|
+
},
|
|
79
|
+
).then((res) => res.json());
|
|
80
|
+
return convertHostedProvider(providers);
|
|
81
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Copyright 2021-2026 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
|
+
export * from "./providers.js";
|
|
15
|
+
export * from "./balancer.js";
|
package/src/providers.ts
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
// Copyright 2021-2026 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 type { EnvironmentTypes, RandomProvider } from "@prosopo/types";
|
|
16
|
+
import { type HardcodedProvider, loadBalancer } from "./index.js";
|
|
17
|
+
|
|
18
|
+
// Keyed by env so a prefetch and a later call with a different env don't share.
|
|
19
|
+
const providerPromiseCache: Map<
|
|
20
|
+
EnvironmentTypes,
|
|
21
|
+
Promise<HardcodedProvider[]>
|
|
22
|
+
> = new Map();
|
|
23
|
+
|
|
24
|
+
/** Optional custom loader for server-side caching (e.g. cacheFile with ETag). */
|
|
25
|
+
let customProviderLoader:
|
|
26
|
+
| ((env: EnvironmentTypes) => Promise<HardcodedProvider[]>)
|
|
27
|
+
| null = null;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Set a custom provider loader that replaces the default HTTP fetch.
|
|
31
|
+
* Use this on the server side to inject cacheFile-based loading with
|
|
32
|
+
* ETag/Last-Modified support for disk persistence across restarts.
|
|
33
|
+
*/
|
|
34
|
+
export function setProviderLoader(
|
|
35
|
+
loader: (env: EnvironmentTypes) => Promise<HardcodedProvider[]>,
|
|
36
|
+
): void {
|
|
37
|
+
customProviderLoader = loader;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function _resetCache() {
|
|
41
|
+
providerPromiseCache.clear();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Selects a weighted random provider using the entropy value.
|
|
46
|
+
* Providers with higher weights are more likely to be selected.
|
|
47
|
+
*
|
|
48
|
+
* @param providers - Array of providers with weights
|
|
49
|
+
* @param entropy - Random seed value for deterministic selection
|
|
50
|
+
* @returns Selected provider
|
|
51
|
+
*/
|
|
52
|
+
export function selectWeightedProvider(
|
|
53
|
+
providers: HardcodedProvider[],
|
|
54
|
+
entropy: number,
|
|
55
|
+
): HardcodedProvider {
|
|
56
|
+
if (providers.length === 0) {
|
|
57
|
+
throw new Error("No providers available");
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const totalWeight = providers.reduce((sum, p) => sum + p.weight, 0);
|
|
61
|
+
|
|
62
|
+
// Use entropy to generate a value between 0 and totalWeight-1
|
|
63
|
+
const randomValue = entropy % totalWeight;
|
|
64
|
+
|
|
65
|
+
// Select provider based on cumulative weight
|
|
66
|
+
let cumulativeWeight = 0;
|
|
67
|
+
for (const provider of providers) {
|
|
68
|
+
cumulativeWeight += provider.weight;
|
|
69
|
+
if (randomValue < cumulativeWeight) {
|
|
70
|
+
return provider;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Fallback (should never reach here)
|
|
75
|
+
const selectedProvider = providers[providers.length - 1];
|
|
76
|
+
if (!selectedProvider) {
|
|
77
|
+
throw new Error("No providers available");
|
|
78
|
+
}
|
|
79
|
+
return selectedProvider;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Load providers using the custom loader if set, otherwise the default fetch. */
|
|
83
|
+
const loadProviders = async (
|
|
84
|
+
env: EnvironmentTypes,
|
|
85
|
+
): Promise<HardcodedProvider[]> => {
|
|
86
|
+
if (customProviderLoader) {
|
|
87
|
+
return customProviderLoader(env);
|
|
88
|
+
}
|
|
89
|
+
return loadBalancer(env);
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
// Caches the in-flight Promise (not the resolved array) so concurrent callers
|
|
93
|
+
// share a single network request rather than racing.
|
|
94
|
+
const getProvidersPromise = (
|
|
95
|
+
env: EnvironmentTypes,
|
|
96
|
+
): Promise<HardcodedProvider[]> => {
|
|
97
|
+
const existing = providerPromiseCache.get(env);
|
|
98
|
+
if (existing) return existing;
|
|
99
|
+
const promise = loadProviders(env).catch((err) => {
|
|
100
|
+
providerPromiseCache.delete(env);
|
|
101
|
+
throw err;
|
|
102
|
+
});
|
|
103
|
+
providerPromiseCache.set(env, promise);
|
|
104
|
+
return promise;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Pre-warms the provider cache for a given environment without requiring entropy.
|
|
109
|
+
* Call this as early as possible to avoid a cold-cache delay when getRandomActiveProvider is first used.
|
|
110
|
+
*/
|
|
111
|
+
export const prefetchProviders = async (
|
|
112
|
+
env: EnvironmentTypes,
|
|
113
|
+
): Promise<void> => {
|
|
114
|
+
await getProvidersPromise(env);
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
export const getRandomActiveProvider = async (
|
|
118
|
+
env: EnvironmentTypes,
|
|
119
|
+
entropy: number,
|
|
120
|
+
): Promise<RandomProvider> => {
|
|
121
|
+
const providers = await getProvidersPromise(env);
|
|
122
|
+
const randomProviderObj = selectWeightedProvider(providers, entropy);
|
|
123
|
+
|
|
124
|
+
return {
|
|
125
|
+
providerAccount: randomProviderObj.address,
|
|
126
|
+
provider: {
|
|
127
|
+
url: randomProviderObj.url,
|
|
128
|
+
datasetId: randomProviderObj.datasetId,
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
};
|