@samva/webhooks 0.49.0 → 0.51.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.
Files changed (3) hide show
  1. package/AGENTS.md +4 -0
  2. package/README.md +48 -0
  3. package/package.json +4 -3
package/AGENTS.md CHANGED
@@ -9,3 +9,7 @@ optional peer `effect` from the `^4.0.4` range this package's `peerDependencies`
9
9
 
10
10
  Import only package subpaths declared in `package.json`. The delivery, queue, persistence, retry,
11
11
  and endpoint-management implementation is not part of this package.
12
+
13
+ To test a handler's failing-signature path, sign a fixture with `signWebhook` and a secret from
14
+ `generateWebhookSecret`, then change one byte of the body or drop a header: `verify` rejects with
15
+ `WebhookSignatureError` or `InvalidWebhookHeadersError`. The README has a runnable example.
package/README.md CHANGED
@@ -52,3 +52,51 @@ For separate decoding, import `MessageBouncedEventSchema` or `SamvaWebhookEventS
52
52
 
53
53
  Signing secrets are `whsec_` followed by Base64-encoded secret bytes. Copy the entire secret from
54
54
  Samva; the suffix is Base64, not hex or Base64url.
55
+
56
+ ## Test your handler
57
+
58
+ `generateWebhookSecret` and `signWebhook` produce the same signed bytes Samva sends, so a test can
59
+ exercise the success path and the failing-signature path without a live delivery. The fixture below
60
+ uses an event type the package does not know, which passes through as an untyped event; a known type
61
+ needs a payload that matches its schema.
62
+
63
+ ```ts
64
+ import {
65
+ generateWebhookSecret,
66
+ InvalidWebhookHeadersError,
67
+ safeVerify,
68
+ signWebhook,
69
+ verify,
70
+ WebhookSignatureError,
71
+ } from "@samva/webhooks";
72
+
73
+ const secret = generateWebhookSecret();
74
+ const body = JSON.stringify({
75
+ type: "example.test",
76
+ timestamp: new Date().toISOString(),
77
+ data: { note: "fixture" },
78
+ });
79
+ const id = "evt_test_1";
80
+ const timestamp = Math.floor(Date.now() / 1000);
81
+ const signature = await signWebhook({ body, id, timestamp, secret });
82
+ const delivery = { body, id, timestamp: String(timestamp), signature, secret };
83
+
84
+ await verify(delivery); // resolves { id, event }
85
+
86
+ // A body changed by one byte fails the signature check.
87
+ await verify({ ...delivery, body: `${body} ` }).catch((error) => {
88
+ console.log(error instanceof WebhookSignatureError); // true
89
+ });
90
+
91
+ // An absent header fails before the signature is read.
92
+ await verify({ ...delivery, signature: undefined }).catch((error) => {
93
+ console.log(error instanceof InvalidWebhookHeadersError); // true
94
+ });
95
+
96
+ // safeVerify returns the failure instead of rejecting.
97
+ console.log((await safeVerify({ ...delivery, body: `${body} ` })).verified); // false
98
+ ```
99
+
100
+ In an HTTP test, send the same `webhook-id`, `webhook-timestamp`, and `webhook-signature` header
101
+ values (the `WEBHOOK_*_HEADER` constants) with the signed body, then repeat the request with a
102
+ changed body and expect your handler to answer `4xx`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@samva/webhooks",
3
- "version": "0.49.0",
3
+ "version": "0.51.0",
4
4
  "description": "Runtime-neutral verification for Samva webhook deliveries",
5
5
  "keywords": [
6
6
  "samva",
@@ -50,7 +50,8 @@
50
50
  "./schemas": {
51
51
  "types": "./dist/schemas.gen.d.ts",
52
52
  "import": "./dist/schemas.gen.js"
53
- }
53
+ },
54
+ "./package.json": "./package.json"
54
55
  },
55
56
  "publishConfig": {
56
57
  "access": "public"
@@ -73,7 +74,7 @@
73
74
  "effect": "^4.1.4",
74
75
  "tsdown": "0.23.7",
75
76
  "typescript": "^7.0.2",
76
- "vitest": "5.1.0"
77
+ "vitest": "5.1.1"
77
78
  },
78
79
  "peerDependencies": {
79
80
  "effect": "^4.0.4"