@better-auth/infra 0.2.6 → 0.2.8

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/dist/native.d.mts CHANGED
@@ -1,9 +1,14 @@
1
- import { a as DashGetAuditLogsInput, i as DashGetAllAuditLogsInput, n as DashAuditLogsResponse, o as dashClient, r as DashClientOptions, t as DashAuditLog } from "./dash-client-CVCHbtoB.mjs";
1
+ import { a as DashGetAuditLogsInput, i as DashGetAllAuditLogsInput, n as DashAuditLogsResponse, o as dashClient, r as DashClientOptions, t as DashAuditLog } from "./dash-client-B6U89e1S.mjs";
2
2
  import { BetterAuthClientPlugin } from "better-auth";
3
3
 
4
4
  //#region src/sentinel/native/client.d.ts
5
5
  interface SentinelNativeClientOptions {
6
6
  identifyUrl?: string;
7
+ /**
8
+ * Timeout for KV identify and related HTTP requests (milliseconds).
9
+ * @default 1000
10
+ */
11
+ kvTimeout?: number;
7
12
  autoSolveChallenge?: boolean;
8
13
  onChallengeReceived?: (reason: string) => void;
9
14
  onChallengeSolved?: (solveTimeMs: number) => void;
package/dist/native.mjs CHANGED
@@ -1,4 +1,6 @@
1
- import { a as identify, c as dashClient, n as encodePoWSolution, o as bytesToHex, r as solvePoWChallenge, t as decodePoWChallenge } from "./pow-CT9ehp8e.mjs";
1
+ import "./constants-CvriWQVc.mjs";
2
+ import { n as createKV } from "./fetch-DiAhoiKA.mjs";
3
+ import { i as solvePoWChallenge, l as dashClient, n as decodePoWChallenge, o as identify, r as encodePoWSolution, s as bytesToHex, t as createPowRetryTimeout } from "./pow-retry-BTL4g3RP.mjs";
2
4
  import { env } from "@better-auth/core/env";
3
5
  import { Dimensions, InteractionManager, PixelRatio, Platform } from "react-native";
4
6
  //#region src/sentinel/native/components.ts
@@ -141,7 +143,7 @@ function createNativeFingerprintRuntime(deps) {
141
143
  incognito: false
142
144
  };
143
145
  try {
144
- await identify(deps.identifyUrl, payload);
146
+ await identify(deps.$kv, payload);
145
147
  } catch (error) {
146
148
  console.warn("[Sentinel native] Identify request failed:", error);
147
149
  } finally {
@@ -149,9 +151,10 @@ function createNativeFingerprintRuntime(deps) {
149
151
  identifyCompleteResolve = null;
150
152
  }
151
153
  }
152
- async function waitForIdentify(timeoutMs = 500) {
154
+ async function waitForIdentify(timeoutMs) {
153
155
  if (!identifyComplete) return;
154
- await Promise.race([identifyComplete, new Promise((resolve) => setTimeout(resolve, timeoutMs))]);
156
+ const ms = timeoutMs ?? 1e3;
157
+ await Promise.race([identifyComplete, new Promise((resolve) => setTimeout(resolve, ms))]);
155
158
  }
156
159
  return {
157
160
  getFingerprint,
@@ -209,7 +212,10 @@ function scheduleIdentify(send) {
209
212
  const sentinelNativeClient = (options) => {
210
213
  const autoSolve = options?.autoSolveChallenge !== false;
211
214
  const runtime = createNativeFingerprintRuntime({
212
- identifyUrl: options?.identifyUrl ?? env.BETTER_AUTH_KV_URL ?? DEFAULT_IDENTIFY_URL,
215
+ $kv: createKV({
216
+ kvUrl: options?.identifyUrl ?? env.BETTER_AUTH_KV_URL ?? DEFAULT_IDENTIFY_URL,
217
+ kvTimeout: options?.kvTimeout
218
+ }),
213
219
  getOrCreateVisitorId: () => getOrCreateVisitorId(options?.storage)
214
220
  });
215
221
  scheduleIdentify(() => {
@@ -221,7 +227,7 @@ const sentinelNativeClient = (options) => {
221
227
  id: "sentinel-fingerprint",
222
228
  name: "sentinel-fingerprint",
223
229
  hooks: { async onRequest(context) {
224
- await runtime.waitForIdentify(500);
230
+ await runtime.waitForIdentify(options?.kvTimeout);
225
231
  const fingerprint = await runtime.getFingerprint();
226
232
  if (!fingerprint) return context;
227
233
  const headers = context.headers || new Headers();
@@ -262,15 +268,22 @@ const sentinelNativeClient = (options) => {
262
268
  retryHeaders.set("Content-Type", "application/json");
263
269
  let body;
264
270
  if (context.request?.body) body = context.request._originalBody;
265
- const retryResponse = await fetch(originalUrl, {
266
- method: context.request?.method || "POST",
267
- headers: retryHeaders,
268
- body
269
- });
270
- return {
271
- ...context,
272
- response: retryResponse
273
- };
271
+ const req = context.request;
272
+ const powRetry = createPowRetryTimeout(req.timeout);
273
+ try {
274
+ const retryResponse = await fetch(originalUrl, {
275
+ method: req.method || "POST",
276
+ headers: retryHeaders,
277
+ body,
278
+ ...powRetry.signal ? { signal: powRetry.signal } : {}
279
+ });
280
+ return {
281
+ ...context,
282
+ response: retryResponse
283
+ };
284
+ } finally {
285
+ powRetry.cleanup?.();
286
+ }
274
287
  } catch (err) {
275
288
  console.error("[Sentinel native] Failed to solve PoW challenge:", err);
276
289
  options?.onChallengeFailed?.(err instanceof Error ? err : new Error(String(err)));
@@ -1,4 +1,3 @@
1
- import "./constants-DdWGfvz1.mjs";
2
1
  //#region src/dash-client.ts
3
2
  function resolveDashUserId(input, options) {
4
3
  return input.userId || options?.resolveUserId?.({
@@ -61,31 +60,15 @@ async function hash(message) {
61
60
  return bytesToHex(new Uint8Array(hashBuffer));
62
61
  }
63
62
  //#endregion
64
- //#region src/abort-signal.ts
65
- /**
66
- * Returns an AbortSignal that aborts after `ms`.
67
- * Uses `AbortSignal.timeout` when present; otherwise `AbortController` + `setTimeout`
68
- * for React Native / older runtimes where `.timeout` is missing.
69
- */
70
- function timeout(ms) {
71
- if (typeof AbortSignal.timeout === "function") return AbortSignal.timeout(ms);
72
- const controller = new AbortController();
73
- setTimeout(() => controller.abort(), ms);
74
- return controller.signal;
75
- }
76
- //#endregion
77
63
  //#region src/identification-client.ts
78
64
  function generateRequestId() {
79
65
  const hex = bytesToHex(randomBytes(16));
80
66
  return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20, 32)}`;
81
67
  }
82
- async function identify(baseURL, payload, signal) {
83
- const base = baseURL.replace(/\/$/, "");
84
- await fetch(`${base}/identify`, {
68
+ async function identify($kv, payload) {
69
+ await $kv("/identify", {
85
70
  method: "POST",
86
- headers: { "Content-Type": "application/json" },
87
- body: JSON.stringify(payload),
88
- signal: signal ?? timeout(5e3)
71
+ body: payload
89
72
  });
90
73
  }
91
74
  //#endregion
@@ -146,4 +129,23 @@ function encodePoWSolution(solution) {
146
129
  return encodeUtf8ToBase64(JSON.stringify(solution));
147
130
  }
148
131
  //#endregion
149
- export { identify as a, dashClient as c, generateRequestId as i, encodePoWSolution as n, bytesToHex as o, solvePoWChallenge as r, hash as s, decodePoWChallenge as t };
132
+ //#region src/sentinel/pow-retry.ts
133
+ /**
134
+ * better-fetch clears its internal timeout once the first response is received.
135
+ * For the PoW retry we apply the same `timeout` value again so the second leg
136
+ * cannot exceed the client's configured limit.
137
+ */
138
+ function createPowRetryTimeout(timeoutMs) {
139
+ if (typeof timeoutMs !== "number" || timeoutMs <= 0) return {
140
+ signal: void 0,
141
+ cleanup: void 0
142
+ };
143
+ const controller = new AbortController();
144
+ const id = setTimeout(() => controller.abort(), timeoutMs);
145
+ return {
146
+ signal: controller.signal,
147
+ cleanup: () => clearTimeout(id)
148
+ };
149
+ }
150
+ //#endregion
151
+ export { generateRequestId as a, hash as c, solvePoWChallenge as i, dashClient as l, decodePoWChallenge as n, identify as o, encodePoWSolution as r, bytesToHex as s, createPowRetryTimeout as t };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@better-auth/infra",
3
- "version": "0.2.6",
3
+ "version": "0.2.8",
4
4
  "description": "Dashboard and analytics plugin for Better Auth",
5
5
  "type": "module",
6
6
  "main": "dist/index.mjs",
@@ -78,7 +78,7 @@
78
78
  "@better-fetch/fetch": "^1.1.21",
79
79
  "better-call": "^1.3.3",
80
80
  "jose": "^6.1.0",
81
- "libphonenumber-js": "^1.12.42"
81
+ "libphonenumber-js": "^1.13.1"
82
82
  },
83
83
  "peerDependencies": {
84
84
  "better-auth": ">=1.4.0",