@pramen/server 0.0.11 → 0.0.12

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.
@@ -17,6 +17,7 @@
17
17
  import { DurableObject } from "cloudflare:workers";
18
18
  import { migrate } from "./runtime/migrate";
19
19
  import { dispatch, tasksFacade, bindTasks } from "./runtime/dispatch";
20
+ import { createMail } from "./runtime/mail";
20
21
  import { ensureOutbox, drainOutbox, listTasks } from "./runtime/outbox";
21
22
  import { Db } from "./runtime/db";
22
23
  import { digest } from "./runtime/digest";
@@ -154,7 +155,15 @@ export class PramenDOBase extends DurableObject {
154
155
  taskCtx() {
155
156
  const identity = { roles: ["admin"] };
156
157
  const db = new Db(this.driver, { acl: this.acl, identity, system: true, schema: this.app.schema, partition: this.partition, suppressTriggers: true }, this.app.schema);
157
- return { db, kv: this.kv, files: this.filesFor(this.tenant), env: this.envBag, identity, tasks: tasksFacade(this.driver) };
158
+ return {
159
+ db,
160
+ kv: this.kv,
161
+ files: this.filesFor(this.tenant),
162
+ env: this.envBag,
163
+ identity,
164
+ tasks: tasksFacade(this.driver),
165
+ mail: createMail(this.envBag, this.kv),
166
+ };
158
167
  }
159
168
  /** Restore (tenant, partition) on a cold wake (e.g. an alarm with no request) so the
160
169
  * task context is scoped correctly. No-op once loaded/persisted this instance. */
package/dist/index.d.ts CHANGED
@@ -11,6 +11,8 @@ export type { Cell, FieldsOf, InferInsert, InferRow, InferUpdate, JsonValue, Pro
11
11
  export type { FileRef, Files, SignUploadOpts, SignDownloadOpts, HeadResult } from "./sdk/files";
12
12
  export { R2Adapter, MemoryAdapter, createFiles, handleFileRequest } from "./runtime/storage";
13
13
  export type { StorageAdapter, PutResult, GetResult } from "./runtime/storage";
14
+ export { Mail, CloudflareEmailAdapter, KvMailAdapter, MemoryMailAdapter, UnconfiguredMailAdapter, createMail } from "./runtime/mail";
15
+ export type { MailMessage, MailAddress, MailAdapter, SendEmailBinding } from "./runtime/mail";
14
16
  export { PramenError, BadRequest, Unauthorized, Forbidden } from "./runtime/errors";
15
17
  export { sqliteDialect, postgresDialect, DoSqliteDriver, D1Driver } from "./runtime/driver";
16
18
  export type { Driver, Dialect, Row } from "./runtime/driver";
package/dist/index.js CHANGED
@@ -15,6 +15,8 @@ export { query, mutation } from "./sdk/handlers";
15
15
  // --- ACL ---
16
16
  export { $identity, $input, allow, deny, policy, resolve, role, isAllow, isDeny, isResolver, isIdentityMarker, isInputMarker } from "./sdk/acl";
17
17
  export { R2Adapter, MemoryAdapter, createFiles, handleFileRequest } from "./runtime/storage";
18
+ // --- mail (ctx.mail) ---
19
+ export { Mail, CloudflareEmailAdapter, KvMailAdapter, MemoryMailAdapter, UnconfiguredMailAdapter, createMail } from "./runtime/mail";
18
20
  // --- errors ---
19
21
  export { PramenError, BadRequest, Unauthorized, Forbidden } from "./runtime/errors";
20
22
  // --- substrate seam (advanced: bring your own SQL backend) ---
@@ -11,6 +11,7 @@ import { Db } from "./db";
11
11
  import { warmup } from "./acl";
12
12
  import { BadRequest } from "./errors";
13
13
  import { enqueueTask } from "./outbox";
14
+ import { createMail } from "./mail";
14
15
  /** The `ctx.tasks` facade over the outbox. `onEnqueue` lets the caller count enqueues
15
16
  * so it can wake the drainer. */
16
17
  export function tasksFacade(driver, onEnqueue) {
@@ -48,7 +49,15 @@ export async function dispatch(handlers, schema, driver, kv, files, env, acl, na
48
49
  const resolved = await warmup(acl.acl, acl.identity, systemDb);
49
50
  const db = new Db(driver, { acl: acl.acl, identity: acl.identity, input: parsed, resolved, schema, partition: acl.partition }, schema);
50
51
  let enqueued = 0;
51
- const ctx = { db, kv, files, env, identity: acl.identity, tasks: tasksFacade(driver, () => enqueued++) };
52
+ const ctx = {
53
+ db,
54
+ kv,
55
+ files,
56
+ env,
57
+ identity: acl.identity,
58
+ tasks: tasksFacade(driver, () => enqueued++),
59
+ mail: createMail(env, kv),
60
+ };
52
61
  const result = handler.kind === "query"
53
62
  ? await handler.run(ctx, parsed)
54
63
  : await driver.transaction(async () => handler.run(ctx, parsed));
@@ -0,0 +1,80 @@
1
+ import type { Kv } from "./kv";
2
+ export interface MailAddress {
3
+ email: string;
4
+ name?: string;
5
+ }
6
+ export interface MailMessage {
7
+ to: string | string[];
8
+ /** Sender. Optional — defaults to MAIL_FROM (a verified address). */
9
+ from?: MailAddress;
10
+ subject: string;
11
+ text?: string;
12
+ html?: string;
13
+ replyTo?: string | MailAddress;
14
+ }
15
+ /** The transport seam. One per backend (Cloudflare Email Sending, a dev stash, …). */
16
+ export interface MailAdapter {
17
+ /** Deliver a fully-resolved message (`from` already filled by the facade). */
18
+ send(message: MailMessage & {
19
+ from: MailAddress;
20
+ }): Promise<void>;
21
+ }
22
+ /** The `ctx.mail` facade: resolves the sender, validates, and delegates to the adapter. */
23
+ export declare class Mail {
24
+ private readonly adapter;
25
+ private readonly defaultFrom?;
26
+ constructor(adapter: MailAdapter, defaultFrom?: MailAddress | undefined);
27
+ send(message: MailMessage): Promise<void>;
28
+ }
29
+ /** The Cloudflare `send_email` binding shape (workers binding form: `from` uses `email`). */
30
+ export interface SendEmailBinding {
31
+ send(message: {
32
+ to: string | string[];
33
+ from: MailAddress;
34
+ subject: string;
35
+ text?: string;
36
+ html?: string;
37
+ replyTo?: string | MailAddress;
38
+ }): Promise<void>;
39
+ }
40
+ /** Cloudflare Email Sending — sends via the `send_email` binding (no API keys). The
41
+ * `from` domain must be onboarded (`wrangler email sending enable yourdomain.com`). */
42
+ export declare class CloudflareEmailAdapter implements MailAdapter {
43
+ private readonly binding;
44
+ constructor(binding: SendEmailBinding);
45
+ send(message: MailMessage & {
46
+ from: MailAddress;
47
+ }): Promise<void>;
48
+ }
49
+ /** Dev/test transport: stash the message in KV under `mail:<recipient>` so an e2e suite
50
+ * (or a dashboard) can read the "inbox" instead of really sending. */
51
+ export declare class KvMailAdapter implements MailAdapter {
52
+ private readonly kv;
53
+ constructor(kv: Kv);
54
+ send(message: MailMessage & {
55
+ from: MailAddress;
56
+ }): Promise<void>;
57
+ }
58
+ /** In-memory transport: captures sent messages (pure; for unit tests). */
59
+ export declare class MemoryMailAdapter implements MailAdapter {
60
+ readonly sent: Array<MailMessage & {
61
+ from: MailAddress;
62
+ }>;
63
+ send(message: MailMessage & {
64
+ from: MailAddress;
65
+ }): Promise<void>;
66
+ }
67
+ /** Fail-closed transport: no real sender and no explicit dev-capture opt-in, so a
68
+ * `send` THROWS rather than silently capturing. Prevents a misconfigured production
69
+ * (no MAIL_FROM) from writing security emails — magic-link tokens, resets — into KV
70
+ * instead of delivering them. Mirrors how files fail closed without FILES_SECRET. */
71
+ export declare class UnconfiguredMailAdapter implements MailAdapter {
72
+ send(): Promise<void>;
73
+ }
74
+ /** Build `ctx.mail` from the environment:
75
+ * - `EMAIL` binding + `MAIL_FROM` → Cloudflare Email Sending (real send).
76
+ * - else `MAIL_CAPTURE=true` → capture (KV inbox if a Kv is given, else in-memory) with
77
+ * a synthetic dev sender — an EXPLICIT dev opt-in, never the production default.
78
+ * - else → fail closed: a `send` throws (so a missing-MAIL_FROM prod doesn't silently
79
+ * stash security emails in KV). */
80
+ export declare function createMail(env: Readonly<Record<string, unknown>>, kv?: Kv): Mail;
@@ -0,0 +1,102 @@
1
+ // ctx.mail — transactional-ish email facade, the same shape as ctx.files: an adapter
2
+ // seam (CloudflareEmailAdapter / KvMailAdapter / MemoryMailAdapter) behind a thin
3
+ // `Mail` facade, chosen from the environment. Handlers send mail without touching the
4
+ // `send_email` binding directly:
5
+ //
6
+ // await ctx.mail.send({ to: "u@x.com", subject: "Welcome", text: "…" });
7
+ //
8
+ // On Cloudflare the transport is Cloudflare Email Sending (the `send_email`/`EMAIL`
9
+ // binding, no API keys). With no verified sender configured (local/dev), mail is
10
+ // captured instead of sent — to KV (so an e2e/dashboard can read the "inbox") or
11
+ // in-memory — so handlers work unchanged off-platform.
12
+ /** The `ctx.mail` facade: resolves the sender, validates, and delegates to the adapter. */
13
+ export class Mail {
14
+ adapter;
15
+ defaultFrom;
16
+ constructor(adapter, defaultFrom) {
17
+ this.adapter = adapter;
18
+ this.defaultFrom = defaultFrom;
19
+ }
20
+ async send(message) {
21
+ const to = Array.isArray(message.to) ? message.to : [message.to];
22
+ if (to.length === 0 || to.some((a) => typeof a !== "string" || a.length === 0)) {
23
+ throw new Error("ctx.mail.send: `to` is required");
24
+ }
25
+ if (typeof message.subject !== "string" || message.subject.length === 0) {
26
+ throw new Error("ctx.mail.send: `subject` is required");
27
+ }
28
+ const from = message.from ?? this.defaultFrom;
29
+ if (!from)
30
+ throw new Error("ctx.mail.send: no sender — set the MAIL_FROM var or pass `from`");
31
+ await this.adapter.send({ ...message, from });
32
+ }
33
+ }
34
+ /** Cloudflare Email Sending — sends via the `send_email` binding (no API keys). The
35
+ * `from` domain must be onboarded (`wrangler email sending enable yourdomain.com`). */
36
+ export class CloudflareEmailAdapter {
37
+ binding;
38
+ constructor(binding) {
39
+ this.binding = binding;
40
+ }
41
+ async send(message) {
42
+ await this.binding.send({
43
+ to: message.to,
44
+ from: message.from,
45
+ subject: message.subject,
46
+ text: message.text,
47
+ html: message.html,
48
+ replyTo: message.replyTo,
49
+ });
50
+ }
51
+ }
52
+ /** Dev/test transport: stash the message in KV under `mail:<recipient>` so an e2e suite
53
+ * (or a dashboard) can read the "inbox" instead of really sending. */
54
+ export class KvMailAdapter {
55
+ kv;
56
+ constructor(kv) {
57
+ this.kv = kv;
58
+ }
59
+ async send(message) {
60
+ const to = Array.isArray(message.to) ? message.to : [message.to];
61
+ const value = JSON.stringify({ from: message.from, subject: message.subject, text: message.text, html: message.html });
62
+ for (const addr of to)
63
+ await this.kv.put(`mail:${addr}`, value, { expirationTtl: 900 });
64
+ }
65
+ }
66
+ /** In-memory transport: captures sent messages (pure; for unit tests). */
67
+ export class MemoryMailAdapter {
68
+ sent = [];
69
+ async send(message) {
70
+ this.sent.push(message);
71
+ }
72
+ }
73
+ /** Fail-closed transport: no real sender and no explicit dev-capture opt-in, so a
74
+ * `send` THROWS rather than silently capturing. Prevents a misconfigured production
75
+ * (no MAIL_FROM) from writing security emails — magic-link tokens, resets — into KV
76
+ * instead of delivering them. Mirrors how files fail closed without FILES_SECRET. */
77
+ export class UnconfiguredMailAdapter {
78
+ async send() {
79
+ throw new Error("ctx.mail: no transport configured — set MAIL_FROM (with the EMAIL binding) to send, " +
80
+ "or MAIL_CAPTURE=true to capture in dev.");
81
+ }
82
+ }
83
+ /** Build `ctx.mail` from the environment:
84
+ * - `EMAIL` binding + `MAIL_FROM` → Cloudflare Email Sending (real send).
85
+ * - else `MAIL_CAPTURE=true` → capture (KV inbox if a Kv is given, else in-memory) with
86
+ * a synthetic dev sender — an EXPLICIT dev opt-in, never the production default.
87
+ * - else → fail closed: a `send` throws (so a missing-MAIL_FROM prod doesn't silently
88
+ * stash security emails in KV). */
89
+ export function createMail(env, kv) {
90
+ const binding = env.EMAIL;
91
+ const fromAddr = typeof env.MAIL_FROM === "string" && env.MAIL_FROM ? env.MAIL_FROM : undefined;
92
+ if (binding && fromAddr) {
93
+ const name = typeof env.MAIL_FROM_NAME === "string" ? env.MAIL_FROM_NAME : undefined;
94
+ return new Mail(new CloudflareEmailAdapter(binding), { email: fromAddr, name });
95
+ }
96
+ if (env.MAIL_CAPTURE === "true") {
97
+ const devFrom = { email: "dev@pramen.local", name: "pramen (dev)" };
98
+ return new Mail(kv ? new KvMailAdapter(kv) : new MemoryMailAdapter(), devFrom);
99
+ }
100
+ // Sentinel `from` so the facade delegates to the adapter, which throws the clear error.
101
+ return new Mail(new UnconfiguredMailAdapter(), { email: "unconfigured@invalid" });
102
+ }
@@ -1,5 +1,6 @@
1
1
  import type { Db } from "../runtime/db";
2
2
  import type { Kv } from "../runtime/kv";
3
+ import type { Mail } from "../runtime/mail";
3
4
  import type { Identity } from "./acl";
4
5
  import type { Files } from "./files";
5
6
  import type { SchemaDef } from "./schema";
@@ -12,6 +13,11 @@ export interface HandlerContext<S extends SchemaDef = SchemaDef> {
12
13
  /** Per-tenant file storage: mint signed upload/download urls, head/delete blobs.
13
14
  * Bytes flow through the Worker /files/* route, never through the DO. */
14
15
  readonly files: Files;
16
+ /** Send email: `ctx.mail.send({ to, subject, text/html })`. On Cloudflare this is
17
+ * Cloudflare Email Sending (the `send_email` binding); off-platform / unconfigured it
18
+ * captures instead of sending. Prefer enqueuing the send as a task (see `ctx.tasks`)
19
+ * so it runs off the single-writer write path. */
20
+ readonly mail: Mail;
15
21
  /** The Worker/DO environment — bindings (KV, R2, DB, …) plus vars and secrets
16
22
  * (AUTH_SECRET, plus anything in wrangler.jsonc / .dev.vars / `wrangler secret`).
17
23
  * Use it to call external services from handlers — Cloudflare bindings (e.g. the
package/dist/worker.js CHANGED
@@ -6,6 +6,7 @@
6
6
  import { authorizeTenant, HmacStrategy, isAdmin, JwksStrategy, resolveIdentity } from "./auth";
7
7
  import { dispatch, tasksFacade, bindTasks } from "./runtime/dispatch";
8
8
  import { ensureOutbox, drainOutbox, listTasks } from "./runtime/outbox";
9
+ import { createMail } from "./runtime/mail";
9
10
  import { migrate } from "./runtime/migrate";
10
11
  import { compileAcl } from "./runtime/acl";
11
12
  import { Db } from "./runtime/db";
@@ -110,7 +111,8 @@ export function makeWorker(app) {
110
111
  const identity = { roles: ["admin"] };
111
112
  const files = createFiles({ tenant: "main", secret: filesSecret(env), adapter: new R2Adapter(env.FILES) });
112
113
  const db = new Db(driver, { acl: d1Acl, identity, system: true, schema: app.schema, suppressTriggers: true }, app.schema);
113
- return { db, kv: new Kv(env.KV), files, env: env, identity, tasks: tasksFacade(driver) };
114
+ const kv = new Kv(env.KV);
115
+ return { db, kv, files, env: env, identity, tasks: tasksFacade(driver), mail: createMail(env, kv) };
114
116
  };
115
117
  /** Drain the D1 outbox in the Worker (no DO/alarm on this path) — called by the
116
118
  * /admin/tasks/drain route with `x-pramen-store: d1`, and by `scheduled()` (Cron). */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pramen/server",
3
- "version": "0.0.11",
3
+ "version": "0.0.12",
4
4
  "description": "pramen server runtime — schema, ACL, ORM, live queries, files, and the createPramen(app) factory for Cloudflare Workers + Durable Objects.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -18,6 +18,7 @@
18
18
  import { DurableObject } from "cloudflare:workers";
19
19
  import { migrate } from "./runtime/migrate";
20
20
  import { dispatch, tasksFacade, bindTasks } from "./runtime/dispatch";
21
+ import { createMail } from "./runtime/mail";
21
22
  import { ensureOutbox, drainOutbox, listTasks } from "./runtime/outbox";
22
23
  import { Db } from "./runtime/db";
23
24
  import { digest } from "./runtime/digest";
@@ -205,7 +206,15 @@ export class PramenDOBase extends DurableObject<DoEnv> {
205
206
  { acl: this.acl, identity, system: true, schema: this.app.schema, partition: this.partition, suppressTriggers: true },
206
207
  this.app.schema,
207
208
  );
208
- return { db, kv: this.kv, files: this.filesFor(this.tenant), env: this.envBag, identity, tasks: tasksFacade(this.driver) };
209
+ return {
210
+ db,
211
+ kv: this.kv,
212
+ files: this.filesFor(this.tenant),
213
+ env: this.envBag,
214
+ identity,
215
+ tasks: tasksFacade(this.driver),
216
+ mail: createMail(this.envBag, this.kv),
217
+ };
209
218
  }
210
219
 
211
220
  /** Restore (tenant, partition) on a cold wake (e.g. an alarm with no request) so the
package/src/index.ts CHANGED
@@ -72,6 +72,10 @@ export type { FileRef, Files, SignUploadOpts, SignDownloadOpts, HeadResult } fro
72
72
  export { R2Adapter, MemoryAdapter, createFiles, handleFileRequest } from "./runtime/storage";
73
73
  export type { StorageAdapter, PutResult, GetResult } from "./runtime/storage";
74
74
 
75
+ // --- mail (ctx.mail) ---
76
+ export { Mail, CloudflareEmailAdapter, KvMailAdapter, MemoryMailAdapter, UnconfiguredMailAdapter, createMail } from "./runtime/mail";
77
+ export type { MailMessage, MailAddress, MailAdapter, SendEmailBinding } from "./runtime/mail";
78
+
75
79
  // --- errors ---
76
80
  export { PramenError, BadRequest, Unauthorized, Forbidden } from "./runtime/errors";
77
81
 
@@ -12,6 +12,7 @@ import { Db } from "./db";
12
12
  import { warmup, type AclContext } from "./acl";
13
13
  import { BadRequest } from "./errors";
14
14
  import { enqueueTask, type TaskMap } from "./outbox";
15
+ import { createMail } from "./mail";
15
16
  import type { Driver } from "./driver";
16
17
  import type { Kv } from "./kv";
17
18
  import type { Files } from "../sdk/files";
@@ -76,7 +77,15 @@ export async function dispatch(
76
77
 
77
78
  const db = new Db(driver, { acl: acl.acl, identity: acl.identity, input: parsed, resolved, schema, partition: acl.partition }, schema);
78
79
  let enqueued = 0;
79
- const ctx: HandlerContext = { db, kv, files, env, identity: acl.identity, tasks: tasksFacade(driver, () => enqueued++) };
80
+ const ctx: HandlerContext = {
81
+ db,
82
+ kv,
83
+ files,
84
+ env,
85
+ identity: acl.identity,
86
+ tasks: tasksFacade(driver, () => enqueued++),
87
+ mail: createMail(env, kv),
88
+ };
80
89
 
81
90
  const result =
82
91
  handler.kind === "query"
@@ -0,0 +1,136 @@
1
+ // ctx.mail — transactional-ish email facade, the same shape as ctx.files: an adapter
2
+ // seam (CloudflareEmailAdapter / KvMailAdapter / MemoryMailAdapter) behind a thin
3
+ // `Mail` facade, chosen from the environment. Handlers send mail without touching the
4
+ // `send_email` binding directly:
5
+ //
6
+ // await ctx.mail.send({ to: "u@x.com", subject: "Welcome", text: "…" });
7
+ //
8
+ // On Cloudflare the transport is Cloudflare Email Sending (the `send_email`/`EMAIL`
9
+ // binding, no API keys). With no verified sender configured (local/dev), mail is
10
+ // captured instead of sent — to KV (so an e2e/dashboard can read the "inbox") or
11
+ // in-memory — so handlers work unchanged off-platform.
12
+
13
+ import type { Kv } from "./kv";
14
+
15
+ export interface MailAddress {
16
+ email: string;
17
+ name?: string;
18
+ }
19
+
20
+ export interface MailMessage {
21
+ to: string | string[];
22
+ /** Sender. Optional — defaults to MAIL_FROM (a verified address). */
23
+ from?: MailAddress;
24
+ subject: string;
25
+ text?: string;
26
+ html?: string;
27
+ replyTo?: string | MailAddress;
28
+ }
29
+
30
+ /** The transport seam. One per backend (Cloudflare Email Sending, a dev stash, …). */
31
+ export interface MailAdapter {
32
+ /** Deliver a fully-resolved message (`from` already filled by the facade). */
33
+ send(message: MailMessage & { from: MailAddress }): Promise<void>;
34
+ }
35
+
36
+ /** The `ctx.mail` facade: resolves the sender, validates, and delegates to the adapter. */
37
+ export class Mail {
38
+ constructor(
39
+ private readonly adapter: MailAdapter,
40
+ private readonly defaultFrom?: MailAddress,
41
+ ) {}
42
+
43
+ async send(message: MailMessage): Promise<void> {
44
+ const to = Array.isArray(message.to) ? message.to : [message.to];
45
+ if (to.length === 0 || to.some((a) => typeof a !== "string" || a.length === 0)) {
46
+ throw new Error("ctx.mail.send: `to` is required");
47
+ }
48
+ if (typeof message.subject !== "string" || message.subject.length === 0) {
49
+ throw new Error("ctx.mail.send: `subject` is required");
50
+ }
51
+ const from = message.from ?? this.defaultFrom;
52
+ if (!from) throw new Error("ctx.mail.send: no sender — set the MAIL_FROM var or pass `from`");
53
+ await this.adapter.send({ ...message, from });
54
+ }
55
+ }
56
+
57
+ /** The Cloudflare `send_email` binding shape (workers binding form: `from` uses `email`). */
58
+ export interface SendEmailBinding {
59
+ send(message: {
60
+ to: string | string[];
61
+ from: MailAddress;
62
+ subject: string;
63
+ text?: string;
64
+ html?: string;
65
+ replyTo?: string | MailAddress;
66
+ }): Promise<void>;
67
+ }
68
+
69
+ /** Cloudflare Email Sending — sends via the `send_email` binding (no API keys). The
70
+ * `from` domain must be onboarded (`wrangler email sending enable yourdomain.com`). */
71
+ export class CloudflareEmailAdapter implements MailAdapter {
72
+ constructor(private readonly binding: SendEmailBinding) {}
73
+ async send(message: MailMessage & { from: MailAddress }): Promise<void> {
74
+ await this.binding.send({
75
+ to: message.to,
76
+ from: message.from,
77
+ subject: message.subject,
78
+ text: message.text,
79
+ html: message.html,
80
+ replyTo: message.replyTo,
81
+ });
82
+ }
83
+ }
84
+
85
+ /** Dev/test transport: stash the message in KV under `mail:<recipient>` so an e2e suite
86
+ * (or a dashboard) can read the "inbox" instead of really sending. */
87
+ export class KvMailAdapter implements MailAdapter {
88
+ constructor(private readonly kv: Kv) {}
89
+ async send(message: MailMessage & { from: MailAddress }): Promise<void> {
90
+ const to = Array.isArray(message.to) ? message.to : [message.to];
91
+ const value = JSON.stringify({ from: message.from, subject: message.subject, text: message.text, html: message.html });
92
+ for (const addr of to) await this.kv.put(`mail:${addr}`, value, { expirationTtl: 900 });
93
+ }
94
+ }
95
+
96
+ /** In-memory transport: captures sent messages (pure; for unit tests). */
97
+ export class MemoryMailAdapter implements MailAdapter {
98
+ readonly sent: Array<MailMessage & { from: MailAddress }> = [];
99
+ async send(message: MailMessage & { from: MailAddress }): Promise<void> {
100
+ this.sent.push(message);
101
+ }
102
+ }
103
+
104
+ /** Fail-closed transport: no real sender and no explicit dev-capture opt-in, so a
105
+ * `send` THROWS rather than silently capturing. Prevents a misconfigured production
106
+ * (no MAIL_FROM) from writing security emails — magic-link tokens, resets — into KV
107
+ * instead of delivering them. Mirrors how files fail closed without FILES_SECRET. */
108
+ export class UnconfiguredMailAdapter implements MailAdapter {
109
+ async send(): Promise<void> {
110
+ throw new Error(
111
+ "ctx.mail: no transport configured — set MAIL_FROM (with the EMAIL binding) to send, " +
112
+ "or MAIL_CAPTURE=true to capture in dev.",
113
+ );
114
+ }
115
+ }
116
+
117
+ /** Build `ctx.mail` from the environment:
118
+ * - `EMAIL` binding + `MAIL_FROM` → Cloudflare Email Sending (real send).
119
+ * - else `MAIL_CAPTURE=true` → capture (KV inbox if a Kv is given, else in-memory) with
120
+ * a synthetic dev sender — an EXPLICIT dev opt-in, never the production default.
121
+ * - else → fail closed: a `send` throws (so a missing-MAIL_FROM prod doesn't silently
122
+ * stash security emails in KV). */
123
+ export function createMail(env: Readonly<Record<string, unknown>>, kv?: Kv): Mail {
124
+ const binding = env.EMAIL as SendEmailBinding | undefined;
125
+ const fromAddr = typeof env.MAIL_FROM === "string" && env.MAIL_FROM ? env.MAIL_FROM : undefined;
126
+ if (binding && fromAddr) {
127
+ const name = typeof env.MAIL_FROM_NAME === "string" ? env.MAIL_FROM_NAME : undefined;
128
+ return new Mail(new CloudflareEmailAdapter(binding), { email: fromAddr, name });
129
+ }
130
+ if (env.MAIL_CAPTURE === "true") {
131
+ const devFrom: MailAddress = { email: "dev@pramen.local", name: "pramen (dev)" };
132
+ return new Mail(kv ? new KvMailAdapter(kv) : new MemoryMailAdapter(), devFrom);
133
+ }
134
+ // Sentinel `from` so the facade delegates to the adapter, which throws the clear error.
135
+ return new Mail(new UnconfiguredMailAdapter(), { email: "unconfigured@invalid" });
136
+ }
@@ -4,6 +4,7 @@
4
4
 
5
5
  import type { Db } from "../runtime/db";
6
6
  import type { Kv } from "../runtime/kv";
7
+ import type { Mail } from "../runtime/mail";
7
8
  import type { Identity } from "./acl";
8
9
  import type { Files } from "./files";
9
10
  import type { SchemaDef } from "./schema";
@@ -17,6 +18,11 @@ export interface HandlerContext<S extends SchemaDef = SchemaDef> {
17
18
  /** Per-tenant file storage: mint signed upload/download urls, head/delete blobs.
18
19
  * Bytes flow through the Worker /files/* route, never through the DO. */
19
20
  readonly files: Files;
21
+ /** Send email: `ctx.mail.send({ to, subject, text/html })`. On Cloudflare this is
22
+ * Cloudflare Email Sending (the `send_email` binding); off-platform / unconfigured it
23
+ * captures instead of sending. Prefer enqueuing the send as a task (see `ctx.tasks`)
24
+ * so it runs off the single-writer write path. */
25
+ readonly mail: Mail;
20
26
  /** The Worker/DO environment — bindings (KV, R2, DB, …) plus vars and secrets
21
27
  * (AUTH_SECRET, plus anything in wrangler.jsonc / .dev.vars / `wrangler secret`).
22
28
  * Use it to call external services from handlers — Cloudflare bindings (e.g. the
package/src/worker.ts CHANGED
@@ -7,6 +7,7 @@
7
7
  import { authorizeTenant, HmacStrategy, isAdmin, JwksStrategy, resolveIdentity, type VerifyStrategy } from "./auth";
8
8
  import { dispatch, tasksFacade, bindTasks } from "./runtime/dispatch";
9
9
  import { ensureOutbox, drainOutbox, listTasks } from "./runtime/outbox";
10
+ import { createMail } from "./runtime/mail";
10
11
  import { migrate } from "./runtime/migrate";
11
12
  import { compileAcl } from "./runtime/acl";
12
13
  import { Db } from "./runtime/db";
@@ -149,7 +150,8 @@ export function makeWorker(app: PramenApp) {
149
150
  const identity: Identity = { roles: ["admin"] };
150
151
  const files = createFiles({ tenant: "main", secret: filesSecret(env), adapter: new R2Adapter(env.FILES) });
151
152
  const db = new Db(driver, { acl: d1Acl, identity, system: true, schema: app.schema, suppressTriggers: true }, app.schema);
152
- return { db, kv: new Kv(env.KV), files, env: env as unknown as Record<string, unknown>, identity, tasks: tasksFacade(driver) };
153
+ const kv = new Kv(env.KV);
154
+ return { db, kv, files, env: env as unknown as Record<string, unknown>, identity, tasks: tasksFacade(driver), mail: createMail(env as unknown as Record<string, unknown>, kv) };
153
155
  };
154
156
 
155
157
  /** Drain the D1 outbox in the Worker (no DO/alarm on this path) — called by the