feat(mesh): teacup remote node and proxy-route builder

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-06-27 20:33:15 -05:00
1 parent f151da4bd7
commit a1e8f73e64
2 files changed
+68

No files matched your search

+35
View File
@@ -0,0 +1,35 @@
import type { Link } from './link';
import type { RequestEnvelope } from './protocol';
import type { ResponseShape } from '../pipeline';
export interface RemoteScopeNode { name: string; run: (ctx: any) => Promise<Record<string, unknown>> }
export interface RemoteRoute { method: string; pattern: string; handler: (req: RequestEnvelope) => Promise<ResponseShape> }
export function envelopeFrom(ctx: any): RequestEnvelope {
return {
method: ctx?.req?.method ?? ctx?.method ?? 'GET',
params: ctx?.params ?? {},
query: ctx?.query ?? {},
body: ctx?.body,
headers: ctx?.headers ?? {},
};
}
export function buildRemote(link: Link, origin: string): {
providers: RemoteScopeNode[]; steps: RemoteScopeNode[]; routes: RemoteRoute[];
} {
const providers: RemoteScopeNode[] = [];
const steps: RemoteScopeNode[] = [];
for (const s of link.manifest.scopes) {
const node: RemoteScopeNode = {
name: s.token,
run: async (ctx: any) => ({ [s.token]: await link.rpc('scope', s.token, envelopeFrom(ctx)) }),
};
(s.scope === 'app' ? providers : steps).push(node);
}
const routes: RemoteRoute[] = link.manifest.routes.map((r) => ({
method: r.method, pattern: r.pattern,
handler: async (req: RequestEnvelope) => (await link.rpc('route', r.pattern, req)) as ResponseShape,
}));
return { providers, steps, routes };
}
+33
View File
@@ -0,0 +1,33 @@
import { describe, it, expect } from 'vitest';
import { buildRemote, envelopeFrom } from '../../src/mesh/teacup';
import type { Link } from '../../src/mesh/link';
const fakeLink = (calls: any[]): Link => ({
manifest: { scopes: [{ token: 'config', scope: 'app' }, { token: 'auth', scope: 'request' }], routes: [{ method: 'GET', pattern: '/u/:id' }] },
rpc: async (kind, name, ctx) => { calls.push({ kind, name, ctx }); return name === 'config' ? { url: 'db://x' } : { id: 'u1' }; },
close() {},
});
describe('teacup buildRemote', () => {
it('makes a provider node (app) and a step node (request) with RPC runners returning merge objects', async () => {
const calls: any[] = [];
const { providers, steps } = buildRemote(fakeLink(calls), 'ws://t');
expect(providers.map((p) => p.name)).toEqual(['config']);
expect(steps.map((s) => s.name)).toEqual(['auth']);
expect(await providers[0].run({})).toEqual({ config: { url: 'db://x' } });
const ctx = { req: {}, params: { id: '9' }, query: {}, body: undefined, headers: { x: '1' } };
expect(await steps[0].run(ctx)).toEqual({ auth: { id: 'u1' } });
expect(calls.find((c) => c.name === 'auth').ctx).toMatchObject({ params: { id: '9' }, headers: { x: '1' } });
});
it('makes a proxy route entry with method+pattern', async () => {
const calls: any[] = [];
const { routes } = buildRemote(fakeLink(calls), 'ws://t');
expect(routes[0]).toMatchObject({ method: 'GET', pattern: '/u/:id' });
});
it('envelopeFrom extracts only serializable request fields', () => {
const env = envelopeFrom({ method: 'POST', params: { a: '1' }, query: { q: 'x' }, body: { n: 1 }, headers: { h: 'v' }, db: { find() {} } } as any);
expect(env).toEqual({ method: 'POST', params: { a: '1' }, query: { q: 'x' }, body: { n: 1 }, headers: { h: 'v' } });
});
});