add done promise for middleware

This commit is contained in:
HorizonCode 2023-05-11 13:11:14 +02:00
parent b974abff1b
commit 10fb61793f
2 changed files with 25 additions and 5 deletions

View File

@ -3,9 +3,13 @@ import { HTTPServer } from "../mod.ts";
const httpServer = new HTTPServer(); const httpServer = new HTTPServer();
httpServer.middleware((req) => { httpServer.middleware(async (req, done) => {
const started = Date.now();
console.log(`${req.method} - ${req.ip()} - ${req.path}`); console.log(`${req.method} - ${req.ip()} - ${req.path}`);
}) await done();
const processTime = Date.now() - started;
console.log(`Processed in ${processTime}ms`);
});
httpServer.error((req, _rep) => { httpServer.error((req, _rep) => {
return JSON.stringify( return JSON.stringify(

22
mod.ts
View File

@ -19,10 +19,14 @@ type RouteHandler = (
| Promise<unknown> | Promise<unknown>
| unknown; | unknown;
type RouteMiddlewareHandler = (req: RouteRequest) => type RouteMiddlewareHandler = (
req: RouteRequest,
done: () => Promise<unknown>,
) =>
| Promise<void> | Promise<void>
| void; | void;
export type RouteParam = {
type RouteParam = {
idx: number; idx: number;
paramKey: string; paramKey: string;
}; };
@ -105,9 +109,16 @@ export class HTTPServer {
this.handleNotFound(routeRequest, routeReply, requestEvent); this.handleNotFound(routeRequest, routeReply, requestEvent);
continue; continue;
} }
let resolveAction: (value?: unknown) => void = () => {};
let middlewarePromise;
if (this.middlewareHandler) { if (this.middlewareHandler) {
this.middlewareHandler(routeRequest); middlewarePromise = (): Promise<unknown> => {
return new Promise((resolve) => {
resolveAction = resolve;
});
};
this.middlewareHandler(routeRequest, middlewarePromise);
} }
if (this.staticServePath && filepath.startsWith(this.staticServePath)) { if (this.staticServePath && filepath.startsWith(this.staticServePath)) {
@ -122,12 +133,14 @@ export class HTTPServer {
file = await Deno.open(pathLoc, { read: true }); file = await Deno.open(pathLoc, { read: true });
} catch { } catch {
this.handleNotFound(routeRequest, routeReply, requestEvent); this.handleNotFound(routeRequest, routeReply, requestEvent);
if (middlewarePromise) resolveAction();
continue; continue;
} }
const readableStream = file.readable; const readableStream = file.readable;
const response = new Response(readableStream); const response = new Response(readableStream);
await requestEvent.respondWith(response); await requestEvent.respondWith(response);
if (middlewarePromise) resolveAction();
return; return;
} }
const routeName = `${requestEvent.request.method}@${filepath}`; const routeName = `${requestEvent.request.method}@${filepath}`;
@ -152,6 +165,7 @@ export class HTTPServer {
statusText: STATUS_TEXT[routeReply.statusCode], statusText: STATUS_TEXT[routeReply.statusCode],
}), }),
); );
if (middlewarePromise) resolveAction();
continue; continue;
} }
@ -183,9 +197,11 @@ export class HTTPServer {
statusText: STATUS_TEXT[routeReply.statusCode], statusText: STATUS_TEXT[routeReply.statusCode],
}), }),
); );
if (middlewarePromise) resolveAction();
continue; continue;
} }
this.handleNotFound(routeRequest, routeReply, requestEvent); this.handleNotFound(routeRequest, routeReply, requestEvent);
if (middlewarePromise) resolveAction();
} }
} }