notifkit 0.1.13 → 0.1.15

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 (35) hide show
  1. package/README.md +4 -0
  2. package/dist/{bootstrap-omGPIBny.mjs → bootstrap-BOmT69OH.mjs} +1 -1
  3. package/dist/{bulk-C4hTcnLu.mjs → bulk-ZPiOJRx-.mjs} +1 -1
  4. package/dist/index.d.mts +16 -4
  5. package/dist/index.mjs +2 -2
  6. package/dist/{main-DAXO-MiH.mjs → main-24J4U8yP.mjs} +3 -3
  7. package/dist/{main-BtnGmyaT.mjs → main-BdZvBgV7.mjs} +9 -3
  8. package/dist/{main-B5BwjIMu.mjs → main-C0m9-Eos.mjs} +58 -5
  9. package/dist/{main-DYGCl7Ja.mjs → main-C6S3EsJ3.mjs} +2 -2
  10. package/dist/{main-B-AZ-xoN.mjs → main-CCqcFDDe.mjs} +2 -2
  11. package/dist/{main-Bes11Ad3.mjs → main-CTjaZ7ZD.mjs} +2 -2
  12. package/dist/{main-BU27VjDC.mjs → main-JEXBLP2A.mjs} +2 -2
  13. package/dist/{main-BxAeC_k6.mjs → main-g33jpia4.mjs} +157 -76
  14. package/dist/{src-CIPTzq9i.mjs → src-9LXITcNi.mjs} +64 -39
  15. package/drizzle/0008_delivery_outbox_destination_idx.sql +3 -0
  16. package/drizzle/meta/_journal.json +7 -0
  17. package/package.json +3 -2
  18. package/scripts/create-admin.mjs +8 -0
  19. package/scripts/create-project.mjs +0 -0
  20. package/scripts/migrate.mjs +0 -0
  21. package/scripts/notifkit.mjs +0 -0
  22. package/src/config/index.ts +6 -3
  23. package/src/contracts/streams.ts +4 -0
  24. package/src/db/schema.ts +3 -0
  25. package/src/queue/index.ts +19 -23
  26. package/src/redis/index.ts +20 -0
  27. package/src/services/api/admin-static.ts +21 -11
  28. package/src/services/api/handlers.ts +51 -43
  29. package/src/services/api/http.ts +12 -9
  30. package/src/services/api/live-events.ts +74 -0
  31. package/src/services/api/main.ts +57 -23
  32. package/src/services/delivery/live-events.ts +68 -0
  33. package/src/services/delivery/main.ts +14 -2
  34. package/src/services/engine/main.ts +13 -1
  35. package/dashboard/README.md +0 -29
@@ -1,12 +1,14 @@
1
- import { $t as STREAMS, At as AddUserSchema, B as ApiRateLimiter, C as ContactRepository, D as TemplateRepository, Dn as LRUCache, E as SegmentRepository, En as normaliseTarget, It as NotifyRequestSchema, J as metrics, Jt as CONSUMER_GROUPS, K as StreamProducer, Lt as PreferencesSchema, Mt as ContactChannelSchema, Nt as CreateWorkflowSchema, O as UserRepository, On as globalEmitter, Q as createDatabase, Qt as PUBSUB_CHANNELS, R as RedisClient, 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 WorkflowRepository, 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, zt as SyncTemplatesSchema } from "./src-CIPTzq9i.mjs";
1
+ import { $ as createDatabase, $t as PUBSUB_CHANNELS, Bt as SyncTemplatesSchema, C as ContactRepository, D as TemplateRepository, Dn as normaliseTarget, E as SegmentRepository, En as getPriorityBucket, Ht as TriggerWorkflowSchema, J as getMetricsRegistry, Jt as buildStreamEvent, Lt as NotifyRequestSchema, Mt as BatchAddContactsSchema, Nt as ContactChannelSchema, O as UserRepository, On as LRUCache, Pt as CreateWorkflowSchema, Rt as PreferencesSchema, S as AdminUserRepository, T as ProjectRepository, V as ApiRateLimiter, Wt as UpdateUserSchema, X as createLogger, Y as metrics, Yt as CONSUMER_GROUPS, at as messageLogs, ct as scheduledPayloads, dt as users, en as STREAMS, ft as workflowDefinitions, g as transportRegistry, hn as readBaseConfig, it as deliveryOutbox, jt as AddUserSchema, k as WorkflowRepository, kn as globalEmitter, lt as suppressions, m as verifyUnsubscribeToken, ot as projectApiKeys, pn as loadEnv, q as StreamProducer, st as projects, ut as userTopicPreferences, z as RedisClient } from "./src-9LXITcNi.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";
5
5
  import { and, desc, eq, gte, inArray, isNotNull, like, lte, or, sql } from "drizzle-orm";
6
6
  import { createServer, request } from "node:http";
7
+ import { StringDecoder } from "node:string_decoder";
7
8
  import { promisify } from "node:util";
8
9
  import { createReadStream, existsSync, statSync } from "node:fs";
9
10
  import { fileURLToPath } from "node:url";
11
+ import { pipeline } from "node:stream";
10
12
  //#region src/services/api/http.ts
