@rebasepro/client 0.10.1-canary.31c773c → 0.10.1-canary.811b3da

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/index.es.js CHANGED
@@ -295,19 +295,39 @@ function createAuth(transport, options) {
295
295
  */
296
296
  function isFatalRefreshError(err) {
297
297
  if (!(err instanceof RebaseApiError)) return false;
298
+ if (err.code === "TOKEN_ALREADY_USED") return false;
298
299
  if (err.code === "INVALID_TOKEN" || err.code === "TOKEN_EXPIRED") return true;
299
300
  return err.status === 401 || err.status === 403;
300
301
  }
302
+ /**
303
+ * Drop this client's session without telling the server.
304
+ *
305
+ * `signOut()` is a user action: it POSTs /logout, which revokes the whole
306
+ * sign-in. That is the wrong hammer for a refresh that failed. Our token
307
+ * may be stale precisely because a sibling tab holds a live one, and
308
+ * logging out on its behalf would turn one tab's bad luck into everybody
309
+ * being signed out — the exact failure this work exists to remove.
310
+ */
311
+ function abandonSessionLocally() {
312
+ currentSession = null;
313
+ clearStoredSession();
314
+ if (refreshTimeout) {
315
+ clearTimeout(refreshTimeout);
316
+ refreshTimeout = null;
317
+ }
318
+ transport.setToken(null);
319
+ emit("SIGNED_OUT", null);
320
+ }
301
321
  async function attemptScheduledRefresh(attempt) {
302
322
  try {
303
323
  await refreshSession();
304
324
  } catch (err) {
305
325
  if (isFatalRefreshError(err)) {
306
- signOut();
326
+ abandonSessionLocally();
307
327
  return;
308
328
  }
309
329
  if (attempt >= MAX_REFRESH_RETRIES) {
310
- signOut();
330
+ abandonSessionLocally();
311
331
  return;
312
332
  }
313
333
  const backoff = Math.min(REFRESH_RETRY_BASE_MS * 2 ** attempt, REFRESH_RETRY_MAX_MS);
@@ -528,9 +548,42 @@ function createAuth(transport, options) {
528
548
  transport.setToken(null);
529
549
  emit("SIGNED_OUT", null);
530
550
  }
551
+ /**
552
+ * Serialise refreshes across TABS, not just within one.
553
+ *
554
+ * The in-flight promise below covers callers inside a single JavaScript
555
+ * context. It does nothing about the far more common case: two tabs of the
556
+ * same app booting together, each firing its own /refresh with the same
557
+ * cookie. The server tolerates that now (superseded tokens stay usable for
558
+ * a grace window), but tolerating a stampede is not the same as avoiding
559
+ * one, and every extra rotation is another chance to end up holding a
560
+ * token whose response never arrived.
561
+ *
562
+ * Web Locks are best-effort on purpose. supabase-js shipped this and then
563
+ * spent a year fielding deadlock reports — a lock held by a crashed or
564
+ * frozen tab must never be able to wedge sign-in — so a lock we cannot
565
+ * take within the timeout is simply not taken, and the refresh proceeds
566
+ * unserialised, exactly as it did before.
567
+ */
568
+ const REFRESH_LOCK_NAME = "rebase-auth-refresh";
569
+ const REFRESH_LOCK_TIMEOUT_MS = 5e3;
570
+ async function withRefreshLock(fn) {
571
+ const locks = globalThis.navigator?.locks;
572
+ if (!locks?.request) return fn();
573
+ const controller = new AbortController();
574
+ const giveUp = setTimeout(() => controller.abort(), REFRESH_LOCK_TIMEOUT_MS);
575
+ try {
576
+ return await locks.request(REFRESH_LOCK_NAME, { signal: controller.signal }, async () => fn());
577
+ } catch (e) {
578
+ if (e?.name !== "AbortError") throw e;
579
+ return fn();
580
+ } finally {
581
+ clearTimeout(giveUp);
582
+ }
583
+ }
531
584
  function refreshSession() {
532
585
  if (inFlightRefresh) return inFlightRefresh;
533
- inFlightRefresh = doRefreshSession().finally(() => {
586
+ inFlightRefresh = withRefreshLock(() => doRefreshSession()).finally(() => {
534
587
  inFlightRefresh = null;
535
588
  });
536
589
  return inFlightRefresh;