@arbidocs/sdk 0.5.25 → 0.5.27

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.
@@ -554,6 +554,13 @@ interface ConnectOptions {
554
554
  * auth handshake itself is owned by the transport — this is a notification.
555
555
  */
556
556
  onAuthenticated?: (msg: WsAuthResultMessage) => void;
557
+ /**
558
+ * Closes the socket (for good, with reconnection) when aborted, even mid-connect: e.g. the
559
+ * client's `auth.sessionSignal()`, so the socket goes when a sign-out begins, before the server
560
+ * revokes its token, and never re-authenticates on it. A connect it stops rejects with an
561
+ * `AbortError`.
562
+ */
563
+ signal?: AbortSignal;
557
564
  }
558
565
  interface ReconnectOptions extends ConnectOptions {
559
566
  /**
@@ -7918,6 +7925,7 @@ declare class Arbi {
7918
7925
  /** The chat keys I hold, opened once per set of epochs (`chatRingSignature`). */
7919
7926
  private chatRings;
7920
7927
  private readonly sessionExpiredListeners;
7928
+ private readonly signingOutListeners;
7921
7929
  private readonly signedInListeners;
7922
7930
  /** The client is this instance's own (`init`), not a host's (`options.session`). */
7923
7931
  private ownsClient;
@@ -8057,6 +8065,14 @@ declare class Arbi {
8057
8065
  * On a host's session it is the host client's expiry.
8058
8066
  */
8059
8067
  onSessionExpired(listener: () => void): () => void;
8068
+ /**
8069
+ * Subscribe to "the session is ending": `logout` has begun. Called once nothing new can start on
8070
+ * the session (no request, no re-login, no workspace open) and before what is in flight is
8071
+ * aborted and the server revokes the token: stop the host's own work on it there (cancel its
8072
+ * queries, close its socket). Returns an unsubscribe. On a host's session it is the host
8073
+ * client's.
8074
+ */
8075
+ onSigningOut(listener: () => void): () => void;
8060
8076
  private expireSession;
8061
8077
  /**
8062
8078
  * Run a raw-fetch operation (streaming, multipart: no middleware) and, when it fails because
@@ -8067,9 +8083,11 @@ declare class Arbi {
8067
8083
  private recovering;
8068
8084
  private setUserProfile;
8069
8085
  /**
8070
- * Sign out: the server session ends (every session of the user with `everywhere`), the session
8071
- * state is cleared and, with `everywhere`, so are the keys kept on this device (a single sign-out
8072
- * keeps them for an offline unlock). Then this instance's own state is cleared.
8086
+ * Sign out: first the session's work stops (nothing new starts, `onSigningOut` listeners run,
8087
+ * requests in flight are aborted), then the server session ends (every session of the user with
8088
+ * `everywhere`), the session state is cleared and, with `everywhere`, so are the keys kept on this
8089
+ * device (a single sign-out keeps them for an offline unlock). Then this instance's own state is
8090
+ * cleared.
8073
8091
  */
8074
8092
  logout(options?: {
8075
8093
  everywhere?: boolean;
@@ -8093,11 +8111,13 @@ declare class Arbi {
8093
8111
  * Reads the access token from the session on each call, so a token refreshed
8094
8112
  * elsewhere is reflected on the next (re)connect. Returns `null` when there is
8095
8113
  * no client/token yet (not logged in) rather than throwing, so callers can
8096
- * poll for readiness instead of guarding against exceptions.
8114
+ * poll for readiness instead of guarding against exceptions. `signal` is the
8115
+ * session's: aborted when a sign-out begins, so the transport closes with it.
8097
8116
  */
8098
8117
  getConnectionInfo(): {
8099
8118
  baseUrl: string;
8100
8119
  accessToken: string;
8120
+ signal: AbortSignal;
8101
8121
  } | null;
8102
8122
  /**
8103
8123
  * Re-login silently and resolve the fresh access token (`null` when the session could not be
@@ -554,6 +554,13 @@ interface ConnectOptions {
554
554
  * auth handshake itself is owned by the transport — this is a notification.
555
555
  */
556
556
  onAuthenticated?: (msg: WsAuthResultMessage) => void;
557
+ /**
558
+ * Closes the socket (for good, with reconnection) when aborted, even mid-connect: e.g. the
559
+ * client's `auth.sessionSignal()`, so the socket goes when a sign-out begins, before the server
560
+ * revokes its token, and never re-authenticates on it. A connect it stops rejects with an
561
+ * `AbortError`.
562
+ */
563
+ signal?: AbortSignal;
557
564
  }
558
565
  interface ReconnectOptions extends ConnectOptions {
559
566
  /**
@@ -7918,6 +7925,7 @@ declare class Arbi {
7918
7925
  /** The chat keys I hold, opened once per set of epochs (`chatRingSignature`). */
7919
7926
  private chatRings;
7920
7927
  private readonly sessionExpiredListeners;
7928
+ private readonly signingOutListeners;
7921
7929
  private readonly signedInListeners;
7922
7930
  /** The client is this instance's own (`init`), not a host's (`options.session`). */
7923
7931
  private ownsClient;
@@ -8057,6 +8065,14 @@ declare class Arbi {
8057
8065
  * On a host's session it is the host client's expiry.
8058
8066
  */
8059
8067
  onSessionExpired(listener: () => void): () => void;
8068
+ /**
8069
+ * Subscribe to "the session is ending": `logout` has begun. Called once nothing new can start on
8070
+ * the session (no request, no re-login, no workspace open) and before what is in flight is
8071
+ * aborted and the server revokes the token: stop the host's own work on it there (cancel its
8072
+ * queries, close its socket). Returns an unsubscribe. On a host's session it is the host
8073
+ * client's.
8074
+ */
8075
+ onSigningOut(listener: () => void): () => void;
8060
8076
  private expireSession;
8061
8077
  /**
8062
8078
  * Run a raw-fetch operation (streaming, multipart: no middleware) and, when it fails because
@@ -8067,9 +8083,11 @@ declare class Arbi {
8067
8083
  private recovering;
8068
8084
  private setUserProfile;
8069
8085
  /**
8070
- * Sign out: the server session ends (every session of the user with `everywhere`), the session
8071
- * state is cleared and, with `everywhere`, so are the keys kept on this device (a single sign-out
8072
- * keeps them for an offline unlock). Then this instance's own state is cleared.
8086
+ * Sign out: first the session's work stops (nothing new starts, `onSigningOut` listeners run,
8087
+ * requests in flight are aborted), then the server session ends (every session of the user with
8088
+ * `everywhere`), the session state is cleared and, with `everywhere`, so are the keys kept on this
8089
+ * device (a single sign-out keeps them for an offline unlock). Then this instance's own state is
8090
+ * cleared.
8073
8091
  */
8074
8092
  logout(options?: {
8075
8093
  everywhere?: boolean;
@@ -8093,11 +8111,13 @@ declare class Arbi {
8093
8111
  * Reads the access token from the session on each call, so a token refreshed
8094
8112
  * elsewhere is reflected on the next (re)connect. Returns `null` when there is
8095
8113
  * no client/token yet (not logged in) rather than throwing, so callers can
8096
- * poll for readiness instead of guarding against exceptions.
8114
+ * poll for readiness instead of guarding against exceptions. `signal` is the
8115
+ * session's: aborted when a sign-out begins, so the transport closes with it.
8097
8116
  */
8098
8117
  getConnectionInfo(): {
8099
8118
  baseUrl: string;
8100
8119
  accessToken: string;
8120
+ signal: AbortSignal;
8101
8121
  } | null;
8102
8122
  /**
8103
8123
  * Re-login silently and resolve the fresh access token (`null` when the session could not be
package/dist/browser.cjs CHANGED
@@ -4219,9 +4219,11 @@ var WebSocketAuthError = class extends Error {
4219
4219
  this.name = "WebSocketAuthError";
4220
4220
  }
4221
4221
  };
4222
+ var wsAborted = () => new DOMException("The connection was aborted.", "AbortError");
4222
4223
  function connectWebSocket(options) {
4223
- const { baseUrl, accessToken, onMessage, onClose, onAuthenticated } = options;
4224
+ const { baseUrl, accessToken, onMessage, onClose, onAuthenticated, signal } = options;
4224
4225
  const url = client.buildWebSocketUrl(baseUrl);
4226
+ if (signal?.aborted) return Promise.reject(wsAborted());
4225
4227
  return new Promise((resolve, reject) => {
4226
4228
  const ws = new WebSocket(url);
4227
4229
  let authenticated = false;
@@ -4231,6 +4233,15 @@ function connectWebSocket(options) {
4231
4233
  reject(new Error("WebSocket auth timed out"));
4232
4234
  }
4233
4235
  }, AUTH_TIMEOUT_MS);
4236
+ signal?.addEventListener(
4237
+ "abort",
4238
+ () => {
4239
+ clearTimeout(timeout);
4240
+ ws.close();
4241
+ reject(wsAborted());
4242
+ },
4243
+ { once: true }
4244
+ );
4234
4245
  ws.addEventListener("open", () => {
4235
4246
  ws.send(client.createAuthMessage(accessToken));
4236
4247
  });
@@ -4280,7 +4291,8 @@ async function connectWithReconnect(options) {
4280
4291
  onAuthenticated,
4281
4292
  shouldReconnect,
4282
4293
  baseUrl,
4283
- accessToken
4294
+ accessToken,
4295
+ signal
4284
4296
  } = options;
4285
4297
  const resolveToken = getAccessToken ?? (() => accessToken);
4286
4298
  let closed = false;
@@ -4292,6 +4304,7 @@ async function connectWithReconnect(options) {
4292
4304
  return connectWebSocket({
4293
4305
  baseUrl,
4294
4306
  accessToken: token,
4307
+ signal,
4295
4308
  onMessage,
4296
4309
  onAuthenticated,
4297
4310
  onClose: (code, reason) => {
@@ -4328,22 +4341,28 @@ async function connectWithReconnect(options) {
4328
4341
  }
4329
4342
  if (!closed) onReconnectFailed?.();
4330
4343
  };
4344
+ const close = () => {
4345
+ closed = true;
4346
+ cancelDelay?.();
4347
+ currentConnection?.close();
4348
+ currentConnection = null;
4349
+ };
4350
+ if (signal?.aborted) throw wsAborted();
4351
+ signal?.addEventListener("abort", close, { once: true });
4331
4352
  try {
4332
4353
  currentConnection = await connectOnce();
4333
4354
  } catch (err) {
4355
+ if (closed) throw wsAborted();
4334
4356
  if (!(err instanceof WebSocketAuthError) || !refreshAuth) throw err;
4335
4357
  await refreshAuth().catch(() => {
4336
4358
  });
4337
4359
  currentConnection = await connectOnce();
4338
4360
  }
4339
- return {
4340
- close: () => {
4341
- closed = true;
4342
- cancelDelay?.();
4343
- currentConnection?.close();
4344
- currentConnection = null;
4345
- }
4346
- };
4361
+ if (closed) {
4362
+ close();
4363
+ throw wsAborted();
4364
+ }
4365
+ return { close };
4347
4366
  }
4348
4367
 
4349
4368
  // src/citations.ts
@@ -10033,6 +10052,10 @@ var WorkspaceOpenIntent = class {
10033
10052
  workspaceId;
10034
10053
  result = null;
10035
10054
  };
10055
+ function keyLookupFailed(error) {
10056
+ if (client.isAbortError(error)) throw error;
10057
+ throw new ArbiError(error instanceof Error ? error.message : String(error));
10058
+ }
10036
10059
  var Arbi = class {
10037
10060
  client = null;
10038
10061
  loginResult = null;
@@ -10045,6 +10068,7 @@ var Arbi = class {
10045
10068
  /** The chat keys I hold, opened once per set of epochs (`chatRingSignature`). */
10046
10069
  chatRings = /* @__PURE__ */ new Map();
10047
10070
  sessionExpiredListeners = /* @__PURE__ */ new Set();
10071
+ signingOutListeners = /* @__PURE__ */ new Set();
10048
10072
  signedInListeners = /* @__PURE__ */ new Set();
10049
10073
  /** The client is this instance's own (`init`), not a host's (`options.session`). */
10050
10074
  ownsClient = false;
@@ -10084,6 +10108,9 @@ var Arbi = class {
10084
10108
  });
10085
10109
  this.ownsClient = true;
10086
10110
  this.client.auth.onSessionExpired(() => this.expireSession());
10111
+ this.client.auth.onSigningOut(() => {
10112
+ for (const listener of [...this.signingOutListeners]) listener();
10113
+ });
10087
10114
  await this.client.crypto.initSodium();
10088
10115
  return this.client;
10089
10116
  }
@@ -10225,9 +10252,7 @@ var Arbi = class {
10225
10252
  if (!key) throw new ArbiError(`Workspace ${workspaceId} not found or has no encryption key`);
10226
10253
  return key;
10227
10254
  };
10228
- const { wrappedKey, fromCache } = await keys.wrappedKey(workspaceId).catch((error) => {
10229
- throw new ArbiError(error instanceof Error ? error.message : String(error));
10230
- });
10255
+ const { wrappedKey, fromCache } = await keys.wrappedKey(workspaceId).catch(keyLookupFailed);
10231
10256
  try {
10232
10257
  return await withKey(required(wrappedKey));
10233
10258
  } catch (error) {
@@ -10317,9 +10342,7 @@ var Arbi = class {
10317
10342
  }),
10318
10343
  `Failed to open workspace ${workspaceId}`
10319
10344
  );
10320
- const listed = await client.workspaceKeys.wrappedKey(workspaceId).catch((error) => {
10321
- throw new ArbiError(error instanceof Error ? error.message : String(error));
10322
- });
10345
+ const listed = await client.workspaceKeys.wrappedKey(workspaceId).catch(keyLookupFailed);
10323
10346
  if (!listed.wrappedKey) return open("");
10324
10347
  return this.withWrappedKey(
10325
10348
  workspaceId,
@@ -10367,6 +10390,18 @@ var Arbi = class {
10367
10390
  this.sessionExpiredListeners.add(listener);
10368
10391
  return () => this.sessionExpiredListeners.delete(listener);
10369
10392
  }
10393
+ /**
10394
+ * Subscribe to "the session is ending": `logout` has begun. Called once nothing new can start on
10395
+ * the session (no request, no re-login, no workspace open) and before what is in flight is
10396
+ * aborted and the server revokes the token: stop the host's own work on it there (cancel its
10397
+ * queries, close its socket). Returns an unsubscribe. On a host's session it is the host
10398
+ * client's.
10399
+ */
10400
+ onSigningOut(listener) {
10401
+ if (this.client && !this.ownsClient) return this.client.auth.onSigningOut(listener);
10402
+ this.signingOutListeners.add(listener);
10403
+ return () => this.signingOutListeners.delete(listener);
10404
+ }
10370
10405
  expireSession() {
10371
10406
  if (this.loginResult) {
10372
10407
  this.loginResult.signingPrivateKey.fill(0);
@@ -10398,6 +10433,7 @@ var Arbi = class {
10398
10433
  const unauthorised = /\b401\b|token has expired/i.test(message);
10399
10434
  if (!(error instanceof ArbiApiError) || !(noWorkspace || unauthorised)) throw error;
10400
10435
  const client = this.requireClient();
10436
+ if (client.auth.isSigningOut()) throw error;
10401
10437
  const recovered = noWorkspace && await client.auth.reopenWorkspace() || await client.auth.relogin() !== null;
10402
10438
  if (!recovered) throw new ArbiSessionExpiredError();
10403
10439
  return run();
@@ -10408,9 +10444,11 @@ var Arbi = class {
10408
10444
  for (const listener of [...this.profileListeners]) listener(profile);
10409
10445
  }
10410
10446
  /**
10411
- * Sign out: the server session ends (every session of the user with `everywhere`), the session
10412
- * state is cleared and, with `everywhere`, so are the keys kept on this device (a single sign-out
10413
- * keeps them for an offline unlock). Then this instance's own state is cleared.
10447
+ * Sign out: first the session's work stops (nothing new starts, `onSigningOut` listeners run,
10448
+ * requests in flight are aborted), then the server session ends (every session of the user with
10449
+ * `everywhere`), the session state is cleared and, with `everywhere`, so are the keys kept on this
10450
+ * device (a single sign-out keeps them for an offline unlock). Then this instance's own state is
10451
+ * cleared.
10414
10452
  */
10415
10453
  async logout(options = {}) {
10416
10454
  if (this.client) {
@@ -10457,12 +10495,13 @@ var Arbi = class {
10457
10495
  * Reads the access token from the session on each call, so a token refreshed
10458
10496
  * elsewhere is reflected on the next (re)connect. Returns `null` when there is
10459
10497
  * no client/token yet (not logged in) rather than throwing, so callers can
10460
- * poll for readiness instead of guarding against exceptions.
10498
+ * poll for readiness instead of guarding against exceptions. `signal` is the
10499
+ * session's: aborted when a sign-out begins, so the transport closes with it.
10461
10500
  */
10462
10501
  getConnectionInfo() {
10463
10502
  const accessToken = this.client?.session.getState().accessToken;
10464
- if (!accessToken) return null;
10465
- return { baseUrl: this.options.url, accessToken };
10503
+ if (!this.client || !accessToken) return null;
10504
+ return { baseUrl: this.options.url, accessToken, signal: this.client.auth.sessionSignal() };
10466
10505
  }
10467
10506
  /**
10468
10507
  * Re-login silently and resolve the fresh access token (`null` when the session could not be