diff --git a/src/server/mod.ts b/src/server/mod.ts index 05d4b04a..37cec50e 100644 --- a/src/server/mod.ts +++ b/src/server/mod.ts @@ -60,10 +60,13 @@ export interface DenoConfig { export { ServerContext }; -export async function start( - routes: Manifest, - opts: StartOptions = {}, -) { +export async function start(routes: Manifest, opts: StartOptions = {}) { const ctx = await ServerContext.fromManifest(routes, opts); - await serve(ctx.handler(), opts); + opts.port ??= 8000; + if (opts.experimentalDenoServe === true) { + // @ts-ignore as `Deno.serve` is still unstable. + await Deno.serve(ctx.handler() as Deno.ServeHandler, opts); + } else { + await serve(ctx.handler(), opts); + } } diff --git a/src/server/types.ts b/src/server/types.ts index 930f339e..db61f868 100644 --- a/src/server/types.ts +++ b/src/server/types.ts @@ -4,7 +4,15 @@ import { InnerRenderFunction, RenderContext } from "./render.ts"; // --- APPLICATION CONFIGURATION --- -export type StartOptions = ServeInit & FreshOptions; +export type StartOptions = ServeInit & FreshOptions & { + /** + * UNSTABLE: use the `Deno.serve` API as the underlying HTTP server instead of + * the `std/http` API. Do not use this in production. + * + * This option is experimental and may be removed in a future Fresh release. + */ + experimentalDenoServe?: boolean; +}; export interface FreshOptions { render?: RenderFunction; diff --git a/tests/fixture/main.ts b/tests/fixture/main.ts index 1409a21d..da8c854a 100644 --- a/tests/fixture/main.ts +++ b/tests/fixture/main.ts @@ -8,4 +8,6 @@ import { start } from "$fresh/server.ts"; import routes from "./fresh.gen.ts"; import options from "./options.ts"; -await start(routes, options); +const experimentalDenoServe = Deno.args.includes("--experimental-deno-serve"); + +await start(routes, { ...options, experimentalDenoServe }); diff --git a/tests/fixture_jsx_pragma/main.ts b/tests/fixture_jsx_pragma/main.ts index bae9199f..84a9d01d 100644 --- a/tests/fixture_jsx_pragma/main.ts +++ b/tests/fixture_jsx_pragma/main.ts @@ -2,7 +2,6 @@ /// /// /// -/// import { start } from "$fresh/server.ts"; import manifest from "./fresh.gen.ts"; diff --git a/tests/main_test.ts b/tests/main_test.ts index 8fb14072..4603927e 100644 --- a/tests/main_test.ts +++ b/tests/main_test.ts @@ -471,6 +471,77 @@ Deno.test({ }, }); +Deno.test("experimental Deno.serve", { + sanitizeOps: false, + sanitizeResources: false, + ignore: Deno.build.os === "windows", // TODO: Deno.serve hang on Windows? +}, async (t) => { + // Preparation + const serverProcess = Deno.run({ + cmd: [ + "deno", + "run", + "-A", + "--unstable", + "./tests/fixture/main.ts", + "--experimental-deno-serve", + ], + stdout: "piped", + stderr: "inherit", + }); + + const decoder = new TextDecoderStream(); + const lines = serverProcess.stdout.readable + .pipeThrough(decoder) + .pipeThrough(new TextLineStream()); + + let started = false; + for await (const line of lines) { + if (line.includes("Listening on http://")) { + started = true; + break; + } + } + if (!started) { + throw new Error("Server didn't start up"); + } + + await delay(100); + + await t.step("ssr", async () => { + const resp = await fetch("http://localhost:8000"); + assert(resp); + assertEquals(resp.status, Status.OK); + assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8"); + assertEquals(resp.headers.get("server"), "fresh test server"); + const body = await resp.text(); + assertStringIncludes(body, ``); + assertStringIncludes(body, "test.js"); + assertStringIncludes(body, "

Hello!

"); + assertStringIncludes(body, "

Viewing JIT render.

"); + assertStringIncludes(body, `>[[{"message":"Hello!"}],[]]`); + assertStringIncludes( + body, + ``, + ); + }); + + await t.step("static file", async () => { + const resp = await fetch("http://localhost:8000/foo.txt"); + assertEquals(resp.status, Status.OK); + const body = await resp.text(); + assert(body.startsWith("bar")); + const etag = resp.headers.get("etag"); + assert(etag); + assert(!etag.startsWith("W/"), "etag should be weak"); + assertEquals(resp.headers.get("content-type"), "text/plain"); + }); + + await lines.cancel(); + serverProcess.kill("SIGTERM"); + serverProcess.close(); +}); + Deno.test("jsx pragma works", { sanitizeOps: false, sanitizeResources: false,