@softure-ai/mailing 0.1.8 → 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 (72) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/README.md +106 -16
  3. package/dist/cli/run.d.ts +3 -1
  4. package/dist/cli/run.d.ts.map +1 -1
  5. package/dist/cli/run.js +202 -30
  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/import-deliveries.d.ts +68 -0
  42. package/dist/server/import-deliveries.d.ts.map +1 -0
  43. package/dist/server/import-deliveries.js +105 -0
  44. package/dist/server/import-deliveries.js.map +1 -0
  45. package/dist/server/index.d.ts +3 -2
  46. package/dist/server/index.d.ts.map +1 -1
  47. package/dist/server/index.js +3 -2
  48. package/dist/server/index.js.map +1 -1
  49. package/dist/server/suppressions.d.ts.map +1 -1
  50. package/dist/server/suppressions.js +9 -6
  51. package/dist/server/suppressions.js.map +1 -1
  52. package/dist/server/unsubscribe-link.d.ts +3 -2
  53. package/dist/server/unsubscribe-link.d.ts.map +1 -1
  54. package/dist/server/unsubscribe-link.js +12 -3
  55. package/dist/server/unsubscribe-link.js.map +1 -1
  56. package/migrations/0004_allow_imported_deliveries.sql +20 -0
  57. package/module.json +1 -1
  58. package/package.json +1 -1
  59. package/src/cli/run.ts +208 -28
  60. package/src/contract.ts +30 -5
  61. package/src/index.ts +4 -1
  62. package/src/next/import-deliveries.ts +9 -0
  63. package/src/next/index.ts +1 -0
  64. package/src/next/route.ts +5 -4
  65. package/src/options.ts +36 -7
  66. package/src/schema.ts +2 -0
  67. package/src/server/campaigns.ts +13 -0
  68. package/src/server/deliveries.ts +15 -8
  69. package/src/server/import-deliveries.ts +149 -0
  70. package/src/server/index.ts +12 -0
  71. package/src/server/suppressions.ts +15 -8
  72. package/src/server/unsubscribe-link.ts +10 -3
package/src/cli/run.ts CHANGED
@@ -1,6 +1,6 @@
1
- // `softure-mail`: sends a campaign from a content file and checks the sender domain's DNS. The app
2
- // passes its validated config, so the same function serves the bin and an app script that a
3
- // bundler packs for a container:
1
+ // `softure-mail`: sends a campaign from a content file, imports an app's delivery history into the
2
+ // ledger and checks the sender domain's DNS. The app passes its validated config, so the same
3
+ // function serves the bin and an app script that a bundler packs for a container:
4
4
  //
5
5
  // // scripts/mail.ts
6
6
  // import { runMailCli } from "@softure-ai/mailing/cli";
@@ -10,11 +10,13 @@ import { readFile } from "node:fs/promises";
10
10
  import { dirname, resolve } from "node:path";
11
11
  import { parseArgs } from "node:util";
12
12
  import { systemClock, type SoftureConfig, type SoftureDatabaseConfig } from "@softure-ai/core";
13
+ import { z } from "zod";
13
14
  import { openCommandDatabase, type CommandDatabase, type DatabaseHandle } from "@softure-ai/db";
14
15
  import { parseCampaignFile, parseRecipientList, type CampaignContent } from "../server/campaign-file.js";
15
- import { planCampaign, sendCampaign, type CampaignSummary } from "../server/campaigns.js";
16
+ import { listConfiguredCampaignRecipients, planCampaign, sendCampaign, type CampaignSummary } from "../server/campaigns.js";
16
17
  import type { DeliveryContext, HaltingErrorCode } from "../server/deliveries.js";
17
18
  import { checkSenderDns, getSenderDomain, resendReturnPath, type DmarcAlignment, type DmarcExpectation, type DmarcPolicy, type DnsCheck, type ResolveCname, type ResolveMx, type ResolveTxt, type ReturnPathHost } from "../server/dns.js";
19
+ import { checkImportedDeliveries, importDeliveries, type ImportedDelivery, type ImportProblem } from "../server/import-deliveries.js";
18
20
  import { getMailingOptions } from "../server/options.js";
19
21
  import { MIN_UNSUBSCRIBE_SECRET_LENGTH, readUnsubscribeSecrets, UNSUBSCRIBE_SECRET_ENV, type Env } from "../server/unsubscribe-link.js";
