From 42ae41ba87d7551320e014883656ae529d2cc967 Mon Sep 17 00:00:00 2001 From: HorizonCode Date: Thu, 11 May 2023 15:59:44 +0200 Subject: [PATCH] replace URL with split to gain some extra performance --- example/test.ts | 10 +++++----- mod.ts | 41 +++++++++++++++++++++++------------------ 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/example/test.ts b/example/test.ts index 081534c..65d87f3 100644 --- a/example/test.ts +++ b/example/test.ts @@ -1,16 +1,16 @@ import { Status } from "https://deno.land/std@0.186.0/http/http_status.ts"; -import { hrtime } from "https://deno.land/std@0.177.0/node/process.ts"; -import prettyTime from 'npm:pretty-time'; +import prettyTime from "npm:pretty-time"; import { HTTPServer } from "../mod.ts"; const httpServer = new HTTPServer(); httpServer.middleware(async (req, done) => { - const started = hrtime(); + const perStart = performance.now(); console.log(`${req.method} - ${req.ip()} - ${req.path}`); await done(); - const processTime = hrtime(started); - console.log(`Processed in ${prettyTime(processTime)}`); + const pt = performance.now() - perStart; + const hrArray: number[] = [0, Math.trunc(pt * 1000000)]; + console.log(`Processed in ${prettyTime(hrArray)}`); }); httpServer.error((req, _rep) => { diff --git a/mod.ts b/mod.ts index 12aae5c..8ae5657 100644 --- a/mod.ts +++ b/mod.ts @@ -22,8 +22,7 @@ type RouteHandler = ( type RouteMiddlewareHandler = ( req: RouteRequest, done: () => Promise, -) => - | Promise; +) => Promise; type RouteParam = { idx: number; @@ -100,14 +99,22 @@ export class HTTPServer { private async handleHttp(conn: Deno.Conn) { const httpConn = Deno.serveHttp(conn); for await (const requestEvent of httpConn) { - const routeRequest = new RouteRequest(requestEvent.request, conn); + const filepath = decodeURIComponent( + "/" + requestEvent.request.url.split("/").slice(3).join("/"), + ); + const routeRequest = new RouteRequest( + requestEvent.request, + conn, + filepath, + requestEvent.request.url, + ); const routeReply: RouteReply = new RouteReply(); - const url = new URL(requestEvent.request.url); - const filepath = decodeURIComponent(url.pathname); + if (filepath.startsWith("/_static")) { this.handleNotFound(routeRequest, routeReply, requestEvent); continue; } + let resolveAction: (value?: unknown) => void = () => {}; let middlewarePromise; @@ -131,21 +138,20 @@ export class HTTPServer { try { file = await Deno.open(pathLoc, { read: true }); } catch { - this.handleNotFound(routeRequest, routeReply, requestEvent); if (middlewarePromise) resolveAction(); + this.handleNotFound(routeRequest, routeReply, requestEvent); continue; } const readableStream = file.readable; const response = new Response(readableStream); - await requestEvent.respondWith(response); if (middlewarePromise) resolveAction(); + await requestEvent.respondWith(response); return; } + const routeName = `${requestEvent.request.method}@${filepath}`; - let route = this.routes.has(routeName) - ? this.routes.get(routeName) - : undefined; + let route = this.routes.get(routeName); if (route) { let handler = await route.handler( @@ -157,6 +163,7 @@ export class HTTPServer { handler = JSON.stringify(handler, null, 2); } + if (middlewarePromise) resolveAction(); await requestEvent.respondWith( new Response(handler as string, { status: routeReply.statusCode, @@ -164,7 +171,6 @@ export class HTTPServer { statusText: STATUS_TEXT[routeReply.statusCode], }), ); - if (middlewarePromise) resolveAction(); continue; } @@ -189,6 +195,7 @@ export class HTTPServer { routeRequest, routeReply, ); + if (middlewarePromise) resolveAction(); await requestEvent.respondWith( new Response(handler as string, { status: routeReply.statusCode, @@ -196,11 +203,10 @@ export class HTTPServer { statusText: STATUS_TEXT[routeReply.statusCode], }), ); - if (middlewarePromise) resolveAction(); continue; } - this.handleNotFound(routeRequest, routeReply, requestEvent); if (middlewarePromise) resolveAction(); + this.handleNotFound(routeRequest, routeReply, requestEvent); } } @@ -293,14 +299,13 @@ export class RouteRequest { pathParams: { [key: string]: string }; private remoteIpAddr: string; - constructor(request: Request, conn: Deno.Conn) { - this.url = request.url; - const urlObj = new URL(request.url); - this.path = decodeURIComponent(urlObj.pathname); + constructor(request: Request, conn: Deno.Conn, path: string, url: string) { + this.url = url; + this.path = decodeURIComponent(path); this.headers = request.headers; this.method = request.method as HTTPMethod; this.pathParams = {}; - this.queryParams = this.paramsToObject(urlObj.searchParams.entries()); + this.queryParams = this.paramsToObject(new URL(url).searchParams.entries()); this.remoteIpAddr = "hostname" in conn.remoteAddr ? conn.remoteAddr["hostname"] : "127.0.0.1";