notifkit 0.1.16 → 0.1.18

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 (59) hide show
  1. package/README.md +5 -1
  2. package/dist/{bootstrap-BMDRPjgp.mjs → bootstrap-Cs1XIp6b.mjs} +1 -1
  3. package/dist/{bulk-CJ5QXiPa.mjs → bulk-4pRIiG2o.mjs} +1 -1
  4. package/dist/index.d.mts +37 -106
  5. package/dist/index.mjs +2 -2
  6. package/dist/{main-8nP3qmCT.mjs → main-BN7ZfvxN.mjs} +5 -14
  7. package/dist/{main-6_bXQBDH.mjs → main-Bl0S4GDd.mjs} +3 -3
  8. package/dist/{main-mGIT8jv4.mjs → main-C5DiM7_r.mjs} +3 -3
  9. package/dist/{main-B82bsFoy.mjs → main-DVzp0DLn.mjs} +8 -58
  10. package/dist/{main-ZW5RaiOK.mjs → main-O9iZ6iz2.mjs} +4 -9
  11. package/dist/{main-CpaHF1Zw.mjs → main-Ppr7Pq1g.mjs} +35 -16
  12. package/dist/{main-DULD8-th.mjs → main-yL5VinQ5.mjs} +5 -12
  13. package/dist/{main-g1KEcXXp.mjs → main-zgxAlH3l.mjs} +23 -94
  14. package/dist/{schedule-or-dispatch-Cd7GLfc3.mjs → schedule-or-dispatch-BwCZWS_P.mjs} +1 -1
  15. package/dist/{src-BE_EUpZ6.mjs → src-C38w4jR4.mjs} +88 -170
  16. package/drizzle/0009_retention_time_indexes.sql +4 -0
  17. package/drizzle/meta/_journal.json +7 -0
  18. package/package.json +1 -1
  19. package/src/client.ts +6 -6
  20. package/src/config/index.ts +9 -11
  21. package/src/contracts/sdk.ts +9 -9
  22. package/src/db/bulk.ts +3 -10
  23. package/src/db/schema.ts +3 -0
  24. package/src/idempotency/index.ts +12 -53
  25. package/src/queue/index.ts +9 -12
  26. package/src/rate-limiter/api-rate-limiter.ts +8 -27
  27. package/src/rate-limiter/project-settings-cache.ts +4 -16
  28. package/src/redis/index.ts +3 -51
  29. package/src/repositories/contact.ts +0 -2
  30. package/src/server.ts +1 -4
  31. package/src/services/api/handlers/auth.ts +3 -11
  32. package/src/services/api/handlers/campaigns.ts +4 -15
  33. package/src/services/api/handlers/events.ts +1 -2
  34. package/src/services/api/handlers/notifications.ts +6 -16
  35. package/src/services/api/handlers/projects.ts +1 -3
  36. package/src/services/api/handlers/shared.ts +7 -29
  37. package/src/services/api/handlers/system.ts +1 -1
  38. package/src/services/api/handlers/unsubscribe.ts +7 -30
  39. package/src/services/api/handlers/users.ts +1 -2
  40. package/src/services/api/http.ts +5 -10
  41. package/src/services/api/main.ts +27 -63
  42. package/src/services/api/webhooks.ts +8 -19
  43. package/src/services/auth/index.ts +2 -21
  44. package/src/services/delivery/main.ts +25 -89
  45. package/src/services/engine/main.ts +33 -69
  46. package/src/services/engine/schedule-or-dispatch.ts +7 -4
  47. package/src/services/enricher/main.ts +19 -49
  48. package/src/services/events/main.ts +3 -12
  49. package/src/services/scheduler/main.ts +26 -32
  50. package/src/services/scheduler/retention.ts +47 -0
  51. package/src/services/scheduler/send-gate.ts +6 -17
  52. package/src/services/workflow/duration.ts +1 -6
  53. package/src/services/workflow/main.ts +3 -9
  54. package/src/services/workflow/timers.ts +2 -1
  55. package/src/shared/batch-processor.ts +3 -7
  56. package/src/shared/utils.ts +2 -8
  57. package/src/transport/index.ts +0 -4
  58. package/src/unsubscribe/index.ts +3 -11
  59. package/src/workers/index.ts +30 -23
