@futurewindai/wotchi 0.1.0-beta.2 → 0.1.0-beta.6

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 (103) hide show
  1. package/README.md +178 -47
  2. package/SECURITY.md +2 -2
  3. package/dist/cjs/core/admission.js +22 -0
  4. package/dist/cjs/core/client.js +416 -21
  5. package/dist/cjs/core/config.js +193 -18
  6. package/dist/cjs/core/diagnostics.js +16 -1
  7. package/dist/cjs/core/group-store.js +22 -9
  8. package/dist/cjs/core/incident-builder.js +18 -0
  9. package/dist/cjs/core/incident-policy.js +2 -7
  10. package/dist/cjs/core/limits.js +17 -0
  11. package/dist/cjs/core/normalize.js +16 -6
  12. package/dist/cjs/core/notification-queue.js +92 -6
  13. package/dist/cjs/core/prometheus.js +129 -0
  14. package/dist/cjs/core/redact.js +57 -8
  15. package/dist/cjs/core/runtime-monitor.js +109 -0
  16. package/dist/cjs/index.js +8 -1
  17. package/dist/cjs/integrations/express/error-handler.js +21 -4
  18. package/dist/cjs/integrations/express/index.js +2 -1
  19. package/dist/cjs/integrations/express/status-observer.js +6 -0
  20. package/dist/cjs/integrations/nest/capture.js +21 -0
  21. package/dist/cjs/integrations/nest/exception-filter.js +64 -8
  22. package/dist/cjs/integrations/nest/filter-wrapper.js +17 -0
  23. package/dist/cjs/integrations/nest/index.js +7 -1
  24. package/dist/cjs/integrations/nest/module.js +44 -0
  25. package/dist/cjs/integrations/nest/register.js +15 -1
  26. package/dist/cjs/integrations/request-context.js +83 -1
  27. package/dist/cjs/notifiers/alert-payload.js +20 -0
  28. package/dist/cjs/notifiers/console.js +85 -15
  29. package/dist/cjs/notifiers/telegram-format.js +75 -5
  30. package/dist/cjs/notifiers/webhook-http.js +394 -0
  31. package/dist/cjs/notifiers/webhook.js +28 -0
  32. package/dist/esm/core/admission.js +19 -0
  33. package/dist/esm/core/client.js +416 -21
  34. package/dist/esm/core/config.js +193 -18
  35. package/dist/esm/core/diagnostics.js +16 -1
  36. package/dist/esm/core/group-store.js +22 -9
  37. package/dist/esm/core/incident-builder.js +18 -0
  38. package/dist/esm/core/incident-policy.js +2 -7
  39. package/dist/esm/core/limits.js +14 -0
  40. package/dist/esm/core/normalize.js +16 -6
  41. package/dist/esm/core/notification-queue.js +92 -6
  42. package/dist/esm/core/prometheus.js +125 -0
  43. package/dist/esm/core/redact.js +57 -8
  44. package/dist/esm/core/runtime-monitor.js +106 -0
  45. package/dist/esm/index.js +4 -1
  46. package/dist/esm/integrations/express/error-handler.js +21 -4
  47. package/dist/esm/integrations/express/index.js +1 -1
  48. package/dist/esm/integrations/express/status-observer.js +6 -0
  49. package/dist/esm/integrations/nest/capture.js +17 -0
  50. package/dist/esm/integrations/nest/exception-filter.js +65 -9
  51. package/dist/esm/integrations/nest/filter-wrapper.js +14 -0
  52. package/dist/esm/integrations/nest/index.js +3 -1
  53. package/dist/esm/integrations/nest/module.js +41 -0
  54. package/dist/esm/integrations/nest/register.js +15 -1
  55. package/dist/esm/integrations/request-context.js +83 -1
  56. package/dist/esm/notifiers/alert-payload.js +16 -0
  57. package/dist/esm/notifiers/console.js +85 -15
  58. package/dist/esm/notifiers/telegram-format.js +75 -5
  59. package/dist/esm/notifiers/webhook-http.js +387 -0
  60. package/dist/esm/notifiers/webhook.js +24 -0
  61. package/dist/types/core/admission.d.ts +10 -0
  62. package/dist/types/core/config.d.ts +17 -1
  63. package/dist/types/core/diagnostics.d.ts +7 -1
  64. package/dist/types/core/incident-policy.d.ts +1 -1
  65. package/dist/types/core/limits.d.ts +14 -0
  66. package/dist/types/core/notification-queue.d.ts +14 -1
  67. package/dist/types/core/prometheus.d.ts +7 -0
  68. package/dist/types/core/runtime-monitor.d.ts +15 -0
  69. package/dist/types/core/types.d.ts +111 -2
  70. package/dist/types/index.d.ts +8 -2
  71. package/dist/types/integrations/express/index.d.ts +2 -2
  72. package/dist/types/integrations/nest/capture.d.ts +4 -0
  73. package/dist/types/integrations/nest/exception-filter.d.ts +6 -2
  74. package/dist/types/integrations/nest/filter-wrapper.d.ts +7 -0
  75. package/dist/types/integrations/nest/index.d.ts +6 -2
  76. package/dist/types/integrations/nest/module.d.ts +9 -0
  77. package/dist/types/integrations/nest/register.d.ts +3 -0
  78. package/dist/types/integrations/request-context.d.ts +3 -0
  79. package/dist/types/notifiers/alert-payload.d.ts +5 -0
  80. package/dist/types/notifiers/webhook-http.d.ts +37 -0
  81. package/dist/types/notifiers/webhook.d.ts +4 -0
  82. package/dist/types-cjs/core/admission.d.cts +10 -0
  83. package/dist/types-cjs/core/config.d.cts +17 -1
  84. package/dist/types-cjs/core/diagnostics.d.cts +7 -1
  85. package/dist/types-cjs/core/incident-policy.d.cts +1 -1
  86. package/dist/types-cjs/core/limits.d.cts +14 -0
  87. package/dist/types-cjs/core/notification-queue.d.cts +14 -1
  88. package/dist/types-cjs/core/prometheus.d.cts +7 -0
  89. package/dist/types-cjs/core/runtime-monitor.d.cts +15 -0
  90. package/dist/types-cjs/core/types.d.cts +111 -2
  91. package/dist/types-cjs/index.d.cts +8 -2
  92. package/dist/types-cjs/integrations/express/index.d.cts +2 -2
  93. package/dist/types-cjs/integrations/nest/capture.d.cts +4 -0
  94. package/dist/types-cjs/integrations/nest/exception-filter.d.cts +6 -2
  95. package/dist/types-cjs/integrations/nest/filter-wrapper.d.cts +7 -0
  96. package/dist/types-cjs/integrations/nest/index.d.cts +6 -2
  97. package/dist/types-cjs/integrations/nest/module.d.cts +9 -0
  98. package/dist/types-cjs/integrations/nest/register.d.cts +3 -0
  99. package/dist/types-cjs/integrations/request-context.d.cts +3 -0
  100. package/dist/types-cjs/notifiers/alert-payload.d.cts +5 -0
  101. package/dist/types-cjs/notifiers/webhook-http.d.cts +37 -0
  102. package/dist/types-cjs/notifiers/webhook.d.cts +4 -0
  103. package/package.json +17 -4
