Skip to content

Commit

Permalink
Merge pull request #501 from kwaa/v2/new-server
Browse files Browse the repository at this point in the history
refactor(server): use `Deno.serve()`
  • Loading branch information
oscarotero authored Oct 22, 2023
2 parents 9a10f33 + 42447bb commit b995007
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 29 deletions.
2 changes: 1 addition & 1 deletion cli/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export async function build(
server.use(...middlewares);
}

await server.start();
server.start();
}

function localIp(): string | undefined {
Expand Down
44 changes: 19 additions & 25 deletions core/server.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
/// <reference lib="deno.unstable" />
// Deno.Server.shutdown() is unstable

import { posix } from "../deps/path.ts";
import Events from "./events.ts";
import {
serveFile as HttpServeFile,
Server as HttpServer,
} from "../deps/http.ts";
import { serveFile as HttpServeFile } from "../deps/http.ts";

import type { Event, EventListener, EventOptions } from "../core.ts";
import type { ConnInfo } from "../deps/http.ts";

/** The options to configure the local server */
export interface Options {
export interface Options extends Deno.ServeOptions {
/** The root path */
root: string;

/** The port to listen on */
port: number;
}

export const defaults: Options = {
Expand All @@ -26,7 +22,7 @@ export type RequestHandler = (req: Request) => Promise<Response>;
export type Middleware = (
req: Request,
next: RequestHandler,
conn: ConnInfo,
info: Deno.ServeHandlerInfo,
) => Promise<Response>;

/** Custom events for server */
Expand All @@ -50,7 +46,7 @@ export default class Server {
events: Events<ServerEvent> = new Events<ServerEvent>();
options: Options;
middlewares: Middleware[] = [];
#server?: HttpServer;
#server?: Deno.Server;

constructor(options: Partial<Options> = {}) {
this.options = { ...defaults, ...options };
Expand Down Expand Up @@ -78,23 +74,18 @@ export default class Server {
}

/** Start the server */
async start() {
const { port } = this.options;

this.#server = new HttpServer({
port: port,
handler: (request, connInfo) => this.handle(request, connInfo),
});

this.dispatchEvent({ type: "start" });

await this.#server.listenAndServe();
start(signal?: Deno.ServeOptions["signal"]) {
this.#server = Deno.serve({
...this.options,
signal,
onListen: () => this.dispatchEvent({ type: "start" }),
}, this.handle);
}

/** Stops the server */
stop() {
try {
this.#server?.close();
this.#server?.shutdown();
} catch (error) {
this.dispatchEvent({
type: "error",
Expand All @@ -104,7 +95,10 @@ export default class Server {
}

/** Handle a http request event */
async handle(request: Request, connInfo: ConnInfo): Promise<Response> {
async handle(
request: Request,
info: Deno.ServeHandlerInfo,
): Promise<Response> {
const middlewares = [...this.middlewares];

const next: RequestHandler = async (
Expand All @@ -113,7 +107,7 @@ export default class Server {
const middleware = middlewares.shift();

if (middleware) {
return await middleware(request, next, connInfo);
return await middleware(request, next, info);
}

return await serveFile(this.options.root, request);
Expand Down
3 changes: 0 additions & 3 deletions deps/http.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
export { serveFile } from "https://deno.land/[email protected]/http/file_server.ts";
export { Server } from "https://deno.land/[email protected]/http/server.ts";

export type { ConnInfo } from "https://deno.land/[email protected]/http/server.ts";

0 comments on commit b995007

Please sign in to comment.