diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b2c102a..fe672e5 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -22,7 +22,7 @@ jobs: - name: Setup Deno run: | - curl -fsSL https://deno.land/x/install/install.sh | sh ${{ matrix.deno == 'old' && '-s v1.34.3' || '' }} + curl -fsSL https://deno.land/x/install/install.sh | sh ${{ matrix.deno == 'old' && '-s v1.35.0' || '' }} echo "$HOME/.deno/bin" >> $${{ runner.os == 'Windows' && 'env:' || '' }}GITHUB_PATH - name: Upgrade to Deno canary if: matrix.deno == 'canary' diff --git a/README.md b/README.md index 164996b..5d6614b 100644 --- a/README.md +++ b/README.md @@ -22,12 +22,7 @@ deno doc https://deno.land/x/composium/mod.ts ## Example ```ts -import { - Context, - createDefaultHandler, - createGetRoute, - listen, -} from "./mod.ts"; +import { Context, createDefaultHandler, createGetRoute } from "./mod.ts"; function welcome(ctx: C) { const name = ctx.result.pathname.groups.name || "nobody"; @@ -38,7 +33,7 @@ function welcome(ctx: C) { const route = createGetRoute({ pathname: "/{:name}?" })(welcome); const handler = createDefaultHandler(route); -await listen(handler)({ port: 8080 }); +Deno.serve(handler); // curl http://localhost:8080/Joe // Welcome, Joe! diff --git a/context.ts b/context.ts index deb4223..2a70e1a 100644 --- a/context.ts +++ b/context.ts @@ -1,5 +1,3 @@ -import { type ConnInfo } from "./deps.ts"; - /** Any object can be assigned to the property `state` of the `Context` object. */ type State = Record; // deno-lint-ignore no-explicit-any @@ -16,7 +14,7 @@ type DefaultState = Record; * ``` */ export class Context { - connInfo: ConnInfo; + connInfo: Deno.ServeHandlerInfo; error: Error | null = null; result: URLPatternResult = {} as URLPatternResult; request: Request; @@ -25,7 +23,7 @@ export class Context { url: URL; startTime = NaN; - constructor(request: Request, connInfo: ConnInfo, state?: S) { + constructor(request: Request, connInfo: Deno.ServeHandlerInfo, state?: S) { this.connInfo = connInfo; this.request = request; this.state = state || {} as S; diff --git a/deps.ts b/deps.ts deleted file mode 100644 index 36b903f..0000000 --- a/deps.ts +++ /dev/null @@ -1,8 +0,0 @@ -export { serve, serveTls } from "https://deno.land/std@0.192.0/http/server.ts"; - -export type { - ConnInfo, - Handler, - ServeInit, - ServeTlsInit, -} from "https://deno.land/std@0.192.0/http/server.ts"; diff --git a/example.ts b/example.ts index 956b247..9881b45 100644 --- a/example.ts +++ b/example.ts @@ -1,4 +1,4 @@ -import { compose, Context, createHandler, createRoute, listen } from "./mod.ts"; +import { compose, Context, createHandler, createRoute } from "./mod.ts"; // You can optionally extend the default `Context` object or pass a `State` type. export class Ctx extends Context<{ start: number }> { @@ -86,4 +86,4 @@ const handler = createHandler(Ctx)(tryMiddleware)(catchMiddleware)( finallyMiddleware, ); -await listen(handler)({ port: 8080 }); +Deno.serve(handler); diff --git a/handler.ts b/handler.ts index 050659d..db9a733 100644 --- a/handler.ts +++ b/handler.ts @@ -1,9 +1,8 @@ -import { type ConnInfo, type Handler } from "./deps.ts"; import { compose } from "./composition.ts"; import { type Middleware } from "./route.ts"; import { type Context } from "./context.ts"; -export type HandlerOptions = { +export type ServerHandlerOptions = { state?: S; enableXResponseTimeHeader?: boolean; enableLogger?: boolean; @@ -38,17 +37,24 @@ export function assertError(caught: unknown): Error { * ``` */ export function createHandler( - Context: new (request: Request, connInfo: ConnInfo, state?: S) => C, + Context: new ( + request: Request, + connInfo: Deno.ServeHandlerInfo, + state?: S, + ) => C, { state, enableXResponseTimeHeader = true, enableLogger = false, - }: HandlerOptions = {}, + }: ServerHandlerOptions = {}, ) { return (...tryMiddlewares: Middleware[]) => (...catchMiddlewares: Middleware[]) => - (...finallyMiddlewares: Middleware[]): Handler => - async (request: Request, connInfo: ConnInfo): Promise => { + (...finallyMiddlewares: Middleware[]): Deno.ServeHandler => + async ( + request: Request, + connInfo: Deno.ServeHandlerInfo, + ): Promise => { const ctx = new Context(request, connInfo, state); try { ctx.startTime = Date.now(); @@ -66,5 +72,3 @@ export function createHandler( : ctx.response; }; } - -export type { Handler }; diff --git a/mod.ts b/mod.ts index c2e86fd..ee4c2b6 100644 --- a/mod.ts +++ b/mod.ts @@ -2,14 +2,12 @@ import { createRoute, type Middleware } from "./route.ts"; import { createHandler } from "./handler.ts"; import { Context } from "./context.ts"; -export { listen } from "./server.ts"; export { Context } from "./context.ts"; export { createRoute, type Method, type Middleware } from "./route.ts"; export { assertError, createHandler, - type Handler, - type HandlerOptions, + type ServerHandlerOptions, } from "./handler.ts"; export { compose, composeSync } from "./composition.ts"; diff --git a/server.ts b/server.ts deleted file mode 100644 index 897a68d..0000000 --- a/server.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { - type Handler, - serve, - type ServeInit, - serveTls, - type ServeTlsInit, -} from "./deps.ts"; - -/** - * A curried function which takes a `Handler` and `options`. It constructs a - * server, creates a listener on the given address, accepts incoming connections, - * upgrades them to TLS and handles requests. - * ```ts - * await listen(handler)({ port: 8080 }) - * ``` - */ -export function listen(handler: Handler) { - return async (options: ServeInit | ServeTlsInit) => { - return "certFile" in options || "keyFile" in options - ? await serveTls(handler, options) - : await serve(handler, options); - }; -} diff --git a/url-pattern/server.ts b/url-pattern/server.ts index f39ca3a..066029b 100644 --- a/url-pattern/server.ts +++ b/url-pattern/server.ts @@ -1,9 +1,4 @@ -import { - type Context, - createDefaultHandler, - createGetRoute, - listen, -} from "../mod.ts"; +import { type Context, createDefaultHandler, createGetRoute } from "../mod.ts"; import { serveDir } from "https://deno.land/std@0.192.0/http/file_server.ts"; import { fromFileUrl } from "https://deno.land/std@0.192.0/path/mod.ts"; @@ -18,4 +13,4 @@ async function serveStatic(ctx: Context) { const mainMiddleware = createGetRoute({ pathname: "*" })(serveStatic); const handler = createDefaultHandler(mainMiddleware); -await listen(handler)({ port: 8080 }); +Deno.serve(handler);