lazypock 0.17.1 → 0.19.0

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.js CHANGED
@@ -58,7 +58,7 @@ var HttpClient = class {
58
58
  const collection = this.authStore.collectionName;
59
59
  if (!collection) return null;
60
60
  try {
61
- const url = this.baseUrl + "/" + encodeURIComponent(collection) + "/auth-refresh";
61
+ const url = this.baseUrl + "/collections/" + encodeURIComponent(collection) + "/auth-refresh";
62
62
  const headers = {
63
63
  "Content-Type": "application/json"
64
64
  };
@@ -841,6 +841,19 @@ var CollectionService = class _CollectionService {
841
841
  encodeId(id) {
842
842
  return encodeURIComponent(id);
843
843
  }
844
+ /**
845
+ * The records endpoint for this collection.
846
+ *
847
+ * Centralised because the server's shape is `/api/collections/<name>/records`,
848
+ * and a path repeated eighteen times is a path that drifts.
849
+ */
850
+ recordsPath() {
851
+ return "/collections/" + this.encodeId(this.collectionName) + "/records";
852
+ }
853
+ /** The collection's own endpoint, which is where its auth routes live. */
854
+ collectionPath() {
855
+ return "/collections/" + this.encodeId(this.collectionName);
856
+ }
844
857
  // ── Field projection (`.select`) ──
845
858
  /**
846
859
  * Project list/read responses to the given fields (PocketBase `fields`
@@ -1248,7 +1261,7 @@ var CollectionService = class _CollectionService {
1248
1261
  }).map(([k, v]) => [k, String(v)])
1249
1262
  )
1250
1263
  ).toString();
1251
- const result = await this.http.get("/" + this.encodeId(this.collectionName) + "?" + qs, {
1264
+ const result = await this.http.get(this.recordsPath() + "?" + qs, {
1252
1265
  requestKey,
1253
1266
  autoCancel,
1254
1267
  cancelKey,
@@ -1333,7 +1346,7 @@ var CollectionService = class _CollectionService {
1333
1346
  }
1334
1347
  const qsStr = qs.toString();
1335
1348
  const record = await this.http.get(
1336
- "/" + this.encodeId(this.collectionName) + "/" + this.encodeId(id) + (qsStr ? "?" + qsStr : ""),
1349
+ this.recordsPath() + "/" + this.encodeId(id) + (qsStr ? "?" + qsStr : ""),
1337
1350
  options
1338
1351
  );
1339
1352
  if (record && expandInfo.narrow.size > 0) {
@@ -1349,7 +1362,7 @@ var CollectionService = class _CollectionService {
1349
1362
  */
1350
1363
  create(data, options) {
1351
1364
  return this.http.post(
1352
- "/" + this.encodeId(this.collectionName),
1365
+ this.recordsPath(),
1353
1366
  data,
1354
1367
  options
1355
1368
  );
@@ -1363,7 +1376,7 @@ var CollectionService = class _CollectionService {
1363
1376
  */
1364
1377
  update(id, data, options) {
1365
1378
  return this.http.patch(
1366
- "/" + this.encodeId(this.collectionName) + "/" + this.encodeId(id),
1379
+ this.recordsPath() + "/" + this.encodeId(id),
1367
1380
  data,
1368
1381
  options
1369
1382
  );
@@ -1375,7 +1388,7 @@ var CollectionService = class _CollectionService {
1375
1388
  */
1376
1389
  delete(id, options) {
1377
1390
  return this.http.delete(
1378
- "/" + this.encodeId(this.collectionName) + "/" + this.encodeId(id),
1391
+ this.recordsPath() + "/" + this.encodeId(id),
1379
1392
  options
1380
1393
  );
1381
1394
  }
@@ -1483,7 +1496,7 @@ var CollectionService = class _CollectionService {
1483
1496
  */
1484
1497
  async authWithPassword(identity, password, options) {
1485
1498
  const data = await this.http.post(
1486
- "/" + this.encodeId(this.collectionName) + "/auth-with-password",
1499
+ this.collectionPath() + "/auth-with-password",
1487
1500
  { identity, password },
1488
1501
  options
1489
1502
  );
@@ -1499,7 +1512,7 @@ var CollectionService = class _CollectionService {
1499
1512
  */
1500
1513
  async authRefresh(options) {
1501
1514
  const data = await this.http.post(
1502
- "/" + this.encodeId(this.collectionName) + "/auth-refresh",
1515
+ this.collectionPath() + "/auth-refresh",
1503
1516
  void 0,
1504
1517
  options
1505
1518
  );
@@ -1516,7 +1529,7 @@ var CollectionService = class _CollectionService {
1516
1529
  */
1517
1530
  async listAuthMethods(options) {
1518
1531
  return this.http.get(
1519
- "/" + this.encodeId(this.collectionName) + "/auth-methods",
1532
+ this.collectionPath() + "/auth-methods",
1520
1533
  options
1521
1534
  );
1522
1535
  }
@@ -1535,7 +1548,7 @@ var CollectionService = class _CollectionService {
1535
1548
  */
1536
1549
  async requestPasswordReset(email, options) {
1537
1550
  await this.http.post(
1538
- "/" + this.encodeId(this.collectionName) + "/request-password-reset",
1551
+ this.collectionPath() + "/request-password-reset",
1539
1552
  { email },
1540
1553
  options
1541
1554
  );
@@ -1550,7 +1563,7 @@ var CollectionService = class _CollectionService {
1550
1563
  */
1551
1564
  async confirmPasswordReset(passwordResetToken, password, passwordConfirm, options) {
1552
1565
  await this.http.post(
1553
- "/" + this.encodeId(this.collectionName) + "/confirm-password-reset",
1566
+ this.collectionPath() + "/confirm-password-reset",
1554
1567
  { token: passwordResetToken, password, passwordConfirm },
1555
1568
  options
1556
1569
  );
@@ -1563,7 +1576,7 @@ var CollectionService = class _CollectionService {
1563
1576
  */
1564
1577
  async requestVerification(email, options) {
1565
1578
  await this.http.post(
1566
- "/" + this.encodeId(this.collectionName) + "/request-verification",
1579
+ this.collectionPath() + "/request-verification",
1567
1580
  { email },
1568
1581
  options
1569
1582
  );
@@ -1575,7 +1588,7 @@ var CollectionService = class _CollectionService {
1575
1588
  */
1576
1589
  async confirmVerification(verificationToken, options) {
1577
1590
  const data = await this.http.post(
1578
- "/" + this.encodeId(this.collectionName) + "/confirm-verification",
1591
+ this.collectionPath() + "/confirm-verification",
1579
1592
  { token: verificationToken },
1580
1593
  options
1581
1594
  );
@@ -1587,7 +1600,7 @@ var CollectionService = class _CollectionService {
1587
1600
  */
1588
1601
  async requestEmailChange(newEmail, options) {
1589
1602
  await this.http.post(
1590
- "/" + this.encodeId(this.collectionName) + "/request-email-change",
1603
+ this.collectionPath() + "/request-email-change",
1591
1604
  { newEmail },
1592
1605
  options
1593
1606
  );
@@ -1600,7 +1613,7 @@ var CollectionService = class _CollectionService {
1600
1613
  */
1601
1614
  async confirmEmailChange(emailChangeToken, password, options) {
1602
1615
  const data = await this.http.post(
1603
- "/" + this.encodeId(this.collectionName) + "/confirm-email-change",
1616
+ this.collectionPath() + "/confirm-email-change",
1604
1617
  { token: emailChangeToken, password },
1605
1618
  options
1606
1619
  );
@@ -1668,7 +1681,7 @@ var CollectionService = class _CollectionService {
1668
1681
  async authWithOAuth2Code(options) {
1669
1682
  const { provider, code, codeVerifier, redirectUrl, createData } = options;
1670
1683
  const data = await this.http.post(
1671
- "/" + this.encodeId(this.collectionName) + "/auth-with-oauth2",
1684
+ this.collectionPath() + "/auth-with-oauth2",
1672
1685
  {
1673
1686
  provider,
1674
1687
  code,
@@ -1695,259 +1708,245 @@ var CollectionService = class _CollectionService {
1695
1708
  };
1696
1709
 
1697
1710
  // src/realtime.ts
1698
- function wsUrlFromBaseUrl(baseUrl) {
1699
- try {
1700
- const url = new URL(baseUrl);
1701
- const protocol = url.protocol === "https:" ? "wss:" : "ws:";
1702
- return `${protocol}//${url.host}/socket/websocket`;
1703
- } catch {
1704
- return `${baseUrl.replace(/^http/, "ws").replace(/\/api$/, "")}/socket/websocket`;
1705
- }
1711
+ function sseUrlFromBaseUrl(baseUrl) {
1712
+ return `${baseUrl.replace(/\/+$/, "")}/realtime`;
1713
+ }
1714
+ function serverTopic(topic) {
1715
+ if (topic === "collections") return "collections";
1716
+ if (!topic.startsWith("collection:")) return null;
1717
+ const rest = topic.slice("collection:".length);
1718
+ const [name, recordId] = rest.split(":");
1719
+ if (!name) return null;
1720
+ return recordId ? `${name}/${recordId}` : name;
1706
1721
  }
1707
1722
  var RealtimeService = class {
1708
1723
  constructor() {
1709
- this.ws = null;
1710
- this.refCounter = 0;
1724
+ this.controller = null;
1725
+ this.open = false;
1726
+ this.closed = false;
1711
1727
  this.subscriptions = /* @__PURE__ */ new Map();
1712
1728
  this.reconnectTimer = null;
1729
+ this.resubscribeTimer = null;
1713
1730
  this.reconnectAttempt = 0;
1714
1731
  this.maxReconnectDelay = 5e3;
1715
1732
  this.url = "";
1716
1733
  this.tokenProvider = null;
1717
- // ── Heartbeat ──
1718
- this.heartbeatInterval = null;
1719
1734
  }
1720
- /** Whether the WebSocket is currently open. */
1735
+ /** Whether the stream is currently open. */
1721
1736
  get isOpen() {
1722
- return this.ws?.readyState === WebSocket.OPEN;
1737
+ return this.open;
1723
1738
  }
1724
- /**
1725
- * The most recently used WebSocket URL (set on {@link connect}).
1726
- * Useful for SDK convenience methods that auto-connect before subscribing.
1727
- */
1739
+ /** The most recently used SSE URL (set on {@link connect}/{@link setUrl}). */
1728
1740
  get lastUrl() {
1729
1741
  return this.url;
1730
1742
  }
1731
- /** The auth token configured for this connection (set on connect). */
1743
+ /** The auth token configured for this connection. */
1732
1744
  get lastToken() {
1733
1745
  return this.token;
1734
1746
  }
1735
- /**
1736
- * Set the per-client connection id (browser tab / device). The SDK
1737
- * appends it as the `connectionId` socket query param at connect time
1738
- * (unless the URL already carries one), matching the `X-Connection-Id`
1739
- * header the HTTP client sends — together they let the server exclude
1740
- * the originating connection from its own realtime broadcasts.
1741
- */
1747
+ /** Set the per-client connection id (browser tab / device). */
1742
1748
  setConnectionId(id) {
1743
1749
  this.connectionId = id;
1744
1750
  }
1745
- /**
1746
- * Set the socket URL. Useful before subscribing so the SDK can
1747
- * auto-connect on the first {@link subscribe}.
1748
- */
1751
+ /** Set the stream URL. Useful before subscribing so the SDK auto-connects. */
1749
1752
  setUrl(url) {
1750
1753
  this.url = url;
1751
1754
  }
1752
- /**
1753
- * Register a token provider consulted at every (re)connect.
1754
- * When set, it takes precedence over the token passed to {@link connect}.
1755
- */
1755
+ /** Register a token provider consulted at every (re)connect. */
1756
1756
  setTokenProvider(provider) {
1757
1757
  this.tokenProvider = provider;
1758
1758
  }
1759
1759
  /**
1760
- * Reconnect the socket immediately with the current token.
1761
- * Called by the SDK when auth changes (login/logout/token refresh) so
1762
- * private-channel joins are authorized with the new credentials. No-op
1763
- * when the socket has never been opened and nothing is subscribed.
1760
+ * Reconnect immediately with the current token, so auth changes
1761
+ * (login/logout/refresh) re-authorize private subscriptions.
1764
1762
  */
1765
1763
  refresh() {
1766
- if (typeof WebSocket === "undefined") return;
1767
- if (!this.ws && this.subscriptions.size === 0) return;
1768
- this.clearReconnectTimer();
1769
- const ws = this.ws;
1770
- this.ws = null;
1771
- if (ws) {
1772
- ws.onclose = null;
1773
- ws.close();
1774
- }
1764
+ if (this.closed) return;
1765
+ if (!this.open && this.subscriptions.size === 0) return;
1775
1766
  this.reconnectAttempt = 0;
1776
1767
  this.doConnect();
1777
1768
  }
1778
- /*
1779
- * Ensure the socket is connected, then subscribe.
1780
- * Used by collection-level convenience wrappers so a connection is opened
1781
- * automatically on the first subscribe (matching PocketBase behaviour —
1782
- * works for anonymous/public collections too).
1783
- */
1769
+ /** Ensure a connection is open, then subscribe. */
1784
1770
  ensureConnected() {
1785
- if (this.isOpen || !this.url) return;
1786
- if (typeof WebSocket === "undefined") return;
1771
+ if (this.open || this.controller || !this.url) return;
1772
+ if (this.serverTopics().length === 0) return;
1787
1773
  this.doConnect();
1788
1774
  }
1789
1775
  connect(opts) {
1790
1776
  this.url = opts.url;
1791
1777
  this.token = opts.token;
1778
+ this.closed = false;
1792
1779
  this.reconnectAttempt = 0;
1793
1780
  this.doConnect();
1794
1781
  }
1795
1782
  disconnect() {
1783
+ this.closed = true;
1796
1784
  this.clearReconnectTimer();
1797
- this.ws?.close();
1798
- this.ws = null;
1785
+ this.closeStream();
1799
1786
  }
1800
1787
  /**
1801
- * Subscribe to a topic (e.g. "collection:posts" or "custom:chat-room").
1802
- * The backend Channel authorizes via listRule on join.
1803
- *
1804
- * @param joinPayload Optional payload forwarded with the channel join
1805
- * (available to the server's join callback / hooks, e.g. `expand`).
1788
+ * Subscribe to a topic (`collection:posts`, `collection:posts:ID`, or the
1789
+ * registry topic `collections`).
1806
1790
  */
1807
1791
  subscribe(topic, callback, joinPayload) {
1808
1792
  const subs = this.subscriptions.get(topic) || [];
1809
1793
  subs.push({ topic, callback, joinPayload });
1810
1794
  this.subscriptions.set(topic, subs);
1811
- if (this.ws?.readyState === WebSocket.OPEN) {
1812
- this.joinTopic(topic, joinPayload);
1813
- }
1795
+ this.resubscribe();
1814
1796
  }
1815
- /**
1816
- * Remove all subscriptions for topics under `prefix` (the topic itself or
1817
- * any topic starting with `prefix + ":"`). Used by collection-level
1818
- * `unsubscribe()` to drop every subscription of a collection.
1819
- */
1797
+ /** Remove every subscription under `prefix` (the topic or `prefix:...`). */
1820
1798
  unsubscribeByPrefix(prefix) {
1799
+ let changed = false;
1821
1800
  for (const topic of [...this.subscriptions.keys()]) {
1822
1801
  if (topic === prefix || topic.startsWith(prefix + ":")) {
1823
1802
  this.subscriptions.delete(topic);
1803
+ changed = true;
1824
1804
  }
1825
1805
  }
1806
+ if (changed) this.resubscribe();
1826
1807
  }
1827
- /**
1828
- * Unsubscribe a specific callback from a topic.
1829
- */
1808
+ /** Remove a specific callback from a topic, or the whole topic. */
1830
1809
  unsubscribe(topic, callback) {
1831
1810
  if (!callback) {
1832
- this.subscriptions.delete(topic);
1811
+ if (this.subscriptions.delete(topic)) this.resubscribe();
1833
1812
  return;
1834
1813
  }
1835
1814
  const subs = this.subscriptions.get(topic)?.filter((s) => s.callback !== callback);
1836
1815
  if (subs && subs.length > 0) {
1837
1816
  this.subscriptions.set(topic, subs);
1838
- } else {
1839
- this.subscriptions.delete(topic);
1817
+ return;
1840
1818
  }
1819
+ this.subscriptions.delete(topic);
1820
+ this.resubscribe();
1841
1821
  }
1842
- resubscribeAll() {
1843
- const seen = /* @__PURE__ */ new Set();
1844
- for (const entries of this.subscriptions.values()) {
1845
- for (const entry of entries) {
1846
- if (seen.has(entry.topic)) continue;
1847
- seen.add(entry.topic);
1848
- if (this.ws?.readyState === WebSocket.OPEN) {
1849
- this.joinTopic(entry.topic, entry.joinPayload);
1850
- }
1851
- }
1822
+ // ── Internals ────────────────────────────────────
1823
+ /** The distinct server topics the current subscriptions need. */
1824
+ serverTopics() {
1825
+ const topics = /* @__PURE__ */ new Set();
1826
+ for (const topic of this.subscriptions.keys()) {
1827
+ const server = serverTopic(topic);
1828
+ if (server) topics.add(server);
1852
1829
  }
1830
+ return [...topics];
1831
+ }
1832
+ /**
1833
+ * The subscription set is part of the connection URL, so a change needs a new
1834
+ * connection. Coalesced, because mounting a page subscribes to several
1835
+ * collections in one tick.
1836
+ */
1837
+ resubscribe() {
1838
+ if (this.resubscribeTimer) clearTimeout(this.resubscribeTimer);
1839
+ this.resubscribeTimer = setTimeout(() => {
1840
+ this.resubscribeTimer = null;
1841
+ if (this.controller) {
1842
+ this.doConnect();
1843
+ } else {
1844
+ this.ensureConnected();
1845
+ }
1846
+ }, 0);
1853
1847
  }
1854
1848
  doConnect() {
1855
- if (typeof WebSocket === "undefined") {
1856
- console.warn(
1857
- "[lazypock] WebSocket not available \u2014 realtime subscriptions disabled"
1858
- );
1859
- return;
1860
- }
1861
- let url = this.url;
1849
+ if (this.closed) return;
1850
+ const topics = this.serverTopics();
1851
+ if (topics.length === 0 || !this.url) return;
1852
+ this.closeStream();
1853
+ const params = new URLSearchParams();
1854
+ params.set("subscribe", topics.join(","));
1855
+ const url = this.url + (this.url.includes("?") ? "&" : "?") + params.toString();
1862
1856
  const token = this.tokenProvider ? this.tokenProvider() : this.token;
1863
- if (token) {
1864
- url += (url.includes("?") ? "&" : "?") + "token=" + encodeURIComponent(token);
1865
- }
1866
- if (this.connectionId && !url.includes("connectionId=")) {
1867
- url += (url.includes("?") ? "&" : "?") + "connectionId=" + encodeURIComponent(this.connectionId);
1868
- }
1869
- this.ws = new WebSocket(url);
1870
- this.ws.onopen = () => {
1857
+ const headers = { Accept: "text/event-stream" };
1858
+ if (token) headers.Authorization = `Bearer ${token}`;
1859
+ if (this.connectionId) headers["X-Connection-Id"] = this.connectionId;
1860
+ const controller = new AbortController();
1861
+ this.controller = controller;
1862
+ void this.read(url, headers, controller);
1863
+ }
1864
+ async read(url, headers, controller) {
1865
+ try {
1866
+ const response = await fetch(url, { headers, signal: controller.signal });
1867
+ if (!response.ok || !response.body) {
1868
+ throw new Error(`realtime connection failed (${response.status})`);
1869
+ }
1870
+ this.open = true;
1871
1871
  this.reconnectAttempt = 0;
1872
- this.resubscribeAll();
1873
- this.startHeartbeat();
1874
- };
1875
- this.ws.onmessage = (msg) => {
1876
- this.handleMessage(msg.data);
1877
- };
1878
- this.ws.onclose = () => {
1879
- this.stopHeartbeat();
1880
- this.onDisconnect?.();
1881
- this.scheduleReconnect();
1882
- };
1883
- this.ws.onerror = (err) => {
1872
+ this.onReconnect?.();
1873
+ const reader = response.body.getReader();
1874
+ const decoder = new TextDecoder();
1875
+ let buffer = "";
1876
+ for (; ; ) {
1877
+ const { done, value } = await reader.read();
1878
+ if (done) break;
1879
+ buffer = (buffer + decoder.decode(value, { stream: true })).replace(
1880
+ /\r\n/g,
1881
+ "\n"
1882
+ );
1883
+ let index;
1884
+ while ((index = buffer.indexOf("\n\n")) >= 0) {
1885
+ this.handleFrame(buffer.slice(0, index));
1886
+ buffer = buffer.slice(index + 2);
1887
+ }
1888
+ }
1889
+ throw new Error("realtime stream ended");
1890
+ } catch (err) {
1891
+ if (controller.signal.aborted) return;
1884
1892
  this.onError?.(err);
1885
- };
1893
+ } finally {
1894
+ if (this.controller === controller) {
1895
+ this.open = false;
1896
+ this.controller = null;
1897
+ this.onDisconnect?.();
1898
+ this.scheduleReconnect();
1899
+ }
1900
+ }
1886
1901
  }
1887
- handleMessage(data) {
1888
- let parsed;
1902
+ handleFrame(frame) {
1903
+ const dataLines = [];
1904
+ for (const line of frame.split("\n")) {
1905
+ if (line.startsWith("data:")) {
1906
+ dataLines.push(line.slice(5).replace(/^ /, ""));
1907
+ }
1908
+ }
1909
+ if (dataLines.length === 0) return;
1910
+ let data;
1889
1911
  try {
1890
- parsed = JSON.parse(data);
1912
+ data = JSON.parse(dataLines.join("\n"));
1891
1913
  } catch {
1892
1914
  return;
1893
1915
  }
1894
- if (typeof parsed !== "object" || !parsed.topic || !parsed.event) return;
1895
- const topic = parsed.topic;
1896
- const event = parsed.event;
1897
- const payload = parsed.payload || {};
1898
- if (event === "phx_reply") return;
1899
- const subs = this.subscriptions.get(topic);
1900
- if (subs) {
1901
- const e = {
1902
- event,
1903
- topic,
1904
- payload
1905
- };
1906
- for (const s of subs) {
1907
- try {
1908
- s.callback(e);
1909
- } catch {
1910
- }
1911
- }
1916
+ this.dispatch(data);
1917
+ }
1918
+ dispatch(data) {
1919
+ const action = typeof data.action === "string" ? data.action : "update";
1920
+ if (data.collection === "collections") {
1921
+ this.emit("collections", action, data.record ?? {});
1922
+ return;
1923
+ }
1924
+ if (!data.collection) return;
1925
+ const payload = { record: data.record ?? {}, action };
1926
+ this.emit(`collection:${data.collection}`, action, payload);
1927
+ if (data.id) {
1928
+ this.emit(`collection:${data.collection}:${data.id}`, action, payload);
1912
1929
  }
1913
1930
  }
1914
- joinTopic(topic, joinPayload) {
1915
- const ref = this.nextRef();
1916
- const msg = JSON.stringify({
1917
- topic,
1918
- event: "phx_join",
1919
- payload: joinPayload ?? {},
1920
- ref
1921
- });
1922
- this.ws?.send(msg);
1923
- }
1924
- nextRef() {
1925
- this.refCounter++;
1926
- return this.refCounter.toString();
1927
- }
1928
- startHeartbeat() {
1929
- this.stopHeartbeat();
1930
- this.heartbeatInterval = setInterval(() => {
1931
- if (this.ws?.readyState === WebSocket.OPEN) {
1932
- const ref = this.nextRef();
1933
- const msg = JSON.stringify({
1934
- topic: "phoenix",
1935
- event: "heartbeat",
1936
- payload: {},
1937
- ref
1938
- });
1939
- this.ws.send(msg);
1931
+ emit(topic, event, payload) {
1932
+ const subs = this.subscriptions.get(topic);
1933
+ if (!subs) return;
1934
+ const e = { event, topic, payload };
1935
+ for (const s of subs) {
1936
+ try {
1937
+ s.callback(e);
1938
+ } catch {
1940
1939
  }
1941
- }, 3e4);
1942
- }
1943
- stopHeartbeat() {
1944
- if (this.heartbeatInterval) {
1945
- clearInterval(this.heartbeatInterval);
1946
- this.heartbeatInterval = null;
1947
1940
  }
1948
1941
  }
1949
- // ── Reconnect ──
1942
+ closeStream() {
1943
+ const controller = this.controller;
1944
+ this.controller = null;
1945
+ this.open = false;
1946
+ controller?.abort();
1947
+ }
1950
1948
  scheduleReconnect() {
1949
+ if (this.closed) return;
1951
1950
  this.clearReconnectTimer();
1952
1951
  const delay = Math.min(
1953
1952
  1e3 * 2 ** this.reconnectAttempt,
@@ -1956,7 +1955,6 @@ var RealtimeService = class {
1956
1955
  this.reconnectAttempt++;
1957
1956
  this.reconnectTimer = setTimeout(() => {
1958
1957
  this.reconnectTimer = null;
1959
- this.onReconnect?.();
1960
1958
  this.doConnect();
1961
1959
  }, delay);
1962
1960
  }
@@ -1969,14 +1967,18 @@ var RealtimeService = class {
1969
1967
  };
1970
1968
 
1971
1969
  // src/files.ts
1972
- function getFileUrl(baseUrl, fileId) {
1973
- return baseUrl.replace(/\/+$/, "") + "/files/" + encodeURIComponent(fileId);
1970
+ function encodeKey(key) {
1971
+ return key.split("/").map(encodeURIComponent).join("/");
1972
+ }
1973
+ function getFileUrl(baseUrl, key) {
1974
+ return baseUrl.replace(/\/+$/, "") + "/files/" + encodeKey(key);
1974
1975
  }
1975
- function getThumbUrl(baseUrl, fileId, size) {
1976
- return baseUrl.replace(/\/+$/, "") + "/files/" + encodeURIComponent(fileId) + "/thumbs/" + encodeURIComponent(size);
1976
+ function getThumbUrl(baseUrl, key, size) {
1977
+ return getFileUrl(baseUrl, key) + "?thumb=" + encodeURIComponent(size);
1977
1978
  }
1978
- function getScaleUrl(baseUrl, fileId, size) {
1979
- return baseUrl.replace(/\/+$/, "") + "/files/" + encodeURIComponent(fileId) + "/scale/" + encodeURIComponent(size);
1979
+ function getScaleUrl(baseUrl, key, size) {
1980
+ const parameter = size.includes("x") ? "thumb" : "preset";
1981
+ return getFileUrl(baseUrl, key) + "?" + parameter + "=" + encodeURIComponent(size);
1980
1982
  }
1981
1983
  function getVariantUrl(baseUrl, fileId, preset) {
1982
1984
  return getScaleUrl(baseUrl, fileId, preset);
@@ -2307,7 +2309,7 @@ var LazypockClient = class {
2307
2309
  this.http = new HttpClient(baseUrl, this.authStore, connectionId);
2308
2310
  this.realtime = options.realtime ?? new RealtimeService();
2309
2311
  if (!options.realtime) {
2310
- this.realtime.setUrl(wsUrlFromBaseUrl(baseUrl));
2312
+ this.realtime.setUrl(sseUrlFromBaseUrl(baseUrl));
2311
2313
  }
2312
2314
  this.realtime.setConnectionId(connectionId);
2313
2315
  this.realtime.setTokenProvider(() => this.authStore.token);
@@ -2438,7 +2440,7 @@ var LazypockClient = class {
2438
2440
  async login(email, password, collection) {
2439
2441
  let data;
2440
2442
  if (collection) {
2441
- data = await this.http.post("/" + encodeURIComponent(collection) + "/auth-with-password", {
2443
+ data = await this.http.post("/collections/" + encodeURIComponent(collection) + "/auth-with-password", {
2442
2444
  identity: email,
2443
2445
  password
2444
2446
  });
@@ -2448,7 +2450,7 @@ var LazypockClient = class {
2448
2450
  }
2449
2451
  } else {
2450
2452
  try {
2451
- data = await this.http.post("/_superusers/auth-with-password", {
2453
+ data = await this.http.post("/collections/_superusers/auth-with-password", {
2452
2454
  identity: email,
2453
2455
  password
2454
2456
  });
@@ -2488,7 +2490,7 @@ var LazypockClient = class {
2488
2490
  */
2489
2491
  async authWithPassword(collection, identity, password, options) {
2490
2492
  const data = await this.http.post(
2491
- "/" + encodeURIComponent(collection) + "/auth-with-password",
2493
+ "/collections/" + encodeURIComponent(collection) + "/auth-with-password",
2492
2494
  { identity, password },
2493
2495
  options
2494
2496
  );
@@ -2507,7 +2509,7 @@ var LazypockClient = class {
2507
2509
  */
2508
2510
  async authRefresh(collection, options) {
2509
2511
  const data = await this.http.post(
2510
- "/" + encodeURIComponent(collection) + "/auth-refresh",
2512
+ "/collections/" + encodeURIComponent(collection) + "/auth-refresh",
2511
2513
  void 0,
2512
2514
  options
2513
2515
  );
@@ -2559,6 +2561,6 @@ export {
2559
2561
  getThumbUrl,
2560
2562
  getVariantUrl,
2561
2563
  schemaFieldType,
2562
- wsUrlFromBaseUrl
2564
+ sseUrlFromBaseUrl
2563
2565
  };
2564
2566
  //# sourceMappingURL=index.js.map