@rebasepro/client 0.12.0 → 0.12.1-canary.g181d0fe

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/auth.d.ts CHANGED
@@ -180,6 +180,7 @@ export declare function createAuth(transport: Transport, options?: CreateAuthOpt
180
180
  getAuthConfig: () => Promise<AuthConfig>;
181
181
  getSession: () => RebaseSession | null;
182
182
  onAuthStateChange: (callback: (event: AuthChangeEvent, session: RebaseSession | null) => void) => () => boolean;
183
+ canRestoreSession: () => boolean;
183
184
  isInitialized: () => Promise<void>;
184
185
  };
185
186
  export interface CookieStorageOptions {
package/dist/index.es.js CHANGED
@@ -907,6 +907,7 @@ function createAuth(transport, options) {
907
907
  getAuthConfig,
908
908
  getSession,
909
909
  onAuthStateChange,
910
+ canRestoreSession: () => persistSession || authFlowMode === "cookie",
910
911
  isInitialized: () => isInitialized
911
912
  };
912
913
  }
@@ -1418,7 +1419,8 @@ function createCollectionClient(transport, slug, ws) {
1418
1419
  function createFunctionsClient(transport) {
1419
1420
  return { async invoke(name, payload, options) {
1420
1421
  const method = options?.method ?? "POST";
1421
- const subPath = options?.path ? `/${options.path.replace(/^\//, "")}` : "";
1422
+ const rawPath = options?.path;
1423
+ const subPath = rawPath ? /^[?#]/.test(rawPath) ? rawPath : `/${rawPath.replace(/^\//, "")}` : "";
1422
1424
  const routePath = `/functions/${encodeURIComponent(name)}${subPath}`;
1423
1425
  const init = { method };
1424
1426
  if (payload !== void 0 && method !== "GET") init.body = JSON.stringify(payload);
@@ -1632,7 +1634,7 @@ function extractMessageError(message) {
1632
1634
  * Fire-and-forget (the server sends no response envelope), and exempt from the
1633
1635
  * client-side auth gate — a public channel is usable without an account.
1634
1636
  */
1635
- var CHANNEL_MESSAGE_TYPES = new Set([
1637
+ var CHANNEL_MESSAGE_TYPES = /* @__PURE__ */ new Set([
1636
1638
  "join_channel",
1637
1639
  "leave_channel",
1638
1640
  "broadcast",
@@ -1661,6 +1663,17 @@ var RebaseWebSocketClient = class {
1661
1663
  /** Set by `close()`. Blocks any later operation from silently redialling. */
1662
1664
  closedByCaller = false;
1663
1665
  /**
1666
+ * Set when the backoff budget ran out, cleared by anything that earns a
1667
+ * fresh one.
1668
+ *
1669
+ * Unlike {@link closedByCaller} this is not final — nobody *asked* for the
1670
+ * socket to stay down. Five attempts with exponential backoff is about a
1671
+ * minute, which a laptop lid, a wifi handover or a backend rollout all
1672
+ * exceed routinely; treating that as permanent meant realtime silently
1673
+ * stopped for the rest of the page's life, with a reload the only cure.
1674
+ */
1675
+ gaveUp = false;
1676
+ /**
1664
1677
  * Whether a socket exists at all (open or still opening).
1665
1678
  *
1666
1679
  * Lets callers distinguish "authenticate the live socket" from "there is
@@ -1733,10 +1746,30 @@ var RebaseWebSocketClient = class {
1733
1746
  }
1734
1747
  return;
1735
1748
  }
1749
+ this.installOnlineListener();
1736
1750
  if (this.ws || this.reconnectTimeout) return;
1751
+ if (this.gaveUp) {
1752
+ this.gaveUp = false;
1753
+ this.reconnectAttempts = 0;
1754
+ }
1737
1755
  this.initWebSocket();
1738
1756
  }
1739
1757
  /**
1758
+ * The browser says the network is back — the usual reason the budget ran
1759
+ * out in the first place. Registered lazily so a Node client, or a page
1760
+ * that never subscribes, adds no listener.
1761
+ */
1762
+ installOnlineListener() {
1763
+ if (this.onlineListener || typeof window === "undefined" || typeof window.addEventListener !== "function") return;
1764
+ this.onlineListener = () => {
1765
+ if (this.closedByCaller || !this.gaveUp) return;
1766
+ console.debug("Network is back — retrying the realtime connection");
1767
+ this.ensureConnected();
1768
+ };
1769
+ window.addEventListener("online", this.onlineListener);
1770
+ }
1771
+ onlineListener = null;
1772
+ /**
1740
1773
  * Authenticate the WebSocket connection
1741
1774
  */
1742
1775
  async authenticate(token) {
@@ -1794,6 +1827,10 @@ var RebaseWebSocketClient = class {
1794
1827
  */
1795
1828
  disconnect(permanent = false) {
1796
1829
  if (permanent) this.closedByCaller = true;
1830
+ if (permanent && this.onlineListener && typeof window !== "undefined") {
1831
+ window.removeEventListener("online", this.onlineListener);
1832
+ this.onlineListener = null;
1833
+ }
1797
1834
  this.isAuthenticated = false;
1798
1835
  this.authPromise = null;
1799
1836
  if (this.reconnectTimeout) {
@@ -1818,7 +1855,8 @@ var RebaseWebSocketClient = class {
1818
1855
  this.ws = null;
1819
1856
  }
1820
1857
  try {
1821
- this.ws = new this.WebSocketConstructor(this.websocketUrl);
1858
+ const socket = new this.WebSocketConstructor(this.websocketUrl);
1859
+ this.ws = socket;
1822
1860
  this.ws.onopen = async () => {
1823
1861
  console.debug("Connected to PostgreSQL backend");
1824
1862
  const wasReconnect = this.reconnectAttempts > 0;
@@ -1848,6 +1886,7 @@ var RebaseWebSocketClient = class {
1848
1886
  };
1849
1887
  this.ws.onclose = () => {
1850
1888
  console.debug("Disconnected from PostgreSQL backend");
1889
+ if (this.ws === socket) this.ws = null;
1851
1890
  this.isConnected = false;
1852
1891
  this.isAuthenticated = false;
1853
1892
  this.authPromise = null;
@@ -1883,6 +1922,7 @@ var RebaseWebSocketClient = class {
1883
1922
  attemptReconnect() {
1884
1923
  if (this.reconnectAttempts >= this.maxReconnectAttempts) {
1885
1924
  console.error("Max reconnection attempts reached");
1925
+ this.gaveUp = true;
1886
1926
  this.failAllPendingSubscriptions(new RebaseApiError$1("Connection lost", { code: "CONNECTION_LOST" }));
1887
1927
  return;
1888
1928
  }
@@ -2425,7 +2465,7 @@ var RebaseWebSocketClient = class {
2425
2465
  if (this.deepEqual(normCached, normIncoming)) return cachedRow;
2426
2466
  else {
2427
2467
  const mismatches = {};
2428
- const allKeys = new Set([...Object.keys(normCached), ...Object.keys(normIncoming)]);
2468
+ const allKeys = /* @__PURE__ */ new Set([...Object.keys(normCached), ...Object.keys(normIncoming)]);
2429
2469
  for (const key of allKeys) if (!this.deepEqual(normCached[key], normIncoming[key])) mismatches[key] = {
2430
2470
  cached: normCached[key],
2431
2471
  incoming: normIncoming[key]
@@ -3228,7 +3268,7 @@ function isNetworkError(error) {
3228
3268
  * bug the same payload will hit again than a blip, and retrying it forever
3229
3269
  * jams every write behind it.
3230
3270
  */
3231
- var RETRYABLE_STATUSES = new Set([
3271
+ var RETRYABLE_STATUSES = /* @__PURE__ */ new Set([
3232
3272
  408,
3233
3273
  425,
3234
3274
  429,
@@ -5295,6 +5335,7 @@ channel: (name, options) => {
5295
5335
  setOnUnauthorized: transport.setOnUnauthorized,
5296
5336
  resolveToken: transport.resolveToken,
5297
5337
  baseUrl: transport.baseUrl,
5338
+ apiPath: transport.apiPath,
5298
5339
  collection,
5299
5340
  call: async (endpoint, payload) => {
5300
5341
  const prefix = endpoint.startsWith("/") ? "" : "/";