@prosopo/procaptcha-frictionless 2.12.21 → 2.12.23

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.
@@ -1,263 +1,170 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __copyProps = (to, from, except, desc) => {
9
- if (from && typeof from === "object" || typeof from === "function") {
10
- for (let key of __getOwnPropNames(from))
11
- if (!__hasOwnProp.call(to, key) && key !== except)
12
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
13
- }
14
- return to;
1
+ const require_customDetectBot = require("./customDetectBot.cjs");
2
+ const require_sessionInvalidatedRecovery = require("./sessionInvalidatedRecovery.cjs");
3
+ let _prosopo_locale = require("@prosopo/locale");
4
+ let _prosopo_procaptcha_common = require("@prosopo/procaptcha-common");
5
+ let _prosopo_types = require("@prosopo/types");
6
+ let _prosopo_widget_skeleton = require("@prosopo/widget-skeleton");
7
+ let react = require("react");
8
+ let react_jsx_runtime = require("react/jsx-runtime");
9
+ //#region src/ProcaptchaFrictionless.tsx
10
+ var ProcaptchaLoader = async () => (await import("@prosopo/procaptcha-react")).Procaptcha;
11
+ var ProcaptchaPuzzleLoader = async () => (await import("@prosopo/procaptcha-puzzle")).ProcaptchaPuzzle;
12
+ var ProcaptchaPowLoader = async () => (await import("@prosopo/procaptcha-pow")).ProcaptchaPow;
13
+ var renderPlaceholder = (theme, mode, errorMessage, isTranslationLoaded, translationFn, loading) => {
14
+ const checkboxTheme = "light" === theme ? _prosopo_widget_skeleton.lightTheme : _prosopo_widget_skeleton.darkTheme;
15
+ if (mode === "invisible") return null;
16
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_prosopo_procaptcha_common.Checkbox, {
17
+ theme: checkboxTheme,
18
+ onChange: async () => {},
19
+ checked: false,
20
+ labelText: isTranslationLoaded ? translationFn("WIDGET.I_AM_HUMAN") : "",
21
+ error: errorMessage,
22
+ "aria-label": "human checkbox",
23
+ loading
24
+ });
15
25
  };
