mailery 0.19.1 → 0.20.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.
@@ -1,6 +1,6 @@
1
1
  import { Db } from 'mongodb';
2
- import { C as ContactAdapter, a as Contact, A as AdapterFilter, b as AdapterSort, M as MailProvider, S as SendArgs, c as SendResult, N as NormalizedEvent, F as FlowStep, a6 as FlowTrigger, q as FlowGoal, Y as TemplateKind, T as TemplateDoc, g as FlowDoc, D as DeliveryWindow, R as RunnerContext, e as Mailer, a7 as QueueDriverConfig } from './null-DxTHQSrJ.cjs';
3
- export { w as NullProvider } from './null-DxTHQSrJ.cjs';
2
+ import { C as ContactAdapter, a as Contact, A as AdapterFilter, b as AdapterSort, M as MailProvider, S as SendArgs, c as SendResult, N as NormalizedEvent, F as FlowStep, a8 as FlowTrigger, r as FlowGoal, _ as TemplateKind, T as TemplateDoc, g as FlowDoc, D as DeliveryWindow, R as RunnerContext, e as Mailer, a9 as QueueDriverConfig } from './null-B3N6bTpc.cjs';
3
+ export { y as NullProvider } from './null-B3N6bTpc.cjs';
4
4
  import 'zod';
5
5
  import 'handlebars';
6
6
  import 'ioredis';
@@ -257,7 +257,7 @@ interface TestMailerOptions {
257
257
  /** Call `mailer.startWorkers()` after init. Requires a non-noop `queue`. */
258
258
  startWorkers?: boolean;
259
259
  /** Override Mailer config (excluding required fields the harness fills in). */
260
- config?: Partial<Omit<Parameters<typeof Mailer.init>[0], 'db' | 'adapter' | 'queue' | 'providers' | 'defaultProvider'>>;
260
+ config?: Partial<Omit<Parameters<typeof Mailer.init>[0], 'db' | 'mongo' | 'adapter' | 'queue' | 'providers' | 'defaultProvider'>>;
261
261
  }
262
262
  interface SeedContactOptions {
263
263
  /** Also create a `subscribed` subscription row. Default true. */
package/dist/testing.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Db } from 'mongodb';
2
- import { C as ContactAdapter, a as Contact, A as AdapterFilter, b as AdapterSort, M as MailProvider, S as SendArgs, c as SendResult, N as NormalizedEvent, F as FlowStep, a6 as FlowTrigger, q as FlowGoal, Y as TemplateKind, T as TemplateDoc, g as FlowDoc, D as DeliveryWindow, R as RunnerContext, e as Mailer, a7 as QueueDriverConfig } from './null-DxTHQSrJ.js';
3
- export { w as NullProvider } from './null-DxTHQSrJ.js';
2
+ import { C as ContactAdapter, a as Contact, A as AdapterFilter, b as AdapterSort, M as MailProvider, S as SendArgs, c as SendResult, N as NormalizedEvent, F as FlowStep, a8 as FlowTrigger, r as FlowGoal, _ as TemplateKind, T as TemplateDoc, g as FlowDoc, D as DeliveryWindow, R as RunnerContext, e as Mailer, a9 as QueueDriverConfig } from './null-B3N6bTpc.js';
3
+ export { y as NullProvider } from './null-B3N6bTpc.js';
4
4
  import 'zod';
5
5
  import 'handlebars';
6
6
  import 'ioredis';
@@ -257,7 +257,7 @@ interface TestMailerOptions {
257
257
  /** Call `mailer.startWorkers()` after init. Requires a non-noop `queue`. */
258
258
  startWorkers?: boolean;
259
259
  /** Override Mailer config (excluding required fields the harness fills in). */
260
- config?: Partial<Omit<Parameters<typeof Mailer.init>[0], 'db' | 'adapter' | 'queue' | 'providers' | 'defaultProvider'>>;
260
+ config?: Partial<Omit<Parameters<typeof Mailer.init>[0], 'db' | 'mongo' | 'adapter' | 'queue' | 'providers' | 'defaultProvider'>>;
261
261
  }
