Skip to content

Email for professionals and teams

perf: fewer round-trips for the dashboard, JMAP reads and sending (#188)

Kevin Kyburz1451cff55ec2
diff --git a/CLAUDE.md b/CLAUDE.md
index 86c415a..d1e112b 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -121,7 +121,7 @@ Native push goes straight to APNs when the server has `APNS_KEY_ID`/`APNS_TEAM_I
### Read receipts are an opt-in pixel
-The composer's eye toggle (`use-track-opens.ts`, remembered per browser) sends `trackOpens`. `deliverEmail` creates the `message_tracking` token at actual delivery, so a cancelled undo-send or reschedule leaves none, and injects the pixel only into the HTML handed to the provider: the stored Sent copy never contains it. The pixel is `GET /api/i/<token>.gif` (public, always the same GIF); `classifyOpen` (`src/lib/tracking/open-tracking-utils.ts`) names the client and device, drops device and location for proxies (Gmail, Yahoo, Apple), and flags Apple Mail Privacy Protection (Apple ASNs) and fetches within 10 s of delivery as `automated`, which `open_count` excludes. Pixel origin is the sender domain's own host from `domain_tracking_hosts` (set on the Domains page, must be the domain or a subdomain and attached to the Worker as a custom domain, kept out of `domains` so sending never depends on migration 0069), else `APP_URL`, else the composer request's origin; with neither, or before migration 0068, mail goes out untracked rather than failing. One pixel per message, so opens are not attributed to a recipient.
+The composer's eye toggle (`use-track-opens.ts`) starts from the account default in `user_compose_preferences` (Settings → Inbox → Compose, `/api/settings/compose`, migration 0071, off before it) and sends `trackOpens`; toggling it changes only that message. `deliverEmail` creates the `message_tracking` token at actual delivery, so a cancelled undo-send or reschedule leaves none, and injects the pixel only into the HTML handed to the provider: the stored Sent copy never contains it. The pixel is `GET /api/i/<token>.gif` (public, always the same GIF); `classifyOpen` (`src/lib/tracking/open-tracking-utils.ts`) names the client and device, drops device and location for proxies (Gmail, Yahoo, Apple), and flags Apple Mail Privacy Protection (Apple ASNs) and fetches within 10 s of delivery as `automated`, which `open_count` excludes. Pixel origin is the sender domain's own host from `domain_tracking_hosts` (set on the Domains page, must be the domain or a subdomain and attached to the Worker as a custom domain, kept out of `domains` so sending never depends on migration 0069), else `APP_URL`, else the composer request's origin; with neither, or before migration 0068, mail goes out untracked rather than failing. One pixel per message, so opens are not attributed to a recipient.
### Spam scoring adds up signals
diff --git a/src/app/api/branding/icon/route.ts b/src/app/api/branding/icon/route.ts
index aa68d2b..bde08cf 100644
--- a/src/app/api/branding/icon/route.ts
+++ b/src/app/api/branding/icon/route.ts
@@ -10,14 +10,17 @@ export const revalidate = 0;
async function getDefaultIcon(env: CloudflareEnv): Promise<Response> {
const asset = await env.ASSETS.fetch("https://mailflare.local/icon-96.png");
- return new Response(await asset.arrayBuffer(), {
+ const headers = new Headers(asset.headers);
+ // The asset binding defaults to revalidation; this endpoint uses a versioned URL on changes.
+ if (asset.ok) headers.set("Cache-Control", "public, max-age=3600");
+ return new Response(asset.body, {
status: asset.status,
statusText: asset.statusText,
- headers: asset.headers,
+ headers,
});
}
-export async function GET(request: Request) {
+export async function GET() {
const env = await getEnvAsync();
if (!(await getLicenseEntitlements(env)).canCustomizeBranding) return getDefaultIcon(env);
try {
@@ -32,7 +35,8 @@ export async function GET(request: Request) {
return new Response(object.body, {
headers: {
"Content-Type": object.httpMetadata?.contentType ?? "image/png",
- "Cache-Control": "no-cache",
+ // Changing the icon bumps the `v` query in this tab; other tabs pick it up within the hour.
+ "Cache-Control": "public, max-age=3600",
"X-Content-Type-Options": "nosniff",
},
});
diff --git a/src/app/api/contacts/avatar/route.ts b/src/app/api/contacts/avatar/route.ts
index 6897db4..05672d2 100644
--- a/src/app/api/contacts/avatar/route.ts
+++ b/src/app/api/contacts/avatar/route.ts
@@ -30,7 +30,8 @@ export async function GET(request: Request) {
.where(and(eq(contacts.userId, access.mailbox.userId), eq(contacts.email, email)))
.limit(1);
const avatarKey = account ? account.avatarKey : contact?.avatarKey;
- if (!avatarKey) return new Response("Not found", { status: 404 });
+ // Uploading an avatar changes the URL's version, so a cached miss never hides a new one.
+ if (!avatarKey) return new Response("Not found", { status: 404, headers: { "Cache-Control": "private, max-age=600" } });
return getAvatarImageResponse(request, env.BUCKET, avatarKey);
}
diff --git a/src/app/api/messages/route.ts b/src/app/api/messages/route.ts
index 6743ccd..85cbad6 100644
--- a/src/app/api/messages/route.ts
+++ b/src/app/api/messages/route.ts
@@ -7,7 +7,7 @@ import { getDb } from "@/db";
import { messages, outboundJobs } from "@/db/schema";
import { getContactDisplayNameMap } from "@/lib/contacts/service";
import { getFirstEmailAddressEntry, normalizeEmailAddress } from "@/lib/email/address";
-import { getMailboxAccessLevel, listAccessibleMailboxes } from "@/lib/mailboxes/access";
+import { getMailboxAccessLevel, hasMailboxPermission, listAccessibleMailboxes } from "@/lib/mailboxes/access";
import { tracksAccountIdentity } from "@/lib/profile/identity-utils";
import { buildSearchConditions } from "@/lib/search/conditions";
import { getRequestTimeZone } from "@/lib/time/utils";
@@ -45,8 +45,13 @@ export async function GET(request: Request) {
const accessibleMailboxIds = accessibleMailboxes.map((mailbox) => mailbox.id);
const conditions: SQL[] = [];
if (mailboxId) {
- const access = await getMailboxAccessLevel(db, user, mailboxId);
- if (!access?.canRead) {
+ const mailbox = accessibleMailboxes.find((mailbox) => mailbox.id === mailboxId);
+ // Reuse this request's authorization; retain the fallback for mailboxes
+ // omitted by the list's domain join (for example, legacy orphaned rows).
+ const canRead = mailbox
+ ? hasMailboxPermission(mailbox.permission, "read_only")
+ : (await getMailboxAccessLevel(db, user, mailboxId))?.canRead;
+ if (!canRead) {
return NextResponse.json({ error: "Mailbox not found" }, { status: 404 });
}
conditions.push(eq(messages.mailboxId, mailboxId));
@@ -134,60 +139,48 @@ export async function GET(request: Request) {
}
}
} else {
- const [totalRow] = await db.select({ total: count() }).from(messages).where(where);
- total = totalRow?.total ?? 0;
- rows = await db
- .select(messageListColumns)
- .from(messages)
- .where(where)
- .orderBy(desc(messages.createdAt))
- .limit(limit)
- .offset(offset);
+ const [totals, pageRows] = await Promise.all([
+ db.select({ total: count() }).from(messages).where(where),
+ db
+ .select(messageListColumns)
+ .from(messages)
+ .where(where)
+ .orderBy(desc(messages.createdAt))
+ .limit(limit)
+ .offset(offset),
+ ]);
+ total = totals[0]?.total ?? 0;
+ rows = pageRows;
}
// Conversation sizes for the rows on this page, so the list can show "(3)"
// next to a subject the way threaded clients do.
const threadIds = Array.from(new Set(rows.map((row) => row.threadId).filter((id): id is string => !!id)));
- const threadCounts = new Map<string, { total: number; unread: number }>();
- if (threadIds.length > 0) {
- const scope = mailboxId
- ? eq(messages.mailboxId, mailboxId)
- : accessibleMailboxIds.length > 0
- ? inArray(messages.mailboxId, accessibleMailboxIds)
- : eq(messages.userId, user.id);
- const countRows = await db
- .select({
- threadId: messages.threadId,
- total: count(),
- unread: sum(sql`case when ${messages.read} = 0 then 1 else 0 end`),
- })
- .from(messages)
- .where(and(scope, inArray(messages.threadId, threadIds), isNotNull(messages.threadId), notInArray(messages.status, ["draft", "trash"])))
- .groupBy(messages.threadId);
- for (const row of countRows) {
- if (row.threadId) threadCounts.set(row.threadId, { total: row.total, unread: Number(row.unread ?? 0) });
- }
- }
- const scheduledAtByMessageId = new Map<string, string>();
- if (scheduled === "true" && rows.length > 0) {
- const jobs = await db
- .select({ messageId: outboundJobs.messageId, scheduledAt: outboundJobs.scheduledAt })
- .from(outboundJobs)
- .where(and(inArray(outboundJobs.messageId, rows.map((row) => row.id)), eq(outboundJobs.status, "queued"), isNotNull(outboundJobs.scheduledAt)));
- for (const job of jobs) {
- if (job.messageId && job.scheduledAt) scheduledAtByMessageId.set(job.messageId, job.scheduledAt.toISOString());
- }
- }
- const openCounts = await getOpenCounts(env, rows.filter((row) => row.direction === "outbound").map((row) => row.id));
- const mailboxNameMap = new Map(
- accessibleMailboxes.map((mailbox) => [
- mailbox.id,
- mailbox.userId === user.id && tracksAccountIdentity(mailbox, user.email)
- ? user.name
- : mailbox.displayName ?? mailbox.localPart,
- ]),
- );
- const contactMapsByUserId = new Map(
- await Promise.all(
+ const scope = mailboxId
+ ? eq(messages.mailboxId, mailboxId)
+ : accessibleMailboxIds.length > 0
+ ? inArray(messages.mailboxId, accessibleMailboxIds)
+ : eq(messages.userId, user.id);
+ // These enrichments depend only on the selected page, not on each other.
+ const [countRows, jobs, openCounts, contactMaps] = await Promise.all([
+ threadIds.length > 0
+ ? db
+ .select({
+ threadId: messages.threadId,
+ total: count(),
+ unread: sum(sql`case when ${messages.read} = 0 then 1 else 0 end`),
+ })
+ .from(messages)
+ .where(and(scope, inArray(messages.threadId, threadIds), isNotNull(messages.threadId), notInArray(messages.status, ["draft", "trash"])))
+ .groupBy(messages.threadId)
+ : [],
+ scheduled === "true" && rows.length > 0
+ ? db
+ .select({ messageId: outboundJobs.messageId, scheduledAt: outboundJobs.scheduledAt })
+ .from(outboundJobs)
+ .where(and(inArray(outboundJobs.messageId, rows.map((row) => row.id)), eq(outboundJobs.status, "queued"), isNotNull(outboundJobs.scheduledAt)))
+ : [],
+ getOpenCounts(env, rows.filter((row) => row.direction === "outbound").map((row) => row.id)),
+ Promise.all(
Array.from(new Set(rows.map((message) => message.userId))).map(async (userId) => [
userId,
await getContactDisplayNameMap(
@@ -199,7 +192,24 @@ export async function GET(request: Request) {
),
] as const),
),
+ ]);
+ const threadCounts = new Map<string, { total: number; unread: number }>();
+ for (const row of countRows) {
+ if (row.threadId) threadCounts.set(row.threadId, { total: row.total, unread: Number(row.unread ?? 0) });
+ }
+ const scheduledAtByMessageId = new Map<string, string>();
+ for (const job of jobs) {
+ if (job.messageId && job.scheduledAt) scheduledAtByMessageId.set(job.messageId, job.scheduledAt.toISOString());
+ }
+ const mailboxNameMap = new Map(
+ accessibleMailboxes.map((mailbox) => [
+ mailbox.id,
+ mailbox.userId === user.id && tracksAccountIdentity(mailbox, user.email)
+ ? user.name
+ : mailbox.displayName ?? mailbox.localPart,
+ ]),
);
+ const contactMapsByUserId = new Map(contactMaps);
// `Message.textBody`/`htmlBody` are optional on the wire type and the
// reading pane loads them from /api/messages/[id]/thread, so they are
// neither selected nor sent here.
diff --git a/src/components/auth/auth-guard.tsx b/src/components/auth/auth-guard.tsx
index 3472001..131fb96 100644
--- a/src/components/auth/auth-guard.tsx
+++ b/src/components/auth/auth-guard.tsx
@@ -2,7 +2,7 @@
import { usePathname, useRouter } from "next/navigation";
import { useEffect, useState } from "react";
-import { authFetch } from "@/lib/auth/client";
+import { fetchSessionInfo, peekSessionInfo } from "@/hooks/use-current-user";
import type { AuthGuardProps } from "./auth-guard-types";
import { LoadingTransition } from "@/components/loading-transition";
import { saveUserTimeZonePreference } from "@/lib/time/client";
@@ -17,25 +17,24 @@ export function AuthGuard({ children, mode = "protected", requireMailbox, requir
async function checkSession() {
try {
- const cookieResponse = await fetch("/api/auth/me", {
- cache: "no-store",
- signal: AbortSignal.timeout(5_000),
- });
- const response = cookieResponse.ok || cookieResponse.status !== 401
- ? cookieResponse
- : await authFetch("/api/auth/me", {
- redirectOnUnauthorized: false,
- signal: AbortSignal.timeout(5_000),
- });
+ // Login pages and onboarding change the answer, so only a finished setup is reused across navigations.
+ const cached = mode === "protected" ? peekSessionInfo() : null;
+ const response = cached && cached.isSetup !== false && cached.hasMailboxes !== false
+ ? cached
+ : await fetchSessionInfo({ fresh: true });
if (cancelled) return;
+ if (!response) {
+ setAuthorized(true);
+ return;
+ }
- if (!response.ok) {
+ if (response.status !== 200) {
if (mode === "protected" && response.status === 401) router.replace("/login");
else setAuthorized(true);
return;
}
- const data = (await response.json()) as { hasMailboxes?: boolean; isSetup?: boolean; user?: { id?: string; role?: string; isPrimaryAdmin?: boolean; timeZone?: string | null } };
+ const data = response;
if (data.user?.id) saveUserTimeZonePreference(data.user.id, data.user.timeZone ?? null);
if (mode === "public") {
if (!allowAuthenticated) router.replace("/inbox");
diff --git a/src/components/compose/compose-form.tsx b/src/components/compose/compose-form.tsx
index 75724b9..8acd1aa 100644
--- a/src/components/compose/compose-form.tsx
+++ b/src/components/compose/compose-form.tsx
@@ -399,11 +399,9 @@ export function ComposeForm({
return;
}
- if (currentDraftId) {
- void authFetch(`/api/drafts/${currentDraftId}`, { method: "DELETE" }).finally(() => {
- window.dispatchEvent(new Event("mailflare:messages-changed"));
- });
- }
+ // With a draft, refresh once it is gone so the lists reload a single time.
+ const refreshLists = () => window.dispatchEvent(new Event("mailflare:messages-changed"));
+ if (currentDraftId) void authFetch(`/api/drafts/${currentDraftId}`, { method: "DELETE" }).finally(refreshLists);
draftGeneration.current += 1;
draftIdRef.current = null;
setDraftId(null);
@@ -431,7 +429,7 @@ export function ComposeForm({
} else {
showNotice(data.scheduled ? t("compose.scheduled") : t("compose.sent"));
}
- window.dispatchEvent(new Event("mailflare:messages-changed"));
+ if (!currentDraftId) refreshLists();
if (onClose) {
dismiss();
diff --git a/src/components/sidebar-state.tsx b/src/components/sidebar-state.tsx
index a96976f..bf2ad1d 100644
--- a/src/components/sidebar-state.tsx
+++ b/src/components/sidebar-state.tsx
@@ -2,6 +2,7 @@
import { createContext, useContext, useEffect, useLayoutEffect, useState } from "react";
import { usePathname } from "next/navigation";
+import { fetchCurrentUser } from "@/hooks/use-current-user";
import { readColumnWidth, readInitialColumnWidth, saveColumnWidth } from "./column-width-preferences";
import { isMobileViewport, useIsMobile } from "./sidebar-mobile-utils";
import { readInitialSidebarMinimal, saveInitialSidebarMinimal, syncInitialSidebarWidth } from "./sidebar-state-utils";
@@ -48,15 +49,9 @@ export function SidebarProvider({ children, expandedWidth = 260, mobileOverlay =
useEffect(() => {
// The sidebar preference is cosmetic, so every failure here degrades to the default.
- // Guard the parse: an error response may carry an empty or non-JSON body, and an
- // unhandled rejection here surfaces as a confusing SyntaxError overlay in dev.
- void fetch("/api/auth/me", { cache: "no-store" })
- .then(async (response) => {
- if (!response.ok) return null;
- return (await response.json().catch(() => null)) as { user?: { id?: string } } | null;
- })
- .then((data) => {
- const userId = data?.user?.id;
+ void fetchCurrentUser()
+ .then((user) => {
+ const userId = user?.id;
if (!userId) return;
setUserId(userId);
const key = `mailflare-sidebar-minimal:${userId}`;
diff --git a/src/hooks/use-current-user.ts b/src/hooks/use-current-user.ts
index 7689cfa..c4be4de 100644
--- a/src/hooks/use-current-user.ts
+++ b/src/hooks/use-current-user.ts
@@ -15,26 +15,62 @@ export type CurrentUser = {
hasAvatar: boolean;
};
+export type SessionInfo = {
+ status: number;
+ hasMailboxes?: boolean;
+ isSetup?: boolean;
+ user?: (CurrentUser & { timeZone?: string | null }) | null;
+};
+
+const SESSION_INFO_MAX_AGE_MS = 5 * 60 * 1000;
+
+let cachedSession: { info: SessionInfo; at: number } | null = null;
let cachedUser: CurrentUser | null = null;
-let inflight: Promise<CurrentUser | null> | null = null;
+let inflight: Promise<SessionInfo | null> | null = null;
+let userRequest: { session: Promise<SessionInfo | null>; user: Promise<CurrentUser | null> } | null = null;
let generation = 0;
export function clearCurrentUserCache(): void {
generation += 1;
+ cachedSession = null;
cachedUser = null;
inflight = null;
+ userRequest = null;
}
-export function fetchCurrentUser(): Promise<CurrentUser | null> {
- if (cachedUser) return Promise.resolve(cachedUser);
+if (typeof window !== "undefined") window.addEventListener(AUTH_SESSION_CHANGED_EVENT, clearCurrentUserCache);
+
+/** The cached answer if it is still fresh, without a request. */
+export function peekSessionInfo(): SessionInfo | null {
+ return cachedSession && Date.now() - cachedSession.at < SESSION_INFO_MAX_AGE_MS ? cachedSession.info : null;
+}
+
+async function requestSessionInfo(): Promise<SessionInfo> {
+ const cookieResponse = await fetch("/api/auth/me", { cache: "no-store", signal: AbortSignal.timeout(5_000) });
+ const response = cookieResponse.ok || cookieResponse.status !== 401
+ ? cookieResponse
+ : await authFetch("/api/auth/me", { redirectOnUnauthorized: false, signal: AbortSignal.timeout(5_000) });
+ if (!response.ok) return { status: response.status };
+ const data = (await response.json()) as Omit<SessionInfo, "status">;
+ return { status: response.status, ...data };
+}
+
+/**
+ * The `/api/auth/me` answer shared by the auth guard, sidebar and account menus,
+ * so navigating does not refetch it. `fresh` bypasses the cache; network
+ * failures resolve to null and are never cached.
+ */
+export function fetchSessionInfo({ fresh = false }: { fresh?: boolean } = {}): Promise<SessionInfo | null> {
+ const cached = fresh ? null : peekSessionInfo();
+ if (cached) return Promise.resolve(cached);
if (!inflight) {
const requestGeneration = generation;
- const request = authFetch("/api/auth/me", { redirectOnUnauthorized: false })
- .then((response) => (response.ok ? response.json() : null))
- .then((data) => {
+ const request = requestSessionInfo()
+ .then((info) => {
if (requestGeneration !== generation) return null;
- cachedUser = (data as { user?: CurrentUser } | null)?.user ?? null;
- return cachedUser;
+ cachedSession = info.status === 200 ? { info, at: Date.now() } : null;
+ cachedUser = info.user ?? null;
+ return info;
})
.catch(() => null)
.finally(() => {
@@ -45,6 +81,15 @@ export function fetchCurrentUser(): Promise<CurrentUser | null> {
return inflight;
}
+export function fetchCurrentUser(): Promise<CurrentUser | null> {
+ if (cachedUser) return Promise.resolve(cachedUser);
+ const session = fetchSessionInfo();
+ if (!userRequest || userRequest.session !== session) {
+ userRequest = { session, user: session.then((info) => info?.user ?? null) };
+ }
+ return userRequest.user;
+}
+
export function useCurrentUser(): CurrentUser | null {
const [user, setUser] = useState<CurrentUser | null>(cachedUser);
const [reloadKey, setReloadKey] = useState(0);
diff --git a/src/hooks/use-message-counts.ts b/src/hooks/use-message-counts.ts
index 512bd24..ab44007 100644
--- a/src/hooks/use-message-counts.ts
+++ b/src/hooks/use-message-counts.ts
@@ -34,11 +34,11 @@ export function useMessageCounts(mailboxId?: string | null, enabled = true) {
let refreshTimer: number | null = null;
let requestNumber = 0;
- async function loadCounts(force = false) {
+ async function loadCounts() {
const currentRequest = ++requestNumber;
setIsLoading(true);
try {
- const nextCounts = await fetchMessageCounts(mailboxId, force);
+ const nextCounts = await fetchMessageCounts(mailboxId);
if (!cancelled && currentRequest === requestNumber) setCounts(nextCounts ?? emptyCounts);
} finally {
if (!cancelled && currentRequest === requestNumber) setIsLoading(false);
@@ -52,7 +52,8 @@ export function useMessageCounts(mailboxId?: string | null, enabled = true) {
if (refreshTimer) window.clearTimeout(refreshTimer);
refreshTimer = window.setTimeout(() => {
refreshTimer = null;
- void loadCounts(true);
+ // The cache was cleared above, so hooks refreshing for the same change share one request.
+ void loadCounts();
}, REFRESH_DEBOUNCE_MS);
}
function onMessageCountsDelta(event: Event) {
diff --git a/src/lib/avatar-images.ts b/src/lib/avatar-images.ts
index 936cdf6..76a314a 100644
--- a/src/lib/avatar-images.ts
+++ b/src/lib/avatar-images.ts
@@ -47,9 +47,13 @@ export async function getAvatarImageResponse(
"Content-Type": object.httpMetadata?.contentType ?? "application/octet-stream",
"X-Content-Type-Options": "nosniff",
"Content-Security-Policy": "default-src 'none'; img-src 'self'; sandbox",
+ // Uploads change the URL's version. Unversioned URLs refresh within five minutes
+ // without an authenticated DB/storage round trip on every component remount.
"Cache-Control": url.searchParams.has("v")
? "private, max-age=31536000, immutable"
- : "private, no-cache",
+ : "private, max-age=300",
+ // The same URL can resolve to different images after switching accounts.
+ Vary: "Cookie, Authorization",
ETag: object.httpEtag,
});
if (request.headers.get("If-None-Match") === object.httpEtag) {
diff --git a/src/lib/jmap/access.ts b/src/lib/jmap/access.ts
index dcc324a..7232f69 100644
--- a/src/lib/jmap/access.ts
+++ b/src/lib/jmap/access.ts
@@ -5,7 +5,21 @@ import { getMailboxDomainAddresses } from "@/lib/mailboxes/domain-addresses";
import type { AccessibleMailbox, JmapContext } from "./types";
/** Every Mailflare mailbox the key's user can read, in a JMAP-shaped record. */
-export async function listJmapMailboxes(ctx: JmapContext): Promise<AccessibleMailbox[]> {
+export function listJmapMailboxes(ctx: JmapContext): Promise<AccessibleMailbox[]> {
+ if (ctx.memo === null) return loadJmapMailboxes(ctx);
+ const memo = (ctx.memo ??= {});
+ if (!memo.mailboxes) {
+ const pending = loadJmapMailboxes(ctx);
+ memo.mailboxes = pending;
+ // A failed lookup is retried by the next call instead of failing it too.
+ pending.catch(() => {
+ if (memo.mailboxes === pending) memo.mailboxes = undefined;
+ });
+ }
+ return memo.mailboxes;
+}
+
+async function loadJmapMailboxes(ctx: JmapContext): Promise<AccessibleMailbox[]> {
const rows = await listAccessibleMailboxes(ctx.db, ctx.auth.user);
return rows.filter((row) => !ctx.auth.mailboxIds || ctx.auth.mailboxIds.includes(row.id)).map((row) => ({
id: row.id,
diff --git a/src/lib/jmap/changes.ts b/src/lib/jmap/changes.ts
index 11f70cb..b4dc70b 100644
--- a/src/lib/jmap/changes.ts
+++ b/src/lib/jmap/changes.ts
@@ -42,7 +42,20 @@ export type ChangeScope = {
state: string;
};
-export async function currentChangeScope(ctx: JmapContext): Promise<ChangeScope> {
+export function currentChangeScope(ctx: JmapContext): Promise<ChangeScope> {
+ if (ctx.memo === null) return loadChangeScope(ctx);
+ const memo = (ctx.memo ??= {});
+ if (!memo.scope) {
+ const pending = loadChangeScope(ctx);
+ memo.scope = pending;
+ pending.catch(() => {
+ if (memo.scope === pending) memo.scope = undefined;
+ });
+ }
+ return memo.scope;
+}
+
+async function loadChangeScope(ctx: JmapContext): Promise<ChangeScope> {
const mailboxes = await listJmapMailboxes(ctx);
const [seq, fingerprint, floor] = await Promise.all([latestSeq(ctx, mailboxes.map((m) => m.id)), accessFingerprint(mailboxes), floorSeq(ctx)]);
const effective = Math.max(seq, floor);
diff --git a/src/lib/jmap/identities.ts b/src/lib/jmap/identities.ts
index 99c8ba2..ebb923e 100644
--- a/src/lib/jmap/identities.ts
+++ b/src/lib/jmap/identities.ts
@@ -12,9 +12,10 @@ import type { JmapContext, JmapMethodHandler } from "./types";
/** One Identity per address the account may send from. */
export async function buildIdentities(ctx: JmapContext) {
const list: Array<Record<string, unknown>> = [];
- for (const mailbox of await listJmapMailboxes(ctx)) {
- if (mailbox.permission === "read_only") continue;
- for (const address of await listSendableAddresses(ctx, mailbox)) {
+ const sendable = (await listJmapMailboxes(ctx)).filter((mailbox) => mailbox.permission !== "read_only");
+ const addresses = await Promise.all(sendable.map((mailbox) => listSendableAddresses(ctx, mailbox)));
+ for (const [index, mailbox] of sendable.entries()) {
+ for (const address of addresses[index]) {
list.push({
id: identityId(mailbox.id, address),
name: mailbox.displayName ?? "",
diff --git a/src/lib/jmap/processor.ts b/src/lib/jmap/processor.ts
index dd203d8..d60e25d 100644
--- a/src/lib/jmap/processor.ts
+++ b/src/lib/jmap/processor.ts
@@ -85,11 +85,40 @@ function resolveCreatedIds(args: Record<string, unknown>, createdIds: Record<str
return walk(args) as Record<string, unknown>;
}
+/** Requests slower than this are logged with the shape of each call, so expensive client patterns show up in the Worker logs. */
+const SLOW_REQUEST_MS = 200;
+
+/** Calls that cannot change mailboxes or move the state, so later calls may reuse what they looked up. */
+const READ_ONLY_METHOD = /\/(get|query|changes|queryChanges)$|^Core\/echo$/;
+
+/** What a call asked for, without ids, filters' values or content. */
+function describeCall(name: string, args: Record<string, unknown>, ms: number) {
+ const size = (value: unknown) => (Array.isArray(value) ? value.length : value === null || value === undefined ? undefined : typeof value === "object" ? "ref" : value);
+ return {
+ name,
+ ms,
+ ids: size(args.ids ?? args["#ids"]),
+ properties: Array.isArray(args.properties) ? args.properties.length : undefined,
+ bodyProperties: Array.isArray(args.bodyProperties) ? args.bodyProperties.length : undefined,
+ fetchBodyValues: args.fetchTextBodyValues || args.fetchHTMLBodyValues || args.fetchAllBodyValues ? true : undefined,
+ limit: typeof args.limit === "number" ? args.limit : undefined,
+ position: typeof args.position === "number" ? args.position : undefined,
+ filter: args.filter && typeof args.filter === "object" ? Object.keys(args.filter as object).join(",") : undefined,
+ collapseThreads: args.collapseThreads === true ? true : undefined,
+ sinceState: typeof args.sinceState === "string" ? true : undefined,
+ };
+}
+
export async function processRequest(ctx: JmapContext, request: JmapRequest): Promise<JmapResponse> {
const responses: JmapInvocation[] = [];
Object.assign(ctx.createdIds, request.createdIds ?? {});
+ const startedAt = Date.now();
+ const calls: ReturnType<typeof describeCall>[] = [];
for (const [name, rawArgs, callId] of request.methodCalls) {
+ const callStartedAt = Date.now();
+ const readOnly = READ_ONLY_METHOD.test(name);
+ if (!readOnly) ctx.memo = undefined;
const handler = METHODS[name];
if (!handler) {
responses.push(["error", { type: "unknownMethod" }, callId]);
@@ -101,7 +130,8 @@ export async function processRequest(ctx: JmapContext, request: JmapRequest): Pr
responses.push(["error", { type: "accountNotFound" }, callId]);
continue;
}
- const result = await handler(ctx, args);
+ const result = await handler(readOnly ? ctx : { ...ctx, memo: null }, args);
+ calls.push(describeCall(name, args, Date.now() - callStartedAt));
if ("type" in result && typeof result.type === "string" && !("accountId" in result) && !("list" in result)) {
responses.push(["error", result, callId]);
continue;
@@ -124,7 +154,12 @@ export async function processRequest(ctx: JmapContext, request: JmapRequest): Pr
}
}
- return { methodResponses: responses, createdIds: Object.keys(ctx.createdIds).length ? ctx.createdIds : undefined, sessionState: await sessionState(ctx) };
+ if (request.methodCalls.some(([name]) => !READ_ONLY_METHOD.test(name))) ctx.memo = undefined;
+ const stateStartedAt = Date.now();
+ const state = await sessionState(ctx);
+ const totalMs = Date.now() - startedAt;
+ if (totalMs >= SLOW_REQUEST_MS) console.log("JMAP slow request", JSON.stringify({ totalMs, stateMs: Date.now() - stateStartedAt, calls }));
+ return { methodResponses: responses, createdIds: Object.keys(ctx.createdIds).length ? ctx.createdIds : undefined, sessionState: state };
}
export async function sessionState(ctx: JmapContext): Promise<string> {
diff --git a/src/lib/jmap/types.d.ts b/src/lib/jmap/types.d.ts
index 70e596c..2742c55 100644
--- a/src/lib/jmap/types.d.ts
+++ b/src/lib/jmap/types.d.ts
@@ -1,5 +1,6 @@
import type { ApiAuthResult } from "@/lib/api/key-auth-types";
import type { AppDatabase } from "@/db";
+import type { ChangeScope } from "./changes";
export type JmapId = string;
@@ -31,6 +32,11 @@ export type JmapContext = {
origin: string;
/** Client-assigned creation ids resolved so far in this request. */
createdIds: Record<string, JmapId>;
+ /**
+ * Lookups shared by the read-only calls of one request. `null` while a
+ * write runs, since it reads the state before and after its change.
+ */
+ memo?: { mailboxes?: Promise<AccessibleMailbox[]>; scope?: Promise<ChangeScope> } | null;
};
export type JmapMethodHandler = (ctx: JmapContext, args: Record<string, unknown>) => Promise<Record<string, unknown> | JmapMethodError>;
diff --git a/tests/current-user-permission-reset.test.mjs b/tests/current-user-permission-reset.test.mjs
index f4d5685..bd02bb5 100644
--- a/tests/current-user-permission-reset.test.mjs
+++ b/tests/current-user-permission-reset.test.mjs
@@ -11,6 +11,7 @@ const directory = mkdtempSync(join(project, "node_modules", "permission-user-tes
after(() => rmSync(directory, { recursive: true, force: true }));
await build({ entryPoints: [join(project, "src/hooks/use-current-user.ts")], outfile: join(directory, "user.mjs"), bundle: true, platform: "node", format: "esm", packages: "external", plugins: [{ name: "test-auth", setup(builder) { builder.onResolve({ filter: /^@\/lib\/auth\/client$/ }, () => ({ path: "auth", namespace: "mock" })); builder.onLoad({ filter: /.*/, namespace: "mock" }, () => ({ contents: "export const AUTH_SESSION_CHANGED_EVENT='permission-session-changed';export function authFetch(){return globalThis.fetchUserResponse()}", loader: "js" })); } }], logLevel: "silent" });
const { fetchCurrentUser, clearCurrentUserCache, useCurrentUser } = await import(pathToFileURL(join(directory, "user.mjs")).href);
+globalThis.fetch = () => globalThis.fetchUserResponse();
const response = role => ({ ok: true, json: async () => ({ user: { id: role, role, isPrimaryAdmin: role === "admin", canManageDomains: role === "admin" } }) });
test("an old account response cannot repopulate permissions or clear the new inflight request", async () => {
diff --git a/tests/image-response-cache.test.mjs b/tests/image-response-cache.test.mjs
new file mode 100644
index 0000000..dcb8de8
--- /dev/null
+++ b/tests/image-response-cache.test.mjs
@@ -0,0 +1,111 @@
+import assert from "node:assert/strict";
+import { mkdtempSync, rmSync } from "node:fs";
+import { dirname, join } from "node:path";
+import test, { after } from "node:test";
+import { fileURLToPath, pathToFileURL } from "node:url";
+import { build } from "esbuild";
+
+const root = join(dirname(fileURLToPath(import.meta.url)), "..");
+const directory = mkdtempSync(join(root, "node_modules", "image-response-test-"));
+const mocks = {
+ "@/db": "export function getDb() { return globalThis.imageResponseTest.db; }",
+ "@/lib/cloudflare": "export async function getEnvAsync() { return globalThis.imageResponseTest.env; }",
+ "@/lib/branding/service": "export const APP_SETTINGS_ID = 'app';",
+ "@/lib/licenses/service": "export async function getLicenseEntitlements() { return { canCustomizeBranding: globalThis.imageResponseTest.custom }; }",
+};
+await build({
+ stdin: {
+ contents: `export { getAvatarImageResponse } from './src/lib/avatar-images.ts';
+ export { GET as getBrandingIcon } from './src/app/api/branding/icon/route.ts';`,
+ resolveDir: root,
+ sourcefile: "image-response-test.ts",
+ },
+ outfile: join(directory, "entry.mjs"),
+ bundle: true, platform: "node", format: "esm", packages: "external", logLevel: "silent",
+ plugins: [{ name: "image-dependencies", setup(builder) {
+ builder.onResolve({ filter: /^@\// }, ({ path }) => path in mocks ? { path, namespace: "mock" } : undefined);
+ builder.onLoad({ filter: /.*/, namespace: "mock" }, ({ path }) => ({ contents: mocks[path], loader: "js" }));
+ } }],
+});
+const { getAvatarImageResponse, getBrandingIcon } = await import(pathToFileURL(join(directory, "entry.mjs")).href);
+after(() => {
+ rmSync(directory, { recursive: true, force: true });
+ delete globalThis.imageResponseTest;
+});
+
+const avatarBucket = () => ({
+ get: async () => ({ body: "avatar", httpMetadata: { contentType: "image/webp" }, httpEtag: '"avatar-etag"' }),
+});
+
+test("unversioned avatars can be reused for five minutes without a server round trip, but never publicly", async () => {
+ for (const suffix of ["", "?variant=preview"]) {
+ const response = await getAvatarImageResponse(new Request(`https://mail.test/api/contacts/avatar${suffix}`), avatarBucket(), "avatar");
+ assert.equal(response.headers.get("Cache-Control"), "private, max-age=300");
+ assert.equal(response.headers.get("Content-Type"), "image/webp");
+ assert.equal(response.headers.get("Vary"), "Cookie, Authorization");
+ assert.equal(await response.text(), "avatar");
+ }
+});
+
+test("versioned avatars retain immutable private caching", async () => {
+ const response = await getAvatarImageResponse(new Request("https://mail.test/api/contacts/avatar?v=123"), avatarBucket(), "avatar");
+ assert.equal(response.headers.get("Cache-Control"), "private, max-age=31536000, immutable");
+});
+
+test("expired avatar revalidation retains ETag and refreshes the private freshness period", async () => {
+ const response = await getAvatarImageResponse(new Request("https://mail.test/api/contacts/avatar", {
+ headers: { "If-None-Match": '"avatar-etag"' },
+ }), avatarBucket(), "avatar");
+ assert.equal(response.status, 304);
+ assert.equal(response.headers.get("ETag"), '"avatar-etag"');
+ assert.equal(response.headers.get("Cache-Control"), "private, max-age=300");
+ assert.equal(await response.text(), "");
+});
+
+function brandingState({ custom = false, iconKey = null, assetStatus = 200, object = null } = {}) {
+ const query = { select() { return this; }, from() { return this; }, where() { return this; }, async limit() { return [{ iconKey }]; } };
+ globalThis.imageResponseTest = {
+ custom, db: query,
+ env: {
+ ASSETS: { async fetch() { return new Response("packaged-icon", {
+ status: assetStatus,
+ headers: { "Content-Type": "image/png", "Cache-Control": "public, max-age=0, must-revalidate", ETag: '"packaged-etag"' },
+ }); } },
+ BUCKET: { async get() { return object; } },
+ },
+ };
+}
+const iconRequest = () => new Request("https://mail.test/api/branding/icon");
+
+test("default branding icon overrides revalidation-only asset headers for unlicensed and licensed installs", async () => {
+ for (const custom of [false, true]) {
+ brandingState({ custom });
+ const response = await getBrandingIcon(iconRequest());
+ assert.equal(response.headers.get("Cache-Control"), "public, max-age=3600");
+ assert.equal(response.headers.get("Content-Type"), "image/png");
+ assert.equal(response.headers.get("ETag"), '"packaged-etag"');
+ assert.equal(await response.text(), "packaged-icon");
+ }
+});
+
+test("missing custom icon uses the same cacheable fallback", async () => {
+ brandingState({ custom: true, iconKey: "missing" });
+ const response = await getBrandingIcon(iconRequest());
+ assert.equal(response.headers.get("Cache-Control"), "public, max-age=3600");
+ assert.equal(await response.text(), "packaged-icon");
+});
+
+test("custom branding icon keeps its existing cache policy and content", async () => {
+ brandingState({ custom: true, iconKey: "custom", object: { body: "custom-icon", httpMetadata: { contentType: "image/webp" } } });
+ const response = await getBrandingIcon(iconRequest());
+ assert.equal(response.headers.get("Cache-Control"), "public, max-age=3600");
+ assert.equal(response.headers.get("Content-Type"), "image/webp");
+ assert.equal(await response.text(), "custom-icon");
+});
+
+test("asset failures do not acquire an hour-long freshness period", async () => {
+ brandingState({ assetStatus: 503 });
+ const response = await getBrandingIcon(iconRequest());
+ assert.equal(response.status, 503);
+ assert.equal(response.headers.get("Cache-Control"), "public, max-age=0, must-revalidate");
+});
diff --git a/tests/jmap-request-memo.test.mjs b/tests/jmap-request-memo.test.mjs
new file mode 100644
index 0000000..8b229f3
--- /dev/null
+++ b/tests/jmap-request-memo.test.mjs
@@ -0,0 +1,67 @@
+import assert from "node:assert/strict";
+import { mkdtempSync, rmSync } from "node:fs";
+import { dirname, join } from "node:path";
+import test, { after } from "node:test";
+import { fileURLToPath, pathToFileURL } from "node:url";
+import { build } from "esbuild";
+
+const root = join(dirname(fileURLToPath(import.meta.url)), "..");
+const outDir = mkdtempSync(join(root, "tests", ".tmp-jmap-memo-"));
+after(() => rmSync(outDir, { recursive: true, force: true }));
+
+await build({
+ entryPoints: {
+ processor: join(root, "src/lib/jmap/processor.ts"),
+ database: join(root, "server/runtime/sqlite-database.ts"),
+ migrate: join(root, "server/runtime/migrate.ts"),
+ dbIndex: join(root, "src/db/index.ts"),
+ },
+ outdir: outDir,
+ outExtension: { ".js": ".mjs" },
+ bundle: true,
+ splitting: true,
+ platform: "node",
+ format: "esm",
+ target: "node22",
+ tsconfig: join(root, "tsconfig.json"),
+ external: ["better-sqlite3", "drizzle-orm", "drizzle-orm/*", "cloudflare:workers", "postal-mime", "mimetext"],
+ logLevel: "silent",
+});
+
+const load = (name) => import(pathToFileURL(join(outDir, `${name}.mjs`)).href);
+const { processRequest } = await load("processor");
+const { SqliteDatabase } = await load("database");
+const { applyMigrations } = await load("migrate");
+const { getDb } = await load("dbIndex");
+
+async function createContext(t) {
+ const database = new SqliteDatabase(join(outDir, `${crypto.randomUUID()}.sqlite`));
+ t.after(() => database.db.close());
+ await applyMigrations(database, join(root, "drizzle/migrations"));
+ database.db.prepare("INSERT INTO users (id, email, name, password_hash, role, created_at) VALUES ('u1', '[email protected]', 'Kevin', 'x', 'admin', 0)").run();
+ database.db.prepare("INSERT INTO domains (id, user_id, hostname, zone_id, created_at) VALUES ('d1', 'u1', 'example.com', 'zone', 0)").run();
+ database.db.prepare("INSERT INTO mailboxes (id, user_id, domain_id, local_part, created_at) VALUES ('mb1', 'u1', 'd1', 'kevin', 0)").run();
+ const env = { DB: database };
+ const user = { id: "u1", email: "[email protected]", role: "admin" };
+ return { env, db: getDb(env), auth: { userId: "u1", email: user.email, scopes: ["jmap"], mailboxIds: null, user }, accountId: "u1", origin: "http://test", createdIds: {} };
+}
+
+test("a write after cached reads still reports the state it moved to", async (t) => {
+ const ctx = await createContext(t);
+ const response = await processRequest(ctx, {
+ using: [],
+ methodCalls: [
+ ["Mailbox/get", {}, "a"],
+ ["Mailbox/set", { create: { f1: { name: "Projects", parentId: "mb1" } } }, "b"],
+ ["Mailbox/get", { ids: null }, "c"],
+ ],
+ });
+ const [[, first], [setName, set], [, last]] = response.methodResponses;
+ assert.equal(setName, "Mailbox/set", JSON.stringify(set));
+ assert.ok(set.created?.f1, JSON.stringify(set));
+ assert.equal(set.oldState, first.state);
+ assert.notEqual(set.newState, set.oldState);
+ assert.equal(last.state, set.newState);
+ assert.equal(response.sessionState, set.newState);
+ assert.ok(last.list.some((mailbox) => mailbox.name === "Projects"), "the read after the write sees the new folder");
+});
diff --git a/tests/messages-route-performance.test.mjs b/tests/messages-route-performance.test.mjs
new file mode 100644
index 0000000..f607edc
--- /dev/null
+++ b/tests/messages-route-performance.test.mjs
@@ -0,0 +1,246 @@
+import assert from "node:assert/strict";
+import { mkdtempSync, rmSync } from "node:fs";
+import { dirname, join } from "node:path";
+import { setImmediate } from "node:timers/promises";
+import test, { after } from "node:test";
+import { fileURLToPath, pathToFileURL } from "node:url";
+import { build } from "esbuild";
+
+const root = join(dirname(fileURLToPath(import.meta.url)), "..");
+const outDir = mkdtempSync(join(root, "tests", ".tmp-messages-route-"));
+after(() => {
+ rmSync(outDir, { recursive: true, force: true });
+ delete globalThis.messagesRouteFixture;
+});
+const mocks = {
+ "@/lib/cloudflare": "export function getEnv() { return globalThis.messagesRouteFixture.env; }",
+ "@/lib/auth/cookies": "export async function getCurrentUser() { return globalThis.messagesRouteFixture.user; }",
+ "@/lib/contacts/service": "export function getContactDisplayNameMap(env, userId, addresses) { return globalThis.messagesRouteFixture.contacts(userId, addresses); }",
+ "@/lib/tracking/open-tracking": "export function getOpenCounts(env, ids) { return globalThis.messagesRouteFixture.opens(ids); }",
+};
+await build({
+ entryPoints: {
+ route: join(root, "src/app/api/messages/route.ts"),
+ database: join(root, "server/runtime/sqlite-database.ts"),
+ migrate: join(root, "server/runtime/migrate.ts"),
+ },
+ outdir: outDir,
+ outExtension: { ".js": ".mjs" },
+ bundle: true,
+ platform: "node",
+ format: "esm",
+ packages: "external",
+ tsconfig: join(root, "tsconfig.json"),
+ logLevel: "silent",
+ plugins: [{
+ name: "message-route-fixtures",
+ setup(builder) {
+ builder.onResolve({ filter: /^next\/server$/ }, () => ({ path: "next/server.js", external: true }));
+ builder.onResolve({ filter: /^@\/lib\// }, (args) => args.path in mocks ? { path: args.path, namespace: "mock" } : undefined);
+ builder.onLoad({ filter: /.*/, namespace: "mock" }, (args) => ({ contents: mocks[args.path], loader: "js" }));
+ },
+ }],
+});
+const { GET } = await import(pathToFileURL(join(outDir, "route.mjs")).href);
+const { SqliteDatabase } = await import(pathToFileURL(join(outDir, "database.mjs")).href);
+const { applyMigrations } = await import(pathToFileURL(join(outDir, "migrate.mjs")).href);
+const request = (query = "") => new Request(`https://mail.example/api/messages?${query}`);
+
+async function fixture(t) {
+ const database = new SqliteDatabase(":memory:");
+ t.after(() => database.db.close());
+ await applyMigrations(database, join(root, "drizzle/migrations"));
+ database.db.exec(`
+ INSERT INTO users (id, email, name, password_hash, role, created_at) VALUES
+ ('owner', '[email protected]', 'Owner', 'x', 'admin', 0),
+ ('other', '[email protected]', 'Other', 'x', 'user', 0);
+ INSERT INTO domains (id, user_id, hostname, zone_id, created_at) VALUES ('domain', 'owner', 'example.com', 'zone', 0);
+ INSERT INTO mailboxes (id, user_id, domain_id, local_part, type, disabled, created_at) VALUES
+ ('mine', 'owner', 'domain', 'owner', 'personal', 0, 0),
+ ('shared', 'other', 'domain', 'shared', 'shared', 0, 0),
+ ('private', 'other', 'domain', 'other', 'personal', 0, 0),
+ ('disabled', 'owner', 'domain', 'disabled', 'personal', 1, 0);
+ INSERT INTO mailbox_access (id, mailbox_id, user_id, permission, created_at) VALUES ('grant', 'shared', 'owner', 'read_only', 0);
+ INSERT INTO license_settings (id, instance_id, plan, state, updated_at) VALUES ('default', 'test', 'team', 'active', 0);
+ `);
+ const state = {
+ database,
+ env: { DB: database },
+ user: { id: "owner", email: "[email protected]", name: "Owner", role: "admin" },
+ queries: [],
+ started: [],
+ async read(label) { this.started.push(label); },
+ async contacts(userId, addresses) {
+ await this.read(`contacts:${userId}`);
+ assert.ok(addresses.includes("[email protected]"));
+ return new Map([["[email protected]", `Friend of ${userId}`]]);
+ },
+ async opens(ids) {
+ await this.read("opens");
+ return new Map(ids.map((id) => [id, 3]));
+ },
+ message(id, { mailbox = "mine", user = "owner", thread = "thread", status = "received", direction = "inbound", read = false, time = 1 } = {}) {
+ database.db.prepare(`INSERT INTO messages (id, user_id, mailbox_id, direction, from_addr, to_addr, status, read, thread_id, snippet, text_body, html_body, raw_r2_key, created_at)
+ VALUES (?, ?, ?, ?, '[email protected]', '[email protected]', ?, ?, ?, 'preview', 'private body', '<p>private</p>', 'raw-key', ?)`).run(id, user, mailbox, direction, status, Number(read), thread, time);
+ },
+ };
+ const prepare = database.prepare.bind(database);
+ database.prepare = (sql) => {
+ state.queries.push(sql);
+ function wrap(statement) {
+ const bind = statement.bind.bind(statement);
+ statement.bind = (...args) => wrap(bind(...args));
+ const raw = statement.raw.bind(statement);
+ statement.raw = async (...args) => {
+ let label;
+ if (sql.includes('from "messages"')) {
+ label = sql.includes("group by") ? "threads" : sql.startsWith("select count(*)") ? "total" : "rows";
+ } else if (sql.includes('from "outbound_jobs"')) label = "jobs";
+ if (label) await state.read(label);
+ return raw(...args);
+ };
+ return statement;
+ }
+ return wrap(prepare(sql));
+ };
+ globalThis.messagesRouteFixture = state;
+ return state;
+}
+
+test("listed owned and read-only shared mailboxes reuse authorization without a second lookup", async (t) => {
+ const f = await fixture(t);
+ f.message("owned");
+ f.message("delegated", { mailbox: "shared", user: "other" });
+ for (const [mailbox, id] of [["mine", "owned"], ["shared", "delegated"]]) {
+ f.queries.length = 0;
+ const response = await GET(request(`mailboxId=${mailbox}`));
+ assert.equal(response.status, 200);
+ assert.deepEqual((await response.json()).messages.map((row) => row.id), [id]);
+ assert.equal(f.queries.filter((sql) => sql.includes('from "mailboxes"') && !sql.includes("join")).length, 0);
+ }
+});
+
+test("missing, disabled, private, invalid grants and inactive sharing remain inaccessible", async (t) => {
+ const f = await fixture(t);
+ for (const mailbox of ["missing", "disabled", "private"]) {
+ assert.equal((await GET(request(`mailboxId=${mailbox}`))).status, 404);
+ }
+ f.database.db.exec("UPDATE mailbox_access SET permission = 'invalid'");
+ assert.equal((await GET(request("mailboxId=shared"))).status, 404);
+ f.database.db.exec("UPDATE mailbox_access SET permission = 'read_only'; UPDATE license_settings SET state = 'inactive'");
+ assert.equal((await GET(request("mailboxId=shared"))).status, 404);
+ assert.equal(f.started.length, 0, "denied requests never load mail or enrichment");
+ f.user = null;
+ f.queries.length = 0;
+ assert.equal((await GET(request())).status, 401);
+ assert.equal(f.queries.length, 0);
+});
+
+test("explicit orphaned mailbox keeps the existing access fallback", async (t) => {
+ const f = await fixture(t);
+ f.database.db.pragma("foreign_keys = OFF");
+ f.database.db.exec("UPDATE mailboxes SET domain_id = 'missing' WHERE id = 'mine'");
+ f.database.db.pragma("foreign_keys = ON");
+ f.message("orphan");
+ assert.equal((await GET(request("mailboxId=mine"))).status, 200);
+});
+
+// Hold reads at their asynchronous boundary, then inspect which siblings started.
+// No elapsed-time performance threshold: the assertion measures overlap, not speed.
+async function holdReads(f, labels, callback) {
+ const gate = Promise.withResolvers();
+ const entered = Promise.withResolvers();
+ f.read = async (label) => {
+ f.started.push(label);
+ if (labels.includes(label)) {
+ entered.resolve();
+ await gate.promise;
+ }
+ };
+ const pending = GET(request("mailboxId=mine&scheduled=true"));
+ try {
+ await entered.promise;
+ await setImmediate();
+ callback();
+ } finally {
+ gate.resolve();
+ await pending;
+ }
+ return (await pending).json();
+}
+
+function schedule(f) {
+ f.message("scheduled", { direction: "outbound", status: "queued" });
+ f.database.db.exec("INSERT INTO outbound_jobs (id, user_id, message_id, payload, scheduled_at, created_at, updated_at) VALUES ('job', 'owner', 'scheduled', '{}', 2000000000, 0, 0)");
+}
+
+test("ungrouped total and page reads start together", async (t) => {
+ const f = await fixture(t);
+ schedule(f);
+ const body = await holdReads(f, ["total"], () => assert.ok(f.started.includes("rows"), "page read must start before count resolves"));
+ assert.equal(body.total, 1);
+ assert.equal(body.messages.length, 1);
+});
+
+test("thread, schedule, tracking and contact enrichment reads overlap", async (t) => {
+ const f = await fixture(t);
+ schedule(f);
+ const body = await holdReads(f, ["threads", "jobs", "opens", "contacts:owner"], () => {
+ for (const label of ["threads", "jobs", "opens", "contacts:owner"]) assert.ok(f.started.includes(label), `${label} must start before other enrichment resolves`);
+ });
+ const [message] = body.messages;
+ assert.equal(message.scheduledAt, "2033-05-18T03:33:20.000Z");
+ assert.equal(message.openCount, 3);
+ assert.equal(message.fromContactName, "Owner");
+ assert.equal(message.toContactName, "Friend of owner");
+ assert.equal(message.snippet, "preview");
+ for (const key of ["textBody", "htmlBody", "rawR2Key"]) assert.equal(key in message, false);
+});
+
+test("grouped unread counts and actions stay folder-scoped while thread totals exclude draft and trash", async (t) => {
+ const f = await fixture(t);
+ f.message("old", { time: 1 });
+ f.message("head", { time: 2, read: true });
+ f.message("archive", { status: "archived", time: 3 });
+ f.message("draft", { status: "draft", time: 4 });
+ f.message("trash", { status: "trash", time: 5 });
+ f.message("private", { mailbox: "private", user: "other", time: 6 });
+ const response = await GET(request("mailboxId=mine&status=received&group=thread"));
+ const body = await response.json();
+ assert.equal(body.grouped, true);
+ assert.equal(body.total, 1);
+ assert.equal(body.messages[0].id, "head");
+ assert.equal(body.messages[0].threadCount, 3);
+ assert.equal(body.messages[0].threadUnread, 1);
+ assert.deepEqual(body.messages[0].threadMessageIds.sort(), ["head", "old"]);
+ const ungrouped = await (await GET(request("mailboxId=mine&status=received&limit=1&offset=1"))).json();
+ assert.equal(ungrouped.total, 2);
+ assert.equal(ungrouped.messages[0].id, "old");
+ assert.equal(ungrouped.messages[0].threadUnread, 2);
+ const drafts = await (await GET(request("mailboxId=mine&status=draft&group=thread"))).json();
+ assert.equal(drafts.grouped, false);
+ assert.equal(drafts.messages[0].id, "draft");
+});
+
+test("all-mailbox lists keep per-owner contacts and reflect revoked sharing on the next request", async (t) => {
+ const f = await fixture(t);
+ f.message("own");
+ f.message("shared", { mailbox: "shared", user: "other", time: 2 });
+ f.message("private", { mailbox: "private", user: "other", time: 3 });
+ const body = await (await GET(request())).json();
+ assert.deepEqual(body.messages.map((row) => [row.id, row.fromContactName]), [["shared", "Friend of other"], ["own", "Friend of owner"]]);
+ f.database.db.exec("DELETE FROM mailbox_access");
+ assert.deepEqual((await (await GET(request())).json()).messages.map((row) => row.id), ["own"]);
+ assert.equal((await GET(request("mailboxId=shared"))).status, 404);
+});
+
+test("empty pages preserve total and skip unnecessary thread and scheduled reads", async (t) => {
+ const f = await fixture(t);
+ schedule(f);
+ const body = await (await GET(request("mailboxId=mine&scheduled=true&offset=100"))).json();
+ assert.equal(body.total, 1);
+ assert.deepEqual(body.messages, []);
+ assert.ok(!f.started.includes("threads"));
+ assert.ok(!f.started.includes("jobs"));
+ assert.ok(!f.started.some((label) => label.startsWith("contacts:")));
+});
diff --git a/wrangler.jsonc b/wrangler.jsonc
index ba96658..1c18a23 100644
--- a/wrangler.jsonc
+++ b/wrangler.jsonc
@@ -62,6 +62,7 @@
],
"observability": {
"enabled": true,
+ "traces": { "enabled": true },
},
"upload_source_maps": true,
"send_email": [
@@ -108,6 +109,8 @@
{
"queue": "mailflare-outbound",
"max_batch_size": 5,
+ // Sends wait out the undo window already; do not also wait for a batch to fill.
+ "max_batch_timeout": 1,
"max_retries": 3,
},
{