notifkit 0.1.12 → 0.1.14

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 (34) hide show
  1. package/README.md +65 -337
  2. package/dashboard/dist/assets/{index-Bsrmo9LE.js → index-ggpJKZqZ.js} +55 -50
  3. package/dashboard/dist/index.html +1 -1
  4. package/dist/{bootstrap-omGPIBny.mjs → bootstrap-m107TukN.mjs} +1 -1
  5. package/dist/{bulk-C4hTcnLu.mjs → bulk-xAd_biYV.mjs} +1 -1
  6. package/dist/index.d.mts +16 -4
  7. package/dist/index.mjs +2 -2
  8. package/dist/{main-BxAeC_k6.mjs → main-5kVwbbnW.mjs} +157 -76
  9. package/dist/{main-DAXO-MiH.mjs → main-C0MjK1Au.mjs} +3 -3
  10. package/dist/{main-Bes11Ad3.mjs → main-C87TSAqM.mjs} +2 -2
  11. package/dist/{main-B-AZ-xoN.mjs → main-CWIBUEc7.mjs} +2 -2
  12. package/dist/{main-BU27VjDC.mjs → main-DRHL2bst.mjs} +2 -2
  13. package/dist/{main-DYGCl7Ja.mjs → main-D_62f9fH.mjs} +2 -2
  14. package/dist/{main-B5BwjIMu.mjs → main-HT5c6jyf.mjs} +58 -5
  15. package/dist/{main-BtnGmyaT.mjs → main-WkxXwP5q.mjs} +2 -2
  16. package/dist/{src-CIPTzq9i.mjs → src-DYqdD694.mjs} +64 -39
  17. package/drizzle/0008_delivery_outbox_destination_idx.sql +3 -0
  18. package/drizzle/meta/_journal.json +7 -0
  19. package/package.json +5 -2
  20. package/scripts/create-admin.mjs +8 -0
  21. package/scripts/create-project.mjs +1 -0
  22. package/scripts/notifkit.mjs +42 -0
  23. package/src/config/index.ts +6 -3
  24. package/src/contracts/streams.ts +4 -0
  25. package/src/db/schema.ts +3 -0
  26. package/src/queue/index.ts +19 -23
  27. package/src/redis/index.ts +20 -0
  28. package/src/services/api/admin-static.ts +21 -11
  29. package/src/services/api/handlers.ts +51 -43
  30. package/src/services/api/http.ts +12 -9
  31. package/src/services/api/live-events.ts +74 -0
  32. package/src/services/api/main.ts +57 -23
  33. package/src/services/delivery/live-events.ts +68 -0
  34. package/src/services/delivery/main.ts +14 -2
@@ -5,7 +5,7 @@
5
5
  <link rel="icon" type="image/svg+xml" href="./favicon.svg" />
6
6
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7
7
  <title>Notifkit Observability & Admin</title>
8
- <script type="module" crossorigin src="./assets/index-Bsrmo9LE.js"></script>
8
+ <script type="module" crossorigin src="./assets/index-ggpJKZqZ.js"></script>
9
9
  <link rel="stylesheet" crossorigin href="./assets/index-s-SXGIwv.css">
10
10
  </head>
11
11
  <body class="h-screen bg-background text-foreground antialiased overflow-hidden">
@@ -1,4 +1,4 @@
1
- import { $t as STREAMS, G as StreamConsumer, K as StreamProducer, Q as createDatabase, R as RedisClient, W as PendingMessageScanner, Y as createLogger } from "./src-CIPTzq9i.mjs";
1
+ import { $ as createDatabase, G as PendingMessageScanner, K as StreamConsumer, X as createLogger, en as STREAMS, q as StreamProducer, z as RedisClient } from "./src-DYqdD694.mjs";
2
2
  //#region src/workers/bootstrap.ts
3
3
  /** Logger, shared Redis connection and Postgres pool for one service. */