262
262
  interface SeedContactOptions {
263
263
  /** Also create a `subscribed` subscription row. Default true. */
package/dist/testing.js CHANGED
@@ -16125,7 +16125,7 @@ function derivePlaintext(html) {
16125
16125
  var PLAIN_TEXT = { noEscape: true };
16126
16126
  async function renderTemplate(template, ctx, opts = {}) {
16127
16127
  const hb = makeHandlebars(opts.helpers);
16128
- const subject = hb.compile(template.subject)(ctx);
16128
+ const subject = hb.compile(template.subject, PLAIN_TEXT)(ctx).replace(/[\r\n]+/g, " ");
16129
16129
  const preheader = hb.compile(template.preheader)(ctx);
16130
16130
  let html;
16131
16131
  if (template.body.html) {
@@ -18464,6 +18464,7 @@ async function processScheduledBroadcasts2(ctx) {
18464
18464
  }
18465
18465
 
18466
18466
  // src/server/mailer.ts
18467
+ var STORAGE_REQUIRED = "Mailer.init: give exactly one of `db` (an open Db) or `mongo` (a connection mailery opens)";
18467
18468
  var Mailer = class _Mailer {
18468
18469
  db;
18469
18470
  collections;
@@ -18473,6 +18474,8 @@ var Mailer = class _Mailer {
18473
18474
  config;
18474
18475
  events;
18475
18476
  queueDriver;
18477
+ /** The client `Mailer.init` opened from `config.mongo`; closed by `stop()`. Null when the host gave `db`. */
18478
+ ownedClient;
18476
18479
  workersStarted = false;
18477
18480
  runnerContext;
18478
18481
  constructor(args) {
@@ -18482,6 +18485,7 @@ var Mailer = class _Mailer {
18482
18485
  this.adapter = args.adapter;
18483
18486
  this.providers = args.providers;
18484
18487
  this.queueDriver = args.queueDriver;
18488
+ this.ownedClient = args.ownedClient;
18485
18489
  this.queues = args.queueDriver.queues;
18486
18490
  this.events = args.events;
18487
18491
  this.runnerContext = {
@@ -18578,36 +18582,58 @@ var Mailer = class _Mailer {
18578
18582
  });
18579
18583
  }
18580
18584
  static async init(input) {
18581
- const config = resolveConfig(input);
18582
- if (!resolveProvider(config.providers, config.defaultProvider)) {
18585
+ if (input.db == null === (input.mongo == null)) throw new Error(STORAGE_REQUIRED);
18586
+ if (!resolveProvider(input.providers, input.defaultProvider)) {
18583
18587
  throw new Error(
18584
- `defaultProvider "${config.defaultProvider}" is not a registered provider. Registered: ${registeredProviderNames(config.providers).join(", ") || "(none)"}`
18588
+ `defaultProvider "${input.defaultProvider}" is not a registered provider. Registered: ${registeredProviderNames(input.providers).join(", ") || "(none)"}`
18585
18589
  );
18586
18590
  }
18587
- if (config.defaultTransactionalProvider != null && !resolveProvider(config.providers, config.defaultTransactionalProvider)) {
18591
+ if (input.defaultTransactionalProvider != null && !resolveProvider(input.providers, input.defaultTransactionalProvider)) {
18588
18592
  throw new Error(
18589
- `defaultTransactionalProvider "${config.defaultTransactionalProvider}" is not a registered provider. Registered: ${registeredProviderNames(config.providers).join(", ") || "(none)"}`
18593
+ `defaultTransactionalProvider "${input.defaultTransactionalProvider}" is not a registered provider. Registered: ${registeredProviderNames(input.providers).join(", ") || "(none)"}`
18590
18594
  );
18591
18595
  }
18592
- if (config.varsAdapter) {
18596
+ if (input.varsAdapter) {
18593
18597
  const { assertNoReservedVarKeys: assertNoReservedVarKeys2 } = await Promise.resolve().then(() => (init_vars(), vars_exports));
18594
- assertNoReservedVarKeys2(config.varsAdapter);
18595
- }
18596
- const collections = getCollections(config.db, config.collectionPrefix);
18597
- await ensureIndexes(config.db, config.collectionPrefix);
18598
- const queueDriver = await createQueueDriver(config.queue, config.db);
18599
- if (!config.workerless && config.queue.driver !== "noop") {
18600
- await queueDriver.scheduleRepeatingTick(config.tickIntervalSeconds);
18601
- }
18602
- return new _Mailer({
18603
- config,
18604
- db: config.db,
18605
- collections,
18606
- adapter: config.adapter,
18607
- providers: config.providers,
18608
- queueDriver,
18609
- events: new EventRegistry()
18610
- });
18598
+ assertNoReservedVarKeys2(input.varsAdapter);
18599
+ }
18600
+ let ownedClient = null;
18601
+ let db;
18602
+ if (input.mongo) {
18603
+ const { MongoClient: MongoClient2 } = await import('mongodb');
18604
+ ownedClient = await MongoClient2.connect(input.mongo.uri, input.mongo.clientOptions);
18605
+ db = ownedClient.db(input.mongo.dbName);
18606
+ } else if (input.db) {
18607
+ db = input.db;
18608
+ } else {
18609
+ throw new Error(STORAGE_REQUIRED);
18610
+ }
18611
+ try {
18612
+ const config = resolveConfig({
18613
+ ...input,
18614
+ db,
18615
+ adapter: typeof input.adapter === "function" ? input.adapter(db) : input.adapter
18616
+ });
18617
+ const collections = getCollections(config.db, config.collectionPrefix);
18618
+ await ensureIndexes(config.db, config.collectionPrefix);
18619
+ const queueDriver = await createQueueDriver(config.queue, config.db);
18620
+ if (!config.workerless && config.queue.driver !== "noop") {
18621
+ await queueDriver.scheduleRepeatingTick(config.tickIntervalSeconds);
18622
+ }
18623
+ return new _Mailer({
18624
+ config,
18625
+ db: config.db,
18626
+ collections,
18627
+ adapter: config.adapter,
18628
+ providers: config.providers,
18629
+ queueDriver,
18630
+ events: new EventRegistry(),
18631
+ ownedClient
18632
+ });
18633
+ } catch (err) {
18634
+ await ownedClient?.close();
18635
+ throw err;
18636
+ }
18611
18637
  }
18612
18638
  // -------------------------------------------------------------------------
18613
18639
  // Event registration + firing
@@ -19080,6 +19106,8 @@ var Mailer = class _Mailer {
19080
19106
  async stop() {
19081
19107
  await this.queueDriver.close();
19082
19108
  this.workersStarted = false;
19109
+ await this.ownedClient?.close();
19110
+ this.ownedClient = null;
19083
19111
  }
19084
19112
  /** Used internally by the admin router and tests; not part of the public API. */
19085
19113
  getRunnerContext() {