11
13
  async function readRawBody(req) {
12
14
  return await new Promise((resolve, reject) => {
@@ -28,21 +30,22 @@ async function readRawBody(req) {
28
30
  }
29
31
  async function readJsonBody(req) {
30
32
  return new Promise((resolve, reject) => {
31
- if (typeof req.setEncoding === "function") req.setEncoding("utf8");
33
+ const decoder = new StringDecoder("utf8");
32
34
  let body = "";
33
35
  let totalLength = 0;
34
36
  const MAX_PAYLOAD_SIZE = 5242880;
35
37
  req.on("data", (chunk) => {
36
- const str = typeof chunk === "string" ? chunk : chunk.toString("utf8");
37
- totalLength += str.length;
38
+ const bytes = typeof chunk === "string" ? Buffer.from(chunk) : chunk;
39
+ totalLength += bytes.length;
38
40
  if (totalLength > MAX_PAYLOAD_SIZE) {
39
41
  if (typeof req.destroy === "function") req.destroy();
40
42
  reject(new HttpError(413, "payload_too_large"));
41
43
  return;
42
44
  }
43
- body += str;
45
+ body += decoder.write(bytes);
44
46
  });
45
47
  req.on("end", () => {
48
+ body += decoder.end();
46
49
  const trimmed = body.trim();
47
50
  if (!trimmed) {
48
51
  resolve(void 0);
@@ -665,36 +668,34 @@ function createHandlers(deps) {
665
668
  });
666
669
  }
667
670
  async function getEventsStream(req, res, ctx) {
671
+ let open = true;
672
+ let stop;
673
+ req.on("close", () => {
674
+ open = false;
675
+ stop?.();
676
+ });
677
+ const unlisten = await deps.liveEvents.listen((e) => {
678
+ if (!ctx.projectId || e.projectId !== ctx.projectId) return;
679
+ res.write(`event: ${e.event}\n`);
680
+ const data = e.event === "delivery:delivered" ? {
681
+ taskId: e.taskId,
682
+ providerMessageId: e.providerMessageId,
683
+ channel: e.channel
684
+ } : {
685
+ taskId: e.taskId,
686
+ error: e.error,
687
+ channel: e.channel
688
+ };
689
+ res.write(`data: ${JSON.stringify(data)}\n\n`);
690
+ });
691
+ if (!open) return unlisten();
692
+ stop = unlisten;
668
693
  res.writeHead(200, {
669
694
  "Content-Type": "text/event-stream",
670
695
  "Cache-Control": "no-cache",
671
696
  Connection: "keep-alive"
672
697
  });
673
698
  res.write("retry: 10000\n\n");
674
- const onDelivered = (taskId, providerMessageId, channel, eventProjectId) => {
675
- if (eventProjectId && eventProjectId !== ctx.projectId) return;
676
- res.write(`event: delivery:delivered\n`);
677
- res.write(`data: ${JSON.stringify({
678
- taskId,
679
- providerMessageId,
680
- channel
681
- })}\n\n`);
682
- };
683
- const onFailed = (taskId, error, channel, eventProjectId) => {
684
- if (eventProjectId && eventProjectId !== ctx.projectId) return;
685
- res.write(`event: delivery:failed\n`);
686
- res.write(`data: ${JSON.stringify({
687
- taskId,
688
- error,
689
- channel
690
- })}\n\n`);
691
- };
692
- globalEmitter.on("delivery:delivered", onDelivered);
693
- globalEmitter.on("delivery:failed", onFailed);
694
- req.on("close", () => {
695
- globalEmitter.off("delivery:delivered", onDelivered);
696
- globalEmitter.off("delivery:failed", onFailed);
697
- });
698
699
  }
699
700
  async function getNotificationLogs(req, res, ctx) {
700
701
  const limit = parseLimit(ctx, 50);
@@ -831,10 +832,7 @@ function createHandlers(deps) {
831
832
  sendJson(res, 201, { name: input.name });
832
833
  } catch (error) {
833
834
  deps.logger.error({ err: error }, "Failed to create workflow definition");
834
- sendJson(res, 500, {
835
- error: "internal_error",
836
- message: error.message
837
- });
835
+ sendJson(res, 500, { error: "internal_error" });
838
836
  }
839
837
  }
840
838
  async function listTemplates(req, res, ctx) {
@@ -1163,10 +1161,11 @@ function createHandlers(deps) {
1163
1161
  stream: targetStream
1164
1162
  });
1165
1163
  } catch (err) {
1166
- sendJson(res, 500, {
1167
- error: "replay_failed",
1168
- message: err.message
1169
- });
1164
+ logger.error({
1165
+ err,
1166
+ messageId
1167
+ }, "DLQ replay failed");
1168
+ sendJson(res, 500, { error: "replay_failed" });
1170
1169
  }
1171
1170
  }
1172
1171
  async function deleteDLQMessage(_req, res, ctx) {
@@ -1180,10 +1179,11 @@ function createHandlers(deps) {
1180
1179
  await deps.redis.native.xdel(STREAMS.DEAD_LETTER, messageId);
1181
1180
  sendJson(res, 200, { success: true });
1182
1181
  } catch (err) {
1183
- sendJson(res, 500, {
1184
- error: "delete_failed",
1185
- message: err.message
1186
- });
1182
+ logger.error({
1183
+ err,
1184
+ messageId
1185
+ }, "DLQ delete failed");
1186
+ sendJson(res, 500, { error: "delete_failed" });
1187
1187
  }
1188
1188
  }
1189
1189
  async function getScheduledMessages(_req, res, ctx) {
@@ -1212,9 +1212,8 @@ function createHandlers(deps) {
1212
1212
  const contactTargets = contacts.map((c) => c.target);
1213
1213
  let logs = [];
1214
1214
  if (contactTargets.length > 0) {
1215
- const outboxRows = await deps.db.select({ taskId: deliveryOutbox.taskId }).from(deliveryOutbox).where(inArray(deliveryOutbox.destination, contactTargets));
1216
- const taskIds = Array.from(new Set(outboxRows.map((r) => r.taskId)));
1217
- if (taskIds.length > 0) logs = await deps.db.select().from(messageLogs).where(and(eq(messageLogs.projectId, ctx.projectId), inArray(messageLogs.taskId, taskIds))).orderBy(desc(messageLogs.timestamp)).limit(50);
1215
+ const sentTo = deps.db.select({ taskId: deliveryOutbox.taskId }).from(deliveryOutbox).where(inArray(deliveryOutbox.destination, contactTargets));
1216
+ logs = await deps.db.select().from(messageLogs).where(and(eq(messageLogs.projectId, ctx.projectId), inArray(messageLogs.taskId, sentTo))).orderBy(desc(messageLogs.timestamp)).limit(50);
1218
1217
  }
1219
1218
  sendJson(res, 200, {
1220
1219
  ...user,
@@ -1473,7 +1472,9 @@ code{background:#f4f4f5;padding:.1rem .35rem;border-radius:4px}</style>
1473
1472
  }
1474
1473
  const { identifier, password } = parsed.data;
1475
1474
  const user = await deps.adminUserRepo.findByEmailOrUsername(identifier);
1476
- const isValid = await verifyPassword(password, user ? user.passwordHash : DUMMY_PASSWORD_HASH);
1475
+ const targetHash = user ? user.passwordHash : DUMMY_PASSWORD_HASH;
1476
+ let isValid = await verifyPassword(password, targetHash);
1477
+ if (!isValid && password.trim() !== password && password.trim()) isValid = await verifyPassword(password.trim(), targetHash);
1477
1478
  if (!user || !isValid) {
1478
1479
  sendJson(res, 401, {
1479
1480
  error: "unauthorized",
@@ -1667,10 +1668,10 @@ async function handleAdminRequest(req, res, url) {
1667
1668
  res.end();
1668
1669
  return true;
1669
1670
  }
1670
- if (process.env.NODE_ENV !== "production") {
1671
- const devProxyUrl = process.env.VITE_DEV_URL || process.env.ADMIN_DEV_URL || process.env.NEXT_DEV_URL;
1672
- const targetPort = devProxyUrl ? parseInt(new URL(devProxyUrl).port, 10) : 5173;
1673
- if (await proxyToDevServer(req, res, devProxyUrl ? new URL(devProxyUrl).hostname : "127.0.0.1", targetPort)) return true;
1671
+ const devProxyUrl = process.env.VITE_DEV_URL || process.env.ADMIN_DEV_URL || process.env.NEXT_DEV_URL;
1672
+ if (devProxyUrl && process.env.NODE_ENV !== "production") {
1673
+ const target = new URL(devProxyUrl);
1674
+ if (await proxyToDevServer(req, res, target.hostname, parseInt(target.port, 10) || 80)) return true;
1674
1675
  }
1675
1676
  const dashboardDir = findDashboardDir();
1676
1677
  if (!dashboardDir) {
@@ -1694,7 +1695,7 @@ async function handleAdminRequest(req, res, url) {
1694
1695
  <p>The dashboard static bundle has not been built yet.</p>
1695
1696
  <p>Please build it by running:</p>
1696
1697
  <p><code>npm run build:dashboard</code></p>
1697
- <p>or run the dev server with <code>npm --prefix dashboard run dev</code>.</p>
1698
+ <p>or run the dev server with <code>npm --prefix dashboard run dev</code> and start notifkit with <code>VITE_DEV_URL=http://127.0.0.1:5173</code>.</p>
1698
1699
  </div>
1699
1700
  </body>
1700
1701
  </html>
@@ -1751,10 +1752,68 @@ async function handleAdminRequest(req, res, url) {
1751
1752
  "Content-Length": stat.size,
1752
1753
  "Cache-Control": cacheControl
1753
1754
  });
1754
- createReadStream(filePath).pipe(res);
1755
+ pipeline(createReadStream(filePath), res, () => {});
1755
1756
  return true;
1756
1757
  }
1757
1758
  //#endregion
1759
+ //#region src/services/api/live-events.ts
1760
+ /**
1761
+ * Relays delivery outcomes published by delivery workers (in this process or
1762
+ * any other) to the API's open event streams.
1763
+ *
1764
+ * Holds the Redis subscription only while at least one stream is open: the
1765
+ * delivery side stops publishing when nobody is subscribed, so an idle API
1766
+ * costs the send path nothing.
1767
+ */
1768
+ var RedisLiveDeliveryEvents = class {
1769
+ redis;
1770
+ logger;
1771
+ subscriber = null;
1772
+ listeners = /* @__PURE__ */ new Set();
1773
+ subscribed = null;
1774
+ constructor(redis, logger) {
1775
+ this.redis = redis;
1776
+ this.logger = logger;
1777
+ }
1778
+ async listen(listener) {
1779
+ this.listeners.add(listener);
1780
+ if (!this.subscribed) this.subscribed = this.subscribe();
1781
+ await this.subscribed;
1782
+ return () => {
1783
+ if (!this.listeners.delete(listener) || this.listeners.size > 0) return;
1784
+ this.subscribed = null;
1785
+ this.subscriber?.unsubscribe(PUBSUB_CHANNELS.DELIVERY_EVENTS).catch(() => {});
1786
+ };
1787
+ }
1788
+ async subscribe() {
1789
+ if (!this.subscriber) {
1790
+ this.subscriber = this.redis.duplicate({ enableAutoPipelining: false });
1791
+ this.subscriber.on("message", (_channel, raw) => {
1792
+ let event;
1793
+ try {
1794
+ event = JSON.parse(raw);
1795
+ } catch {
1796
+ return;
1797
+ }
1798
+ for (const listener of this.listeners) listener(event);
1799
+ });
1800
+ }
1801
+ try {
1802
+ await this.subscriber.subscribe(PUBSUB_CHANNELS.DELIVERY_EVENTS);
1803
+ await this.redis.publish(PUBSUB_CHANNELS.DELIVERY_EVENTS_WANTED, "1");
1804
+ } catch (err) {
1805
+ this.subscribed = null;
1806
+ this.logger?.warn({ err }, "could not subscribe to live delivery events");
1807
+ }
1808
+ }
1809
+ async close() {
1810
+ this.listeners.clear();
1811
+ this.subscribed = null;
1812
+ this.subscriber?.disconnect();
1813
+ this.subscriber = null;
1814
+ }
1815
+ };
1816
+ //#endregion
1758
1817
  //#region src/services/api/main.ts
1759
1818
  /** Pub/sub channel used to drop a cached API key across every API process. */
1760
1819
  const API_KEY_INVALIDATION_CHANNEL = "apikey.invalidated";
@@ -1774,6 +1833,7 @@ let authCache = new LRUCache(1e3, AUTH_CACHE_TTL_MS);
1774
1833
  let tokenHashToToken = /* @__PURE__ */ new Map();
1775
1834
  let authSubscriber = null;
1776
1835
  let router;
1836
+ let liveEvents = null;
1777
1837
  let apiRateLimiter;
1778
1838
  function extractAuthToken(req) {
1779
1839
  const authHeader = req.headers["authorization"];
@@ -1782,17 +1842,26 @@ function extractAuthToken(req) {
1782
1842
  if (typeof apiKeyHeader === "string") return apiKeyHeader.trim();
1783
1843
  }
1784
1844
  /**
1785
- * Resolves client IP address, respecting reverse proxy headers only when TRUST_PROXY is enabled.
1845
+ * What a dashboard user's role may do. `admin_users.role` is free text (the
1846
+ * create-admin script takes any value), so only the roles known to grant
1847
+ * writes do; anything else, `read_only` included, is read-only.
1786
1848
  */
1787
- function getClientIp(req, trustProxy) {
1788
- if (trustProxy) {
1789
- const forwarded = req.headers["x-forwarded-for"];
1790
- if (typeof forwarded === "string") {
1791
- const first = forwarded.split(",")[0]?.trim();
1792
- if (first) return first;
1793
- }
1794
- }
1795
- return req.socket.remoteAddress || "unknown";
1849
+ function sessionKeyRole(role) {
1850
+ return role === "superadmin" || role === "admin" ? "admin" : "read_only";
1851
+ }
1852
+ /**
1853
+ * Resolves the client address, given how many proxies (TRUST_PROXY) are in
1854
+ * front of the API. Each one appends the address it saw to X-Forwarded-For,
1855
+ * so the client is `trustedHops` back from the connecting peer. The leftmost
1856
+ * entry is the one a client can write itself, so it is used only when the
1857
+ * chain is shorter than the configured hops.
1858
+ */
1859
+ function getClientIp(req, trustedHops) {
1860
+ const peer = req.socket.remoteAddress || "unknown";
1861
+ const forwarded = req.headers["x-forwarded-for"];
1862
+ if (trustedHops <= 0 || typeof forwarded !== "string") return peer;
1863
+ const chain = [...forwarded.split(",").map((s) => s.trim()).filter(Boolean), peer];
1864
+ return chain[Math.max(chain.length - 1 - trustedHops, 0)];
1796
1865
  }
1797
1866
  async function handleCreateProject(req, res) {
1798
1867
  const parsed = z.object({ name: z.string().min(1) }).safeParse(await readJsonBody(req));
@@ -1986,9 +2055,11 @@ async function startApiServer() {
1986
2055
  } catch (err) {
1987
2056
  logger.error({ err }, "Failed to bootstrap default admin user");
1988
2057
  }
2058
+ liveEvents = new RedisLiveDeliveryEvents(redis.native, logger);
1989
2059
  deps = {
1990
2060
  logger,
1991
2061
  redis,
2062
+ liveEvents,
1992
2063
  producers,
1993
2064
  userRepo: new UserRepository(db),
1994
2065
  contactRepo: new ContactRepository(db),
@@ -2057,7 +2128,7 @@ async function startApiServer() {
2057
2128
  const session = await getAdminSession(redis.native, token);
2058
2129
  if (session) {
2059
2130
  isAdminToken = true;
2060
- keyRole = session.role === "superadmin" ? "admin" : session.role;
2131
+ keyRole = sessionKeyRole(session.role);
2061
2132
  }
2062
2133
  } else if (config.ADMIN_API_KEY) {
2063
2134
  const expectedBuffer = Buffer.from(config.ADMIN_API_KEY);
@@ -2146,7 +2217,17 @@ async function startApiServer() {
2146
2217
  }
2147
2218
  let authorized = false;
2148
2219
  if (token.startsWith("nk_sess_")) {
2149
- if (await getAdminSession(redis.native, token)) authorized = true;
2220
+ const session = await getAdminSession(redis.native, token);
2221
+ if (session) {
2222
+ if (sessionKeyRole(session.role) === "read_only" && req.method !== "GET") {
2223
+ sendJson(res, 403, {
2224
+ error: "forbidden",
2225
+ message: "Session is read-only"
2226
+ });
2227
+ return;
2228
+ }
2229
+ authorized = true;
2230
+ }
2150
2231
  } else if (config.ADMIN_API_KEY) {
2151
2232
  const expectedBuffer = Buffer.from(config.ADMIN_API_KEY);
2152
2233
  const providedBuffer = Buffer.from(token);
@@ -2162,12 +2243,10 @@ async function startApiServer() {
2162
2243
  isAdminToken = true;
2163
2244
  }
2164
2245
  if (isPublicUnsubscribe) {
2165
- const clientIp = getClientIp(req, Boolean(config.TRUST_PROXY));
2246
+ const clientIp = getClientIp(req, config.TRUST_PROXY);
2166
2247
  try {
2167
2248
  const key = `rate-limit:api:unsub:${clientIp}`;
2168
- const count = await redis.native.incr(key);
2169
- if (count === 1) await redis.native.expire(key, 60);
2170
- if (count > 60) {
2249
+ if (await redis.native.countRequest(key, 60) > 60) {
2171
2250
  res.setHeader("Retry-After", "60");
2172
2251
  sendJson(res, 429, { error: "too_many_requests" });
2173
2252
  return;
@@ -2177,12 +2256,10 @@ async function startApiServer() {
2177
2256
  }
2178
2257
  }
2179
2258
  if (isPublicAuth) {
2180
- const clientIp = getClientIp(req, Boolean(config.TRUST_PROXY));
2259
+ const clientIp = getClientIp(req, config.TRUST_PROXY);
2181
2260
  try {
2182
2261
  const key = `rate-limit:api:auth:${clientIp}`;
2183
- const count = await redis.native.incr(key);
2184
- if (count === 1) await redis.native.expire(key, 60);
2185
- if (count > 10) {
2262
+ if (await redis.native.countRequest(key, 60) > 10) {
2186
2263
  res.setHeader("Retry-After", "60");
2187
2264
  sendJson(res, 429, {
2188
2265
  error: "too_many_requests",
@@ -2268,6 +2345,7 @@ async function startApiServer() {
2268
2345
  }
2269
2346
  if (transport.parseWebhook) {
2270
2347
  router.post(transport.webhookPath, async (req, res) => {
2348
+ let deadline;
2271
2349
  try {
2272
2350
  await Promise.race([(async () => {
2273
2351
  const rawBody = await readRawBody(req);
@@ -2350,16 +2428,17 @@ async function startApiServer() {
2350
2428
  }, "suppression events carried no recipient address — transport must set WebhookEvent.recipient");
2351
2429
  }
2352
2430
  if (!res.headersSent) sendJson(res, 200, { success: true });
2353
- })(), new Promise((_, reject) => setTimeout(() => reject(/* @__PURE__ */ new Error("Webhook processing timeout")), 15e3))]);
2431
+ })(), new Promise((_, reject) => {
2432
+ deadline = setTimeout(() => reject(/* @__PURE__ */ new Error("Webhook processing timeout")), 15e3);
2433
+ })]);
2354
2434
  } catch (err) {
2355
2435
  logger.error({
2356
2436
  err,
2357
2437
  channel
2358
2438
  }, "webhook processing error or timeout");
2359
- if (!res.headersSent) sendJson(res, 500, {
2360
- error: "internal_error",
2361
- message: err.message
2362
- });
2439
+ if (!res.headersSent) sendJson(res, 500, { error: "internal_error" });
2440
+ } finally {
2441
+ clearTimeout(deadline);
2363
2442
  }
2364
2443
  });
2365
2444
  logger.info(`Mounted webhook for ${channel} at ${transport.webhookPath}`);
@@ -2386,6 +2465,8 @@ async function stopApiServer() {
2386
2465
  authSubscriber.disconnect();
2387
2466
  authSubscriber = null;
2388
2467
  }
2468
+ await liveEvents?.close();
2469
+ liveEvents = null;
2389
2470
  authCache = new LRUCache(1e3, AUTH_CACHE_TTL_MS);
2390
2471
  tokenHashToToken.clear();
2391
2472
  cachedHealth = null;
@@ -397,10 +397,13 @@ const baseConfigSchema = z.object({
397
397
  */
398
398
  UNSUBSCRIBE_SECRET: z.string().min(16).optional(),
399
399
  /**
400
- * When true, trusts X-Forwarded-For headers from reverse proxies for client IP resolution.
401
- * Defaults to false to prevent client-spoofed headers from bypassing rate limits.
400
+ * How many reverse proxies sit in front of the API, for reading the client
401
+ * address out of X-Forwarded-For. `true` means one. Each proxy appends the
402
+ * address it saw, so the client is that many entries from the right; the
403
+ * entries further left are whatever the client sent. Defaults to 0, which
404
+ * ignores the header so it cannot be used to get around rate limits.
402
405
  */
403
- TRUST_PROXY: z.string().optional().transform((v) => v === "true" || v === "1"),
406
+ TRUST_PROXY: z.string().optional().transform((v) => v === "true" ? 1 : v && /^\d+$/.test(v) ? Number(v) : 0),
404
407
  /**
405
408
  * Comma-separated list of allowed origins for CORS on session-authenticated admin routes.
406
409
  */
@@ -567,7 +570,11 @@ const PUBSUB_CHANNELS = {
567
570
  /** Payload: `{projectId}`. Published when project settings change. */
568
571
  PROJECT_INVALIDATED: "project.invalidated",
569
572
  /** Payload: a token hash, or `*` for the whole cache. */
570
- API_KEY_INVALIDATED: "apikey.invalidated"
573
+ API_KEY_INVALIDATED: "apikey.invalidated",
574
+ /** Delivery outcomes for GET /v1/events/stream; see services/delivery/live-events. */
575
+ DELIVERY_EVENTS: "delivery.events",
576
+ /** An API began listening on DELIVERY_EVENTS: delivery workers resume publishing. */
577
+ DELIVERY_EVENTS_WANTED: "delivery.events.wanted"
571
578
  };
572
579
  const CONSUMER_GROUPS = {
573
580
  ENRICHER: "notifkit:group:enricher",
@@ -1280,11 +1287,14 @@ const deliveryOutbox = pgTable("delivery_outbox", {
1280
1287
  destination: text("destination").notNull(),
1281
1288
  providerMessageId: varchar("provider_message_id"),
1282
1289
  createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull()
1283
- }, (table) => ({ pk: primaryKey({ columns: [
1284
- table.taskId,
1285
- table.channel,
1286
- table.destination
1287
- ] }) }));
1290
+ }, (table) => ({
1291
+ pk: primaryKey({ columns: [
1292
+ table.taskId,
1293
+ table.channel,
1294
+ table.destination
1295
+ ] }),
1296
+ destinationIdx: index("delivery_outbox_destination_idx").on(table.destination)
1297
+ }));
1288
1298
  const scheduledPayloads = pgTable("scheduled_payloads", {
1289
1299
  taskId: varchar("task_id").primaryKey(),
1290
1300
  payload: jsonb("payload").notNull(),
@@ -1672,11 +1682,7 @@ var StreamProducer = class {
1672
1682
  try {
1673
1683
  if (typeof this.redis.pipeline === "function") {
1674
1684
  const pipeline = this.redis.pipeline();
1675
- for (let i = 0; i < batch.length; i++) {
1676
- const item = batch[i];
1677
- if (i === batch.length - 1) pipeline.xadd(this.stream, "MAXLEN", "~", String(this.maxLen), "*", "data", JSON.stringify(item.event));
1678
- else pipeline.xadd(this.stream, "*", "data", JSON.stringify(item.event));
1679
- }
1685
+ for (const item of batch) pipeline.xadd(this.stream, "*", "data", JSON.stringify(item.event));
1680
1686
  const results = await pipeline.exec();
1681
1687
  if (!results) throw new Error(`Pipeline execution failed for ${this.stream}`);
1682
1688
  for (let i = 0; i < results.length; i++) {
@@ -1687,7 +1693,7 @@ var StreamProducer = class {
1687
1693
  } else for (let i = 0; i < batch.length; i++) {
1688
1694
  const item = batch[i];
1689
1695
  try {
1690
- const msgId = await this.redis.xadd(this.stream, "MAXLEN", "~", String(this.maxLen), "*", "data", JSON.stringify(item.event));
1696
+ const msgId = await this.redis.xadd(this.stream, "*", "data", JSON.stringify(item.event));
1691
1697
  if (!msgId) throw new Error(`XADD to ${this.stream} returned null`);
1692
1698
  item.resolve(msgId);
1693
1699
  } catch (err) {
@@ -1710,7 +1716,7 @@ var StreamProducer = class {
1710
1716
  stream,
1711
1717
  len,
1712
1718
  maxLen: this.maxLen
1713
- }, "stream is nearing MAXLEN limit (80%+)");
1719
+ }, "stream backlog is past 80% of QUEUE_MAX_LEN — consumers are falling behind");
1714
1720
  const dlqLen = await this.redis.xlen(STREAMS.DEAD_LETTER);
1715
1721
  metrics.queueSize.set({ stream: STREAMS.DEAD_LETTER }, dlqLen);
1716
1722
  } catch (err) {
@@ -1804,8 +1810,10 @@ var StreamConsumer = class {
1804
1810
  })]);
1805
1811
  } catch (err) {
1806
1812
  if (err.message === "XREADGROUP_TIMEOUT_DEAD_CONNECTION") {
1807
- this.logger?.warn("XREADGROUP took too long, assuming dead connection. Disconnecting...");
1808
- this.blockingRedis.disconnect();
1813
+ this.logger?.warn("XREADGROUP took too long, assuming dead connection. Reconnecting...");
1814
+ const dead = this.blockingRedis;
1815
+ this.blockingRedis = this.redis.duplicate({ enableAutoPipelining: false });
1816
+ dead.disconnect();
1809
1817
  throw err;
1810
1818
  }
1811
1819
  throw err;
@@ -1871,7 +1879,7 @@ var StreamConsumer = class {
1871
1879
  /**
1872
1880
  * Acks and then deletes the entries. Every stream is read by exactly one
1873
1881
  * consumer group, so an acked entry is never read again; leaving it in place
1874
- * holds Redis memory until MAXLEN trims it, and a group recreated from "0"
1882
+ * holds Redis memory for good, and a group recreated from "0"
1875
1883
  * (ensureGroup) would process it a second time. Both commands are issued
1876
1884
  * before either is awaited, so auto-pipelining sends them in one round trip,
1877
1885
  * and Redis runs them in order.
@@ -2360,6 +2368,19 @@ const LUA_SCHEDULER_POLL = `
2360
2368
  end
2361
2369
  return tasks
2362
2370
  `;
2371
+ /**
2372
+ * Fixed-window request counter: INCR, and an expiry whenever the key has none.
2373
+ * One script so the two cannot come apart. With INCR and EXPIRE sent
2374
+ * separately, a failure between them left a counter with no expiry, and the
2375
+ * address it counted stayed over its limit for good.
2376
+ */
2377
+ const LUA_COUNT_REQUEST = `
2378
+ local count = redis.call('INCR', KEYS[1])
2379
+ if redis.call('TTL', KEYS[1]) < 0 then
2380
+ redis.call('EXPIRE', KEYS[1], ARGV[1])
2381
+ end
2382
+ return count
2383
+ `;
2363
2384
  /** Two-phase idempotency lease acquisition. Checks sent/legacy, then attempts SET NX lock. */
2364
2385
  const LUA_ACQUIRE_LEASE = `
2365
2386
  if redis.call('EXISTS', KEYS[1]) == 1 or redis.call('EXISTS', KEYS[3]) == 1 then
@@ -2411,6 +2432,10 @@ function registerCustomCommands(redis) {
2411
2432
  numberOfKeys: 3,
2412
2433
  lua: LUA_ACQUIRE_LEASE
2413
2434
  });
2435
+ redis.defineCommand("countRequest", {
2436
+ numberOfKeys: 1,
2437
+ lua: LUA_COUNT_REQUEST
2438
+ });
2414
2439
  redis.defineCommand("markProcessed", {
2415
2440
  numberOfKeys: 3,
2416
2441
  lua: LUA_MARK_PROCESSED
@@ -4169,35 +4194,35 @@ var NotifkitServer = class extends EventEmitter {
4169
4194
  }
4170
4195
  const startupPromises = [];
4171
4196
  if (services.includes("api")) {
4172
- const { startApiServer } = await import("./main-BxAeC_k6.mjs");
4197
+ const { startApiServer } = await import("./main-g33jpia4.mjs");
4173
4198
  startupPromises.push(startApiServer());
4174
4199
  }
4175
4200
  if (services.includes("delivery")) {
4176
- const { startDeliveryWorker } = await import("./main-B5BwjIMu.mjs");
4201
+ const { startDeliveryWorker } = await import("./main-C0m9-Eos.mjs");
4177
4202
  startupPromises.push(startDeliveryWorker());
4178
4203
  }
4179
4204
  if (services.includes("engine")) {
4180
- const { startEngineWorker } = await import("./main-BtnGmyaT.mjs");
4205
+ const { startEngineWorker } = await import("./main-BdZvBgV7.mjs");
4181
4206
  startupPromises.push(startEngineWorker());
4182
4207
  }
4183
4208
  if (services.includes("enricher")) {
4184
- const { startEnricherWorker } = await import("./main-BU27VjDC.mjs");
4209
+ const { startEnricherWorker } = await import("./main-JEXBLP2A.mjs");
4185
4210
  startupPromises.push(startEnricherWorker());
4186
4211
  }
4187
4212
  if (services.includes("scheduler")) {
4188
- const { startSchedulerWorker } = await import("./main-Bes11Ad3.mjs");
4213
+ const { startSchedulerWorker } = await import("./main-CTjaZ7ZD.mjs");
4189
4214
  startupPromises.push(startSchedulerWorker());
4190
4215
  }
4191
4216
  if (services.includes("ai")) {
4192
- const { startAiWorker } = await import("./main-B-AZ-xoN.mjs");
4217
+ const { startAiWorker } = await import("./main-CCqcFDDe.mjs");
4193
4218
  startupPromises.push(startAiWorker());
4194
4219
  }
4195
4220
  if (services.includes("workflow")) {
4196
- const { startWorkflowWorker } = await import("./main-DYGCl7Ja.mjs");
4221
+ const { startWorkflowWorker } = await import("./main-C6S3EsJ3.mjs");
4197
4222
  startupPromises.push(startWorkflowWorker());
4198
4223
  }
4199
4224
  if (services.includes("events")) {
4200
- const { startEventWorker } = await import("./main-DAXO-MiH.mjs");
4225
+ const { startEventWorker } = await import("./main-24J4U8yP.mjs");
4201
4226
  startupPromises.push(startEventWorker());
4202
4227
  }
4203
4228
  const handleSignal = async (signal) => {
@@ -4219,9 +4244,9 @@ var NotifkitServer = class extends EventEmitter {
4219
4244
  await Promise.all(startupPromises);
4220
4245
  if (readBaseConfig().PIPELINE_FUSED && services.includes("enricher") && services.includes("engine") && services.includes("delivery")) {
4221
4246
  const [{ getEnricherWorker }, { getEngineWorker }, { getDeliveryWorker }] = await Promise.all([
4222
- import("./main-BU27VjDC.mjs"),
4223
- import("./main-BtnGmyaT.mjs"),
4224
- import("./main-B5BwjIMu.mjs")
4247
+ import("./main-JEXBLP2A.mjs"),
4248
+ import("./main-BdZvBgV7.mjs"),
4249
+ import("./main-C0m9-Eos.mjs")
4225
4250
  ]);
4226
4251
  const enricher = getEnricherWorker();
4227
4252
  const engine = getEngineWorker();
@@ -4252,14 +4277,14 @@ var NotifkitServer = class extends EventEmitter {
4252
4277
  "events"
4253
4278
  ] : this.options.services;
4254
4279
  const shutdownOrder = [
4255
- ["api", async () => (await import("./main-BxAeC_k6.mjs")).stopApiServer()],
4256
- ["workflow", async () => (await import("./main-DYGCl7Ja.mjs")).stopWorkflowWorker()],
4257
- ["enricher", async () => (await import("./main-BU27VjDC.mjs")).stopEnricherWorker()],
4258
- ["ai", async () => (await import("./main-B-AZ-xoN.mjs")).stopAiWorker()],
4259
- ["engine", async () => (await import("./main-BtnGmyaT.mjs")).stopEngineWorker()],
4260
- ["scheduler", async () => (await import("./main-Bes11Ad3.mjs")).stopSchedulerWorker()],
4261
- ["delivery", async () => (await import("./main-B5BwjIMu.mjs")).stopDeliveryWorker()],
4262
- ["events", async () => (await import("./main-DAXO-MiH.mjs")).stopEventWorker()]
4280
+ ["api", async () => (await import("./main-g33jpia4.mjs")).stopApiServer()],
4281
+ ["workflow", async () => (await import("./main-C6S3EsJ3.mjs")).stopWorkflowWorker()],
4282
+ ["enricher", async () => (await import("./main-JEXBLP2A.mjs")).stopEnricherWorker()],
4283
+ ["ai", async () => (await import("./main-CCqcFDDe.mjs")).stopAiWorker()],
4284
+ ["engine", async () => (await import("./main-BdZvBgV7.mjs")).stopEngineWorker()],
4285
+ ["scheduler", async () => (await import("./main-CTjaZ7ZD.mjs")).stopSchedulerWorker()],
4286
+ ["delivery", async () => (await import("./main-C0m9-Eos.mjs")).stopDeliveryWorker()],
4287
+ ["events", async () => (await import("./main-24J4U8yP.mjs")).stopEventWorker()]
4263
4288
  ];
4264
4289
  for (const [name, stop] of shutdownOrder) if (services.includes(name)) await stop();
4265
4290
  if (this.pgContainer) {
@@ -4273,4 +4298,4 @@ var NotifkitServer = class extends EventEmitter {
4273
4298
  }
4274
4299
  };
4275
4300
  //#endregion
4276
- export { migrateDatabase as $, STREAMS as $t, LUA_ACQUIRE_LEASE as A, AddUserSchema as At, ApiRateLimiter as B, TemplateSchema as Bt, ContactRepository as C, BatchProcessor as Cn, NotificationScheduledPayloadSchema as Ct, TemplateRepository as D, LRUCache as Dn, NotificationRequestedPayloadSchema as Dt, SegmentRepository as E, normaliseTarget as En, NotificationCreatedPayloadSchema as Et, LUA_SCHEDULER_POLL as F, InlineUserSchema as Ft, StreamConsumer as G, WorkflowNotifyPayloadSchema as Gt, UserThrottle as H, UpdateProjectSchema as Ht, LUA_USER_THROTTLE as I, NotifyRequestSchema as It, metrics as J, CONSUMER_GROUPS as Jt, StreamProducer as K, WorkflowStepSchema as Kt, Redis as L, PreferencesSchema as Lt, LUA_MARK_PROCESSED as M, ContactChannelSchema as Mt, LUA_RELEASE_LOCK as N, CreateWorkflowSchema as Nt, UserRepository as O, globalEmitter as On, NotificationTargetSchema as Ot, LUA_RENEW_LOCK as P, IngestEventSchema as Pt, createDatabase as Q, PUBSUB_CHANNELS as Qt, RedisClient as R, QuietHoursSchema as Rt, AdminUserRepository as S, CircuitBreaker as Sn, RenderedContentSchema as St, ProjectRepository as T, getPriorityBucket as Tn, RecipientProfileSchema as Tt, calculateTokenBatchSize as U, UpdateUserSchema as Ut, ProjectSettingsCache 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, WorkflowRepository 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, PreferenceRepository 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 };
4301
+ export { createDatabase as $, PUBSUB_CHANNELS as $t, LUA_ACQUIRE_LEASE as A, AddContactSchema as At, registerCustomCommands as B, SyncTemplatesSchema as Bt, ContactRepository as C, CircuitBreaker as Cn, RenderedContentSchema as Ct, TemplateRepository as D, normaliseTarget as Dn, NotificationCreatedPayloadSchema as Dt, SegmentRepository as E, getPriorityBucket as En, RecipientProfileSchema as Et, LUA_RENEW_LOCK as F, IngestEventSchema as Ft, PendingMessageScanner as G, UserContactInputSchema as Gt, ProjectSettingsCache as H, TriggerWorkflowSchema as Ht, LUA_SCHEDULER_POLL as I, InlineUserSchema as It, getMetricsRegistry as J, buildStreamEvent as Jt, StreamConsumer as K, WorkflowNotifyPayloadSchema as Kt, LUA_USER_THROTTLE as L, NotifyRequestSchema as Lt, LUA_LEASE_API_RATE_LIMIT as M, BatchAddContactsSchema as Mt, LUA_MARK_PROCESSED as N, ContactChannelSchema as Nt, UserRepository as O, LRUCache as On, NotificationRequestedPayloadSchema as Ot, LUA_RELEASE_LOCK as P, CreateWorkflowSchema as Pt, assertTablesInSchema as Q, OUTBOUND_STREAMS as Qt, Redis as R, PreferencesSchema as Rt, AdminUserRepository as S, DataLoader as Sn, NotificationDispatchedPayloadSchema as St, ProjectRepository as T, AsyncSemaphore as Tn, NotificationEnrichedPayloadSchema as Tt, UserThrottle as U, UpdateProjectSchema as Ut, ApiRateLimiter as V, TemplateSchema as Vt, calculateTokenBatchSize as W, UpdateUserSchema as Wt, createLogger as X, ENRICHED_STREAMS as Xt, metrics as Y, CONSUMER_GROUPS as Yt, IdempotencyGuard as Z, INBOUND_STREAMS as Zt, TemplateCache as _, setGlobalConfig as _n, NotificationCanceledPayloadSchema as _t, SuspendExecutionError as a, registry as an, messageLogs as at, interpolate as b, generateId as bn, NotificationDeliveredPayloadSchema as bt, BaseWorker as c, NotificationPrioritySchema as cn, scheduledPayloads as ct, startHealthReporter as d, baseConfigSchema as dn, users as dt, STREAMS as en, migrateDatabase as et, buildUnsubscribeHeaders as f, getAiConfig as fn, workflowDefinitions as ft, transportRegistry as g, setAiConfig as gn, NotificationAiPendingPayloadSchema as gt, registerTransport as h, readBaseConfig as hn, workflowWaiters as ht, workflowRegistry as i, EventRegistry as in, deliveryOutbox as it, LUA_COUNT_REQUEST as j, AddUserSchema as jt, WorkflowRepository as k, globalEmitter as kn, NotificationTargetSchema as kt, LockHeldError as l, NotificationStatusSchema as ln, suppressions as lt, verifyUnsubscribeToken as m, parseConfig as mn, workflowSteps as mt, NotifkitClient as n, StreamEventMetadataSchema as nn, qualifyMigrationSql as nt, buildStepNotifyPayload as o, EventMetadataSchema as on, projectApiKeys as ot, signUnsubscribeToken as p, loadEnv as pn, workflowInstances as pt, StreamProducer as q, WorkflowStepSchema as qt, workflow as r, StreamEventSchema as rn, runMigrations as rt, resolveStepTarget as s, NotificationChannelSchema as sn, projects as st, NotifkitServer as t, EventEnvelopeSchema as tn, notifkitTableNames as tt, NonRetryableError as u, AI_DEFAULTS as un, userTopicPreferences as ut, escapeHeader as v, AppError as vn, NotificationSkippedPayloadSchema as vt, PreferenceRepository as w, BatchProcessor as wn, NotificationScheduledPayloadSchema as wt, renderWithTemplate as x, sleep as xn, DeliveryOptionsSchema as xt, escapeHtml as y, ValidationError as yn, NotificationFailedPayloadSchema as yt, RedisClient as z, QuietHoursSchema as zt };
@@ -0,0 +1,3 @@
1
+ -- delivery_outbox's key leads with task_id, so GET /v1/users/:id/details,
2
+ -- which looks up the sends to a user's addresses, scanned the whole table.
3
+ CREATE INDEX IF NOT EXISTS "delivery_outbox_destination_idx" ON "delivery_outbox" USING btree ("destination");