@prosopo/procaptcha-frictionless 2.17.0 → 2.18.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 (67) hide show
  1. package/.turbo/turbo-build$colon$cjs.log +6 -6
  2. package/.turbo/turbo-build$colon$tsc.log +24 -24
  3. package/.turbo/turbo-build.log +12 -12
  4. package/CHANGELOG.md +47 -0
  5. package/dist/{AuthenticatedBadge.d.ts → authenticatedBadge.d.ts} +3 -3
  6. package/dist/authenticatedBadge.d.ts.map +1 -0
  7. package/dist/authenticatedBadge.js +79 -0
  8. package/dist/authenticatedBadge.js.map +1 -0
  9. package/dist/cjs/authenticatedBadge.cjs +79 -0
  10. package/dist/cjs/detectorPrefetch.cjs +2 -2
  11. package/dist/cjs/index.cjs +2 -2
  12. package/dist/cjs/procaptchaFrictionless.cjs +269 -0
  13. package/dist/detectorPrefetch.js +2 -2
  14. package/dist/index.d.ts +1 -1
  15. package/dist/index.js +2 -2
  16. package/dist/procaptchaFrictionless.d.ts +6 -0
  17. package/dist/procaptchaFrictionless.d.ts.map +1 -0
  18. package/dist/procaptchaFrictionless.js +269 -0
  19. package/dist/procaptchaFrictionless.js.map +1 -0
  20. package/dist/tests/escalationClientSession.test.d.ts +0 -3
  21. package/dist/tests/escalationClientSession.test.d.ts.map +1 -1
  22. package/dist/tests/escalationClientSession.test.js +52 -61
  23. package/dist/tests/escalationClientSession.test.js.map +1 -1
  24. package/dist/tests/escalationHandoff.integration.test.d.ts +0 -3
  25. package/dist/tests/escalationHandoff.integration.test.d.ts.map +1 -1
  26. package/dist/tests/escalationHandoff.integration.test.js +62 -61
  27. package/dist/tests/escalationHandoff.integration.test.js.map +1 -1
  28. package/dist/tests/executeBeforeMount.test.d.ts +0 -3
  29. package/dist/tests/executeBeforeMount.test.d.ts.map +1 -1
  30. package/dist/tests/executeBeforeMount.test.js +53 -49
  31. package/dist/tests/executeBeforeMount.test.js.map +1 -1
  32. package/dist/tests/manualStart.test.d.ts +0 -3
  33. package/dist/tests/manualStart.test.d.ts.map +1 -1
  34. package/dist/tests/manualStart.test.js +55 -52
  35. package/dist/tests/manualStart.test.js.map +1 -1
  36. package/dist/tests/procaptchaFrictionless.test.d.ts +2 -0
  37. package/dist/tests/procaptchaFrictionless.test.d.ts.map +1 -0
  38. package/dist/tests/procaptchaFrictionless.test.js +244 -0
  39. package/dist/tests/procaptchaFrictionless.test.js.map +1 -0
  40. package/dist/tests/sessionInvalidatedRemint.test.d.ts +0 -3
  41. package/dist/tests/sessionInvalidatedRemint.test.d.ts.map +1 -1
  42. package/dist/tests/sessionInvalidatedRemint.test.js +49 -43
  43. package/dist/tests/sessionInvalidatedRemint.test.js.map +1 -1
  44. package/package.json +11 -13
  45. package/src/authenticatedBadge.ts +151 -0
  46. package/src/customDetectBot.ts +2 -2
  47. package/src/detectorPrefetch.ts +2 -2
  48. package/src/index.ts +1 -1
  49. package/src/{ProcaptchaFrictionless.tsx → procaptchaFrictionless.ts} +285 -314
  50. package/src/tests/{escalationClientSession.test.tsx → escalationClientSession.test.ts} +83 -92
  51. package/src/tests/{escalationHandoff.integration.test.tsx → escalationHandoff.integration.test.ts} +110 -108
  52. package/src/tests/{executeBeforeMount.test.tsx → executeBeforeMount.test.ts} +79 -77
  53. package/src/tests/{manualStart.test.tsx → manualStart.test.ts} +87 -83
  54. package/src/tests/procaptchaFrictionless.test.ts +408 -0
  55. package/src/tests/{sessionInvalidatedRemint.test.tsx → sessionInvalidatedRemint.test.ts} +67 -64
  56. package/tsconfig.cjs.json +1 -2
  57. package/tsconfig.tsbuildinfo +1 -1
  58. package/dist/AuthenticatedBadge.d.ts.map +0 -1
  59. package/dist/AuthenticatedBadge.js +0 -75
  60. package/dist/AuthenticatedBadge.js.map +0 -1
  61. package/dist/ProcaptchaFrictionless.d.ts +0 -3
  62. package/dist/ProcaptchaFrictionless.d.ts.map +0 -1
  63. package/dist/ProcaptchaFrictionless.js +0 -287
  64. package/dist/ProcaptchaFrictionless.js.map +0 -1
  65. package/dist/cjs/AuthenticatedBadge.cjs +0 -75
  66. package/dist/cjs/ProcaptchaFrictionless.cjs +0 -287
  67. package/src/AuthenticatedBadge.tsx +0 -141
