Files
core/test/deno/mesh-interop.test.ts
T
chrnx 9634ec73e8
CI / dco (pull_request) Successful in 6s
CI / build (pull_request) Successful in 30s
CI / runtimes (bun) (pull_request) Successful in 16s
CI / runtimes (deno) (pull_request) Successful in 20s
CI / runtimes (edge) (pull_request) Successful in 13s
feat(runtime)!: serveDeno and serveBun boot before they bind
Both were synchronous and left the boot to the first request, with the fix
documented as something the caller should remember: `await app.boot()` before
`Deno.serve` / `Bun.serve`. That made the correct use of a core helper depend
on reading a README, in a framework whose whole argument is that order should
not be something you have to get right.

The failure it leaves is the bad kind. `app.fetch` boots lazily and the boot
memo keeps the *rejection*, so a missing signing key does not fail the deploy:
the port binds, every request is answered 500 by the runtime, and none of them
reach `onError`. Anything that health-checks by asking whether the process is
listening sees a healthy server.

`listen()` has always awaited `bootApp()` before binding its socket, with a
comment saying it exists so Node and Deno/Bun serve an identical graph. They
did not. Now the three helpers agree, and the two adapters return promises.

`edgeHandler` is deliberately unchanged: on workerd there is no startup outside
a request, so there is no earlier moment to move the failure to.

Both new tests were confirmed to fail with the gate commented out — without it
`serveBun` resolves instead of rejecting, and serveDeno binds a port that 500s.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
2026-09-19 12:38:50 -05:00

171 lines
5.4 KiB
TypeScript

// Cross-runtime mesh: a Node peer and a Deno peer, in separate processes, talking over the
// wire protocol. This is the test the whole cross-runtime effort exists for.
//
// Deno<->Deno and Node<->Node passing prove each runtime works with itself; neither proves a
// Node teapot can serve a Deno teacup, which is the shape mesh actually deploys in once it
// spans runtimes. The two sides use genuinely different code here — Node serves through the
// `ws` package (WebSocketServer + nodeSocket) and connects with it too; Deno uses its global
// WebSocket both ways — so only a real cross-process run exercises the pairing.
//
// Run with: npm run test:deno (needs --allow-run to spawn the Node peer)
import 'npm:reflect-metadata';
import { assertEquals, assertStringIncludes } from 'jsr:@std/assert';
import { createApp, Step, Route, Get, Module, needs } from '../../src/index.ts';
const SECRET = 's3cr3t';
const NODE_PEER = 'test/interop/_node-peer.ts';
@Step({ provides: 'config', needs: [], export: true })
class Config {
run() {
return { config: { region: 'mx', runtime: 'deno' } };
}
}
@Step({ provides: 'auth', needs: [], export: true })
class Auth {
run(ctx: any) {
return { auth: { token: ctx.headers?.['x-token'] ?? 'anon' } };
}
}
@Module({ mountpoint: '/api', steps: [Config, Auth] })
class TeapotModule {}
@Route('/local')
class LocalCtl {
@Get('/who')
who(@needs('config') config: any, @needs('auth') auth: any) {
return { config, auth };
}
}
@Module({ mountpoint: '/api', controllers: [LocalCtl] })
class TeacupModule {}
const controlUrl = (port: number) => `ws://127.0.0.1:${port}/__mesh__/control`;
/** Spawns the Node peer under tsx, streaming its stdout lines. */
function spawnNodePeer(args: string[]) {
const child = new Deno.Command('npx', {
args: ['tsx', NODE_PEER, ...args],
env: { ...Deno.env.toObject(), MESH_SECRET: SECRET },
stdout: 'piped',
stderr: 'piped',
}).spawn();
return child;
}
/** Reads stdout until a JSON line parses, or the peer dies / we run out of patience. */
async function firstJsonLine(child: Deno.ChildProcess, timeoutMs = 30_000): Promise<any> {
const reader = child.stdout.getReader();
const decoder = new TextDecoder();
let buffer = '';
const deadline = Date.now() + timeoutMs;
try {
while (Date.now() < deadline) {
const { value, done } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
for (const line of buffer.split('\n')) {
const trimmed = line.trim();
if (!trimmed.startsWith('{')) continue;
try {
return JSON.parse(trimmed);
} catch {
/* partial line; keep reading */
}
}
}
} finally {
reader.releaseLock();
}
throw new Error(`node peer produced no JSON line within ${timeoutMs}ms`);
}
async function kill(child: Deno.ChildProcess): Promise<void> {
try {
child.kill('SIGKILL');
} catch {
/* already gone */
}
await child.status;
try {
await child.stdout.cancel();
await child.stderr.cancel();
} catch {
/* already released */
}
}
Deno.test('interop: a Deno teacup consumes a Node teapot', async () => {
const node = spawnNodePeer(['teapot']);
const { port } = await firstJsonLine(node);
const teacup = createApp({
modules: [TeacupModule],
experimental: true,
mesh: { teapots: [{ url: controlUrl(port), secret: SECRET }], timeoutMs: 5000 },
});
try {
const res = await teacup.fetch(new Request('http://x/api/local/who', { headers: { 'x-token': 'abc' } }));
assertEquals(res.status, 200);
// the config came from the Node process: proof the remote step crossed the runtime boundary
assertEquals(await res.json(), {
config: { region: 'mx', runtime: 'node' },
auth: { token: 'abc' },
});
} finally {
await teacup.close();
await kill(node);
}
});
Deno.test('interop: a Node teacup consumes a Deno teapot', async () => {
const teapot = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: SECRET } });
let port = 0;
const { serveDeno } = await import('../../src/deno.ts');
const server = await serveDeno(teapot, {
port: 0,
onListen: (addr) => {
port = addr.port;
},
});
await new Promise((resolve) => setTimeout(resolve, 50));
const node = spawnNodePeer(['teacup', controlUrl(port)]);
try {
const result = await firstJsonLine(node);
assertEquals(result.ok, true);
// the Node peer ran a step served by Deno's app.upgrade control channel
assertEquals(result.body.config, { region: 'mx', runtime: 'deno' });
assertEquals(result.body.auth, { token: 'abc' });
} finally {
await kill(node);
await server.shutdown();
}
});
Deno.test('interop: a version-skewed peer is refused across runtimes', async () => {
const node = spawnNodePeer(['teapot']);
const { port } = await firstJsonLine(node);
try {
// hand-rolled peer speaking a future protocol: the teapot must refuse before the secret
const ws = new WebSocket(controlUrl(port));
const closed = await new Promise<{ code: number; reason: string }>((resolve, reject) => {
ws.onopen = () => ws.send(JSON.stringify({ type: 'hello', v: 999, secret: SECRET }));
ws.onclose = (e) => resolve({ code: e.code, reason: e.reason });
ws.onerror = () => reject(new Error('ws error'));
});
assertEquals(closed.code, 1008);
assertStringIncludes(closed.reason, '999');
} finally {
await kill(node);
}
});