@prosopo/procaptcha-bundle 3.2.10 → 4.1.7

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 (93) hide show
  1. package/.turbo/turbo-build$colon$cjs.log +28 -35
  2. package/.turbo/turbo-build$colon$tsc.log +80 -0
  3. package/.turbo/turbo-build.log +33 -36
  4. package/CHANGELOG.md +575 -0
  5. package/dist/cjs/index.cjs +4 -9
  6. package/dist/cjs/util/captcha/captchaRenderer.cjs +10 -9
  7. package/dist/cjs/util/captcha/components/{frictionlessCaptcha.cjs → bundleCaptcha.cjs} +2 -2
  8. package/dist/cjs/util/config.cjs +14 -4
  9. package/dist/cjs/util/widgetFactory.cjs +4 -6
  10. package/dist/components.d.ts +2 -0
  11. package/dist/components.d.ts.map +1 -0
  12. package/dist/components.js +2 -0
  13. package/dist/components.js.map +1 -0
  14. package/dist/index.d.ts +16 -0
  15. package/dist/index.d.ts.map +1 -0
  16. package/dist/index.js +4 -9
  17. package/dist/index.js.map +1 -0
  18. package/dist/tests/procaptchaReady.unit.test.d.ts +2 -0
  19. package/dist/tests/procaptchaReady.unit.test.d.ts.map +1 -0
  20. package/dist/tests/procaptchaReady.unit.test.js +32 -0
  21. package/dist/tests/procaptchaReady.unit.test.js.map +1 -0
  22. package/dist/tests/util/config.test.d.ts +2 -0
  23. package/dist/tests/util/config.test.d.ts.map +1 -0
  24. package/dist/tests/util/config.test.js +69 -0
  25. package/dist/tests/util/config.test.js.map +1 -0
  26. package/dist/tests/util/renderLogic.test.d.ts +2 -0
  27. package/dist/tests/util/renderLogic.test.d.ts.map +1 -0
  28. package/dist/tests/util/renderLogic.test.js +144 -0
  29. package/dist/tests/util/renderLogic.test.js.map +1 -0
  30. package/dist/util/captcha/captchaRenderer.d.ts +19 -0
  31. package/dist/util/captcha/captchaRenderer.d.ts.map +1 -0
  32. package/dist/util/captcha/captchaRenderer.js +10 -9
  33. package/dist/util/captcha/captchaRenderer.js.map +1 -0
  34. package/dist/util/captcha/components/bundleCaptcha.d.ts +11 -0
  35. package/dist/util/captcha/components/bundleCaptcha.d.ts.map +1 -0
  36. package/dist/util/captcha/components/{frictionlessCaptcha.js → bundleCaptcha.js} +2 -2
  37. package/dist/util/captcha/components/bundleCaptcha.js.map +1 -0
  38. package/dist/util/config.d.ts +6 -0
  39. package/dist/util/config.d.ts.map +1 -0
  40. package/dist/util/config.js +14 -4
  41. package/dist/util/config.js.map +1 -0
  42. package/dist/util/configCreator.d.ts +5 -0
  43. package/dist/util/configCreator.d.ts.map +1 -0
  44. package/dist/util/configCreator.js.map +1 -0
  45. package/dist/util/language.d.ts +3 -0
  46. package/dist/util/language.d.ts.map +1 -0
  47. package/dist/util/language.js.map +1 -0
  48. package/dist/util/timeout.d.ts +3 -0
  49. package/dist/util/timeout.d.ts.map +1 -0
  50. package/dist/util/timeout.js.map +1 -0
  51. package/dist/util/widgetFactory.d.ts +18 -0
  52. package/dist/util/widgetFactory.d.ts.map +1 -0
  53. package/dist/util/widgetFactory.js +4 -6
  54. package/dist/util/widgetFactory.js.map +1 -0
  55. package/dist/util/widgetThemeResolver.d.ts +9 -0
  56. package/dist/util/widgetThemeResolver.d.ts.map +1 -0
  57. package/dist/util/widgetThemeResolver.js.map +1 -0
  58. package/package.json +14 -13
  59. package/src/components.ts +15 -0
  60. package/src/index.html +11 -0
  61. package/src/index.ts +259 -0
  62. package/src/tests/procaptchaReady.unit.test.ts +58 -0
  63. package/src/tests/util/config.test.ts +97 -0
  64. package/src/tests/util/renderLogic.test.tsx +221 -0
  65. package/src/util/captcha/captchaRenderer.tsx +126 -0
  66. package/src/util/captcha/components/bundleCaptcha.tsx +57 -0
  67. package/src/util/config.ts +40 -0
  68. package/src/util/configCreator.ts +51 -0
  69. package/src/util/language.ts +53 -0
  70. package/src/util/timeout.ts +43 -0
  71. package/src/util/widgetFactory.ts +145 -0
  72. package/src/util/widgetThemeResolver.ts +40 -0
  73. package/tsconfig.cjs.json +35 -0
  74. package/tsconfig.json +42 -0
  75. package/tsconfig.tsbuildinfo +1 -0
  76. package/tsconfig.types.json +9 -0
  77. package/vite.cjs.config.ts +1 -1
  78. package/vite.config.ts +23 -2
  79. package/vite.esm.config.ts +1 -1
  80. package/vite.iife.config.ts +119 -0
  81. package/vite.serve.config.ts +23 -1
  82. package/vite.test.config.ts +1 -1
  83. package/webpack.config.cjs +1 -1
  84. package/dist/cjs/util/captcha/captchaComponentProvider.cjs +0 -24
  85. package/dist/cjs/util/captcha/captchaComponentsList.cjs +0 -12
  86. package/dist/cjs/util/captcha/captchaType.cjs +0 -12
  87. package/dist/cjs/util/captcha/components/imageCaptcha.cjs +0 -17
  88. package/dist/cjs/util/captcha/components/powCaptcha.cjs +0 -17
  89. package/dist/util/captcha/captchaComponentProvider.js +0 -24
  90. package/dist/util/captcha/captchaComponentsList.js +0 -12
  91. package/dist/util/captcha/captchaType.js +0 -12
  92. package/dist/util/captcha/components/imageCaptcha.js +0 -17
  93. package/dist/util/captcha/components/powCaptcha.js +0 -17