@@ -19,69 +19,73 @@ import {
19
19
  type BotDetectionFunctionResult,
20
20
  CaptchaType,
21
21
  ModeEnum,
22
- type ProcaptchaClientConfigInput,
22
+ type ProcaptchaClientConfigOutput,
23
23
  type ProcaptchaProps,
24
24
  type RandomProvider,
25
25
  StartModeEnum,
26
26
  } from "@prosopo/types";
27
- import { type ReactElement, act, createElement, useEffect } from "react";
28
- import { type Root, createRoot } from "react-dom/client";
29
27
  import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
30
28
 
31
- declare global {
32
- var IS_REACT_ACT_ENVIRONMENT: boolean;
33
- }
34
-
35
- globalThis.IS_REACT_ACT_ENVIRONMENT = true;
36
-
37
29
  const EXECUTE_EVENT = "procaptcha:execute";
38
30
 
39
- const mocks = vi.hoisted(() => ({
40
- executes: [] as ("container" | "document")[],
41
- }));
42
-
43
- // Listens the way the real inner widgets do: on the container in either
44
- // mode, and on document only when invisible.
45
- const ListeningWidget = (props: ProcaptchaProps) => {
46
- const { container } = props;
47
- const invisible = props.config.mode === "invisible";
48
- useEffect(() => {
49
- const onContainer = () => mocks.executes.push("container");
50
- const onDocument = () => mocks.executes.push("document");
31
+ const mocks = vi.hoisted(() => {
32
+ const executes: ("container" | "document")[] = [];
33
+ // Listens the way the real inner widgets do: on the container in either
34
+ // mode, and on document only when invisible.
35
+ const listeningWidget = (host: HTMLElement, props: ProcaptchaProps) => {
36
+ const { container } = props;
37
+ const invisible = "invisible" === props.config.mode;
38
+ const onContainer = () => executes.push("container");
39
+ const onDocument = () => executes.push("document");
51
40
  container?.addEventListener(EXECUTE_EVENT, onContainer);
52
41
  if (invisible) document.addEventListener(EXECUTE_EVENT, onDocument);
53
- return () => {
54
- container?.removeEventListener(EXECUTE_EVENT, onContainer);
55
- if (invisible) document.removeEventListener(EXECUTE_EVENT, onDocument);
42
+ const element = host.ownerDocument.createElement("div");
43
+ element.setAttribute("data-widget", "image");
44
+ host.appendChild(element);
45
+ return {
46
+ destroy: () => {
47
+ container?.removeEventListener(EXECUTE_EVENT, onContainer);
48
+ if (invisible) document.removeEventListener(EXECUTE_EVENT, onDocument);
49
+ element.remove();
50
+ },
56
51
  };
57
- }, [container, invisible]);
58
- return createElement("div", { "data-widget": "image" });
59
- };
52
+ };
53
+ return { executes, listeningWidget };
54
+ });
60
55
 
61
- vi.mock("@prosopo/procaptcha-react", () => ({ Procaptcha: ListeningWidget }));
62
- vi.mock("@prosopo/procaptcha-pow", () => ({ ProcaptchaPow: ListeningWidget }));
56
+ vi.mock("@prosopo/procaptcha-react", () => ({
57
+ mountProcaptchaImageWidget: mocks.listeningWidget,
58
+ }));
59
+ vi.mock("@prosopo/procaptcha-pow", () => ({
60
+ mountProcaptchaPowWidget: mocks.listeningWidget,
61
+ }));
63
62
  vi.mock("@prosopo/procaptcha-puzzle", () => ({
64
- ProcaptchaPuzzle: ListeningWidget,
63
+ mountProcaptchaPuzzleWidget: mocks.listeningWidget,
65
64
  }));
66
65
 
67
- vi.mock("@prosopo/procaptcha-common", async (importOriginal) => {
68
- const actual =
69
- await importOriginal<typeof import("@prosopo/procaptcha-common")>();
70
- return { ...actual, isSecureBrowserContext: () => true };
71
- });
66
+ // jsdom serves the suite over http, so the widget's own HTTPS guard would
67
+ // short-circuit every test before detection ran.
68
+ vi.mock("@prosopo/procaptcha-common", async (importOriginal) => ({
69
+ ...(await importOriginal<typeof import("@prosopo/procaptcha-common")>()),
70
+ isSecureBrowserContext: () => true,
71
+ }));
72
72
 
73
- const { ProcaptchaFrictionless } = await import("../ProcaptchaFrictionless.js");
73
+ const { mountProcaptchaFrictionless } = await import(
74
+ "../procaptchaFrictionless.js"
75
+ );
74
76
 
75
77
  const config = (
76
- overrides: Partial<ProcaptchaClientConfigInput> = {},
77
- ): ProcaptchaClientConfigInput => ({
78
- account: { address: "5siteKey" },
79
- userAccountAddress: "",
80
- web2: true,
81
- mode: ModeEnum.invisible,
82
- startMode: StartModeEnum.auto,
83
- ...overrides,
84
- });
78
+ overrides: Partial<ProcaptchaClientConfigOutput> = {},
79
+ ): ProcaptchaClientConfigOutput =>
80
+ ({
81
+ account: { address: "5siteKey" },
82
+ userAccountAddress: "",
83
+ web2: true,
84
+ theme: "light",
85
+ mode: ModeEnum.invisible,
86
+ startMode: StartModeEnum.auto,
87
+ ...overrides,
88
+ }) as unknown as ProcaptchaClientConfigOutput;
85
89
 
