@lunora/mail 1.0.0-alpha.14 → 1.0.0-alpha.141

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.
Files changed (43) hide show
  1. package/LICENSE.md +201 -104
  2. package/README.md +3 -3
  3. package/dist/inbound/index.d.mts +340 -89
  4. package/dist/inbound/index.d.ts +340 -89
  5. package/dist/inbound/index.mjs +1 -2
  6. package/dist/index.d.mts +279 -97
  7. package/dist/index.d.ts +279 -97
  8. package/dist/index.mjs +1 -7
  9. package/dist/packem_shared/MailSendError-BYih5mnM.mjs +1 -0
  10. package/dist/packem_shared/assertValidIdempotencyKey-CyJq0nZq.mjs +3 -0
  11. package/dist/packem_shared/authenticatesFrom-UBe_EgRT.mjs +1 -0
  12. package/dist/packem_shared/capture-transport.d-G-OVrGhG.d.mts +170 -0
  13. package/dist/packem_shared/capture-transport.d-G-OVrGhG.d.ts +170 -0
  14. package/dist/packem_shared/consumeQueuedSend-BwNGdlaG.mjs +1 -0
  15. package/dist/packem_shared/createCaptureSink-BCAMuG4n.mjs +1 -0
  16. package/dist/packem_shared/createCaptureTransport-DZhMUmaI.mjs +1 -0
  17. package/dist/packem_shared/createCloudflareTransport-Dxjf_b6q.mjs +1 -0
  18. package/dist/packem_shared/createInboundEmailHandler-ByzpfcEI.mjs +1 -0
  19. package/dist/packem_shared/createMailer-Cv1B9wYq.mjs +1 -0
  20. package/dist/packem_shared/createResendTransport-CsJfCLAP.mjs +1 -0
  21. package/dist/packem_shared/provider-transport-Ce2xVUuM.mjs +1 -0
  22. package/dist/packem_shared/renderEmail-xypKDdux.mjs +1 -0
  23. package/dist/packem_shared/shard-Bgo7dYFl.mjs +1 -0
  24. package/dist/packem_shared/{shard.d-C4kxvfmd.d.mts → shard.d-DVADjmEJ.d.mts} +7 -12
  25. package/dist/packem_shared/{shard.d-C4kxvfmd.d.ts → shard.d-DVADjmEJ.d.ts} +7 -12
  26. package/dist/testing.d.mts +9 -9
  27. package/dist/testing.d.ts +9 -9
  28. package/dist/testing.mjs +1 -65
  29. package/package.json +7 -6
  30. package/dist/packem_shared/address-vSUAVU2T.mjs +0 -64
  31. package/dist/packem_shared/capture-transport.d-ChnhdPO2.d.mts +0 -117
  32. package/dist/packem_shared/capture-transport.d-ChnhdPO2.d.ts +0 -117
  33. package/dist/packem_shared/consumeQueuedSend-B9hTDOZ6.mjs +0 -77
  34. package/dist/packem_shared/createCaptureSink-B0c7fQJf.mjs +0 -77
  35. package/dist/packem_shared/createCaptureTransport-Crz_8822.mjs +0 -11
  36. package/dist/packem_shared/createCloudflareTransport-DXh1nUNi.mjs +0 -28
  37. package/dist/packem_shared/createInboundEmailHandler-BMB5t-qh.mjs +0 -74
  38. package/dist/packem_shared/createMailer-B4Z_Cgiu.mjs +0 -81
  39. package/dist/packem_shared/createResendTransport-CMQWjZoi.mjs +0 -16
  40. package/dist/packem_shared/parseInboundEmail-DXVOH0wz.mjs +0 -73
  41. package/dist/packem_shared/provider-transport-C0xHi3oy.mjs +0 -48
  42. package/dist/packem_shared/renderEmail-hyS1bpVP.mjs +0 -8
  43. package/dist/packem_shared/shard-DEDcQoFg.mjs +0 -36
@@ -1,5 +1,5 @@
1
- import { D as DurableObjectJurisdiction, S as ShardNamespaceLike } from "../packem_shared/shard.d-C4kxvfmd.mjs";
2
- export type { a as ShardStubLike } from "../packem_shared/shard.d-C4kxvfmd.mjs";
1
+ import { D as DurableObjectJurisdiction, S as ShardNamespaceLike } from "../packem_shared/shard.d-DVADjmEJ.mjs";
2
+ export type { a as ShardStubLike } from "../packem_shared/shard.d-DVADjmEJ.mjs";
3
3
  /** A raw RFC 822 message as accepted by the parser. */
