@zap-studio/webhooks 1.1.1 → 2.0.1

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 CHANGED
@@ -4,27 +4,43 @@ All notable changes to this project will be documented in this file.
4
4
 
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [2.0.1]
8
+
9
+ ### Fixed
10
+
11
+ The `@opentelemetry/api` peer dependency was published as the raw pnpm `catalog:` protocol string instead of a resolved version range, an invalid semver range. This release republishes with it resolved.
12
+
13
+ ## [2.0.0]
14
+
15
+ ### Added
16
+
17
+ Native OpenTelemetry support. Every `handle(request)` call now gets a `SERVER` delivery span. It reads the sender's `traceparent` header, so the delivery continues their trace instead of starting a new one. Each handler dispatch gets its own child `INTERNAL` span. A non-2xx response marks the delivery span `ERROR`. A thrown handler error is also recorded as an exception on the handler span. See [OpenTelemetry](https://www.zapstudio.dev/webhooks/opentelemetry).
18
+
19
+ ### Changed
20
+
21
+ **Breaking:** `@opentelemetry/api` is now a required peer dependency. It is small, has no side effects, and does nothing until an app registers a real SDK. So nothing changes at runtime if you don't set one up. But the package will not resolve unless it is installed: `npm install @opentelemetry/api`.
22
+
7
23
  ## [1.1.1]
8
24
 
9
25
  ### Changed
10
26
 
11
- `@zap-studio/logger` is now an optional peer dependency instead of a regular dependency. Every import from it is type-only (`import type { Logger }`), so it was never pulled in at runtime pass any object matching the `Logger` shape (including `pino`) with no install required. Existing consumers of `logger?: Logger` are unaffected.
27
+ `@zap-studio/logger` is now an optional peer dependency, not a regular one. Every import from it is type-only (`import type { Logger }`), so it was never loaded at runtime anyway. You can pass any object with the `Logger` shape (`pino` included) with no install needed. This does not affect existing use of `logger?: Logger`.
12
28
 
13
29
  ## [1.1.0]
14
30
 
15
31
  ### Added
16
32
 
