feat: don't require connInfo in request handler (#1278)

This commit is contained in:
Luca Casonato
2023-06-18 21:39:11 +02:00
committed by GitHub
parent d0b277a135
commit 29966ad8bb
4 changed files with 60 additions and 66 deletions
+10 -3
View File
@@ -5,7 +5,6 @@ import {
fromFileUrl,
join,
JSONC,
RequestHandler,
Status,
toFileUrl,
typeByExtension,
@@ -47,6 +46,11 @@ import {
JSXConfig,
} from "../build/mod.ts";
const DEFAULT_CONN_INFO: ConnInfo = {
localAddr: { transport: "tcp", hostname: "localhost", port: 8080 },
remoteAddr: { transport: "tcp", hostname: "localhost", port: 1234 },
};
interface RouterState {
state: Record<string, unknown>;
}
@@ -357,7 +361,7 @@ export class ServerContext {
* This functions returns a request handler that handles all routes required
* by fresh, including static files.
*/
handler(): RequestHandler {
handler(): (req: Request, connInfo?: ConnInfo) => Promise<Response> {
const handlers = this.#handlers();
const inner = router.router<RouterState>(handlers);
const withMiddlewares = this.#composeMiddlewares(
@@ -365,7 +369,10 @@ export class ServerContext {
handlers.errorHandler,
);
const trailingSlashEnabled = this.#routerOptions?.trailingSlash;
return async function handler(req: Request, connInfo: ConnInfo) {
return async function handler(
req: Request,
connInfo: ConnInfo = DEFAULT_CONN_INFO,
) {
// Redirect requests that end with a trailing slash to their non-trailing
// slash counterpart.
// Ex: /about/ -> /about
-1
View File
@@ -13,7 +13,6 @@ export {
} from "https://deno.land/std@0.190.0/http/server.ts";
export type {
ConnInfo,
Handler as RequestHandler,
ServeInit,
} from "https://deno.land/std@0.190.0/http/server.ts";
export { Status } from "https://deno.land/std@0.190.0/http/http_status.ts";
+2 -2
View File
@@ -1,6 +1,6 @@
import { ServerContext } from "./context.ts";
import * as colors from "https://deno.land/std@0.190.0/fmt/colors.ts";
import { serve, ServeHandler } from "./deps.ts";
import { ConnInfo, serve, ServeHandler } from "./deps.ts";
export { Status } from "./deps.ts";
import {
AppModule,
@@ -69,7 +69,7 @@ export { ServerContext };
export async function createHandler(
routes: Manifest,
opts: StartOptions = {},
) {
): Promise<(req: Request, connInfo?: ConnInfo) => Promise<Response>> {
const ctx = await ServerContext.fromManifest(routes, opts);
return ctx.handler();
}