hooksend 0.0.0-stage → 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/CHANGELOG.md +12 -0
- package/LICENSE +21 -0
- package/README.md +315 -2
- package/dist/chunk-SCUPIBTS.js +501 -0
- package/dist/cli.cjs +766 -0
- package/dist/cli.js +266 -0
- package/dist/index.cjs +719 -0
- package/dist/index.d.cts +301 -0
- package/dist/index.d.ts +301 -0
- package/dist/index.js +204 -0
- package/package.json +71 -4
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
/** The three Standard Webhooks headers. */
|
|
2
|
+
interface SignatureHeaders {
|
|
3
|
+
"webhook-id": string;
|
|
4
|
+
"webhook-timestamp": string;
|
|
5
|
+
"webhook-signature": string;
|
|
6
|
+
}
|
|
7
|
+
interface SignOptions {
|
|
8
|
+
/** Message id, sent as `webhook-id`. Default: a new `msg_…` id. */
|
|
9
|
+
id?: string;
|
|
10
|
+
/** Signing time as a Date or epoch seconds. Default: now. */
|
|
11
|
+
timestamp?: Date | number;
|
|
12
|
+
/** The exact request body that will be sent. */
|
|
13
|
+
body: string | Uint8Array;
|
|
14
|
+
/** Signing secret (`whsec_…` or plain base64). Pass an array to sign with several (key rotation). */
|
|
15
|
+
secret?: string | string[];
|
|
16
|
+
/** Same as `secret` with an array; both are merged. */
|
|
17
|
+
secrets?: string[];
|
|
18
|
+
}
|
|
19
|
+
/** Creates a new signing secret: `whsec_` + base64 of 32 random bytes. */
|
|
20
|
+
declare function generateSecret(): string;
|
|
21
|
+
/** Creates a unique message id: `msg_` + 24 random letters and digits (about 143 bits). */
|
|
22
|
+
declare function createMessageId(): string;
|
|
23
|
+
/** Decodes a `whsec_…` (or plain base64) secret into its key bytes. Throws on invalid input. */
|
|
24
|
+
declare function decodeSecret(secret: string): Buffer;
|
|
25
|
+
/**
|
|
26
|
+
* Signs a webhook following the Standard Webhooks spec
|
|
27
|
+
* (HMAC-SHA256 over `id.timestamp.body`) and returns the headers to send.
|
|
28
|
+
*/
|
|
29
|
+
declare function sign(options: SignOptions): SignatureHeaders;
|
|
30
|
+
|
|
31
|
+
/** One resolved address. */
|
|
32
|
+
interface ResolvedAddress {
|
|
33
|
+
address: string;
|
|
34
|
+
family: number;
|
|
35
|
+
}
|
|
36
|
+
/** Resolves a hostname to every address it points at. */
|
|
37
|
+
type Resolver = (hostname: string) => Promise<ResolvedAddress[]>;
|
|
38
|
+
interface NetworkPolicy {
|
|
39
|
+
/** Allow private, loopback and link-local targets. Default: false */
|
|
40
|
+
allowPrivateNetworks?: boolean;
|
|
41
|
+
/** Hostnames or IPs that skip the private-address check (e.g. for local testing). */
|
|
42
|
+
allowHosts?: string[];
|
|
43
|
+
/** Custom DNS resolver (mainly for tests). Default: `dns.lookup` with `all: true`. */
|
|
44
|
+
resolver?: Resolver;
|
|
45
|
+
}
|
|
46
|
+
/** Thrown (as a request error) when a target is refused by the SSRF check. */
|
|
47
|
+
declare class BlockedError extends Error {
|
|
48
|
+
readonly code = "HOOKSEND_BLOCKED";
|
|
49
|
+
constructor(message: string);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* True when `ip` is not a public unicast address: loopback, private, link-local
|
|
53
|
+
* (including cloud metadata 169.254.169.254), CGNAT, multicast, reserved, or
|
|
54
|
+
* an IPv6 form of one of those. Anything that is not a valid IP also returns
|
|
55
|
+
* true, so callers fail closed.
|
|
56
|
+
*/
|
|
57
|
+
declare function isPrivateAddress(ip: string): boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Checks a URL before any connection is made: only http/https, and IP-literal
|
|
60
|
+
* hosts must be public (unless allowed). Hostnames are checked later, at
|
|
61
|
+
* connect time, by the lookup from `createSafeLookup`. Throws `BlockedError`.
|
|
62
|
+
*/
|
|
63
|
+
declare function checkUrl(url: URL, policy?: NetworkPolicy): void;
|
|
64
|
+
type LookupCallback = (err: NodeJS.ErrnoException | null, address: string | ResolvedAddress[], family?: number) => void;
|
|
65
|
+
/**
|
|
66
|
+
* Builds a `lookup` function for `http.request` that resolves the hostname and
|
|
67
|
+
* refuses the connection if ANY resolved address is private. Because the check
|
|
68
|
+
* runs on the addresses actually used to connect, DNS rebinding cannot slip a
|
|
69
|
+
* private IP past an earlier check.
|
|
70
|
+
*/
|
|
71
|
+
declare function createSafeLookup(policy?: NetworkPolicy): (hostname: string, options: unknown, callback: LookupCallback) => void;
|
|
72
|
+
|
|
73
|
+
/** Where a webhook goes. */
|
|
74
|
+
interface Endpoint {
|
|
75
|
+
/** The receiver URL (http or https). */
|
|
76
|
+
url: string;
|
|
77
|
+
/** Signing secret, or several during key rotation (each one signs). */
|
|
78
|
+
secret: string | string[];
|
|
79
|
+
/** Extra request headers for this endpoint. */
|
|
80
|
+
headers?: Record<string, string>;
|
|
81
|
+
}
|
|
82
|
+
/** What to send. */
|
|
83
|
+
interface Message {
|
|
84
|
+
/** Event type, e.g. `"order.paid"`. */
|
|
85
|
+
type?: string;
|
|
86
|
+
/** Event payload. Sent as `{ type, timestamp, data }` JSON. */
|
|
87
|
+
data?: unknown;
|
|
88
|
+
/** Message id (`webhook-id`). Default: a new `msg_…` id. Keep it stable across retries. */
|
|
89
|
+
id?: string;
|
|
90
|
+
/** Event time used in the JSON body (Date, ISO string or epoch ms). Default: now. */
|
|
91
|
+
timestamp?: Date | string | number;
|
|
92
|
+
/** Send this exact body instead of the JSON envelope. */
|
|
93
|
+
body?: string | Uint8Array;
|
|
94
|
+
}
|
|
95
|
+
type AttemptErrorType = "timeout" | "network" | "blocked" | "aborted" | "invalid";
|
|
96
|
+
/** The outcome of one HTTP attempt. */
|
|
97
|
+
interface AttemptResult {
|
|
98
|
+
/** True for a 2xx response. */
|
|
99
|
+
ok: boolean;
|
|
100
|
+
/** HTTP status, or `null` when no response was received. */
|
|
101
|
+
status: number | null;
|
|
102
|
+
/** HTTP status text, e.g. `"Service Unavailable"`. */
|
|
103
|
+
statusText?: string;
|
|
104
|
+
/** Time from start of the attempt to the response (or error), in ms. */
|
|
105
|
+
durationMs: number;
|
|
106
|
+
/** Attempt number, starting at 1. */
|
|
107
|
+
attempt: number;
|
|
108
|
+
/** Target URL. */
|
|
109
|
+
url: string;
|
|
110
|
+
/** Message id sent as `webhook-id`. */
|
|
111
|
+
id: string;
|
|
112
|
+
/** Why the attempt failed (network error, timeout, redirect, block, status text). */
|
|
113
|
+
error?: string;
|
|
114
|
+
/** Category of a failure without a usable response. */
|
|
115
|
+
errorType?: AttemptErrorType;
|
|
116
|
+
/** First 2 KB of the response body. */
|
|
117
|
+
responseBody?: string;
|
|
118
|
+
/** Parsed `Retry-After` header, in ms. */
|
|
119
|
+
retryAfterMs?: number;
|
|
120
|
+
/** Set by `deliver()` when another attempt follows: the wait before it, in ms. */
|
|
121
|
+
retryInMs?: number;
|
|
122
|
+
}
|
|
123
|
+
interface SendOptions extends NetworkPolicy {
|
|
124
|
+
/** Per-attempt timeout in ms. Default: 15000 */
|
|
125
|
+
timeoutMs?: number;
|
|
126
|
+
/** Extra headers (merged under the endpoint's own headers). */
|
|
127
|
+
headers?: Record<string, string>;
|
|
128
|
+
/** Attempt number reported in the result. Default: 1 */
|
|
129
|
+
attempt?: number;
|
|
130
|
+
/** Cancels the request. */
|
|
131
|
+
signal?: AbortSignal;
|
|
132
|
+
/** Clock in epoch ms, used for the signature timestamp. Default: `Date.now` */
|
|
133
|
+
now?: () => number;
|
|
134
|
+
}
|
|
135
|
+
/** A message ready to send: fixed id and body bytes. */
|
|
136
|
+
interface PreparedMessage {
|
|
137
|
+
id: string;
|
|
138
|
+
body: Uint8Array;
|
|
139
|
+
}
|
|
140
|
+
declare const DEFAULT_TIMEOUT_MS = 15000;
|
|
141
|
+
/** Builds the id and body once, so every retry sends the same bytes. */
|
|
142
|
+
declare function prepareMessage(message: Message, now?: () => number): PreparedMessage;
|
|
143
|
+
/** Parses a `Retry-After` value (seconds or HTTP date) into ms from now. */
|
|
144
|
+
declare function parseRetryAfter(value: string | undefined, now?: number): number | undefined;
|
|
145
|
+
/**
|
|
146
|
+
* Sends one signed webhook request. Never throws for network or HTTP
|
|
147
|
+
* failures: the result says what happened. Redirects are never followed.
|
|
148
|
+
*/
|
|
149
|
+
declare function sendOnce(endpoint: Endpoint, message: Message | PreparedMessage, options?: SendOptions): Promise<AttemptResult>;
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Default wait before each attempt (like Svix): immediately, 5s, 5m, 30m, 2h,
|
|
153
|
+
* 5h, 10h, 10h. Eight attempts over about 27.5 hours.
|
|
154
|
+
*/
|
|
155
|
+
declare const DEFAULT_SCHEDULE: readonly number[];
|
|
156
|
+
declare const DEFAULT_JITTER = 0.2;
|
|
157
|
+
declare const DEFAULT_MAX_RETRY_AFTER_MS: number;
|
|
158
|
+
type Sleep = (ms: number, signal?: AbortSignal) => Promise<void>;
|
|
159
|
+
interface DeliverOptions extends Omit<SendOptions, "attempt"> {
|
|
160
|
+
/** Wait in ms before each attempt; its length is the maximum number of attempts. Default: `DEFAULT_SCHEDULE` */
|
|
161
|
+
schedule?: readonly number[];
|
|
162
|
+
/** Random spread applied to each wait, as a fraction (0.2 = ±20%). Default: 0.2 */
|
|
163
|
+
jitter?: number;
|
|
164
|
+
/** Decides whether a failed attempt is retried. Default: `shouldRetry` */
|
|
165
|
+
retryOn?: (result: AttemptResult) => boolean;
|
|
166
|
+
/** Upper limit for a `Retry-After` wait, in ms. Default: 3600000 (1 hour) */
|
|
167
|
+
maxRetryAfterMs?: number;
|
|
168
|
+
/** Called after every attempt (with `retryInMs` set when another attempt follows). */
|
|
169
|
+
onAttempt?: (result: AttemptResult) => void;
|
|
170
|
+
/** Wait function (inject a fake one in tests). Default: a `setTimeout` that the signal cancels. */
|
|
171
|
+
sleep?: Sleep;
|
|
172
|
+
/** Random source for jitter. Default: `Math.random` */
|
|
173
|
+
random?: () => number;
|
|
174
|
+
/** Start from this attempt number (to resume a stored job). The first attempt is then sent right away. Default: 1 */
|
|
175
|
+
startAttempt?: number;
|
|
176
|
+
}
|
|
177
|
+
/** The outcome of `deliver()`. */
|
|
178
|
+
interface DeliveryResult {
|
|
179
|
+
/** True when an attempt got a 2xx response. */
|
|
180
|
+
delivered: boolean;
|
|
181
|
+
/** True when the endpoint answered `410 Gone`: stop sending to it. */
|
|
182
|
+
disabled: boolean;
|
|
183
|
+
/** True when the signal stopped the delivery. */
|
|
184
|
+
aborted: boolean;
|
|
185
|
+
/** Message id sent as `webhook-id`. */
|
|
186
|
+
id: string;
|
|
187
|
+
/** Every attempt, in order. */
|
|
188
|
+
attempts: AttemptResult[];
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Default retry rule: retry network errors, timeouts, 408, 429 and 5xx.
|
|
192
|
+
* Never retry other 4xx, redirects, blocked or invalid URLs.
|
|
193
|
+
*/
|
|
194
|
+
declare function shouldRetry(result: AttemptResult): boolean;
|
|
195
|
+
/** Applies ±`jitter` random spread to a wait. */
|
|
196
|
+
declare function applyJitter(ms: number, jitter?: number, random?: () => number): number;
|
|
197
|
+
/**
|
|
198
|
+
* Delivers a webhook with retries: signs every attempt, retries network
|
|
199
|
+
* errors, timeouts, 408, 429 and 5xx on the schedule, honours `Retry-After`,
|
|
200
|
+
* and stops on `410 Gone` (`disabled: true`). Never throws for delivery
|
|
201
|
+
* failures.
|
|
202
|
+
*/
|
|
203
|
+
declare function deliver(endpoint: Endpoint, message: Message, options?: DeliverOptions): Promise<DeliveryResult>;
|
|
204
|
+
|
|
205
|
+
/** A pending delivery to one endpoint, as kept in a store. */
|
|
206
|
+
interface DeliveryJob {
|
|
207
|
+
/** Job id: `<messageId>:<endpoint index>`. */
|
|
208
|
+
id: string;
|
|
209
|
+
/** Message id sent as `webhook-id`. */
|
|
210
|
+
messageId: string;
|
|
211
|
+
/** Event type. */
|
|
212
|
+
type: string;
|
|
213
|
+
/** The exact JSON body (identical on every retry). */
|
|
214
|
+
body: string;
|
|
215
|
+
/** Target endpoint, including its secret. */
|
|
216
|
+
endpoint: Endpoint;
|
|
217
|
+
/** The next attempt number (1 = not tried yet). */
|
|
218
|
+
attempt: number;
|
|
219
|
+
/** When the next attempt is due, epoch ms. */
|
|
220
|
+
nextAttemptAt: number;
|
|
221
|
+
/** When the job was created, epoch ms. */
|
|
222
|
+
createdAt: number;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Where pending jobs live. The default keeps them in memory; implement this
|
|
226
|
+
* with Redis or a database so retries survive a restart.
|
|
227
|
+
*/
|
|
228
|
+
interface DeliveryStore {
|
|
229
|
+
/** Insert or update a job. */
|
|
230
|
+
save(job: DeliveryJob): void | Promise<void>;
|
|
231
|
+
/** Delete a finished job (delivered, failed for good, or disabled). */
|
|
232
|
+
remove(id: string): void | Promise<void>;
|
|
233
|
+
/** Jobs whose `nextAttemptAt` is at or before `now`. */
|
|
234
|
+
due(now: number): DeliveryJob[] | Promise<DeliveryJob[]>;
|
|
235
|
+
}
|
|
236
|
+
/** An in-memory `DeliveryStore` (the default). Jobs are lost when the process exits. */
|
|
237
|
+
declare class MemoryStore implements DeliveryStore {
|
|
238
|
+
readonly jobs: Map<string, DeliveryJob>;
|
|
239
|
+
save(job: DeliveryJob): void;
|
|
240
|
+
remove(id: string): void;
|
|
241
|
+
due(now: number): DeliveryJob[];
|
|
242
|
+
}
|
|
243
|
+
/** The outcome for one endpoint. */
|
|
244
|
+
interface EndpointResult extends DeliveryResult {
|
|
245
|
+
/** Endpoint URL. */
|
|
246
|
+
url: string;
|
|
247
|
+
/** Job id in the store. */
|
|
248
|
+
jobId: string;
|
|
249
|
+
/** Set when the store failed and delivery stopped. */
|
|
250
|
+
error?: string;
|
|
251
|
+
}
|
|
252
|
+
interface DispatchResult {
|
|
253
|
+
/** Message id sent to every endpoint. */
|
|
254
|
+
id: string;
|
|
255
|
+
/** One result per endpoint, in the same order. */
|
|
256
|
+
results: EndpointResult[];
|
|
257
|
+
}
|
|
258
|
+
interface DispatchInput {
|
|
259
|
+
/** Event type, e.g. `"order.paid"`. */
|
|
260
|
+
type: string;
|
|
261
|
+
/** Event payload. */
|
|
262
|
+
data?: unknown;
|
|
263
|
+
/** Message id. Default: a new `msg_…` id. */
|
|
264
|
+
id?: string;
|
|
265
|
+
/** Event time used in the body (Date, ISO string or epoch ms). Default: now. */
|
|
266
|
+
timestamp?: Date | string | number;
|
|
267
|
+
/** Every subscriber that should get this event. */
|
|
268
|
+
endpoints: Endpoint[];
|
|
269
|
+
}
|
|
270
|
+
interface DispatcherOptions extends Omit<DeliverOptions, "onAttempt" | "signal" | "startAttempt"> {
|
|
271
|
+
/** Maximum HTTP requests in flight at once (waits between retries do not count). Default: 10 */
|
|
272
|
+
concurrency?: number;
|
|
273
|
+
/** Where pending jobs are kept. Default: a new `MemoryStore` */
|
|
274
|
+
store?: DeliveryStore;
|
|
275
|
+
onAttempt?: (result: AttemptResult) => void;
|
|
276
|
+
onDelivered?: (result: EndpointResult) => void;
|
|
277
|
+
onFailed?: (result: EndpointResult) => void;
|
|
278
|
+
onDisabled?: (result: EndpointResult) => void;
|
|
279
|
+
}
|
|
280
|
+
interface Dispatcher {
|
|
281
|
+
/** Delivers one event to every endpoint in parallel, each with its own retries. */
|
|
282
|
+
dispatch(input: DispatchInput): Promise<DispatchResult>;
|
|
283
|
+
/** Runs the store's due jobs (call on startup with a persistent store). */
|
|
284
|
+
resume(): Promise<EndpointResult[]>;
|
|
285
|
+
/** Resolves when every delivery in progress (including retries) has finished. */
|
|
286
|
+
drain(): Promise<void>;
|
|
287
|
+
/** Stops accepting work, cancels pending retry waits (jobs stay in the store) and waits for requests in flight. */
|
|
288
|
+
close(): Promise<void>;
|
|
289
|
+
/** Number of endpoint deliveries in progress. */
|
|
290
|
+
readonly pending: number;
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Creates a dispatcher that fans events out to many endpoints with a shared
|
|
294
|
+
* concurrency limit. Pending retries live in `store` (in memory by default).
|
|
295
|
+
*/
|
|
296
|
+
declare function createDispatcher(options?: DispatcherOptions): Dispatcher;
|
|
297
|
+
|
|
298
|
+
/** The package version (injected at build time). */
|
|
299
|
+
declare const VERSION: string;
|
|
300
|
+
|
|
301
|
+
export { type AttemptErrorType, type AttemptResult, BlockedError, DEFAULT_JITTER, DEFAULT_MAX_RETRY_AFTER_MS, DEFAULT_SCHEDULE, DEFAULT_TIMEOUT_MS, type DeliverOptions, type DeliveryJob, type DeliveryResult, type DeliveryStore, type DispatchInput, type DispatchResult, type Dispatcher, type DispatcherOptions, type Endpoint, type EndpointResult, MemoryStore, type Message, type NetworkPolicy, type PreparedMessage, type ResolvedAddress, type Resolver, type SendOptions, type SignOptions, type SignatureHeaders, type Sleep, VERSION, applyJitter, checkUrl, createDispatcher, createMessageId, createSafeLookup, decodeSecret, deliver, generateSecret, isPrivateAddress, parseRetryAfter, prepareMessage, sendOnce, shouldRetry, sign };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
/** The three Standard Webhooks headers. */
|
|
2
|
+
interface SignatureHeaders {
|
|
3
|
+
"webhook-id": string;
|
|
4
|
+
"webhook-timestamp": string;
|
|
5
|
+
"webhook-signature": string;
|
|
6
|
+
}
|
|
7
|
+
interface SignOptions {
|
|
8
|
+
/** Message id, sent as `webhook-id`. Default: a new `msg_…` id. */
|
|
9
|
+
id?: string;
|
|
10
|
+
/** Signing time as a Date or epoch seconds. Default: now. */
|
|
11
|
+
timestamp?: Date | number;
|
|
12
|
+
/** The exact request body that will be sent. */
|
|
13
|
+
body: string | Uint8Array;
|
|
14
|
+
/** Signing secret (`whsec_…` or plain base64). Pass an array to sign with several (key rotation). */
|
|
15
|
+
secret?: string | string[];
|
|
16
|
+
/** Same as `secret` with an array; both are merged. */
|
|
17
|
+
secrets?: string[];
|
|
18
|
+
}
|
|
19
|
+
/** Creates a new signing secret: `whsec_` + base64 of 32 random bytes. */
|
|
20
|
+
declare function generateSecret(): string;
|
|
21
|
+
/** Creates a unique message id: `msg_` + 24 random letters and digits (about 143 bits). */
|
|
22
|
+
declare function createMessageId(): string;
|
|
23
|
+
/** Decodes a `whsec_…` (or plain base64) secret into its key bytes. Throws on invalid input. */
|
|
24
|
+
declare function decodeSecret(secret: string): Buffer;
|
|
25
|
+
/**
|
|
26
|
+
* Signs a webhook following the Standard Webhooks spec
|
|
27
|
+
* (HMAC-SHA256 over `id.timestamp.body`) and returns the headers to send.
|
|
28
|
+
*/
|
|
29
|
+
declare function sign(options: SignOptions): SignatureHeaders;
|
|
30
|
+
|
|
31
|
+
/** One resolved address. */
|
|
32
|
+
interface ResolvedAddress {
|
|
33
|
+
address: string;
|
|
34
|
+
family: number;
|
|
35
|
+
}
|
|
36
|
+
/** Resolves a hostname to every address it points at. */
|
|
37
|
+
type Resolver = (hostname: string) => Promise<ResolvedAddress[]>;
|
|
38
|
+
interface NetworkPolicy {
|
|
39
|
+
/** Allow private, loopback and link-local targets. Default: false */
|
|
40
|
+
allowPrivateNetworks?: boolean;
|
|
41
|
+
/** Hostnames or IPs that skip the private-address check (e.g. for local testing). */
|
|
42
|
+
allowHosts?: string[];
|
|
43
|
+
/** Custom DNS resolver (mainly for tests). Default: `dns.lookup` with `all: true`. */
|
|
44
|
+
resolver?: Resolver;
|
|
45
|
+
}
|
|
46
|
+
/** Thrown (as a request error) when a target is refused by the SSRF check. */
|
|
47
|
+
declare class BlockedError extends Error {
|
|
48
|
+
readonly code = "HOOKSEND_BLOCKED";
|
|
49
|
+
constructor(message: string);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* True when `ip` is not a public unicast address: loopback, private, link-local
|
|
53
|
+
* (including cloud metadata 169.254.169.254), CGNAT, multicast, reserved, or
|
|
54
|
+
* an IPv6 form of one of those. Anything that is not a valid IP also returns
|
|
55
|
+
* true, so callers fail closed.
|
|
56
|
+
*/
|
|
57
|
+
declare function isPrivateAddress(ip: string): boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Checks a URL before any connection is made: only http/https, and IP-literal
|
|
60
|
+
* hosts must be public (unless allowed). Hostnames are checked later, at
|
|
61
|
+
* connect time, by the lookup from `createSafeLookup`. Throws `BlockedError`.
|
|
62
|
+
*/
|
|
63
|
+
declare function checkUrl(url: URL, policy?: NetworkPolicy): void;
|
|
64
|
+
type LookupCallback = (err: NodeJS.ErrnoException | null, address: string | ResolvedAddress[], family?: number) => void;
|
|
65
|
+
/**
|
|
66
|
+
* Builds a `lookup` function for `http.request` that resolves the hostname and
|
|
67
|
+
* refuses the connection if ANY resolved address is private. Because the check
|
|
68
|
+
* runs on the addresses actually used to connect, DNS rebinding cannot slip a
|
|
69
|
+
* private IP past an earlier check.
|
|
70
|
+
*/
|
|
71
|
+
declare function createSafeLookup(policy?: NetworkPolicy): (hostname: string, options: unknown, callback: LookupCallback) => void;
|
|
72
|
+
|
|
73
|
+
/** Where a webhook goes. */
|
|
74
|
+
interface Endpoint {
|
|
75
|
+
/** The receiver URL (http or https). */
|
|
76
|
+
url: string;
|
|
77
|
+
/** Signing secret, or several during key rotation (each one signs). */
|
|
78
|
+
secret: string | string[];
|
|
79
|
+
/** Extra request headers for this endpoint. */
|
|
80
|
+
headers?: Record<string, string>;
|
|
81
|
+
}
|
|
82
|
+
/** What to send. */
|
|
83
|
+
interface Message {
|
|
84
|
+
/** Event type, e.g. `"order.paid"`. */
|
|
85
|
+
type?: string;
|
|
86
|
+
/** Event payload. Sent as `{ type, timestamp, data }` JSON. */
|
|
87
|
+
data?: unknown;
|
|
88
|
+
/** Message id (`webhook-id`). Default: a new `msg_…` id. Keep it stable across retries. */
|
|
89
|
+
id?: string;
|
|
90
|
+
/** Event time used in the JSON body (Date, ISO string or epoch ms). Default: now. */
|
|
91
|
+
timestamp?: Date | string | number;
|
|
92
|
+
/** Send this exact body instead of the JSON envelope. */
|
|
93
|
+
body?: string | Uint8Array;
|
|
94
|
+
}
|
|
95
|
+
type AttemptErrorType = "timeout" | "network" | "blocked" | "aborted" | "invalid";
|
|
96
|
+
/** The outcome of one HTTP attempt. */
|
|
97
|
+
interface AttemptResult {
|
|
98
|
+
/** True for a 2xx response. */
|
|
99
|
+
ok: boolean;
|
|
100
|
+
/** HTTP status, or `null` when no response was received. */
|
|
101
|
+
status: number | null;
|
|
102
|
+
/** HTTP status text, e.g. `"Service Unavailable"`. */
|
|
103
|
+
statusText?: string;
|
|
104
|
+
/** Time from start of the attempt to the response (or error), in ms. */
|
|
105
|
+
durationMs: number;
|
|
106
|
+
/** Attempt number, starting at 1. */
|
|
107
|
+
attempt: number;
|
|
108
|
+
/** Target URL. */
|
|
109
|
+
url: string;
|
|
110
|
+
/** Message id sent as `webhook-id`. */
|
|
111
|
+
id: string;
|
|
112
|
+
/** Why the attempt failed (network error, timeout, redirect, block, status text). */
|
|
113
|
+
error?: string;
|
|
114
|
+
/** Category of a failure without a usable response. */
|
|
115
|
+
errorType?: AttemptErrorType;
|
|
116
|
+
/** First 2 KB of the response body. */
|
|
117
|
+
responseBody?: string;
|
|
118
|
+
/** Parsed `Retry-After` header, in ms. */
|
|
119
|
+
retryAfterMs?: number;
|
|
120
|
+
/** Set by `deliver()` when another attempt follows: the wait before it, in ms. */
|
|
121
|
+
retryInMs?: number;
|
|
122
|
+
}
|
|
123
|
+
interface SendOptions extends NetworkPolicy {
|
|
124
|
+
/** Per-attempt timeout in ms. Default: 15000 */
|
|
125
|
+
timeoutMs?: number;
|
|
126
|
+
/** Extra headers (merged under the endpoint's own headers). */
|
|
127
|
+
headers?: Record<string, string>;
|
|
128
|
+
/** Attempt number reported in the result. Default: 1 */
|
|
129
|
+
attempt?: number;
|
|
130
|
+
/** Cancels the request. */
|
|
131
|
+
signal?: AbortSignal;
|
|
132
|
+
/** Clock in epoch ms, used for the signature timestamp. Default: `Date.now` */
|
|
133
|
+
now?: () => number;
|
|
134
|
+
}
|
|
135
|
+
/** A message ready to send: fixed id and body bytes. */
|
|
136
|
+
interface PreparedMessage {
|
|
137
|
+
id: string;
|
|
138
|
+
body: Uint8Array;
|
|
139
|
+
}
|
|
140
|
+
declare const DEFAULT_TIMEOUT_MS = 15000;
|
|
141
|
+
/** Builds the id and body once, so every retry sends the same bytes. */
|
|
142
|
+
declare function prepareMessage(message: Message, now?: () => number): PreparedMessage;
|
|
143
|
+
/** Parses a `Retry-After` value (seconds or HTTP date) into ms from now. */
|
|
144
|
+
declare function parseRetryAfter(value: string | undefined, now?: number): number | undefined;
|
|
145
|
+
/**
|
|
146
|
+
* Sends one signed webhook request. Never throws for network or HTTP
|
|
147
|
+
* failures: the result says what happened. Redirects are never followed.
|
|
148
|
+
*/
|
|
149
|
+
declare function sendOnce(endpoint: Endpoint, message: Message | PreparedMessage, options?: SendOptions): Promise<AttemptResult>;
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Default wait before each attempt (like Svix): immediately, 5s, 5m, 30m, 2h,
|
|
153
|
+
* 5h, 10h, 10h. Eight attempts over about 27.5 hours.
|
|
154
|
+
*/
|
|
155
|
+
declare const DEFAULT_SCHEDULE: readonly number[];
|
|
156
|
+
declare const DEFAULT_JITTER = 0.2;
|
|
157
|
+
declare const DEFAULT_MAX_RETRY_AFTER_MS: number;
|
|
158
|
+
type Sleep = (ms: number, signal?: AbortSignal) => Promise<void>;
|
|
159
|
+
interface DeliverOptions extends Omit<SendOptions, "attempt"> {
|
|
160
|
+
/** Wait in ms before each attempt; its length is the maximum number of attempts. Default: `DEFAULT_SCHEDULE` */
|
|
161
|
+
schedule?: readonly number[];
|
|
162
|
+
/** Random spread applied to each wait, as a fraction (0.2 = ±20%). Default: 0.2 */
|
|
163
|
+
jitter?: number;
|
|
164
|
+
/** Decides whether a failed attempt is retried. Default: `shouldRetry` */
|
|
165
|
+
retryOn?: (result: AttemptResult) => boolean;
|
|
166
|
+
/** Upper limit for a `Retry-After` wait, in ms. Default: 3600000 (1 hour) */
|
|
167
|
+
maxRetryAfterMs?: number;
|
|
168
|
+
/** Called after every attempt (with `retryInMs` set when another attempt follows). */
|
|
169
|
+
onAttempt?: (result: AttemptResult) => void;
|
|
170
|
+
/** Wait function (inject a fake one in tests). Default: a `setTimeout` that the signal cancels. */
|
|
171
|
+
sleep?: Sleep;
|
|
172
|
+
/** Random source for jitter. Default: `Math.random` */
|
|
173
|
+
random?: () => number;
|
|
174
|
+
/** Start from this attempt number (to resume a stored job). The first attempt is then sent right away. Default: 1 */
|
|
175
|
+
startAttempt?: number;
|
|
176
|
+
}
|
|
177
|
+
/** The outcome of `deliver()`. */
|
|
178
|
+
interface DeliveryResult {
|
|
179
|
+
/** True when an attempt got a 2xx response. */
|
|
180
|
+
delivered: boolean;
|
|
181
|
+
/** True when the endpoint answered `410 Gone`: stop sending to it. */
|
|
182
|
+
disabled: boolean;
|
|
183
|
+
/** True when the signal stopped the delivery. */
|
|
184
|
+
aborted: boolean;
|
|
185
|
+
/** Message id sent as `webhook-id`. */
|
|
186
|
+
id: string;
|
|
187
|
+
/** Every attempt, in order. */
|
|
188
|
+
attempts: AttemptResult[];
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Default retry rule: retry network errors, timeouts, 408, 429 and 5xx.
|
|
192
|
+
* Never retry other 4xx, redirects, blocked or invalid URLs.
|
|
193
|
+
*/
|
|
194
|
+
declare function shouldRetry(result: AttemptResult): boolean;
|
|
195
|
+
/** Applies ±`jitter` random spread to a wait. */
|
|
196
|
+
declare function applyJitter(ms: number, jitter?: number, random?: () => number): number;
|
|
197
|
+
/**
|
|
198
|
+
* Delivers a webhook with retries: signs every attempt, retries network
|
|
199
|
+
* errors, timeouts, 408, 429 and 5xx on the schedule, honours `Retry-After`,
|
|
200
|
+
* and stops on `410 Gone` (`disabled: true`). Never throws for delivery
|
|
201
|
+
* failures.
|
|
202
|
+
*/
|
|
203
|
+
declare function deliver(endpoint: Endpoint, message: Message, options?: DeliverOptions): Promise<DeliveryResult>;
|
|
204
|
+
|
|
205
|
+
/** A pending delivery to one endpoint, as kept in a store. */
|
|
206
|
+
interface DeliveryJob {
|
|
207
|
+
/** Job id: `<messageId>:<endpoint index>`. */
|
|
208
|
+
id: string;
|
|
209
|
+
/** Message id sent as `webhook-id`. */
|
|
210
|
+
messageId: string;
|
|
211
|
+
/** Event type. */
|
|
212
|
+
type: string;
|
|
213
|
+
/** The exact JSON body (identical on every retry). */
|
|
214
|
+
body: string;
|
|
215
|
+
/** Target endpoint, including its secret. */
|
|
216
|
+
endpoint: Endpoint;
|
|
217
|
+
/** The next attempt number (1 = not tried yet). */
|
|
218
|
+
attempt: number;
|
|
219
|
+
/** When the next attempt is due, epoch ms. */
|
|
220
|
+
nextAttemptAt: number;
|
|
221
|
+
/** When the job was created, epoch ms. */
|
|
222
|
+
createdAt: number;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Where pending jobs live. The default keeps them in memory; implement this
|
|
226
|
+
* with Redis or a database so retries survive a restart.
|
|
227
|
+
*/
|
|
228
|
+
interface DeliveryStore {
|
|
229
|
+
/** Insert or update a job. */
|
|
230
|
+
save(job: DeliveryJob): void | Promise<void>;
|
|
231
|
+
/** Delete a finished job (delivered, failed for good, or disabled). */
|
|
232
|
+
remove(id: string): void | Promise<void>;
|
|
233
|
+
/** Jobs whose `nextAttemptAt` is at or before `now`. */
|
|
234
|
+
due(now: number): DeliveryJob[] | Promise<DeliveryJob[]>;
|
|
235
|
+
}
|
|
236
|
+
/** An in-memory `DeliveryStore` (the default). Jobs are lost when the process exits. */
|
|
237
|
+
declare class MemoryStore implements DeliveryStore {
|
|
238
|
+
readonly jobs: Map<string, DeliveryJob>;
|
|
239
|
+
save(job: DeliveryJob): void;
|
|
240
|
+
remove(id: string): void;
|
|
241
|
+
due(now: number): DeliveryJob[];
|
|
242
|
+
}
|
|
243
|
+
/** The outcome for one endpoint. */
|
|
244
|
+
interface EndpointResult extends DeliveryResult {
|
|
245
|
+
/** Endpoint URL. */
|
|
246
|
+
url: string;
|
|
247
|
+
/** Job id in the store. */
|
|
248
|
+
jobId: string;
|
|
249
|
+
/** Set when the store failed and delivery stopped. */
|
|
250
|
+
error?: string;
|
|
251
|
+
}
|
|
252
|
+
interface DispatchResult {
|
|
253
|
+
/** Message id sent to every endpoint. */
|
|
254
|
+
id: string;
|
|
255
|
+
/** One result per endpoint, in the same order. */
|
|
256
|
+
results: EndpointResult[];
|
|
257
|
+
}
|
|
258
|
+
interface DispatchInput {
|
|
259
|
+
/** Event type, e.g. `"order.paid"`. */
|
|
260
|
+
type: string;
|
|
261
|
+
/** Event payload. */
|
|
262
|
+
data?: unknown;
|
|
263
|
+
/** Message id. Default: a new `msg_…` id. */
|
|
264
|
+
id?: string;
|
|
265
|
+
/** Event time used in the body (Date, ISO string or epoch ms). Default: now. */
|
|
266
|
+
timestamp?: Date | string | number;
|
|
267
|
+
/** Every subscriber that should get this event. */
|
|
268
|
+
endpoints: Endpoint[];
|
|
269
|
+
}
|
|
270
|
+
interface DispatcherOptions extends Omit<DeliverOptions, "onAttempt" | "signal" | "startAttempt"> {
|
|
271
|
+
/** Maximum HTTP requests in flight at once (waits between retries do not count). Default: 10 */
|
|
272
|
+
concurrency?: number;
|
|
273
|
+
/** Where pending jobs are kept. Default: a new `MemoryStore` */
|
|
274
|
+
store?: DeliveryStore;
|
|
275
|
+
onAttempt?: (result: AttemptResult) => void;
|
|
276
|
+
onDelivered?: (result: EndpointResult) => void;
|
|
277
|
+
onFailed?: (result: EndpointResult) => void;
|
|
278
|
+
onDisabled?: (result: EndpointResult) => void;
|
|
279
|
+
}
|
|
280
|
+
interface Dispatcher {
|
|
281
|
+
/** Delivers one event to every endpoint in parallel, each with its own retries. */
|
|
282
|
+
dispatch(input: DispatchInput): Promise<DispatchResult>;
|
|
283
|
+
/** Runs the store's due jobs (call on startup with a persistent store). */
|
|
284
|
+
resume(): Promise<EndpointResult[]>;
|
|
285
|
+
/** Resolves when every delivery in progress (including retries) has finished. */
|
|
286
|
+
drain(): Promise<void>;
|
|
287
|
+
/** Stops accepting work, cancels pending retry waits (jobs stay in the store) and waits for requests in flight. */
|
|
288
|
+
close(): Promise<void>;
|
|
289
|
+
/** Number of endpoint deliveries in progress. */
|
|
290
|
+
readonly pending: number;
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Creates a dispatcher that fans events out to many endpoints with a shared
|
|
294
|
+
* concurrency limit. Pending retries live in `store` (in memory by default).
|
|
295
|
+
*/
|
|
296
|
+
declare function createDispatcher(options?: DispatcherOptions): Dispatcher;
|
|
297
|
+
|
|
298
|
+
/** The package version (injected at build time). */
|
|
299
|
+
declare const VERSION: string;
|
|
300
|
+
|
|
301
|
+
export { type AttemptErrorType, type AttemptResult, BlockedError, DEFAULT_JITTER, DEFAULT_MAX_RETRY_AFTER_MS, DEFAULT_SCHEDULE, DEFAULT_TIMEOUT_MS, type DeliverOptions, type DeliveryJob, type DeliveryResult, type DeliveryStore, type DispatchInput, type DispatchResult, type Dispatcher, type DispatcherOptions, type Endpoint, type EndpointResult, MemoryStore, type Message, type NetworkPolicy, type PreparedMessage, type ResolvedAddress, type Resolver, type SendOptions, type SignOptions, type SignatureHeaders, type Sleep, VERSION, applyJitter, checkUrl, createDispatcher, createMessageId, createSafeLookup, decodeSecret, deliver, generateSecret, isPrivateAddress, parseRetryAfter, prepareMessage, sendOnce, shouldRetry, sign };
|