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>
109 lines
3.6 KiB
TypeScript
109 lines
3.6 KiB
TypeScript
// Real Deno integration test for mesh: a teapot exporting its steps and a teacup consuming
|
|
// them, both under the actual Deno runtime, driven entirely through serveDeno.
|
|
//
|
|
// This is the test that makes "mesh runs off Node" a fact rather than an intention. It covers,
|
|
// in one round-trip, everything that used to be Node-only:
|
|
// - the teacup's WebSocket client is Deno's global (no `ws` package here at all)
|
|
// - the teapot's control channel is served via app.upgrade (Deno has no server.on('upgrade'))
|
|
// - the graph boots on the first request; `listen()` is never called and cannot be
|
|
// - node:crypto's timingSafeEqual + Buffer resolve under Deno's node-compat
|
|
//
|
|
// Run with: npm run test:deno
|
|
// `--no-check`: see the note in ws.test.ts — Deno type-checks the whole reachable graph,
|
|
// including Node-only modules this test never executes. tsc covers those.
|
|
import 'npm:reflect-metadata';
|
|
import { assertEquals, assertStringIncludes } from 'jsr:@std/assert';
|
|
import { createApp, Step, Route, Get, Module, needs } from '../../src/index.ts';
|
|
import { serveDeno } from '../../src/deno.ts';
|
|
|
|
const SECRET = 's3cr3t';
|
|
|
|
@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 {}
|
|
|
|
/** Starts an app on Deno and resolves once its port is known. */
|
|
async function serve(app: ReturnType<typeof createApp>) {
|
|
let port = 0;
|
|
const server = await serveDeno(app, {
|
|
port: 0,
|
|
onListen: (addr) => {
|
|
port = addr.port;
|
|
},
|
|
});
|
|
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
return { server, port };
|
|
}
|
|
|
|
const controlUrl = (port: number) => `ws://127.0.0.1:${port}/__mesh__/control`;
|
|
|
|
Deno.test('mesh on Deno: teacup resolves a Deno teapot through the graph', async () => {
|
|
const teapot = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: SECRET } });
|
|
const t = await serve(teapot);
|
|
|
|
const teacup = createApp({
|
|
modules: [TeacupModule],
|
|
experimental: true,
|
|
mesh: { teapots: [{ url: controlUrl(t.port), secret: SECRET }] },
|
|
});
|
|
const c = await serve(teacup);
|
|
|
|
try {
|
|
const res = await fetch(`http://127.0.0.1:${c.port}/api/local/who`, { headers: { 'x-token': 'abc' } });
|
|
assertEquals(res.status, 200);
|
|
assertEquals(await res.json(), {
|
|
config: { region: 'mx', runtime: 'deno' },
|
|
auth: { token: 'abc' },
|
|
});
|
|
} finally {
|
|
await teacup.close();
|
|
await c.server.shutdown();
|
|
await t.server.shutdown();
|
|
}
|
|
});
|
|
|
|
Deno.test('mesh on Deno: a teapot refuses a bad secret over its control channel', async () => {
|
|
const teapot = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: SECRET } });
|
|
const t = await serve(teapot);
|
|
|
|
const teacup = createApp({
|
|
modules: [TeacupModule],
|
|
experimental: true,
|
|
mesh: { teapots: [{ url: controlUrl(t.port), secret: 'WRONG' }], timeoutMs: 1000 },
|
|
});
|
|
|
|
try {
|
|
let message = '';
|
|
try {
|
|
await teacup.fetch(new Request('http://x/api/local/who'));
|
|
} catch (err) {
|
|
message = String((err as Error).message);
|
|
}
|
|
assertStringIncludes(message, 'mesh');
|
|
} finally {
|
|
await teacup.close();
|
|
await t.server.shutdown();
|
|
}
|
|
});
|