feat: pass ConnInfo to various handler contexts (#121)
This commit is contained in:
+21
-12
@@ -1,7 +1,9 @@
|
|||||||
import {
|
import {
|
||||||
|
ConnInfo,
|
||||||
extname,
|
extname,
|
||||||
fromFileUrl,
|
fromFileUrl,
|
||||||
mediaTypeLookup,
|
mediaTypeLookup,
|
||||||
|
RequestHandler,
|
||||||
router,
|
router,
|
||||||
toFileUrl,
|
toFileUrl,
|
||||||
walk,
|
walk,
|
||||||
@@ -177,7 +179,7 @@ export class ServerContext {
|
|||||||
name,
|
name,
|
||||||
component,
|
component,
|
||||||
handler: handler ??
|
handler: handler ??
|
||||||
((req, ctx) => router.defaultErrorHandler(req, ctx.error)),
|
((req, ctx) => router.defaultErrorHandler(req, ctx, ctx.error)),
|
||||||
csp: Boolean(config?.csp ?? false),
|
csp: Boolean(config?.csp ?? false),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -256,10 +258,10 @@ export class ServerContext {
|
|||||||
* This functions returns a request handler that handles all routes required
|
* This functions returns a request handler that handles all routes required
|
||||||
* by fresh, including static files.
|
* by fresh, including static files.
|
||||||
*/
|
*/
|
||||||
handler(): router.RequestHandler {
|
handler(): RequestHandler {
|
||||||
const inner = router.router(...this.#routes());
|
const inner = router.router(...this.#routes());
|
||||||
const middleware = this.#middleware;
|
const middleware = this.#middleware;
|
||||||
return function handler(req: Request) {
|
return function handler(req: Request, connInfo: ConnInfo) {
|
||||||
// Redirect requests that end with a trailing slash
|
// Redirect requests that end with a trailing slash
|
||||||
// to their non-trailing slash counterpart.
|
// to their non-trailing slash counterpart.
|
||||||
// Ex: /about/ -> /about
|
// Ex: /about/ -> /about
|
||||||
@@ -268,8 +270,8 @@ export class ServerContext {
|
|||||||
url.pathname = url.pathname.slice(0, -1);
|
url.pathname = url.pathname.slice(0, -1);
|
||||||
return Response.redirect(url.href, 307);
|
return Response.redirect(url.href, 307);
|
||||||
}
|
}
|
||||||
const handle = () => Promise.resolve(inner(req));
|
const handle = () => Promise.resolve(inner(req, connInfo));
|
||||||
return middleware.handler(req, { handle });
|
return middleware.handler(req, { handle, ...connInfo });
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -277,7 +279,7 @@ export class ServerContext {
|
|||||||
* This function returns all routes required by fresh as an extended
|
* This function returns all routes required by fresh as an extended
|
||||||
* path-to-regex, to handler mapping.
|
* path-to-regex, to handler mapping.
|
||||||
*/
|
*/
|
||||||
#routes(): [router.Routes, router.RequestHandler, router.ErrorHandler] {
|
#routes(): [router.Routes, RequestHandler, router.ErrorHandler] {
|
||||||
const routes: router.Routes = {};
|
const routes: router.Routes = {};
|
||||||
|
|
||||||
// Add the static file routes.
|
// Add the static file routes.
|
||||||
@@ -351,30 +353,36 @@ export class ServerContext {
|
|||||||
for (const page of this.#pages) {
|
for (const page of this.#pages) {
|
||||||
const createRender = genRender(page, 200);
|
const createRender = genRender(page, 200);
|
||||||
if (typeof page.handler === "function") {
|
if (typeof page.handler === "function") {
|
||||||
routes[page.route] = (req, params) =>
|
routes[page.route] = (req, connInfo, params) =>
|
||||||
(page.handler as Handler)(req, {
|
(page.handler as Handler)(req, {
|
||||||
|
...connInfo,
|
||||||
params,
|
params,
|
||||||
render: createRender(req, params),
|
render: createRender(req, params),
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
for (const [method, handler] of Object.entries(page.handler)) {
|
for (const [method, handler] of Object.entries(page.handler)) {
|
||||||
routes[`${method}@${page.route}`] = (req, params) =>
|
routes[`${method}@${page.route}`] = (req, connInfo, params) =>
|
||||||
handler(req, { params, render: createRender(req, params) });
|
handler(req, {
|
||||||
|
...connInfo,
|
||||||
|
params,
|
||||||
|
render: createRender(req, params),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const unknownHandlerRender = genRender(this.#notFound, 404);
|
const unknownHandlerRender = genRender(this.#notFound, 404);
|
||||||
const unknownHandler = (req: Request) =>
|
const unknownHandler = (req: Request, connInfo: ConnInfo) =>
|
||||||
this.#notFound.handler(
|
this.#notFound.handler(
|
||||||
req,
|
req,
|
||||||
{
|
{
|
||||||
|
...connInfo,
|
||||||
render: unknownHandlerRender(req, {}),
|
render: unknownHandlerRender(req, {}),
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
const errorHandlerRender = genRender(this.#error, 500);
|
const errorHandlerRender = genRender(this.#error, 500);
|
||||||
const errorHandler = (req: Request, error: unknown) => {
|
const errorHandler = (req: Request, connInfo: ConnInfo, error: unknown) => {
|
||||||
console.error(
|
console.error(
|
||||||
"%cAn error occured during route handling or page rendering.",
|
"%cAn error occured during route handling or page rendering.",
|
||||||
"color:red",
|
"color:red",
|
||||||
@@ -383,6 +391,7 @@ export class ServerContext {
|
|||||||
return this.#error.handler(
|
return this.#error.handler(
|
||||||
req,
|
req,
|
||||||
{
|
{
|
||||||
|
...connInfo,
|
||||||
error,
|
error,
|
||||||
render: errorHandlerRender(req, {}, error),
|
render: errorHandlerRender(req, {}, error),
|
||||||
},
|
},
|
||||||
@@ -455,7 +464,7 @@ export class ServerContext {
|
|||||||
* constants.INTERNAL_PREFIX
|
* constants.INTERNAL_PREFIX
|
||||||
*/
|
*/
|
||||||
#bundleAssetRoute = (): router.MatchHandler => {
|
#bundleAssetRoute = (): router.MatchHandler => {
|
||||||
return async (_req, params) => {
|
return async (_req, _connInfo, params) => {
|
||||||
const path = `/${params.path}`;
|
const path = `/${params.path}`;
|
||||||
const file = await this.#bundler.get(path);
|
const file = await this.#bundler.get(path);
|
||||||
let res;
|
let res;
|
||||||
|
|||||||
+5
-1
@@ -9,9 +9,13 @@ export {
|
|||||||
} from "https://deno.land/std@0.128.0/path/mod.ts";
|
} from "https://deno.land/std@0.128.0/path/mod.ts";
|
||||||
export { walk } from "https://deno.land/std@0.128.0/fs/walk.ts";
|
export { walk } from "https://deno.land/std@0.128.0/fs/walk.ts";
|
||||||
export { serve } from "https://deno.land/std@0.128.0/http/server.ts";
|
export { serve } from "https://deno.land/std@0.128.0/http/server.ts";
|
||||||
|
export type {
|
||||||
|
ConnInfo,
|
||||||
|
Handler as RequestHandler,
|
||||||
|
} from "https://deno.land/std@0.128.0/http/server.ts";
|
||||||
|
|
||||||
// -- router --
|
// -- router --
|
||||||
export * as router from "https://crux.land/router@0.0.5";
|
export * as router from "https://crux.land/router@0.0.6";
|
||||||
|
|
||||||
// -- media types --
|
// -- media types --
|
||||||
export { lookup as mediaTypeLookup } from "https://deno.land/x/media_types@v2.10.2/mod.ts";
|
export { lookup as mediaTypeLookup } from "https://deno.land/x/media_types@v2.10.2/mod.ts";
|
||||||
|
|||||||
+5
-5
@@ -1,5 +1,5 @@
|
|||||||
import { ComponentType } from "../runtime/deps.ts";
|
import { ComponentType } from "../runtime/deps.ts";
|
||||||
import { router } from "./deps.ts";
|
import { ConnInfo, router } from "./deps.ts";
|
||||||
import {
|
import {
|
||||||
AppProps,
|
AppProps,
|
||||||
ErrorPageProps,
|
ErrorPageProps,
|
||||||
@@ -32,21 +32,21 @@ export interface IslandModule {
|
|||||||
default: ComponentType<any>;
|
default: ComponentType<any>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface HandlerContext<T = unknown> {
|
export interface HandlerContext<T = unknown> extends ConnInfo {
|
||||||
params: Record<string, string>;
|
params: Record<string, string>;
|
||||||
render: (data?: T) => Response;
|
render: (data?: T) => Response;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface UnknownHandlerContext {
|
export interface UnknownHandlerContext extends ConnInfo {
|
||||||
render: () => Response;
|
render: () => Response;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ErrorHandlerContext {
|
export interface ErrorHandlerContext extends ConnInfo {
|
||||||
error: unknown;
|
error: unknown;
|
||||||
render: () => Response;
|
render: () => Response;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface MiddlewareHandlerContext {
|
export interface MiddlewareHandlerContext extends ConnInfo {
|
||||||
handle: () => Promise<Response>;
|
handle: () => Promise<Response>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+14
-1
@@ -3,7 +3,20 @@ import { assert, assertEquals, assertStringIncludes } from "./deps.ts";
|
|||||||
import manifest from "./fixture_error/fresh.gen.ts";
|
import manifest from "./fixture_error/fresh.gen.ts";
|
||||||
|
|
||||||
const ctx = await ServerContext.fromManifest(manifest);
|
const ctx = await ServerContext.fromManifest(manifest);
|
||||||
const router = ctx.handler();
|
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",
|
||||||
|
port: 80,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
Deno.test("error page rendered", async () => {
|
Deno.test("error page rendered", async () => {
|
||||||
const resp = await router(new Request("https://fresh.deno.dev/"));
|
const resp = await router(new Request("https://fresh.deno.dev/"));
|
||||||
|
|||||||
+18
-16
@@ -10,14 +10,15 @@ import * as $4 from "./routes/_middleware.ts";
|
|||||||
import * as $5 from "./routes/_render.ts";
|
import * as $5 from "./routes/_render.ts";
|
||||||
import * as $6 from "./routes/api/get_only.ts";
|
import * as $6 from "./routes/api/get_only.ts";
|
||||||
import * as $7 from "./routes/books/[id].tsx";
|
import * as $7 from "./routes/books/[id].tsx";
|
||||||
import * as $8 from "./routes/failure.ts";
|
import * as $8 from "./routes/connInfo.ts";
|
||||||
import * as $9 from "./routes/index.tsx";
|
import * as $9 from "./routes/failure.ts";
|
||||||
import * as $10 from "./routes/intercept.tsx";
|
import * as $10 from "./routes/index.tsx";
|
||||||
import * as $11 from "./routes/intercept_args.tsx";
|
import * as $11 from "./routes/intercept.tsx";
|
||||||
import * as $12 from "./routes/params.tsx";
|
import * as $12 from "./routes/intercept_args.tsx";
|
||||||
import * as $13 from "./routes/props/[id].tsx";
|
import * as $13 from "./routes/params.tsx";
|
||||||
import * as $14 from "./routes/static.tsx";
|
import * as $14 from "./routes/props/[id].tsx";
|
||||||
import * as $15 from "./routes/wildcard.tsx";
|
import * as $15 from "./routes/static.tsx";
|
||||||
|
import * as $16 from "./routes/wildcard.tsx";
|
||||||
import * as $$0 from "./islands/Test.tsx";
|
import * as $$0 from "./islands/Test.tsx";
|
||||||
|
|
||||||
const manifest = {
|
const manifest = {
|
||||||
@@ -30,14 +31,15 @@ const manifest = {
|
|||||||
"./routes/_render.ts": $5,
|
"./routes/_render.ts": $5,
|
||||||
"./routes/api/get_only.ts": $6,
|
"./routes/api/get_only.ts": $6,
|
||||||
"./routes/books/[id].tsx": $7,
|
"./routes/books/[id].tsx": $7,
|
||||||
"./routes/failure.ts": $8,
|
"./routes/connInfo.ts": $8,
|
||||||
"./routes/index.tsx": $9,
|
"./routes/failure.ts": $9,
|
||||||
"./routes/intercept.tsx": $10,
|
"./routes/index.tsx": $10,
|
||||||
"./routes/intercept_args.tsx": $11,
|
"./routes/intercept.tsx": $11,
|
||||||
"./routes/params.tsx": $12,
|
"./routes/intercept_args.tsx": $12,
|
||||||
"./routes/props/[id].tsx": $13,
|
"./routes/params.tsx": $13,
|
||||||
"./routes/static.tsx": $14,
|
"./routes/props/[id].tsx": $14,
|
||||||
"./routes/wildcard.tsx": $15,
|
"./routes/static.tsx": $15,
|
||||||
|
"./routes/wildcard.tsx": $16,
|
||||||
},
|
},
|
||||||
islands: {
|
islands: {
|
||||||
"./islands/Test.tsx": $$0,
|
"./islands/Test.tsx": $$0,
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
import { Handlers } from "../../../server.ts";
|
||||||
|
|
||||||
|
export const handler: Handlers = {
|
||||||
|
GET(_req, ctx) {
|
||||||
|
return new Response((ctx.remoteAddr as Deno.NetAddr).hostname);
|
||||||
|
},
|
||||||
|
};
|
||||||
+22
-1
@@ -3,7 +3,20 @@ import { assert, assertEquals, assertStringIncludes } from "./deps.ts";
|
|||||||
import manifest from "./fixture/fresh.gen.ts";
|
import manifest from "./fixture/fresh.gen.ts";
|
||||||
|
|
||||||
const ctx = await ServerContext.fromManifest(manifest);
|
const ctx = await ServerContext.fromManifest(manifest);
|
||||||
const router = ctx.handler();
|
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",
|
||||||
|
port: 80,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
Deno.test("/ page prerender", async () => {
|
Deno.test("/ page prerender", async () => {
|
||||||
const resp = await router(new Request("https://fresh.deno.dev/"));
|
const resp = await router(new Request("https://fresh.deno.dev/"));
|
||||||
@@ -194,3 +207,11 @@ Deno.test("/params/:path*", async () => {
|
|||||||
const body = await resp.text();
|
const body = await resp.text();
|
||||||
assertEquals(body, "bar/baz");
|
assertEquals(body, "bar/baz");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test("/connInfo", async () => {
|
||||||
|
const resp = await router(new Request("https://fresh.deno.dev/connInfo"));
|
||||||
|
assert(resp);
|
||||||
|
assertEquals(resp.status, 200);
|
||||||
|
const body = await resp.text();
|
||||||
|
assertEquals(body, "127.0.0.1");
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user