Skip to content

Commit

Permalink
create sessionstore and use it in sessionprovider
Browse files Browse the repository at this point in the history
  • Loading branch information
ujjwalguptaofficial committed Nov 25, 2023
1 parent 33f7c21 commit afb6b70
Show file tree
Hide file tree
Showing 17 changed files with 155 additions and 143 deletions.
1 change: 0 additions & 1 deletion src/abstracts/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export * from './controller';
export * from './shield';
export * from './session_provider';
export * from './guard';
export * from './view_engine';
export * from './wall';
Expand Down
39 changes: 0 additions & 39 deletions src/abstracts/session_provider.ts

This file was deleted.

15 changes: 7 additions & 8 deletions src/constants/fort_global.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { ErrorHandler, Logger } from "../models";
import { ViewEngine, XmlParser, ComponentOption, Shield } from "../abstracts";
import { EtagOption, FolderMap } from "../types";
import { GenericGuard, GenericSessionProvider, GenericShield, GenericWall, GenericXmlParser } from "../generics";
import { ViewEngine, XmlParser, ComponentOption } from "../abstracts";
import { EtagOption, FolderMap, TSessionStore } from "../types";
import { GenericGuard, GenericShield, GenericWall, GenericXmlParser } from "../generics";
import { MustacheViewEngine, MemorySessionProvider, DtoValidator } from "../extra";
import { APP_NAME, CURRENT_PATH } from "./index";
import * as path from "path";
import { ETAG_TYPE } from "../enums";
import { IDtoValidator } from "../interfaces";
import { CookieEvaluatorWall, PostDataEvaluatorGuard } from "../providers";
import { CookieEvaluatorWall, MemorySessionStore, PostDataEvaluatorGuard } from "../providers";

