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