@objectstack/plugin-webhooks 5.0.0 → 5.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/.turbo/turbo-build.log +35 -13
  2. package/CHANGELOG.md +9 -28
  3. package/dist/chunk-JN76ZRWN.js +164 -0
  4. package/dist/chunk-JN76ZRWN.js.map +1 -0
  5. package/dist/chunk-M4M5FWIH.cjs +15 -0
  6. package/dist/chunk-M4M5FWIH.cjs.map +1 -0
  7. package/dist/chunk-NYSUNT6X.js +15 -0
  8. package/dist/chunk-NYSUNT6X.js.map +1 -0
  9. package/dist/chunk-OW7ESXOK.cjs +164 -0
  10. package/dist/chunk-OW7ESXOK.cjs.map +1 -0
  11. package/dist/index.cjs +747 -0
  12. package/dist/index.cjs.map +1 -0
  13. package/dist/index.d.cts +455 -0
  14. package/dist/index.d.ts +425 -74
  15. package/dist/index.js +712 -218
  16. package/dist/index.js.map +1 -1
  17. package/dist/outbox-bPQmKYPN.d.cts +128 -0
  18. package/dist/outbox-bPQmKYPN.d.ts +128 -0
  19. package/dist/schema.cjs +9 -0
  20. package/dist/schema.cjs.map +1 -0
  21. package/dist/schema.d.cts +4772 -0
  22. package/dist/schema.d.ts +4772 -0
  23. package/dist/schema.js +9 -0
  24. package/dist/schema.js.map +1 -0
  25. package/dist/sql-outbox.cjs +184 -0
  26. package/dist/sql-outbox.cjs.map +1 -0
  27. package/dist/sql-outbox.d.cts +54 -0
  28. package/dist/sql-outbox.d.ts +54 -0
  29. package/dist/sql-outbox.js +184 -0
  30. package/dist/sql-outbox.js.map +1 -0
  31. package/package.json +30 -10
  32. package/src/auto-enqueuer.test.ts +391 -0
  33. package/src/auto-enqueuer.ts +335 -0
  34. package/src/dispatcher.test.ts +324 -0
  35. package/src/dispatcher.ts +218 -0
  36. package/src/http-sender.ts +187 -0
  37. package/src/index.ts +48 -12
  38. package/src/memory-outbox.ts +127 -0
  39. package/src/outbox.ts +141 -0
  40. package/src/partition.ts +19 -0
  41. package/src/retention.test.ts +116 -0
  42. package/src/retention.ts +144 -0
  43. package/src/schema.ts +22 -0
  44. package/src/sql-outbox.test.ts +410 -0
  45. package/src/sql-outbox.ts +282 -0
  46. package/src/sys-webhook-delivery.object.ts +202 -0
  47. package/src/webhook-outbox-plugin.ts +280 -0
  48. package/tsconfig.json +5 -13
  49. package/tsup.config.ts +14 -0
  50. package/dist/index.d.mts +0 -104
  51. package/dist/index.mjs +0 -216
  52. package/dist/index.mjs.map +0 -1
  53. package/src/webhooks-plugin.test.ts +0 -218
  54. package/src/webhooks-plugin.ts +0 -294
