Merge pull request 'docs: list every package, and show the graph doing the work' (#4) from docs/steps-and-index into develop
This commit was merged in pull request #4.
This commit is contained in:
commit
3e8707034c
3 files changed
+63
-2
No files matched your search
@@ -7,6 +7,8 @@ and why not the others.
|
||||
| Package | What it does |
|
||||
|---|---|
|
||||
| [`@green-tea/jwt`](packages/jwt) | Loads signing keys at boot and publishes a sign/verify registry |
|
||||
| [`@green-tea/rate-limit`](packages/rate-limit) | A per-request limiter with a branded `429` and a swept in-memory store |
|
||||
| [`@green-tea/metrics`](packages/metrics) | Counts the lifecycle stream and renders the Prometheus text format |
|
||||
|
||||
Plugins import core as types only, so installing one never pulls a second copy of core — and never
|
||||
warns about a version range, which under CalVer no range can express.
|
||||
@@ -31,6 +31,8 @@ const app = createApp({
|
||||
});
|
||||
```
|
||||
|
||||
Signing belongs in the handler — it is the response to that one request:
|
||||
|
||||
```ts
|
||||
@Post('/signin')
|
||||
async signIn(@needs('jwt') jwt: JwtRegistry) {
|
||||
@@ -38,6 +40,31 @@ async signIn(@needs('jwt') jwt: JwtRegistry) {
|
||||
}
|
||||
```
|
||||
|
||||
Verifying does not. It is a precondition shared by every protected route, and its result is what
|
||||
those routes are actually asking for, so it belongs in a `@Step`:
|
||||
|
||||
```ts
|
||||
@Step({ provides: 'user', needs: ['jwt', 'req'] })
|
||||
class Authenticate {
|
||||
async run(ctx) {
|
||||
const header = ctx.req.headers.authorization ?? '';
|
||||
if (!header.startsWith('Bearer ')) throw new Unauthorized('missing bearer token');
|
||||
|
||||
// verify() throws on a bad signature, a wrong issuer or an expired token, which cuts the
|
||||
// request before the handler — the handler only ever sees a token that was good.
|
||||
return { user: await ctx.jwt.get('access').verify(header.slice(7)) };
|
||||
}
|
||||
}
|
||||
|
||||
@Get('/me')
|
||||
me(@needs('user') user: Claims) {
|
||||
return user;
|
||||
}
|
||||
```
|
||||
|
||||
The handler asks for `user`, never for `jwt`, and green-tea runs `Authenticate` only for the routes
|
||||
that do — the route table says which ones, rather than a matcher on a middleware chain.
|
||||
|
||||
Keys are read once, while providers boot. A missing or malformed key therefore fails `app.listen()` —
|
||||
or `await app.boot()` on Deno and Bun — instead of the first sign-in of the day. Call `app.boot()`
|
||||
before `Deno.serve` or `Bun.serve`; without it, the failure surfaces on every request instead.
|
||||
|
||||
@@ -31,14 +31,46 @@ const app = createApp({
|
||||
});
|
||||
```
|
||||
|
||||
Then spend the limit in a `@Step`, so the handler stays the thing it is about and the limit is a
|
||||
dependency rather than a first line of code someone can forget to write:
|
||||
|
||||
```ts
|
||||
@Step({ provides: 'signinAllowed', needs: ['rateLimit', 'req'] })
|
||||
class ThrottleSignin {
|
||||
async run(ctx) {
|
||||
await ctx.rateLimit('signin'); // throws 429, so the handler never runs
|
||||
return { signinAllowed: true };
|
||||
}
|
||||
}
|
||||
|
||||
@Post('/signin')
|
||||
async signIn(@needs('rateLimit') limit: RateLimiter) {
|
||||
await limit('signin');
|
||||
async signIn(@needs('signinAllowed') _allowed: true, @body() credentials: Credentials) {
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
**The step has to provide something the handler asks for.** green-tea slices the graph per route and
|
||||
runs only what the handler transitively needs, so a step providing a token nobody names is not in the
|
||||
closure and never runs — the limit would silently not apply. That is why there is a
|
||||
`signinAllowed` at all.
|
||||
|
||||
Better still, fold it into a step that already provides something real. A protected route usually
|
||||
has one:
|
||||
|
||||
```ts
|
||||
@Step({ provides: 'user', needs: ['jwt', 'rateLimit', 'req'] })
|
||||
class Authenticate {
|
||||
async run(ctx) {
|
||||
await ctx.rateLimit('api'); // spend it before the expensive part
|
||||
return { user: await verifyBearer(ctx) };
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Calling `@needs('rateLimit')` straight from the handler and awaiting it there works too, and for a
|
||||
single route it is less machinery. It just puts the limit somewhere a reader of the signature cannot
|
||||
see it.
|
||||
|
||||
Exceeding a rule throws a `429` carrying `Retry-After`. The error is recognised by green-tea through
|
||||
a registered symbol rather than by extending its `HttpError`, so this package never imports core at
|
||||
runtime.
|
||||
|
||||
Reference in new issue
Block a user