Files
mailflare/server/runtime/sqlite-database.ts
T
Oday BakkourandClaude Fable 5.1 f0dc5a16fc feat(self-host): run Mailflare as a Docker container on any host
Adds a second runtime next to Cloudflare Workers. One Node process serves
the Next app, a realtime WebSocket hub, the job queues, the backup schedule
and an SMTP listener; SQLite and local files replace D1 and R2. The
application code is unchanged: the runtime builds an object with the same
binding interfaces (`server/runtime/`) and publishes it before Next starts,
so `getEnv()` hands route handlers the same shape on both platforms.

Runtime (server/)
- `sqlite-database.ts`: the D1 API over better-sqlite3, so Drizzle's D1
  driver, the raw `env.DB.prepare` callers, the FTS5 index and its triggers
  all work as-is. Migrations are applied from drizzle/migrations at start,
  recorded in `d1_migrations` like Wrangler does.
- `file-bucket.ts`: the R2 API over a directory, with metadata sidecars and
  byte ranges. `mailer.ts`: the send_email builder over nodemailer (any SMTP
  relay) or Cloudflare's Email Sending REST API; generates the Message-ID so
  threading matches Workers. `queue.ts`: in-process queues with the same
  retry policy. `realtime.ts`: per-user WebSocket fan-out in place of the
  Durable Object. `misc.ts`: rate limiter and ASSETS fetcher.
- `smtp.ts`: inbound SMTP on port 25 via smtp-server; reject rules answer
  550, forward rules relay the raw message through the outbound SMTP.
- `index.ts`: the custom server; `scripts/build-server.mjs` bundles it with
  esbuild to dist/server.mjs.

Application changes
- `src/lib/runtime.ts` + `getEnv()` prefer the Node env when present.
- `src/lib/email/intake.ts` factors the Worker email handler's logic so the
  SMTP listener and the new signed `/api/inbound` webhook share it.
- Domains without Cloudflare credentials are recorded with zone id
  "manual": Cloudflare calls no-op, the domain is active at once, and the
  DNS page lists MX/SPF/DMARC records to create. Setup checks, the domains
  page copy and the self-update button adapt to the runtime; `/api/auth/me`
  exposes `runtime` and `managesDns`.
- `APP_URL` overrides the origin for reset links and the JMAP session
  behind a proxy. The direct `getCloudflareContext` import in the update
  route is replaced with `getEnv()`.

Deployment
- Dockerfile (multi-stage, 1.1 GB, Workers toolchain pruned), compose file,
  `.env.docker.example`, `docs/self-hosting.md`, README section.
- `deploy/cloudflare-email-relay`: a Worker that keeps MX on Cloudflare
  Email Routing and posts messages to the server with an HMAC signature,
  acting on the reject/forward decision it returns.

Verified on a fresh data directory and again inside the built image:
migrations apply, first-run setup and registration work with no Cloudflare
credentials, mail over SMTP lands in the inbox with the raw copy on disk,
a reject rule answers 550, a send through SMTP_URL loops back and is stored
as sent and received, an inbound reply threads with the sent copy, FTS
search finds body text, a backup completes to the volume, the WebSocket
delivers a push, the relay webhook rejects a bad signature and returns
store/reject decisions, JMAP serves a session with the APP_URL origin.
Container healthcheck reports healthy. tsc (11) and eslint (105) unchanged.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PGeWDVSkXtVEmDAN2AtB9h
2026-09-12 14:34:11 +03:00

129 lines
3.5 KiB
TypeScript

import Database from "better-sqlite3";
/**
* The D1 database API over a local SQLite file. Drizzle's D1 driver and the
* app's raw `env.DB.prepare(...)` calls both go through this, so the schema,
* migrations, FTS index and triggers are identical to a Cloudflare deploy.
*/
type Row = Record<string, unknown>;
function meta(info?: Database.RunResult) {
return {
duration: 0,
size_after: 0,
rows_read: 0,
rows_written: info?.changes ?? 0,
last_row_id: Number(info?.lastInsertRowid ?? 0),
changed_db: (info?.changes ?? 0) > 0,
changes: info?.changes ?? 0,
};
}
function normalizeParam(value: unknown): unknown {
if (value instanceof Date) return Math.floor(value.getTime() / 1000);
if (typeof value === "boolean") return value ? 1 : 0;
if (value instanceof ArrayBuffer) return Buffer.from(value);
if (ArrayBuffer.isView(value)) return Buffer.from(value.buffer, value.byteOffset, value.byteLength);
return value;
}
class SqlitePreparedStatement {
constructor(
private readonly db: Database.Database,
private readonly sql: string,
private readonly params: unknown[] = [],
) {}
bind(...values: unknown[]) {
return new SqlitePreparedStatement(this.db, this.sql, values.map(normalizeParam));
}
private statement() {
return this.db.prepare(this.sql);
}
private isRead(): boolean {
return /^\s*(select|with|pragma|explain)\b/i.test(this.sql) || /\breturning\b/i.test(this.sql);
}
async first<T = Row>(column?: string): Promise<T | null> {
const row = this.statement().get(...this.params) as Row | undefined;
if (!row) return null;
return (column ? row[column] : row) as T;
}
async run<T = Row>() {
if (this.isRead()) {
const results = this.statement().all(...this.params) as T[];
return { results, success: true as const, meta: meta() };
}
const info = this.statement().run(...this.params);
return { results: [] as T[], success: true as const, meta: meta(info) };
}
async all<T = Row>() {
return this.run<T>();
}
async raw<T = unknown[]>(options?: { columnNames?: boolean }): Promise<T[]> {
if (!this.isRead()) {
this.statement().run(...this.params);
return [];
}
const statement = this.statement();
const rows = statement.raw(true).all(...this.params) as T[];
if (options?.columnNames) {
const names = statement.columns().map((column) => column.name) as unknown as T;
return [names, ...rows];
}
return rows;
}
}
export class SqliteDatabase {
readonly db: Database.Database;
constructor(filename: string) {
this.db = new Database(filename);
this.db.pragma("journal_mode = WAL");
this.db.pragma("foreign_keys = ON");
this.db.pragma("busy_timeout = 5000");
}
prepare(sql: string) {
return new SqlitePreparedStatement(this.db, sql);
}
/** Run statements atomically, like D1's batch. */
async batch<T = Row>(statements: SqlitePreparedStatement[]) {
const results: Awaited<ReturnType<SqlitePreparedStatement["all"]>>[] = [];
this.db.exec("BEGIN");
try {
for (const statement of statements) results.push(await statement.all<T>());
this.db.exec("COMMIT");
} catch (error) {
this.db.exec("ROLLBACK");
throw error;
}
return results;
}
async exec(sql: string) {
this.db.exec(sql);
return { count: 0, duration: 0 };
}
async dump(): Promise<ArrayBuffer> {
const buffer = this.db.serialize();
return buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength) as ArrayBuffer;
}
withSession() {
return this;
}
}
export function openSqliteDatabase(filename: string): D1Database {
return new SqliteDatabase(filename) as unknown as D1Database;
}