notifkit 0.1.15 → 0.1.17

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 (86) hide show
  1. package/dashboard/dist/assets/index-C-DJYu0_.css +1 -0
  2. package/dashboard/dist/assets/index-CzFZcEon.js +318 -0
  3. package/dashboard/dist/index.html +2 -2
  4. package/dist/{bootstrap-BOmT69OH.mjs → bootstrap-P63acBG9.mjs} +1 -1
  5. package/dist/{bulk-ZPiOJRx-.mjs → bulk-Beblt3ny.mjs} +1 -1
  6. package/dist/index.d.mts +84 -27
  7. package/dist/index.mjs +2 -2
  8. package/dist/{main-CTjaZ7ZD.mjs → main-BIahf4f9.mjs} +13 -15
  9. package/dist/{main-CCqcFDDe.mjs → main-C3XcLoCb.mjs} +21 -41
  10. package/dist/{main-g33jpia4.mjs → main-CktEylpL.mjs} +676 -582
  11. package/dist/{main-BdZvBgV7.mjs → main-DB0bUh2r.mjs} +22 -48
  12. package/dist/{main-JEXBLP2A.mjs → main-Dk92nti8.mjs} +8 -6
  13. package/dist/{main-C0m9-Eos.mjs → main-Lcbg7aEn.mjs} +183 -334
  14. package/dist/{main-C6S3EsJ3.mjs → main-NHUvynW2.mjs} +77 -64
  15. package/dist/{main-24J4U8yP.mjs → main-_I4b1cmP.mjs} +4 -4
  16. package/dist/schedule-or-dispatch-C5qfxQCv.mjs +52 -0
  17. package/dist/{src-9LXITcNi.mjs → src-BRatuWJS.mjs} +125 -95
  18. package/package.json +1 -1
  19. package/scripts/create-project.mjs +6 -2
  20. package/src/client.ts +3 -3
  21. package/src/config/index.ts +2 -1
  22. package/src/contracts/common.ts +2 -0
  23. package/src/contracts/envelope.ts +1 -1
  24. package/src/contracts/events/notification-created.ts +2 -0
  25. package/src/contracts/events/notification-delivered.ts +1 -1
  26. package/src/contracts/events/notification-failed.ts +1 -1
  27. package/src/contracts/events/notification-requested.ts +3 -2
  28. package/src/contracts/helpers.ts +1 -2
  29. package/src/contracts/index.ts +1 -0
  30. package/src/contracts/registry.ts +1 -0
  31. package/src/contracts/sdk.ts +8 -37
  32. package/src/db/schema.ts +8 -13
  33. package/src/idempotency/index.ts +6 -8
  34. package/src/metrics/index.ts +2 -0
  35. package/src/queue/index.ts +8 -0
  36. package/src/rate-limiter/api-rate-limiter.ts +208 -0
  37. package/src/rate-limiter/index.ts +3 -411
  38. package/src/rate-limiter/project-settings-cache.ts +99 -0
  39. package/src/rate-limiter/user-throttle.ts +100 -0
  40. package/src/redis/index.ts +10 -3
  41. package/src/repositories/admin-user.ts +83 -0
  42. package/src/repositories/contact.ts +359 -0
  43. package/src/repositories/index.ts +9 -1416
  44. package/src/repositories/preference.ts +35 -0
  45. package/src/repositories/project.ts +110 -0
  46. package/src/repositories/segment.ts +16 -0
  47. package/src/repositories/template.ts +72 -0
  48. package/src/repositories/types.ts +49 -0
  49. package/src/repositories/user.ts +644 -0
  50. package/src/repositories/workflow.ts +64 -0
  51. package/src/services/ai/main.ts +29 -71
  52. package/src/services/api/admin-static.ts +4 -1
  53. package/src/services/api/handlers/auth.ts +109 -0
  54. package/src/services/api/handlers/campaigns.ts +191 -0
  55. package/src/services/api/handlers/events.ts +99 -0
  56. package/src/services/api/handlers/notifications.ts +356 -0
  57. package/src/services/api/handlers/projects.ts +116 -0
  58. package/src/services/api/handlers/shared.ts +262 -0
  59. package/src/services/api/handlers/suppressions.ts +95 -0
  60. package/src/services/api/handlers/system.ts +314 -0
  61. package/src/services/api/handlers/templates.ts +97 -0
  62. package/src/services/api/handlers/unsubscribe.ts +215 -0
  63. package/src/services/api/handlers/users.ts +262 -0
  64. package/src/services/api/handlers/workflows.ts +150 -0
  65. package/src/services/api/handlers.ts +25 -2190
  66. package/src/services/api/health.ts +133 -0
  67. package/src/services/api/http.ts +12 -0
  68. package/src/services/api/main.ts +32 -322
  69. package/src/services/api/router.ts +1 -1
  70. package/src/services/api/webhooks.ts +172 -0
  71. package/src/services/delivery/main.ts +256 -498
  72. package/src/services/engine/main.ts +31 -247
  73. package/src/services/engine/quiet-hours.ts +151 -0
  74. package/src/services/engine/schedule-or-dispatch.ts +104 -0
  75. package/src/services/enricher/main.ts +9 -21
  76. package/src/services/events/main.ts +2 -2
  77. package/src/services/scheduler/main.ts +16 -25
  78. package/src/services/workflow/duration.ts +24 -0
  79. package/src/services/workflow/main.ts +6 -164
  80. package/src/services/workflow/timers.ts +157 -0
  81. package/src/shared/events.ts +3 -2
  82. package/src/templates/render.ts +1 -0
  83. package/src/workers/index.ts +34 -5
  84. package/src/workflows/sdk.ts +1 -0
  85. package/dashboard/dist/assets/index-ggpJKZqZ.js +0 -315
  86. package/dashboard/dist/assets/index-s-SXGIwv.css +0 -1