@@ -0,0 +1,394 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.normalizeWebhookOptions = normalizeWebhookOptions;
7
+ exports.sendWebhookAlert = sendWebhookAlert;
8
+ const node_dns_1 = __importDefault(require("node:dns"));
9
+ const node_http_1 = __importDefault(require("node:http"));
10
+ const node_https_1 = __importDefault(require("node:https"));
11
+ const node_net_1 = __importDefault(require("node:net"));
12
+ const alert_payload_js_1 = require("./alert-payload.js");
13
+ const DEFAULT_TIMEOUT_MS = 3_000;
14
+ const MAX_TIMEOUT_MS = 30_000;
15
+ const MAX_RESPONSE_BYTES = 8_192;
16
+ const MAX_PAYLOAD_BYTES = 32_768;
17
+ const MAX_URL_LENGTH = 2_048;
18
+ const MAX_HEADER_COUNT = 20;
19
+ const MAX_HEADER_VALUE_LENGTH = 2_000;
20
+ const wait = (delayMs) => new Promise((resolve) => {
21
+ setTimeout(resolve, delayMs);
22
+ });
23
+ const normalizeHost = (value) => value
24
+ .toLowerCase()
25
+ .replace(/^\[|\]$/g, "")
26
+ .replace(/\.$/g, "");
27
+ const parseIPv4 = (value) => {
28
+ const parts = value.split(".");
29
+ if (parts.length !== 4 || parts.some((part) => !/^\d+$/.test(part))) {
30
+ return undefined;
31
+ }
32
+ const numbers = parts.map(Number);
33
+ return numbers.every((part) => part >= 0 && part <= 255) ? numbers : undefined;
34
+ };
35
+ const isPrivateIPv4 = (value) => {
36
+ const parts = parseIPv4(value);
37
+ if (parts === undefined) {
38
+ return false;
39
+ }
40
+ const first = parts[0] ?? -1;
41
+ const second = parts[1] ?? -1;
42
+ return (first === 0 ||
43
+ first === 10 ||
44
+ first === 127 ||
45
+ (first === 100 && second >= 64 && second <= 127) ||
46
+ (first === 169 && second === 254) ||
47
+ (first === 172 && second >= 16 && second <= 31) ||
48
+ (first === 192 && second === 168) ||
49
+ (first === 198 && (second === 18 || second === 19)));
50
+ };
51
+ const parseIPv6 = (value) => {
52
+ const host = normalizeHost(value);
53
+ const halves = host.split("::");
54
+ if (halves.length > 2) {
55
+ return undefined;
56
+ }
57
+ const parseHalf = (half) => {
58
+ if (half === "") {
59
+ return [];
60
+ }
61
+ const groups = half.split(":");
62
+ const values = [];
63
+ for (const group of groups) {
64
+ if (group.includes(".")) {
65
+ const ipv4 = parseIPv4(group);
66
+ if (ipv4 === undefined) {
67
+ return undefined;
68
+ }
69
+ values.push((ipv4[0] ?? 0) * 256 + (ipv4[1] ?? 0));
70
+ values.push((ipv4[2] ?? 0) * 256 + (ipv4[3] ?? 0));
71
+ continue;
72
+ }
73
+ if (!/^[0-9a-f]{1,4}$/i.test(group)) {
74
+ return undefined;
75
+ }
76
+ values.push(Number.parseInt(group, 16));
77
+ }
78
+ return values;
79
+ };
80
+ const left = parseHalf(halves[0] ?? "");
81
+ const right = parseHalf(halves[1] ?? "");
82
+ if (left === undefined || right === undefined) {
83
+ return undefined;
84
+ }
85
+ const missing = halves.length === 2 ? 8 - left.length - right.length : 0;
86
+ if (missing < 1 || left.length + right.length + missing !== 8) {
87
+ return undefined;
88
+ }
89
+ const groups = [...left, ...Array.from({ length: missing }, () => 0), ...right];
90
+ return groups.flatMap((group) => [(group >> 8) & 0xff, group & 0xff]);
91
+ };
92
+ const isPrivateIPv6 = (value) => {
93
+ const bytes = parseIPv6(value);
94
+ if (bytes === undefined) {
95
+ return false;
96
+ }
97
+ const allZero = bytes.every((byte) => byte === 0);
98
+ const loopback = allZero || (bytes.slice(0, 15).every((byte) => byte === 0) && bytes[15] === 1);
99
+ const first = bytes[0] ?? 0;
100
+ const second = bytes[1] ?? 0;
101
+ if (loopback ||
102
+ first === 0xfc ||
103
+ first === 0xfd ||
104
+ (first === 0xfe && (second & 0xc0) === 0x80)) {
105
+ return true;
106
+ }
107
+ const mapped = bytes.slice(0, 10).every((byte) => byte === 0) && bytes[10] === 0xff && bytes[11] === 0xff;
108
+ const compatible = bytes.slice(0, 12).every((byte) => byte === 0);
109
+ if (!mapped && !compatible) {
110
+ return false;
111
+ }
112
+ return isPrivateIPv4(bytes.slice(12).join("."));
113
+ };
114
+ const isPrivateDestination = (value) => {
115
+ const host = normalizeHost(value);
116
+ if (host === "localhost" ||
117
+ host.endsWith(".localhost") ||
118
+ host.endsWith(".local") ||
119
+ host === "metadata.google.internal" ||
120
+ host.endsWith(".internal")) {
121
+ return true;
122
+ }
123
+ const family = node_net_1.default.isIP(host);
124
+ return family === 4 ? isPrivateIPv4(host) : family === 6 && isPrivateIPv6(host);
125
+ };
126
+ const privateDestinationError = () => new TypeError("webhook private or internal destinations require allowPrivateDestinations");
127
+ const normalizeUrl = (value, allowHttpLoopback = false, allowPrivateDestinations = false) => {
128
+ if (typeof value !== "string" || value.trim().length === 0 || value.length > MAX_URL_LENGTH) {
129
+ throw new TypeError("webhook url is invalid");
130
+ }
131
+ let url;
132
+ try {
133
+ url = new URL(value);
134
+ }
135
+ catch {
136
+ throw new TypeError("webhook url is invalid");
137
+ }
138
+ if (url.username !== "" || url.password !== "" || url.hash !== "") {
139
+ throw new TypeError("webhook url must not contain credentials or a fragment");
140
+ }
141
+ if (url.protocol === "https:" &&
142
+ !allowPrivateDestinations &&
143
+ isPrivateDestination(url.hostname)) {
144
+ throw privateDestinationError();
145
+ }
146
+ if (url.protocol === "https:") {
147
+ return url;
148
+ }
149
+ const loopback = new Set(["localhost", "127.0.0.1", "[::1]", "::1"]);
150
+ if (url.protocol !== "http:" || !allowHttpLoopback || !loopback.has(url.hostname)) {
151
+ throw new TypeError("webhook url must be HTTPS or explicitly enabled loopback HTTP");
152
+ }
153
+ return url;
154
+ };
155
+ const normalizeHeaders = (headers) => {
156
+ if (headers === undefined) {
157
+ return undefined;
158
+ }
159
+ const entries = Object.entries(headers);
160
+ if (entries.length > MAX_HEADER_COUNT) {
161
+ throw new TypeError("webhook headers exceed the maximum count");
162
+ }
163
+ const normalized = {};
164
+ for (const [name, value] of entries) {
165
+ if (!/^[A-Za-z0-9-]+$/.test(name) || name.length > 100) {
166
+ throw new TypeError("webhook header name is invalid");
167
+ }
168
+ if (typeof value !== "string" ||
169
+ value.length > MAX_HEADER_VALUE_LENGTH ||
170
+ /[\r\n]/.test(value)) {
171
+ throw new TypeError("webhook header value is invalid");
172
+ }
173
+ const normalizedName = name.toLowerCase();
174
+ if (normalizedName === "content-length" || normalizedName === "host") {
175
+ throw new TypeError(`webhook header ${normalizedName} is reserved`);
176
+ }
177
+ normalized[normalizedName] = value;
178
+ }
179
+ return normalized;
180
+ };
181
+ function normalizeWebhookOptions(options) {
182
+ if (options.allowHttpLoopback !== undefined && typeof options.allowHttpLoopback !== "boolean") {
183
+ throw new TypeError("webhook allowHttpLoopback is invalid");
184
+ }
185
+ const allowHttpLoopback = options.allowHttpLoopback === true;
186
+ const allowPrivateDestinations = options.allowPrivateDestinations === true;
187
+ const url = normalizeUrl(options.url, allowHttpLoopback, allowPrivateDestinations);
188
+ const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
189
+ if (!Number.isSafeInteger(timeoutMs) || timeoutMs <= 0 || timeoutMs > MAX_TIMEOUT_MS) {
190
+ throw new TypeError("webhook timeoutMs is invalid");
191
+ }
192
+ const maxRetries = options.maxRetries ?? 1;
193
+ if (!Number.isSafeInteger(maxRetries) || maxRetries < 0 || maxRetries > 1) {
194
+ throw new TypeError("webhook maxRetries must be 0 or 1");
195
+ }
196
+ if (options.payloadBuilder !== undefined && typeof options.payloadBuilder !== "function") {
197
+ throw new TypeError("webhook payloadBuilder is invalid");
198
+ }
199
+ const headers = normalizeHeaders(options.headers);
200
+ return {
201
+ url,
202
+ ...(headers === undefined ? {} : { headers }),
203
+ timeoutMs,
204
+ maxRetries,
205
+ allowHttpLoopback,
206
+ allowPrivateDestinations: allowPrivateDestinations || (url.protocol === "http:" && allowHttpLoopback),
207
+ ...(options.payloadBuilder === undefined ? {} : { payloadBuilder: options.payloadBuilder }),
208
+ };
209
+ }
210
+ const resolveWebhookAddress = async (hostname, timeoutMs, signal) => {
211
+ let timer;
212
+ let removeAbortListener = () => undefined;
213
+ try {
214
+ const timeout = new Promise((_, reject) => {
215
+ timer = setTimeout(() => reject(new Error("webhook destination lookup timed out")), timeoutMs);
216
+ });
217
+ const races = [node_dns_1.default.promises.lookup(hostname), timeout];
218
+ if (signal !== undefined) {
219
+ const aborted = new Promise((_, reject) => {
220
+ const onAbort = () => reject(new Error("webhook request timed out"));
221
+ if (signal.aborted) {
222
+ onAbort();
223
+ return;
224
+ }
225
+ signal.addEventListener("abort", onAbort, { once: true });
226
+ removeAbortListener = () => signal.removeEventListener("abort", onAbort);
227
+ });
228
+ races.push(aborted);
229
+ }
230
+ return await Promise.race(races);
231
+ }
232
+ finally {
233
+ if (timer !== undefined) {
234
+ clearTimeout(timer);
235
+ }
236
+ removeAbortListener();
237
+ }
238
+ };
239
+ const defaultRequest = async (options, body, timeoutMs) => {
240
+ const resolvedAddress = await resolveWebhookAddress(options.hostname, timeoutMs, options.signal);
241
+ if (!options.allowPrivateDestinations && isPrivateDestination(resolvedAddress.address)) {
242
+ throw privateDestinationError();
243
+ }
244
+ return new Promise((resolve, reject) => {
245
+ let settled = false;
246
+ let removeAbortListener = () => undefined;
247
+ const finish = (callback) => {
248
+ if (settled) {
249
+ return;
250
+ }
251
+ settled = true;
252
+ removeAbortListener();
253
+ callback();
254
+ };
255
+ const { allowPrivateDestinations: _allowPrivateDestinations, ...nodeOptions } = options;
256
+ const request = (options.protocol === "http:" ? node_http_1.default : node_https_1.default).request({
257
+ ...nodeOptions,
258
+ lookup: (_hostname, _lookupOptions, callback) => callback(null, resolvedAddress.address, resolvedAddress.family),
259
+ }, (response) => {
260
+ const chunks = [];
261
+ let size = 0;
262
+ response.on("data", (chunk) => {
263
+ size += Buffer.byteLength(chunk);
264
+ if (size > MAX_RESPONSE_BYTES) {
265
+ request.destroy();
266
+ finish(() => reject(new Error("webhook response body exceeded limit")));
267
+ return;
268
+ }
269
+ chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
270
+ });
271
+ response.once("error", (error) => finish(() => reject(error)));
272
+ response.once("end", () => finish(() => resolve({
273
+ statusCode: response.statusCode ?? 0,
274
+ headers: response.headers,
275
+ body: Buffer.concat(chunks).toString("utf8"),
276
+ })));
277
+ });
278
+ const onAbort = () => {
279
+ request.destroy();
280
+ };
281
+ if (options.signal !== undefined) {
282
+ options.signal.addEventListener("abort", onAbort, { once: true });
283
+ removeAbortListener = () => options.signal?.removeEventListener("abort", onAbort);
284
+ if (options.signal.aborted) {
285
+ onAbort();
286
+ }
287
+ }
288
+ request.once("error", (error) => finish(() => reject(error)));
289
+ request.setTimeout(timeoutMs, () => {
290
+ request.destroy();
291
+ finish(() => reject(new Error("webhook request timed out")));
292
+ });
293
+ request.end(body);
294
+ });
295
+ };
296
+ const requestWithTimeout = async (request, options, body, timeoutMs) => {
297
+ let timer;
298
+ const controller = new AbortController();
299
+ try {
300
+ const timeout = new Promise((_, reject) => {
301
+ timer = setTimeout(() => {
302
+ controller.abort();
303
+ reject(new Error("webhook request timed out"));
304
+ }, timeoutMs);
305
+ });
306
+ return await Promise.race([
307
+ request({ ...options, signal: controller.signal }, body, timeoutMs),
308
+ timeout,
309
+ ]);
310
+ }
311
+ finally {
312
+ if (timer !== undefined) {
313
+ clearTimeout(timer);
314
+ }
315
+ }
316
+ };
317
+ const deepFreeze = (value) => {
318
+ if (typeof value !== "object" || value === null || Object.isFrozen(value)) {
319
+ return value;
320
+ }
321
+ Object.freeze(value);
322
+ for (const child of Object.values(value)) {
323
+ deepFreeze(child);
324
+ }
325
+ return value;
326
+ };
327
+ const serializeAlert = (alert, payloadBuilder) => {
328
+ const sanitizedAlert = deepFreeze((0, alert_payload_js_1.toBoundedAlertPayload)(alert));
329
+ let payload;
330
+ try {
331
+ const custom = payloadBuilder === undefined ? sanitizedAlert : payloadBuilder(sanitizedAlert);
332
+ payload = (0, alert_payload_js_1.toBoundedPayload)(custom);
333
+ }
334
+ catch {
335
+ throw new Error("webhook payload builder failed");
336
+ }
337
+ const envelope = {
338
+ version: 1,
339
+ type: "incident.alert",
340
+ sentAt: new Date().toISOString(),
341
+ alert: payload,
342
+ };
343
+ const serialized = JSON.stringify(envelope);
344
+ if (Buffer.byteLength(serialized, "utf8") <= MAX_PAYLOAD_BYTES) {
345
+ return serialized;
346
+ }
347
+ const fallback = JSON.stringify({
348
+ version: 1,
349
+ type: "incident.alert",
350
+ sentAt: new Date().toISOString(),
351
+ alert: {
352
+ title: alert.title.slice(0, 300),
353
+ fingerprint: alert.fingerprint.slice(0, 200),
354
+ severity: alert.severity,
355
+ summary: alert.summary.slice(0, 2_000),
356
+ service: alert.service.slice(0, 200),
357
+ environment: alert.environment.slice(0, 200),
358
+ occurrences: alert.occurrences,
359
+ truncated: true,
360
+ },
361
+ });
362
+ return fallback;
363
+ };
364
+ async function sendWebhookAlert(options, request = defaultRequest) {
365
+ const normalized = normalizeWebhookOptions(options);
366
+ const body = serializeAlert(options.alert, normalized.payloadBuilder);
367
+ const requestOptions = {
368
+ protocol: normalized.url.protocol,
369
+ hostname: normalized.url.hostname.replace(/^\[|\]$/g, ""),
370
+ ...(normalized.url.port === "" ? {} : { port: normalized.url.port }),
371
+ method: "POST",
372
+ path: `${normalized.url.pathname || "/"}${normalized.url.search}`,
373
+ headers: {
374
+ ...(normalized.headers ?? {}),
375
+ "content-type": "application/json",
376
+ "content-length": Buffer.byteLength(body, "utf8"),
377
+ },
378
+ allowPrivateDestinations: normalized.allowPrivateDestinations,
379
+ };
380
+ let attempt = 0;
381
+ while (true) {
382
+ const response = await requestWithTimeout(request, requestOptions, body, normalized.timeoutMs);
383
+ if (response.statusCode >= 200 && response.statusCode < 300) {
384
+ return;
385
+ }
386
+ if (attempt < normalized.maxRetries &&
387
+ (response.statusCode === 429 || response.statusCode >= 500)) {
388
+ attempt += 1;
389
+ await wait(50);
390
+ continue;
391
+ }
392
+ throw new Error(`Webhook request failed with status ${response.statusCode}`);
393
+ }
394
+ }
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createWebhookNotifier = createWebhookNotifier;
4
+ exports.webhookNotifier = webhookNotifier;
5
+ const webhook_http_js_1 = require("./webhook-http.js");
6
+ function createWebhookNotifier(options, request) {
7
+ const normalized = (0, webhook_http_js_1.normalizeWebhookOptions)(options);
8
+ const sendOptions = {
9
+ url: normalized.url.toString(),
10
+ ...(normalized.headers === undefined ? {} : { headers: normalized.headers }),
11
+ timeoutMs: normalized.timeoutMs,
12
+ maxRetries: normalized.maxRetries,
13
+ allowHttpLoopback: normalized.allowHttpLoopback,
14
+ allowPrivateDestinations: normalized.allowPrivateDestinations,
15
+ ...(normalized.payloadBuilder === undefined
16
+ ? {}
17
+ : { payloadBuilder: normalized.payloadBuilder }),
18
+ };
19
+ return {
20
+ name: "webhook",
21
+ async send(alert) {
22
+ await (0, webhook_http_js_1.sendWebhookAlert)({ ...sendOptions, alert }, request);
23
+ },
24
+ };
25
+ }
26
+ function webhookNotifier(options) {
27
+ return createWebhookNotifier(options);
28
+ }
@@ -0,0 +1,19 @@
1
+ /** A small token bucket used before normalization to bound overload work. */
2
+ export function createCaptureAdmission(options) {
3
+ const now = options.now ?? Date.now;
4
+ let tokens = options.burst;
5
+ let lastRefill = now();
6
+ return {
7
+ tryAcquire: () => {
8
+ const timestamp = now();
9
+ const elapsedMs = Math.max(0, timestamp - lastRefill);
10
+ tokens = Math.min(options.burst, tokens + (elapsedMs * options.maxEventsPerSecond) / 1_000);
11
+ lastRefill = timestamp;
12
+ if (tokens < 1) {
13
+ return false;
14
+ }
15
+ tokens -= 1;
16
+ return true;
17
+ },
18
+ };
19
+ }