notifkit 0.1.13 → 0.1.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -0
- package/dist/{bootstrap-omGPIBny.mjs → bootstrap-BOmT69OH.mjs} +1 -1
- package/dist/{bulk-C4hTcnLu.mjs → bulk-ZPiOJRx-.mjs} +1 -1
- package/dist/index.d.mts +16 -4
- package/dist/index.mjs +2 -2
- package/dist/{main-DAXO-MiH.mjs → main-24J4U8yP.mjs} +3 -3
- package/dist/{main-BtnGmyaT.mjs → main-BdZvBgV7.mjs} +9 -3
- package/dist/{main-B5BwjIMu.mjs → main-C0m9-Eos.mjs} +58 -5
- package/dist/{main-DYGCl7Ja.mjs → main-C6S3EsJ3.mjs} +2 -2
- package/dist/{main-B-AZ-xoN.mjs → main-CCqcFDDe.mjs} +2 -2
- package/dist/{main-Bes11Ad3.mjs → main-CTjaZ7ZD.mjs} +2 -2
- package/dist/{main-BU27VjDC.mjs → main-JEXBLP2A.mjs} +2 -2
- package/dist/{main-BxAeC_k6.mjs → main-g33jpia4.mjs} +157 -76
- package/dist/{src-CIPTzq9i.mjs → src-9LXITcNi.mjs} +64 -39
- package/drizzle/0008_delivery_outbox_destination_idx.sql +3 -0
- package/drizzle/meta/_journal.json +7 -0
- package/package.json +3 -2
- package/scripts/create-admin.mjs +8 -0
- package/scripts/create-project.mjs +0 -0
- package/scripts/migrate.mjs +0 -0
- package/scripts/notifkit.mjs +0 -0
- package/src/config/index.ts +6 -3
- package/src/contracts/streams.ts +4 -0
- package/src/db/schema.ts +3 -0
- package/src/queue/index.ts +19 -23
- package/src/redis/index.ts +20 -0
- package/src/services/api/admin-static.ts +21 -11
- package/src/services/api/handlers.ts +51 -43
- package/src/services/api/http.ts +12 -9
- package/src/services/api/live-events.ts +74 -0
- package/src/services/api/main.ts +57 -23
- package/src/services/delivery/live-events.ts +68 -0
- package/src/services/delivery/main.ts +14 -2
- package/src/services/engine/main.ts +13 -1
- package/dashboard/README.md +0 -29
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "notifkit",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.15",
|
|
4
4
|
"description": "Self-hosted notification infrastructure. One call delivers to email, SMS, push, and webhook — routed by preference, quiet hours, and consent.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "devkitshq",
|
|
@@ -71,7 +71,8 @@
|
|
|
71
71
|
"build:dashboard": "npm --prefix dashboard run build",
|
|
72
72
|
"test": "vitest run --coverage",
|
|
73
73
|
"test:watch": "vitest",
|
|
74
|
-
"lint": "eslint .
|
|
74
|
+
"lint": "eslint .",
|
|
75
|
+
"lint:fix": "eslint . --fix",
|
|
75
76
|
"typecheck": "tsc --noEmit",
|
|
76
77
|
"typecheck:workspaces": "npm run typecheck --workspaces --if-present",
|
|
77
78
|
"prepare": "husky",
|
package/scripts/create-admin.mjs
CHANGED
|
@@ -34,6 +34,14 @@ if (!email || !password) {
|
|
|
34
34
|
process.exit(1);
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
// The API grants writes to superadmin and admin and treats every other value
|
|
38
|
+
// as read-only, so a typo would quietly produce a user who cannot do anything.
|
|
39
|
+
const ROLES = ["superadmin", "admin", "read_only"];
|
|
40
|
+
if (!ROLES.includes(role)) {
|
|
41
|
+
console.error(`Unknown role "${role}". Use one of: ${ROLES.join(", ")}.`);
|
|
42
|
+
process.exit(1);
|
|
43
|
+
}
|
|
44
|
+
|
|
37
45
|
const databaseUrl =
|
|
38
46
|
process.env.DATABASE_URL || "postgres://platform:platform@localhost:5432/notifkit";
|
|
39
47
|
const schema = (process.env.DB_SCHEMA || "public").trim();
|
|
File without changes
|
package/scripts/migrate.mjs
CHANGED
|
File without changes
|
package/scripts/notifkit.mjs
CHANGED
|
File without changes
|
package/src/config/index.ts
CHANGED
|
@@ -97,13 +97,16 @@ export const baseConfigSchema = z.object({
|
|
|
97
97
|
*/
|
|
98
98
|
UNSUBSCRIBE_SECRET: z.string().min(16).optional(),
|
|
99
99
|
/**
|
|
100
|
-
*
|
|
101
|
-
*
|
|
100
|
+
* How many reverse proxies sit in front of the API, for reading the client
|
|
101
|
+
* address out of X-Forwarded-For. `true` means one. Each proxy appends the
|
|
102
|
+
* address it saw, so the client is that many entries from the right; the
|
|
103
|
+
* entries further left are whatever the client sent. Defaults to 0, which
|
|
104
|
+
* ignores the header so it cannot be used to get around rate limits.
|
|
102
105
|
*/
|
|
103
106
|
TRUST_PROXY: z
|
|
104
107
|
.string()
|
|
105
108
|
.optional()
|
|
106
|
-
.transform((v) => v === "true"
|
|
109
|
+
.transform((v) => (v === "true" ? 1 : v && /^\d+$/.test(v) ? Number(v) : 0)),
|
|
107
110
|
/**
|
|
108
111
|
* Comma-separated list of allowed origins for CORS on session-authenticated admin routes.
|
|
109
112
|
*/
|
package/src/contracts/streams.ts
CHANGED
|
@@ -47,6 +47,10 @@ export const PUBSUB_CHANNELS = {
|
|
|
47
47
|
PROJECT_INVALIDATED: "project.invalidated",
|
|
48
48
|
/** Payload: a token hash, or `*` for the whole cache. */
|
|
49
49
|
API_KEY_INVALIDATED: "apikey.invalidated",
|
|
50
|
+
/** Delivery outcomes for GET /v1/events/stream; see services/delivery/live-events. */
|
|
51
|
+
DELIVERY_EVENTS: "delivery.events",
|
|
52
|
+
/** An API began listening on DELIVERY_EVENTS: delivery workers resume publishing. */
|
|
53
|
+
DELIVERY_EVENTS_WANTED: "delivery.events.wanted",
|
|
50
54
|
} as const;
|
|
51
55
|
export type PubSubChannel = (typeof PUBSUB_CHANNELS)[keyof typeof PUBSUB_CHANNELS];
|
|
52
56
|
|
package/src/db/schema.ts
CHANGED
|
@@ -358,6 +358,9 @@ export const deliveryOutbox = pgTable(
|
|
|
358
358
|
},
|
|
359
359
|
(table) => ({
|
|
360
360
|
pk: primaryKey({ columns: [table.taskId, table.channel, table.destination] }),
|
|
361
|
+
// The key leads with task_id, so looking up what an address was sent
|
|
362
|
+
// (a user's details) read the whole table.
|
|
363
|
+
destinationIdx: index("delivery_outbox_destination_idx").on(table.destination),
|
|
361
364
|
}),
|
|
362
365
|
);
|
|
363
366
|
|
package/src/queue/index.ts
CHANGED
|
@@ -123,24 +123,15 @@ export class StreamProducer {
|
|
|
123
123
|
this.pendingQueue = [];
|
|
124
124
|
if (batch.length === 0) return;
|
|
125
125
|
|
|
126
|
+
// No MAXLEN trimming. Consumers delete entries as they ack them, so a
|
|
127
|
+
// stream only ever holds work that has not been done; trimming it threw
|
|
128
|
+
// away unsent notifications whenever the backlog passed maxLen. maxLen is
|
|
129
|
+
// the threshold monitorMaxLen warns at instead.
|
|
126
130
|
try {
|
|
127
131
|
if (typeof this.redis.pipeline === "function") {
|
|
128
132
|
const pipeline = this.redis.pipeline();
|
|
129
|
-
for (
|
|
130
|
-
|
|
131
|
-
if (i === batch.length - 1) {
|
|
132
|
-
pipeline.xadd(
|
|
133
|
-
this.stream,
|
|
134
|
-
"MAXLEN",
|
|
135
|
-
"~",
|
|
136
|
-
String(this.maxLen),
|
|
137
|
-
"*",
|
|
138
|
-
"data",
|
|
139
|
-
JSON.stringify(item.event),
|
|
140
|
-
);
|
|
141
|
-
} else {
|
|
142
|
-
pipeline.xadd(this.stream, "*", "data", JSON.stringify(item.event));
|
|
143
|
-
}
|
|
133
|
+
for (const item of batch) {
|
|
134
|
+
pipeline.xadd(this.stream, "*", "data", JSON.stringify(item.event));
|
|
144
135
|
}
|
|
145
136
|
|
|
146
137
|
const results = await pipeline.exec();
|
|
@@ -160,9 +151,6 @@ export class StreamProducer {
|
|
|
160
151
|
try {
|
|
161
152
|
const msgId = await this.redis.xadd(
|
|
162
153
|
this.stream,
|
|
163
|
-
"MAXLEN",
|
|
164
|
-
"~",
|
|
165
|
-
String(this.maxLen),
|
|
166
154
|
"*",
|
|
167
155
|
"data",
|
|
168
156
|
JSON.stringify(item.event),
|
|
@@ -195,7 +183,7 @@ export class StreamProducer {
|
|
|
195
183
|
if (len > this.maxLen * 0.8) {
|
|
196
184
|
this.logger?.warn(
|
|
197
185
|
{ stream, len, maxLen: this.maxLen },
|
|
198
|
-
"stream is
|
|
186
|
+
"stream backlog is past 80% of QUEUE_MAX_LEN — consumers are falling behind",
|
|
199
187
|
);
|
|
200
188
|
}
|
|
201
189
|
|
|
@@ -253,7 +241,7 @@ type XReadGroupResult = Array<[string, Array<[string, string[] | null]>]> | null
|
|
|
253
241
|
|
|
254
242
|
export class StreamConsumer {
|
|
255
243
|
readonly redis: Redis;
|
|
256
|
-
private
|
|
244
|
+
private blockingRedis: Redis;
|
|
257
245
|
private readonly streams: StreamName[];
|
|
258
246
|
private readonly group: ConsumerGroup;
|
|
259
247
|
private readonly consumer: string;
|
|
@@ -355,9 +343,17 @@ export class StreamConsumer {
|
|
|
355
343
|
} catch (err: any) {
|
|
356
344
|
if (err.message === "XREADGROUP_TIMEOUT_DEAD_CONNECTION") {
|
|
357
345
|
this.logger?.warn(
|
|
358
|
-
"XREADGROUP took too long, assuming dead connection.
|
|
346
|
+
"XREADGROUP took too long, assuming dead connection. Reconnecting...",
|
|
359
347
|
);
|
|
360
|
-
|
|
348
|
+
// Read on a fresh connection and close the old one for good. The
|
|
349
|
+
// abandoned read is still queued on it (on the wire, or in the
|
|
350
|
+
// offline queue if the connection never came up), and reconnecting
|
|
351
|
+
// the same client would run it later, claiming messages into this
|
|
352
|
+
// consumer's pending list with nobody left to receive them. A
|
|
353
|
+
// closed client rejects what it still holds instead.
|
|
354
|
+
const dead = this.blockingRedis;
|
|
355
|
+
this.blockingRedis = this.redis.duplicate({ enableAutoPipelining: false });
|
|
356
|
+
dead.disconnect();
|
|
361
357
|
throw err;
|
|
362
358
|
}
|
|
363
359
|
throw err;
|
|
@@ -452,7 +448,7 @@ export class StreamConsumer {
|
|
|
452
448
|
/**
|
|
453
449
|
* Acks and then deletes the entries. Every stream is read by exactly one
|
|
454
450
|
* consumer group, so an acked entry is never read again; leaving it in place
|
|
455
|
-
* holds Redis memory
|
|
451
|
+
* holds Redis memory for good, and a group recreated from "0"
|
|
456
452
|
* (ensureGroup) would process it a second time. Both commands are issued
|
|
457
453
|
* before either is awaited, so auto-pipelining sends them in one round trip,
|
|
458
454
|
* and Redis runs them in order.
|
package/src/redis/index.ts
CHANGED
|
@@ -13,6 +13,7 @@ declare module "ioredis" {
|
|
|
13
13
|
ttl?: number | string,
|
|
14
14
|
): Promise<number>;
|
|
15
15
|
releaseLock(lockKey: string, lockToken: string): Promise<number>;
|
|
16
|
+
countRequest(key: string, ttlSeconds: number | string): Promise<number>;
|
|
16
17
|
renewLock(lockKey: string, lockToken: string, ttlSeconds: number | string): Promise<number>;
|
|
17
18
|
throttleUser(
|
|
18
19
|
key: string,
|
|
@@ -155,6 +156,20 @@ export const LUA_SCHEDULER_POLL = `
|
|
|
155
156
|
return tasks
|
|
156
157
|
`;
|
|
157
158
|
|
|
159
|
+
/**
|
|
160
|
+
* Fixed-window request counter: INCR, and an expiry whenever the key has none.
|
|
161
|
+
* One script so the two cannot come apart. With INCR and EXPIRE sent
|
|
162
|
+
* separately, a failure between them left a counter with no expiry, and the
|
|
163
|
+
* address it counted stayed over its limit for good.
|
|
164
|
+
*/
|
|
165
|
+
export const LUA_COUNT_REQUEST = `
|
|
166
|
+
local count = redis.call('INCR', KEYS[1])
|
|
167
|
+
if redis.call('TTL', KEYS[1]) < 0 then
|
|
168
|
+
redis.call('EXPIRE', KEYS[1], ARGV[1])
|
|
169
|
+
end
|
|
170
|
+
return count
|
|
171
|
+
`;
|
|
172
|
+
|
|
158
173
|
/** Two-phase idempotency lease acquisition. Checks sent/legacy, then attempts SET NX lock. */
|
|
159
174
|
export const LUA_ACQUIRE_LEASE = `
|
|
160
175
|
if redis.call('EXISTS', KEYS[1]) == 1 or redis.call('EXISTS', KEYS[3]) == 1 then
|
|
@@ -214,6 +229,11 @@ export function registerCustomCommands(redis: Redis): void {
|
|
|
214
229
|
lua: LUA_ACQUIRE_LEASE,
|
|
215
230
|
});
|
|
216
231
|
|
|
232
|
+
redis.defineCommand("countRequest", {
|
|
233
|
+
numberOfKeys: 1,
|
|
234
|
+
lua: LUA_COUNT_REQUEST,
|
|
235
|
+
});
|
|
236
|
+
|
|
217
237
|
redis.defineCommand("markProcessed", {
|
|
218
238
|
numberOfKeys: 3,
|
|
219
239
|
lua: LUA_MARK_PROCESSED,
|
|
@@ -3,6 +3,7 @@ import { resolve, extname, join, relative, isAbsolute, dirname } from "node:path
|
|
|
3
3
|
import { existsSync, statSync, createReadStream } from "node:fs";
|
|
4
4
|
import { fileURLToPath } from "node:url";
|
|
5
5
|
import { request as httpRequest } from "node:http";
|
|
6
|
+
import { pipeline } from "node:stream";
|
|
6
7
|
|
|
7
8
|
const MIME_TYPES: Record<string, string> = {
|
|
8
9
|
".html": "text/html; charset=utf-8",
|
|
@@ -127,14 +128,20 @@ export async function handleAdminRequest(
|
|
|
127
128
|
return true;
|
|
128
129
|
}
|
|
129
130
|
|
|
130
|
-
//
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
const
|
|
131
|
+
// Proxy to the dashboard's dev server only when one is named. Defaulting to
|
|
132
|
+
// 127.0.0.1:5173 outside production meant a deployment that never set
|
|
133
|
+
// NODE_ENV sent every /admin request, credentials included, to whatever
|
|
134
|
+
// listened there, and served its reply as the dashboard.
|
|
135
|
+
const devProxyUrl =
|
|
136
|
+
process.env.VITE_DEV_URL || process.env.ADMIN_DEV_URL || process.env.NEXT_DEV_URL;
|
|
137
|
+
if (devProxyUrl && process.env.NODE_ENV !== "production") {
|
|
138
|
+
const target = new URL(devProxyUrl);
|
|
139
|
+
const proxied = await proxyToDevServer(
|
|
140
|
+
req,
|
|
141
|
+
res,
|
|
142
|
+
target.hostname,
|
|
143
|
+
parseInt(target.port, 10) || 80,
|
|
144
|
+
);
|
|
138
145
|
if (proxied) {
|
|
139
146
|
return true;
|
|
140
147
|
}
|
|
@@ -163,7 +170,7 @@ export async function handleAdminRequest(
|
|
|
163
170
|
<p>The dashboard static bundle has not been built yet.</p>
|
|
164
171
|
<p>Please build it by running:</p>
|
|
165
172
|
<p><code>npm run build:dashboard</code></p>
|
|
166
|
-
<p>or run the dev server with <code>npm --prefix dashboard run dev</code>.</p>
|
|
173
|
+
<p>or run the dev server with <code>npm --prefix dashboard run dev</code> and start notifkit with <code>VITE_DEV_URL=http://127.0.0.1:5173</code>.</p>
|
|
167
174
|
</div>
|
|
168
175
|
</body>
|
|
169
176
|
</html>
|
|
@@ -248,7 +255,10 @@ export async function handleAdminRequest(
|
|
|
248
255
|
"Cache-Control": cacheControl,
|
|
249
256
|
});
|
|
250
257
|
|
|
251
|
-
|
|
252
|
-
|
|
258
|
+
// pipeline, not pipe: a file that vanished or turned unreadable after the
|
|
259
|
+
// stat above fails the read, and pipe left that error unhandled (crashing
|
|
260
|
+
// the process) and the response open. pipeline destroys the response, so
|
|
261
|
+
// the client sees it cut short, and the error stops here.
|
|
262
|
+
pipeline(createReadStream(filePath), res, () => {});
|
|
253
263
|
return true;
|
|
254
264
|
}
|
|
@@ -41,6 +41,7 @@ import {
|
|
|
41
41
|
} from "@/contracts/index.js";
|
|
42
42
|
import { readJsonBody, sendJson, sendNoContent, sendValidationError } from "./http.js";
|
|
43
43
|
import type { RouteContext } from "./router.js";
|
|
44
|
+
import type { LiveDeliveryEvents } from "./live-events.js";
|
|
44
45
|
import { globalEmitter, getPriorityBucket, normaliseTarget } from "@/shared/index.js";
|
|
45
46
|
import { metrics } from "@/metrics/index.js";
|
|
46
47
|
import { eq, desc, and, lte, gte, sql, like, or, isNotNull, inArray } from "drizzle-orm";
|
|
@@ -75,6 +76,8 @@ export interface Deps {
|
|
|
75
76
|
segmentRepo: SegmentRepository;
|
|
76
77
|
adminUserRepo?: AdminUserRepository;
|
|
77
78
|
db: Db;
|
|
79
|
+
/** Delivery outcomes for GET /v1/events/stream. */
|
|
80
|
+
liveEvents: LiveDeliveryEvents;
|
|
78
81
|
}
|
|
79
82
|
|
|
80
83
|
// Inline user shape (email/phone/pushToken already normalised to string[] by Zod).
|
|
@@ -715,37 +718,35 @@ export function createHandlers(deps: Deps) {
|
|
|
715
718
|
res: ServerResponse,
|
|
716
719
|
ctx: RouteContext,
|
|
717
720
|
): Promise<void> {
|
|
721
|
+
let open = true;
|
|
722
|
+
let stop: (() => void) | undefined;
|
|
723
|
+
req.on("close", () => {
|
|
724
|
+
open = false;
|
|
725
|
+
stop?.();
|
|
726
|
+
});
|
|
727
|
+
|
|
728
|
+
const unlisten = await deps.liveEvents.listen((e) => {
|
|
729
|
+
// Only events that name this project. One naming none belongs to no
|
|
730
|
+
// tenant, so it goes to nobody rather than to everybody.
|
|
731
|
+
if (!ctx.projectId || e.projectId !== ctx.projectId) return;
|
|
732
|
+
res.write(`event: ${e.event}\n`);
|
|
733
|
+
const data =
|
|
734
|
+
e.event === "delivery:delivered"
|
|
735
|
+
? { taskId: e.taskId, providerMessageId: e.providerMessageId, channel: e.channel }
|
|
736
|
+
: { taskId: e.taskId, error: e.error, channel: e.channel };
|
|
737
|
+
res.write(`data: ${JSON.stringify(data)}\n\n`);
|
|
738
|
+
});
|
|
739
|
+
if (!open) return unlisten();
|
|
740
|
+
stop = unlisten;
|
|
741
|
+
|
|
742
|
+
// Only once subscribed: a client that has the headers is already listening,
|
|
743
|
+
// so whatever it triggers next cannot be missed.
|
|
718
744
|
res.writeHead(200, {
|
|
719
745
|
"Content-Type": "text/event-stream",
|
|
720
746
|
"Cache-Control": "no-cache",
|
|
721
747
|
Connection: "keep-alive",
|
|
722
748
|
});
|
|
723
749
|
res.write("retry: 10000\n\n");
|
|
724
|
-
|
|
725
|
-
const onDelivered = (
|
|
726
|
-
taskId: string,
|
|
727
|
-
providerMessageId: string,
|
|
728
|
-
channel: string,
|
|
729
|
-
eventProjectId: string,
|
|
730
|
-
) => {
|
|
731
|
-
if (eventProjectId && eventProjectId !== ctx.projectId) return;
|
|
732
|
-
res.write(`event: delivery:delivered\n`);
|
|
733
|
-
res.write(`data: ${JSON.stringify({ taskId, providerMessageId, channel })}\n\n`);
|
|
734
|
-
};
|
|
735
|
-
|
|
736
|
-
const onFailed = (taskId: string, error: string, channel: string, eventProjectId: string) => {
|
|
737
|
-
if (eventProjectId && eventProjectId !== ctx.projectId) return;
|
|
738
|
-
res.write(`event: delivery:failed\n`);
|
|
739
|
-
res.write(`data: ${JSON.stringify({ taskId, error, channel })}\n\n`);
|
|
740
|
-
};
|
|
741
|
-
|
|
742
|
-
globalEmitter.on("delivery:delivered", onDelivered);
|
|
743
|
-
globalEmitter.on("delivery:failed", onFailed);
|
|
744
|
-
|
|
745
|
-
req.on("close", () => {
|
|
746
|
-
globalEmitter.off("delivery:delivered", onDelivered);
|
|
747
|
-
globalEmitter.off("delivery:failed", onFailed);
|
|
748
|
-
});
|
|
749
750
|
}
|
|
750
751
|
|
|
751
752
|
// ── GET /v1/notifications/logs — getNotificationLogs ────────────────────
|
|
@@ -964,8 +965,9 @@ export function createHandlers(deps: Deps) {
|
|
|
964
965
|
});
|
|
965
966
|
sendJson(res, 201, { name: input.name });
|
|
966
967
|
} catch (error: any) {
|
|
968
|
+
// Logged, not returned: driver errors name tables, constraints and values.
|
|
967
969
|
deps.logger.error({ err: error }, "Failed to create workflow definition");
|
|
968
|
-
sendJson(res, 500, { error: "internal_error"
|
|
970
|
+
sendJson(res, 500, { error: "internal_error" });
|
|
969
971
|
}
|
|
970
972
|
}
|
|
971
973
|
|
|
@@ -1490,8 +1492,9 @@ export function createHandlers(deps: Deps) {
|
|
|
1490
1492
|
await deps.redis.native.xdel(STREAMS.DEAD_LETTER, messageId);
|
|
1491
1493
|
|
|
1492
1494
|
sendJson(res, 200, { success: true, replayedId: messageId, stream: targetStream });
|
|
1493
|
-
} catch (err
|
|
1494
|
-
|
|
1495
|
+
} catch (err) {
|
|
1496
|
+
logger.error({ err, messageId }, "DLQ replay failed");
|
|
1497
|
+
sendJson(res, 500, { error: "replay_failed" });
|
|
1495
1498
|
}
|
|
1496
1499
|
}
|
|
1497
1500
|
|
|
@@ -1511,8 +1514,9 @@ export function createHandlers(deps: Deps) {
|
|
|
1511
1514
|
}
|
|
1512
1515
|
await deps.redis.native.xdel(STREAMS.DEAD_LETTER, messageId);
|
|
1513
1516
|
sendJson(res, 200, { success: true });
|
|
1514
|
-
} catch (err
|
|
1515
|
-
|
|
1517
|
+
} catch (err) {
|
|
1518
|
+
logger.error({ err, messageId }, "DLQ delete failed");
|
|
1519
|
+
sendJson(res, 500, { error: "delete_failed" });
|
|
1516
1520
|
}
|
|
1517
1521
|
}
|
|
1518
1522
|
|
|
@@ -1563,21 +1567,18 @@ export function createHandlers(deps: Deps) {
|
|
|
1563
1567
|
const contactTargets = contacts.map((c) => c.target);
|
|
1564
1568
|
let logs: any[] = [];
|
|
1565
1569
|
if (contactTargets.length > 0) {
|
|
1566
|
-
|
|
1570
|
+
// One statement: fetching every task id the addresses were ever sent and
|
|
1571
|
+
// passing them back as parameters failed outright past 65,535 sends.
|
|
1572
|
+
const sentTo = deps.db
|
|
1567
1573
|
.select({ taskId: deliveryOutbox.taskId })
|
|
1568
1574
|
.from(deliveryOutbox)
|
|
1569
1575
|
.where(inArray(deliveryOutbox.destination, contactTargets));
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
and(eq(messageLogs.projectId, ctx.projectId!), inArray(messageLogs.taskId, taskIds)),
|
|
1577
|
-
)
|
|
1578
|
-
.orderBy(desc(messageLogs.timestamp))
|
|
1579
|
-
.limit(50);
|
|
1580
|
-
}
|
|
1576
|
+
logs = await deps.db
|
|
1577
|
+
.select()
|
|
1578
|
+
.from(messageLogs)
|
|
1579
|
+
.where(and(eq(messageLogs.projectId, ctx.projectId!), inArray(messageLogs.taskId, sentTo)))
|
|
1580
|
+
.orderBy(desc(messageLogs.timestamp))
|
|
1581
|
+
.limit(50);
|
|
1581
1582
|
}
|
|
1582
1583
|
|
|
1583
1584
|
sendJson(res, 200, { ...user, contacts, logs });
|
|
@@ -2073,7 +2074,14 @@ code{background:#f4f4f5;padding:.1rem .35rem;border-radius:4px}</style>
|
|
|
2073
2074
|
const { identifier, password } = parsed.data;
|
|
2074
2075
|
const user = await deps.adminUserRepo.findByEmailOrUsername(identifier);
|
|
2075
2076
|
const targetHash = user ? user.passwordHash : DUMMY_PASSWORD_HASH;
|
|
2076
|
-
|
|
2077
|
+
let isValid = await verifyPassword(password, targetHash);
|
|
2078
|
+
// The startup admin's ADMIN_PASSWORD is stored trimmed (a Windows `set`
|
|
2079
|
+
// leaves a trailing space in it), so one configured with spaces at either
|
|
2080
|
+
// end would otherwise never match what is typed. Only a failed attempt
|
|
2081
|
+
// with such spaces pays the second check.
|
|
2082
|
+
if (!isValid && password.trim() !== password && password.trim()) {
|
|
2083
|
+
isValid = await verifyPassword(password.trim(), targetHash);
|
|
2084
|
+
}
|
|
2077
2085
|
if (!user || !isValid) {
|
|
2078
2086
|
sendJson(res, 401, { error: "unauthorized", message: "Invalid credentials" });
|
|
2079
2087
|
return;
|
package/src/services/api/http.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
+
import { StringDecoder } from "node:string_decoder";
|
|
2
3
|
import type { ZodError } from "zod";
|
|
3
4
|
|
|
4
5
|
export async function readRawBody(req: IncomingMessage): Promise<string> {
|
|
@@ -23,26 +24,28 @@ export async function readRawBody(req: IncomingMessage): Promise<string> {
|
|
|
23
24
|
|
|
24
25
|
export async function readJsonBody(req: IncomingMessage): Promise<unknown> {
|
|
25
26
|
return new Promise<unknown>((resolve, reject) => {
|
|
26
|
-
//
|
|
27
|
-
//
|
|
28
|
-
//
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
27
|
+
// Decode chunk by chunk: avoids the Buffer.concat + toString() copy that
|
|
28
|
+
// readRawBody pays. readRawBody is kept for webhook callers that need raw
|
|
29
|
+
// bytes for HMAC verification. The limit counts bytes as they arrive;
|
|
30
|
+
// counting decoded characters let multibyte text through at up to three
|
|
31
|
+
// times the limit. StringDecoder keeps a character split across two
|
|
32
|
+
// chunks intact.
|
|
33
|
+
const decoder = new StringDecoder("utf8");
|
|
32
34
|
let body = "";
|
|
33
35
|
let totalLength = 0;
|
|
34
36
|
const MAX_PAYLOAD_SIZE = 5 * 1024 * 1024;
|
|
35
37
|
req.on("data", (chunk: string | Buffer) => {
|
|
36
|
-
const
|
|
37
|
-
totalLength +=
|
|
38
|
+
const bytes = typeof chunk === "string" ? Buffer.from(chunk) : chunk;
|
|
39
|
+
totalLength += bytes.length;
|
|
38
40
|
if (totalLength > MAX_PAYLOAD_SIZE) {
|
|
39
41
|
if (typeof req.destroy === "function") req.destroy();
|
|
40
42
|
reject(new HttpError(413, "payload_too_large"));
|
|
41
43
|
return;
|
|
42
44
|
}
|
|
43
|
-
body +=
|
|
45
|
+
body += decoder.write(bytes);
|
|
44
46
|
});
|
|
45
47
|
req.on("end", () => {
|
|
48
|
+
body += decoder.end();
|
|
46
49
|
const trimmed = body.trim();
|
|
47
50
|
if (!trimmed) {
|
|
48
51
|
resolve(undefined);
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { Redis } from "@/redis/index.js";
|
|
2
|
+
import type { Logger } from "@/logger/index.js";
|
|
3
|
+
import { PUBSUB_CHANNELS } from "@/contracts/index.js";
|
|
4
|
+
import type { LiveDeliveryEvent } from "@/services/delivery/live-events.js";
|
|
5
|
+
|
|
6
|
+
export type LiveDeliveryListener = (event: LiveDeliveryEvent) => void;
|
|
7
|
+
|
|
8
|
+
/** What getEventsStream needs: somewhere to listen for delivery outcomes. */
|
|
9
|
+
export interface LiveDeliveryEvents {
|
|
10
|
+
/** Starts calling `listener` for every outcome; the returned function stops it. */
|
|
11
|
+
listen(listener: LiveDeliveryListener): Promise<() => void>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Relays delivery outcomes published by delivery workers (in this process or
|
|
16
|
+
* any other) to the API's open event streams.
|
|
17
|
+
*
|
|
18
|
+
* Holds the Redis subscription only while at least one stream is open: the
|
|
19
|
+
* delivery side stops publishing when nobody is subscribed, so an idle API
|
|
20
|
+
* costs the send path nothing.
|
|
21
|
+
*/
|
|
22
|
+
export class RedisLiveDeliveryEvents implements LiveDeliveryEvents {
|
|
23
|
+
private subscriber: Redis | null = null;
|
|
24
|
+
private readonly listeners = new Set<LiveDeliveryListener>();
|
|
25
|
+
private subscribed: Promise<unknown> | null = null;
|
|
26
|
+
|
|
27
|
+
constructor(
|
|
28
|
+
private readonly redis: Redis,
|
|
29
|
+
private readonly logger?: Logger,
|
|
30
|
+
) {}
|
|
31
|
+
|
|
32
|
+
async listen(listener: LiveDeliveryListener): Promise<() => void> {
|
|
33
|
+
this.listeners.add(listener);
|
|
34
|
+
if (!this.subscribed) this.subscribed = this.subscribe();
|
|
35
|
+
await this.subscribed;
|
|
36
|
+
return () => {
|
|
37
|
+
if (!this.listeners.delete(listener) || this.listeners.size > 0) return;
|
|
38
|
+
this.subscribed = null;
|
|
39
|
+
this.subscriber?.unsubscribe(PUBSUB_CHANNELS.DELIVERY_EVENTS).catch(() => {});
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
private async subscribe(): Promise<void> {
|
|
44
|
+
if (!this.subscriber) {
|
|
45
|
+
this.subscriber = this.redis.duplicate({ enableAutoPipelining: false });
|
|
46
|
+
this.subscriber.on("message", (_channel: string, raw: string) => {
|
|
47
|
+
let event: LiveDeliveryEvent;
|
|
48
|
+
try {
|
|
49
|
+
event = JSON.parse(raw);
|
|
50
|
+
} catch {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
for (const listener of this.listeners) listener(event);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
try {
|
|
57
|
+
await this.subscriber.subscribe(PUBSUB_CHANNELS.DELIVERY_EVENTS);
|
|
58
|
+
// Delivery workers that found nobody listening have gone quiet; wake
|
|
59
|
+
// them now that someone is, so the next outcome is not missed.
|
|
60
|
+
await this.redis.publish(PUBSUB_CHANNELS.DELIVERY_EVENTS_WANTED, "1");
|
|
61
|
+
} catch (err) {
|
|
62
|
+
// The stream stays open and simply carries nothing until a retry.
|
|
63
|
+
this.subscribed = null;
|
|
64
|
+
this.logger?.warn({ err }, "could not subscribe to live delivery events");
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async close(): Promise<void> {
|
|
69
|
+
this.listeners.clear();
|
|
70
|
+
this.subscribed = null;
|
|
71
|
+
this.subscriber?.disconnect();
|
|
72
|
+
this.subscriber = null;
|
|
73
|
+
}
|
|
74
|
+
}
|