4
4
  function createWorkerRuntime(config, name, loggerName = name) {
@@ -1,4 +1,4 @@
1
- import { it as messageLogs, rt as deliveryOutbox } from "./src-CIPTzq9i.mjs";
1
+ import { at as messageLogs, it as deliveryOutbox } from "./src-DYqdD694.mjs";
2
2
  import { sql } from "drizzle-orm";
3
3
  //#region src/db/bulk.ts
4
4
  function rawClient(db) {
package/dist/index.d.mts CHANGED
@@ -44,7 +44,7 @@ declare const baseConfigSchema: z.ZodObject<{
44
44
  SEGMENT_MAX_USERS: z.ZodDefault<z.ZodCoercedNumber<unknown>>;
45
45
  PUBLIC_URL: z.ZodOptional<z.ZodString>;
46
46
  UNSUBSCRIBE_SECRET: z.ZodOptional<z.ZodString>;
47
- TRUST_PROXY: z.ZodPipe<z.ZodOptional<z.ZodString>, z.ZodTransform<boolean, string | undefined>>;
47
+ TRUST_PROXY: z.ZodPipe<z.ZodOptional<z.ZodString>, z.ZodTransform<number, string | undefined>>;
48
48
  CORS_ORIGIN: z.ZodOptional<z.ZodString>;
49
49
  }, z.core.$strip>;
50
50
  type BaseConfig = z.infer<typeof baseConfigSchema>;
@@ -698,6 +698,10 @@ declare const PUBSUB_CHANNELS: {
698
698
  readonly PROJECT_INVALIDATED: "project.invalidated";
699
699
  /** Payload: a token hash, or `*` for the whole cache. */
700
700
  readonly API_KEY_INVALIDATED: "apikey.invalidated";
701
+ /** Delivery outcomes for GET /v1/events/stream; see services/delivery/live-events. */
702
+ readonly DELIVERY_EVENTS: "delivery.events";
703
+ /** An API began listening on DELIVERY_EVENTS: delivery workers resume publishing. */
704
+ readonly DELIVERY_EVENTS_WANTED: "delivery.events.wanted";
701
705
  };
702
706
  type PubSubChannel = (typeof PUBSUB_CHANNELS)[keyof typeof PUBSUB_CHANNELS];
703
707
  declare const CONSUMER_GROUPS: {
@@ -4077,7 +4081,7 @@ interface StreamConsumerOptions {
4077
4081
  }
4078
4082
  declare class StreamConsumer {
4079
4083
  readonly redis: Redis;
4080
- private readonly blockingRedis;
4084
+ private blockingRedis;
4081
4085
  private readonly streams;
4082
4086
  private readonly group;
4083
4087
  private readonly consumer;
@@ -4098,7 +4102,7 @@ declare class StreamConsumer {
4098
4102
  /**
4099
4103
  * Acks and then deletes the entries. Every stream is read by exactly one
4100
4104
  * consumer group, so an acked entry is never read again; leaving it in place
4101
- * holds Redis memory until MAXLEN trims it, and a group recreated from "0"
4105
+ * holds Redis memory for good, and a group recreated from "0"
4102
4106
  * (ensureGroup) would process it a second time. Both commands are issued
4103
4107
  * before either is awaited, so auto-pipelining sends them in one round trip,
4104
4108
  * and Redis runs them in order.
@@ -4258,6 +4262,7 @@ declare module "ioredis" {
4258
4262
  interface Redis {
4259
4263
  leaseApiRateLimit(currentKey: string, prevKey: string, now: number | string, window: number | string, maxReqs: number | string, requested: number | string, ttl?: number | string): Promise<number>;
4260
4264
  releaseLock(lockKey: string, lockToken: string): Promise<number>;
4265
+ countRequest(key: string, ttlSeconds: number | string): Promise<number>;
4261
4266
  renewLock(lockKey: string, lockToken: string, ttlSeconds: number | string): Promise<number>;
4262
4267
  throttleUser(key: string, windowStart: number | string, limit: number | string, targetTime: number | string, memberId: string, ttlSeconds: number | string, pruneBefore?: number | string): Promise<number>;
4263
4268
  schedulerPoll(key: string, maxScore: number | string, limit: number | string, visibilityTimeout: number | string): Promise<string[]>;
@@ -4293,6 +4298,13 @@ declare const LUA_RENEW_LOCK = "\n if redis.call('GET', KEYS[1]) == ARGV[1] the
4293
4298
  declare const LUA_USER_THROTTLE = "\n local limit = tonumber(ARGV[2])\n local targetTime = tonumber(ARGV[3])\n local ttl = tonumber(ARGV[5])\n redis.call(\"ZREMRANGEBYSCORE\", KEYS[1], \"-inf\", ARGV[6] or ARGV[1])\n if redis.call(\"ZSCORE\", KEYS[1], ARGV[4]) then\n return 0\n end\n local count = redis.call(\"ZCOUNT\", KEYS[1], \"(\" .. ARGV[1], targetTime)\n if count < limit then\n redis.call(\"ZADD\", KEYS[1], targetTime, ARGV[4])\n -- Only ever extend: a send today must not cut short the life of a key\n -- that is still holding a send scheduled for next week.\n if redis.call(\"TTL\", KEYS[1]) < ttl then\n redis.call(\"EXPIRE\", KEYS[1], ttl)\n end\n end\n return count + 1\n";
4294
4299
  /** Polls scheduler zsets with visibility timeouts. */
4295
4300
  declare const LUA_SCHEDULER_POLL = "\n local key = KEYS[1]\n local maxScore = tonumber(ARGV[1])\n local limit = tonumber(ARGV[2])\n local visibilityTimeout = tonumber(ARGV[3]) or 0\n local tasks = redis.call('ZRANGE', key, 0, maxScore, 'BYSCORE', 'LIMIT', 0, limit)\n if #tasks > 0 then\n for i, task in ipairs(tasks) do\n redis.call('ZADD', key, maxScore + visibilityTimeout, task)\n end\n end\n return tasks\n";
4301
+ /**
4302
+ * Fixed-window request counter: INCR, and an expiry whenever the key has none.
4303
+ * One script so the two cannot come apart. With INCR and EXPIRE sent
4304
+ * separately, a failure between them left a counter with no expiry, and the
4305
+ * address it counted stayed over its limit for good.
4306
+ */
4307
+ declare const LUA_COUNT_REQUEST = "\n local count = redis.call('INCR', KEYS[1])\n if redis.call('TTL', KEYS[1]) < 0 then\n redis.call('EXPIRE', KEYS[1], ARGV[1])\n end\n return count\n";
4296
4308
  /** Two-phase idempotency lease acquisition. Checks sent/legacy, then attempts SET NX lock. */
4297
4309
  declare const LUA_ACQUIRE_LEASE = "\n if redis.call('EXISTS', KEYS[1]) == 1 or redis.call('EXISTS', KEYS[3]) == 1 then\n return 'completed'\n end\n local lockAcquired = redis.call('SET', KEYS[2], '1', 'EX', ARGV[1], 'NX')\n if lockAcquired then\n return 'acquired'\n end\n if redis.call('EXISTS', KEYS[1]) == 1 or redis.call('EXISTS', KEYS[3]) == 1 then\n return 'completed'\n end\n return 'locked'\n";
4298
4310
  /** Two-phase idempotency completion marker. Atomically sets completed + legacy and deletes lock. */
@@ -5236,4 +5248,4 @@ declare class NotifkitServer extends EventEmitter {
5236
5248
  stop(): Promise<void>;
5237
5249
  }
5238
5250
  //#endregion
5239
- export { AI_DEFAULTS, AddContactInput, AddContactSchema, AddUserInput, AddUserSchema, AdminUserRecord, AdminUserRepository, AiConfig, ApiRateLimiter, ApiRateLimiterOptions, AppError, AsyncSemaphore, BaseConfig, BaseWorker, BatchAddContactsInput, BatchAddContactsSchema, BatchItem, BatchProcessor, CONSUMER_GROUPS, CircuitBreaker, CircuitBreakerOptions, ConsumerGroup, ContactChannel, ContactChannelSchema, ContactRepository, CreateWorkflowInput, CreateWorkflowSchema, CreatedProjectKeyRecord, DLQMessageRecord, DataLoader, DataLoaderOptions, DatabaseClients, DatabaseOptions, Db, DeliveryOptions, DeliveryOptionsSchema, DeliveryResult, DevicePlatform, DeviceToken, DispatchedTaskPayload, ENRICHED_STREAMS, EscapeMode, EventEnvelope, EventEnvelopeSchema, EventMetadata, EventMetadataSchema, EventPayloadMap, EventRegistry, INBOUND_STREAMS, IdempotencyGuard, IdempotencyOptions, IngestEventInput, IngestEventSchema, InlineUser, InlineUserSchema, KnownEventType, LRUCache, LUA_ACQUIRE_LEASE, LUA_LEASE_API_RATE_LIMIT, LUA_MARK_PROCESSED, LUA_RELEASE_LOCK, LUA_RENEW_LOCK, LUA_SCHEDULER_POLL, LUA_USER_THROTTLE, LeaseResult, LockHeldError, LogLevel, Logger, LoggerOptions, MigrationOptions, NonRetryableError, NotificationAiPendingPayload, NotificationAiPendingPayloadSchema, NotificationCanceledPayload, NotificationCanceledPayloadSchema, NotificationChannel, NotificationChannelSchema, NotificationCreatedPayload, NotificationCreatedPayloadSchema, NotificationDeliveredPayload, NotificationDeliveredPayloadSchema, NotificationDispatchedPayload, NotificationDispatchedPayloadSchema, NotificationEnrichedPayload, NotificationEnrichedPayloadSchema, NotificationFailedPayload, NotificationFailedPayloadSchema, NotificationLogRecord, NotificationPreference, NotificationPriority, NotificationPrioritySchema, NotificationRequestedPayload, NotificationRequestedPayloadSchema, NotificationScheduledPayload, NotificationScheduledPayloadSchema, NotificationSkippedPayload, NotificationSkippedPayloadSchema, NotificationStatus, NotificationStatusSchema, NotificationTarget, NotificationTargetSchema, NotifkitClient, NotifkitClientOptions, NotifkitOptions, NotifkitServer, NotifyRequestInput, NotifyRequestSchema, OUTBOUND_STREAMS, PUBSUB_CHANNELS, ParseResult, PendingEntry, PendingMessageScanner, PendingMessageScannerOptions, PreferenceRepository, Preferences, PreferencesSchema, ProcessResult, ProjectKeyRecord, ProjectRecord, ProjectRepository, ProjectSettingsCache, ProjectSettingsCacheOptions, ProjectThrottleSettings, PubSubChannel, QuietHours, QuietHoursSchema, RateLimitConfig, RecipientProfile, RecipientProfileSchema, Redis, RedisClient, RedisClientOptions, type RedisOptions, RenderedContent, RenderedContentSchema, STREAMS, SegmentRepository, Sql, StreamConsumer, StreamConsumerOptions, StreamEvent, StreamEventMetadata, StreamEventMetadataSchema, StreamEventSchema, StreamMessage, StreamName, StreamProducer, StreamProducerOptions, SuppressionRecord, SuspendExecutionError, SyncTemplatesInput, SyncTemplatesSchema, SystemHealthRecord, SystemMetricsRecord, TemplateCache, TemplateInput, TemplateRecord, TemplateRecordResponse, TemplateRepository, TemplateSchema, ThrottleCheckOptions, ThrottleResult, Transport, TriggerWorkflowInput, TriggerWorkflowSchema, TypedEventEnvelope, UnsubscribeClaim, UnsubscribeHeaderOptions, UpdateProjectInput, UpdateProjectSchema, UpdateUserInput, UpdateUserSchema, UserContact, UserContactInput, UserContactInputSchema, UserContactResponse, UserDetailResponse, UserProfile, UserProfileResponse, UserRecord, UserRepository, UserResponse, UserThrottle, UserThrottleOptions, ValidationError, WebhookEvent, WorkerHealth, WorkerOptions, WorkerState, WorkflowContext, WorkflowDefinitionRecord, WorkflowEvent, WorkflowInstanceRecord, WorkflowNotifyInput, WorkflowNotifyPayloadSchema, WorkflowNotifyResult, WorkflowRepository, WorkflowStepContext, WorkflowStepDef, WorkflowStepRecord, WorkflowStepSchema, WorkflowWaiterRecord, assertTablesInSchema, baseConfigSchema, buildStepNotifyPayload, buildStreamEvent, buildUnsubscribeHeaders, calculateTokenBatchSize, createDatabase, createLogger, escapeHeader, escapeHtml, generateId, getAiConfig, getMetricsRegistry, getPriorityBucket, globalEmitter, interpolate, loadEnv, metrics, migrateDatabase, normaliseTarget, notifkitTableNames, parseConfig, qualifyMigrationSql, readBaseConfig, registerCustomCommands, registerTransport, registry, renderWithTemplate, resolveStepTarget, runMigrations, setAiConfig, setGlobalConfig, signUnsubscribeToken, sleep, startHealthReporter, transportRegistry, verifyUnsubscribeToken, workflow, workflowRegistry };
5251
+ export { AI_DEFAULTS, AddContactInput, AddContactSchema, AddUserInput, AddUserSchema, AdminUserRecord, AdminUserRepository, AiConfig, ApiRateLimiter, ApiRateLimiterOptions, AppError, AsyncSemaphore, BaseConfig, BaseWorker, BatchAddContactsInput, BatchAddContactsSchema, BatchItem, BatchProcessor, CONSUMER_GROUPS, CircuitBreaker, CircuitBreakerOptions, ConsumerGroup, ContactChannel, ContactChannelSchema, ContactRepository, CreateWorkflowInput, CreateWorkflowSchema, CreatedProjectKeyRecord, DLQMessageRecord, DataLoader, DataLoaderOptions, DatabaseClients, DatabaseOptions, Db, DeliveryOptions, DeliveryOptionsSchema, DeliveryResult, DevicePlatform, DeviceToken, DispatchedTaskPayload, ENRICHED_STREAMS, EscapeMode, EventEnvelope, EventEnvelopeSchema, EventMetadata, EventMetadataSchema, EventPayloadMap, EventRegistry, INBOUND_STREAMS, IdempotencyGuard, IdempotencyOptions, IngestEventInput, IngestEventSchema, InlineUser, InlineUserSchema, KnownEventType, LRUCache, LUA_ACQUIRE_LEASE, LUA_COUNT_REQUEST, LUA_LEASE_API_RATE_LIMIT, LUA_MARK_PROCESSED, LUA_RELEASE_LOCK, LUA_RENEW_LOCK, LUA_SCHEDULER_POLL, LUA_USER_THROTTLE, LeaseResult, LockHeldError, LogLevel, Logger, LoggerOptions, MigrationOptions, NonRetryableError, NotificationAiPendingPayload, NotificationAiPendingPayloadSchema, NotificationCanceledPayload, NotificationCanceledPayloadSchema, NotificationChannel, NotificationChannelSchema, NotificationCreatedPayload, NotificationCreatedPayloadSchema, NotificationDeliveredPayload, NotificationDeliveredPayloadSchema, NotificationDispatchedPayload, NotificationDispatchedPayloadSchema, NotificationEnrichedPayload, NotificationEnrichedPayloadSchema, NotificationFailedPayload, NotificationFailedPayloadSchema, NotificationLogRecord, NotificationPreference, NotificationPriority, NotificationPrioritySchema, NotificationRequestedPayload, NotificationRequestedPayloadSchema, NotificationScheduledPayload, NotificationScheduledPayloadSchema, NotificationSkippedPayload, NotificationSkippedPayloadSchema, NotificationStatus, NotificationStatusSchema, NotificationTarget, NotificationTargetSchema, NotifkitClient, NotifkitClientOptions, NotifkitOptions, NotifkitServer, NotifyRequestInput, NotifyRequestSchema, OUTBOUND_STREAMS, PUBSUB_CHANNELS, ParseResult, PendingEntry, PendingMessageScanner, PendingMessageScannerOptions, PreferenceRepository, Preferences, PreferencesSchema, ProcessResult, ProjectKeyRecord, ProjectRecord, ProjectRepository, ProjectSettingsCache, ProjectSettingsCacheOptions, ProjectThrottleSettings, PubSubChannel, QuietHours, QuietHoursSchema, RateLimitConfig, RecipientProfile, RecipientProfileSchema, Redis, RedisClient, RedisClientOptions, type RedisOptions, RenderedContent, RenderedContentSchema, STREAMS, SegmentRepository, Sql, StreamConsumer, StreamConsumerOptions, StreamEvent, StreamEventMetadata, StreamEventMetadataSchema, StreamEventSchema, StreamMessage, StreamName, StreamProducer, StreamProducerOptions, SuppressionRecord, SuspendExecutionError, SyncTemplatesInput, SyncTemplatesSchema, SystemHealthRecord, SystemMetricsRecord, TemplateCache, TemplateInput, TemplateRecord, TemplateRecordResponse, TemplateRepository, TemplateSchema, ThrottleCheckOptions, ThrottleResult, Transport, TriggerWorkflowInput, TriggerWorkflowSchema, TypedEventEnvelope, UnsubscribeClaim, UnsubscribeHeaderOptions, UpdateProjectInput, UpdateProjectSchema, UpdateUserInput, UpdateUserSchema, UserContact, UserContactInput, UserContactInputSchema, UserContactResponse, UserDetailResponse, UserProfile, UserProfileResponse, UserRecord, UserRepository, UserResponse, UserThrottle, UserThrottleOptions, ValidationError, WebhookEvent, WorkerHealth, WorkerOptions, WorkerState, WorkflowContext, WorkflowDefinitionRecord, WorkflowEvent, WorkflowInstanceRecord, WorkflowNotifyInput, WorkflowNotifyPayloadSchema, WorkflowNotifyResult, WorkflowRepository, WorkflowStepContext, WorkflowStepDef, WorkflowStepRecord, WorkflowStepSchema, WorkflowWaiterRecord, assertTablesInSchema, baseConfigSchema, buildStepNotifyPayload, buildStreamEvent, buildUnsubscribeHeaders, calculateTokenBatchSize, createDatabase, createLogger, escapeHeader, escapeHtml, generateId, getAiConfig, getMetricsRegistry, getPriorityBucket, globalEmitter, interpolate, loadEnv, metrics, migrateDatabase, normaliseTarget, notifkitTableNames, parseConfig, qualifyMigrationSql, readBaseConfig, registerCustomCommands, registerTransport, registry, renderWithTemplate, resolveStepTarget, runMigrations, setAiConfig, setGlobalConfig, signUnsubscribeToken, sleep, startHealthReporter, transportRegistry, verifyUnsubscribeToken, workflow, workflowRegistry };
package/dist/index.mjs CHANGED
@@ -1,2 +1,2 @@
1
- import { $ as migrateDatabase, $t as STREAMS, A as LUA_ACQUIRE_LEASE, At as AddUserSchema, B as ApiRateLimiter, Bt as TemplateSchema, C as ContactRepository, Cn as BatchProcessor, Ct as NotificationScheduledPayloadSchema, D as TemplateRepository, Dn as LRUCache, Dt as NotificationRequestedPayloadSchema, E as SegmentRepository, En as normaliseTarget, Et as NotificationCreatedPayloadSchema, F as LUA_SCHEDULER_POLL, Ft as InlineUserSchema, G as StreamConsumer, Gt as WorkflowNotifyPayloadSchema, H as UserThrottle, Ht as UpdateProjectSchema, I as LUA_USER_THROTTLE, It as NotifyRequestSchema, J as metrics, Jt as CONSUMER_GROUPS, K as StreamProducer, Kt as WorkflowStepSchema, L as Redis, Lt as PreferencesSchema, M as LUA_MARK_PROCESSED, Mt as ContactChannelSchema, N as LUA_RELEASE_LOCK, Nt as CreateWorkflowSchema, O as UserRepository, On as globalEmitter, Ot as NotificationTargetSchema, P as LUA_RENEW_LOCK, Pt as IngestEventSchema, Q as createDatabase, Qt as PUBSUB_CHANNELS, R as RedisClient, Rt as QuietHoursSchema, S as AdminUserRepository, Sn as CircuitBreaker, St as RenderedContentSchema, T as ProjectRepository, Tn as getPriorityBucket, Tt as RecipientProfileSchema, U as calculateTokenBatchSize, Ut as UpdateUserSchema, V as ProjectSettingsCache, Vt as TriggerWorkflowSchema, W as PendingMessageScanner, Wt as UserContactInputSchema, X as IdempotencyGuard, Xt as INBOUND_STREAMS, Y as createLogger, Yt as ENRICHED_STREAMS, Z as assertTablesInSchema, Zt as OUTBOUND_STREAMS, _ as TemplateCache, _n as AppError, _t as NotificationSkippedPayloadSchema, a as SuspendExecutionError, an as EventMetadataSchema, b as interpolate, bn as sleep, bt as DeliveryOptionsSchema, c as BaseWorker, cn as NotificationStatusSchema, d as startHealthReporter, dn as getAiConfig, en as EventEnvelopeSchema, et as notifkitTableNames, f as buildUnsubscribeHeaders, fn as loadEnv, g as transportRegistry, gn as setGlobalConfig, gt as NotificationCanceledPayloadSchema, h as registerTransport, hn as setAiConfig, ht as NotificationAiPendingPayloadSchema, i as workflowRegistry, in as registry, j as LUA_LEASE_API_RATE_LIMIT, jt as BatchAddContactsSchema, k as WorkflowRepository, kt as AddContactSchema, l as LockHeldError, ln as AI_DEFAULTS, m as verifyUnsubscribeToken, mn as readBaseConfig, n as NotifkitClient, nn as StreamEventSchema, nt as runMigrations, o as buildStepNotifyPayload, on as NotificationChannelSchema, p as signUnsubscribeToken, pn as parseConfig, q as getMetricsRegistry, qt as buildStreamEvent, r as workflow, rn as EventRegistry, s as resolveStepTarget, sn as NotificationPrioritySchema, t as NotifkitServer, tn as StreamEventMetadataSchema, tt as qualifyMigrationSql, u as NonRetryableError, un as baseConfigSchema, v as escapeHeader, vn as ValidationError, vt as NotificationFailedPayloadSchema, w as PreferenceRepository, wn as AsyncSemaphore, wt as NotificationEnrichedPayloadSchema, x as renderWithTemplate, xn as DataLoader, xt as NotificationDispatchedPayloadSchema, y as escapeHtml, yn as generateId, yt as NotificationDeliveredPayloadSchema, z as registerCustomCommands, zt as SyncTemplatesSchema } from "./src-CIPTzq9i.mjs";
2
- export { AI_DEFAULTS, AddContactSchema, AddUserSchema, AdminUserRepository, ApiRateLimiter, AppError, AsyncSemaphore, BaseWorker, BatchAddContactsSchema, BatchProcessor, CONSUMER_GROUPS, CircuitBreaker, ContactChannelSchema, ContactRepository, CreateWorkflowSchema, DataLoader, DeliveryOptionsSchema, ENRICHED_STREAMS, EventEnvelopeSchema, EventMetadataSchema, EventRegistry, INBOUND_STREAMS, IdempotencyGuard, IngestEventSchema, InlineUserSchema, LRUCache, LUA_ACQUIRE_LEASE, LUA_LEASE_API_RATE_LIMIT, LUA_MARK_PROCESSED, LUA_RELEASE_LOCK, LUA_RENEW_LOCK, LUA_SCHEDULER_POLL, LUA_USER_THROTTLE, LockHeldError, NonRetryableError, NotificationAiPendingPayloadSchema, NotificationCanceledPayloadSchema, NotificationChannelSchema, NotificationCreatedPayloadSchema, NotificationDeliveredPayloadSchema, NotificationDispatchedPayloadSchema, NotificationEnrichedPayloadSchema, NotificationFailedPayloadSchema, NotificationPrioritySchema, NotificationRequestedPayloadSchema, NotificationScheduledPayloadSchema, NotificationSkippedPayloadSchema, NotificationStatusSchema, NotificationTargetSchema, NotifkitClient, NotifkitServer, NotifyRequestSchema, OUTBOUND_STREAMS, PUBSUB_CHANNELS, PendingMessageScanner, PreferenceRepository, PreferencesSchema, ProjectRepository, ProjectSettingsCache, QuietHoursSchema, RecipientProfileSchema, Redis, RedisClient, RenderedContentSchema, STREAMS, SegmentRepository, StreamConsumer, StreamEventMetadataSchema, StreamEventSchema, StreamProducer, SuspendExecutionError, SyncTemplatesSchema, TemplateCache, TemplateRepository, TemplateSchema, TriggerWorkflowSchema, UpdateProjectSchema, UpdateUserSchema, UserContactInputSchema, UserRepository, UserThrottle, ValidationError, WorkflowNotifyPayloadSchema, WorkflowRepository, WorkflowStepSchema, assertTablesInSchema, baseConfigSchema, buildStepNotifyPayload, buildStreamEvent, buildUnsubscribeHeaders, calculateTokenBatchSize, createDatabase, createLogger, escapeHeader, escapeHtml, generateId, getAiConfig, getMetricsRegistry, getPriorityBucket, globalEmitter, interpolate, loadEnv, metrics, migrateDatabase, normaliseTarget, notifkitTableNames, parseConfig, qualifyMigrationSql, readBaseConfig, registerCustomCommands, registerTransport, registry, renderWithTemplate, resolveStepTarget, runMigrations, setAiConfig, setGlobalConfig, signUnsubscribeToken, sleep, startHealthReporter, transportRegistry, verifyUnsubscribeToken, workflow, workflowRegistry };
1
+ import { $ as createDatabase, $t as PUBSUB_CHANNELS, A as LUA_ACQUIRE_LEASE, At as AddContactSchema, B as registerCustomCommands, Bt as SyncTemplatesSchema, C as ContactRepository, Cn as CircuitBreaker, Ct as RenderedContentSchema, D as TemplateRepository, Dn as normaliseTarget, Dt as NotificationCreatedPayloadSchema, E as SegmentRepository, En as getPriorityBucket, Et as RecipientProfileSchema, F as LUA_RENEW_LOCK, Ft as IngestEventSchema, G as PendingMessageScanner, Gt as UserContactInputSchema, H as ProjectSettingsCache, Ht as TriggerWorkflowSchema, I as LUA_SCHEDULER_POLL, It as InlineUserSchema, J as getMetricsRegistry, Jt as buildStreamEvent, K as StreamConsumer, Kt as WorkflowNotifyPayloadSchema, L as LUA_USER_THROTTLE, Lt as NotifyRequestSchema, M as LUA_LEASE_API_RATE_LIMIT, Mt as BatchAddContactsSchema, N as LUA_MARK_PROCESSED, Nt as ContactChannelSchema, O as UserRepository, On as LRUCache, Ot as NotificationRequestedPayloadSchema, P as LUA_RELEASE_LOCK, Pt as CreateWorkflowSchema, Q as assertTablesInSchema, Qt as OUTBOUND_STREAMS, R as Redis, Rt as PreferencesSchema, S as AdminUserRepository, Sn as DataLoader, St as NotificationDispatchedPayloadSchema, T as ProjectRepository, Tn as AsyncSemaphore, Tt as NotificationEnrichedPayloadSchema, U as UserThrottle, Ut as UpdateProjectSchema, V as ApiRateLimiter, Vt as TemplateSchema, W as calculateTokenBatchSize, Wt as UpdateUserSchema, X as createLogger, Xt as ENRICHED_STREAMS, Y as metrics, Yt as CONSUMER_GROUPS, Z as IdempotencyGuard, Zt as INBOUND_STREAMS, _ as TemplateCache, _n as setGlobalConfig, _t as NotificationCanceledPayloadSchema, a as SuspendExecutionError, an as registry, b as interpolate, bn as generateId, bt as NotificationDeliveredPayloadSchema, c as BaseWorker, cn as NotificationPrioritySchema, d as startHealthReporter, dn as baseConfigSchema, en as STREAMS, et as migrateDatabase, f as buildUnsubscribeHeaders, fn as getAiConfig, g as transportRegistry, gn as setAiConfig, gt as NotificationAiPendingPayloadSchema, h as registerTransport, hn as readBaseConfig, i as workflowRegistry, in as EventRegistry, j as LUA_COUNT_REQUEST, jt as AddUserSchema, k as WorkflowRepository, kn as globalEmitter, kt as NotificationTargetSchema, l as LockHeldError, ln as NotificationStatusSchema, m as verifyUnsubscribeToken, mn as parseConfig, n as NotifkitClient, nn as StreamEventMetadataSchema, nt as qualifyMigrationSql, o as buildStepNotifyPayload, on as EventMetadataSchema, p as signUnsubscribeToken, pn as loadEnv, q as StreamProducer, qt as WorkflowStepSchema, r as workflow, rn as StreamEventSchema, rt as runMigrations, s as resolveStepTarget, sn as NotificationChannelSchema, t as NotifkitServer, tn as EventEnvelopeSchema, tt as notifkitTableNames, u as NonRetryableError, un as AI_DEFAULTS, v as escapeHeader, vn as AppError, vt as NotificationSkippedPayloadSchema, w as PreferenceRepository, wn as BatchProcessor, wt as NotificationScheduledPayloadSchema, x as renderWithTemplate, xn as sleep, xt as DeliveryOptionsSchema, y as escapeHtml, yn as ValidationError, yt as NotificationFailedPayloadSchema, z as RedisClient, zt as QuietHoursSchema } from "./src-DYqdD694.mjs";
2
+ export { AI_DEFAULTS, AddContactSchema, AddUserSchema, AdminUserRepository, ApiRateLimiter, AppError, AsyncSemaphore, BaseWorker, BatchAddContactsSchema, BatchProcessor, CONSUMER_GROUPS, CircuitBreaker, ContactChannelSchema, ContactRepository, CreateWorkflowSchema, DataLoader, DeliveryOptionsSchema, ENRICHED_STREAMS, EventEnvelopeSchema, EventMetadataSchema, EventRegistry, INBOUND_STREAMS, IdempotencyGuard, IngestEventSchema, InlineUserSchema, LRUCache, LUA_ACQUIRE_LEASE, LUA_COUNT_REQUEST, LUA_LEASE_API_RATE_LIMIT, LUA_MARK_PROCESSED, LUA_RELEASE_LOCK, LUA_RENEW_LOCK, LUA_SCHEDULER_POLL, LUA_USER_THROTTLE, LockHeldError, NonRetryableError, NotificationAiPendingPayloadSchema, NotificationCanceledPayloadSchema, NotificationChannelSchema, NotificationCreatedPayloadSchema, NotificationDeliveredPayloadSchema, NotificationDispatchedPayloadSchema, NotificationEnrichedPayloadSchema, NotificationFailedPayloadSchema, NotificationPrioritySchema, NotificationRequestedPayloadSchema, NotificationScheduledPayloadSchema, NotificationSkippedPayloadSchema, NotificationStatusSchema, NotificationTargetSchema, NotifkitClient, NotifkitServer, NotifyRequestSchema, OUTBOUND_STREAMS, PUBSUB_CHANNELS, PendingMessageScanner, PreferenceRepository, PreferencesSchema, ProjectRepository, ProjectSettingsCache, QuietHoursSchema, RecipientProfileSchema, Redis, RedisClient, RenderedContentSchema, STREAMS, SegmentRepository, StreamConsumer, StreamEventMetadataSchema, StreamEventSchema, StreamProducer, SuspendExecutionError, SyncTemplatesSchema, TemplateCache, TemplateRepository, TemplateSchema, TriggerWorkflowSchema, UpdateProjectSchema, UpdateUserSchema, UserContactInputSchema, UserRepository, UserThrottle, ValidationError, WorkflowNotifyPayloadSchema, WorkflowRepository, WorkflowStepSchema, assertTablesInSchema, baseConfigSchema, buildStepNotifyPayload, buildStreamEvent, buildUnsubscribeHeaders, calculateTokenBatchSize, createDatabase, createLogger, escapeHeader, escapeHtml, generateId, getAiConfig, getMetricsRegistry, getPriorityBucket, globalEmitter, interpolate, loadEnv, metrics, migrateDatabase, normaliseTarget, notifkitTableNames, parseConfig, qualifyMigrationSql, readBaseConfig, registerCustomCommands, registerTransport, registry, renderWithTemplate, resolveStepTarget, runMigrations, setAiConfig, setGlobalConfig, signUnsubscribeToken, sleep, startHealthReporter, transportRegistry, verifyUnsubscribeToken, workflow, workflowRegistry };
@@ -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-DYqdD694.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;
@@ -1,6 +1,6 @@
1
- import { $t as STREAMS, Jt as CONSUMER_GROUPS, K as StreamProducer, c as BaseWorker, d as startHealthReporter, fn as loadEnv, ft as workflowInstances, mn as readBaseConfig, mt as workflowWaiters, pt as workflowSteps, qt as buildStreamEvent } from "./src-CIPTzq9i.mjs";
2
- import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime } from "./bootstrap-omGPIBny.mjs";
3
- import { t as insertMessageLogs } from "./bulk-C4hTcnLu.mjs";
1
+ import { Jt as buildStreamEvent, Yt as CONSUMER_GROUPS, c as BaseWorker, d as startHealthReporter, en as STREAMS, hn as readBaseConfig, ht as workflowWaiters, mt as workflowSteps, pn as loadEnv, pt as workflowInstances, q as StreamProducer } from "./src-DYqdD694.mjs";
2
+ import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime } from "./bootstrap-m107TukN.mjs";
3
+ import { t as insertMessageLogs } from "./bulk-xAd_biYV.mjs";
4
4
  import { and, eq, gt, isNull, or } from "drizzle-orm";
5
5
  //#region src/services/events/main.ts
6
6
  loadEnv();
@@ -1,5 +1,5 @@
1
- import { $t as STREAMS, C as ContactRepository, D as TemplateRepository, En as normaliseTarget, Jt as CONSUMER_GROUPS, O as UserRepository, On as globalEmitter, Tn as getPriorityBucket, c as BaseWorker, ct as suppressions, d as startHealthReporter, fn as loadEnv, in as registry, mn as readBaseConfig, qt as buildStreamEvent, st as scheduledPayloads } from "./src-CIPTzq9i.mjs";
2
- import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime, t as createPriorityProducers } from "./bootstrap-omGPIBny.mjs";
1
+ import { C as ContactRepository, D as TemplateRepository, Dn as normaliseTarget, En as getPriorityBucket, Jt as buildStreamEvent, O as UserRepository, Yt as CONSUMER_GROUPS, an as registry, c as BaseWorker, ct as scheduledPayloads, d as startHealthReporter, en as STREAMS, hn as readBaseConfig, kn as globalEmitter, lt as suppressions, pn as loadEnv } from "./src-DYqdD694.mjs";
2
+ import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime, t as createPriorityProducers } from "./bootstrap-m107TukN.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
@@ -1,5 +1,5 @@
1
- import { $t as STREAMS, D as TemplateRepository, Jt as CONSUMER_GROUPS, K as StreamProducer, On as globalEmitter, Tn as getPriorityBucket, X as IdempotencyGuard, _ as TemplateCache, c as BaseWorker, d as startHealthReporter, dn as getAiConfig, fn as loadEnv, in as registry, ln as AI_DEFAULTS, mn as readBaseConfig, qt as buildStreamEvent, st as scheduledPayloads, x as renderWithTemplate } from "./src-CIPTzq9i.mjs";
2
- import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime, t as createPriorityProducers } from "./bootstrap-omGPIBny.mjs";
1
+ import { D as TemplateRepository, En as getPriorityBucket, Jt as buildStreamEvent, Yt as CONSUMER_GROUPS, Z as IdempotencyGuard, _ as TemplateCache, an as registry, c as BaseWorker, ct as scheduledPayloads, d as startHealthReporter, en as STREAMS, fn as getAiConfig, hn as readBaseConfig, kn as globalEmitter, pn as loadEnv, q as StreamProducer, un as AI_DEFAULTS, x as renderWithTemplate } from "./src-DYqdD694.mjs";
2
+ import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime, t as createPriorityProducers } from "./bootstrap-m107TukN.mjs";
3
3
  import { generateText } from "ai";