const isDevelopment = process.env.NODE_ENV === 'development';
const isProduction = process.env.NODE_ENV === "production";
Expand All @@ -17,7 +17,8 @@ export class FortGlobal {
viewPath;
shouldParseCookie = true;
shouldParseBody = true;
sessionProvider: typeof GenericSessionProvider;
sessionStore: TSessionStore;
sessionProvider = MemorySessionProvider;
sessionTimeOut = 60;
viewEngine: ViewEngine;
walls: Array<typeof GenericWall> = [];
Expand Down Expand Up @@ -58,9 +59,7 @@ export class FortGlobal {
this.logger = this.logger || new Logger();
}

if (this.sessionProvider == null) {
this.sessionProvider = MemorySessionProvider as any;
}
this.sessionStore = this.sessionStore || MemorySessionStore;

if (this.xmlParser == null) {
this.xmlParser = GenericXmlParser;
Expand Down
81 changes: 46 additions & 35 deletions src/extra/memory_session_provider.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,75 @@
import { SessionProvider } from "../abstracts/session_provider";
import { promiseResolve } from "../utils";
import { FORT_GLOBAL } from "../constants";
import { ISessonStore } from "../interfaces";
import { CookieManager } from "../models";
import * as getUniqId from "uniqid";
import { TSessionStore } from "../types";

const sessionValues: Map<string, { [key: string]: any }> = new Map();

export class MemorySessionProvider extends SessionProvider {
export class MemorySessionProvider {

private getSessionValue_() {
return sessionValues.get(this.sessionId);
sessionId: string;
protected cookie: CookieManager;
sessionStore: ISessonStore;

constructor(cookie: CookieManager, sessionStore: TSessionStore) {
this.sessionId = cookie.cookieCollection[FORT_GLOBAL.appSessionIdentifier];
this.sessionStore = new sessionStore(this.sessionId);
this.cookie = cookie;
}

protected createSession(sessionId?) {
const now = new Date();
this.sessionId = sessionId != null ? sessionId : getUniqId();
this.cookie.addCookie({
name: FORT_GLOBAL.appSessionIdentifier,
value: this.sessionId,
httpOnly: true,
path: "/",
expires: new Date(now.setMinutes(now.getMinutes() + FORT_GLOBAL.sessionTimeOut)),
maxAge: FORT_GLOBAL.sessionTimeOut * 60
});
}

protected destroySession() {
const cookie = this.cookie.getCookie(FORT_GLOBAL.appSessionIdentifier);
cookie.httpOnly = true;
cookie.path = "/";
this.cookie.removeCookie(cookie);
}

get(key: string) {
const savedValue = this.getSessionValue_()
return promiseResolve(savedValue != null ? savedValue[key] : null);
return this.sessionStore.get(key);
}

isExist(key: string) {
const savedValue = this.getSessionValue_()
return promiseResolve<boolean>(savedValue == null ? false : savedValue[key] != null);
return this.sessionStore.isExist(key);
}

getAll() {
const savedValue = this.getSessionValue_();
return promiseResolve(savedValue || {});
return this.sessionStore.getAll();
}

set(key: string, val: any) {
const savedValue = this.getSessionValue_();
if (savedValue == null) {
async set(key: string, val: any) {
const savedValue = await this.sessionStore.isAnyExist();
if (savedValue === false) {
this.createSession();
sessionValues.set(this.sessionId, {
[key]: val
});
}
else {
savedValue[key] = val;
this.sessionStore.sessionId = this.sessionId;
}
return promiseResolve<void>(null);
await this.sessionStore.set(key, val);
}

setMany(values: { [key: string]: any }) {
return Promise.all(
Object.keys(values).map((key) => {
return this.set(key, values[key]);
})
);
return this.sessionStore.setMany(values);
}

remove(key: string) {
const savedValue = this.getSessionValue_();
if (savedValue != null) {
savedValue[key] = null;
}
return promiseResolve<void>(null);
return this.sessionStore.remove(key);
}

clear() {
async clear() {
// remove session values
sessionValues.delete(this.sessionId);
await this.sessionStore.clear();
// expire cookie in browser
this.destroySession();
return promiseResolve<void>(null);
}
}
38 changes: 0 additions & 38 deletions src/generics/generic_session_provider.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/generics/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export * from './generic_guard';
export * from './generic_shield';
export * from './generic_session_provider';
export * from "./generic_wall";
export * from "./generic_controller";
export * from './generic_xml_parser';
4 changes: 2 additions & 2 deletions src/interfaces/component_prop.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import * as http from "http";
import { SessionProvider } from "../abstracts";
import { CookieManager, FileManager } from "../models";
import { FortGlobal } from "../constants";
import { MemorySessionProvider } from "../extra";

export interface IComponentProp {
request: http.IncomingMessage;
response: http.ServerResponse;
query: { [key: string]: any };
body?: { [key: string]: any };
session: SessionProvider;
session: MemorySessionProvider;
cookie: CookieManager;
param?: { [key: string]: string };
data: { [key: string]: any };
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/controller.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { HttpRequest, HttpResponse } from "../types";
import { CookieManager, FileManager } from "../models";
import { SessionProvider } from "../abstracts";
import { MemorySessionProvider } from "../extra";

export interface IController {
request: HttpRequest;
response: HttpResponse;
query: { [key: string]: string };
body?: { [key: string]: any };
session: SessionProvider;
session: MemorySessionProvider;
cookie: CookieManager;
param?: { [key: string]: string };
data: { [key: string]: any };
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export * from './worker_info';
export * from './component_prop';
export * from './validator';
export * from './http_result';
export * from './session_store';

11 changes: 11 additions & 0 deletions src/interfaces/session_store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export interface ISessonStore {
sessionId: string;
get(key: string): Promise<any>;
isAnyExist(): Promise<boolean>;
isExist(key: string): Promise<boolean>;
getAll(): Promise<{ [key: string]: any }>;
set(key: string, val: any): Promise<void>;
setMany(values: { [key: string]: any }): Promise<void[]>;
remove(key: string): Promise<void>;
clear(): Promise<void>;
}
12 changes: 6 additions & 6 deletions src/models/fort.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { ParentRoute, EtagOption, FolderMap } from "../types";
import { Wall, ViewEngine, SessionProvider, XmlParser, ResultMapper, Shield, Guard } from "../abstracts";
import { ParentRoute, EtagOption, FolderMap, TSessionStore } from "../types";
import { Wall, ViewEngine, XmlParser, ResultMapper, Shield, Guard } from "../abstracts";
import { RouteHandler, RequestHandler } from "../handlers";
import { FORT_GLOBAL } from "../constants/fort_global";
import { ErrorHandler } from ".";
import * as http from "http";
import { ERROR_TYPE } from "../enums";
import { LogHelper, promise, removeLastSlash, removeFirstSlash, setResultMapper } from "../helpers";
import { GenericSessionProvider, GenericController } from "../generics";
import { GenericController } from "../generics";
import { isArray } from "../utils";
import { Logger } from "./logger";
import { ComponentOption } from "../abstracts/component_option";
Expand Down Expand Up @@ -99,13 +99,13 @@ export class Fort {
}

/**
* sessionProvider class, default - MemorySessionProvider
* sessionStore class, default - MemorySessionStore
*
* @static
* @memberof Fort
*/
static set sessionProvider(value: typeof SessionProvider) {
FORT_GLOBAL.sessionProvider = value as typeof GenericSessionProvider;
static set sessionStore(value: TSessionStore) {
FORT_GLOBAL.sessionStore = value;
}

static set resultMapper(value: typeof ResultMapper) {
Expand Down
7 changes: 3 additions & 4 deletions src/providers/cookie_wall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ export class CookieEvaluatorWall extends Wall {
const request = this.request;
const rawCookie = (request.headers[COOKIE] || request.headers["cookie"]) as string;
const parsedCookies = parseCookie(rawCookie);
const session = new FORT_GLOBAL.sessionProvider();
session.cookie = new CookieManager(parsedCookies);
session.sessionId = parsedCookies[FORT_GLOBAL.appSessionIdentifier];
const cookie = new CookieManager(parsedCookies);
const session = new FORT_GLOBAL.sessionProvider(cookie, FORT_GLOBAL.sessionStore);
componentProps.session = session;
componentProps.cookie = session.cookie;
componentProps.cookie = cookie;
}

async onIncoming(): Promise<void | IHttpResult> {
Expand Down
3 changes: 2 additions & 1 deletion src/providers/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./cookie_wall";
export * from "./post_data_evaluator_guard";
export * from "./post_data_evaluator_guard";
export * from "./memory_session_store";
Loading

0 comments on commit afb6b70

Please sign in to comment.