diff --git a/src/server/context.ts b/src/server/context.ts index bfd9f2eb..59a96056 100644 --- a/src/server/context.ts +++ b/src/server/context.ts @@ -1,7 +1,9 @@ import { + ConnInfo, extname, fromFileUrl, mediaTypeLookup, + RequestHandler, router, toFileUrl, walk, @@ -177,7 +179,7 @@ export class ServerContext { name, component, handler: handler ?? - ((req, ctx) => router.defaultErrorHandler(req, ctx.error)), + ((req, ctx) => router.defaultErrorHandler(req, ctx, ctx.error)), csp: Boolean(config?.csp ?? false), }; } @@ -256,10 +258,10 @@ export class ServerContext { * This functions returns a request handler that handles all routes required * by fresh, including static files. */ - handler(): router.RequestHandler { + handler(): RequestHandler { const inner = router.router(...this.#routes()); const middleware = this.#middleware; - return function handler(req: Request) { + return function handler(req: Request, connInfo: ConnInfo) { // Redirect requests that end with a trailing slash // to their non-trailing slash counterpart. // Ex: /about/ -> /about @@ -268,8 +270,8 @@ export class ServerContext { url.pathname = url.pathname.slice(0, -1); return Response.redirect(url.href, 307); } - const handle = () => Promise.resolve(inner(req)); - return middleware.handler(req, { handle }); + const handle = () => Promise.resolve(inner(req, connInfo)); + return middleware.handler(req, { handle, ...connInfo }); }; } @@ -277,7 +279,7 @@ export class ServerContext { * This function returns all routes required by fresh as an extended * path-to-regex, to handler mapping. */ - #routes(): [router.Routes, router.RequestHandler, router.ErrorHandler] { + #routes(): [router.Routes, RequestHandler, router.ErrorHandler] { const routes: router.Routes = {}; // Add the static file routes. @@ -351,30 +353,36 @@ export class ServerContext { for (const page of this.#pages) { const createRender = genRender(page, 200); if (typeof page.handler === "function") { - routes[page.route] = (req, params) => + routes[page.route] = (req, connInfo, params) => (page.handler as Handler)(req, { + ...connInfo, params, render: createRender(req, params), }); } else { for (const [method, handler] of Object.entries(page.handler)) { - routes[`${method}@${page.route}`] = (req, params) => - handler(req, { params, render: createRender(req, params) }); + routes[`${method}@${page.route}`] = (req, connInfo, params) => + handler(req, { + ...connInfo, + params, + render: createRender(req, params), + }); } } } const unknownHandlerRender = genRender(this.#notFound, 404); - const unknownHandler = (req: Request) => + const unknownHandler = (req: Request, connInfo: ConnInfo) => this.#notFound.handler( req, { + ...connInfo, render: unknownHandlerRender(req, {}), }, ); const errorHandlerRender = genRender(this.#error, 500); - const errorHandler = (req: Request, error: unknown) => { + const errorHandler = (req: Request, connInfo: ConnInfo, error: unknown) => { console.error( "%cAn error occured during route handling or page rendering.", "color:red", @@ -383,6 +391,7 @@ export class ServerContext { return this.#error.handler( req, { + ...connInfo, error, render: errorHandlerRender(req, {}, error), }, @@ -455,7 +464,7 @@ export class ServerContext { * constants.INTERNAL_PREFIX */ #bundleAssetRoute = (): router.MatchHandler => { - return async (_req, params) => { + return async (_req, _connInfo, params) => { const path = `/${params.path}`; const file = await this.#bundler.get(path); let res; diff --git a/src/server/deps.ts b/src/server/deps.ts index 9d4cd926..c9a7dbe2 100644 --- a/src/server/deps.ts +++ b/src/server/deps.ts @@ -9,9 +9,13 @@ export { } 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 { 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 -- -export * as router from "https://crux.land/router@0.0.5"; +export * as router from "https://crux.land/router@0.0.6"; // -- media types -- export { lookup as mediaTypeLookup } from "https://deno.land/x/media_types@v2.10.2/mod.ts"; diff --git a/src/server/types.ts b/src/server/types.ts index 6a994e74..9d8ffcfe 100644 --- a/src/server/types.ts +++ b/src/server/types.ts @@ -1,5 +1,5 @@ import { ComponentType } from "../runtime/deps.ts"; -import { router } from "./deps.ts"; +import { ConnInfo, router } from "./deps.ts"; import { AppProps, ErrorPageProps, @@ -32,21 +32,21 @@ export interface IslandModule { default: ComponentType; } -export interface HandlerContext { +export interface HandlerContext extends ConnInfo { params: Record; render: (data?: T) => Response; } -export interface UnknownHandlerContext { +export interface UnknownHandlerContext extends ConnInfo { render: () => Response; } -export interface ErrorHandlerContext { +export interface ErrorHandlerContext extends ConnInfo { error: unknown; render: () => Response; } -export interface MiddlewareHandlerContext { +export interface MiddlewareHandlerContext extends ConnInfo { handle: () => Promise; } diff --git a/tests/error_test.ts b/tests/error_test.ts index 313352b2..340ab332 100644 --- a/tests/error_test.ts +++ b/tests/error_test.ts @@ -3,7 +3,20 @@ import { assert, assertEquals, assertStringIncludes } from "./deps.ts"; import manifest from "./fixture_error/fresh.gen.ts"; 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 () => { const resp = await router(new Request("https://fresh.deno.dev/")); diff --git a/tests/fixture/fresh.gen.ts b/tests/fixture/fresh.gen.ts index 14117582..f1add8a5 100644 --- a/tests/fixture/fresh.gen.ts +++ b/tests/fixture/fresh.gen.ts @@ -10,14 +10,15 @@ import * as $4 from "./routes/_middleware.ts"; import * as $5 from "./routes/_render.ts"; import * as $6 from "./routes/api/get_only.ts"; import * as $7 from "./routes/books/[id].tsx"; -import * as $8 from "./routes/failure.ts"; -import * as $9 from "./routes/index.tsx"; -import * as $10 from "./routes/intercept.tsx"; -import * as $11 from "./routes/intercept_args.tsx"; -import * as $12 from "./routes/params.tsx"; -import * as $13 from "./routes/props/[id].tsx"; -import * as $14 from "./routes/static.tsx"; -import * as $15 from "./routes/wildcard.tsx"; +import * as $8 from "./routes/connInfo.ts"; +import * as $9 from "./routes/failure.ts"; +import * as $10 from "./routes/index.tsx"; +import * as $11 from "./routes/intercept.tsx"; +import * as $12 from "./routes/intercept_args.tsx"; +import * as $13 from "./routes/params.tsx"; +import * as $14 from "./routes/props/[id].tsx"; +import * as $15 from "./routes/static.tsx"; +import * as $16 from "./routes/wildcard.tsx"; import * as $$0 from "./islands/Test.tsx"; const manifest = { @@ -30,14 +31,15 @@ const manifest = { "./routes/_render.ts": $5, "./routes/api/get_only.ts": $6, "./routes/books/[id].tsx": $7, - "./routes/failure.ts": $8, - "./routes/index.tsx": $9, - "./routes/intercept.tsx": $10, - "./routes/intercept_args.tsx": $11, - "./routes/params.tsx": $12, - "./routes/props/[id].tsx": $13, - "./routes/static.tsx": $14, - "./routes/wildcard.tsx": $15, + "./routes/connInfo.ts": $8, + "./routes/failure.ts": $9, + "./routes/index.tsx": $10, + "./routes/intercept.tsx": $11, + "./routes/intercept_args.tsx": $12, + "./routes/params.tsx": $13, + "./routes/props/[id].tsx": $14, + "./routes/static.tsx": $15, + "./routes/wildcard.tsx": $16, }, islands: { "./islands/Test.tsx": $$0, diff --git a/tests/fixture/routes/connInfo.ts b/tests/fixture/routes/connInfo.ts new file mode 100644 index 00000000..f493400c --- /dev/null +++ b/tests/fixture/routes/connInfo.ts @@ -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); + }, +}; diff --git a/tests/main_test.ts b/tests/main_test.ts index 6c7c687e..2c93f165 100644 --- a/tests/main_test.ts +++ b/tests/main_test.ts @@ -3,7 +3,20 @@ import { assert, assertEquals, assertStringIncludes } from "./deps.ts"; import manifest from "./fixture/fresh.gen.ts"; 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 () => { const resp = await router(new Request("https://fresh.deno.dev/")); @@ -194,3 +207,11 @@ Deno.test("/params/:path*", async () => { const body = await resp.text(); 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"); +});