Skip to content

Commit

Permalink
Use Deno.serve
Browse files Browse the repository at this point in the history
  • Loading branch information
timonson committed Jul 5, 2023
1 parent 80a203d commit 5884924
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<C extends Context>(ctx: C) {
const name = ctx.result.pathname.groups.name || "nobody";
Expand All @@ -38,7 +33,7 @@ function welcome<C extends Context>(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!
Expand Down
6 changes: 2 additions & 4 deletions context.ts
Original file line number Diff line number Diff line change
@@ -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<string | number | symbol, unknown>;
// deno-lint-ignore no-explicit-any
Expand All @@ -16,7 +14,7 @@ type DefaultState = Record<string, any>;
* ```
*/
export class Context<S extends State = DefaultState> {
connInfo: ConnInfo;
connInfo: Deno.ServeHandlerInfo;
error: Error | null = null;
result: URLPatternResult = {} as URLPatternResult;
request: Request;
Expand All @@ -25,7 +23,7 @@ export class Context<S extends State = DefaultState> {
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;
Expand Down
8 changes: 0 additions & 8 deletions deps.ts

This file was deleted.

4 changes: 2 additions & 2 deletions example.ts
Original file line number Diff line number Diff line change
@@ -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 }> {
Expand Down Expand Up @@ -86,4 +86,4 @@ const handler = createHandler(Ctx)(tryMiddleware)(catchMiddleware)(
finallyMiddleware,
);

await listen(handler)({ port: 8080 });
Deno.serve(handler);
20 changes: 12 additions & 8 deletions handler.ts
Original file line number Diff line number Diff line change
@@ -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<S> = {
export type ServerHandlerOptions<S> = {
state?: S;
enableXResponseTimeHeader?: boolean;
enableLogger?: boolean;
Expand Down Expand Up @@ -38,17 +37,24 @@ export function assertError(caught: unknown): Error {
* ```
*/
export function createHandler<C extends Context, S>(
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<S> = {},
}: ServerHandlerOptions<S> = {},
) {
return (...tryMiddlewares: Middleware<C>[]) =>
(...catchMiddlewares: Middleware<C>[]) =>
(...finallyMiddlewares: Middleware<C>[]): Handler =>
async (request: Request, connInfo: ConnInfo): Promise<Response> => {
(...finallyMiddlewares: Middleware<C>[]): Deno.ServeHandler =>
async (
request: Request,
connInfo: Deno.ServeHandlerInfo,
): Promise<Response> => {
const ctx = new Context(request, connInfo, state);
try {
ctx.startTime = Date.now();
Expand All @@ -66,5 +72,3 @@ export function createHandler<C extends Context, S>(
: ctx.response;
};
}

export type { Handler };
4 changes: 1 addition & 3 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
23 changes: 0 additions & 23 deletions server.ts

This file was deleted.

9 changes: 2 additions & 7 deletions url-pattern/server.ts
Original file line number Diff line number Diff line change
@@ -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/[email protected]/http/file_server.ts";
import { fromFileUrl } from "https://deno.land/[email protected]/path/mod.ts";

Expand All @@ -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);

0 comments on commit 5884924

Please sign in to comment.