@softure-ai/mailing 0.1.7 → 0.1.9

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 (77) hide show
  1. package/CHANGELOG.md +41 -0
  2. package/README.md +131 -23
  3. package/dist/cli/run.d.ts +6 -2
  4. package/dist/cli/run.d.ts.map +1 -1
  5. package/dist/cli/run.js +297 -40
  6. package/dist/cli/run.js.map +1 -1
  7. package/dist/contract.d.ts +35 -5
  8. package/dist/contract.d.ts.map +1 -1
  9. package/dist/index.d.ts +14 -2
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/index.js +1 -1
  12. package/dist/index.js.map +1 -1
  13. package/dist/next/import-deliveries.d.ts +4 -0
  14. package/dist/next/import-deliveries.d.ts.map +1 -0
  15. package/dist/next/import-deliveries.js +9 -0
  16. package/dist/next/import-deliveries.js.map +1 -0
  17. package/dist/next/index.d.ts +1 -0
  18. package/dist/next/index.d.ts.map +1 -1
  19. package/dist/next/index.js +1 -0
  20. package/dist/next/index.js.map +1 -1
  21. package/dist/next/route.d.ts +3 -2
  22. package/dist/next/route.d.ts.map +1 -1
  23. package/dist/next/route.js +5 -4
  24. package/dist/next/route.js.map +1 -1
  25. package/dist/options.d.ts +19 -2
  26. package/dist/options.d.ts.map +1 -1
  27. package/dist/options.js +30 -6
  28. package/dist/options.js.map +1 -1
  29. package/dist/schema.d.ts +17 -0
  30. package/dist/schema.d.ts.map +1 -1
  31. package/dist/schema.js +2 -0
  32. package/dist/schema.js.map +1 -1
  33. package/dist/server/campaigns.d.ts +5 -0
  34. package/dist/server/campaigns.d.ts.map +1 -1
  35. package/dist/server/campaigns.js +10 -0
  36. package/dist/server/campaigns.js.map +1 -1
  37. package/dist/server/deliveries.d.ts +9 -5
  38. package/dist/server/deliveries.d.ts.map +1 -1
  39. package/dist/server/deliveries.js +9 -7
  40. package/dist/server/deliveries.js.map +1 -1
  41. package/dist/server/dns.d.ts +48 -1
  42. package/dist/server/dns.d.ts.map +1 -1
  43. package/dist/server/dns.js +124 -15
  44. package/dist/server/dns.js.map +1 -1
  45. package/dist/server/import-deliveries.d.ts +68 -0
  46. package/dist/server/import-deliveries.d.ts.map +1 -0
  47. package/dist/server/import-deliveries.js +105 -0
  48. package/dist/server/import-deliveries.js.map +1 -0
  49. package/dist/server/index.d.ts +4 -3
  50. package/dist/server/index.d.ts.map +1 -1
  51. package/dist/server/index.js +4 -3
  52. package/dist/server/index.js.map +1 -1
  53. package/dist/server/suppressions.d.ts.map +1 -1
  54. package/dist/server/suppressions.js +9 -6
  55. package/dist/server/suppressions.js.map +1 -1
  56. package/dist/server/unsubscribe-link.d.ts +3 -2
  57. package/dist/server/unsubscribe-link.d.ts.map +1 -1
  58. package/dist/server/unsubscribe-link.js +12 -3
  59. package/dist/server/unsubscribe-link.js.map +1 -1
  60. package/migrations/0004_allow_imported_deliveries.sql +20 -0
  61. package/module.json +1 -1
  62. package/package.json +1 -1
  63. package/src/cli/run.ts +311 -39
  64. package/src/contract.ts +30 -5
  65. package/src/index.ts +4 -1
  66. package/src/next/import-deliveries.ts +9 -0
  67. package/src/next/index.ts +1 -0
  68. package/src/next/route.ts +5 -4
  69. package/src/options.ts +36 -7
  70. package/src/schema.ts +2 -0
  71. package/src/server/campaigns.ts +13 -0
  72. package/src/server/deliveries.ts +15 -8
  73. package/src/server/dns.ts +174 -16
  74. package/src/server/import-deliveries.ts +149 -0
  75. package/src/server/index.ts +21 -0
  76. package/src/server/suppressions.ts +15 -8
  77. package/src/server/unsubscribe-link.ts +10 -3
package/src/options.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  // The options an app passes to `mailing({ ... })` in softure.config.ts, parsed at startup.
2
2
  import { z } from "zod";
3
3
  import { isMailbox, isSingleAddress } from "./address.js";
4
- import type { CampaignRecipientFilter, LegacyUnsubscribe, MailProvider, OnUnsubscribedHook } from "./contract.js";
4
+ import type { CampaignRecipientFilter, CampaignRecipientSource, LegacyUnsubscribe, MailProvider, OnUnsubscribedHook } from "./contract.js";
5
5
 
6
6
  export const DEFAULT_TIMEOUT_MS = 10_000;
7
7
  /** How long a delivery claim may stay open before another sender may take it over (and send again, same key). */
@@ -11,18 +11,32 @@ export const DEFAULT_STALE_CLAIM_MS = 15 * 60_000;
11
11
  * the provider may have forgotten the idempotency key (Resend keeps one for 24 hours), so a retake could mail twice.
12
12
  */
13
13
  export const DEFAULT_UNCERTAIN_CLAIM_MS = 23 * 60 * 60_000;
14
+ /**
15
+ * Claims a delivery gets before `mailing.unavailable` becomes its final outcome. A short outage across a few runs is
16
+ * ridden out, while a provider that keeps failing on one mail does not keep it open forever.
17
+ */
18
+ export const DEFAULT_MAX_ATTEMPTS = 5;
14
19
 
