All files / sync r2-poller.ts

87.96% Statements 117/133
73.46% Branches 36/49
75.86% Functions 22/29
92.8% Lines 116/125

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407                                            16x 16x 16x                                                                                                       53x 53x     53x 53x 53x 53x               48x                           48x 17x 17x           4x 1x   4x         2x 2x 2x 2x         32x 32x 32x                   2x   2x 2x 1x 1x 1x   1x         2x               18x 18x 17x               7x       7x 5x   7x       21x                             21x 21x 1x         1x 1x           18x   18x 18x   18x 18x 18x 14x 14x 14x 14x 18x   4x 4x 4x 4x     4x                     4x   18x 18x       18x 18x 18x               18x 18x   16x 16x 15x   14x               18x 18x 18x 18x   18x             18x 18x 2x 2x     16x 16x                   15x 15x             15x 15x 1x     14x       14x 14x     1x 1x 1x     13x 1x 1x 1x         12x 12x     1x           11x 11x         13x 13x             13x   13x 1x                       20x   20x 20x 19x   20x         19x                         28x   56x      
/**
 * Cloudflare R2 queue poller for stork.
 *
 * Implements the queue/poll half of the durable email ingest model:
 *   1. A Cloudflare Email Worker writes each inbound email as a JSON object to
 *      an R2 bucket under a configurable prefix (default: "pending/").
 *   2. This poller lists the bucket on a configurable interval, downloads each
 *      pending object, parses and stores the email, then deletes the object.
 *   3. Objects are only deleted after a successful DB write — if stork is
 *      locked or the write fails, the object stays in R2 and is retried on
 *      the next poll cycle.
 *   4. Message-ID deduplication in storeInboundEmail prevents duplicates on
 *      the rare path where a crash occurs between write and delete.
 *
 * Authentication uses AWS SigV4 with Cloudflare R2's S3-compatible API.
 * The region string for R2 is "auto".
 */
 
import type Database from "better-sqlite3-multiple-ciphers";
import { storeInboundEmail } from "../storage/email-storage.js";
import { signR2Request } from "./r2-sigv4.js";
 
const DEFAULT_POLL_INTERVAL_MS = 60_000; // 60 seconds
const MAX_BACKOFF_MS = 30 * 60 * 1000; // 30 minutes
const MAX_KEYS_PER_POLL = 100;
 
interface R2ConnectorRow {
	id: number;
	cf_r2_account_id: string;
	cf_r2_bucket_name: string;
	cf_r2_access_key_id: string;
	cf_r2_secret_access_key: string;
	cf_r2_prefix: string;
	cf_r2_poll_interval_ms: number | null;
}
 
interface PollerState {
	connectorId: number;
	cfAccountId: string;
	bucketName: string;
	accessKeyId: string;
	secretAccessKey: string;
	prefix: string;
	pollIntervalMs: number;
	timer: ReturnType<typeof setInterval> | null;
	running: boolean;
	pollPromise: Promise<void> | null;
	lastPoll: number | null;
	lastError: string | null;
	consecutiveErrors: number;
}
 
export interface R2PollerOptions {
	/** Default poll interval in ms when a connector has no per-connector override (default: 60s) */
	defaultPollIntervalMs?: number;
	/** Called after each successful poll cycle */
	onPollComplete?: (connectorId: number, stored: number) => void;
	/** Called when a poll cycle fails */
	onPollError?: (connectorId: number, error: Error) => void;
}
 
/**
 * Manages periodic polling of Cloudflare R2 buckets for queued inbound emails.
 *
 * Usage:
 *   const poller = new R2Poller(db, { onPollComplete, onPollError });
 *   poller.loadConnectorsFromDb();
 *   poller.start();
 *   // later:
 *   await poller.stop();
 */
export class R2Poller {
	private db: Database.Database;
	private defaultPollIntervalMs: number;
	private onPollComplete?: (connectorId: number, stored: number) => void;
	private onPollError?: (connectorId: number, error: Error) => void;
	private connectors: Map<number, PollerState> = new Map();
	private started = false;
 
	constructor(db: Database.Database, options: R2PollerOptions = {}) {
		this.db = db;
		this.defaultPollIntervalMs = options.defaultPollIntervalMs ?? DEFAULT_POLL_INTERVAL_MS;
		this.onPollComplete = options.onPollComplete;
		this.onPollError = options.onPollError;
	}
 
	/**
	 * Load all fully-configured cloudflare-r2 connectors from the database.
	 * Skips connectors with missing required fields (logs a warning).
	 */
	loadConnectorsFromDb(): void {
		const rows = this.db
			.prepare(
				`SELECT id, cf_r2_account_id, cf_r2_bucket_name,
					cf_r2_access_key_id, cf_r2_secret_access_key,
					cf_r2_prefix, cf_r2_poll_interval_ms
				FROM inbound_connectors
				WHERE type = 'cloudflare-r2'
					AND cf_r2_account_id IS NOT NULL
					AND cf_r2_bucket_name IS NOT NULL
					AND cf_r2_access_key_id IS NOT NULL
					AND cf_r2_secret_access_key IS NOT NULL`,
			)
			.all() as R2ConnectorRow[];
 
		for (const row of rows) {
			Iif (this.connectors.has(row.id)) continue;
			this.addConnectorFromRow(row);
		}
	}
 