@@ -0,0 +1,221 @@
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 { ProcaptchaFrictionless } from "@prosopo/procaptcha-frictionless";
16
+ import type { Callbacks, ProcaptchaProps } from "@prosopo/types";
17
+ import { type DOMWindow, JSDOM } from "jsdom";
18
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
19
+ import { WidgetFactory } from "../../util/widgetFactory.js";
20
+ import { WidgetThemeResolver } from "../../util/widgetThemeResolver.js";
21
+
22
+ const widgetFactory = new WidgetFactory(new WidgetThemeResolver());
23
+
24
+ interface TestContext {
25
+ document?: Document;
26
+ window?: DOMWindow;
27
+ }
28
+
29
+ const callbacks: Callbacks = {
30
+ onHuman: () => console.log("onHuman"),
31
+ onChallengeExpired: () => console.log("onChallengeExpired"),
32
+ onExpired: () => console.log("onExpired"),
33
+ onError: () => console.log("onError"),
34
+ onClose: () => console.log("onClose"),
35
+ onOpen: () => console.log("onOpen"),
36
+ onFailed: () => console.log("onFailed"),
37
+ onReset: () => console.log("onReset"),
38
+ onExtensionNotFound: () => console.log("onExtensionNotFound"),
39
+ onReload: () => console.log("onReload"),
40
+ };
41
+
42
+ vi.mock("@prosopo/detector", async () => {
43
+ return {
44
+ isBot: async () => {
45
+ return { isBot: true };
46
+ },
47
+ };
48
+ });
49
+
50
+ vi.mock("@polkadot/extension-dapp", async () => {
51
+ return {
52
+ web3Enable: async () => {
53
+ return [];
54
+ },
55
+ };
56
+ });
57
+
58
+ vi.mock("@prosopo/procaptcha-frictionless", async () => {
59
+ return {
60
+ ProcaptchaFrictionless: vi.fn((props: ProcaptchaProps) => {
61
+ return (
62
+ // @ts-ignore
63
+ <div config={props.config} callbacks={props.callbacks}>
64
+ ProcaptchaFrictionless
65
+ </div>
66
+ );
67
+ }),
68
+ };
69
+ });
70
+
71
+ describe("Config utility functions", () => {
72
+ let dom: JSDOM;
73
+
74
+ beforeEach<TestContext>((context) => {
75
+ dom = new JSDOM("<!DOCTYPE html><html><body></body></html>", {
76
+ url: "https://example.com",
77
+ });
78
+ context.document = dom.window.document;
79
+ context.window = dom.window;
80
+ // @ts-ignore
81
+ global.window = dom.window;
82
+ // @ts-ignore
83
+ global.document = dom.window.document;
84
+ // @ts-ignore
85
+ global.HTMLElement = dom.window.HTMLElement;
86
+ // @ts-ignore
87
+ global.Element = dom.window.Element;
88
+ // @ts-ignore
89
+ global.Node = dom.window.Node;
90
+ // @ts-ignore
91
+ global.navigator = dom.window.navigator;
92
+ vi.clearAllMocks();
93
+ });
94
+
95
+ afterEach<TestContext>((context) => {
96
+ context.document = undefined;
97
+ context.window = undefined;
98
+ // @ts-ignore
99
+ global.HTMLElement = undefined;
100
+ // @ts-ignore
101
+ global.Element = undefined;
102
+ // @ts-ignore
103
+ global.Node = undefined;
104
+ // @ts-ignore
105
+ global.navigator = undefined;
106
+ vi.clearAllMocks();
107
+ });
108
+
109
+ it<TestContext>("always mounts the unified frictionless dispatcher", async (ctx) => {
110
+ // @ts-ignore
111
+ const script = ctx.document.createElement("script");
112
+ script.src = "https://example.com/procaptcha.js";
113
+ document.body.appendChild(script);
114
+
115
+ expect(ctx.window).toBeDefined();
116
+
117
+ if (!ctx.window) {
118
+ expect(true).toBe(false);
119
+ return;
120
+ }
121
+
122
+ await widgetFactory.createWidgets([script], {
123
+ siteKey: "1234",
124
+ });
125
+
126
+ await vi.waitFor(() => {
127
+ expect(ProcaptchaFrictionless).toHaveBeenCalledTimes(1);
128
+ });
129
+ });
130
+
131
+ it<TestContext>("ignores any legacy data-captcha-type attribute on the host element", async (ctx) => {
132
+ // @ts-ignore
133
+ const script = ctx.document.createElement("script");
134
+ script.src = "https://example.com/procaptcha.js";
135
+ // Customers may still have this attribute on their HTML from before
136
+ // the server-driven captchaType refactor. The bundle must silently
137
+ // ignore it and always mount the unified dispatcher.
138
+ script.setAttribute("data-captcha-type", "image");
139
+ document.body.appendChild(script);
140
+
141
+ expect(ctx.window).toBeDefined();
142
+
143
+ if (!ctx.window) {
144
+ expect(true).toBe(false);
145
+ return;
146
+ }
147
+
148
+ await widgetFactory.createWidgets([script], {
149
+ siteKey: "1234",
150
+ });
151
+
152
+ await vi.waitFor(() => {
153
+ expect(ProcaptchaFrictionless).toHaveBeenCalledTimes(1);
154
+ });
155
+ });
156
+
157
+ it<TestContext>("user callbacks should be set when defined on the window and pulled in via data attributes", async (ctx) => {
158
+ // @ts-ignore
159
+ const script = ctx.document.createElement("script");
160
+ script.src = "https://example.com/procaptcha.js";
161
+ // @ts-ignore
162
+ script["data-callback"] = "onHuman";
163
+ // @ts-ignore
164
+ script["data-chalexpired-callback"] = "onChallengeExpired";
165
+ // @ts-ignore
166
+ script["data-expired-callback"] = "onExpired";
167
+ // @ts-ignore
168
+ script["data-error-callback"] = "onError";
169
+ // @ts-ignore
170
+ script["data-close-callback"] = "onClose";
171
+ // @ts-ignore
172
+ script["data-open-callback"] = "onOpen";
173
+ // @ts-ignore
174
+ script["data-failed-callback"] = "onFailed";
175
+ // @ts-ignore
176
+ script["data-reset-callback"] = "onReset";
177
+ document.body.appendChild(script);
178
+
179
+ expect(ctx.window).toBeDefined();
180
+
181
+ if (!ctx.window) {
182
+ expect(true).toBe(false);
183
+ return;
184
+ }
185
+
186
+ ctx.window.onHuman = callbacks.onHuman;
187
+ ctx.window.onChallengeExpired = callbacks.onChallengeExpired;
188
+ ctx.window.onExpired = callbacks.onExpired;
189
+ ctx.window.onError = callbacks.onError;
190
+ ctx.window.onClose = callbacks.onClose;
191
+ ctx.window.onOpen = callbacks.onOpen;
192
+ ctx.window.onFailed = callbacks.onFailed;
193
+ ctx.window.onReset = callbacks.onReset;
194
+ ctx.window.onExtensionNotFound = callbacks.onExtensionNotFound;
195
+ ctx.window.onReload = callbacks.onReload;
196
+
197
+ await widgetFactory.createWidgets([script], {
198
+ siteKey: "1234",
199
+ });
200
+
201
+ await vi.waitFor(() => {
202
+ expect(ProcaptchaFrictionless).toHaveBeenCalledTimes(1);
203
+ // hack to get mock calls
204
+ // @ts-ignore
205
+ const call = ProcaptchaFrictionless.mock.calls[0];
206
+ const props = call[0];
207
+
208
+ // Check that the core properties we care about are present
209
+ expect(props.config.account.address).toBe("1234");
210
+ expect(props.config.web2).toBe(true);
211
+ expect(props.config.solutionThreshold).toBe(80);
212
+ expect(props.config.dappName).toBe("ProsopoClientDapp");
213
+ expect(props.config.theme).toBe("light");
214
+ expect(props.config.mode).toBe("visible");
215
+
216
+ // Check that callbacks object exists (it will be empty since we're not testing callback extraction here)
217
+ expect(props.callbacks).toBeDefined();
218
+ expect(typeof props.callbacks).toBe("object");
219
+ });
220
+ });
221
+ });
@@ -0,0 +1,126 @@
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 createCache, { type EmotionCache } from "@emotion/cache";
16
+ import { CacheProvider } from "@emotion/react";
17
+ import type { Ti18n } from "@prosopo/locale";
18
+ import type {
19
+ Callbacks,
20
+ ProcaptchaClientConfigOutput,
21
+ ProcaptchaRenderOptions,
22
+ } from "@prosopo/types";
23
+ import type { ReactNode } from "react";
24
+ import { type Root, createRoot } from "react-dom/client";
25
+ import { createConfig } from "../configCreator.js";
26
+ import { setLanguage } from "../language.js";
27
+ import { setValidChallengeLength } from "../timeout.js";
28
+ import { BundleCaptcha } from "./components/bundleCaptcha.js";
29
+
30
+ interface RenderSettings {
31
+ identifierPrefix: string;
32
+ emotionCacheKey: string;
33
+ webComponentTag: string;
34
+ }
35
+
36
+ class CaptchaRenderer {
37
+ public renderCaptcha(
38
+ settings: RenderSettings,
39
+ container: HTMLElement,
40
+ renderOptions: ProcaptchaRenderOptions,
41
+ callbacks: Callbacks,
42
+ isWeb2: boolean,
43
+ i18n: Ti18n,
44
+ invisible: boolean,
45
+ widgetContainer: HTMLElement,
46
+ sourceElement?: Element,
47
+ ): Root {
48
+ const config = createConfig(
49
+ renderOptions.siteKey,
50
+ renderOptions.theme,
51
+ renderOptions.language,
52
+ isWeb2,
53
+ invisible,
54
+ renderOptions.userAccountAddress,
55
+ );
56
+ this.readAndValidateSettings(
57
+ sourceElement || container,
58
+ config,
59
+ renderOptions,
60
+ );
61
+
62
+ const reactRoot = this.createReactRoot(
63
+ container,
64
+ settings.identifierPrefix,
65
+ );
66
+
67
+ const emotionCache = this.makeEmotionCache(
68
+ settings.emotionCacheKey,
69
+ container,
70
+ );
71
+
72
+ const captchaComponent = (
73
+ <BundleCaptcha
74
+ config={config}
75
+ callbacks={callbacks}
76
+ i18n={i18n}
77
+ container={widgetContainer}
78
+ />
79
+ );
80
+
81
+ this.renderCaptchaComponent(reactRoot, emotionCache, captchaComponent);
82
+
83
+ return reactRoot;
84
+ }
85
+
86
+ protected readAndValidateSettings(
87
+ element: Element,
88
+ config: ProcaptchaClientConfigOutput,
89
+ renderOptions: ProcaptchaRenderOptions,
90
+ ): void {
91
+ setValidChallengeLength(renderOptions, element, config);
92
+ setLanguage(renderOptions, element, config);
93
+ }
94
+
95
+ protected makeEmotionCache(
96
+ cacheKey: string,
97
+ container: HTMLElement,
98
+ ): EmotionCache {
99
+ return createCache({
100
+ key: cacheKey,
101
+ prepend: true,
102
+ container: container,
103
+ });
104
+ }
105
+
106
+ protected createReactRoot(
107
+ container: HTMLElement,
108
+ identifierPrefix: string,
109
+ ): Root {
110
+ return createRoot(container, {
111
+ identifierPrefix: identifierPrefix,
112
+ });
113
+ }
114
+
115
+ protected renderCaptchaComponent(
116
+ reactRoot: Root,
117
+ emotionCache: EmotionCache,
118
+ captchaComponent: ReactNode,
119
+ ): void {
120
+ reactRoot.render(
121
+ <CacheProvider value={emotionCache}>{captchaComponent}</CacheProvider>,
122
+ );
123
+ }
124
+ }
125
+
126
+ export { CaptchaRenderer };
@@ -0,0 +1,57 @@
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 { Ti18n } from "@prosopo/locale";
16
+ import { getDefaultEvents } from "@prosopo/procaptcha-common";
17
+ import { ProcaptchaFrictionless } from "@prosopo/procaptcha-frictionless";
18
+ import type {
19
+ ProcaptchaCallbacks,
20
+ ProcaptchaClientConfigInput,
21
+ } from "@prosopo/types";
22
+ import { useState } from "react";
23
+
24
+ interface BundleCaptchaProps {
25
+ config: ProcaptchaClientConfigInput;
26
+ callbacks: ProcaptchaCallbacks;
27
+ i18n: Ti18n;
28
+ container: HTMLElement;
29
+ }
30
+
31
+ // Universal captcha mount used by the script-tag bundle. Wraps
32
+ // ProcaptchaFrictionless with a `key`-driven restart hook so a "no session"
33
+ // error can fully unmount and remount the underlying widget. The server
34
+ // decides which concrete challenge type to render via the /frictionless
35
+ // endpoint — the wrapper itself is type-agnostic.
36
+ const BundleCaptcha = (props: BundleCaptchaProps) => {
37
+ const { config, callbacks, i18n, container } = props;
38
+
39
+ const [componentKey, setComponentKey] = useState(0);
40
+
41
+ const restart = () => {
42
+ setComponentKey((prevKey) => prevKey + 1);
43
+ };
44
+
45
+ return (
46
+ <ProcaptchaFrictionless
47
+ key={componentKey}
48
+ config={config}
49
+ callbacks={getDefaultEvents(callbacks)}
50
+ restart={restart}
51
+ i18n={i18n}
52
+ container={container}
53
+ />
54
+ );
55
+ };
56
+
57
+ export { BundleCaptcha };
@@ -0,0 +1,40 @@
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
+ export const getProcaptchaScript = (
16
+ names: string[],
17
+ ): HTMLScriptElement | null => {
18
+ for (const name of names) {
19
+ const script = document.querySelector<HTMLScriptElement>(
20
+ `script[src*="${name}"]`,
21
+ );
22
+ if (script) {
23
+ return script;
24
+ }
25
+ }
26
+ return null;
27
+ };
28
+
29
+ export const extractParams = (names: string[]) => {
30
+ const script = getProcaptchaScript(names);
31
+ if (script) {
32
+ const params = new URLSearchParams(script.src.split("?")[1]);
33
+ return {
34
+ onloadUrlCallback: params.get("onload") || undefined,
35
+ renderExplicit: params.get("render") || undefined,
36
+ };
37
+ }
38
+
39
+ return { onloadUrlCallback: undefined, renderExplicit: undefined };
40
+ };
@@ -0,0 +1,51 @@
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 { Languages } from "@prosopo/locale";
16
+ import {
17
+ EnvironmentTypesSchema,
18
+ type ProcaptchaClientConfigOutput,
19
+ ProcaptchaConfigSchema,
20
+ } from "@prosopo/types";
21
+
22
+ function createConfig(
23
+ siteKey?: string,
24
+ theme: "light" | "dark" = "light",
25
+ language?: (typeof Languages)[keyof typeof Languages],
26
+ web2 = true,
27
+ invisible = false,
28
+ userAccountAddress?: string,
29
+ ): ProcaptchaClientConfigOutput {
30
+ if (!siteKey) {
31
+ siteKey = process.env.PROSOPO_SITE_KEY || "";
32
+ }
33
+
34
+ return ProcaptchaConfigSchema.parse({
35
+ defaultEnvironment: process.env.PROSOPO_DEFAULT_ENVIRONMENT
36
+ ? EnvironmentTypesSchema.parse(process.env.PROSOPO_DEFAULT_ENVIRONMENT)
37
+ : EnvironmentTypesSchema.enum.development,
38
+ userAccountAddress: userAccountAddress || "",
39
+ account: {
40
+ address: siteKey,
41
+ },
42
+ serverUrl: process.env.PROSOPO_SERVER_URL || "",
43
+ mongoAtlasUri: process.env.PROSOPO_MONGO_EVENTS_URI || "",
44
+ web2,
45
+ mode: invisible ? "invisible" : "visible",
46
+ theme,
47
+ language,
48
+ });
49
+ }
50
+
51
+ export { createConfig };
@@ -0,0 +1,53 @@
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 { LanguageSchema, type Languages } from "@prosopo/locale";
16
+ import type {
17
+ ProcaptchaClientConfigInput,
18
+ ProcaptchaRenderOptions,
19
+ } from "@prosopo/types";
20
+
21
+ /**
22
+ * Prioritizes language selection from:
23
+ * 1. renderOptions.language
24
+ * 2. element's data-language attribute
25
+ * 3. Defaults to 'en'
26
+ *
27
+ * Validates the language against currently supported languages
28
+ *
29
+ * @param renderOptions
30
+ * @param element
31
+ * @param config
32
+ */
33
+ export const setLanguage = (
34
+ renderOptions: ProcaptchaRenderOptions | undefined,
35
+ element: Element,
36
+ config: ProcaptchaClientConfigInput,
37
+ ) => {
38
+ const languageAttribute =
39
+ renderOptions?.language || element.getAttribute("data-language");
40
+
41
+ if (languageAttribute) {
42
+ config.language = validateLanguage(languageAttribute);
43
+ }
44
+ };
45
+
46
+ const validateLanguage = (languageAttribute: string | typeof Languages) => {
47
+ try {
48
+ return LanguageSchema.parse(languageAttribute);
49
+ } catch (error) {
50
+ console.error(`Invalid language attribute: ${languageAttribute}`);
51
+ return LanguageSchema.parse("en");
52
+ }
53
+ };
@@ -0,0 +1,43 @@
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 type {
15
+ ProcaptchaClientConfigOutput,
16
+ ProcaptchaRenderOptions,
17
+ } from "@prosopo/types";
18
+
19
+ /**
20
+ * Set the timeout for a solved captcha
21
+ *
22
+ * @param renderOptions
23
+ * @param element
24
+ * @param config
25
+ */
26
+ export const setValidChallengeLength = (
27
+ renderOptions: ProcaptchaRenderOptions | undefined,
28
+ element: Element,
29
+ config: ProcaptchaClientConfigOutput,
30
+ ): void => {
31
+ const challengeValidLengthAttribute =
32
+ renderOptions?.["challenge-valid-length"] ||
33
+ element.getAttribute("data-challenge-valid-length");
34
+
35
+ if (challengeValidLengthAttribute) {
36
+ config.captchas.image.solutionTimeout = Number.parseInt(
37
+ challengeValidLengthAttribute,
38
+ );
39
+ config.captchas.pow.solutionTimeout = Number.parseInt(
40
+ challengeValidLengthAttribute,
41
+ );
42
+ }
43
+ };