add sessionExpire option

This commit is contained in:
HorizonCode 2023-05-13 04:58:01 +02:00
parent d519c7bf9c
commit 5392b032d0
2 changed files with 12 additions and 4 deletions

View File

@ -1,6 +1,6 @@
import { Status } from "https://deno.land/std@0.186.0/http/http_status.ts";
import prettyTime from "npm:pretty-time";
import { HTTPServer } from "../mod.ts";
import { HTTPServer, SessionExpire } from "../mod.ts";
const JOKES = [
"Why do Java developers often wear glasses? They can't C#.",
@ -194,4 +194,5 @@ httpServer.listen({
staticLocalDir: "/static",
staticServePath: "/assets",
sessionSecret: "SuperDuperSecret",
sessionExpire: SessionExpire.NEVER
});

13
mod.ts
View File

@ -13,8 +13,13 @@ type HTTPServerOptions = {
staticLocalDir?: string;
staticServePath?: string;
sessionSecret?: string;
sessionExpire?: SessionExpire | number;
};
export enum SessionExpire {
NEVER = 2147483647,
}
export enum HTTPMethod {
GET = "GET",
POST = "POST",
@ -277,7 +282,9 @@ export class HTTPServer {
sessionObject,
this.settings?.sessionSecret,
);
routeReply.cookie("session", encodedSession);
routeReply.cookie("session", encodedSession, {
maxAge: this.settings.sessionExpire ?? undefined,
});
}
}
@ -433,9 +440,9 @@ export class RouteRequest {
sessionCookie,
httpServer.settings.sessionSecret,
);
try{
try {
this.session = JSON.parse(decodedSessionCookie);
}catch(_err){
} catch (_err) {
// Ignore if sessionCookie is not in JSON format
}
}