@arbidocs/sdk 0.5.69 → 0.5.70

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