change middleware done result to own type
This commit is contained in:
parent
b335bb61bd
commit
997fe6dc77
|
@ -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)
|
||||
}`,
|
||||
);
|
||||
}
|
||||
|
|
45
mod.ts
45
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<number[]>,
|
||||
done: () => Promise<MiddlewareResult>,
|
||||
) => Promise<void>;
|
||||
|
||||
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<number[]> => {
|
||||
middlewarePromise = (): Promise<MiddlewareResult> => {
|
||||
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);
|
||||
|
|
Reference in New Issue
Block a user