17
- `WebhookRouter` (and `createWebhookRouter(...)`) gain an optional `logger?: Logger` option (from `@zap-studio/logger`). When provided, it logs each delivery attempt and handler dispatch at `debug`, and verification failures and unmatched routes at `warn`. Omitting it keeps zero logging overhead. See [Logging](https://www.zapstudio.dev/webhooks/logging).
33
+ `WebhookRouter` (and `createWebhookRouter(...)`) now take an optional `logger?: Logger` option, from `@zap-studio/logger`. When you pass one, it logs each delivery attempt and handler dispatch at `debug`, and logs verification failures and unmatched routes at `warn`. Leave it out, and there is no logging cost at all. See [Logging](https://www.zapstudio.dev/webhooks/logging).
18
34
 
19
35
  ## [1.0.0]
20
36
 
21
37
  ### Changed
22
38
 
23
- `WebhookRouter`'s stateless private static methods (`runBeforeHooks`, `runAfterHooks`, `createHandlerEntry`, `parseRequestBody`, `validatePayload`, `executeHandler`) are now module-level functions in `router.ts`. Internal-only change; the public API (`WebhookRouter`, `createWebhookRouter`, `.register()`, `.handle()`) is unaffected.
39
+ `WebhookRouter`'s stateless private static methods `runBeforeHooks`, `runAfterHooks`, `createHandlerEntry`, `parseRequestBody`, `validatePayload`, `executeHandler` are now plain functions at the module level in `router.ts`. This is internal only. The public API (`WebhookRouter`, `createWebhookRouter`, `.register()`, `.handle()`) does not change.
24
40
 
25
- HMAC signature verification decodes the incoming header's hex signature to bytes and compares it against the computed digest byte-for-byte, instead of hex-encoding the digest and comparing hex text. Behavior is unchanged for valid requests; this only affects internals (fewer bytes compared, and the header's hex is no longer case-normalized as text since decoding handles case natively).
41
+ HMAC signature verification now decodes the header's hex signature into bytes, and compares it to the computed digest byte by byte. Before, it turned the digest into hex text and compared that text instead. Behavior stays the same for valid requests. This only changes internals: fewer bytes get compared, and the header's hex case no longer needs normalizing as text, since decoding handles the case on its own.
26
42
 
27
- `constantTimeEquals` moved from `utils.ts` into `verify.ts` (its only consumer) and now compares `Uint8Array`s (bytes) instead of strings. Still exported from `@zap-studio/webhooks` and `@zap-studio/webhooks/verify`.
43
+ `constantTimeEquals` moved from `utils.ts` to `verify.ts`, its only user, and now compares `Uint8Array`s (bytes) instead of strings. It is still exported from `@zap-studio/webhooks` and `@zap-studio/webhooks/verify`.
28
44
 
29
45
  ### Removed
30
46
 
@@ -34,25 +50,25 @@ Removed the `./utils` subpath export.
34
50
 
35
51
  ### Changed
36
52
 
37
- The custom `NormalizedRequest`/`NormalizedResponse` contract is gone. `router.handle` now takes a standard Web API `Request` and returns a standard `Response`, so the router plugs directly into fetch-native runtimes (Bun, Deno, Cloudflare Workers, Next.js route handlers, Hono) with no adapter layer.
53
+ The custom `NormalizedRequest`/`NormalizedResponse` contract is gone. `router.handle` now takes a standard Web API `Request` and returns a standard `Response`. So the router works directly with fetch-native runtimes Bun, Deno, Cloudflare Workers, Next.js route handlers, Hono with no adapter layer needed.
38
54
 
39
55
  **Breaking changes:**
40
56
 
41
57
  - `handle(req: NormalizedRequest): Promise<NormalizedResponse>` → `handle(request: Request): Promise<Response>`.
42
- - Handlers receive `{ request, rawBody, path, payload }` (a `WebhookContext` plus the validated `payload`) and return a `Response` or `undefined` (default `200` `"ok"`). The `ack` helper is removed use `Response.json(body, init)`.
43
- - Hooks and `verify` are retyped against the context: `BeforeHook(ctx)`, `AfterHook(ctx, response)`, `ErrorHook(error, ctx)`, `VerifyFn(ctx)`. After hooks must `clone()` the response before reading its body.
44
- - `Adapter`, `BaseAdapter`, and the `./adapters/base` export are removed. Node `http` users can bridge with `srvx` or `@hono/node-server`.
45
- - The prefix is normalized to a trailing slash and only matches on a path boundary: `prefix: "/api"` now behaves as `/api/`, so `/apihello` no longer matches a route (previously it matched `ihello`).
58
+ - Handlers now get `{ request, rawBody, path, payload }` a `WebhookContext` plus the validated `payload` and return a `Response`, or `undefined` for the default `200 "ok"`. The `ack` helper is removed. Use `Response.json(body, init)` instead.
59
+ - Hooks and `verify` now use the context type: `BeforeHook(ctx)`, `AfterHook(ctx, response)`, `ErrorHook(error, ctx)`, `VerifyFn(ctx)`. An after hook must call `clone()` on the response before it reads the body.
60
+ - `Adapter`, `BaseAdapter`, and the `./adapters/base` export are removed. If you use Node's `http`, bridge with `srvx` or `@hono/node-server`.
61
+ - The prefix now always gets a trailing slash, and only matches at a path boundary. `prefix: "/api"` now behaves like `/api/`. So `/apihello` no longer matches a route before this fix, it matched the route `ihello`.
46
62
 
47
- Behavior kept: hook execution order, prefix semantics (default `/webhooks/`), exact-match routing, HMAC verification, and the `404`/`400`/`500` error body shapes. Unknown routes now return `404` without reading the request body.
63
+ Kept the same: hook order, how the prefix works (default `/webhooks/`), exact-match routing, HMAC verification, and the `404`/`400`/`500` error body shapes. One change: an unknown route now returns `404` without reading the request body.
48
64
 
49
65
  ## [0.3.0]
50
66
 
51
67
  ### Changed
52
68
 
53
- `Adapter` and `BaseAdapter` are now generic over the framework request/response types (`Adapter<TReq, TRes>`, `BaseAdapter<TReq, TRes>`), replacing the previous per-method generics. The mapping members (`toNormalizedRequest`, `toFrameworkResponse`, `handleWebhook`) are now arrow properties, so custom adapters must override them with property syntax rather than method syntax.
69
+ `Adapter` and `BaseAdapter` are now generic over the framework's request/response types: `Adapter<TReq, TRes>`, `BaseAdapter<TReq, TRes>`. This replaces the old per-method generics. The mapping members `toNormalizedRequest`, `toFrameworkResponse`, `handleWebhook` are now arrow properties. So a custom adapter must now override them as properties, not as methods.
54
70
 
55
- Also: `register()` now returns `this`, error hooks always receive a real `Error` instance, and `rawBody` is typed as `Uint8Array`. Internal formatting and lint cleanup migrated to ultracite.
71
+ Also: `register()` now returns `this`. Error hooks always get a real `Error` instance. `rawBody` is now typed as `Uint8Array`. Internal formatting and lint cleanup moved to ultracite.
56
72
 
57
73
  ## [0.2.2]
58
74
 
@@ -64,22 +80,22 @@ Also: `register()` now returns `this`, error hooks always receive a real `Error`
64
80
 
65
81
  ### Changed
66
82
 
67
- - 5fa58b1: Reduced webhook router complexity by consolidating hook normalization and handler entry creation.
68
- - 7004e9f: Allow explicit `undefined` in option handling, then follow with d707800 to remove redundant `| undefined` unions from public types.
69
- - 9f31f87: Switched the package build to ESNext-aligned output and updated package tooling and publish metadata.
83
+ - 5fa58b1: Made the webhook router simpler. Hook normalization and handler entry creation are now combined.
84
+ - 7004e9f: Option handling now allows explicit `undefined`. d707800 then removed extra `| undefined` unions from public types that this made redundant.
85
+ - 9f31f87: Switched the package build to ESNext-aligned output, and updated package tooling and publish metadata.
70
86
  - Updated dependency `@zap-studio/validation` to `0.3.3`.
71
87
 
72
88
  ### Fixed
73
89
 
74
- - 3a950dc: Preserve registered hook assignment types while keeping the schema-first router API unchanged.
90
+ - 3a950dc: Kept the types of registered hook assignments correct, with no change to the schema-first router API.
75
91
 
76
92
  ## [0.2.0]
77
93
 
78
94
  ### Changed
79
95
 
80
- - c686862: Switch `createHmacVerifier` to Web Crypto and standardize the verifier around string secrets.
96
+ - c686862: Switched `createHmacVerifier` to Web Crypto, and made all verifiers use string secrets.
81
97
 
82
- This change removes the Node `crypto` dependency from the verifier path, keeps `req.rawBody` as `Uint8Array`, simplifies `createHmacVerifier` to take a string secret, and adds public `VerificationError` in `@zap-studio/webhooks/errors` for verifier setup and signature failures.
98
+ This removes the Node `crypto` dependency from the verifier path. `req.rawBody` stays a `Uint8Array`. `createHmacVerifier` is now simpler: it takes a string secret. It also adds a public `VerificationError` in `@zap-studio/webhooks/errors`, for verifier setup and signature failures.
83
99
 
84
100
  ## [0.1.4]
85
101
 
@@ -99,7 +115,7 @@ Also: `register()` now returns `this`, error hooks always receive a real `Error`
99
115
 
100
116
  ### Fixed
101
117
 
102
- - c209a27: Fix payload schema validation internals to use the current async `standardValidate` options API (`{ throwOnError: false }`), restoring typecheck compatibility after the validation helper signature update.
118
+ - c209a27: Fixed payload schema validation internals to use the current async `standardValidate` options API (`{ throwOnError: false }`). This restores typecheck compatibility after a signature update to the validation helper.
103
119
 
104
120
  ## [0.1.1]
105
121
 
package/README.md CHANGED
@@ -104,11 +104,7 @@ router.register("/event", {
104
104
  Built-in HMAC verifier with constant-time comparison, or plug in your own `verify` function.
105
105
 
106
106
  ```ts
107
- import {
108
- createHmacVerifier,
109
- createWebhookRouter,
110
- VerificationError,
111
- } from "@zap-studio/webhooks";
107
+ import { createHmacVerifier, createWebhookRouter, VerificationError } from "@zap-studio/webhooks";
112
108
 
113
109
  const router = createWebhookRouter({
114
110
  verify: createHmacVerifier({
@@ -161,6 +157,31 @@ const router = createWebhookRouter({ prefix: "/webhooks", logger });
161
157
 
162
158
  Each delivery attempt and handler dispatch logs at `debug`; verification failures and unmatched routes log at `warn`.
163
159
 
160
+ ## OpenTelemetry
161
+
162
+ `@opentelemetry/api` is a required peer dependency — tiny, side-effect-free, and a no-op until an app registers a real SDK, so installing it costs nothing at runtime for consumers who never set one up.
163
+
164
+ Each delivery gets a `SERVER` span, and the sender's `traceparent` header is extracted so the delivery continues their trace instead of starting a new one. Each handler dispatch gets its own child `INTERNAL` span:
165
+
166
+ ```bash
167
+ npm install @opentelemetry/api
168
+ ```
169
+
170
+ ```ts
171
+ import { createWebhookRouter } from "@zap-studio/webhooks";
172
+
173
+ const router = createWebhookRouter({ prefix: "/webhooks" });
174
+ router.register("/stripe", { schema: stripeEventSchema, handler });
175
+
176
+ // If your app has registered an OpenTelemetry SDK, router.handle(request)
177
+ // now produces a SERVER span per delivery (continuing the sender's trace
178
+ // when a traceparent header is present) and an INTERNAL span per handler
179
+ // dispatch. If not, it's a no-op — no wiring required either way.
180
+ export default { fetch: (request: Request) => router.handle(request) };
181
+ ```
182
+
183
+ A non-2xx response (unmatched route, validation failure, verification failure, handler error) marks the delivery span `ERROR`; a thrown handler error is also recorded as an exception on the handler span.
184
+
164
185
  ## Runtime Support
165
186
 
166
187
  | Runtime | Minimum version |
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
1
  import { VerificationError } from "./errors.js";
2
- import { WebhookRouter, createWebhookRouter } from "./router.js";
2
+ import { n as createWebhookRouter, t as WebhookRouter } from "./router-Ba9jU5pC.js";
3
3
  import { constantTimeEquals, createHmacVerifier } from "./verify.js";
4
4
  export { VerificationError, WebhookRouter, constantTimeEquals, createHmacVerifier, createWebhookRouter };
@@ -0,0 +1,291 @@
1
+ import { SpanKind, SpanStatusCode, context, propagation, trace } from "@opentelemetry/api";
2
+ import { standardValidate } from "@zap-studio/validation";
3
+ //#endregion
4
+ //#region src/_otel.ts
5
+ /**
6
+ * OpenTelemetry tracer for this package. Resolved once against the global
7
+ * `TracerProvider`; a no-op provider (the default until an app registers an
8
+ * SDK) makes every span/propagation call below a no-op too.
9
+ */
10
+ const tracer = trace.getTracer("@zap-studio/webhooks", "2.0.1");
11
+ /**
12
+ * `TextMapGetter` for the Web `Headers` API, used to extract an inbound
13
+ * delivery's `traceparent` (and any other registered propagator fields) so
14
+ * the delivery span continues the sender's trace instead of starting a new one.
15
+ */
16
+ const HEADERS_GETTER = {
17
+ get(carrier, key) {
18
+ return carrier.get(key) ?? void 0;
19
+ },
20
+ keys(carrier) {
21
+ return [...carrier.keys()];
22
+ }
23
+ };
24
+ /**
25
+ * Records `error` on `span` and marks it as failed. `recordException` only
26
+ * accepts an `Error` or `string`, so other thrown values just get the
27
+ * `ERROR` status without an attached exception event.
28
+ */
29
+ const recordSpanError = (span, error) => {
30
+ if (error instanceof Error || typeof error === "string") span.recordException(error);
31
+ span.setStatus({ code: SpanStatusCode.ERROR });
32
+ };
33
+ //#endregion
34
+ //#region src/router.ts
35
+ /**
36
+ * Schema-first webhook router with path dispatching, validation, and optional verification.
37
+ *
38
+ * @template TMap - Internal route payload map built incrementally via `register`.
39
+ */
40
+ const toArray = (value) => {
41
+ if (value === void 0) return [];
42
+ return Array.isArray(value) ? value : [value];
43
+ };
44
+ const notFoundResponse = () => Response.json({ error: "not found" }, { status: 404 });
45
+ /** Sets `http.response.status_code` and marks `span` `ERROR` on a non-2xx response. */
46
+ const finishDelivery = (span, response) => {
47
+ span.setAttribute("http.response.status_code", response.status);
48
+ if (!response.ok) span.setStatus({ code: SpanStatusCode.ERROR });
49
+ return response;
50
+ };
51
+ const bodyDecoder = new TextDecoder();
52
+ /**
53
+ * Normalizes a path to its canonical form: leading slash, no trailing slash,
54
+ * duplicate slashes collapsed. The root path is `"/"`.
55
+ */
56
+ const normalizePath = (path) => {
57
+ const withLeadingSlash = path.startsWith("/") ? path : `/${path}`;
58
+ const collapsed = withLeadingSlash.includes("//") ? withLeadingSlash.replaceAll(/\/{2,}/gu, "/") : withLeadingSlash;
59
+ return collapsed.length > 1 && collapsed.endsWith("/") ? collapsed.slice(0, -1) : collapsed;
60
+ };
61
+ /** Runs the given before-hooks in order against the request context. */
62
+ const runBeforeHooks = async (ctx, hooks) => {
63
+ if (!hooks || hooks.length === 0) return;
64
+ for (const hook of hooks) await hook(ctx);
65
+ };
66
+ /** Runs the given after-hooks in order against the request context and response. */
67
+ const runAfterHooks = async (ctx, response, hooks) => {
68
+ if (!hooks || hooks.length === 0) return;
69
+ for (const hook of hooks) await hook(ctx, response);
70
+ };
71
+ /** Builds an internal handler entry from route registration options. */
72
+ const createHandlerEntry = (options) => {
73
+ const entry = { handler: options.handler };
74
+ if (options.schema !== void 0) entry.schema = options.schema;
75
+ if (options.before !== void 0) entry.before = toArray(options.before);
76
+ if (options.after !== void 0) entry.after = toArray(options.after);
77
+ return entry;
78
+ };
79
+ /** Parses the request's raw body bytes as JSON, returning `undefined` on invalid JSON. */
80
+ const parseRequestBody = (ctx) => {
81
+ try {
82
+ return JSON.parse(bodyDecoder.decode(ctx.rawBody));
83
+ } catch {
84
+ return;
85
+ }
86
+ };
87
+ /** Validates the parsed payload against the route schema, returning either the validated value or a `400` response. */
88
+ const validatePayload = async (parsedJson, schema) => {
89
+ if (!schema) return parsedJson;
90
+ const result = await standardValidate(parsedJson, schema, { throwOnError: false });
91
+ if (result.issues) return Response.json({
92
+ error: "validation failed",
93
+ issues: result.issues.map((issue) => ({
94
+ message: issue.message,
95
+ path: issue.path?.map((p) => typeof p === "object" && "key" in p ? String(p.key) : String(p))
96
+ }))
97
+ }, { status: 400 });
98
+ return result.value;
99
+ };
100
+ /** Invokes the route handler with the validated payload, defaulting to a `200 "ok"` response. */
101
+ const executeHandler = async (handler, ctx, validatedPayload) => {
102
+ return await handler({
103
+ ...ctx,
104
+ payload: validatedPayload
105
+ }) ?? Response.json("ok");
106
+ };
107
+ /** Runs the route handler inside its own `INTERNAL` span, nested under the delivery span. */
108
+ const dispatchHandler = async (handlerEntry, ctx, validatedPayload, deliveryContext) => {
109
+ const handlerSpan = tracer.startSpan(`webhook.handler ${ctx.path}`, { kind: SpanKind.INTERNAL }, deliveryContext);
110
+ try {
111
+ return await context.with(trace.setSpan(deliveryContext, handlerSpan), async () => await executeHandler(handlerEntry.handler, ctx, validatedPayload));
112
+ } catch (error) {
113
+ recordSpanError(handlerSpan, error);
114
+ throw error;
115
+ } finally {
116
+ handlerSpan.end();
117
+ }
118
+ };
119
+ /**
120
+ * Main webhook router class.
121
+ *
122
+ * Register routes with typed schemas and call `handle` with a Web API `Request`.
123
+ *
124
+ * @example
125
+ * ```ts
126
+ * import { WebhookRouter } from "@zap-studio/webhooks";
127
+ *
128
+ * const router = new WebhookRouter({ prefix: "/webhooks" });
129
+ *
130
+ * router.register("/stripe", {
131
+ * schema: stripeEventSchema,
132
+ * handler: async ({ payload }) => {
133
+ * console.log("Stripe event:", payload.type);
134
+ * },
135
+ * });
136
+ *
137
+ * export default { fetch: (request: Request) => router.handle(request) };
138
+ * ```
139
+ */
140
+ var WebhookRouter = class {
141
+ handlers = /* @__PURE__ */ new Map();
142
+ verify;
143
+ globalBeforeHooks = [];
144
+ globalAfterHooks = [];
145
+ globalErrorHook;
146
+ logger;
147
+ prefix;
148
+ prefixWithSlash;
149
+ /**
150
+ * Creates a webhook router with optional global hooks and verification behavior.
151
+ *
152
+ * @param opts - Router-level options.
153
+ *
154
+ * @example
155
+ * ```ts
156
+ * const router = new WebhookRouter({
157
+ * prefix: "/webhooks",
158
+ * verify: createHmacVerifier({ headerName: "x-signature", secret }),
159
+ * onError: (error) => Response.json({ error: error.message }, { status: 500 }),
160
+ * });
161
+ * ```
162
+ */
163
+ constructor(opts = {}) {
164
+ this.prefix = normalizePath(opts.prefix ?? "/webhooks");
165
+ this.prefixWithSlash = `${this.prefix}/`;
166
+ this.verify = opts.verify;
167
+ this.globalBeforeHooks = toArray(opts.before);
168
+ this.globalAfterHooks = toArray(opts.after);
169
+ this.globalErrorHook = opts.onError;
170
+ this.logger = opts.logger;
171
+ }
172
+ register(path, handlerOrOptions) {
173
+ this.handlers.set(normalizePath(path), typeof handlerOrOptions === "function" ? { handler: handlerOrOptions } : createHandlerEntry(handlerOrOptions));
174
+ return this;
175
+ }
176
+ /**
177
+ * Handles an incoming webhook request.
178
+ *
179
+ * The request body is read exactly once; hooks and handlers receive the raw
180
+ * bytes through the webhook context instead of the request stream.
181
+ *
182
+ * @param request - Incoming Web API request.
183
+ * @returns Web API response for the runtime to send back.
184
+ *
185
+ * @example
186
+ * ```ts
187
+ * // Framework-agnostic: works with any Web API Request/Response runtime.
188
+ * export async function POST(request: Request): Promise<Response> {
189
+ * return router.handle(request);
190
+ * }
191
+ * ```
192
+ */
193
+ async handle(request) {
194
+ const requestPath = new URL(request.url).pathname;
195
+ const { method } = request;
196
+ this.logger?.debug("webhook delivery attempt", { path: requestPath });
197
+ const parentContext = propagation.extract(context.active(), request.headers, HEADERS_GETTER);
198
+ const deliverySpan = tracer.startSpan(`${method} ${requestPath}`, {
199
+ attributes: {
200
+ "http.request.method": method,
201
+ "url.path": requestPath
202
+ },
203
+ kind: SpanKind.SERVER
204
+ }, parentContext);
205
+ const deliveryContext = trace.setSpan(parentContext, deliverySpan);
206
+ try {
207
+ const response = await context.with(deliveryContext, async () => await this.dispatch(request, requestPath, deliveryContext));
208
+ return finishDelivery(deliverySpan, response);
209
+ } finally {
210
+ deliverySpan.end();
211
+ }
212
+ }
213
+ /** Matches the route, runs hooks/verification/validation, and dispatches the handler. */
214
+ async dispatch(request, requestPath, deliveryContext) {
215
+ const path = this.matchPath(request);
216
+ if (path === null) {
217
+ this.logger?.warn("webhook route not matched", { path: requestPath });
218
+ return notFoundResponse();
219
+ }
220
+ const handlerEntry = this.handlers.get(path);
221
+ if (!handlerEntry) {
222
+ this.logger?.warn("webhook route not matched", { path });
223
+ return notFoundResponse();
224
+ }
225
+ const ctx = {
226
+ path,
227
+ rawBody: /* @__PURE__ */ new Uint8Array(0),
228
+ request
229
+ };
230
+ try {
231
+ ctx.rawBody = new Uint8Array(await request.arrayBuffer());
232
+ await runBeforeHooks(ctx, this.globalBeforeHooks);
233
+ await runBeforeHooks(ctx, handlerEntry.before);
234
+ if (this.verify) try {
235
+ await this.verify(ctx);
236
+ } catch (error) {
237
+ this.logger?.warn("webhook verification failed", {
238
+ error,
239
+ path
240
+ });
241
+ throw error;
242
+ }
243
+ const parsedJson = parseRequestBody(ctx);
244
+ const validationResult = await validatePayload(parsedJson, handlerEntry.schema);
245
+ if (validationResult instanceof Response) return validationResult;
246
+ this.logger?.debug("webhook handler dispatch", { path });
247
+ const response = await dispatchHandler(handlerEntry, ctx, validationResult, deliveryContext);
248
+ await runAfterHooks(ctx, response, handlerEntry.after);
249
+ await runAfterHooks(ctx, response, this.globalAfterHooks);
250
+ return response;
251
+ } catch (error) {
252
+ return await this.handleError(error, ctx);
253
+ }
254
+ }
255
+ /** Resolves the incoming request's URL to a registered route key, or `null` if it doesn't match the configured prefix. */
256
+ matchPath(request) {
257
+ const pathname = normalizePath(new URL(request.url).pathname);
258
+ if (this.prefix === "/") return pathname;
259
+ if (pathname === this.prefix) return "/";
260
+ if (!pathname.startsWith(this.prefixWithSlash)) return null;
261
+ return pathname.slice(this.prefix.length);
262
+ }
263
+ /** Builds the error response for a failed request, deferring to the global error hook when set. */
264
+ async handleError(error, ctx) {
265
+ if (this.globalErrorHook) {
266
+ const normalizedError = error instanceof Error ? error : /* @__PURE__ */ new Error("Internal server error");
267
+ const errorResponse = await this.globalErrorHook(normalizedError, ctx);
268
+ if (errorResponse) return errorResponse;
269
+ }
270
+ return Response.json({ error: error instanceof Error ? error.message : "Internal server error" }, { status: 500 });
271
+ }
272
+ };
273
+ /**
274
+ * Factory helper for creating a webhook router instance.
275
+ *
276
+ * @param opts - Optional global router options.
277
+ * @returns A new webhook router.
278
+ *
279
+ * @example
280
+ * ```ts
281
+ * import { createWebhookRouter } from "@zap-studio/webhooks";
282
+ *
283
+ * const router = createWebhookRouter({ prefix: "/webhooks" });
284
+ * router.register("/stripe", { schema: stripeEventSchema, handler });
285
+ * ```
286
+ */
287
+ const createWebhookRouter = (opts) => new WebhookRouter(opts);
288
+ //#endregion
289
+ export { createWebhookRouter as n, WebhookRouter as t };
290
+
291
+ //# sourceMappingURL=router-Ba9jU5pC.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"router-Ba9jU5pC.js","names":["pkg.name","pkg.version","otelContext"],"sources":["../package.json","../src/_otel.ts","../src/router.ts"],"sourcesContent":["","/**\n * Internal OpenTelemetry wiring for the webhooks package: tracer resolution,\n * the `Headers` extraction carrier, and span error recording. Kept out of\n * `router.ts` so dispatch logic doesn't get tangled with tracing concerns.\n *\n * @module @zap-studio/webhooks/otel\n */\n\nimport type { Span, TextMapGetter, Tracer } from \"@opentelemetry/api\";\n\nimport { SpanStatusCode, trace } from \"@opentelemetry/api\";\n\nimport pkg from \"../package.json\" with { type: \"json\" };\n\n/**\n * OpenTelemetry tracer for this package. Resolved once against the global\n * `TracerProvider`; a no-op provider (the default until an app registers an\n * SDK) makes every span/propagation call below a no-op too.\n */\nexport const tracer: Tracer = trace.getTracer(pkg.name, pkg.version);\n\n/**\n * `TextMapGetter` for the Web `Headers` API, used to extract an inbound\n * delivery's `traceparent` (and any other registered propagator fields) so\n * the delivery span continues the sender's trace instead of starting a new one.\n */\nexport const HEADERS_GETTER: TextMapGetter<Headers> = {\n get(carrier, key) {\n return carrier.get(key) ?? undefined;\n },\n keys(carrier) {\n return [...carrier.keys()];\n },\n};\n\n/**\n * Records `error` on `span` and marks it as failed. `recordException` only\n * accepts an `Error` or `string`, so other thrown values just get the\n * `ERROR` status without an attached exception event.\n */\nexport const recordSpanError = (span: Span, error: unknown): void => {\n if (error instanceof Error || typeof error === \"string\") {\n span.recordException(error);\n }\n span.setStatus({ code: SpanStatusCode.ERROR });\n};\n","/**\n * Schema-first webhook router primitives.\n *\n * @module @zap-studio/webhooks/router\n */\n\nimport type { Context, Span } from \"@opentelemetry/api\";\nimport type { Logger } from \"@zap-studio/logger\";\nimport type { StandardSchemaV1 } from \"@zap-studio/validation\";\n\nimport {\n SpanKind,\n SpanStatusCode,\n context as otelContext,\n propagation,\n trace,\n} from \"@opentelemetry/api\";\nimport { standardValidate } from \"@zap-studio/validation\";\n\nimport type {\n AfterHook,\n BeforeHook,\n ErrorHook,\n HandlerEntry,\n InferSchemaOutput,\n RegisterOptions,\n SchemaRouteOptions,\n VerifyFn,\n WebhookContext,\n WebhookHandler,\n WebhookRouterOptions,\n} from \"./types.ts\";\n\nimport { HEADERS_GETTER, recordSpanError, tracer } from \"./_otel.ts\";\n\n/**\n * Schema-first webhook router with path dispatching, validation, and optional verification.\n *\n * @template TMap - Internal route payload map built incrementally via `register`.\n */\n\nconst toArray = <T>(value: T | T[] | undefined): T[] => {\n if (value === undefined) {\n return [];\n }\n\n return Array.isArray(value) ? value : [value];\n};\n\nconst notFoundResponse = (): Response => Response.json({ error: \"not found\" }, { status: 404 });\n\n/** Sets `http.response.status_code` and marks `span` `ERROR` on a non-2xx response. */\nconst finishDelivery = (span: Span, response: Response): Response => {\n span.setAttribute(\"http.response.status_code\", response.status);\n if (!response.ok) {\n span.setStatus({ code: SpanStatusCode.ERROR });\n }\n return response;\n};\n\nconst bodyDecoder = new TextDecoder();\n\n/**\n * Normalizes a path to its canonical form: leading slash, no trailing slash,\n * duplicate slashes collapsed. The root path is `\"/\"`.\n */\nconst normalizePath = (path: string): string => {\n const withLeadingSlash = path.startsWith(\"/\") ? path : `/${path}`;\n const collapsed = withLeadingSlash.includes(\"//\")\n ? withLeadingSlash.replaceAll(/\\/{2,}/gu, \"/\")\n : withLeadingSlash;\n\n return collapsed.length > 1 && collapsed.endsWith(\"/\") ? collapsed.slice(0, -1) : collapsed;\n};\n\n/** Runs the given before-hooks in order against the request context. */\nconst runBeforeHooks = async (ctx: WebhookContext, hooks?: BeforeHook[]): Promise<void> => {\n if (!hooks || hooks.length === 0) {\n return;\n }\n\n for (const hook of hooks) {\n // oxlint-disable-next-line react-doctor/async-await-in-loop -- false positive: before-hooks must run in the order they were added, because a later hook can depend on an earlier one. A throw must also stop the hooks that are left. Promise.all would run them at the same time and lose both rules.\n await hook(ctx);\n }\n};\n\n/** Runs the given after-hooks in order against the request context and response. */\nconst runAfterHooks = async (\n ctx: WebhookContext,\n response: Response,\n hooks?: AfterHook[],\n): Promise<void> => {\n if (!hooks || hooks.length === 0) {\n return;\n }\n\n for (const hook of hooks) {\n // oxlint-disable-next-line react-doctor/async-await-in-loop -- false positive: same rules as runBeforeHooks above. After-hooks must run in the order they were added, and a throw must stop the hooks that are left.\n await hook(ctx, response);\n }\n};\n\n/** Builds an internal handler entry from route registration options. */\nconst createHandlerEntry = (options: RegisterOptions<unknown>): HandlerEntry => {\n const entry: HandlerEntry = {\n handler: options.handler,\n };\n\n if (options.schema !== undefined) {\n entry.schema = options.schema;\n }\n\n if (options.before !== undefined) {\n entry.before = toArray(options.before);\n }\n\n if (options.after !== undefined) {\n entry.after = toArray(options.after);\n }\n\n return entry;\n};\n\n/** Parses the request's raw body bytes as JSON, returning `undefined` on invalid JSON. */\nconst parseRequestBody = (ctx: WebhookContext): unknown => {\n try {\n return JSON.parse(bodyDecoder.decode(ctx.rawBody));\n } catch {\n return undefined;\n }\n};\n\n/** Validates the parsed payload against the route schema, returning either the validated value or a `400` response. */\nconst validatePayload = async <TPayload>(\n parsedJson: unknown,\n schema?: StandardSchemaV1<unknown, TPayload>,\n): Promise<TPayload | Response> => {\n if (!schema) {\n // SAFETY: no schema was given, so there is nothing to validate against. The `TPayload` type declared by the caller is the route's only contract.\n return parsedJson as TPayload;\n }\n\n const result = await standardValidate(parsedJson, schema, {\n throwOnError: false,\n });\n\n if (result.issues) {\n return Response.json(\n {\n error: \"validation failed\",\n issues: result.issues.map((issue) => ({\n message: issue.message,\n path: issue.path?.map((p) =>\n typeof p === \"object\" && \"key\" in p ? String(p.key) : String(p),\n ),\n })),\n },\n { status: 400 },\n );\n }\n\n return result.value;\n};\n\n/** Invokes the route handler with the validated payload, defaulting to a `200 \"ok\"` response. */\nconst executeHandler = async <TPayload = unknown>(\n handler: WebhookHandler<TPayload>,\n ctx: WebhookContext,\n validatedPayload: TPayload,\n): Promise<Response> => {\n const responded = await handler({\n ...ctx,\n payload: validatedPayload,\n });\n\n return responded ?? Response.json(\"ok\");\n};\n\n/** Runs the route handler inside its own `INTERNAL` span, nested under the delivery span. */\nconst dispatchHandler = async (\n handlerEntry: HandlerEntry,\n ctx: WebhookContext,\n validatedPayload: unknown,\n deliveryContext: Context,\n): Promise<Response> => {\n const handlerSpan = tracer.startSpan(\n `webhook.handler ${ctx.path}`,\n { kind: SpanKind.INTERNAL },\n deliveryContext,\n );\n\n try {\n return await otelContext.with(\n trace.setSpan(deliveryContext, handlerSpan),\n async () => await executeHandler(handlerEntry.handler, ctx, validatedPayload),\n );\n } catch (error) {\n recordSpanError(handlerSpan, error);\n throw error;\n } finally {\n handlerSpan.end();\n }\n};\n\n/**\n * Main webhook router class.\n *\n * Register routes with typed schemas and call `handle` with a Web API `Request`.\n *\n * @example\n * ```ts\n * import { WebhookRouter } from \"@zap-studio/webhooks\";\n *\n * const router = new WebhookRouter({ prefix: \"/webhooks\" });\n *\n * router.register(\"/stripe\", {\n * schema: stripeEventSchema,\n * handler: async ({ payload }) => {\n * console.log(\"Stripe event:\", payload.type);\n * },\n * });\n *\n * export default { fetch: (request: Request) => router.handle(request) };\n * ```\n */\nexport class WebhookRouter<TMap = unknown> {\n private readonly handlers = new Map<string, HandlerEntry>();\n private readonly verify: VerifyFn | undefined;\n private readonly globalBeforeHooks: BeforeHook[] = [];\n private readonly globalAfterHooks: AfterHook[] = [];\n private readonly globalErrorHook: ErrorHook | undefined;\n private readonly logger: Logger | undefined;\n private readonly prefix: string;\n private readonly prefixWithSlash: string;\n\n /**\n * Creates a webhook router with optional global hooks and verification behavior.\n *\n * @param opts - Router-level options.\n *\n * @example\n * ```ts\n * const router = new WebhookRouter({\n * prefix: \"/webhooks\",\n * verify: createHmacVerifier({ headerName: \"x-signature\", secret }),\n * onError: (error) => Response.json({ error: error.message }, { status: 500 }),\n * });\n * ```\n */\n constructor(opts: WebhookRouterOptions = {}) {\n this.prefix = normalizePath(opts.prefix ?? \"/webhooks\");\n this.prefixWithSlash = `${this.prefix}/`;\n this.verify = opts.verify;\n this.globalBeforeHooks = toArray(opts.before);\n this.globalAfterHooks = toArray(opts.after);\n this.globalErrorHook = opts.onError;\n this.logger = opts.logger;\n }\n\n /**\n * Register a webhook handler for a specific path.\n *\n * When a schema is provided, `payload` is inferred from the schema output type.\n *\n * @param path - Route path relative to configured prefix, starting with `/` (e.g. `\"/stripe\"`).\n * @param handlerOrOptions - Handler function or schema-based registration options.\n * @returns The same router instance with an updated internal route type map.\n *\n * @example\n * ```ts\n * router.register(\"/stripe\", {\n * schema: stripeEventSchema,\n * handler: async ({ payload }) => {\n * console.log(payload.type); // typed from stripeEventSchema\n * },\n * });\n * ```\n */\n register<Path extends `/${string}`, TSchema extends StandardSchemaV1<unknown, unknown>>(\n path: Path,\n handlerOrOptions: SchemaRouteOptions<TSchema>,\n ): WebhookRouter<TMap & Record<Path, InferSchemaOutput<TSchema>>>;\n /**\n * Register a webhook handler for a specific path, with schema-less registration options.\n *\n * @param path - Route path relative to configured prefix, starting with `/` (e.g. `\"/stripe\"`).\n * @param handlerOrOptions - Registration options without a schema.\n * @returns The same router instance with an updated internal route type map.\n *\n * @example\n * ```ts\n * router.register(\"/ping\", {\n * before: (ctx) => console.log(\"received\", ctx.path),\n * handler: () => Response.json({ ok: true }),\n * });\n * ```\n */\n register<Path extends `/${string}`, TPayload>(\n path: Path,\n handlerOrOptions: RegisterOptions<TPayload>,\n ): WebhookRouter<TMap & Record<Path, TPayload>>;\n /**\n * Register a webhook handler for a specific path, using a plain handler function.\n *\n * @param path - Route path relative to configured prefix, starting with `/` (e.g. `\"/stripe\"`).\n * @param handlerOrOptions - Handler function to process the webhook.\n * @returns The same router instance with an updated internal route type map.\n *\n * @example\n * ```ts\n * router.register(\"/health\", () => Response.json({ status: \"ok\" }));\n * ```\n */\n register<Path extends `/${string}`>(\n path: Path,\n handlerOrOptions: WebhookHandler,\n ): WebhookRouter<TMap & Record<Path, unknown>>;\n register(path: string, handlerOrOptions: WebhookHandler | RegisterOptions<unknown>): this {\n this.handlers.set(\n normalizePath(path),\n typeof handlerOrOptions === \"function\"\n ? { handler: handlerOrOptions }\n : createHandlerEntry(handlerOrOptions),\n );\n\n return this;\n }\n\n /**\n * Handles an incoming webhook request.\n *\n * The request body is read exactly once; hooks and handlers receive the raw\n * bytes through the webhook context instead of the request stream.\n *\n * @param request - Incoming Web API request.\n * @returns Web API response for the runtime to send back.\n *\n * @example\n * ```ts\n * // Framework-agnostic: works with any Web API Request/Response runtime.\n * export async function POST(request: Request): Promise<Response> {\n * return router.handle(request);\n * }\n * ```\n */\n async handle(request: Request): Promise<Response> {\n const requestPath = new URL(request.url).pathname;\n const { method } = request;\n this.logger?.debug(\"webhook delivery attempt\", { path: requestPath });\n\n const parentContext = propagation.extract(\n otelContext.active(),\n request.headers,\n HEADERS_GETTER,\n );\n const deliverySpan = tracer.startSpan(\n `${method} ${requestPath}`,\n {\n attributes: {\n \"http.request.method\": method,\n \"url.path\": requestPath,\n },\n kind: SpanKind.SERVER,\n },\n parentContext,\n );\n const deliveryContext = trace.setSpan(parentContext, deliverySpan);\n\n try {\n const response = await otelContext.with(\n deliveryContext,\n async () => await this.dispatch(request, requestPath, deliveryContext),\n );\n return finishDelivery(deliverySpan, response);\n } finally {\n deliverySpan.end();\n }\n }\n\n /** Matches the route, runs hooks/verification/validation, and dispatches the handler. */\n private async dispatch(\n request: Request,\n requestPath: string,\n deliveryContext: Context,\n ): Promise<Response> {\n const path = this.matchPath(request);\n if (path === null) {\n this.logger?.warn(\"webhook route not matched\", { path: requestPath });\n return notFoundResponse();\n }\n\n const handlerEntry = this.handlers.get(path);\n if (!handlerEntry) {\n this.logger?.warn(\"webhook route not matched\", { path });\n return notFoundResponse();\n }\n\n const ctx: WebhookContext = {\n path,\n rawBody: new Uint8Array(0),\n request,\n };\n\n try {\n ctx.rawBody = new Uint8Array(await request.arrayBuffer());\n\n await runBeforeHooks(ctx, this.globalBeforeHooks);\n await runBeforeHooks(ctx, handlerEntry.before);\n\n if (this.verify) {\n try {\n await this.verify(ctx);\n } catch (error) {\n this.logger?.warn(\"webhook verification failed\", { error, path });\n throw error;\n }\n }\n\n const parsedJson = parseRequestBody(ctx);\n const validationResult = await validatePayload(parsedJson, handlerEntry.schema);\n\n if (validationResult instanceof Response) {\n return validationResult;\n }\n\n this.logger?.debug(\"webhook handler dispatch\", { path });\n const response = await dispatchHandler(handlerEntry, ctx, validationResult, deliveryContext);\n\n await runAfterHooks(ctx, response, handlerEntry.after);\n await runAfterHooks(ctx, response, this.globalAfterHooks);\n\n return response;\n } catch (error) {\n return await this.handleError(error, ctx);\n }\n }\n\n /** Resolves the incoming request's URL to a registered route key, or `null` if it doesn't match the configured prefix. */\n private matchPath(request: Request): string | null {\n const pathname = normalizePath(new URL(request.url).pathname);\n\n // Root mount: the whole pathname is the route path.\n if (this.prefix === \"/\") {\n return pathname;\n }\n\n if (pathname === this.prefix) {\n return \"/\";\n }\n\n // Require prefix followed by a segment boundary, then match handlers on\n // the remainder (e.g. /webhooks/stripe -> /stripe).\n if (!pathname.startsWith(this.prefixWithSlash)) {\n return null;\n }\n\n return pathname.slice(this.prefix.length);\n }\n\n /** Builds the error response for a failed request, deferring to the global error hook when set. */\n private async handleError(error: unknown, ctx: WebhookContext): Promise<Response> {\n if (this.globalErrorHook) {\n const normalizedError = error instanceof Error ? error : new Error(\"Internal server error\");\n const errorResponse = await this.globalErrorHook(normalizedError, ctx);\n if (errorResponse) {\n return errorResponse;\n }\n }\n\n return Response.json(\n {\n error: error instanceof Error ? error.message : \"Internal server error\",\n },\n { status: 500 },\n );\n }\n}\n\n/**\n * Factory helper for creating a webhook router instance.\n *\n * @param opts - Optional global router options.\n * @returns A new webhook router.\n *\n * @example\n * ```ts\n * import { createWebhookRouter } from \"@zap-studio/webhooks\";\n *\n * const router = createWebhookRouter({ prefix: \"/webhooks\" });\n * router.register(\"/stripe\", { schema: stripeEventSchema, handler });\n * ```\n */\nexport const createWebhookRouter = (opts?: WebhookRouterOptions): WebhookRouter =>\n new WebhookRouter(opts);\n"],"mappings":";;;;;;;;;ACmBA,MAAa,SAAiB,MAAM,UAAUA,wBAAUC,OAAW;;;;;;AAOnE,MAAa,iBAAyC;CACpD,IAAI,SAAS,KAAK;EAChB,OAAO,QAAQ,IAAI,GAAG,KAAK,KAAA;CAC7B;CACA,KAAK,SAAS;EACZ,OAAO,CAAC,GAAG,QAAQ,KAAK,CAAC;CAC3B;AACF;;;;;;AAOA,MAAa,mBAAmB,MAAY,UAAyB;CACnE,IAAI,iBAAiB,SAAS,OAAO,UAAU,UAC7C,KAAK,gBAAgB,KAAK;CAE5B,KAAK,UAAU,EAAE,MAAM,eAAe,MAAM,CAAC;AAC/C;;;;;;;;ACJA,MAAM,WAAc,UAAoC;CACtD,IAAI,UAAU,KAAA,GACZ,OAAO,CAAC;CAGV,OAAO,MAAM,QAAQ,KAAK,IAAI,QAAQ,CAAC,KAAK;AAC9C;AAEA,MAAM,yBAAmC,SAAS,KAAK,EAAE,OAAO,YAAY,GAAG,EAAE,QAAQ,IAAI,CAAC;;AAG9F,MAAM,kBAAkB,MAAY,aAAiC;CACnE,KAAK,aAAa,6BAA6B,SAAS,MAAM;CAC9D,IAAI,CAAC,SAAS,IACZ,KAAK,UAAU,EAAE,MAAM,eAAe,MAAM,CAAC;CAE/C,OAAO;AACT;AAEA,MAAM,cAAc,IAAI,YAAY;;;;;AAMpC,MAAM,iBAAiB,SAAyB;CAC9C,MAAM,mBAAmB,KAAK,WAAW,GAAG,IAAI,O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GF,MAAM,aAAa,iBAAiB,GAAG;GACvC,MAAM,mBAAmB,MAAM,gBAAgB,YAAY,aAAa,MAAM;GAE9E,IAAI,4BAA4B,UAC9B,OAAO;GAGT,KAAK,QAAQ,MAAM,4BAA4B,EAAE,KAAK,CAAC;GACvD,MAAM,WAAW,MAAM,gBAAgB,cAAc,KAAK,kBAAkB,eAAe;GAE3F,MAAM,cAAc,KAAK,UAAU,aAAa,KAAK;GACrD,MAAM,cAAc,KAAK,UAAU,KAAK,gBAAgB;GAExD,OAAO;EACT,SAAS,OAAO;GACd,OAAO,MAAM,KAAK,YAAY,OAAO,GAAG;EAC1C;CACF;;CAGA,UAAkB,SAAiC;EACjD,MAAM,WAAW,cAAc,IAAI,IAAI,QAAQ,GAAG,CAAC,CAAC,QAAQ;EAG5D,IAAI,KAAK,WAAW,KAClB,OAAO;EAGT,IAAI,aAAa,KAAK,QACpB,OAAO;EAKT,IAAI,CAAC,SAAS,WAAW,KAAK,eAAe,GAC3C,OAAO;EAGT,OAAO,SAAS,MAAM,KAAK,OAAO,MAAM;CAC1C;;CAGA,MAAc,YAAY,OAAgB,KAAwC;EAChF,IAAI,KAAK,iBAAiB;GACxB,MAAM,kBAAkB,iBAAiB,QAAQ,wBAAQ,IAAI,MAAM,uBAAuB;GAC1F,MAAM,gBAAgB,MAAM,KAAK,gBAAgB,iBAAiB,GAAG;GACrE,IAAI,eACF,OAAO;EAEX;EAEA,OAAO,SAAS,KACd,EACE,OAAO,iBAAiB,QAAQ,MAAM,UAAU,wBAClD,GACA,EAAE,QAAQ,IAAI,CAChB;CACF;AACF;;;;;;;;;;;;;;;AAgBA,MAAa,uBAAuB,SAClC,IAAI,cAAc,IAAI"}
package/dist/router.d.ts CHANGED
@@ -113,6 +113,8 @@ declare class WebhookRouter<TMap = unknown> {
113
113
  * ```
114
114
  */
115
115
  handle(request: Request): Promise<Response>;
116
+ /** Matches the route, runs hooks/verification/validation, and dispatches the handler. */
117
+ private dispatch;
116
118
  /** Resolves the incoming request's URL to a registered route key, or `null` if it doesn't match the configured prefix. */
117
119
  private matchPath;
118
120
  /** Builds the error response for a failed request, deferring to the global error hook when set. */
@@ -1 +1 @@
1
- {"version":3,"file":"router.d.ts","names":[],"sources":["../src/router.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;cA4La,cAAc;mBACR;mBACA;mBACA;mBACA;mBACA;mBACA;mBACA;mBACA;;;;;;;;;;;;;;;EAgBjB,YAAY,OAAM;;;;;;;;;;;;;;;;;;;;EA6BlB,SACE,2BACA,gBAAgB,oCAEhB,MAAM,MACN,kBAAkB,mBAAmB,WACpC,cAAc,OAAO,OAAO,MAAM,kBAAkB;;;;;;;;;;;;;;;;EAgBvD,SAAS,2BAA2B,UAClC,MAAM,MACN,kBAAkB,gBAAgB,YACjC,cAAc,OAAO,OAAO,MAAM;;;;;;;;;;;;;EAarC,SAAS,2BACP,MAAM,MACN,kBAAkB,iBACjB,cAAc,OAAO,OAAO;;;;;;;;;;;;;;;;;;EAgC/B,OAAa,SAAS,UAAU,QAAQ;;UAgEhC;;UAsBM;;;;;;;;;;;;;;;;cAoCH,sBACX,OAAO,yBACN"}
1
+ {"version":3,"file":"router.d.ts","names":[],"sources":["../src/router.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;cAkOa,cAAc;mBACR;mBACA;mBACA;mBACA;mBACA;mBACA;mBACA;mBACA;;;;;;;;;;;;;;;EAgBjB,YAAY,OAAM;;;;;;;;;;;;;;;;;;;;EA6BlB,SAAS,2BAA2B,gBAAgB,oCAClD,MAAM,MACN,kBAAkB,mBAAmB,WACpC,cAAc,OAAO,OAAO,MAAM,kBAAkB;;;;;;;;;;;;;;;;EAgBvD,SAAS,2BAA2B,UAClC,MAAM,MACN,kBAAkB,gBAAgB,YACjC,cAAc,OAAO,OAAO,MAAM;;;;;;;;;;;;;EAarC,SAAS,2BACP,MAAM,MACN,kBAAkB,iBACjB,cAAc,OAAO,OAAO;;;;;;;;;;;;;;;;;;EA6B/B,OAAa,SAAS,UAAU,QAAQ;;UAmC1B;;UA0DN;;UAsBM;;;;;;;;;;;;;;;;cAgCH,sBAAuB,OAAO,yBAAuB"}
package/dist/router.js CHANGED
@@ -1,222 +1,2 @@
1
- import { standardValidate } from "@zap-studio/validation";
2
- //#region src/router.ts
3
- /**
4
- * Schema-first webhook router with path dispatching, validation, and optional verification.
5
- *
6
- * @template TMap - Internal route payload map built incrementally via `register`.
7
- */
8
- const toArray = (value) => {
9
- if (value === void 0) return [];
10
- return Array.isArray(value) ? value : [value];
11
- };
12
- const notFoundResponse = () => Response.json({ error: "not found" }, { status: 404 });
13
- const bodyDecoder = new TextDecoder();
14
- /**
15
- * Normalizes a path to its canonical form: leading slash, no trailing slash,
16
- * duplicate slashes collapsed. The root path is `"/"`.
17
- */
18
- const normalizePath = (path) => {
19
- const withLeadingSlash = path.startsWith("/") ? path : `/${path}`;
20
- const collapsed = withLeadingSlash.includes("//") ? withLeadingSlash.replaceAll(/\/{2,}/gu, "/") : withLeadingSlash;
21
- return collapsed.length > 1 && collapsed.endsWith("/") ? collapsed.slice(0, -1) : collapsed;
22
- };
23
- /** Runs the given before-hooks in order against the request context. */
24
- const runBeforeHooks = async (ctx, hooks) => {
25
- if (!hooks || hooks.length === 0) return;
26
- for (const hook of hooks) await hook(ctx);
27
- };
28
- /** Runs the given after-hooks in order against the request context and response. */
29
- const runAfterHooks = async (ctx, response, hooks) => {
30
- if (!hooks || hooks.length === 0) return;
31
- for (const hook of hooks) await hook(ctx, response);
32
- };
33
- /** Builds an internal handler entry from route registration options. */
34
- const createHandlerEntry = (options) => {
35
- const entry = { handler: options.handler };
36
- if (options.schema !== void 0) entry.schema = options.schema;
37
- if (options.before !== void 0) entry.before = toArray(options.before);
38
- if (options.after !== void 0) entry.after = toArray(options.after);
39
- return entry;
40
- };
41
- /** Parses the request's raw body bytes as JSON, returning `undefined` on invalid JSON. */
42
- const parseRequestBody = (ctx) => {
43
- try {
44
- return JSON.parse(bodyDecoder.decode(ctx.rawBody));
45
- } catch {
46
- return;
47
- }
48
- };
49
- /** Validates the parsed payload against the route schema, returning either the validated value or a `400` response. */
50
- const validatePayload = async (parsedJson, schema) => {
51
- if (!schema) return parsedJson;
52
- const result = await standardValidate(parsedJson, schema, { throwOnError: false });
53
- if (result.issues) return Response.json({
54
- error: "validation failed",
55
- issues: result.issues.map((issue) => ({
56
- message: issue.message,
57
- path: issue.path?.map((p) => typeof p === "object" && "key" in p ? String(p.key) : String(p))
58
- }))
59
- }, { status: 400 });
60
- return result.value;
61
- };
62
- /** Invokes the route handler with the validated payload, defaulting to a `200 "ok"` response. */
63
- const executeHandler = async (handler, ctx, validatedPayload) => {
64
- return await handler({
65
- ...ctx,
66
- payload: validatedPayload
67
- }) ?? Response.json("ok");
68
- };
69
- /**
70
- * Main webhook router class.
71
- *
72
- * Register routes with typed schemas and call `handle` with a Web API `Request`.
73
- *
74
- * @example
75
- * ```ts
76
- * import { WebhookRouter } from "@zap-studio/webhooks";
77
- *
78
- * const router = new WebhookRouter({ prefix: "/webhooks" });
79
- *
80
- * router.register("/stripe", {
81
- * schema: stripeEventSchema,
82
- * handler: async ({ payload }) => {
83
- * console.log("Stripe event:", payload.type);
84
- * },
85
- * });
86
- *
87
- * export default { fetch: (request: Request) => router.handle(request) };
88
- * ```
89
- */
90
- var WebhookRouter = class {
91
- handlers = /* @__PURE__ */ new Map();
92
- verify;
93
- globalBeforeHooks = [];
94
- globalAfterHooks = [];
95
- globalErrorHook;
96
- logger;
97
- prefix;
98
- prefixWithSlash;
99
- /**
100
- * Creates a webhook router with optional global hooks and verification behavior.
101
- *
102
- * @param opts - Router-level options.
103
- *
104
- * @example
105
- * ```ts
106
- * const router = new WebhookRouter({
107
- * prefix: "/webhooks",
108
- * verify: createHmacVerifier({ headerName: "x-signature", secret }),
109
- * onError: (error) => Response.json({ error: error.message }, { status: 500 }),
110
- * });
111
- * ```
112
- */
113
- constructor(opts = {}) {
114
- this.prefix = normalizePath(opts.prefix ?? "/webhooks");
115
- this.prefixWithSlash = `${this.prefix}/`;
116
- this.verify = opts.verify;
117
- this.globalBeforeHooks = toArray(opts.before);
118
- this.globalAfterHooks = toArray(opts.after);
119
- this.globalErrorHook = opts.onError;
120
- this.logger = opts.logger;
121
- }
122
- register(path, handlerOrOptions) {
123
- this.handlers.set(normalizePath(path), typeof handlerOrOptions === "function" ? { handler: handlerOrOptions } : createHandlerEntry(handlerOrOptions));
124
- return this;
125
- }
126
- /**
127
- * Handles an incoming webhook request.
128
- *
129
- * The request body is read exactly once; hooks and handlers receive the raw
130
- * bytes through the webhook context instead of the request stream.
131
- *
132
- * @param request - Incoming Web API request.
133
- * @returns Web API response for the runtime to send back.
134
- *
135
- * @example
136
- * ```ts
137
- * // Framework-agnostic: works with any Web API Request/Response runtime.
138
- * export async function POST(request: Request): Promise<Response> {
139
- * return router.handle(request);
140
- * }
141
- * ```
142
- */
143
- async handle(request) {
144
- const requestPath = new URL(request.url).pathname;
145
- this.logger?.debug("webhook delivery attempt", { path: requestPath });
146
- const path = this.matchPath(request);
147
- if (path === null) {
148
- this.logger?.warn("webhook route not matched", { path: requestPath });
149
- return notFoundResponse();
150
- }
151
- const handlerEntry = this.handlers.get(path);
152
- if (!handlerEntry) {
153
- this.logger?.warn("webhook route not matched", { path });
154
- return notFoundResponse();
155
- }
156
- const ctx = {
157
- path,
158
- rawBody: /* @__PURE__ */ new Uint8Array(0),
159
- request
160
- };
161
- try {
162
- ctx.rawBody = new Uint8Array(await request.arrayBuffer());
163
- await runBeforeHooks(ctx, this.globalBeforeHooks);
164
- await runBeforeHooks(ctx, handlerEntry.before);
165
- if (this.verify) try {
166
- await this.verify(ctx);
167
- } catch (error) {
168
- this.logger?.warn("webhook verification failed", {
169
- error,
170
- path
171
- });
172
- throw error;
173
- }
174
- const parsedJson = parseRequestBody(ctx);
175
- const validationResult = await validatePayload(parsedJson, handlerEntry.schema);
176
- if (validationResult instanceof Response) return validationResult;
177
- this.logger?.debug("webhook handler dispatch", { path });
178
- const response = await executeHandler(handlerEntry.handler, ctx, validationResult);
179
- await runAfterHooks(ctx, response, handlerEntry.after);
180
- await runAfterHooks(ctx, response, this.globalAfterHooks);
181
- return response;
182
- } catch (error) {
183
- return await this.handleError(error, ctx);
184
- }
185
- }
186
- /** Resolves the incoming request's URL to a registered route key, or `null` if it doesn't match the configured prefix. */
187
- matchPath(request) {
188
- const pathname = normalizePath(new URL(request.url).pathname);
189
- if (this.prefix === "/") return pathname;
190
- if (pathname === this.prefix) return "/";
191
- if (!pathname.startsWith(this.prefixWithSlash)) return null;
192
- return pathname.slice(this.prefix.length);
193
- }
194
- /** Builds the error response for a failed request, deferring to the global error hook when set. */
195
- async handleError(error, ctx) {
196
- if (this.globalErrorHook) {
197
- const normalizedError = error instanceof Error ? error : /* @__PURE__ */ new Error("Internal server error");
198
- const errorResponse = await this.globalErrorHook(normalizedError, ctx);
199
- if (errorResponse) return errorResponse;
200
- }
201
- return Response.json({ error: error instanceof Error ? error.message : "Internal server error" }, { status: 500 });
202
- }
203
- };
204
- /**
205
- * Factory helper for creating a webhook router instance.
206
- *
207
- * @param opts - Optional global router options.
208
- * @returns A new webhook router.
209
- *
210
- * @example
211
- * ```ts
212
- * import { createWebhookRouter } from "@zap-studio/webhooks";
213
- *
214
- * const router = createWebhookRouter({ prefix: "/webhooks" });
215
- * router.register("/stripe", { schema: stripeEventSchema, handler });
216
- * ```
217
- */
218
- const createWebhookRouter = (opts) => new WebhookRouter(opts);
219
- //#endregion
1
+ import { n as createWebhookRouter, t as WebhookRouter } from "./router-Ba9jU5pC.js";
220
2
  export { WebhookRouter, createWebhookRouter };
221
-
222
- //# sourceMappingURL=router.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","names":[],"sources":["../src/types.ts"],"mappings":";;;;;;;;;;;;;;;;UAsBiB;;EAEf;;EAEA,SAAS;;EAET,SAAS;;;;;;;;;;;;;;UAeM,eAAe,4BAA4B;;EAE1D,SAAS;;;UAIM,aAAa;;EAE5B,QAAQ;;EA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EAEf,QAAQ,YAAY;;EAEpB,SAAS,aAAa;;EAEtB,UAAU;;;;;;;EAOV,SAAS;;;;;;;EAOT;;EAEA,SAAS;;;;;;;;;;;;;UAcM,gBAAgB;;EAE/B,QAAQ,YAAY;;EAEpB,SAAS,aAAa;;EAEtB,SAAS,eAAe;;EAExB,SAAS,0BAA0B;;;;;;;KAQzB,kBAAkB,WAC5B,gBAAgB,gCAAgC,WAAW;;;;;;;;;;;;;;KAejD,mBAAmB,gBAAgB,sCAAsC,KACnF,gBAAgB,kBAAkB;EAGlC,QAAQ;;;UAIO;;EAEf,QAAQ,YAAY;;EAEpB,SAAS,aAAa;;EAEtB,SAAS;;EAET,QAAQ;;;;;;;;;;;;;;KAeE,aAAa,gBAAgB,eAAe,iBACrD,WAAW,UAAU,mBAAmB,QAAQ;;;;;;;;;;;;;;KAgBvC,eAAe,uBACzB,KAAK,eAAe,cACjB,QAAQ,wBAAwB;;;;;;;;;;;KAYzB,WAAW,aAAa,8BACjC,WAAW,OAAO,eAAe,KAAK;;;;;;;;;;;;;KAe7B,0BAA0B,gBAAgB,eAAe,iBAClE,WAAW,UAAU,kBAAkB,QAAQ;;;;;;;;;KAWtC,YAAY,KAAK,mBAAmB;;;;;;;;;KAUpC,cAAc,KAAK,mBAAmB;;;;;;;;;;;;KAatC,aAAa,KAAK,gBAAgB,UAAU,aAAa;;;;;;;;;KAUzD,aACV,OAAO,OACP,KAAK,mBACF,QAAQ,wBAAwB"}
@@ -1 +1 @@
1
- {"version":3,"file":"verify.d.ts","names":[],"sources":["../src/verify.ts"],"mappings":";;;;;cAYa,qBAAsB,GAAG,YAAY,GAAG;cAc/C;WACJ;WACA;WACA;WACA;;KAGG,6BAA6B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAuDrB,uBACX,YACA,QACA;EAEA;EACA;EACA,OAAO;MACL"}
1
+ {"version":3,"file":"verify.d.ts","names":[],"sources":["../src/verify.ts"],"mappings":";;;;;cAaa,qBAAsB,GAAG,YAAY,GAAG;cAc/C;WACJ;WACA;WACA;WACA;;KAGG,6BAA6B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAwDrB,uBACX,YACA,QACA;EAEA;EACA;EACA,OAAO;MACL"}
package/dist/verify.js CHANGED
@@ -1,18 +1,13 @@
1
1
  import { VerificationError } from "./errors.js";
2
2
  //#region src/verify.ts
3
3
  /**
4
- * Signature verification helpers for webhook requests.
5
- *
6
- * @module @zap-studio/webhooks/verify
7
- */
8
- /**
9
4
  * Compares two byte arrays in constant time to prevent timing attacks.
10
5
  */
11
6
  const constantTimeEquals = (a, b) => {
12
7
  if (a.length !== b.length) return false;
13
8
  let result = 0;
14
9
  for (let i = 0; i < a.length; i += 1)
15
- // v8 ignore next -- `?? 0` fallback is unreachable: a Uint8Array never holds `undefined` at an in-bounds index, this exists only to satisfy noUncheckedIndexedAccess.
10
+ // v8 ignore next -- the `?? 0` fallback never runs, because a Uint8Array never holds `undefined` at a valid index. It is only here for noUncheckedIndexedAccess.
16
11
  result |= (a[i] ?? 0) ^ (b[i] ?? 0);
17
12
  return result === 0;
18
13
  };
@@ -23,6 +18,7 @@ const HMAC_HASH = {
23
18
  sha512: "SHA-512"
24
19
  };
25
20
  const HEX_PATTERN = /^[0-9a-f]*$/iu;
21
+ const SIGNATURE_PREFIX_PATTERN = /^[a-z0-9-]+=/iu;
26
22
  /**
27
23
  * Decodes a hex string into bytes, or `undefined` when it is not valid hex.
28
24
  */
@@ -32,7 +28,7 @@ const hexToBytes = (hex) => {
32
28
  for (let i = 0; i < bytes.length; i += 1) bytes[i] = Number.parseInt(hex.slice(i * 2, i * 2 + 2), 16);
33
29
  return bytes;
34
30
  };
35
- const normalizeSignature = (signature) => signature.replace(/^[a-z0-9-]+=/iu, "").trim();
31
+ const normalizeSignature = (signature) => signature.replace(SIGNATURE_PREFIX_PATTERN, "").trim();
36
32
  /**
37
33
  * Creates a webhook verifier that validates an HMAC signature from a request header.
38
34
  *
@@ -1 +1 @@
1
- {"version":3,"file":"verify.js","names":[],"sources":["../src/verify.ts"],"sourcesContent":["/**\n * Signature verification helpers for webhook requests.\n *\n * @module @zap-studio/webhooks/verify\n */\n\nimport { VerificationError } from \"./errors.js\";\nimport type { VerifyFn } from \"./types.js\";\n\n/**\n * Compares two byte arrays in constant time to prevent timing attacks.\n */\nexport const constantTimeEquals = (a: Uint8Array, b: Uint8Array): boolean => {\n if (a.length !== b.length) {\n return false;\n }\n\n let result = 0;\n for (let i = 0; i < a.length; i += 1) {\n // v8 ignore next -- `?? 0` fallback is unreachable: a Uint8Array never holds `undefined` at an in-bounds index, this exists only to satisfy noUncheckedIndexedAccess.\n result |= (a[i] ?? 0) ^ (b[i] ?? 0); // oxlint-disable-line no-bitwise -- XOR is the constant-time compare trick.\n }\n\n return result === 0;\n};\n\nconst HMAC_HASH = {\n sha1: \"SHA-1\",\n sha256: \"SHA-256\",\n sha384: \"SHA-384\",\n sha512: \"SHA-512\",\n} as const;\n\ntype HmacAlgorithm = keyof typeof HMAC_HASH;\n\nconst HEX_PATTERN = /^[0-9a-f]*$/iu;\n\n/**\n * Decodes a hex string into bytes, or `undefined` when it is not valid hex.\n */\nconst hexToBytes = (hex: string): Uint8Array | undefined => {\n if (hex.length % 2 !== 0 || !HEX_PATTERN.test(hex)) {\n return undefined;\n }\n\n const bytes = new Uint8Array(hex.length / 2);\n for (let i = 0; i < bytes.length; i += 1) {\n bytes[i] = Number.parseInt(hex.slice(i * 2, i * 2 + 2), 16);\n }\n\n return bytes;\n};\n\nconst normalizeSignature = (signature: string): string =>\n signature.replace(/^[a-z0-9-]+=/iu, \"\").trim();\n\n/**\n * Creates a webhook verifier that validates an HMAC signature from a request header.\n *\n * The verifier imports the provided string secret once, computes an HMAC from\n * `ctx.rawBody`, normalizes the incoming header value, and compares both\n * signatures in constant time.\n *\n * Header values like `sha256=<hex>` are supported so common provider formats\n * such as GitHub work without extra parsing.\n *\n * @example\n * ```ts\n * import { createWebhookRouter } from \"@zap-studio/webhooks\";\n * import { createHmacVerifier } from \"@zap-studio/webhooks/verify\";\n *\n * const router = createWebhookRouter({\n * verify: createHmacVerifier({\n * headerName: \"x-hub-signature-256\",\n * secret: process.env.GITHUB_WEBHOOK_SECRET!,\n * }),\n * });\n * ```\n *\n * @param options - Verifier configuration.\n * @param options.headerName - Header containing the provider signature.\n * @param options.secret - Shared HMAC secret as a string.\n * @param options.algo - HMAC hash algorithm. Defaults to `\"sha256\"`.\n * @returns A router-compatible request verifier.\n *\n * @throws {VerificationError}\n * Thrown when verifier setup fails or request verification does not pass.\n */\nexport const createHmacVerifier = ({\n headerName,\n secret,\n algo = \"sha256\",\n}: {\n headerName: string;\n secret: string;\n algo?: HmacAlgorithm;\n}): VerifyFn => {\n if (globalThis.crypto?.subtle === undefined) {\n throw new VerificationError(\n \"Web Crypto API is unavailable in this runtime\"\n );\n }\n\n const { subtle } = globalThis.crypto;\n\n const hash = HMAC_HASH[algo];\n if (!hash) {\n throw new VerificationError(`Unsupported HMAC algorithm: ${algo}`);\n }\n\n const keyPromise = subtle.importKey(\n \"raw\",\n new TextEncoder().encode(secret),\n { hash, name: \"HMAC\" },\n false,\n [\"sign\"]\n );\n\n return async (ctx) => {\n const actual = ctx.request.headers.get(headerName);\n if (actual === null || actual.length === 0) {\n throw new VerificationError(`Missing signature header: ${headerName}`);\n }\n\n const key = await keyPromise;\n const signature = await subtle.sign(\n \"HMAC\",\n key,\n new Uint8Array(ctx.rawBody)\n );\n const expected = new Uint8Array(signature);\n const provided = hexToBytes(normalizeSignature(actual));\n\n if (provided === undefined || !constantTimeEquals(expected, provided)) {\n throw new VerificationError(\n `Invalid signature for header: ${headerName}`\n );\n }\n };\n};\n"],"mappings":";;;;;;;;;;AAYA,MAAa,sBAAsB,GAAe,MAA2B;CAC3E,IAAI,EAAE,WAAW,EAAE,QACjB,OAAO;CAGT,IAAI,SAAS;CACb,KAAK,IAAI,IAAI,GAAG,IAAI,EAAE,QAAQ,KAAK;;CAEjC,WAAW,EAAE,MAAM,MAA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,MAAM,CAAC;EAEtD,IAAI,aAAa,KAAA,KAAa,CAAC,mBAAmB,UAAU,QAAQ,GAClE,MAAM,IAAI,kBACR,iCAAiC,YACnC;CAEJ;AACF"}
1
+ {"version":3,"file":"verify.js","names":[],"sources":["../src/verify.ts"],"sourcesContent":["/**\n * Signature verification helpers for webhook requests.\n *\n * @module @zap-studio/webhooks/verify\n */\n\nimport type { VerifyFn } from \"./types.ts\";\n\nimport { VerificationError } from \"./errors.ts\";\n\n/**\n * Compares two byte arrays in constant time to prevent timing attacks.\n */\nexport const constantTimeEquals = (a: Uint8Array, b: Uint8Array): boolean => {\n if (a.length !== b.length) {\n return false;\n }\n\n let result = 0;\n for (let i = 0; i < a.length; i += 1) {\n // v8 ignore next -- the `?? 0` fallback never runs, because a Uint8Array never holds `undefined` at a valid index. It is only here for noUncheckedIndexedAccess.\n result |= (a[i] ?? 0) ^ (b[i] ?? 0);\n }\n\n return result === 0;\n};\n\nconst HMAC_HASH = {\n sha1: \"SHA-1\",\n sha256: \"SHA-256\",\n sha384: \"SHA-384\",\n sha512: \"SHA-512\",\n} as const;\n\ntype HmacAlgorithm = keyof typeof HMAC_HASH;\n\nconst HEX_PATTERN = /^[0-9a-f]*$/iu;\nconst SIGNATURE_PREFIX_PATTERN = /^[a-z0-9-]+=/iu;\n\n/**\n * Decodes a hex string into bytes, or `undefined` when it is not valid hex.\n */\nconst hexToBytes = (hex: string): Uint8Array | undefined => {\n if (hex.length % 2 !== 0 || !HEX_PATTERN.test(hex)) {\n return undefined;\n }\n\n const bytes = new Uint8Array(hex.length / 2);\n for (let i = 0; i < bytes.length; i += 1) {\n bytes[i] = Number.parseInt(hex.slice(i * 2, i * 2 + 2), 16);\n }\n\n return bytes;\n};\n\nconst normalizeSignature = (signature: string): string =>\n signature.replace(SIGNATURE_PREFIX_PATTERN, \"\").trim();\n\n/**\n * Creates a webhook verifier that validates an HMAC signature from a request header.\n *\n * The verifier imports the provided string secret once, computes an HMAC from\n * `ctx.rawBody`, normalizes the incoming header value, and compares both\n * signatures in constant time.\n *\n * Header values like `sha256=<hex>` are supported so common provider formats\n * such as GitHub work without extra parsing.\n *\n * @example\n * ```ts\n * import { createWebhookRouter } from \"@zap-studio/webhooks\";\n * import { createHmacVerifier } from \"@zap-studio/webhooks/verify\";\n *\n * const router = createWebhookRouter({\n * verify: createHmacVerifier({\n * headerName: \"x-hub-signature-256\",\n * secret: process.env.GITHUB_WEBHOOK_SECRET!,\n * }),\n * });\n * ```\n *\n * @param options - Verifier configuration.\n * @param options.headerName - Header containing the provider signature.\n * @param options.secret - Shared HMAC secret as a string.\n * @param options.algo - HMAC hash algorithm. Defaults to `\"sha256\"`.\n * @returns A router-compatible request verifier.\n *\n * @throws {VerificationError}\n * Thrown when verifier setup fails or request verification does not pass.\n */\nexport const createHmacVerifier = ({\n headerName,\n secret,\n algo = \"sha256\",\n}: {\n headerName: string;\n secret: string;\n algo?: HmacAlgorithm;\n}): VerifyFn => {\n if (globalThis.crypto?.subtle === undefined) {\n throw new VerificationError(\"Web Crypto API is unavailable in this runtime\");\n }\n\n const { subtle } = globalThis.crypto;\n\n const hash = HMAC_HASH[algo];\n if (!hash) {\n throw new VerificationError(`Unsupported HMAC algorithm: ${algo}`);\n }\n\n const keyPromise = subtle.importKey(\n \"raw\",\n new TextEncoder().encode(secret),\n { hash, name: \"HMAC\" },\n false,\n [\"sign\"],\n );\n\n return async (ctx) => {\n const actual = ctx.request.headers.get(headerName);\n if (actual === null || actual.length === 0) {\n throw new VerificationError(`Missing signature header: ${headerName}`);\n }\n\n const key = await keyPromise;\n const signature = await subtle.sign(\"HMAC\", key, new Uint8Array(ctx.rawBody));\n const expected = new Uint8Array(signature);\n const provided = hexToBytes(normalizeSignature(actual));\n\n if (provided === undefined || !constantTimeEquals(expected, provided)) {\n throw new VerificationError(`Invalid signature for header: ${headerName}`);\n }\n };\n};\n"],"mappings":";;;;;AAaA,MAAa,sBAAsB,GAAe,MAA2B;CAC3E,IAAI,EAAE,WAAW,EA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EAClB,MAAM,YAAY,MAAM,OAAO,KAAK,QAAQ,KAAK,IAAI,WAAW,IAAI,OAAO,CAAC;EAC5E,MAAM,WAAW,IAAI,WAAW,SAAS;EACzC,MAAM,WAAW,WAAW,mBAAmB,MAAM,CAAC;EAEtD,IAAI,aAAa,KAAA,KAAa,CAAC,mBAAmB,UAAU,QAAQ,GAClE,MAAM,IAAI,kBAAkB,iCAAiC,YAAY;CAE7E;AACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zap-studio/webhooks",
3
- "version": "1.1.1",
3
+ "version": "2.0.1",
4
4
  "private": false,
5
5
  "description": "A lightweight, type-safe, tree-shakeable webhook router with Standard Schema validation, signature verification, and lifecycle hooks.",
6
6
  "keywords": [
@@ -46,18 +46,23 @@
46
46
  "access": "public"
47
47
  },
48
48
  "dependencies": {
49
- "@zap-studio/validation": "1.0.0"
49
+ "@zap-studio/validation": "1.1.1"
50
50
  },
51
51
  "devDependencies": {
52
+ "@opentelemetry/api": "^1.9.1",
53
+ "@opentelemetry/context-async-hooks": "^2.10.0",
54
+ "@opentelemetry/core": "^2.10.0",
55
+ "@opentelemetry/sdk-trace-base": "^2.10.0",
56
+ "@zap-studio/logger": "2.1.0",
57
+ "@zap-studio/typescript": "0.0.0",
52
58
  "tsdown": "^0.22.14",
53
59
  "typescript": "^7.0.2",
54
- "vitest": "^4.1.10",
55
- "zod": "^4.4.3",
56
- "@zap-studio/typescript": "0.0.0",
57
- "@zap-studio/logger": "1.0.0"
60
+ "vitest": "^4.1.11",
61
+ "zod": "^4.5.4"
58
62
  },
59
63
  "peerDependencies": {
60
- "@zap-studio/logger": "1.0.0"
64
+ "@opentelemetry/api": "^1.9.1",
65
+ "@zap-studio/logger": "2.1.0"
61
66
  },
62
67
  "peerDependenciesMeta": {
63
68
  "@zap-studio/logger": {
@@ -66,5 +71,6 @@
66
71
  },
67
72
  "engines": {
68
73
  "node": ">=18.0.0"
69
- }
74
+ },
75
+ "scripts": {}
70
76
  }