15
20
  /** Query names the signed link and the page use; a legacy link cannot claim them. */
16
21
  const RESERVED_LINK_PARAMS: ReadonlySet<string> = new Set(["r", "status"]);
17
22
  const LINK_PARAM = /^[A-Za-z][A-Za-z0-9_-]{0,31}$/;
18
23
 
24
+ const MAX_LINK_PARAMS = 8;
25
+ const linkParamNames = z.array(z.string().regex(LINK_PARAM, "must be a query name: a letter, then letters, digits, _ or -, at most 32 characters"));
26
+
27
+ /** Both forms of `params`, normalised to required and optional names, with the limits on all names together. */
28
+ const legacyParamsSchema = z
29
+ .union([linkParamNames.min(1), z.strictObject({ required: linkParamNames.min(1), optional: linkParamNames.default([]) })])
30
+ .transform((params) => (Array.isArray(params) ? { required: params, optional: [] } : params))
31
+ .refine(({ required, optional }) => required.length + optional.length <= MAX_LINK_PARAMS, `must name at most ${String(MAX_LINK_PARAMS)} parameters`)
32
+ .refine(({ required, optional }) => new Set([...required, ...optional]).size === required.length + optional.length, "must not repeat a name")
33
+ .refine(
34
+ ({ required, optional }) => [...required, ...optional].every((param) => !RESERVED_LINK_PARAMS.has(param)),
35
+ "must not use r or status: the signed link and the page own them",
36
+ );
37
+
19
38
  const legacyUnsubscribeSchema = z.strictObject({
20
- params: z
21
- .array(z.string().regex(LINK_PARAM, "must be a query name: a letter, then letters, digits, _ or -, at most 32 characters"))
22
- .min(1)
23
- .max(8)
24
- .refine((params) => new Set(params).size === params.length, "must not repeat a name")
25
- .refine((params) => params.every((param) => !RESERVED_LINK_PARAMS.has(param)), "must not use r or status: the signed link and the page own them"),
39
+ params: legacyParamsSchema,
26
40
  verify: z.custom<LegacyUnsubscribe["verify"]>((value) => typeof value === "function", "must be a function"),
27
41
  });
28
42
 
@@ -53,8 +67,23 @@ export const mailingOptionsSchema = z.strictObject({
53
67
  onUnsubscribed: z.custom<OnUnsubscribedHook>((value) => typeof value === "function", "must be a function").optional(),
54
68
  /** Verifies unsubscribe links the app sent before it adopted the module (see `LegacyUnsubscribe`). */
55
69
  legacyUnsubscribe: legacyUnsubscribeSchema.optional(),
70
+ /**
71
+ * What the one-click route answers for a link that does not verify: 400 (the default), or 200 so the answer never
72
+ * tells whether a token is live. A failure still answers 500, so the mail client retries; the page is unaffected.
73
+ */
74
+ oneClickInvalidLinkStatus: z.union([z.literal(200), z.literal(400)]).default(400),
56
75
  /** Decides per recipient whether a campaign goes to them, e.g. by consent scope (see `CampaignRecipientFilter`). */
57
76
  filterCampaignRecipient: z.custom<CampaignRecipientFilter>((value) => typeof value === "function", "must be a function").optional(),
77
+ /**
78
+ * Lists a campaign's recipients from the app's data; `softure-mail campaign` uses it when no `--recipients` file
79
+ * is given (see `CampaignRecipientSource`).
80
+ */
81
+ listCampaignRecipients: z.custom<CampaignRecipientSource>((value) => typeof value === "function", "must be a function").optional(),
82
+ /**
83
+ * Claims a delivery gets before `mailing.unavailable` closes it as rejected, 1 to 100; `null` never closes it, for
84
+ * apps that retry on their own schedule. Default 5.
85
+ */
86
+ maxAttempts: z.number().int().min(1).max(100).nullable().default(DEFAULT_MAX_ATTEMPTS),
58
87
  /** How long a delivery claim may stay open before another sender takes it over. 1 minute to 23 hours. */
59
88
  staleClaimMs: z.number().int().min(60_000).max(23 * HOUR_MS).default(DEFAULT_STALE_CLAIM_MS),
60
89
  /**
package/src/schema.ts CHANGED
@@ -33,6 +33,8 @@ export const deliveries = mailingSchema.table(
33
33
  reason: text("reason"),
34
34
  /** The provider's HTTP status of the last answer that failed or was released; null when there was none. */
35
35
  providerStatus: integer("provider_status"),
36
+ /** When `importDeliveries` wrote the row; null for a delivery the module made. */
37
+ importedAt: timestamp("imported_at", { withTimezone: true }),
36
38
  createdAt: timestamp("created_at", { withTimezone: true }).notNull(),
37
39
  },
38
40
  (table) => [primaryKey({ columns: [table.scope, table.recipientKey] })],
@@ -135,6 +135,19 @@ function createRecipientFilter(ctx: DeliveryContext, campaign: CampaignContent):
135
135
  };
136
136
  }
137
137
 
