@prosopo/procaptcha-bundle 4.2.4 → 4.3.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 (36) hide show
  1. package/.turbo/turbo-build$colon$cjs.log +16 -15
  2. package/.turbo/turbo-build$colon$tsc.log +26 -26
  3. package/.turbo/turbo-build.log +7 -6
  4. package/CHANGELOG.md +68 -0
  5. package/dist/cjs/index.cjs +45 -10
  6. package/dist/cjs/util/captcha/captchaRenderer.cjs +2 -0
  7. package/dist/cjs/util/startMode.cjs +20 -0
  8. package/dist/index.d.ts +3 -1
  9. package/dist/index.d.ts.map +1 -1
  10. package/dist/index.js +45 -11
  11. package/dist/index.js.map +1 -1
  12. package/dist/tests/procaptchaReady.unit.test.js +1 -0
  13. package/dist/tests/procaptchaReady.unit.test.js.map +1 -1
  14. package/dist/tests/start.unit.test.d.ts +2 -0
  15. package/dist/tests/start.unit.test.d.ts.map +1 -0
  16. package/dist/tests/start.unit.test.js +100 -0
  17. package/dist/tests/start.unit.test.js.map +1 -0
  18. package/dist/tests/util/startMode.test.d.ts +2 -0
  19. package/dist/tests/util/startMode.test.d.ts.map +1 -0
  20. package/dist/tests/util/startMode.test.js +48 -0
  21. package/dist/tests/util/startMode.test.js.map +1 -0
  22. package/dist/util/captcha/captchaRenderer.d.ts.map +1 -1
  23. package/dist/util/captcha/captchaRenderer.js +2 -0
  24. package/dist/util/captcha/captchaRenderer.js.map +1 -1
  25. package/dist/util/startMode.d.ts +5 -0
  26. package/dist/util/startMode.d.ts.map +1 -0
  27. package/dist/util/startMode.js +18 -0
  28. package/dist/util/startMode.js.map +1 -0
  29. package/package.json +12 -12
  30. package/src/index.ts +53 -10
  31. package/src/tests/procaptchaReady.unit.test.ts +1 -0
  32. package/src/tests/start.unit.test.ts +152 -0
  33. package/src/tests/util/startMode.test.ts +113 -0
  34. package/src/util/captcha/captchaRenderer.tsx +2 -0
  35. package/src/util/startMode.ts +53 -0
  36. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,152 @@
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 {
16
+ PROCAPTCHA_START_EVENT,
17
+ type ProcaptchaStartEventDetail,
18
+ StartModeEnum,
19
+ } from "@prosopo/types";
20
+ import type { Root } from "react-dom/client";
21
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
22
+
23
+ const mocks = vi.hoisted(() => ({
24
+ prefetchDetector: vi.fn(),
25
+ createWidgets: vi.fn(),
26
+ }));
27
+
28
+ vi.mock("@prosopo/procaptcha-frictionless", () => ({
29
+ prefetchDetector: mocks.prefetchDetector,
30
+ }));
31
+
32
+ vi.mock("@prosopo/procaptcha-common", () => ({
33
+ getWindowCallback: vi.fn(),
34
+ pickIpMode: vi.fn(() => undefined),
35
+ }));
36
+
37
+ vi.mock("../util/widgetFactory.js", () => ({
38
+ WidgetFactory: vi.fn(function () {
39
+ return { createWidgets: mocks.createWidgets };
40
+ }),
41
+ }));
42
+
43
+ const { render, remove, start } = await import("../index.js");
44
+
45
+ const SITE_KEY = "5CcNvLUdiXFpzKDMjThGLSK9rhWHA1H4EF3zrgkpkjAdqmuP";
46
+
47
+ const makeRoot = (): Root =>
48
+ ({ unmount: vi.fn(), render: vi.fn() }) as unknown as Root;
49
+
50
+ const flush = (): Promise<void> =>
51
+ new Promise((resolve) => setTimeout(resolve, 0));
52
+
53
+ const renderWidget = async (
54
+ options: Partial<Parameters<typeof render>[1]> = {},
55
+ ): Promise<{ element: HTMLDivElement; id: string }> => {
56
+ mocks.createWidgets.mockResolvedValueOnce([makeRoot()]);
57
+ const element = document.createElement("div");
58
+ const id = await render(element, { siteKey: SITE_KEY, ...options });
59
+ if (!id) throw new Error("expected render to return a widget id");
60
+ return { element, id };
61
+ };
62
+
63
+ let received: ProcaptchaStartEventDetail[];
64
+ const onStart = (event: Event): void => {
65
+ received.push((event as CustomEvent<ProcaptchaStartEventDetail>).detail);
66
+ };
67
+
68
+ beforeEach(async () => {
69
+ vi.clearAllMocks();
70
+ await remove();
71
+ received = [];
72
+ document.addEventListener(PROCAPTCHA_START_EVENT, onStart);
73
+ });
74
+
75
+ afterEach(() => {
76
+ document.removeEventListener(PROCAPTCHA_START_EVENT, onStart);
77
+ });
78
+
79
+ describe("start", () => {
80
+ it("addresses one event to each widget when no id is given", async () => {
81
+ const first = await renderWidget();
82
+ const second = await renderWidget();
83
+
84
+ start();
85
+
86
+ expect(received.map((detail) => detail.element)).toEqual([
87
+ first.element,
88
+ second.element,
89
+ ]);
90
+ });
91
+
92
+ it("addresses the event to the widget whose id is given", async () => {
93
+ await renderWidget();
94
+ const second = await renderWidget();
95
+
96
+ start(second.id);
97
+
98
+ expect(received).toHaveLength(1);
99
+ expect(received[0]?.element).toBe(second.element);
100
+ });
101
+
102
+ it("reports an unknown id instead of dispatching", async () => {
103
+ const error = vi.spyOn(console, "error").mockImplementation(() => {});
104
+ await renderWidget();
105
+
106
+ start("procaptcha-widget-does-not-exist");
107
+
108
+ expect(received).toHaveLength(0);
109
+ expect(error).toHaveBeenCalledTimes(1);
110
+ error.mockRestore();
111
+ });
112
+
113
+ it("reports when there is nothing to start", () => {
114
+ const error = vi.spyOn(console, "error").mockImplementation(() => {});
115
+
116
+ start();
117
+
118
+ expect(received).toHaveLength(0);
119
+ expect(error).toHaveBeenCalledTimes(1);
120
+ error.mockRestore();
121
+ });
122
+
123
+ it("is exposed on window.procaptcha", () => {
124
+ expect(window.procaptcha.start).toBe(start);
125
+ });
126
+ });
127
+
128
+ describe("detector prefetch", () => {
129
+ it("runs on render in auto mode", async () => {
130
+ await renderWidget();
131
+ await flush();
132
+
133
+ expect(mocks.prefetchDetector).toHaveBeenCalledTimes(1);
134
+ });
135
+
136
+ it("is skipped in manual mode so nothing reaches the provider on load", async () => {
137
+ await renderWidget({ startMode: StartModeEnum.manual });
138
+ await flush();
139
+
140
+ expect(mocks.prefetchDetector).not.toHaveBeenCalled();
141
+ });
142
+
143
+ it("is skipped when the element asks for manual mode", async () => {
144
+ mocks.createWidgets.mockResolvedValueOnce([makeRoot()]);
145
+ const element = document.createElement("div");
146
+ element.setAttribute("data-start-mode", "manual");
147
+ await render(element, { siteKey: SITE_KEY });
148
+ await flush();
149
+
150
+ expect(mocks.prefetchDetector).not.toHaveBeenCalled();
151
+ });
152
+ });
@@ -0,0 +1,113 @@
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 {
16
+ type ProcaptchaClientConfigOutput,
17
+ ProcaptchaConfigSchema,
18
+ type ProcaptchaRenderOptions,
19
+ StartModeEnum,
20
+ } from "@prosopo/types";
21
+ import { JSDOM } from "jsdom";
22
+ import { beforeEach, describe, expect, it, vi } from "vitest";
23
+ import {
24
+ START_MODE_ATTRIBUTE,
25
+ resolveStartMode,
26
+ setStartMode,
27
+ } from "../../util/startMode.js";
28
+
29
+ const SITE_KEY = "5site";
30
+
31
+ const makeConfig = (): ProcaptchaClientConfigOutput =>
32
+ ProcaptchaConfigSchema.parse({ account: { address: SITE_KEY } });
33
+
34
+ const makeElement = (attributes: Record<string, string> = {}): Element => {
35
+ const dom = new JSDOM("<!DOCTYPE html><html><body></body></html>");
36
+ const element = dom.window.document.createElement("div");
37
+ for (const [name, value] of Object.entries(attributes)) {
38
+ element.setAttribute(name, value);
39
+ }
40
+ return element;
41
+ };
42
+
43
+ const renderOptions = (
44
+ overrides: Partial<ProcaptchaRenderOptions> = {},
45
+ ): ProcaptchaRenderOptions => ({ siteKey: SITE_KEY, ...overrides });
46
+
47
+ describe("resolveStartMode", () => {
48
+ beforeEach(() => {
49
+ vi.restoreAllMocks();
50
+ });
51
+
52
+ it("defaults to auto", () => {
53
+ expect(resolveStartMode(renderOptions(), makeElement())).toBe(
54
+ StartModeEnum.auto,
55
+ );
56
+ expect(resolveStartMode(undefined, makeElement())).toBe(StartModeEnum.auto);
57
+ });
58
+
59
+ it("reads the start mode from the render options", () => {
60
+ expect(
61
+ resolveStartMode(
62
+ renderOptions({ startMode: StartModeEnum.manual }),
63
+ makeElement(),
64
+ ),
65
+ ).toBe(StartModeEnum.manual);
66
+ });
67
+
68
+ it("reads the start mode from the data attribute", () => {
69
+ expect(
70
+ resolveStartMode(
71
+ renderOptions(),
72
+ makeElement({ [START_MODE_ATTRIBUTE]: "manual" }),
73
+ ),
74
+ ).toBe(StartModeEnum.manual);
75
+ });
76
+
77
+ it("prefers the render options over the attribute", () => {
78
+ expect(
79
+ resolveStartMode(
80
+ renderOptions({ startMode: StartModeEnum.auto }),
81
+ makeElement({ [START_MODE_ATTRIBUTE]: "manual" }),
82
+ ),
83
+ ).toBe(StartModeEnum.auto);
84
+ });
85
+
86
+ it("falls back to auto on an unknown value and says so", () => {
87
+ const error = vi.spyOn(console, "error").mockImplementation(() => {});
88
+
89
+ expect(
90
+ resolveStartMode(
91
+ renderOptions(),
92
+ makeElement({ [START_MODE_ATTRIBUTE]: "later" }),
93
+ ),
94
+ ).toBe(StartModeEnum.auto);
95
+ expect(error).toHaveBeenCalledTimes(1);
96
+ expect(error.mock.calls[0]?.[0]).toContain("later");
97
+ });
98
+ });
99
+
100
+ describe("setStartMode", () => {
101
+ it("writes the resolved mode onto the config", () => {
102
+ const config = makeConfig();
103
+ expect(config.startMode).toBe(StartModeEnum.auto);
104
+
105
+ setStartMode(
106
+ renderOptions(),
107
+ makeElement({ [START_MODE_ATTRIBUTE]: "manual" }),
108
+ config,
109
+ );
110
+
111
+ expect(config.startMode).toBe(StartModeEnum.manual);
112
+ });
113
+ });
@@ -25,6 +25,7 @@ import { type Root, createRoot } from "react-dom/client";
25
25
  import { setClientSessionId } from "../clientSession.js";
