From 997fe6dc77a4b1fce35b42a5ac88450a511357ac Mon Sep 17 00:00:00 2001 From: HorizonCode Date: Sun, 14 May 2023 22:07:08 +0200 Subject: [PATCH] change middleware done result to own type --- example/test.ts | 5 +++-- mod.ts | 45 ++++++++++++++++++++++++++++++--------------- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/example/test.ts b/example/test.ts index f2cc3cb..1b04b92 100644 --- a/example/test.ts +++ b/example/test.ts @@ -29,11 +29,12 @@ httpServer.preprocessor((_req, rep) => { }); httpServer.middleware(async (req, _rep, done) => { - const processTime = await done(); + const result = await done(); + const hrArray: number[] = [0, Math.trunc(result.processTime * 1000000)]; if (!req.resourceRequest) { console.log( `${req.method} - ${req.remoteIpAddr} - ${req.path} - ${ - prettyTime(processTime) + prettyTime(hrArray) }`, ); } diff --git a/mod.ts b/mod.ts index e0c5165..5b5566b 100644 --- a/mod.ts +++ b/mod.ts @@ -5,7 +5,10 @@ import { import * as path from "https://deno.land/std@0.185.0/path/mod.ts"; import * as cookie from "https://deno.land/std@0.185.0/http/cookie.ts"; import { Aes } from "https://deno.land/x/crypto@v0.10.0/aes.ts"; -import { Cbc, Padding } from "https://deno.land/x/crypto@v0.10.0/block-modes.ts"; +import { + Cbc, + Padding, +} from "https://deno.land/x/crypto@v0.10.0/block-modes.ts"; import { cryptoRandomString } from "https://deno.land/x/crypto_random_string@1.0.0/mod.ts"; type HTTPServerOptions = { @@ -17,6 +20,10 @@ type HTTPServerOptions = { sessionExpire?: SessionExpire | number; }; +type MiddlewareResult = { + processTime: number; +}; + export enum SessionExpire { NEVER = 2147483647, } @@ -38,7 +45,7 @@ type RouteHandler = ( type RouteMiddlewareHandler = ( req: RouteRequest, rep: RouteReply, - done: () => Promise, + done: () => Promise, ) => Promise; type RoutePreprocessor = ( @@ -65,7 +72,10 @@ export class HTTPServer { if (options.sessionSecret) { if (![16, 24, 32].includes(options.sessionSecret.length)) { const randomString = cryptoRandomString({ length: 32 }); - throw new Error("\nInvalid key size (must be either 16, 24 or 32 bytes)\nHere is a pregenerated key: " + randomString); + throw new Error( + "\nInvalid key size (must be either 16, 24 or 32 bytes)\nHere is a pregenerated key: " + + randomString, + ); } } this.settings = options; @@ -152,11 +162,11 @@ export class HTTPServer { preProcessor(routeRequest, routeReply) ); - let resolveAction: (value: number[]) => void = () => {}; + let resolveAction: (value: MiddlewareResult) => void = () => {}; let middlewarePromise; const perStart = performance.now(); if (this.middlewareHandler) { - middlewarePromise = (): Promise => { + middlewarePromise = (): Promise => { return new Promise((resolve) => { resolveAction = resolve; }); @@ -177,8 +187,9 @@ export class HTTPServer { } catch { if (middlewarePromise) { const pt = performance.now() - perStart; - const hrArray: number[] = [0, Math.trunc(pt * 1000000)]; - resolveAction(hrArray); + resolveAction({ + processTime: pt, + }); } this.processSession(routeRequest, routeReply); this.handleNotFound(routeRequest, routeReply, requestEvent); @@ -189,8 +200,9 @@ export class HTTPServer { const response = new Response(readableStream); if (middlewarePromise) { const pt = performance.now() - perStart; - const hrArray: number[] = [0, Math.trunc(pt * 1000000)]; - resolveAction(hrArray); + resolveAction({ + processTime: pt, + }); } this.processSession(routeRequest, routeReply); await requestEvent.respondWith(response); @@ -214,8 +226,9 @@ export class HTTPServer { if (middlewarePromise) { const pt = performance.now() - perStart; - const hrArray: number[] = [0, Math.trunc(pt * 1000000)]; - resolveAction(hrArray); + resolveAction({ + processTime: pt, + }); } this.processSession(routeRequest, routeReply); await requestEvent.respondWith( @@ -254,8 +267,9 @@ export class HTTPServer { ); if (middlewarePromise) { const pt = performance.now() - perStart; - const hrArray: number[] = [0, Math.trunc(pt * 1000000)]; - resolveAction(hrArray); + resolveAction({ + processTime: pt, + }); } this.processSession(routeRequest, routeReply); @@ -270,8 +284,9 @@ export class HTTPServer { } if (middlewarePromise) { const pt = performance.now() - perStart; - const hrArray: number[] = [0, Math.trunc(pt * 1000000)]; - resolveAction(hrArray); + resolveAction({ + processTime: pt, + }); } this.processSession(routeRequest, routeReply); this.handleNotFound(routeRequest, routeReply, requestEvent);