@@ -1 +0,0 @@
1
- {"version":3,"file":"router.js","names":[],"sources":["../src/router.ts"],"sourcesContent":["/**\n * Schema-first webhook router primitives.\n *\n * @module @zap-studio/webhooks/router\n */\n\nimport type { Logger } from \"@zap-studio/logger\";\nimport type { StandardSchemaV1 } from \"@zap-studio/validation\";\nimport { standardValidate } from \"@zap-studio/validation\";\n\nimport type {\n AfterHook,\n BeforeHook,\n ErrorHook,\n HandlerEntry,\n InferSchemaOutput,\n RegisterOptions,\n SchemaRouteOptions,\n VerifyFn,\n WebhookContext,\n WebhookHandler,\n WebhookRouterOptions,\n} from \"./types.js\";\n\n/**\n * Schema-first webhook router with path dispatching, validation, and optional verification.\n *\n * @template TMap - Internal route payload map built incrementally via `register`.\n */\n\nconst toArray = <T>(value: T | T[] | undefined): T[] => {\n if (value === undefined) {\n return [];\n }\n\n return Array.isArray(value) ? value : [value];\n};\n\nconst notFoundResponse = (): Response =>\n Response.json({ error: \"not found\" }, { status: 404 });\n\nconst bodyDecoder = new TextDecoder();\n\n/**\n * Normalizes a path to its canonical form: leading slash, no trailing slash,\n * duplicate slashes collapsed. The root path is `\"/\"`.\n */\nconst normalizePath = (path: string): string => {\n const withLeadingSlash = path.startsWith(\"/\") ? path : `/${path}`;\n const collapsed = withLeadingSlash.includes(\"//\")\n ? withLeadingSlash.replaceAll(/\\/{2,}/gu, \"/\")\n : withLeadingSlash;\n\n return collapsed.length > 1 && collapsed.endsWith(\"/\")\n ? collapsed.slice(0, -1)\n : collapsed;\n};\n\n/** Runs the given before-hooks in order against the request context. */\nconst runBeforeHooks = async (\n ctx: WebhookContext,\n hooks?: BeforeHook[]\n): Promise<void> => {\n if (!hooks || hooks.length === 0) {\n return;\n }\n\n for (const hook of hooks) {\n // oxlint-disable-next-line no-await-in-loop -- hooks run sequentially; order + short-circuit matter.\n await hook(ctx);\n }\n};\n\n/** Runs the given after-hooks in order against the request context and response. */\nconst runAfterHooks = async (\n ctx: WebhookContext,\n response: Response,\n hooks?: AfterHook[]\n): Promise<void> => {\n if (!hooks || hooks.length === 0) {\n return;\n }\n\n for (const hook of hooks) {\n // oxlint-disable-next-line no-await-in-loop -- hooks run sequentially; order + short-circuit matter.\n await hook(ctx, response);\n }\n};\n\n/** Builds an internal handler entry from route registration options. */\nconst createHandlerEntry = (\n options: RegisterOptions<unknown>\n): HandlerEntry => {\n const entry: HandlerEntry = {\n handler: options.handler,\n };\n\n if (options.schema !== undefined) {\n entry.schema = options.schema;\n }\n\n if (options.before !== undefined) {\n entry.before = toArray(options.before);\n }\n\n if (options.after !== undefined) {\n entry.after = toArray(options.after);\n }\n\n return entry;\n};\n\n/** Parses the request's raw body bytes as JSON, returning `undefined` on invalid JSON. */\nconst parseRequestBody = (ctx: WebhookContext): unknown => {\n try {\n return JSON.parse(bodyDecoder.decode(ctx.rawBody));\n } catch {\n return undefined;\n }\n};\n\n/** Validates the parsed payload against the route schema, returning either the validated value or a `400` response. */\nconst validatePayload = async <TPayload>(\n parsedJson: unknown,\n schema?: StandardSchemaV1<unknown, TPayload>\n): Promise<TPayload | Response> => {\n if (!schema) {\n // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- Without a schema, caller-declared payload type is the route contract.\n return parsedJson as TPayload;\n }\n\n const result = await standardValidate(parsedJson, schema, {\n throwOnError: false,\n });\n\n if (result.issues) {\n return Response.json(\n {\n error: \"validation failed\",\n issues: result.issues.map((issue) => ({\n message: issue.message,\n path: issue.path?.map((p) =>\n typeof p === \"object\" && \"key\" in p ? String(p.key) : String(p)\n ),\n })),\n },\n { status: 400 }\n );\n }\n\n return result.value;\n};\n\n/** Invokes the route handler with the validated payload, defaulting to a `200 \"ok\"` response. */\nconst executeHandler = async <TPayload = unknown>(\n handler: WebhookHandler<TPayload>,\n ctx: WebhookContext,\n validatedPayload: TPayload\n): Promise<Response> => {\n const responded = await handler({\n ...ctx,\n payload: validatedPayload,\n });\n\n return responded ?? Response.json(\"ok\");\n};\n\n/**\n * Main webhook router class.\n *\n * Register routes with typed schemas and call `handle` with a Web API `Request`.\n *\n * @example\n * ```ts\n * import { WebhookRouter } from \"@zap-studio/webhooks\";\n *\n * const router = new WebhookRouter({ prefix: \"/webhooks\" });\n *\n * router.register(\"/stripe\", {\n * schema: stripeEventSchema,\n * handler: async ({ payload }) => {\n * console.log(\"Stripe event:\", payload.type);\n * },\n * });\n *\n * export default { fetch: (request: Request) => router.handle(request) };\n * ```\n */\nexport class WebhookRouter<TMap = unknown> {\n private readonly handlers = new Map<string, HandlerEntry>();\n private readonly verify: VerifyFn | undefined;\n private readonly globalBeforeHooks: BeforeHook[] = [];\n private readonly globalAfterHooks: AfterHook[] = [];\n private readonly globalErrorHook: ErrorHook | undefined;\n private readonly logger: Logger | undefined;\n private readonly prefix: string;\n private readonly prefixWithSlash: string;\n\n /**\n * Creates a webhook router with optional global hooks and verification behavior.\n *\n * @param opts - Router-level options.\n *\n * @example\n * ```ts\n * const router = new WebhookRouter({\n * prefix: \"/webhooks\",\n * verify: createHmacVerifier({ headerName: \"x-signature\", secret }),\n * onError: (error) => Response.json({ error: error.message }, { status: 500 }),\n * });\n * ```\n */\n constructor(opts: WebhookRouterOptions = {}) {\n this.prefix = normalizePath(opts.prefix ?? \"/webhooks\");\n this.prefixWithSlash = `${this.prefix}/`;\n this.verify = opts.verify;\n this.globalBeforeHooks = toArray(opts.before);\n this.globalAfterHooks = toArray(opts.after);\n this.globalErrorHook = opts.onError;\n this.logger = opts.logger;\n }\n\n /**\n * Register a webhook handler for a specific path.\n *\n * When a schema is provided, `payload` is inferred from the schema output type.\n *\n * @param path - Route path relative to configured prefix, starting with `/` (e.g. `\"/stripe\"`).\n * @param handlerOrOptions - Handler function or schema-based registration options.\n * @returns The same router instance with an updated internal route type map.\n *\n * @example\n * ```ts\n * router.register(\"/stripe\", {\n * schema: stripeEventSchema,\n * handler: async ({ payload }) => {\n * console.log(payload.type); // typed from stripeEventSchema\n * },\n * });\n * ```\n */\n register<\n Path extends `/${string}`,\n TSchema extends StandardSchemaV1<unknown, unknown>,\n >(\n path: Path,\n handlerOrOptions: SchemaRouteOptions<TSchema>\n ): WebhookRouter<TMap & Record<Path, InferSchemaOutput<TSchema>>>;\n /**\n * Register a webhook handler for a specific path, with schema-less registration options.\n *\n * @param path - Route path relative to configured prefix, starting with `/` (e.g. `\"/stripe\"`).\n * @param handlerOrOptions - Registration options without a schema.\n * @returns The same router instance with an updated internal route type map.\n *\n * @example\n * ```ts\n * router.register(\"/ping\", {\n * before: (ctx) => console.log(\"received\", ctx.path),\n * handler: () => Response.json({ ok: true }),\n * });\n * ```\n */\n register<Path extends `/${string}`, TPayload>(\n path: Path,\n handlerOrOptions: RegisterOptions<TPayload>\n ): WebhookRouter<TMap & Record<Path, TPayload>>;\n /**\n * Register a webhook handler for a specific path, using a plain handler function.\n *\n * @param path - Route path relative to configured prefix, starting with `/` (e.g. `\"/stripe\"`).\n * @param handlerOrOptions - Handler function to process the webhook.\n * @returns The same router instance with an updated internal route type map.\n *\n * @example\n * ```ts\n * router.register(\"/health\", () => Response.json({ status: \"ok\" }));\n * ```\n */\n register<Path extends `/${string}`>(\n path: Path,\n handlerOrOptions: WebhookHandler\n ): WebhookRouter<TMap & Record<Path, unknown>>;\n register(\n path: string,\n handlerOrOptions: WebhookHandler | RegisterOptions<unknown>\n ): this {\n this.handlers.set(\n normalizePath(path),\n typeof handlerOrOptions === \"function\"\n ? { handler: handlerOrOptions }\n : createHandlerEntry(handlerOrOptions)\n );\n\n return this;\n }\n\n /**\n * Handles an incoming webhook request.\n *\n * The request body is read exactly once; hooks and handlers receive the raw\n * bytes through the webhook context instead of the request stream.\n *\n * @param request - Incoming Web API request.\n * @returns Web API response for the runtime to send back.\n *\n * @example\n * ```ts\n * // Framework-agnostic: works with any Web API Request/Response runtime.\n * export async function POST(request: Request): Promise<Response> {\n * return router.handle(request);\n * }\n * ```\n */\n async handle(request: Request): Promise<Response> {\n const requestPath = new URL(request.url).pathname;\n this.logger?.debug(\"webhook delivery attempt\", { path: requestPath });\n\n const path = this.matchPath(request);\n if (path === null) {\n this.logger?.warn(\"webhook route not matched\", { path: requestPath });\n return notFoundResponse();\n }\n\n const handlerEntry = this.handlers.get(path);\n if (!handlerEntry) {\n this.logger?.warn(\"webhook route not matched\", { path });\n return notFoundResponse();\n }\n\n const ctx: WebhookContext = {\n path,\n rawBody: new Uint8Array(0),\n request,\n };\n\n try {\n ctx.rawBody = new Uint8Array(await request.arrayBuffer());\n\n await runBeforeHooks(ctx, this.globalBeforeHooks);\n await runBeforeHooks(ctx, handlerEntry.before);\n\n if (this.verify) {\n try {\n await this.verify(ctx);\n } catch (error) {\n this.logger?.warn(\"webhook verification failed\", { error, path });\n throw error;\n }\n }\n\n const parsedJson = parseRequestBody(ctx);\n const validationResult = await validatePayload(\n parsedJson,\n handlerEntry.schema\n );\n\n if (validationResult instanceof Response) {\n return validationResult;\n }\n\n this.logger?.debug(\"webhook handler dispatch\", { path });\n const response = await executeHandler(\n handlerEntry.handler,\n ctx,\n validationResult\n );\n\n await runAfterHooks(ctx, response, handlerEntry.after);\n await runAfterHooks(ctx, response, this.globalAfterHooks);\n\n return response;\n } catch (error) {\n return await this.handleError(error, ctx);\n }\n }\n\n /** Resolves the incoming request's URL to a registered route key, or `null` if it doesn't match the configured prefix. */\n private matchPath(request: Request): string | null {\n const pathname = normalizePath(new URL(request.url).pathname);\n\n // Root mount: the whole pathname is the route path.\n if (this.prefix === \"/\") {\n return pathname;\n }\n\n if (pathname === this.prefix) {\n return \"/\";\n }\n\n // Require prefix followed by a segment boundary, then match handlers on\n // the remainder (e.g. /webhooks/stripe -> /stripe).\n if (!pathname.startsWith(this.prefixWithSlash)) {\n return null;\n }\n\n return pathname.slice(this.prefix.length);\n }\n\n /** Builds the error response for a failed request, deferring to the global error hook when set. */\n private async handleError(\n error: unknown,\n ctx: WebhookContext\n ): Promise<Response> {\n if (this.globalErrorHook) {\n const normalizedError =\n error instanceof Error ? error : new Error(\"Internal server error\");\n const errorResponse = await this.globalErrorHook(normalizedError, ctx);\n if (errorResponse) {\n return errorResponse;\n }\n }\n\n return Response.json(\n {\n error: error instanceof Error ? error.message : \"Internal server error\",\n },\n { status: 500 }\n );\n }\n}\n\n/**\n * Factory helper for creating a webhook router instance.\n *\n * @param opts - Optional global router options.\n * @returns A new webhook router.\n *\n * @example\n * ```ts\n * import { createWebhookRouter } from \"@zap-studio/webhooks\";\n *\n * const router = createWebhookRouter({ prefix: \"/webhooks\" });\n * router.register(\"/stripe\", { schema: stripeEventSchema, handler });\n * ```\n */\nexport const createWebhookRouter = (\n opts?: WebhookRouterOptions\n): WebhookRouter => new WebhookRouter(opts);\n"],"mappings":";;;;;;;AA8BA,MAAM,WAAc,UAAoC;CACtD,IAAI,UAAU,KAAA,GACZ,OAAO,CAAC;CAGV,OAAO,MAAM,QAAQ,KAAK,IAAI,QAAQ,CAAC,KAAK;AAC9C;AAEA,MAAM,yBACJ,SAAS,KAAK,EAAE,OAAO,YAAY,GAAG,EAAE,QAAQ,IAAI,CAAC;AAEvD,MAAM,cAAc,IAAI,YAAY;;;;;AAMpC,MAAM,iBAAiB,SAAyB;CAC9C,MAAM,mBAAmB,KAAK,WAAW,GAAG,IAAI,OAAO,IAAI;CAC3D,MAAM,YAAY,iBAAiB,SAAS,IAAI,IAC5C,iBAAiB,WAAW,YAAY,GAAG,IAC3C;CAEJ,OAAO,UAAU,SAAS,KAAK,UAAU,SAAS,GAAG,IACjD,UAAU,MAAM,GAAG,EAAE,IACrB;AACN;;AAGA,MAAM,iBAAiB,OACrB,KACA,UACkB;CAClB,IAAI,CAAC,SAAS,MAAM,WAAW,GAC7B;CAGF,KAAK,MAAM,QAAQ,OAEjB,MAAM,KAAK,GAAG;AAElB;;AAGA,MAAM,gBAAgB,OACpB,KACA,UACA,UACkB;CAClB,IAAI,CAAC,SAAS,MAAM,WAAW,GAC7B;CAGF,KAAK,MAAM,QAAQ,OAEjB,MAAM,KAAK,KAAK,QAAQ;AAE5B;;AAGA,MAAM,sBACJ,YACiB;CACjB,MAA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