86
90
  const i18nStub = {
87
91
  isInitialized: true,
@@ -90,13 +94,19 @@ const i18nStub = {
90
94
  changeLanguage: vi.fn(),
91
95
  } as unknown as Ti18n;
92
96
 
93
- const detectionResult = (): BotDetectionFunctionResult => ({
94
- status: "ok",
95
- captchaType: CaptchaType.image,
96
- sessionId: "provider-session",
97
- provider: { provider: { url: "https://provider.test" } } as RandomProvider,
98
- userAccount: { account: { address: "5FakeUserAccountAddress" } } as Account,
99
- });
97
+ const detectionResult = (): BotDetectionFunctionResult =>
98
+ ({
99
+ status: "ok",
100
+ captchaType: CaptchaType.image,
101
+ sessionId: "provider-session",
102
+ provider: { provider: { url: "https://provider.test" } } as RandomProvider,
103
+ userAccount: { account: { address: "5FakeUserAccountAddress" } } as Account,
104
+ }) as unknown as BotDetectionFunctionResult;
105
+
106
+ const settle = async (): Promise<void> => {
107
+ await new Promise<void>((resolve: () => void) => setTimeout(resolve, 0));
108
+ await Promise.resolve();
109
+ };
100
110
 
101
111
  interface HeldDetection {
102
112
  detectBot: ReturnType<typeof vi.fn<BotDetectionFunction>>;
@@ -112,55 +122,47 @@ const heldDetection = (): HeldDetection => {
112
122
  }),
113
123
  );
114
124
  const finish = async () => {
115
- await act(async () => {
116
- resolve?.(detectionResult());
117
- });
125
+ resolve?.(detectionResult());
126
+ await settle();
118
127
  };
119
128
  return { detectBot, finish };
120
129
  };
121
130
 
122
131
  let host: HTMLDivElement;
123
132
  let container: HTMLDivElement;
124
- let root: Root;
133
+ let widget: { destroy: () => void } | undefined;
125
134
 
