@prosopo/load-balancer 2.10.42 → 2.10.44

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/src/providers.ts CHANGED
@@ -18,6 +18,7 @@ import {
18
18
  type IpMode,
19
19
  loadBalancer,
20
20
  } from "./balancer.js";
21
+ import { getDevelopmentProviderUrl } from "./developmentProviderUrl.js";
21
22
  import { retryWithBackoff } from "./retry.js";
22
23
 
23
24
  // Base DNS endpoint per env — the `pronode.prosopo.io` family is latency-routed
@@ -25,11 +26,13 @@ import { retryWithBackoff } from "./retry.js";
25
26
  // once to discover which specific pronodeN the DNS layer picked, then pin
26
27
  // subsequent captcha calls to that pronode so session creation and submission
27
28
  // land on the same backend.
28
- const DNS_ENDPOINT: Record<EnvironmentTypes, string> = {
29
- development: "https://localhost:9229",
29
+ // Resolved per call rather than once at module load, so a development override
30
+ // set after this module is imported is still picked up.
31
+ const dnsEndpoints = (): Record<EnvironmentTypes, string> => ({
32
+ development: getDevelopmentProviderUrl(),
30
33
  staging: "https://staging.pronode.prosopo.io",
31
34
  production: "https://pronode.prosopo.io",
32
- };
35
+ });
33
36
 
34
37
  // Apply the `ipv4.` / `ipv6.` DNS label to a hostname. The single-stack
35
38
  // sub-zones only resolve to A or AAAA records respectively, so this pins the
@@ -93,8 +96,10 @@ const fetchPinnedHostWithRetry = (baseUrl: string): Promise<string> =>
93
96
  maxDelayMs: healthzRetryMaxDelayMs,
94
97
  });
95
98
 
96
- const resolveBaseUrl = (env: EnvironmentTypes): string =>
97
- DNS_ENDPOINT[env] ?? DNS_ENDPOINT.development;
99
+ const resolveBaseUrl = (env: EnvironmentTypes): string => {
100
+ const endpoints = dnsEndpoints();
101
+ return endpoints[env] ?? endpoints.development;
102
+ };
98
103
 
99
104
  const resolvePinnedUrl = async (
100
105
  env: EnvironmentTypes,
@@ -0,0 +1,76 @@
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 } from "vitest";
16
+ import {
17
+ DEFAULT_DEVELOPMENT_PROVIDER_URL,
18
+ getDevelopmentProviderUrl,
19
+ readDevelopmentProviderUrlOverride,
20
+ } from "../developmentProviderUrl.js";
21
+
22
+ const ENV_KEY = "PROSOPO_PROVIDER_URL_DEVELOPMENT";
23
+ const original: string | undefined = process.env[ENV_KEY];
24
+
25
+ afterEach(() => {
26
+ if (original === undefined) delete process.env[ENV_KEY];
27
+ else process.env[ENV_KEY] = original;
28
+ });
29
+
30
+ describe("the development provider url", () => {
31
+ it("is localhost when nothing overrides it", () => {
32
+ expect(getDevelopmentProviderUrl(undefined)).toBe("https://localhost:9229");
33
+ expect(DEFAULT_DEVELOPMENT_PROVIDER_URL).toBe("https://localhost:9229");
34
+ });
35
+
36
+ it("is the override when one is set", () => {
37
+ // The case this exists for: a phone loading the demo over the LAN cannot
38
+ // reach the provider via `localhost`, which resolves to the phone.
39
+ expect(getDevelopmentProviderUrl("https://192.0.2.10:9229")).toBe(
40
+ "https://192.0.2.10:9229",
41
+ );
42
+ });
43
+
44
+ it("drops a trailing slash so the url composes with /healthz", () => {
45
+ expect(getDevelopmentProviderUrl("https://192.0.2.10:9229/")).toBe(
46
+ "https://192.0.2.10:9229",
47
+ );
48
+ });
49
+
50
+ it("falls back rather than yielding an empty url", () => {
51
+ for (const blank of ["", " ", undefined]) {
52
+ expect(getDevelopmentProviderUrl(blank)).toBe(
53
+ DEFAULT_DEVELOPMENT_PROVIDER_URL,
54
+ );
55
+ }
56
+ });
57
+ });
58
+
59
+ describe("reading the override", () => {
60
+ it("reads the environment variable", () => {
61
+ process.env[ENV_KEY] = "https://198.51.100.7:9229";
62
+ expect(readDevelopmentProviderUrlOverride()).toBe(
63
+ "https://198.51.100.7:9229",
64
+ );
65
+ });
66
+
67
+ it("is undefined when the variable is unset", () => {
68
+ delete process.env[ENV_KEY];
69
+ expect(readDevelopmentProviderUrlOverride()).toBeUndefined();
70
+ });
71
+
72
+ it("defaults when the variable is unset", () => {
73
+ delete process.env[ENV_KEY];
74
+ expect(getDevelopmentProviderUrl()).toBe(DEFAULT_DEVELOPMENT_PROVIDER_URL);
75
+ });
76
+ });