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>
102 lines
3.1 KiB
TypeScript
102 lines
3.1 KiB
TypeScript
// Reconnection under the real Bun runtime.
|
|
//
|
|
// Separate from the Node and Deno suites deliberately. Bun's WebSocket lifecycle is a third
|
|
// implementation — its socket events land on a server-level handler rather than the socket — and a
|
|
// link that swaps its socket underneath a live graph is precisely where three implementations
|
|
// stop behaving the same. Defining this on Node and hoping is not enough.
|
|
//
|
|
// Run with: npm run test:bun
|
|
import 'reflect-metadata';
|
|
import { test, expect } from 'bun:test';
|
|
import { createApp, Step, Route, Get, Module, needs } from '../../src/index.ts';
|
|
import { serveBun } from '../../src/bun.ts';
|
|
|
|
const SECRET = 's3cr3t';
|
|
|
|
@Step({ provides: 'config', needs: [], export: true })
|
|
class Config {
|
|
run() {
|
|
return { config: { runtime: 'bun' } };
|
|
}
|
|
}
|
|
@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 {}
|
|
|
|
/** A port nobody is on, so the teapot can be killed and brought back at the same address. */
|
|
function freePort(): number {
|
|
const probe = Bun.serve({ port: 0, fetch: () => new Response('') });
|
|
const { port } = probe;
|
|
probe.stop(true);
|
|
|
|
return port;
|
|
}
|
|
|
|
/** Boot a teapot on a fixed port; the handle kills it and leaves the port free again. */
|
|
async function startTeapot(port: number) {
|
|
const app = createApp({ modules: [TeapotModule], experimental: true, mesh: { secret: SECRET } });
|
|
const server = await serveBun(app, { port });
|
|
|
|
return {
|
|
stop: async () => {
|
|
server.stop(true);
|
|
await app.close();
|
|
},
|
|
};
|
|
}
|
|
|
|
const settle = (ms: number) => new Promise((r) => setTimeout(r, ms));
|
|
|
|
test('mesh on Bun: a teacup reconnects to a teapot that came back', async () => {
|
|
const port = freePort();
|
|
let teapot = await startTeapot(port);
|
|
|
|
const teacup = createApp({
|
|
modules: [TeacupModule],
|
|
experimental: true,
|
|
mesh: {
|
|
teapots: [{ url: `ws://127.0.0.1:${port}/__mesh__/control`, secret: SECRET }],
|
|
reconnect: { initialDelayMs: 20, maxDelayMs: 60 },
|
|
heartbeatMs: 50,
|
|
timeoutMs: 2000,
|
|
},
|
|
});
|
|
|
|
try {
|
|
const before = await teacup.fetch(new Request('http://x/api/local/who', { headers: { 'x-token': 'abc' } }));
|
|
expect(before.status).toBe(200);
|
|
|
|
await teapot.stop();
|
|
await settle(100);
|
|
|
|
// the down window answers immediately rather than waiting out timeoutMs
|
|
const down = await teacup.fetch(new Request('http://x/api/local/who'));
|
|
expect(down.status).toBe(503);
|
|
|
|
teapot = await startTeapot(port);
|
|
await settle(400);
|
|
|
|
const after = await teacup.fetch(new Request('http://x/api/local/who', { headers: { 'x-token': 'xyz' } }));
|
|
expect(after.status).toBe(200);
|
|
expect((await after.json()).auth).toEqual({ token: 'xyz' });
|
|
} finally {
|
|
await teacup.close();
|
|
await teapot.stop();
|
|
}
|
|
});
|