hooksend 1.0.0-test → 1.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/dist/index.js ADDED
@@ -0,0 +1,204 @@
1
+ import {
2
+ BlockedError,
3
+ DEFAULT_JITTER,
4
+ DEFAULT_MAX_RETRY_AFTER_MS,
5
+ DEFAULT_SCHEDULE,
6
+ DEFAULT_TIMEOUT_MS,
7
+ VERSION,
8
+ applyJitter,
9
+ checkUrl,
10
+ createMessageId,
11
+ createSafeLookup,
12
+ decodeSecret,
13
+ deliver,
14
+ deliverWith,
15
+ generateSecret,
16
+ isPrivateAddress,
17
+ parseRetryAfter,
18
+ prepareMessage,
19
+ sendOnce,
20
+ shouldRetry,
21
+ sign
22
+ } from "./chunk-SCUPIBTS.js";
23
+
24
+ // src/dispatcher.ts
25
+ var MemoryStore = class {
26
+ jobs = /* @__PURE__ */ new Map();
27
+ save(job) {
28
+ this.jobs.set(job.id, { ...job });
29
+ }
30
+ remove(id) {
31
+ this.jobs.delete(id);
32
+ }
33
+ due(now) {
34
+ return [...this.jobs.values()].filter((job) => job.nextAttemptAt <= now).map((job) => ({ ...job }));
35
+ }
36
+ };
37
+ function createLimiter(max) {
38
+ let active = 0;
39
+ const queue = [];
40
+ const next = () => {
41
+ if (active >= max) return;
42
+ const run = queue.shift();
43
+ if (run) {
44
+ active++;
45
+ run();
46
+ }
47
+ };
48
+ return (fn) => new Promise((resolve, reject) => {
49
+ queue.push(() => {
50
+ fn().then(resolve, reject).finally(() => {
51
+ active--;
52
+ next();
53
+ });
54
+ });
55
+ next();
56
+ });
57
+ }
58
+ function safeCall(fn, arg) {
59
+ if (!fn) return;
60
+ try {
61
+ fn(arg);
62
+ } catch {
63
+ }
64
+ }
65
+ function createDispatcher(options = {}) {
66
+ const {
67
+ concurrency = 10,
68
+ store = new MemoryStore(),
69
+ onAttempt,
70
+ onDelivered,
71
+ onFailed,
72
+ onDisabled,
73
+ ...deliverOptions
74
+ } = options;
75
+ if (!Number.isInteger(concurrency) || concurrency < 1) throw new TypeError("concurrency must be a positive integer");
76
+ const now = options.now ?? Date.now;
77
+ const limit = createLimiter(concurrency);
78
+ const controller = new AbortController();
79
+ const active = /* @__PURE__ */ new Map();
80
+ let closed = false;
81
+ async function runJob(job) {
82
+ const base = { url: job.endpoint.url, jobId: job.id };
83
+ let res;
84
+ try {
85
+ await store.save(job);
86
+ res = await deliverWith(
87
+ job.endpoint,
88
+ { id: job.messageId, body: Buffer.from(job.body, "utf8") },
89
+ {
90
+ ...deliverOptions,
91
+ startAttempt: job.attempt,
92
+ ...onAttempt ? { onAttempt } : {}
93
+ },
94
+ {
95
+ limit,
96
+ waitSignal: controller.signal,
97
+ onRetryScheduled: async (nextAttempt, delayMs) => {
98
+ job.attempt = nextAttempt;
99
+ job.nextAttemptAt = now() + delayMs;
100
+ await store.save(job);
101
+ }
102
+ }
103
+ );
104
+ } catch (err) {
105
+ return {
106
+ ...base,
107
+ delivered: false,
108
+ disabled: false,
109
+ aborted: false,
110
+ id: job.messageId,
111
+ attempts: [],
112
+ error: `Store error: ${err instanceof Error ? err.message : String(err)}`
113
+ };
114
+ }
115
+ const out = { ...base, ...res };
116
+ if (res.aborted) return out;
117
+ try {
118
+ await store.remove(job.id);
119
+ } catch (err) {
120
+ out.error = `Store error: ${err instanceof Error ? err.message : String(err)}`;
121
+ }
122
+ if (res.delivered) safeCall(onDelivered, out);
123
+ else if (res.disabled) safeCall(onDisabled, out);
124
+ else safeCall(onFailed, out);
125
+ return out;
126
+ }
127
+ function track(job) {
128
+ const existing = active.get(job.id);
129
+ if (existing) return existing;
130
+ const p = runJob(job).finally(() => active.delete(job.id));
131
+ active.set(job.id, p);
132
+ return p;
133
+ }
134
+ return {
135
+ async dispatch(input) {
136
+ if (closed) throw new Error("Dispatcher is closed");
137
+ if (!input || typeof input.type !== "string" || input.type.length === 0) {
138
+ throw new TypeError("dispatch() needs a `type`");
139
+ }
140
+ if (!Array.isArray(input.endpoints)) throw new TypeError("dispatch() needs an `endpoints` array");
141
+ const id = input.id ?? createMessageId();
142
+ const prepared = prepareMessage(
143
+ { type: input.type, data: input.data, id, ...input.timestamp !== void 0 ? { timestamp: input.timestamp } : {} },
144
+ now
145
+ );
146
+ const body = Buffer.from(prepared.body).toString("utf8");
147
+ const createdAt = now();
148
+ const results = await Promise.all(
149
+ input.endpoints.map(
150
+ (endpoint, i) => track({
151
+ id: `${id}:${i}`,
152
+ messageId: id,
153
+ type: input.type,
154
+ body,
155
+ endpoint,
156
+ attempt: 1,
157
+ nextAttemptAt: createdAt,
158
+ createdAt
159
+ })
160
+ )
161
+ );
162
+ return { id, results };
163
+ },
164
+ async resume() {
165
+ if (closed) throw new Error("Dispatcher is closed");
166
+ const jobs = await store.due(now());
167
+ return Promise.all(jobs.map((job) => track({ ...job })));
168
+ },
169
+ async drain() {
170
+ while (active.size > 0) await Promise.allSettled([...active.values()]);
171
+ },
172
+ async close() {
173
+ closed = true;
174
+ controller.abort();
175
+ while (active.size > 0) await Promise.allSettled([...active.values()]);
176
+ },
177
+ get pending() {
178
+ return active.size;
179
+ }
180
+ };
181
+ }
182
+ export {
183
+ BlockedError,
184
+ DEFAULT_JITTER,
185
+ DEFAULT_MAX_RETRY_AFTER_MS,
186
+ DEFAULT_SCHEDULE,
187
+ DEFAULT_TIMEOUT_MS,
188
+ MemoryStore,
189
+ VERSION,
190
+ applyJitter,
191
+ checkUrl,
192
+ createDispatcher,
193
+ createMessageId,
194
+ createSafeLookup,
195
+ decodeSecret,
196
+ deliver,
197
+ generateSecret,
198
+ isPrivateAddress,
199
+ parseRetryAfter,
200
+ prepareMessage,
201
+ sendOnce,
202
+ shouldRetry,
203
+ sign
204
+ };
package/package.json CHANGED
@@ -1,9 +1,73 @@
1
1
  {
2
2
  "name": "hooksend",
3
- "version": "1.0.0-test",
4
- "description": "Send webhooks reliably: Standard Webhooks signing, retries with backoff, SSRF protection (placeholder - in development)",
5
- "main": "index.js",
6
- "files": ["index.js", "README.md"],
3
+ "version": "1.0.0",
4
+ "description": "Send webhooks reliably: Standard Webhooks signing, retries with backoff, SSRF protection and fan-out to many endpoints. Self-hosted, zero dependencies.",
5
+ "keywords": [
6
+ "webhook",
7
+ "webhooks",
8
+ "send-webhooks",
9
+ "webhook-sender",
10
+ "webhook-delivery",
11
+ "outgoing-webhooks",
12
+ "standard-webhooks",
13
+ "standardwebhooks",
14
+ "svix",
15
+ "svix-alternative",
16
+ "hmac",
17
+ "signature",
18
+ "signing",
19
+ "retry",
20
+ "exponential-backoff",
21
+ "ssrf",
22
+ "ssrf-protection",
23
+ "dns-rebinding",
24
+ "fan-out",
25
+ "self-hosted",
26
+ "zero-dependencies",
27
+ "cli"
28
+ ],
29
+ "license": "MIT",
7
30
  "author": "CK Giri <ckgdev@gmail.com>",
8
- "license": "MIT"
31
+ "type": "module",
32
+ "sideEffects": false,
33
+ "main": "./dist/index.cjs",
34
+ "module": "./dist/index.js",
35
+ "types": "./dist/index.d.ts",
36
+ "bin": {
37
+ "hooksend": "./dist/cli.js"
38
+ },
39
+ "exports": {
40
+ ".": {
41
+ "import": {
42
+ "types": "./dist/index.d.ts",
43
+ "default": "./dist/index.js"
44
+ },
45
+ "require": {
46
+ "types": "./dist/index.d.cts",
47
+ "default": "./dist/index.cjs"
48
+ }
49
+ },
50
+ "./package.json": "./package.json"
51
+ },
52
+ "files": [
53
+ "dist",
54
+ "CHANGELOG.md"
55
+ ],
56
+ "engines": {
57
+ "node": ">=20"
58
+ },
59
+ "scripts": {
60
+ "build": "tsup",
61
+ "test": "vitest run",
62
+ "typecheck": "tsc -p .",
63
+ "prepublishOnly": "npm run typecheck && npm test && npm run build"
64
+ },
65
+ "devDependencies": {
66
+ "@types/node": "^20.14.0",
67
+ "hookseal": "^0.1.0",
68
+ "standardwebhooks": "^1.0.0",
69
+ "tsup": "^8.3.0",
70
+ "typescript": "^5.6.0",
71
+ "vitest": "^2.1.0"
72
+ }
9
73
  }
package/index.js DELETED
@@ -1 +0,0 @@
1
- module.exports = {};