20
22
 
@@ -24,7 +26,7 @@ export interface CliOutput {
24
26
  }
25
27
 
26
28
  export interface RunMailCliOptions {
27
- /** The app's config. Needed by `campaign`, and by `dns` without `--domain`. */
29
+ /** The app's config. Needed by `campaign` and `import`, and by `dns` without `--domain`. */
28
30
  readonly config?: SoftureConfig;
29
31
  /** The arguments after the executable, e.g. `["campaign", "launch.md", "--recipients", "list.txt"]`. */
30
32
  readonly argv: readonly string[];
@@ -38,6 +40,8 @@ export interface RunMailCliOptions {
38
40
  readonly resolveCname?: ResolveCname;
39
41
  readonly sleep?: (ms: number) => Promise<void>;
40
42
  readonly env?: Env;
43
+ /** Reads all of standard input, for a file given as `-`. Default: `process.stdin`. */
44
+ readonly readStdin?: () => Promise<string>;
41
45
  }
42
46
 
43
47
  export const EXIT_OK = 0;
@@ -48,20 +52,30 @@ export const EXIT_USAGE = 2;
48
52
  export const DEFAULT_PAUSE_MS = 500;
49
53
 
50
54
  export const MAIL_USAGE = `Usage:
51
- softure-mail campaign <content-file> --recipients <file> [--dry-run] [--pause-ms <ms>] [--resend-uncertain]
55
+ softure-mail campaign <content-file> [--recipients <file>] [--dry-run] [--pause-ms <ms>] [--resend-uncertain]
56
+ softure-mail import <history-file> [--dry-run]
52
57
  softure-mail dns [--domain <domain>] [--dkim-selector <name>]... [--spf-host <host>]...
53
58
  [--dmarc-policy <p>] [--dmarc-sp <p>] [--dmarc-adkim <a>] [--dmarc-aspf <a>]
54
59
  [--reply-to <address>] [--return-path <host>]... [--resend-return-path]
55
60
 
56
61
  campaign Sends the campaign in <content-file> (frontmatter id, kind, subject, optional html,
57
62
  then the text body) to every address in the recipients file (one per line), at most
58
- once each. Re-running sends only to recipients without an outcome yet. A refused
59
- API key or a spent quota stops the run; the rest go out on the next one.
63
+ once each. Without --recipients, the recipients come from listCampaignRecipients in
64
+ the mailing options. Re-running sends only to recipients without an outcome yet. A
65
+ refused API key or a spent quota stops the run; the rest go out on the next one.
66
+ --recipients <file> the recipients, one address per line
60
67
  --dry-run count what would be sent and change nothing
61
68
  --pause-ms <ms> pause after every send (default ${String(DEFAULT_PAUSE_MS)})
62
69
  --resend-uncertain also send to recipients whose send was interrupted long ago
63
70
  (it may have gone out: they may get the mail twice)
64
71
 
72
+ import Writes deliveries the app made before it adopted the module into the ledger, so
73
+ later sends skip them. <history-file> holds one JSON object per line: scope,
74
+ address, status (sent or rejected), finishedAt, and optional providerMessageId,
75
+ reason, kind. Rows already in the ledger are left as they are; any invalid row
76
+ stops the import before anything is written.
77
+ --dry-run check the file and change nothing
78
+
65
79
  dns Checks SPF, DKIM and DMARC for the sender domain (default: the domain of "from").
66
80
  --domain <domain> check this domain instead
67
81
  --dkim-selector <name> DKIM selector to look up (default resend; repeatable)
@@ -77,9 +91,12 @@ dns Checks SPF, DKIM and DMARC for the sender domain (default: the domain
77
91
  --return-path <host> a return-path host that must be a CNAME or have MX (repeatable)
78
92
  --resend-return-path check Resend's send.<domain> and rsend.<domain> CNAMEs
79
93
 
80
- Options for both:
94
+ Options for all:
81
95
  --config <file> the app's softure.config file (bin only)
82
- --help show this help`;
96
+ --help show this help
97
+
98
+ A file given as - is read from standard input (one of them per command), e.g.
99
+ docker compose exec -T app npx softure-mail campaign - < launch.md`;
83
100
 
84
101
  const consoleOutput: CliOutput = {
85
102
  log: (line) => console.log(line),
@@ -91,11 +108,13 @@ type Command =
91
108
  | {
92
109
  readonly kind: "campaign";
93
110
  readonly contentFile: string;
94
- readonly recipientsFile: string;
111
+ /** `null`: the module's `listCampaignRecipients`. */
112
+ readonly recipientsFile: string | null;
95
113
  readonly dryRun: boolean;
96
114
  readonly pauseMs: number;
97
115
  readonly resendUncertain: boolean;
98
116
  }
117
+ | { readonly kind: "import"; readonly historyFile: string; readonly dryRun: boolean }
99
118
  | {
100
119
  readonly kind: "dns";
101
120
  readonly domain: string | undefined;
@@ -109,6 +128,9 @@ type Command =
109
128
 
110
129
  const DMARC_POLICIES: readonly DmarcPolicy[] = ["quarantine", "reject"];
111
130
  const DMARC_ALIGNMENTS: readonly DmarcAlignment[] = ["r", "s"];
131
+ /** A file name that means standard input. */
132
+ const STDIN = "-";
133
+ const CAMPAIGN_ONLY_OPTIONS = ["recipients", "pause-ms", "resend-uncertain"] as const;
112
134
  const DNS_ONLY_OPTIONS = ["domain", "dkim-selector", "spf-host", "dmarc-policy", "dmarc-sp", "dmarc-adkim", "dmarc-aspf", "reply-to", "return-path", "resend-return-path"] as const;
113
135
 
114
136
  /** Runs the command and returns the process exit code: 0 done, 1 failed or incomplete, 2 usage error. */
@@ -128,14 +150,16 @@ export async function runMailCli(options: RunMailCliOptions): Promise<number> {
128
150
  return runDns(command, options, output);
129
151
  case "campaign":
130
152
  return runCampaign(command, options, output);
153
+ case "import":
154
+ return runImport(command, options, output);
131
155
  }
132
156
  }
133
157
 
134
158
  function parseCommand(argv: readonly string[]): Command | string {
135
159
  const [name, ...rest] = argv;
136
- if (name === undefined) return "missing command; use campaign or dns";
160
+ if (name === undefined) return "missing command; use campaign, import or dns";
137
161
  if (name === "--help") return { kind: "help" };
138
- if (name !== "campaign" && name !== "dns") return `unknown command "${name}"; use campaign or dns`;
162
+ if (name !== "campaign" && name !== "import" && name !== "dns") return `unknown command "${name}"; use campaign, import or dns`;
139
163
 
140
164
  let parsed: ReturnType<typeof parseCommandArgs>;
141
165
  try {
@@ -147,7 +171,7 @@ function parseCommand(argv: readonly string[]): Command | string {
147
171
  if (values.help === true) return { kind: "help" };
148
172
 
149
173
  if (name === "dns") {
150
- const misplaced = (["recipients", "dry-run", "pause-ms", "resend-uncertain"] as const).filter((option) => values[option] !== undefined);
174
+ const misplaced = ([...CAMPAIGN_ONLY_OPTIONS, "dry-run"] as const).filter((option) => values[option] !== undefined);
151
175
  if (misplaced.length > 0) return `dns does not take --${misplaced.join(", --")}`;
152
176
  if (positionals.length > 0) return `dns takes no file, got "${positionals.join(" ")}"`;
153
177
  const expectDmarc = parseDmarcExpectation(values);
@@ -164,15 +188,24 @@ function parseCommand(argv: readonly string[]): Command | string {
164
188
  };
165
189
  }
166
190
 
191
+ if (name === "import") {
192
+ const misplaced = [...CAMPAIGN_ONLY_OPTIONS, ...DNS_ONLY_OPTIONS].filter((option) => values[option] !== undefined);
193
+ if (misplaced.length > 0) return `import does not take --${misplaced.join(", --")}`;
194
+ const [historyFile, ...extra] = positionals;
195
+ if (historyFile === undefined) return "import needs a history file (- for standard input)";
196
+ if (extra.length > 0) return `import takes one history file, got also "${extra.join(" ")}"`;
197
+ return { kind: "import", historyFile, dryRun: values["dry-run"] === true };
198
+ }
199
+
167
200
  const misplaced = DNS_ONLY_OPTIONS.filter((option) => values[option] !== undefined);
168
201
  if (misplaced.length > 0) return `campaign does not take --${misplaced.join(", --")}`;
169
202
  const [contentFile, ...extra] = positionals;
170
203
  if (contentFile === undefined) return "campaign needs a content file";
171
204
  if (extra.length > 0) return `campaign takes one content file, got also "${extra.join(" ")}"`;
172
- if (values.recipients === undefined) return "campaign needs --recipients <file>";
205
+ if (contentFile === STDIN && values.recipients === STDIN) return "campaign can read only one of the content file and --recipients from standard input";
173
206
  const pauseMs = values["pause-ms"] === undefined ? DEFAULT_PAUSE_MS : Number(values["pause-ms"]);
174
207
  if (!Number.isInteger(pauseMs) || pauseMs < 0 || pauseMs > 60_000) return `--pause-ms expects whole milliseconds from 0 to 60000, got "${values["pause-ms"] ?? ""}"`;
175
- return { kind: "campaign", contentFile, recipientsFile: values.recipients, dryRun: values["dry-run"] === true, pauseMs, resendUncertain: values["resend-uncertain"] === true };
208
+ return { kind: "campaign", contentFile, recipientsFile: values.recipients ?? null, dryRun: values["dry-run"] === true, pauseMs, resendUncertain: values["resend-uncertain"] === true };
176
209
  }
177
210
 
178
211
  function parseCommandArgs(args: readonly string[]) {
@@ -285,9 +318,19 @@ async function runCampaign(command: Extract<Command, { kind: "campaign" }>, opti
285
318
  output.error("softure-mail campaign: needs the app's config");
286
319
  return EXIT_FAILED;
287
320
  }
288
- const loaded = await loadCampaign(resolve(cwd, command.contentFile), resolve(cwd, command.recipientsFile));
289
- if (typeof loaded === "string") {
290
- output.error(`softure-mail campaign: ${loaded}`);
321
+ const input = createInputReader(cwd, options.readStdin ?? readProcessStdin);
322
+ const content = await loadCampaignContent(command.contentFile, input);
323
+ if (typeof content === "string") {
324
+ output.error(`softure-mail campaign: ${content}`);
325
+ return EXIT_FAILED;
326
+ }
327
+ const listed = command.recipientsFile === null ? null : await loadRecipientFile(command.recipientsFile, input);
328
+ if (typeof listed === "string") {
329
+ output.error(`softure-mail campaign: ${listed}`);
330
+ return EXIT_FAILED;
331
+ }
332
+ if (listed === null && getMailingOptions(config).listCampaignRecipients === undefined) {
333
+ output.error("softure-mail campaign: pass --recipients <file>, or set listCampaignRecipients in the mailing options");
291
334
  return EXIT_FAILED;
292
335
  }
293
336
  if (!command.dryRun && readUnsubscribeSecrets(options.env ?? process.env).current === null) {
@@ -308,6 +351,7 @@ async function runCampaign(command: Extract<Command, { kind: "campaign" }>, opti
308
351
  }
309
352
  const ctx: DeliveryContext = { db: opened.handle.db, clock: systemClock, config };
310
353
  try {
354
+ const loaded: LoadedCampaign = { campaign: content, recipients: listed ?? (await collectRecipients(ctx, content)) };
311
355
  return command.dryRun
312
356
  ? await reportPlan(ctx, loaded, command.resendUncertain, output)
313
357
  : await reportSend(ctx, loaded, { pauseMs: command.pauseMs, sleep: options.sleep, retakeUncertain: command.resendUncertain }, output);
@@ -325,22 +369,152 @@ interface LoadedCampaign {
325
369
  readonly recipients: readonly string[];
326
370
  }
327
371
 
328
- async function loadCampaign(contentPath: string, recipientsPath: string): Promise<LoadedCampaign | string> {
329
- const source = await readText(contentPath);
330
- if (source === null) return `cannot read the content file ${contentPath}`;
331
- const parsed = parseCampaignFile(source);
332
- if (!parsed.ok) return `${contentPath} is not a campaign:\n ${parsed.problems.join("\n ")}`;
372
+ /** Reads a named file, or standard input for `-`; `name` is what messages call it. */
373
+ interface InputReader {
374
+ readonly read: (file: string) => Promise<{ readonly text: string | null; readonly name: string; readonly dir: string }>;
375
+ }
376
+
377
+ function createInputReader(cwd: string, readStdin: () => Promise<string>): InputReader {
378
+ return {
379
+ read: async (file) => {
380
+ if (file === STDIN) return { text: await readStdin(), name: "standard input", dir: cwd };
381
+ const path = resolve(cwd, file);
382
+ return { text: await readText(path), name: path, dir: dirname(path) };
383
+ },
384
+ };
385
+ }
386
+
387
+ async function loadCampaignContent(file: string, input: InputReader): Promise<CampaignContent | string> {
388
+ const source = await input.read(file);
389
+ if (source.text === null) return `cannot read the content file ${source.name}`;
390
+ const parsed = parseCampaignFile(source.text);
391
+ if (!parsed.ok) return `${source.name} is not a campaign:\n ${parsed.problems.join("\n ")}`;
333
392
  const { htmlPath, ...content } = parsed.value;
334
393
  let html: string | null = null;
335
394
  if (htmlPath !== null) {
336
- const htmlFile = resolve(dirname(contentPath), htmlPath);
395
+ // Next to the content file; for standard input, the working directory.
396
+ const htmlFile = resolve(source.dir, htmlPath);
337
397
  html = await readText(htmlFile);
338
398
  if (html === null) return `cannot read the HTML body ${htmlFile}`;
339
399
  if (html.trim() === "") return `the HTML body ${htmlFile} is empty`;
340
400
  }
341
- const list = await readText(recipientsPath);
342
- if (list === null) return `cannot read the recipients file ${recipientsPath}`;
343
- return { campaign: { ...content, html }, recipients: parseRecipientList(list) };
401
+ return { ...content, html };
402
+ }
403
+
404
+ async function loadRecipientFile(file: string, input: InputReader): Promise<readonly string[] | string> {
405
+ const list = await input.read(file);
406
+ if (list.text === null) return `cannot read the recipients file ${list.name}`;
407
+ return parseRecipientList(list.text);
408
+ }
409
+
410
+ /** The recipients from `listCampaignRecipients`, which the caller checked is set. */
411
+ async function collectRecipients(ctx: DeliveryContext, campaign: CampaignContent): Promise<readonly string[]> {
412
+ const recipients: string[] = [];
413
+ const source = await listConfiguredCampaignRecipients(ctx, campaign);
414
+ if (source === null) return recipients;
415
+ for await (const address of source) recipients.push(address);
416
+ return recipients;
417
+ }
418
+
419
+ /** One line of a history file. Unknown keys are refused, so a typo does not drop a field. */
420
+ const historyRowSchema = z.strictObject({
421
+ scope: z.string(),
422
+ address: z.string(),
423
+ status: z.enum(["sent", "rejected"]),
424
+ finishedAt: z.string(),
425
+ providerMessageId: z.string().optional(),
426
+ reason: z.enum(["mailing.invalid_input", "mailing.rejected", "mailing.unavailable", "mailing.suppressed"]).optional(),
427
+ kind: z.string().optional(),
428
+ });
429
+
430
+ async function runImport(command: Extract<Command, { kind: "import" }>, options: RunMailCliOptions, output: CliOutput): Promise<number> {
431
+ const config = options.config;
432
+ if (config === undefined) {
433
+ output.error("softure-mail import: needs the app's config");
434
+ return EXIT_FAILED;
435
+ }
436
+ const source = await createInputReader(options.cwd ?? process.cwd(), options.readStdin ?? readProcessStdin).read(command.historyFile);
437
+ if (source.text === null) {
438
+ output.error(`softure-mail import: cannot read the history file ${source.name}`);
439
+ return EXIT_FAILED;
440
+ }
441
+ const parsed = parseHistory(source.text);
442
+ if (parsed.problems.length > 0) return reportImportProblems(parsed.problems, output);
443
+ const rows = parsed.rows.map((row) => row.delivery);
444
+ const lineOf = (problem: ImportProblem) => parsed.rows[problem.index]?.line ?? 0;
445
+
446
+ // Checked before the database is opened, so a bad file costs no connection.
447
+ const checked = checkImportedDeliveries({ clock: systemClock }, rows);
448
+ if (!checked.ok) return reportImportProblems(checked.problems.map((problem) => ({ line: lineOf(problem), problem: problem.problem })), output);
449
+ if (command.dryRun) {
450
+ output.log(`import, dry run: nothing written; rows ${String(rows.length)}, duplicates ${String(checked.value.duplicates)}`);
451
+ return EXIT_OK;
452
+ }
453
+ if (config.database === null) {
454
+ output.error("softure-mail import: the config has no database; set database.url in softure.config");
455
+ return EXIT_FAILED;
456
+ }
457
+ let opened: CommandDatabase;
458
+ try {
459
+ opened = await openDatabase(config.database, options.openDatabase);
460
+ } catch (error) {
461
+ output.error(`softure-mail import: ${describeError(error)}`);
462
+ return EXIT_FAILED;
463
+ }
464
+ try {
465
+ const result = await importDeliveries({ db: opened.handle.db, clock: systemClock, config }, rows);
466
+ if (!result.ok) return reportImportProblems(result.problems.map((problem) => ({ line: lineOf(problem), problem: problem.problem })), output);
467
+ const summary = result.value;
468
+ output.log(`import: rows ${String(summary.rows)}, imported ${String(summary.imported)}, already present ${String(summary.alreadyPresent)}, duplicates ${String(summary.duplicates)}`);
469
+ return EXIT_OK;
470
+ } catch (error) {
471
+ output.error(`softure-mail import: ${describeError(error)} (did softure migrate run?)`);
472
+ return EXIT_FAILED;
473
+ } finally {
474
+ await opened.close();
475
+ }
476
+ }
477
+
478
+ interface LineProblem {
479
+ readonly line: number;
480
+ readonly problem: string;
481
+ }
482
+
483
+ /** The history file's rows with their line numbers; problems name lines and fields, never values. */
484
+ function parseHistory(text: string): { readonly rows: readonly { readonly line: number; readonly delivery: ImportedDelivery }[]; readonly problems: readonly LineProblem[] } {
485
+ const rows: { line: number; delivery: ImportedDelivery }[] = [];
486
+ const problems: LineProblem[] = [];
487
+ text.split(/\r?\n/).forEach((source, index) => {
488
+ const line = index + 1;
489
+ if (source.trim() === "") return;
490
+ let value: unknown;
491
+ try {
492
+ value = JSON.parse(source);
493
+ } catch {
494
+ problems.push({ line, problem: "not a JSON object" });
495
+ return;
496
+ }
497
+ const parsed = historyRowSchema.safeParse(value);
498
+ if (!parsed.success) {
499
+ for (const issue of parsed.error.issues) {
500
+ const field = issue.path.length === 0 ? "row" : issue.path.join(".");
501
+ problems.push({ line, problem: issue.code === "unrecognized_keys" ? `unknown field(s) ${issue.keys.join(", ")}` : `${field}: ${issue.message}` });
502
+ }
503
+ return;
504
+ }
505
+ const { providerMessageId, reason, kind, ...required } = parsed.data;
506
+ rows.push({
507
+ line,
508
+ delivery: { ...required, ...(providerMessageId === undefined ? {} : { providerMessageId }), ...(reason === undefined ? {} : { reason }), ...(kind === undefined ? {} : { kind }) },
509
+ });
510
+ });
511
+ return { rows, problems };
512
+ }
513
+
514
+ function reportImportProblems(problems: readonly LineProblem[], output: CliOutput): number {
515
+ output.error(`softure-mail import: nothing written, ${String(problems.length)} problem(s):`);
516
+ for (const { line, problem } of problems) output.error(` line ${String(line)}: ${problem}`);
517
+ return EXIT_FAILED;
344
518
  }
345
519
 
346
520
  async function reportPlan(ctx: DeliveryContext, loaded: LoadedCampaign, retakeUncertain: boolean, output: CliOutput): Promise<number> {
@@ -433,6 +607,12 @@ async function openDatabase(database: SoftureDatabaseConfig, open: ((url: string
433
607
  return { handle, close: handle.close };
434
608
  }
435
609
 
610
+ async function readProcessStdin(): Promise<string> {
611
+ const chunks: Buffer[] = [];
612
+ for await (const chunk of process.stdin) chunks.push(typeof chunk === "string" ? Buffer.from(chunk) : (chunk as Buffer));
613
+ return Buffer.concat(chunks).toString("utf8");
614
+ }
615
+
436
616
  async function readText(path: string): Promise<string | null> {
437
617
  try {
438
618
  return await readFile(path, "utf8");
package/src/contract.ts CHANGED
@@ -32,11 +32,18 @@ export const TRANSACTIONAL_KIND = "transactional";
32
32
  /** How an opt-out arrived: a mail client's one-click POST, the page's button, or an operator. */
33
33
  export type SuppressionSource = "one-click" | "page" | "operator";
34
34
 
35
- /** What `onUnsubscribed` receives: who opted out (never the address) and how. */
35
+ /**
36
+ * The link an unsubscribe came through, once verified: the module's signed link, or a legacy link with the values
37
+ * the app's `verify` accepted (only the names the link carried), so a hook can find the row that link named.
38
+ */
39
+ export type VerifiedUnsubscribeLink = { readonly scheme: "signed" } | { readonly scheme: "legacy"; readonly values: Readonly<Record<string, string>> };
40
+
41
+ /** What `onUnsubscribed` receives: who opted out (never the address), how, and through which link. */
36
42
  export interface UnsubscribeEvent {
37
43
  /** The recipient key of the link: privacy's email key of the same address (`getEmailKey`). */
38
44
  readonly recipientKey: string;
39
45
  readonly source: Exclude<SuppressionSource, "operator">;
46
+ readonly link: VerifiedUnsubscribeLink;
40
47
  }
41
48
 
42
49
  /**
@@ -61,14 +68,32 @@ export interface CampaignRecipient {
61
68
  */
62
69
  export type CampaignRecipientFilter = (recipient: CampaignRecipient, ctx: ModuleContext<Queryable>) => Promise<boolean>;
63
70
 
71
+ /**
72
+ * Lists the addresses a campaign goes to, from the app's own data (e.g. the people who opted in to its kind), so a
73
+ * campaign can run where the database is without a recipients file. Duplicates are fine; `filterCampaignRecipient`
74
+ * and the suppression list still apply. A throw stops the campaign. Set in `mailing({ listCampaignRecipients })`.
75
+ */
76
+ export type CampaignRecipientSource = (
77
+ campaign: { readonly id: string; readonly kind: string },
78
+ ctx: ModuleContext<Queryable>,
79
+ ) => Iterable<string> | AsyncIterable<string> | Promise<Iterable<string>>;
80
+
81
+ /**
82
+ * The query names of a legacy link. An array lists names the link must all carry. The object form adds names it
83
+ * may carry: a link is legacy when it has every `required` name, and the `optional` ones it has go to `verify` too.
84
+ * At most 8 names in all, none repeated, never `r` or `status`.
85
+ */
86
+ export type LegacyUnsubscribeParams = readonly string[] | { readonly required: readonly string[]; readonly optional?: readonly string[] };
87
+
64
88
  /**
65
89
  * Unsubscribe links an app sent before it adopted the module, in its own scheme. `params` are the query names of
66
- * such a link; `verify` receives their values (all present once, each at most 512 characters) and returns the
67
- * recipient's address when the link is genuine, else `null`. It must check the link's signature itself: whatever
68
- * address it returns is unsubscribed. A throw reads as a failure (the person may try again), not as a bad link.
90
+ * such a link; `verify` receives the values the link carried (every required name, the optional names that are
91
+ * present and non-empty, each at most 512 characters) and returns the recipient's address when the link is genuine,
92
+ * else `null`. It must check the link's signature itself: whatever address it returns is unsubscribed. A throw
93
+ * reads as a failure (the person may try again), not as a bad link.
69
94
  */
70
95
  export interface LegacyUnsubscribe {
71
- readonly params: readonly string[];
96
+ readonly params: LegacyUnsubscribeParams;
72
97
  readonly verify: (values: Readonly<Record<string, string>>, ctx: ModuleContext<Queryable>) => Promise<string | null>;
73
98
  }
74
99
 
package/src/index.ts CHANGED
@@ -17,7 +17,7 @@ export const MODULE_ID = "mailing";
17
17
  export const mailing = defineModule({
18
18
  manifest: {
19
19
  id: MODULE_ID,
20
- version: "0.1.8",
20
+ version: "0.1.9",
21
21
  dependsOn: {},
22
22
  dbSchema: "mailing",
23
23
  tables: ["suppressions", "campaigns", "deliveries"],
@@ -68,7 +68,9 @@ export { HALTING_ERROR_CODES, TRANSACTIONAL_KIND } from "./contract.js";
68
68
  export type {
69
69
  CampaignRecipient,
70
70
  CampaignRecipientFilter,
71
+ CampaignRecipientSource,
71
72
  LegacyUnsubscribe,
73
+ LegacyUnsubscribeParams,
72
74
  MailingErrorCode,
73
75
  MailProvider,
74
76
  OnUnsubscribedHook,
@@ -83,6 +85,7 @@ export type {
83
85
  SuppressionSource,
84
86
  UnsubscribeErrorCode,
85
87
  UnsubscribeEvent,
88
+ VerifiedUnsubscribeLink,
86
89
  } from "./contract.js";
87
90
  export { getMailingErrorMessage, mailingMessages, type MailingMessages } from "./messages/index.js";
88
91
  export { DEFAULT_STALE_CLAIM_MS, DEFAULT_TIMEOUT_MS, DEFAULT_UNCERTAIN_CLAIM_MS, type MailingOptions, type MailingOptionsInput } from "./options.js";
@@ -0,0 +1,9 @@
1
+ // `importDeliveries` on the app's registered configuration and the shared database, e.g. for a one-off route or
2
+ // script that seeds the ledger from the app's own history.
3
+ import { importDeliveries as importDeliveriesWithContext, type ImportedDelivery, type ImportResult } from "../server/import-deliveries.js";
4
+ import { getMailingContext } from "./context.js";
5
+
6
+ /** Seeds the delivery ledger; see `importDeliveries` in `@softure-ai/mailing/server`. */
7
+ export async function importDeliveries(rows: readonly ImportedDelivery[]): Promise<ImportResult> {
8
+ return importDeliveriesWithContext(await getMailingContext(), rows);
9
+ }
package/src/next/index.ts CHANGED
@@ -3,6 +3,7 @@
3
3
  // (docs/02-module-standard.md §8).
4
4
  export { unsubscribeAction } from "./actions.js";
5
5
  export { deliverOnce } from "./deliver-once.js";
6
+ export { importDeliveries } from "./import-deliveries.js";
6
7
  export { getMailingMessages } from "./messages.js";
7
8
  export { UnsubscribePage, type UnsubscribePageProps } from "./pages.js";
8
9
  export { UNSUBSCRIBE_STATUS_PARAM, type UnsubscribeStatus } from "./params.js";
package/src/next/route.ts CHANGED
@@ -10,7 +10,7 @@
10
10
  // the only write is an idempotent insert.
11
11
  import { errorLogLabel } from "@softure-ai/core";
12
12
  import { getSoftureConfig } from "@softure-ai/core/next";
13
- import { getMailingRoutes } from "../server/options.js";
13
+ import { getMailingOptions, getMailingRoutes } from "../server/options.js";
14
14
  import { unsubscribe } from "../server/suppressions.js";
15
15
  import { readUnsubscribeLink } from "../server/unsubscribe-link.js";
16
16
  import { getMailingContext } from "./context.js";
@@ -20,8 +20,9 @@ const NO_STORE = { "cache-control": "no-store" };
20
20
  /**
21
21
  * Records the opt-out of the link in the URL. The body (`List-Unsubscribe=One-Click`) is not read:
22
22
  * anyone can send it, the signature is the proof. 200 once recorded (also again), 400 for a link
23
- * that does not verify, 500 when the database fails: the client may retry, and 200 would claim an
24
- * opt-out that did not happen. A legacy link (`mailing({ legacyUnsubscribe })`) is handled the same
23
+ * that does not verify (200 with `mailing({ oneClickInvalidLinkStatus: 200 })`, so the answer never
24
+ * tells whether a token is live), 500 when the database fails: the client may retry, and 200 would
25
+ * claim an opt-out that did not happen. A legacy link (`mailing({ legacyUnsubscribe })`) is handled the same
25
26
  * way; mount this route at the old link's path too.
26
27
  */
27
28
  export async function postUnsubscribeRoute(request: Request): Promise<Response> {
@@ -29,7 +30,7 @@ export async function postUnsubscribeRoute(request: Request): Promise<Response>
29
30
  const ctx = await getMailingContext();
30
31
  const link = readUnsubscribeLink(new URL(request.url).searchParams, ctx.config);
31
32
  const result = await unsubscribe(ctx, link, "one-click");
32
- return new Response(null, { status: result.ok ? 200 : 400, headers: NO_STORE });
33
+ return new Response(null, { status: result.ok ? 200 : getMailingOptions(ctx.config).oneClickInvalidLinkStatus, headers: NO_STORE });
33
34
  } catch (error) {
34
35
  console.error(`@softure-ai/mailing: one-click unsubscribe failed: ${errorLogLabel(error)}`);
35
36
  return new Response(null, { status: 500, headers: NO_STORE });
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. */