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
@@ -1,23 +1,12 @@
1
- import { An as globalEmitter, D as ContactRepository, Dn as getPriorityBucket, E as TemplateRepository, On as normaliseTarget, Xt as CONSUMER_GROUPS, Yt as buildStreamEvent, c as BaseWorker, d as startHealthReporter, gn as readBaseConfig, k as UserRepository, lt as scheduledPayloads, mn as loadEnv, on as registry, tn as STREAMS, ut as suppressions } from "./src-BE_EUpZ6.mjs";
2
- import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime, t as createPriorityProducers } from "./bootstrap-BMDRPjgp.mjs";
1
+ import { $t as STREAMS, D as ContactRepository, E as TemplateRepository, En as normaliseTarget, Jt as CONSUMER_GROUPS, On as globalEmitter, Tn as getPriorityBucket, c as BaseWorker, ct as suppressions, d as startHealthReporter, fn as loadEnv, in as registry, it as messageLogs, k as UserRepository, mn as readBaseConfig, qt as buildStreamEvent, rt as deliveryOutbox, st as scheduledPayloads } from "./src-C38w4jR4.mjs";
2
+ import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime, t as createPriorityProducers } from "./bootstrap-Cs1XIp6b.mjs";
3
3
  import { randomUUID } from "node:crypto";
4
4
  import { and, asc, eq, gt, inArray, or, sql } from "drizzle-orm";
5
5
  //#region src/services/scheduler/send-gate.ts
6
6
  /**
7
- * Re-checks parked sends at the moment they are released.
8
- *
9
- * The engine clears a message against suppressions, opt-outs and contacts when
10
- * it first sees it. A send parked for later — a `sendAt`, a quiet-hours
11
- * deferral, a retry — can sit for hours or days, and whatever the person did in
12
- * between (unsubscribed, complained, hard-bounced, removed the address) must
13
- * still stop it. Without this the scheduler sent to them anyway.
14
- *
15
- * Reads Postgres directly rather than through the repositories' caches: this
16
- * runs once per poll, not once per message, and a stale "still subscribed" is
17
- * the very thing it exists to prevent.
18
- *
19
- * Returns the blocked tasks with the reason. Throws if a lookup fails, so the
20
- * caller holds the batch rather than sending it unchecked.
7
+ * Re-checks parked sends at release: an unsubscribe, bounce or removed address
8
+ * since parking must still stop them. Reads Postgres, not the caches, and
9
+ * throws on a failed lookup so the batch is held rather than sent unchecked.
21
10
  */
22
11
  async function findBlockedAtRelease(db, tasks) {
23
12
  const blocked = /* @__PURE__ */ new Map();
@@ -64,6 +53,35 @@ function decide(t, topics, suppressed, user, contactsByUser) {
64
53
  }
65
54
  }
66
55
  //#endregion
56
+ //#region src/services/scheduler/retention.ts
57
+ const BATCH = 5e3;
58
+ const PRUNE_LOCK_KEY = "notif:lock:scheduler:prune";
59
+ /** Deletes delivery log and outbox rows older than `retentionDays`, in batches. */
60
+ async function pruneDeliveryLogs(db, retentionDays) {
61
+ const cutoff = (/* @__PURE__ */ new Date(Date.now() - retentionDays * 864e5)).toISOString();
62
+ const tables = [[messageLogs, messageLogs.timestamp], [deliveryOutbox, deliveryOutbox.createdAt]];
63
+ let total = 0;
64
+ for (const [table, column] of tables) for (;;) {
65
+ const rows = await db.execute(sql`DELETE FROM ${table} WHERE ctid IN (SELECT ctid FROM ${table} WHERE ${column} < ${cutoff}::timestamptz LIMIT ${BATCH}) RETURNING 1`);
66
+ total += rows.length;
67
+ if (rows.length < BATCH) break;
68
+ }
69
+ return total;
70
+ }
71
+ /** Runs the prune once per hour across all scheduler processes. */
72
+ async function maybePruneDeliveryLogs(redis, db, logger, retentionDays) {
73
+ try {
74
+ if (await redis.set(PRUNE_LOCK_KEY, "1", "EX", 3600, "NX") !== "OK") return;
75
+ const deleted = await pruneDeliveryLogs(db, retentionDays);
76
+ if (deleted > 0) logger.info({
77
+ deleted,
78
+ retentionDays
79
+ }, "pruned old delivery logs");
80
+ } catch (err) {
81
+ logger.error({ err }, "delivery log prune failed");
82
+ }
83
+ }
84
+ //#endregion
67
85
  //#region src/services/scheduler/main.ts
68
86
  loadEnv();
69
87
  const config = readBaseConfig();