	/** Add a single R2 connector at runtime (e.g. after creation via API). */
	addConnector(row: R2ConnectorRow): void {
		if (this.connectors.has(row.id)) {
			this.removeConnector(row.id);
		}
		this.addConnectorFromRow(row);
	}
 
	/** Remove a connector and stop polling it. */
	removeConnector(connectorId: number): void {
		const state = this.connectors.get(connectorId);
		Iif (!state) return;
		Iif (state.timer) clearInterval(state.timer);
		this.connectors.delete(connectorId);
	}
 
	/** Start the poller — begins periodic polling for all registered connectors. */
	start(): void {
		Iif (this.started) return;
		this.started = true;
		for (const state of this.connectors.values()) {
			this.startConnectorPoll(state);
		}
	}
 
	/**
	 * Stop all polling — cancels timers and waits for any in-flight polls to finish
	 * (up to 5 seconds).
	 */
	async stop(): Promise<void> {
		this.started = false;
 
		const pending: Promise<void>[] = [];
		for (const state of this.connectors.values()) {
			Eif (state.timer) {
				clearInterval(state.timer);
				state.timer = null;
			}
			Iif (state.pollPromise) {
				pending.push(state.pollPromise);
			}
		}
 
		Iif (pending.length > 0) {
			const timeout = new Promise<void>((resolve) => setTimeout(resolve, 5000));
			await Promise.race([Promise.allSettled(pending), timeout]);
		}
	}
 
	/** Trigger an immediate poll for a specific connector. */
	async pollNow(connectorId: number): Promise<number> {
		const state = this.connectors.get(connectorId);
		if (!state) throw new Error(`R2 connector ${connectorId} is not registered`);
		return this.runPoll(state);
	}
 
	/** Return status of all registered connectors. */
	getStatus(): Map<
		number,
		{ running: boolean; lastPoll: number | null; lastError: string | null }
	> {
		const out = new Map<
			number,
			{ running: boolean; lastPoll: number | null; lastError: string | null }
		>();
		for (const [id, s] of this.connectors) {
			out.set(id, { running: s.running, lastPoll: s.lastPoll, lastError: s.lastError });
		}
		return out;
	}
 
	private addConnectorFromRow(row: R2ConnectorRow): void {
		const state: PollerState = {
			connectorId: row.id,
			cfAccountId: row.cf_r2_account_id,
			bucketName: row.cf_r2_bucket_name,
			accessKeyId: row.cf_r2_access_key_id,
			secretAccessKey: row.cf_r2_secret_access_key,
			prefix: row.cf_r2_prefix ?? "pending/",
			pollIntervalMs: row.cf_r2_poll_interval_ms ?? this.defaultPollIntervalMs,
			timer: null,
			running: false,
			pollPromise: null,
			lastPoll: null,
			lastError: null,
			consecutiveErrors: 0,
		};
		this.connectors.set(row.id, state);
		if (this.started) {
			this.startConnectorPoll(state);
		}
	}
 
	private startConnectorPoll(state: PollerState): void {
		this.runPoll(state).catch(() => {});
		state.timer = setInterval(() => {
			this.runPoll(state).catch(() => {});
		}, state.pollIntervalMs);
	}
 
	private async runPoll(state: PollerState): Promise<number> {
		Iif (state.running) return 0;
 
		state.running = true;
		const connectorId = state.connectorId;
 
		const doRun = async (): Promise<number> => {
			try {
				const stored = await this.pollConnector(state);
				state.lastPoll = Date.now();
				state.lastError = null;
				state.consecutiveErrors = 0;
				this.onPollComplete?.(connectorId, stored);
				return stored;
			} catch (err) {
				const error = err instanceof Error ? err : new Error(String(err));
				state.lastError = error.message;
				state.consecutiveErrors++;
				this.onPollError?.(connectorId, error);
 
				// Apply exponential backoff
				Iif (state.consecutiveErrors > 1 && state.timer) {
					clearInterval(state.timer);
					const backoffMs = Math.min(
						state.pollIntervalMs * 2 ** (state.consecutiveErrors - 1),
						MAX_BACKOFF_MS,
					);
					state.timer = setInterval(() => {
						this.runPoll(state).catch(() => {});
					}, backoffMs);
				}
 
				throw error;
			} finally {
				state.running = false;
				state.pollPromise = null;
			}
		};
 
		const promise = doRun();
		state.pollPromise = promise.then(() => {}).catch(() => {});
		return promise;
	}
 
	/**
	 * Execute one poll cycle for a connector:
	 * list pending objects → download each → parse + store → delete on success.
	 */
	private async pollConnector(state: PollerState): Promise<number> {
		const baseUrl = `https://${state.cfAccountId}.r2.cloudflarestorage.com`;
		const keys = await this.listObjects(state, baseUrl);
 
		let stored = 0;
		for (const key of keys) {
			stored += await this.processObject(state, baseUrl, key);
		}
		return stored;
	}
 