4
4
  type RawInboundEmail = ArrayBuffer | ReadableStream<Uint8Array> | string | Uint8Array;
5
5
  /** One parsed attachment. `content` is preserved as the parser decoded it. */
@@ -14,46 +14,82 @@ interface InboundAttachment {
14
14
  filename: string | null;
15
15
  /** Declared MIME type (e.g. `image/png`). */
16
16
  mimeType: string;
17
+ /** Binary content dropped because the forwarded envelope exceeded the attachment byte budget. */
18
+ truncated?: boolean;
19
+ }
20
+ /** One reported `method=result [ptype.property=value]` clause of an `Authentication-Results` header. */
21
+ interface InboundAuthResult {
22
+ /**
23
+ * The identifier the result is about, lowercased — DKIM's signing domain
24
+ * (`header.d=`), SPF's envelope `MAIL FROM` domain (`smtp.mailfrom=`, local
25
+ * part dropped) or DMARC's `header.from=`. `null` when the clause reported
26
+ * none, in which case it cannot be aligned and vouches for nothing.
27
+ */
28
+ domain: string | null;
29
+ /** The result token, lowercased (`"pass"`, `"fail"`, `"none"`, …). */
30
+ result: string;
17
31
  }
18
32
  /**
19
- * Sender-authentication verdicts pulled from the `Authentication-Results` header
20
- * the receiving MX (e.g. Cloudflare Email Routing) stamped on the message.
21
- *
22
- * SECURITY: Cloudflare Email Routing authenticates only the *recipient* domain,
23
- * **not** the sender. The envelope `from` and message content are trivially
24
- * spoofable, so a downstream handler MUST NOT make trust/authorization decisions
25
- * on `email.from` alone — gate on these verdicts (or your own policy) instead.
26
- * Verdicts are best-effort: when the receiving MX did not stamp an
27
- * `Authentication-Results` header, every field is `null` ("unknown").
28
- *
29
- * SECURITY: verdicts are read from the **first/topmost** `Authentication-Results`
30
- * header in document order. The receiving MX prepends its own genuine header per
31
- * RFC 8601, so the topmost occurrence is the trustworthy one; any lower
32
- * occurrences (which an untrusted sender can inject into the raw message) are
33
- * ignored. As defense-in-depth a consumer may additionally verify the topmost
34
- * header's `authserv-id` matches its receiving MX (e.g. Cloudflare) — that needs
35
- * config this runtime-agnostic parser does not carry, so it is left to the host.
36
- */
33
+ * Sender-authentication verdicts pulled from the `Authentication-Results` header
34
+ * the receiving MX (e.g. Cloudflare Email Routing) stamped on the message.
35
+ *
36
+ * SECURITY: Cloudflare Email Routing authenticates only the *recipient* domain,
37
+ * **not** the sender. The envelope `from` and message content are trivially
38
+ * spoofable, so a downstream handler MUST NOT make trust/authorization decisions
39
+ * on `email.from` alone — gate on these verdicts (or your own policy) instead.
40
+ * Verdicts are best-effort: when the receiving MX did not stamp an
41
+ * `Authentication-Results` header, every list is empty ("unknown").
42
+ *
43
+ * Each method holds a LIST, because RFC 8601 lets one header report the same
44
+ * method more than once and real mail does. An ESP-relayed message carries two
45
+ * DKIM signatures — the relay's and the author domain's — and the MX stamps a
46
+ * clause per signature in whatever order it verified them. Keeping only the
47
+ * first threw the aligned one away whenever it was not the one that happened to
48
+ * come first, and the message was rejected as unauthenticated. A consumer
49
+ * therefore asks "does ANY reported clause pass and align?", not "did the first
50
+ * one?".
51
+ *
52
+ * SECURITY: a bare `"pass"` is NOT proof the `From` header is genuine. SPF
53
+ * authenticates the envelope `MAIL FROM` domain and DKIM the signing domain
54
+ * (`d=`), and an attacker controls both — `spf=pass smtp.mailfrom=evil.example;
55
+ * dkim=pass header.d=evil.example` is routine for a message whose `From` says
56
+ * `ceo@victim.example`. Each verdict therefore carries the identifier it is
57
+ * about ({@link InboundAuthResult.domain}): an SPF or DKIM pass only vouches for
58
+ * `from` when that domain equals the `From` address's domain (RFC 7489 strict
59
+ * alignment). Only a DMARC pass already checked alignment for you. A pass with
60
+ * no identifier reported cannot be aligned and must be treated as
61
+ * unauthenticated.
62
+ *
63
+ * SECURITY: verdicts are read from the **first/topmost** `Authentication-Results`
64
+ * header in document order. The receiving MX prepends its own genuine header per
65
+ * RFC 8601, so the topmost occurrence is the trustworthy one; any lower
66
+ * occurrences (which an untrusted sender can inject into the raw message) are
67
+ * ignored. As defense-in-depth a consumer may additionally verify the topmost
68
+ * header's `authserv-id` matches its receiving MX (e.g. Cloudflare) — that needs
69
+ * config this runtime-agnostic parser does not carry, so it is left to the host.
70
+ */
37
71
  interface InboundAuthentication {
38
- /** DKIM verdict (`"pass"`/`"fail"`/…), or `null` when not reported. */
39
- dkim: string | null;
40
- /** DMARC verdict (`"pass"`/`"fail"`/…), or `null` when not reported. */
41
- dmarc: string | null;
42
- /** SPF verdict (`"pass"`/`"fail"`/…), or `null` when not reported. */
43
- spf: string | null;
72
+ /** Every DKIM clause the header reported, in header order; empty when the method was not reported. */
73
+ dkim: InboundAuthResult[];
74
+ /** Every DMARC clause the header reported, in header order; empty when the method was not reported. */
75
+ dmarc: InboundAuthResult[];
76
+ /** Every SPF clause the header reported, in header order; empty when the method was not reported. */
77
+ spf: InboundAuthResult[];
44
78
  }
