$ cat docs/edge-bun.md
Edge & BunEdge e Bun
The same app runs on Cloudflare Workers, Deno, Fastly and Bun: the adapters only forward the incoming Request.O mesmo app roda em Cloudflare Workers, Deno, Fastly e Bun: os adaptadores só encaminham a Request recebida.
Cloudflare Workers
createEdgeWorker(app) exports { fetch(request, env, ctx) }. Bindings reach handlers
through ctx.runtime; getEnv<Env>(ctx) gives typed access and throws if the runtime
did not provide env.
createEdgeWorker(app) exporta { fetch(request, env, ctx) }. Os bindings chegam aos
handlers em ctx.runtime; getEnv<Env>(ctx) dá acesso tipado e lança erro se o
runtime não forneceu env.
import { createApp, createModule } from '@tupik/core';
import { createEdgeWorker, getEnv } from '@tupik/edge';
interface Env {
GREETING: string;
}
const routes = createModule().get('/', {
access: 'public',
handler: (ctx) => ({ message: getEnv<Env>(ctx).GREETING }),
});
export default createEdgeWorker<Env>(createApp().register(routes));
To validate bindings and secrets before the first request, wrap the worker with withEnv from
@tupik/env.
Para validar bindings e segredos antes da primeira requisição, envolva o worker com o withEnv do
@tupik/env.
toEdgeHandler
A plain (request) => Promise<Response> for Deno, Fastly and other Fetch runtimes.
Um (request) => Promise<Response> simples para Deno, Fastly e outros runtimes Fetch.
import { toEdgeHandler } from '@tupik/edge';
// Deno, Fastly or any runtime with fetch(request):
const handle = toEdgeHandler(app);
Deno.serve((request) => handle(request));import { toEdgeHandler } from '@tupik/edge';
// Deno, Fastly ou qualquer runtime com fetch(request):
const handle = toEdgeHandler(app);
Deno.serve((request) => handle(request)); Bun
createBunHandler(app) returns a fetch compatible with Bun.serve;
serveBun(app, options) starts the server and throws outside Bun. It registers the app paths as native
Bun.serve({ routes }) and keeps fetch as fallback; hooks, access rules and middleware run
the same way.
createBunHandler(app) devolve um fetch compatível com Bun.serve;
serveBun(app, options) sobe o servidor e lança erro fora do Bun. Ele registra os caminhos do app como
Bun.serve({ routes }) nativo e mantém o fetch como reserva; hooks, regras de acesso e
middleware funcionam igual.
import { createApp, createModule } from '@tupik/core';
import { createBunHandler, serveBun } from '@tupik/bun';
const app = createApp().register(
createModule().get('/', { access: 'public', handler: () => ({ ok: true }) }),
);
serveBun(app, { port: 3000 });
// or, with full control over Bun.serve:
Bun.serve({ port: 3001, fetch: createBunHandler(app) });import { createApp, createModule } from '@tupik/core';
import { createBunHandler, serveBun } from '@tupik/bun';
const app = createApp().register(
createModule().get('/', { access: 'public', handler: () => ({ ok: true }) }),
);
serveBun(app, { port: 3000 });
// ou, com controle total do Bun.serve:
Bun.serve({ port: 3001, fetch: createBunHandler(app) });