@prosopo/procaptcha-puzzle 2.10.39 → 2.10.40
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/.turbo/turbo-build$colon$cjs.log +13 -11
- package/.turbo/turbo-build$colon$tsc.log +19 -19
- package/.turbo/turbo-build.log +15 -12
- package/CHANGELOG.md +34 -0
- package/dist/_virtual/_rolldown/runtime.js +3 -0
- package/dist/cjs/components/ProcaptchaPuzzle.cjs +6 -9
- package/dist/cjs/components/ProcaptchaWidget.cjs +186 -213
- package/dist/cjs/components/PuzzleCanvas.cjs +228 -270
- package/dist/cjs/index.cjs +2 -4
- package/dist/cjs/services/Manager.cjs +209 -289
- package/dist/components/ProcaptchaPuzzle.js +6 -8
- package/dist/components/ProcaptchaWidget.js +185 -212
- package/dist/components/PuzzleCanvas.js +229 -271
- package/dist/index.js +2 -4
- package/dist/services/Manager.d.ts.map +1 -1
- package/dist/services/Manager.js +205 -286
- package/dist/services/Manager.js.map +1 -1
- package/package.json +11 -11
- package/src/services/Manager.ts +4 -4
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -1,215 +1,188 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { useTranslation, loadI18next } from "@prosopo/locale";
|
|
3
|
-
import { useProcaptcha, buildUpdateState, Honeypot, Checkbox } from "@prosopo/procaptcha-common";
|
|
4
|
-
import { ModeEnum } from "@prosopo/types";
|
|
5
|
-
import { lightTheme, darkTheme } from "@prosopo/widget-skeleton";
|
|
6
|
-
import { useState, useRef, useEffect, useCallback } from "react";
|
|
1
|
+
import "../_virtual/_rolldown/runtime.js";
|
|
7
2
|
import { Manager } from "../services/Manager.js";
|
|
8
3
|
import { PuzzleCanvas } from "./PuzzleCanvas.js";
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
x = mouseOrTouchEvent.touches[0].clientX;
|
|
192
|
-
y = mouseOrTouchEvent.touches[0].clientY;
|
|
193
|
-
} else if ("clientX" in mouseOrTouchEvent && "clientY" in mouseOrTouchEvent) {
|
|
194
|
-
x = mouseOrTouchEvent.clientX;
|
|
195
|
-
y = mouseOrTouchEvent.clientY;
|
|
196
|
-
}
|
|
197
|
-
lastCoordsRef.current = { x, y };
|
|
198
|
-
const challenge = await manager.current.start(x, y);
|
|
199
|
-
if (challenge) {
|
|
200
|
-
setChallengeData(challenge);
|
|
201
|
-
setPuzzlePhase("dragging");
|
|
202
|
-
}
|
|
203
|
-
setLoading(false);
|
|
204
|
-
},
|
|
205
|
-
labelText: isTranslationReady ? t("WIDGET.I_AM_HUMAN") : "",
|
|
206
|
-
error: state.error?.message,
|
|
207
|
-
"aria-label": "human checkbox",
|
|
208
|
-
loading: loading || puzzlePhase === "submitting"
|
|
209
|
-
}
|
|
210
|
-
)
|
|
211
|
-
] });
|
|
212
|
-
};
|
|
213
|
-
export {
|
|
214
|
-
Procaptcha as default
|
|
4
|
+
import { loadI18next, useTranslation } from "@prosopo/locale";
|
|
5
|
+
import { Checkbox, Honeypot, buildUpdateState, useProcaptcha } from "@prosopo/procaptcha-common";
|
|
6
|
+
import { ModeEnum } from "@prosopo/types";
|
|
7
|
+
import { darkTheme, lightTheme } from "@prosopo/widget-skeleton";
|
|
8
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
9
|
+
import { Fragment, jsx, jsxs } from "@emotion/react/jsx-runtime";
|
|
10
|
+
//#region src/components/ProcaptchaWidget.tsx
|
|
11
|
+
var PROCAPTCHA_EXECUTE_EVENT = "procaptcha:execute";
|
|
12
|
+
var Procaptcha = (props) => {
|
|
13
|
+
const { t, ready: isTranslationReady } = useTranslation();
|
|
14
|
+
const config = props.config;
|
|
15
|
+
const i18n = props.i18n;
|
|
16
|
+
const theme = "light" === config.theme ? lightTheme : darkTheme;
|
|
17
|
+
const frictionlessState = props.frictionlessState;
|
|
18
|
+
const callbacks = props.callbacks || {};
|
|
19
|
+
const [state, _updateState] = useProcaptcha(useState, useRef);
|
|
20
|
+
const [loading, setLoading] = useState(false);
|
|
21
|
+
const [puzzlePhase, setPuzzlePhase] = useState("checkbox");
|
|
22
|
+
const [challengeData, setChallengeData] = useState(null);
|
|
23
|
+
const [showRetry, setShowRetry] = useState(false);
|
|
24
|
+
const updateState = buildUpdateState(state, _updateState);
|
|
25
|
+
const hpRef = useRef(null);
|
|
26
|
+
const manager = useRef(Manager(config, state, updateState, callbacks, frictionlessState, () => hpRef.current?.value || void 0));
|
|
27
|
+
const lastCoordsRef = useRef(null);
|
|
28
|
+
const sessionInvalidatedFiredRef = useRef(false);
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
if (!config.language) return;
|
|
31
|
+
if (i18n) {
|
|
32
|
+
if (i18n.language !== config.language) i18n.changeLanguage(config.language);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
loadI18next(false, config.language);
|
|
36
|
+
}, [i18n, config.language]);
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
if (!props.autoStart) return;
|
|
39
|
+
setLoading(true);
|
|
40
|
+
setShowRetry(false);
|
|
41
|
+
const coords = props.startCoords;
|
|
42
|
+
lastCoordsRef.current = coords ?? null;
|
|
43
|
+
manager.current.start(coords?.x ?? 0, coords?.y ?? 0).then((challenge) => {
|
|
44
|
+
if (challenge) {
|
|
45
|
+
setChallengeData(challenge);
|
|
46
|
+
setPuzzlePhase("dragging");
|
|
47
|
+
}
|
|
48
|
+
setLoading(false);
|
|
49
|
+
}, () => setLoading(false));
|
|
50
|
+
}, [props.autoStart, props.startCoords]);
|
|
51
|
+
useEffect(() => {
|
|
52
|
+
if (!state.error) return void 0;
|
|
53
|
+
setLoading(false);
|
|
54
|
+
setPuzzlePhase("checkbox");
|
|
55
|
+
setChallengeData(null);
|
|
56
|
+
setShowRetry(false);
|
|
57
|
+
if (state.error.key !== "CAPTCHA.NO_SESSION_FOUND") return void 0;
|
|
58
|
+
if (props.onSessionInvalidated && !sessionInvalidatedFiredRef.current) {
|
|
59
|
+
sessionInvalidatedFiredRef.current = true;
|
|
60
|
+
const coords = lastCoordsRef.current;
|
|
61
|
+
props.onSessionInvalidated(coords?.x, coords?.y);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
if (frictionlessState) {
|
|
65
|
+
const timer = setTimeout(() => {
|
|
66
|
+
frictionlessState.restart();
|
|
67
|
+
}, 100);
|
|
68
|
+
return () => clearTimeout(timer);
|
|
69
|
+
}
|
|
70
|
+
}, [
|
|
71
|
+
state.error,
|
|
72
|
+
frictionlessState,
|
|
73
|
+
props.onSessionInvalidated
|
|
74
|
+
]);
|
|
75
|
+
useEffect(() => {
|
|
76
|
+
if (config.mode === ModeEnum.invisible) {
|
|
77
|
+
const handleExecuteEvent = async () => {
|
|
78
|
+
if (loading) return;
|
|
79
|
+
setLoading(true);
|
|
80
|
+
setShowRetry(false);
|
|
81
|
+
try {
|
|
82
|
+
const challenge = await manager.current.start();
|
|
83
|
+
if (challenge) {
|
|
84
|
+
setChallengeData(challenge);
|
|
85
|
+
setPuzzlePhase("dragging");
|
|
86
|
+
}
|
|
87
|
+
} catch (error) {
|
|
88
|
+
callbacks.onError?.(error instanceof Error ? error : new Error(String(error)));
|
|
89
|
+
} finally {
|
|
90
|
+
setLoading(false);
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
document.addEventListener(PROCAPTCHA_EXECUTE_EVENT, handleExecuteEvent);
|
|
94
|
+
return () => {
|
|
95
|
+
document.removeEventListener(PROCAPTCHA_EXECUTE_EVENT, handleExecuteEvent);
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
return () => {};
|
|
99
|
+
}, [
|
|
100
|
+
config.mode,
|
|
101
|
+
callbacks.onError,
|
|
102
|
+
loading
|
|
103
|
+
]);
|
|
104
|
+
const handlePuzzleComplete = useCallback(async (finalX, finalY, puzzleEvents) => {
|
|
105
|
+
setPuzzlePhase("submitting");
|
|
106
|
+
let verified = false;
|
|
107
|
+
try {
|
|
108
|
+
verified = await manager.current.submitSolution(finalX, finalY, puzzleEvents);
|
|
109
|
+
} catch (error) {
|
|
110
|
+
callbacks.onError?.(error instanceof Error ? error : new Error(String(error)));
|
|
111
|
+
}
|
|
112
|
+
if (verified) {
|
|
113
|
+
setPuzzlePhase("checkbox");
|
|
114
|
+
setChallengeData(null);
|
|
115
|
+
setShowRetry(false);
|
|
116
|
+
setLoading(false);
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
setShowRetry(true);
|
|
120
|
+
setPuzzlePhase("dragging");
|
|
121
|
+
try {
|
|
122
|
+
const newChallenge = await manager.current.start();
|
|
123
|
+
if (newChallenge) setChallengeData(newChallenge);
|
|
124
|
+
else {
|
|
125
|
+
setPuzzlePhase("checkbox");
|
|
126
|
+
setChallengeData(null);
|
|
127
|
+
setShowRetry(false);
|
|
128
|
+
}
|
|
129
|
+
} catch {
|
|
130
|
+
setPuzzlePhase("checkbox");
|
|
131
|
+
setChallengeData(null);
|
|
132
|
+
setShowRetry(false);
|
|
133
|
+
}
|
|
134
|
+
setLoading(false);
|
|
135
|
+
}, [callbacks.onError]);
|
|
136
|
+
const isInvisible = config.mode === ModeEnum.invisible;
|
|
137
|
+
const showPuzzleOverlay = (puzzlePhase === "dragging" || puzzlePhase === "submitting") && challengeData;
|
|
138
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
139
|
+
frictionlessState?.hp && /* @__PURE__ */ jsx(Honeypot, {
|
|
140
|
+
ref: hpRef,
|
|
141
|
+
encodedQuestion: frictionlessState.hp
|
|
142
|
+
}),
|
|
143
|
+
showPuzzleOverlay && /* @__PURE__ */ jsx(PuzzleCanvas, {
|
|
144
|
+
originX: challengeData.originX,
|
|
145
|
+
originY: challengeData.originY,
|
|
146
|
+
targetX: challengeData.targetX,
|
|
147
|
+
targetY: challengeData.targetY,
|
|
148
|
+
onComplete: handlePuzzleComplete,
|
|
149
|
+
showRetry,
|
|
150
|
+
submitting: puzzlePhase === "submitting"
|
|
151
|
+
}),
|
|
152
|
+
!isInvisible && /* @__PURE__ */ jsx(Checkbox, {
|
|
153
|
+
checked: state.isHuman,
|
|
154
|
+
theme,
|
|
155
|
+
onChange: async (event) => {
|
|
156
|
+
if (loading) return;
|
|
157
|
+
setLoading(true);
|
|
158
|
+
setShowRetry(false);
|
|
159
|
+
let x = 0;
|
|
160
|
+
let y = 0;
|
|
161
|
+
const mouseOrTouchEvent = event.nativeEvent;
|
|
162
|
+
if (!mouseOrTouchEvent.isTrusted) {} else if ("touches" in mouseOrTouchEvent && mouseOrTouchEvent.touches.length > 0 && mouseOrTouchEvent.touches[0]) {
|
|
163
|
+
x = mouseOrTouchEvent.touches[0].clientX;
|
|
164
|
+
y = mouseOrTouchEvent.touches[0].clientY;
|
|
165
|
+
} else if ("clientX" in mouseOrTouchEvent && "clientY" in mouseOrTouchEvent) {
|
|
166
|
+
x = mouseOrTouchEvent.clientX;
|
|
167
|
+
y = mouseOrTouchEvent.clientY;
|
|
168
|
+
}
|
|
169
|
+
lastCoordsRef.current = {
|
|
170
|
+
x,
|
|
171
|
+
y
|
|
172
|
+
};
|
|
173
|
+
const challenge = await manager.current.start(x, y);
|
|
174
|
+
if (challenge) {
|
|
175
|
+
setChallengeData(challenge);
|
|
176
|
+
setPuzzlePhase("dragging");
|
|
177
|
+
}
|
|
178
|
+
setLoading(false);
|
|
179
|
+
},
|
|
180
|
+
labelText: isTranslationReady ? t("WIDGET.I_AM_HUMAN") : "",
|
|
181
|
+
error: state.error?.message,
|
|
182
|
+
"aria-label": "human checkbox",
|
|
183
|
+
loading: loading || puzzlePhase === "submitting"
|
|
184
|
+
})
|
|
185
|
+
] });
|
|
215
186
|
};
|
|
187
|
+
//#endregion
|
|
188
|
+
export { Procaptcha as default };
|