package/dist/index.cjs ADDED
@@ -0,0 +1,747 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
2
+
3
+ var _chunkM4M5FWIHcjs = require('./chunk-M4M5FWIH.cjs');
4
+
5
+
6
+
7
+ var _chunkOW7ESXOKcjs = require('./chunk-OW7ESXOK.cjs');
8
+
9
+ // src/webhook-outbox-plugin.ts
10
+ var _integration = require('@objectstack/platform-objects/integration');
11
+
12
+ // src/auto-enqueuer.ts
13
+ var AutoEnqueuer = class {
14
+ constructor(engine, realtime, outbox, opts = {}) {
15
+ this.engine = engine;
16
+ this.realtime = realtime;
17
+ this.outbox = outbox;
18
+ this.subscriptions = /* @__PURE__ */ new Map();
19
+ this.running = false;
20
+ this.subscriptionsObject = _nullishCoalesce(opts.subscriptionsObject, () => ( "sys_webhook"));
21
+ this.refreshIntervalMs = _nullishCoalesce(opts.refreshIntervalMs, () => ( 6e4));
22
+ this.logger = _nullishCoalesce(opts.logger, () => ( {}));
23
+ }
24
+ /**
25
+ * Load the subscription cache and start listening for events.
26
+ */
27
+ async start() {
28
+ if (this.running) return;
29
+ this.running = true;
30
+ await this.refresh();
31
+ this.subId = await this.realtime.subscribe(
32
+ "webhook-auto-enqueuer",
33
+ (event) => this.handleEvent(event)
34
+ );
35
+ this.subIdSelfHeal = await this.realtime.subscribe(
36
+ "webhook-auto-enqueuer-self-heal",
37
+ (event) => this.handleSelfHealEvent(event),
38
+ { object: this.subscriptionsObject }
39
+ );
40
+ if (this.refreshIntervalMs > 0) {
41
+ this.refreshTimer = setInterval(() => {
42
+ this.refresh().catch(
43
+ (err) => _optionalChain([this, 'access', _ => _.logger, 'access', _2 => _2.warn, 'optionalCall', _3 => _3("[webhook-auto-enqueuer] periodic refresh failed", err)])
44
+ );
45
+ }, this.refreshIntervalMs);
46
+ _optionalChain([this, 'access', _4 => _4.refreshTimer, 'access', _5 => _5.unref, 'optionalCall', _6 => _6()]);
47
+ }
48
+ }
49
+ async stop() {
50
+ if (!this.running) return;
51
+ this.running = false;
52
+ if (this.subId) await this.realtime.unsubscribe(this.subId);
53
+ if (this.subIdSelfHeal) await this.realtime.unsubscribe(this.subIdSelfHeal);
54
+ if (this.refreshTimer) clearInterval(this.refreshTimer);
55
+ this.subId = void 0;
56
+ this.subIdSelfHeal = void 0;
57
+ this.refreshTimer = void 0;
58
+ }
59
+ /**
60
+ * Force-refresh the subscription cache from storage. Concurrent
61
+ * callers share a single in-flight refresh.
62
+ */
63
+ async refresh() {
64
+ if (this.refreshing) return this.refreshing;
65
+ this.refreshing = this.doRefresh().finally(() => {
66
+ this.refreshing = void 0;
67
+ });
68
+ return this.refreshing;
69
+ }
70
+ async doRefresh() {
71
+ let rows;
72
+ try {
73
+ rows = await this.engine.find(this.subscriptionsObject, {
74
+ where: { active: true }
75
+ });
76
+ } catch (err) {
77
+ _optionalChain([this, 'access', _7 => _7.logger, 'access', _8 => _8.warn, 'optionalCall', _9 => _9(
78
+ `[webhook-auto-enqueuer] failed to load ${this.subscriptionsObject}`,
79
+ err
80
+ )]);
81
+ return;
82
+ }
83
+ const next = /* @__PURE__ */ new Map();
84
+ for (const row of rows) {
85
+ const sub = this.parseRow(row);
86
+ if (!sub) continue;
87
+ const key = _nullishCoalesce(sub.objectName, () => ( "*"));
88
+ const arr = _nullishCoalesce(next.get(key), () => ( []));
89
+ arr.push(sub);
90
+ next.set(key, arr);
91
+ }
92
+ this.subscriptions.clear();
93
+ for (const [k, v] of next) this.subscriptions.set(k, v);
94
+ _optionalChain([this, 'access', _10 => _10.logger, 'access', _11 => _11.debug, 'optionalCall', _12 => _12("[webhook-auto-enqueuer] cache refreshed", {
95
+ objects: this.subscriptions.size,
96
+ rows: rows.length
97
+ })]);
98
+ }
99
+ parseRow(row) {
100
+ if (!_optionalChain([row, 'optionalAccess', _13 => _13.id]) || !_optionalChain([row, 'optionalAccess', _14 => _14.url])) return null;
101
+ const triggersField = _nullishCoalesce(row.triggers, () => ( ""));
102
+ const triggers = new Set(
103
+ triggersField.split(",").map((s) => s.trim().toLowerCase()).filter(Boolean)
104
+ );
105
+ if (triggers.size === 0) {
106
+ return null;
107
+ }
108
+ let defn = {};
109
+ if (typeof row.definition_json === "string" && row.definition_json.length > 0) {
110
+ try {
111
+ defn = _nullishCoalesce(JSON.parse(row.definition_json), () => ( {}));
112
+ } catch (e2) {
113
+ defn = {};
114
+ }
115
+ }
116
+ return {
117
+ id: row.id,
118
+ name: _nullishCoalesce(row.name, () => ( row.id)),
119
+ objectName: row.object_name ? String(row.object_name) : void 0,
120
+ triggers,
121
+ url: String(row.url),
122
+ method: _nullishCoalesce(_nullishCoalesce(row.method, () => ( defn.method)), () => ( "POST")),
123
+ headers: defn.headers,
124
+ secret: defn.secret,
125
+ timeoutMs: defn.timeoutMs
126
+ };
127
+ }
128
+ /**
129
+ * Handler for the firehose subscription.
130
+ *
131
+ * NOTE: we intentionally `void` the inner enqueue() so the realtime
132
+ * publisher (and therefore the user's request) is never blocked on
133
+ * webhook persistence.
134
+ */
135
+ handleEvent(event) {
136
+ if (!_optionalChain([event, 'access', _15 => _15.type, 'optionalAccess', _16 => _16.startsWith, 'call', _17 => _17("data.record.")])) return;
137
+ if (!event.object) return;
138
+ if (event.object === this.subscriptionsObject) return;
139
+ const action = event.type.slice("data.record.".length);
140
+ const trigger = mapActionToTrigger(action);
141
+ if (!trigger) return;
142
+ const subs = [
143
+ ..._nullishCoalesce(this.subscriptions.get(event.object), () => ( [])),
144
+ ..._nullishCoalesce(this.subscriptions.get("*"), () => ( []))
145
+ ];
146
+ if (subs.length === 0) return;
147
+ const payload = _nullishCoalesce(event.payload, () => ( {}));
148
+ const recordId = _nullishCoalesce(_nullishCoalesce(_nullishCoalesce(_nullishCoalesce(payload.recordId, () => ( payload.id)), () => ( _optionalChain([payload, 'access', _18 => _18.after, 'optionalAccess', _19 => _19.id]))), () => ( _optionalChain([payload, 'access', _20 => _20.before, 'optionalAccess', _21 => _21.id]))), () => ( "unknown"));
149
+ const eventId = `${event.object}:${recordId}:${action}:${event.timestamp}`;
150
+ for (const sub of subs) {
151
+ if (!sub.triggers.has(trigger)) continue;
152
+ void this.outbox.enqueue({
153
+ webhookId: sub.id,
154
+ eventId,
155
+ eventType: event.type,
156
+ url: sub.url,
157
+ method: sub.method,
158
+ headers: sub.headers,
159
+ secret: sub.secret,
160
+ timeoutMs: sub.timeoutMs,
161
+ payload: {
162
+ object: event.object,
163
+ recordId,
164
+ action,
165
+ timestamp: event.timestamp,
166
+ ...payload
167
+ }
168
+ }).catch(
169
+ (err) => _optionalChain([this, 'access', _22 => _22.logger, 'access', _23 => _23.warn, 'optionalCall', _24 => _24("[webhook-auto-enqueuer] enqueue failed", {
170
+ webhook: sub.name,
171
+ eventId,
172
+ err: _nullishCoalesce(_optionalChain([err, 'optionalAccess', _25 => _25.message]), () => ( err))
173
+ })])
174
+ );
175
+ }
176
+ }
177
+ handleSelfHealEvent(event) {
178
+ if (event.object !== this.subscriptionsObject) return;
179
+ if (!_optionalChain([event, 'access', _26 => _26.type, 'optionalAccess', _27 => _27.startsWith, 'call', _28 => _28("data.record.")])) return;
180
+ this.refresh().catch(
181
+ (err) => _optionalChain([this, 'access', _29 => _29.logger, 'access', _30 => _30.warn, 'optionalCall', _31 => _31("[webhook-auto-enqueuer] self-heal refresh failed", err)])
182
+ );
183
+ }
184
+ /** Test / admin accessor. */
185
+ snapshot() {
186
+ return this.subscriptions;
187
+ }
188
+ };
189
+ function mapActionToTrigger(action) {
190
+ switch (action) {
191
+ case "created":
192
+ return "create";
193
+ case "updated":
194
+ return "update";
195
+ case "deleted":
196
+ return "delete";
197
+ case "undeleted":
198
+ return "undelete";
199
+ default:
200
+ return null;
201
+ }
202
+ }
203
+
204
+ // src/http-sender.ts
205
+ var _crypto = require('crypto');
206
+ var DEFAULT_TIMEOUT_MS = 15e3;
207
+ var RESPONSE_BODY_CAP = 16 * 1024;
208
+ async function sendOnce(delivery, fetchImpl) {
209
+ const body = typeof delivery.payload === "string" ? delivery.payload : JSON.stringify(delivery.payload);
210
+ const headers = {
211
+ "Content-Type": "application/json",
212
+ "User-Agent": "ObjectStack-Webhooks/1.0",
213
+ "X-Objectstack-Event": delivery.eventType,
214
+ "X-Objectstack-Delivery": delivery.id,
215
+ "X-Objectstack-Attempt": String(delivery.attempts + 1),
216
+ ..._nullishCoalesce(delivery.headers, () => ( {}))
217
+ };
218
+ if (delivery.secret) {
219
+ const sig = _crypto.createHmac.call(void 0, "sha256", delivery.secret).update(body).digest("hex");
220
+ headers["X-Objectstack-Signature"] = `sha256=${sig}`;
221
+ }
222
+ const timeoutMs = _nullishCoalesce(delivery.timeoutMs, () => ( DEFAULT_TIMEOUT_MS));
223
+ const controller = new AbortController();
224
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
225
+ const start = Date.now();
226
+ try {
227
+ const res = await fetchImpl(delivery.url, {
228
+ method: _nullishCoalesce(delivery.method, () => ( "POST")),
229
+ headers,
230
+ body,
231
+ signal: controller.signal
232
+ });
233
+ clearTimeout(timer);
234
+ const responseText = await safeReadBody(res);
235
+ const durationMs = Date.now() - start;
236
+ if (res.ok) {
237
+ return { success: true, httpStatus: res.status, responseBody: responseText, durationMs };
238
+ }
239
+ const retriable = res.status === 408 || res.status === 429 || res.status >= 500;
240
+ return {
241
+ success: false,
242
+ retriable,
243
+ httpStatus: res.status,
244
+ responseBody: responseText,
245
+ error: `HTTP ${res.status}`,
246
+ durationMs
247
+ };
248
+ } catch (err) {
249
+ clearTimeout(timer);
250
+ const durationMs = Date.now() - start;
251
+ const e = err;
252
+ const error = _optionalChain([e, 'optionalAccess', _32 => _32.name]) === "AbortError" ? `timeout after ${timeoutMs}ms` : _nullishCoalesce(_optionalChain([e, 'optionalAccess', _33 => _33.message]), () => ( String(err)));
253
+ return { success: false, retriable: true, error, durationMs };
254
+ }
255
+ }
256
+ async function safeReadBody(res) {
257
+ try {
258
+ const text = await res.text();
259
+ return text.length > RESPONSE_BODY_CAP ? text.slice(0, RESPONSE_BODY_CAP) : text;
260
+ } catch (e3) {
261
+ return void 0;
262
+ }
263
+ }
264
+ function nextRetryDelayMs(attemptsSoFar, rng = Math.random) {
265
+ const SCHEDULE = [1e3, 1e4, 6e4, 6e5, 36e5, 216e5, 864e5];
266
+ if (attemptsSoFar < 1 || attemptsSoFar > SCHEDULE.length) return null;
267
+ const base = SCHEDULE[attemptsSoFar - 1];
268
+ const jitter = 0.8 + rng() * 0.4;
269
+ return Math.floor(base * jitter);
270
+ }
271
+ function classifyAttempt(outcome, attemptsSoFar, now = Date.now(), rng) {
272
+ if (outcome.success) return outcome;
273
+ if (!outcome.retriable) {
274
+ return {
275
+ success: false,
276
+ httpStatus: outcome.httpStatus,
277
+ responseBody: outcome.responseBody,
278
+ error: outcome.error,
279
+ durationMs: outcome.durationMs,
280
+ dead: true
281
+ };
282
+ }
283
+ const delay = nextRetryDelayMs(attemptsSoFar + 1, rng);
284
+ if (delay === null) {
285
+ return {
286
+ success: false,
287
+ httpStatus: outcome.httpStatus,
288
+ responseBody: outcome.responseBody,
289
+ error: outcome.error,
290
+ durationMs: outcome.durationMs,
291
+ dead: true
292
+ };
293
+ }
294
+ return {
295
+ success: false,
296
+ httpStatus: outcome.httpStatus,
297
+ responseBody: outcome.responseBody,
298
+ error: outcome.error,
299
+ durationMs: outcome.durationMs,
300
+ nextRetryAt: now + delay
301
+ };
302
+ }
303
+
304
+ // src/dispatcher.ts
305
+ var WebhookDispatcher = class {
306
+ constructor(options) {
307
+ this.running = false;
308
+ const intervalMs = _nullishCoalesce(options.intervalMs, () => ( 250));
309
+ const lockTtlMs = _nullishCoalesce(options.lockTtlMs, () => ( intervalMs * 5));
310
+ this.opts = {
311
+ nodeId: options.nodeId,
312
+ cluster: options.cluster,
313
+ outbox: options.outbox,
314
+ partitionCount: _nullishCoalesce(options.partitionCount, () => ( 8)),
315
+ batchSize: _nullishCoalesce(options.batchSize, () => ( 32)),
316
+ intervalMs,
317
+ lockTtlMs,
318
+ claimTtlMs: _nullishCoalesce(options.claimTtlMs, () => ( lockTtlMs * 2)),
319
+ onAttempt: options.onAttempt,
320
+ fetchImpl: options.fetchImpl,
321
+ rng: options.rng,
322
+ logger: options.logger
323
+ };
324
+ }
325
+ /** Begin the periodic loop. Safe to call once; subsequent calls are no-ops. */
326
+ start() {
327
+ if (this.running) return;
328
+ this.running = true;
329
+ this.scheduleTick();
330
+ this.timer = setInterval(() => this.scheduleTick(), this.opts.intervalMs);
331
+ }
332
+ /** Stop the loop and wait for the in-flight tick to drain. */
333
+ async stop() {
334
+ if (!this.running) return;
335
+ this.running = false;
336
+ if (this.timer) {
337
+ clearInterval(this.timer);
338
+ this.timer = void 0;
339
+ }
340
+ if (this.inflightTick) {
341
+ try {
342
+ await this.inflightTick;
343
+ } catch (e4) {
344
+ }
345
+ }
346
+ }
347
+ /**
348
+ * Run one full tick (all partitions, single attempt each). Exposed for
349
+ * deterministic tests that want to step the dispatcher manually.
350
+ */
351
+ async tick() {
352
+ await this.runTick();
353
+ }
354
+ scheduleTick() {
355
+ if (this.inflightTick) return;
356
+ this.inflightTick = this.runTick().catch((err) => {
357
+ _optionalChain([this, 'access', _34 => _34.opts, 'access', _35 => _35.logger, 'optionalAccess', _36 => _36.warn, 'optionalCall', _37 => _37("webhook-dispatcher: tick failed", {
358
+ nodeId: this.opts.nodeId,
359
+ error: _nullishCoalesce(_optionalChain([err, 'optionalAccess', _38 => _38.message]), () => ( String(err)))
360
+ })]);
361
+ }).finally(() => {
362
+ this.inflightTick = void 0;
363
+ });
364
+ }
365
+ async runTick() {
366
+ const partitionCount = this.opts.partitionCount;
367
+ const offset = stableNodeOffset(this.opts.nodeId, partitionCount);
368
+ for (let step = 0; step < partitionCount; step++) {
369
+ const i = (offset + step) % partitionCount;
370
+ await this.runPartition(i);
371
+ }
372
+ }
373
+ async runPartition(index) {
374
+ const key = `webhook.dispatcher.partition.${index}`;
375
+ const handle = await this.opts.cluster.lock.acquire(key, {
376
+ ttlMs: this.opts.lockTtlMs,
377
+ // waitMs=0 → fail-fast; we'll try this partition again next tick.
378
+ waitMs: 0
379
+ });
380
+ if (!handle) return;
381
+ try {
382
+ const claimed = await this.opts.outbox.claim({
383
+ nodeId: this.opts.nodeId,
384
+ limit: this.opts.batchSize,
385
+ partition: { index, count: this.opts.partitionCount },
386
+ claimTtlMs: this.opts.claimTtlMs
387
+ });
388
+ if (claimed.length === 0) return;
389
+ await handle.renew(this.opts.lockTtlMs);
390
+ for (const row of claimed) {
391
+ if (!handle.isHeld()) break;
392
+ await this.processRow(row);
393
+ }
394
+ } finally {
395
+ await handle.release();
396
+ }
397
+ }
398
+ async processRow(row) {
399
+ const fetchImpl = _nullishCoalesce(this.opts.fetchImpl, () => ( globalThis.fetch));
400
+ if (!fetchImpl) {
401
+ _optionalChain([this, 'access', _39 => _39.opts, 'access', _40 => _40.logger, 'optionalAccess', _41 => _41.warn, 'optionalCall', _42 => _42("webhook-dispatcher: no fetch impl available", {
402
+ rowId: row.id
403
+ })]);
404
+ await this.opts.outbox.ack(row.id, {
405
+ success: false,
406
+ error: "no fetch implementation",
407
+ durationMs: 0,
408
+ dead: true
409
+ });
410
+ return;
411
+ }
412
+ const outcome = await sendOnce(row, fetchImpl);
413
+ const result = classifyAttempt(outcome, row.attempts, Date.now(), this.opts.rng);
414
+ await this.opts.outbox.ack(row.id, result);
415
+ _optionalChain([this, 'access', _43 => _43.opts, 'access', _44 => _44.onAttempt, 'optionalCall', _45 => _45(row, result.success)]);
416
+ }
417
+ };
418
+ function stableNodeOffset(nodeId, partitionCount) {
419
+ let h = 0;
420
+ for (let i = 0; i < nodeId.length; i++) {
421
+ h = h * 31 + nodeId.charCodeAt(i) | 0;
422
+ }
423
+ return Math.abs(h) % partitionCount;
424
+ }
425
+
426
+ // src/memory-outbox.ts
427
+
428
+ var MemoryWebhookOutbox = class {
429
+ constructor() {
430
+ this.rows = /* @__PURE__ */ new Map();
431
+ /** Dedup index keyed by `${eventId}::${webhookId}` -> row id. */
432
+ this.dedup = /* @__PURE__ */ new Map();
433
+ }
434
+ async enqueue(input) {
435
+ const dedupKey = `${input.eventId}::${input.webhookId}`;
436
+ const existing = this.dedup.get(dedupKey);
437
+ if (existing) return existing;
438
+ const id = _crypto.randomUUID.call(void 0, );
439
+ const now = Date.now();
440
+ const row = {
441
+ id,
442
+ webhookId: input.webhookId,
443
+ eventId: input.eventId,
444
+ eventType: input.eventType,
445
+ url: input.url,
446
+ method: _nullishCoalesce(input.method, () => ( "POST")),
447
+ headers: input.headers,
448
+ secret: input.secret,
449
+ timeoutMs: input.timeoutMs,
450
+ payload: input.payload,
451
+ status: "pending",
452
+ attempts: 0,
453
+ createdAt: now,
454
+ updatedAt: now
455
+ };
456
+ this.rows.set(id, row);
457
+ this.dedup.set(dedupKey, id);
458
+ return id;
459
+ }
460
+ async claim(opts) {
461
+ const now = _nullishCoalesce(opts.now, () => ( Date.now()));
462
+ const claimed = [];
463
+ for (const row of this.rows.values()) {
464
+ if (row.status === "in_flight" && row.claimedAt !== void 0 && now - row.claimedAt > opts.claimTtlMs) {
465
+ row.status = "pending";
466
+ row.claimedBy = void 0;
467
+ row.claimedAt = void 0;
468
+ row.updatedAt = now;
469
+ }
470
+ }
471
+ for (const row of this.rows.values()) {
472
+ if (claimed.length >= opts.limit) break;
473
+ if (row.status !== "pending") continue;
474
+ if (row.nextRetryAt !== void 0 && row.nextRetryAt > now) continue;
475
+ if (opts.partition) {
476
+ const p = _chunkM4M5FWIHcjs.hashPartition.call(void 0, row.webhookId, opts.partition.count);
477
+ if (p !== opts.partition.index) continue;
478
+ }
479
+ row.status = "in_flight";
480
+ row.claimedBy = opts.nodeId;
481
+ row.claimedAt = now;
482
+ row.updatedAt = now;
483
+ claimed.push({ ...row });
484
+ }
485
+ return claimed;
486
+ }
487
+ async ack(id, result) {
488
+ const row = this.rows.get(id);
489
+ if (!row) return;
490
+ const now = Date.now();
491
+ row.attempts += 1;
492
+ row.lastAttemptedAt = now;
493
+ row.updatedAt = now;
494
+ row.claimedBy = void 0;
495
+ row.claimedAt = void 0;
496
+ row.responseCode = result.httpStatus;
497
+ row.responseBody = result.responseBody;
498
+ let status;
499
+ if (result.success) {
500
+ status = "success";
501
+ row.nextRetryAt = void 0;
502
+ row.error = void 0;
503
+ } else if (result.dead) {
504
+ status = "dead";
505
+ row.error = result.error;
506
+ row.nextRetryAt = void 0;
507
+ } else {
508
+ status = "pending";
509
+ row.error = result.error;
510
+ row.nextRetryAt = result.nextRetryAt;
511
+ }
512
+ row.status = status;
513
+ }
514
+ async list(filter) {
515
+ const all = Array.from(this.rows.values()).map((r) => ({ ...r }));
516
+ return _optionalChain([filter, 'optionalAccess', _46 => _46.status]) ? all.filter((r) => r.status === filter.status) : all;
517
+ }
518
+ };
519
+
520
+ // src/retention.ts
521
+ var DEFAULTS = {
522
+ successTtlMs: 7 * 24 * 60 * 60 * 1e3,
523
+ deadTtlMs: 30 * 24 * 60 * 60 * 1e3,
524
+ sweepIntervalMs: 60 * 60 * 1e3
525
+ };
526
+ var DeliveryRetentionSweeper = class {
527
+ constructor(engine, opts = {}) {
528
+ this.engine = engine;
529
+ this.running = false;
530
+ this.objectName = _nullishCoalesce(opts.objectName, () => ( _chunkOW7ESXOKcjs.SYS_WEBHOOK_DELIVERY));
531
+ this.successTtlMs = _nullishCoalesce(opts.successTtlMs, () => ( DEFAULTS.successTtlMs));
532
+ this.deadTtlMs = _nullishCoalesce(opts.deadTtlMs, () => ( DEFAULTS.deadTtlMs));
533
+ this.sweepIntervalMs = _nullishCoalesce(opts.sweepIntervalMs, () => ( DEFAULTS.sweepIntervalMs));
534
+ this.logger = _nullishCoalesce(opts.logger, () => ( {}));
535
+ }
536
+ start() {
537
+ if (this.running) return;
538
+ this.running = true;
539
+ this.timer = setInterval(() => {
540
+ this.sweep().catch(
541
+ (err) => _optionalChain([this, 'access', _47 => _47.logger, 'access', _48 => _48.warn, 'optionalCall', _49 => _49("[webhook-retention] sweep failed", err)])
542
+ );
543
+ }, this.sweepIntervalMs);
544
+ _optionalChain([this, 'access', _50 => _50.timer, 'access', _51 => _51.unref, 'optionalCall', _52 => _52()]);
545
+ }
546
+ stop() {
547
+ if (!this.running) return;
548
+ this.running = false;
549
+ if (this.timer) clearInterval(this.timer);
550
+ this.timer = void 0;
551
+ }
552
+ /** Run one sweep immediately. Returns the number of rows deleted. */
553
+ async sweep(now = Date.now()) {
554
+ let successDeleted = 0;
555
+ let deadDeleted = 0;
556
+ if (this.successTtlMs > 0) {
557
+ try {
558
+ const res = await this.engine.delete(this.objectName, {
559
+ where: {
560
+ status: "success",
561
+ updated_at: { $lt: now - this.successTtlMs }
562
+ }
563
+ });
564
+ successDeleted = _nullishCoalesce(_optionalChain([res, 'optionalAccess', _53 => _53.affected]), () => ( 0));
565
+ } catch (err) {
566
+ _optionalChain([this, 'access', _54 => _54.logger, 'access', _55 => _55.warn, 'optionalCall', _56 => _56("[webhook-retention] success sweep failed", err)]);
567
+ }
568
+ }
569
+ if (this.deadTtlMs > 0) {
570
+ try {
571
+ const res = await this.engine.delete(this.objectName, {
572
+ where: {
573
+ status: "dead",
574
+ updated_at: { $lt: now - this.deadTtlMs }
575
+ }
576
+ });
577
+ deadDeleted = _nullishCoalesce(_optionalChain([res, 'optionalAccess', _57 => _57.affected]), () => ( 0));
578
+ } catch (err) {
579
+ _optionalChain([this, 'access', _58 => _58.logger, 'access', _59 => _59.warn, 'optionalCall', _60 => _60("[webhook-retention] dead sweep failed", err)]);
580
+ }
581
+ }
582
+ if (successDeleted + deadDeleted > 0) {
583
+ _optionalChain([this, 'access', _61 => _61.logger, 'access', _62 => _62.info, 'optionalCall', _63 => _63("[webhook-retention] sweep complete", {
584
+ success: successDeleted,
585
+ dead: deadDeleted
586
+ })]);
587
+ }
588
+ return { success: successDeleted, dead: deadDeleted };
589
+ }
590
+ };
591
+
592
+ // src/webhook-outbox-plugin.ts
593
+ var WebhookOutboxPlugin = class {
594
+ constructor(options = {}) {
595
+ this.options = options;
596
+ this.name = "com.objectstack.plugin-webhook-outbox";
597
+ this.version = "1.1.0";
598
+ this.type = "standard";
599
+ this.dependencies = ["com.objectstack.service.cluster"];
600
+ }
601
+ async init(ctx) {
602
+ const cluster = ctx.getService("cluster");
603
+ if (!cluster) {
604
+ throw new Error(
605
+ 'WebhookOutboxPlugin: required service "cluster" not found \u2014 register ClusterServicePlugin first'
606
+ );
607
+ }
608
+ const manifest = ctx.getService("manifest");
609
+ if (manifest && typeof manifest.register === "function") {
610
+ manifest.register({
611
+ id: "com.objectstack.plugin-webhook-outbox.schema",
612
+ namespace: "sys",
613
+ version: this.version,
614
+ type: "plugin",
615
+ scope: "system",
616
+ name: "Webhook Outbox Schemas",
617
+ description: "Registers sys_webhook (configuration) and sys_webhook_delivery (durable outbox telemetry).",
618
+ objects: [_integration.SysWebhook, _chunkOW7ESXOKcjs.SysWebhookDelivery]
619
+ });
620
+ } else {
621
+ _optionalChain([ctx, 'access', _64 => _64.logger, 'access', _65 => _65.warn, 'optionalCall', _66 => _66(
622
+ "[webhook-outbox] manifest service unavailable \u2014 sys_webhook / sys_webhook_delivery will NOT appear in REST or Studio nav. Register MetadataService before WebhookOutboxPlugin."
623
+ )]);
624
+ }
625
+ const outbox = this.resolveOutbox(ctx);
626
+ this.outboxInstance = outbox;
627
+ const nodeId = _nullishCoalesce(_nullishCoalesce(this.options.nodeId, () => ( process.env.OBJECTSTACK_NODE_ID)), () => ( `node-${Math.random().toString(36).slice(2, 10)}`));
628
+ const dispatcher = new WebhookDispatcher({
629
+ nodeId,
630
+ cluster,
631
+ outbox,
632
+ partitionCount: this.options.partitionCount,
633
+ batchSize: this.options.batchSize,
634
+ intervalMs: this.options.intervalMs,
635
+ lockTtlMs: this.options.lockTtlMs,
636
+ claimTtlMs: this.options.claimTtlMs,
637
+ fetchImpl: this.options.fetchImpl,
638
+ onAttempt: this.options.onAttempt,
639
+ rng: this.options.rng,
640
+ logger: ctx.logger
641
+ });
642
+ this.dispatcher = dispatcher;
643
+ ctx.registerService("webhook.outbox", outbox);
644
+ ctx.registerService("webhook.dispatcher", dispatcher);
645
+ if (this.options.autoStart !== false) {
646
+ dispatcher.start();
647
+ }
648
+ const usingMemoryOutbox = outbox instanceof MemoryWebhookOutbox;
649
+ if (usingMemoryOutbox && process.env.NODE_ENV === "production") {
650
+ _optionalChain([ctx, 'access', _67 => _67.logger, 'access', _68 => _68.warn, 'optionalCall', _69 => _69(
651
+ '[webhook-outbox] MemoryWebhookOutbox in production \u2014 webhook deliveries WILL be lost on process exit. Pass `outbox: (ctx) => new SqlWebhookOutbox(ctx.getService("objectql"), { partitionCount: 8 })` from `@objectstack/plugin-webhooks/sql`.'
652
+ )]);
653
+ }
654
+ const autoEnqueueOpt = _nullishCoalesce(this.options.autoEnqueue, () => ( true));
655
+ const retentionOpt = _nullishCoalesce(this.options.retention, () => ( true));
656
+ const needsReadyHook = autoEnqueueOpt !== false || retentionOpt !== false;
657
+ if (needsReadyHook && typeof ctx.hook === "function") {
658
+ ctx.hook("kernel:ready", async () => {
659
+ await this.bootAutoEnqueue(ctx, autoEnqueueOpt);
660
+ this.bootRetention(ctx, retentionOpt);
661
+ });
662
+ }
663
+ _optionalChain([ctx, 'access', _70 => _70.logger, 'access', _71 => _71.info, 'optionalCall', _72 => _72("[webhook-outbox] initialised", {
664
+ nodeId,
665
+ partitions: _nullishCoalesce(this.options.partitionCount, () => ( 8)),
666
+ interval: _nullishCoalesce(this.options.intervalMs, () => ( 250)),
667
+ autoEnqueue: autoEnqueueOpt !== false,
668
+ retention: retentionOpt !== false
669
+ })]);
670
+ }
671
+ async dispose() {
672
+ await _optionalChain([this, 'access', _73 => _73.autoEnqueuer, 'optionalAccess', _74 => _74.stop, 'call', _75 => _75()]);
673
+ _optionalChain([this, 'access', _76 => _76.retention, 'optionalAccess', _77 => _77.stop, 'call', _78 => _78()]);
674
+ await _optionalChain([this, 'access', _79 => _79.dispatcher, 'optionalAccess', _80 => _80.stop, 'call', _81 => _81()]);
675
+ }
676
+ resolveOutbox(ctx) {
677
+ const opt = this.options.outbox;
678
+ if (!opt) return new MemoryWebhookOutbox();
679
+ if (typeof opt === "function") return opt(ctx);
680
+ return opt;
681
+ }
682
+ async bootAutoEnqueue(ctx, opt) {
683
+ if (opt === false) return;
684
+ const engine = this.tryGetService(ctx, ["objectql", "data"]);
685
+ const realtime = this.tryGetService(ctx, ["realtime"]);
686
+ if (!engine || !realtime) {
687
+ _optionalChain([ctx, 'access', _82 => _82.logger, 'access', _83 => _83.warn, 'optionalCall', _84 => _84(
688
+ "[webhook-auto-enqueuer] disabled \u2014 ObjectQL or Realtime service not available",
689
+ { hasEngine: !!engine, hasRealtime: !!realtime }
690
+ )]);
691
+ return;
692
+ }
693
+ if (!this.outboxInstance) return;
694
+ const enqOpts = typeof opt === "object" ? opt : {};
695
+ this.autoEnqueuer = new AutoEnqueuer(
696
+ engine,
697
+ realtime,
698
+ this.outboxInstance,
699
+ { ...enqOpts, logger: ctx.logger }
700
+ );
701
+ await this.autoEnqueuer.start();
702
+ ctx.registerService("webhook.autoEnqueuer", this.autoEnqueuer);
703
+ _optionalChain([ctx, 'access', _85 => _85.logger, 'access', _86 => _86.info, 'optionalCall', _87 => _87("[webhook-auto-enqueuer] started")]);
704
+ }
705
+ bootRetention(ctx, opt) {
706
+ if (opt === false) return;
707
+ if (this.outboxInstance instanceof MemoryWebhookOutbox) return;
708
+ const engine = this.tryGetService(ctx, ["objectql", "data"]);
709
+ if (!engine) {
710
+ _optionalChain([ctx, 'access', _88 => _88.logger, 'access', _89 => _89.warn, 'optionalCall', _90 => _90(
711
+ "[webhook-retention] disabled \u2014 ObjectQL service not available"
712
+ )]);
713
+ return;
714
+ }
715
+ const retOpts = typeof opt === "object" ? opt : {};
716
+ this.retention = new DeliveryRetentionSweeper(engine, {
717
+ ...retOpts,
718
+ logger: ctx.logger
719
+ });
720
+ this.retention.start();
721
+ ctx.registerService("webhook.retention", this.retention);
722
+ _optionalChain([ctx, 'access', _91 => _91.logger, 'access', _92 => _92.info, 'optionalCall', _93 => _93("[webhook-retention] sweeper started")]);
723
+ }
724
+ tryGetService(ctx, names) {
725
+ for (const n of names) {
726
+ try {
727
+ const svc = ctx.getService(n);
728
+ if (svc) return svc;
729
+ } catch (e5) {
730
+ }
731
+ }
732
+ return void 0;
733
+ }
734
+ };
735
+
736
+
737
+
738
+
739
+
740
+
741
+
742
+
743
+
744
+
745
+
746
+ exports.AutoEnqueuer = AutoEnqueuer; exports.DEFAULT_TIMEOUT_MS = DEFAULT_TIMEOUT_MS; exports.DeliveryRetentionSweeper = DeliveryRetentionSweeper; exports.MemoryWebhookOutbox = MemoryWebhookOutbox; exports.WebhookDispatcher = WebhookDispatcher; exports.WebhookOutboxPlugin = WebhookOutboxPlugin; exports.classifyAttempt = classifyAttempt; exports.hashPartition = _chunkM4M5FWIHcjs.hashPartition; exports.nextRetryDelayMs = nextRetryDelayMs; exports.sendOnce = sendOnce;
747
+ //# sourceMappingURL=index.cjs.map