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
@@ -66,14 +66,8 @@ function getPriorityBucket(priority) {
66
66
  return p === "critical" ? "critical" : p === "low" ? "low" : "normal";
67
67
  }
68
68
  /**
69
- * Canonical form of a destination, for suppression lookups.
70
- *
71
- * Both the writer (the provider webhook) and the reader (the engine's
72
- * pre-dispatch gate) must agree on this, or an unsubscribe recorded as
73
- * `Bob@Example.com` will not match a send addressed to `bob@example.com` and
74
- * the person keeps receiving mail. Case folding is safe for email domains and
75
- * for the local part in every mailbox provider in practice; phone numbers and
76
- * push tokens are case-sensitive and are only trimmed.
69
+ * Canonical address for suppression lookups; writers and readers must both use
70
+ * it. Emails are lower-cased; phone numbers and push tokens are case-sensitive.
77
71
  */
78
72
  function normaliseTarget(target) {
79
73
  const trimmed = target.trim();
@@ -108,13 +102,9 @@ var AsyncSemaphore = class {
108
102
  //#endregion
109
103
  //#region src/shared/batch-processor.ts
110
104
  /**
111
- * Coalesces individual `add()` calls into batched `flushFn` calls.
112
- *
113
- * A batch goes out when `maxSize` items are waiting or `maxWaitMs` has passed
114
- * since the first one arrived. Up to `maxConcurrent` flushes may be in flight
115
- * at once: with a single flight, every caller queued behind a slow flush holds
116
- * its worker slot for the whole round trip, which caps a worker's throughput at
117
- * `maxSize / flushLatency` no matter how much concurrency it was given.
105
+ * Batches `add()` calls into `flushFn` calls, at `maxSize` items or after
106
+ * `maxWaitMs`. Several flushes may run at once so one slow flush doesn't cap
107
+ * the worker's throughput.
118
108
  */
119
109
  var BatchProcessor = class {
120
110
  maxSize;
@@ -364,14 +354,13 @@ const baseConfigSchema = z.object({
364
354
  PIPELINE_FUSED: z.string().optional().transform((v) => v === "true" || v === "1"),
365
355
  QUEUE_MAX_LEN: z.coerce.number().int().min(1).default(1e5),
366
356
  /**
367
- * How long the pipeline remembers it handled a message, so a redelivery
368
- * after a crash is not sent twice. It only has to outlast redelivery (about
369
- * ten minutes) plus however far behind the streams run; a caller's own
370
- * X-Idempotency-Key is always kept for 24 hours regardless. These markers
371
- * are most of Redis memory under load, so the cost of a longer window is
372
- * real: a few hundred bytes per message for as long as it lasts.
357
+ * How long the pipeline remembers a handled message, so a redelivery isn't
358
+ * sent twice. Must outlast redelivery plus stream lag. These markers are most
359
+ * of Redis memory under load. A caller's X-Idempotency-Key always keeps 24h.
373
360
  */
374
361
  IDEMPOTENCY_TTL_SECONDS: z.coerce.number().int().min(60).default(3600),
362
+ /** Non-critical sends per user per hour, unless the project overrides it. */
363
+ RATE_LIMIT_PER_HOUR: z.coerce.number().int().min(0).default(100),
375
364
  DB_MAX_CONNECTIONS: z.coerce.number().int().min(1).default(2),
376
365
  /**
377
366
  * PostgreSQL schema that holds notifkit's tables and enum types. Defaults to
@@ -381,8 +370,10 @@ const baseConfigSchema = z.object({
381
370
  */
382
371
  DB_SCHEMA: z.string().trim().regex(/^[a-z_][a-z0-9_]{0,62}$/, "must be a lowercase PostgreSQL identifier").default("public"),
383
372
  LOG_FLUSH_INTERVAL_MS: z.coerce.number().int().min(50).default(500),
373
+ /** Days of delivery logs to keep; the scheduler deletes older rows hourly. Unset keeps them forever. */
374
+ LOG_RETENTION_DAYS: z.coerce.number().int().min(1).optional(),
384
375
  LOG_BUFFER_MAX_SIZE: z.coerce.number().int().min(100).default(5e3),
385
- SEGMENT_MAX_USERS: z.coerce.number().int().min(1).default(1e4),
376
+ SEGMENT_MAX_USERS: z.coerce.number().int().min(1).default(1e5),
386
377
  /**
387
378
  * Externally reachable base URL of this API. Unsubscribe links are built from
388
379
  * it, so it must be what an inbox can actually reach — not `HOST`/`PORT`,
@@ -1168,6 +1159,7 @@ const messageLogs = pgTable("message_logs", {
1168
1159
  projectIdTaskIdIdx: index("message_logs_project_task_idx").on(table.projectId, table.taskId),
1169
1160
  providerMsgIdx: index("provider_msg_idx").on(table.providerMessageId),
1170
1161
  projectTimeIdx: index("msg_log_proj_time_idx").on(table.projectId, table.timestamp),
1162
+ timeIdx: index("msg_log_time_idx").on(table.timestamp),
1171
1163
  templateIdx: index("msg_log_template_idx").on(table.projectId, table.templateId),
1172
1164
  workflowIdx: index("msg_log_workflow_idx").on(table.projectId, table.workflowInstanceId),
1173
1165
  campaignIdx: index("msg_log_campaign_idx").on(table.projectId, table.campaignId),
@@ -1259,7 +1251,8 @@ const deliveryOutbox = pgTable("delivery_outbox", {
1259
1251
  table.channel,
1260
1252
  table.destination
1261
1253
  ] }),
1262
- destinationIdx: index("delivery_outbox_destination_idx").on(table.destination)
1254
+ destinationIdx: index("delivery_outbox_destination_idx").on(table.destination),
1255
+ createdIdx: index("delivery_outbox_created_idx").on(table.createdAt)
1263
1256
  }));
1264
1257
  const scheduledPayloads = pgTable("scheduled_payloads", {
1265
1258
  taskId: varchar("task_id").primaryKey(),
@@ -1421,10 +1414,7 @@ async function migrateDatabase({ url, schema: dbSchema }) {
1421
1414
  }
1422
1415
  //#endregion
1423
1416
  //#region src/idempotency/index.ts
1424
- /**
1425
- * SET NX-based idempotency guard.
1426
- * One key per id: `L` while leased, `1` once processed.
1427
- */
1417
+ /** One Redis key per id: `L` while a worker holds it, `1` once processed. */
1428
1418
  var IdempotencyGuard = class {
1429
1419
  redis;
1430
1420
  keyPrefix;
@@ -1437,38 +1427,20 @@ var IdempotencyGuard = class {
1437
1427
  key(id) {
1438
1428
  return `${this.keyPrefix}:${id}`;
1439
1429
  }
1440
- /** @deprecated Nothing writes `:lock` keys any more; read only for markers from older versions. Remove in the next minor. */
1441
- lockKey(id) {
1442
- return `${this.keyPrefix}:${id}:lock`;
1443
- }
1444
- /** @deprecated Nothing writes `:sent` keys any more; read only for markers from older versions. Remove in the next minor. */
1445
- completedKey(id) {
1446
- return `${this.keyPrefix}:${id}:sent`;
1447
- }
1448
- /**
1449
- * Leases the id with SET NX. One key per id: `L` while leased, `1` once processed.
1450
- * Returns "completed" if already processed, "locked" if another worker holds the lease.
1451
- */
1452
1430
  async acquireLease(id, lockTtlSeconds = 30) {
1453
- const mainKey = this.key(id);
1454
- const lockKey = this.lockKey(id);
1455
- if (await this.redis.set(mainKey, "L", "EX", lockTtlSeconds, "NX") === "OK") return "acquired";
1456
- const [val, sent, lockVal] = await Promise.all([
1457
- this.redis.get(mainKey),
1458
- this.redis.get(this.completedKey(id)),
1459
- this.redis.get(lockKey)
1460
- ]);
1461
- if (val === "S" || val === "1" || sent !== null) return "completed";
1462
- if (val === "L" || lockVal !== null) return "locked";
1463
- if (await this.redis.set(mainKey, "L", "EX", lockTtlSeconds, "NX") === "OK") return "acquired";
1464
- return "locked";
1465
- }
1466
- /** Atomically mark id as processed. Returns true on first call; false if already seen. */
1431
+ const key = this.key(id);
1432
+ if (await this.redis.set(key, "L", "EX", lockTtlSeconds, "NX") === "OK") return "acquired";
1433
+ const val = await this.redis.get(key);
1434
+ if (val === "1") return "completed";
1435
+ if (val === "L") return "locked";
1436
+ return await this.redis.set(key, "L", "EX", lockTtlSeconds, "NX") === "OK" ? "acquired" : "locked";
1437
+ }
1438
+ /** Marks the id processed. Returns false if it was already seen. */
1467
1439
  async checkAndMark(id, customTtlSeconds) {
1468
1440
  const ttl = customTtlSeconds ?? this.ttlSeconds;
1469
1441
  return await this.redis.set(this.key(id), "1", "EX", ttl, "NX") === "OK";
1470
1442
  }
1471
- /** Unconditionally mark id as processed (upgrades lock to completed "1" with full TTL). */
1443
+ /** Marks the id processed, replacing a lease with the full TTL. */
1472
1444
  async markProcessed(id, customTtlSeconds) {
1473
1445
  const ttl = customTtlSeconds ?? this.ttlSeconds;
1474
1446
  await this.redis.set(this.key(id), "1", "EX", ttl);
@@ -1477,13 +1449,8 @@ var IdempotencyGuard = class {
1477
1449
  const val = await this.redis.get(this.key(id));
1478
1450
  return val !== null && val !== void 0;
1479
1451
  }
1480
- /** Remove the idempotency marker (useful in tests or manual rollbacks). */
1481
1452
  async unmark(id) {
1482
- await Promise.all([
1483
- this.redis.del(this.key(id)),
1484
- this.redis.del(this.lockKey(id)),
1485
- this.redis.del(this.completedKey(id))
1486
- ]);
1453
+ await this.redis.del(this.key(id));
1487
1454
  }
1488
1455
  };
1489
1456
  //#endregion
@@ -1944,6 +1911,13 @@ var PendingMessageScanner = class {
1944
1911
  this.consumer = consumer;
1945
1912
  this.logger = logger;
1946
1913
  }
1914
+ /**
1915
+ * Resets a held entry's idle time so no scanner reclaims it while its
1916
+ * handler is still running. JUSTID leaves the delivery count alone.
1917
+ */
1918
+ async touch(id, stream) {
1919
+ await this.redis.xclaim(stream, this.group, this.consumer, 0, id, "JUSTID");
1920
+ }
1947
1921
  async getPendingCount() {
1948
1922
  let total = 0;
1949
1923
  for (const s of this.streams) {
@@ -2008,26 +1982,14 @@ const NO_OVERRIDES = {
2008
1982
  throttleWindowHours: null
2009
1983
  };
2010
1984
  /**
2011
- * Caches per-project throttle overrides for the engine.
2012
- *
2013
- * The throttle check runs once per notification, so an uncached lookup here
2014
- * would put a Postgres round trip on the hot path. Projects with no overrides
2015
- * are cached as well — the common case must not cost a query per message.
2016
- *
2017
- * The TTL bounds staleness on its own; `invalidate()` exists so a settings
2018
- * change published over pub/sub applies immediately rather than at expiry.
1985
+ * Per-project throttle overrides, read once per notification. Projects with no
1986
+ * overrides are cached too. `invalidate()` lets a settings change apply before
1987
+ * the TTL runs out.
2019
1988
  */
2020
1989
  var ProjectSettingsCache = class {
2021
1990
  load;
2022
1991
  cache;
2023
- /**
2024
- * Lookups already on the wire, keyed by project.
2025
- *
2026
- * The cache only fills once a query has come back, so without this a cold
2027
- * project at the start of a campaign puts one query per in-flight message on
2028
- * Postgres before the first answer lands — exactly when the database is
2029
- * busiest. Followers wait on the leader's promise instead.
2030
- */
1992
+ /** Lookups in flight, so a cold project at the start of a campaign costs one query. */
2031
1993
  inFlight = /* @__PURE__ */ new Map();
2032
1994
  constructor(load, { maxSize = 1e3, ttlMs = 6e4 } = {}) {
2033
1995
  this.load = load;
@@ -2113,11 +2075,7 @@ var UserThrottle = class {
2113
2075
  };
2114
2076
  //#endregion
2115
2077
  //#region src/rate-limiter/api-rate-limiter.ts
2116
- /**
2117
- * Computes dynamic token lease batch size based on RPM.
2118
- * Targets ~5s of limit capacity so Redis is contacted at most ~12 times/min
2119
- * regardless of instantaneous request rate. Bounded between 1 and maxBatchSize.
2120
- */
2078
+ /** About five seconds' worth of the limit, so Redis is asked ~12 times a minute. */
2121
2079
  function calculateTokenBatchSize(limitRpm, maxBatchSize = 500) {
2122
2080
  if (limitRpm <= 0) return 0;
2123
2081
  if (limitRpm <= 10) return 1;
@@ -2139,11 +2097,6 @@ var ApiRateLimiter = class {
2139
2097
  this.logger = logger;
2140
2098
  this.keyPrefix = keyPrefix;
2141
2099
  }
2142
- /**
2143
- * Evaluates if a request for `projectId` is allowed within its `limitRpm`.
2144
- * Fast-path uses local in-memory token lease (0ms Redis network latency).
2145
- * Refills in the background before tokens are depleted.
2146
- */
2147
2100
  async check(projectId, limitRpm) {
2148
2101
  if (limitRpm <= 0) return false;
2149
2102
  const nowMs = Date.now();
@@ -2218,11 +2171,9 @@ var ApiRateLimiter = class {
2218
2171
  for (const [id, s] of this.leases) if (currentBucket - s.bucketIndex > 1) this.leases.delete(id);
2219
2172
  }
2220
2173
  }
2221
- /** Reset all local cached leases */
2222
2174
  clear() {
2223
2175
  this.leases.clear();
2224
2176
  }
2225
- /** Invalidate local lease for a specific project */
2226
2177
  invalidate(projectId) {
2227
2178
  this.leases.delete(projectId);
2228
2179
  }
@@ -2352,33 +2303,6 @@ const LUA_COUNT_REQUEST = `
2352
2303
  return count
2353
2304
  `;
2354
2305
  /**
2355
- * Two-phase idempotency lease acquisition. Checks sent/legacy, then attempts SET NX lock.
2356
- * @deprecated Nothing calls the `acquireLease` command; removed in the next minor version.
2357
- */
2358
- const LUA_ACQUIRE_LEASE = `
2359
- if redis.call('EXISTS', KEYS[1]) == 1 or redis.call('EXISTS', KEYS[3]) == 1 then
2360
- return 'completed'
2361
- end
2362
- local lockAcquired = redis.call('SET', KEYS[2], '1', 'EX', ARGV[1], 'NX')
2363
- if lockAcquired then
2364
- return 'acquired'
2365
- end
2366
- if redis.call('EXISTS', KEYS[1]) == 1 or redis.call('EXISTS', KEYS[3]) == 1 then
2367
- return 'completed'
2368
- end
2369
- return 'locked'
2370
- `;
2371
- /**
2372
- * Two-phase idempotency completion marker. Atomically sets completed + legacy and deletes lock.
2373
- * @deprecated Nothing calls the `markProcessed` command; removed in the next minor version.
2374
- */
2375
- const LUA_MARK_PROCESSED = `
2376
- redis.call('SET', KEYS[1], '1', 'EX', ARGV[1])
2377
- redis.call('SET', KEYS[2], '1', 'EX', ARGV[1])
2378
- redis.call('DEL', KEYS[3])
2379
- return 1
2380
- `;
2381
- /**
2382
2306
  * Registers pre-compiled custom commands on an ioredis instance.
2383
2307
  * Calls defineCommand so ioredis uses EVALSHA rather than re-transmitting
2384
2308
  * full script strings over the wire.
@@ -2404,34 +2328,18 @@ function registerCustomCommands(redis) {
2404
2328
  numberOfKeys: 1,
2405
2329
  lua: LUA_SCHEDULER_POLL
2406
2330
  });
2407
- redis.defineCommand("acquireLease", {
2408
- numberOfKeys: 3,
2409
- lua: LUA_ACQUIRE_LEASE
2410
- });
2411
2331
  redis.defineCommand("countRequest", {
2412
2332
  numberOfKeys: 1,
2413
2333
  lua: LUA_COUNT_REQUEST
2414
2334
  });
2415
- redis.defineCommand("markProcessed", {
2416
- numberOfKeys: 3,
2417
- lua: LUA_MARK_PROCESSED
2418
- });
2419
2335
  }
2420
2336
  const sharedClients = /* @__PURE__ */ new Map();
2421
2337
  var RedisClient = class RedisClient {
2422
2338
  native;
2423
2339
  /**
2424
- * One command connection per Redis URL for every service in this process.
2425
- *
2426
- * Each service used to open its own, so a single notification's commands
2427
- * reached Redis over three or four sockets, one round trip each. Sharing a
2428
- * connection lets auto-pipelining merge whatever the services issue in the
2429
- * same tick into one write, and Redis spends most of its time per round trip
2430
- * rather than per command. Blocking reads and subscribers still duplicate
2431
- * their own connections.
2432
- *
2433
- * The handle's `disconnect()` releases this caller's reference; the
2434
- * connection closes when the last service lets go.
2340
+ * One command connection per URL for every service in the process, so
2341
+ * auto-pipelining can batch what they send in the same tick. `disconnect()`
2342
+ * drops this caller's reference; the last one closes the connection.
2435
2343
  */
2436
2344
  static shared(options) {
2437
2345
  let entry = sharedClients.get(options.url);
@@ -3618,15 +3526,9 @@ var BaseWorker = class {
3618
3526
  };
3619
3527
  }
3620
3528
  /**
3621
- * Processes an event produced by an upstream stage in this same process,
3622
- * skipping the stream hop in between.
3623
- *
3624
- * The caller is still holding its own stream entry, and does not ack it until
3625
- * this returns. A thrown error therefore leaves that entry pending and the
3626
- * whole chain is replayed from the upstream stage; the idempotency keys each
3627
- * stage already checks make the replay safe. Non-retryable failures go to the
3628
- * dead-letter stream here, as they would have from this worker's own stream,
3629
- * so they do not send the upstream message there instead.
3529
+ * Processes an event from an upstream stage in this process, skipping the stream.
3530
+ * A throw leaves the caller's entry pending, so the chain replays from upstream.
3531
+ * Non-retryable failures are dead-lettered here so the upstream message isn't.
3630
3532
  */
3631
3533
  async processInline(event) {
3632
3534
  try {
@@ -3676,7 +3578,7 @@ var BaseWorker = class {
3676
3578
  globalEmitter.emit("notification:failed", message.event.id, "Poison pill: max retries exceeded", message.event.type);
3677
3579
  return;
3678
3580
  }
3679
- const result = await this.process(message, retryCount);
3581
+ const result = await this.processHoldingLease(message, retryCount);
3680
3582
  if (result && result.ack === false) {
3681
3583
  this.logger.debug({
3682
3584
  messageId: message.id,
@@ -3728,12 +3630,28 @@ var BaseWorker = class {
3728
3630
  }
3729
3631
  }
3730
3632
  /**
3731
- * `recoveryIntervalMs` is how long an entry must sit idle before it is
3732
- * claimed, not how often to look. Scanning only once per interval, timed from
3733
- * this worker's start, meant a worker restarted more often than that (a crash
3734
- * loop, an eager autoscaler) never scanned at all, and whatever its
3735
- * predecessors held stayed pending for good. So scan on start and then
3736
- * several times per interval; the idle threshold alone decides what is taken.
3633
+ * Keeps the entry's idle time under the reclaim threshold while `process` runs,
3634
+ * so a long fan-out isn't reclaimed mid-run. If this process dies, the touches stop.
3635
+ */
3636
+ async processHoldingLease(message, attempt) {
3637
+ const stream = message.stream;
3638
+ const timer = stream ? setInterval(() => {
3639
+ this.pendingScanner.touch(message.id, stream).catch((err) => {
3640
+ this.logger.warn({
3641
+ err,
3642
+ messageId: message.id
3643
+ }, "failed to extend message lease");
3644
+ });
3645
+ }, Math.max(Math.floor(this.recoveryIntervalMs / 2), 10)) : void 0;
3646
+ try {
3647
+ return await this.process(message, attempt);
3648
+ } finally {
3649
+ clearInterval(timer);
3650
+ }
3651
+ }
3652
+ /**
3653
+ * `recoveryIntervalMs` is the idle time before a claim, not the scan period.
3654
+ * Scan on start and several times per interval so crash-looping workers still recover.
3737
3655
  */
3738
3656
  startRecoveryLoop() {
3739
3657
  const scan = () => {
@@ -4194,35 +4112,35 @@ var NotifkitServer = class extends EventEmitter {
4194
4112
  }
4195
4113
  const startupPromises = [];
4196
4114
  if (services.includes("api")) {
4197
- const { startApiServer } = await import("./main-g1KEcXXp.mjs");
4115
+ const { startApiServer } = await import("./main-zgxAlH3l.mjs");
4198
4116
  startupPromises.push(startApiServer());
4199
4117
  }
4200
4118
  if (services.includes("delivery")) {
4201
- const { startDeliveryWorker } = await import("./main-B82bsFoy.mjs");
4119
+ const { startDeliveryWorker } = await import("./main-DVzp0DLn.mjs");
4202
4120
  startupPromises.push(startDeliveryWorker());
4203
4121
  }
4204
4122
  if (services.includes("engine")) {
4205
- const { startEngineWorker } = await import("./main-8nP3qmCT.mjs");
4123
+ const { startEngineWorker } = await import("./main-BN7ZfvxN.mjs");
4206
4124
  startupPromises.push(startEngineWorker());
4207
4125
  }
4208
4126
  if (services.includes("enricher")) {
4209
- const { startEnricherWorker } = await import("./main-DULD8-th.mjs");
4127
+ const { startEnricherWorker } = await import("./main-yL5VinQ5.mjs");
4210
4128
  startupPromises.push(startEnricherWorker());
4211
4129
  }
4212
4130
  if (services.includes("scheduler")) {
4213
- const { startSchedulerWorker } = await import("./main-CpaHF1Zw.mjs");
4131
+ const { startSchedulerWorker } = await import("./main-Ppr7Pq1g.mjs");
4214
4132
  startupPromises.push(startSchedulerWorker());
4215
4133
  }
4216
4134
  if (services.includes("ai")) {
4217
- const { startAiWorker } = await import("./main-6_bXQBDH.mjs");
4135
+ const { startAiWorker } = await import("./main-Bl0S4GDd.mjs");
4218
4136
  startupPromises.push(startAiWorker());
4219
4137
  }
4220
4138
  if (services.includes("workflow")) {
4221
- const { startWorkflowWorker } = await import("./main-ZW5RaiOK.mjs");
4139
+ const { startWorkflowWorker } = await import("./main-O9iZ6iz2.mjs");
4222
4140
  startupPromises.push(startWorkflowWorker());
4223
4141
  }
4224
4142
  if (services.includes("events")) {
4225
- const { startEventWorker } = await import("./main-mGIT8jv4.mjs");
4143
+ const { startEventWorker } = await import("./main-C5DiM7_r.mjs");
4226
4144
  startupPromises.push(startEventWorker());
4227
4145
  }
4228
4146
  const handleSignal = async (signal) => {
@@ -4244,9 +4162,9 @@ var NotifkitServer = class extends EventEmitter {
4244
4162
  await Promise.all(startupPromises);
4245
4163
  if (readBaseConfig().PIPELINE_FUSED && services.includes("enricher") && services.includes("engine") && services.includes("delivery")) {
4246
4164
  const [{ getEnricherWorker }, { getEngineWorker }, { getDeliveryWorker }] = await Promise.all([
4247
- import("./main-DULD8-th.mjs"),
4248
- import("./main-8nP3qmCT.mjs"),
4249
- import("./main-B82bsFoy.mjs")
4165
+ import("./main-yL5VinQ5.mjs"),
4166
+ import("./main-BN7ZfvxN.mjs"),
4167
+ import("./main-DVzp0DLn.mjs")
4250
4168
  ]);
4251
4169
  const enricher = getEnricherWorker();
4252
4170
  const engine = getEngineWorker();
@@ -4277,14 +4195,14 @@ var NotifkitServer = class extends EventEmitter {
4277
4195
  "events"
4278
4196
  ] : this.options.services;
4279
4197
  const shutdownOrder = [
4280
- ["api", async () => (await import("./main-g1KEcXXp.mjs")).stopApiServer()],
4281
- ["workflow", async () => (await import("./main-ZW5RaiOK.mjs")).stopWorkflowWorker()],
4282
- ["enricher", async () => (await import("./main-DULD8-th.mjs")).stopEnricherWorker()],
4283
- ["ai", async () => (await import("./main-6_bXQBDH.mjs")).stopAiWorker()],
4284
- ["engine", async () => (await import("./main-8nP3qmCT.mjs")).stopEngineWorker()],
4285
- ["scheduler", async () => (await import("./main-CpaHF1Zw.mjs")).stopSchedulerWorker()],
4286
- ["delivery", async () => (await import("./main-B82bsFoy.mjs")).stopDeliveryWorker()],
4287
- ["events", async () => (await import("./main-mGIT8jv4.mjs")).stopEventWorker()]
4198
+ ["api", async () => (await import("./main-zgxAlH3l.mjs")).stopApiServer()],
4199
+ ["workflow", async () => (await import("./main-O9iZ6iz2.mjs")).stopWorkflowWorker()],
4200
+ ["enricher", async () => (await import("./main-yL5VinQ5.mjs")).stopEnricherWorker()],
4201
+ ["ai", async () => (await import("./main-Bl0S4GDd.mjs")).stopAiWorker()],
4202
+ ["engine", async () => (await import("./main-BN7ZfvxN.mjs")).stopEngineWorker()],
4203
+ ["scheduler", async () => (await import("./main-Ppr7Pq1g.mjs")).stopSchedulerWorker()],
4204
+ ["delivery", async () => (await import("./main-DVzp0DLn.mjs")).stopDeliveryWorker()],
4205
+ ["events", async () => (await import("./main-C5DiM7_r.mjs")).stopEventWorker()]
4288
4206
  ];
4289
4207
  for (const [name, stop] of shutdownOrder) if (services.includes(name)) await stop();
4290
4208
  if (this.pgContainer) {
@@ -4298,4 +4216,4 @@ var NotifkitServer = class extends EventEmitter {
4298
4216
  }
4299
4217
  };
4300
4218
  //#endregion
4301
- export { assertTablesInSchema as $, OUTBOUND_STREAMS as $t, LUA_ACQUIRE_LEASE as A, globalEmitter as An, NotificationTargetSchema as At, redactUrl as B, QuietHoursSchema as Bt, SegmentRepository as C, DataLoader as Cn, NotificationDispatchedPayloadSchema as Ct, ContactRepository as D, getPriorityBucket as Dn, RecipientProfileSchema as Dt, TemplateRepository as E, AsyncSemaphore as En, NotificationEnrichedPayloadSchema as Et, LUA_RENEW_LOCK as F, CreateWorkflowSchema as Ft, ProjectSettingsCache as G, UpdateUserSchema as Gt, ApiRateLimiter as H, TemplateSchema as Ht, LUA_SCHEDULER_POLL as I, IngestEventSchema as It, StreamProducer as J, WorkflowStepSchema as Jt, PendingMessageScanner as K, UserContactInputSchema as Kt, LUA_USER_THROTTLE as L, InlineUserSchema as Lt, LUA_LEASE_API_RATE_LIMIT as M, AddUserSchema as Mt, LUA_MARK_PROCESSED as N, BatchAddContactsSchema as Nt, PreferenceRepository as O, normaliseTarget as On, NotificationCreatedPayloadSchema as Ot, LUA_RELEASE_LOCK as P, ContactChannelSchema as Pt, IdempotencyGuard as Q, INBOUND_STREAMS as Qt, Redis as R, NotifyRequestSchema as Rt, AdminUserRepository as S, sleep as Sn, DeliveryOptionsSchema as St, ProjectRepository as T, BatchProcessor as Tn, NotificationScheduledPayloadSchema as Tt, calculateTokenBatchSize as U, TriggerWorkflowSchema as Ut, registerCustomCommands as V, SyncTemplatesSchema as Vt, UserThrottle as W, UpdateProjectSchema as Wt, metrics as X, CONSUMER_GROUPS as Xt, getMetricsRegistry as Y, buildStreamEvent as Yt, createLogger as Z, ENRICHED_STREAMS as Zt, TemplateCache as _, setAiConfig as _n, NotificationAiPendingPayloadSchema as _t, SuspendExecutionError as a, EventRegistry as an, deliveryOutbox as at, interpolate as b, ValidationError as bn, NotificationFailedPayloadSchema as bt, BaseWorker as c, NotificationChannelSchema as cn, projects as ct, startHealthReporter as d, AI_DEFAULTS as dn, userTopicPreferences as dt, PUBSUB_CHANNELS as en, createDatabase as et, buildUnsubscribeHeaders as f, baseConfigSchema as fn, users as ft, transportRegistry as g, readBaseConfig as gn, workflowWaiters as gt, registerTransport as h, parseConfig as hn, workflowSteps as ht, workflowRegistry as i, StreamEventSchema as in, runMigrations as it, LUA_COUNT_REQUEST as j, AddContactSchema as jt, UserRepository as k, LRUCache as kn, NotificationRequestedPayloadSchema as kt, LockHeldError as l, NotificationPrioritySchema as ln, scheduledPayloads as lt, verifyUnsubscribeToken as m, loadEnv as mn, workflowInstances as mt, NotifkitClient as n, EventEnvelopeSchema as nn, notifkitTableNames as nt, buildStepNotifyPayload as o, registry as on, messageLogs as ot, signUnsubscribeToken as p, getAiConfig as pn, workflowDefinitions as pt, StreamConsumer as q, WorkflowNotifyPayloadSchema as qt, workflow as r, StreamEventMetadataSchema as rn, qualifyMigrationSql as rt, resolveStepTarget as s, EventMetadataSchema as sn, projectApiKeys as st, NotifkitServer as t, STREAMS as tn, migrateDatabase as tt, NonRetryableError as u, NotificationStatusSchema as un, suppressions as ut, escapeHeader as v, setGlobalConfig as vn, NotificationCanceledPayloadSchema as vt, WorkflowRepository as w, CircuitBreaker as wn, RenderedContentSchema as wt, renderWithTemplate as x, generateId as xn, NotificationDeliveredPayloadSchema as xt, escapeHtml as y, AppError as yn, NotificationSkippedPayloadSchema as yt, RedisClient as z, PreferencesSchema as zt };
4219
+ export { migrateDatabase as $, STREAMS as $t, LUA_COUNT_REQUEST as A, AddUserSchema as At, ApiRateLimiter as B, TemplateSchema as Bt, SegmentRepository as C, BatchProcessor as Cn, NotificationScheduledPayloadSchema as Ct, ContactRepository as D, LRUCache as Dn, NotificationRequestedPayloadSchema as Dt, TemplateRepository as E, normaliseTarget as En, NotificationCreatedPayloadSchema as Et, LUA_USER_THROTTLE as F, InlineUserSchema as Ft, StreamConsumer as G, WorkflowNotifyPayloadSchema as Gt, UserThrottle as H, UpdateProjectSchema as Ht, Redis as I, NotifyRequestSchema as It, metrics as J, CONSUMER_GROUPS as Jt, StreamProducer as K, WorkflowStepSchema as Kt, RedisClient as L, PreferencesSchema as Lt, LUA_RELEASE_LOCK as M, ContactChannelSchema as Mt, LUA_RENEW_LOCK as N, CreateWorkflowSchema as Nt, PreferenceRepository as O, globalEmitter as On, NotificationTargetSchema as Ot, LUA_SCHEDULER_POLL as P, IngestEventSchema as Pt, createDatabase as Q, PUBSUB_CHANNELS as Qt, redactUrl as R, QuietHoursSchema as Rt, AdminUserRepository as S, CircuitBreaker as Sn, RenderedContentSchema as St, ProjectRepository as T, getPriorityBucket as Tn, RecipientProfileSchema as Tt, ProjectSettingsCache as U, UpdateUserSchema as Ut, calculateTokenBatchSize as V, TriggerWorkflowSchema as Vt, PendingMessageScanner as W, UserContactInputSchema as Wt, IdempotencyGuard as X, INBOUND_STREAMS as Xt, createLogger as Y, ENRICHED_STREAMS as Yt, assertTablesInSchema as Z, OUTBOUND_STREAMS as Zt, TemplateCache as _, AppError as _n, NotificationSkippedPayloadSchema as _t, SuspendExecutionError as a, EventMetadataSchema as an, projectApiKeys as at, interpolate as b, sleep as bn, DeliveryOptionsSchema as bt, BaseWorker as c, NotificationStatusSchema as cn, suppressions as ct, startHealthReporter as d, getAiConfig as dn, workflowDefinitions as dt, EventEnvelopeSchema as en, notifkitTableNames as et, buildUnsubscribeHeaders as f, loadEnv as fn, workflowInstances as ft, transportRegistry as g, setGlobalConfig as gn, NotificationCanceledPayloadSchema as gt, registerTransport as h, setAiConfig as hn, NotificationAiPendingPayloadSchema as ht, workflowRegistry as i, registry as in, messageLogs as it, LUA_LEASE_API_RATE_LIMIT as j, BatchAddContactsSchema as jt, UserRepository as k, AddContactSchema as kt, LockHeldError as l, AI_DEFAULTS as ln, userTopicPreferences as lt, verifyUnsubscribeToken as m, readBaseConfig as mn, workflowWaiters as mt, NotifkitClient as n, StreamEventSchema as nn, runMigrations as nt, buildStepNotifyPayload as o, NotificationChannelSchema as on, projects as ot, signUnsubscribeToken as p, parseConfig as pn, workflowSteps as pt, getMetricsRegistry as q, buildStreamEvent as qt, workflow as r, EventRegistry as rn, deliveryOutbox as rt, resolveStepTarget as s, NotificationPrioritySchema as sn, scheduledPayloads as st, NotifkitServer as t, StreamEventMetadataSchema as tn, qualifyMigrationSql as tt, NonRetryableError as u, baseConfigSchema as un, users as ut, escapeHeader as v, ValidationError as vn, NotificationFailedPayloadSchema as vt, WorkflowRepository as w, AsyncSemaphore as wn, NotificationEnrichedPayloadSchema as wt, renderWithTemplate as x, DataLoader as xn, NotificationDispatchedPayloadSchema as xt, escapeHtml as y, generateId as yn, NotificationDeliveredPayloadSchema as yt, registerCustomCommands as z, SyncTemplatesSchema as zt };
@@ -0,0 +1,4 @@
1
+ -- The hourly retention prune deletes by age; without these it scans both tables.
2
+ -- Timestamps only grow, so each insert appends to the right edge of the index.
3
+ CREATE INDEX IF NOT EXISTS "msg_log_time_idx" ON "message_logs" USING btree ("timestamp");--> statement-breakpoint
4
+ CREATE INDEX IF NOT EXISTS "delivery_outbox_created_idx" ON "delivery_outbox" USING btree ("created_at");
@@ -64,6 +64,13 @@
64
64
  "when": 1790800000000,
65
65
  "tag": "0008_delivery_outbox_destination_idx",
66
66
  "breakpoints": true
67
+ },
68
+ {
69
+ "idx": 9,
70
+ "version": "7",
71
+ "when": 1790900000000,
72
+ "tag": "0009_retention_time_indexes",
73
+ "breakpoints": true
67
74
  }
68
75
  ]
69
76
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "notifkit",
3
- "version": "0.1.16",
3
+ "version": "0.1.18",
4
4
  "description": "Self-hosted notification infrastructure. One call delivers to email, SMS, push, and webhook — routed by preference, quiet hours, and consent.",
5
5
  "license": "MIT",
6
6
  "author": "devkitshq",
package/src/client.ts CHANGED
@@ -320,7 +320,7 @@ export class NotifkitClient {
320
320
  return this.request("/v1/segments", "GET");
321
321
  }
322
322
 
323
- // ─── Campaigns ───────────────────────────────────────────────────────────────
323
+ // Campaigns
324
324
 
325
325
  /** List campaign labels seen in the delivery log, most recent activity first. */
326
326
  async listCampaigns(options?: {
@@ -371,7 +371,7 @@ export class NotifkitClient {
371
371
  return this.request(`/v1/campaigns/${encodeURIComponent(campaign)}/stats`, "GET");
372
372
  }
373
373
 
374
- // ─── Suppressions ────────────────────────────────────────────────────────────
374
+ // Suppressions
375
375
 
376
376
  /** List suppressed destinations. */
377
377
  async listSuppressions(options?: {
@@ -406,7 +406,7 @@ export class NotifkitClient {
406
406
  );
407
407
  }
408
408
 
409
- // ─── Notification Status & Cancellation ─────────────────────────────────────
409
+ // Notification Status & Cancellation
410
410
 
411
411
  /** Get real-time status and delivery logs for a specific notification task. */
412
412
  async getNotificationStatus(
@@ -425,7 +425,7 @@ export class NotifkitClient {
425
425
  return this.request("/v1/notifications/scheduled", "GET");
426
426
  }
427
427
 
428
- // ─── User Profile & Preferences ─────────────────────────────────────────────
428
+ // User Profile & Preferences
429
429
 
430
430
  /** Get user profile and contacts by ID. */
431
431
  async getUser(id: string): Promise<UserResponse> {
@@ -450,7 +450,7 @@ export class NotifkitClient {
450
450
  return this.request(`/v1/users/${encodeURIComponent(id)}/preferences`, "PATCH", preferences);
451
451
  }
452
452
 
453
- // ─── Templates Querying ─────────────────────────────────────────────────────
453
+ // Templates Querying
454
454
 
455
455
  /** List all templates for the project. */
456
456
  async listTemplates(): Promise<{ templates: TemplateRecordResponse[] }> {
@@ -462,7 +462,7 @@ export class NotifkitClient {
462
462
  return this.request(`/v1/templates/${encodeURIComponent(id)}`, "GET");
463
463
  }
464
464
 
465
- // ─── System Health, Metrics & DLQ ───────────────────────────────────────────
465
+ // System Health, Metrics & DLQ
466
466
 
467
467
  /** Get system health and worker status. */
468
468
  async getSystemHealth(): Promise<SystemHealthRecord> {
@@ -38,10 +38,7 @@ export const baseConfigSchema = z.object({
38
38
  HOST: z.string().default("127.0.0.1"),
39
39
  REDIS_URL: z.string().url().default("redis://localhost:6379"),
40
40
  DATABASE_URL: z.string().url().default("postgres://platform:platform@localhost:5432/notifkit"),
41
- // Trimmed because the incoming bearer token is trimmed before comparison, so
42
- // an untrimmed value here could never match it. `set KEY=value && cmd` on
43
- // Windows puts a trailing space in the variable, which otherwise turns every
44
- // admin request into a 401 that reads like a wrong key.
41
+ // Trimmed like the incoming token; Windows `set KEY=value && cmd` adds a trailing space.
45
42
  ADMIN_API_KEY: z.string().trim().optional(),
46
43
  ADMIN_EMAIL: z.string().trim().email().optional(),
47
44
  ADMIN_PASSWORD: z.string().trim().min(6).optional(),
@@ -60,14 +57,13 @@ export const baseConfigSchema = z.object({
60
57
  .transform((v) => v === "true" || v === "1"),
61
58
  QUEUE_MAX_LEN: z.coerce.number().int().min(1).default(100000),
62
59
  /**
63
- * How long the pipeline remembers it handled a message, so a redelivery
64
- * after a crash is not sent twice. It only has to outlast redelivery (about
65
- * ten minutes) plus however far behind the streams run; a caller's own
66
- * X-Idempotency-Key is always kept for 24 hours regardless. These markers
67
- * are most of Redis memory under load, so the cost of a longer window is
68
- * real: a few hundred bytes per message for as long as it lasts.
60
+ * How long the pipeline remembers a handled message, so a redelivery isn't
61
+ * sent twice. Must outlast redelivery plus stream lag. These markers are most
62
+ * of Redis memory under load. A caller's X-Idempotency-Key always keeps 24h.
69
63
  */
70
64
  IDEMPOTENCY_TTL_SECONDS: z.coerce.number().int().min(60).default(3600),
65
+ /** Non-critical sends per user per hour, unless the project overrides it. */
66
+ RATE_LIMIT_PER_HOUR: z.coerce.number().int().min(0).default(100),
71
67
  DB_MAX_CONNECTIONS: z.coerce.number().int().min(1).default(2),
72
68
  /**
73
69
  * PostgreSQL schema that holds notifkit's tables and enum types. Defaults to
@@ -81,8 +77,10 @@ export const baseConfigSchema = z.object({
81
77
  .regex(/^[a-z_][a-z0-9_]{0,62}$/, "must be a lowercase PostgreSQL identifier")
82
78
  .default("public"),
83
79
  LOG_FLUSH_INTERVAL_MS: z.coerce.number().int().min(50).default(500),
80
+ /** Days of delivery logs to keep; the scheduler deletes older rows hourly. Unset keeps them forever. */
81
+ LOG_RETENTION_DAYS: z.coerce.number().int().min(1).optional(),
84
82
  LOG_BUFFER_MAX_SIZE: z.coerce.number().int().min(100).default(5000),
85
- SEGMENT_MAX_USERS: z.coerce.number().int().min(1).default(10000),
83
+ SEGMENT_MAX_USERS: z.coerce.number().int().min(1).default(100_000),
86
84
  /**
87
85
  * Externally reachable base URL of this API. Unsubscribe links are built from
88
86
  * it, so it must be what an inbox can actually reach — not `HOST`/`PORT`,