@classytic/notifications 1.2.0 → 2.0.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.
- package/CHANGELOG.md +70 -37
- package/README.md +351 -201
- package/dist/channels/index.d.mts +2 -2
- package/dist/channels/index.mjs +2 -2
- package/dist/index.d.mts +38 -18
- package/dist/index.mjs +117 -28
- package/dist/{console.channel-BoWi_5nH.d.mts → push.channel-4mRkzrEu.d.mts} +18 -2
- package/dist/{console.channel-CfA4b_Er.mjs → push.channel-D1FdTPUi.mjs} +228 -6
- package/dist/status-webhook-BDaI1BMy.d.mts +268 -0
- package/dist/status-webhook-XY42DCj_.mjs +582 -0
- package/dist/{types-BnjZ7x2U.d.mts → types-D94B3VNX.d.mts} +251 -2
- package/dist/utils/index.d.mts +3 -3
- package/dist/utils/index.mjs +3 -3
- package/package.json +11 -11
- package/dist/concurrency-CV_XyiBQ.d.mts +0 -139
- package/dist/merge-hooks-B4_uHR9x.mjs +0 -233
- /package/dist/{errors-DAIVZx_Q.mjs → errors-FYJYmfvj.mjs} +0 -0
|
@@ -1,233 +0,0 @@
|
|
|
1
|
-
//#region src/utils/emitter.ts
|
|
2
|
-
var Emitter = class {
|
|
3
|
-
handlers = /* @__PURE__ */ new Map();
|
|
4
|
-
/** Register an event handler */
|
|
5
|
-
on(event, handler) {
|
|
6
|
-
const list = this.handlers.get(event) ?? [];
|
|
7
|
-
list.push(handler);
|
|
8
|
-
this.handlers.set(event, list);
|
|
9
|
-
return this;
|
|
10
|
-
}
|
|
11
|
-
/** Remove an event handler */
|
|
12
|
-
off(event, handler) {
|
|
13
|
-
const list = this.handlers.get(event);
|
|
14
|
-
if (list) this.handlers.set(event, list.filter((h) => h !== handler));
|
|
15
|
-
return this;
|
|
16
|
-
}
|
|
17
|
-
/** Emit an event (runs handlers sequentially, awaits async) */
|
|
18
|
-
async emit(event, ...args) {
|
|
19
|
-
const list = this.handlers.get(event);
|
|
20
|
-
if (!list) return;
|
|
21
|
-
for (const handler of list) await handler(...args);
|
|
22
|
-
}
|
|
23
|
-
/** Remove all handlers for an event (or all events) */
|
|
24
|
-
removeAll(event) {
|
|
25
|
-
if (event) this.handlers.delete(event);
|
|
26
|
-
else this.handlers.clear();
|
|
27
|
-
return this;
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
//#endregion
|
|
32
|
-
//#region src/utils/retry.ts
|
|
33
|
-
/** Resolve retry config with defaults */
|
|
34
|
-
function resolveRetryConfig(config) {
|
|
35
|
-
return {
|
|
36
|
-
maxAttempts: config?.maxAttempts ?? 1,
|
|
37
|
-
backoff: config?.backoff ?? "exponential",
|
|
38
|
-
initialDelay: config?.initialDelay ?? 500,
|
|
39
|
-
maxDelay: config?.maxDelay ?? 3e4
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
/** Calculate delay for a given attempt */
|
|
43
|
-
function calculateDelay(attempt, config) {
|
|
44
|
-
let delay;
|
|
45
|
-
switch (config.backoff) {
|
|
46
|
-
case "exponential":
|
|
47
|
-
delay = config.initialDelay * Math.pow(2, attempt - 1);
|
|
48
|
-
break;
|
|
49
|
-
case "linear":
|
|
50
|
-
delay = config.initialDelay * attempt;
|
|
51
|
-
break;
|
|
52
|
-
default: delay = config.initialDelay;
|
|
53
|
-
}
|
|
54
|
-
const jitter = delay * .25 * (Math.random() * 2 - 1);
|
|
55
|
-
delay = Math.min(delay + jitter, config.maxDelay);
|
|
56
|
-
return Math.max(0, Math.round(delay));
|
|
57
|
-
}
|
|
58
|
-
/** Sleep for a given duration */
|
|
59
|
-
function sleep(ms) {
|
|
60
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* Execute a function with retry and backoff
|
|
64
|
-
*
|
|
65
|
-
* @param fn - Async function to retry
|
|
66
|
-
* @param config - Retry configuration
|
|
67
|
-
* @param onRetry - Called before each retry attempt
|
|
68
|
-
* @returns Result of fn
|
|
69
|
-
* @throws Last error after all attempts exhausted
|
|
70
|
-
*/
|
|
71
|
-
async function withRetry(fn, config, onRetry) {
|
|
72
|
-
let lastError;
|
|
73
|
-
for (let attempt = 1; attempt <= config.maxAttempts; attempt++) try {
|
|
74
|
-
return await fn();
|
|
75
|
-
} catch (err) {
|
|
76
|
-
lastError = err instanceof Error ? err : new Error(String(err));
|
|
77
|
-
if (attempt === config.maxAttempts) break;
|
|
78
|
-
const delay = calculateDelay(attempt, config);
|
|
79
|
-
onRetry?.(attempt, lastError);
|
|
80
|
-
await sleep(delay);
|
|
81
|
-
}
|
|
82
|
-
throw lastError;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
//#endregion
|
|
86
|
-
//#region src/utils/quiet-hours.ts
|
|
87
|
-
/**
|
|
88
|
-
* Check if the current time falls within quiet hours.
|
|
89
|
-
*
|
|
90
|
-
* Supports overnight ranges (e.g. 22:00 - 07:00).
|
|
91
|
-
* Returns false if start/end are missing or malformed.
|
|
92
|
-
*
|
|
93
|
-
* @param quiet - Quiet hours config with start, end, and optional timezone
|
|
94
|
-
* @param now - Current date (defaults to new Date(), injectable for testing)
|
|
95
|
-
*/
|
|
96
|
-
function isQuietHours(quiet, now = /* @__PURE__ */ new Date()) {
|
|
97
|
-
if (!quiet.start || !quiet.end) return false;
|
|
98
|
-
const startMinutes = parseTimeToMinutes(quiet.start);
|
|
99
|
-
const endMinutes = parseTimeToMinutes(quiet.end);
|
|
100
|
-
if (startMinutes === -1 || endMinutes === -1) return false;
|
|
101
|
-
const currentMinutes = getCurrentMinutes(now, quiet.timezone);
|
|
102
|
-
if (startMinutes <= endMinutes) return currentMinutes >= startMinutes && currentMinutes < endMinutes;
|
|
103
|
-
return currentMinutes >= startMinutes || currentMinutes < endMinutes;
|
|
104
|
-
}
|
|
105
|
-
/** Parse "HH:MM" to minutes since midnight. Returns -1 on invalid input. */
|
|
106
|
-
function parseTimeToMinutes(time) {
|
|
107
|
-
const match = time.match(/^(\d{1,2}):(\d{2})$/);
|
|
108
|
-
if (!match) return -1;
|
|
109
|
-
const hours = parseInt(match[1], 10);
|
|
110
|
-
const minutes = parseInt(match[2], 10);
|
|
111
|
-
if (hours < 0 || hours > 23 || minutes < 0 || minutes > 59) return -1;
|
|
112
|
-
return hours * 60 + minutes;
|
|
113
|
-
}
|
|
114
|
-
/** Get current minutes-since-midnight in the given timezone (or UTC). */
|
|
115
|
-
function getCurrentMinutes(now, timezone) {
|
|
116
|
-
if (!timezone) return now.getUTCHours() * 60 + now.getUTCMinutes();
|
|
117
|
-
const parts = new Intl.DateTimeFormat("en-US", {
|
|
118
|
-
timeZone: timezone,
|
|
119
|
-
hour: "numeric",
|
|
120
|
-
minute: "numeric",
|
|
121
|
-
hour12: false
|
|
122
|
-
}).formatToParts(now);
|
|
123
|
-
const hour = parseInt(parts.find((p) => p.type === "hour")?.value ?? "0", 10);
|
|
124
|
-
const minute = parseInt(parts.find((p) => p.type === "minute")?.value ?? "0", 10);
|
|
125
|
-
return hour * 60 + minute;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
//#endregion
|
|
129
|
-
//#region src/utils/idempotency.ts
|
|
130
|
-
/**
|
|
131
|
-
* Idempotency / deduplication utilities
|
|
132
|
-
* @module @classytic/notifications/utils
|
|
133
|
-
*
|
|
134
|
-
* Prevents duplicate notification delivery using idempotency keys.
|
|
135
|
-
* Ships with an in-memory store; implement IdempotencyStore for
|
|
136
|
-
* Redis, database, or other distributed backends.
|
|
137
|
-
*/
|
|
138
|
-
/** Default TTL: 24 hours */
|
|
139
|
-
const DEFAULT_TTL = 1440 * 60 * 1e3;
|
|
140
|
-
/**
|
|
141
|
-
* In-memory idempotency store with TTL-based expiry.
|
|
142
|
-
*
|
|
143
|
-
* Suitable for single-process apps. For distributed systems,
|
|
144
|
-
* implement `IdempotencyStore` with Redis or your database.
|
|
145
|
-
*
|
|
146
|
-
* Expired entries are lazily cleaned up every `cleanupInterval` writes.
|
|
147
|
-
*/
|
|
148
|
-
var MemoryIdempotencyStore = class {
|
|
149
|
-
store = /* @__PURE__ */ new Map();
|
|
150
|
-
cleanupCounter = 0;
|
|
151
|
-
cleanupInterval;
|
|
152
|
-
constructor(options) {
|
|
153
|
-
this.cleanupInterval = options?.cleanupInterval ?? 100;
|
|
154
|
-
}
|
|
155
|
-
has(key) {
|
|
156
|
-
const expiry = this.store.get(key);
|
|
157
|
-
if (expiry === void 0) return false;
|
|
158
|
-
if (Date.now() > expiry) {
|
|
159
|
-
this.store.delete(key);
|
|
160
|
-
return false;
|
|
161
|
-
}
|
|
162
|
-
return true;
|
|
163
|
-
}
|
|
164
|
-
set(key, ttlMs) {
|
|
165
|
-
this.store.set(key, Date.now() + ttlMs);
|
|
166
|
-
this.maybeCleanup();
|
|
167
|
-
}
|
|
168
|
-
/** Number of entries (including possibly expired) */
|
|
169
|
-
get size() {
|
|
170
|
-
return this.store.size;
|
|
171
|
-
}
|
|
172
|
-
/** Remove all entries */
|
|
173
|
-
clear() {
|
|
174
|
-
this.store.clear();
|
|
175
|
-
}
|
|
176
|
-
maybeCleanup() {
|
|
177
|
-
if (++this.cleanupCounter < this.cleanupInterval) return;
|
|
178
|
-
this.cleanupCounter = 0;
|
|
179
|
-
const now = Date.now();
|
|
180
|
-
for (const [key, expiry] of this.store) if (now > expiry) this.store.delete(key);
|
|
181
|
-
}
|
|
182
|
-
};
|
|
183
|
-
/** Default TTL for idempotency keys (24 hours) */
|
|
184
|
-
const IDEMPOTENCY_DEFAULT_TTL = DEFAULT_TTL;
|
|
185
|
-
|
|
186
|
-
//#endregion
|
|
187
|
-
//#region src/utils/concurrency.ts
|
|
188
|
-
/**
|
|
189
|
-
* Map over items with controlled concurrency.
|
|
190
|
-
*
|
|
191
|
-
* Like `Promise.all(items.map(fn))` but with a max number of
|
|
192
|
-
* parallel operations. Results are returned in input order.
|
|
193
|
-
*
|
|
194
|
-
* Uses a worker-pool pattern: N workers pull from a shared index,
|
|
195
|
-
* so the pipeline stays full even when some items are slower.
|
|
196
|
-
*
|
|
197
|
-
* @param items - Array of items to process
|
|
198
|
-
* @param fn - Async function to apply to each item
|
|
199
|
-
* @param options - Concurrency options
|
|
200
|
-
* @returns Array of results in the same order as input
|
|
201
|
-
*/
|
|
202
|
-
async function pMap(items, fn, options = {}) {
|
|
203
|
-
const { concurrency = 10 } = options;
|
|
204
|
-
if (items.length === 0) return [];
|
|
205
|
-
const results = new Array(items.length);
|
|
206
|
-
let nextIndex = 0;
|
|
207
|
-
async function worker() {
|
|
208
|
-
while (nextIndex < items.length) {
|
|
209
|
-
const i = nextIndex++;
|
|
210
|
-
results[i] = await fn(items[i], i);
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
await Promise.all(Array.from({ length: Math.min(concurrency, items.length) }, () => worker()));
|
|
214
|
-
return results;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
//#endregion
|
|
218
|
-
//#region src/utils/merge-hooks.ts
|
|
219
|
-
function mergeHooks(...hookConfigs) {
|
|
220
|
-
const merged = {};
|
|
221
|
-
for (const config of hookConfigs) {
|
|
222
|
-
if (!config || typeof config !== "object") continue;
|
|
223
|
-
for (const [event, handlers] of Object.entries(config)) {
|
|
224
|
-
if (!merged[event]) merged[event] = [];
|
|
225
|
-
const handlerArray = Array.isArray(handlers) ? handlers : [handlers];
|
|
226
|
-
merged[event].push(...handlerArray);
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
return merged;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
//#endregion
|
|
233
|
-
export { isQuietHours as a, withRetry as c, MemoryIdempotencyStore as i, Emitter as l, pMap as n, calculateDelay as o, IDEMPOTENCY_DEFAULT_TTL as r, resolveRetryConfig as s, mergeHooks as t };
|
|
File without changes
|