@@ -282,6 +300,7 @@ async function startSchedulerWorker() {
282
300
  if (!isPolling) return;
283
301
  try {
284
302
  if (!await redis.native.exists("notif:scheduler:intact")) await rebuildScheduledQueue(redis.native, db, logger);
303
+ if (config.LOG_RETENTION_DAYS) maybePruneDeliveryLogs(redis.native, db, logger, config.LOG_RETENTION_DAYS);
285
304
  const hasMore = await executeSchedulerPoll(redis.native, outboundProducers, logger, db);
286
305
  if (isPolling) pollTimeout = setTimeout(() => void pollLoop(), hasMore ? 0 : 5e3);
287
306
  } catch (err) {
@@ -1,14 +1,10 @@
1
- import { An as globalEmitter, D as ContactRepository, Dn as getPriorityBucket, E as TemplateRepository, Q as IdempotencyGuard, Qt as INBOUND_STREAMS, Xt as CONSUMER_GROUPS, Yt as buildStreamEvent, _ as TemplateCache, c as BaseWorker, d as startHealthReporter, gn as readBaseConfig, k as UserRepository, mn as loadEnv, on as registry } from "./src-BE_EUpZ6.mjs";
2
- import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime, t as createPriorityProducers } from "./bootstrap-BMDRPjgp.mjs";
1
+ import { D as ContactRepository, E as TemplateRepository, Jt as CONSUMER_GROUPS, On as globalEmitter, Tn as getPriorityBucket, X as IdempotencyGuard, Xt as INBOUND_STREAMS, _ as TemplateCache, c as BaseWorker, d as startHealthReporter, fn as loadEnv, in as registry, k as UserRepository, mn as readBaseConfig, qt as buildStreamEvent } from "./src-C38w4jR4.mjs";
2
+ import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime, t as createPriorityProducers } from "./bootstrap-Cs1XIp6b.mjs";
3
3
  import { randomUUID } from "node:crypto";
4
4
  //#region src/services/enricher/main.ts
5
5
  loadEnv();
6
6
  const config = readBaseConfig();
7
- /**
8
- * A caller's own idempotency key (X-Idempotency-Key, or a workflow step's) is a
9
- * documented 24-hour promise. Without one the key is the event's id, which only
10
- * a redelivery can bring back, so the guard's shorter TTL is enough.
11
- */
7
+ const chunkArray = (arr, size) => Array.from({ length: Math.ceil(arr.length / size) }, (_, i) => arr.slice(i * size, i * size + size));
12
8
  const CALLER_KEY_TTL_SECONDS = 86400;
13
9
  const dedupeTtl = (raw) => raw.idempotencyKey ? CALLER_KEY_TTL_SECONDS : void 0;
14
10
  let logger;
@@ -77,10 +73,8 @@ var EnricherWorker = class extends BaseWorker {
77
73
  this.flushTimer = setInterval(() => void this.flushWorkerBuffers(), 5);
78
74
  }
79
75
  /**
80
- * Hands enriched events for single-user notifications straight to `next`
81
- * instead of the enriched stream. Fan-outs still go through the stream: an
82
- * inline chain holds this worker's slot until the last send completes, and a
83
- * segment would hold one slot for thousands of them.
76
+ * Single-user notifications skip the enriched stream. Fan-outs keep it, since an
77
+ * inline chain holds this worker's slot until its last send completes.
84
78
  */
85
79
  setInlineNext(next) {
86
80
  this.inlineNext = next;
@@ -270,7 +264,6 @@ var EnricherWorker = class extends BaseWorker {
270
264
  const topics = (raw.templateId ? await this.templateCache.getCachedTemplate(raw.projectId, raw.templateId) : null)?.topics ?? [];
271
265
  const channels = raw.channels && raw.channels.length > 0 ? raw.channels : ["email"];
272
266
  const isFallback = raw.fallback === true;
273
- const chunkArray = (arr, size) => Array.from({ length: Math.ceil(arr.length / size) }, (v, i) => arr.slice(i * size, i * size + size));
274
267
  const chunks = chunkArray(userIds, 500);
275
268
  const inline = raw.target.type === "user" ? this.inlineNext : null;
276
269
  for (const chunk of chunks) {
@@ -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-BE_EUpZ6.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-C38w4jR4.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,4 +1,4 @@
1
- import { Dn as getPriorityBucket, Yt as buildStreamEvent, lt as scheduledPayloads } from "./src-BE_EUpZ6.mjs";
1
+ import { Tn as getPriorityBucket, qt as buildStreamEvent, st as scheduledPayloads } from "./src-C38w4jR4.mjs";
2
2
  import { randomUUID } from "node:crypto";
3
3
  //#region src/services/engine/schedule-or-dispatch.ts
4
4
  /**