@arbidocs/sdk 0.5.25 → 0.5.26

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.cjs CHANGED
@@ -4647,9 +4647,11 @@ var WebSocketAuthError = class extends Error {
4647
4647
  this.name = "WebSocketAuthError";
4648
4648
  }
4649
4649
  };
4650
+ var wsAborted = () => new DOMException("The connection was aborted.", "AbortError");
4650
4651
  function connectWebSocket(options) {
4651
- const { baseUrl, accessToken, onMessage, onClose, onAuthenticated } = options;
4652
+ const { baseUrl, accessToken, onMessage, onClose, onAuthenticated, signal } = options;
4652
4653
  const url = client.buildWebSocketUrl(baseUrl);
4654
+ if (signal?.aborted) return Promise.reject(wsAborted());
4653
4655
  return new Promise((resolve2, reject) => {
4654
4656
  const ws = new WebSocket(url);
4655
4657
  let authenticated = false;
@@ -4659,6 +4661,15 @@ function connectWebSocket(options) {
4659
4661
  reject(new Error("WebSocket auth timed out"));
4660
4662
  }
4661
4663
  }, AUTH_TIMEOUT_MS);
4664
+ signal?.addEventListener(
4665
+ "abort",
4666
+ () => {
4667
+ clearTimeout(timeout);
4668
+ ws.close();
4669
+ reject(wsAborted());
4670
+ },
4671
+ { once: true }
4672
+ );
4662
4673
  ws.addEventListener("open", () => {
4663
4674
  ws.send(client.createAuthMessage(accessToken));
4664
4675
  });
@@ -4708,7 +4719,8 @@ async function connectWithReconnect(options) {
4708
4719
  onAuthenticated,
4709
4720
  shouldReconnect,
4710
4721
  baseUrl,
4711
- accessToken
4722
+ accessToken,
4723
+ signal
4712
4724
  } = options;
4713
4725
  const resolveToken = getAccessToken ?? (() => accessToken);
4714
4726
  let closed = false;
@@ -4720,6 +4732,7 @@ async function connectWithReconnect(options) {
4720
4732
  return connectWebSocket({
4721
4733
  baseUrl,
4722
4734
  accessToken: token,
4735
+ signal,
4723
4736
  onMessage,
4724
4737
  onAuthenticated,
4725
4738
  onClose: (code, reason) => {
@@ -4756,22 +4769,28 @@ async function connectWithReconnect(options) {
4756
4769
  }
4757
4770
  if (!closed) onReconnectFailed?.();
4758
4771
  };
4772
+ const close = () => {
4773
+ closed = true;
4774
+ cancelDelay?.();
4775
+ currentConnection?.close();
4776
+ currentConnection = null;
4777
+ };
4778
+ if (signal?.aborted) throw wsAborted();
4779
+ signal?.addEventListener("abort", close, { once: true });
4759
4780
  try {
4760
4781
  currentConnection = await connectOnce();
4761
4782
  } catch (err) {
4783
+ if (closed) throw wsAborted();
4762
4784
  if (!(err instanceof WebSocketAuthError) || !refreshAuth) throw err;
4763
4785
  await refreshAuth().catch(() => {
4764
4786
  });
4765
4787
  currentConnection = await connectOnce();
4766
4788
  }
4767
- return {
4768
- close: () => {
4769
- closed = true;
4770
- cancelDelay?.();
4771
- currentConnection?.close();
4772
- currentConnection = null;
4773
- }
4774
- };
4789
+ if (closed) {
4790
+ close();
4791
+ throw wsAborted();
4792
+ }
4793
+ return { close };
4775
4794
  }
4776
4795
  var DOC_TERMINAL_STATUSES = /* @__PURE__ */ new Set([
4777
4796
  "completed",
@@ -10631,6 +10650,10 @@ var WorkspaceOpenIntent = class {
10631
10650
  workspaceId;
10632
10651
  result = null;
10633
10652
  };
10653
+ function keyLookupFailed(error) {
10654
+ if (client.isAbortError(error)) throw error;
10655
+ throw new ArbiError(error instanceof Error ? error.message : String(error));
10656
+ }
10634
10657
  var Arbi = class {
10635
10658
  client = null;
10636
10659
  loginResult = null;
@@ -10643,6 +10666,7 @@ var Arbi = class {
10643
10666
  /** The chat keys I hold, opened once per set of epochs (`chatRingSignature`). */
10644
10667
  chatRings = /* @__PURE__ */ new Map();
10645
10668
  sessionExpiredListeners = /* @__PURE__ */ new Set();
10669
+ signingOutListeners = /* @__PURE__ */ new Set();
10646
10670
  signedInListeners = /* @__PURE__ */ new Set();
10647
10671
  /** The client is this instance's own (`init`), not a host's (`options.session`). */
10648
10672
  ownsClient = false;
@@ -10682,6 +10706,9 @@ var Arbi = class {
10682
10706
  });
10683
10707
  this.ownsClient = true;
10684
10708
  this.client.auth.onSessionExpired(() => this.expireSession());
10709
+ this.client.auth.onSigningOut(() => {
10710
+ for (const listener of [...this.signingOutListeners]) listener();
10711
+ });
10685
10712
  await this.client.crypto.initSodium();
10686
10713
  return this.client;
10687
10714
  }
@@ -10823,9 +10850,7 @@ var Arbi = class {
10823
10850
  if (!key) throw new ArbiError(`Workspace ${workspaceId} not found or has no encryption key`);
10824
10851
  return key;
10825
10852
  };
10826
- const { wrappedKey, fromCache } = await keys.wrappedKey(workspaceId).catch((error) => {
10827
- throw new ArbiError(error instanceof Error ? error.message : String(error));
10828
- });
10853
+ const { wrappedKey, fromCache } = await keys.wrappedKey(workspaceId).catch(keyLookupFailed);
10829
10854
  try {
10830
10855
  return await withKey(required(wrappedKey));
10831
10856
  } catch (error) {
@@ -10915,9 +10940,7 @@ var Arbi = class {
10915
10940
  }),
10916
10941
  `Failed to open workspace ${workspaceId}`
10917
10942
  );
10918
- const listed = await client.workspaceKeys.wrappedKey(workspaceId).catch((error) => {
10919
- throw new ArbiError(error instanceof Error ? error.message : String(error));
10920
- });
10943
+ const listed = await client.workspaceKeys.wrappedKey(workspaceId).catch(keyLookupFailed);
10921
10944
  if (!listed.wrappedKey) return open("");
10922
10945
  return this.withWrappedKey(
10923
10946
  workspaceId,
@@ -10965,6 +10988,18 @@ var Arbi = class {
10965
10988
  this.sessionExpiredListeners.add(listener);
10966
10989
  return () => this.sessionExpiredListeners.delete(listener);
10967
10990
  }
10991
+ /**
10992
+ * Subscribe to "the session is ending": `logout` has begun. Called once nothing new can start on
10993
+ * the session (no request, no re-login, no workspace open) and before what is in flight is
10994
+ * aborted and the server revokes the token: stop the host's own work on it there (cancel its
10995
+ * queries, close its socket). Returns an unsubscribe. On a host's session it is the host
10996
+ * client's.
10997
+ */
10998
+ onSigningOut(listener) {
10999
+ if (this.client && !this.ownsClient) return this.client.auth.onSigningOut(listener);
11000
+ this.signingOutListeners.add(listener);
11001
+ return () => this.signingOutListeners.delete(listener);
11002
+ }
10968
11003
  expireSession() {
10969
11004
  if (this.loginResult) {
10970
11005
  this.loginResult.signingPrivateKey.fill(0);
@@ -10996,6 +11031,7 @@ var Arbi = class {
10996
11031
  const unauthorised = /\b401\b|token has expired/i.test(message);
10997
11032
  if (!(error instanceof ArbiApiError) || !(noWorkspace || unauthorised)) throw error;
10998
11033
  const client = this.requireClient();
11034
+ if (client.auth.isSigningOut()) throw error;
10999
11035
  const recovered = noWorkspace && await client.auth.reopenWorkspace() || await client.auth.relogin() !== null;
11000
11036
  if (!recovered) throw new ArbiSessionExpiredError();
11001
11037
  return run();
@@ -11006,9 +11042,11 @@ var Arbi = class {
11006
11042
  for (const listener of [...this.profileListeners]) listener(profile);
11007
11043
  }
11008
11044
  /**
11009
- * Sign out: the server session ends (every session of the user with `everywhere`), the session
11010
- * state is cleared and, with `everywhere`, so are the keys kept on this device (a single sign-out
11011
- * keeps them for an offline unlock). Then this instance's own state is cleared.
11045
+ * Sign out: first the session's work stops (nothing new starts, `onSigningOut` listeners run,
11046
+ * requests in flight are aborted), then the server session ends (every session of the user with
11047
+ * `everywhere`), the session state is cleared and, with `everywhere`, so are the keys kept on this
11048
+ * device (a single sign-out keeps them for an offline unlock). Then this instance's own state is
11049
+ * cleared.
11012
11050
  */
11013
11051
  async logout(options = {}) {
11014
11052
  if (this.client) {
@@ -11055,12 +11093,13 @@ var Arbi = class {
11055
11093
  * Reads the access token from the session on each call, so a token refreshed
11056
11094
  * elsewhere is reflected on the next (re)connect. Returns `null` when there is
11057
11095
  * no client/token yet (not logged in) rather than throwing, so callers can
11058
- * poll for readiness instead of guarding against exceptions.
11096
+ * poll for readiness instead of guarding against exceptions. `signal` is the
11097
+ * session's: aborted when a sign-out begins, so the transport closes with it.
11059
11098
  */
11060
11099
  getConnectionInfo() {
11061
11100
  const accessToken = this.client?.session.getState().accessToken;
11062
- if (!accessToken) return null;
11063
- return { baseUrl: this.options.url, accessToken };
11101
+ if (!this.client || !accessToken) return null;
11102
+ return { baseUrl: this.options.url, accessToken, signal: this.client.auth.sessionSignal() };
11064
11103
  }
11065
11104
  /**
11066
11105
  * Re-login silently and resolve the fresh access token (`null` when the session could not be