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
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user