feat: experimental support for Deno.serve (#681)

The experimental support can be enabled through the
`experimentalDenoServe` option for `start()`.

Co-authored-by: Luca Casonato <hello@lcas.dev>
This commit is contained in:
Mark Bennett
2022-09-07 23:04:45 +00:00
committed by GitHub
co-authored by Luca Casonato
parent b7f0d5c860
commit ba7c78bcc2
5 changed files with 91 additions and 8 deletions
+8 -5
View File
@@ -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);
}
}
+9 -1
View File
@@ -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;
+3 -1
View File
@@ -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 });
-1
View File
@@ -2,7 +2,6 @@
/// <reference lib="dom" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />
/// <reference lib="deno.unstable" />
import { start } from "$fresh/server.ts";
import manifest from "./fresh.gen.ts";
+71
View File
@@ -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, `<html lang="en">`);
assertStringIncludes(body, "test.js");
assertStringIncludes(body, "<p>Hello!</p>");
assertStringIncludes(body, "<p>Viewing JIT render.</p>");
assertStringIncludes(body, `>[[{"message":"Hello!"}],[]]</script>`);
assertStringIncludes(
body,
`<meta name="description" content="Hello world!" />`,
);
});
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,