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.
- package/README.md +5 -1
- package/dist/{bootstrap-BMDRPjgp.mjs → bootstrap-Cs1XIp6b.mjs} +1 -1
- package/dist/{bulk-CJ5QXiPa.mjs → bulk-4pRIiG2o.mjs} +1 -1
- package/dist/index.d.mts +37 -106
- package/dist/index.mjs +2 -2
- package/dist/{main-8nP3qmCT.mjs → main-BN7ZfvxN.mjs} +5 -14
- package/dist/{main-6_bXQBDH.mjs → main-Bl0S4GDd.mjs} +3 -3
- package/dist/{main-mGIT8jv4.mjs → main-C5DiM7_r.mjs} +3 -3
- package/dist/{main-B82bsFoy.mjs → main-DVzp0DLn.mjs} +8 -58
- package/dist/{main-ZW5RaiOK.mjs → main-O9iZ6iz2.mjs} +4 -9
- package/dist/{main-CpaHF1Zw.mjs → main-Ppr7Pq1g.mjs} +35 -16
- package/dist/{main-DULD8-th.mjs → main-yL5VinQ5.mjs} +5 -12
- package/dist/{main-g1KEcXXp.mjs → main-zgxAlH3l.mjs} +23 -94
- package/dist/{schedule-or-dispatch-Cd7GLfc3.mjs → schedule-or-dispatch-BwCZWS_P.mjs} +1 -1
- package/dist/{src-BE_EUpZ6.mjs → src-C38w4jR4.mjs} +88 -170
- package/drizzle/0009_retention_time_indexes.sql +4 -0
- package/drizzle/meta/_journal.json +7 -0
- package/package.json +1 -1
- package/src/client.ts +6 -6
- package/src/config/index.ts +9 -11
- package/src/contracts/sdk.ts +9 -9
- package/src/db/bulk.ts +3 -10
- package/src/db/schema.ts +3 -0
- package/src/idempotency/index.ts +12 -53
- package/src/queue/index.ts +9 -12
- package/src/rate-limiter/api-rate-limiter.ts +8 -27
- package/src/rate-limiter/project-settings-cache.ts +4 -16
- package/src/redis/index.ts +3 -51
- package/src/repositories/contact.ts +0 -2
- package/src/server.ts +1 -4
- package/src/services/api/handlers/auth.ts +3 -11
- package/src/services/api/handlers/campaigns.ts +4 -15
- package/src/services/api/handlers/events.ts +1 -2
- package/src/services/api/handlers/notifications.ts +6 -16
- package/src/services/api/handlers/projects.ts +1 -3
- package/src/services/api/handlers/shared.ts +7 -29
- package/src/services/api/handlers/system.ts +1 -1
- package/src/services/api/handlers/unsubscribe.ts +7 -30
- package/src/services/api/handlers/users.ts +1 -2
- package/src/services/api/http.ts +5 -10
- package/src/services/api/main.ts +27 -63
- package/src/services/api/webhooks.ts +8 -19
- package/src/services/auth/index.ts +2 -21
- package/src/services/delivery/main.ts +25 -89
- package/src/services/engine/main.ts +33 -69
- package/src/services/engine/schedule-or-dispatch.ts +7 -4
- package/src/services/enricher/main.ts +19 -49
- package/src/services/events/main.ts +3 -12
- package/src/services/scheduler/main.ts +26 -32
- package/src/services/scheduler/retention.ts +47 -0
- package/src/services/scheduler/send-gate.ts +6 -17
- package/src/services/workflow/duration.ts +1 -6
- package/src/services/workflow/main.ts +3 -9
- package/src/services/workflow/timers.ts +2 -1
- package/src/shared/batch-processor.ts +3 -7
- package/src/shared/utils.ts +2 -8
- package/src/transport/index.ts +0 -4
- package/src/unsubscribe/index.ts +3 -11
- package/src/workers/index.ts +30 -23
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ Self-hosted notification infrastructure for product notifications. One API call
|
|
|
8
8
|
|
|
9
9
|
[](https://www.npmjs.com/package/notifkit) [](https://www.npmjs.com/package/notifkit) [](https://github.com/devkitshq/notifkit/actions/workflows/ci.yml) [](https://www.typescriptlang.org/) [](https://nodejs.org) [](./LICENSE)
|
|
10
10
|
|
|
11
|
-
[Documentation](https://notifkit.dev/docs/) · [Quickstart](https://notifkit.dev/docs/quickstart.html) · [Examples](https://notifkit.dev/docs/examples.html) · [notifkit.dev](https://notifkit.dev)
|
|
11
|
+
[Documentation](https://notifkit.dev/docs/) · [Quickstart](https://notifkit.dev/docs/quickstart.html) · [Examples](https://notifkit.dev/docs/examples.html) · [Production support](#production-support) · [notifkit.dev](https://notifkit.dev)
|
|
12
12
|
|
|
13
13
|
https://github.com/user-attachments/assets/ad8955bd-4205-4d47-8dfc-9010abd3ad62
|
|
14
14
|
|
|
@@ -140,6 +140,10 @@ To sign in, set `ADMIN_EMAIL` and `ADMIN_PASSWORD` before starting the server, t
|
|
|
140
140
|
|
|
141
141
|
Everything lives at **[notifkit.dev/docs](https://notifkit.dev/docs/)**: [concepts](https://notifkit.dev/docs/concepts.html), [routing and fallback](https://notifkit.dev/docs/guides/routing.html), [preferences](https://notifkit.dev/docs/guides/preferences.html), [workflows](https://notifkit.dev/docs/guides/workflows.html), [architecture](https://notifkit.dev/docs/architecture.html), [operations](https://notifkit.dev/docs/operations.html) and the [API reference](https://notifkit.dev/docs/reference.html).
|
|
142
142
|
|
|
143
|
+
## Production support
|
|
144
|
+
|
|
145
|
+
Need help running notifkit in production? We can deploy it on your infrastructure, migrate your templates from Knock, Courier, Novu or your custom engine, and give you priority support. Email [contact.devkitshq@gmail.com](mailto:contact.devkitshq@gmail.com?subject=notifkit%20production%20support).
|
|
146
|
+
|
|
143
147
|
## Contributing
|
|
144
148
|
|
|
145
149
|
Issues and pull requests are welcome.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { $t as STREAMS, G as StreamConsumer, K as StreamProducer, L as RedisClient, Q as createDatabase, W as PendingMessageScanner, Y as createLogger } from "./src-C38w4jR4.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) {
|
package/dist/index.d.mts
CHANGED
|
@@ -37,9 +37,11 @@ declare const baseConfigSchema: z.ZodObject<{
|
|
|
37
37
|
PIPELINE_FUSED: z.ZodPipe<z.ZodOptional<z.ZodString>, z.ZodTransform<boolean, string | undefined>>;
|
|
38
38
|
QUEUE_MAX_LEN: z.ZodDefault<z.ZodCoercedNumber<unknown>>;
|
|
39
39
|
IDEMPOTENCY_TTL_SECONDS: z.ZodDefault<z.ZodCoercedNumber<unknown>>;
|
|
40
|
+
RATE_LIMIT_PER_HOUR: z.ZodDefault<z.ZodCoercedNumber<unknown>>;
|
|
40
41
|
DB_MAX_CONNECTIONS: z.ZodDefault<z.ZodCoercedNumber<unknown>>;
|
|
41
42
|
DB_SCHEMA: z.ZodDefault<z.ZodString>;
|
|
42
43
|
LOG_FLUSH_INTERVAL_MS: z.ZodDefault<z.ZodCoercedNumber<unknown>>;
|
|
44
|
+
LOG_RETENTION_DAYS: z.ZodOptional<z.ZodCoercedNumber<unknown>>;
|
|
43
45
|
LOG_BUFFER_MAX_SIZE: z.ZodDefault<z.ZodCoercedNumber<unknown>>;
|
|
44
46
|
SEGMENT_MAX_USERS: z.ZodDefault<z.ZodCoercedNumber<unknown>>;
|
|
45
47
|
PUBLIC_URL: z.ZodOptional<z.ZodString>;
|
|
@@ -3978,31 +3980,19 @@ interface IdempotencyOptions {
|
|
|
3978
3980
|
ttlSeconds?: number;
|
|
3979
3981
|
}
|
|
3980
3982
|
type LeaseResult = "acquired" | "locked" | "completed";
|
|
3981
|
-
/**
|
|
3982
|
-
* SET NX-based idempotency guard.
|
|
3983
|
-
* One key per id: `L` while leased, `1` once processed.
|
|
3984
|
-
*/
|
|
3983
|
+
/** One Redis key per id: `L` while a worker holds it, `1` once processed. */
|
|
3985
3984
|
declare class IdempotencyGuard {
|
|
3986
3985
|
private readonly redis;
|
|
3987
3986
|
private readonly keyPrefix;
|
|
3988
3987
|
private readonly ttlSeconds;
|
|
3989
3988
|
constructor({ redis, keyPrefix, ttlSeconds }: IdempotencyOptions);
|
|
3990
3989
|
private key;
|
|
3991
|
-
/** @deprecated Nothing writes `:lock` keys any more; read only for markers from older versions. Remove in the next minor. */
|
|
3992
|
-
private lockKey;
|
|
3993
|
-
/** @deprecated Nothing writes `:sent` keys any more; read only for markers from older versions. Remove in the next minor. */
|
|
3994
|
-
private completedKey;
|
|
3995
|
-
/**
|
|
3996
|
-
* Leases the id with SET NX. One key per id: `L` while leased, `1` once processed.
|
|
3997
|
-
* Returns "completed" if already processed, "locked" if another worker holds the lease.
|
|
3998
|
-
*/
|
|
3999
3990
|
acquireLease(id: string, lockTtlSeconds?: number): Promise<LeaseResult>;
|
|
4000
|
-
/**
|
|
3991
|
+
/** Marks the id processed. Returns false if it was already seen. */
|
|
4001
3992
|
checkAndMark(id: string, customTtlSeconds?: number): Promise<boolean>;
|
|
4002
|
-
/**
|
|
3993
|
+
/** Marks the id processed, replacing a lease with the full TTL. */
|
|
4003
3994
|
markProcessed(id: string, customTtlSeconds?: number): Promise<void>;
|
|
4004
3995
|
isProcessed(id: string): Promise<boolean>;
|
|
4005
|
-
/** Remove the idempotency marker (useful in tests or manual rollbacks). */
|
|
4006
3996
|
unmark(id: string): Promise<void>;
|
|
4007
3997
|
}
|
|
4008
3998
|
//#endregion
|
|
@@ -4141,6 +4131,11 @@ declare class PendingMessageScanner {
|
|
|
4141
4131
|
private readonly consumer;
|
|
4142
4132
|
private readonly logger?;
|
|
4143
4133
|
constructor({ redis, stream, group, consumer, logger }: PendingMessageScannerOptions);
|
|
4134
|
+
/**
|
|
4135
|
+
* Resets a held entry's idle time so no scanner reclaims it while its
|
|
4136
|
+
* handler is still running. JUSTID leaves the delivery count alone.
|
|
4137
|
+
*/
|
|
4138
|
+
touch(id: string, stream: string): Promise<void>;
|
|
4144
4139
|
getPendingCount(): Promise<number>;
|
|
4145
4140
|
/** Pending entries for one stream, or across all of them when `stream` is omitted. */
|
|
4146
4141
|
getPendingEntries(limit?: number, stream?: StreamName): Promise<PendingEntry[]>;
|
|
@@ -4161,26 +4156,14 @@ interface ProjectSettingsCacheOptions {
|
|
|
4161
4156
|
ttlMs?: number;
|
|
4162
4157
|
}
|
|
4163
4158
|
/**
|
|
4164
|
-
*
|
|
4165
|
-
*
|
|
4166
|
-
*
|
|
4167
|
-
* would put a Postgres round trip on the hot path. Projects with no overrides
|
|
4168
|
-
* are cached as well — the common case must not cost a query per message.
|
|
4169
|
-
*
|
|
4170
|
-
* The TTL bounds staleness on its own; `invalidate()` exists so a settings
|
|
4171
|
-
* change published over pub/sub applies immediately rather than at expiry.
|
|
4159
|
+
* Per-project throttle overrides, read once per notification. Projects with no
|
|
4160
|
+
* overrides are cached too. `invalidate()` lets a settings change apply before
|
|
4161
|
+
* the TTL runs out.
|
|
4172
4162
|
*/
|
|
4173
4163
|
declare class ProjectSettingsCache {
|
|
4174
4164
|
private readonly load;
|
|
4175
4165
|
private readonly cache;
|
|
4176
|
-
/**
|
|
4177
|
-
* Lookups already on the wire, keyed by project.
|
|
4178
|
-
*
|
|
4179
|
-
* The cache only fills once a query has come back, so without this a cold
|
|
4180
|
-
* project at the start of a campaign puts one query per in-flight message on
|
|
4181
|
-
* Postgres before the first answer lands — exactly when the database is
|
|
4182
|
-
* busiest. Followers wait on the leader's promise instead.
|
|
4183
|
-
*/
|
|
4166
|
+
/** Lookups in flight, so a cold project at the start of a campaign costs one query. */
|
|
4184
4167
|
private readonly inFlight;
|
|
4185
4168
|
constructor(load: (projectId: string) => Promise<ProjectThrottleSettings | null>, { maxSize, ttlMs }?: ProjectSettingsCacheOptions);
|
|
4186
4169
|
/**
|
|
@@ -4240,11 +4223,7 @@ interface ApiRateLimiterOptions {
|
|
|
4240
4223
|
/** Redis key namespace. Distinct limiters must not share one. */
|
|
4241
4224
|
keyPrefix?: string;
|
|
4242
4225
|
}
|
|
4243
|
-
/**
|
|
4244
|
-
* Computes dynamic token lease batch size based on RPM.
|
|
4245
|
-
* Targets ~5s of limit capacity so Redis is contacted at most ~12 times/min
|
|
4246
|
-
* regardless of instantaneous request rate. Bounded between 1 and maxBatchSize.
|
|
4247
|
-
*/
|
|
4226
|
+
/** About five seconds' worth of the limit, so Redis is asked ~12 times a minute. */
|
|
4248
4227
|
declare function calculateTokenBatchSize(limitRpm: number, maxBatchSize?: number): number;
|
|
4249
4228
|
declare class ApiRateLimiter {
|
|
4250
4229
|
private readonly redis;
|
|
@@ -4254,17 +4233,10 @@ declare class ApiRateLimiter {
|
|
|
4254
4233
|
private readonly keyPrefix;
|
|
4255
4234
|
private readonly leases;
|
|
4256
4235
|
constructor({ redis, windowMs, maxBatchSize, logger, keyPrefix }: ApiRateLimiterOptions);
|
|
4257
|
-
/**
|
|
4258
|
-
* Evaluates if a request for `projectId` is allowed within its `limitRpm`.
|
|
4259
|
-
* Fast-path uses local in-memory token lease (0ms Redis network latency).
|
|
4260
|
-
* Refills in the background before tokens are depleted.
|
|
4261
|
-
*/
|
|
4262
4236
|
check(projectId: string, limitRpm: number): Promise<boolean>;
|
|
4263
4237
|
private refill;
|
|
4264
4238
|
private maybePrune;
|
|
4265
|
-
/** Reset all local cached leases */
|
|
4266
4239
|
clear(): void;
|
|
4267
|
-
/** Invalidate local lease for a specific project */
|
|
4268
4240
|
invalidate(projectId: string): void;
|
|
4269
4241
|
}
|
|
4270
4242
|
//#endregion
|
|
@@ -4316,16 +4288,6 @@ declare const LUA_SCHEDULER_POLL = "\n local key = KEYS[1]\n local maxScore =
|
|
|
4316
4288
|
* address it counted stayed over its limit for good.
|
|
4317
4289
|
*/
|
|
4318
4290
|
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";
|
|
4319
|
-
/**
|
|
4320
|
-
* Two-phase idempotency lease acquisition. Checks sent/legacy, then attempts SET NX lock.
|
|
4321
|
-
* @deprecated Nothing calls the `acquireLease` command; removed in the next minor version.
|
|
4322
|
-
*/
|
|
4323
|
-
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";
|
|
4324
|
-
/**
|
|
4325
|
-
* Two-phase idempotency completion marker. Atomically sets completed + legacy and deletes lock.
|
|
4326
|
-
* @deprecated Nothing calls the `markProcessed` command; removed in the next minor version.
|
|
4327
|
-
*/
|
|
4328
|
-
declare const LUA_MARK_PROCESSED = "\n redis.call('SET', KEYS[1], '1', 'EX', ARGV[1])\n redis.call('SET', KEYS[2], '1', 'EX', ARGV[1])\n redis.call('DEL', KEYS[3])\n return 1\n";
|
|
4329
4291
|
/**
|
|
4330
4292
|
* Registers pre-compiled custom commands on an ioredis instance.
|
|
4331
4293
|
* Calls defineCommand so ioredis uses EVALSHA rather than re-transmitting
|
|
@@ -4341,17 +4303,9 @@ interface RedisClientOptions {
|
|
|
4341
4303
|
declare class RedisClient {
|
|
4342
4304
|
readonly native: Redis;
|
|
4343
4305
|
/**
|
|
4344
|
-
* One command connection per
|
|
4345
|
-
*
|
|
4346
|
-
*
|
|
4347
|
-
* reached Redis over three or four sockets, one round trip each. Sharing a
|
|
4348
|
-
* connection lets auto-pipelining merge whatever the services issue in the
|
|
4349
|
-
* same tick into one write, and Redis spends most of its time per round trip
|
|
4350
|
-
* rather than per command. Blocking reads and subscribers still duplicate
|
|
4351
|
-
* their own connections.
|
|
4352
|
-
*
|
|
4353
|
-
* The handle's `disconnect()` releases this caller's reference; the
|
|
4354
|
-
* connection closes when the last service lets go.
|
|
4306
|
+
* One command connection per URL for every service in the process, so
|
|
4307
|
+
* auto-pipelining can batch what they send in the same tick. `disconnect()`
|
|
4308
|
+
* drops this caller's reference; the last one closes the connection.
|
|
4355
4309
|
*/
|
|
4356
4310
|
static shared(options: RedisClientOptions): RedisClient;
|
|
4357
4311
|
private readonly logger?;
|
|
@@ -4586,14 +4540,8 @@ declare class LRUCache<K, V> {
|
|
|
4586
4540
|
//#region src/shared/utils.d.ts
|
|
4587
4541
|
declare function getPriorityBucket(priority: string | undefined): "critical" | "normal" | "low";
|
|
4588
4542
|
/**
|
|
4589
|
-
* Canonical
|
|
4590
|
-
*
|
|
4591
|
-
* Both the writer (the provider webhook) and the reader (the engine's
|
|
4592
|
-
* pre-dispatch gate) must agree on this, or an unsubscribe recorded as
|
|
4593
|
-
* `Bob@Example.com` will not match a send addressed to `bob@example.com` and
|
|
4594
|
-
* the person keeps receiving mail. Case folding is safe for email domains and
|
|
4595
|
-
* for the local part in every mailbox provider in practice; phone numbers and
|
|
4596
|
-
* push tokens are case-sensitive and are only trimmed.
|
|
4543
|
+
* Canonical address for suppression lookups; writers and readers must both use
|
|
4544
|
+
* it. Emails are lower-cased; phone numbers and push tokens are case-sensitive.
|
|
4597
4545
|
*/
|
|
4598
4546
|
declare function normaliseTarget(target: string): string;
|
|
4599
4547
|
//#endregion
|
|
@@ -4615,13 +4563,9 @@ interface BatchItem<T, R> {
|
|
|
4615
4563
|
reject: (reason?: any) => void;
|
|
4616
4564
|
}
|
|
4617
4565
|
/**
|
|
4618
|
-
*
|
|
4619
|
-
*
|
|
4620
|
-
*
|
|
4621
|
-
* since the first one arrived. Up to `maxConcurrent` flushes may be in flight
|
|
4622
|
-
* at once: with a single flight, every caller queued behind a slow flush holds
|
|
4623
|
-
* its worker slot for the whole round trip, which caps a worker's throughput at
|
|
4624
|
-
* `maxSize / flushLatency` no matter how much concurrency it was given.
|
|
4566
|
+
* Batches `add()` calls into `flushFn` calls, at `maxSize` items or after
|
|
4567
|
+
* `maxWaitMs`. Several flushes may run at once so one slow flush doesn't cap
|
|
4568
|
+
* the worker's throughput.
|
|
4625
4569
|
*/
|
|
4626
4570
|
declare class BatchProcessor<T, R = void> {
|
|
4627
4571
|
private readonly maxSize;
|
|
@@ -4748,25 +4692,20 @@ declare abstract class BaseWorker {
|
|
|
4748
4692
|
recover(): Promise<void>;
|
|
4749
4693
|
health(): WorkerHealth;
|
|
4750
4694
|
/**
|
|
4751
|
-
* Processes an event
|
|
4752
|
-
*
|
|
4753
|
-
*
|
|
4754
|
-
* The caller is still holding its own stream entry, and does not ack it until
|
|
4755
|
-
* this returns. A thrown error therefore leaves that entry pending and the
|
|
4756
|
-
* whole chain is replayed from the upstream stage; the idempotency keys each
|
|
4757
|
-
* stage already checks make the replay safe. Non-retryable failures go to the
|
|
4758
|
-
* dead-letter stream here, as they would have from this worker's own stream,
|
|
4759
|
-
* so they do not send the upstream message there instead.
|
|
4695
|
+
* Processes an event from an upstream stage in this process, skipping the stream.
|
|
4696
|
+
* A throw leaves the caller's entry pending, so the chain replays from upstream.
|
|
4697
|
+
* Non-retryable failures are dead-lettered here so the upstream message isn't.
|
|
4760
4698
|
*/
|
|
4761
4699
|
processInline(event: StreamEvent): Promise<void>;
|
|
4762
4700
|
private processWithTracking;
|
|
4763
4701
|
/**
|
|
4764
|
-
*
|
|
4765
|
-
*
|
|
4766
|
-
|
|
4767
|
-
|
|
4768
|
-
|
|
4769
|
-
*
|
|
4702
|
+
* Keeps the entry's idle time under the reclaim threshold while `process` runs,
|
|
4703
|
+
* so a long fan-out isn't reclaimed mid-run. If this process dies, the touches stop.
|
|
4704
|
+
*/
|
|
4705
|
+
private processHoldingLease;
|
|
4706
|
+
/**
|
|
4707
|
+
* `recoveryIntervalMs` is the idle time before a claim, not the scan period.
|
|
4708
|
+
* Scan on start and several times per interval so crash-looping workers still recover.
|
|
4770
4709
|
*/
|
|
4771
4710
|
private startRecoveryLoop;
|
|
4772
4711
|
private stopRecoveryLoop;
|
|
@@ -4880,18 +4819,10 @@ declare function registerTransport(transport: Transport, priority?: number): voi
|
|
|
4880
4819
|
//#endregion
|
|
4881
4820
|
//#region src/unsubscribe/index.d.ts
|
|
4882
4821
|
/**
|
|
4883
|
-
* One-click unsubscribe (RFC 8058).
|
|
4884
|
-
*
|
|
4885
|
-
* Gmail and Yahoo have required `List-Unsubscribe` + `List-Unsubscribe-Post` on
|
|
4886
|
-
* bulk mail since early 2024; without them bulk sends get throttled or junked,
|
|
4887
|
-
* and because deliverability is reputation on the *sending domain*, that
|
|
4888
|
-
* eventually drags transactional mail down with it.
|
|
4822
|
+
* One-click unsubscribe (RFC 8058), required by Gmail and Yahoo for bulk mail.
|
|
4889
4823
|
*
|
|
4890
|
-
* The
|
|
4891
|
-
*
|
|
4892
|
-
* stored per message, and there is no expiry: an unsubscribe link that has
|
|
4893
|
-
* stopped working is worse than useless, because the recipient's next move is
|
|
4894
|
-
* the spam button.
|
|
4824
|
+
* The token is signed, not stored, and never expires: the link has to keep
|
|
4825
|
+
* working from an old email, and a dead one sends people to the spam button.
|
|
4895
4826
|
*/
|
|
4896
4827
|
interface UnsubscribeClaim {
|
|
4897
4828
|
/** Project the send belonged to. */
|
|
@@ -5292,4 +5223,4 @@ declare class NotifkitServer extends EventEmitter {
|
|
|
5292
5223
|
stop(): Promise<void>;
|
|
5293
5224
|
}
|
|
5294
5225
|
//#endregion
|
|
5295
|
-
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,
|
|
5226
|
+
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_COUNT_REQUEST, LUA_LEASE_API_RATE_LIMIT, 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, redactUrl, 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
|
|
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,
|
|
1
|
+
import { $ as migrateDatabase, $t as STREAMS, A as LUA_COUNT_REQUEST, At as AddUserSchema, B as ApiRateLimiter, Bt as TemplateSchema, C as SegmentRepository, Cn as BatchProcessor, Ct as NotificationScheduledPayloadSchema, D as ContactRepository, Dn as LRUCache, Dt as NotificationRequestedPayloadSchema, E as TemplateRepository, En as normaliseTarget, Et as NotificationCreatedPayloadSchema, F as LUA_USER_THROTTLE, Ft as InlineUserSchema, G as StreamConsumer, Gt as WorkflowNotifyPayloadSchema, H as UserThrottle, Ht as UpdateProjectSchema, I as Redis, It as NotifyRequestSchema, J as metrics, Jt as CONSUMER_GROUPS, K as StreamProducer, Kt as WorkflowStepSchema, L as RedisClient, Lt as PreferencesSchema, M as LUA_RELEASE_LOCK, Mt as ContactChannelSchema, N as LUA_RENEW_LOCK, Nt as CreateWorkflowSchema, O as PreferenceRepository, On as globalEmitter, Ot as NotificationTargetSchema, P as LUA_SCHEDULER_POLL, Pt as IngestEventSchema, Q as createDatabase, Qt as PUBSUB_CHANNELS, R as redactUrl, Rt as QuietHoursSchema, S as AdminUserRepository, Sn as CircuitBreaker, St as RenderedContentSchema, T as ProjectRepository, Tn as getPriorityBucket, Tt as RecipientProfileSchema, U as ProjectSettingsCache, Ut as UpdateUserSchema, V as calculateTokenBatchSize, 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 UserRepository, 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 WorkflowRepository, 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-C38w4jR4.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_COUNT_REQUEST, LUA_LEASE_API_RATE_LIMIT, 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, redactUrl, registerCustomCommands, registerTransport, registry, renderWithTemplate, resolveStepTarget, runMigrations, setAiConfig, setGlobalConfig, signUnsubscribeToken, sleep, startHealthReporter, transportRegistry, verifyUnsubscribeToken, workflow, workflowRegistry };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { t as scheduleOrDispatch } from "./schedule-or-dispatch-
|
|
3
|
-
import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime, t as createPriorityProducers } from "./bootstrap-
|
|
1
|
+
import { $t as STREAMS, D as ContactRepository, Dn as LRUCache, E as TemplateRepository, En as normaliseTarget, H as UserThrottle, Jt as CONSUMER_GROUPS, K as StreamProducer, On as globalEmitter, Qt as PUBSUB_CHANNELS, T as ProjectRepository, U as ProjectSettingsCache, X as IdempotencyGuard, Yt as ENRICHED_STREAMS, _ as TemplateCache, c as BaseWorker, ct as suppressions, d as startHealthReporter, f as buildUnsubscribeHeaders, fn as loadEnv, in as registry, mn as readBaseConfig, qt as buildStreamEvent, xn as DataLoader } from "./src-C38w4jR4.mjs";
|
|
2
|
+
import { t as scheduleOrDispatch } from "./schedule-or-dispatch-BwCZWS_P.mjs";
|
|
3
|
+
import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime, t as createPriorityProducers } from "./bootstrap-Cs1XIp6b.mjs";
|
|
4
4
|
import { randomUUID } from "node:crypto";
|
|
5
5
|
import { and, eq } from "drizzle-orm";
|
|
6
6
|
//#region src/services/engine/quiet-hours.ts
|
|
@@ -174,15 +174,6 @@ var EngineWorker = class extends BaseWorker {
|
|
|
174
174
|
batchDelayMs: 2,
|
|
175
175
|
maxBatchSize: 500
|
|
176
176
|
});
|
|
177
|
-
/**
|
|
178
|
-
* Suppressed destinations for one (project, channel), as a normalised set.
|
|
179
|
-
*
|
|
180
|
-
* Loaded per project+channel rather than per address: a campaign is thousands
|
|
181
|
-
* of messages against one channel, so this collapses to a single query for
|
|
182
|
-
* the whole batch. Cached for the loader's lifetime of a tick, which means a
|
|
183
|
-
* suppression written mid-batch takes effect on the next batch — acceptable,
|
|
184
|
-
* since the webhook that writes it is itself minutes behind the send.
|
|
185
|
-
*/
|
|
186
177
|
suppressionsCache = new LRUCache(1e3, 1e4);
|
|
187
178
|
suppressionsLoader = new DataLoader(async (keys) => {
|
|
188
179
|
const results = /* @__PURE__ */ new Map();
|
|
@@ -228,7 +219,7 @@ var EngineWorker = class extends BaseWorker {
|
|
|
228
219
|
if (msUntil > 0) customTtl = config.IDEMPOTENCY_TTL_SECONDS + Math.ceil(msUntil / 1e3);
|
|
229
220
|
}
|
|
230
221
|
const idempotency = message.inline ? NO_IDEMPOTENCY : this.idempotency;
|
|
231
|
-
if (!await idempotency.checkAndMark(idempotencyKey,
|
|
222
|
+
if (!await idempotency.checkAndMark(idempotencyKey, 60)) {
|
|
232
223
|
this.logger.debug({
|
|
233
224
|
messageId: message.id,
|
|
234
225
|
eventId: event.id
|
|
@@ -502,7 +493,7 @@ async function startEngineWorker() {
|
|
|
502
493
|
});
|
|
503
494
|
const throttle = new UserThrottle({
|
|
504
495
|
redis: redis.native,
|
|
505
|
-
maxPerHour:
|
|
496
|
+
maxPerHour: config.RATE_LIMIT_PER_HOUR
|
|
506
497
|
});
|
|
507
498
|
const projectRepo = new ProjectRepository(db);
|
|
508
499
|
const projectSettings = new ProjectSettingsCache((projectId) => projectRepo.findThrottleSettings(projectId));
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { t as scheduleOrDispatch } from "./schedule-or-dispatch-
|
|
3
|
-
import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime, t as createPriorityProducers } from "./bootstrap-
|
|
1
|
+
import { $t as STREAMS, E as TemplateRepository, Jt as CONSUMER_GROUPS, K as StreamProducer, On as globalEmitter, Qt as PUBSUB_CHANNELS, R as redactUrl, 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, x as renderWithTemplate } from "./src-C38w4jR4.mjs";
|
|
2
|
+
import { t as scheduleOrDispatch } from "./schedule-or-dispatch-BwCZWS_P.mjs";
|
|
3
|
+
import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime, t as createPriorityProducers } from "./bootstrap-Cs1XIp6b.mjs";
|
|
4
4
|
import { generateText } from "ai";
|
|
5
5
|
//#region src/services/ai/main.ts
|
|
6
6
|
loadEnv();
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime } from "./bootstrap-
|
|
3
|
-
import { t as insertMessageLogs } from "./bulk-
|
|
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-C38w4jR4.mjs";
|
|
2
|
+
import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime } from "./bootstrap-Cs1XIp6b.mjs";
|
|
3
|
+
import { t as insertMessageLogs } from "./bulk-4pRIiG2o.mjs";
|
|
4
4
|
import { and, eq, gt, isNull, or } from "drizzle-orm";
|
|
5
5
|
//#region src/services/events/main.ts
|
|
6
6
|
loadEnv();
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { $t as
|
|
2
|
-
import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime, t as createPriorityProducers } from "./bootstrap-
|
|
3
|
-
import { n as upsertOutboxProviderIds } from "./bulk-
|
|
1
|
+
import { $t as STREAMS, B as ApiRateLimiter, Cn as BatchProcessor, D as ContactRepository, E as TemplateRepository, J as metrics, Jt as CONSUMER_GROUPS, K as StreamProducer, On as globalEmitter, Qt as PUBSUB_CHANNELS, Sn as CircuitBreaker, Tn as getPriorityBucket, X as IdempotencyGuard, Zt as OUTBOUND_STREAMS, _ as TemplateCache, c as BaseWorker, d as startHealthReporter, fn as loadEnv, g as transportRegistry, in as registry, mn as readBaseConfig, qt as buildStreamEvent, st as scheduledPayloads, u as NonRetryableError, x as renderWithTemplate } from "./src-C38w4jR4.mjs";
|
|
2
|
+
import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime, t as createPriorityProducers } from "./bootstrap-Cs1XIp6b.mjs";
|
|
3
|
+
import { n as upsertOutboxProviderIds } from "./bulk-4pRIiG2o.mjs";
|
|
4
4
|
import { sql } from "drizzle-orm";
|
|
5
5
|
//#region src/services/delivery/live-events.ts
|
|
6
6
|
/**
|
|
@@ -117,30 +117,12 @@ let subscriber = null;
|
|
|
117
117
|
let stopLiveEvents = null;
|
|
118
118
|
/** How long a send may run before its dispatched log entry is published alone. */
|
|
119
119
|
const DISPATCH_LOG_DELAY_MS = 1e3;
|
|
120
|
-
/**
|
|
121
|
-
* Wait before re-trying a send that failed at the provider, by prior failures.
|
|
122
|
-
* The first step matches the circuit breaker's reset timeout, so a retry of a
|
|
123
|
-
* task the open breaker turned away reaches the provider's recovery probe
|
|
124
|
-
* rather than the same closed door.
|
|
125
|
-
*/
|
|
126
120
|
const RETRY_BACKOFF_MS = [
|
|
127
121
|
3e4,
|
|
128
122
|
12e4,
|
|
129
123
|
6e5
|
|
130
124
|
];
|
|
131
|
-
/**
|
|
132
|
-
* How many times a send may be put back for want of provider capacity before
|
|
133
|
-
* it is failed. Separate from `maxAttempts`, which counts sends the provider
|
|
134
|
-
* refused: waiting in line is not a failed attempt, and a campaign several
|
|
135
|
-
* windows deeper than the provider's limit has to be able to wait that long.
|
|
136
|
-
*/
|
|
137
125
|
const MAX_THROTTLE_DEFERRALS = 50;
|
|
138
|
-
/**
|
|
139
|
-
* Names the provider at `transports[i]` for its rate limit and circuit breaker.
|
|
140
|
-
* The rate limit's budget lives in Redis and is shared by every delivery
|
|
141
|
-
* process, so the name must come out the same everywhere: the class name,
|
|
142
|
-
* numbered when a channel has two providers of one class.
|
|
143
|
-
*/
|
|
144
126
|
function providerKey(channel, transports, i) {
|
|
145
127
|
const name = transports[i].constructor?.name ?? "provider";
|
|
146
128
|
const nth = transports.slice(0, i).filter((t) => t.constructor?.name === name).length;
|
|
@@ -166,7 +148,6 @@ var DeliveryWorker = class extends BaseWorker {
|
|
|
166
148
|
db;
|
|
167
149
|
eventProcessor;
|
|
168
150
|
outboxUpdateProcessor;
|
|
169
|
-
outboxInsertProcessor;
|
|
170
151
|
breakers = /* @__PURE__ */ new Map();
|
|
171
152
|
providerThrottle;
|
|
172
153
|
templateCache;
|
|
@@ -198,40 +179,19 @@ var DeliveryWorker = class extends BaseWorker {
|
|
|
198
179
|
await upsertOutboxProviderIds(this.db, values).catch((e) => this.logger.error({ err: e }, "background update failed"));
|
|
199
180
|
return updates.map(() => void 0);
|
|
200
181
|
}, flushConcurrency);
|
|
201
|
-
this.outboxInsertProcessor = new BatchProcessor(batchSize, 5, async (tasks) => {
|
|
202
|
-
const values = tasks.map((task) => ({
|
|
203
|
-
taskId: task.taskId,
|
|
204
|
-
channel: task.channel,
|
|
205
|
-
destination: task.destination
|
|
206
|
-
}));
|
|
207
|
-
await this.db.insert(deliveryOutbox).values(values).onConflictDoNothing();
|
|
208
|
-
return tasks.map(() => true);
|
|
209
|
-
});
|
|
210
182
|
}
|
|
211
183
|
async stop() {
|
|
212
|
-
await Promise.all([
|
|
213
|
-
this.eventProcessor.flush(),
|
|
214
|
-
this.outboxUpdateProcessor.flush(),
|
|
215
|
-
this.outboxInsertProcessor.flush()
|
|
216
|
-
]);
|
|
184
|
+
await Promise.all([this.eventProcessor.flush(), this.outboxUpdateProcessor.flush()]);
|
|
217
185
|
await super.stop();
|
|
218
186
|
}
|
|
219
|
-
/**
|
|
220
|
-
* Renders a task the engine dispatched unrendered. Done here, after the
|
|
221
|
-
* throttle and the lease, so the body is built once per actual send and never
|
|
222
|
-
* travels through the outbound stream.
|
|
223
|
-
*/
|
|
187
|
+
/** Rendered after the throttle and lease, so once per real send. */
|
|
224
188
|
async render(task) {
|
|
225
189
|
if (!task.templateId) return renderWithTemplate(null, task.templateVariables);
|
|
226
190
|
const template = this.templateCache ? await this.templateCache.getCachedTemplate(task.projectId, task.templateId) : null;
|
|
227
191
|
if (!template) throw new NonRetryableError(`template "${task.templateId}" not found at send time`);
|
|
228
192
|
return renderWithTemplate(template, task.templateVariables);
|
|
229
193
|
}
|
|
230
|
-
/**
|
|
231
|
-
* Parks a task in Postgres and the scheduler until `sendAt`. The payload
|
|
232
|
-
* carries the send time, so the scheduler can rebuild it if Redis loses its
|
|
233
|
-
* data.
|
|
234
|
-
*/
|
|
194
|
+
/** Parks a task until `sendAt`. Postgres keeps the send time so Redis can be rebuilt. */
|
|
235
195
|
async reschedule(task, sendAt, reason) {
|
|
236
196
|
const scheduledAt = sendAt.toISOString();
|
|
237
197
|
await this.db.insert(scheduledPayloads).values({
|
|
@@ -318,11 +278,7 @@ var DeliveryWorker = class extends BaseWorker {
|
|
|
318
278
|
dispatchedAt
|
|
319
279
|
};
|
|
320
280
|
};
|
|
321
|
-
/**
|
|
322
|
-
* Queues the task's `notification.failed` event; once sending has started it
|
|
323
|
-
* is the attempt's outcome. Only the non-push provider failure leaves out
|
|
324
|
-
* `recipientId`.
|
|
325
|
-
*/
|
|
281
|
+
/** Queues `notification.failed`. Only the non-push provider failure omits recipientId. */
|
|
326
282
|
const failed = (failureReason, failureCode, withRecipientId = true) => {
|
|
327
283
|
const payload = {
|
|
328
284
|
projectId: task.projectId,
|
|
@@ -450,13 +406,7 @@ var DeliveryWorker = class extends BaseWorker {
|
|
|
450
406
|
const tDispatchStart = Date.now();
|
|
451
407
|
const MAX_DISPATCH_BUDGET_MS = 25e3;
|
|
452
408
|
let retryScheduled = false;
|
|
453
|
-
/**
|
|
454
|
-
* Re-queues a send the provider turned away, unless it said the failure is
|
|
455
|
-
* permanent or the task is out of attempts. An outage, a timeout or an open
|
|
456
|
-
* circuit is exactly what a later attempt can get past; failing the task
|
|
457
|
-
* outright turned every blip into lost mail. The attempt stays in the log as
|
|
458
|
-
* dispatched, and the task's outcome is whatever its last attempt comes to.
|
|
459
|
-
*/
|
|
409
|
+
/** Re-queues a failed send unless the provider called it permanent or attempts ran out. */
|
|
460
410
|
const retryLater = async (failure) => {
|
|
461
411
|
const maxAttempts = task.deliveryOptions?.maxAttempts ?? 3;
|
|
462
412
|
if (failure.retryable === false || attemptNo >= maxAttempts) return false;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime } from "./bootstrap-
|
|
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-C38w4jR4.mjs";
|
|
2
|
+
import { a as uniqueConsumerId, i as shutdownWorker, n as createStreamConsumer, r as createWorkerRuntime } from "./bootstrap-Cs1XIp6b.mjs";
|
|
3
3
|
import { randomUUID } from "node:crypto";
|
|
4
4
|
import { and, eq } from "drizzle-orm";
|
|
5
5
|
//#region src/services/workflow/timers.ts
|
|
@@ -51,7 +51,7 @@ async function reapStuckInstances({ redis, db, logger }) {
|
|
|
51
51
|
for (const inst of runningInstances) {
|
|
52
52
|
const lockKey = `lock:workflow:${inst.id}`;
|
|
53
53
|
if (!await redis.native.exists(lockKey)) {
|
|
54
|
-
await db.update(workflowInstances).set({ status: "pending" }).where(eq(workflowInstances.id, inst.id));
|
|
54
|
+
await db.update(workflowInstances).set({ status: "pending" }).where(and(eq(workflowInstances.id, inst.id), eq(workflowInstances.status, "running")));
|
|
55
55
|
logger.info({ instanceId: inst.id }, "Reaped stuck workflow instance (lock expired)");
|
|
56
56
|
}
|
|
57
57
|
}
|
|
@@ -69,12 +69,7 @@ const DURATION_UNIT_SECONDS = {
|
|
|
69
69
|
h: 3600,
|
|
70
70
|
d: 86400
|
|
71
71
|
};
|
|
72
|
-
/**
|
|
73
|
-
* `"500ms"`, `"0.5s"`, `"15m"`, `"1.5h"`, `"2.5d"` to seconds. Decimals are
|
|
74
|
-
* read exactly; anything that is not a number and a unit throws. The old
|
|
75
|
-
* parser matched on the last character, so "500ms" meant 500 seconds, and ran
|
|
76
|
-
* parseInt, so "1.5h" meant one hour.
|
|
77
|
-
*/
|
|
72
|
+
/** `"500ms"`, `"1.5h"`, `"2.5d"` to seconds. Anything else throws. */
|
|
78
73
|
function parseDurationSeconds(value, what) {
|
|
79
74
|
const match = /^(\d+(?:\.\d+)?|\.\d+)(ms|s|m|h|d)$/.exec(value.trim());
|
|
80
75
|
if (!match) throw new Error(`Invalid ${what}: ${value}`);
|