26
26
  import { createConfig } from "../configCreator.js";
27
27
  import { setLanguage } from "../language.js";
28
+ import { setStartMode } from "../startMode.js";
28
29
  import { setValidChallengeLength } from "../timeout.js";
29
30
  import { BundleCaptcha } from "./components/bundleCaptcha.js";
30
31
 
@@ -94,6 +95,7 @@ class CaptchaRenderer {
94
95
  setValidChallengeLength(renderOptions, element, config);
95
96
  setLanguage(renderOptions, element, config);
96
97
  setClientSessionId(renderOptions, element, config);
98
+ setStartMode(renderOptions, element, config);
97
99
  }
98
100
 
99
101
  protected makeEmotionCache(
@@ -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 {
16
+ type ProcaptchaClientConfigOutput,
17
+ type ProcaptchaRenderOptions,
18
+ type StartMode,
19
+ StartModeEnum,
20
+ StartModeSchema,
21
+ } from "@prosopo/types";
22
+
23
+ export const START_MODE_ATTRIBUTE = "data-start-mode";
24
+
25
+ export const resolveStartMode = (
26
+ renderOptions: ProcaptchaRenderOptions | undefined,
27
+ element: Element,
28
+ ): StartMode => {
29
+ const requested =
30
+ renderOptions?.startMode || element.getAttribute(START_MODE_ATTRIBUTE);
31
+
32
+ if (!requested) {
33
+ return StartModeEnum.auto;
34
+ }
35
+
36
+ const parsed = StartModeSchema.safeParse(requested);
37
+ if (!parsed.success) {
38
+ console.error(
39
+ `Ignoring unknown start mode "${requested}"; expected one of ${StartModeSchema.options.join(", ")}`,
40
+ );
41
+ return StartModeEnum.auto;
42
+ }
43
+
44
+ return parsed.data;
45
+ };
46
+
47
+ export const setStartMode = (
48
+ renderOptions: ProcaptchaRenderOptions | undefined,
49
+ element: Element,
50
+ config: ProcaptchaClientConfigOutput,
51
+ ): void => {
52
+ config.startMode = resolveStartMode(renderOptions, element);
53
+ };