@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/index.cjs CHANGED
@@ -6576,6 +6576,139 @@ async function unlockModelConnections(arbi, keys, onlyId) {
6576
6576
  }
6577
6577
  }
6578
6578
 
6579
+ // src/operations/secure-mail.ts
6580
+ var secure_mail_exports = {};
6581
+ __export(secure_mail_exports, {
6582
+ claimSecureMail: () => claimSecureMail,
6583
+ prepareSecureMail: () => prepareSecureMail,
6584
+ secureMime: () => secureMime
6585
+ });
6586
+
6587
+ // src/operations/mail-encoding.ts
6588
+ function bytesToBase64(bytes) {
6589
+ let binary = "";
6590
+ for (let i2 = 0; i2 < bytes.length; i2 += 32768) {
6591
+ binary += String.fromCharCode(...bytes.subarray(i2, i2 + 32768));
6592
+ }
6593
+ return btoa(binary);
6594
+ }
6595
+ var MAX_MESSAGE_BYTES = 25 * 1024 * 1024;
6596
+ var utf8 = new TextEncoder();
6597
+ var encodedWord = (value) => `=?UTF-8?B?${bytesToBase64(utf8.encode(value))}?=`;
6598
+ var folded = (value) => value.match(/.{1,76}/g)?.join("\r\n") ?? "";
6599
+ var safeHeader = (value) => {
6600
+ if (/[\r\n\0]/.test(value)) throw new Error("Email headers cannot contain newlines");
6601
+ return value;
6602
+ };
6603
+ function secureMime(compose, sender) {
6604
+ const address = (r2) => `${r2.name ? encodedWord(safeHeader(r2.name)) + " " : ""}<${safeHeader(r2.email)}>`;
6605
+ const boundary = "arbi-" + crypto.randomUUID();
6606
+ const headers = [
6607
+ `From: ${address({ email: sender.email, name: sender.display_name })}`,
6608
+ ...compose.to.length ? [`To: ${compose.to.map(address).join(", ")}`] : [],
6609
+ ...compose.cc.length ? [`Cc: ${compose.cc.map(address).join(", ")}`] : [],
6610
+ `Subject: ${encodedWord(safeHeader(compose.subject))}`,
6611
+ `Date: ${(/* @__PURE__ */ new Date()).toUTCString()}`,
6612
+ `Message-ID: <${crypto.randomUUID()}@${sender.email.split("@")[1]}>`,
6613
+ ...compose.in_reply_to ? [`In-Reply-To: ${safeHeader(compose.in_reply_to)}`] : [],
6614
+ ...compose.references?.length ? [`References: ${compose.references.map(safeHeader).join(" ")}`] : [],
6615
+ "MIME-Version: 1.0",
6616
+ "X-ARBI-Secure-Delivery: 1",
6617
+ `Content-Type: multipart/mixed; boundary="${boundary}"`
6618
+ ];
6619
+ const alternative = "alternative-" + crypto.randomUUID();
6620
+ const plainPart = `Content-Type: text/plain; charset="utf-8"\r
6621
+ Content-Transfer-Encoding: base64\r
6622
+ \r
6623
+ ${folded(bytesToBase64(utf8.encode(compose.text)))}`;
6624
+ const bodyPart = compose.html ? `Content-Type: multipart/alternative; boundary="${alternative}"\r
6625
+ \r
6626
+ --${alternative}\r
6627
+ ${plainPart}\r
6628
+ --${alternative}\r
6629
+ Content-Type: text/html; charset="utf-8"\r
6630
+ Content-Transfer-Encoding: base64\r
6631
+ \r
6632
+ ${folded(bytesToBase64(utf8.encode(compose.html)))}\r
6633
+ --${alternative}--\r
6634
+ ` : plainPart;
6635
+ const parts = [
6636
+ `--${boundary}\r
6637
+ ${bodyPart}`,
6638
+ ...compose.attachments.map((a2) => {
6639
+ if (!a2.content_base64) throw new Error("Attachment content is missing");
6640
+ const name = encodeURIComponent(safeHeader(a2.name)).replace(/'/g, "%27");
6641
+ return `--${boundary}\r
6642
+ Content-Type: ${safeHeader(a2.content_type || "application/octet-stream")}\r
6643
+ Content-Disposition: attachment; filename*=UTF-8''${name}\r
6644
+ Content-Transfer-Encoding: base64\r
6645
+ \r
6646
+ ${folded(a2.content_base64)}`;
6647
+ }),
6648
+ `--${boundary}--\r
6649
+ `
6650
+ ];
6651
+ return utf8.encode(headers.join("\r\n") + "\r\n\r\n" + parts.join("\r\n"));
6652
+ }
6653
+ async function prepareSecureMail(compose, sender, { requestId, senderPublicKey, frontendOrigin }) {
6654
+ const raw = secureMime(compose, sender);
6655
+ try {
6656
+ if (raw.length > MAX_MESSAGE_BYTES) throw new Error("Email exceeds the message size limit");
6657
+ const metadata = {
6658
+ secure_delivery: true,
6659
+ id: "",
6660
+ subject: compose.subject,
6661
+ sender: [{ email: sender.email, name: sender.display_name }],
6662
+ to: compose.to,
6663
+ cc: compose.cc,
6664
+ bcc: [],
6665
+ received_at: (/* @__PURE__ */ new Date()).toISOString(),
6666
+ preview: compose.text.slice(0, 220),
6667
+ unread: false,
6668
+ text: "",
6669
+ references: compose.references ?? [],
6670
+ attachments: compose.attachments.map((a2, i2) => ({
6671
+ ...a2,
6672
+ id: String(i2),
6673
+ content_base64: null
6674
+ }))
6675
+ };
6676
+ const recipients = [
6677
+ ...new Set([...compose.to, ...compose.cc, ...compose.bcc].map((r2) => r2.email.toLowerCase()))
6678
+ ];
6679
+ const sealed = await client.sealSecureMail(raw, metadata, senderPublicKey);
6680
+ return {
6681
+ request_id: requestId,
6682
+ draft_id: compose.draft_id,
6683
+ recipients: recipients.map((email) => ({
6684
+ id: crypto.randomUUID(),
6685
+ email,
6686
+ fragment_key: sealed.fragment_key
6687
+ })),
6688
+ ciphertext: sealed.ciphertext,
6689
+ metadata_ciphertext: sealed.metadata_ciphertext,
6690
+ sender_wrapped_key: sealed.sender_wrapped_key,
6691
+ frontend_origin: frontendOrigin
6692
+ };
6693
+ } finally {
6694
+ raw.fill(0);
6695
+ }
6696
+ }
6697
+ async function claimSecureMail(mail, id, fragmentKey, recipientPublicKey) {
6698
+ const key = client.decodeSecureMailKey(fragmentKey);
6699
+ try {
6700
+ const path5 = `/secure/${encodeURIComponent(id)}`;
6701
+ const response = await mail(path5);
6702
+ const { raw } = await client.openSecureMail(await response.json(), key);
6703
+ raw.fill(0);
6704
+ const claim = await client.secureMailClaim(key, recipientPublicKey);
6705
+ const claimed = await mail(`${path5}/claim`, { method: "POST", body: JSON.stringify(claim) });
6706
+ return await claimed.json();
6707
+ } finally {
6708
+ key.fill(0);
6709
+ }
6710
+ }
6711
+
6579
6712
  // src/operations/workspaces.ts
6580
6713
  var workspaces_exports = {};
6581
6714
  __export(workspaces_exports, {
@@ -10234,7 +10367,6 @@ __export(mail_exports, {
10234
10367
  NATIVE_ACCOUNT: () => NATIVE_ACCOUNT,
10235
10368
  attachmentFromBytes: () => attachmentFromBytes,
10236
10369
  claimNativeAlias: () => claimNativeAlias,
10237
- claimSecureMail: () => claimSecureMail,
10238
10370
  createMailFetch: () => createMailFetch,
10239
10371
  downloadAttachment: () => downloadAttachment,
10240
10372
  getMailPolicy: () => getMailPolicy,
@@ -10340,34 +10472,15 @@ async function unlockNativeMail(mail, keys) {
10340
10472
  secretKey.fill(0);
10341
10473
  }
10342
10474
  }
10343
- async function claimSecureMail(mail, id, fragmentKey, recipientPublicKey) {
10344
- const key = client.decodeSecureMailKey(fragmentKey);
10345
- try {
10346
- const path5 = `/secure/${encodeURIComponent(id)}`;
10347
- const { raw } = await client.openSecureMail(await json(mail(path5)), key);
10348
- raw.fill(0);
10349
- const claim = await client.secureMailClaim(key, recipientPublicKey);
10350
- return await json(mail(`${path5}/claim`, { method: "POST", body: JSON.stringify(claim) }));
10351
- } finally {
10352
- key.fill(0);
10353
- }
10354
- }
10355
10475
  function attachmentFromBytes(name, contentType, bytes) {
10356
10476
  return {
10357
10477
  id: "",
10358
10478
  name,
10359
10479
  content_type: contentType,
10360
10480
  size: bytes.length,
10361
- content_base64: bytesToBase643(bytes)
10481
+ content_base64: bytesToBase64(bytes)
10362
10482
  };
10363
10483
  }
10364
- function bytesToBase643(bytes) {
10365
- let binary = "";
10366
- for (let i2 = 0; i2 < bytes.length; i2 += 32768) {
10367
- binary += String.fromCharCode(...bytes.subarray(i2, i2 + 32768));
10368
- }
10369
- return btoa(binary);
10370
- }
10371
10484
 
10372
10485
  // src/operations/calendar.ts
10373
10486
  var calendar_exports = {};
@@ -12646,6 +12759,42 @@ var Arbi = class {
12646
12759
  });
12647
12760
  }
12648
12761
  };
12762
+ // ── Secure mail ─────────────────────────────────────────────────────
12763
+ /**
12764
+ * Mail to anyone, sealed in this browser under a key that travels only in the link's fragment
12765
+ * (see `operations/secure-mail`). Both sides use the signed-in user's encryption key: the
12766
+ * login's, or (a new tab not signed in to the server yet) the one kept on this device.
12767
+ */
12768
+ secureMail = {
12769
+ /**
12770
+ * The `/secure/send` payload for `compose` from `account`, shown with the user's own name.
12771
+ * Links open on `frontendOrigin` (default: this page's origin).
12772
+ */
12773
+ prepare: async (compose, account, requestId, frontendOrigin = globalThis.location?.origin ?? "") => {
12774
+ const user = this.client?.session.getState().user;
12775
+ const name = user ? [user.given_name, user.family_name] : [this.userProfile?.givenName, this.userProfile?.familyName];
12776
+ const senderName = name.filter(Boolean).join(" ");
12777
+ return prepareSecureMail(
12778
+ compose,
12779
+ { ...account, display_name: senderName || account.display_name },
12780
+ { requestId, senderPublicKey: await this.encryptionPublicKey(), frontendOrigin }
12781
+ );
12782
+ },
12783
+ /**
12784
+ * Claim the secure message `id` with the key from its link into the user's native inbox, over
12785
+ * `mail` (default: this session's mail transport). Resolves to the message's id there.
12786
+ */
12787
+ claim: async (id, fragmentKey, mail = this.mail.fetch) => claimSecureMail(mail, id, fragmentKey, await this.encryptionPublicKey())
12788
+ };
12789
+ /** The signed-in user's X25519 public key (from the identity key; its secret half is wiped). */
12790
+ async encryptionPublicKey() {
12791
+ const signingKey = this.loginResult?.signingPrivateKey ?? (await this.client?.storage.getSession().catch(() => null))?.signingPrivateKey;
12792
+ if (!signingKey) throw new ArbiError("Sign in again to unlock your encryption key");
12793
+ await client.initSodium();
12794
+ const pair = client.encryptionKeypairFromSigningKey(signingKey);
12795
+ pair.secretKey.fill(0);
12796
+ return pair.publicKey;
12797
+ }
12649
12798
  async openResponseHandle(open, options, id, bufferEvents = false) {
12650
12799
  const controller = new AbortController();
12651
12800
  const abort = () => controller.abort();
@@ -15713,6 +15862,7 @@ exports.resolveSummary = resolveSummary;
15713
15862
  exports.resolveWorkspace = resolveWorkspace;
15714
15863
  exports.responses = responses_exports;
15715
15864
  exports.searchChunks = searchChunks;
15865
+ exports.secureMail = secure_mail_exports;
15716
15866
  exports.selectFullContextDocument = selectFullContextDocument;
15717
15867
  exports.selectWorkspace = selectWorkspace;
15718
15868
  exports.selectWorkspaceById = selectWorkspaceById;