@prosopo/load-balancer 2.10.49 → 2.11.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.
Files changed (38) hide show
  1. package/.turbo/turbo-build$colon$cjs.log +9 -8
  2. package/.turbo/turbo-build$colon$tsc.log +12 -12
  3. package/.turbo/turbo-build.log +10 -9
  4. package/CHANGELOG.md +82 -0
  5. package/README.md +34 -0
  6. package/dist/balancer.d.ts +3 -14
  7. package/dist/balancer.d.ts.map +1 -1
  8. package/dist/balancer.js +61 -11
  9. package/dist/balancer.js.map +1 -1
  10. package/dist/cjs/balancer.cjs +61 -10
  11. package/dist/cjs/index.cjs +3 -0
  12. package/dist/cjs/providerUrlOverride.cjs +33 -0
  13. package/dist/cjs/providers.cjs +5 -0
  14. package/dist/index.d.ts +1 -0
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js +3 -2
  17. package/dist/index.js.map +1 -1
  18. package/dist/providerUrlOverride.d.ts +2 -0
  19. package/dist/providerUrlOverride.d.ts.map +1 -0
  20. package/dist/providerUrlOverride.js +33 -0
  21. package/dist/providerUrlOverride.js.map +1 -0
  22. package/dist/providers.d.ts.map +1 -1
  23. package/dist/providers.js +6 -1
  24. package/dist/providers.js.map +1 -1
  25. package/dist/tests/providerUrlOverride.unit.test.d.ts +2 -0
  26. package/dist/tests/providerUrlOverride.unit.test.d.ts.map +1 -0
  27. package/dist/tests/providerUrlOverride.unit.test.js +144 -0
  28. package/dist/tests/providerUrlOverride.unit.test.js.map +1 -0
  29. package/dist/tests/providers.unit.test.js +2 -0
  30. package/dist/tests/providers.unit.test.js.map +1 -1
  31. package/package.json +4 -5
  32. package/src/balancer.ts +112 -18
  33. package/src/index.ts +1 -0
  34. package/src/providerUrlOverride.ts +44 -0
  35. package/src/providers.ts +13 -0
  36. package/src/tests/providerUrlOverride.unit.test.ts +182 -0
  37. package/src/tests/providers.unit.test.ts +6 -0
  38. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,182 @@
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 { afterEach, describe, expect, it, vi } from "vitest";
16
+ import { getProviderListOverride, loadBalancer } from "../balancer.js";
17
+ import { readProviderListOverride } from "../providerUrlOverride.js";
18
+
19
+ const ENV_KEY = "PROSOPO_PROVIDER_LIST";
20
+ const original: string | undefined = process.env[ENV_KEY];
21
+ const originalFetch = globalThis.fetch;
22
+
23
+ afterEach(() => {
24
+ if (original === undefined) delete process.env[ENV_KEY];
25
+ else process.env[ENV_KEY] = original;
26
+ globalThis.fetch = originalFetch;
27
+ });
28
+
29
+ const PROVIDER_LIST_JSON = JSON.stringify({
30
+ one: {
31
+ address: "5Provider1",
32
+ url: "https://one.example.com/",
33
+ datasetId: "0xabc",
34
+ weight: 3,
35
+ },
36
+ two: {
37
+ address: "5Provider2",
38
+ url: "https://two.example.com",
39
+ datasetId: "0xdef",
40
+ },
41
+ });
42
+
43
+ describe("reading the override", () => {
44
+ it("returns undefined when unset", () => {
45
+ delete process.env[ENV_KEY];
46
+ expect(readProviderListOverride()).toBeUndefined();
47
+ });
48
+
49
+ it("returns the raw value when set", () => {
50
+ process.env[ENV_KEY] = "https://captcha.example.com";
51
+ expect(readProviderListOverride()).toBe("https://captcha.example.com");
52
+ });
53
+ });
54
+
55
+ describe("parsing the override", () => {
56
+ it("is empty when unset or blank, so normal discovery is untouched", () => {
57
+ expect(getProviderListOverride(undefined)).toEqual([]);
58
+ expect(getProviderListOverride("")).toEqual([]);
59
+ expect(getProviderListOverride(" ")).toEqual([]);
60
+ });
61
+
62
+ it("accepts a single bare url", () => {
63
+ expect(getProviderListOverride("https://captcha.example.com")).toEqual([
64
+ {
65
+ address: "self-hosted",
66
+ url: "https://captcha.example.com",
67
+ datasetId: "",
68
+ weight: 1,
69
+ },
70
+ ]);
71
+ });
72
+
73
+ it("accepts several comma-separated urls", () => {
74
+ const providers = getProviderListOverride(
75
+ "https://one.example.com, https://two.example.com",
76
+ );
77
+ expect(providers.map((p) => p.url)).toEqual([
78
+ "https://one.example.com",
79
+ "https://two.example.com",
80
+ ]);
81
+ });
82
+
83
+ it("strips trailing slashes from both forms", () => {
84
+ // A token embeds the provider url it was minted against and the verifier
85
+ // matches it by exact string, so the two spellings must normalise.
86
+ expect(
87
+ getProviderListOverride("https://captcha.example.com/")[0]?.url,
88
+ ).toBe("https://captcha.example.com");
89
+ const fromJson = getProviderListOverride(PROVIDER_LIST_JSON);
90
+ expect(fromJson.map((p) => p.url)).toEqual([
91
+ "https://one.example.com",
92
+ "https://two.example.com",
93
+ ]);
94
+ });
95
+
96
+ it("accepts the hosted provider-list JSON, preserving address and weight", () => {
97
+ const providers = getProviderListOverride(PROVIDER_LIST_JSON);
98
+ expect(providers).toEqual([
99
+ {
100
+ address: "5Provider1",
101
+ url: "https://one.example.com",
102
+ datasetId: "0xabc",
103
+ weight: 3,
104
+ },
105
+ {
106
+ address: "5Provider2",
107
+ url: "https://two.example.com",
108
+ datasetId: "0xdef",
109
+ weight: 1,
110
+ },
111
+ ]);
112
+ });
113
+
114
+ it("reads the ipv4 / ipv6 sub-lists when the JSON carries them", () => {
115
+ const raw = JSON.stringify({
116
+ dual: {
117
+ address: "5Dual",
118
+ url: "https://dual.example.com",
119
+ datasetId: "0x1",
120
+ },
121
+ ipv4: {
122
+ four: {
123
+ address: "5Four",
124
+ url: "https://v4.example.com",
125
+ datasetId: "0x2",
126
+ },
127
+ },
128
+ });
129
+ expect(getProviderListOverride(raw, "ipv4").map((p) => p.url)).toEqual([
130
+ "https://v4.example.com",
131
+ ]);
132
+ expect(getProviderListOverride(raw).map((p) => p.url)).toEqual([
133
+ "https://dual.example.com",
134
+ ]);
135
+ });
136
+
137
+ it("falls back to normal discovery rather than throwing on malformed JSON", () => {
138
+ // A deployment-time typo must not take down every captcha on the page.
139
+ expect(getProviderListOverride("{not json")).toEqual([]);
140
+ expect(
141
+ getProviderListOverride('{"one":{"url":"missing-other-fields"}}'),
142
+ ).toEqual([]);
143
+ });
144
+ });
145
+
146
+ describe("loadBalancer with an override", () => {
147
+ it("returns the override without fetching the hosted list", async () => {
148
+ process.env[ENV_KEY] = "https://captcha.example.com/";
149
+ const fetchSpy = vi.fn();
150
+ // biome-ignore lint/suspicious/noExplicitAny: asserting the fetch never happens
151
+ globalThis.fetch = fetchSpy as any;
152
+
153
+ await expect(loadBalancer("production")).resolves.toEqual([
154
+ {
155
+ address: "self-hosted",
156
+ url: "https://captcha.example.com",
157
+ datasetId: "",
158
+ weight: 1,
159
+ },
160
+ ]);
161
+ expect(fetchSpy).not.toHaveBeenCalled();
162
+ });
163
+
164
+ it("applies in every environment, not just development", async () => {
165
+ process.env[ENV_KEY] = "https://captcha.example.com";
166
+ for (const env of ["development", "staging", "production"] as const) {
167
+ const providers = await loadBalancer(env);
168
+ expect(providers.map((p) => p.url)).toEqual([
169
+ "https://captcha.example.com",
170
+ ]);
171
+ }
172
+ });
173
+
174
+ it("does not prefix ipv4/ipv6 labels onto a bare-url override", async () => {
175
+ // Those labels only resolve inside our own fleet's DNS.
176
+ process.env[ENV_KEY] = "https://captcha.example.com";
177
+ const providers = await loadBalancer("production", "ipv4");
178
+ expect(providers.map((p) => p.url)).toEqual([
179
+ "https://captcha.example.com",
180
+ ]);
181
+ });
182
+ });
@@ -28,8 +28,14 @@ import {
28
28
  // can be exercised without real network calls. getRandomActiveProvider tests
29
29
  // don't use loadBalancer, so this mock leaves them unaffected.
30
30
  const loadBalancer = vi.fn();
31
+ // Defaults to "no override" so these cases exercise the normal healthz
32
+ // discovery path; the override's own behaviour is covered in
33
+ // providerUrlOverride.unit.test.ts.
34
+ const getProviderListOverride = vi.fn<() => HardcodedProvider[]>(() => []);
31
35
  vi.mock("../balancer.js", () => ({
32
36
  loadBalancer: (...args: unknown[]) => loadBalancer(...args),
37
+ getProviderListOverride: (...args: unknown[]) =>
38
+ getProviderListOverride(...(args as [])),
33
39
  }));
34
40
 
35
41
  const originalFetch = globalThis.fetch;