change middleware done result to own type

This commit is contained in:
HorizonCode 2023-05-14 22:07:08 +02:00
parent b335bb61bd
commit 997fe6dc77
2 changed files with 33 additions and 17 deletions

View File

@ -29,11 +29,12 @@ httpServer.preprocessor((_req, rep) => {
}); });
httpServer.middleware(async (req, _rep, done) => { 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) { if (!req.resourceRequest) {
console.log( console.log(
`${req.method} - ${req.remoteIpAddr} - ${req.path} - ${ `${req.method} - ${req.remoteIpAddr} - ${req.path} - ${
prettyTime(processTime) prettyTime(hrArray)
}`, }`,
); );
} }

45
mod.ts
View File

@ -5,7 +5,10 @@ import {
import * as path from "https://deno.land/std@0.185.0/path/mod.ts"; 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 * 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 { 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"; import { cryptoRandomString } from "https://deno.land/x/crypto_random_string@1.0.0/mod.ts";
type HTTPServerOptions = { type HTTPServerOptions = {
@ -17,6 +20,10 @@ type HTTPServerOptions = {
sessionExpire?: SessionExpire | number; sessionExpire?: SessionExpire | number;
}; };
type MiddlewareResult = {
processTime: number;
};
export enum SessionExpire { export enum SessionExpire {
NEVER = 2147483647, NEVER = 2147483647,
} }
@ -38,7 +45,7 @@ type RouteHandler = (
type RouteMiddlewareHandler = ( type RouteMiddlewareHandler = (
req: RouteRequest, req: RouteRequest,
rep: RouteReply, rep: RouteReply,
done: () => Promise<number[]>, done: () => Promise<MiddlewareResult>,
) => Promise<void>; ) => Promise<void>;
type RoutePreprocessor = ( type RoutePreprocessor = (
@ -65,7 +72,10 @@ export class HTTPServer {
if (options.sessionSecret) { if (options.sessionSecret) {
if (![16, 24, 32].includes(options.sessionSecret.length)) { if (![16, 24, 32].includes(options.sessionSecret.length)) {
const randomString = cryptoRandomString({ length: 32 }); 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; this.settings = options;
@ -152,11 +162,11 @@ export class HTTPServer {
preProcessor(routeRequest, routeReply) preProcessor(routeRequest, routeReply)
); );
let resolveAction: (value: number[]) => void = () => {}; let resolveAction: (value: MiddlewareResult) => void = () => {};
let middlewarePromise; let middlewarePromise;
const perStart = performance.now(); const perStart = performance.now();
if (this.middlewareHandler) { if (this.middlewareHandler) {
middlewarePromise = (): Promise<number[]> => { middlewarePromise = (): Promise<MiddlewareResult> => {
return new Promise((resolve) => { return new Promise((resolve) => {
resolveAction = resolve; resolveAction = resolve;
}); });
@ -177,8 +187,9 @@ export class HTTPServer {
} catch { } catch {
if (middlewarePromise) { if (middlewarePromise) {
const pt = performance.now() - perStart; const pt = performance.now() - perStart;
const hrArray: number[] = [0, Math.trunc(pt * 1000000)]; resolveAction({
resolveAction(hrArray); processTime: pt,
});
} }
this.processSession(routeRequest, routeReply); this.processSession(routeRequest, routeReply);
this.handleNotFound(routeRequest, routeReply, requestEvent); this.handleNotFound(routeRequest, routeReply, requestEvent);
@ -189,8 +200,9 @@ export class HTTPServer {
const response = new Response(readableStream); const response = new Response(readableStream);
if (middlewarePromise) { if (middlewarePromise) {
const pt = performance.now() - perStart; const pt = performance.now() - perStart;
const hrArray: number[] = [0, Math.trunc(pt * 1000000)]; resolveAction({
resolveAction(hrArray); processTime: pt,
});
} }
this.processSession(routeRequest, routeReply); this.processSession(routeRequest, routeReply);
await requestEvent.respondWith(response); await requestEvent.respondWith(response);
@ -214,8 +226,9 @@ export class HTTPServer {
if (middlewarePromise) { if (middlewarePromise) {
const pt = performance.now() - perStart; const pt = performance.now() - perStart;
const hrArray: number[] = [0, Math.trunc(pt * 1000000)]; resolveAction({
resolveAction(hrArray); processTime: pt,
});
} }
this.processSession(routeRequest, routeReply); this.processSession(routeRequest, routeReply);
await requestEvent.respondWith( await requestEvent.respondWith(
@ -254,8 +267,9 @@ export class HTTPServer {
); );
if (middlewarePromise) { if (middlewarePromise) {
const pt = performance.now() - perStart; const pt = performance.now() - perStart;
const hrArray: number[] = [0, Math.trunc(pt * 1000000)]; resolveAction({
resolveAction(hrArray); processTime: pt,
});
} }
this.processSession(routeRequest, routeReply); this.processSession(routeRequest, routeReply);
@ -270,8 +284,9 @@ export class HTTPServer {
} }
if (middlewarePromise) { if (middlewarePromise) {
const pt = performance.now() - perStart; const pt = performance.now() - perStart;
const hrArray: number[] = [0, Math.trunc(pt * 1000000)]; resolveAction({
resolveAction(hrArray); processTime: pt,
});
} }
this.processSession(routeRequest, routeReply); this.processSession(routeRequest, routeReply);
this.handleNotFound(routeRequest, routeReply, requestEvent); this.handleNotFound(routeRequest, routeReply, requestEvent);