@tgoliveira/outpost 1.1.0
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.
- package/AGENTS.md +268 -0
- package/LICENSE +21 -0
- package/README.md +117 -0
- package/dist/adapters/drizzle/index.cjs +405 -0
- package/dist/adapters/drizzle/index.cjs.map +1 -0
- package/dist/adapters/drizzle/index.d.cts +1765 -0
- package/dist/adapters/drizzle/index.d.ts +1765 -0
- package/dist/adapters/drizzle/index.js +387 -0
- package/dist/adapters/drizzle/index.js.map +1 -0
- package/dist/adapters/index.cjs +584 -0
- package/dist/adapters/index.cjs.map +1 -0
- package/dist/adapters/index.d.cts +232 -0
- package/dist/adapters/index.d.ts +232 -0
- package/dist/adapters/index.js +565 -0
- package/dist/adapters/index.js.map +1 -0
- package/dist/fake-BY6t7UXb.d.cts +30 -0
- package/dist/fake-en-oOdg0.d.ts +30 -0
- package/dist/http/index.cjs +224 -0
- package/dist/http/index.cjs.map +1 -0
- package/dist/http/index.d.cts +49 -0
- package/dist/http/index.d.ts +49 -0
- package/dist/http/index.js +221 -0
- package/dist/http/index.js.map +1 -0
- package/dist/index-CVUvaJms.d.ts +179 -0
- package/dist/index-Dmt-aLH2.d.cts +179 -0
- package/dist/index.cjs +1387 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +153 -0
- package/dist/index.d.ts +153 -0
- package/dist/index.js +1331 -0
- package/dist/index.js.map +1 -0
- package/dist/outpost-client-yUijE00q.d.cts +271 -0
- package/dist/outpost-client-ymTJISpu.d.ts +271 -0
- package/dist/repositories-Bck6wJNS.d.ts +180 -0
- package/dist/repositories-DhUe8f2R.d.cts +180 -0
- package/dist/services-CQINhAGo.d.cts +90 -0
- package/dist/services-CfTEsetF.d.ts +90 -0
- package/dist/template-CREUq7bB.d.ts +260 -0
- package/dist/template-DxI_p2Ny.d.cts +260 -0
- package/dist/testing/index.cjs +321 -0
- package/dist/testing/index.cjs.map +1 -0
- package/dist/testing/index.d.cts +97 -0
- package/dist/testing/index.d.ts +97 -0
- package/dist/testing/index.js +312 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/webhook-kp6CiXk-.d.cts +149 -0
- package/dist/webhook-kp6CiXk-.d.ts +149 -0
- package/dist/workers/index.cjs +430 -0
- package/dist/workers/index.cjs.map +1 -0
- package/dist/workers/index.d.cts +5 -0
- package/dist/workers/index.d.ts +5 -0
- package/dist/workers/index.js +427 -0
- package/dist/workers/index.js.map +1 -0
- package/package.json +114 -0
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { D as DispatchableMessage, W as WebhookEvent } from './webhook-kp6CiXk-.cjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Receipt returned by a provider when it accepts a message for delivery.
|
|
5
|
+
* `accepted` means the provider took responsibility — NOT that it was
|
|
6
|
+
* delivered. Delivery is confirmed asynchronously via `verifyWebhook`.
|
|
7
|
+
*/
|
|
8
|
+
interface ProviderReceipt {
|
|
9
|
+
readonly providerMessageId: string;
|
|
10
|
+
/** Provider-specific accepted timestamp, if returned. */
|
|
11
|
+
readonly acceptedAt?: Date;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Raw inbound webhook request as received by the HTTP layer, handed to the
|
|
15
|
+
* provider adapter for signature verification + normalization.
|
|
16
|
+
*/
|
|
17
|
+
interface RawWebhook {
|
|
18
|
+
readonly headers: Record<string, string | string[] | undefined>;
|
|
19
|
+
/** The exact raw request body bytes/string — required for HMAC signatures. */
|
|
20
|
+
readonly rawBody: string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The single transport port (TDR §3.8, §6). Every provider — SMTP/Mailpit,
|
|
24
|
+
* Resend, SES, the in-memory fake — implements exactly this. Adding a provider
|
|
25
|
+
* means adding an implementation, never editing the core (Open/Closed).
|
|
26
|
+
*
|
|
27
|
+
* Implementations MUST throw `ProviderError` (transient | permanent) on
|
|
28
|
+
* failure so the dispatch pipeline can decide retry vs. dead-letter without
|
|
29
|
+
* inspecting provider-specific error shapes.
|
|
30
|
+
*/
|
|
31
|
+
interface EmailProvider {
|
|
32
|
+
/** Stable key used in config and persisted on the message (e.g. "resend"). */
|
|
33
|
+
readonly name: string;
|
|
34
|
+
/** Hand a fully-resolved, decrypted message to the provider. */
|
|
35
|
+
send(msg: DispatchableMessage): Promise<ProviderReceipt>;
|
|
36
|
+
/**
|
|
37
|
+
* Verify a raw inbound webhook and normalize it. Throws
|
|
38
|
+
* `WebhookVerificationError` if the signature is invalid. Returning a
|
|
39
|
+
* uniform `WebhookEvent` is what enables provider fallback and one ingestion
|
|
40
|
+
* path for all providers.
|
|
41
|
+
*/
|
|
42
|
+
verifyWebhook(req: RawWebhook): Promise<WebhookEvent>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Cross-cutting service ports (TDR §6): rate limiting, time, ids, logging,
|
|
47
|
+
* and observability. Narrow by design; injected at the edge.
|
|
48
|
+
*/
|
|
49
|
+
/** Scope a rate-limit acquisition targets: global, a provider, or a domain. */
|
|
50
|
+
interface RateLimitScope {
|
|
51
|
+
readonly kind: "global" | "provider" | "recipientDomain";
|
|
52
|
+
/** The provider name or recipient domain; absent for global. */
|
|
53
|
+
readonly key?: string;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Layered rate limiter (TDR §3.2 step 4). `acquire` returns false when the
|
|
57
|
+
* caller is over budget for the given scope; the send worker then re-queues
|
|
58
|
+
* the message with a short backoff rather than dropping it.
|
|
59
|
+
*/
|
|
60
|
+
interface RateLimiter {
|
|
61
|
+
acquire(scope: RateLimitScope): Promise<boolean>;
|
|
62
|
+
}
|
|
63
|
+
/** Injectable clock — keeps use cases deterministic and testable. */
|
|
64
|
+
interface Clock {
|
|
65
|
+
now(): Date;
|
|
66
|
+
}
|
|
67
|
+
/** Injectable id generator (UUID v4 by default). */
|
|
68
|
+
interface IdGenerator {
|
|
69
|
+
generate(): string;
|
|
70
|
+
}
|
|
71
|
+
type LogLevel = "debug" | "info" | "warn" | "error";
|
|
72
|
+
/** Minimal structured logger port. Console adapter provided by default. */
|
|
73
|
+
interface Logger {
|
|
74
|
+
log(level: LogLevel, message: string, fields?: Record<string, unknown>): void;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Observability port (TDR §8). A thin wrapper so the core can emit spans and
|
|
78
|
+
* metrics without importing the OpenTelemetry SDK directly. A no-op default is
|
|
79
|
+
* used when otel is disabled; an OTel-backed adapter is provided.
|
|
80
|
+
*/
|
|
81
|
+
interface Telemetry {
|
|
82
|
+
/** Run `fn` inside a span named `name`, recording errors and duration. */
|
|
83
|
+
span<T>(name: string, attrs: Record<string, unknown>, fn: () => Promise<T>): Promise<T>;
|
|
84
|
+
/** Increment a counter metric. */
|
|
85
|
+
counter(name: string, value: number, attrs?: Record<string, unknown>): void;
|
|
86
|
+
/** Record a gauge/observation (e.g. queue depth). */
|
|
87
|
+
gauge(name: string, value: number, attrs?: Record<string, unknown>): void;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export type { Clock as C, EmailProvider as E, IdGenerator as I, Logger as L, ProviderReceipt as P, RawWebhook as R, Telemetry as T, RateLimiter as a, LogLevel as b, RateLimitScope as c };
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { D as DispatchableMessage, W as WebhookEvent } from './webhook-kp6CiXk-.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Receipt returned by a provider when it accepts a message for delivery.
|
|
5
|
+
* `accepted` means the provider took responsibility — NOT that it was
|
|
6
|
+
* delivered. Delivery is confirmed asynchronously via `verifyWebhook`.
|
|
7
|
+
*/
|
|
8
|
+
interface ProviderReceipt {
|
|
9
|
+
readonly providerMessageId: string;
|
|
10
|
+
/** Provider-specific accepted timestamp, if returned. */
|
|
11
|
+
readonly acceptedAt?: Date;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Raw inbound webhook request as received by the HTTP layer, handed to the
|
|
15
|
+
* provider adapter for signature verification + normalization.
|
|
16
|
+
*/
|
|
17
|
+
interface RawWebhook {
|
|
18
|
+
readonly headers: Record<string, string | string[] | undefined>;
|
|
19
|
+
/** The exact raw request body bytes/string — required for HMAC signatures. */
|
|
20
|
+
readonly rawBody: string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The single transport port (TDR §3.8, §6). Every provider — SMTP/Mailpit,
|
|
24
|
+
* Resend, SES, the in-memory fake — implements exactly this. Adding a provider
|
|
25
|
+
* means adding an implementation, never editing the core (Open/Closed).
|
|
26
|
+
*
|
|
27
|
+
* Implementations MUST throw `ProviderError` (transient | permanent) on
|
|
28
|
+
* failure so the dispatch pipeline can decide retry vs. dead-letter without
|
|
29
|
+
* inspecting provider-specific error shapes.
|
|
30
|
+
*/
|
|
31
|
+
interface EmailProvider {
|
|
32
|
+
/** Stable key used in config and persisted on the message (e.g. "resend"). */
|
|
33
|
+
readonly name: string;
|
|
34
|
+
/** Hand a fully-resolved, decrypted message to the provider. */
|
|
35
|
+
send(msg: DispatchableMessage): Promise<ProviderReceipt>;
|
|
36
|
+
/**
|
|
37
|
+
* Verify a raw inbound webhook and normalize it. Throws
|
|
38
|
+
* `WebhookVerificationError` if the signature is invalid. Returning a
|
|
39
|
+
* uniform `WebhookEvent` is what enables provider fallback and one ingestion
|
|
40
|
+
* path for all providers.
|
|
41
|
+
*/
|
|
42
|
+
verifyWebhook(req: RawWebhook): Promise<WebhookEvent>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Cross-cutting service ports (TDR §6): rate limiting, time, ids, logging,
|
|
47
|
+
* and observability. Narrow by design; injected at the edge.
|
|
48
|
+
*/
|
|
49
|
+
/** Scope a rate-limit acquisition targets: global, a provider, or a domain. */
|
|
50
|
+
interface RateLimitScope {
|
|
51
|
+
readonly kind: "global" | "provider" | "recipientDomain";
|
|
52
|
+
/** The provider name or recipient domain; absent for global. */
|
|
53
|
+
readonly key?: string;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Layered rate limiter (TDR §3.2 step 4). `acquire` returns false when the
|
|
57
|
+
* caller is over budget for the given scope; the send worker then re-queues
|
|
58
|
+
* the message with a short backoff rather than dropping it.
|
|
59
|
+
*/
|
|
60
|
+
interface RateLimiter {
|
|
61
|
+
acquire(scope: RateLimitScope): Promise<boolean>;
|
|
62
|
+
}
|
|
63
|
+
/** Injectable clock — keeps use cases deterministic and testable. */
|
|
64
|
+
interface Clock {
|
|
65
|
+
now(): Date;
|
|
66
|
+
}
|
|
67
|
+
/** Injectable id generator (UUID v4 by default). */
|
|
68
|
+
interface IdGenerator {
|
|
69
|
+
generate(): string;
|
|
70
|
+
}
|
|
71
|
+
type LogLevel = "debug" | "info" | "warn" | "error";
|
|
72
|
+
/** Minimal structured logger port. Console adapter provided by default. */
|
|
73
|
+
interface Logger {
|
|
74
|
+
log(level: LogLevel, message: string, fields?: Record<string, unknown>): void;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Observability port (TDR §8). A thin wrapper so the core can emit spans and
|
|
78
|
+
* metrics without importing the OpenTelemetry SDK directly. A no-op default is
|
|
79
|
+
* used when otel is disabled; an OTel-backed adapter is provided.
|
|
80
|
+
*/
|
|
81
|
+
interface Telemetry {
|
|
82
|
+
/** Run `fn` inside a span named `name`, recording errors and duration. */
|
|
83
|
+
span<T>(name: string, attrs: Record<string, unknown>, fn: () => Promise<T>): Promise<T>;
|
|
84
|
+
/** Increment a counter metric. */
|
|
85
|
+
counter(name: string, value: number, attrs?: Record<string, unknown>): void;
|
|
86
|
+
/** Record a gauge/observation (e.g. queue depth). */
|
|
87
|
+
gauge(name: string, value: number, attrs?: Record<string, unknown>): void;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export type { Clock as C, EmailProvider as E, IdGenerator as I, Logger as L, ProviderReceipt as P, RawWebhook as R, Telemetry as T, RateLimiter as a, LogLevel as b, RateLimitScope as c };
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import { S as Sealed, b as MessageBody } from './webhook-kp6CiXk-.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Encryption ports (TDR §5.4).
|
|
5
|
+
*
|
|
6
|
+
* `Encryptor` is split into seal (write) and open (read) so the asymmetric,
|
|
7
|
+
* least-privilege model is expressible in the type system: the ingestion API
|
|
8
|
+
* is wired with a seal-only encryptor (public key), while the send worker holds
|
|
9
|
+
* the open-capable one (private key). Compromising the web API therefore can
|
|
10
|
+
* never read plaintext bodies back.
|
|
11
|
+
*/
|
|
12
|
+
interface Encryptor {
|
|
13
|
+
/** Algorithm tag stamped onto produced `Sealed` values. */
|
|
14
|
+
readonly alg: string;
|
|
15
|
+
/** Encrypt plaintext for storage at rest. */
|
|
16
|
+
seal(plaintext: string): Promise<Sealed>;
|
|
17
|
+
/**
|
|
18
|
+
* Decrypt a sealed value. Seal-only encryptors (public-key ingestion side)
|
|
19
|
+
* reject this with an error — by construction they cannot read.
|
|
20
|
+
*/
|
|
21
|
+
open(sealed: Sealed): Promise<string>;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Deterministic keyed HMAC of a recipient address. Used as the searchable
|
|
25
|
+
* column for suppression matching and idempotency-by-recipient when the
|
|
26
|
+
* plaintext recipient is encrypted (TDR §5.4 condition 3). Deterministic so the
|
|
27
|
+
* same address always maps to the same digest; keyed so the digest cannot be
|
|
28
|
+
* brute-forced from a leaked dump without the key.
|
|
29
|
+
*/
|
|
30
|
+
interface RecipientHasher {
|
|
31
|
+
hash(address: string): string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Retry policy: exponential backoff with full jitter (TDR §3.4).
|
|
36
|
+
*
|
|
37
|
+
* Pure function of (attempt number, config) → delay. Jitter is injected as a
|
|
38
|
+
* 0..1 random sample so the policy stays deterministic and testable (the
|
|
39
|
+
* worker passes a real random; tests pass a fixed value).
|
|
40
|
+
*/
|
|
41
|
+
interface RetryConfig {
|
|
42
|
+
/** Max dispatch attempts before a message is dead-lettered. */
|
|
43
|
+
readonly maxAttempts: number;
|
|
44
|
+
/** Base backoff in ms (first retry waits ~this, before jitter). */
|
|
45
|
+
readonly baseDelayMs: number;
|
|
46
|
+
/** Cap on the backoff in ms. */
|
|
47
|
+
readonly maxDelayMs: number;
|
|
48
|
+
}
|
|
49
|
+
declare const DEFAULT_RETRY: RetryConfig;
|
|
50
|
+
/** True once attempts have reached the budget — message goes to the DLQ. */
|
|
51
|
+
declare function isExhausted(attempts: number, config: RetryConfig): boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Backoff for the NEXT attempt after `attempts` failed tries.
|
|
54
|
+
* delay = min(maxDelay, base * 2^(attempts-1)) then full-jitter: random in
|
|
55
|
+
* [0, delay]. `jitter` is a sample in [0,1).
|
|
56
|
+
*/
|
|
57
|
+
declare function backoffMs(attempts: number, config: RetryConfig, jitter: number): number;
|
|
58
|
+
/** Convenience: the Date of the next attempt from `now`. */
|
|
59
|
+
declare function nextAttemptAt(attempts: number, config: RetryConfig, jitter: number, now: Date): Date;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Outbound sanitization (TDR §3.2 step 2, §5.3).
|
|
63
|
+
*
|
|
64
|
+
* These are conservative, dependency-free guards focused on the email-specific
|
|
65
|
+
* attack surface: header injection (CRLF) and oversize payloads. For rich HTML
|
|
66
|
+
* sanitization against XSS you SHOULD plug a hardened sanitizer (e.g.
|
|
67
|
+
* `sanitize-html` / DOMPurify) via `createOutpost({ sanitizeHtml })`; the
|
|
68
|
+
* built-in `stripDangerousHtml` below is a last-resort fallback, not a
|
|
69
|
+
* substitute for a real sanitizer.
|
|
70
|
+
*/
|
|
71
|
+
/** Default ceilings; override via config. */
|
|
72
|
+
interface SanitizeLimits {
|
|
73
|
+
readonly maxSubjectBytes: number;
|
|
74
|
+
readonly maxBodyBytes: number;
|
|
75
|
+
readonly maxRecipientBytes: number;
|
|
76
|
+
readonly maxHeaderValueBytes: number;
|
|
77
|
+
}
|
|
78
|
+
declare const DEFAULT_SANITIZE_LIMITS: SanitizeLimits;
|
|
79
|
+
/**
|
|
80
|
+
* Reject control characters in any value used as an email header (To, Subject,
|
|
81
|
+
* custom headers). This is THE header-injection defense — without it an attacker
|
|
82
|
+
* can inject a CR/LF + "Bcc:" and turn one message into many, or use a NUL to
|
|
83
|
+
* truncate. Implemented as a char-code scan (not a regex) so the intent is
|
|
84
|
+
* unambiguous: reject every C0 control char (0x00–0x1F, which includes CR, LF,
|
|
85
|
+
* NUL and TAB) and DEL (0x7F), while ALLOWING ordinary printable characters
|
|
86
|
+
* including the space (0x20). An earlier regex form also matched the space,
|
|
87
|
+
* which wrongly rejected normal multi-word subjects.
|
|
88
|
+
*/
|
|
89
|
+
declare function assertNoHeaderInjection(field: string, value: string): void;
|
|
90
|
+
declare function sanitizeSubject(subject: string, limits?: SanitizeLimits): string;
|
|
91
|
+
declare function sanitizeRecipient(to: string, limits?: SanitizeLimits): string;
|
|
92
|
+
declare function sanitizeHeaders(headers: Record<string, string>, limits?: SanitizeLimits): Record<string, string>;
|
|
93
|
+
declare function assertBodyWithinLimits(body: {
|
|
94
|
+
html?: string;
|
|
95
|
+
text?: string;
|
|
96
|
+
}, limits?: SanitizeLimits): void;
|
|
97
|
+
/**
|
|
98
|
+
* Last-resort HTML hardening: strip <script>/<style>/<iframe>/<object> blocks,
|
|
99
|
+
* `on*=` event handlers, and `javascript:` URLs. NOT a full sanitizer — prefer
|
|
100
|
+
* a real one via config. Returns cleaned HTML.
|
|
101
|
+
*/
|
|
102
|
+
declare function stripDangerousHtml(html: string): string;
|
|
103
|
+
/** Validate an attachment's MIME type and size (TDR §3.2). */
|
|
104
|
+
interface AttachmentSpec {
|
|
105
|
+
readonly filename: string;
|
|
106
|
+
readonly contentType: string;
|
|
107
|
+
readonly sizeBytes: number;
|
|
108
|
+
}
|
|
109
|
+
interface AttachmentPolicy {
|
|
110
|
+
readonly allowedMimeTypes: readonly string[];
|
|
111
|
+
readonly maxSizeBytes: number;
|
|
112
|
+
}
|
|
113
|
+
declare const DEFAULT_ATTACHMENT_POLICY: AttachmentPolicy;
|
|
114
|
+
declare function validateAttachment(att: AttachmentSpec, policy?: AttachmentPolicy): void;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Recipient domain validation (TDR §3.2 step 3, §3.10, decision #4).
|
|
118
|
+
*
|
|
119
|
+
* Two checks, in order of cost:
|
|
120
|
+
* 1. Syntax — always on. RFC-5322-ish address shape.
|
|
121
|
+
* 2. MX records — optional, cached. Confirms the domain can receive mail.
|
|
122
|
+
*
|
|
123
|
+
* Outpost deliberately does NOT probe whether an individual mailbox exists:
|
|
124
|
+
* it is unreliable and damages sender reputation (decision #4).
|
|
125
|
+
*/
|
|
126
|
+
interface DomainValidationConfig {
|
|
127
|
+
readonly syntax: boolean;
|
|
128
|
+
readonly mx: boolean;
|
|
129
|
+
readonly mailboxProbe: false;
|
|
130
|
+
/** TTL for the MX-result cache, ms. */
|
|
131
|
+
readonly mxCacheTtlMs?: number;
|
|
132
|
+
}
|
|
133
|
+
declare const DEFAULT_DOMAIN_VALIDATION: DomainValidationConfig;
|
|
134
|
+
/**
|
|
135
|
+
* Pluggable MX resolver so the core stays free of `node:dns`. The default
|
|
136
|
+
* resolver (provided in adapters) uses `dns/promises`. Returns the MX hosts.
|
|
137
|
+
*/
|
|
138
|
+
interface MxResolver {
|
|
139
|
+
resolveMx(domain: string): Promise<string[]>;
|
|
140
|
+
}
|
|
141
|
+
declare function extractDomain(address: string): string;
|
|
142
|
+
declare function validateSyntax(address: string): string;
|
|
143
|
+
/** Small in-process TTL cache for MX results to avoid hammering DNS. */
|
|
144
|
+
declare class MxCache {
|
|
145
|
+
private readonly ttlMs;
|
|
146
|
+
private readonly entries;
|
|
147
|
+
constructor(ttlMs: number);
|
|
148
|
+
get(domain: string, nowMs: number): string[] | undefined;
|
|
149
|
+
set(domain: string, hosts: string[], nowMs: number): void;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Full recipient validation. `syntax` is enforced synchronously; `mx`, when
|
|
153
|
+
* enabled, resolves (cached) and throws if the domain has no usable MX (and no
|
|
154
|
+
* fallback A record — many small domains accept mail on the A record).
|
|
155
|
+
*/
|
|
156
|
+
declare function validateRecipient(address: string, config: DomainValidationConfig, deps: {
|
|
157
|
+
mxResolver?: MxResolver;
|
|
158
|
+
mxCache?: MxCache;
|
|
159
|
+
nowMs: number;
|
|
160
|
+
}): Promise<void>;
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Retention policy domain types (TDR §3.9, §5.5).
|
|
164
|
+
*
|
|
165
|
+
* Operational data (the outbox payload) and audit data have independent TTLs.
|
|
166
|
+
* The default action on purge is REDACTION, not deletion: the PII payload is
|
|
167
|
+
* wiped while the audit row (metadata + HMAC) survives. This satisfies data
|
|
168
|
+
* minimization without destroying the audit trail.
|
|
169
|
+
*/
|
|
170
|
+
interface RetentionPolicy {
|
|
171
|
+
/** Age (days) after which a terminal outbox row's PII may be purged. */
|
|
172
|
+
readonly operationalTtlDays: number;
|
|
173
|
+
/** Age (days) after which audit events may be deleted. */
|
|
174
|
+
readonly auditTtlDays: number;
|
|
175
|
+
/** When true (default) purge redacts PII; when false it deletes the row. */
|
|
176
|
+
readonly redactOnPurge: boolean;
|
|
177
|
+
/**
|
|
178
|
+
* Grace window (hours) after a message reaches a terminal state during which
|
|
179
|
+
* it is NOT purged, so late webhooks (delivery/bounce) can still arrive.
|
|
180
|
+
* Purging earlier would break bounce/complaint handling.
|
|
181
|
+
*/
|
|
182
|
+
readonly webhookWindowHours: number;
|
|
183
|
+
/** Rows deleted/redacted per batch, to avoid long table locks. */
|
|
184
|
+
readonly batchSize: number;
|
|
185
|
+
}
|
|
186
|
+
declare const DEFAULT_RETENTION: RetentionPolicy;
|
|
187
|
+
/**
|
|
188
|
+
* The cutoff before which a terminal message becomes eligible for purge:
|
|
189
|
+
* it must be older than the operational TTL AND past the webhook window.
|
|
190
|
+
* Returns the latest (most conservative) of the two cutoffs.
|
|
191
|
+
*/
|
|
192
|
+
declare function purgeCutoff(policy: RetentionPolicy, now: Date): Date;
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Layered rate-limit budgets (TDR §3.2 step 4). Each is a token-bucket-style
|
|
196
|
+
* "max events per window". The in-memory limiter and any external limiter share
|
|
197
|
+
* this shape via the `RateLimiter` port.
|
|
198
|
+
*/
|
|
199
|
+
interface RateLimitConfig {
|
|
200
|
+
/** Global ceiling across all providers/recipients. */
|
|
201
|
+
readonly global?: RateBudget;
|
|
202
|
+
/** Per-provider ceiling, keyed by provider name. */
|
|
203
|
+
readonly perProvider?: RateBudget;
|
|
204
|
+
/** Per recipient-domain ceiling. */
|
|
205
|
+
readonly perRecipientDomain?: RateBudget;
|
|
206
|
+
}
|
|
207
|
+
interface RateBudget {
|
|
208
|
+
readonly max: number;
|
|
209
|
+
readonly windowMs: number;
|
|
210
|
+
}
|
|
211
|
+
/** Fully-resolved internal config — every field defaulted, no optionals. */
|
|
212
|
+
interface ResolvedConfig {
|
|
213
|
+
readonly retry: RetryConfig;
|
|
214
|
+
readonly retention: RetentionPolicy;
|
|
215
|
+
readonly sanitize: SanitizeLimits;
|
|
216
|
+
readonly attachments: AttachmentPolicy;
|
|
217
|
+
readonly domainValidation: DomainValidationConfig;
|
|
218
|
+
readonly rateLimits: RateLimitConfig;
|
|
219
|
+
/** Max rows the send worker claims per poll. */
|
|
220
|
+
readonly sendBatchSize: number;
|
|
221
|
+
/**
|
|
222
|
+
* Lease window for `sending` rows, ms. A row left in `sending` longer than
|
|
223
|
+
* this is treated as abandoned (its worker crashed mid-dispatch) and is
|
|
224
|
+
* reclaimed by the next poll. Keep comfortably larger than a real dispatch.
|
|
225
|
+
*/
|
|
226
|
+
readonly staleSendingLeaseMs: number;
|
|
227
|
+
}
|
|
228
|
+
interface ConfigOverrides {
|
|
229
|
+
retry?: Partial<RetryConfig>;
|
|
230
|
+
retention?: Partial<RetentionPolicy>;
|
|
231
|
+
sanitize?: Partial<SanitizeLimits>;
|
|
232
|
+
attachments?: Partial<AttachmentPolicy>;
|
|
233
|
+
domainValidation?: Partial<Omit<DomainValidationConfig, "mailboxProbe">>;
|
|
234
|
+
rateLimits?: RateLimitConfig;
|
|
235
|
+
sendBatchSize?: number;
|
|
236
|
+
staleSendingLeaseMs?: number;
|
|
237
|
+
}
|
|
238
|
+
declare function resolveConfig(overrides?: ConfigOverrides): ResolvedConfig;
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Template port (TDR §3.7). Resolves a template id+version and a bag of
|
|
242
|
+
* variables into a concrete subject + body. Implementations MUST escape
|
|
243
|
+
* user-supplied variables to prevent injection into HTML.
|
|
244
|
+
*
|
|
245
|
+
* Phase 1 ships a simple in-memory renderer; versioning + an admin editor are
|
|
246
|
+
* Phase 2, but the port is defined now so the API shape is stable.
|
|
247
|
+
*/
|
|
248
|
+
interface RenderedTemplate {
|
|
249
|
+
readonly subject: string;
|
|
250
|
+
readonly body: MessageBody;
|
|
251
|
+
}
|
|
252
|
+
interface TemplateRenderer {
|
|
253
|
+
render(input: {
|
|
254
|
+
id: string;
|
|
255
|
+
version?: number;
|
|
256
|
+
vars: Record<string, unknown>;
|
|
257
|
+
}): Promise<RenderedTemplate>;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export { type AttachmentPolicy as A, validateRecipient as B, type ConfigOverrides as C, DEFAULT_ATTACHMENT_POLICY as D, type Encryptor as E, validateSyntax as F, type MxResolver as M, type RateLimitConfig as R, type SanitizeLimits as S, type TemplateRenderer as T, type AttachmentSpec as a, DEFAULT_DOMAIN_VALIDATION as b, DEFAULT_RETENTION as c, DEFAULT_RETRY as d, DEFAULT_SANITIZE_LIMITS as e, type DomainValidationConfig as f, MxCache as g, type RateBudget as h, type RecipientHasher as i, type RenderedTemplate as j, type ResolvedConfig as k, type RetentionPolicy as l, type RetryConfig as m, assertBodyWithinLimits as n, assertNoHeaderInjection as o, backoffMs as p, extractDomain as q, isExhausted as r, nextAttemptAt as s, purgeCutoff as t, resolveConfig as u, sanitizeHeaders as v, sanitizeRecipient as w, sanitizeSubject as x, stripDangerousHtml as y, validateAttachment as z };
|