@@ -1,8 +1,9 @@
1
- import { $t as PUBSUB_CHANNELS, C as ContactRepository, D as TemplateRepository, Dn as normaliseTarget, En as getPriorityBucket, H as ProjectSettingsCache, Jt as buildStreamEvent, On as LRUCache, Sn as DataLoader, T as ProjectRepository, U as UserThrottle, Xt as ENRICHED_STREAMS, Yt as CONSUMER_GROUPS, Z as IdempotencyGuard, _ as TemplateCache, an as registry, c as BaseWorker, ct as scheduledPayloads, d as startHealthReporter, en as STREAMS, f as buildUnsubscribeHeaders, hn as readBaseConfig, kn as globalEmitter, lt as suppressions, pn as loadEnv, q as StreamProducer } from "./src-9LXITcNi.mjs";
2
- import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime, t as createPriorityProducers } from "./bootstrap-BOmT69OH.mjs";
1
+ import { An as globalEmitter, Cn as DataLoader, D as ContactRepository, E as TemplateRepository, G as ProjectSettingsCache, J as StreamProducer, On as normaliseTarget, Q as IdempotencyGuard, T as ProjectRepository, W as UserThrottle, Xt as CONSUMER_GROUPS, Yt as buildStreamEvent, Zt as ENRICHED_STREAMS, _ as TemplateCache, c as BaseWorker, d as startHealthReporter, en as PUBSUB_CHANNELS, f as buildUnsubscribeHeaders, gn as readBaseConfig, kn as LRUCache, mn as loadEnv, on as registry, tn as STREAMS, ut as suppressions } from "./src-BRatuWJS.mjs";
2
+ import { t as scheduleOrDispatch } from "./schedule-or-dispatch-C5qfxQCv.mjs";
3
+ import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime, t as createPriorityProducers } from "./bootstrap-P63acBG9.mjs";
3
4
  import { randomUUID } from "node:crypto";
4
5
  import { and, eq } from "drizzle-orm";