	/**
	 * List object keys in the R2 bucket under the configured prefix.
	 * Uses the S3 ListObjectsV2 API (list-type=2).
	 */
	private async listObjects(state: PollerState, baseUrl: string): Promise<string[]> {
		const url = new URL(`${baseUrl}/${state.bucketName}`);
		url.searchParams.set("list-type", "2");
		url.searchParams.set("prefix", state.prefix);
		url.searchParams.set("max-keys", String(MAX_KEYS_PER_POLL));
 
		const headers = signR2Request({
			method: "GET",
			url,
			accessKeyId: state.accessKeyId,
			secretAccessKey: state.secretAccessKey,
		});
 
		const res = await fetch(url.toString(), { method: "GET", headers });
		if (!res.ok) {
			const body = await res.text().catch(() => "");
			throw new Error(`R2 list failed: ${res.status} ${res.statusText} — ${body}`);
		}
 
		const xml = await res.text();
		return parseListObjectsKeys(xml);
	}
 
	/**
	 * Download, parse, and store one R2 object as an inbound email.
	 * Deletes the object from R2 on success or on unrecoverable parse errors.
	 * Returns the number of identities the message was stored for.
	 */
	private async processObject(state: PollerState, baseUrl: string, key: string): Promise<number> {
		// Download the object
		const getUrl = new URL(`${baseUrl}/${state.bucketName}/${encodeObjectKey(key)}`);
		const getHeaders = signR2Request({
			method: "GET",
			url: getUrl,
			accessKeyId: state.accessKeyId,
			secretAccessKey: state.secretAccessKey,
		});
 
		const res = await fetch(getUrl.toString(), { method: "GET", headers: getHeaders });
		if (!res.ok) {
			throw new Error(`R2 get failed for ${key}: ${res.status} ${res.statusText}`);
		}
 
		const body = await res.text();
 
		// Parse the payload
		let payload: { from: string; to: string; raw: string; rawSize: number };
		try {
			payload = JSON.parse(body);
		} catch (err) {
			// Unrecoverable — bad JSON will never succeed on retry; delete and skip
			console.error(`[r2-poller] Deleting unparseable object ${key}: ${err}`);
			await this.deleteObject(state, baseUrl, key);
			return 0;
		}
 
		if (!payload.raw || typeof payload.raw !== "string") {
			console.error(`[r2-poller] Deleting malformed payload for ${key}: missing 'raw' field`);
			await this.deleteObject(state, baseUrl, key);
			return 0;
		}
 
		// Store in DB — if this throws (e.g. locked container), the object stays in R2
		let result: { stored: number };
		try {
			result = await storeInboundEmail(this.db, state.connectorId, payload);
		} catch (err) {
			// Transient DB error — leave in R2 for retry
			throw new Error(
				`DB write failed for ${key}: ${err instanceof Error ? err.message : String(err)}`,
			);
		}
 
		// Delete from R2 after successful write (or if all deliveries were duplicates)
		await this.deleteObject(state, baseUrl, key);
		return result.stored;
	}
 
	/** Delete an object from R2. Errors are logged but not rethrown. */
	private async deleteObject(state: PollerState, baseUrl: string, key: string): Promise<void> {
		const url = new URL(`${baseUrl}/${state.bucketName}/${encodeObjectKey(key)}`);
		const headers = signR2Request({
			method: "DELETE",
			url,
			accessKeyId: state.accessKeyId,
			secretAccessKey: state.secretAccessKey,
		});
 
		const res = await fetch(url.toString(), { method: "DELETE", headers });
		// 204 = deleted, 404 = already gone — both are fine
		if (!res.ok && res.status !== 404) {
			console.error(
				`[r2-poller] Failed to delete R2 object ${key}: ${res.status} ${res.statusText}`,
			);
		}
	}
}
 
/**
 * Extract object keys from an S3 ListObjectsV2 XML response.
 * Keys appear as <Key>...</Key> inside <Contents> blocks.
 */
export function parseListObjectsKeys(xml: string): string[] {
	const keys: string[] = [];
	// Match all <Key>...</Key> elements (contents keys)
	const re = /<Key>([^<]+)<\/Key>/g;
	for (const match of xml.matchAll(re)) {
		keys.push(unescapeXml(match[1]));
	}
	return keys;
}
 
/** Unescape XML character references in an attribute value. */
function unescapeXml(s: string): string {
	return s
		.replace(/&amp;/g, "&")
		.replace(/&lt;/g, "<")
		.replace(/&gt;/g, ">")
		.replace(/&apos;/g, "'")
		.replace(/&quot;/g, '"');
}
 
/**
 * Encode an R2 object key for use in a URL path component.
 * Encodes all characters except '/' (which separates path segments in key names).
 */
function encodeObjectKey(key: string): string {
	return key
		.split("/")
		.map((seg) => encodeURIComponent(seg))
		.join("/");
}