@arbidocs/sdk 0.5.69 → 0.5.71

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/browser.cjs CHANGED
@@ -6011,6 +6011,139 @@ async function unlockModelConnections(arbi, keys, onlyId) {
6011
6011
  }
6012
6012
  }
6013
6013
 
6014
+ // src/operations/secure-mail.ts
6015
+ var secure_mail_exports = {};
6016
+ __export(secure_mail_exports, {
6017
+ claimSecureMail: () => claimSecureMail,
6018
+ prepareSecureMail: () => prepareSecureMail,
6019
+ secureMime: () => secureMime
6020
+ });
6021
+
6022
+ // src/operations/mail-encoding.ts
6023
+ function bytesToBase64(bytes) {
6024
+ let binary = "";
6025
+ for (let i2 = 0; i2 < bytes.length; i2 += 32768) {
6026
+ binary += String.fromCharCode(...bytes.subarray(i2, i2 + 32768));
6027
+ }
6028
+ return btoa(binary);
6029
+ }
6030
+ var MAX_MESSAGE_BYTES = 25 * 1024 * 1024;
6031
+ var utf8 = new TextEncoder();
6032
+ var encodedWord = (value) => `=?UTF-8?B?${bytesToBase64(utf8.encode(value))}?=`;
6033
+ var folded = (value) => value.match(/.{1,76}/g)?.join("\r\n") ?? "";
6034
+ var safeHeader = (value) => {
6035
+ if (/[\r\n\0]/.test(value)) throw new Error("Email headers cannot contain newlines");
6036
+ return value;
6037
+ };
6038
+ function secureMime(compose, sender) {
6039
+ const address = (r2) => `${r2.name ? encodedWord(safeHeader(r2.name)) + " " : ""}<${safeHeader(r2.email)}>`;
6040
+ const boundary = "arbi-" + crypto.randomUUID();
6041
+ const headers = [
6042
+ `From: ${address({ email: sender.email, name: sender.display_name })}`,
6043
+ ...compose.to.length ? [`To: ${compose.to.map(address).join(", ")}`] : [],
6044
+ ...compose.cc.length ? [`Cc: ${compose.cc.map(address).join(", ")}`] : [],
6045
+ `Subject: ${encodedWord(safeHeader(compose.subject))}`,
6046
+ `Date: ${(/* @__PURE__ */ new Date()).toUTCString()}`,
6047
+ `Message-ID: <${crypto.randomUUID()}@${sender.email.split("@")[1]}>`,
6048
+ ...compose.in_reply_to ? [`In-Reply-To: ${safeHeader(compose.in_reply_to)}`] : [],
6049
+ ...compose.references?.length ? [`References: ${compose.references.map(safeHeader).join(" ")}`] : [],
6050
+ "MIME-Version: 1.0",
6051
+ "X-ARBI-Secure-Delivery: 1",
6052
+ `Content-Type: multipart/mixed; boundary="${boundary}"`
6053
+ ];
6054
+ const alternative = "alternative-" + crypto.randomUUID();
6055
+ const plainPart = `Content-Type: text/plain; charset="utf-8"\r
6056
+ Content-Transfer-Encoding: base64\r
6057
+ \r
6058
+ ${folded(bytesToBase64(utf8.encode(compose.text)))}`;
6059
+ const bodyPart = compose.html ? `Content-Type: multipart/alternative; boundary="${alternative}"\r
6060
+ \r
6061
+ --${alternative}\r
6062
+ ${plainPart}\r
6063
+ --${alternative}\r
6064
+ Content-Type: text/html; charset="utf-8"\r
6065
+ Content-Transfer-Encoding: base64\r
6066
+ \r
6067
+ ${folded(bytesToBase64(utf8.encode(compose.html)))}\r
6068
+ --${alternative}--\r
6069
+ ` : plainPart;
6070
+ const parts = [
6071
+ `--${boundary}\r
6072
+ ${bodyPart}`,
6073
+ ...compose.attachments.map((a2) => {
6074
+ if (!a2.content_base64) throw new Error("Attachment content is missing");
6075
+ const name = encodeURIComponent(safeHeader(a2.name)).replace(/'/g, "%27");
6076
+ return `--${boundary}\r
6077
+ Content-Type: ${safeHeader(a2.content_type || "application/octet-stream")}\r
6078
+ Content-Disposition: attachment; filename*=UTF-8''${name}\r
6079
+ Content-Transfer-Encoding: base64\r
6080
+ \r
6081
+ ${folded(a2.content_base64)}`;
6082
+ }),
6083
+ `--${boundary}--\r
6084
+ `
6085
+ ];
6086
+ return utf8.encode(headers.join("\r\n") + "\r\n\r\n" + parts.join("\r\n"));
6087
+ }
6088
+ async function prepareSecureMail(compose, sender, { requestId, senderPublicKey, frontendOrigin }) {
6089
+ const raw = secureMime(compose, sender);
6090
+ try {
6091
+ if (raw.length > MAX_MESSAGE_BYTES) throw new Error("Email exceeds the message size limit");
6092
+ const metadata = {
6093
+ secure_delivery: true,
6094
+ id: "",
6095
+ subject: compose.subject,
6096
+ sender: [{ email: sender.email, name: sender.display_name }],
6097
+ to: compose.to,
6098
+ cc: compose.cc,
6099
+ bcc: [],
6100
+ received_at: (/* @__PURE__ */ new Date()).toISOString(),
6101
+ preview: compose.text.slice(0, 220),
6102
+ unread: false,
6103
+ text: "",
6104
+ references: compose.references ?? [],
6105
+ attachments: compose.attachments.map((a2, i2) => ({
6106
+ ...a2,
6107
+ id: String(i2),
6108
+ content_base64: null
6109
+ }))
6110
+ };
6111
+ const recipients = [
6112
+ ...new Set([...compose.to, ...compose.cc, ...compose.bcc].map((r2) => r2.email.toLowerCase()))
6113
+ ];
6114
+ const sealed = await client.sealSecureMail(raw, metadata, senderPublicKey);
6115
+ return {
6116
+ request_id: requestId,
6117
+ draft_id: compose.draft_id,
6118
+ recipients: recipients.map((email) => ({
6119
+ id: crypto.randomUUID(),
6120
+ email,
6121
+ fragment_key: sealed.fragment_key
6122
+ })),
6123
+ ciphertext: sealed.ciphertext,
6124
+ metadata_ciphertext: sealed.metadata_ciphertext,
6125
+ sender_wrapped_key: sealed.sender_wrapped_key,
6126
+ frontend_origin: frontendOrigin
6127
+ };
6128
+ } finally {
6129
+ raw.fill(0);
6130
+ }
6131
+ }
6132
+ async function claimSecureMail(mail, id, fragmentKey, recipientPublicKey) {
6133
+ const key = client.decodeSecureMailKey(fragmentKey);
6134
+ try {
6135
+ const path2 = `/secure/${encodeURIComponent(id)}`;
6136
+ const response = await mail(path2);
6137
+ const { raw } = await client.openSecureMail(await response.json(), key);
6138
+ raw.fill(0);
6139
+ const claim = await client.secureMailClaim(key, recipientPublicKey);
6140
+ const claimed = await mail(`${path2}/claim`, { method: "POST", body: JSON.stringify(claim) });
6141
+ return await claimed.json();
6142
+ } finally {
6143
+ key.fill(0);
6144
+ }
6145
+ }
6146
+
6014
6147
  // src/operations/workspaces.ts
6015
6148
  var workspaces_exports = {};
6016
6149
  __export(workspaces_exports, {
@@ -9648,7 +9781,6 @@ __export(mail_exports, {
9648
9781
  NATIVE_ACCOUNT: () => NATIVE_ACCOUNT,
9649
9782
  attachmentFromBytes: () => attachmentFromBytes,
9650
9783
  claimNativeAlias: () => claimNativeAlias,
9651
- claimSecureMail: () => claimSecureMail,
9652
9784
  createMailFetch: () => createMailFetch,
9653
9785
  downloadAttachment: () => downloadAttachment,
9654
9786
  getMailPolicy: () => getMailPolicy,
@@ -9754,34 +9886,15 @@ async function unlockNativeMail(mail, keys) {
9754
9886
  secretKey.fill(0);
9755
9887
  }
9756
9888
  }
9757
- async function claimSecureMail(mail, id, fragmentKey, recipientPublicKey) {
9758
- const key = client.decodeSecureMailKey(fragmentKey);
9759
- try {
9760
- const path2 = `/secure/${encodeURIComponent(id)}`;
9761
- const { raw } = await client.openSecureMail(await json(mail(path2)), key);
9762
- raw.fill(0);
9763
- const claim = await client.secureMailClaim(key, recipientPublicKey);
9764
- return await json(mail(`${path2}/claim`, { method: "POST", body: JSON.stringify(claim) }));
9765
- } finally {
9766
- key.fill(0);
9767
- }
9768
- }
9769
9889
  function attachmentFromBytes(name, contentType, bytes) {
9770
9890
  return {
9771
9891
  id: "",
9772
9892
  name,
9773
9893
  content_type: contentType,
9774
9894
  size: bytes.length,
9775
- content_base64: bytesToBase643(bytes)
9895
+ content_base64: bytesToBase64(bytes)
9776
9896
  };
9777
9897
  }
9778
- function bytesToBase643(bytes) {
9779
- let binary = "";
9780
- for (let i2 = 0; i2 < bytes.length; i2 += 32768) {
9781
- binary += String.fromCharCode(...bytes.subarray(i2, i2 + 32768));
9782
- }
9783
- return btoa(binary);
9784
- }
9785
9898
 
9786
9899
  // src/operations/calendar.ts
9787
9900
  var calendar_exports = {};
@@ -12060,6 +12173,42 @@ var Arbi = class {
12060
12173
  });
12061
12174
  }
12062
12175
  };
12176
+ // ── Secure mail ─────────────────────────────────────────────────────
12177
+ /**
12178
+ * Mail to anyone, sealed in this browser under a key that travels only in the link's fragment
12179
+ * (see `operations/secure-mail`). Both sides use the signed-in user's encryption key: the
12180
+ * login's, or (a new tab not signed in to the server yet) the one kept on this device.
12181
+ */
12182
+ secureMail = {
12183
+ /**
12184
+ * The `/secure/send` payload for `compose` from `account`, shown with the user's own name.
12185
+ * Links open on `frontendOrigin` (default: this page's origin).
12186
+ */
12187
+ prepare: async (compose, account, requestId, frontendOrigin = globalThis.location?.origin ?? "") => {
12188
+ const user = this.client?.session.getState().user;
12189
+ const name = user ? [user.given_name, user.family_name] : [this.userProfile?.givenName, this.userProfile?.familyName];
12190
+ const senderName = name.filter(Boolean).join(" ");
12191
+ return prepareSecureMail(
12192
+ compose,
12193
+ { ...account, display_name: senderName || account.display_name },
12194
+ { requestId, senderPublicKey: await this.encryptionPublicKey(), frontendOrigin }
12195
+ );
12196
+ },
12197
+ /**
12198
+ * Claim the secure message `id` with the key from its link into the user's native inbox, over
12199
+ * `mail` (default: this session's mail transport). Resolves to the message's id there.
12200
+ */
12201
+ claim: async (id, fragmentKey, mail = this.mail.fetch) => claimSecureMail(mail, id, fragmentKey, await this.encryptionPublicKey())
12202
+ };
12203
+ /** The signed-in user's X25519 public key (from the identity key; its secret half is wiped). */
12204
+ async encryptionPublicKey() {
12205
+ const signingKey = this.loginResult?.signingPrivateKey ?? (await this.client?.storage.getSession().catch(() => null))?.signingPrivateKey;
12206
+ if (!signingKey) throw new ArbiError("Sign in again to unlock your encryption key");
12207
+ await client.initSodium();
12208
+ const pair = client.encryptionKeypairFromSigningKey(signingKey);
12209
+ pair.secretKey.fill(0);
12210
+ return pair.publicKey;
12211
+ }
12063
12212
  async openResponseHandle(open, options, id, bufferEvents = false) {
12064
12213
  const controller = new AbortController();
12065
12214
  const abort = () => controller.abort();
@@ -14034,6 +14183,7 @@ exports.resolveSummary = resolveSummary;
14034
14183
  exports.resolveWorkspace = resolveWorkspace;
14035
14184
  exports.responses = responses_exports;
14036
14185
  exports.searchChunks = searchChunks;
14186
+ exports.secureMail = secure_mail_exports;
14037
14187
  exports.selectFullContextDocument = selectFullContextDocument;
14038
14188
  exports.selectWorkspace = selectWorkspace;
14039
14189
  exports.selectWorkspaceById = selectWorkspaceById;