refactor: make App.prototype.handler() sync (#2906)

This commit is contained in:
Tim Reichen
2025-05-12 10:12:50 +02:00
committed by GitHub
parent 9605cd81b5
commit 1cfb4bc876
13 changed files with 49 additions and 48 deletions
+5 -4
View File
@@ -243,11 +243,12 @@ export class App<State> {
return this; return this;
} }
async handler(): Promise< handler(): (
(request: Request, info?: Deno.ServeHandlerInfo) => Promise<Response> request: Request,
> { info?: Deno.ServeHandlerInfo,
) => Promise<Response> {
if (this.#buildCache === null) { if (this.#buildCache === null) {
this.#buildCache = await ProdBuildCache.fromSnapshot( this.#buildCache = ProdBuildCache.fromSnapshot(
this.config, this.config,
this.#islandRegistry.size, this.#islandRegistry.size,
); );
+24 -24
View File
@@ -15,7 +15,7 @@ Deno.test("FreshApp - .use()", async () => {
}) })
.get("/", (ctx) => new Response(ctx.state.text)); .get("/", (ctx) => new Response(ctx.state.text));
const server = new FakeServer(await app.handler()); const server = new FakeServer(app.handler());
const res = await server.get("/"); const res = await server.get("/");
expect(await res.text()).toEqual("AB"); expect(await res.text()).toEqual("AB");
@@ -27,7 +27,7 @@ Deno.test("FreshApp - .use() #2", async () => {
.get("/foo/bar", () => new Response("ok #2")) .get("/foo/bar", () => new Response("ok #2"))
.get("/", () => new Response("ok #3")); .get("/", () => new Response("ok #3"));
const server = new FakeServer(await app.handler()); const server = new FakeServer(app.handler());
const res = await server.get("/"); const res = await server.get("/");
expect(await res.text()).toEqual("ok #1"); expect(await res.text()).toEqual("ok #1");
@@ -40,7 +40,7 @@ Deno.test("FreshApp - .get()", async () => {
.get("/", () => new Response("ok")) .get("/", () => new Response("ok"))
.get("/foo", () => new Response("ok")); .get("/foo", () => new Response("ok"));
const server = new FakeServer(await app.handler()); const server = new FakeServer(app.handler());
let res = await server.get("/"); let res = await server.get("/");
expect(await res.text()).toEqual("ok"); expect(await res.text()).toEqual("ok");
@@ -54,7 +54,7 @@ Deno.test("FreshApp - .get() with basePath", async () => {
.get("/", () => new Response("ok")) .get("/", () => new Response("ok"))
.get("/foo", () => new Response("ok")); .get("/foo", () => new Response("ok"));
const server = new FakeServer(await app.handler()); const server = new FakeServer(app.handler());
let res = await server.get("/"); let res = await server.get("/");
expect(res.status).toEqual(404); expect(res.status).toEqual(404);
@@ -74,7 +74,7 @@ Deno.test("FreshApp - .post()", async () => {
.post("/", () => new Response("ok")) .post("/", () => new Response("ok"))
.post("/foo", () => new Response("ok")); .post("/foo", () => new Response("ok"));
const server = new FakeServer(await app.handler()); const server = new FakeServer(app.handler());
let res = await server.post("/"); let res = await server.post("/");
expect(await res.text()).toEqual("ok"); expect(await res.text()).toEqual("ok");
@@ -88,7 +88,7 @@ Deno.test("FreshApp - .post() with basePath", async () => {
.post("/", () => new Response("ok")) .post("/", () => new Response("ok"))
.post("/foo", () => new Response("ok")); .post("/foo", () => new Response("ok"));
const server = new FakeServer(await app.handler()); const server = new FakeServer(app.handler());
let res = await server.post("/"); let res = await server.post("/");
expect(res.status).toEqual(404); expect(res.status).toEqual(404);
@@ -108,7 +108,7 @@ Deno.test("FreshApp - .patch()", async () => {
.patch("/", () => new Response("ok")) .patch("/", () => new Response("ok"))
.patch("/foo", () => new Response("ok")); .patch("/foo", () => new Response("ok"));
const server = new FakeServer(await app.handler()); const server = new FakeServer(app.handler());
let res = await server.patch("/"); let res = await server.patch("/");
expect(await res.text()).toEqual("ok"); expect(await res.text()).toEqual("ok");
@@ -122,7 +122,7 @@ Deno.test("FreshApp - .patch() with basePath", async () => {
.patch("/", () => new Response("ok")) .patch("/", () => new Response("ok"))
.patch("/foo", () => new Response("ok")); .patch("/foo", () => new Response("ok"));
const server = new FakeServer(await app.handler()); const server = new FakeServer(app.handler());
let res = await server.patch("/"); let res = await server.patch("/");
expect(res.status).toEqual(404); expect(res.status).toEqual(404);
@@ -142,7 +142,7 @@ Deno.test("FreshApp - .put()", async () => {
.put("/", () => new Response("ok")) .put("/", () => new Response("ok"))
.put("/foo", () => new Response("ok")); .put("/foo", () => new Response("ok"));
const server = new FakeServer(await app.handler()); const server = new FakeServer(app.handler());
let res = await server.put("/"); let res = await server.put("/");
expect(await res.text()).toEqual("ok"); expect(await res.text()).toEqual("ok");
@@ -156,7 +156,7 @@ Deno.test("FreshApp - .put() with basePath", async () => {
.put("/", () => new Response("ok")) .put("/", () => new Response("ok"))
.put("/foo", () => new Response("ok")); .put("/foo", () => new Response("ok"));
const server = new FakeServer(await app.handler()); const server = new FakeServer(app.handler());
let res = await server.put("/"); let res = await server.put("/");
expect(res.status).toEqual(404); expect(res.status).toEqual(404);
@@ -176,7 +176,7 @@ Deno.test("FreshApp - .delete()", async () => {
.delete("/", () => new Response("ok")) .delete("/", () => new Response("ok"))
.delete("/foo", () => new Response("ok")); .delete("/foo", () => new Response("ok"));
const server = new FakeServer(await app.handler()); const server = new FakeServer(app.handler());
let res = await server.delete("/"); let res = await server.delete("/");
expect(await res.text()).toEqual("ok"); expect(await res.text()).toEqual("ok");
@@ -190,7 +190,7 @@ Deno.test("FreshApp - .delete() with basePath", async () => {
.delete("/", () => new Response("ok")) .delete("/", () => new Response("ok"))
.delete("/foo", () => new Response("ok")); .delete("/foo", () => new Response("ok"));
const server = new FakeServer(await app.handler()); const server = new FakeServer(app.handler());
let res = await server.delete("/"); let res = await server.delete("/");
expect(res.status).toEqual(404); expect(res.status).toEqual(404);
@@ -208,7 +208,7 @@ Deno.test("FreshApp - wrong method match", async () => {
.get("/", () => new Response("ok")) .get("/", () => new Response("ok"))
.post("/", () => new Response("ok")); .post("/", () => new Response("ok"));
const server = new FakeServer(await app.handler()); const server = new FakeServer(app.handler());
let res = await server.put("/"); let res = await server.put("/");
expect(res.status).toEqual(405); expect(res.status).toEqual(405);
@@ -228,7 +228,7 @@ Deno.test("FreshApp - methods with middleware", async () => {
.get("/", (ctx) => new Response(ctx.state.text)) .get("/", (ctx) => new Response(ctx.state.text))
.post("/", (ctx) => new Response(ctx.state.text)); .post("/", (ctx) => new Response(ctx.state.text));
const server = new FakeServer(await app.handler()); const server = new FakeServer(app.handler());
let res = await server.get("/"); let res = await server.get("/");
expect(await res.text()).toEqual("A"); expect(await res.text()).toEqual("A");
@@ -250,7 +250,7 @@ Deno.test("FreshApp - .mountApp() compose apps", async () => {
.get("/", () => new Response("ok")) .get("/", () => new Response("ok"))
.mountApp("/foo", innerApp); .mountApp("/foo", innerApp);
const server = new FakeServer(await app.handler()); const server = new FakeServer(app.handler());
let res = await server.get("/"); let res = await server.get("/");
expect(await res.text()).toEqual("ok"); expect(await res.text()).toEqual("ok");
@@ -275,7 +275,7 @@ Deno.test("FreshApp - .mountApp() self mount, no middleware", async () => {
.get("/", () => new Response("ok")) .get("/", () => new Response("ok"))
.mountApp("/", innerApp); .mountApp("/", innerApp);
const server = new FakeServer(await app.handler()); const server = new FakeServer(app.handler());
let res = await server.get("/"); let res = await server.get("/");
expect(await res.text()).toEqual("ok"); expect(await res.text()).toEqual("ok");
@@ -306,7 +306,7 @@ Deno.test(
.get("/", () => new Response("ok")) .get("/", () => new Response("ok"))
.mountApp("/", innerApp); .mountApp("/", innerApp);
const server = new FakeServer(await app.handler()); const server = new FakeServer(app.handler());
let res = await server.get("/"); let res = await server.get("/");
expect(await res.text()).toEqual("ok"); expect(await res.text()).toEqual("ok");
@@ -338,7 +338,7 @@ Deno.test(
.get("/", () => new Response("ok")) .get("/", () => new Response("ok"))
.mountApp("/", innerApp); .mountApp("/", innerApp);
const server = new FakeServer(await app.handler()); const server = new FakeServer(app.handler());
let res = await server.get("/"); let res = await server.get("/");
expect(await res.text()).toEqual("ok"); expect(await res.text()).toEqual("ok");
@@ -362,7 +362,7 @@ Deno.test("FreshApp - .mountApp() self mount empty", async () => {
const app = new App<{ text: string }>() const app = new App<{ text: string }>()
.mountApp("/", innerApp); .mountApp("/", innerApp);
const server = new FakeServer(await app.handler()); const server = new FakeServer(app.handler());
const res = await server.get("/foo"); const res = await server.get("/foo");
expect(await res.text()).toEqual("A"); expect(await res.text()).toEqual("A");
@@ -385,7 +385,7 @@ Deno.test(
}) })
.mountApp("/", innerApp); .mountApp("/", innerApp);
const server = new FakeServer(await app.handler()); const server = new FakeServer(app.handler());
const res = await server.get("/"); const res = await server.get("/");
expect(await res.text()).toEqual("Outer_Inner"); expect(await res.text()).toEqual("Outer_Inner");
@@ -408,7 +408,7 @@ Deno.test("FreshApp - catches errors", async () => {
throw new Error("fail"); throw new Error("fail");
}); });
const server = new FakeServer(await app.handler()); const server = new FakeServer(app.handler());
const res = await server.get("/"); const res = await server.get("/");
expect(res.status).toEqual(500); expect(res.status).toEqual(500);
@@ -432,7 +432,7 @@ Deno.test.ignore("FreshApp - finish setup", async () => {
}, getIslandRegistry(app).size), }, getIslandRegistry(app).size),
); );
const server = new FakeServer(await app.handler()); const server = new FakeServer(app.handler());
const res = await server.get("/"); const res = await server.get("/");
const text = await res.text(); const text = await res.text();
expect(text).toContain("Finish setting up"); expect(text).toContain("Finish setting up");
@@ -462,7 +462,7 @@ Deno.test("FreshApp - sets error on context", async () => {
throw "<mock error>"; throw "<mock error>";
}); });
const server = new FakeServer(await app.handler()); const server = new FakeServer(app.handler());
const res = await server.get("/"); const res = await server.get("/");
await res.body?.cancel(); await res.body?.cancel();
@@ -480,7 +480,7 @@ Deno.test("FreshApp - support setting request init in ctx.render()", async () =>
}); });
}); });
const server = new FakeServer(await app.handler()); const server = new FakeServer(app.handler());
const res = await server.get("/"); const res = await server.get("/");
await res.body?.cancel(); await res.body?.cancel();
expect(res.status).toEqual(416); expect(res.status).toEqual(416);
+2 -2
View File
@@ -29,7 +29,7 @@ export interface BuildCache {
} }
export class ProdBuildCache implements BuildCache { export class ProdBuildCache implements BuildCache {
static async fromSnapshot(config: ResolvedFreshConfig, islandCount: number) { static fromSnapshot(config: ResolvedFreshConfig, islandCount: number) {
const snapshotPath = getSnapshotPath(config); const snapshotPath = getSnapshotPath(config);
const staticFiles = new Map<string, FileSnapshot>(); const staticFiles = new Map<string, FileSnapshot>();
@@ -37,7 +37,7 @@ export class ProdBuildCache implements BuildCache {
let hasSnapshot = false; let hasSnapshot = false;
try { try {
const content = await Deno.readTextFile(snapshotPath); const content = Deno.readTextFileSync(snapshotPath);
const snapshot = JSON.parse(content) as BuildSnapshot; const snapshot = JSON.parse(content) as BuildSnapshot;
hasSnapshot = true; hasSnapshot = true;
setBuildId(snapshot.buildId); setBuildId(snapshot.buildId);
+3 -3
View File
@@ -38,7 +38,7 @@ Deno.test("render asset()", async () => {
</>, </>,
)); ));
const server = new FakeServer(await app.handler()); const server = new FakeServer(app.handler());
const res = await server.get("/"); const res = await server.get("/");
const doc = parseHtml(await res.text()); const doc = parseHtml(await res.text());
@@ -52,7 +52,7 @@ Deno.test("ctx.render - throw with no arguments", async () => {
const app = new App() const app = new App()
// deno-lint-ignore no-explicit-any // deno-lint-ignore no-explicit-any
.get("/", (ctx) => (ctx as any).render()); .get("/", (ctx) => (ctx as any).render());
const server = new FakeServer(await app.handler()); const server = new FakeServer(app.handler());
const res = await server.get("/"); const res = await server.get("/");
await res.body?.cancel(); await res.body?.cancel();
@@ -63,7 +63,7 @@ Deno.test("ctx.render - throw with invalid first arg", async () => {
const app = new App() const app = new App()
// deno-lint-ignore no-explicit-any // deno-lint-ignore no-explicit-any
.get("/", (ctx) => (ctx as any).render({})); .get("/", (ctx) => (ctx as any).render({}));
const server = new FakeServer(await app.handler()); const server = new FakeServer(app.handler());
const res = await server.get("/"); const res = await server.get("/");
await res.body?.cancel(); await res.body?.cancel();
@@ -13,7 +13,7 @@ Deno.test("error overlay - show when error is thrown", async () => {
throw new Error("fail"); throw new Error("fail");
}); });
const server = new FakeServer(await app.handler()); const server = new FakeServer(app.handler());
const res = await server.get("/", { const res = await server.get("/", {
headers: { headers: {
accept: "text/html", accept: "text/html",
@@ -36,7 +36,7 @@ Deno.test("error overlay - should not be visible for HttpError <500", async () =
throw new HttpError(500); throw new HttpError(500);
}); });
const server = new FakeServer(await app.handler()); const server = new FakeServer(app.handler());
let res = await server.get("/", { let res = await server.get("/", {
headers: { headers: {
accept: "text/html", accept: "text/html",
@@ -74,7 +74,7 @@ Deno.test(
throw new HttpError(404); throw new HttpError(404);
}); });
const server = new FakeServer(await app.handler()); const server = new FakeServer(app.handler());
const res = await server.get("/", { const res = await server.get("/", {
headers: { headers: {
accept: "text/html", accept: "text/html",
@@ -94,7 +94,7 @@ Deno.test("error overlay - should not be visible in prod", async () => {
throw new HttpError(500); throw new HttpError(500);
}); });
const server = new FakeServer(await app.handler()); const server = new FakeServer(app.handler());
const res = await server.get("/", { const res = await server.get("/", {
headers: { headers: {
accept: "text/html", accept: "text/html",
+2 -2
View File
@@ -17,7 +17,7 @@ import { STATUS_TEXT } from "@std/http/status";
* throw new HttpError(404, "Nothing here"); * throw new HttpError(404, "Nothing here");
* }); * });
* *
* const handler = await app.handler(); * const handler = app.handler();
* *
* try { * try {
* await handler(new Request("http://localhost/not-found")) * await handler(new Request("http://localhost/not-found"))
@@ -43,7 +43,7 @@ export class HttpError extends Error {
* throw new HttpError(404, "Nothing here"); * throw new HttpError(404, "Nothing here");
* }); * });
* *
* const handler = await app.handler(); * const handler = app.handler();
* *
* try { * try {
* await handler(new Request("http://localhost/not-found")) * await handler(new Request("http://localhost/not-found"))
+1 -1
View File
@@ -35,7 +35,7 @@ async function createServer<T>(
_fs: createFakeFs(files), _fs: createFakeFs(files),
} as FsRoutesOptions & TESTING_ONLY__FsRoutesOptions, } as FsRoutesOptions & TESTING_ONLY__FsRoutesOptions,
); );
return new FakeServer(await app.handler()); return new FakeServer(app.handler());
} }
Deno.test("fsRoutes - throws error when file has no exports", async () => { Deno.test("fsRoutes - throws error when file has no exports", async () => {
+1 -1
View File
@@ -66,7 +66,7 @@ Deno.test({
return ctx.render(<View />); return ctx.render(<View />);
}); });
const server = new FakeServer(await app.handler()); const server = new FakeServer(app.handler());
let res = await server.get("/active_nav"); let res = await server.get("/active_nav");
let doc = parseHtml(await res.text()); let doc = parseHtml(await res.text());
+1 -1
View File
@@ -25,7 +25,7 @@ const app = new App({ staticDir: "./static" }).get(
), ),
); );
const handler = await app.handler(); const handler = app.handler();
const res = await handler(new Request("http://localhost/")); const res = await handler(new Request("http://localhost/"));
// deno-lint-ignore no-console // deno-lint-ignore no-console
console.log(await res.text()); console.log(await res.text());
+1 -1
View File
@@ -718,7 +718,7 @@ Deno.test({
</Doc>, </Doc>,
)); ));
const server = new FakeServer(await app.handler()); const server = new FakeServer(app.handler());
const res = await server.get("/"); const res = await server.get("/");
await res.body?.cancel(); await res.body?.cancel();
+3 -3
View File
@@ -188,7 +188,7 @@ Deno.test({
); );
}); });
const server = new FakeServer(await app.handler()); const server = new FakeServer(app.handler());
let checked = false; let checked = false;
try { try {
const res = await server.get("/"); const res = await server.get("/");
@@ -306,7 +306,7 @@ Deno.test({
}); });
await buildProd(app); await buildProd(app);
const server = new FakeServer(await app.handler()); const server = new FakeServer(app.handler());
let checked = false; let checked = false;
try { try {
const res = await server.get("/"); const res = await server.get("/");
@@ -845,7 +845,7 @@ Deno.test({
); );
}); });
const server = new FakeServer(await app.handler()); const server = new FakeServer(app.handler());
const res = await server.get("/"); const res = await server.get("/");
const html = await res.text(); const html = await res.text();
+1 -1
View File
@@ -83,7 +83,7 @@ export async function withBrowserApp(
hostname: "localhost", hostname: "localhost",
port: 0, port: 0,
signal: aborter.signal, signal: aborter.signal,
}, await app.handler()); }, app.handler());
const browser = await launch({ const browser = await launch({
args: [ args: [
+1 -1
View File
@@ -5,7 +5,7 @@ import { expect } from "@std/expect";
import { retry } from "@std/async/retry"; import { retry } from "@std/async/retry";
await buildProd(app); await buildProd(app);
const handler = await app.handler(); const handler = app.handler();
Deno.test("CORS should not set on GET /fresh-badge.svg", async () => { Deno.test("CORS should not set on GET /fresh-badge.svg", async () => {
const req = new Request("http://localhost/fresh-badge.svg"); const req = new Request("http://localhost/fresh-badge.svg");