@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.
Files changed (54) hide show
  1. package/AGENTS.md +268 -0
  2. package/LICENSE +21 -0
  3. package/README.md +117 -0
  4. package/dist/adapters/drizzle/index.cjs +405 -0
  5. package/dist/adapters/drizzle/index.cjs.map +1 -0
  6. package/dist/adapters/drizzle/index.d.cts +1765 -0
  7. package/dist/adapters/drizzle/index.d.ts +1765 -0
  8. package/dist/adapters/drizzle/index.js +387 -0
  9. package/dist/adapters/drizzle/index.js.map +1 -0
  10. package/dist/adapters/index.cjs +584 -0
  11. package/dist/adapters/index.cjs.map +1 -0
  12. package/dist/adapters/index.d.cts +232 -0
  13. package/dist/adapters/index.d.ts +232 -0
  14. package/dist/adapters/index.js +565 -0
  15. package/dist/adapters/index.js.map +1 -0
  16. package/dist/fake-BY6t7UXb.d.cts +30 -0
  17. package/dist/fake-en-oOdg0.d.ts +30 -0
  18. package/dist/http/index.cjs +224 -0
  19. package/dist/http/index.cjs.map +1 -0
  20. package/dist/http/index.d.cts +49 -0
  21. package/dist/http/index.d.ts +49 -0
  22. package/dist/http/index.js +221 -0
  23. package/dist/http/index.js.map +1 -0
  24. package/dist/index-CVUvaJms.d.ts +179 -0
  25. package/dist/index-Dmt-aLH2.d.cts +179 -0
  26. package/dist/index.cjs +1387 -0
  27. package/dist/index.cjs.map +1 -0
  28. package/dist/index.d.cts +153 -0
  29. package/dist/index.d.ts +153 -0
  30. package/dist/index.js +1331 -0
  31. package/dist/index.js.map +1 -0
  32. package/dist/outpost-client-yUijE00q.d.cts +271 -0
  33. package/dist/outpost-client-ymTJISpu.d.ts +271 -0
  34. package/dist/repositories-Bck6wJNS.d.ts +180 -0
  35. package/dist/repositories-DhUe8f2R.d.cts +180 -0
  36. package/dist/services-CQINhAGo.d.cts +90 -0
  37. package/dist/services-CfTEsetF.d.ts +90 -0
  38. package/dist/template-CREUq7bB.d.ts +260 -0
  39. package/dist/template-DxI_p2Ny.d.cts +260 -0
  40. package/dist/testing/index.cjs +321 -0
  41. package/dist/testing/index.cjs.map +1 -0
  42. package/dist/testing/index.d.cts +97 -0
  43. package/dist/testing/index.d.ts +97 -0
  44. package/dist/testing/index.js +312 -0
  45. package/dist/testing/index.js.map +1 -0
  46. package/dist/webhook-kp6CiXk-.d.cts +149 -0
  47. package/dist/webhook-kp6CiXk-.d.ts +149 -0
  48. package/dist/workers/index.cjs +430 -0
  49. package/dist/workers/index.cjs.map +1 -0
  50. package/dist/workers/index.d.cts +5 -0
  51. package/dist/workers/index.d.ts +5 -0
  52. package/dist/workers/index.js +427 -0
  53. package/dist/workers/index.js.map +1 -0
  54. package/package.json +114 -0
