briefkasten 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +128 -2
- package/dist/index.d.mts +3072 -0
- package/dist/index.mjs +3324 -0
- package/package.json +60 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Lukas Mateffy
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,129 @@
|
|
|
1
|
-
#
|
|
1
|
+
# briefkasten
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Webhooks for TypeScript apps. briefkasten checks each provider's signature, turns the payload
|
|
4
|
+
into a typed event and runs your handlers. Use it in one of two ways:
|
|
5
|
+
|
|
6
|
+
- **Embedded**: briefkasten runs inside your app, and your app receives the webhooks itself.
|
|
7
|
+
- **Client**: your app connects out to a briefkasten gateway (briefkasten cloud, or one you host)
|
|
8
|
+
and receives its events over a WebSocket. Your server needs no public URL.
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install briefkasten @briefkasten/adapter-github
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
The adapters are `@briefkasten/adapter-github`, `@briefkasten/adapter-linear`,
|
|
15
|
+
`@briefkasten/adapter-nightwatch`, `@briefkasten/adapter-posthog`, `@briefkasten/adapter-sentry`
|
|
16
|
+
and `@briefkasten/adapter-generic`. The CLI is `@briefkasten/cli`, the gateway to host yourself
|
|
17
|
+
`@briefkasten/server` (Node) and `@briefkasten/cloudflare` (Workers). Node 22 or newer.
|
|
18
|
+
|
|
19
|
+
Docs: <https://briefkasten.fabrik.computer/docs>
|
|
20
|
+
|
|
21
|
+
## Embedded
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { briefkasten } from "briefkasten";
|
|
25
|
+
import { github } from "@briefkasten/adapter-github";
|
|
26
|
+
|
|
27
|
+
export const hooks = briefkasten({
|
|
28
|
+
sources: { github: github({ secret: process.env.GITHUB_WEBHOOK_SECRET ?? "" }) },
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
hooks.on("github:pull_request.opened", async (event, context) => {
|
|
32
|
+
// event.body is GitHub's pull request payload, typed
|
|
33
|
+
context.log(`#${event.body.pull_request.number} opened in ${event.body.repository.full_name}`);
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
A source's name is the last part of its URL: the source `github` takes the deliveries posted to
|
|
38
|
+
`/webhooks/github`. Mount the app:
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
// Hono, Next.js (app/webhooks/[...source]/route.ts), a Cloudflare Worker: a fetch handler
|
|
42
|
+
app.post("/webhooks/*", (c) => hooks.fetch(c.req.raw));
|
|
43
|
+
|
|
44
|
+
// Express or node:http: before express.json(), since signatures are checked on the raw body
|
|
45
|
+
app.use("/webhooks", hooks.node);
|
|
46
|
+
app.use(express.json());
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
| Case | Answer |
|
|
50
|
+
| -------------------------- | ----------------------------- |
|
|
51
|
+
| Signature checks out | `202`, then your handlers run |
|
|
52
|
+
| Signature missing or wrong | `401`, and no handler runs |
|
|
53
|
+
| Unknown source in the path | `404` |
|
|
54
|
+
| Body too large | `413` |
|
|
55
|
+
|
|
56
|
+
Handlers run after the answer, one event after another. A handler that throws runs again with
|
|
57
|
+
backoff (`retry: { attempts, backoffMs }`, default 5 attempts from 1 s). Events are kept for
|
|
58
|
+
`hooks.replay(id)` (in memory by default, or in the `store` you give).
|
|
59
|
+
|
|
60
|
+
## Client
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
import { connect } from "briefkasten";
|
|
64
|
+
import type { GithubEvents } from "@briefkasten/adapter-github";
|
|
65
|
+
|
|
66
|
+
const inbox = connect<{ github: GithubEvents }>({
|
|
67
|
+
url: "https://briefkasten.example",
|
|
68
|
+
token: process.env.BRIEFKASTEN_TOKEN ?? "", // a subscriber token, bk_sub_…
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
inbox.on("github:pull_request.opened", async (event) => {
|
|
72
|
+
await review(event.body.pull_request);
|
|
73
|
+
});
|
|
74
|
+
inbox.on("error", (error) => console.warn(error));
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
`connect()` opens a WebSocket at once (SSE when no WebSocket opens) and keeps it open. After any
|
|
78
|
+
drop it reconnects with backoff and resumes after the last event it handled. Options: `filters`
|
|
79
|
+
(only these patterns travel over the wire), `project`, `since` (start after this event id), and
|
|
80
|
+
`onEvent` (after each handled event, e.g. to store its id for the next start). A refused token,
|
|
81
|
+
or a tenant that was suspended or deleted, ends the connection with a `BriefkastenError`
|
|
82
|
+
(`unauthorized`, `tenant_suspended`, `not_found`), said to the `error` handlers.
|
|
83
|
+
|
|
84
|
+
## Patterns
|
|
85
|
+
|
|
86
|
+
`on()` and `filters` take a glob over `source:event`, or over the full address
|
|
87
|
+
`project:adapter:source:target:event`. `*` matches within one part, `**` across parts:
|
|
88
|
+
`github:push`, `github:pull_request.*`, `*:issue.created`, `acme-shop:github:*:acme/shop:**`.
|
|
89
|
+
With a known source, `event.body` is typed by the event's name.
|
|
90
|
+
|
|
91
|
+
## Sending webhooks
|
|
92
|
+
|
|
93
|
+
Each adapter has typed builders for its provider's deliveries, made from its samples with the
|
|
94
|
+
fields you give, and signs them the way the provider does. `send` posts one, a real delivery that
|
|
95
|
+
goes through the receiver's verification:
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
import { send, withValues } from "briefkasten";
|
|
99
|
+
import { github } from "@briefkasten/adapter-github";
|
|
100
|
+
|
|
101
|
+
await send({
|
|
102
|
+
url: hookUrl, // the source's hook URL
|
|
103
|
+
secret, // the source's secret
|
|
104
|
+
delivery: github.events.pullRequest.opened({ repo: "acme/shop", number: 42, title: "Fix login" }),
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
// Any value of the payload, by its path:
|
|
108
|
+
const draft = withValues(github.events.pullRequest.opened(), { "pull_request.draft": true });
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
From the command line, `briefkasten send <source>` does the same: `--event pull_request.opened`,
|
|
112
|
+
`--set pull_request.title="Fix login"`, `--json <payload>`, and for simulated traffic
|
|
113
|
+
`--every 5s`, `--count N` and `--random` (random events with random repositories, numbers, titles
|
|
114
|
+
and authors). It needs the source's secret: a local config has it, a remote target never shows
|
|
115
|
+
it again (`--secret`).
|
|
116
|
+
|
|
117
|
+
**`send` or `source test`?** `send` posts from where you are to the hook URL, signed with the
|
|
118
|
+
secret, like the provider: it tests the whole way in, verification included. `briefkasten source
|
|
119
|
+
test` asks the gateway to run a sample through the source itself, with no secret: what the
|
|
120
|
+
dashboard and the MCP tools use, and the quick check that routes and subscribers see an event.
|
|
121
|
+
|
|
122
|
+
## Errors
|
|
123
|
+
|
|
124
|
+
Everything throws a `BriefkastenError` with a `code` (`unauthorized`, `not_found`,
|
|
125
|
+
`rate_limited`, …) and the HTTP `status`.
|
|
126
|
+
|
|
127
|
+
## License
|
|
128
|
+
|
|
129
|
+
MIT
|