notifkit 0.1.9 → 0.1.11
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 +66 -72
- package/dashboard/dist/assets/{index-7mvXb4bS.js → index-Bsrmo9LE.js} +1 -1
- package/dashboard/dist/assets/index-s-SXGIwv.css +1 -0
- package/dashboard/dist/index.html +2 -2
- package/dist/bootstrap-GRXzgPvW.mjs +89 -0
- package/dist/bulk-BvRfz6Of.mjs +77 -0
- package/dist/index.d.mts +1107 -8885
- package/dist/index.mjs +2 -38
- package/dist/{main-DSmh1e-V.mjs → main-BENOY7HG.mjs} +49 -75
- package/dist/{main-BPbPnBIG.mjs → main-BpnuFX62.mjs} +119 -112
- package/dist/{main-4Wyge9ms.mjs → main-BqVH2BHj.mjs} +57 -54
- package/dist/{main-D19Wxs0x.mjs → main-C_5VFIwp.mjs} +135 -79
- package/dist/main-D6od9N8h.mjs +312 -0
- package/dist/main-D84OX0ux.mjs +811 -0
- package/dist/{main-JdOvJ70b.mjs → main-HH-WwsMM.mjs} +395 -184
- package/dist/{main-AO0JaY5z.mjs → main-HY_TDs61.mjs} +149 -134
- package/dist/{src-CGmXRfsM.mjs → src-CI7FGf3h.mjs} +1223 -651
- package/drizzle/0006_message_logs_redundant_indexes.sql +7 -0
- package/drizzle/0007_workflow_status_canceled.sql +3 -0
- package/drizzle/meta/_journal.json +14 -0
- package/package.json +17 -17
- package/scripts/create-admin.mjs +8 -11
- package/src/client.ts +15 -5
- package/src/config/index.ts +26 -3
- package/src/contracts/events/notification-ai-pending.ts +12 -0
- package/src/contracts/events/notification-delivered.ts +7 -0
- package/src/contracts/events/notification-dispatched.ts +21 -2
- package/src/contracts/events/notification-enriched.ts +16 -0
- package/src/contracts/events/notification-failed.ts +2 -0
- package/src/contracts/events/notification-requested.ts +2 -0
- package/src/contracts/registry.ts +2 -2
- package/src/contracts/sdk.ts +77 -29
- package/src/db/bulk.ts +132 -0
- package/src/db/index.ts +2 -2
- package/src/db/schema.ts +3 -50
- package/src/idempotency/index.ts +60 -6
- package/src/logger/index.ts +0 -12
- package/src/queue/index.ts +255 -76
- package/src/rate-limiter/index.ts +217 -16
- package/src/redis/index.ts +265 -0
- package/src/repositories/index.ts +171 -82
- package/src/server.ts +71 -41
- package/src/services/ai/main.ts +83 -70
- package/src/services/api/admin-static.ts +41 -12
- package/src/services/api/handlers.ts +357 -159
- package/src/services/api/http.ts +34 -7
- package/src/services/api/main.ts +104 -67
- package/src/services/api/router.ts +18 -5
- package/src/services/delivery/main.ts +495 -230
- package/src/services/delivery/throttle.ts +42 -59
- package/src/services/engine/main.ts +206 -152
- package/src/services/enricher/main.ts +155 -107
- package/src/services/events/main.ts +100 -55
- package/src/services/scheduler/main.ts +151 -79
- package/src/services/scheduler/send-gate.ts +117 -0
- package/src/services/workflow/main.ts +220 -100
- package/src/shared/batch-processor.ts +65 -34
- package/src/shared/dataloader.ts +58 -22
- package/src/shared/events.ts +3 -0
- package/src/shared/index.ts +0 -1
- package/src/shared/utils.ts +2 -47
- package/src/templates/index.ts +0 -67
- package/src/transport/index.ts +6 -0
- package/src/workers/bootstrap.ts +124 -0
- package/src/workers/index.ts +99 -24
- package/src/workflows/sdk.ts +2 -0
- package/dashboard/dist/assets/index-o7L-f3-c.css +0 -1
- package/dist/index.d.mts.map +0 -1
- package/dist/index.mjs.map +0 -1
- package/dist/main-4Wyge9ms.mjs.map +0 -1
- package/dist/main-AO0JaY5z.mjs.map +0 -1
- package/dist/main-BMYSYcv2.mjs +0 -224
- package/dist/main-BMYSYcv2.mjs.map +0 -1
- package/dist/main-BPbPnBIG.mjs.map +0 -1
- package/dist/main-CyeQ_yDt.mjs +0 -632
- package/dist/main-CyeQ_yDt.mjs.map +0 -1
- package/dist/main-D19Wxs0x.mjs.map +0 -1
- package/dist/main-DSmh1e-V.mjs.map +0 -1
- package/dist/main-JdOvJ70b.mjs.map +0 -1
- package/dist/src-CGmXRfsM.mjs.map +0 -1
|
@@ -1,34 +1,314 @@
|
|
|
1
1
|
import { t as __exportAll } from "./rolldown-runtime-D7D4PA-g.mjs";
|
|
2
|
-
import { registerTransport } from "./index.mjs";
|
|
3
2
|
import { config } from "dotenv";
|
|
4
3
|
import { resolve } from "node:path";
|
|
5
4
|
import { z } from "zod";
|
|
6
5
|
import { createHmac, randomUUID, timingSafeEqual } from "node:crypto";
|
|
6
|
+
import { EventEmitter } from "node:events";
|
|
7
7
|
import postgres from "postgres";
|
|
8
8
|
import { drizzle } from "drizzle-orm/postgres-js";
|
|
9
9
|
import { boolean, check, index, integer, jsonb, pgEnum, pgTable, primaryKey, text, time, timestamp, unique, uuid, varchar } from "drizzle-orm/pg-core";
|
|
10
10
|
import { and, desc, eq, inArray, or, sql } from "drizzle-orm";
|
|
11
|
-
import { createInsertSchema, createSelectSchema } from "drizzle-zod";
|
|
12
11
|
import { migrate } from "drizzle-orm/postgres-js/migrator";
|
|
13
12
|
import { fileURLToPath } from "url";
|
|
14
13
|
import path from "path";
|
|
15
14
|
import fs from "fs";
|
|
16
15
|
import pino from "pino";
|
|
17
16
|
import promClient from "prom-client";
|
|
18
|
-
import { EventEmitter } from "node:events";
|
|
19
17
|
import { randomUUID as randomUUID$1 } from "crypto";
|
|
20
18
|
import { Redis } from "ioredis";
|
|
19
|
+
//#region src/shared/events.ts
|
|
20
|
+
const globalEmitter = new EventEmitter();
|
|
21
|
+
globalEmitter.setMaxListeners(0);
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region src/shared/cache.ts
|
|
24
|
+
var LRUCache = class {
|
|
25
|
+
cache = /* @__PURE__ */ new Map();
|
|
26
|
+
maxSize;
|
|
27
|
+
defaultTtlMs;
|
|
28
|
+
constructor(maxSize = 1e3, defaultTtlMs = 3e5) {
|
|
29
|
+
this.maxSize = maxSize;
|
|
30
|
+
this.defaultTtlMs = defaultTtlMs;
|
|
31
|
+
}
|
|
32
|
+
get(key) {
|
|
33
|
+
const item = this.cache.get(key);
|
|
34
|
+
if (!item) return void 0;
|
|
35
|
+
if (Date.now() > item.expiresAt) {
|
|
36
|
+
this.cache.delete(key);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
this.cache.delete(key);
|
|
40
|
+
this.cache.set(key, item);
|
|
41
|
+
return item.value;
|
|
42
|
+
}
|
|
43
|
+
set(key, value, ttlMs = this.defaultTtlMs) {
|
|
44
|
+
if (this.cache.has(key)) this.cache.delete(key);
|
|
45
|
+
else if (this.cache.size >= this.maxSize) {
|
|
46
|
+
const oldestKey = this.cache.keys().next().value;
|
|
47
|
+
if (oldestKey !== void 0) this.cache.delete(oldestKey);
|
|
48
|
+
}
|
|
49
|
+
this.cache.set(key, {
|
|
50
|
+
value,
|
|
51
|
+
expiresAt: Date.now() + ttlMs
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
delete(key) {
|
|
55
|
+
this.cache.delete(key);
|
|
56
|
+
}
|
|
57
|
+
clear() {
|
|
58
|
+
this.cache.clear();
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
//#endregion
|
|
62
|
+
//#region src/shared/utils.ts
|
|
63
|
+
function getPriorityBucket(priority) {
|
|
64
|
+
const p = priority || "normal";
|
|
65
|
+
return p === "critical" ? "critical" : p === "low" ? "low" : "normal";
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Canonical form of a destination, for suppression lookups.
|
|
69
|
+
*
|
|
70
|
+
* Both the writer (the provider webhook) and the reader (the engine's
|
|
71
|
+
* pre-dispatch gate) must agree on this, or an unsubscribe recorded as
|
|
72
|
+
* `Bob@Example.com` will not match a send addressed to `bob@example.com` and
|
|
73
|
+
* the person keeps receiving mail. Case folding is safe for email domains and
|
|
74
|
+
* for the local part in every mailbox provider in practice; phone numbers and
|
|
75
|
+
* push tokens are case-sensitive and are only trimmed.
|
|
76
|
+
*/
|
|
77
|
+
function normaliseTarget(target) {
|
|
78
|
+
const trimmed = target.trim();
|
|
79
|
+
return trimmed.includes("@") ? trimmed.toLowerCase() : trimmed;
|
|
80
|
+
}
|
|
81
|
+
//#endregion
|
|
82
|
+
//#region src/shared/semaphore.ts
|
|
83
|
+
var AsyncSemaphore = class {
|
|
84
|
+
max;
|
|
85
|
+
count = 0;
|
|
86
|
+
queue = [];
|
|
87
|
+
constructor(max) {
|
|
88
|
+
this.max = max;
|
|
89
|
+
}
|
|
90
|
+
async acquire() {
|
|
91
|
+
if (this.count < this.max) {
|
|
92
|
+
this.count++;
|
|
93
|
+
return Promise.resolve();
|
|
94
|
+
}
|
|
95
|
+
return new Promise((resolve) => {
|
|
96
|
+
this.queue.push(resolve);
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
release() {
|
|
100
|
+
if (this.queue.length > 0) this.queue.shift()();
|
|
101
|
+
else if (this.count > 0) this.count--;
|
|
102
|
+
}
|
|
103
|
+
get activeCount() {
|
|
104
|
+
return this.count;
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
//#endregion
|
|
108
|
+
//#region src/shared/batch-processor.ts
|
|
109
|
+
/**
|
|
110
|
+
* Coalesces individual `add()` calls into batched `flushFn` calls.
|
|
111
|
+
*
|
|
112
|
+
* A batch goes out when `maxSize` items are waiting or `maxWaitMs` has passed
|
|
113
|
+
* since the first one arrived. Up to `maxConcurrent` flushes may be in flight
|
|
114
|
+
* at once: with a single flight, every caller queued behind a slow flush holds
|
|
115
|
+
* its worker slot for the whole round trip, which caps a worker's throughput at
|
|
116
|
+
* `maxSize / flushLatency` no matter how much concurrency it was given.
|
|
117
|
+
*/
|
|
118
|
+
var BatchProcessor = class {
|
|
119
|
+
maxSize;
|
|
120
|
+
maxWaitMs;
|
|
121
|
+
flushFn;
|
|
122
|
+
maxConcurrent;
|
|
123
|
+
buffer = [];
|
|
124
|
+
timer = null;
|
|
125
|
+
inFlight = /* @__PURE__ */ new Set();
|
|
126
|
+
constructor(maxSize, maxWaitMs, flushFn, maxConcurrent = 1) {
|
|
127
|
+
this.maxSize = maxSize;
|
|
128
|
+
this.maxWaitMs = maxWaitMs;
|
|
129
|
+
this.flushFn = flushFn;
|
|
130
|
+
this.maxConcurrent = maxConcurrent;
|
|
131
|
+
}
|
|
132
|
+
add(item) {
|
|
133
|
+
return new Promise((resolve, reject) => {
|
|
134
|
+
this.buffer.push({
|
|
135
|
+
item,
|
|
136
|
+
resolve,
|
|
137
|
+
reject
|
|
138
|
+
});
|
|
139
|
+
if (this.buffer.length >= this.maxSize && this.inFlight.size < this.maxConcurrent) {
|
|
140
|
+
this.clearTimer();
|
|
141
|
+
this.flushOnce();
|
|
142
|
+
} else if (!this.timer && this.inFlight.size < this.maxConcurrent) this.armTimer();
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
/** Flushes everything buffered, waiting for a free flush slot if needed. */
|
|
146
|
+
async flush() {
|
|
147
|
+
while (this.buffer.length > 0) {
|
|
148
|
+
while (this.inFlight.size >= this.maxConcurrent) await Promise.race(this.inFlight);
|
|
149
|
+
this.clearTimer();
|
|
150
|
+
await this.flushOnce();
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
flushOnce() {
|
|
154
|
+
if (this.buffer.length === 0 || this.inFlight.size >= this.maxConcurrent) return Promise.resolve();
|
|
155
|
+
const batch = this.buffer;
|
|
156
|
+
this.buffer = [];
|
|
157
|
+
const run = (async () => {
|
|
158
|
+
try {
|
|
159
|
+
const results = await this.flushFn(batch.map((b) => b.item));
|
|
160
|
+
for (let i = 0; i < batch.length; i++) batch[i].resolve(results[i]);
|
|
161
|
+
} catch (err) {
|
|
162
|
+
for (const b of batch) b.reject(err);
|
|
163
|
+
}
|
|
164
|
+
})();
|
|
165
|
+
this.inFlight.add(run);
|
|
166
|
+
return run.finally(() => {
|
|
167
|
+
this.inFlight.delete(run);
|
|
168
|
+
if (this.buffer.length === 0 || this.timer) return;
|
|
169
|
+
if (this.buffer.length >= this.maxSize) this.flushOnce();
|
|
170
|
+
else this.armTimer();
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
armTimer() {
|
|
174
|
+
this.timer = setTimeout(() => {
|
|
175
|
+
this.timer = null;
|
|
176
|
+
this.flushOnce();
|
|
177
|
+
}, this.maxWaitMs);
|
|
178
|
+
}
|
|
179
|
+
clearTimer() {
|
|
180
|
+
if (this.timer) {
|
|
181
|
+
clearTimeout(this.timer);
|
|
182
|
+
this.timer = null;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
//#endregion
|
|
187
|
+
//#region src/shared/circuit-breaker.ts
|
|
188
|
+
var CircuitBreaker = class {
|
|
189
|
+
state = "CLOSED";
|
|
190
|
+
failures = 0;
|
|
191
|
+
nextAttemptAt = 0;
|
|
192
|
+
/** True while one caller is testing whether the dependency has recovered. */
|
|
193
|
+
probeInFlight = false;
|
|
194
|
+
threshold;
|
|
195
|
+
timeout;
|
|
196
|
+
constructor(options) {
|
|
197
|
+
this.threshold = options.failureThreshold;
|
|
198
|
+
this.timeout = options.resetTimeoutMs;
|
|
199
|
+
}
|
|
200
|
+
async execute(action) {
|
|
201
|
+
let isProbe = false;
|
|
202
|
+
if (this.state === "OPEN") if (Date.now() > this.nextAttemptAt && !this.probeInFlight) {
|
|
203
|
+
this.state = "HALF_OPEN";
|
|
204
|
+
this.probeInFlight = true;
|
|
205
|
+
isProbe = true;
|
|
206
|
+
} else throw new Error("Circuit breaker is OPEN");
|
|
207
|
+
else if (this.state === "HALF_OPEN") {
|
|
208
|
+
if (this.probeInFlight) throw new Error("Circuit breaker is OPEN");
|
|
209
|
+
this.probeInFlight = true;
|
|
210
|
+
isProbe = true;
|
|
211
|
+
}
|
|
212
|
+
try {
|
|
213
|
+
const result = await action();
|
|
214
|
+
this.onSuccess();
|
|
215
|
+
return result;
|
|
216
|
+
} catch (err) {
|
|
217
|
+
this.onFailure();
|
|
218
|
+
throw err;
|
|
219
|
+
} finally {
|
|
220
|
+
if (isProbe) this.probeInFlight = false;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
onSuccess() {
|
|
224
|
+
this.failures = 0;
|
|
225
|
+
this.state = "CLOSED";
|
|
226
|
+
}
|
|
227
|
+
onFailure() {
|
|
228
|
+
this.failures++;
|
|
229
|
+
if (this.failures >= this.threshold) {
|
|
230
|
+
this.state = "OPEN";
|
|
231
|
+
this.nextAttemptAt = Date.now() + this.timeout;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
getState() {
|
|
235
|
+
return this.state;
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
//#endregion
|
|
239
|
+
//#region src/shared/dataloader.ts
|
|
240
|
+
var DataLoader = class {
|
|
241
|
+
batchLoadFn;
|
|
242
|
+
keys = [];
|
|
243
|
+
promises = [];
|
|
244
|
+
scheduled = false;
|
|
245
|
+
timer = null;
|
|
246
|
+
batchDelayMs;
|
|
247
|
+
maxBatchSize;
|
|
248
|
+
constructor(batchLoadFn, { batchDelayMs = 0, maxBatchSize = Infinity } = {}) {
|
|
249
|
+
this.batchLoadFn = batchLoadFn;
|
|
250
|
+
this.batchDelayMs = batchDelayMs;
|
|
251
|
+
this.maxBatchSize = maxBatchSize;
|
|
252
|
+
}
|
|
253
|
+
load(key) {
|
|
254
|
+
return new Promise((resolve, reject) => {
|
|
255
|
+
this.keys.push(key);
|
|
256
|
+
this.promises.push({ resolve: (value) => {
|
|
257
|
+
if (value instanceof Error) reject(value);
|
|
258
|
+
else resolve(value);
|
|
259
|
+
} });
|
|
260
|
+
if (this.keys.length >= this.maxBatchSize) this.dispatch();
|
|
261
|
+
else if (!this.scheduled) {
|
|
262
|
+
this.scheduled = true;
|
|
263
|
+
if (this.batchDelayMs > 0) this.timer = setTimeout(() => this.dispatch(), this.batchDelayMs);
|
|
264
|
+
else Promise.resolve().then(() => this.dispatch());
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
dispatch() {
|
|
269
|
+
if (this.timer) {
|
|
270
|
+
clearTimeout(this.timer);
|
|
271
|
+
this.timer = null;
|
|
272
|
+
}
|
|
273
|
+
this.scheduled = false;
|
|
274
|
+
if (this.keys.length === 0) return;
|
|
275
|
+
const keysToLoad = this.keys;
|
|
276
|
+
const currentPromises = this.promises;
|
|
277
|
+
this.keys = [];
|
|
278
|
+
this.promises = [];
|
|
279
|
+
this.batchLoadFn(keysToLoad).then((results) => {
|
|
280
|
+
for (let i = 0; i < currentPromises.length; i++) currentPromises[i].resolve(results[i]);
|
|
281
|
+
}).catch((err) => {
|
|
282
|
+
for (const p of currentPromises) p.resolve(err);
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
//#endregion
|
|
287
|
+
//#region src/shared/index.ts
|
|
288
|
+
function generateId() {
|
|
289
|
+
return randomUUID();
|
|
290
|
+
}
|
|
291
|
+
function sleep(ms) {
|
|
292
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
293
|
+
}
|
|
294
|
+
var AppError = class extends Error {
|
|
295
|
+
code;
|
|
296
|
+
constructor(message, code, options) {
|
|
297
|
+
super(message, options);
|
|
298
|
+
this.name = "AppError";
|
|
299
|
+
this.code = code;
|
|
300
|
+
}
|
|
301
|
+
};
|
|
302
|
+
var ValidationError = class extends AppError {
|
|
303
|
+
fields;
|
|
304
|
+
constructor(message, fields, options) {
|
|
305
|
+
super(message, "VALIDATION_ERROR", options);
|
|
306
|
+
this.name = "ValidationError";
|
|
307
|
+
this.fields = fields;
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
//#endregion
|
|
21
311
|
//#region src/config/index.ts
|
|
22
|
-
var config_exports = /* @__PURE__ */ __exportAll({
|
|
23
|
-
AI_DEFAULTS: () => AI_DEFAULTS,
|
|
24
|
-
baseConfigSchema: () => baseConfigSchema,
|
|
25
|
-
getAiConfig: () => getAiConfig,
|
|
26
|
-
loadEnv: () => loadEnv,
|
|
27
|
-
parseConfig: () => parseConfig,
|
|
28
|
-
readBaseConfig: () => readBaseConfig,
|
|
29
|
-
setAiConfig: () => setAiConfig,
|
|
30
|
-
setGlobalConfig: () => setGlobalConfig
|
|
31
|
-
});
|
|
32
312
|
function loadEnv(path) {
|
|
33
313
|
const envPath = path ?? resolve(process.cwd(), ".env");
|
|
34
314
|
config({
|
|
@@ -73,7 +353,24 @@ const baseConfigSchema = z.object({
|
|
|
73
353
|
ADMIN_PASSWORD: z.string().trim().min(6).optional(),
|
|
74
354
|
ADMIN_USERNAME: z.string().trim().optional(),
|
|
75
355
|
WORKER_CONCURRENCY: z.coerce.number().int().min(1).default(10),
|
|
76
|
-
|
|
356
|
+
/** In-flight sends per delivery worker. Defaults to WORKER_CONCURRENCY. */
|
|
357
|
+
DELIVERY_CONCURRENCY: z.coerce.number().int().min(1).optional(),
|
|
358
|
+
/**
|
|
359
|
+
* When enricher, engine and delivery run in the same process, hand
|
|
360
|
+
* single-recipient notifications from stage to stage in memory instead of
|
|
361
|
+
* through the enriched/outbound streams. See `NotifkitOptions.fusedPipeline`.
|
|
362
|
+
*/
|
|
363
|
+
PIPELINE_FUSED: z.string().optional().transform((v) => v === "true" || v === "1"),
|
|
364
|
+
QUEUE_MAX_LEN: z.coerce.number().int().min(1).default(1e5),
|
|
365
|
+
/**
|
|
366
|
+
* How long the pipeline remembers it handled a message, so a redelivery
|
|
367
|
+
* after a crash is not sent twice. It only has to outlast redelivery (about
|
|
368
|
+
* ten minutes) plus however far behind the streams run; a caller's own
|
|
369
|
+
* X-Idempotency-Key is always kept for 24 hours regardless. These markers
|
|
370
|
+
* are most of Redis memory under load, so the cost of a longer window is
|
|
371
|
+
* real: a few hundred bytes per message for as long as it lasts.
|
|
372
|
+
*/
|
|
373
|
+
IDEMPOTENCY_TTL_SECONDS: z.coerce.number().int().min(60).default(3600),
|
|
77
374
|
DB_MAX_CONNECTIONS: z.coerce.number().int().min(1).default(2),
|
|
78
375
|
LOG_FLUSH_INTERVAL_MS: z.coerce.number().int().min(50).default(500),
|
|
79
376
|
LOG_BUFFER_MAX_SIZE: z.coerce.number().int().min(100).default(5e3),
|
|
@@ -95,7 +392,7 @@ const baseConfigSchema = z.object({
|
|
|
95
392
|
* When true, trusts X-Forwarded-For headers from reverse proxies for client IP resolution.
|
|
96
393
|
* Defaults to false to prevent client-spoofed headers from bypassing rate limits.
|
|
97
394
|
*/
|
|
98
|
-
TRUST_PROXY: z.
|
|
395
|
+
TRUST_PROXY: z.string().optional().transform((v) => v === "true" || v === "1"),
|
|
99
396
|
/**
|
|
100
397
|
* Comma-separated list of allowed origins for CORS on session-authenticated admin routes.
|
|
101
398
|
*/
|
|
@@ -464,7 +761,15 @@ const WorkflowStepSchema = z.discriminatedUnion("action", [
|
|
|
464
761
|
z.object({
|
|
465
762
|
action: z.literal("waitForEvent"),
|
|
466
763
|
event: z.string(),
|
|
467
|
-
options: z.object({
|
|
764
|
+
options: z.object({
|
|
765
|
+
timeout: z.string().optional(),
|
|
766
|
+
/**
|
|
767
|
+
* Fields the event must carry to wake this instance, e.g.
|
|
768
|
+
* `{ userId: "u1" }`. Without it any event of that name in the project
|
|
769
|
+
* wakes every instance waiting on it.
|
|
770
|
+
*/
|
|
771
|
+
match: z.record(z.string(), z.unknown()).optional()
|
|
772
|
+
}).optional()
|
|
468
773
|
})
|
|
469
774
|
]);
|
|
470
775
|
const CreateWorkflowSchema = z.object({
|
|
@@ -515,7 +820,9 @@ const NotificationRequestedPayloadSchema = z.object({
|
|
|
515
820
|
* Groups every message this request fans out into, so the send can be
|
|
516
821
|
* reported on afterwards. Carried unchanged to the delivery log.
|
|
517
822
|
*/
|
|
518
|
-
campaignId: z.string().min(1).max(128).optional()
|
|
823
|
+
campaignId: z.string().min(1).max(128).optional(),
|
|
824
|
+
/** Set by the workflow worker on a step's notify, carried to the delivery log. */
|
|
825
|
+
workflowInstanceId: z.string().uuid().optional()
|
|
519
826
|
});
|
|
520
827
|
//#endregion
|
|
521
828
|
//#region src/contracts/events/notification-created.ts
|
|
@@ -564,8 +871,24 @@ const NotificationEnrichedPayloadSchema = z.object({
|
|
|
564
871
|
aiPrompts: z.record(z.string(), z.string()).optional(),
|
|
565
872
|
scheduledAt: z.string().datetime().optional(),
|
|
566
873
|
fallbackChain: z.array(NotificationChannelSchema).optional(),
|
|
874
|
+
/**
|
|
875
|
+
* The contact the enricher resolved this message to, and its address. When
|
|
876
|
+
* present the engine sends to exactly this contact instead of re-reading the
|
|
877
|
+
* user's contacts. Absent on events from delivery's channel fallback and the
|
|
878
|
+
* AI worker, which carry a recipient but no resolved contact.
|
|
879
|
+
*/
|
|
880
|
+
contactId: z.string().min(1).optional(),
|
|
881
|
+
destination: z.string().min(1).optional(),
|
|
567
882
|
/** Campaign this message belongs to, carried from the originating request. */
|
|
568
|
-
campaignId: z.string().min(1).max(128).optional()
|
|
883
|
+
campaignId: z.string().min(1).max(128).optional(),
|
|
884
|
+
/** Workflow instance whose step sent this, carried to the delivery log. */
|
|
885
|
+
workflowInstanceId: z.string().uuid().optional(),
|
|
886
|
+
/**
|
|
887
|
+
* Set on delivery's channel fallback. The notification was counted against
|
|
888
|
+
* the user's throttle when it was first sent; counting it again per channel
|
|
889
|
+
* it falls back through would drop the very send meant to reach them.
|
|
890
|
+
*/
|
|
891
|
+
throttleCounted: z.boolean().optional()
|
|
569
892
|
});
|
|
570
893
|
//#endregion
|
|
571
894
|
//#region src/contracts/events/notification-scheduled.ts
|
|
@@ -601,13 +924,26 @@ const NotificationDispatchedPayloadSchema = z.object({
|
|
|
601
924
|
templateVariables: z.record(z.string(), z.unknown()).default({}),
|
|
602
925
|
aiPrompts: z.record(z.string(), z.string()).optional(),
|
|
603
926
|
recipient: RecipientProfileSchema.optional(),
|
|
604
|
-
|
|
927
|
+
/**
|
|
928
|
+
* Absent on tasks the engine dispatches: delivery renders `templateId` with
|
|
929
|
+
* `templateVariables` just before sending, so the rendered body never rides
|
|
930
|
+
* through the outbound stream or sits in a scheduled payload. The AI worker
|
|
931
|
+
* still sends it pre-rendered, since its variables are generated.
|
|
932
|
+
*/
|
|
933
|
+
renderedContent: RenderedContentSchema.optional(),
|
|
605
934
|
destination: z.string().min(1).optional(),
|
|
606
935
|
deliveryOptions: DeliveryOptionsSchema,
|
|
607
936
|
fallbackChain: z.array(NotificationChannelSchema).optional(),
|
|
608
937
|
throttleAttemptCount: z.number().int().nonnegative().optional(),
|
|
938
|
+
/**
|
|
939
|
+
* Provider attempts that already failed. Delivery re-queues a failed send
|
|
940
|
+
* through the scheduler until this reaches `deliveryOptions.maxAttempts`.
|
|
941
|
+
*/
|
|
942
|
+
deliveryAttemptCount: z.number().int().nonnegative().optional(),
|
|
609
943
|
/** Campaign this message belongs to, carried from the originating request. */
|
|
610
|
-
campaignId: z.string().min(1).max(128).optional()
|
|
944
|
+
campaignId: z.string().min(1).max(128).optional(),
|
|
945
|
+
/** Workflow instance whose step sent this, recorded on the delivery log. */
|
|
946
|
+
workflowInstanceId: z.string().uuid().optional()
|
|
611
947
|
});
|
|
612
948
|
//#endregion
|
|
613
949
|
//#region src/contracts/events/notification-delivered.ts
|
|
@@ -622,7 +958,14 @@ const NotificationDeliveredPayloadSchema = z.object({
|
|
|
622
958
|
templateId: z.string().uuid().optional(),
|
|
623
959
|
workflowInstanceId: z.string().uuid().optional(),
|
|
624
960
|
/** Campaign this message belongs to, carried from the originating request. */
|
|
625
|
-
campaignId: z.string().min(1).max(128).optional()
|
|
961
|
+
campaignId: z.string().min(1).max(128).optional(),
|
|
962
|
+
attempt: z.number().int().positive().optional(),
|
|
963
|
+
/**
|
|
964
|
+
* Set when this event also stands in for the attempt's
|
|
965
|
+
* `notification.dispatched` log entry (see DeliveryWorker): the send finished
|
|
966
|
+
* quickly enough that both were published as one stream entry.
|
|
967
|
+
*/
|
|
968
|
+
dispatchedAt: z.string().datetime().optional()
|
|
626
969
|
});
|
|
627
970
|
//#endregion
|
|
628
971
|
//#region src/contracts/events/notification-failed.ts
|
|
@@ -639,7 +982,9 @@ const NotificationFailedPayloadSchema = z.object({
|
|
|
639
982
|
templateId: z.string().uuid().optional(),
|
|
640
983
|
workflowInstanceId: z.string().uuid().optional(),
|
|
641
984
|
/** Campaign this message belongs to, carried from the originating request. */
|
|
642
|
-
campaignId: z.string().min(1).max(128).optional()
|
|
985
|
+
campaignId: z.string().min(1).max(128).optional(),
|
|
986
|
+
/** See NotificationDeliveredPayload.dispatchedAt. */
|
|
987
|
+
dispatchedAt: z.string().datetime().optional()
|
|
643
988
|
});
|
|
644
989
|
//#endregion
|
|
645
990
|
//#region src/contracts/events/notification-skipped.ts
|
|
@@ -668,7 +1013,19 @@ const NotificationAiPendingPayloadSchema = z.object({
|
|
|
668
1013
|
recipient: RecipientProfileSchema,
|
|
669
1014
|
aiPrompts: z.record(z.string(), z.string()),
|
|
670
1015
|
scheduledAt: z.string().datetime().optional(),
|
|
671
|
-
fallbackChain: z.array(NotificationChannelSchema).optional()
|
|
1016
|
+
fallbackChain: z.array(NotificationChannelSchema).optional(),
|
|
1017
|
+
/**
|
|
1018
|
+
* The task id the engine assigned. Stable across replays, so a re-run of
|
|
1019
|
+
* this event dispatches the same task — which delivery's lease then sends
|
|
1020
|
+
* once — rather than a fresh one.
|
|
1021
|
+
*/
|
|
1022
|
+
taskId: z.string().min(1).optional(),
|
|
1023
|
+
/** The contact address the engine resolved and cleared against suppressions. */
|
|
1024
|
+
destination: z.string().min(1).optional(),
|
|
1025
|
+
/** Headers for the provider, e.g. one-click unsubscribe. */
|
|
1026
|
+
deliveryHeaders: z.record(z.string(), z.string()).optional(),
|
|
1027
|
+
campaignId: z.string().min(1).max(128).optional(),
|
|
1028
|
+
workflowInstanceId: z.string().uuid().optional()
|
|
672
1029
|
});
|
|
673
1030
|
//#endregion
|
|
674
1031
|
//#region src/contracts/index.ts
|
|
@@ -690,41 +1047,11 @@ var schema_exports = /* @__PURE__ */ __exportAll({
|
|
|
690
1047
|
channelEnum: () => channelEnum,
|
|
691
1048
|
contactTopicPreferences: () => contactTopicPreferences,
|
|
692
1049
|
deliveryOutbox: () => deliveryOutbox,
|
|
693
|
-
insertAdminUserSchema: () => insertAdminUserSchema,
|
|
694
|
-
insertDeliveryOutboxSchema: () => insertDeliveryOutboxSchema,
|
|
695
|
-
insertMessageLogSchema: () => insertMessageLogSchema,
|
|
696
|
-
insertProjectApiKeySchema: () => insertProjectApiKeySchema,
|
|
697
|
-
insertProjectSchema: () => insertProjectSchema,
|
|
698
|
-
insertScheduledPayloadSchema: () => insertScheduledPayloadSchema,
|
|
699
|
-
insertSuppressionSchema: () => insertSuppressionSchema,
|
|
700
|
-
insertUserChannelPreferenceSchema: () => insertUserChannelPreferenceSchema,
|
|
701
|
-
insertUserContactSchema: () => insertUserContactSchema,
|
|
702
|
-
insertUserSchema: () => insertUserSchema,
|
|
703
|
-
insertUserSegmentSchema: () => insertUserSegmentSchema,
|
|
704
|
-
insertUserTopicPreferenceSchema: () => insertUserTopicPreferenceSchema,
|
|
705
|
-
insertWorkflowInstanceSchema: () => insertWorkflowInstanceSchema,
|
|
706
|
-
insertWorkflowStepSchema: () => insertWorkflowStepSchema,
|
|
707
|
-
insertWorkflowWaiterSchema: () => insertWorkflowWaiterSchema,
|
|
708
1050
|
messageLogs: () => messageLogs,
|
|
709
1051
|
projectApiKeys: () => projectApiKeys,
|
|
710
1052
|
projects: () => projects,
|
|
711
1053
|
quietHours: () => quietHours,
|
|
712
1054
|
scheduledPayloads: () => scheduledPayloads,
|
|
713
|
-
selectAdminUserSchema: () => selectAdminUserSchema,
|
|
714
|
-
selectDeliveryOutboxSchema: () => selectDeliveryOutboxSchema,
|
|
715
|
-
selectMessageLogSchema: () => selectMessageLogSchema,
|
|
716
|
-
selectProjectApiKeySchema: () => selectProjectApiKeySchema,
|
|
717
|
-
selectProjectSchema: () => selectProjectSchema,
|
|
718
|
-
selectScheduledPayloadSchema: () => selectScheduledPayloadSchema,
|
|
719
|
-
selectSuppressionSchema: () => selectSuppressionSchema,
|
|
720
|
-
selectUserChannelPreferenceSchema: () => selectUserChannelPreferenceSchema,
|
|
721
|
-
selectUserContactSchema: () => selectUserContactSchema,
|
|
722
|
-
selectUserSchema: () => selectUserSchema,
|
|
723
|
-
selectUserSegmentSchema: () => selectUserSegmentSchema,
|
|
724
|
-
selectUserTopicPreferenceSchema: () => selectUserTopicPreferenceSchema,
|
|
725
|
-
selectWorkflowInstanceSchema: () => selectWorkflowInstanceSchema,
|
|
726
|
-
selectWorkflowStepSchema: () => selectWorkflowStepSchema,
|
|
727
|
-
selectWorkflowWaiterSchema: () => selectWorkflowWaiterSchema,
|
|
728
1055
|
suppressions: () => suppressions,
|
|
729
1056
|
templates: () => templates,
|
|
730
1057
|
userChannelPreferences: () => userChannelPreferences,
|
|
@@ -857,8 +1184,6 @@ const messageLogs = pgTable("message_logs", {
|
|
|
857
1184
|
metadata: jsonb("metadata"),
|
|
858
1185
|
timestamp: timestamp("timestamp", { withTimezone: true }).defaultNow().notNull()
|
|
859
1186
|
}, (table) => ({
|
|
860
|
-
projectIdx: index("message_logs_project_idx").on(table.projectId),
|
|
861
|
-
taskIdx: index("task_idx").on(table.taskId),
|
|
862
1187
|
projectIdTaskIdIdx: index("message_logs_project_task_idx").on(table.projectId, table.taskId),
|
|
863
1188
|
providerMsgIdx: index("provider_msg_idx").on(table.providerMessageId),
|
|
864
1189
|
projectTimeIdx: index("msg_log_proj_time_idx").on(table.projectId, table.timestamp),
|
|
@@ -898,7 +1223,8 @@ const workflowStatusEnum = pgEnum("workflow_status", [
|
|
|
898
1223
|
"pending",
|
|
899
1224
|
"running",
|
|
900
1225
|
"completed",
|
|
901
|
-
"failed"
|
|
1226
|
+
"failed",
|
|
1227
|
+
"canceled"
|
|
902
1228
|
]);
|
|
903
1229
|
const workflowDefinitions = pgTable("workflow_definitions", {
|
|
904
1230
|
id: uuid("id").primaryKey().defaultRandom(),
|
|
@@ -965,42 +1291,8 @@ const adminUsers = pgTable("admin_users", {
|
|
|
965
1291
|
createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
|
|
966
1292
|
updatedAt: timestamp("updated_at", { withTimezone: true }).defaultNow().notNull()
|
|
967
1293
|
});
|
|
968
|
-
const insertAdminUserSchema = createInsertSchema(adminUsers);
|
|
969
|
-
const selectAdminUserSchema = createSelectSchema(adminUsers);
|
|
970
|
-
const insertProjectSchema = createInsertSchema(projects);
|
|
971
|
-
const selectProjectSchema = createSelectSchema(projects);
|
|
972
|
-
const insertProjectApiKeySchema = createInsertSchema(projectApiKeys);
|
|
973
|
-
const selectProjectApiKeySchema = createSelectSchema(projectApiKeys);
|
|
974
|
-
const insertUserSchema = createInsertSchema(users);
|
|
975
|
-
const selectUserSchema = createSelectSchema(users);
|
|
976
|
-
const insertUserSegmentSchema = createInsertSchema(userSegments);
|
|
977
|
-
const selectUserSegmentSchema = createSelectSchema(userSegments);
|
|
978
|
-
const insertUserContactSchema = createInsertSchema(userContacts);
|
|
979
|
-
const selectUserContactSchema = createSelectSchema(userContacts);
|
|
980
|
-
const insertUserChannelPreferenceSchema = createInsertSchema(userChannelPreferences);
|
|
981
|
-
const selectUserChannelPreferenceSchema = createSelectSchema(userChannelPreferences);
|
|
982
|
-
const insertUserTopicPreferenceSchema = createInsertSchema(userTopicPreferences);
|
|
983
|
-
const selectUserTopicPreferenceSchema = createSelectSchema(userTopicPreferences);
|
|
984
|
-
const insertMessageLogSchema = createInsertSchema(messageLogs);
|
|
985
|
-
const selectMessageLogSchema = createSelectSchema(messageLogs);
|
|
986
|
-
const insertSuppressionSchema = createInsertSchema(suppressions);
|
|
987
|
-
const selectSuppressionSchema = createSelectSchema(suppressions);
|
|
988
|
-
const insertWorkflowInstanceSchema = createInsertSchema(workflowInstances);
|
|
989
|
-
const selectWorkflowInstanceSchema = createSelectSchema(workflowInstances);
|
|
990
|
-
const insertWorkflowStepSchema = createInsertSchema(workflowSteps);
|
|
991
|
-
const selectWorkflowStepSchema = createSelectSchema(workflowSteps);
|
|
992
|
-
const insertWorkflowWaiterSchema = createInsertSchema(workflowWaiters);
|
|
993
|
-
const selectWorkflowWaiterSchema = createSelectSchema(workflowWaiters);
|
|
994
|
-
const insertDeliveryOutboxSchema = createInsertSchema(deliveryOutbox);
|
|
995
|
-
const selectDeliveryOutboxSchema = createSelectSchema(deliveryOutbox);
|
|
996
|
-
const insertScheduledPayloadSchema = createInsertSchema(scheduledPayloads);
|
|
997
|
-
const selectScheduledPayloadSchema = createSelectSchema(scheduledPayloads);
|
|
998
1294
|
//#endregion
|
|
999
1295
|
//#region src/db/index.ts
|
|
1000
|
-
var db_exports = /* @__PURE__ */ __exportAll({
|
|
1001
|
-
createDatabase: () => createDatabase,
|
|
1002
|
-
runMigrations: () => runMigrations
|
|
1003
|
-
});
|
|
1004
1296
|
/**
|
|
1005
1297
|
* Create a postgres.js connection pool and initialize Drizzle ORM.
|
|
1006
1298
|
* Call once at application startup; pass db into repositories.
|
|
@@ -1034,9 +1326,10 @@ async function runMigrations(db) {
|
|
|
1034
1326
|
//#endregion
|
|
1035
1327
|
//#region src/idempotency/index.ts
|
|
1036
1328
|
/**
|
|
1037
|
-
* SETNX-based idempotency guard.
|
|
1038
|
-
*
|
|
1039
|
-
*
|
|
1329
|
+
* Two-phase SETNX-based idempotency guard.
|
|
1330
|
+
* Supports:
|
|
1331
|
+
* - Phase 1 (:lock): Short lease acquired at the start of message processing.
|
|
1332
|
+
* - Phase 2 (:sent): Long-lived completion marker recorded when processing succeeds.
|
|
1040
1333
|
*/
|
|
1041
1334
|
var IdempotencyGuard = class {
|
|
1042
1335
|
redis;
|
|
@@ -1050,22 +1343,53 @@ var IdempotencyGuard = class {
|
|
|
1050
1343
|
key(id) {
|
|
1051
1344
|
return `${this.keyPrefix}:${id}`;
|
|
1052
1345
|
}
|
|
1346
|
+
lockKey(id) {
|
|
1347
|
+
return `${this.keyPrefix}:${id}:lock`;
|
|
1348
|
+
}
|
|
1349
|
+
completedKey(id) {
|
|
1350
|
+
return `${this.keyPrefix}:${id}:sent`;
|
|
1351
|
+
}
|
|
1352
|
+
/**
|
|
1353
|
+
* Two-phase lease acquisition.
|
|
1354
|
+
* Atomically checks existing state and acquires lock if available.
|
|
1355
|
+
* - Happy path (new message): single native SET NX command storing "L" (lock lease).
|
|
1356
|
+
* - If already present: checks if "S" (sent/completed) or "L" (locked by another worker).
|
|
1357
|
+
*/
|
|
1358
|
+
async acquireLease(id, lockTtlSeconds = 30) {
|
|
1359
|
+
const mainKey = this.key(id);
|
|
1360
|
+
const lockKey = this.lockKey(id);
|
|
1361
|
+
if (await this.redis.set(mainKey, "L", "EX", lockTtlSeconds, "NX") === "OK") return "acquired";
|
|
1362
|
+
const [val, sent, lockVal] = await Promise.all([
|
|
1363
|
+
this.redis.get(mainKey),
|
|
1364
|
+
this.redis.get(this.completedKey(id)),
|
|
1365
|
+
this.redis.get(lockKey)
|
|
1366
|
+
]);
|
|
1367
|
+
if (val === "S" || val === "1" || sent !== null) return "completed";
|
|
1368
|
+
if (val === "L" || lockVal !== null) return "locked";
|
|
1369
|
+
if (await this.redis.set(mainKey, "L", "EX", lockTtlSeconds, "NX") === "OK") return "acquired";
|
|
1370
|
+
return "locked";
|
|
1371
|
+
}
|
|
1053
1372
|
/** Atomically mark id as processed. Returns true on first call; false if already seen. */
|
|
1054
1373
|
async checkAndMark(id, customTtlSeconds) {
|
|
1055
1374
|
const ttl = customTtlSeconds ?? this.ttlSeconds;
|
|
1056
1375
|
return await this.redis.set(this.key(id), "1", "EX", ttl, "NX") === "OK";
|
|
1057
1376
|
}
|
|
1058
|
-
/** Unconditionally mark id as processed (
|
|
1377
|
+
/** Unconditionally mark id as processed (upgrades lock to completed "1" with full TTL). */
|
|
1059
1378
|
async markProcessed(id, customTtlSeconds) {
|
|
1060
1379
|
const ttl = customTtlSeconds ?? this.ttlSeconds;
|
|
1061
1380
|
await this.redis.set(this.key(id), "1", "EX", ttl);
|
|
1062
1381
|
}
|
|
1063
1382
|
async isProcessed(id) {
|
|
1064
|
-
|
|
1383
|
+
const val = await this.redis.get(this.key(id));
|
|
1384
|
+
return val !== null && val !== void 0;
|
|
1065
1385
|
}
|
|
1066
1386
|
/** Remove the idempotency marker (useful in tests or manual rollbacks). */
|
|
1067
1387
|
async unmark(id) {
|
|
1068
|
-
await
|
|
1388
|
+
await Promise.all([
|
|
1389
|
+
this.redis.del(this.key(id)),
|
|
1390
|
+
this.redis.del(this.lockKey(id)),
|
|
1391
|
+
this.redis.del(this.completedKey(id))
|
|
1392
|
+
]);
|
|
1069
1393
|
}
|
|
1070
1394
|
};
|
|
1071
1395
|
//#endregion
|
|
@@ -1101,15 +1425,6 @@ function createLogger({ name, level = "info", pretty, context }) {
|
|
|
1101
1425
|
};
|
|
1102
1426
|
return pino(options);
|
|
1103
1427
|
}
|
|
1104
|
-
function withRequestId(logger, requestId) {
|
|
1105
|
-
return logger.child({ requestId });
|
|
1106
|
-
}
|
|
1107
|
-
function withContext(logger, context) {
|
|
1108
|
-
return logger.child(context);
|
|
1109
|
-
}
|
|
1110
|
-
function childLogger(logger, bindings) {
|
|
1111
|
-
return logger.child(bindings);
|
|
1112
|
-
}
|
|
1113
1428
|
//#endregion
|
|
1114
1429
|
//#region src/metrics/index.ts
|
|
1115
1430
|
const register = new promClient.Registry();
|
|
@@ -1197,6 +1512,9 @@ var StreamProducer = class {
|
|
|
1197
1512
|
stream;
|
|
1198
1513
|
logger;
|
|
1199
1514
|
maxLen;
|
|
1515
|
+
pendingQueue = [];
|
|
1516
|
+
flushScheduled = false;
|
|
1517
|
+
lastMaxLenCheck = 0;
|
|
1200
1518
|
constructor({ redis, stream, logger, maxLen }) {
|
|
1201
1519
|
this.redis = redis;
|
|
1202
1520
|
this.stream = stream;
|
|
@@ -1204,23 +1522,68 @@ var StreamProducer = class {
|
|
|
1204
1522
|
this.maxLen = maxLen ?? readBaseConfig().QUEUE_MAX_LEN;
|
|
1205
1523
|
}
|
|
1206
1524
|
async publish(partial) {
|
|
1207
|
-
|
|
1525
|
+
return this.enqueue({
|
|
1208
1526
|
...partial,
|
|
1209
1527
|
id: crypto.randomUUID(),
|
|
1210
1528
|
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
1211
|
-
};
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1529
|
+
});
|
|
1530
|
+
}
|
|
1531
|
+
enqueue(event) {
|
|
1532
|
+
return new Promise((resolve, reject) => {
|
|
1533
|
+
this.pendingQueue.push({
|
|
1534
|
+
event,
|
|
1535
|
+
resolve,
|
|
1536
|
+
reject
|
|
1537
|
+
});
|
|
1538
|
+
if (this.pendingQueue.length >= 100) this.flush();
|
|
1539
|
+
else if (!this.flushScheduled) {
|
|
1540
|
+
this.flushScheduled = true;
|
|
1541
|
+
setImmediate(() => {
|
|
1542
|
+
this.flush();
|
|
1543
|
+
});
|
|
1544
|
+
}
|
|
1545
|
+
});
|
|
1546
|
+
}
|
|
1547
|
+
async flush() {
|
|
1548
|
+
this.flushScheduled = false;
|
|
1549
|
+
const batch = this.pendingQueue;
|
|
1550
|
+
this.pendingQueue = [];
|
|
1551
|
+
if (batch.length === 0) return;
|
|
1552
|
+
try {
|
|
1553
|
+
if (typeof this.redis.pipeline === "function") {
|
|
1554
|
+
const pipeline = this.redis.pipeline();
|
|
1555
|
+
for (let i = 0; i < batch.length; i++) {
|
|
1556
|
+
const item = batch[i];
|
|
1557
|
+
if (i === batch.length - 1) pipeline.xadd(this.stream, "MAXLEN", "~", String(this.maxLen), "*", "data", JSON.stringify(item.event));
|
|
1558
|
+
else pipeline.xadd(this.stream, "*", "data", JSON.stringify(item.event));
|
|
1559
|
+
}
|
|
1560
|
+
const results = await pipeline.exec();
|
|
1561
|
+
if (!results) throw new Error(`Pipeline execution failed for ${this.stream}`);
|
|
1562
|
+
for (let i = 0; i < results.length; i++) {
|
|
1563
|
+
const [err, msgId] = results[i] ?? [];
|
|
1564
|
+
if (err) batch[i].reject(err);
|
|
1565
|
+
else batch[i].resolve(msgId);
|
|
1566
|
+
}
|
|
1567
|
+
} else for (let i = 0; i < batch.length; i++) {
|
|
1568
|
+
const item = batch[i];
|
|
1569
|
+
try {
|
|
1570
|
+
const msgId = await this.redis.xadd(this.stream, "MAXLEN", "~", String(this.maxLen), "*", "data", JSON.stringify(item.event));
|
|
1571
|
+
if (!msgId) throw new Error(`XADD to ${this.stream} returned null`);
|
|
1572
|
+
item.resolve(msgId);
|
|
1573
|
+
} catch (err) {
|
|
1574
|
+
item.reject(err);
|
|
1575
|
+
}
|
|
1576
|
+
}
|
|
1577
|
+
this.monitorMaxLen(this.stream).catch(() => {});
|
|
1578
|
+
} catch (err) {
|
|
1579
|
+
for (const item of batch) item.reject(err);
|
|
1580
|
+
}
|
|
1221
1581
|
}
|
|
1222
1582
|
async monitorMaxLen(stream) {
|
|
1223
|
-
|
|
1583
|
+
const now = Date.now();
|
|
1584
|
+
if (now - this.lastMaxLenCheck < 5e3) return;
|
|
1585
|
+
this.lastMaxLenCheck = now;
|
|
1586
|
+
try {
|
|
1224
1587
|
const len = await this.redis.xlen(stream);
|
|
1225
1588
|
metrics.queueSize.set({ stream }, len);
|
|
1226
1589
|
if (len > this.maxLen * .8) this.logger?.warn({
|
|
@@ -1239,36 +1602,19 @@ var StreamProducer = class {
|
|
|
1239
1602
|
messageIds: [],
|
|
1240
1603
|
eventIds: []
|
|
1241
1604
|
};
|
|
1242
|
-
const pipeline = this.redis.pipeline();
|
|
1243
|
-
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
1244
1605
|
const eventIds = [];
|
|
1606
|
+
const promises = [];
|
|
1245
1607
|
for (const partial of partials) {
|
|
1246
1608
|
const id = crypto.randomUUID();
|
|
1247
1609
|
eventIds.push(id);
|
|
1248
|
-
|
|
1610
|
+
promises.push(this.enqueue({
|
|
1249
1611
|
...partial,
|
|
1250
1612
|
id,
|
|
1251
|
-
timestamp
|
|
1252
|
-
};
|
|
1253
|
-
|
|
1254
|
-
}
|
|
1255
|
-
const results = await pipeline.exec();
|
|
1256
|
-
if (!results) throw new Error(`Pipeline execution failed for ${this.stream}`);
|
|
1257
|
-
const messageIds = [];
|
|
1258
|
-
for (let i = 0; i < results.length; i++) {
|
|
1259
|
-
const result = results[i];
|
|
1260
|
-
if (!result) throw new Error("Pipeline result is undefined");
|
|
1261
|
-
const [err, msgId] = result;
|
|
1262
|
-
if (err) throw err;
|
|
1263
|
-
messageIds.push(msgId);
|
|
1264
|
-
}
|
|
1265
|
-
this.logger?.debug({
|
|
1266
|
-
stream: this.stream,
|
|
1267
|
-
count: partials.length
|
|
1268
|
-
}, "batch events published");
|
|
1269
|
-
this.monitorMaxLen(this.stream).catch(() => {});
|
|
1613
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
1614
|
+
}));
|
|
1615
|
+
}
|
|
1270
1616
|
return {
|
|
1271
|
-
messageIds,
|
|
1617
|
+
messageIds: await Promise.all(promises),
|
|
1272
1618
|
eventIds
|
|
1273
1619
|
};
|
|
1274
1620
|
}
|
|
@@ -1283,10 +1629,15 @@ var StreamConsumer = class {
|
|
|
1283
1629
|
logger;
|
|
1284
1630
|
batchSize;
|
|
1285
1631
|
blockMs;
|
|
1632
|
+
bufferAcks;
|
|
1633
|
+
ackFlushMs;
|
|
1634
|
+
coalesceMs;
|
|
1635
|
+
ackBuffers = /* @__PURE__ */ new Map();
|
|
1636
|
+
ackTimer = null;
|
|
1286
1637
|
running = false;
|
|
1287
|
-
constructor({ redis, stream, group, consumer, dlqStream, logger, batchSize = 10, blockMs = 5e3 }) {
|
|
1638
|
+
constructor({ redis, stream, group, consumer, dlqStream, logger, batchSize = 10, blockMs = 5e3, bufferAcks = false, ackFlushMs = 5, coalesceMs = 0 }) {
|
|
1288
1639
|
this.redis = redis;
|
|
1289
|
-
this.blockingRedis = redis.duplicate();
|
|
1640
|
+
this.blockingRedis = redis.duplicate({ enableAutoPipelining: false });
|
|
1290
1641
|
this.streams = Array.isArray(stream) ? stream : [stream];
|
|
1291
1642
|
this.group = group;
|
|
1292
1643
|
this.consumer = consumer;
|
|
@@ -1294,6 +1645,9 @@ var StreamConsumer = class {
|
|
|
1294
1645
|
this.logger = logger;
|
|
1295
1646
|
this.batchSize = batchSize;
|
|
1296
1647
|
this.blockMs = blockMs;
|
|
1648
|
+
this.bufferAcks = bufferAcks;
|
|
1649
|
+
this.ackFlushMs = ackFlushMs;
|
|
1650
|
+
this.coalesceMs = coalesceMs;
|
|
1297
1651
|
}
|
|
1298
1652
|
async ensureGroup() {
|
|
1299
1653
|
for (const s of this.streams) try {
|
|
@@ -1344,15 +1698,27 @@ var StreamConsumer = class {
|
|
|
1344
1698
|
for (const [streamName, messages] of results) for (const [id, fields] of messages) {
|
|
1345
1699
|
const msg = parseMessage(id, fields, this.logger);
|
|
1346
1700
|
if (!msg) {
|
|
1347
|
-
await this.
|
|
1701
|
+
await this.ackAndDelete(streamName, [id]);
|
|
1348
1702
|
continue;
|
|
1349
1703
|
}
|
|
1350
1704
|
msg.stream = streamName;
|
|
1705
|
+
msg.deliveryCount = 1;
|
|
1351
1706
|
batch.push(msg);
|
|
1352
1707
|
}
|
|
1353
1708
|
if (batch.length > 0) yield batch;
|
|
1709
|
+
if (this.coalesceMs > 0 && batch.length < this.batchSize && this.running) await new Promise((resolve) => setTimeout(resolve, this.coalesceMs));
|
|
1354
1710
|
} catch (err) {
|
|
1355
1711
|
if (!this.running && err instanceof Error && err?.message?.toLowerCase?.().includes("connection is closed")) break;
|
|
1712
|
+
if (err instanceof Error && err.message.startsWith("NOGROUP")) try {
|
|
1713
|
+
await this.ensureGroup();
|
|
1714
|
+
this.logger?.warn({
|
|
1715
|
+
group: this.group,
|
|
1716
|
+
streams: this.streams
|
|
1717
|
+
}, "consumer group was missing — recreated it");
|
|
1718
|
+
continue;
|
|
1719
|
+
} catch (recreateErr) {
|
|
1720
|
+
this.logger?.error({ err: recreateErr }, "failed to recreate consumer group");
|
|
1721
|
+
}
|
|
1356
1722
|
this.logger?.error({ err }, "error reading from stream");
|
|
1357
1723
|
await new Promise((resolve) => setTimeout(resolve, retryDelay));
|
|
1358
1724
|
retryDelay = Math.min(retryDelay * 2, 3e4);
|
|
@@ -1362,24 +1728,96 @@ var StreamConsumer = class {
|
|
|
1362
1728
|
const s = stream ?? this.streams[0];
|
|
1363
1729
|
const ids = Array.isArray(messageId) ? messageId : [messageId];
|
|
1364
1730
|
if (ids.length === 0) return;
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1731
|
+
if (!this.bufferAcks) {
|
|
1732
|
+
await this.ackAndDelete(s, ids);
|
|
1733
|
+
this.logger?.debug({
|
|
1734
|
+
stream: s,
|
|
1735
|
+
count: ids.length
|
|
1736
|
+
}, "messages acknowledged");
|
|
1737
|
+
return;
|
|
1738
|
+
}
|
|
1739
|
+
let buf = this.ackBuffers.get(s);
|
|
1740
|
+
if (!buf) {
|
|
1741
|
+
buf = [];
|
|
1742
|
+
this.ackBuffers.set(s, buf);
|
|
1743
|
+
}
|
|
1744
|
+
buf.push(...ids);
|
|
1745
|
+
if (buf.length >= this.batchSize) await this.flushAcksForStream(s);
|
|
1746
|
+
else if (!this.ackTimer) this.ackTimer = setTimeout(() => {
|
|
1747
|
+
this.ackTimer = null;
|
|
1748
|
+
this.flushAcks();
|
|
1749
|
+
}, this.ackFlushMs);
|
|
1370
1750
|
}
|
|
1371
|
-
|
|
1751
|
+
/**
|
|
1752
|
+
* Acks and then deletes the entries. Every stream is read by exactly one
|
|
1753
|
+
* consumer group, so an acked entry is never read again; leaving it in place
|
|
1754
|
+
* holds Redis memory until MAXLEN trims it, and a group recreated from "0"
|
|
1755
|
+
* (ensureGroup) would process it a second time. Both commands are issued
|
|
1756
|
+
* before either is awaited, so auto-pipelining sends them in one round trip,
|
|
1757
|
+
* and Redis runs them in order.
|
|
1758
|
+
*/
|
|
1759
|
+
async ackAndDelete(stream, ids) {
|
|
1760
|
+
await Promise.all([this.redis.xack(stream, this.group, ...ids), this.redis.xdel(stream, ...ids)]);
|
|
1761
|
+
}
|
|
1762
|
+
async flushAcksForStream(stream) {
|
|
1763
|
+
const ids = this.ackBuffers.get(stream);
|
|
1764
|
+
if (!ids || ids.length === 0) return;
|
|
1765
|
+
this.ackBuffers.set(stream, []);
|
|
1766
|
+
try {
|
|
1767
|
+
await this.ackAndDelete(stream, ids);
|
|
1768
|
+
this.logger?.debug({
|
|
1769
|
+
stream,
|
|
1770
|
+
count: ids.length
|
|
1771
|
+
}, "buffered messages acknowledged");
|
|
1772
|
+
} catch (err) {
|
|
1773
|
+
this.logger?.error({
|
|
1774
|
+
err,
|
|
1775
|
+
stream,
|
|
1776
|
+
count: ids.length
|
|
1777
|
+
}, "failed to flush buffered acks");
|
|
1778
|
+
}
|
|
1779
|
+
}
|
|
1780
|
+
async flushAcks() {
|
|
1781
|
+
if (this.ackTimer) {
|
|
1782
|
+
clearTimeout(this.ackTimer);
|
|
1783
|
+
this.ackTimer = null;
|
|
1784
|
+
}
|
|
1785
|
+
const streams = Array.from(this.ackBuffers.keys());
|
|
1786
|
+
await Promise.all(streams.map((s) => this.flushAcksForStream(s)));
|
|
1787
|
+
}
|
|
1788
|
+
/**
|
|
1789
|
+
* Writes an event that never had a stream entry of its own (one handed
|
|
1790
|
+
* straight to a worker in-process) to the dead-letter stream, recorded
|
|
1791
|
+
* against this consumer's stream so a replay lands where it would have been
|
|
1792
|
+
* read from.
|
|
1793
|
+
*/
|
|
1794
|
+
async deadLetter(event, reason) {
|
|
1795
|
+
if (!this.dlqStream) {
|
|
1796
|
+
this.logger?.error({ eventId: event.id }, "no dead-letter stream configured — dropping event");
|
|
1797
|
+
return;
|
|
1798
|
+
}
|
|
1799
|
+
await this.redis.xadd(this.dlqStream, "*", "data", JSON.stringify({
|
|
1800
|
+
...event,
|
|
1801
|
+
dlq: {
|
|
1802
|
+
originalStream: this.streams[0],
|
|
1803
|
+
ackedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1804
|
+
reason
|
|
1805
|
+
}
|
|
1806
|
+
}));
|
|
1807
|
+
}
|
|
1808
|
+
async nack(messageId, event, stream, reason) {
|
|
1372
1809
|
const s = stream ?? this.streams[0];
|
|
1373
1810
|
if (this.dlqStream) {
|
|
1374
1811
|
const dlqId = await this.redis.xadd(this.dlqStream, "*", "data", JSON.stringify({
|
|
1375
1812
|
...event,
|
|
1376
1813
|
dlq: {
|
|
1377
1814
|
originalStream: s,
|
|
1378
|
-
ackedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1815
|
+
ackedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1816
|
+
reason
|
|
1379
1817
|
}
|
|
1380
1818
|
}));
|
|
1381
1819
|
if (!dlqId) throw new Error(`XADD to dead-letter stream ${this.dlqStream} returned null`);
|
|
1382
|
-
await this.
|
|
1820
|
+
await this.ackAndDelete(s, [messageId]);
|
|
1383
1821
|
this.logger?.warn({
|
|
1384
1822
|
stream: s,
|
|
1385
1823
|
dlqStream: this.dlqStream,
|
|
@@ -1391,6 +1829,7 @@ var StreamConsumer = class {
|
|
|
1391
1829
|
}
|
|
1392
1830
|
async stop() {
|
|
1393
1831
|
this.running = false;
|
|
1832
|
+
await this.flushAcks().catch(() => {});
|
|
1394
1833
|
try {
|
|
1395
1834
|
await this.blockingRedis.quit();
|
|
1396
1835
|
} catch (err) {
|
|
@@ -1432,328 +1871,40 @@ var PendingMessageScanner = class {
|
|
|
1432
1871
|
for (const item of result) if (Array.isArray(item)) allEntries.push({
|
|
1433
1872
|
id: item[0],
|
|
1434
1873
|
consumer: item[1],
|
|
1435
|
-
idleMs: item[2],
|
|
1436
|
-
deliveryCount: item[3],
|
|
1437
|
-
stream: s
|
|
1438
|
-
});
|
|
1439
|
-
}
|
|
1440
|
-
if (allEntries.length >= limit) break;
|
|
1441
|
-
}
|
|
1442
|
-
return allEntries.slice(0, limit);
|
|
1443
|
-
}
|
|
1444
|
-
async autoclaim(minIdleMs, limit = 10) {
|
|
1445
|
-
const recovered = [];
|
|
1446
|
-
for (const s of this.streams) {
|
|
1447
|
-
if (recovered.length >= limit) break;
|
|
1448
|
-
const toClaim = (await this.getPendingEntries(limit * 2, s)).filter((p) => p.idleMs > minIdleMs * Math.pow(2, p.deliveryCount - 1)).slice(0, limit - recovered.length);
|
|
1449
|
-
if (toClaim.length === 0) continue;
|
|
1450
|
-
const ids = toClaim.map((p) => p.id);
|
|
1451
|
-
const result = await this.redis.xclaim(s, this.group, this.consumer, minIdleMs, ...ids);
|
|
1452
|
-
for (const raw of result) {
|
|
1453
|
-
if (!raw) continue;
|
|
1454
|
-
const [id, fields] = raw;
|
|
1455
|
-
const msg = parseMessage(id, fields, this.logger);
|
|
1456
|
-
if (msg) {
|
|
1457
|
-
msg.stream = s;
|
|
1458
|
-
recovered.push(msg);
|
|
1459
|
-
}
|
|
1460
|
-
}
|
|
1461
|
-
}
|
|
1462
|
-
if (recovered.length > 0) this.logger?.info({
|
|
1463
|
-
group: this.group,
|
|
1464
|
-
count: recovered.length
|
|
1465
|
-
}, "autoclaimed pending messages");
|
|
1466
|
-
return recovered;
|
|
1467
|
-
}
|
|
1468
|
-
};
|
|
1469
|
-
//#endregion
|
|
1470
|
-
//#region src/shared/events.ts
|
|
1471
|
-
const globalEmitter = new EventEmitter();
|
|
1472
|
-
//#endregion
|
|
1473
|
-
//#region src/shared/cache.ts
|
|
1474
|
-
var LRUCache = class {
|
|
1475
|
-
cache = /* @__PURE__ */ new Map();
|
|
1476
|
-
maxSize;
|
|
1477
|
-
defaultTtlMs;
|
|
1478
|
-
constructor(maxSize = 1e3, defaultTtlMs = 3e5) {
|
|
1479
|
-
this.maxSize = maxSize;
|
|
1480
|
-
this.defaultTtlMs = defaultTtlMs;
|
|
1481
|
-
}
|
|
1482
|
-
get(key) {
|
|
1483
|
-
const item = this.cache.get(key);
|
|
1484
|
-
if (!item) return void 0;
|
|
1485
|
-
if (Date.now() > item.expiresAt) {
|
|
1486
|
-
this.cache.delete(key);
|
|
1487
|
-
return;
|
|
1488
|
-
}
|
|
1489
|
-
this.cache.delete(key);
|
|
1490
|
-
this.cache.set(key, item);
|
|
1491
|
-
return item.value;
|
|
1492
|
-
}
|
|
1493
|
-
set(key, value, ttlMs = this.defaultTtlMs) {
|
|
1494
|
-
if (this.cache.has(key)) this.cache.delete(key);
|
|
1495
|
-
else if (this.cache.size >= this.maxSize) {
|
|
1496
|
-
const oldestKey = this.cache.keys().next().value;
|
|
1497
|
-
if (oldestKey !== void 0) this.cache.delete(oldestKey);
|
|
1498
|
-
}
|
|
1499
|
-
this.cache.set(key, {
|
|
1500
|
-
value,
|
|
1501
|
-
expiresAt: Date.now() + ttlMs
|
|
1502
|
-
});
|
|
1503
|
-
}
|
|
1504
|
-
delete(key) {
|
|
1505
|
-
this.cache.delete(key);
|
|
1506
|
-
}
|
|
1507
|
-
clear() {
|
|
1508
|
-
this.cache.clear();
|
|
1509
|
-
}
|
|
1510
|
-
};
|
|
1511
|
-
//#endregion
|
|
1512
|
-
//#region src/shared/utils.ts
|
|
1513
|
-
function getPriorityBucket(priority) {
|
|
1514
|
-
const p = priority || "normal";
|
|
1515
|
-
return p === "critical" || p === "high" ? "critical" : p === "low" ? "low" : "normal";
|
|
1516
|
-
}
|
|
1517
|
-
/**
|
|
1518
|
-
* Canonical form of a destination, for suppression lookups.
|
|
1519
|
-
*
|
|
1520
|
-
* Both the writer (the provider webhook) and the reader (the engine's
|
|
1521
|
-
* pre-dispatch gate) must agree on this, or an unsubscribe recorded as
|
|
1522
|
-
* `Bob@Example.com` will not match a send addressed to `bob@example.com` and
|
|
1523
|
-
* the person keeps receiving mail. Case folding is safe for email domains and
|
|
1524
|
-
* for the local part in every mailbox provider in practice; phone numbers and
|
|
1525
|
-
* push tokens are case-sensitive and are only trimmed.
|
|
1526
|
-
*/
|
|
1527
|
-
function normaliseTarget(target) {
|
|
1528
|
-
const trimmed = target.trim();
|
|
1529
|
-
return trimmed.includes("@") ? trimmed.toLowerCase() : trimmed;
|
|
1530
|
-
}
|
|
1531
|
-
const LUA_SCHEDULER_POLL = `
|
|
1532
|
-
local key = KEYS[1]
|
|
1533
|
-
local maxScore = tonumber(ARGV[1])
|
|
1534
|
-
local limit = tonumber(ARGV[2])
|
|
1535
|
-
local visibilityTimeout = tonumber(ARGV[3]) or 0
|
|
1536
|
-
local tasks = redis.call('ZRANGE', key, 0, maxScore, 'BYSCORE', 'LIMIT', 0, limit)
|
|
1537
|
-
if #tasks > 0 then
|
|
1538
|
-
for i, task in ipairs(tasks) do
|
|
1539
|
-
redis.call('ZADD', key, maxScore + visibilityTimeout, task)
|
|
1540
|
-
end
|
|
1541
|
-
end
|
|
1542
|
-
return tasks
|
|
1543
|
-
`;
|
|
1544
|
-
const LUA_SCHEDULER_CLAIM = `
|
|
1545
|
-
local payloadKey = KEYS[1]
|
|
1546
|
-
local claimedKey = KEYS[2]
|
|
1547
|
-
|
|
1548
|
-
if redis.call('EXISTS', payloadKey) == 1 then
|
|
1549
|
-
redis.call('RENAME', payloadKey, claimedKey)
|
|
1550
|
-
return redis.call('GET', claimedKey)
|
|
1551
|
-
elseif redis.call('EXISTS', claimedKey) == 1 then
|
|
1552
|
-
return redis.call('GET', claimedKey)
|
|
1553
|
-
else
|
|
1554
|
-
return nil
|
|
1555
|
-
end
|
|
1556
|
-
`;
|
|
1557
|
-
/** Release a lock only if we still hold it (value matches our token). */
|
|
1558
|
-
const LUA_RELEASE_LOCK = `
|
|
1559
|
-
if redis.call('GET', KEYS[1]) == ARGV[1] then
|
|
1560
|
-
return redis.call('DEL', KEYS[1])
|
|
1561
|
-
end
|
|
1562
|
-
return 0
|
|
1563
|
-
`;
|
|
1564
|
-
/** Extend a lock's TTL only if we still hold it. */
|
|
1565
|
-
const LUA_RENEW_LOCK = `
|
|
1566
|
-
if redis.call('GET', KEYS[1]) == ARGV[1] then
|
|
1567
|
-
return redis.call('EXPIRE', KEYS[1], ARGV[2])
|
|
1568
|
-
end
|
|
1569
|
-
return 0
|
|
1570
|
-
`;
|
|
1571
|
-
//#endregion
|
|
1572
|
-
//#region src/shared/semaphore.ts
|
|
1573
|
-
var AsyncSemaphore = class {
|
|
1574
|
-
max;
|
|
1575
|
-
count = 0;
|
|
1576
|
-
queue = [];
|
|
1577
|
-
constructor(max) {
|
|
1578
|
-
this.max = max;
|
|
1579
|
-
}
|
|
1580
|
-
async acquire() {
|
|
1581
|
-
if (this.count < this.max) {
|
|
1582
|
-
this.count++;
|
|
1583
|
-
return Promise.resolve();
|
|
1584
|
-
}
|
|
1585
|
-
return new Promise((resolve) => {
|
|
1586
|
-
this.queue.push(resolve);
|
|
1587
|
-
});
|
|
1588
|
-
}
|
|
1589
|
-
release() {
|
|
1590
|
-
if (this.queue.length > 0) this.queue.shift()();
|
|
1591
|
-
else if (this.count > 0) this.count--;
|
|
1592
|
-
}
|
|
1593
|
-
get activeCount() {
|
|
1594
|
-
return this.count;
|
|
1595
|
-
}
|
|
1596
|
-
};
|
|
1597
|
-
//#endregion
|
|
1598
|
-
//#region src/shared/batch-processor.ts
|
|
1599
|
-
var BatchProcessor = class {
|
|
1600
|
-
maxSize;
|
|
1601
|
-
maxWaitMs;
|
|
1602
|
-
flushFn;
|
|
1603
|
-
buffer = [];
|
|
1604
|
-
timer = null;
|
|
1605
|
-
isFlushing = false;
|
|
1606
|
-
constructor(maxSize, maxWaitMs, flushFn) {
|
|
1607
|
-
this.maxSize = maxSize;
|
|
1608
|
-
this.maxWaitMs = maxWaitMs;
|
|
1609
|
-
this.flushFn = flushFn;
|
|
1610
|
-
}
|
|
1611
|
-
add(item) {
|
|
1612
|
-
return new Promise((resolve, reject) => {
|
|
1613
|
-
this.buffer.push({
|
|
1614
|
-
item,
|
|
1615
|
-
resolve,
|
|
1616
|
-
reject
|
|
1617
|
-
});
|
|
1618
|
-
if (this.buffer.length >= this.maxSize && !this.isFlushing) {
|
|
1619
|
-
if (this.timer) {
|
|
1620
|
-
clearTimeout(this.timer);
|
|
1621
|
-
this.timer = null;
|
|
1622
|
-
}
|
|
1623
|
-
this.flush();
|
|
1624
|
-
} else if (!this.timer && !this.isFlushing) this.timer = setTimeout(() => {
|
|
1625
|
-
this.timer = null;
|
|
1626
|
-
this.flush();
|
|
1627
|
-
}, this.maxWaitMs);
|
|
1628
|
-
});
|
|
1629
|
-
}
|
|
1630
|
-
async flush() {
|
|
1631
|
-
if (this.isFlushing || this.buffer.length === 0) return;
|
|
1632
|
-
this.isFlushing = true;
|
|
1633
|
-
const batch = this.buffer;
|
|
1634
|
-
this.buffer = [];
|
|
1635
|
-
try {
|
|
1636
|
-
const items = batch.map((b) => b.item);
|
|
1637
|
-
const results = await this.flushFn(items);
|
|
1638
|
-
for (let i = 0; i < batch.length; i++) batch[i].resolve(results[i]);
|
|
1639
|
-
} catch (err) {
|
|
1640
|
-
for (const b of batch) b.reject(err);
|
|
1641
|
-
} finally {
|
|
1642
|
-
this.isFlushing = false;
|
|
1643
|
-
if (this.buffer.length > 0 && !this.timer) if (this.buffer.length >= this.maxSize) this.flush();
|
|
1644
|
-
else this.timer = setTimeout(() => {
|
|
1645
|
-
this.timer = null;
|
|
1646
|
-
this.flush();
|
|
1647
|
-
}, this.maxWaitMs);
|
|
1648
|
-
}
|
|
1649
|
-
}
|
|
1650
|
-
};
|
|
1651
|
-
//#endregion
|
|
1652
|
-
//#region src/shared/circuit-breaker.ts
|
|
1653
|
-
var CircuitBreaker = class {
|
|
1654
|
-
state = "CLOSED";
|
|
1655
|
-
failures = 0;
|
|
1656
|
-
nextAttemptAt = 0;
|
|
1657
|
-
/** True while one caller is testing whether the dependency has recovered. */
|
|
1658
|
-
probeInFlight = false;
|
|
1659
|
-
threshold;
|
|
1660
|
-
timeout;
|
|
1661
|
-
constructor(options) {
|
|
1662
|
-
this.threshold = options.failureThreshold;
|
|
1663
|
-
this.timeout = options.resetTimeoutMs;
|
|
1664
|
-
}
|
|
1665
|
-
async execute(action) {
|
|
1666
|
-
let isProbe = false;
|
|
1667
|
-
if (this.state === "OPEN") if (Date.now() > this.nextAttemptAt && !this.probeInFlight) {
|
|
1668
|
-
this.state = "HALF_OPEN";
|
|
1669
|
-
this.probeInFlight = true;
|
|
1670
|
-
isProbe = true;
|
|
1671
|
-
} else throw new Error("Circuit breaker is OPEN");
|
|
1672
|
-
else if (this.state === "HALF_OPEN") {
|
|
1673
|
-
if (this.probeInFlight) throw new Error("Circuit breaker is OPEN");
|
|
1674
|
-
this.probeInFlight = true;
|
|
1675
|
-
isProbe = true;
|
|
1676
|
-
}
|
|
1677
|
-
try {
|
|
1678
|
-
const result = await action();
|
|
1679
|
-
this.onSuccess();
|
|
1680
|
-
return result;
|
|
1681
|
-
} catch (err) {
|
|
1682
|
-
this.onFailure();
|
|
1683
|
-
throw err;
|
|
1684
|
-
} finally {
|
|
1685
|
-
if (isProbe) this.probeInFlight = false;
|
|
1686
|
-
}
|
|
1687
|
-
}
|
|
1688
|
-
onSuccess() {
|
|
1689
|
-
this.failures = 0;
|
|
1690
|
-
this.state = "CLOSED";
|
|
1691
|
-
}
|
|
1692
|
-
onFailure() {
|
|
1693
|
-
this.failures++;
|
|
1694
|
-
if (this.failures >= this.threshold) {
|
|
1695
|
-
this.state = "OPEN";
|
|
1696
|
-
this.nextAttemptAt = Date.now() + this.timeout;
|
|
1697
|
-
}
|
|
1698
|
-
}
|
|
1699
|
-
getState() {
|
|
1700
|
-
return this.state;
|
|
1701
|
-
}
|
|
1702
|
-
};
|
|
1703
|
-
//#endregion
|
|
1704
|
-
//#region src/shared/dataloader.ts
|
|
1705
|
-
var DataLoader = class {
|
|
1706
|
-
batchLoadFn;
|
|
1707
|
-
keys = [];
|
|
1708
|
-
promises = [];
|
|
1709
|
-
currentTick = null;
|
|
1710
|
-
constructor(batchLoadFn) {
|
|
1711
|
-
this.batchLoadFn = batchLoadFn;
|
|
1712
|
-
}
|
|
1713
|
-
load(key) {
|
|
1714
|
-
return new Promise((resolve, reject) => {
|
|
1715
|
-
this.keys.push(key);
|
|
1716
|
-
this.promises.push({ resolve: (value) => {
|
|
1717
|
-
if (value instanceof Error) reject(value);
|
|
1718
|
-
else resolve(value);
|
|
1719
|
-
} });
|
|
1720
|
-
if (!this.currentTick) this.currentTick = Promise.resolve().then(() => {
|
|
1721
|
-
const keysToLoad = this.keys;
|
|
1722
|
-
const currentPromises = this.promises;
|
|
1723
|
-
this.keys = [];
|
|
1724
|
-
this.promises = [];
|
|
1725
|
-
this.currentTick = null;
|
|
1726
|
-
this.batchLoadFn(keysToLoad).then((results) => {
|
|
1727
|
-
for (let i = 0; i < currentPromises.length; i++) currentPromises[i].resolve(results[i]);
|
|
1728
|
-
}).catch((err) => {
|
|
1729
|
-
for (const p of currentPromises) p.resolve(err);
|
|
1874
|
+
idleMs: item[2],
|
|
1875
|
+
deliveryCount: item[3],
|
|
1876
|
+
stream: s
|
|
1730
1877
|
});
|
|
1731
|
-
}
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
//#endregion
|
|
1736
|
-
//#region src/shared/index.ts
|
|
1737
|
-
function generateId() {
|
|
1738
|
-
return randomUUID();
|
|
1739
|
-
}
|
|
1740
|
-
function sleep(ms) {
|
|
1741
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
1742
|
-
}
|
|
1743
|
-
var AppError = class extends Error {
|
|
1744
|
-
code;
|
|
1745
|
-
constructor(message, code, options) {
|
|
1746
|
-
super(message, options);
|
|
1747
|
-
this.name = "AppError";
|
|
1748
|
-
this.code = code;
|
|
1878
|
+
}
|
|
1879
|
+
if (allEntries.length >= limit) break;
|
|
1880
|
+
}
|
|
1881
|
+
return allEntries.slice(0, limit);
|
|
1749
1882
|
}
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1883
|
+
async autoclaim(minIdleMs, limit = 10) {
|
|
1884
|
+
const recovered = [];
|
|
1885
|
+
for (const s of this.streams) {
|
|
1886
|
+
if (recovered.length >= limit) break;
|
|
1887
|
+
const toClaim = (await this.getPendingEntries(limit * 2, s)).filter((p) => p.idleMs > minIdleMs * Math.pow(2, p.deliveryCount - 1)).slice(0, limit - recovered.length);
|
|
1888
|
+
if (toClaim.length === 0) continue;
|
|
1889
|
+
const ids = toClaim.map((p) => p.id);
|
|
1890
|
+
const countsById = new Map(toClaim.map((p) => [p.id, p.deliveryCount]));
|
|
1891
|
+
const result = await this.redis.xclaim(s, this.group, this.consumer, minIdleMs, ...ids);
|
|
1892
|
+
for (const raw of result) {
|
|
1893
|
+
if (!raw) continue;
|
|
1894
|
+
const [id, fields] = raw;
|
|
1895
|
+
const msg = parseMessage(id, fields, this.logger);
|
|
1896
|
+
if (msg) {
|
|
1897
|
+
msg.stream = s;
|
|
1898
|
+
msg.deliveryCount = (countsById.get(id) ?? 1) + 1;
|
|
1899
|
+
recovered.push(msg);
|
|
1900
|
+
}
|
|
1901
|
+
}
|
|
1902
|
+
}
|
|
1903
|
+
if (recovered.length > 0) this.logger?.info({
|
|
1904
|
+
group: this.group,
|
|
1905
|
+
count: recovered.length
|
|
1906
|
+
}, "autoclaimed pending messages");
|
|
1907
|
+
return recovered;
|
|
1757
1908
|
}
|
|
1758
1909
|
};
|
|
1759
1910
|
//#endregion
|
|
@@ -1853,20 +2004,10 @@ var UserThrottle = class {
|
|
|
1853
2004
|
const key = `throttle:${projectId}:user:${userId}`;
|
|
1854
2005
|
const targetTime = options.scheduledAt ? new Date(options.scheduledAt).getTime() : Date.now();
|
|
1855
2006
|
const windowStart = targetTime - windowMs;
|
|
1856
|
-
const memberId = randomUUID$1();
|
|
1857
|
-
const LUA_THROTTLE = `
|
|
1858
|
-
redis.call("ZREMRANGEBYSCORE", KEYS[1], "-inf", ARGV[1])
|
|
1859
|
-
local count = redis.call("ZCARD", KEYS[1])
|
|
1860
|
-
if tonumber(count) < tonumber(ARGV[2]) then
|
|
1861
|
-
redis.call("ZADD", KEYS[1], tonumber(ARGV[3]), ARGV[4])
|
|
1862
|
-
redis.call("EXPIRE", KEYS[1], tonumber(ARGV[5]))
|
|
1863
|
-
return tonumber(count) + 1
|
|
1864
|
-
end
|
|
1865
|
-
return tonumber(count) + 1
|
|
1866
|
-
`;
|
|
2007
|
+
const memberId = options.messageId ?? randomUUID$1();
|
|
1867
2008
|
const windowSeconds = Math.ceil(windowMs / 1e3);
|
|
1868
2009
|
const ttlSeconds = Math.max(windowSeconds, Math.ceil((targetTime - Date.now()) / 1e3) + windowSeconds);
|
|
1869
|
-
const count = await this.redis.
|
|
2010
|
+
const count = await this.redis.throttleUser(key, windowStart, limit, targetTime, memberId, ttlSeconds, Date.now() - windowMs);
|
|
1870
2011
|
return {
|
|
1871
2012
|
allowed: count <= limit,
|
|
1872
2013
|
count,
|
|
@@ -1874,10 +2015,330 @@ var UserThrottle = class {
|
|
|
1874
2015
|
};
|
|
1875
2016
|
}
|
|
1876
2017
|
};
|
|
2018
|
+
/**
|
|
2019
|
+
* Computes dynamic token lease batch size based on RPM.
|
|
2020
|
+
* Targets ~5s of limit capacity so Redis is contacted at most ~12 times/min
|
|
2021
|
+
* regardless of instantaneous request rate. Bounded between 1 and maxBatchSize.
|
|
2022
|
+
*/
|
|
2023
|
+
function calculateTokenBatchSize(limitRpm, maxBatchSize = 500) {
|
|
2024
|
+
if (limitRpm <= 0) return 0;
|
|
2025
|
+
if (limitRpm <= 10) return 1;
|
|
2026
|
+
if (limitRpm <= 60) return 5;
|
|
2027
|
+
const perSecond = limitRpm / 60;
|
|
2028
|
+
return Math.min(Math.max(Math.ceil(perSecond * 5), 10), maxBatchSize);
|
|
2029
|
+
}
|
|
2030
|
+
var ApiRateLimiter = class {
|
|
2031
|
+
redis;
|
|
2032
|
+
windowMs;
|
|
2033
|
+
maxBatchSize;
|
|
2034
|
+
logger;
|
|
2035
|
+
keyPrefix;
|
|
2036
|
+
leases = /* @__PURE__ */ new Map();
|
|
2037
|
+
constructor({ redis, windowMs = 6e4, maxBatchSize = 500, logger, keyPrefix = "rate-limit:api:req" }) {
|
|
2038
|
+
this.redis = redis;
|
|
2039
|
+
this.windowMs = windowMs;
|
|
2040
|
+
this.maxBatchSize = maxBatchSize;
|
|
2041
|
+
this.logger = logger;
|
|
2042
|
+
this.keyPrefix = keyPrefix;
|
|
2043
|
+
}
|
|
2044
|
+
/**
|
|
2045
|
+
* Evaluates if a request for `projectId` is allowed within its `limitRpm`.
|
|
2046
|
+
* Fast-path uses local in-memory token lease (0ms Redis network latency).
|
|
2047
|
+
* Refills in the background before tokens are depleted.
|
|
2048
|
+
*/
|
|
2049
|
+
async check(projectId, limitRpm) {
|
|
2050
|
+
if (limitRpm <= 0) return false;
|
|
2051
|
+
const nowMs = Date.now();
|
|
2052
|
+
const currentBucket = Math.floor(nowMs / this.windowMs);
|
|
2053
|
+
let state = this.leases.get(projectId);
|
|
2054
|
+
if (!state) {
|
|
2055
|
+
state = {
|
|
2056
|
+
bucketIndex: currentBucket,
|
|
2057
|
+
tokens: 0,
|
|
2058
|
+
batchSize: calculateTokenBatchSize(limitRpm, this.maxBatchSize),
|
|
2059
|
+
inFlightLease: null
|
|
2060
|
+
};
|
|
2061
|
+
this.leases.set(projectId, state);
|
|
2062
|
+
this.maybePrune(currentBucket);
|
|
2063
|
+
}
|
|
2064
|
+
if (state.bucketIndex !== currentBucket) {
|
|
2065
|
+
state.bucketIndex = currentBucket;
|
|
2066
|
+
state.tokens = 0;
|
|
2067
|
+
state.batchSize = calculateTokenBatchSize(limitRpm, this.maxBatchSize);
|
|
2068
|
+
state.inFlightLease = null;
|
|
2069
|
+
this.refill(projectId, state, limitRpm, nowMs, currentBucket).catch(() => {});
|
|
2070
|
+
}
|
|
2071
|
+
if (state.tokens > 0) {
|
|
2072
|
+
state.tokens--;
|
|
2073
|
+
const refillThreshold = Math.max(1, Math.floor(state.batchSize * .25));
|
|
2074
|
+
if (state.tokens <= refillThreshold && !state.inFlightLease) this.refill(projectId, state, limitRpm, nowMs, currentBucket).catch(() => {});
|
|
2075
|
+
return true;
|
|
2076
|
+
}
|
|
2077
|
+
for (;;) {
|
|
2078
|
+
if (!state.inFlightLease) this.refill(projectId, state, limitRpm, nowMs, currentBucket).catch(() => {});
|
|
2079
|
+
const granted = state.inFlightLease ? await state.inFlightLease : 0;
|
|
2080
|
+
if (state.bucketIndex !== currentBucket) return false;
|
|
2081
|
+
if (state.tokens > 0) {
|
|
2082
|
+
state.tokens--;
|
|
2083
|
+
return true;
|
|
2084
|
+
}
|
|
2085
|
+
if (granted <= 0) return false;
|
|
2086
|
+
}
|
|
2087
|
+
}
|
|
2088
|
+
async refill(projectId, state, limitRpm, nowMs, bucketIndex) {
|
|
2089
|
+
if (state.inFlightLease) return state.inFlightLease;
|
|
2090
|
+
const prevBucket = bucketIndex - 1;
|
|
2091
|
+
const rlTag = `{${this.keyPrefix}:${projectId || "global"}}`;
|
|
2092
|
+
const currentKey = `${rlTag}:${bucketIndex}`;
|
|
2093
|
+
const prevKey = `${rlTag}:${prevBucket}`;
|
|
2094
|
+
const requested = state.batchSize;
|
|
2095
|
+
let inFlightRef = null;
|
|
2096
|
+
const executeLease = async () => {
|
|
2097
|
+
try {
|
|
2098
|
+
let granted = await this.redis.leaseApiRateLimit(currentKey, prevKey, nowMs, this.windowMs, limitRpm, requested);
|
|
2099
|
+
if (typeof granted !== "number" || granted < 0) granted = requested;
|
|
2100
|
+
if (state.bucketIndex === bucketIndex) state.tokens += granted;
|
|
2101
|
+
return granted;
|
|
2102
|
+
} catch (err) {
|
|
2103
|
+
this.logger?.warn({
|
|
2104
|
+
err,
|
|
2105
|
+
projectId
|
|
2106
|
+
}, "project rate limit lease failed — allowing request (fail-open)");
|
|
2107
|
+
if (state.bucketIndex === bucketIndex) state.tokens += requested;
|
|
2108
|
+
return requested;
|
|
2109
|
+
} finally {
|
|
2110
|
+
if (state.inFlightLease === inFlightRef) state.inFlightLease = null;
|
|
2111
|
+
}
|
|
2112
|
+
};
|
|
2113
|
+
const promise = executeLease();
|
|
2114
|
+
inFlightRef = promise;
|
|
2115
|
+
state.inFlightLease = promise;
|
|
2116
|
+
return promise;
|
|
2117
|
+
}
|
|
2118
|
+
maybePrune(currentBucket) {
|
|
2119
|
+
if (this.leases.size > 2e3) {
|
|
2120
|
+
for (const [id, s] of this.leases) if (currentBucket - s.bucketIndex > 1) this.leases.delete(id);
|
|
2121
|
+
}
|
|
2122
|
+
}
|
|
2123
|
+
/** Reset all local cached leases */
|
|
2124
|
+
clear() {
|
|
2125
|
+
this.leases.clear();
|
|
2126
|
+
}
|
|
2127
|
+
/** Invalidate local lease for a specific project */
|
|
2128
|
+
invalidate(projectId) {
|
|
2129
|
+
this.leases.delete(projectId);
|
|
2130
|
+
}
|
|
2131
|
+
};
|
|
1877
2132
|
//#endregion
|
|
1878
2133
|
//#region src/redis/index.ts
|
|
1879
|
-
|
|
2134
|
+
/**
|
|
2135
|
+
* Sliding-window token leasing for high-throughput API rate limiting.
|
|
2136
|
+
* Atomically checks available capacity and reserves up to N tokens in one round trip.
|
|
2137
|
+
*/
|
|
2138
|
+
const LUA_LEASE_API_RATE_LIMIT = `
|
|
2139
|
+
local currentKey = KEYS[1]
|
|
2140
|
+
local prevKey = KEYS[2]
|
|
2141
|
+
local now = tonumber(ARGV[1])
|
|
2142
|
+
local window = tonumber(ARGV[2])
|
|
2143
|
+
local maxReqs = tonumber(ARGV[3])
|
|
2144
|
+
local requested = tonumber(ARGV[4]) or 1
|
|
2145
|
+
local ttl = tonumber(ARGV[5]) or (math.ceil((window * 2) / 1000) + 60)
|
|
2146
|
+
|
|
2147
|
+
if not now or not window or window <= 0 then
|
|
2148
|
+
return -1
|
|
2149
|
+
end
|
|
2150
|
+
if not maxReqs or maxReqs <= 0 then
|
|
2151
|
+
return 0
|
|
2152
|
+
end
|
|
2153
|
+
|
|
2154
|
+
if not prevKey then
|
|
2155
|
+
local currentBucket = math.floor(now / window)
|
|
2156
|
+
local prevBucket = currentBucket - 1
|
|
2157
|
+
currentKey = KEYS[1] .. ":" .. currentBucket
|
|
2158
|
+
prevKey = KEYS[1] .. ":" .. prevBucket
|
|
2159
|
+
end
|
|
2160
|
+
|
|
2161
|
+
local currentCount = tonumber(redis.call("GET", currentKey) or "0")
|
|
2162
|
+
local prevCount = tonumber(redis.call("GET", prevKey) or "0")
|
|
2163
|
+
|
|
2164
|
+
local timeIntoCurrent = now % window
|
|
2165
|
+
local weight = (window - timeIntoCurrent) / window
|
|
2166
|
+
local estimated = math.floor(prevCount * weight + currentCount)
|
|
2167
|
+
local available = maxReqs - estimated
|
|
2168
|
+
|
|
2169
|
+
if available <= 0 then
|
|
2170
|
+
return 0
|
|
2171
|
+
end
|
|
2172
|
+
|
|
2173
|
+
local toGrant = math.min(requested, available)
|
|
2174
|
+
local newCount = redis.call("INCRBY", currentKey, toGrant)
|
|
2175
|
+
if newCount == toGrant then
|
|
2176
|
+
redis.call("EXPIRE", currentKey, ttl)
|
|
2177
|
+
end
|
|
2178
|
+
|
|
2179
|
+
return toGrant
|
|
2180
|
+
`;
|
|
2181
|
+
/** Release a lock only if we still hold it (value matches our token). */
|
|
2182
|
+
const LUA_RELEASE_LOCK = `
|
|
2183
|
+
if redis.call('GET', KEYS[1]) == ARGV[1] then
|
|
2184
|
+
return redis.call('DEL', KEYS[1])
|
|
2185
|
+
end
|
|
2186
|
+
return 0
|
|
2187
|
+
`;
|
|
2188
|
+
/** Extend a lock's TTL only if we still hold it. */
|
|
2189
|
+
const LUA_RENEW_LOCK = `
|
|
2190
|
+
if redis.call('GET', KEYS[1]) == ARGV[1] then
|
|
2191
|
+
return redis.call('EXPIRE', KEYS[1], ARGV[2])
|
|
2192
|
+
end
|
|
2193
|
+
return 0
|
|
2194
|
+
`;
|
|
2195
|
+
/**
|
|
2196
|
+
* User-level sliding window notification throttle.
|
|
2197
|
+
*
|
|
2198
|
+
* Members are sends scored by when they go out, which for a scheduled send is
|
|
2199
|
+
* in the future. A send at targetTime is checked against the window ending at
|
|
2200
|
+
* it, (windowStart, targetTime], so sends scheduled later do not count against
|
|
2201
|
+
* it. Pruning uses pruneBefore (now minus one window), never windowStart: a
|
|
2202
|
+
* check for a far-future send must not delete sends that went out today.
|
|
2203
|
+
*
|
|
2204
|
+
* A member already present was counted by an earlier attempt at the same
|
|
2205
|
+
* message (a replay after a crash), so it is allowed again without counting
|
|
2206
|
+
* twice. Returns 0 in that case, otherwise the would-be count.
|
|
2207
|
+
*
|
|
2208
|
+
* ARGV: windowStart, limit, targetTime, member, ttlSeconds, pruneBefore
|
|
2209
|
+
*/
|
|
2210
|
+
const LUA_USER_THROTTLE = `
|
|
2211
|
+
local limit = tonumber(ARGV[2])
|
|
2212
|
+
local targetTime = tonumber(ARGV[3])
|
|
2213
|
+
local ttl = tonumber(ARGV[5])
|
|
2214
|
+
redis.call("ZREMRANGEBYSCORE", KEYS[1], "-inf", ARGV[6] or ARGV[1])
|
|
2215
|
+
if redis.call("ZSCORE", KEYS[1], ARGV[4]) then
|
|
2216
|
+
return 0
|
|
2217
|
+
end
|
|
2218
|
+
local count = redis.call("ZCOUNT", KEYS[1], "(" .. ARGV[1], targetTime)
|
|
2219
|
+
if count < limit then
|
|
2220
|
+
redis.call("ZADD", KEYS[1], targetTime, ARGV[4])
|
|
2221
|
+
-- Only ever extend: a send today must not cut short the life of a key
|
|
2222
|
+
-- that is still holding a send scheduled for next week.
|
|
2223
|
+
if redis.call("TTL", KEYS[1]) < ttl then
|
|
2224
|
+
redis.call("EXPIRE", KEYS[1], ttl)
|
|
2225
|
+
end
|
|
2226
|
+
end
|
|
2227
|
+
return count + 1
|
|
2228
|
+
`;
|
|
2229
|
+
/** Polls scheduler zsets with visibility timeouts. */
|
|
2230
|
+
const LUA_SCHEDULER_POLL = `
|
|
2231
|
+
local key = KEYS[1]
|
|
2232
|
+
local maxScore = tonumber(ARGV[1])
|
|
2233
|
+
local limit = tonumber(ARGV[2])
|
|
2234
|
+
local visibilityTimeout = tonumber(ARGV[3]) or 0
|
|
2235
|
+
local tasks = redis.call('ZRANGE', key, 0, maxScore, 'BYSCORE', 'LIMIT', 0, limit)
|
|
2236
|
+
if #tasks > 0 then
|
|
2237
|
+
for i, task in ipairs(tasks) do
|
|
2238
|
+
redis.call('ZADD', key, maxScore + visibilityTimeout, task)
|
|
2239
|
+
end
|
|
2240
|
+
end
|
|
2241
|
+
return tasks
|
|
2242
|
+
`;
|
|
2243
|
+
/** Two-phase idempotency lease acquisition. Checks sent/legacy, then attempts SET NX lock. */
|
|
2244
|
+
const LUA_ACQUIRE_LEASE = `
|
|
2245
|
+
if redis.call('EXISTS', KEYS[1]) == 1 or redis.call('EXISTS', KEYS[3]) == 1 then
|
|
2246
|
+
return 'completed'
|
|
2247
|
+
end
|
|
2248
|
+
local lockAcquired = redis.call('SET', KEYS[2], '1', 'EX', ARGV[1], 'NX')
|
|
2249
|
+
if lockAcquired then
|
|
2250
|
+
return 'acquired'
|
|
2251
|
+
end
|
|
2252
|
+
if redis.call('EXISTS', KEYS[1]) == 1 or redis.call('EXISTS', KEYS[3]) == 1 then
|
|
2253
|
+
return 'completed'
|
|
2254
|
+
end
|
|
2255
|
+
return 'locked'
|
|
2256
|
+
`;
|
|
2257
|
+
/** Two-phase idempotency completion marker. Atomically sets completed + legacy and deletes lock. */
|
|
2258
|
+
const LUA_MARK_PROCESSED = `
|
|
2259
|
+
redis.call('SET', KEYS[1], '1', 'EX', ARGV[1])
|
|
2260
|
+
redis.call('SET', KEYS[2], '1', 'EX', ARGV[1])
|
|
2261
|
+
redis.call('DEL', KEYS[3])
|
|
2262
|
+
return 1
|
|
2263
|
+
`;
|
|
2264
|
+
/**
|
|
2265
|
+
* Registers pre-compiled custom commands on an ioredis instance.
|
|
2266
|
+
* Calls defineCommand so ioredis uses EVALSHA rather than re-transmitting
|
|
2267
|
+
* full script strings over the wire.
|
|
2268
|
+
*/
|
|
2269
|
+
function registerCustomCommands(redis) {
|
|
2270
|
+
redis.defineCommand("leaseApiRateLimit", {
|
|
2271
|
+
numberOfKeys: 2,
|
|
2272
|
+
lua: LUA_LEASE_API_RATE_LIMIT
|
|
2273
|
+
});
|
|
2274
|
+
redis.defineCommand("releaseLock", {
|
|
2275
|
+
numberOfKeys: 1,
|
|
2276
|
+
lua: LUA_RELEASE_LOCK
|
|
2277
|
+
});
|
|
2278
|
+
redis.defineCommand("renewLock", {
|
|
2279
|
+
numberOfKeys: 1,
|
|
2280
|
+
lua: LUA_RENEW_LOCK
|
|
2281
|
+
});
|
|
2282
|
+
redis.defineCommand("throttleUser", {
|
|
2283
|
+
numberOfKeys: 1,
|
|
2284
|
+
lua: LUA_USER_THROTTLE
|
|
2285
|
+
});
|
|
2286
|
+
redis.defineCommand("schedulerPoll", {
|
|
2287
|
+
numberOfKeys: 1,
|
|
2288
|
+
lua: LUA_SCHEDULER_POLL
|
|
2289
|
+
});
|
|
2290
|
+
redis.defineCommand("acquireLease", {
|
|
2291
|
+
numberOfKeys: 3,
|
|
2292
|
+
lua: LUA_ACQUIRE_LEASE
|
|
2293
|
+
});
|
|
2294
|
+
redis.defineCommand("markProcessed", {
|
|
2295
|
+
numberOfKeys: 3,
|
|
2296
|
+
lua: LUA_MARK_PROCESSED
|
|
2297
|
+
});
|
|
2298
|
+
}
|
|
2299
|
+
const sharedClients = /* @__PURE__ */ new Map();
|
|
2300
|
+
var RedisClient = class RedisClient {
|
|
1880
2301
|
native;
|
|
2302
|
+
/**
|
|
2303
|
+
* One command connection per Redis URL for every service in this process.
|
|
2304
|
+
*
|
|
2305
|
+
* Each service used to open its own, so a single notification's commands
|
|
2306
|
+
* reached Redis over three or four sockets, one round trip each. Sharing a
|
|
2307
|
+
* connection lets auto-pipelining merge whatever the services issue in the
|
|
2308
|
+
* same tick into one write, and Redis spends most of its time per round trip
|
|
2309
|
+
* rather than per command. Blocking reads and subscribers still duplicate
|
|
2310
|
+
* their own connections.
|
|
2311
|
+
*
|
|
2312
|
+
* The handle's `disconnect()` releases this caller's reference; the
|
|
2313
|
+
* connection closes when the last service lets go.
|
|
2314
|
+
*/
|
|
2315
|
+
static shared(options) {
|
|
2316
|
+
let entry = sharedClients.get(options.url);
|
|
2317
|
+
if (!entry) {
|
|
2318
|
+
entry = {
|
|
2319
|
+
client: new RedisClient({
|
|
2320
|
+
...options,
|
|
2321
|
+
name: "notifkit"
|
|
2322
|
+
}),
|
|
2323
|
+
refs: 0
|
|
2324
|
+
};
|
|
2325
|
+
sharedClients.set(options.url, entry);
|
|
2326
|
+
}
|
|
2327
|
+
entry.refs++;
|
|
2328
|
+
const owner = entry;
|
|
2329
|
+
let released = false;
|
|
2330
|
+
const handle = Object.create(owner.client);
|
|
2331
|
+
handle.disconnect = async () => {
|
|
2332
|
+
if (released) return;
|
|
2333
|
+
released = true;
|
|
2334
|
+
owner.refs--;
|
|
2335
|
+
if (owner.refs === 0) {
|
|
2336
|
+
if (sharedClients.get(options.url) === owner) sharedClients.delete(options.url);
|
|
2337
|
+
await owner.client.disconnect();
|
|
2338
|
+
}
|
|
2339
|
+
};
|
|
2340
|
+
return handle;
|
|
2341
|
+
}
|
|
1881
2342
|
logger;
|
|
1882
2343
|
isClosing = false;
|
|
1883
2344
|
constructor({ url, name = "notifkit", logger, redisOptions }) {
|
|
@@ -1887,8 +2348,10 @@ var RedisClient = class {
|
|
|
1887
2348
|
enableReadyCheck: true,
|
|
1888
2349
|
lazyConnect: false,
|
|
1889
2350
|
connectionName: name,
|
|
2351
|
+
enableAutoPipelining: true,
|
|
1890
2352
|
...redisOptions
|
|
1891
2353
|
});
|
|
2354
|
+
registerCustomCommands(this.native);
|
|
1892
2355
|
this.native.on("connect", () => {
|
|
1893
2356
|
this.logger?.info({ url: redactUrl(url) }, "redis connected");
|
|
1894
2357
|
});
|
|
@@ -1937,8 +2400,16 @@ function redactUrl(url) {
|
|
|
1937
2400
|
}
|
|
1938
2401
|
//#endregion
|
|
1939
2402
|
//#region src/repositories/index.ts
|
|
2403
|
+
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
2404
|
+
/**
|
|
2405
|
+
* Applied when a profile is read, not when it is written: an upsert that
|
|
2406
|
+
* leaves a field out must keep the stored value rather than reset it.
|
|
2407
|
+
*/
|
|
2408
|
+
const DEFAULT_LANGUAGE = "en";
|
|
2409
|
+
const DEFAULT_TIMEZONE = "UTC";
|
|
1940
2410
|
var UserRepository = class {
|
|
1941
2411
|
db;
|
|
2412
|
+
recordCache = new LRUCache(5e3, 3e4);
|
|
1942
2413
|
constructor(db) {
|
|
1943
2414
|
this.db = db;
|
|
1944
2415
|
}
|
|
@@ -1948,49 +2419,30 @@ var UserRepository = class {
|
|
|
1948
2419
|
const attrs = rows[0].attributes;
|
|
1949
2420
|
return {
|
|
1950
2421
|
userId: rows[0].externalId,
|
|
1951
|
-
language: attrs.language,
|
|
1952
|
-
timezone: attrs.timezone,
|
|
2422
|
+
language: attrs.language ?? DEFAULT_LANGUAGE,
|
|
2423
|
+
timezone: attrs.timezone ?? DEFAULT_TIMEZONE,
|
|
1953
2424
|
email: attrs.email
|
|
1954
2425
|
};
|
|
1955
2426
|
}
|
|
1956
2427
|
async findRecordById(projectId, userId) {
|
|
1957
|
-
const
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
const attrs = userRow.attributes;
|
|
1962
|
-
const [segmentRows, topicRows, channelRows, qhRows] = await Promise.all([
|
|
1963
|
-
this.db.select().from(userSegments).where(eq(userSegments.userId, internalId)),
|
|
1964
|
-
this.db.select().from(userTopicPreferences).where(eq(userTopicPreferences.userId, internalId)),
|
|
1965
|
-
this.db.select().from(userChannelPreferences).where(eq(userChannelPreferences.userId, internalId)),
|
|
1966
|
-
this.db.select().from(quietHours).where(eq(quietHours.userId, internalId))
|
|
1967
|
-
]);
|
|
1968
|
-
const segments = segmentRows.map((r) => r.segment);
|
|
1969
|
-
const topics = {};
|
|
1970
|
-
for (const r of topicRows) topics[r.topic] = r.enabled;
|
|
1971
|
-
const channels = {};
|
|
1972
|
-
for (const r of channelRows) channels[r.channel] = r.enabled;
|
|
1973
|
-
const quietHoursList = qhRows.map((r) => ({
|
|
1974
|
-
start: r.startTime.slice(0, 5),
|
|
1975
|
-
end: r.endTime.slice(0, 5)
|
|
1976
|
-
}));
|
|
1977
|
-
return {
|
|
1978
|
-
userId: userRow.externalId,
|
|
1979
|
-
language: attrs.language,
|
|
1980
|
-
timezone: attrs.timezone,
|
|
1981
|
-
email: attrs.email,
|
|
1982
|
-
segments,
|
|
1983
|
-
preferences: {
|
|
1984
|
-
channels,
|
|
1985
|
-
topics,
|
|
1986
|
-
quietHours: quietHoursList.length > 0 ? quietHoursList : void 0
|
|
1987
|
-
}
|
|
1988
|
-
};
|
|
2428
|
+
const cacheKey = `${projectId}:${userId}`;
|
|
2429
|
+
const cached = this.recordCache.get(cacheKey);
|
|
2430
|
+
if (cached !== void 0) return cached;
|
|
2431
|
+
return (await this.findRecordsByIds(projectId, [userId]))[0] ?? null;
|
|
1989
2432
|
}
|
|
1990
2433
|
async findRecordsByIds(projectId, userIds) {
|
|
1991
2434
|
if (userIds.length === 0) return [];
|
|
1992
|
-
const
|
|
1993
|
-
|
|
2435
|
+
const results = [];
|
|
2436
|
+
const missingIds = [];
|
|
2437
|
+
for (const id of userIds) {
|
|
2438
|
+
const cacheKey = `${projectId}:${id}`;
|
|
2439
|
+
const cached = this.recordCache.get(cacheKey);
|
|
2440
|
+
if (cached !== void 0) results.push(cached);
|
|
2441
|
+
else missingIds.push(id);
|
|
2442
|
+
}
|
|
2443
|
+
if (missingIds.length === 0) return results;
|
|
2444
|
+
const usersRows = await this.db.select().from(users).where(and(inArray(users.externalId, missingIds), eq(users.projectId, projectId)));
|
|
2445
|
+
if (usersRows.length === 0) return results;
|
|
1994
2446
|
const internalIds = usersRows.map((r) => r.id);
|
|
1995
2447
|
const [segmentRows, topicRows, channelRows, qhRows] = await Promise.all([
|
|
1996
2448
|
this.db.select().from(userSegments).where(inArray(userSegments.userId, internalIds)),
|
|
@@ -2022,14 +2474,13 @@ var UserRepository = class {
|
|
|
2022
2474
|
end: r.endTime.slice(0, 5)
|
|
2023
2475
|
});
|
|
2024
2476
|
}
|
|
2025
|
-
const userRecords = [];
|
|
2026
2477
|
for (const userRow of usersRows) {
|
|
2027
2478
|
const attrs = userRow.attributes;
|
|
2028
2479
|
const internalId = userRow.id;
|
|
2029
|
-
|
|
2480
|
+
const record = {
|
|
2030
2481
|
userId: userRow.externalId,
|
|
2031
|
-
language: attrs.language,
|
|
2032
|
-
timezone: attrs.timezone,
|
|
2482
|
+
language: attrs.language ?? DEFAULT_LANGUAGE,
|
|
2483
|
+
timezone: attrs.timezone ?? DEFAULT_TIMEZONE,
|
|
2033
2484
|
email: attrs.email,
|
|
2034
2485
|
segments: segmentsByUserId.get(internalId) || [],
|
|
2035
2486
|
preferences: {
|
|
@@ -2037,9 +2488,11 @@ var UserRepository = class {
|
|
|
2037
2488
|
topics: topicsByUserId.get(internalId) || {},
|
|
2038
2489
|
quietHours: qhByUserId.get(internalId)
|
|
2039
2490
|
}
|
|
2040
|
-
}
|
|
2491
|
+
};
|
|
2492
|
+
this.recordCache.set(`${projectId}:${record.userId}`, record);
|
|
2493
|
+
results.push(record);
|
|
2041
2494
|
}
|
|
2042
|
-
return
|
|
2495
|
+
return results;
|
|
2043
2496
|
}
|
|
2044
2497
|
async upsertFull(projectId, user) {
|
|
2045
2498
|
await this.db.transaction(async (tx) => {
|
|
@@ -2099,13 +2552,16 @@ var UserRepository = class {
|
|
|
2099
2552
|
})));
|
|
2100
2553
|
}
|
|
2101
2554
|
});
|
|
2555
|
+
this.recordCache.delete(`${projectId}:${user.userId}`);
|
|
2102
2556
|
}
|
|
2103
2557
|
async upsertManyFull(projectId, usersList) {
|
|
2104
2558
|
if (usersList.length === 0) return;
|
|
2559
|
+
const uniqueUsers = [...new Map(usersList.map((u) => [u.userId, u])).values()];
|
|
2560
|
+
const winners = new Set(uniqueUsers);
|
|
2105
2561
|
let attempts = 0;
|
|
2106
2562
|
while (attempts < 3) try {
|
|
2107
2563
|
await this.db.transaction(async (tx) => {
|
|
2108
|
-
await tx.insert(users).values(
|
|
2564
|
+
await tx.insert(users).values(uniqueUsers.map((u) => ({
|
|
2109
2565
|
projectId,
|
|
2110
2566
|
externalId: u.userId,
|
|
2111
2567
|
attributes: {
|
|
@@ -2116,7 +2572,7 @@ var UserRepository = class {
|
|
|
2116
2572
|
}))).onConflictDoUpdate({
|
|
2117
2573
|
target: [users.projectId, users.externalId],
|
|
2118
2574
|
set: {
|
|
2119
|
-
attributes: sql
|
|
2575
|
+
attributes: sql`${users.attributes} || excluded.attributes`,
|
|
2120
2576
|
updatedAt: /* @__PURE__ */ new Date()
|
|
2121
2577
|
}
|
|
2122
2578
|
});
|
|
@@ -2146,7 +2602,7 @@ var UserRepository = class {
|
|
|
2146
2602
|
channel,
|
|
2147
2603
|
enabled
|
|
2148
2604
|
});
|
|
2149
|
-
if (u.preferences?.quietHours && u.preferences.quietHours.length > 0) for (const window of u.preferences.quietHours) quietHoursInserts.push({
|
|
2605
|
+
if (winners.has(u) && u.preferences?.quietHours && u.preferences.quietHours.length > 0) for (const window of u.preferences.quietHours) quietHoursInserts.push({
|
|
2150
2606
|
userId: internalId,
|
|
2151
2607
|
startTime: window.start,
|
|
2152
2608
|
endTime: window.end
|
|
@@ -2176,10 +2632,11 @@ var UserRepository = class {
|
|
|
2176
2632
|
target: [userChannelPreferences.userId, userChannelPreferences.channel],
|
|
2177
2633
|
set: { enabled: sql`excluded.enabled` }
|
|
2178
2634
|
});
|
|
2179
|
-
const internalIdsToClearQuietHours =
|
|
2635
|
+
const internalIdsToClearQuietHours = uniqueUsers.filter((u) => u.preferences?.quietHours !== void 0).map((u) => idMap.get(u.userId)).filter(Boolean);
|
|
2180
2636
|
if (internalIdsToClearQuietHours.length > 0) await tx.delete(quietHours).where(inArray(quietHours.userId, internalIdsToClearQuietHours));
|
|
2181
2637
|
if (quietHoursInserts.length > 0) await tx.insert(quietHours).values(quietHoursInserts);
|
|
2182
2638
|
});
|
|
2639
|
+
for (const u of usersList) this.recordCache.delete(`${projectId}:${u.userId}`);
|
|
2183
2640
|
return;
|
|
2184
2641
|
} catch (err) {
|
|
2185
2642
|
attempts++;
|
|
@@ -2193,7 +2650,7 @@ var UserRepository = class {
|
|
|
2193
2650
|
const attrs = {
|
|
2194
2651
|
language: patch.language ?? existing.language,
|
|
2195
2652
|
timezone: patch.timezone ?? existing.timezone,
|
|
2196
|
-
email: patch.email
|
|
2653
|
+
email: patch.email !== void 0 ? patch.email : existing.email
|
|
2197
2654
|
};
|
|
2198
2655
|
await this.db.transaction(async (tx) => {
|
|
2199
2656
|
await tx.update(users).set({
|
|
@@ -2240,38 +2697,37 @@ var UserRepository = class {
|
|
|
2240
2697
|
})));
|
|
2241
2698
|
}
|
|
2242
2699
|
});
|
|
2700
|
+
this.recordCache.delete(`${projectId}:${userId}`);
|
|
2243
2701
|
return true;
|
|
2244
2702
|
}
|
|
2245
2703
|
async delete(projectId, userId) {
|
|
2704
|
+
this.recordCache.delete(`${projectId}:${userId}`);
|
|
2246
2705
|
return (await this.db.delete(users).where(and(eq(users.externalId, userId), eq(users.projectId, projectId))).returning()).length > 0;
|
|
2247
2706
|
}
|
|
2248
2707
|
async list(projectId, limit, cursor, filters) {
|
|
2249
2708
|
const conditions = [eq(users.projectId, projectId)];
|
|
2250
|
-
if (cursor) {
|
|
2251
|
-
const cursorDate = new Date(parseInt(cursor, 10));
|
|
2252
|
-
if (!isNaN(cursorDate.getTime())) conditions.push(sql`${users.createdAt} < ${cursorDate.toISOString()}`);
|
|
2253
|
-
}
|
|
2709
|
+
if (cursor && UUID_RE.test(cursor)) conditions.push(sql`(${users.createdAt}, ${users.id}) < (SELECT c.created_at, c.id FROM ${users} c WHERE c.id = ${cursor})`);
|
|
2254
2710
|
if (filters?.language) conditions.push(sql`(${users.attributes}->>'language') = ${filters.language}`);
|
|
2255
2711
|
if (filters?.timezone) conditions.push(sql`(${users.attributes}->>'timezone') = ${filters.timezone}`);
|
|
2256
2712
|
if (filters?.search) {
|
|
2257
|
-
const term = `%${filters.search.trim()}%`;
|
|
2713
|
+
const term = `%${filters.search.trim().replace(/[\\%_]/g, "\\$&")}%`;
|
|
2258
2714
|
conditions.push(sql`(${users.externalId} ILIKE ${term} OR (${users.attributes}->>'email') ILIKE ${term})`);
|
|
2259
2715
|
}
|
|
2260
2716
|
if (filters?.segment) conditions.push(sql`EXISTS (SELECT 1 FROM ${userSegments} WHERE ${userSegments.userId} = ${users.id} AND ${userSegments.segment} = ${filters.segment})`);
|
|
2261
2717
|
if (filters?.channel) conditions.push(sql`EXISTS (SELECT 1 FROM ${userContacts} WHERE ${userContacts.userId} = ${users.id} AND ${userContacts.channel} = ${filters.channel})`);
|
|
2262
|
-
const
|
|
2263
|
-
const attrs = r.attributes;
|
|
2264
|
-
return {
|
|
2265
|
-
userId: r.externalId,
|
|
2266
|
-
language: attrs.language,
|
|
2267
|
-
timezone: attrs.timezone,
|
|
2268
|
-
email: attrs.email,
|
|
2269
|
-
createdAt: r.createdAt.getTime()
|
|
2270
|
-
};
|
|
2271
|
-
});
|
|
2718
|
+
const rows = await this.db.select().from(users).where(and(...conditions)).orderBy(desc(users.createdAt), desc(users.id)).limit(limit);
|
|
2272
2719
|
return {
|
|
2273
|
-
users:
|
|
2274
|
-
|
|
2720
|
+
users: rows.map((r) => {
|
|
2721
|
+
const attrs = r.attributes;
|
|
2722
|
+
return {
|
|
2723
|
+
userId: r.externalId,
|
|
2724
|
+
language: attrs.language ?? DEFAULT_LANGUAGE,
|
|
2725
|
+
timezone: attrs.timezone ?? DEFAULT_TIMEZONE,
|
|
2726
|
+
email: attrs.email,
|
|
2727
|
+
createdAt: r.createdAt.getTime()
|
|
2728
|
+
};
|
|
2729
|
+
}),
|
|
2730
|
+
nextCursor: rows.length === limit ? rows[rows.length - 1].id : null
|
|
2275
2731
|
};
|
|
2276
2732
|
}
|
|
2277
2733
|
async findUsersBySegment(projectId, segmentName) {
|
|
@@ -2302,6 +2758,7 @@ var PreferenceRepository = class {
|
|
|
2302
2758
|
};
|
|
2303
2759
|
var ContactRepository = class {
|
|
2304
2760
|
db;
|
|
2761
|
+
activeContactsCache = new LRUCache(5e3, 3e4);
|
|
2305
2762
|
constructor(db) {
|
|
2306
2763
|
this.db = db;
|
|
2307
2764
|
}
|
|
@@ -2329,24 +2786,49 @@ var ContactRepository = class {
|
|
|
2329
2786
|
async findActiveByUserIds(projectId, userIds) {
|
|
2330
2787
|
const byUser = /* @__PURE__ */ new Map();
|
|
2331
2788
|
if (userIds.length === 0) return byUser;
|
|
2789
|
+
const missingIds = [];
|
|
2790
|
+
for (const userId of userIds) {
|
|
2791
|
+
const cacheKey = `${projectId}:${userId}`;
|
|
2792
|
+
const cached = this.activeContactsCache.get(cacheKey);
|
|
2793
|
+
if (cached !== void 0) {
|
|
2794
|
+
if (cached.length > 0) byUser.set(userId, cached);
|
|
2795
|
+
} else missingIds.push(userId);
|
|
2796
|
+
}
|
|
2797
|
+
if (missingIds.length === 0) return byUser;
|
|
2332
2798
|
const rows = await this.db.select({
|
|
2333
2799
|
userId: users.externalId,
|
|
2334
2800
|
id: userContacts.id,
|
|
2335
2801
|
channel: userContacts.channel,
|
|
2336
2802
|
target: userContacts.target,
|
|
2337
2803
|
enabled: userContacts.enabled
|
|
2338
|
-
}).from(users).innerJoin(userContacts, eq(users.id, userContacts.userId)).where(and(eq(users.projectId, projectId), inArray(users.externalId,
|
|
2804
|
+
}).from(users).innerJoin(userContacts, eq(users.id, userContacts.userId)).where(and(eq(users.projectId, projectId), inArray(users.externalId, missingIds), eq(userContacts.enabled, true)));
|
|
2805
|
+
const topicsByContact = /* @__PURE__ */ new Map();
|
|
2806
|
+
if (rows.length > 0) {
|
|
2807
|
+
const topicRows = await this.db.select().from(contactTopicPreferences).where(inArray(contactTopicPreferences.contactId, rows.map((r) => r.id)));
|
|
2808
|
+
for (const t of topicRows) {
|
|
2809
|
+
if (!topicsByContact.has(t.contactId)) topicsByContact.set(t.contactId, {});
|
|
2810
|
+
topicsByContact.get(t.contactId)[t.topic] = t.enabled;
|
|
2811
|
+
}
|
|
2812
|
+
}
|
|
2813
|
+
const fetchedByUser = /* @__PURE__ */ new Map();
|
|
2814
|
+
for (const id of missingIds) fetchedByUser.set(id, []);
|
|
2339
2815
|
for (const row of rows) {
|
|
2340
|
-
const contacts =
|
|
2816
|
+
const contacts = fetchedByUser.get(row.userId) ?? [];
|
|
2817
|
+
const topics = topicsByContact.get(row.id);
|
|
2341
2818
|
contacts.push({
|
|
2342
2819
|
id: row.id,
|
|
2343
2820
|
userId: row.userId,
|
|
2344
2821
|
channel: row.channel,
|
|
2345
2822
|
target: row.target,
|
|
2346
|
-
preferences: {},
|
|
2823
|
+
preferences: topics ? { topics } : {},
|
|
2347
2824
|
active: row.enabled
|
|
2348
2825
|
});
|
|
2349
|
-
|
|
2826
|
+
fetchedByUser.set(row.userId, contacts);
|
|
2827
|
+
}
|
|
2828
|
+
for (const id of missingIds) {
|
|
2829
|
+
const contacts = fetchedByUser.get(id) ?? [];
|
|
2830
|
+
this.activeContactsCache.set(`${projectId}:${id}`, contacts);
|
|
2831
|
+
if (contacts.length > 0) byUser.set(id, contacts);
|
|
2350
2832
|
}
|
|
2351
2833
|
return byUser;
|
|
2352
2834
|
}
|
|
@@ -2376,6 +2858,7 @@ var ContactRepository = class {
|
|
|
2376
2858
|
target: [contactTopicPreferences.contactId, contactTopicPreferences.topic],
|
|
2377
2859
|
set: { enabled: sql`excluded.enabled` }
|
|
2378
2860
|
});
|
|
2861
|
+
this.activeContactsCache.delete(`${projectId}:${userId}`);
|
|
2379
2862
|
}
|
|
2380
2863
|
async upsertMany(projectId, contactsList) {
|
|
2381
2864
|
if (contactsList.length === 0) return;
|
|
@@ -2384,7 +2867,24 @@ var ContactRepository = class {
|
|
|
2384
2867
|
externalId: users.externalId
|
|
2385
2868
|
}).from(users).where(and(inArray(users.externalId, contactsList.map((c) => c.userId)), eq(users.projectId, projectId)));
|
|
2386
2869
|
const idMap = new Map(internalUserIdRows.map((r) => [r.externalId, r.id]));
|
|
2387
|
-
const
|
|
2870
|
+
const byAddress = /* @__PURE__ */ new Map();
|
|
2871
|
+
for (const c of contactsList) {
|
|
2872
|
+
if (!idMap.has(c.userId)) continue;
|
|
2873
|
+
const key = `${c.userId}\u0000${c.channel}\u0000${c.target}`;
|
|
2874
|
+
const seen = byAddress.get(key);
|
|
2875
|
+
byAddress.set(key, seen ? {
|
|
2876
|
+
...c,
|
|
2877
|
+
preferences: {
|
|
2878
|
+
...seen.preferences,
|
|
2879
|
+
...c.preferences,
|
|
2880
|
+
topics: {
|
|
2881
|
+
...seen.preferences?.topics,
|
|
2882
|
+
...c.preferences?.topics
|
|
2883
|
+
}
|
|
2884
|
+
}
|
|
2885
|
+
} : c);
|
|
2886
|
+
}
|
|
2887
|
+
const validContacts = [...byAddress.values()];
|
|
2388
2888
|
if (validContacts.length === 0) return;
|
|
2389
2889
|
const inserted = await this.db.insert(userContacts).values(validContacts.map((c) => ({
|
|
2390
2890
|
userId: idMap.get(c.userId),
|
|
@@ -2421,6 +2921,7 @@ var ContactRepository = class {
|
|
|
2421
2921
|
target: [contactTopicPreferences.contactId, contactTopicPreferences.topic],
|
|
2422
2922
|
set: { enabled: sql`excluded.enabled` }
|
|
2423
2923
|
});
|
|
2924
|
+
for (const c of validContacts) this.activeContactsCache.delete(`${projectId}:${c.userId}`);
|
|
2424
2925
|
}
|
|
2425
2926
|
/**
|
|
2426
2927
|
* Mark a contact unusable without destroying it — used when a provider
|
|
@@ -2428,11 +2929,13 @@ var ContactRepository = class {
|
|
|
2428
2929
|
* device permanently on what is often a transient provider response.
|
|
2429
2930
|
*/
|
|
2430
2931
|
async deactivate(projectId, userId, channel, target) {
|
|
2932
|
+
this.activeContactsCache.delete(`${projectId}:${userId}`);
|
|
2431
2933
|
const internalId = (await this.db.select({ id: users.id }).from(users).where(and(eq(users.externalId, userId), eq(users.projectId, projectId))))[0]?.id;
|
|
2432
2934
|
if (!internalId) return false;
|
|
2433
2935
|
return (await this.db.update(userContacts).set({ enabled: false }).where(and(eq(userContacts.userId, internalId), eq(userContacts.channel, channel), eq(userContacts.target, target))).returning()).length > 0;
|
|
2434
2936
|
}
|
|
2435
2937
|
async delete(projectId, userId, channel, target) {
|
|
2938
|
+
this.activeContactsCache.delete(`${projectId}:${userId}`);
|
|
2436
2939
|
const internalId = (await this.db.select({ id: users.id }).from(users).where(and(eq(users.externalId, userId), eq(users.projectId, projectId))))[0]?.id;
|
|
2437
2940
|
if (!internalId) return false;
|
|
2438
2941
|
return (await this.db.delete(userContacts).where(and(eq(userContacts.userId, internalId), eq(userContacts.channel, channel), eq(userContacts.target, target))).returning()).length > 0;
|
|
@@ -2518,6 +3021,7 @@ var ProjectRepository = class {
|
|
|
2518
3021
|
await tx.delete(suppressions).where(eq(suppressions.projectId, id));
|
|
2519
3022
|
await tx.delete(messageLogs).where(eq(messageLogs.projectId, id));
|
|
2520
3023
|
await tx.delete(workflowInstances).where(eq(workflowInstances.projectId, id));
|
|
3024
|
+
await tx.delete(workflowDefinitions).where(eq(workflowDefinitions.projectId, id));
|
|
2521
3025
|
return (await tx.delete(projects).where(eq(projects.id, id)).returning()).length > 0;
|
|
2522
3026
|
});
|
|
2523
3027
|
}
|
|
@@ -2747,43 +3251,33 @@ var TemplateCache = class {
|
|
|
2747
3251
|
}
|
|
2748
3252
|
};
|
|
2749
3253
|
//#endregion
|
|
2750
|
-
//#region src/
|
|
2751
|
-
|
|
2752
|
-
|
|
2753
|
-
|
|
2754
|
-
|
|
2755
|
-
|
|
2756
|
-
|
|
2757
|
-
|
|
2758
|
-
|
|
2759
|
-
|
|
2760
|
-
|
|
2761
|
-
register(eventType, renderer) {
|
|
2762
|
-
this.renderers.set(eventType, renderer);
|
|
3254
|
+
//#region src/transport/index.ts
|
|
3255
|
+
var TransportRegistry = class {
|
|
3256
|
+
transports = /* @__PURE__ */ new Map();
|
|
3257
|
+
register(transport, priority = 0) {
|
|
3258
|
+
const list = this.transports.get(transport.channel) || [];
|
|
3259
|
+
list.push({
|
|
3260
|
+
transport,
|
|
3261
|
+
priority
|
|
3262
|
+
});
|
|
3263
|
+
list.sort((a, b) => b.priority - a.priority);
|
|
3264
|
+
this.transports.set(transport.channel, list);
|
|
2763
3265
|
}
|
|
2764
|
-
|
|
2765
|
-
|
|
2766
|
-
|
|
2767
|
-
if (renderer) return renderer(ctx);
|
|
2768
|
-
return defaultRenderer(ctx);
|
|
3266
|
+
get(channel) {
|
|
3267
|
+
const list = this.transports.get(channel);
|
|
3268
|
+
return list && list.length > 0 ? list[0]?.transport : void 0;
|
|
2769
3269
|
}
|
|
2770
|
-
|
|
2771
|
-
|
|
3270
|
+
getAll(channel) {
|
|
3271
|
+
const list = this.transports.get(channel);
|
|
3272
|
+
return list ? list.map((item) => item.transport) : [];
|
|
2772
3273
|
}
|
|
2773
|
-
|
|
2774
|
-
return [...this.
|
|
3274
|
+
registeredChannels() {
|
|
3275
|
+
return [...this.transports.keys()];
|
|
2775
3276
|
}
|
|
2776
3277
|
};
|
|
2777
|
-
|
|
2778
|
-
|
|
2779
|
-
|
|
2780
|
-
subject: asText(vars.subject ?? vars.title),
|
|
2781
|
-
body: asText(vars.body ?? vars.message) ?? `Notification: ${ctx.eventType}`
|
|
2782
|
-
} };
|
|
2783
|
-
}
|
|
2784
|
-
const templateRegistry = new TemplateRegistry();
|
|
2785
|
-
function renderTemplate(ctx) {
|
|
2786
|
-
return templateRegistry.render(ctx);
|
|
3278
|
+
const transportRegistry = new TransportRegistry();
|
|
3279
|
+
function registerTransport(transport, priority = 0) {
|
|
3280
|
+
transportRegistry.register(transport, priority);
|
|
2787
3281
|
}
|
|
2788
3282
|
//#endregion
|
|
2789
3283
|
//#region src/unsubscribe/index.ts
|
|
@@ -2878,6 +3372,13 @@ var NonRetryableError = class extends Error {
|
|
|
2878
3372
|
this.name = "NonRetryableError";
|
|
2879
3373
|
}
|
|
2880
3374
|
};
|
|
3375
|
+
var LockHeldError = class extends Error {
|
|
3376
|
+
lockHeld = true;
|
|
3377
|
+
constructor(message = "Idempotency lock held — leaving message pending") {
|
|
3378
|
+
super(message);
|
|
3379
|
+
this.name = "LockHeldError";
|
|
3380
|
+
}
|
|
3381
|
+
};
|
|
2881
3382
|
var BaseWorker = class {
|
|
2882
3383
|
logger;
|
|
2883
3384
|
consumer;
|
|
@@ -2892,6 +3393,7 @@ var BaseWorker = class {
|
|
|
2892
3393
|
lastProcessedAt = null;
|
|
2893
3394
|
lastErrorAt = null;
|
|
2894
3395
|
recoveryTimer = null;
|
|
3396
|
+
recovering = false;
|
|
2895
3397
|
lastPendingCount = null;
|
|
2896
3398
|
active = /* @__PURE__ */ new Set();
|
|
2897
3399
|
semaphore;
|
|
@@ -2971,25 +3473,74 @@ var BaseWorker = class {
|
|
|
2971
3473
|
pendingCount: this.lastPendingCount
|
|
2972
3474
|
};
|
|
2973
3475
|
}
|
|
3476
|
+
/**
|
|
3477
|
+
* Processes an event produced by an upstream stage in this same process,
|
|
3478
|
+
* skipping the stream hop in between.
|
|
3479
|
+
*
|
|
3480
|
+
* The caller is still holding its own stream entry, and does not ack it until
|
|
3481
|
+
* this returns. A thrown error therefore leaves that entry pending and the
|
|
3482
|
+
* whole chain is replayed from the upstream stage; the idempotency keys each
|
|
3483
|
+
* stage already checks make the replay safe. Non-retryable failures go to the
|
|
3484
|
+
* dead-letter stream here, as they would have from this worker's own stream,
|
|
3485
|
+
* so they do not send the upstream message there instead.
|
|
3486
|
+
*/
|
|
3487
|
+
async processInline(event) {
|
|
3488
|
+
try {
|
|
3489
|
+
await this.process({
|
|
3490
|
+
id: event.id,
|
|
3491
|
+
event,
|
|
3492
|
+
deliveryCount: 1,
|
|
3493
|
+
inline: true
|
|
3494
|
+
}, 1);
|
|
3495
|
+
this.processedCount += 1;
|
|
3496
|
+
this.lastProcessedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
3497
|
+
metrics.messagesProcessed.inc({
|
|
3498
|
+
worker: this.constructor.name,
|
|
3499
|
+
status: "success"
|
|
3500
|
+
});
|
|
3501
|
+
} catch (err) {
|
|
3502
|
+
if (err instanceof LockHeldError || err?.lockHeld) return;
|
|
3503
|
+
this.errorCount += 1;
|
|
3504
|
+
this.lastErrorAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
3505
|
+
metrics.messagesProcessed.inc({
|
|
3506
|
+
worker: this.constructor.name,
|
|
3507
|
+
status: "error"
|
|
3508
|
+
});
|
|
3509
|
+
if (err instanceof NonRetryableError || err?.nonRetryable) {
|
|
3510
|
+
this.logger.warn({
|
|
3511
|
+
err,
|
|
3512
|
+
eventId: event.id
|
|
3513
|
+
}, "non-retryable error on inline message — moving to dead-letter queue");
|
|
3514
|
+
await this.consumer.deadLetter(event, err.message);
|
|
3515
|
+
globalEmitter.emit("notification:failed", event.id, err.message, event.type);
|
|
3516
|
+
return;
|
|
3517
|
+
}
|
|
3518
|
+
throw err;
|
|
3519
|
+
}
|
|
3520
|
+
}
|
|
2974
3521
|
async processWithTracking(message) {
|
|
2975
3522
|
const start = Date.now();
|
|
2976
3523
|
const stream = message.stream;
|
|
2977
|
-
const
|
|
3524
|
+
const retryCount = message.deliveryCount ?? 1;
|
|
2978
3525
|
try {
|
|
2979
|
-
const retryCount = (await this.consumer.redis.multi().incr(retryKey).expire(retryKey, 7200).exec())?.[0]?.[1] ?? 1;
|
|
2980
3526
|
if (retryCount > this.maxRetriesBeforeDlq) {
|
|
2981
3527
|
this.logger.warn({
|
|
2982
3528
|
messageId: message.id,
|
|
2983
3529
|
retryCount
|
|
2984
3530
|
}, "max retries exceeded, moving to dead-letter queue");
|
|
2985
|
-
await this.consumer.nack(message.id, message.event, stream);
|
|
2986
|
-
|
|
2987
|
-
|
|
3531
|
+
await this.consumer.nack(message.id, message.event, stream, `max retries exceeded (${retryCount - 1} attempts)`);
|
|
3532
|
+
globalEmitter.emit("notification:failed", message.event.id, "Poison pill: max retries exceeded", message.event.type);
|
|
3533
|
+
return;
|
|
3534
|
+
}
|
|
3535
|
+
const result = await this.process(message, retryCount);
|
|
3536
|
+
if (result && result.ack === false) {
|
|
3537
|
+
this.logger.debug({
|
|
3538
|
+
messageId: message.id,
|
|
3539
|
+
eventType: message.event.type
|
|
3540
|
+
}, "message skipped without ack — leaving pending in stream");
|
|
2988
3541
|
return;
|
|
2989
3542
|
}
|
|
2990
|
-
await this.process(message, retryCount);
|
|
2991
3543
|
await this.consumer.ack(message.id, stream);
|
|
2992
|
-
await this.consumer.redis.del(retryKey);
|
|
2993
3544
|
this.processedCount += 1;
|
|
2994
3545
|
this.lastProcessedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
2995
3546
|
metrics.messagesProcessed.inc({
|
|
@@ -3002,6 +3553,14 @@ var BaseWorker = class {
|
|
|
3002
3553
|
durationMs: Date.now() - start
|
|
3003
3554
|
}, "message processed");
|
|
3004
3555
|
} catch (err) {
|
|
3556
|
+
if (err instanceof LockHeldError || err?.lockHeld) {
|
|
3557
|
+
this.logger.debug({
|
|
3558
|
+
err,
|
|
3559
|
+
messageId: message.id,
|
|
3560
|
+
eventType: message.event.type
|
|
3561
|
+
}, "lock held by another worker — leaving message pending in stream");
|
|
3562
|
+
return;
|
|
3563
|
+
}
|
|
3005
3564
|
this.errorCount += 1;
|
|
3006
3565
|
this.lastErrorAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
3007
3566
|
metrics.messagesProcessed.inc({
|
|
@@ -3013,9 +3572,8 @@ var BaseWorker = class {
|
|
|
3013
3572
|
err,
|
|
3014
3573
|
messageId: message.id
|
|
3015
3574
|
}, "non-retryable error encountered, immediately moving to dead-letter queue without retry loop");
|
|
3016
|
-
await this.consumer.nack(message.id, message.event, stream);
|
|
3017
|
-
|
|
3018
|
-
globalEmitter.emit("notification:failed", message.id, err.message, message.event.type);
|
|
3575
|
+
await this.consumer.nack(message.id, message.event, stream, err.message);
|
|
3576
|
+
globalEmitter.emit("notification:failed", message.event.id, err.message, message.event.type);
|
|
3019
3577
|
return;
|
|
3020
3578
|
}
|
|
3021
3579
|
this.logger.error({
|
|
@@ -3025,12 +3583,26 @@ var BaseWorker = class {
|
|
|
3025
3583
|
}, "failed to process message");
|
|
3026
3584
|
}
|
|
3027
3585
|
}
|
|
3586
|
+
/**
|
|
3587
|
+
* `recoveryIntervalMs` is how long an entry must sit idle before it is
|
|
3588
|
+
* claimed, not how often to look. Scanning only once per interval, timed from
|
|
3589
|
+
* this worker's start, meant a worker restarted more often than that (a crash
|
|
3590
|
+
* loop, an eager autoscaler) never scanned at all, and whatever its
|
|
3591
|
+
* predecessors held stayed pending for good. So scan on start and then
|
|
3592
|
+
* several times per interval; the idle threshold alone decides what is taken.
|
|
3593
|
+
*/
|
|
3028
3594
|
startRecoveryLoop() {
|
|
3029
|
-
|
|
3030
|
-
if (this.state
|
|
3595
|
+
const scan = () => {
|
|
3596
|
+
if (this.state !== "running" || this.recovering) return;
|
|
3597
|
+
this.recovering = true;
|
|
3598
|
+
this.recover().catch((err) => {
|
|
3031
3599
|
this.logger.error({ err }, "recovery loop error");
|
|
3600
|
+
}).finally(() => {
|
|
3601
|
+
this.recovering = false;
|
|
3032
3602
|
});
|
|
3033
|
-
}
|
|
3603
|
+
};
|
|
3604
|
+
scan();
|
|
3605
|
+
this.recoveryTimer = setInterval(scan, Math.max(Math.floor(this.recoveryIntervalMs / 4), 10));
|
|
3034
3606
|
}
|
|
3035
3607
|
stopRecoveryLoop() {
|
|
3036
3608
|
if (this.recoveryTimer) {
|
|
@@ -3088,7 +3660,7 @@ function resolveStepTarget(args, instanceInput) {
|
|
|
3088
3660
|
* `templateId`, `sendAt` becomes `scheduledAt` — so this translation is
|
|
3089
3661
|
* deliberate rather than a spread, and the return type keeps it honest.
|
|
3090
3662
|
*/
|
|
3091
|
-
function buildStepNotifyPayload(args, instanceInput, projectId, idempotencyKey) {
|
|
3663
|
+
function buildStepNotifyPayload(args, instanceInput, projectId, idempotencyKey, workflowInstanceId) {
|
|
3092
3664
|
return {
|
|
3093
3665
|
projectId,
|
|
3094
3666
|
target: resolveStepTarget(args, instanceInput),
|
|
@@ -3099,7 +3671,8 @@ function buildStepNotifyPayload(args, instanceInput, projectId, idempotencyKey)
|
|
|
3099
3671
|
aiPrompts: args.aiPrompts,
|
|
3100
3672
|
fallback: args.fallback ?? false,
|
|
3101
3673
|
scheduledAt: args.sendAt,
|
|
3102
|
-
idempotencyKey
|
|
3674
|
+
idempotencyKey,
|
|
3675
|
+
...workflowInstanceId ? { workflowInstanceId } : {}
|
|
3103
3676
|
};
|
|
3104
3677
|
}
|
|
3105
3678
|
//#endregion
|
|
@@ -3140,9 +3713,16 @@ var NotifkitClient = class {
|
|
|
3140
3713
|
body: body ? JSON.stringify(body) : void 0
|
|
3141
3714
|
});
|
|
3142
3715
|
if (res.status === 204) return;
|
|
3143
|
-
const
|
|
3716
|
+
const text = await res.text();
|
|
3717
|
+
let data;
|
|
3718
|
+
try {
|
|
3719
|
+
data = text ? JSON.parse(text) : void 0;
|
|
3720
|
+
} catch {
|
|
3721
|
+
data = void 0;
|
|
3722
|
+
}
|
|
3144
3723
|
if (!res.ok) {
|
|
3145
|
-
const
|
|
3724
|
+
const body = data;
|
|
3725
|
+
const errorMsg = body?.message || body?.error || `Request failed with status ${res.status}`;
|
|
3146
3726
|
throw new Error(errorMsg);
|
|
3147
3727
|
}
|
|
3148
3728
|
return data;
|
|
@@ -3415,11 +3995,15 @@ var NotifkitServer = class extends EventEmitter {
|
|
|
3415
3995
|
}
|
|
3416
3996
|
}
|
|
3417
3997
|
async start() {
|
|
3418
|
-
const { setGlobalConfig, readBaseConfig } = await Promise.resolve().then(() => config_exports);
|
|
3419
3998
|
if (this.options.port) process.env.PORT = String(this.options.port);
|
|
3420
3999
|
if (this.options.logLevel) process.env.LOG_LEVEL = this.options.logLevel;
|
|
3421
4000
|
if (this.options.nodeEnv) process.env.NODE_ENV = this.options.nodeEnv;
|
|
4001
|
+
if (this.options.adminUser?.email) process.env.ADMIN_EMAIL = this.options.adminUser.email;
|
|
4002
|
+
if (this.options.adminUser?.password) process.env.ADMIN_PASSWORD = this.options.adminUser.password;
|
|
4003
|
+
if (this.options.adminUser?.username) process.env.ADMIN_USERNAME = this.options.adminUser.username;
|
|
3422
4004
|
if (this.options.workerConcurrency) process.env.WORKER_CONCURRENCY = String(this.options.workerConcurrency);
|
|
4005
|
+
if (this.options.fusedPipeline !== void 0) process.env.PIPELINE_FUSED = String(this.options.fusedPipeline);
|
|
4006
|
+
if (this.options.deliveryConcurrency) process.env.DELIVERY_CONCURRENCY = String(this.options.deliveryConcurrency);
|
|
3423
4007
|
if (this.options.redisOptions?.maxQueueLength) process.env.QUEUE_MAX_LEN = String(this.options.redisOptions.maxQueueLength);
|
|
3424
4008
|
if (this.options.dbOptions?.maxConnections) process.env.DB_MAX_CONNECTIONS = String(this.options.dbOptions.maxConnections);
|
|
3425
4009
|
const isProduction = process.env.NODE_ENV === "production";
|
|
@@ -3440,13 +4024,9 @@ var NotifkitServer = class extends EventEmitter {
|
|
|
3440
4024
|
if (this.options.redisUrl) process.env.REDIS_URL = this.options.redisUrl;
|
|
3441
4025
|
if (this.options.databaseUrl) process.env.DATABASE_URL = this.options.databaseUrl;
|
|
3442
4026
|
setGlobalConfig(readBaseConfig());
|
|
3443
|
-
if (this.options.aiModel) {
|
|
3444
|
-
const { setAiConfig } = await Promise.resolve().then(() => config_exports);
|
|
3445
|
-
setAiConfig({ aiModel: this.options.aiModel });
|
|
3446
|
-
}
|
|
4027
|
+
if (this.options.aiModel) setAiConfig({ aiModel: this.options.aiModel });
|
|
3447
4028
|
if (this.options.autoMigrate !== false) {
|
|
3448
4029
|
this.logger.info("Running database migrations...");
|
|
3449
|
-
const { createDatabase, runMigrations } = await Promise.resolve().then(() => db_exports);
|
|
3450
4030
|
const { db, sql } = createDatabase({ url: this.options.databaseUrl });
|
|
3451
4031
|
await runMigrations(db);
|
|
3452
4032
|
await sql.end();
|
|
@@ -3468,35 +4048,35 @@ var NotifkitServer = class extends EventEmitter {
|
|
|
3468
4048
|
}
|
|
3469
4049
|
const startupPromises = [];
|
|
3470
4050
|
if (services.includes("api")) {
|
|
3471
|
-
const { startApiServer } = await import("./main-
|
|
4051
|
+
const { startApiServer } = await import("./main-HH-WwsMM.mjs");
|
|
3472
4052
|
startupPromises.push(startApiServer());
|
|
3473
4053
|
}
|
|
3474
4054
|
if (services.includes("delivery")) {
|
|
3475
|
-
const { startDeliveryWorker } = await import("./main-
|
|
4055
|
+
const { startDeliveryWorker } = await import("./main-D84OX0ux.mjs");
|
|
3476
4056
|
startupPromises.push(startDeliveryWorker());
|
|
3477
4057
|
}
|
|
3478
4058
|
if (services.includes("engine")) {
|
|
3479
|
-
const { startEngineWorker } = await import("./main-
|
|
4059
|
+
const { startEngineWorker } = await import("./main-BpnuFX62.mjs");
|
|
3480
4060
|
startupPromises.push(startEngineWorker());
|
|
3481
4061
|
}
|
|
3482
4062
|
if (services.includes("enricher")) {
|
|
3483
|
-
const { startEnricherWorker } = await import("./main-
|
|
4063
|
+
const { startEnricherWorker } = await import("./main-HY_TDs61.mjs");
|
|
3484
4064
|
startupPromises.push(startEnricherWorker());
|
|
3485
4065
|
}
|
|
3486
4066
|
if (services.includes("scheduler")) {
|
|
3487
|
-
const { startSchedulerWorker } = await import("./main-
|
|
4067
|
+
const { startSchedulerWorker } = await import("./main-D6od9N8h.mjs");
|
|
3488
4068
|
startupPromises.push(startSchedulerWorker());
|
|
3489
4069
|
}
|
|
3490
4070
|
if (services.includes("ai")) {
|
|
3491
|
-
const { startAiWorker } = await import("./main-
|
|
4071
|
+
const { startAiWorker } = await import("./main-BENOY7HG.mjs");
|
|
3492
4072
|
startupPromises.push(startAiWorker());
|
|
3493
4073
|
}
|
|
3494
4074
|
if (services.includes("workflow")) {
|
|
3495
|
-
const { startWorkflowWorker } = await import("./main-
|
|
4075
|
+
const { startWorkflowWorker } = await import("./main-C_5VFIwp.mjs");
|
|
3496
4076
|
startupPromises.push(startWorkflowWorker());
|
|
3497
4077
|
}
|
|
3498
4078
|
if (services.includes("events")) {
|
|
3499
|
-
const { startEventWorker } = await import("./main-
|
|
4079
|
+
const { startEventWorker } = await import("./main-BqVH2BHj.mjs");
|
|
3500
4080
|
startupPromises.push(startEventWorker());
|
|
3501
4081
|
}
|
|
3502
4082
|
const handleSignal = async (signal) => {
|
|
@@ -3516,6 +4096,21 @@ var NotifkitServer = class extends EventEmitter {
|
|
|
3516
4096
|
this.signalHandlersAttached = true;
|
|
3517
4097
|
}
|
|
3518
4098
|
await Promise.all(startupPromises);
|
|
4099
|
+
if (readBaseConfig().PIPELINE_FUSED && services.includes("enricher") && services.includes("engine") && services.includes("delivery")) {
|
|
4100
|
+
const [{ getEnricherWorker }, { getEngineWorker }, { getDeliveryWorker }] = await Promise.all([
|
|
4101
|
+
import("./main-HY_TDs61.mjs"),
|
|
4102
|
+
import("./main-BpnuFX62.mjs"),
|
|
4103
|
+
import("./main-D84OX0ux.mjs")
|
|
4104
|
+
]);
|
|
4105
|
+
const enricher = getEnricherWorker();
|
|
4106
|
+
const engine = getEngineWorker();
|
|
4107
|
+
const delivery = getDeliveryWorker();
|
|
4108
|
+
if (enricher && engine && delivery) {
|
|
4109
|
+
engine.setInlineNext((event) => delivery.processInline(event));
|
|
4110
|
+
enricher.setInlineNext((event) => engine.processInline(event));
|
|
4111
|
+
this.logger.info("pipeline fused: enricher → engine → delivery hand off in-process");
|
|
4112
|
+
}
|
|
4113
|
+
}
|
|
3519
4114
|
}
|
|
3520
4115
|
async stop() {
|
|
3521
4116
|
if (this.signalHandlersAttached) {
|
|
@@ -3535,38 +4130,17 @@ var NotifkitServer = class extends EventEmitter {
|
|
|
3535
4130
|
"workflow",
|
|
3536
4131
|
"events"
|
|
3537
4132
|
] : this.options.services;
|
|
3538
|
-
|
|
3539
|
-
|
|
3540
|
-
await
|
|
3541
|
-
|
|
3542
|
-
|
|
3543
|
-
|
|
3544
|
-
await
|
|
3545
|
-
|
|
3546
|
-
|
|
3547
|
-
|
|
3548
|
-
|
|
3549
|
-
}
|
|
3550
|
-
if (services.includes("enricher")) {
|
|
3551
|
-
const { stopEnricherWorker } = await import("./main-AO0JaY5z.mjs");
|
|
3552
|
-
await stopEnricherWorker();
|
|
3553
|
-
}
|
|
3554
|
-
if (services.includes("scheduler")) {
|
|
3555
|
-
const { stopSchedulerWorker } = await import("./main-BMYSYcv2.mjs");
|
|
3556
|
-
await stopSchedulerWorker();
|
|
3557
|
-
}
|
|
3558
|
-
if (services.includes("ai")) {
|
|
3559
|
-
const { stopAiWorker } = await import("./main-DSmh1e-V.mjs");
|
|
3560
|
-
await stopAiWorker();
|
|
3561
|
-
}
|
|
3562
|
-
if (services.includes("workflow")) {
|
|
3563
|
-
const { stopWorkflowWorker } = await import("./main-D19Wxs0x.mjs");
|
|
3564
|
-
await stopWorkflowWorker();
|
|
3565
|
-
}
|
|
3566
|
-
if (services.includes("events")) {
|
|
3567
|
-
const { stopEventWorker } = await import("./main-4Wyge9ms.mjs");
|
|
3568
|
-
await stopEventWorker();
|
|
3569
|
-
}
|
|
4133
|
+
const shutdownOrder = [
|
|
4134
|
+
["api", async () => (await import("./main-HH-WwsMM.mjs")).stopApiServer()],
|
|
4135
|
+
["workflow", async () => (await import("./main-C_5VFIwp.mjs")).stopWorkflowWorker()],
|
|
4136
|
+
["enricher", async () => (await import("./main-HY_TDs61.mjs")).stopEnricherWorker()],
|
|
4137
|
+
["ai", async () => (await import("./main-BENOY7HG.mjs")).stopAiWorker()],
|
|
4138
|
+
["engine", async () => (await import("./main-BpnuFX62.mjs")).stopEngineWorker()],
|
|
4139
|
+
["scheduler", async () => (await import("./main-D6od9N8h.mjs")).stopSchedulerWorker()],
|
|
4140
|
+
["delivery", async () => (await import("./main-D84OX0ux.mjs")).stopDeliveryWorker()],
|
|
4141
|
+
["events", async () => (await import("./main-BqVH2BHj.mjs")).stopEventWorker()]
|
|
4142
|
+
];
|
|
4143
|
+
for (const [name, stop] of shutdownOrder) if (services.includes(name)) await stop();
|
|
3570
4144
|
if (this.pgContainer) {
|
|
3571
4145
|
this.logger.info("Stopping PostgreSQL container...");
|
|
3572
4146
|
await this.pgContainer.stop();
|
|
@@ -3578,6 +4152,4 @@ var NotifkitServer = class extends EventEmitter {
|
|
|
3578
4152
|
}
|
|
3579
4153
|
};
|
|
3580
4154
|
//#endregion
|
|
3581
|
-
export {
|
|
3582
|
-
|
|
3583
|
-
//# sourceMappingURL=src-CGmXRfsM.mjs.map
|
|
4155
|
+
export { deliveryOutbox as $, EventRegistry as $t, LUA_ACQUIRE_LEASE as A, IngestEventSchema as At, ApiRateLimiter as B, UserContactInputSchema as Bt, ContactRepository as C, LRUCache as Cn, NotificationRequestedPayloadSchema as Ct, TemplateRepository as D, BatchAddContactsSchema as Dt, SegmentRepository as E, AddUserSchema as Et, LUA_SCHEDULER_POLL as F, SyncTemplatesSchema as Ft, StreamConsumer as G, ENRICHED_STREAMS as Gt, UserThrottle as H, WorkflowStepSchema as Ht, LUA_USER_THROTTLE as I, TemplateSchema as It, metrics as J, PUBSUB_CHANNELS as Jt, StreamProducer as K, INBOUND_STREAMS as Kt, Redis as L, TriggerWorkflowSchema as Lt, LUA_MARK_PROCESSED as M, NotifyRequestSchema as Mt, LUA_RELEASE_LOCK as N, PreferencesSchema as Nt, UserRepository as O, ContactChannelSchema as Ot, LUA_RENEW_LOCK as P, QuietHoursSchema as Pt, runMigrations as Q, StreamEventSchema as Qt, RedisClient as R, UpdateProjectSchema as Rt, AdminUserRepository as S, normaliseTarget as Sn, NotificationCreatedPayloadSchema as St, ProjectRepository as T, AddContactSchema as Tt, calculateTokenBatchSize as U, buildStreamEvent as Ut, ProjectSettingsCache as V, WorkflowNotifyPayloadSchema as Vt, PendingMessageScanner as W, CONSUMER_GROUPS as Wt, IdempotencyGuard as X, EventEnvelopeSchema as Xt, createLogger as Y, STREAMS as Yt, createDatabase as Z, StreamEventMetadataSchema as Zt, TemplateCache as _, DataLoader as _n, NotificationDispatchedPayloadSchema as _t, SuspendExecutionError as a, AI_DEFAULTS as an, userTopicPreferences as at, interpolate as b, AsyncSemaphore as bn, NotificationEnrichedPayloadSchema as bt, BaseWorker as c, loadEnv as cn, workflowInstances as ct, startHealthReporter as d, setAiConfig as dn, NotificationAiPendingPayloadSchema as dt, registry as en, messageLogs as et, buildUnsubscribeHeaders as f, setGlobalConfig as fn, NotificationCanceledPayloadSchema as ft, transportRegistry as g, sleep as gn, DeliveryOptionsSchema as gt, registerTransport as h, generateId as hn, NotificationDeliveredPayloadSchema as ht, workflowRegistry as i, NotificationStatusSchema as in, suppressions as it, LUA_LEASE_API_RATE_LIMIT as j, InlineUserSchema as jt, WorkflowRepository as k, CreateWorkflowSchema as kt, LockHeldError as l, parseConfig as ln, workflowSteps as lt, verifyUnsubscribeToken as m, ValidationError as mn, NotificationFailedPayloadSchema as mt, NotifkitClient as n, NotificationChannelSchema as nn, projects as nt, buildStepNotifyPayload as o, baseConfigSchema as on, users as ot, signUnsubscribeToken as p, AppError as pn, NotificationSkippedPayloadSchema as pt, getMetricsRegistry as q, OUTBOUND_STREAMS as qt, workflow as r, NotificationPrioritySchema as rn, scheduledPayloads as rt, resolveStepTarget as s, getAiConfig as sn, workflowDefinitions as st, NotifkitServer as t, EventMetadataSchema as tn, projectApiKeys as tt, NonRetryableError as u, readBaseConfig as un, workflowWaiters as ut, escapeHeader as v, CircuitBreaker as vn, RenderedContentSchema as vt, PreferenceRepository as w, globalEmitter as wn, NotificationTargetSchema as wt, renderWithTemplate as x, getPriorityBucket as xn, RecipientProfileSchema as xt, escapeHtml as y, BatchProcessor as yn, NotificationScheduledPayloadSchema as yt, registerCustomCommands as z, UpdateUserSchema as zt };
|