@arbidocs/sdk 0.5.76 → 0.5.77

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
@@ -10550,8 +10550,10 @@ __export(mail_exports, {
10550
10550
  listFolders: () => listFolders,
10551
10551
  listMailAccounts: () => listMailAccounts,
10552
10552
  listMessages: () => listMessages,
10553
+ openDraftEditor: () => openDraftEditor,
10553
10554
  saveDraft: () => saveDraft,
10554
10555
  sendMessage: () => sendMessage,
10556
+ unlockMailCredentials: () => unlockMailCredentials,
10555
10557
  unlockNativeMail: () => unlockNativeMail,
10556
10558
  updateMessage: () => updateMessage
10557
10559
  });
@@ -10648,6 +10650,47 @@ async function unlockNativeMail(mail, keys) {
10648
10650
  secretKey.fill(0);
10649
10651
  }
10650
10652
  }
10653
+ async function unlockMailCredentials(mail, accountId, keys) {
10654
+ await client.initSodium();
10655
+ const secretKey = client.encryptionKeypairFromSigningKey(keys.signingPrivateKey).secretKey;
10656
+ const { sealed_credentials } = await json(
10657
+ mail(`${accountPath(accountId)}/sealed-credentials`)
10658
+ );
10659
+ const bytes = client.sealedBoxDecrypt(sealed_credentials, secretKey);
10660
+ try {
10661
+ const access = await client.sealKeyForSession(bytes, keys.serverSessionKey);
10662
+ await mail(`${accountPath(accountId)}/session-credentials`, {
10663
+ method: "POST",
10664
+ body: JSON.stringify({ access })
10665
+ });
10666
+ } finally {
10667
+ bytes.fill(0);
10668
+ secretKey.fill(0);
10669
+ }
10670
+ }
10671
+ async function openDraftEditor(mail, body, keys) {
10672
+ const open = (extra = {}) => json(
10673
+ mail("/drafts/editor", { method: "POST", body: JSON.stringify({ ...body, ...extra }) })
10674
+ );
10675
+ try {
10676
+ return await open();
10677
+ } catch (error) {
10678
+ const { status, apiError } = error;
10679
+ const locked = apiError?.detail;
10680
+ if (status !== 423 || !locked?.sealed_key || !locked.room_id) throw error;
10681
+ const { signingPrivateKey, serverSessionKey } = await keys();
10682
+ await client.initSodium();
10683
+ const secretKey = client.encryptionKeypairFromSigningKey(signingPrivateKey).secretKey;
10684
+ const key = client.sealedBoxDecrypt(locked.sealed_key, secretKey);
10685
+ try {
10686
+ const sealed = await client.sealKeyForSession(key, serverSessionKey);
10687
+ return await open({ room_id: locked.room_id, key: sealed });
10688
+ } finally {
10689
+ key.fill(0);
10690
+ secretKey.fill(0);
10691
+ }
10692
+ }
10693
+ }
10651
10694
  function attachmentFromBytes(name, contentType, bytes) {
10652
10695
  return {
10653
10696
  id: "",
@@ -12916,34 +12959,84 @@ var Arbi = class {
12916
12959
  * recovery. Pass `fetch` to the `mail` operations, e.g. `mail.listFolders(arbi.mail.fetch)`.
12917
12960
  */
12918
12961
  mail = {
12919
- /** Fetch a path under `/v1/mail`; rejects a non-2xx response with an `ArbiApiError` (`status`). */
12920
- fetch: (path5, init = {}) => this.recovering(async () => {
12921
- const { baseUrl, accessToken, fetch: send = globalThis.fetch } = this.getAuthHeaders();
12922
- const headers = new Headers(init.headers);
12923
- headers.set("Authorization", `Bearer ${accessToken}`);
12924
- if (init.body && !headers.has("Content-Type"))
12925
- headers.set("Content-Type", "application/json");
12926
- const response = await send(`${baseUrl}/v1/mail${path5}`, { ...init, headers });
12927
- if (!response.ok) {
12928
- const body = await response.json().catch(() => null);
12929
- throw new ArbiApiError(
12930
- typeof body?.detail === "string" ? body.detail : "The mail request could not be completed",
12931
- body,
12932
- response.status
12933
- );
12934
- }
12935
- return response;
12962
+ /**
12963
+ * Fetch a path under `/v1/mail`; rejects a non-2xx response with an `ArbiApiError` (`status`).
12964
+ * A read of the native inbox unlocks it first (`unlockNative`), so mail that arrived since is
12965
+ * readable too; `nativeUnlocked` skips that when the caller already unlocked it for this pass.
12966
+ * With no token yet (a new tab) the session is restored first; a 401 re-logs in and is sent
12967
+ * again once.
12968
+ */
12969
+ fetch: (path5, init = {}, { nativeUnlocked = false } = {}) => this.recovering(async () => {
12970
+ const nativeRead = path5.startsWith("/accounts/native/") && (init.method ?? "GET").toUpperCase() !== "POST";
12971
+ if (nativeRead && !nativeUnlocked) await this.mail.unlockNative();
12972
+ return this.mailRequest(path5, init);
12936
12973
  }),
12937
- /** Unlock the signed-in user's native inbox for this server session (see `unlockNativeMail`). */
12938
- unlockNative: () => {
12939
- const login = this.loginResult;
12940
- if (!login) return Promise.reject(new ArbiError("Sign in to unlock your inbox"));
12941
- return unlockNativeMail(this.mail.fetch, {
12942
- signingPrivateKey: login.signingPrivateKey,
12943
- serverSessionKey: login.serverSessionKey
12944
- });
12945
- }
12974
+ /**
12975
+ * Unlock the signed-in user's native inbox for this server session (see `unlockNativeMail`).
12976
+ * Concurrent calls share one unlock.
12977
+ */
12978
+ unlockNative: () => this.nativeUnlock ??= this.nativeMailKeys().then((keys) => unlockNativeMail(this.mail.fetch, keys)).finally(() => {
12979
+ this.nativeUnlock = null;
12980
+ }),
12981
+ /**
12982
+ * Hand this session what the server needs to read `account`: the native inbox's keys, a
12983
+ * Microsoft or Gmail account's connector tokens, an IMAP account's sealed credentials.
12984
+ */
12985
+ unlockAccount: async (account) => {
12986
+ if (account.provider === "native") return this.mail.unlockNative();
12987
+ if (account.provider === "microsoft" || account.provider === "gmail") {
12988
+ await this.connectors.bridgeTokens({ vendor: account.provider });
12989
+ return;
12990
+ }
12991
+ await unlockMailCredentials(this.mail.fetch, account.id, await this.nativeMailKeys());
12992
+ },
12993
+ /**
12994
+ * Open a native draft's body in the Word editor, unlocking its room for this session with the
12995
+ * user's key when the server asks (see `openDraftEditor`).
12996
+ */
12997
+ openDraftEditor: (body) => openDraftEditor(this.mail.fetch, body, () => this.nativeMailKeys())
12946
12998
  };
12999
+ /** The native inbox unlock in flight (`mail.unlockNative`). */
13000
+ nativeUnlock = null;
13001
+ /** One request under `/v1/mail` on the session's token (see `mail.fetch`). */
13002
+ async mailRequest(path5, init) {
13003
+ const client = this.requireClient();
13004
+ let token = client.session.getState().accessToken;
13005
+ if (!token) token = await client.auth.relogin();
13006
+ if (!token) throw new ArbiError("Sign in again to restore your session");
13007
+ const send = client.http?.fetch ?? globalThis.fetch;
13008
+ const request = (bearer) => {
13009
+ const headers = new Headers(init.headers);
13010
+ headers.set("Authorization", `Bearer ${bearer}`);
13011
+ if (init.body && !headers.has("Content-Type")) headers.set("Content-Type", "application/json");
13012
+ return send(`${this.options.url}/v1/mail${path5}`, { ...init, headers });
13013
+ };
13014
+ let response = await request(token);
13015
+ if (response.status === 401) {
13016
+ const refreshed = await client.auth.relogin();
13017
+ if (refreshed) response = await request(refreshed);
13018
+ }
13019
+ if (!response.ok) {
13020
+ const body = await response.json().catch(() => null);
13021
+ throw new ArbiApiError(
13022
+ typeof body?.detail === "string" ? body.detail : "The mail request could not be completed",
13023
+ body,
13024
+ response.status
13025
+ );
13026
+ }
13027
+ return response;
13028
+ }
13029
+ /**
13030
+ * The keys that unlock the user's mail for this server session: the session's key (current after
13031
+ * a re-login) and the identity key (the login's, or the one kept on this device).
13032
+ */
13033
+ async nativeMailKeys() {
13034
+ const client = this.requireClient();
13035
+ const serverSessionKey = client.session.getState().serverSessionKey ?? this.loginResult?.serverSessionKey ?? null;
13036
+ const signingPrivateKey = this.loginResult?.signingPrivateKey ?? (await client.storage.getSession().catch(() => null))?.signingPrivateKey;
13037
+ if (!serverSessionKey || !signingPrivateKey) throw new ArbiError("Sign in to unlock your inbox");
13038
+ return { signingPrivateKey, serverSessionKey };
13039
+ }
12947
13040
  // ── Secure mail ─────────────────────────────────────────────────────
12948
13041
  /**
12949
13042
  * Mail to anyone, sealed in this browser under a key that travels only in the link's fragment