5
- //#region src/services/engine/main.ts
6
+ //#region src/services/engine/quiet-hours.ts
6
7
  function localTimeToUtc(year, month, day, hour, minute, second, timezone) {
7
8
  let utcMs = Date.UTC(year, month - 1, day, hour, minute, second);
8
9
  const formatter = new Intl.DateTimeFormat("en-US", {
@@ -101,6 +102,8 @@ function isInQuietHours(timezone, quietHours, fromDate = /* @__PURE__ */ new Dat
101
102
  nextActiveTime: candidateTime
102
103
  };
103
104
  }
105
+ //#endregion
106
+ //#region src/services/engine/main.ts
104
107
  /** Idempotency stand-in for inline messages; see EngineWorker.process. */
105
108
  const NO_IDEMPOTENCY = {
106
109
  checkAndMark: async (_id, _ttl) => true,
@@ -124,7 +127,6 @@ var EngineWorker = class extends BaseWorker {
124
127
  idempotency;
125
128
  throttle;
126
129
  projectSettings;
127
- redisCli;
128
130
  templateCache;
129
131
  aiPendingProducer;
130
132
  scheduledProducer;
@@ -140,7 +142,6 @@ var EngineWorker = class extends BaseWorker {
140
142
  this.idempotency = options.idempotency;
141
143
  this.throttle = options.throttle;
142
144
  this.projectSettings = options.projectSettings;
143
- this.redisCli = options.redis;
144
145
  this.templateCache = options.templateCache;
145
146
  this.aiPendingProducer = options.aiPendingProducer;
146
147
  this.scheduledProducer = options.scheduledProducer;
@@ -383,10 +384,6 @@ var EngineWorker = class extends BaseWorker {
383
384
  }
384
385
  const taskId = `${enriched.rawEventId}:${contact.id || randomUUID()}`;
385
386
  const resolvedDestination = contact.target;
386
- if (enriched.channel === "email" && templateTopics.length > 0 && !config.UNSUBSCRIBE_SECRET) this.logger.warn({
387
- projectId: enriched.projectId,
388
- recipientId: enriched.recipientId
389
- }, "UNSUBSCRIBE_SECRET is not configured — email sent without RFC 8058 one-click unsubscribe headers");
390
387
  const unsubscribeHeaders = enriched.channel === "email" && templateTopics.length > 0 && config.UNSUBSCRIBE_SECRET && config.PUBLIC_URL && resolvedDestination ? buildUnsubscribeHeaders({
391
388
  claim: {
392
389
  projectId: enriched.projectId,
@@ -445,45 +442,21 @@ var EngineWorker = class extends BaseWorker {
445
442
  }, "task routed to AI worker");
446
443
  continue;
447
444
  }
448
- const envelope = buildStreamEvent("notification.dispatched", taskPayload, "engine", event.metadata.traceId);
449
- const now = Date.now();
450
- if ((enriched.scheduledAt ? new Date(enriched.scheduledAt).getTime() : now) > now) {
451
- await this.db.insert(scheduledPayloads).values({
452
- taskId,
453
- payload: {
454
- ...taskPayload,
455
- scheduledAt: enriched.scheduledAt
456
- }
457
- }).onConflictDoNothing();
458
- const scheduledEnvelope = buildStreamEvent("notification.scheduled", {
459
- projectId: enriched.projectId,
460
- enrichedEventId: event.id,
461
- taskId,
462
- scheduledAt: enriched.scheduledAt
463
- }, "engine", event.metadata.traceId);
464
- await this.scheduledProducer.publish(scheduledEnvelope);
465
- this.logger.debug({
466
- messageId: message.id,
467
- taskId,
468
- scheduledAt: enriched.scheduledAt,
469
- traceId: event.metadata.traceId
470
- }, "task scheduled and payload cached");
471
- } else {
472
- const p = getPriorityBucket(enriched.priority);
473
- const outboundProducer = this.outboundProducers[p] ?? this.outboundProducers["normal"];
474
- if (this.inlineNext) await this.inlineNext({
475
- ...envelope,
476
- id: randomUUID(),
477
- timestamp: (/* @__PURE__ */ new Date()).toISOString()
478
- });
479
- else await outboundProducer.publish(envelope);
480
- this.logger.debug({
481
- messageId: message.id,
482
- taskId,
483
- recipientId: enriched.recipientId,
484
- traceId: event.metadata.traceId
485
- }, "task dispatched");
486
- }
445
+ await scheduleOrDispatch({
446
+ db: this.db,
447
+ scheduledProducer: this.scheduledProducer,
448
+ outboundProducers: this.outboundProducers,
449
+ logger: this.logger,
450
+ inlineNext: this.inlineNext
451
+ }, taskPayload, {
452
+ source: "engine",
453
+ scheduledAt: enriched.scheduledAt,
454
+ messageId: message.id,
455
+ traceId: event.metadata.traceId,
456
+ logLevel: "debug",
457
+ scheduledMessage: "task scheduled and payload cached",
458
+ dispatchedMessage: "task dispatched"
459
+ });
487
460
  }
488
461
  await idempotency.markProcessed(idempotencyKey, customTtl);
489
462
  } catch (err) {
@@ -498,6 +471,7 @@ async function startEngineWorker() {
498
471
  templateRepo = new TemplateRepository(db);
499
472
  templateCache = new TemplateCache(templateRepo);
500
473
  const contactRepo = new ContactRepository(db);
474
+ if (!config.UNSUBSCRIBE_SECRET) logger.warn("UNSUBSCRIBE_SECRET is not configured — emails with topics are sent without RFC 8058 one-click unsubscribe headers");
501
475
  const consumerId = uniqueConsumerId("engine");
502
476
  ({consumer, pendingScanner} = createStreamConsumer({
503
477
  redis,
@@ -1,5 +1,5 @@
1
- import { C as ContactRepository, D as TemplateRepository, En as getPriorityBucket, Jt as buildStreamEvent, O as UserRepository, Yt as CONSUMER_GROUPS, Z as IdempotencyGuard, Zt as INBOUND_STREAMS, _ as TemplateCache, an as registry, c as BaseWorker, d as startHealthReporter, hn as readBaseConfig, kn as globalEmitter, pn as loadEnv, w as PreferenceRepository } from "./src-9LXITcNi.mjs";
2
- import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime, t as createPriorityProducers } from "./bootstrap-BOmT69OH.mjs";
1
+ import { An as globalEmitter, D as ContactRepository, Dn as getPriorityBucket, E as TemplateRepository, Q as IdempotencyGuard, Qt as INBOUND_STREAMS, Xt as CONSUMER_GROUPS, Yt as buildStreamEvent, _ as TemplateCache, c as BaseWorker, d as startHealthReporter, gn as readBaseConfig, k as UserRepository, mn as loadEnv, on as registry } from "./src-BRatuWJS.mjs";
2
+ import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime, t as createPriorityProducers } from "./bootstrap-P63acBG9.mjs";
3
3
  import { randomUUID } from "node:crypto";
4
4
  //#region src/services/enricher/main.ts
5
5
  loadEnv();
@@ -23,7 +23,6 @@ var EnricherWorker = class extends BaseWorker {
23
23
  producers;
24
24
  idempotency;
25
25
  userRepo;
26
- prefRepo;
27
26
  contactRepo;
28
27
  templateCache;
29
28
  /** Engine in this process, when the pipeline is fused. See `setInlineNext`. */
@@ -73,7 +72,6 @@ var EnricherWorker = class extends BaseWorker {
73
72
  this.producers = options.producers;
74
73
  this.idempotency = options.idempotency;
75
74
  this.userRepo = options.userRepo;
76
- this.prefRepo = options.prefRepo;
77
75
  this.contactRepo = options.contactRepo;
78
76
  this.templateCache = options.templateCache;
79
77
  this.flushTimer = setInterval(() => void this.flushWorkerBuffers(), 5);
@@ -176,6 +174,12 @@ var EnricherWorker = class extends BaseWorker {
176
174
  }, "invalid payload — skipping");
177
175
  return;
178
176
  }
177
+ /**
178
+ * Legacy `notification.created` path.
179
+ * @deprecated Nothing in notifkit publishes `notification.created` any more;
180
+ * this branch only serves callers writing straight to the inbound stream.
181
+ * Removed in the next minor version.
182
+ */
179
183
  if (!isRequested) {
180
184
  const raw = createdResult.data;
181
185
  const dedupeId = `${raw.projectId}:${raw.idempotencyKey ?? event.id}`;
@@ -411,7 +415,6 @@ async function startEnricherWorker() {
411
415
  ttlSeconds: config.IDEMPOTENCY_TTL_SECONDS
412
416
  });
413
417
  const userRepo = new UserRepository(db);
414
- const prefRepo = new PreferenceRepository(db);
415
418
  const contactRepo = new ContactRepository(db);
416
419
  const templateCache = new TemplateCache(new TemplateRepository(db));
417
420
  worker = new EnricherWorker({
@@ -423,7 +426,6 @@ async function startEnricherWorker() {
423
426
  producers,
424
427
  idempotency,
425
428
  userRepo,
426
- prefRepo,
427
429
  contactRepo,
428
430
  templateCache
429
431
  });