4
4
  //#region src/services/ai/main.ts
5
5
  loadEnv();
@@ -1,5 +1,5 @@
1
- import { C as ContactRepository, D as TemplateRepository, Jt as CONSUMER_GROUPS, O as UserRepository, 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, mn as readBaseConfig, qt as buildStreamEvent, w as PreferenceRepository } from "./src-CIPTzq9i.mjs";
2
- import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime, t as createPriorityProducers } from "./bootstrap-omGPIBny.mjs";
1
+ import { C as ContactRepository, D as TemplateRepository, En as getPriorityBucket, Jt as buildStreamEvent, O as UserRepository, Yt as CONSUMER_GROUPS, Z as IdempotencyGuard, Zt as INBOUND_STREAMS, _ as TemplateCache, an as registry, c as BaseWorker, d as startHealthReporter, hn as readBaseConfig, kn as globalEmitter, pn as loadEnv, w as PreferenceRepository } from "./src-DYqdD694.mjs";
2
+ import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime, t as createPriorityProducers } from "./bootstrap-m107TukN.mjs";
3
3
  import { randomUUID } from "node:crypto";
4
4
  //#region src/services/enricher/main.ts
5
5
  loadEnv();
@@ -1,5 +1,5 @@
1
- import { $t as STREAMS, Jt as CONSUMER_GROUPS, K as StreamProducer, a as SuspendExecutionError, c as BaseWorker, d as startHealthReporter, dt as workflowDefinitions, fn as loadEnv, ft as workflowInstances, i as workflowRegistry, mn as readBaseConfig, mt as workflowWaiters, o as buildStepNotifyPayload, pt as workflowSteps, qt as buildStreamEvent } from "./src-CIPTzq9i.mjs";
2
- import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime } from "./bootstrap-omGPIBny.mjs";
1
+ import { Jt as buildStreamEvent, Yt as CONSUMER_GROUPS, a as SuspendExecutionError, c as BaseWorker, d as startHealthReporter, en as STREAMS, ft as workflowDefinitions, hn as readBaseConfig, ht as workflowWaiters, i as workflowRegistry, mt as workflowSteps, o as buildStepNotifyPayload, pn as loadEnv, pt as workflowInstances, q as StreamProducer } from "./src-DYqdD694.mjs";
2
+ import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime } from "./bootstrap-m107TukN.mjs";
3
3
  import { randomUUID } from "node:crypto";
4
4
  import { and, eq } from "drizzle-orm";
5
5
  //#region src/services/workflow/main.ts