16
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
17
- // If the importer is in node compatibility mode or this is not an ESM
18
- // file that has been converted to a CommonJS file using a Babel-
19
- // compatible transform (i.e. "__esModule" has not been set), then set
20
- // "default" to the CommonJS "module.exports" for node compatibility.
21
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
- mod
23
- ));
24
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
25
- const jsxRuntime = require("react/jsx-runtime");
26
- const locale = require("@prosopo/locale");
27
- const procaptchaCommon = require("@prosopo/procaptcha-common");
28
- const types = require("@prosopo/types");
29
- const widgetSkeleton = require("@prosopo/widget-skeleton");
30
- const react = require("react");
31
- const customDetectBot = require("./customDetectBot.cjs");
32
- const sessionInvalidatedRecovery = require("./sessionInvalidatedRecovery.cjs");
33
- const ProcaptchaLoader = async () => (await import("@prosopo/procaptcha-react")).Procaptcha;
34
- const ProcaptchaPuzzleLoader = async () => (await import("@prosopo/procaptcha-puzzle")).ProcaptchaPuzzle;
35
- const ProcaptchaPowLoader = async () => (await import("@prosopo/procaptcha-pow")).ProcaptchaPow;
36
- const renderPlaceholder = (theme, mode, errorMessage, isTranslationLoaded, translationFn, loading) => {
37
- const checkboxTheme = "light" === theme ? widgetSkeleton.lightTheme : widgetSkeleton.darkTheme;
38
- if (mode === "invisible") {
39
- return null;
40
- }
41
- return /* @__PURE__ */ jsxRuntime.jsx(
42
- procaptchaCommon.Checkbox,
43
- {
44
- theme: checkboxTheme,
45
- onChange: async () => {
46
- },
47
- checked: false,
48
- labelText: isTranslationLoaded ? translationFn("WIDGET.I_AM_HUMAN") : "",
49
- error: errorMessage,
50
- "aria-label": "human checkbox",
51
- loading
52
- }
53
- );
54
- };
55
- const defaultLoadingState = (attemptCount) => ({
56
- loading: false,
57
- attemptCount: attemptCount || 0
26
+ var defaultLoadingState = (attemptCount) => ({
27
+ loading: false,
28
+ attemptCount: attemptCount || 0
58
29
  });
59
- const ProcaptchaFrictionless = ({
60
- config,
61
- callbacks,
62
- restart,
63
- i18n,
64
- detectBot = customDetectBot.default,
65
- container
66
- }) => {
67
- const stateRef = react.useRef(defaultLoadingState(0));
68
- const events = procaptchaCommon.getDefaultEvents(callbacks);
69
- const pendingRetryCoordsRef = react.useRef(null);
70
- const sessionInvalidatedFiredRef = react.useRef(false);
71
- react.useEffect(() => {
72
- if (!config.language) return;
73
- if (i18n) {
74
- if (i18n.language !== config.language) {
75
- void i18n.changeLanguage(config.language);
76
- }
77
- return;
78
- }
79
- void locale.loadI18next(false, config.language);
80
- }, [i18n, config.language]);
81
- const [componentToRender, setComponentToRender] = react.useState(
82
- renderPlaceholder(
83
- config.theme,
84
- config.mode,
85
- stateRef.current.errorMessage,
86
- i18n.isInitialized,
87
- i18n.t,
88
- true
89
- )
90
- );
91
- const resetState = (attemptCount) => {
92
- stateRef.current = defaultLoadingState(
93
- attemptCount || stateRef.current.attemptCount
94
- );
95
- };
96
- const fallOverWithStyle = (errorMessage, errorKey) => {
97
- if (errorKey === "CAPTCHA.NO_SESSION_FOUND") {
98
- setTimeout(() => {
99
- restartComponentTimeout();
100
- }, 0);
101
- }
102
- setComponentToRender(
103
- renderPlaceholder(
104
- config.theme,
105
- config.mode,
106
- errorMessage || "Cannot load CAPTCHA",
107
- i18n.isInitialized,
108
- i18n.t,
109
- false
110
- )
111
- );
112
- };
113
- const restartComponentTimeout = () => {
114
- setTimeout(() => {
115
- resetState(0);
116
- events.onReset();
117
- restart();
118
- }, 1e4);
119
- };
120
- const renderForCaptchaType = async (captchaType, frictionlessState, autoStart = false, escalationCoords) => {
121
- const onEscalate = (next, newSessionId, coords) => {
122
- void renderForCaptchaType(
123
- next,
124
- {
125
- ...frictionlessState,
126
- sessionId: newSessionId
127
- },
128
- true,
129
- coords
130
- );
131
- };
132
- const onSessionInvalidated = (x, y) => {
133
- const { shouldRestart } = sessionInvalidatedRecovery.handleSessionInvalidated(
134
- x,
135
- y,
136
- sessionInvalidatedFiredRef,
137
- pendingRetryCoordsRef
138
- );
139
- if (!shouldRestart) return;
140
- resetState(0);
141
- void start();
142
- };
143
- const { autoStart: resumedAutoStart, startCoords: retryStartCoords } = sessionInvalidatedRecovery.consumeRetryMountProps(pendingRetryCoordsRef, autoStart);
144
- const startCoords = escalationCoords ?? retryStartCoords;
145
- if (captchaType === types.CaptchaType.image) {
146
- const Procaptcha = await ProcaptchaLoader();
147
- setComponentToRender(
148
- /* @__PURE__ */ jsxRuntime.jsx(
149
- Procaptcha,
150
- {
151
- config,
152
- callbacks,
153
- frictionlessState,
154
- i18n,
155
- autoStart: resumedAutoStart,
156
- startCoords,
157
- onSessionInvalidated
158
- }
159
- )
160
- );
161
- } else if (captchaType === types.CaptchaType.puzzle) {
162
- const ProcaptchaPuzzle = await ProcaptchaPuzzleLoader();
163
- setComponentToRender(
164
- /* @__PURE__ */ jsxRuntime.jsx(
165
- ProcaptchaPuzzle,
166
- {
167
- config,
168
- callbacks,
169
- frictionlessState,
170
- i18n,
171
- autoStart: resumedAutoStart,
172
- startCoords,
173
- onSessionInvalidated
174
- }
175
- )
176
- );
177
- } else {
178
- const ProcaptchaPow = await ProcaptchaPowLoader();
179
- setComponentToRender(
180
- /* @__PURE__ */ jsxRuntime.jsx(
181
- ProcaptchaPow,
182
- {
183
- config,
184
- callbacks,
185
- frictionlessState,
186
- i18n,
187
- onEscalate,
188
- autoStart: resumedAutoStart,
189
- startCoords,
190
- onSessionInvalidated
191
- }
192
- )
193
- );
194
- }
195
- };
196
- const start = async () => {
197
- if (!procaptchaCommon.isSecureBrowserContext()) {
198
- const errorMessage = i18n.isInitialized ? i18n.t("WIDGET.INSECURE_CONTEXT") : "Procaptcha requires a secure (HTTPS) connection";
199
- events.onError(new Error(errorMessage));
200
- fallOverWithStyle(errorMessage, "WIDGET.INSECURE_CONTEXT");
201
- return;
202
- }
203
- await procaptchaCommon.providerRetry(
204
- async () => {
205
- stateRef.current.attemptCount += 1;
206
- const configOutput = types.ProcaptchaConfigSchema.parse(config);
207
- const result = await detectBot(configOutput, container, restart, {
208
- attempt: stateRef.current.attemptCount
209
- });
210
- if (result.error?.message) {
211
- stateRef.current = {
212
- ...stateRef.current,
213
- loading: false,
214
- errorMessage: result.error?.message
215
- };
216
- events.onError(new Error(result.error?.message));
217
- fallOverWithStyle(result.error?.message, result.error?.key);
218
- return;
219
- }
220
- const frictionlessState = {
221
- provider: result.provider,
222
- sessionId: result.sessionId,
223
- userAccount: result.userAccount,
224
- restart,
225
- // Pass restart function
226
- behaviorCollector1: result.behaviorCollector1,
227
- behaviorCollector2: result.behaviorCollector2,
228
- behaviorCollector3: result.behaviorCollector3,
229
- deviceCapability: result.deviceCapability,
230
- encryptBehavioralData: result.encryptBehavioralData,
231
- getSimdReadings: result.getSimdReadings,
232
- hp: result.hp
233
- };
234
- await renderForCaptchaType(result.captchaType, frictionlessState);
235
- stateRef.current = {
236
- ...stateRef.current,
237
- loading: false
238
- };
239
- },
240
- start,
241
- resetState,
242
- stateRef.current.attemptCount,
243
- 5
244
- ).finally(() => {
245
- if (stateRef.current.attemptCount >= 5) {
246
- fallOverWithStyle();
247
- restartComponentTimeout();
248
- }
249
- });
250
- };
251
- const startedForKeyRef = react.useRef(null);
252
- react.useEffect(() => {
253
- const key = `${config.account?.address ?? ""}|${config.language ?? ""}|${config.mode ?? ""}`;
254
- if (startedForKeyRef.current === key) return;
255
- startedForKeyRef.current = key;
256
- void start();
257
- }, [config.account?.address, config.language, config.mode]);
258
- return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
259
- /* @__PURE__ */ jsxRuntime.jsx(procaptchaCommon.TestModeBanner, { siteKey: config.account?.address ?? "" }),
260
- componentToRender
261
- ] });
30
+ var ProcaptchaFrictionless = ({ config, callbacks, restart, i18n, detectBot = require_customDetectBot.default, container }) => {
31
+ const stateRef = (0, react.useRef)(defaultLoadingState(0));
32
+ const events = (0, _prosopo_procaptcha_common.getDefaultEvents)(callbacks);
33
+ const pendingRetryCoordsRef = (0, react.useRef)(null);
34
+ const sessionInvalidatedFiredRef = (0, react.useRef)(false);
35
+ (0, react.useEffect)(() => {
36
+ if (!config.language) return;
37
+ if (i18n) {
38
+ if (i18n.language !== config.language) i18n.changeLanguage(config.language);
39
+ return;
40
+ }
41
+ (0, _prosopo_locale.loadI18next)(false, config.language);
42
+ }, [i18n, config.language]);
43
+ const [componentToRender, setComponentToRender] = (0, react.useState)(renderPlaceholder(config.theme, config.mode, stateRef.current.errorMessage, i18n.isInitialized, i18n.t, true));
44
+ const resetState = (attemptCount) => {
45
+ stateRef.current = defaultLoadingState(attemptCount || stateRef.current.attemptCount);
46
+ };
47
+ const fallOverWithStyle = (errorMessage, errorKey) => {
48
+ if (errorKey === "CAPTCHA.NO_SESSION_FOUND") setTimeout(() => {
49
+ restartComponentTimeout();
50
+ }, 0);
51
+ setComponentToRender(renderPlaceholder(config.theme, config.mode, errorMessage || "Cannot load CAPTCHA", i18n.isInitialized, i18n.t, false));
52
+ };
53
+ const restartComponentTimeout = () => {
54
+ setTimeout(() => {
55
+ resetState(0);
56
+ events.onReset();
57
+ restart();
58
+ }, 1e4);
59
+ };
60
+ const renderForCaptchaType = async (captchaType, frictionlessState, autoStart = false, escalationCoords) => {
61
+ const onEscalate = (next, newSessionId, coords) => {
62
+ renderForCaptchaType(next, {
63
+ ...frictionlessState,
64
+ sessionId: newSessionId
65
+ }, true, coords);
66
+ };
67
+ const onSessionInvalidated = (x, y) => {
68
+ const { shouldRestart } = require_sessionInvalidatedRecovery.handleSessionInvalidated(x, y, sessionInvalidatedFiredRef, pendingRetryCoordsRef);
69
+ if (!shouldRestart) return;
70
+ resetState(0);
71
+ start();
72
+ };
73
+ const { autoStart: resumedAutoStart, startCoords: retryStartCoords } = require_sessionInvalidatedRecovery.consumeRetryMountProps(pendingRetryCoordsRef, autoStart);
74
+ const startCoords = escalationCoords ?? retryStartCoords;
75
+ if (captchaType === _prosopo_types.CaptchaType.image) {
76
+ const Procaptcha = await ProcaptchaLoader();
77
+ setComponentToRender(/* @__PURE__ */ (0, react_jsx_runtime.jsx)(Procaptcha, {
78
+ config,
79
+ callbacks,
80
+ frictionlessState,
81
+ i18n,
82
+ autoStart: resumedAutoStart,
83
+ startCoords,
84
+ onSessionInvalidated
85
+ }));
86
+ } else if (captchaType === _prosopo_types.CaptchaType.puzzle) {
87
+ const ProcaptchaPuzzle = await ProcaptchaPuzzleLoader();
88
+ setComponentToRender(/* @__PURE__ */ (0, react_jsx_runtime.jsx)(ProcaptchaPuzzle, {
89
+ config,
90
+ callbacks,
91
+ frictionlessState,
92
+ i18n,
93
+ autoStart: resumedAutoStart,
94
+ startCoords,
95
+ onSessionInvalidated
96
+ }));
97
+ } else {
98
+ const ProcaptchaPow = await ProcaptchaPowLoader();
99
+ setComponentToRender(/* @__PURE__ */ (0, react_jsx_runtime.jsx)(ProcaptchaPow, {
100
+ config,
101
+ callbacks,
102
+ frictionlessState,
103
+ i18n,
104
+ onEscalate,
105
+ autoStart: resumedAutoStart,
106
+ startCoords,
107
+ onSessionInvalidated
108
+ }));
109
+ }
110
+ };
111
+ const start = async () => {
112
+ if (!(0, _prosopo_procaptcha_common.isSecureBrowserContext)()) {
113
+ const errorMessage = i18n.isInitialized ? i18n.t("WIDGET.INSECURE_CONTEXT") : "Procaptcha requires a secure (HTTPS) connection";
114
+ events.onError(new Error(errorMessage));
115
+ fallOverWithStyle(errorMessage, "WIDGET.INSECURE_CONTEXT");
116
+ return;
117
+ }
118
+ await (0, _prosopo_procaptcha_common.providerRetry)(async () => {
119
+ stateRef.current.attemptCount += 1;
120
+ const result = await detectBot(_prosopo_types.ProcaptchaConfigSchema.parse(config), container, restart, { attempt: stateRef.current.attemptCount });
121
+ if (result.error?.message) {
122
+ stateRef.current = {
123
+ ...stateRef.current,
124
+ loading: false,
125
+ errorMessage: result.error?.message
126
+ };
127
+ events.onError(new Error(result.error?.message));
128
+ fallOverWithStyle(result.error?.message, result.error?.key);
129
+ return;
130
+ }
131
+ const frictionlessState = {
132
+ provider: result.provider,
133
+ sessionId: result.sessionId,
134
+ userAccount: result.userAccount,
135
+ restart,
136
+ behaviorCollector1: result.behaviorCollector1,
137
+ behaviorCollector2: result.behaviorCollector2,
138
+ behaviorCollector3: result.behaviorCollector3,
139
+ deviceCapability: result.deviceCapability,
140
+ encryptBehavioralData: result.encryptBehavioralData,
141
+ getSimdReadings: result.getSimdReadings,
142
+ hp: result.hp
143
+ };
144
+ await renderForCaptchaType(result.captchaType, frictionlessState);
145
+ stateRef.current = {
146
+ ...stateRef.current,
147
+ loading: false
148
+ };
149
+ }, start, resetState, stateRef.current.attemptCount, 5).finally(() => {
150
+ if (stateRef.current.attemptCount >= 5) {
151
+ fallOverWithStyle();
152
+ restartComponentTimeout();
153
+ }
154
+ });
155
+ };
156
+ const startedForKeyRef = (0, react.useRef)(null);
157
+ (0, react.useEffect)(() => {
158
+ const key = `${config.account?.address ?? ""}|${config.language ?? ""}|${config.mode ?? ""}`;
159
+ if (startedForKeyRef.current === key) return;
160
+ startedForKeyRef.current = key;
161
+ start();
162
+ }, [
163
+ config.account?.address,
164
+ config.language,
165
+ config.mode
166
+ ]);
167
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_prosopo_procaptcha_common.TestModeBanner, { siteKey: config.account?.address ?? "" }), componentToRender] });
262
168
  };
169
+ //#endregion
263
170
  exports.ProcaptchaFrictionless = ProcaptchaFrictionless;