Repository navigation
Expand file tree
/
Copy pathllms.txt
More file actions
322 lines (251 loc) · 20.7 KB
/
Copy pathllms.txt
File metadata and controls
322 lines (251 loc) · 20.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# green-tea
> green-tea (`@green-tea/core`) is a TypeScript HTTP framework for Node >= 22, Deno, Bun and Cloudflare Workers in which the request pipeline is a dependency graph, not a middleware chain. Providers (built once at boot) and steps (run per request) declare the tokens they `provides` and `needs`; green-tea derives the execution order, validates the wiring when the app is created, and runs only the part of the graph each route's handler asks for. Handlers declare their inputs with parameter decorators and return plain values. It is not Express, NestJS, Fastify or Hono: there is no `app.use`, no `next()`, no `req`/`res` to mutate, and no constructor injection. This file ships with the package and matches the installed version; read it before writing green-tea code.
## Mental model
- The app is a graph of tokens (strings). A `@Provider({ provides: 'db' })` builds an app-scope singleton once, at boot. A `@Step({ provides: 'user', needs: ['db', 'req'] })` runs per request.
- Order is derived from `needs`/`provides` by a topological sort. Never order things by hand.
- A handler declares its dependencies in its signature: `getUser(@needs('user') user, @param('id') id)`. The signature is the contract.
- A route runs only the transitive closure of its handler's `@needs`. A step nothing on that route needs does not run.
- The graph is validated by `createApp`: a needed token with no producer throws `handler 'x' needs 'usr' but nothing (local or mesh) provides it — did you mean 'user'?`.
- One producer per token: two nodes providing the same token fail `createApp`. A node cannot need the token it provides (`cycle detected`).
- `provide()` and `run(ctx)` return an object keyed by the token (`{ db }`, `{ user }`). It is merged into one per-request context object. Throw to stop the request.
- No `app.use`, no `next()`, no `req.user = ...`, no `res.status().json()`. Return a value; throw an `HttpError` for an error status.
- Classes are instantiated by the framework with no constructor arguments, once. Dependencies arrive through `ctx` (providers/steps) or argument decorators (handlers).
## Setup
`npm install @green-tea/core@rc` (each prerelease publishes under its own dist-tag, `rc` for release candidates and `beta` for betas, and until the first stable release `latest` follows the newest one; optional peers: `ws` for `@Ws` on Node, `busboy` for multipart uploads). In `tsconfig.json` set `"experimentalDecorators": true`. `emitDecoratorMetadata` is not needed, and core imports `reflect-metadata` itself.
## Minimal complete app
```ts
import { createApp, Module, Provider, Step, Route, Get, Post, needs, param, body, Unauthorized, NotFound } from '@green-tea/core';
type User = { id: string; name: string };
@Provider({ provides: 'db' })
class Database {
provide() {
const users: Record<string, User> = { u1: { id: 'u1', name: 'Diego' } };
return { db: { find: (id: string): User | null => users[id] ?? null } }; // keyed by the token
}
}
@Step({ provides: 'user', needs: ['db', 'req'] })
class Authenticate {
run(ctx: any) {
const user = ctx.db.find(ctx.headers['x-token']);
if (!user) throw new Unauthorized('invalid token'); // stops the request -> 401
return { user }; // merged into ctx; continues
}
}
@Route('/users')
class UserController {
@Get('/me')
me(@needs('user') user: User) { return user; } // 200 JSON; static /me wins over /:id
@Get('/:id')
byId(@param('id') id: string, @needs('db') db: { find(id: string): User | null }) {
const found = db.find(id);
if (!found) throw new NotFound(`no user ${id}`);
return found;
}
@Post('/')
create(@body() input: { name: string }) { return { created: input.name }; }
}
@Module({ mountpoint: '/api', providers: [Database], steps: [Authenticate], controllers: [UserController] })
class ApiModule {}
export const app = createApp({ modules: [ApiModule] });
```
Serve it. Each entry point boots providers before binding, so a provider that fails rejects the call:
```ts
await app.listen(3000); // Node: always await; rejects on EADDRINUSE or a failed provider
import { serveDeno } from '@green-tea/core/deno';
await serveDeno(app, { port: 8000 }); // Deno (async since beta.2: await it)
import { serveBun } from '@green-tea/core/bun';
await serveBun(app, { port: 3000 }); // Bun (async: await it)
import { edgeHandler } from '@green-tea/core/edge';
export default { fetch: edgeHandler(app) }; // Cloudflare Workers; needs compatibility_flags = ["nodejs_compat"]
```
Use one entry point per file. `mountpoint: ''` mounts at the root. A controller is served only when listed in a module's `controllers`. `await app.boot()` boots without serving; `await app.close()` drains and runs teardown. The edge has no `listen()`, no filesystem (`static`, `@Html` file modes), no mesh and no teardown.
## Cheat sheet
### Class decorators
| Decorator | Signature | Notes |
|---|---|---|
| `@Module` | `Module({ mountpoint: string; providers?: Ctor[]; steps?: Ctor[]; controllers?: Ctor[] })` | `mountpoint` is required |
| `@Route` | `Route(prefix: string)` | marks a controller. There is no `@Controller` |
| `@Provider` | `Provider({ provides: string; needs?: string[]; optional?: boolean })` | class has `provide(ctx)`, optional `dispose()`. No `export` option |
| `@Step` | `Step({ provides: string; needs?: string[]; optional?: boolean; export?: boolean })` | class has `run(ctx)`. `export` is mesh-only |
### Method decorators
| Decorator | Signature | Transport |
|---|---|---|
| `@Get @Post @Put @Patch @Delete @Head @Options` | `(path: string, opts?: { duplicates?: 'array' \| 'last'; maxBodyBytes?: number; maxParts?: number; export?: boolean })` | buffered: must return a value |
| `@Sse` | same | GET, `text/event-stream`: must return an `AsyncIterable` |
| `@Stream` | same | GET, negotiated: SSE (`Accept: text/event-stream`), WebSocket (`Upgrade`), else ndjson |
| `@Ws` | same | GET, WebSocket duplex: must return an `AsyncIterable` |
| `@Transformer` | `Transformer(fn: TransformerFn)` | custom status/headers/body for one handler |
| `@Html` | bare `@Html`, or `Html(path: string, opts?: { template?: boolean })` | buffered GET/POST only; not combinable with `@Transformer` |
Paths: `/:id`, constrained `/:id(\\d+)`, catch-all `/:path*`. Unmatched path -> 404, wrong method -> 405, a buffered GET answers HEAD, OPTIONS is automatic.
### Argument decorators
| Decorator | Injects | Forms |
|---|---|---|
| `@needs(key)` | a graph token (boot-validated edge) | `('user')` |
| `@ctx()` | the whole context (`ctx.ip`, `ctx.protocol`, tokens) | `()` |
| `@param` | route params | `()` `('id')` `('id', schema)` |
| `@query` | parsed query string (values are strings) | `()` `('q')` `(['a','b'])` `(schema)` |
| `@body` | parsed JSON / urlencoded / multipart body | `()` `('field')` `(schema)` |
| `@headers` / `@header` | request headers, lower-case keys; lookup is case-insensitive | `()` `('authorization')` `(['a','b'])` `(schema)` / `('x-trace')` `('x-count', schema)` |
| `@cookies` / `@cookie` | parsed `Cookie` header; `@cookie` is `undefined` when absent | `()` `('sid')` `(['a','b'])` `(schema)` / `('sid')` `('sid', schema)` |
| `@inbound()` | incoming WebSocket messages (`AsyncIterable`) | `()` |
| `@abort()` | `AbortSignal` that fires on disconnect | `()` |
### Tokens
- A handler may `@needs` any produced token plus the request tokens `req`, `params`, `query`, `body`, `headers`, `cookies`, `inbound`, `abort`.
- A provider's or step's `needs` may list produced tokens, `req` and `params` only. `needs: ['headers']` (or `query`, `body`, `cookies`, `ip`) fails `createApp` with `missing dependency: headers needed by <step>`. Declare `needs: ['req']` and read `ctx.headers`, `ctx.query`, `ctx.body`, `ctx.params`, `ctx.req`, `ctx.ip`, `ctx.protocol`. A step does not get `ctx.cookies`: use `parseCookies(ctx.headers.cookie)`.
- Built-in tokens: `logger` (the app `Logger`: `debug/info/warn/error(message, fields?)`), `rooms` (shared `Rooms`: `room<T>(name)` returns a multicast `Channel<T>`), `events` (read-only `{ on(event, listener) }` of the lifecycle bus).
- Reserved names: `logger`, `rooms`, `events`, `bus`. Providing one fails boot. `@needs('bus')` fails boot; use `@needs('events')` or a plugin.
### What `provide()` and `run()` return
- `provide(ctx)` runs once at boot (may be async). `ctx` holds the values of its `needs`. It must return `{ [provides]: value }`. Returning anything else fails the boot (`provider 'db' returned …, but it must return … { db: <value> }`); a step's `run()` that does so fails the request.
- `run(ctx)` runs per request (may be async). Return `{ [provides]: value }` to continue; throw to short-circuit. The returned object is `Object.assign`ed into the shared context.
- Optional `dispose()` on a provider is awaited by `close()`, in reverse boot order (not on the edge).
- `optional: true` on a provider: a failing boot is logged and degrades instead of aborting (`app.degraded()`).
- One instance per class for the app's life: never keep per-request state on `this` in a step.
## Returning responses
- Plain value (object, array, string, number, `null`) -> `200`, JSON-encoded. A returned string is sent as a JSON string; use `@Html` for HTML.
- `undefined` (no `return`) -> `204`, no body.
- Never return a `Response`: it fails the request with a `TypeError`. Return a plain value; use `@Transformer` for status and headers.
- Errors are thrown, not returned: `Unauthorized(msg?)` 401, `NotFound(msg?)` 404, `Redirect(location)` 302 + `Location`, `NotModified()` 304, `ValidationError` 422 (raised by schemas), `HttpError(status, message?, body?, headers?)` any status. Anything else -> 500 `{"error":"Internal Server Error"}`; the message is not leaked. Default body: `{"error": message}`, or `body` when given.
- Check errors with `isHttpError(e)` / `isValidationError(e)`, not `instanceof` (fails across two copies of core).
- Custom rendering of every error response (including 404/405/413): `createApp({ onError(error, req) { return { status, headers, body } | undefined } })`.
- Header values are `string | string[]`. A `set-cookie` array is sent as one line per cookie; other arrays are joined with `, `.
```ts
import { Route, Post, Transformer, setCookie, HttpError, type TransformerFn } from '@green-tea/core';
const created: TransformerFn = (value) => ({
status: 201,
headers: { 'content-type': 'application/json', 'set-cookie': [setCookie('session', 'abc', { maxAge: 3600 }), setCookie('theme', 'dark', { httpOnly: false })] },
body: JSON.stringify(value),
});
@Route('/auth')
class Sessions {
@Post('/login') @Transformer(created)
login() { return { ok: true }; } // -> 201 with two Set-Cookie lines
@Post('/logout')
logout() { throw new HttpError(401, 'signed out', undefined, { 'set-cookie': setCookie('session', '', { maxAge: 0 }) }); }
}
```
`setCookie(name, value, options?)` defaults to `HttpOnly; SameSite=Lax; Path=/; Secure` and throws on unsafe input. Pass `secure: false` for plain-HTTP dev hosts the browser will not trust. Sessions and signing are not in core.
## Validation, streaming, HTML
Pass any Standard Schema (zod >= 3.24, valibot, arktype) to `@body`, `@query`, `@headers`, `@param`. The handler gets the parsed/coerced output; failure -> `422 {"error":"Validation failed","source":"body","issues":[{"path","message"}]}`. `@param` takes the key first: `@param('id', IdSchema)`. Steps always see the raw `ctx.body`/`ctx.query`.
```ts
import { z } from 'zod';
import { Route, Post, Get, Sse, Ws, body, query, needs, inbound, header, sse, Rooms } from '@green-tea/core';
const CreateUser = z.object({ email: z.string().email() });
const Page = z.object({ page: z.coerce.number() });
@Route('/live')
class Live {
@Post('/users')
create(@body(CreateUser) user: { email: string }) { return { created: user.email }; }
@Get('/list')
list(@query(Page) q: { page: number }) { return { page: q.page }; }
@Sse('/ticks') // each yield is one SSE event
ticks(@header('last-event-id') from?: string) {
return (async function* () {
for (let n = Number(from ?? 0) + 1; n <= 3; n++) yield sse({ n }, { id: String(n) });
})();
}
@Ws('/room/:name') // consume @inbound, return the outbound iterable
join(@needs('rooms') rooms: Rooms, @needs('params') p: { name: string }, @inbound() incoming: AsyncIterable<string>) {
const room = rooms.room<string>(p.name);
(async () => { for await (const msg of incoming) room.push(msg); })();
return room;
}
}
```
- `channel<T>({ buffer? })` is a multicast `AsyncIterable` with `push`, `close`, `fail` (bounded, drop-oldest). Yield plain values for bare `data:` frames; `sse(data, { id, event, retry })` adds fields.
- The decorator decides the transport, never the return type: an iterable from `@Get`, or a value from `@Sse`/`@Ws`, is a 500 `TransportMismatchError`.
- A step that throws `Unauthorized` on a WebSocket route closes the socket with `4000 + status` (4401).
- `@Html` returns a string as `text/html`; `@Html('page.html')` serves a file (relative to `views`, default `process.cwd()`); `@Html('page.html', { template: true })` renders it with the returned data (`{{ }}` engine, or `viewEngine`). `static: true` serves `./public`. No filesystem modes on the edge.
## createApp options agents commonly need
```ts
const app = createApp({
modules: [ApiModule], // required
security: true, // default: nosniff, X-Frame-Options, Referrer-Policy, HSTS on TLS; false or SecurityOptions
cors: { origins: ['https://app.example.com'], credentials: true }, // origins: string | string[] | '*' | (origin) => boolean
trustProxy: false, // true: X-Forwarded-* feed ctx.ip / ctx.protocol
limits: { maxBodyBytes: 1_000_000, requestTimeoutMs: 30_000, maxConcurrentRequests: 0 },
health: true, // GET/HEAD /__live__ and /__ready__
compression: false, // true or { threshold, encodings: ['br', 'gzip'] }
etag: false, // weak ETag + 304 for buffered GET/HEAD
static: false, // true -> ./public, a dir string, or { dir, cacheControl }
logger: undefined, // any { debug, info, warn, error }; default structured JSON
logRequests: false, // one log line per request
handleSignals: false, // true: SIGINT/SIGTERM run close() and exit
overrides: {}, // tests: replace a token's value or runner
});
```
Also: `plugins`, `hooks: [{ onShutdown }]`, `onError`, `shutdownTimeoutMs` (default 10000), `teardownTimeoutMs`, `devGraph` (`GET /__graph__`, dev only), `devOpenapi`, `bodyDuplicates`, `views`, `viewEngine`, `warnGraphDepth`. `tls: { key, cert }` works only with Node's `app.listen()`; `serveDeno()` and `serveBun()` reject an app that sets it. Use the platform or a proxy for TLS there.
To run something on every route regardless of `@needs` (global guard, audit), add a plugin step that provides nothing:
```ts
import { Unauthorized, type Plugin } from '@green-tea/core';
export const apiKey: Plugin = {
name: 'api-key',
mount(api) {
api.scope.add({ kind: 'step', name: 'apiKey', needs: ['req'], provides: [], run: (ctx) => {
if (ctx.headers['x-api-key'] !== process.env.API_KEY) throw new Unauthorized('bad key');
return {};
} });
},
};
// createApp({ modules: [ApiModule], plugins: [apiKey] })
```
A plugin gets only `bus.on`, `scope.add` and `onShutdown`; it cannot reorder or remove other nodes.
The official plugins (`@green-tea/jwt`, `@green-tea/metrics`, `@green-tea/rate-limit`) ship no llms.txt: read the installed one's `node_modules/@green-tea/<name>/README.md`.
## Common mistakes
1. `app.use(authMiddleware)` -> a `@Step({ provides: 'user', needs: ['req'] })` that handlers `@needs('user')`, or a plugin step with `provides: []` for every route.
2. Calling `next()` -> `run(ctx)` returns `{ token: value }` to continue and throws to stop.
3. `req.user = user` -> `return { user }` from the step, and `@needs('user') user` in the handler.
4. `res.status(201).json(x)`, `res.cookie()`, `return new Response(...)` -> return the value, plus `@Transformer(fn)` for status/headers/`setCookie()`. A returned `Response` is sent as `{}`.
5. `provide() { return new Pool() }` -> `provide() { return { db: new Pool() } }`. The bare value fails the boot.
6. NestJS constructor injection (`constructor(private db: Db)`) -> nothing is injected. Use `needs` + `ctx.db` in providers/steps, `@needs('db')` in handlers.
7. `@Step({ needs: ['headers'] })` (or `query`, `body`, `cookies`) -> boot error. Use `needs: ['req']` and read `ctx.headers`. For cookies: `parseCookies(ctx.headers.cookie)`.
8. Expecting an auth step to protect every route -> it runs only where its token is needed. Guard with `@needs('user')` on each protected handler, or a `provides: []` plugin step.
9. `@Controller('/users')` -> `@Route('/users')`, listed in a `@Module({ controllers: [...] })`.
10. Returning an async generator from `@Get` -> declare `@Sse`, `@Stream` or `@Ws`. Returning a value from `@Sse` fails too.
11. Returning an HTML string from `@Get` -> sent as a JSON string. Add `@Html`.
12. `@needs('bus')` or providing `logger`/`rooms`/`events` -> boot error. Use `@needs('events')`, `@needs('logger')`, `@needs('rooms')`.
13. Two classes `provides: 'user'` -> boot error. One producer per token; rename one, or use `overrides` in tests.
14. `e instanceof HttpError` -> `isHttpError(e)`.
15. `app.listen(3000)` without `await`, or `serveDeno(app)` without `await` -> boot and bind failures go unhandled. Always await.
16. `@param(IdSchema)` -> `@param('id', IdSchema)`.
17. Multipart `@body('title')` is `undefined` -> `@body() form: MultipartBody`, then `form.fields.title` / `form.files.avatar`.
18. Inventing a token in `@needs('x')` -> check `matcha tokens` or `app.graph()` first; unknown tokens fail boot.
## Testing
No port needed: `app.fetch(new Request(...))` returns a `Response` on every runtime.
```ts
import { createApp } from '@green-tea/core';
const test = createApp({
modules: [ApiModule],
overrides: {
db: { find: () => ({ id: 't', name: 'Test' }) }, // plain value: wrapped as { db: value }
user: () => ({ user: { id: 'stub' } }), // a function is the runner: return { token: value }
},
});
const res = await test.fetch(new Request('http://test/api/users/me'));
// res.status === 200; await res.json() -> { id: 'stub' }
```
- An override must name an existing token; an unknown one throws on `boot()` or the first `fetch`, not in `createApp`.
- A function value is treated as a runner. To override a token whose value is a function, wrap it: `{ hash: () => ({ hash: fakeHash }) }`.
## Inspect an existing app before editing it
```ts
app.explain('/api/users/me');
// { pattern, method: 'GET', transport: 'buffer', chain: [{ name, kind: 'provider'|'step'|'handler', origin, needs, provides }, ...] }
app.graph(); // { nodes: [{ name, kind, origin, needs, provides }], routes: [{ pattern, method, transport, chain: string[] }] }
app.toMermaid(); // or app.toDOT()
```
`explain` takes the declared pattern (`/users/:id`, not `/users/42`) and throws `no route: ...` on a miss. `graph().nodes` includes the built-ins (`origin: 'builtin'`).
With matcha (the CLI; `routes`, `tokens` and `--json` need a matcha that lists them in `matcha --help`) run from the project root. It imports `export const app` from `src/app.ts` (override with `--entry`) and calls `app.ready()`, so providers are not booted:
```bash
matcha routes --json # every route: method, pattern, transport, chain
matcha tokens --json # every token: who provides it, who needs it
matcha explain /users/:id --json # one route's chain, in execution order
matcha graph --format json # the raw GraphView
```
Run `matcha tokens` before adding `@needs('x')`: if nothing provides `x`, you must add the provider or step too. Request tokens (`req`, `params`, `body`, ...) are listed as coming from the request. Other generators: `matcha new`, `matcha create controller|step|provider|module|transformer <Name>` (wires the class into its module), `matcha create plugin` (in-app, or `--package` for its own package), `matcha add sse|stream|buffer|ws|upload|cookie`.
## Stability
- The public API of `@green-tea/core`, `/deno`, `/bun` and `/edge` is frozen from RC.1: between RC.1 and stable, fixes only. Check `### Breaking` in CHANGELOG.md.
- Mesh (`mesh` option, `connectLink`, `MeshConfig`, teapot/teacup) is alpha and `@experimental`, gated by `createApp({ experimental: true })`. Do not use it unless the user asks.
- The `Headers` type is deprecated; import `RequestHeaders` instead.
## Links
- Docs: https://green-tea.expressive-tea.io/docs/
- Full docs in one file: https://green-tea.expressive-tea.io/docs/llms-full.txt
- Source: https://github.com/Expressive-Tea/green-tea