45
79
  /** Normalised, transport-agnostic view of a received message. */
46
80
  interface InboundEmail {
47
81
  /** Decoded attachments (empty array when none). */
48
82
  attachments: InboundAttachment[];
49
83
  /**
50
- * Sender-authentication verdicts (DKIM/SPF/DMARC) parsed from the receiving
51
- * MX's **first/topmost** `Authentication-Results` header. SECURITY: see
52
- * {@link InboundAuthentication} — `from` is spoofable; gate trust on these
53
- * verdicts, not on `from`. Reading the raw `headers["authentication-results"]`
54
- * map instead exposes last-wins (a lower, potentially attacker-injected)
55
- * value — trust `authentication`, not the raw map.
56
- */
84
+ * Sender-authentication verdicts (DKIM/SPF/DMARC) and the domain each is
85
+ * about, parsed from the receiving MX's **first/topmost**
86
+ * `Authentication-Results` header. SECURITY: see
87
+ * {@link InboundAuthentication} — `from` is spoofable, and an SPF/DKIM pass
88
+ * vouches for it only when that clause's `domain` equals the `From` domain (a
89
+ * DMARC pass checked that already). Reading the raw `headers["authentication-results"]`
90
+ * map instead exposes last-wins (a lower, potentially attacker-injected)
91
+ * value — trust `authentication`, not the raw map.
92
+ */
57
93
  authentication: InboundAuthentication;
58
94
  /** Sender mailbox (`from`), CR/LF-checked. Empty string when the message omitted it. SECURITY: spoofable — do not trust for authorization. */
59
95
  from: string;
@@ -75,17 +111,48 @@ interface InboundEmail {
75
111
  to: string[];
76
112
  }
77
113
  /**
78
- * Parse a raw RFC 822 message into a normalised {@link InboundEmail}. Accepts the
79
- * shapes a Cloudflare Email Worker can hand off — `ReadableStream`, `ArrayBuffer`,
80
- * `Uint8Array`, or a decoded string.
81
- */
114
+ * Parse a raw RFC 822 message into a normalised {@link InboundEmail}. Accepts the
115
+ * shapes a Cloudflare Email Worker can hand off — `ReadableStream`, `ArrayBuffer`,
116
+ * `Uint8Array`, or a decoded string.
117
+ */
82
118
  declare const parseInboundEmail: (raw: RawInboundEmail) => Promise<InboundEmail>;
