notifkit 0.1.17 → 0.1.19

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 -5
  2. package/dist/{bootstrap-P63acBG9.mjs → bootstrap-CHOj1rDv.mjs} +1 -1
  3. package/dist/{bulk-Beblt3ny.mjs → bulk-CAsYwF-9.mjs} +1 -1
  4. package/dist/index.d.mts +29 -111
  5. package/dist/index.mjs +2 -2
  6. package/dist/{main-Dk92nti8.mjs → main-B88bnGHO.mjs} +5 -12
  7. package/dist/{main-BIahf4f9.mjs → main-BDTDp4Bg.mjs} +35 -16
  8. package/dist/{main-C3XcLoCb.mjs → main-C9ekO6RS.mjs} +3 -3
  9. package/dist/{main-_I4b1cmP.mjs → main-CBeKd8wJ.mjs} +3 -3
  10. package/dist/{main-NHUvynW2.mjs → main-CVTZoGzA.mjs} +4 -9
  11. package/dist/{main-CktEylpL.mjs → main-CYxNbgX5.mjs} +23 -94
  12. package/dist/{main-DB0bUh2r.mjs → main-CcGiccDe.mjs} +5 -14
  13. package/dist/{main-Lcbg7aEn.mjs → main-DGF3WtFw.mjs} +8 -58
  14. package/dist/{schedule-or-dispatch-C5qfxQCv.mjs → schedule-or-dispatch-BF85qhMh.mjs} +1 -1
  15. package/dist/{src-BRatuWJS.mjs → src-aiRAj2xr.mjs} +72 -175
  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 +8 -10
  21. package/src/contracts/sdk.ts +26 -11
  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 +1 -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 +8 -27
@@ -1,4 +1,4 @@
1
- import { An as globalEmitter, C as SegmentRepository, D as ContactRepository, Dn as getPriorityBucket, E as TemplateRepository, Ft as CreateWorkflowSchema, Gt as UpdateUserSchema, H as ApiRateLimiter, It as IngestEventSchema, J as StreamProducer, Mt as AddUserSchema, Nt as BatchAddContactsSchema, On as normaliseTarget, Pt as ContactChannelSchema, Rt as NotifyRequestSchema, S as AdminUserRepository, T as ProjectRepository, Ut as TriggerWorkflowSchema, Vt as SyncTemplatesSchema, Wt as UpdateProjectSchema, X as metrics, Xt as CONSUMER_GROUPS, Y as getMetricsRegistry, Yt as buildStreamEvent, Z as createLogger, at as deliveryOutbox, ct as projects, dt as userTopicPreferences, en as PUBSUB_CHANNELS, et as createDatabase, ft as users, g as transportRegistry, gn as readBaseConfig, k as UserRepository, kn as LRUCache, lt as scheduledPayloads, m as verifyUnsubscribeToken, mn as loadEnv, ot as messageLogs, pt as workflowDefinitions, st as projectApiKeys, tn as STREAMS, ut as suppressions, w as WorkflowRepository, z as RedisClient, zt as PreferencesSchema } from "./src-BRatuWJS.mjs";
1
+ import { $t as STREAMS, At as AddUserSchema, B as ApiRateLimiter, C as SegmentRepository, D as ContactRepository, Dn as LRUCache, E as TemplateRepository, En as normaliseTarget, Ht as UpdateProjectSchema, It as NotifyRequestSchema, J as metrics, Jt as CONSUMER_GROUPS, K as StreamProducer, L as RedisClient, Lt as PreferencesSchema, Mt as ContactChannelSchema, Nt as CreateWorkflowSchema, On as globalEmitter, Pt as IngestEventSchema, Q as createDatabase, Qt as PUBSUB_CHANNELS, S as AdminUserRepository, T as ProjectRepository, Tn as getPriorityBucket, Ut as UpdateUserSchema, Vt as TriggerWorkflowSchema, Y as createLogger, at as projectApiKeys, ct as suppressions, dt as workflowDefinitions, fn as loadEnv, g as transportRegistry, it as messageLogs, jt as BatchAddContactsSchema, k as UserRepository, lt as userTopicPreferences, m as verifyUnsubscribeToken, mn as readBaseConfig, ot as projects, q as getMetricsRegistry, qt as buildStreamEvent, rt as deliveryOutbox, st as scheduledPayloads, ut as users, w as WorkflowRepository, zt as SyncTemplatesSchema } from "./src-aiRAj2xr.mjs";
2
2
  import { dirname, extname, isAbsolute, join, relative, resolve } from "node:path";