126
135
  const mountWrapper = async (
127
136
  detectBot: BotDetectionFunction,
128
- overrides: Partial<ProcaptchaClientConfigInput> = {},
137
+ overrides: Partial<ProcaptchaClientConfigOutput> = {},
129
138
  ): Promise<void> => {
130
- await act(async () => {
131
- root.render(
132
- createElement(ProcaptchaFrictionless, {
133
- config: config(overrides),
134
- callbacks: {},
135
- restart: vi.fn(),
136
- i18n: i18nStub,
137
- detectBot,
138
- container,
139
- }) as ReactElement,
140
- );
139
+ widget = mountProcaptchaFrictionless(host, {
140
+ config: config(overrides),
141
+ callbacks: {},
142
+ restart: vi.fn(),
143
+ i18n: i18nStub,
144
+ detectBot,
145
+ container,
141
146
  });
147
+ await settle();
142
148
  };
143
149
 
144
150
  const dispatch = async (target: EventTarget): Promise<void> => {
145
- await act(async () => {
146
- target.dispatchEvent(new CustomEvent(EXECUTE_EVENT));
147
- });
151
+ target.dispatchEvent(new CustomEvent(EXECUTE_EVENT));
152
+ await settle();
148
153
  };
149
154
 
150
155
  beforeEach(() => {
151
156
  mocks.executes.length = 0;
157
+ widget = undefined;
152
158
  host = document.createElement("div");
153
159
  container = document.createElement("div");
154
160
  document.body.append(host, container);
155
- act(() => {
156
- root = createRoot(host);
157
- });
158
161
  });
159
162
 
160
163
  afterEach(() => {
161
- act(() => {
162
- root.unmount();
163
- });
164
+ widget?.destroy();
165
+ widget = undefined;
164
166
  host.remove();
165
167
  container.remove();
166
168
  vi.clearAllMocks();
@@ -20,63 +20,70 @@ import {
20
20
  CaptchaType,
21
21
  ModeEnum,
22
22
  PROCAPTCHA_START_EVENT,
23
- type ProcaptchaClientConfigInput,
23
+ type ProcaptchaClientConfigOutput,
24
+ type ProcaptchaFrictionlessProps,
24
25
  type ProcaptchaProps,
25
26
  type ProcaptchaStartEventDetail,
26
27
  type RandomProvider,
27
28
  StartModeEnum,
28
29
  } from "@prosopo/types";
29
- import { type ReactElement, act, createElement } from "react";
30
- import { type Root, createRoot } from "react-dom/client";
31
30
  import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
32
31
 
33
- declare global {
34
- var IS_REACT_ACT_ENVIRONMENT: boolean;
35
- }
36
-
37
- globalThis.IS_REACT_ACT_ENVIRONMENT = true;
38
-
39
32
  type InnerWidget = "pow" | "image" | "puzzle";
40
33
 
41
- const mocks = vi.hoisted(() => ({
42
- mounts: [] as {
34
+ const mocks = vi.hoisted(() => {
35
+ const mounts: {
43
36
  widget: "pow" | "image" | "puzzle";
44
37
  props: ProcaptchaProps;
45
- }[],
46
- }));
47
-
48
- const stub = (widget: InnerWidget) => (props: ProcaptchaProps) => {
49
- mocks.mounts.push({ widget, props });
50
- return createElement("div", { "data-widget": widget });
51
- };
38
+ }[] = [];
39
+ const stub =
40
+ (widget: "pow" | "image" | "puzzle") =>
41
+ (container: HTMLElement, props: ProcaptchaProps) => {
42
+ mounts.push({ widget, props });
43
+ const element = container.ownerDocument.createElement("div");
44
+ element.setAttribute("data-widget", widget);
45
+ container.appendChild(element);
46
+ return { destroy: () => element.remove() };
47
+ };
48
+ return { mounts, stub };
49
+ });
52
50
 
53
- vi.mock("@prosopo/procaptcha-pow", () => ({ ProcaptchaPow: stub("pow") }));
54
- vi.mock("@prosopo/procaptcha-react", () => ({ Procaptcha: stub("image") }));
51
+ vi.mock("@prosopo/procaptcha-pow", () => ({
52
+ mountProcaptchaPowWidget: mocks.stub("pow"),
53
+ }));
54
+ vi.mock("@prosopo/procaptcha-react", () => ({
55
+ mountProcaptchaImageWidget: mocks.stub("image"),
56
+ }));
55
57
  vi.mock("@prosopo/procaptcha-puzzle", () => ({
56
- ProcaptchaPuzzle: stub("puzzle"),
58
+ mountProcaptchaPuzzleWidget: mocks.stub("puzzle"),
57
59
  }));
58
60
 
59
- vi.mock("@prosopo/procaptcha-common", async (importOriginal) => {
60
- const actual =
61
- await importOriginal<typeof import("@prosopo/procaptcha-common")>();
62
- return { ...actual, isSecureBrowserContext: () => true };
63
- });
61
+ // jsdom serves the suite over http, so the widget's own HTTPS guard would
62
+ // short-circuit every test before detection ran.
63
+ vi.mock("@prosopo/procaptcha-common", async (importOriginal) => ({
64
+ ...(await importOriginal<typeof import("@prosopo/procaptcha-common")>()),
65
+ isSecureBrowserContext: () => true,
66
+ }));
64
67
 
65
- const { ProcaptchaFrictionless } = await import("../ProcaptchaFrictionless.js");
68
+ const { mountProcaptchaFrictionless } = await import(
69
+ "../procaptchaFrictionless.js"
70
+ );
66
71
 
67
72
  const SITE_KEY = "5siteKey";
68
73
  const SESSION_ID = "provider-session";
69
74
 
70
75
  const config = (
71
- overrides: Partial<ProcaptchaClientConfigInput> = {},
72
- ): ProcaptchaClientConfigInput => ({
73
- account: { address: SITE_KEY },
74
- userAccountAddress: "",
75
- web2: true,
76
- mode: ModeEnum.visible,
77
- startMode: StartModeEnum.manual,
78
- ...overrides,
79
- });
76
+ overrides: Partial<ProcaptchaClientConfigOutput> = {},
77
+ ): ProcaptchaClientConfigOutput =>
78
+ ({
79
+ account: { address: SITE_KEY },
80
+ userAccountAddress: "",
81
+ web2: true,
82
+ theme: "light",
83
+ mode: ModeEnum.visible,
84
+ startMode: StartModeEnum.manual,
85
+ ...overrides,
86
+ }) as unknown as ProcaptchaClientConfigOutput;
80
87
 
81
88
  const i18nStub = {
82
89
  isInitialized: true,
@@ -85,22 +92,28 @@ const i18nStub = {
85
92
  changeLanguage: vi.fn(),
86
93
  } as unknown as Ti18n;
87
94
 
88
- const detectionResult = (): BotDetectionFunctionResult => ({
89
- status: "ok",
90
- captchaType: CaptchaType.image,
91
- sessionId: SESSION_ID,
92
- provider: { provider: { url: "https://provider.test" } } as RandomProvider,
93
- userAccount: { account: { address: "5FakeUserAccountAddress" } } as Account,
94
- });
95
+ const detectionResult = (): BotDetectionFunctionResult =>
96
+ ({
97
+ status: "ok",
98
+ captchaType: CaptchaType.image,
99
+ sessionId: SESSION_ID,
100
+ provider: { provider: { url: "https://provider.test" } } as RandomProvider,
101
+ userAccount: { account: { address: "5FakeUserAccountAddress" } } as Account,
102
+ }) as unknown as BotDetectionFunctionResult;
95
103
 
96
104
  const detectBotResolving = () =>
97
105
  vi.fn<BotDetectionFunction>().mockResolvedValue(detectionResult());
98
106
 
99
107
  let host: HTMLDivElement;
100
- let root: Root;
108
+ let widget: { destroy: () => void } | undefined;
109
+
110
+ const settle = async (): Promise<void> => {
111
+ await new Promise<void>((resolve: () => void) => setTimeout(resolve, 0));
112
+ await Promise.resolve();
113
+ };
101
114
 
102
115
  interface MountOptions {
103
- config?: ProcaptchaClientConfigInput;
116
+ config?: ProcaptchaClientConfigOutput;
104
117
  detectBot?: ReturnType<typeof detectBotResolving>;
105
118
  container?: HTMLElement;
106
119
  }
@@ -109,18 +122,16 @@ const mountWrapper = async (
109
122
  options: MountOptions = {},
110
123
  ): Promise<ReturnType<typeof detectBotResolving>> => {
111
124
  const detectBot = options.detectBot ?? detectBotResolving();
112
- await act(async () => {
113
- root.render(
114
- createElement(ProcaptchaFrictionless, {
115
- config: options.config ?? config(),
116
- callbacks: {},
117
- restart: vi.fn(),
118
- i18n: i18nStub,
119
- detectBot,
120
- container: options.container,
121
- }) as ReactElement,
122
- );
123
- });
125
+ const props: ProcaptchaFrictionlessProps = {
126
+ config: options.config ?? config(),
127
+ callbacks: {},
128
+ restart: vi.fn(),
129
+ i18n: i18nStub,
130
+ detectBot,
131
+ container: options.container,
132
+ };
133
+ widget = mountProcaptchaFrictionless(host, props);
134
+ await settle();
124
135
  return detectBot;
125
136
  };
126
137
 
@@ -134,9 +145,9 @@ const checkbox = (): HTMLInputElement => {
134
145
 
135
146
  const spinner = (): Element | null => host.querySelector('[role="status"]');
136
147
 
137
- const lastMountOf = (widget: InnerWidget) => {
138
- const mount = mocks.mounts.filter((m) => m.widget === widget).at(-1);
139
- if (!mount) throw new Error(`expected the ${widget} widget to have mounted`);
148
+ const lastMountOf = (target: InnerWidget) => {
149
+ const mount = mocks.mounts.filter((m) => m.widget === target).at(-1);
150
+ if (!mount) throw new Error(`expected the ${target} widget to have mounted`);
140
151
  return mount;
141
152
  };
142
153
 
@@ -174,43 +185,37 @@ const click = async (
174
185
  clientY: options.clientY ?? 0,
175
186
  });
176
187
  setTrusted(event, options.trusted ?? true);
177
- await act(async () => {
178
- element.dispatchEvent(event);
179
- });
188
+ element.dispatchEvent(event);
189
+ await settle();
180
190
  };
181
191
 
182
192
  const dispatchStart = async (
183
193
  detail?: ProcaptchaStartEventDetail,
184
194
  ): Promise<void> => {
185
- await act(async () => {
186
- document.dispatchEvent(
187
- new CustomEvent<ProcaptchaStartEventDetail>(PROCAPTCHA_START_EVENT, {
188
- detail,
189
- bubbles: true,
190
- }),
191
- );
192
- });
195
+ document.dispatchEvent(
196
+ new CustomEvent<ProcaptchaStartEventDetail>(PROCAPTCHA_START_EVENT, {
197
+ detail,
198
+ bubbles: true,
199
+ }),
200
+ );
201
+ await settle();
193
202
  };
194
203
 
195
204
  const dispatchExecute = async (): Promise<void> => {
196
- await act(async () => {
197
- document.dispatchEvent(new CustomEvent("procaptcha:execute"));
198
- });
205
+ document.dispatchEvent(new CustomEvent("procaptcha:execute"));
206
+ await settle();
199
207
  };
200
208
 
201
209
  beforeEach(() => {
202
210
  mocks.mounts.length = 0;
211
+ widget = undefined;
203
212
  host = document.createElement("div");
204
213
  document.body.appendChild(host);
205
- act(() => {
206
- root = createRoot(host);
207
- });
208
214
  });
209
215
 
210
216
  afterEach(() => {
211
- act(() => {
212
- root.unmount();
213
- });
217
+ widget?.destroy();
218
+ widget = undefined;
214
219
  host.remove();
215
220
  vi.clearAllMocks();
216
221
  });
@@ -357,9 +362,8 @@ describe("manual start mode", () => {
357
362
  expect(spinner()).not.toBeNull();
358
363
  expect(mocks.mounts).toHaveLength(0);
359
364
 
360
- await act(async () => {
361
- finish?.(detectionResult());
362
- });
365
+ finish?.(detectionResult());
366
+ await settle();
363
367
 
364
368
  expect(spinner()).toBeNull();
365
369
  expect(lastMountOf("image").props.autoStart).toBe(true);