feat: move to Deno.serve (#1427)
Co-authored-by: Marvin Hagemeister <marvin@deno.com>
This commit is contained in:
co-authored by
Marvin Hagemeister
parent
46ae49d654
commit
e2a1bf8643
@@ -25,7 +25,7 @@ The [documentation](https://fresh.deno.dev/docs/) is available on
|
||||
|
||||
## 🚀 Getting started
|
||||
|
||||
Install [Deno CLI](https://deno.land/) version 1.31.0 or higher.
|
||||
Install [Deno CLI](https://deno.land/) version 1.35.0 or higher.
|
||||
|
||||
You can scaffold a new project by running the Fresh init script. To scaffold a
|
||||
project run the following:
|
||||
|
||||
@@ -120,11 +120,15 @@ the value of `acme` in your middleware.
|
||||
To set the stage for this section, `MiddlewareHandlerContext` looks like this:
|
||||
|
||||
```ts
|
||||
export interface MiddlewareHandlerContext<State = Record<string, unknown>>
|
||||
extends ConnInfo {
|
||||
export interface MiddlewareHandlerContext<State = Record<string, unknown>> {
|
||||
next: () => Promise<Response>;
|
||||
state: State;
|
||||
destination: router.DestinationKind;
|
||||
remoteAddr: {
|
||||
transport: "tcp" | "udp";
|
||||
hostname: string;
|
||||
port: number;
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ caching **will** cause your project to not function correctly.
|
||||
Here is an example `Dockerfile` for a Fresh project:
|
||||
|
||||
```dockerfile
|
||||
FROM denoland/deno:1.33.2
|
||||
FROM denoland/deno:1.35.0
|
||||
|
||||
ARG GIT_REVISION
|
||||
ENV DENO_DEPLOYMENT_ID=${GIT_REVISION}
|
||||
|
||||
@@ -100,11 +100,15 @@ export const handler = [
|
||||
To set the stage for this section, `MiddlewareHandlerContext` looks like this:
|
||||
|
||||
```ts
|
||||
export interface MiddlewareHandlerContext<State = Record<string, unknown>>
|
||||
extends ConnInfo {
|
||||
export interface MiddlewareHandlerContext<State = Record<string, unknown>> {
|
||||
next: () => Promise<Response>;
|
||||
state: State;
|
||||
destination: router.DestinationKind;
|
||||
remoteAddr: {
|
||||
transport: "tcp" | "udp";
|
||||
hostname: string;
|
||||
port: number;
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -17,19 +17,10 @@ export async function start(routes: Manifest, opts: StartOptions = {});
|
||||
things get interesting. `StartOptions` looks like this:
|
||||
|
||||
```
|
||||
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 type StartOptions = ServeInit & FreshOptions;
|
||||
```
|
||||
|
||||
You can play around with `Deno.serve` if you'd like, but heed the warning. The
|
||||
good stuff is really in...
|
||||
The good stuff is really in...
|
||||
|
||||
```ts
|
||||
export interface FreshOptions {
|
||||
|
||||
@@ -23,6 +23,7 @@ below.
|
||||
| 1.0.0-1.0.2 | >=10.8.1 <11.0.0 | >=5.2.0 <6.0.0 | >= 1.23.0 |
|
||||
| 1.1.0-1.1.5 | >=10.8.1 <11.0.0 | >=5.2.0 <6.0.0 | >= 1.25.0 |
|
||||
| 1.2.0 | >=10.15.0 <11.0.0 | >=6.1.0 | >= 1.25.0 |
|
||||
| 1.3.0 | >=10.15.0 <11.0.0 | >=6.1.0 | >= 1.35.0 |
|
||||
|
||||
## Updating dependencies
|
||||
|
||||
|
||||
@@ -53,13 +53,11 @@ export default function FooPage() {
|
||||
```ts
|
||||
// tests/main_test.ts
|
||||
|
||||
import { createHandler } from "$fresh/server.ts";
|
||||
import { createHandler, ServeHandlerInfo } from "$fresh/server.ts";
|
||||
import manifest from "../fresh.gen.ts";
|
||||
import { assert, assertEquals } from "$std/testing/asserts.ts";
|
||||
import type { ConnInfo } from "$std/http/server.ts";
|
||||
|
||||
const CONN_INFO: ConnInfo = {
|
||||
localAddr: { hostname: "127.0.0.1", port: 8000, transport: "tcp" },
|
||||
const CONN_INFO: ServeHandlerInfo = {
|
||||
remoteAddr: { hostname: "127.0.0.1", port: 53496, transport: "tcp" },
|
||||
};
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ framework. You'll learn how to create a new project, run it locally, edit and
|
||||
create pages, fetch data, handle user interactions, and how to then deploy the
|
||||
project to [Deno Deploy][deno-deploy].
|
||||
|
||||
The documentation assumes you have Deno 1.31.0 or later installed.
|
||||
The documentation assumes you have Deno 1.35.0 or later installed.
|
||||
|
||||
To install Deno, follow the
|
||||
[installation instructions in the Deno manual][manual-installation].
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import {
|
||||
ConnInfo,
|
||||
dirname,
|
||||
extname,
|
||||
fromFileUrl,
|
||||
@@ -33,6 +32,7 @@ import {
|
||||
Route,
|
||||
RouteModule,
|
||||
RouterOptions,
|
||||
ServeHandlerInfo,
|
||||
UnknownPage,
|
||||
UnknownPageModule,
|
||||
} from "./types.ts";
|
||||
@@ -47,7 +47,7 @@ import {
|
||||
} from "../build/mod.ts";
|
||||
import { InternalRoute } from "./router.ts";
|
||||
|
||||
const DEFAULT_CONN_INFO: ConnInfo = {
|
||||
const DEFAULT_CONN_INFO: ServeHandlerInfo = {
|
||||
localAddr: { transport: "tcp", hostname: "localhost", port: 8080 },
|
||||
remoteAddr: { transport: "tcp", hostname: "localhost", port: 1234 },
|
||||
};
|
||||
@@ -374,7 +374,7 @@ export class ServerContext {
|
||||
* This functions returns a request handler that handles all routes required
|
||||
* by fresh, including static files.
|
||||
*/
|
||||
handler(): (req: Request, connInfo?: ConnInfo) => Promise<Response> {
|
||||
handler(): (req: Request, connInfo?: ServeHandlerInfo) => Promise<Response> {
|
||||
const handlers = this.#handlers();
|
||||
const inner = router.router<RouterState>(handlers);
|
||||
const withMiddlewares = this.#composeMiddlewares(
|
||||
@@ -385,7 +385,7 @@ export class ServerContext {
|
||||
const trailingSlashEnabled = this.#routerOptions?.trailingSlash;
|
||||
return async function handler(
|
||||
req: Request,
|
||||
connInfo: ConnInfo = DEFAULT_CONN_INFO,
|
||||
connInfo: ServeHandlerInfo = DEFAULT_CONN_INFO,
|
||||
) {
|
||||
// Redirect requests that end with a trailing slash to their non-trailing
|
||||
// slash counterpart.
|
||||
@@ -457,7 +457,7 @@ export class ServerContext {
|
||||
) {
|
||||
return (
|
||||
req: Request,
|
||||
connInfo: ConnInfo,
|
||||
connInfo: ServeHandlerInfo,
|
||||
inner: router.FinalHandler<RouterState>,
|
||||
) => {
|
||||
// identify middlewares to apply, if any.
|
||||
|
||||
+1
-4
@@ -11,10 +11,7 @@ export {
|
||||
type Handler as ServeHandler,
|
||||
serve,
|
||||
} from "https://deno.land/std@0.193.0/http/server.ts";
|
||||
export type {
|
||||
ConnInfo,
|
||||
ServeInit,
|
||||
} from "https://deno.land/std@0.193.0/http/server.ts";
|
||||
export type { ServeInit } from "https://deno.land/std@0.193.0/http/server.ts";
|
||||
export { Status } from "https://deno.land/std@0.193.0/http/http_status.ts";
|
||||
export {
|
||||
typeByExtension,
|
||||
|
||||
+12
-5
@@ -1,13 +1,15 @@
|
||||
import { ServerContext } from "./context.ts";
|
||||
import * as colors from "https://deno.land/std@0.193.0/fmt/colors.ts";
|
||||
import { ConnInfo, serve, ServeHandler } from "./deps.ts";
|
||||
export { Status } from "./deps.ts";
|
||||
import { serve } from "./deps.ts";
|
||||
import {
|
||||
AppModule,
|
||||
ErrorPageModule,
|
||||
IslandModule,
|
||||
MiddlewareModule,
|
||||
RouteModule,
|
||||
ServeHandler,
|
||||
ServeHandlerInfo,
|
||||
StartOptions,
|
||||
UnknownPageModule,
|
||||
} from "./types.ts";
|
||||
@@ -35,6 +37,7 @@ export type {
|
||||
PluginRenderStyleTag,
|
||||
RenderFunction,
|
||||
RouteConfig,
|
||||
ServeHandlerInfo,
|
||||
StartOptions,
|
||||
UnknownHandler,
|
||||
UnknownHandlerContext,
|
||||
@@ -70,7 +73,9 @@ export { ServerContext };
|
||||
export async function createHandler(
|
||||
routes: Manifest,
|
||||
opts: StartOptions = {},
|
||||
): Promise<(req: Request, connInfo?: ConnInfo) => Promise<Response>> {
|
||||
): Promise<
|
||||
(req: Request, connInfo?: ServeHandlerInfo) => Promise<Response>
|
||||
> {
|
||||
const ctx = await ServerContext.fromManifest(routes, opts);
|
||||
return ctx.handler();
|
||||
}
|
||||
@@ -132,10 +137,12 @@ export async function start(routes: Manifest, opts: StartOptions = {}) {
|
||||
}
|
||||
|
||||
async function bootServer(handler: ServeHandler, opts: StartOptions) {
|
||||
if (opts.experimentalDenoServe === true) {
|
||||
// @ts-ignore as `Deno.serve` is still unstable.
|
||||
await Deno.serve({ ...opts, handler }).finished;
|
||||
// @ts-ignore Ignore type error when type checking with Deno versions
|
||||
if (typeof Deno.serve === "function") {
|
||||
// @ts-ignore Ignore type error when type checking with Deno versions
|
||||
await Deno.serve(opts, handler).finished;
|
||||
} else {
|
||||
// @ts-ignore Deprecated std serve way
|
||||
await serve(handler, opts);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { ConnInfo } from "./deps.ts";
|
||||
import { ServeHandlerInfo } from "./types.ts";
|
||||
|
||||
type HandlerContext<T = unknown> = T & ConnInfo;
|
||||
type HandlerContext<T = unknown> = T & ServeHandlerInfo;
|
||||
|
||||
export type Handler<T = unknown> = (
|
||||
req: Request,
|
||||
|
||||
+20
-14
@@ -1,19 +1,11 @@
|
||||
import { ComponentType } from "preact";
|
||||
import { ConnInfo, ServeInit } from "./deps.ts";
|
||||
import { ServeInit } from "./deps.ts";
|
||||
import * as router from "./router.ts";
|
||||
import { InnerRenderFunction, RenderContext } from "./render.ts";
|
||||
|
||||
// --- APPLICATION CONFIGURATION ---
|
||||
|
||||
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 type StartOptions = ServeInit & FreshOptions;
|
||||
|
||||
export interface FreshOptions {
|
||||
render?: RenderFunction;
|
||||
@@ -85,8 +77,22 @@ export interface RouteConfig {
|
||||
// deno-lint-ignore no-empty-interface
|
||||
export interface RenderOptions extends ResponseInit {}
|
||||
|
||||
export type ServeHandlerInfo = {
|
||||
/**
|
||||
* Backwards compatible with std/server
|
||||
* @deprecated
|
||||
*/
|
||||
localAddr?: Deno.NetAddr;
|
||||
remoteAddr: Deno.NetAddr;
|
||||
};
|
||||
|
||||
export type ServeHandler = (
|
||||
request: Request,
|
||||
info: ServeHandlerInfo,
|
||||
) => Response | Promise<Response>;
|
||||
|
||||
export interface HandlerContext<Data = unknown, State = Record<string, unknown>>
|
||||
extends ConnInfo {
|
||||
extends ServeHandlerInfo {
|
||||
params: Record<string, string>;
|
||||
render: (
|
||||
data?: Data,
|
||||
@@ -158,7 +164,7 @@ export interface UnknownPageProps<T = any> {
|
||||
}
|
||||
|
||||
export interface UnknownHandlerContext<State = Record<string, unknown>>
|
||||
extends ConnInfo {
|
||||
extends ServeHandlerInfo {
|
||||
render: () => Response | Promise<Response>;
|
||||
state: State;
|
||||
}
|
||||
@@ -198,7 +204,7 @@ export interface ErrorPageProps {
|
||||
}
|
||||
|
||||
export interface ErrorHandlerContext<State = Record<string, unknown>>
|
||||
extends ConnInfo {
|
||||
extends ServeHandlerInfo {
|
||||
error: unknown;
|
||||
render: () => Response | Promise<Response>;
|
||||
state: State;
|
||||
@@ -227,7 +233,7 @@ export interface ErrorPage {
|
||||
// --- MIDDLEWARES ---
|
||||
|
||||
export interface MiddlewareHandlerContext<State = Record<string, unknown>>
|
||||
extends ConnInfo {
|
||||
extends ServeHandlerInfo {
|
||||
next: () => Promise<Response>;
|
||||
state: State;
|
||||
destination: router.DestinationKind;
|
||||
|
||||
@@ -7,11 +7,6 @@ const ctx = await ServerContext.fromManifest(manifest, {});
|
||||
const handler = ctx.handler();
|
||||
const router = (req: Request) => {
|
||||
return handler(req, {
|
||||
localAddr: {
|
||||
transport: "tcp",
|
||||
hostname: "127.0.0.1",
|
||||
port: 80,
|
||||
},
|
||||
remoteAddr: {
|
||||
transport: "tcp",
|
||||
hostname: "127.0.0.1",
|
||||
|
||||
@@ -8,6 +8,4 @@ import { start } from "$fresh/server.ts";
|
||||
import routes from "./fresh.gen.ts";
|
||||
import options from "./options.ts";
|
||||
|
||||
const experimentalDenoServe = Deno.args.includes("--experimental-deno-serve");
|
||||
|
||||
await start(routes, { ...options, experimentalDenoServe });
|
||||
await start(routes, options);
|
||||
|
||||
@@ -11,6 +11,4 @@ import { start } from "$fresh/server.ts";
|
||||
import routes from "./fresh.gen.ts";
|
||||
import options from "./options.ts";
|
||||
|
||||
const experimentalDenoServe = Deno.args.includes("--experimental-deno-serve");
|
||||
|
||||
await start(routes, { ...options, experimentalDenoServe });
|
||||
await start(routes, options);
|
||||
|
||||
@@ -7,6 +7,4 @@
|
||||
import { start } from "$fresh/server.ts";
|
||||
import routes from "./fresh.gen.ts";
|
||||
|
||||
const experimentalDenoServe = Deno.args.includes("--experimental-deno-serve");
|
||||
|
||||
await start(routes, { experimentalDenoServe });
|
||||
await start(routes);
|
||||
|
||||
@@ -10,6 +10,4 @@ import "./polyfill_deno_deploy.ts";
|
||||
import { start } from "$fresh/server.ts";
|
||||
import routes from "./fresh.gen.ts";
|
||||
|
||||
const experimentalDenoServe = Deno.args.includes("--experimental-deno-serve");
|
||||
|
||||
await start(routes, { experimentalDenoServe });
|
||||
await start(routes);
|
||||
|
||||
@@ -8,6 +8,4 @@ import { start } from "$fresh/server.ts";
|
||||
import routes from "./fresh.gen.ts";
|
||||
import options from "./options.ts";
|
||||
|
||||
const experimentalDenoServe = Deno.args.includes("--experimental-deno-serve");
|
||||
|
||||
await start(routes, { ...options, experimentalDenoServe });
|
||||
await start(routes, options);
|
||||
|
||||
@@ -11,6 +11,4 @@ import { start } from "$fresh/server.ts";
|
||||
import routes from "./fresh.gen.ts";
|
||||
import options from "./options.ts";
|
||||
|
||||
const experimentalDenoServe = Deno.args.includes("--experimental-deno-serve");
|
||||
|
||||
await start(routes, { ...options, experimentalDenoServe });
|
||||
await start(routes, options);
|
||||
|
||||
@@ -269,11 +269,6 @@ Deno.test("static files in custom directory", async () => {
|
||||
});
|
||||
const newRouter = (req: Request) => {
|
||||
return newCtx.handler()(req, {
|
||||
localAddr: {
|
||||
transport: "tcp",
|
||||
hostname: "127.0.0.1",
|
||||
port: 80,
|
||||
},
|
||||
remoteAddr: {
|
||||
transport: "tcp",
|
||||
hostname: "127.0.0.1",
|
||||
@@ -670,67 +665,6 @@ 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, lines, address } = await startFreshServer({
|
||||
args: [
|
||||
"run",
|
||||
"-A",
|
||||
"--unstable",
|
||||
"./tests/fixture/main.ts",
|
||||
"--experimental-deno-serve",
|
||||
],
|
||||
});
|
||||
|
||||
await delay(100);
|
||||
|
||||
await t.step("ssr", async () => {
|
||||
const resp = await fetch(address);
|
||||
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_default.js");
|
||||
assertStringIncludes(body, "<p>Hello!</p>");
|
||||
assertStringIncludes(body, "<p>Viewing JIT render.</p>");
|
||||
assertStringIncludes(body, `>{"v":[[{"message":"Hello!"}],[]]}</script>`);
|
||||
assertStringIncludes(
|
||||
body,
|
||||
'<meta name="description" content="Hello world!"/>',
|
||||
);
|
||||
assertStringIncludes(
|
||||
body,
|
||||
'<meta name="generator" content="The freshest framework!"/>',
|
||||
);
|
||||
assert(
|
||||
!body.includes("specialTag"),
|
||||
`Expected actual: "${body}" to not contain: "specialTag"`,
|
||||
);
|
||||
});
|
||||
|
||||
await t.step("static file", async () => {
|
||||
const resp = await fetch(`${address}/foo.txt`);
|
||||
assertEquals(resp.status, Status.OK);
|
||||
const body = await resp.text();
|
||||
assert(body.startsWith("bar"));
|
||||
const etag = resp.headers.get("etag");
|
||||
assert(etag);
|
||||
// TODO(kt3k): Enable this assertion when new Deno.serve is released.
|
||||
// https://github.com/denoland/deno/pull/18568
|
||||
// assert(etag.startsWith("W/"), "etag should be weak");
|
||||
assertEquals(resp.headers.get("content-type"), "text/plain");
|
||||
});
|
||||
|
||||
await lines.cancel();
|
||||
serverProcess.kill("SIGTERM");
|
||||
});
|
||||
|
||||
Deno.test("jsx pragma works", {
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
|
||||
@@ -14,11 +14,6 @@ const ctx = await ServerContext.fromManifest(manifest, options);
|
||||
const handler = ctx.handler();
|
||||
const router = (req: Request) => {
|
||||
return handler(req, {
|
||||
localAddr: {
|
||||
transport: "tcp",
|
||||
hostname: "127.0.0.1",
|
||||
port: 80,
|
||||
},
|
||||
remoteAddr: {
|
||||
transport: "tcp",
|
||||
hostname: "127.0.0.1",
|
||||
|
||||
@@ -10,11 +10,6 @@ const ctx = await ServerContext.fromManifest(manifest, {
|
||||
});
|
||||
const router = (req: Request) => {
|
||||
return ctx.handler()(req, {
|
||||
localAddr: {
|
||||
transport: "tcp",
|
||||
hostname: "127.0.0.1",
|
||||
port: 80,
|
||||
},
|
||||
remoteAddr: {
|
||||
transport: "tcp",
|
||||
hostname: "127.0.0.1",
|
||||
|
||||
@@ -61,6 +61,7 @@ function ga4(
|
||||
|
||||
// Create basic report.
|
||||
const measurementId = GA4_MEASUREMENT_ID;
|
||||
// @ts-ignore GA4Report doesn't even use the localAddress parameter
|
||||
const report = new GA4Report({ measurementId, request, response, conn });
|
||||
|
||||
// Override the default (page_view) event.
|
||||
|
||||
Reference in New Issue
Block a user