Repository navigation
Expand file tree
/
Copy pathworker.ts
More file actions
180 lines (170 loc) · 8.6 KB
/
Copy pathworker.ts
File metadata and controls
180 lines (170 loc) · 8.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { withDatabase } from "./src/db/with-database";
import { withStorage } from "./src/lib/storage";
import vinextHandler from "vinext/server/fetch-handler";
import {
processInboundMessage,
storeRawToR2,
type InboundQueueMessage,
} from "./src/lib/email/inbound";
import { processOutboundQueue, type OutboundQueueMessage } from "./src/lib/email/send";
import {
isImportJobMessage,
isInboundQueueMessage,
isWebhookRetryMessage,
settleFailedInboundMessage,
withD1BookmarkCookie,
withD1ReadSession,
} from "./worker-utils";
import { runWithD1Session } from "./src/lib/d1-session-context";
import { processImportJob } from "./src/lib/import/jobs";
import { autoUpdateJob } from "./src/lib/scheduled/auto-update-job";
import { dailyJobs, frequentJobs, runScheduledJob } from "./src/lib/scheduled/jobs";
import { processWebhookRetry, type WebhookRetryMessage } from "./src/lib/email/webhooks";
import { resolveIncomingMail, forwardMessage } from "./src/lib/email/incoming";
import { createBatchBudget } from "./src/lib/deliverability/ingest";
import { getSessionFromToken, getUserFromSession } from "./src/lib/auth/session";
import { getSessionTokenFromRequest } from "./src/lib/realtime/utils";
import { inboundAttachmentLimitReasonFromRaw } from "./src/lib/email/inbound-attachments";
import { hasValidSessionMutationOrigin } from "./src/lib/auth/origin";
import {
getAccountForwardingDestination,
isForwardedMarkerValid,
MAILFLARE_FORWARDED_HEADER,
} from "./src/lib/email/account-forwarding";
import { processAgentDraftJob } from "./src/lib/agent/jobs/utils";
import { handleDavRequest, isDavPath } from "./src/lib/dav/handler";
import { guardMtaStsHost, isMtaStsHost } from "./src/lib/deliverability/mta-sts";
export { RealtimeHub } from "./src/lib/realtime/hub";
export default {
async fetch(request: Request, rawEnv: CloudflareEnv, ctx: ExecutionContext) {
// An mta-sts.<domain> host serves the policy file and nothing else of the app.
const requestUrl = new URL(request.url);
const blocked = guardMtaStsHost({ hostname: requestUrl.hostname, pathname: requestUrl.pathname, method: request.method });
if (blocked) return blocked;
// CalDAV/CardDAV use methods Next route handlers cannot express (PROPFIND, REPORT). They skip the D1 read
// session on purpose: a client that writes and then asks for changes must read its own writes.
if (isDavPath(requestUrl.pathname)) {
return (await handleDavRequest(request, withDatabase(withStorage(rawEnv)))) ?? new Response("Not found", { status: 404 });
}
// Reads go through a D1 session anchored at the client's bookmark cookie so D1 can serve
// them from read replicas; email/queue/scheduled below keep primary reads on purpose.
// The policy answer stays cookie free, so it opens no read session (which would add a bookmark cookie).
const d1Session = isMtaStsHost(requestUrl.hostname) ? null : withD1ReadSession(rawEnv, request);
const env = withDatabase(withStorage(d1Session?.env ?? rawEnv));
const url = requestUrl;
if (url.pathname === "/api/realtime") {
if (request.headers.get("Upgrade")?.toLowerCase() !== "websocket") {
return new Response("Expected WebSocket upgrade", { status: 426 });
}
if (!hasValidSessionMutationOrigin(request)) {
return new Response("Invalid origin", { status: 403 });
}
const token = getSessionTokenFromRequest(request);
const [user, session] = await Promise.all([getUserFromSession(env, token), getSessionFromToken(env, token)]);
if (!user || !session || user.disabled || user.deletedAt) {
return new Response("Unauthorized", { status: 401 });
}
const hub = env.REALTIME.getByName(user.id);
const hubRequest = new Request("https://mailflare-realtime/connect", request);
hubRequest.headers.set("X-Mailflare-Realtime-User", user.id);
hubRequest.headers.set("X-Mailflare-Realtime-Session", session.id);
return hub.fetch(hubRequest);
}
const response = await runWithD1Session(d1Session?.session, () => vinextHandler.fetch(request, env, ctx));
return withD1BookmarkCookie(request, response, d1Session?.session);
},
async email(message: ForwardableEmailMessage, rawEnv: CloudflareEnv, ctx: ExecutionContext) {
const env = withDatabase(withStorage(rawEnv));
try {
if (message.rawSize > 25 * 1024 * 1024) {
message.setReject("Message rejected: raw email exceeds the 25 MiB receiving limit. Send a download link instead.");
return;
}
// Domain routing rules are resolved here rather than in the queue because reject and
// forward can only be actioned on the live ForwardableEmailMessage.
const headerMap = Object.fromEntries(message.headers);
const alreadyForwarded = await isForwardedMarkerValid(env, message.headers.get(MAILFLARE_FORWARDED_HEADER), headerMap);
const decision = await resolveIncomingMail(env, message.from, message.to, {
subject: message.headers.get("subject"),
from: message.headers.get("from"),
alreadyForwarded,
all: headerMap,
});
if (decision?.action === "reject") {
message.setReject(decision.rejectReason ?? "Message rejected by routing rule");
return;
}
const raw = await new Response(message.raw).arrayBuffer();
const attachmentLimitReason = await inboundAttachmentLimitReasonFromRaw(raw);
if (attachmentLimitReason) {
message.setReject(attachmentLimitReason);
return;
}
if (decision?.action === "forward" && decision.forwardTo) {
const forwarded = await forwardMessage(env, message, decision.forwardTo);
// A forward rule drops the message unless it was explicitly asked to keep a copy.
// If the forward itself failed we still store it, so mail is never silently lost.
if (forwarded && !decision.keepCopy) return;
}
// Report mail is never forwarded, by a rule or by the account's forwarding address.
if (!alreadyForwarded && decision?.action !== "report") {
const forwardingDestination = await getAccountForwardingDestination(env, message.to);
if (forwardingDestination) {
await forwardMessage(env, message, forwardingDestination);
}
}
const rawR2Key = await storeRawToR2(env, message.from, message.to, raw);
const payload: InboundQueueMessage = {
from: message.from,
to: message.to,
rawR2Key,
headers: headerMap,
};
await env.INBOUND_QUEUE.send(payload);
} catch (err) {
console.error("Inbound enqueue failed", err);
// setReject bounces permanently; throwing is a temporary SMTP failure, so the sender retries.
throw err;
}
},
async queue(batch: MessageBatch, rawEnv: CloudflareEnv): Promise<void> {
const env = withDatabase(withStorage(rawEnv));
// One budget per batch, so the reverse lookups of every DMARC report in it stay within the invocation's subrequests.
const reportBudget = createBatchBudget();
for (const msg of batch.messages) {
try {
if (isInboundQueueMessage(msg.body)) {
await processInboundMessage(env, msg.body, { reportBudget });
} else if (typeof msg.body === "object" && msg.body !== null && (msg.body as { kind?: unknown }).kind === "agent.draft" && typeof (msg.body as { jobId?: unknown }).jobId === "string") {
await processAgentDraftJob(env, (msg.body as { jobId: string }).jobId);
} else if (isWebhookRetryMessage(msg.body)) {
await processWebhookRetry(env, msg.body as WebhookRetryMessage);
} else if (isImportJobMessage(msg.body)) {
await processImportJob(env, { ...msg.body, attempt: msg.attempts });
} else if (typeof msg.body === "object" && msg.body !== null && (msg.body as { kind?: unknown }).kind === "email.scheduled") {
await processOutboundQueue(env, msg.body as OutboundQueueMessage);
} else {
throw new Error("Unknown queue message type");
}
msg.ack();
} catch (err) {
console.error("Queue processing failed", {
rawR2Key: isInboundQueueMessage(msg.body) ? msg.body.rawR2Key : undefined,
recipient: isInboundQueueMessage(msg.body) ? msg.body.to : undefined,
attempts: msg.attempts,
error: err,
});
if (isInboundQueueMessage(msg.body)) settleFailedInboundMessage(msg, err);
else msg.retry({ delaySeconds: 10 });
}
}
},
async scheduled(controller: ScheduledController, rawEnv: CloudflareEnv, ctx: ExecutionContext) {
const env = withDatabase(withStorage(rawEnv));
const now = new Date(controller.scheduledTime);
// Both crons run the frequent jobs; the daily ones belong to 02:00 UTC alone. The lists are shared with
// the self-hosted scheduler and the Vercel cron routes (src/lib/scheduled/jobs.ts).
const jobs = [...frequentJobs(env, now), autoUpdateJob(env), ...(controller.cron === "0 2 * * *" ? dailyJobs(env, now) : [])];
for (const job of jobs) ctx.waitUntil(runScheduledJob(job));
},
} satisfies ExportedHandler<CloudflareEnv>;