@@ -4,8 +4,6 @@ import { globalEmitter, AsyncSemaphore } from "@/shared/index.js";
4
4
  import { metrics } from "@/metrics/index.js";
5
5
  export * from "./health.js";
6
6
 
7
- // ─── Types ─────────────────────────────────────────────────────────────────
8
-
9
7
  export class NonRetryableError extends Error {
10
8
  readonly nonRetryable = true;
11
9
  constructor(message: string) {
@@ -44,8 +42,6 @@ export interface WorkerOptions {
44
42
  maxRetriesBeforeDlq?: number;
45
43
  }
46
44
 
47
- // ─── BaseWorker ────────────────────────────────────────────────────────────
48
-
49
45
  export abstract class BaseWorker {
50
46
  protected readonly logger: Logger;
51
47
 
@@ -193,21 +189,13 @@ export abstract class BaseWorker {
193
189
  }
194
190
 
195
191
  /**
196
- * Processes an event produced by an upstream stage in this same process,
197
- * skipping the stream hop in between.
198
- *
199
- * The caller is still holding its own stream entry, and does not ack it until
200
- * this returns. A thrown error therefore leaves that entry pending and the
201
- * whole chain is replayed from the upstream stage; the idempotency keys each
202
- * stage already checks make the replay safe. Non-retryable failures go to the
203
- * dead-letter stream here, as they would have from this worker's own stream,
204
- * so they do not send the upstream message there instead.
192
+ * Processes an event from an upstream stage in this process, skipping the stream.
193
+ * A throw leaves the caller's entry pending, so the chain replays from upstream.
194
+ * Non-retryable failures are dead-lettered here so the upstream message isn't.
205
195
  */
206
196
  async processInline(event: StreamEvent): Promise<void> {
207
197
  try {
208
- // `{ ack: false }` means another holder owns this message right now. Its
209
- // own stream entry is what gets retried if it fails, so there is nothing
210
- // for the caller to wait on.
198
+ // `{ ack: false }`: another holder owns it and its own entry gets retried.
211
199
  await this.process({ id: event.id, event, deliveryCount: 1, inline: true }, 1);
212
200
  this.processedCount += 1;
213
201
  this.lastProcessedAt = new Date().toISOString();
@@ -258,7 +246,7 @@ export abstract class BaseWorker {
258
246
  return;
259
247
  }
260
248
 
261
- const result = await this.process(message, retryCount);
249
+ const result = await this.processHoldingLease(message, retryCount);
262
250
  if (result && (result as any).ack === false) {
263
251
  this.logger.debug(
264
252
  { messageId: message.id, eventType: message.event.type },
@@ -312,12 +300,31 @@ export abstract class BaseWorker {
312
300
  }
313
301
 
314
302
  /**
315
- * `recoveryIntervalMs` is how long an entry must sit idle before it is
316
- * claimed, not how often to look. Scanning only once per interval, timed from
317
- * this worker's start, meant a worker restarted more often than that (a crash
318
- * loop, an eager autoscaler) never scanned at all, and whatever its
319
- * predecessors held stayed pending for good. So scan on start and then
320
- * several times per interval; the idle threshold alone decides what is taken.
303
+ * Keeps the entry's idle time under the reclaim threshold while `process` runs,
304
+ * so a long fan-out isn't reclaimed mid-run. If this process dies, the touches stop.
305
+ */
306
+ private async processHoldingLease(message: StreamMessage, attempt: number) {
307
+ const stream = message.stream;
308
+ const timer = stream
309
+ ? setInterval(
310
+ () => {
311
+ this.pendingScanner.touch(message.id, stream).catch((err: unknown) => {
312
+ this.logger.warn({ err, messageId: message.id }, "failed to extend message lease");
313
+ });
314
+ },
315
+ Math.max(Math.floor(this.recoveryIntervalMs / 2), 10),
316
+ )
317
+ : undefined;
318
+ try {
319
+ return await this.process(message, attempt);
320
+ } finally {
321
+ clearInterval(timer);
322
+ }
323
+ }
324
+
325
+ /**
326
+ * `recoveryIntervalMs` is the idle time before a claim, not the scan period.
327
+ * Scan on start and several times per interval so crash-looping workers still recover.
321
328
  */
322
329
  private startRecoveryLoop(): void {
323
330
  const scan = () => {