3
3
  import { z } from "zod";
4
4
  import { createHash, randomBytes, randomUUID, scrypt, timingSafeEqual } from "node:crypto";
@@ -10,11 +10,12 @@ import { createReadStream, existsSync, statSync } from "node:fs";
10
10
  import { fileURLToPath } from "node:url";
11
11
  import { pipeline } from "node:stream";
12
12
  //#region src/services/api/http.ts
13
+ const MAX_PAYLOAD_SIZE = 5242880;
14
+ /** The body as text, for webhook signatures that are computed over the raw bytes. */
13
15
  async function readRawBody(req) {
14
16
  return await new Promise((resolve, reject) => {
15
17
  const chunks = [];
16
18
  let totalLength = 0;
17
- const MAX_PAYLOAD_SIZE = 5242880;
18
19
  req.on("data", (chunk) => {
19
20
  totalLength += chunk.length;
20
21
  if (totalLength > MAX_PAYLOAD_SIZE) {
@@ -33,7 +34,6 @@ async function readJsonBody(req) {
33
34
  const decoder = new StringDecoder("utf8");
34
35
  let body = "";
35
36
  let totalLength = 0;
36
- const MAX_PAYLOAD_SIZE = 5242880;
37
37
  req.on("data", (chunk) => {
38
38
  const bytes = typeof chunk === "string" ? Buffer.from(chunk) : chunk;
39
39
  totalLength += bytes.length;
@@ -77,7 +77,6 @@ function sendJson(res, status, body) {
77
77
  function sendNoContent(res) {
78
78
  res.writeHead(204).end();
79
79
  }
80
- /** Turn a Zod error into a 400 response with field-level issues. */
81
80
  function sendValidationError(res, error) {
82
81
  sendJson(res, 400, {
83
82
  error: "validation_error",
@@ -87,7 +86,6 @@ function sendValidationError(res, error) {
87
86
  }))
88
87
  });
89
88
  }
90
- /** A thrown HttpError short-circuits a handler with a specific status/body. */
91
89
  var HttpError = class extends Error {
92
90
  status;
93
91
  code;
@@ -176,7 +174,6 @@ function split(path) {
176
174
  }
177
175
  //#endregion
178
176
  //#region src/services/api/handlers/shared.ts
179
- /** Map an inline or user input's contacts and legacy arrays into resolved contact records. */
180
177
  function contactsOf(user) {
181
178
  const list = [];
182
179
  if (user.contacts && Array.isArray(user.contacts)) for (const c of user.contacts) list.push({
@@ -201,7 +198,6 @@ function contactsOf(user) {
201
198
  });
202
199
  return list;
203
200
  }
204
- /** Persist user records + their contacts in bulk. Shared by addUser and inline notify. */
205
201
  async function persistUsers(deps, users, projectId) {
206
202
  const usersList = users.map((u) => {
207
203
  const primaryEmail = u.email?.[0] ?? u.contacts?.find((c) => c.channel === "email")?.target;
@@ -256,10 +252,7 @@ function parseDlqEntry(id, rawFields) {
256
252
  fields
257
253
  };
258
254
  }
259
- /**
260
- * Ids that live in uuid columns. Anything else can match no row, and passing it
261
- * to Postgres fails the query outright — a 500 for what is simply not found.
262
- */
255
+ /** Postgres errors on a non-uuid in a uuid column; callers answer 404 instead. */
263
256
  const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
264
257
  const isUuid = (v) => typeof v === "string" && UUID_RE.test(v);
265
258
  /** Opaque position in the delivery log: a row's full-precision timestamp and id. */
@@ -281,22 +274,14 @@ function decodeLogCursor(cursor) {
281
274
  } catch {}
282
275
  return null;
283
276
  }
284
- /**
285
- * A LIKE pattern matching `term` anywhere, with the term's own `%`, `_` and
286
- * `\` taken literally rather than as wildcards.
287
- */
277
+ /** A LIKE pattern matching `term` anywhere, its own wildcards escaped. */
288
278
  function contains(term) {
289
279
  return `%${term.replace(/[\\%_]/g, "\\$&")}%`;
290
280
  }
291
281
  function getQueryParam(ctx, key) {
292
- if (!ctx.query) return void 0;
293
- if (typeof ctx.query.get === "function") return ctx.query.get(key) ?? void 0;
294
- return ctx.query[key] ?? void 0;
282
+ return ctx.query.get(key) ?? void 0;
295
283
  }
296
- /**
297
- * Page size from `?limit=`, clamped to [1, max]. Missing or non-numeric
298
- * values fall back to `fallback`; `undefined` there means "no limit".
299
- */
284
+ /** `?limit=` clamped to [1, max]; missing or junk gives `fallback` (`undefined` = no limit). */
300
285
  function parseLimit(ctx, fallback, max = 100) {
301
286
  const n = parseInt(getQueryParam(ctx, "limit") ?? "", 10);
302
287
  return Number.isNaN(n) ? fallback : Math.min(Math.max(n, 1), max);
@@ -572,11 +557,7 @@ function createNotificationsHandlers(deps) {
572
557
  userId: u.id
573
558
  });
574
559
  }
575
- if (inlineUsersToSync.length > 0) try {
576
- await persistUsers(deps, inlineUsersToSync, ctx.projectId);
577
- } catch (err) {
578
- deps.logger.error({ err }, "inline user bulk sync failed");
579
- }
560
+ if (inlineUsersToSync.length > 0) await persistUsers(deps, inlineUsersToSync, ctx.projectId);
580
561
  } else if (body.segment !== void 0) targets.push({
581
562
  type: "segment",
582
563
  segment: body.segment
@@ -1256,14 +1237,6 @@ button{font:inherit;padding:.6rem 1.2rem;border:0;border-radius:6px;background:#
1256
1237
  code{background:#f4f4f5;padding:.1rem .35rem;border-radius:4px}</style>
1257
1238
  </head><body><h1>${escapeHtml(title)}</h1>${message}</body></html>`;
1258
1239
  }
1259
- /**
1260
- * GET — the page a human lands on after clicking the link in the mail body.
1261
- *
1262
- * Deliberately does not unsubscribe anything. Corporate mail scanners and
1263
- * link prescanners issue a GET against every URL in a message; performing the
1264
- * opt-out here would unsubscribe people who never clicked. The mutation lives
1265
- * behind the POST.
1266
- */
1267
1240
  async function unsubscribePage(_req, res, ctx) {
1268
1241
  const cfg = readBaseConfig();
1269
1242
  const token = ctx.query.get("token") ?? "";
@@ -1274,15 +1247,6 @@ code{background:#f4f4f5;padding:.1rem .35rem;border-radius:4px}</style>
1274
1247
  <form method="post" action="/v1/unsubscribe?token=${encodeURIComponent(token)}">
1275
1248
  <button type="submit">Unsubscribe</button></form>`));
1276
1249
  }
1277
- /**
1278
- * POST — the real opt-out.
1279
- *
1280
- * Serves two callers with one handler: a mail client doing RFC 8058 one-click
1281
- * (body `List-Unsubscribe=One-Click`, token in the query string, no
1282
- * confirmation possible), and the form on the page above. Neither can carry a
1283
- * CSRF token, and the one-click spec forbids an interstitial, so the signed
1284
- * token is the whole of the authorisation.
1285
- */
1286
1250
  async function unsubscribe(req, res, ctx) {
1287
1251
  req.resume();
1288
1252
  const cfg = readBaseConfig();
@@ -1493,23 +1457,13 @@ function createSuppressionsHandlers(deps) {
1493
1457
  const scryptAsync = promisify(scrypt);
1494
1458
  const DEFAULT_SESSION_TTL_SECONDS = 604800;
1495
1459
  const SESSION_PREFIX = "notif:session:";
1496
- /**
1497
- * Pre-computed dummy hash conforming to salt(16 bytes hex):hash(64 bytes hex).
1498
- * Used during authentication failure to equalize execution time and prevent
1499
- * user enumeration via scrypt timing side-channels.
1500
- */
1460
+ /** Verified against when the user does not exist, so a miss costs as much as a wrong password. */
1501
1461
  const DUMMY_PASSWORD_HASH = "00000000000000000000000000000000:00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
1502
- /**
1503
- * Hashes a plaintext password using crypto.scrypt with a random 16-byte salt.
1504
- * Returns salt and hash separated by a colon.
1505
- */
1462
+ /** Returns `salt:hash`, both hex. */
1506
1463
  async function hashPassword(password) {
1507
1464
  const salt = randomBytes(16).toString("hex");
1508
1465
  return `${salt}:${(await scryptAsync(password, salt, 64)).toString("hex")}`;
1509
1466
  }
1510
- /**
1511
- * Verifies a plaintext password against a stored salt:hash string using constant-time comparison.
1512
- */
1513
1467
  async function verifyPassword(password, storedHash) {
1514
1468
  const [salt, key] = storedHash.split(":");
1515
1469
  if (!salt || !key) return false;
@@ -1518,9 +1472,6 @@ async function verifyPassword(password, storedHash) {
1518
1472
  if (derivedKey.length !== keyBuffer.length) return false;
1519
1473
  return timingSafeEqual(derivedKey, keyBuffer);
1520
1474
  }
1521
- /**
1522
- * Creates a secure session in Redis for an authenticated admin user.
1523
- */
1524
1475
  async function createAdminSession(redis, user, ttlSeconds = DEFAULT_SESSION_TTL_SECONDS) {
1525
1476
  const token = `nk_sess_${randomBytes(32).toString("hex")}`;
1526
1477
  const now = Date.now();
@@ -1536,9 +1487,6 @@ async function createAdminSession(redis, user, ttlSeconds = DEFAULT_SESSION_TTL_
1536
1487
  await redis.set(`${SESSION_PREFIX}${token}`, JSON.stringify(session), "EX", ttlSeconds);
1537
1488
  return session;
1538
1489
  }
1539
- /**
1540
- * Retrieves an admin session from Redis if valid and unexpired.
1541
- */
1542
1490
  async function getAdminSession(redis, token) {
1543
1491
  if (!token || !token.startsWith("nk_sess_")) return null;
1544
1492
  const raw = await redis.get(`${SESSION_PREFIX}${token}`);
@@ -1554,9 +1502,6 @@ async function getAdminSession(redis, token) {
1554
1502
  return null;
1555
1503
  }
1556
1504
  }
1557
- /**
1558
- * Revokes an active admin session.
1559
- */
1560
1505
  async function revokeAdminSession(redis, token) {
1561
1506
  if (!token) return;
1562
1507
  await redis.del(`${SESSION_PREFIX}${token}`);
@@ -1606,16 +1551,12 @@ function createAuthHandlers(deps) {
1606
1551
  });
1607
1552
  }
1608
1553
  async function logout(req, res) {
1609
- const authHeader = req.headers["authorization"];
1610
- let token;
1611
- if (typeof authHeader === "string" && authHeader.toLowerCase().startsWith("bearer ")) token = authHeader.slice(7).trim();
1554
+ const token = extractAuthToken(req);
1612
1555
  if (token) await revokeAdminSession(deps.redis.native, token);
1613
1556
  sendJson(res, 200, { message: "Logged out successfully" });
1614
1557
  }
1615
1558
  async function getMe(req, res) {
1616
- const authHeader = req.headers["authorization"];
1617
- let token;
1618
- if (typeof authHeader === "string" && authHeader.toLowerCase().startsWith("bearer ")) token = authHeader.slice(7).trim();
1559
+ const token = extractAuthToken(req);
1619
1560
  if (!token) {
1620
1561
  sendJson(res, 401, {
1621
1562
  error: "unauthorized",
@@ -2005,7 +1946,6 @@ function createHealthHandlers({ redis, sql, isAdminApiKey }) {
2005
1946
  }
2006
1947
  //#endregion
2007
1948
  //#region src/services/api/webhooks.ts
2008
- /** The POST route a provider delivers webhook events to. */
2009
1949
  function createWebhookHandler(transport, channel, deps) {
2010
1950
  const { db, logger } = deps;
2011
1951
  return async (req, res) => {
@@ -2108,9 +2048,7 @@ function createWebhookHandler(transport, channel, deps) {
2108
2048
  }
2109
2049
  //#endregion
2110
2050
  //#region src/services/api/main.ts
2111
- /** Pub/sub channel used to drop a cached API key across every API process. */
2112
2051
  const API_KEY_INVALIDATION_CHANNEL = "apikey.invalidated";
2113
- /** Default window length in milliseconds for API rate limiting (1 minute). */
2114
2052
  const API_RATE_LIMIT_WINDOW_MS = 6e4;
2115
2053
  loadEnv();
2116
2054
  const config = readBaseConfig();
@@ -2128,27 +2066,20 @@ let authSubscriber = null;
2128
2066
  let router;
2129
2067
  let liveEvents = null;
2130
2068
  let apiRateLimiter;
2131
- /** Whether `token` is ADMIN_API_KEY, compared in constant time. */
2132
2069
  function isAdminApiKey(token) {
2133
2070
  if (!config.ADMIN_API_KEY) return false;
2134
2071
  const expectedBuffer = Buffer.from(config.ADMIN_API_KEY);
2135
2072
  const providedBuffer = Buffer.from(token);
2136
2073
  return expectedBuffer.length === providedBuffer.length && timingSafeEqual(expectedBuffer, providedBuffer);
2137
2074
  }
2138
- /**
2139
- * What a dashboard user's role may do. `admin_users.role` is free text (the
2140
- * create-admin script takes any value), so only the roles known to grant
2141
- * writes do; anything else, `read_only` included, is read-only.
2142
- */
2075
+ /** `admin_users.role` is free text, so anything but a known writer role is read-only. */
2143
2076
  function sessionKeyRole(role) {
2144
2077
  return role === "superadmin" || role === "admin" ? "admin" : "read_only";
2145
2078
  }
2146
2079
  /**
2147
- * Resolves the client address, given how many proxies (TRUST_PROXY) are in
2148
- * front of the API. Each one appends the address it saw to X-Forwarded-For,
2149
- * so the client is `trustedHops` back from the connecting peer. The leftmost
2150
- * entry is the one a client can write itself, so it is used only when the
2151
- * chain is shorter than the configured hops.
2080
+ * Each trusted proxy appends the address it saw to X-Forwarded-For, so the
2081
+ * client is `trustedHops` back from the peer. Entries further left are
2082
+ * client-written.
2152
2083
  */
2153
2084
  function getClientIp(req, trustedHops) {
2154
2085
  const peer = req.socket.remoteAddress || "unknown";
@@ -2379,7 +2310,7 @@ async function startApiServer() {
2379
2310
  }
2380
2311
  }
2381
2312
  }
2382
- if (keyRole === "read_only") {
2313
+ if (keyRole === "read_only" && pathname !== "/v1/auth/logout") {
2383
2314
  if (req.method !== "GET" && req.method !== "OPTIONS") {
2384
2315
  sendJson(res, 403, {
2385
2316
  error: "forbidden",
@@ -2388,14 +2319,12 @@ async function startApiServer() {
2388
2319
  return;
2389
2320
  }
2390
2321
  }
2391
- if (!(apiRateLimiter ? await apiRateLimiter.check(projectId || "global", projectRateLimitRpm) : true)) {
2392
- if (!res.headersSent) {
2393
- res.setHeader("Retry-After", "60");
2394
- sendJson(res, 429, {
2395
- error: "too_many_requests",
2396
- message: `Project rate limit exceeded (max ${projectRateLimitRpm} req/min)`
2397
- });
2398
- }
2322
+ if (!await apiRateLimiter.check(projectId || "global", projectRateLimitRpm)) {
2323
+ res.setHeader("Retry-After", "60");
2324
+ sendJson(res, 429, {
2325
+ error: "too_many_requests",
2326
+ message: `Project rate limit exceeded (max ${projectRateLimitRpm} req/min)`
2327
+ });
2399
2328
  return;
2400
2329
  }
2401
2330
  } else if (isProjectManagement) {
@@ -1,6 +1,6 @@
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";
1
+ import { $t as STREAMS, D as ContactRepository, Dn as LRUCache, E as TemplateRepository, En as normaliseTarget, H as UserThrottle, Jt as CONSUMER_GROUPS, K as StreamProducer, On as globalEmitter, Qt as PUBSUB_CHANNELS, T as ProjectRepository, U as ProjectSettingsCache, X as IdempotencyGuard, Yt as ENRICHED_STREAMS, _ as TemplateCache, c as BaseWorker, ct as suppressions, d as startHealthReporter, f as buildUnsubscribeHeaders, fn as loadEnv, in as registry, mn as readBaseConfig, qt as buildStreamEvent, xn as DataLoader } from "./src-aiRAj2xr.mjs";
2
+ import { t as scheduleOrDispatch } from "./schedule-or-dispatch-BF85qhMh.mjs";
3
+ import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime, t as createPriorityProducers } from "./bootstrap-CHOj1rDv.mjs";
4
4
  import { randomUUID } from "node:crypto";
5
5
  import { and, eq } from "drizzle-orm";
6
6
  //#region src/services/engine/quiet-hours.ts
@@ -174,15 +174,6 @@ var EngineWorker = class extends BaseWorker {
174
174
  batchDelayMs: 2,
175
175
  maxBatchSize: 500
176
176
  });
177
- /**
178
- * Suppressed destinations for one (project, channel), as a normalised set.
179
- *
180
- * Loaded per project+channel rather than per address: a campaign is thousands
181
- * of messages against one channel, so this collapses to a single query for
182
- * the whole batch. Cached for the loader's lifetime of a tick, which means a
183
- * suppression written mid-batch takes effect on the next batch — acceptable,
184
- * since the webhook that writes it is itself minutes behind the send.
185
- */
186
177
  suppressionsCache = new LRUCache(1e3, 1e4);
187
178
  suppressionsLoader = new DataLoader(async (keys) => {
188
179
  const results = /* @__PURE__ */ new Map();
@@ -228,7 +219,7 @@ var EngineWorker = class extends BaseWorker {
228
219
  if (msUntil > 0) customTtl = config.IDEMPOTENCY_TTL_SECONDS + Math.ceil(msUntil / 1e3);
229
220
  }
230
221
  const idempotency = message.inline ? NO_IDEMPOTENCY : this.idempotency;
231
- if (!await idempotency.checkAndMark(idempotencyKey, customTtl ?? 60)) {
222
+ if (!await idempotency.checkAndMark(idempotencyKey, 60)) {
232
223
  this.logger.debug({
233
224
  messageId: message.id,
234
225
  eventId: event.id
@@ -502,7 +493,7 @@ async function startEngineWorker() {
502
493
  });
503
494
  const throttle = new UserThrottle({
504
495
  redis: redis.native,
505
- maxPerHour: parseInt(process.env.RATE_LIMIT_PER_HOUR || "100", 10)
496
+ maxPerHour: config.RATE_LIMIT_PER_HOUR
506
497
  });
507
498
  const projectRepo = new ProjectRepository(db);
508
499
  const projectSettings = new ProjectSettingsCache((projectId) => projectRepo.findThrottleSettings(projectId));
@@ -1,6 +1,6 @@
1
- import { $t as OUTBOUND_STREAMS, An as globalEmitter, D as ContactRepository, Dn as getPriorityBucket, E as TemplateRepository, H as ApiRateLimiter, J as StreamProducer, Q as IdempotencyGuard, Tn as BatchProcessor, X as metrics, Xt as CONSUMER_GROUPS, Yt as buildStreamEvent, _ as TemplateCache, at as deliveryOutbox, c as BaseWorker, d as startHealthReporter, en as PUBSUB_CHANNELS, g as transportRegistry, gn as readBaseConfig, lt as scheduledPayloads, mn as loadEnv, on as registry, tn as STREAMS, u as NonRetryableError, wn as CircuitBreaker, x as renderWithTemplate } 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
- import { n as upsertOutboxProviderIds } from "./bulk-Beblt3ny.mjs";
1
+ import { $t as STREAMS, B as ApiRateLimiter, Cn as BatchProcessor, D as ContactRepository, E as TemplateRepository, J as metrics, Jt as CONSUMER_GROUPS, K as StreamProducer, On as globalEmitter, Qt as PUBSUB_CHANNELS, Sn as CircuitBreaker, Tn as getPriorityBucket, X as IdempotencyGuard, Zt as OUTBOUND_STREAMS, _ as TemplateCache, c as BaseWorker, d as startHealthReporter, fn as loadEnv, g as transportRegistry, in as registry, mn as readBaseConfig, qt as buildStreamEvent, st as scheduledPayloads, u as NonRetryableError, x as renderWithTemplate } from "./src-aiRAj2xr.mjs";
2
+ import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime, t as createPriorityProducers } from "./bootstrap-CHOj1rDv.mjs";
3
+ import { n as upsertOutboxProviderIds } from "./bulk-CAsYwF-9.mjs";
4
4
  import { sql } from "drizzle-orm";
5
5
  //#region src/services/delivery/live-events.ts
6
6
  /**
@@ -117,30 +117,12 @@ let subscriber = null;
117
117
  let stopLiveEvents = null;
118
118
  /** How long a send may run before its dispatched log entry is published alone. */
119
119
  const DISPATCH_LOG_DELAY_MS = 1e3;
120
- /**
121
- * Wait before re-trying a send that failed at the provider, by prior failures.
122
- * The first step matches the circuit breaker's reset timeout, so a retry of a
123
- * task the open breaker turned away reaches the provider's recovery probe
124
- * rather than the same closed door.
125
- */
126
120
  const RETRY_BACKOFF_MS = [
127
121
  3e4,
128
122
  12e4,
129
123
  6e5
130
124
  ];
131
- /**
132
- * How many times a send may be put back for want of provider capacity before
133
- * it is failed. Separate from `maxAttempts`, which counts sends the provider
134
- * refused: waiting in line is not a failed attempt, and a campaign several
135
- * windows deeper than the provider's limit has to be able to wait that long.
136
- */
137
125
  const MAX_THROTTLE_DEFERRALS = 50;
138
- /**
139
- * Names the provider at `transports[i]` for its rate limit and circuit breaker.
140
- * The rate limit's budget lives in Redis and is shared by every delivery
141
- * process, so the name must come out the same everywhere: the class name,
142
- * numbered when a channel has two providers of one class.
143
- */
144
126
  function providerKey(channel, transports, i) {
145
127
  const name = transports[i].constructor?.name ?? "provider";
146
128
  const nth = transports.slice(0, i).filter((t) => t.constructor?.name === name).length;
@@ -166,7 +148,6 @@ var DeliveryWorker = class extends BaseWorker {
166
148
  db;
167
149
  eventProcessor;
168
150
  outboxUpdateProcessor;
169
- outboxInsertProcessor;
170
151
  breakers = /* @__PURE__ */ new Map();
171
152
  providerThrottle;
172
153
  templateCache;
@@ -198,40 +179,19 @@ var DeliveryWorker = class extends BaseWorker {
198
179
  await upsertOutboxProviderIds(this.db, values).catch((e) => this.logger.error({ err: e }, "background update failed"));
199
180
  return updates.map(() => void 0);
200
181
  }, flushConcurrency);
201
- this.outboxInsertProcessor = new BatchProcessor(batchSize, 5, async (tasks) => {
202
- const values = tasks.map((task) => ({
203
- taskId: task.taskId,
204
- channel: task.channel,
205
- destination: task.destination
206
- }));
207
- await this.db.insert(deliveryOutbox).values(values).onConflictDoNothing();
208
- return tasks.map(() => true);
209
- });
210
182
  }
211
183
  async stop() {
212
- await Promise.all([
213
- this.eventProcessor.flush(),
214
- this.outboxUpdateProcessor.flush(),
215
- this.outboxInsertProcessor.flush()
216
- ]);
184
+ await Promise.all([this.eventProcessor.flush(), this.outboxUpdateProcessor.flush()]);
217
185
  await super.stop();
218
186
  }
219
- /**
220
- * Renders a task the engine dispatched unrendered. Done here, after the
221
- * throttle and the lease, so the body is built once per actual send and never
222
- * travels through the outbound stream.
223
- */
187
+ /** Rendered after the throttle and lease, so once per real send. */
224
188
  async render(task) {
225
189
  if (!task.templateId) return renderWithTemplate(null, task.templateVariables);
226
190
  const template = this.templateCache ? await this.templateCache.getCachedTemplate(task.projectId, task.templateId) : null;
227
191
  if (!template) throw new NonRetryableError(`template "${task.templateId}" not found at send time`);
228
192
  return renderWithTemplate(template, task.templateVariables);
229
193
  }
230
- /**
231
- * Parks a task in Postgres and the scheduler until `sendAt`. The payload
232
- * carries the send time, so the scheduler can rebuild it if Redis loses its
233
- * data.
234
- */
194
+ /** Parks a task until `sendAt`. Postgres keeps the send time so Redis can be rebuilt. */
235
195
  async reschedule(task, sendAt, reason) {
236
196
  const scheduledAt = sendAt.toISOString();
237
197
  await this.db.insert(scheduledPayloads).values({
@@ -318,11 +278,7 @@ var DeliveryWorker = class extends BaseWorker {
318
278
  dispatchedAt
319
279
  };
320
280
  };
321
- /**
322
- * Queues the task's `notification.failed` event; once sending has started it
323
- * is the attempt's outcome. Only the non-push provider failure leaves out
324
- * `recipientId`.
325
- */
281
+ /** Queues `notification.failed`. Only the non-push provider failure omits recipientId. */
326
282
  const failed = (failureReason, failureCode, withRecipientId = true) => {
327
283
  const payload = {
328
284
  projectId: task.projectId,
@@ -450,13 +406,7 @@ var DeliveryWorker = class extends BaseWorker {
450
406
  const tDispatchStart = Date.now();
451
407
  const MAX_DISPATCH_BUDGET_MS = 25e3;
452
408
  let retryScheduled = false;
453
- /**
454
- * Re-queues a send the provider turned away, unless it said the failure is
455
- * permanent or the task is out of attempts. An outage, a timeout or an open
456
- * circuit is exactly what a later attempt can get past; failing the task
457
- * outright turned every blip into lost mail. The attempt stays in the log as
458
- * dispatched, and the task's outcome is whatever its last attempt comes to.
459
- */
409
+ /** Re-queues a failed send unless the provider called it permanent or attempts ran out. */
460
410
  const retryLater = async (failure) => {
461
411
  const maxAttempts = task.deliveryOptions?.maxAttempts ?? 3;
462
412
  if (failure.retryable === false || attemptNo >= maxAttempts) return false;
@@ -1,4 +1,4 @@
1
- import { Dn as getPriorityBucket, Yt as buildStreamEvent, lt as scheduledPayloads } from "./src-BRatuWJS.mjs";
1
+ import { Tn as getPriorityBucket, qt as buildStreamEvent, st as scheduledPayloads } from "./src-aiRAj2xr.mjs";
2
2
  import { randomUUID } from "node:crypto";
3
3
  //#region src/services/engine/schedule-or-dispatch.ts
4
4
  /**