138
+ /**
139
+ * The campaign's recipients from the module's `listCampaignRecipients`, or `null` when the app set none. Throws
140
+ * what the app's function throws.
141
+ */
142
+ export async function listConfiguredCampaignRecipients(
143
+ ctx: DeliveryContext,
144
+ campaign: Pick<CampaignContent, "id" | "kind">,
145
+ ): Promise<Iterable<string> | AsyncIterable<string> | null> {
146
+ const source = getMailingOptions(ctx.config).listCampaignRecipients;
147
+ if (source === undefined) return null;
148
+ return source({ id: campaign.id, kind: campaign.kind }, ctx);
149
+ }
150
+
138
151
  export interface CampaignPlan {
139
152
  readonly recipients: number;
140
153
  /** Already closed by an earlier run. */
@@ -10,7 +10,7 @@ import type { Queryable } from "@softure-ai/db";
10
10
  import { and, eq, gte, lt, or, sql, type SQL } from "drizzle-orm";
11
11
  import { isMailKind } from "../address.js";
12
12
  import { HALTING_ERROR_CODES, TRANSACTIONAL_KIND, type MailingErrorCode, type OutgoingMail } from "../contract.js";
13
- import { DEFAULT_STALE_CLAIM_MS, DEFAULT_UNCERTAIN_CLAIM_MS } from "../options.js";
13
+ import { DEFAULT_MAX_ATTEMPTS, DEFAULT_STALE_CLAIM_MS, DEFAULT_UNCERTAIN_CLAIM_MS } from "../options.js";
14
14
  import { deliveries } from "../schema.js";
15
15
  import { getMailingOptions } from "./options.js";
16
16
  import { sendMail } from "./send-mail.js";
@@ -18,9 +18,7 @@ import { getRecipientKey } from "./unsubscribe-link.js";
18
18
 
19
19
  export type DeliveryContext = ModuleContext<Queryable>;
20
20
 
21
- export { DEFAULT_STALE_CLAIM_MS, DEFAULT_UNCERTAIN_CLAIM_MS };
22
- /** Claims a delivery gets before `mailing.unavailable` becomes its final outcome. */
23
- export const DEFAULT_MAX_ATTEMPTS = 5;
21
+ export { DEFAULT_MAX_ATTEMPTS, DEFAULT_STALE_CLAIM_MS, DEFAULT_UNCERTAIN_CLAIM_MS };
24
22
 
25
23
  const MAX_SCOPE_LENGTH = 128;
26
24
  const SCOPE = /^[a-z0-9][a-z0-9._:-]*$/;
@@ -48,7 +46,11 @@ export interface DeliverOptions {
48
46
  * An operator's decision, e.g. `softure-mail campaign --resend-uncertain`. Default false.
49
47
  */
50
48
  readonly retakeUncertain?: boolean;
51
- readonly maxAttempts?: number;
49
+ /**
50
+ * Claims before `mailing.unavailable` closes the delivery as rejected; `null` never closes it. Default: the module's
51
+ * `maxAttempts` (5).
52
+ */
53
+ readonly maxAttempts?: number | null;
52
54
  }
53
55
 
54
56
  /** The codes that stop a run: the sending account, not the mail, is the problem. */
@@ -84,7 +86,7 @@ export type DeliveryOutcome =
84
86
  */
85
87
  export async function deliverOnce(ctx: DeliveryContext, delivery: Delivery, options: DeliverOptions = {}): Promise<DeliveryOutcome> {
86
88
  const windows = resolveClaimWindows(ctx, options);
87
- const { maxAttempts = DEFAULT_MAX_ATTEMPTS } = options;
89
+ const maxAttempts = options.maxAttempts === undefined ? getMailingOptions(ctx.config).maxAttempts : options.maxAttempts;
88
90
  const kind = delivery.mail.kind ?? TRANSACTIONAL_KIND;
89
91
  assertDelivery(delivery, kind);
90
92
 
@@ -105,7 +107,7 @@ export async function deliverOnce(ctx: DeliveryContext, delivery: Delivery, opti
105
107
  await releaseDelivery(ctx, fence, { providerStatus, giveAttemptBack: true });
106
108
  return { status: "halted", reason: result.error, ...httpStatus };
107
109
  }
108
- if (result.error === "mailing.unavailable" && attempt < maxAttempts) {
110
+ if (result.error === "mailing.unavailable" && (maxAttempts === null || attempt < maxAttempts)) {
109
111
  await releaseDelivery(ctx, fence, { providerStatus, giveAttemptBack: false });
110
112
  return { status: "retry-later", ...httpStatus };
111
113
  }
@@ -134,8 +136,13 @@ function resolveClaimWindows(ctx: DeliveryContext, options: DeliverOptions): Cla
134
136
  return { staleClaimMs, uncertainClaimMs, retakeUncertain: options.retakeUncertain ?? false };
135
137
  }
136
138
 
139
+ /** Lowercase letters, digits and `._:-`, at most 128 characters. */
140
+ export function isDeliveryScope(scope: string): boolean {
141
+ return scope.length <= MAX_SCOPE_LENGTH && SCOPE.test(scope);
142
+ }
143
+
137
144
  function assertDelivery(delivery: Delivery, kind: string): void {
138
- if (delivery.scope.length > MAX_SCOPE_LENGTH || !SCOPE.test(delivery.scope)) {
145
+ if (!isDeliveryScope(delivery.scope)) {
139
146
  throw new Error(`@softure-ai/mailing: deliverOnce scope "${delivery.scope}" must be lowercase letters, digits and ._:- (at most 128 characters)`);
140
147
  }
141
148
  if (!isMailKind(kind)) {
package/src/server/dns.ts CHANGED
@@ -1,7 +1,9 @@
1
1
  // The sender domain's DNS, as receivers check it: SPF (RFC 7208), DKIM (RFC 6376) and DMARC
2
- // (RFC 7489). Without all three, list mail lands in spam or is refused outright. The check only
3
- // reads public TXT records; it cannot see whether the provider signs with the published key.
4
- import { resolveTxt as resolveTxtRecords } from "node:dns/promises";
2
+ // (RFC 7489). Without all three, list mail lands in spam or is refused outright. On request it also
3
+ // holds DMARC to a required minimum, checks that the reply-to domain accepts mail (MX) and that the
4
+ // provider's return-path hosts resolve. The check only reads public records; it cannot see whether
5
+ // the provider signs with the published key.
6
+ import { resolveCname as resolveCnameRecords, resolveMx as resolveMxRecords, resolveTxt as resolveTxtRecords } from "node:dns/promises";
5
7
 
6
8
  export type DnsCheckStatus = "pass" | "warn" | "fail";
7
9
 
@@ -13,6 +15,9 @@ export type DnsFinding =
13
15
  | "revoked"
14
16
  | "monitor-only"
15
17
  | "inherited"
18
+ | "weak"
19
+ | "null-mx"
20
+ | "unexpected-target"
16
21
  | "lookup-failed";
17
22
 
18
23
  export interface DnsCheck {
@@ -29,9 +34,41 @@ export interface SenderDnsReport {
29
34
  readonly spf: DnsCheck;
30
35
  readonly dkim: DnsCheck;
31
36
  readonly dmarc: DnsCheck;
37
+ /** The reply-to domain's MX, when `replyTo` was given. */
38
+ readonly replyTo: DnsCheck | null;
39
+ /** One check per `returnPath` host, in the order given. */
40
+ readonly returnPath: readonly DnsCheck[];
32
41
  }
33
42
 
34
43
  export type ResolveTxt = (hostname: string) => Promise<string[][]>;
44
+ export interface MxRecord {
45
+ readonly exchange: string;
46
+ readonly priority: number;
47
+ }
48
+ export type ResolveMx = (hostname: string) => Promise<MxRecord[]>;
49
+ export type ResolveCname = (hostname: string) => Promise<string[]>;
50
+
51
+ export type DmarcPolicy = "quarantine" | "reject";
52
+ export type DmarcAlignment = "r" | "s";
53
+
54
+ /** The least a DMARC record must carry; a weaker one fails as `weak`. Absent tags count as their defaults. */
55
+ export interface DmarcExpectation {
56
+ /** The policy for the checked domain: `p`, or `sp ?? p` of a parent's record. Also requires `pct` 100. */
57
+ readonly policy?: DmarcPolicy;
58
+ /** The policy for subdomains: `sp ?? p`. */
59
+ readonly subdomainPolicy?: DmarcPolicy;
60
+ /** `adkim`; absent means relaxed (`r`). */
61
+ readonly adkim?: DmarcAlignment;
62
+ /** `aspf`; absent means relaxed (`r`). */
63
+ readonly aspf?: DmarcAlignment;
64
+ }
65
+
66
+ /** A host the provider uses for MAIL FROM or bounces. */
67
+ export interface ReturnPathHost {
68
+ readonly host: string;
69
+ /** When set, a CNAME must point at this domain or under it (`rmta.net` accepts `send.forge.rmta.net`). */
70
+ readonly targetDomain?: string;
71
+ }
35
72
 
36
73
  export interface CheckSenderDnsOptions {
37
74
  /** DKIM selectors to look up under `_domainkey`. Default `["resend"]`, the Resend selector. */
@@ -41,22 +78,52 @@ export interface CheckSenderDnsOptions {
41
78
  * return path); DMARC accepts that in relaxed alignment.
42
79
  */
43
80
  readonly spfHosts?: readonly string[];
81
+ /** Turns a DMARC record weaker than this into a `fail` (e.g. `{ policy: "reject", adkim: "s", aspf: "s" }`). */
82
+ readonly expectDmarc?: DmarcExpectation;
83
+ /**
84
+ * Where replies go (an address, `Name <address>` or a domain): its domain must have MX records and at most one
85
+ * SPF record, or replies bounce while the sender checks pass.
86
+ */
87
+ readonly replyTo?: string;
88
+ /** Hosts that must resolve as a CNAME or with MX records; `resendReturnPath(domain)` gives Resend's. */
89
+ readonly returnPath?: readonly ReturnPathHost[];
44
90
  readonly resolveTxt?: ResolveTxt;
91
+ readonly resolveMx?: ResolveMx;
92
+ readonly resolveCname?: ResolveCname;
45
93
  }
46
94
 
47
95
  export const DEFAULT_DKIM_SELECTORS: readonly string[] = ["resend"];
48
96
 
97
+ /** Domain of the CNAME targets Resend publishes for its return-path hosts. */
98
+ export const RESEND_RETURN_PATH_DOMAIN = "rmta.net";
99
+
100
+ /**
101
+ * Resend's return-path hosts: `send.<domain>` (MAIL FROM) and `rsend.<domain>`, CNAMEs to `*.rmta.net`. Domains
102
+ * set up before Resend published `rsend` have MX records on `send` and no `rsend`: check `send` alone there.
103
+ */
104
+ export function resendReturnPath(domain: string): ReturnPathHost[] {
105
+ const name = normalizeHost(domain);
106
+ return [
107
+ { host: `send.${name}`, targetDomain: RESEND_RETURN_PATH_DOMAIN },
108
+ { host: `rsend.${name}`, targetDomain: RESEND_RETURN_PATH_DOMAIN },
109
+ ];
110
+ }
111
+
49
112
  /** Reads the SPF, DKIM and DMARC records of `domain`. Never throws: a failed lookup is a finding. */
50
113
  export async function checkSenderDns(domain: string, options: CheckSenderDnsOptions = {}): Promise<SenderDnsReport> {
51
- const name = domain.trim().toLowerCase().replace(/\.$/, "");
114
+ const name = normalizeHost(domain);
52
115
  const resolveTxt = options.resolveTxt ?? resolveTxtRecords;
53
116
  const lookup = (host: string) => lookupTxt(resolveTxt, host);
54
- const [spf, dkim, dmarc] = await Promise.all([
117
+ const lookupMx = (host: string) => lookupRecords(options.resolveMx ?? resolveMxRecords, host);
118
+ const lookupCname = (host: string) => lookupRecords(options.resolveCname ?? resolveCnameRecords, host);
119
+ const [spf, dkim, dmarc, replyTo, returnPath] = await Promise.all([
55
120
  checkSpf(lookup, options.spfHosts ?? [name]),
56
121
  checkDkim(lookup, (options.dkimSelectors ?? DEFAULT_DKIM_SELECTORS).map((selector) => `${selector}._domainkey.${name}`)),
57
- checkDmarc(lookup, name),
122
+ checkDmarc(lookup, name, options.expectDmarc),
123
+ options.replyTo === undefined ? null : checkReplyPath(lookup, lookupMx, normalizeHost(getSenderDomain(options.replyTo))),
124
+ Promise.all((options.returnPath ?? []).map((path) => checkReturnPath(lookupCname, lookupMx, path))),
58
125
  ]);
59
- return { domain: name, spf, dkim, dmarc };
126
+ return { domain: name, spf, dkim, dmarc, replyTo, returnPath };
60
127
  }
61
128
 
62
129
  /** The domain part of a sender (`Plan <hello@mail.example.com>` gives `mail.example.com`). */
@@ -69,15 +136,21 @@ export function getSenderDomain(from: string): string {
69
136
  return address.slice(address.lastIndexOf("@") + 1).trim().toLowerCase();
70
137
  }
71
138
 
139
+ function normalizeHost(host: string): string {
140
+ return host.trim().toLowerCase().replace(/\.$/, "");
141
+ }
142
+
143
+ type Answer<T> = { readonly kind: "records"; readonly records: T[] } | { readonly kind: "none" } | { readonly kind: "error" };
144
+ type TxtAnswer = Answer<string>;
72
145
  type Lookup = (host: string) => Promise<TxtAnswer>;
73
- type TxtAnswer = { readonly kind: "records"; readonly records: string[] } | { readonly kind: "none" } | { readonly kind: "error" };
146
+ type MxLookup = (host: string) => Promise<Answer<MxRecord>>;
147
+ type CnameLookup = (host: string) => Promise<Answer<string>>;
74
148
 
75
149
  const NO_RECORD_CODES = new Set(["ENOTFOUND", "ENODATA", "NXDOMAIN"]);
76
150
 
77
- async function lookupTxt(resolveTxt: ResolveTxt, host: string): Promise<TxtAnswer> {
151
+ async function lookupRecords<T>(resolve: (host: string) => Promise<T[]>, host: string): Promise<Answer<T>> {
78
152
  try {
79
- // A TXT record may arrive in 255-byte chunks; they form one string.
80
- const records = (await resolveTxt(host)).map((chunks) => chunks.join(""));
153
+ const records = await resolve(host);
81
154
  return records.length === 0 ? { kind: "none" } : { kind: "records", records };
82
155
  } catch (error) {
83
156
  const code = (error as { code?: unknown } | null)?.code;
@@ -85,6 +158,15 @@ async function lookupTxt(resolveTxt: ResolveTxt, host: string): Promise<TxtAnswe
85
158
  }
86
159
  }
87
160
 
161
+ async function lookupTxt(resolveTxt: ResolveTxt, host: string): Promise<TxtAnswer> {
162
+ // A TXT record may arrive in 255-byte chunks; they form one string.
163
+ return lookupRecords(async (name) => (await resolveTxt(name)).map((chunks) => chunks.join("")), host);
164
+ }
165
+
166
+ function isSpfRecord(record: string): boolean {
167
+ return /^v=spf1(\s|$)/i.test(record.trim());
168
+ }
169
+
88
170
  function startsWithTag(record: string, tag: string): boolean {
89
171
  return record.trim().toLowerCase().startsWith(tag);
90
172
  }
@@ -98,7 +180,7 @@ async function checkSpf(lookup: Lookup, hosts: readonly string[]): Promise<DnsCh
98
180
  lookupFailed ??= host;
99
181
  continue;
100
182
  }
101
- const records = answer.kind === "records" ? answer.records.filter((record) => /^v=spf1(\s|$)/i.test(record.trim())) : [];
183
+ const records = answer.kind === "records" ? answer.records.filter(isSpfRecord) : [];
102
184
  if (records.length > 1) return { status: "fail", finding: "multiple", host, record: null };
103
185
  const record = records[0];
104
186
  if (record === undefined) continue;
@@ -129,9 +211,10 @@ async function checkDkim(lookup: Lookup, hosts: readonly string[]): Promise<DnsC
129
211
 
130
212
  /**
131
213
  * `_dmarc.<domain>`, or the nearest parent's record (the organisational domain's policy, with
132
- * `sp=` for subdomains when it is set). `p=none` only reports; it protects nothing.
214
+ * `sp=` for subdomains when it is set). `p=none` only reports; it protects nothing. With an
215
+ * expectation, a record weaker than it fails as `weak`.
133
216
  */
134
- async function checkDmarc(lookup: Lookup, domain: string): Promise<DnsCheck> {
217
+ async function checkDmarc(lookup: Lookup, domain: string, expected: DmarcExpectation | undefined): Promise<DnsCheck> {
135
218
  const labels = domain.split(".");
136
219
  const first = `_dmarc.${domain}`;
137
220
  for (let index = 0; index <= labels.length - 2; index += 1) {
@@ -144,10 +227,85 @@ async function checkDmarc(lookup: Lookup, domain: string): Promise<DnsCheck> {
144
227
  const record = records[0];
145
228
  if (record === undefined) continue;
146
229
  const inherited = host !== first;
147
- const tag = (name: string) => new RegExp(`(?:^|;)\\s*${name}=([^;]*)`, "i").exec(record)?.[1]?.trim().toLowerCase();
148
- const policy = (inherited ? (tag("sp") ?? tag("p")) : tag("p")) ?? "none";
230
+ const tags = readDmarcTags(record);
231
+ const policy = (inherited ? (tags.get("sp") ?? tags.get("p")) : tags.get("p")) ?? "none";
232
+ if (expected !== undefined && !meetsDmarcExpectation(tags, policy, expected)) return { status: "fail", finding: "weak", host, record };
149
233
  if (policy === "none") return { status: "warn", finding: "monitor-only", host, record };
150
234
  return inherited ? { status: "pass", finding: "inherited", host, record } : { status: "pass", finding: "found", host, record };
151
235
  }
152
236
  return { status: "fail", finding: "missing", host: first, record: null };
153
237
  }
238
+
239
+ /** `tag=value` pairs of a DMARC record, names and values lower-cased; the first occurrence wins. */
240
+ function readDmarcTags(record: string): Map<string, string> {
241
+ const tags = new Map<string, string>();
242
+ for (const part of record.split(";")) {
243
+ const separator = part.indexOf("=");
244
+ if (separator === -1) continue;
245
+ const name = part.slice(0, separator).trim().toLowerCase();
246
+ if (!tags.has(name)) tags.set(name, part.slice(separator + 1).trim().toLowerCase());
247
+ }
248
+ return tags;
249
+ }
250
+
251
+ const POLICY_RANK: Readonly<Record<string, number>> = { none: 0, quarantine: 1, reject: 2 };
252
+
253
+ /** An unknown policy value counts as `none`, as receivers treat an invalid `p=`. */
254
+ function rankPolicy(policy: string | undefined): number {
255
+ return (policy === undefined ? undefined : POLICY_RANK[policy]) ?? 0;
256
+ }
257
+
258
+ function meetsAlignment(actual: string | undefined, expected: DmarcAlignment | undefined): boolean {
259
+ return expected !== "s" || actual === "s";
260
+ }
261
+
262
+ function meetsDmarcExpectation(tags: ReadonlyMap<string, string>, policy: string, expected: DmarcExpectation): boolean {
263
+ if (expected.policy !== undefined) {
264
+ if (rankPolicy(policy) < rankPolicy(expected.policy)) return false;
265
+ const percent = tags.get("pct");
266
+ if (percent !== undefined && Number(percent) !== 100) return false;
267
+ }
268
+ if (expected.subdomainPolicy !== undefined && rankPolicy(tags.get("sp") ?? tags.get("p")) < rankPolicy(expected.subdomainPolicy)) return false;
269
+ return meetsAlignment(tags.get("adkim"), expected.adkim) && meetsAlignment(tags.get("aspf"), expected.aspf);
270
+ }
271
+
272
+ function formatMx(records: readonly MxRecord[]): string {
273
+ return [...records]
274
+ .sort((left, right) => left.priority - right.priority)
275
+ .map((record) => `${String(record.priority)} ${normalizeHost(record.exchange) || "."}`)
276
+ .join(", ");
277
+ }
278
+
279
+ /** A null MX (RFC 7505: one record, exchange `.`) says the domain accepts no mail. Node reports it as `""`. */
280
+ function isNullMx(records: readonly MxRecord[]): boolean {
281
+ return records.length === 1 && normalizeHost(records[0]?.exchange ?? "") === "";
282
+ }
283
+
284
+ /** The reply-to domain must accept mail (MX, not a null MX) and publish at most one SPF record. */
285
+ async function checkReplyPath(lookup: Lookup, lookupMx: MxLookup, host: string): Promise<DnsCheck> {
286
+ const mx = await lookupMx(host);
287
+ if (mx.kind === "error") return { status: "fail", finding: "lookup-failed", host, record: null };
288
+ if (mx.kind === "none") return { status: "fail", finding: "missing", host, record: null };
289
+ if (isNullMx(mx.records)) return { status: "fail", finding: "null-mx", host, record: formatMx(mx.records) };
290
+ const txt = await lookup(host);
291
+ if (txt.kind === "error") return { status: "fail", finding: "lookup-failed", host, record: null };
292
+ if (txt.kind === "records" && txt.records.filter(isSpfRecord).length > 1) return { status: "fail", finding: "multiple", host, record: null };
293
+ return { status: "pass", finding: "found", host, record: formatMx(mx.records) };
294
+ }
295
+
296
+ /** A CNAME (to `targetDomain` or under it, when set), else MX records. Strings, not patterns: the domain is input. */
297
+ async function checkReturnPath(lookupCname: CnameLookup, lookupMx: MxLookup, path: ReturnPathHost): Promise<DnsCheck> {
298
+ const host = normalizeHost(path.host);
299
+ const cname = await lookupCname(host);
300
+ if (cname.kind === "error") return { status: "fail", finding: "lookup-failed", host, record: null };
301
+ if (cname.kind === "records") {
302
+ const target = normalizeHost(cname.records[0] ?? "");
303
+ const expected = path.targetDomain === undefined ? null : normalizeHost(path.targetDomain);
304
+ const matches = expected === null || target === expected || target.endsWith(`.${expected}`);
305
+ return matches ? { status: "pass", finding: "found", host, record: target } : { status: "fail", finding: "unexpected-target", host, record: target };
306
+ }
307
+ const mx = await lookupMx(host);
308
+ if (mx.kind === "error") return { status: "fail", finding: "lookup-failed", host, record: null };
309
+ if (mx.kind === "none" || isNullMx(mx.records)) return { status: "fail", finding: "missing", host, record: null };
310
+ return { status: "pass", finding: "found", host, record: formatMx(mx.records) };
311
+ }
@@ -0,0 +1,149 @@
1
+ // Seeds the delivery ledger with deliveries an app made before it adopted the module, so `deliverOnce` and
2
+ // campaigns skip them. Every row is checked before anything is written; a row whose scope and recipient are
3
+ // already in the ledger is left as it is (the ledger wins, and a live claim keeps its fence), so a re-run imports
4
+ // nothing twice.
5
+ import { isMailKind, isSingleAddress } from "../address.js";
6
+ import { TRANSACTIONAL_KIND, type MailingErrorCode } from "../contract.js";
7
+ import { deliveries } from "../schema.js";
8
+ import { isDeliveryScope, type DeliveryContext } from "./deliveries.js";
9
+ import { getRecipientKey } from "./unsubscribe-link.js";
10
+
11
+ /** The reasons a rejected row may carry: the codes `deliverOnce` closes a row with. */
12
+ export type ImportedRejectionReason = Extract<MailingErrorCode, "mailing.invalid_input" | "mailing.rejected" | "mailing.unavailable" | "mailing.suppressed">;
13
+
14
+ const REJECTION_REASONS: ReadonlySet<string> = new Set<ImportedRejectionReason>(["mailing.invalid_input", "mailing.rejected", "mailing.unavailable", "mailing.suppressed"]);
15
+
16
+ /** Rows written per statement. */
17
+ const BATCH_SIZE = 500;
18
+
19
+ export interface ImportedDelivery {
20
+ /** The scope `deliverOnce` will use for this mail: `campaign:<id>` for a campaign, else e.g. `billing.trial-ending:sub_42`. */
21
+ readonly scope: string;
22
+ /** The recipient's address; only its key is stored (any case or spacing gives the same key). */
23
+ readonly address: string;
24
+ readonly status: "sent" | "rejected";
25
+ /** When the delivery was closed; a `Date` or an ISO 8601 string, not in the future. */
26
+ readonly finishedAt: Date | string;
27
+ /** The provider's message id, when the app kept it. Only on `sent`. */
28
+ readonly providerMessageId?: string;
29
+ /** Why a rejected delivery is final. Only on `rejected`; default `mailing.rejected`. */
30
+ readonly reason?: ImportedRejectionReason;
31
+ /** The mail kind. Default `transactional`. */
32
+ readonly kind?: string;
33
+ }
34
+
35
+ export interface ImportProblem {
36
+ /** The row's index in the input (0-based). */
37
+ readonly index: number;
38
+ /** What is wrong, naming the field; never the address. */
39
+ readonly problem: string;
40
+ }
41
+
42
+ export interface ImportSummary {
43
+ /** Rows given. */
44
+ readonly rows: number;
45
+ /** Rows written now. */
46
+ readonly imported: number;
47
+ /** Rows whose scope and recipient the ledger already had (from an earlier import or a send); left as they were. */
48
+ readonly alreadyPresent: number;
49
+ /** Rows with the scope and recipient of an earlier row in the input; the first one counts. */
50
+ readonly duplicates: number;
51
+ }
52
+
53
+ /** The import's answer: a summary, or every problem (and nothing written). */
54
+ export type ImportResult = { readonly ok: true; readonly value: ImportSummary } | { readonly ok: false; readonly problems: readonly ImportProblem[] };
55
+
56
+ /** The checked rows, ready for the ledger, or every problem. */
57
+ export type ImportCheck =
58
+ | { readonly ok: true; readonly value: { readonly ledgerRows: readonly LedgerRow[]; readonly duplicates: number } }
59
+ | { readonly ok: false; readonly problems: readonly ImportProblem[] };
60
+
61
+ type LedgerRow = typeof deliveries.$inferInsert;
62
+
63
+ /**
64
+ * Writes `rows` into `mailing.deliveries` as closed deliveries. Returns the problems, and writes nothing, when any
65
+ * row is invalid. Throws on a database failure; rows written before it stay, and a re-run skips them.
66
+ */
67
+ export async function importDeliveries(ctx: DeliveryContext, rows: readonly ImportedDelivery[]): Promise<ImportResult> {
68
+ const checked = checkImportedDeliveries(ctx, rows);
69
+ if (!checked.ok) return checked;
70
+ const { ledgerRows, duplicates } = checked.value;
71
+
72
+ let imported = 0;
73
+ for (let start = 0; start < ledgerRows.length; start += BATCH_SIZE) {
74
+ const written = await ctx.db
75
+ .insert(deliveries)
76
+ .values(ledgerRows.slice(start, start + BATCH_SIZE))
77
+ .onConflictDoNothing({ target: [deliveries.scope, deliveries.recipientKey] })
78
+ .returning();
79
+ imported += written.length;
80
+ }
81
+ return { ok: true, value: { rows: rows.length, imported, alreadyPresent: ledgerRows.length - imported, duplicates } };
82
+ }
83
+
84
+ /**
85
+ * Checks every row and builds the ledger rows, without touching the database (e.g. for a dry run). The problems
86
+ * cover every invalid row, not only the first.
87
+ */
88
+ export function checkImportedDeliveries(
89
+ ctx: Pick<DeliveryContext, "clock">,
90
+ rows: readonly ImportedDelivery[],
91
+ ): ImportCheck {
92
+ const now = ctx.clock.now();
93
+ const problems: ImportProblem[] = [];
94
+ const ledgerRows: LedgerRow[] = [];
95
+ const seen = new Set<string>();
96
+ let duplicates = 0;
97
+ rows.forEach((row, index) => {
98
+ const rowProblems = findRowProblems(row, now);
99
+ if (rowProblems.length > 0) {
100
+ problems.push(...rowProblems.map((problem) => ({ index, problem })));
101
+ return;
102
+ }
103
+ const recipientKey = getRecipientKey(row.address);
104
+ const key = `${row.scope}\n${recipientKey}`;
105
+ if (seen.has(key)) {
106
+ duplicates += 1;
107
+ return;
108
+ }
109
+ seen.add(key);
110
+ const finishedAt = new Date(row.finishedAt);
111
+ ledgerRows.push({
112
+ scope: row.scope,
113
+ recipientKey,
114
+ kind: row.kind ?? TRANSACTIONAL_KIND,
115
+ // Campaign rows are matched by scope; a campaign id would need the campaign's content, which history lacks.
116
+ campaignId: null,
117
+ status: row.status,
118
+ attempts: 1,
119
+ claimedAt: finishedAt,
120
+ finishedAt,
121
+ providerMessageId: row.status === "sent" ? (row.providerMessageId ?? null) : null,
122
+ reason: row.status === "rejected" ? (row.reason ?? "mailing.rejected") : null,
123
+ providerStatus: null,
124
+ createdAt: now,
125
+ importedAt: now,
126
+ });
127
+ });
128
+ return problems.length > 0 ? { ok: false, problems } : { ok: true, value: { ledgerRows, duplicates } };
129
+ }
130
+
131
+ function findRowProblems(row: ImportedDelivery, now: Date): string[] {
132
+ const problems: string[] = [];
133
+ if (!isDeliveryScope(row.scope)) problems.push("scope must be lowercase letters, digits and ._:- (at most 128 characters)");
134
+ if (!isSingleAddress(row.address.trim())) problems.push("address must be one email address");
135
+ if (row.status !== "sent" && row.status !== "rejected") problems.push('status must be "sent" or "rejected"');
136
+ const finishedAt = new Date(row.finishedAt);
137
+ if (Number.isNaN(finishedAt.getTime())) problems.push("finishedAt must be a date");
138
+ else if (finishedAt.getTime() > now.getTime()) problems.push("finishedAt must not be in the future");
139
+ if (row.providerMessageId !== undefined) {
140
+ if (row.status !== "sent") problems.push("providerMessageId belongs only to a sent row");
141
+ else if (row.providerMessageId.trim() === "" || row.providerMessageId.length > 256) problems.push("providerMessageId must be 1 to 256 characters");
142
+ }
143
+ if (row.reason !== undefined) {
144
+ if (row.status !== "rejected") problems.push("reason belongs only to a rejected row");
145
+ else if (!REJECTION_REASONS.has(row.reason)) problems.push(`reason must be one of ${[...REJECTION_REASONS].join(", ")}`);
146
+ }
147
+ if (row.kind !== undefined && !isMailKind(row.kind)) problems.push("kind must be kebab-case (at most 64 characters)");
148
+ return problems;
149
+ }
@@ -3,6 +3,7 @@
3
3
  export { getCampaignProblems, parseCampaignFile, parseRecipientList, type CampaignContent, type CampaignFile, type CampaignFileResult } from "./campaign-file.js";
4
4
  export {
5
5
  getCampaignContentHash,
6
+ listConfiguredCampaignRecipients,
6
7
  planCampaign,
7
8
  registerCampaign,
8
9
  sendCampaign,
@@ -17,6 +18,7 @@ export {
17
18
  DEFAULT_STALE_CLAIM_MS,
18
19
  DEFAULT_UNCERTAIN_CLAIM_MS,
19
20
  deliverOnce,
21
+ isDeliveryScope,
20
22
  type DeliverOptions,
21
23
  type Delivery,
22
24
  type DeliveryContext,
@@ -27,14 +29,33 @@ export {
27
29
  checkSenderDns,
28
30
  DEFAULT_DKIM_SELECTORS,
29
31
  getSenderDomain,
32
+ RESEND_RETURN_PATH_DOMAIN,
33
+ resendReturnPath,
30
34
  type CheckSenderDnsOptions,
35
+ type DmarcAlignment,
36
+ type DmarcExpectation,
37
+ type DmarcPolicy,
31
38
  type DnsCheck,
32
39
  type DnsCheckStatus,
33
40
  type DnsFinding,
41
+ type MxRecord,
42
+ type ResolveCname,
43
+ type ResolveMx,
34
44
  type ResolveTxt,
45
+ type ReturnPathHost,
35
46
  type SenderDnsReport,
36
47
  } from "./dns.js";
37
48
  export { checkMailingTables } from "./health.js";
49
+ export {
50
+ checkImportedDeliveries,
51
+ importDeliveries,
52
+ type ImportCheck,
53
+ type ImportedDelivery,
54
+ type ImportedRejectionReason,
55
+ type ImportProblem,
56
+ type ImportResult,
57
+ type ImportSummary,
58
+ } from "./import-deliveries.js";
38
59
  export { addHtmlFooter, addTextFooter, getListUnsubscribeHeaders, SIGNATURE_SEPARATOR } from "./list-mail.js";
39
60
  export { getMailingModule, getMailingOptions, getMailingRoutes, type MailingRoutes } from "./options.js";
40
61
  export { sendMail, type MailContext } from "./send-mail.js";