83
119
  /**
84
- * Structural projection of Cloudflare's `ForwardableEmailMessage` (verified
85
- * against `@cloudflare/workers-types`' `ForwardableEmailMessage`). Only the
86
- * members the handler touches are modelled, so the host can pass the real
87
- * runtime object without `@lunora/mail` importing `cloudflare:email`.
88
- */
120
+ * THE inbound sender-authentication gate: does the receiving MX vouch for this
121
+ * message's `From` domain? Pass it as the `verify` hook of
122
+ * `createInboundEmailHandler` (or call it from your own) — it is one exported
123
+ * helper precisely so the insecure variants cannot be hand-rolled again.
124
+ *
125
+ * True when ANY reported DMARC, SPF or DKIM clause both **passes** and names a
126
+ * `domain` equal to the `From` address's domain. False otherwise — including for
127
+ * an empty verdict list (the MX stamped no `Authentication-Results` header at
128
+ * all, which is "unknown", not "fine") and for a `From` with no single mailbox
129
+ * to align against.
130
+ *
131
+ * SECURITY — the two halves are each load-bearing.
132
+ *
133
+ * **Alignment.** A bare `pass` proves nothing about `From`. SPF authenticates the
134
+ * envelope `MAIL FROM` domain and DKIM the signing `d=`, both attacker-chosen, so
135
+ * `spf=pass`+`dkim=pass` for `evil.example` is routine on a message whose `From`
136
+ * says `ceo@victim.example`. Only a clause whose own `domain` equals the `From`
137
+ * domain vouches for it. Alignment is STRICT (RFC 7489): there is no
138
+ * public-suffix list here, so `mail.example.com` does not vouch for
139
+ * `example.com`. A pass reporting no domain (`null`) cannot be aligned and is
140
+ * rejected. A DMARC pass already checked alignment at the MX.
141
+ *
142
+ * **Every clause, not the first.** One header legitimately reports a method more
143
+ * than once (an ESP-relayed message is DKIM-signed by both the relay and the
144
+ * author domain), and the aligned clause is not reliably the first, so reading
145
+ * only the first bounced fully authenticated mail. "Any clause passes AND aligns"
146
+ * stays strictly narrower than a bare pass: a clause vouching for some other
147
+ * domain contributes nothing.
148
+ */
149
+ declare const authenticatesFrom: (email: InboundEmail) => boolean;
150
+ /**
151
+ * Structural projection of Cloudflare's `ForwardableEmailMessage` (verified
152
+ * against `@cloudflare/workers-types`' `ForwardableEmailMessage`). Only the
153
+ * members the handler touches are modelled, so the host can pass the real
154
+ * runtime object without `@lunora/mail` importing `cloudflare:email`.
155
+ */
89
156
  interface ForwardableEmailMessageLike {
90
157
  /** Forward this message to a verified destination address. */
91
158
  forward: (rcptTo: string, headers?: Headers) => Promise<unknown>;
@@ -95,13 +162,25 @@ interface ForwardableEmailMessageLike {
95
162
  readonly headers: Headers;
96
163
  /** Stream of the raw RFC 822 message content. */
97
164
  readonly raw: ReadableStream<Uint8Array>;
165
+ /**
166
+ * Size of `raw` in bytes. Cloudflare always sets it; a message without a finite
167
+ * size is refused by the handler's size guard rather than read unbounded.
168
+ */
169
+ readonly rawSize: number;
98
170
  /** Reply to the sender with a new message. */
99
171
  reply: (message: {
100
172
  from: string;
101
173
  raw: string;
102
174
  to: string;
103
175
  }) => Promise<unknown>;
104
- /** Reject the message with a permanent SMTP error (Cloudflare bounces/retries). */
176
+ /**
177
+ * Reject the message with a PERMANENT SMTP error — Cloudflare returns it to
178
+ * the connecting client with this reason, and it is never redelivered.
179
+ * Documented at https://developers.cloudflare.com/email-routing/email-workers/runtime-api/
180
+ * ("Reject this email message by returning a permanent SMTP error back to
181
+ * the connecting client, including the given reason") and mirrored in
182
+ * workerd's own `types/defines/email.d.ts`.
183
+ */
105
184
  setReject: (reason: string) => void;
106
185
  /** Envelope `To`. */
107
186
  readonly to: string;
@@ -118,43 +197,82 @@ interface InboundDispatchContext<TEnv = Record<string, unknown>> {
118
197
  /** Routes a parsed message into a Lunora function (or anywhere). */
119
198
  type InboundDispatch<TEnv = Record<string, unknown>> = (email: InboundEmail, context: InboundDispatchContext<TEnv>) => Promise<void>;
120
199
  /**
121
- * Opt-in sender-verification gate. Runs after `parse` and before `dispatch` with
122
- * the parsed message. Return `false` (or throw) to reject the message before it
123
- * reaches the privileged dispatch — use it to enforce DKIM/SPF/DMARC via
124
- * `email.authentication`, an allow-list, etc. Returning `true`/`undefined`
125
- * proceeds.
126
- */
200
+ * Opt-in sender-verification gate. Runs after `parse` and before `dispatch` with
201
+ * the parsed message. Return `false` (or throw) to reject the message before it
202
+ * reaches the privileged dispatch — use it to enforce DKIM/SPF/DMARC via
203
+ * `email.authentication`, an allow-list, etc.
204
+ *
205
+ * `true` and `undefined` are the ONLY answers that proceed — `undefined` so a
206
+ * `(): void` hook that rejects by throwing type-checks. Anything else is read as a
207
+ * rejection: this is the gate whose failure mode would otherwise grant.
208
+ */
127
209
  type InboundVerify<TEnv = Record<string, unknown>> = (email: InboundEmail, context: InboundDispatchContext<TEnv>) => Promise<boolean | void> | boolean | void;
210
+ /**
211
+ * Opt-in durable sink for a failed `dispatch`. Hand the parsed message to
212
+ * something that owns the retry — a queue producer, a Durable Object, an alarm
213
+ * — and the handler ACCEPTS the SMTP session instead of bouncing, because the
214
+ * message is now owned rather than lost. Returning normally means "I have it";
215
+ * throwing means the hand-off itself failed and the message bounces with the
216
+ * generic reason (see {@link createInboundEmailHandler}).
217
+ *
218
+ * `error` is the dispatch failure, for classification/logging by the sink.
219
+ *
220
+ * NOTE: binary attachment `content` is an `ArrayBuffer`/`Uint8Array`, which
221
+ * survives structured clone (Cloudflare Queues, DO storage) but is corrupted by
222
+ * `JSON.stringify` — encode it yourself if the sink is JSON-bodied.
223
+ */
224
+ type InboundRetain<TEnv = Record<string, unknown>> = (email: InboundEmail, context: InboundDispatchContext<TEnv>, error: unknown) => Promise<void> | void;
128
225
  /** Options for {@link createInboundEmailHandler}. */
129
226
  interface InboundEmailHandlerOptions<TEnv = Record<string, unknown>> {
130
227
  /** Routes the parsed message onward (e.g. {@link dispatchToLunoraFunction}). */
131
228
  dispatch: InboundDispatch<TEnv>;
132
229
  /**
133
- * Called when `parse`/`verify`/`dispatch` throws. The default rejects the
134
- * message via `message.setReject` so Cloudflare bounces/retries rather than
135
- * silently dropping it. SECURITY: the reject reason is delivered to the
136
- * (attacker-controlled) sender as a bounce, so the default reason is a fixed,
137
- * generic string and the real error is logged server-side. Override to log,
138
- * forward, or swallow — but never pass internal error text to `setReject`.
139
- */
230
+ * Largest message, in bytes (`message.rawSize`), the handler parses; anything
231
+ * bigger is refused through `onError` before a byte of it is read. Default
232
+ * 10 MiB. Parsing holds the raw bytes, the decoded parts and every attachment
233
+ * at once, so a message near Email Routing's 25 MiB ceiling can outgrow the
234
+ * isolate's 128 MB. Must be a positive, finite number.
235
+ */
236
+ maxRawBytes?: number;
237
+ /**
238
+ * Called when the size guard (`maxRawBytes`), `parse`, `verify`, or `dispatch`
239
+ * fails — but with two DIFFERENT contracts, because only the first three decide
240
+ * the message's fate here:
241
+ *
242
+ * - size / `parse` / `verify` — this hook DECIDES the outcome. The default
243
+ * ({@link rejectOnError}) rejects via `message.setReject`; supplying your own
244
+ * replaces that, so the message is accepted unless you reject it yourself.
245
+ * - `dispatch` — this hook is OBSERVABILITY ONLY. It is called for the side
246
+ * effect (log, alert, forward) and the outcome is then decided by `retain`
247
+ * (accept) or a generic reject, regardless of what it does; the built-in
248
+ * default is deliberately NOT applied there. A `setReject` from inside it
249
+ * still takes effect, which would bounce a message `retain` went on to
250
+ * accept — almost certainly not what you want. A throw from the hook itself
251
+ * is logged and swallowed so it cannot mask the original dispatch error.
252
+ *
253
+ * SECURITY: a reject reason is delivered to the (attacker-controlled) sender
254
+ * as a bounce, so the default reason is a fixed, generic string and the real
255
+ * error is logged server-side. Never pass internal error text to `setReject`.
256
+ */
140
257
  onError?: (error: unknown, context: InboundDispatchContext<TEnv>) => Promise<void> | void;
141
258
  /** Parses raw bytes into an {@link InboundEmail} (e.g. `parseInboundEmail`). */
142
259
  parse: (raw: RawInboundEmail) => Promise<InboundEmail>;
143
260
  /**
144
- * Opt-in sender-authentication gate run before `dispatch`. SECURITY: inbound
145
- * `from` is spoofable and dispatch is privileged — supply this (gating on
146
- * `email.authentication`) when an inbound function makes any trust decision.
147
- */
261
+ * Opt-in: take durable ownership of a message whose `dispatch` failed, so a
262
+ * transient fault (a shard 502, a briefly-absent admin token) is retried
263
+ * instead of bounced. Omit it and a dispatch failure bounces, as it always
264
+ * has. See {@link InboundRetain}.
265
+ */
266
+ retain?: InboundRetain<TEnv>;
267
+ /**
268
+ * Opt-in sender-authentication gate run before `dispatch`. SECURITY: inbound
269
+ * `from` is spoofable and dispatch is privileged — supply this (gating on
270
+ * `email.authentication`) when an inbound function makes any trust decision.
271
+ */
148
272
  verify?: InboundVerify<TEnv>;
149
273
  }
150
274
  /** The `email(message, env, ctx)` callback the factory returns. */
151
275
  type InboundEmailHandler<TEnv = Record<string, unknown>> = (message: ForwardableEmailMessageLike, env: TEnv, context: unknown) => Promise<void>;
152
- /**
153
- * Build the `email(message, env, ctx)` handler. It (a) reads `message.raw`,
154
- * (b) parses it via `parse`, (c) runs the optional `verify` gate, then
155
- * (d) calls `dispatch(parsed, { message, env, ctx })`. Any throw (or a falsy
156
- * `verify`) routes through `onError` (default: a generic `message.setReject`).
157
- */
158
276
  declare const createInboundEmailHandler: <TEnv = Record<string, unknown>>(options: InboundEmailHandlerOptions<TEnv>) => InboundEmailHandler<TEnv>;
159
277
  /** The `RpcEnvelope` shape the runtime's `/_lunora/rpc` path consumes. */
160
278
  interface RpcEnvelope {
@@ -165,23 +283,25 @@ interface RpcEnvelope {
165
283
  /** Options for {@link dispatchToLunoraFunction}. */
166
284
  interface DispatchToLunoraFunctionOptions<TEnv = Record<string, unknown>> {
167
285
  /**
168
- * Admin bearer authorizing the shard RPC. Defaults to reading
169
- * `env.LUNORA_ADMIN_TOKEN` at dispatch time.
170
- */
286
+ * Admin bearer authorizing the shard RPC. Defaults to reading
287
+ * `env.LUNORA_ADMIN_TOKEN` at dispatch time.
288
+ */
171
289
  adminToken?: string;
172
290
  /** `functionPath` of the target mutation/action (e.g. `"inbound:onEmail"`). */
173
291
  functionPath: string;
174
292
  /**
175
- * Pin inbound dispatch to a Cloudflare data-residency jurisdiction. Pass the
176
- * same value as the worker's `jurisdiction` so inbound mail routes to the
177
- * jurisdiction-pinned shard. Omit for the un-pinned global namespace.
178
- */
293
+ * Pin inbound dispatch to a Cloudflare data-residency jurisdiction. Defaults
294
+ * to the jurisdiction the schema declares (`.jurisdiction("eu")`), so a
295
+ * generated app needs nothing here; set it only for a hand-written worker, to
296
+ * the same value as its `jurisdiction`. A value that contradicts the schema's
297
+ * throws.
298
+ */
179
299
  jurisdiction?: DurableObjectJurisdiction;
180
300
  /**
181
- * Map the parsed message into the function's args. Defaults to passing the
182
- * whole {@link InboundEmail} with binary attachment `content` base64-encoded
183
- * (see {@link toJsonSafeEmail}) so it survives the JSON-serialised RPC body.
184
- */
301
+ * Map the parsed message into the function's args. Defaults to passing the
302
+ * whole {@link InboundEmail} with binary attachment `content` base64-encoded
303
+ * (see {@link toJsonSafeEmail}) so it survives the JSON-serialised RPC body.
304
+ */
185
305
  resolveArgs?: (email: InboundEmail, context: InboundDispatchContext<TEnv>) => unknown;
186
306
  /** The `SHARD` Durable Object namespace. */
187
307
  shard: ShardNamespaceLike;
@@ -189,16 +309,147 @@ interface DispatchToLunoraFunctionOptions<TEnv = Record<string, unknown>> {
189
309
  shardKey?: string;
190
310
  }
191
311
  /**
192
- * Build a {@link InboundDispatch} that posts an {@link RpcEnvelope} to the root
193
- * shard stub — the same admin-RPC-over-shard path the dev capture sink uses
194
- * (`from-env.ts`) — routing the parsed message into a named Lunora
195
- * mutation/action. Throws on a non-2xx RPC or a missing admin token so the
196
- * handler's `onError` (default `setReject`) bounces the message.
197
- *
198
- * SECURITY: the RPC carries the admin bearer, so the target function runs with
199
- * RLS bypassed over fully attacker-controlled, spoofable input — see the module
200
- * docstring. Verify the sender (`verify` hook / `email.authentication`) before
201
- * making any trust decision in the target function.
202
- */
312
+ * Build a {@link InboundDispatch} that posts an {@link RpcEnvelope} to the root
313
+ * shard stub — the same admin-RPC-over-shard path the dev capture sink uses
314
+ * (`from-env.ts`) — routing the parsed message into a named Lunora
315
+ * mutation/action. Throws on a non-2xx RPC or a missing admin token — both
316
+ * transient in principle, so the handler reports it through a custom `onError`
317
+ * and then hands the message to `retain` if one is configured, bouncing only
318
+ * when there is nowhere durable to put it (see {@link createInboundEmailHandler}).
319
+ *
320
+ * SECURITY: the RPC is marked a trusted system dispatch, so `functionPath` may
321
+ * (and should) name an `internalMutation`/`internalAction` — a public `mutation`
322
+ * target is callable by any browser client with a forged message. The input is
323
+ * fully attacker-controlled and spoofable; verify the sender (`verify` hook /
324
+ * `email.authentication`) before making any trust decision in the target.
325
+ */
203
326
  declare const dispatchToLunoraFunction: <TEnv extends Record<string, unknown> = Record<string, unknown>>(options: DispatchToLunoraFunctionOptions<TEnv>) => InboundDispatch<TEnv>;
204
- export { type DispatchToLunoraFunctionOptions, type ForwardableEmailMessageLike, type InboundAttachment, type InboundAuthentication, type InboundDispatch, type InboundDispatchContext, type InboundEmail, type InboundEmailHandler, type InboundEmailHandlerOptions, type InboundVerify, type RawInboundEmail, type RpcEnvelope, type ShardNamespaceLike, createInboundEmailHandler, dispatchToLunoraFunction, parseInboundEmail };
327
+ export {
328
+ /**
329
+ * `@lunora/mail/inbound` — inbound Email Routing support.
330
+ *
331
+ * Wire a Cloudflare Email Worker entry to route received mail into a Lunora
332
+ * mutation/action:
333
+ *
334
+ * ```ts
335
+ * import { createInboundEmailHandler, parseInboundEmail, dispatchToLunoraFunction } from "@lunora/mail/inbound";
336
+ *
337
+ * export const email = createInboundEmailHandler({
338
+ * parse: parseInboundEmail,
339
+ * dispatch: dispatchToLunoraFunction({ shard: env.SHARD, functionPath: "inbound:onEmail" }),
340
+ * });
341
+ * ```
342
+ */
343
+ type DispatchToLunoraFunctionOptions,
344
+ /**
345
+ * `@lunora/mail/inbound` — inbound Email Routing support.
346
+ *
347
+ * Wire a Cloudflare Email Worker entry to route received mail into a Lunora
348
+ * mutation/action:
349
+ *
350
+ * ```ts
351
+ * import { createInboundEmailHandler, parseInboundEmail, dispatchToLunoraFunction } from "@lunora/mail/inbound";
352
+ *
353
+ * export const email = createInboundEmailHandler({
354
+ * parse: parseInboundEmail,
355
+ * dispatch: dispatchToLunoraFunction({ shard: env.SHARD, functionPath: "inbound:onEmail" }),
356
+ * });
357
+ * ```
358
+ */
359
+ type ForwardableEmailMessageLike, type InboundAttachment, type InboundAuthResult, type InboundAuthentication,
360
+ /**
361
+ * `@lunora/mail/inbound` — inbound Email Routing support.
362
+ *
363
+ * Wire a Cloudflare Email Worker entry to route received mail into a Lunora
364
+ * mutation/action:
365
+ *
366
+ * ```ts
367
+ * import { createInboundEmailHandler, parseInboundEmail, dispatchToLunoraFunction } from "@lunora/mail/inbound";
368
+ *
369
+ * export const email = createInboundEmailHandler({
370
+ * parse: parseInboundEmail,
371
+ * dispatch: dispatchToLunoraFunction({ shard: env.SHARD, functionPath: "inbound:onEmail" }),
372
+ * });
373
+ * ```
374
+ */
375
+ type InboundDispatch,
376
+ /**
377
+ * `@lunora/mail/inbound` — inbound Email Routing support.
378
+ *
379
+ * Wire a Cloudflare Email Worker entry to route received mail into a Lunora
380
+ * mutation/action:
381
+ *
382
+ * ```ts
383
+ * import { createInboundEmailHandler, parseInboundEmail, dispatchToLunoraFunction } from "@lunora/mail/inbound";
384
+ *
385
+ * export const email = createInboundEmailHandler({
386
+ * parse: parseInboundEmail,
387
+ * dispatch: dispatchToLunoraFunction({ shard: env.SHARD, functionPath: "inbound:onEmail" }),
388
+ * });
389
+ * ```
390
+ */
391
+ type InboundDispatchContext, type InboundEmail,
392
+ /**
393
+ * `@lunora/mail/inbound` — inbound Email Routing support.
394
+ *
395
+ * Wire a Cloudflare Email Worker entry to route received mail into a Lunora
396
+ * mutation/action:
397
+ *
398
+ * ```ts
399
+ * import { createInboundEmailHandler, parseInboundEmail, dispatchToLunoraFunction } from "@lunora/mail/inbound";
400
+ *
401
+ * export const email = createInboundEmailHandler({
402
+ * parse: parseInboundEmail,
403
+ * dispatch: dispatchToLunoraFunction({ shard: env.SHARD, functionPath: "inbound:onEmail" }),
404
+ * });
405
+ * ```
406
+ */
407
+ type InboundEmailHandler,
408
+ /**
409
+ * `@lunora/mail/inbound` — inbound Email Routing support.
410
+ *
411
+ * Wire a Cloudflare Email Worker entry to route received mail into a Lunora
412
+ * mutation/action:
413
+ *
414
+ * ```ts
415
+ * import { createInboundEmailHandler, parseInboundEmail, dispatchToLunoraFunction } from "@lunora/mail/inbound";
416
+ *
417
+ * export const email = createInboundEmailHandler({
418
+ * parse: parseInboundEmail,
419
+ * dispatch: dispatchToLunoraFunction({ shard: env.SHARD, functionPath: "inbound:onEmail" }),
420
+ * });
421
+ * ```
422
+ */
423
+ type InboundEmailHandlerOptions,
424
+ /**
425
+ * `@lunora/mail/inbound` — inbound Email Routing support.
426
+ *
427
+ * Wire a Cloudflare Email Worker entry to route received mail into a Lunora
428
+ * mutation/action:
429
+ *
430
+ * ```ts
431
+ * import { createInboundEmailHandler, parseInboundEmail, dispatchToLunoraFunction } from "@lunora/mail/inbound";
432
+ *
433
+ * export const email = createInboundEmailHandler({
434
+ * parse: parseInboundEmail,
435
+ * dispatch: dispatchToLunoraFunction({ shard: env.SHARD, functionPath: "inbound:onEmail" }),
436
+ * });
437
+ * ```
438
+ */
439
+ type InboundVerify, type RawInboundEmail,
440
+ /**
441
+ * `@lunora/mail/inbound` — inbound Email Routing support.
442
+ *
443
+ * Wire a Cloudflare Email Worker entry to route received mail into a Lunora
444
+ * mutation/action:
445
+ *
446
+ * ```ts
447
+ * import { createInboundEmailHandler, parseInboundEmail, dispatchToLunoraFunction } from "@lunora/mail/inbound";
448
+ *
449
+ * export const email = createInboundEmailHandler({
450
+ * parse: parseInboundEmail,
451
+ * dispatch: dispatchToLunoraFunction({ shard: env.SHARD, functionPath: "inbound:onEmail" }),
452
+ * });
453
+ * ```
454
+ */
455
+ type RpcEnvelope, type ShardNamespaceLike, authenticatesFrom, createInboundEmailHandler, dispatchToLunoraFunction, parseInboundEmail };