@zeyolabs/kit 2026.11.9 → 2026.11.10

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zeyolabs/kit",
3
- "version": "2026.11.9",
3
+ "version": "2026.11.10",
4
4
  "description": "Shared SvelteKit-on-Cloudflare server toolkit: remote-function wrappers, passwordless auth (magic link + passkeys), email over a send_email binding or SMTP, Cloudflare context, monitoring, storage clients, utils and the ESLint config.",
5
5
  "type": "module",
6
6
  "repository": {
@@ -1,7 +1,10 @@
1
+ import { FormResult } from '../utils/form';
2
+
3
+ const SITEVERIFY_URL = 'https://challenges.cloudflare.com/turnstile/v0/siteverify';
4
+
1
5
  /**
2
6
  * Server-side verification of a Cloudflare Turnstile token against the siteverify API. Pure
3
- * `fetch`, no framework or binding dependency. Each app owns how it obtains the secret key and how
4
- * a rejection surfaces (see `apps/web/src/lib/server/turnstile.ts`).
7
+ * `fetch`, no framework or binding dependency. Prefer `Turnstile.verify` / `Turnstile.gate`.
5
8
  */
6
9
  export async function verifyTurnstile(
7
10
  token: string,
@@ -10,10 +13,58 @@ export async function verifyTurnstile(
10
13
  ): Promise<boolean> {
11
14
  const params = new URLSearchParams({ response: token, secret: secretKey });
12
15
  if (ip) params.set('remoteip', ip);
13
- const response = await fetch('https://challenges.cloudflare.com/turnstile/v0/siteverify', {
14
- body: params,
15
- method: 'POST',
16
- });
16
+ const response = await fetch(SITEVERIFY_URL, { body: params, method: 'POST' });
17
17
  const result = (await response.json()) as { success?: boolean };
18
18
  return result.success === true;
19
19
  }
20
+
21
+ /** Where a gate gets the request's secret key and client address. The app supplies both. */
22
+ export type TurnstileGateOptions = {
23
+ /** Client address per call (SvelteKit's `getRequestEvent().getClientAddress()`); may be null. */
24
+ ip?: () => null | string | undefined;
25
+ /** The error shown when the check fails. */
26
+ message?: string;
27
+ /** The secret key: a string, or a function called per check (a Secrets Store binding, a loopback test key). */
28
+ secret: (() => Promise<string> | string) | string;
29
+ };
30
+
31
+ /**
32
+ * Turnstile helpers: a library (an object of static functions, no state, no bindings), like
33
+ * `Schema` and `FormResult`.
34
+ */
35
+ export const Turnstile = {
36
+ /**
37
+ * Gates a form handler behind Turnstile. The validated input carries `turnstileToken`; the gate
38
+ * verifies it and strips it before calling the handler. A missing or rejected token returns
39
+ * `FormResult.err(message)` and the handler never runs; a throwing `secret` or `fetch` also
40
+ * stops the handler (fails closed).
41
+ */
42
+ gate({
43
+ ip = () => null,
44
+ message = 'Bot check failed. Please try again.',
45
+ secret,
46
+ }: TurnstileGateOptions) {
47
+ return function gated<TData extends { turnstileToken: string }, TResult>(
48
+ handler: (data: Omit<TData, 'turnstileToken'>) => Promise<FormResult<TResult | void>>
49
+ ): (data: TData) => Promise<FormResult<TResult | void>> {
50
+ return async ({ turnstileToken, ...data }: TData) => {
51
+ if (!(await Turnstile.verify(turnstileToken, ip(), secret))) return FormResult.err(message);
52
+ return handler(data as Omit<TData, 'turnstileToken'>);
53
+ };
54
+ };
55
+ },
56
+
57
+ /**
58
+ * True only when siteverify accepts the token. An empty token is false without a request, and
59
+ * `secret` is only resolved when there is a token to check.
60
+ */
61
+ async verify(
62
+ token: string,
63
+ ip: null | string | undefined,
64
+ secret: (() => Promise<string> | string) | string
65
+ ): Promise<boolean> {
66
+ if (!token) return false;
67
+ const key = typeof secret === 'function' ? await secret() : secret;
68
+ return verifyTurnstile(token, ip, key);
69
+ },
70
+ };