Files
core/example/server.ts
T
chrnx 75f9583068
CI / dco (pull_request) Successful in 4s
CI / runtimes (bun) (pull_request) Successful in 21s
CI / build (pull_request) Successful in 31s
CI / runtimes (edge) (pull_request) Successful in 23s
CI / runtimes (deno) (pull_request) Successful in 34s
docs: bring the contributor docs and examples in line with the repository
CONTRIBUTING said none of the Deno, Bun or edge suites run in CI. They do:
the runtimes job runs all three on every push and gates a merge. It now
says so, names test:all, and lists the audit step CI runs.

The README's benchmark section quoted a ranking against Fastify that went
stale with the next measurement. It now explains what the harness measures
and why, and leaves every number to BENCHMARKS.md. The install section
states the Node 22 floor and that Node 18 and 20 are not supported.

The PR template asks which runtime suites were run. The example README
pointed at ts-node, which is not a dependency, and a dist path tsup never
builds; it now runs each example with tsx. example/server.ts crashed at
boot because its teacup lacked experimental: true.

docs/plans held agent implementation plans from August and September.
Those belong in .specs/, which is not committed, so they leave the
repository. The three places that linked the mesh degrade plan now
describe the planned expects list instead.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
2026-10-28 14:32:03 -05:00

131 lines
4.0 KiB
TypeScript

import { createApp, Provider, Step, Route, Get, Module, Transformer, JsonTransformer, Unauthorized, Plugin, needs, param, Sse, Ws, inbound, channel, MESH_CONTROL_PATH } from '../src/index';
interface User { id: string; name: string }
@Provider({ provides: 'db' })
class Database {
provide() {
const users: Record<string, User> = { u1: { id: 'u1', name: 'Diego' } };
return { db: { find: (token: string) => users[token] ?? null } };
}
}
@Step({ provides: 'user', needs: ['db', 'req'] })
class Authenticate {
run(ctx: { db: { find: (t: string) => User | null }; req: { headers: Record<string, string> } }) {
const user = ctx.db.find(ctx.req.headers['x-token']);
if (!user) throw new Unauthorized('invalid token');
return { user };
}
}
@Route('/users')
class UserController {
@Get('/:id')
@Transformer(JsonTransformer)
getUser(@needs('user') user: User, @param('id') id: string) {
return { requested: id, you: user };
}
}
@Module({ mountpoint: '/api', providers: [Database], steps: [Authenticate], controllers: [UserController] })
class ApiModule {}
const logger: Plugin = {
name: 'step-logger',
mount(api) {
api.bus.on('request:step:enter', (p) => console.log(`-> ${p.name}`));
},
};
export const app = createApp({ modules: [ApiModule], plugins: [logger] });
@Route('/feed')
class FeedController {
@Sse('/ticks')
ticks() {
return (async function* () {
for (let n = 1; n <= 3; n++) yield { tick: n };
})();
}
}
@Route('/chat')
class ChatController {
@Ws('/echo')
echo(@inbound() incoming: AsyncIterable<string>) {
const out = channel<string>();
(async () => {
for await (const msg of incoming) out.push(`echo: ${msg}`);
out.close();
})();
return out;
}
}
@Module({ mountpoint: '/', controllers: [FeedController, ChatController] })
class StreamModule {}
export const streamApp = createApp({ modules: [StreamModule] });
if (require.main === module) {
console.log('chain for /api/users/:id ->', app.inspect('/api/users/:id'));
console.log('graph:\n' + app.toMermaid());
console.log('explain /api/users/:id:', app.explain('/api/users/:id'));
app.listen(3000).then(() => console.log('green-tea on http://localhost:3000'));
streamApp.listen(3001).then(() => console.log('green-tea streams on http://localhost:3001'));
}
// ── Mesh Demo (teapot / teacup) ───────────────────────────────────────────
@Step({ provides: 'config', needs: [], export: true })
class ConfigStep {
run() {
return { config: { env: 'demo' } };
}
}
@Step({ provides: 'auth', needs: [], export: true })
class AuthStep {
run(ctx: { headers?: Record<string, string | string[] | undefined> }) {
const tok = ctx.headers?.['x-token'];
return { auth: Array.isArray(tok) ? tok[0] ?? 'anonymous' : tok ?? 'anonymous' };
}
}
@Route('/svc')
class PingController {
@Get('/ping', { export: true })
ping(@needs('config') config: { env: string }, @needs('auth') auth: string) {
return { pong: true, config, auth };
}
}
@Module({ mountpoint: '/mesh', steps: [ConfigStep, AuthStep], controllers: [PingController] })
class TeapotModule {}
export const teapotApp = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: 'demo-secret' } });
@Route('/remote')
class TeacupController {
@Get('/status')
status(@needs('config') config: { env: string }, @needs('auth') auth: string) {
return { via: 'teacup', config, auth };
}
}
@Module({ mountpoint: '/', controllers: [TeacupController] })
class TeacupModule {}
if (require.main === module) {
teapotApp.listen(3002).then(() => {
console.log('teapot on http://localhost:3002');
const teacupApp = createApp({
modules: [TeacupModule],
experimental: true,
mesh: { teapots: [{ url: `ws://127.0.0.1:3002${MESH_CONTROL_PATH}`, secret: 'demo-secret' }] },
});
teacupApp.listen(3003).then(() => console.log('teacup on http://localhost:3003'));
});
}