@open-workspace/email 0.4.2 → 0.5.1
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/README.md +9 -4
- package/alchemy/index.ts +130 -2
- package/drizzle/mailbox/0003_after_0_4_2.sql +180 -0
- package/drizzle/mailbox/meta/0003_snapshot.json +1507 -0
- package/drizzle/mailbox/meta/_journal.json +7 -0
- package/drizzle/mailbox/migrations.js +3 -1
- package/migrations/0002_after_0_4_2.sql +35 -0
- package/migrations/meta/0002_snapshot.json +807 -0
- package/migrations/meta/_journal.json +7 -0
- package/package.json +23 -7
- package/shared/attachment-preview.ts +18 -8
- package/shared/folder-order.ts +31 -6
- package/shared/mail-headers.ts +3 -3
- package/shared/mail-html.ts +25 -0
- package/shared/mail-import.ts +0 -7
- package/shared/mail-received.ts +4 -4
- package/shared/mail-rule-actions.ts +47 -0
- package/shared/mail-sender-intel.ts +5 -1
- package/shared/mail.ts +70 -4
- package/shared/office-preview.ts +0 -4
- package/shared/pdfjs-assets.ts +1 -3
- package/shared/rich-content.ts +35 -0
- package/shared/signature.ts +15 -0
- package/src/admin/address-auto-reply.tsx +52 -0
- package/src/admin/blocked-senders.tsx +86 -66
- package/src/admin/forward-destinations.tsx +140 -0
- package/src/admin/mail-archive.tsx +134 -0
- package/src/admin/team-signature.tsx +90 -0
- package/src/client.tsx +130 -16
- package/src/mail/attachment-preview-dialog.tsx +13 -5
- package/src/mail/auto-reply-banner.tsx +61 -0
- package/src/mail/blocked-senders.tsx +124 -91
- package/src/mail/compose-window-chrome.tsx +20 -8
- package/src/mail/compose-window.tsx +391 -365
- package/src/mail/composer-asset-bookmarks.ts +47 -12
- package/src/mail/composer-attachment-list.tsx +42 -17
- package/src/mail/composer-editor-document.ts +54 -10
- package/src/mail/composer-editor.tsx +44 -44
- package/src/mail/composer-link-text.ts +42 -0
- package/src/mail/composer-recipient-fields.tsx +25 -16
- package/src/mail/composer-recipients.ts +62 -16
- package/src/mail/composer-session.ts +107 -23
- package/src/mail/composer-toolbar.tsx +15 -1
- package/src/mail/composer-upload-client.ts +29 -1
- package/src/mail/composer-upload-progress.ts +2 -2
- package/src/mail/conversation-actions-model.ts +56 -0
- package/src/mail/conversation-list.tsx +263 -128
- package/src/mail/conversation-loading-skeleton.tsx +165 -90
- package/src/mail/conversation-message.tsx +257 -102
- package/src/mail/conversation-order.ts +12 -0
- package/src/mail/conversation-reply.tsx +313 -189
- package/src/mail/conversation-selection-toolbar.tsx +188 -143
- package/src/mail/conversation-view.tsx +315 -85
- package/src/mail/delivery-indicator.tsx +33 -12
- package/src/mail/draft-attachments.ts +194 -0
- package/src/mail/email-html-body.tsx +97 -53
- package/src/mail/folder-cleanup-bar.tsx +193 -0
- package/src/mail/folder-glyph.tsx +5 -0
- package/src/mail/folder-manager.tsx +71 -38
- package/src/mail/mail-app-bar.tsx +15 -3
- package/src/mail/mail-dock.tsx +19 -3
- package/src/mail/mail-focus.ts +48 -0
- package/src/mail/mail-folders.ts +14 -2
- package/src/mail/mail-layout.css +20 -0
- package/src/mail/mail-mailbox-sheet.tsx +22 -69
- package/src/mail/mail-nav-rail.tsx +7 -4
- package/src/mail/mail-nav.tsx +66 -46
- package/src/mail/mail-prefetch.ts +30 -9
- package/src/mail/mail-primitives.tsx +6 -4
- package/src/mail/mail-queries.ts +122 -20
- package/src/mail/mail-query-cache.ts +1085 -173
- package/src/mail/mail-search-params.ts +35 -0
- package/src/mail/mail-shell.tsx +32 -9
- package/src/mail/mailbox-ai-settings.tsx +56 -42
- package/src/mail/mailbox-realtime.tsx +4 -12
- package/src/mail/mailbox-rules.tsx +898 -0
- package/src/mail/message-actions.tsx +38 -26
- package/src/mail/message-attachments.tsx +20 -12
- package/src/mail/message-body.ts +53 -0
- package/src/mail/message-format.ts +8 -2
- package/src/mail/message-header.tsx +3 -1
- package/src/mail/message-translation.tsx +34 -67
- package/src/mail/original-message-sheet.tsx +63 -50
- package/src/mail/original-message.ts +6 -6
- package/src/mail/pane-splitter.tsx +3 -3
- package/src/mail/pdf-preview.tsx +70 -62
- package/src/mail/plain-message-text.tsx +28 -0
- package/src/mail/print-message.ts +31 -36
- package/src/mail/reader-pane.tsx +134 -103
- package/src/mail/recipient-combobox.tsx +52 -29
- package/src/mail/recipient-field-row.tsx +3 -0
- package/src/mail/rich-content.ts +128 -0
- package/src/mail/sanitize-email-html.ts +53 -32
- package/src/mail/sender-actions.tsx +48 -46
- package/src/mail/sender-authentication.tsx +1 -1
- package/src/mail/sender-dialogs.tsx +67 -43
- package/src/mail/sender-security.tsx +12 -11
- package/src/mail/signature.ts +43 -0
- package/src/mail/translation-html.ts +3 -3
- package/src/mail/types.ts +12 -0
- package/src/mail/use-composer-attachment-preflight.ts +23 -60
- package/src/mail/use-composer-core.ts +286 -0
- package/src/mail/use-composer-navigation.ts +120 -0
- package/src/mail/use-composer-submission.ts +154 -19
- package/src/mail/use-conversation-actions.ts +292 -0
- package/src/mail/use-conversation-position.ts +51 -0
- package/src/mail/use-conversation-selection.ts +192 -89
- package/src/mail/use-draft-autosave.ts +394 -0
- package/src/mail/use-drafts.ts +194 -0
- package/src/mail/use-mail-controller.ts +758 -213
- package/src/mail/use-mail-data.ts +210 -100
- package/src/mail/use-mail-safety.ts +19 -38
- package/src/mail/use-mail-send.ts +18 -6
- package/src/mail/use-pdf-translation.ts +1 -1
- package/src/mail/use-reply-cc.ts +61 -0
- package/src/mail/use-visible-message-read.ts +128 -64
- package/src/pwa/push-key.ts +18 -0
- package/src/pwa/push-subscription.ts +88 -26
- package/src/settings/auto-reply-editor.tsx +454 -0
- package/src/settings/auto-reply-page.tsx +59 -0
- package/src/settings/mail-apps-page.tsx +52 -43
- package/src/settings/notifications-page.tsx +82 -67
- package/src/settings/rich-content-editor.tsx +182 -0
- package/src/settings/settings-queries.ts +57 -0
- package/src/settings/signature-editor.tsx +142 -0
- package/src/settings/signature-page.tsx +70 -0
- package/worker/admin/blocked-sender-routes.ts +10 -16
- package/worker/backups/eml.ts +185 -0
- package/worker/backups/file-parts.ts +140 -0
- package/worker/backups/mail-backup.ts +644 -0
- package/worker/backups/settings-backup.ts +866 -0
- package/worker/backups/stream-eml.ts +73 -0
- package/worker/db/schema.ts +89 -5
- package/worker/jmap/attachment-blobs.ts +113 -0
- package/worker/jmap/blobs.ts +162 -0
- package/worker/jmap/draft-content.ts +42 -0
- package/worker/jmap/drafts.ts +70 -0
- package/worker/jmap/email-body.ts +152 -0
- package/worker/jmap/email-methods.ts +1223 -0
- package/worker/jmap/eventsource.ts +9 -8
- package/worker/jmap/import-methods.ts +205 -0
- package/worker/jmap/mailbox-methods.ts +187 -0
- package/worker/jmap/method-core.ts +379 -0
- package/worker/jmap/methods.ts +119 -1816
- package/worker/jmap/push-subscription-methods.ts +373 -0
- package/worker/jmap/push-types.ts +46 -2
- package/worker/jmap/push.ts +24 -31
- package/worker/jmap/routes.ts +144 -153
- package/worker/jmap/submission-methods.ts +395 -0
- package/worker/mail/access.ts +35 -12
- package/worker/mail/ai-classification.ts +2 -2
- package/worker/mail/archive-object.ts +96 -0
- package/worker/mail/archive-routes.ts +35 -0
- package/worker/mail/archive.ts +700 -0
- package/worker/mail/auth-headers.ts +57 -43
- package/worker/mail/auto-reply-routes.ts +260 -0
- package/worker/mail/auto-reply.ts +243 -0
- package/worker/mail/bimi.ts +53 -36
- package/worker/mail/bootstrap.ts +11 -3
- package/worker/mail/capabilities.ts +69 -0
- package/worker/mail/classification-signals.ts +1 -1
- package/worker/mail/confusables-data.ts +9 -0
- package/worker/mail/conversation-cursor.ts +6 -15
- package/worker/mail/delivery-events.ts +9 -7
- package/worker/mail/delivery-facts.ts +1 -1
- package/worker/mail/demo-routes.ts +3 -3
- package/worker/mail/doh.ts +3 -3
- package/worker/mail/download-links.ts +153 -0
- package/worker/mail/downloads.ts +2 -6
- package/worker/mail/draft-routes.ts +473 -0
- package/worker/mail/draft-sender.ts +54 -0
- package/worker/mail/edge-cache.ts +41 -55
- package/worker/mail/email-routing.ts +123 -0
- package/worker/mail/forward-destinations.ts +114 -0
- package/worker/mail/forward-downloads.ts +115 -0
- package/worker/mail/forward-html.ts +85 -0
- package/worker/mail/header-metadata.ts +2 -5
- package/worker/mail/impersonation.ts +102 -101
- package/worker/mail/import-routes.ts +29 -82
- package/worker/mail/import.ts +130 -0
- package/worker/mail/inbound-delivery.ts +6 -5
- package/worker/mail/inbound-threading.ts +15 -2
- package/worker/mail/inbound.ts +58 -28
- package/worker/mail/ip-intel.ts +8 -17
- package/worker/mail/isolate-cache.ts +1 -1
- package/worker/mail/mailbox-deletion.ts +33 -12
- package/worker/mail/mailbox-directory.ts +39 -65
- package/worker/mail/mailing-list.ts +29 -58
- package/worker/mail/message-security.ts +19 -8
- package/worker/mail/message-view.ts +12 -10
- package/worker/mail/office-conversion.ts +3 -5
- package/worker/mail/office-preview-container.ts +3 -8
- package/worker/mail/outbound-content.ts +139 -65
- package/worker/mail/outbound-delivery.ts +20 -17
- package/worker/mail/outbound-service.ts +34 -9
- package/worker/mail/outbound.ts +146 -75
- package/worker/mail/outgoing-context.ts +60 -30
- package/worker/mail/participants.ts +10 -15
- package/worker/mail/plain-body.ts +39 -0
- package/worker/mail/push-endpoint.ts +17 -0
- package/worker/mail/push-notifications.ts +53 -62
- package/worker/mail/quote-parser.ts +36 -24
- package/worker/mail/receive.ts +146 -39
- package/worker/mail/recipients.ts +1 -1
- package/worker/mail/remote-proxy.ts +3 -3
- package/worker/mail/repeat.ts +1 -1
- package/worker/mail/reply-plan.ts +124 -26
- package/worker/mail/rfc.ts +15 -7
- package/worker/mail/rich-content.ts +207 -0
- package/worker/mail/routes.ts +698 -304
- package/worker/mail/routing.ts +274 -0
- package/worker/mail/rules.ts +230 -0
- package/worker/mail/safety-routes.ts +88 -86
- package/worker/mail/schemas.ts +222 -31
- package/worker/mail/search.ts +25 -2
- package/worker/mail/security-settings.ts +4 -10
- package/worker/mail/send-message.ts +238 -0
- package/worker/mail/sender-intel.ts +12 -20
- package/worker/mail/signature-routes.ts +206 -0
- package/worker/mail/signatures.ts +195 -0
- package/worker/mail/spamhaus.ts +28 -16
- package/worker/mail/storage-keys.ts +35 -0
- package/worker/mail/unsubscribe.ts +19 -41
- package/worker/mail/uploads.ts +192 -80
- package/worker/mail/webhook-source.ts +3 -2
- package/worker/mail/workspace-safety.ts +194 -60
- package/worker/mailbox/demo.ts +13 -15
- package/worker/mailbox/email-index.ts +65 -35
- package/worker/mailbox/folder-model.ts +0 -20
- package/worker/mailbox/import-placement.ts +21 -36
- package/worker/mailbox/inbound-decision.ts +114 -0
- package/worker/mailbox/inbox-counts.ts +45 -25
- package/worker/mailbox/index.ts +756 -3201
- package/worker/mailbox/jmap-change-log.ts +195 -0
- package/worker/mailbox/jmap-email-rows.ts +186 -0
- package/worker/mailbox/jmap-mailboxes.ts +116 -0
- package/worker/mailbox/jmap-placement.ts +139 -0
- package/worker/mailbox/jmap-query-filter.ts +231 -0
- package/worker/mailbox/jmap-store.ts +167 -299
- package/worker/mailbox/mailbox-auto-reply.ts +316 -0
- package/worker/mailbox/mailbox-backup.ts +538 -0
- package/worker/mailbox/mailbox-drafts.ts +417 -0
- package/worker/mailbox/mailbox-folders.ts +242 -0
- package/worker/mailbox/mailbox-import.ts +455 -0
- package/worker/mailbox/mailbox-inbound.ts +934 -0
- package/worker/mailbox/mailbox-jmap.ts +294 -0
- package/worker/mailbox/mailbox-outbound.ts +413 -0
- package/worker/mailbox/mailbox-rules.ts +336 -0
- package/worker/mailbox/mailbox-senders.ts +294 -0
- package/worker/mailbox/mailbox-storage.ts +438 -0
- package/worker/mailbox/mailbox-views.ts +891 -0
- package/worker/mailbox/model.ts +1 -0
- package/worker/mailbox/office-preview-jobs.ts +59 -47
- package/worker/mailbox/placement.ts +933 -0
- package/worker/mailbox/restore-thread.ts +52 -0
- package/worker/mailbox/retention.ts +36 -0
- package/worker/mailbox/schema.ts +200 -1
- package/worker/mailbox/senders.ts +216 -178
- package/worker/mailbox/spam-undo-filing.ts +138 -0
- package/worker/mailbox/spam-undo.ts +253 -0
- package/worker/mailbox/support.ts +416 -0
- package/worker/mcp/mail-action-tools.ts +161 -1
- package/worker/mcp/mail-compose-tools.ts +7 -1
- package/worker/mcp/mail-safety-tools.ts +4 -4
- package/worker/module.ts +23 -4
- package/src/mail/use-compose-submission.ts +0 -208
- package/src/mail/use-reply-submission.ts +0 -114
- package/worker/mail/capped-body.ts +0 -36
|
@@ -2,14 +2,12 @@ import {
|
|
|
2
2
|
zValidator,
|
|
3
3
|
oauthScope,
|
|
4
4
|
createDb,
|
|
5
|
-
configuredMailDomains,
|
|
6
|
-
mailboxes,
|
|
7
|
-
users,
|
|
8
5
|
apiError,
|
|
9
6
|
requireAuth,
|
|
10
7
|
requirePermission,
|
|
11
8
|
} from "@open-workspace/core/module";
|
|
12
9
|
import type { AppEnv } from "@open-workspace/core/module";
|
|
10
|
+
import { mailboxes, users } from "@open-workspace/core/schema";
|
|
13
11
|
import { desc, eq } from "drizzle-orm";
|
|
14
12
|
import { Hono } from "hono";
|
|
15
13
|
|
|
@@ -19,7 +17,10 @@ import {
|
|
|
19
17
|
MANAGE_BLOCKED_SENDERS,
|
|
20
18
|
} from "../../shared/permissions";
|
|
21
19
|
import { blockedSenders } from "../db/schema";
|
|
22
|
-
import {
|
|
20
|
+
import {
|
|
21
|
+
blocksOwnMailDomain,
|
|
22
|
+
forgetWorkspaceBlockPatterns,
|
|
23
|
+
} from "../mail/workspace-safety";
|
|
23
24
|
import { mailboxStub } from "../mailbox";
|
|
24
25
|
import { workspaceBlockQuerySchema, workspaceBlockSchema } from "./schemas";
|
|
25
26
|
|
|
@@ -27,9 +28,11 @@ const MAILBOX_FANOUT = 20;
|
|
|
27
28
|
|
|
28
29
|
/** Moves existing mail from a newly blocked pattern to Spam in every mailbox. */
|
|
29
30
|
async function moveBlockedMailToSpam(env: Env, pattern: string) {
|
|
31
|
+
// Only mailboxes keep mail; lists, aliases and forwards have none.
|
|
30
32
|
const rows = await createDb(env.DB)
|
|
31
33
|
.select({ id: mailboxes.id })
|
|
32
|
-
.from(mailboxes)
|
|
34
|
+
.from(mailboxes)
|
|
35
|
+
.where(eq(mailboxes.delivery, "inbox"));
|
|
33
36
|
|
|
34
37
|
let conversations = 0;
|
|
35
38
|
let affectedMailboxes = 0;
|
|
@@ -106,17 +109,8 @@ export const blockedSenderAdminRoutes = new Hono<AppEnv>()
|
|
|
106
109
|
}
|
|
107
110
|
|
|
108
111
|
const db = createDb(c.env.DB);
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
if (
|
|
113
|
-
own.some(
|
|
114
|
-
(name) =>
|
|
115
|
-
domain === name ||
|
|
116
|
-
domain.endsWith(`.${name}`) ||
|
|
117
|
-
name.endsWith(`.${domain}`)
|
|
118
|
-
)
|
|
119
|
-
) {
|
|
112
|
+
|
|
113
|
+
if (blocksOwnMailDomain(pattern)) {
|
|
120
114
|
return apiError(
|
|
121
115
|
c,
|
|
122
116
|
400,
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import type { MailAddress } from "../mailbox/model";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A small RFC 5322 / MIME writer for mail that has no stored raw MIME:
|
|
5
|
+
* outgoing mail goes to Email Sending as structured fields. The same input
|
|
6
|
+
* always gives the same bytes, so backup objects deduplicate by hash.
|
|
7
|
+
*/
|
|
8
|
+
export type EmlInput = {
|
|
9
|
+
id: string;
|
|
10
|
+
from: MailAddress[];
|
|
11
|
+
to: MailAddress[];
|
|
12
|
+
cc: MailAddress[];
|
|
13
|
+
bcc: MailAddress[];
|
|
14
|
+
replyTo: MailAddress[];
|
|
15
|
+
subject: string;
|
|
16
|
+
date: Date;
|
|
17
|
+
messageId: string | null;
|
|
18
|
+
inReplyTo: string[];
|
|
19
|
+
references: string[];
|
|
20
|
+
/** Other headers, as given to Email Sending (e.g. Auto-Submitted). */
|
|
21
|
+
headers?: Record<string, string>;
|
|
22
|
+
text?: string;
|
|
23
|
+
html?: string;
|
|
24
|
+
attachments: {
|
|
25
|
+
filename: string;
|
|
26
|
+
type: string;
|
|
27
|
+
disposition: "attachment" | "inline";
|
|
28
|
+
contentId?: string;
|
|
29
|
+
content: ArrayBuffer;
|
|
30
|
+
}[];
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const CRLF = "\r\n";
|
|
34
|
+
const encoder = new TextEncoder();
|
|
35
|
+
const ASCII = /^[ -~]*$/u;
|
|
36
|
+
|
|
37
|
+
/** One line: CR and LF never reach a header from stored values. */
|
|
38
|
+
function headerSafe(value: string) {
|
|
39
|
+
return value.replaceAll(/[\r\n]+/gu, " ");
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** RFC 2047 encoded words, split on code points to stay under 75 characters. */
|
|
43
|
+
export function encodeHeaderWord(value: string) {
|
|
44
|
+
const clean = headerSafe(value);
|
|
45
|
+
|
|
46
|
+
if (ASCII.test(clean)) return clean;
|
|
47
|
+
const words: string[] = [];
|
|
48
|
+
let chunk = "";
|
|
49
|
+
|
|
50
|
+
for (const character of clean) {
|
|
51
|
+
if (encoder.encode(chunk + character).byteLength > 45) {
|
|
52
|
+
words.push(chunk);
|
|
53
|
+
chunk = "";
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
chunk += character;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (chunk) words.push(chunk);
|
|
60
|
+
|
|
61
|
+
return words
|
|
62
|
+
.map((word) => `=?UTF-8?B?${encoder.encode(word).toBase64()}?=`)
|
|
63
|
+
.join(`${CRLF} `);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function formatAddress({ address, name }: MailAddress) {
|
|
67
|
+
if (!name) return headerSafe(address);
|
|
68
|
+
|
|
69
|
+
const display = ASCII.test(name)
|
|
70
|
+
? `"${headerSafe(name).replaceAll(/["\\]/gu, String.raw`\$&`)}"`
|
|
71
|
+
: encodeHeaderWord(name);
|
|
72
|
+
|
|
73
|
+
return `${display} <${headerSafe(address)}>`;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function base64Lines(bytes: Uint8Array) {
|
|
77
|
+
return bytes.toBase64().replaceAll(/.{1,76}/gu, `$&${CRLF}`);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function textPart(contentType: string, value: string) {
|
|
81
|
+
return [
|
|
82
|
+
`Content-Type: ${contentType}; charset=utf-8`,
|
|
83
|
+
"Content-Transfer-Encoding: base64",
|
|
84
|
+
"",
|
|
85
|
+
base64Lines(encoder.encode(value)),
|
|
86
|
+
].join(CRLF);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function multipart(subtype: string, boundary: string, parts: string[]) {
|
|
90
|
+
return [
|
|
91
|
+
`Content-Type: multipart/${subtype}; boundary="${boundary}"`,
|
|
92
|
+
"",
|
|
93
|
+
...parts.map((part) => `--${boundary}${CRLF}${part}`),
|
|
94
|
+
`--${boundary}--`,
|
|
95
|
+
"",
|
|
96
|
+
].join(CRLF);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function attachmentHeaders(
|
|
100
|
+
attachment: Omit<EmlInput["attachments"][number], "content">
|
|
101
|
+
) {
|
|
102
|
+
const filename = encodeHeaderWord(attachment.filename).replaceAll('"', "'");
|
|
103
|
+
|
|
104
|
+
const lines = [
|
|
105
|
+
`Content-Type: ${headerSafe(attachment.type)}; name="${filename}"`,
|
|
106
|
+
`Content-Disposition: ${attachment.disposition}; filename="${filename}"`,
|
|
107
|
+
];
|
|
108
|
+
|
|
109
|
+
if (attachment.contentId) {
|
|
110
|
+
const id = headerSafe(attachment.contentId).replaceAll(/^<|>$/gu, "");
|
|
111
|
+
lines.push(`Content-ID: <${id}>`);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return [...lines, "Content-Transfer-Encoding: base64", "", ""].join(CRLF);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
type EmlFields = Omit<EmlInput, "attachments">;
|
|
118
|
+
|
|
119
|
+
export function emlBody(input: EmlFields) {
|
|
120
|
+
const bodies: string[] = [];
|
|
121
|
+
|
|
122
|
+
if (input.text !== undefined) bodies.push(textPart("text/plain", input.text));
|
|
123
|
+
|
|
124
|
+
if (input.html !== undefined) bodies.push(textPart("text/html", input.html));
|
|
125
|
+
|
|
126
|
+
return bodies.length === 2
|
|
127
|
+
? multipart("alternative", `ow-alt-${input.id}`, bodies)
|
|
128
|
+
: (bodies[0] ?? textPart("text/plain", ""));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function emlHeaders(input: EmlFields) {
|
|
132
|
+
const headers: string[] = [];
|
|
133
|
+
|
|
134
|
+
const addresses = (name: string, list: MailAddress[]) => {
|
|
135
|
+
if (list.length) {
|
|
136
|
+
headers.push(`${name}: ${list.map(formatAddress).join(", ")}`);
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
addresses("From", input.from);
|
|
141
|
+
addresses("To", input.to);
|
|
142
|
+
addresses("Cc", input.cc);
|
|
143
|
+
addresses("Bcc", input.bcc);
|
|
144
|
+
addresses("Reply-To", input.replyTo);
|
|
145
|
+
headers.push(
|
|
146
|
+
`Subject: ${encodeHeaderWord(input.subject)}`,
|
|
147
|
+
`Date: ${input.date.toUTCString().replace("GMT", "+0000")}`
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
if (input.messageId) {
|
|
151
|
+
headers.push(`Message-ID: ${headerSafe(input.messageId)}`);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (input.inReplyTo.length) {
|
|
155
|
+
headers.push(`In-Reply-To: ${headerSafe(input.inReplyTo.join(" "))}`);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (input.references.length) {
|
|
159
|
+
headers.push(`References: ${headerSafe(input.references.join(" "))}`);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
for (const [name, value] of Object.entries(input.headers ?? {})) {
|
|
163
|
+
headers.push(`${headerSafe(name)}: ${headerSafe(value)}`);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
headers.push("MIME-Version: 1.0");
|
|
167
|
+
|
|
168
|
+
return headers.join(CRLF) + CRLF;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export function serializeEml(input: EmlInput) {
|
|
172
|
+
let body = emlBody(input);
|
|
173
|
+
|
|
174
|
+
if (input.attachments.length) {
|
|
175
|
+
body = multipart("mixed", `ow-mixed-${input.id}`, [
|
|
176
|
+
body,
|
|
177
|
+
...input.attachments.map(
|
|
178
|
+
(file) =>
|
|
179
|
+
attachmentHeaders(file) + base64Lines(new Uint8Array(file.content))
|
|
180
|
+
),
|
|
181
|
+
]);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return encoder.encode(emlHeaders(input) + body);
|
|
185
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/* oxlint-disable eslint/no-await-in-loop -- R2 file ranges are read and written in order with bounded memory. */
|
|
2
|
+
import type {
|
|
3
|
+
BackupRestoreItem,
|
|
4
|
+
BackupSnapshot,
|
|
5
|
+
} from "@open-workspace/core/module";
|
|
6
|
+
import { z } from "zod";
|
|
7
|
+
|
|
8
|
+
import { MAX_COMPOSER_ATTACHMENT_BYTES } from "../../shared/mail";
|
|
9
|
+
|
|
10
|
+
export const PART_BYTES = 8 * 1024 * 1024;
|
|
11
|
+
const binary = { extension: "bin", contentType: "application/octet-stream" };
|
|
12
|
+
const objectName = z.string().regex(/^[0-9a-f]{64}\.bin$/u);
|
|
13
|
+
|
|
14
|
+
export const backupFile = z
|
|
15
|
+
.object({
|
|
16
|
+
size: z.number().int().positive().max(MAX_COMPOSER_ATTACHMENT_BYTES),
|
|
17
|
+
parts: z
|
|
18
|
+
.array(z.object({ object: objectName }))
|
|
19
|
+
.min(1)
|
|
20
|
+
.max(Math.ceil(MAX_COMPOSER_ATTACHMENT_BYTES / PART_BYTES)),
|
|
21
|
+
})
|
|
22
|
+
.refine(
|
|
23
|
+
(file) => file.parts.length === Math.ceil(file.size / PART_BYTES),
|
|
24
|
+
"Invalid file part count"
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
export type BackupFile = z.infer<typeof backupFile>;
|
|
28
|
+
|
|
29
|
+
export const linkedAttachment = z.object({
|
|
30
|
+
downloadTokenHash: z.string().nullable(),
|
|
31
|
+
filename: z.string().min(1).max(255),
|
|
32
|
+
contentType: z.string().min(1).max(255),
|
|
33
|
+
file: backupFile,
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
export type LinkedAttachment = z.infer<typeof linkedAttachment>;
|
|
37
|
+
|
|
38
|
+
/** Read an immutable mail object a range at a time, keeping each request within the restore limit. */
|
|
39
|
+
export async function backupFileParts(
|
|
40
|
+
bucket: R2Bucket,
|
|
41
|
+
key: string,
|
|
42
|
+
snapshot: BackupSnapshot
|
|
43
|
+
) {
|
|
44
|
+
const head = await bucket.head(key);
|
|
45
|
+
|
|
46
|
+
if (!head || head.size < 1 || head.size > MAX_COMPOSER_ATTACHMENT_BYTES) {
|
|
47
|
+
throw new Error("Attachment is missing or has an invalid size");
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const parts: BackupFile["parts"] = [];
|
|
51
|
+
|
|
52
|
+
for (let offset = 0; offset < head.size; offset += PART_BYTES) {
|
|
53
|
+
const length = Math.min(PART_BYTES, head.size - offset);
|
|
54
|
+
|
|
55
|
+
const range = await bucket.get(key, {
|
|
56
|
+
range: { offset, length },
|
|
57
|
+
onlyIf: { etagMatches: head.etag },
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
if (!range || !("body" in range)) {
|
|
61
|
+
throw new Error("Attachment changed while backing up");
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const bytes = new Uint8Array(await range.arrayBuffer());
|
|
65
|
+
|
|
66
|
+
if (bytes.byteLength !== length) {
|
|
67
|
+
throw new Error("Incomplete attachment range");
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const hash = await snapshot.putObject(bytes, binary);
|
|
71
|
+
parts.push({ object: `${hash}.bin` });
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return { size: head.size, parts };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// The composer's staging prefix expires in R2. A retry can upload the same part again.
|
|
78
|
+
function stagedPart(userId: string, object: string) {
|
|
79
|
+
return `composer-uploads/backup-parts/${userId}/${object}`;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const partLine = z.object({
|
|
83
|
+
kind: z.literal("backup-part"),
|
|
84
|
+
object: objectName,
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
export async function stageBackupPart(
|
|
88
|
+
bucket: R2Bucket,
|
|
89
|
+
userId: string,
|
|
90
|
+
{ line, object: bytes }: BackupRestoreItem
|
|
91
|
+
) {
|
|
92
|
+
const part = partLine.safeParse(line).data;
|
|
93
|
+
|
|
94
|
+
if (!part) return null;
|
|
95
|
+
|
|
96
|
+
if (!bytes?.byteLength || bytes.byteLength > PART_BYTES) {
|
|
97
|
+
return { status: "skipped", reason: "damaged" } as const;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const sha256 = part.object.slice(0, 64);
|
|
101
|
+
await bucket.put(stagedPart(userId, part.object), bytes, { sha256 });
|
|
102
|
+
|
|
103
|
+
return { status: "restored" } as const;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Reassemble authenticated uploads into one R2 object without buffering the file in the Worker. */
|
|
107
|
+
export async function restoreFileParts(
|
|
108
|
+
bucket: R2Bucket,
|
|
109
|
+
userId: string,
|
|
110
|
+
key: string,
|
|
111
|
+
file: BackupFile,
|
|
112
|
+
options: R2PutOptions
|
|
113
|
+
) {
|
|
114
|
+
const { readable, writable } = new FixedLengthStream(file.size);
|
|
115
|
+
const writer = writable.getWriter();
|
|
116
|
+
|
|
117
|
+
const copy = async () => {
|
|
118
|
+
try {
|
|
119
|
+
for (const [i, part] of file.parts.entries()) {
|
|
120
|
+
const object = await bucket.get(stagedPart(userId, part.object));
|
|
121
|
+
|
|
122
|
+
if (
|
|
123
|
+
!object ||
|
|
124
|
+
object.size !== Math.min(PART_BYTES, file.size - i * PART_BYTES)
|
|
125
|
+
) {
|
|
126
|
+
throw new Error("Missing or incomplete backup attachment part");
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
for await (const bytes of object.body) await writer.write(bytes);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
await writer.close();
|
|
133
|
+
} catch (error) {
|
|
134
|
+
await writer.abort(error);
|
|
135
|
+
throw error;
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
await Promise.all([bucket.put(key, readable, options), copy()]);
|
|
140
|
+
}
|