@lunora/mail 0.0.0 → 1.0.0-alpha.1

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.
@@ -0,0 +1,207 @@
1
+ /** A raw RFC 822 message as accepted by the parser. */
2
+ type RawInboundEmail = ArrayBuffer | ReadableStream<Uint8Array> | string | Uint8Array;
3
+ /** One parsed attachment. `content` is preserved as the parser decoded it. */
4
+ interface InboundAttachment {
5
+ /** Raw attachment content (base64 string or binary buffer, per `encoding`). */
6
+ content: ArrayBuffer | string | Uint8Array;
7
+ /** `Content-Disposition` (`"attachment"`/`"inline"`), or `null` when absent. */
8
+ disposition: "attachment" | "inline" | null;
9
+ /** How `content` is encoded, when the parser reported it. */
10
+ encoding?: "base64" | "utf8";
11
+ /** Filename, or `null` when the part declared none. */
12
+ filename: string | null;
13
+ /** Declared MIME type (e.g. `image/png`). */
14
+ mimeType: string;
15
+ }
16
+ /**
17
+ * Sender-authentication verdicts pulled from the `Authentication-Results` header
18
+ * the receiving MX (e.g. Cloudflare Email Routing) stamped on the message.
19
+ *
20
+ * SECURITY: Cloudflare Email Routing authenticates only the *recipient* domain,
21
+ * **not** the sender. The envelope `from` and message content are trivially
22
+ * spoofable, so a downstream handler MUST NOT make trust/authorization decisions
23
+ * on `email.from` alone — gate on these verdicts (or your own policy) instead.
24
+ * Verdicts are best-effort: when the receiving MX did not stamp an
25
+ * `Authentication-Results` header, every field is `null` ("unknown").
26
+ */
27
+ interface InboundAuthentication {
28
+ /** DKIM verdict (`"pass"`/`"fail"`/…), or `null` when not reported. */
29
+ dkim: string | null;
30
+ /** DMARC verdict (`"pass"`/`"fail"`/…), or `null` when not reported. */
31
+ dmarc: string | null;
32
+ /** SPF verdict (`"pass"`/`"fail"`/…), or `null` when not reported. */
33
+ spf: string | null;
34
+ }
35
+ /** Normalised, transport-agnostic view of a received message. */
36
+ interface InboundEmail {
37
+ /** Decoded attachments (empty array when none). */
38
+ attachments: InboundAttachment[];
39
+ /**
40
+ * Sender-authentication verdicts (DKIM/SPF/DMARC) parsed from the receiving
41
+ * MX's `Authentication-Results` header. SECURITY: see {@link InboundAuthentication}
42
+ * — `from` is spoofable; gate trust on these verdicts, not on `from`.
43
+ */
44
+ authentication: InboundAuthentication;
45
+ /** Sender mailbox (`from`), CR/LF-checked. Empty string when the message omitted it. SECURITY: spoofable — do not trust for authorization. */
46
+ from: string;
47
+ /** Flattened `key → value` of the parsed headers (lowercase keys), each value CR/LF-checked. */
48
+ headers: Record<string, string>;
49
+ /** HTML body, when present. */
50
+ html?: string;
51
+ /** `In-Reply-To` header (threading), CR/LF-checked. */
52
+ inReplyTo?: string;
53
+ /** `Message-ID` of this message, CR/LF-checked. */
54
+ messageId?: string;
55
+ /** `References` header (threading), CR/LF-checked. */
56
+ references?: string;
57
+ /** Subject line, CR/LF-checked. */
58
+ subject?: string;
59
+ /** Plain-text body, when present. */
60
+ text?: string;
61
+ /** Recipient mailboxes (`to`), each CR/LF-checked. */
62
+ to: string[];
63
+ }
64
+ /**
65
+ * Parse a raw RFC 822 message into a normalised {@link InboundEmail}. Accepts the
66
+ * shapes a Cloudflare Email Worker can hand off — `ReadableStream`, `ArrayBuffer`,
67
+ * `Uint8Array`, or a decoded string.
68
+ */
69
+ declare const parseInboundEmail: (raw: RawInboundEmail) => Promise<InboundEmail>;
70
+ /**
71
+ * Structural projections of the `SHARD` Durable Object namespace + one shard
72
+ * stub, shared by the inbound dispatcher. Mirrors the shapes the outbound dev
73
+ * capture sink uses (`packages/mail/src/from-env.ts`) so inbound dispatch routes
74
+ * a parsed message into a Lunora function over the exact same admin-RPC-over-shard
75
+ * path — without importing any Cloudflare types into `@lunora/mail`.
76
+ */
77
+ /** Structural projection of one shard stub — only `fetch` returning something with `.json()`. */
78
+ interface ShardStubLike {
79
+ fetch: (input: string, init?: {
80
+ body?: string;
81
+ headers?: Record<string, string>;
82
+ method?: string;
83
+ }) => Promise<{
84
+ json: () => Promise<unknown>;
85
+ }>;
86
+ }
87
+ /** Structural projection of the `SHARD` Durable Object namespace. */
88
+ interface ShardNamespaceLike {
89
+ get: (id: unknown) => ShardStubLike;
90
+ idFromName: (name: string) => unknown;
91
+ }
92
+ /**
93
+ * Structural projection of Cloudflare's `ForwardableEmailMessage` (verified
94
+ * against `@cloudflare/workers-types`' `ForwardableEmailMessage`). Only the
95
+ * members the handler touches are modelled, so the host can pass the real
96
+ * runtime object without `@lunora/mail` importing `cloudflare:email`.
97
+ */
98
+ interface ForwardableEmailMessageLike {
99
+ /** Forward this message to a verified destination address. */
100
+ forward: (rcptTo: string, headers?: Headers) => Promise<unknown>;
101
+ /** Envelope `From`. */
102
+ readonly from: string;
103
+ /** Parsed top-level headers. */
104
+ readonly headers: Headers;
105
+ /** Stream of the raw RFC 822 message content. */
106
+ readonly raw: ReadableStream<Uint8Array>;
107
+ /** Reply to the sender with a new message. */
108
+ reply: (message: {
109
+ from: string;
110
+ raw: string;
111
+ to: string;
112
+ }) => Promise<unknown>;
113
+ /** Reject the message with a permanent SMTP error (Cloudflare bounces/retries). */
114
+ setReject: (reason: string) => void;
115
+ /** Envelope `To`. */
116
+ readonly to: string;
117
+ }
118
+ /** Context threaded to a `dispatch` callback alongside the parsed message. */
119
+ interface InboundDispatchContext<TEnv = Record<string, unknown>> {
120
+ /** Cloudflare's `ExecutionContext`, forwarded verbatim from the `email()` entry. */
121
+ ctx: unknown;
122
+ /** Worker `env` (bindings, vars, secrets) projected as a plain record. */
123
+ env: TEnv;
124
+ /** The originating message, for `setReject`/`forward`/`reply`. */
125
+ message: ForwardableEmailMessageLike;
126
+ }
127
+ /** Routes a parsed message into a Lunora function (or anywhere). */
128
+ type InboundDispatch<TEnv = Record<string, unknown>> = (email: InboundEmail, context: InboundDispatchContext<TEnv>) => Promise<void>;
129
+ /**
130
+ * Opt-in sender-verification gate. Runs after `parse` and before `dispatch` with
131
+ * the parsed message. Return `false` (or throw) to reject the message before it
132
+ * reaches the privileged dispatch — use it to enforce DKIM/SPF/DMARC via
133
+ * `email.authentication`, an allow-list, etc. Returning `true`/`undefined`
134
+ * proceeds.
135
+ */
136
+ type InboundVerify<TEnv = Record<string, unknown>> = (email: InboundEmail, context: InboundDispatchContext<TEnv>) => Promise<boolean | void> | boolean | void;
137
+ /** Options for {@link createInboundEmailHandler}. */
138
+ interface InboundEmailHandlerOptions<TEnv = Record<string, unknown>> {
139
+ /** Routes the parsed message onward (e.g. {@link dispatchToLunoraFunction}). */
140
+ dispatch: InboundDispatch<TEnv>;
141
+ /**
142
+ * Called when `parse`/`verify`/`dispatch` throws. The default rejects the
143
+ * message via `message.setReject` so Cloudflare bounces/retries rather than
144
+ * silently dropping it. SECURITY: the reject reason is delivered to the
145
+ * (attacker-controlled) sender as a bounce, so the default reason is a fixed,
146
+ * generic string and the real error is logged server-side. Override to log,
147
+ * forward, or swallow — but never pass internal error text to `setReject`.
148
+ */
149
+ onError?: (error: unknown, context: InboundDispatchContext<TEnv>) => Promise<void> | void;
150
+ /** Parses raw bytes into an {@link InboundEmail} (e.g. `parseInboundEmail`). */
151
+ parse: (raw: RawInboundEmail) => Promise<InboundEmail>;
152
+ /**
153
+ * Opt-in sender-authentication gate run before `dispatch`. SECURITY: inbound
154
+ * `from` is spoofable and dispatch is privileged — supply this (gating on
155
+ * `email.authentication`) when an inbound function makes any trust decision.
156
+ */
157
+ verify?: InboundVerify<TEnv>;
158
+ }
159
+ /** The `email(message, env, ctx)` callback the factory returns. */
160
+ type InboundEmailHandler<TEnv = Record<string, unknown>> = (message: ForwardableEmailMessageLike, env: TEnv, context: unknown) => Promise<void>;
161
+ /**
162
+ * Build the `email(message, env, ctx)` handler. It (a) reads `message.raw`,
163
+ * (b) parses it via `parse`, (c) runs the optional `verify` gate, then
164
+ * (d) calls `dispatch(parsed, { message, env, ctx })`. Any throw (or a falsy
165
+ * `verify`) routes through `onError` (default: a generic `message.setReject`).
166
+ */
167
+ declare const createInboundEmailHandler: <TEnv = Record<string, unknown>>(options: InboundEmailHandlerOptions<TEnv>) => InboundEmailHandler<TEnv>;
168
+ /** The `RpcEnvelope` shape the runtime's `/_lunora/rpc` path consumes. */
169
+ interface RpcEnvelope {
170
+ args: unknown;
171
+ functionPath: string;
172
+ shardKey?: string;
173
+ }
174
+ /** Options for {@link dispatchToLunoraFunction}. */
175
+ interface DispatchToLunoraFunctionOptions<TEnv = Record<string, unknown>> {
176
+ /**
177
+ * Admin bearer authorizing the shard RPC. Defaults to reading
178
+ * `env.LUNORA_ADMIN_TOKEN` at dispatch time.
179
+ */
180
+ adminToken?: string;
181
+ /** `functionPath` of the target mutation/action (e.g. `"inbound:onEmail"`). */
182
+ functionPath: string;
183
+ /**
184
+ * Map the parsed message into the function's args. Defaults to passing the
185
+ * whole {@link InboundEmail} with binary attachment `content` base64-encoded
186
+ * (see {@link toJsonSafeEmail}) so it survives the JSON-serialised RPC body.
187
+ */
188
+ resolveArgs?: (email: InboundEmail, context: InboundDispatchContext<TEnv>) => unknown;
189
+ /** The `SHARD` Durable Object namespace. */
190
+ shard: ShardNamespaceLike;
191
+ /** Shard the function runs on. Defaults to the runtime's default root shard. */
192
+ shardKey?: string;
193
+ }
194
+ /**
195
+ * Build a {@link InboundDispatch} that posts an {@link RpcEnvelope} to the root
196
+ * shard stub — the same admin-RPC-over-shard path the dev capture sink uses
197
+ * (`from-env.ts`) — routing the parsed message into a named Lunora
198
+ * mutation/action. Throws on a non-2xx RPC or a missing admin token so the
199
+ * handler's `onError` (default `setReject`) bounces the message.
200
+ *
201
+ * SECURITY: the RPC carries the admin bearer, so the target function runs with
202
+ * RLS bypassed over fully attacker-controlled, spoofable input — see the module
203
+ * docstring. Verify the sender (`verify` hook / `email.authentication`) before
204
+ * making any trust decision in the target function.
205
+ */
206
+ declare const dispatchToLunoraFunction: <TEnv extends Record<string, unknown> = Record<string, unknown>>(options: DispatchToLunoraFunctionOptions<TEnv>) => InboundDispatch<TEnv>;
207
+ 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, type ShardStubLike, createInboundEmailHandler, dispatchToLunoraFunction, parseInboundEmail };
@@ -0,0 +1,207 @@
1
+ /** A raw RFC 822 message as accepted by the parser. */
2
+ type RawInboundEmail = ArrayBuffer | ReadableStream<Uint8Array> | string | Uint8Array;
3
+ /** One parsed attachment. `content` is preserved as the parser decoded it. */
4
+ interface InboundAttachment {
5
+ /** Raw attachment content (base64 string or binary buffer, per `encoding`). */
6
+ content: ArrayBuffer | string | Uint8Array;
7
+ /** `Content-Disposition` (`"attachment"`/`"inline"`), or `null` when absent. */
8
+ disposition: "attachment" | "inline" | null;
9
+ /** How `content` is encoded, when the parser reported it. */
10
+ encoding?: "base64" | "utf8";
11
+ /** Filename, or `null` when the part declared none. */
12
+ filename: string | null;
13
+ /** Declared MIME type (e.g. `image/png`). */
14
+ mimeType: string;
15
+ }
16
+ /**
17
+ * Sender-authentication verdicts pulled from the `Authentication-Results` header
18
+ * the receiving MX (e.g. Cloudflare Email Routing) stamped on the message.
19
+ *
20
+ * SECURITY: Cloudflare Email Routing authenticates only the *recipient* domain,
21
+ * **not** the sender. The envelope `from` and message content are trivially
22
+ * spoofable, so a downstream handler MUST NOT make trust/authorization decisions
23
+ * on `email.from` alone — gate on these verdicts (or your own policy) instead.
24
+ * Verdicts are best-effort: when the receiving MX did not stamp an
25
+ * `Authentication-Results` header, every field is `null` ("unknown").
26
+ */
27
+ interface InboundAuthentication {
28
+ /** DKIM verdict (`"pass"`/`"fail"`/…), or `null` when not reported. */
29
+ dkim: string | null;
30
+ /** DMARC verdict (`"pass"`/`"fail"`/…), or `null` when not reported. */
31
+ dmarc: string | null;
32
+ /** SPF verdict (`"pass"`/`"fail"`/…), or `null` when not reported. */
33
+ spf: string | null;
34
+ }
35
+ /** Normalised, transport-agnostic view of a received message. */
36
+ interface InboundEmail {
37
+ /** Decoded attachments (empty array when none). */
38
+ attachments: InboundAttachment[];
39
+ /**
40
+ * Sender-authentication verdicts (DKIM/SPF/DMARC) parsed from the receiving
41
+ * MX's `Authentication-Results` header. SECURITY: see {@link InboundAuthentication}
42
+ * — `from` is spoofable; gate trust on these verdicts, not on `from`.
43
+ */
44
+ authentication: InboundAuthentication;
45
+ /** Sender mailbox (`from`), CR/LF-checked. Empty string when the message omitted it. SECURITY: spoofable — do not trust for authorization. */
46
+ from: string;
47
+ /** Flattened `key → value` of the parsed headers (lowercase keys), each value CR/LF-checked. */
48
+ headers: Record<string, string>;
49
+ /** HTML body, when present. */
50
+ html?: string;
51
+ /** `In-Reply-To` header (threading), CR/LF-checked. */
52
+ inReplyTo?: string;
53
+ /** `Message-ID` of this message, CR/LF-checked. */
54
+ messageId?: string;
55
+ /** `References` header (threading), CR/LF-checked. */
56
+ references?: string;
57
+ /** Subject line, CR/LF-checked. */
58
+ subject?: string;
59
+ /** Plain-text body, when present. */
60
+ text?: string;
61
+ /** Recipient mailboxes (`to`), each CR/LF-checked. */
62
+ to: string[];
63
+ }
64
+ /**
65
+ * Parse a raw RFC 822 message into a normalised {@link InboundEmail}. Accepts the
66
+ * shapes a Cloudflare Email Worker can hand off — `ReadableStream`, `ArrayBuffer`,
67
+ * `Uint8Array`, or a decoded string.
68
+ */
69
+ declare const parseInboundEmail: (raw: RawInboundEmail) => Promise<InboundEmail>;
70
+ /**
71
+ * Structural projections of the `SHARD` Durable Object namespace + one shard
72
+ * stub, shared by the inbound dispatcher. Mirrors the shapes the outbound dev
73
+ * capture sink uses (`packages/mail/src/from-env.ts`) so inbound dispatch routes
74
+ * a parsed message into a Lunora function over the exact same admin-RPC-over-shard
75
+ * path — without importing any Cloudflare types into `@lunora/mail`.
76
+ */
77
+ /** Structural projection of one shard stub — only `fetch` returning something with `.json()`. */
78
+ interface ShardStubLike {
79
+ fetch: (input: string, init?: {
80
+ body?: string;
81
+ headers?: Record<string, string>;
82
+ method?: string;
83
+ }) => Promise<{
84
+ json: () => Promise<unknown>;
85
+ }>;
86
+ }
87
+ /** Structural projection of the `SHARD` Durable Object namespace. */
88
+ interface ShardNamespaceLike {
89
+ get: (id: unknown) => ShardStubLike;
90
+ idFromName: (name: string) => unknown;
91
+ }
92
+ /**
93
+ * Structural projection of Cloudflare's `ForwardableEmailMessage` (verified
94
+ * against `@cloudflare/workers-types`' `ForwardableEmailMessage`). Only the
95
+ * members the handler touches are modelled, so the host can pass the real
96
+ * runtime object without `@lunora/mail` importing `cloudflare:email`.
97
+ */
98
+ interface ForwardableEmailMessageLike {
99
+ /** Forward this message to a verified destination address. */
100
+ forward: (rcptTo: string, headers?: Headers) => Promise<unknown>;
101
+ /** Envelope `From`. */
102
+ readonly from: string;
103
+ /** Parsed top-level headers. */
104
+ readonly headers: Headers;
105
+ /** Stream of the raw RFC 822 message content. */
106
+ readonly raw: ReadableStream<Uint8Array>;
107
+ /** Reply to the sender with a new message. */
108
+ reply: (message: {
109
+ from: string;
110
+ raw: string;
111
+ to: string;
112
+ }) => Promise<unknown>;
113
+ /** Reject the message with a permanent SMTP error (Cloudflare bounces/retries). */
114
+ setReject: (reason: string) => void;
115
+ /** Envelope `To`. */
116
+ readonly to: string;
117
+ }
118
+ /** Context threaded to a `dispatch` callback alongside the parsed message. */
119
+ interface InboundDispatchContext<TEnv = Record<string, unknown>> {
120
+ /** Cloudflare's `ExecutionContext`, forwarded verbatim from the `email()` entry. */
121
+ ctx: unknown;
122
+ /** Worker `env` (bindings, vars, secrets) projected as a plain record. */
123
+ env: TEnv;
124
+ /** The originating message, for `setReject`/`forward`/`reply`. */
125
+ message: ForwardableEmailMessageLike;
126
+ }
127
+ /** Routes a parsed message into a Lunora function (or anywhere). */
128
+ type InboundDispatch<TEnv = Record<string, unknown>> = (email: InboundEmail, context: InboundDispatchContext<TEnv>) => Promise<void>;
129
+ /**
130
+ * Opt-in sender-verification gate. Runs after `parse` and before `dispatch` with
131
+ * the parsed message. Return `false` (or throw) to reject the message before it
132
+ * reaches the privileged dispatch — use it to enforce DKIM/SPF/DMARC via
133
+ * `email.authentication`, an allow-list, etc. Returning `true`/`undefined`
134
+ * proceeds.
135
+ */
136
+ type InboundVerify<TEnv = Record<string, unknown>> = (email: InboundEmail, context: InboundDispatchContext<TEnv>) => Promise<boolean | void> | boolean | void;
137
+ /** Options for {@link createInboundEmailHandler}. */
138
+ interface InboundEmailHandlerOptions<TEnv = Record<string, unknown>> {
139
+ /** Routes the parsed message onward (e.g. {@link dispatchToLunoraFunction}). */
140
+ dispatch: InboundDispatch<TEnv>;
141
+ /**
142
+ * Called when `parse`/`verify`/`dispatch` throws. The default rejects the
143
+ * message via `message.setReject` so Cloudflare bounces/retries rather than
144
+ * silently dropping it. SECURITY: the reject reason is delivered to the
145
+ * (attacker-controlled) sender as a bounce, so the default reason is a fixed,
146
+ * generic string and the real error is logged server-side. Override to log,
147
+ * forward, or swallow — but never pass internal error text to `setReject`.
148
+ */
149
+ onError?: (error: unknown, context: InboundDispatchContext<TEnv>) => Promise<void> | void;
150
+ /** Parses raw bytes into an {@link InboundEmail} (e.g. `parseInboundEmail`). */
151
+ parse: (raw: RawInboundEmail) => Promise<InboundEmail>;
152
+ /**
153
+ * Opt-in sender-authentication gate run before `dispatch`. SECURITY: inbound
154
+ * `from` is spoofable and dispatch is privileged — supply this (gating on
155
+ * `email.authentication`) when an inbound function makes any trust decision.
156
+ */
157
+ verify?: InboundVerify<TEnv>;
158
+ }
159
+ /** The `email(message, env, ctx)` callback the factory returns. */
160
+ type InboundEmailHandler<TEnv = Record<string, unknown>> = (message: ForwardableEmailMessageLike, env: TEnv, context: unknown) => Promise<void>;
161
+ /**
162
+ * Build the `email(message, env, ctx)` handler. It (a) reads `message.raw`,
163
+ * (b) parses it via `parse`, (c) runs the optional `verify` gate, then
164
+ * (d) calls `dispatch(parsed, { message, env, ctx })`. Any throw (or a falsy
165
+ * `verify`) routes through `onError` (default: a generic `message.setReject`).
166
+ */
167
+ declare const createInboundEmailHandler: <TEnv = Record<string, unknown>>(options: InboundEmailHandlerOptions<TEnv>) => InboundEmailHandler<TEnv>;
168
+ /** The `RpcEnvelope` shape the runtime's `/_lunora/rpc` path consumes. */
169
+ interface RpcEnvelope {
170
+ args: unknown;
171
+ functionPath: string;
172
+ shardKey?: string;
173
+ }
174
+ /** Options for {@link dispatchToLunoraFunction}. */
175
+ interface DispatchToLunoraFunctionOptions<TEnv = Record<string, unknown>> {
176
+ /**
177
+ * Admin bearer authorizing the shard RPC. Defaults to reading
178
+ * `env.LUNORA_ADMIN_TOKEN` at dispatch time.
179
+ */
180
+ adminToken?: string;
181
+ /** `functionPath` of the target mutation/action (e.g. `"inbound:onEmail"`). */
182
+ functionPath: string;
183
+ /**
184
+ * Map the parsed message into the function's args. Defaults to passing the
185
+ * whole {@link InboundEmail} with binary attachment `content` base64-encoded
186
+ * (see {@link toJsonSafeEmail}) so it survives the JSON-serialised RPC body.
187
+ */
188
+ resolveArgs?: (email: InboundEmail, context: InboundDispatchContext<TEnv>) => unknown;
189
+ /** The `SHARD` Durable Object namespace. */
190
+ shard: ShardNamespaceLike;
191
+ /** Shard the function runs on. Defaults to the runtime's default root shard. */
192
+ shardKey?: string;
193
+ }
194
+ /**
195
+ * Build a {@link InboundDispatch} that posts an {@link RpcEnvelope} to the root
196
+ * shard stub — the same admin-RPC-over-shard path the dev capture sink uses
197
+ * (`from-env.ts`) — routing the parsed message into a named Lunora
198
+ * mutation/action. Throws on a non-2xx RPC or a missing admin token so the
199
+ * handler's `onError` (default `setReject`) bounces the message.
200
+ *
201
+ * SECURITY: the RPC carries the admin bearer, so the target function runs with
202
+ * RLS bypassed over fully attacker-controlled, spoofable input — see the module
203
+ * docstring. Verify the sender (`verify` hook / `email.authentication`) before
204
+ * making any trust decision in the target function.
205
+ */
206
+ declare const dispatchToLunoraFunction: <TEnv extends Record<string, unknown> = Record<string, unknown>>(options: DispatchToLunoraFunctionOptions<TEnv>) => InboundDispatch<TEnv>;
207
+ 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, type ShardStubLike, createInboundEmailHandler, dispatchToLunoraFunction, parseInboundEmail };
@@ -0,0 +1,2 @@
1
+ export { createInboundEmailHandler, dispatchToLunoraFunction } from '../packem_shared/createInboundEmailHandler-Cd8dyzB7.mjs';
2
+ export { parseInboundEmail } from '../packem_shared/parseInboundEmail-Bw9u_1oc.mjs';
@@ -0,0 +1,126 @@
1
+ import { M as MailTransport, L as LunoraMailOptions, a as Mailer, b as MailboxSink, S as SendOptions } from "./packem_shared/capture-transport.d-ChnhdPO2.mjs";
2
+ export { type C as CapturedMail, type Q as QueueLike, type c as SendPayload, d as createCaptureTransport } from "./packem_shared/capture-transport.d-ChnhdPO2.mjs";
3
+ import { ReactElement } from 'react';
4
+ /**
5
+ * Sends a raw RFC 822 message through a Worker's Email send binding. The Workers
6
+ * runtime owns the binding, so the caller supplies this thin callback — keeping
7
+ * `@lunora/mail` free of a `cloudflare:email` import and unit-testable. Wire it
8
+ * in your project as:
9
+ *
10
+ * ```ts
11
+ * send: async (from, to, raw) => {
12
+ * const { EmailMessage } = await import("cloudflare:email");
13
+ * await env.SEND_EMAIL.send(new EmailMessage(from, to, raw));
14
+ * }
15
+ * ```
16
+ */
17
+ type CloudflareSend = (from: string, to: string, raw: string) => Promise<void>;
18
+ interface CloudflareTransportOptions {
19
+ /** Default sender used when a `SendOptions.from` isn't supplied. */
20
+ from: string;
21
+ /** RFC 822 send callback bound to the Worker's `send_email` binding. */
22
+ send: CloudflareSend;
23
+ }
24
+ /**
25
+ * Build the default Cloudflare Email Workers transport via `@visulima/email`.
26
+ * Cloudflare's `send_email` binding is **single-recipient** and only delivers to
27
+ * **verified Email Routing destination addresses**, and the underlying provider
28
+ * rejects any `cc`/`bcc` or a non-single `to` outright. So this enforces a single
29
+ * `to` recipient (throwing a clear error instead of silently dropping the rest)
30
+ * and rejects `cc`/`bcc` rather than forwarding them into a generic "send failed".
31
+ * Multi-recipient transactional mail must fan out one send per recipient at the
32
+ * call site. In dev the capture transport intercepts before this runs, so the
33
+ * verified-address constraint never bites the dev loop.
34
+ */
35
+ declare const createCloudflareTransport: (options: CloudflareTransportOptions) => MailTransport;
36
+ declare const createMailer: (options: LunoraMailOptions) => Mailer;
37
+ /** A Worker `env` projected as a plain record (vars, secrets, and bindings are `unknown`-valued). */
38
+ type MailEnv = Record<string, unknown>;
39
+ /** Options for {@link createMailerFromEnv}. */
40
+ interface FromEnvOptions {
41
+ /** RFC 822 send callback bound to the Worker's `send_email` binding (Cloudflare default transport). */
42
+ cloudflareSend?: CloudflareSend;
43
+ /** Shard the captured-mail inbox lives on; override if your worker sets a custom `defaultShardKey`. */
44
+ rootShard?: string;
45
+ }
46
+ /**
47
+ * Whether outbound mail should be captured (into the studio inbox) rather than
48
+ * delivered. Explicit `LUNORA_MAIL_CAPTURE` (`"1"`/`"true"` vs `"0"`/`"false"`)
49
+ * always wins; unset, capture is on only in a development environment. It does
50
+ * NOT fall back to "no SEND_EMAIL binding ⇒ capture" — a production deploy that
51
+ * forgot the binding must fail loudly on send, not silently swallow mail.
52
+ */
53
+ declare const shouldCaptureMail: (env: MailEnv) => boolean;
54
+ /**
55
+ * Build the {@link MailboxSink} that records a captured message into the studio's
56
+ * root-shard inbox via the reserved `recordMail` admin RPC — the same
57
+ * worker→root-shard path the runtime uses for auth events. Best-effort: without
58
+ * the `SHARD` binding or `LUNORA_ADMIN_TOKEN` it returns a sentinel id so a send
59
+ * never fails for lack of somewhere to record.
60
+ */
61
+ declare const createCaptureSink: (env: MailEnv, rootShard?: string) => MailboxSink;
62
+ /**
63
+ * Build a {@link Mailer} from a Worker `env`. In a dev environment every send is
64
+ * captured into the studio's Mail inbox; otherwise it delivers via the supplied
65
+ * `cloudflareSend` (the `SEND_EMAIL` binding) or, failing that, `RESEND_API_KEY`.
66
+ * Throws when neither a capture context nor a real transport is available, so a
67
+ * misconfigured production deploy fails loudly instead of silently dropping mail.
68
+ *
69
+ * `MAIL_FROM` is required (the default sender).
70
+ */
71
+ declare const createMailerFromEnv: (env: MailEnv, options?: FromEnvOptions) => Mailer;
72
+ /** Serializable representation of a `SendOptions` payload — drops the `react` field. */
73
+ interface QueuedSend {
74
+ bcc?: string[];
75
+ cc?: string[];
76
+ from?: string;
77
+ headers?: Record<string, string>;
78
+ html?: string;
79
+ replyTo?: string;
80
+ subject: string;
81
+ text?: string;
82
+ to: string | string[];
83
+ }
84
+ /**
85
+ * Narrow a `SendOptions` to its serializable `QueuedSend` shape by dropping the
86
+ * non-cloneable `react` field. React elements are not structured-cloneable, so
87
+ * the queue body must carry only the pre-rendered html/text and scalar fields.
88
+ */
89
+ declare const toQueuedPayload: (options: SendOptions) => QueuedSend;
90
+ /**
91
+ * Helper used by Queue consumers: rehydrate a `QueuedSend` payload and forward
92
+ * to a configured `Mailer.send()`. Use this inside your Worker's `queue()`
93
+ * handler.
94
+ *
95
+ * ```ts
96
+ * export default {
97
+ * queue: async (batch, env) => {
98
+ * const mailer = createMailer({ apiKey: env.RESEND_API_KEY, from: "..." });
99
+ * for (const message of batch.messages) {
100
+ * await consumeQueuedSend(mailer, message.body);
101
+ * }
102
+ * },
103
+ * };
104
+ * ```
105
+ */
106
+ declare const consumeQueuedSend: (mailer: Mailer, payload: unknown) => Promise<{
107
+ id: string;
108
+ }>;
109
+ /**
110
+ * Render a React element to an HTML/text pair suitable for inlining into a
111
+ * provider payload. Wraps `@react-email/render` so we can swap to
112
+ * `@visulima/email`'s react-email template engine without touching callers.
113
+ */
114
+ declare const renderEmail: (element: ReactElement) => Promise<{
115
+ html: string;
116
+ text: string;
117
+ }>;
118
+ /**
119
+ * Build a Resend-backed transport via `@visulima/email`. Wraps the provider's
120
+ * `sendEmail()` into the minimal `{ send(payload) -> { id } }` shape the rest of
121
+ * `@lunora/mail` consumes. Kept reachable as a named export so a project that
122
+ * prefers Resend over the Cloudflare default can pass
123
+ * `transport: createResendTransport(apiKey, from)` to `createMailer`.
124
+ */
125
+ declare const createResendTransport: (apiKey: string, defaultFrom: string) => MailTransport;
126
+ export { type CloudflareSend, type CloudflareTransportOptions, type FromEnvOptions, type LunoraMailOptions, type MailEnv, type MailTransport, type MailboxSink, type Mailer, type QueuedSend, type SendOptions, consumeQueuedSend, createCaptureSink, createCloudflareTransport, createMailer, createMailerFromEnv, createResendTransport, renderEmail, shouldCaptureMail, toQueuedPayload };
@@ -0,0 +1,126 @@
1
+ import { M as MailTransport, L as LunoraMailOptions, a as Mailer, b as MailboxSink, S as SendOptions } from "./packem_shared/capture-transport.d-ChnhdPO2.js";
2
+ export { type C as CapturedMail, type Q as QueueLike, type c as SendPayload, d as createCaptureTransport } from "./packem_shared/capture-transport.d-ChnhdPO2.js";
3
+ import { ReactElement } from 'react';
4
+ /**
5
+ * Sends a raw RFC 822 message through a Worker's Email send binding. The Workers
6
+ * runtime owns the binding, so the caller supplies this thin callback — keeping
7
+ * `@lunora/mail` free of a `cloudflare:email` import and unit-testable. Wire it
8
+ * in your project as:
9
+ *
10
+ * ```ts
11
+ * send: async (from, to, raw) => {
12
+ * const { EmailMessage } = await import("cloudflare:email");
13
+ * await env.SEND_EMAIL.send(new EmailMessage(from, to, raw));
14
+ * }
15
+ * ```
16
+ */
17
+ type CloudflareSend = (from: string, to: string, raw: string) => Promise<void>;
18
+ interface CloudflareTransportOptions {
19
+ /** Default sender used when a `SendOptions.from` isn't supplied. */
20
+ from: string;
21
+ /** RFC 822 send callback bound to the Worker's `send_email` binding. */
22
+ send: CloudflareSend;
23
+ }
24
+ /**
25
+ * Build the default Cloudflare Email Workers transport via `@visulima/email`.
26
+ * Cloudflare's `send_email` binding is **single-recipient** and only delivers to
27
+ * **verified Email Routing destination addresses**, and the underlying provider
28
+ * rejects any `cc`/`bcc` or a non-single `to` outright. So this enforces a single
29
+ * `to` recipient (throwing a clear error instead of silently dropping the rest)
30
+ * and rejects `cc`/`bcc` rather than forwarding them into a generic "send failed".
31
+ * Multi-recipient transactional mail must fan out one send per recipient at the
32
+ * call site. In dev the capture transport intercepts before this runs, so the
33
+ * verified-address constraint never bites the dev loop.
34
+ */
35
+ declare const createCloudflareTransport: (options: CloudflareTransportOptions) => MailTransport;
36
+ declare const createMailer: (options: LunoraMailOptions) => Mailer;
37
+ /** A Worker `env` projected as a plain record (vars, secrets, and bindings are `unknown`-valued). */
38
+ type MailEnv = Record<string, unknown>;
39
+ /** Options for {@link createMailerFromEnv}. */
40
+ interface FromEnvOptions {
41
+ /** RFC 822 send callback bound to the Worker's `send_email` binding (Cloudflare default transport). */
42
+ cloudflareSend?: CloudflareSend;
43
+ /** Shard the captured-mail inbox lives on; override if your worker sets a custom `defaultShardKey`. */
44
+ rootShard?: string;
45
+ }
46
+ /**
47
+ * Whether outbound mail should be captured (into the studio inbox) rather than
48
+ * delivered. Explicit `LUNORA_MAIL_CAPTURE` (`"1"`/`"true"` vs `"0"`/`"false"`)
49
+ * always wins; unset, capture is on only in a development environment. It does
50
+ * NOT fall back to "no SEND_EMAIL binding ⇒ capture" — a production deploy that
51
+ * forgot the binding must fail loudly on send, not silently swallow mail.
52
+ */
53
+ declare const shouldCaptureMail: (env: MailEnv) => boolean;
54
+ /**
55
+ * Build the {@link MailboxSink} that records a captured message into the studio's
56
+ * root-shard inbox via the reserved `recordMail` admin RPC — the same
57
+ * worker→root-shard path the runtime uses for auth events. Best-effort: without
58
+ * the `SHARD` binding or `LUNORA_ADMIN_TOKEN` it returns a sentinel id so a send
59
+ * never fails for lack of somewhere to record.
60
+ */
61
+ declare const createCaptureSink: (env: MailEnv, rootShard?: string) => MailboxSink;
62
+ /**
63
+ * Build a {@link Mailer} from a Worker `env`. In a dev environment every send is
64
+ * captured into the studio's Mail inbox; otherwise it delivers via the supplied
65
+ * `cloudflareSend` (the `SEND_EMAIL` binding) or, failing that, `RESEND_API_KEY`.
66
+ * Throws when neither a capture context nor a real transport is available, so a
67
+ * misconfigured production deploy fails loudly instead of silently dropping mail.
68
+ *
69
+ * `MAIL_FROM` is required (the default sender).
70
+ */
71
+ declare const createMailerFromEnv: (env: MailEnv, options?: FromEnvOptions) => Mailer;
72
+ /** Serializable representation of a `SendOptions` payload — drops the `react` field. */
73
+ interface QueuedSend {
74
+ bcc?: string[];
75
+ cc?: string[];
76
+ from?: string;
77
+ headers?: Record<string, string>;
78
+ html?: string;
79
+ replyTo?: string;
80
+ subject: string;
81
+ text?: string;
82
+ to: string | string[];
83
+ }
84
+ /**
85
+ * Narrow a `SendOptions` to its serializable `QueuedSend` shape by dropping the
86
+ * non-cloneable `react` field. React elements are not structured-cloneable, so
87
+ * the queue body must carry only the pre-rendered html/text and scalar fields.
88
+ */
89
+ declare const toQueuedPayload: (options: SendOptions) => QueuedSend;
90
+ /**
91
+ * Helper used by Queue consumers: rehydrate a `QueuedSend` payload and forward
92
+ * to a configured `Mailer.send()`. Use this inside your Worker's `queue()`
93
+ * handler.
94
+ *
95
+ * ```ts
96
+ * export default {
97
+ * queue: async (batch, env) => {
98
+ * const mailer = createMailer({ apiKey: env.RESEND_API_KEY, from: "..." });
99
+ * for (const message of batch.messages) {
100
+ * await consumeQueuedSend(mailer, message.body);
101
+ * }
102
+ * },
103
+ * };
104
+ * ```
105
+ */
106
+ declare const consumeQueuedSend: (mailer: Mailer, payload: unknown) => Promise<{
107
+ id: string;
108
+ }>;
109
+ /**
110
+ * Render a React element to an HTML/text pair suitable for inlining into a
111
+ * provider payload. Wraps `@react-email/render` so we can swap to
112
+ * `@visulima/email`'s react-email template engine without touching callers.
113
+ */
114
+ declare const renderEmail: (element: ReactElement) => Promise<{
115
+ html: string;
116
+ text: string;
117
+ }>;
118
+ /**
119
+ * Build a Resend-backed transport via `@visulima/email`. Wraps the provider's
120
+ * `sendEmail()` into the minimal `{ send(payload) -> { id } }` shape the rest of
121
+ * `@lunora/mail` consumes. Kept reachable as a named export so a project that
122
+ * prefers Resend over the Cloudflare default can pass
123
+ * `transport: createResendTransport(apiKey, from)` to `createMailer`.
124
+ */
125
+ declare const createResendTransport: (apiKey: string, defaultFrom: string) => MailTransport;
126
+ export { type CloudflareSend, type CloudflareTransportOptions, type FromEnvOptions, type LunoraMailOptions, type MailEnv, type MailTransport, type MailboxSink, type Mailer, type QueuedSend, type SendOptions, consumeQueuedSend, createCaptureSink, createCloudflareTransport, createMailer, createMailerFromEnv, createResendTransport, renderEmail, shouldCaptureMail, toQueuedPayload };