diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..4cf1e99 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,56 @@ +# Contributing to green-tea + +Thanks for your interest. green-tea is open source, but not anyone can push directly — contributions land through reviewed pull/merge requests with a **DCO sign-off**. + +## Developer Certificate of Origin (DCO) + +We use the [Developer Certificate of Origin](./DCO) instead of a CLA. It's a lightweight, per-commit affirmation that you have the right to submit your work under the project's license. No copyright assignment, no paperwork. + +**Every commit must be signed off.** Add the `-s` flag: + +```bash +git commit -s -m "feat: add the thing" +``` + +This appends a trailer to your commit message: + +``` +Signed-off-by: Your Name +``` + +The name and email must match your real `git config user.name` / `user.email`. Commits without a valid `Signed-off-by` line are rejected. + +To sign off a range you forgot to sign: + +```bash +git rebase --signoff +``` + +## Branch model (GitFlow) + +- `main` — production-ready, protected. No direct pushes. +- `develop` — active development, protected. Feature branches merge here. +- `feature/` — branch from `develop`. +- `hotfix/` — branch from `main`. +- `release/` — release stabilization. + +`main` and `develop` are protected: they require a reviewed MR and signed-off commits. (Branch protection is configured on the git server, not in this repo.) + +## Commits + +Follow [Conventional Commits](https://www.conventionalcommits.org/): `feat:`, `fix:`, `chore:`, `docs:`, `test:`, `refactor:`. Focus the message on the *why*. Do not add AI co-authoring attribution. + +## Before you open an MR + +```bash +npm test # all tests must pass +npm run typecheck # must be clean — this includes the compile-time-guarantee type test +``` + +- Write tests for new behavior (TDD welcome). We use Vitest. +- Keep runtime dependencies at zero beyond `reflect-metadata`. +- New public API needs a doc note in `README.md`. + +## Reporting issues + +Open an issue with a minimal reproduction: green-tea version, Node version, and the smallest module/route that shows the problem. diff --git a/DCO b/DCO new file mode 100644 index 0000000..8201f99 --- /dev/null +++ b/DCO @@ -0,0 +1,37 @@ +Developer Certificate of Origin +Version 1.1 + +Copyright (C) 2004, 2006 The Linux Foundation and its contributors. +1 Letterman Drive +Suite D4700 +San Francisco, CA, 94129 + +Everyone is permitted to copy and distribute verbatim copies of this +license document, but changing it is not allowed. + + +Developer's Certificate of Origin 1.1 + +By making a contribution to this project, I certify that: + +(a) The contribution was created in whole or in part by me and I + have the right to submit it under the open source license + indicated in the file; or + +(b) The contribution is based upon previous work that, to the best + of my knowledge, is covered under an appropriate open source + license and I have the right under that license to submit that + work with modifications, whether created in whole or in part + by me, under the same open source license (unless I am + permitted to submit under a different license), as indicated + in the file; or + +(c) The contribution was provided directly to me by some other + person who certified (a), (b) or (c) and I have not modified + it. + +(d) I understand and agree that this project and the contribution + are public and that a record of the contribution (including all + personal information I submit with it, including my sign-off) is + maintained indefinitely and may be redistributed consistent with + this project or the open source license(s) involved. diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..eae7524 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 green-tea contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..7fa2a2e --- /dev/null +++ b/README.md @@ -0,0 +1,101 @@ +# 🍵 green-tea + +> A declarative, type-safe HTTP framework for Node. The request pipeline is an explicit **dependency graph**, not a mutable middleware chain. + +`@green-tea/core` — **alpha**. Zero runtime dependencies beyond `reflect-metadata`. + +## Why + +Express/Fastify middleware is positional, untyped, and globally mutable: order depends on the line you wrote `app.use()` on, `req.user` may or may not exist, and a plugin can quietly delete your body parser. green-tea replaces that model: + +- **Declarative, not positional.** You declare what each step *needs* and *produces*. The graph order is computed at boot via topological sort — there is no "put this before that". +- **The type is the contract.** What a handler reads from the context *is* its dependency declaration. The typed `flow` core makes a handler that reads `ctx.user` **fail to compile** if no step produces `user`. +- **Structural plugin isolation.** Plugins get only `bus.on(...)` (observe) and `scope.add(...)` (extend their own scope). There is no API to reorder or delete another scope's steps. +- **Response = return value.** No `Response.json()`. Return data; a `@Transformer` serializes it in a leave phase. Cut a request by `throw`ing a typed signal. +- **Fail-fast boot.** A required provider that can't initialize (e.g. the DB) stops the server from serving — no 500s because a resource wasn't ready. + +## Quick look + +```typescript +import { + createApp, Provider, Step, Route, Get, Module, + Transformer, JsonTransformer, Unauthorized, needs, param, +} from '@green-tea/core'; + +@Provider({ provides: 'db' }) +class Database { + provide() { + const users = { u1: { id: 'u1', name: 'Diego' } }; + return { db: { find: (token: string) => users[token] ?? null } }; + } +} + +@Step({ provides: 'user', needs: ['db', 'req'] }) +class Authenticate { + run(ctx) { + const user = ctx.db.find(ctx.req.headers['x-token']); + if (!user) throw new Unauthorized('invalid token'); // cut the request + return { user }; // continue + } +} + +@Route('/users') +class UserController { + @Get('/:id') + @Transformer(JsonTransformer) + getUser(@needs('user') user, @param('id') id) { // the signature IS the contract + return { requested: id, you: user }; + } +} + +@Module({ mountpoint: '/api', providers: [Database], steps: [Authenticate], controllers: [UserController] }) +class ApiModule {} + +const app = createApp({ modules: [ApiModule] }); +console.log(app.inspect('/api/users/:id')); // auditable: ordered chain with origins +app.listen(3000); +``` + +```bash +curl -H 'x-token: u1' http://localhost:3000/api/users/9 +# {"requested":"9","you":{"id":"u1","name":"Diego"}} +``` + +## Argument decorators + +The handler signature declares exactly what it wants — in any order, nothing more. + +| Decorator | Injects | Forms | +|---|---|---| +| `@needs('user')` | a graph-produced value (boot-validated) | `('key')` | +| `@ctx()` | the whole accumulated context | `()` | +| `@param(...)` | route params | `()` · `('id')` · `(['a','b'])` | +| `@query(...)` | parsed query string | `()` · `('q')` · `(['a','b'])` | +| `@body(...)` | parsed JSON body | `()` · `('field')` · `(['a','b'])` | +| `@headers(...)` | request headers | `()` · `('authorization')` · `(['a','b'])` | + +`@needs` keys are validated at boot: if nothing provides the key, `createApp` throws with a clear error instead of serving `undefined`. + +## Two layers + +1. **Typed functional core (`flow`)** — `flow().step(...).step(...).handle(...)`; step outputs accumulate into the context type (`Acc & Out`). This is where the compile-time guarantee lives. +2. **Declarative decorator layer** — `@Provider/@Step/@Module/@Route/@Get/@Transformer` + argument decorators. Emits runtime metadata, builds and topologically sorts the graph, validates at boot. + +## Development + +```bash +npm install +npm test # vitest +npm run typecheck # tsc --noEmit (includes the compile-time-guarantee type test) +npm run build # emit dist/ +``` + +## Roadmap + +- **streams** — native WebSocket/SSE + reactive streams (steps that open an observed channel). +- **mesh** — `teapot`/`teacup`: distributed scopes over a persistent channel with auto-registration. Model: a BEAM/OTP-style cluster, *not* microservices. The core's `resolve()` is already async + transport-agnostic to allow this without a rewrite. +- official plugins (cors, body parsers, auth) — structurally unable to sabotage the global pipeline. + +## License + +[MIT](./LICENSE) © green-tea contributors. Contributions require a [DCO sign-off](./CONTRIBUTING.md).