@@ -0,0 +1,149 @@
1
+ /**
2
+ * Message lifecycle states and the transition rules between them.
3
+ *
4
+ * This module is PURE: no I/O, no framework, no provider. It encodes the
5
+ * state machine described in the TDR §3.3. Everything outward (use cases,
6
+ * repositories, workers) leans on these rules rather than re-deriving them.
7
+ *
8
+ * ┌─────────────┐
9
+ * enqueue ─────▶│ queued │
10
+ * └──────┬──────┘
11
+ * │ claimed by send worker
12
+ * ▼
13
+ * ┌─────────────┐ suppressed recipient
14
+ * │ sending │────────────────────────▶ suppressed
15
+ * └──────┬──────┘
16
+ * │ provider accepted
17
+ * ▼
18
+ * ┌─────────────┐
19
+ * │ sent │ (accepted, not yet confirmed)
20
+ * └──────┬──────┘
21
+ * │ webhook
22
+ * ┌──────────────┼───────────────┬────────────┐
23
+ * ▼ ▼ ▼ ▼
24
+ * delivered bounced complained failed
25
+ *
26
+ * Transient dispatch error → back to `queued` with backoff (retry).
27
+ * Permanent error / retries exhausted → `failed` (terminal, DLQ).
28
+ */
29
+ declare const LIFECYCLE_STATES: readonly ["queued", "sending", "sent", "delivered", "bounced", "complained", "failed", "suppressed"];
30
+ type LifecycleState = (typeof LIFECYCLE_STATES)[number];
31
+ /**
32
+ * Terminal states never transition again. The retention worker only ever acts
33
+ * on rows in a terminal state (and only after the webhook window has closed),
34
+ * which is what makes it safe to run concurrently with the send worker.
35
+ */
36
+ declare const TERMINAL_STATES: readonly ["delivered", "bounced", "complained", "failed", "suppressed"];
37
+ declare function isTerminal(state: LifecycleState): boolean;
38
+ declare function canTransition(from: LifecycleState, to: LifecycleState): boolean;
39
+ /**
40
+ * Which lifecycle states must feed the suppression list when reached.
41
+ * Hard bounces and complaints damage sender reputation if re-sent, so the
42
+ * recipient is suppressed automatically (TDR §3.6).
43
+ */
44
+ declare function shouldSuppressOn(state: LifecycleState): boolean;
45
+
46
+ /**
47
+ * The Message entity — the heart of the outbox. Pure data + invariants.
48
+ *
49
+ * PII fields (`recipientSealed`, `bodySealed`) hold ciphertext when encryption
50
+ * is enabled, plaintext otherwise. The plaintext recipient is NEVER stored in
51
+ * a queryable column; suppression and idempotency-by-recipient match on
52
+ * `recipientHmac` instead (TDR §5.4).
53
+ */
54
+ interface Message {
55
+ readonly id: string;
56
+ /** Caller-supplied or content-derived. Unique. Dedupes re-enqueues. */
57
+ readonly idempotencyKey: string;
58
+ state: LifecycleState;
59
+ /** Deterministic keyed HMAC of the recipient address. Searchable. */
60
+ readonly recipientHmac: string;
61
+ /** Sealed (encrypted-at-rest) recipient address. */
62
+ recipientSealed: Sealed;
63
+ /** Sealed (encrypted-at-rest) rendered body (html and/or text). */
64
+ bodySealed: Sealed;
65
+ /** Clear, queryable. Subject line (already sanitized). */
66
+ subject: string;
67
+ readonly templateId: string | null;
68
+ readonly templateVersion: number | null;
69
+ /** Provider key selected for dispatch (e.g. "resend", "smtp"). */
70
+ provider: string;
71
+ /** Clear, queryable JSON the caller attaches (e.g. orderId). Never PII. */
72
+ readonly metadata: Record<string, unknown>;
73
+ /** Number of dispatch attempts so far. */
74
+ attempts: number;
75
+ /** Earliest time the send worker may (re-)attempt dispatch. */
76
+ nextAttemptAt: Date;
77
+ /** Optional future-send time (Phase 2 scheduling). */
78
+ scheduledFor: Date | null;
79
+ /** Provider's message id, populated once accepted. */
80
+ providerMessageId: string | null;
81
+ /** Human-readable reason for the latest terminal/error state. */
82
+ lastError: string | null;
83
+ readonly createdAt: Date;
84
+ updatedAt: Date;
85
+ }
86
+ /**
87
+ * An encrypted-at-rest payload. When encryption is disabled the `Encryptor`
88
+ * returns a `Sealed` with `alg: "plain"` so the column shape never changes —
89
+ * encryption can be turned on later without a migration of the row format.
90
+ */
91
+ interface Sealed {
92
+ /** Algorithm tag, e.g. "plain" | "aes-256-gcm" | "hybrid-rsa-aes-256-gcm". */
93
+ readonly alg: string;
94
+ /** Base64 ciphertext (or plaintext when alg === "plain"). */
95
+ readonly ciphertext: string;
96
+ /** Base64 IV/nonce (AEAD schemes). */
97
+ readonly iv?: string;
98
+ /** Base64 auth tag (AEAD schemes). */
99
+ readonly tag?: string;
100
+ /** Base64 wrapped data key (hybrid schemes). */
101
+ readonly wrappedKey?: string;
102
+ /** Identifier of the key/key-version used, for rotation. */
103
+ readonly keyId?: string;
104
+ }
105
+ /** The decrypted body handed to the provider at dispatch time. */
106
+ interface MessageBody {
107
+ html?: string;
108
+ text?: string;
109
+ }
110
+ /**
111
+ * The fully-resolved, decrypted message a provider adapter receives. The send
112
+ * worker constructs this immediately before dispatch and never persists it.
113
+ */
114
+ interface DispatchableMessage {
115
+ readonly id: string;
116
+ readonly to: string;
117
+ readonly subject: string;
118
+ readonly body: MessageBody;
119
+ readonly headers: Record<string, string>;
120
+ readonly metadata: Record<string, unknown>;
121
+ }
122
+
123
+ /**
124
+ * Normalized webhook event (TDR §3.5).
125
+ *
126
+ * Each provider has its own webhook payload shape and signature scheme. The
127
+ * provider adapter's `verifyWebhook` verifies the signature and maps the native
128
+ * payload onto this uniform type, so the `IngestWebhook` use case never has to
129
+ * know about provider specifics.
130
+ */
131
+ type WebhookEventType = "delivered" | "bounced" | "complained" | "opened" | "failed";
132
+ interface WebhookEvent {
133
+ readonly provider: string;
134
+ readonly type: WebhookEventType;
135
+ /** Provider's message id — used to correlate back to the outbox row. */
136
+ readonly providerMessageId: string;
137
+ /** Recipient address as the provider reports it (used to derive the HMAC). */
138
+ readonly recipient?: string;
139
+ /** True for hard bounces; soft bounces should not suppress. */
140
+ readonly isHardBounce?: boolean;
141
+ /** Provider's own timestamp for the event, if supplied. */
142
+ readonly occurredAt?: Date;
143
+ /** Raw provider payload, retained for traceability/replay. */
144
+ readonly raw: unknown;
145
+ }
146
+ /** Maps a webhook event type to the lifecycle state it drives the message to. */
147
+ declare function webhookToState(type: WebhookEventType): LifecycleState | null;
148
+
149
+ export { type DispatchableMessage as D, LIFECYCLE_STATES as L, type Message as M, type Sealed as S, TERMINAL_STATES as T, type WebhookEvent as W, type LifecycleState as a, type MessageBody as b, type WebhookEventType as c, canTransition as d, isTerminal as i, shouldSuppressOn as s, webhookToState as w };
@@ -0,0 +1,149 @@
1
+ /**
2
+ * Message lifecycle states and the transition rules between them.
3
+ *
4
+ * This module is PURE: no I/O, no framework, no provider. It encodes the
5
+ * state machine described in the TDR §3.3. Everything outward (use cases,
6
+ * repositories, workers) leans on these rules rather than re-deriving them.
7
+ *
8
+ * ┌─────────────┐
9
+ * enqueue ─────▶│ queued │
10
+ * └──────┬──────┘
11
+ * │ claimed by send worker
12
+ * ▼
13
+ * ┌─────────────┐ suppressed recipient
14
+ * │ sending │────────────────────────▶ suppressed
15
+ * └──────┬──────┘
16
+ * │ provider accepted
17
+ * ▼
18
+ * ┌─────────────┐
19
+ * │ sent │ (accepted, not yet confirmed)
20
+ * └──────┬──────┘
21
+ * │ webhook
22
+ * ┌──────────────┼───────────────┬────────────┐
23
+ * ▼ ▼ ▼ ▼
24
+ * delivered bounced complained failed
25
+ *
26
+ * Transient dispatch error → back to `queued` with backoff (retry).
27
+ * Permanent error / retries exhausted → `failed` (terminal, DLQ).
28
+ */
29
+ declare const LIFECYCLE_STATES: readonly ["queued", "sending", "sent", "delivered", "bounced", "complained", "failed", "suppressed"];
30
+ type LifecycleState = (typeof LIFECYCLE_STATES)[number];
31
+ /**
32
+ * Terminal states never transition again. The retention worker only ever acts
33
+ * on rows in a terminal state (and only after the webhook window has closed),
34
+ * which is what makes it safe to run concurrently with the send worker.
35
+ */
36
+ declare const TERMINAL_STATES: readonly ["delivered", "bounced", "complained", "failed", "suppressed"];
37
+ declare function isTerminal(state: LifecycleState): boolean;
38
+ declare function canTransition(from: LifecycleState, to: LifecycleState): boolean;
39
+ /**
40
+ * Which lifecycle states must feed the suppression list when reached.
41
+ * Hard bounces and complaints damage sender reputation if re-sent, so the
42
+ * recipient is suppressed automatically (TDR §3.6).
43
+ */
44
+ declare function shouldSuppressOn(state: LifecycleState): boolean;
45
+
46
+ /**
47
+ * The Message entity — the heart of the outbox. Pure data + invariants.
48
+ *
49
+ * PII fields (`recipientSealed`, `bodySealed`) hold ciphertext when encryption
50
+ * is enabled, plaintext otherwise. The plaintext recipient is NEVER stored in
51
+ * a queryable column; suppression and idempotency-by-recipient match on
52
+ * `recipientHmac` instead (TDR §5.4).
53
+ */
54
+ interface Message {
55
+ readonly id: string;
56
+ /** Caller-supplied or content-derived. Unique. Dedupes re-enqueues. */
57
+ readonly idempotencyKey: string;
58
+ state: LifecycleState;
59
+ /** Deterministic keyed HMAC of the recipient address. Searchable. */
60
+ readonly recipientHmac: string;
61
+ /** Sealed (encrypted-at-rest) recipient address. */
62
+ recipientSealed: Sealed;
63
+ /** Sealed (encrypted-at-rest) rendered body (html and/or text). */
64
+ bodySealed: Sealed;
65
+ /** Clear, queryable. Subject line (already sanitized). */
66
+ subject: string;
67
+ readonly templateId: string | null;
68
+ readonly templateVersion: number | null;
69
+ /** Provider key selected for dispatch (e.g. "resend", "smtp"). */
70
+ provider: string;
71
+ /** Clear, queryable JSON the caller attaches (e.g. orderId). Never PII. */
72
+ readonly metadata: Record<string, unknown>;
73
+ /** Number of dispatch attempts so far. */
74
+ attempts: number;
75
+ /** Earliest time the send worker may (re-)attempt dispatch. */
76
+ nextAttemptAt: Date;
77
+ /** Optional future-send time (Phase 2 scheduling). */
78
+ scheduledFor: Date | null;
79
+ /** Provider's message id, populated once accepted. */
80
+ providerMessageId: string | null;
81
+ /** Human-readable reason for the latest terminal/error state. */
82
+ lastError: string | null;
83
+ readonly createdAt: Date;
84
+ updatedAt: Date;
85
+ }
86
+ /**
87
+ * An encrypted-at-rest payload. When encryption is disabled the `Encryptor`
88
+ * returns a `Sealed` with `alg: "plain"` so the column shape never changes —
89
+ * encryption can be turned on later without a migration of the row format.
90
+ */
91
+ interface Sealed {
92
+ /** Algorithm tag, e.g. "plain" | "aes-256-gcm" | "hybrid-rsa-aes-256-gcm". */
93
+ readonly alg: string;
94
+ /** Base64 ciphertext (or plaintext when alg === "plain"). */
95
+ readonly ciphertext: string;
96
+ /** Base64 IV/nonce (AEAD schemes). */
97
+ readonly iv?: string;
98
+ /** Base64 auth tag (AEAD schemes). */
99
+ readonly tag?: string;
100
+ /** Base64 wrapped data key (hybrid schemes). */
101
+ readonly wrappedKey?: string;
102
+ /** Identifier of the key/key-version used, for rotation. */
103
+ readonly keyId?: string;
104
+ }
105
+ /** The decrypted body handed to the provider at dispatch time. */
106
+ interface MessageBody {
107
+ html?: string;
108
+ text?: string;
109
+ }
110
+ /**
111
+ * The fully-resolved, decrypted message a provider adapter receives. The send
112
+ * worker constructs this immediately before dispatch and never persists it.
113
+ */
114
+ interface DispatchableMessage {
115
+ readonly id: string;
116
+ readonly to: string;
117
+ readonly subject: string;
118
+ readonly body: MessageBody;
119
+ readonly headers: Record<string, string>;
120
+ readonly metadata: Record<string, unknown>;
121
+ }
122
+
123
+ /**
124
+ * Normalized webhook event (TDR §3.5).
125
+ *
126
+ * Each provider has its own webhook payload shape and signature scheme. The
127
+ * provider adapter's `verifyWebhook` verifies the signature and maps the native
128
+ * payload onto this uniform type, so the `IngestWebhook` use case never has to
129
+ * know about provider specifics.
130
+ */
131
+ type WebhookEventType = "delivered" | "bounced" | "complained" | "opened" | "failed";
132
+ interface WebhookEvent {
133
+ readonly provider: string;
134
+ readonly type: WebhookEventType;
135
+ /** Provider's message id — used to correlate back to the outbox row. */
136
+ readonly providerMessageId: string;
137
+ /** Recipient address as the provider reports it (used to derive the HMAC). */
138
+ readonly recipient?: string;
139
+ /** True for hard bounces; soft bounces should not suppress. */
140
+ readonly isHardBounce?: boolean;
141
+ /** Provider's own timestamp for the event, if supplied. */
142
+ readonly occurredAt?: Date;
143
+ /** Raw provider payload, retained for traceability/replay. */
144
+ readonly raw: unknown;
145
+ }
146
+ /** Maps a webhook event type to the lifecycle state it drives the message to. */
147
+ declare function webhookToState(type: WebhookEventType): LifecycleState | null;
148
+
149
+ export { type DispatchableMessage as D, LIFECYCLE_STATES as L, type Message as M, type Sealed as S, TERMINAL_STATES as T, type WebhookEvent as W, type LifecycleState as a, type MessageBody as b, type WebhookEventType as c, canTransition as d, isTerminal as i, shouldSuppressOn as s, webhookToState as w };