Skip to content

Commit

Permalink
Merge pull request #35 from ujjwalguptaofficial/ug-feat-session-store
Browse files Browse the repository at this point in the history
Expose session store insteadof session provider
  • Loading branch information
ujjwalguptaofficial authored Nov 25, 2023
2 parents 33f7c21 + 546b092 commit 8bdeb90
Show file tree
Hide file tree
Showing 19 changed files with 148 additions and 138 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.

16 changes: 7 additions & 9 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 { MustacheViewEngine, MemorySessionProvider, DtoValidator } from "../extra";
import { ViewEngine, XmlParser, ComponentOption } from "../abstracts";
import { EtagOption, FolderMap, TSessionStore } from "../types";
import { GenericGuard, GenericShield, GenericWall, GenericXmlParser } from "../generics";
import { MustacheViewEngine, 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,7 @@ export class FortGlobal {
viewPath;
shouldParseCookie = true;
shouldParseBody = true;
sessionProvider: typeof GenericSessionProvider;
sessionStore: TSessionStore;
sessionTimeOut = 60;
viewEngine: ViewEngine;
walls: Array<typeof GenericWall> = [];
Expand Down Expand Up @@ -58,9 +58,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
5 changes: 1 addition & 4 deletions src/extra/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
export * from './mustache_view_engine';
export * from "./memory_session_provider";
export * from "./dto_validator";
// export * from "./expect_body_guard";
// export * from "./expect_query_shield";
export * from "./dto_validator";
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 { SessionManager } from "../utils";

export interface IComponentProp {
request: http.IncomingMessage;
response: http.ServerResponse;
query: { [key: string]: any };
body?: { [key: string]: any };
session: SessionProvider;
session: SessionManager;
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 { SessionManager } from "../utils";

export interface IController {
request: HttpRequest;
response: HttpResponse;
query: { [key: string]: string };
body?: { [key: string]: any };
session: SessionProvider;
session: SessionManager;
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
8 changes: 4 additions & 4 deletions src/providers/cookie_wall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { COOKIE, FORT_GLOBAL } from "../constants";
import { parseCookie } from "../helpers";
import { IHttpResult } from "../interfaces";
import { CookieManager } from "../models";
import { SessionManager } from "../utils";

export class CookieEvaluatorWall extends Wall {
parseCookieFromRequest() {
Expand All @@ -14,11 +15,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 SessionManager(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";
Original file line number Diff line number Diff line change
@@ -1,41 +1,52 @@
import { SessionProvider } from "../abstracts/session_provider";
import { promiseResolve } from "../utils";
import { ISessonStore } from "../interfaces";

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

export class MemorySessionProvider extends SessionProvider {
export class MemorySessionStore implements ISessonStore {
sessionId: string;

constructor(sessionId: string) {
this.sessionId = sessionId;
}

private getSessionValue_() {
return sessionValues.get(this.sessionId);
}

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

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

getAll() {
async getAll(): Promise<{ [key: string]: any; }> {
const savedValue = this.getSessionValue_();
return promiseResolve(savedValue || {});
return savedValue || {};
}

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

set(key: string, val: any) {
async clear(): Promise<void> {
// remove session values
sessionValues.delete(this.sessionId);
}

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

setMany(values: { [key: string]: any }) {
Expand All @@ -46,19 +57,10 @@ export class MemorySessionProvider extends SessionProvider {
);
}

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

clear() {
// remove session values
sessionValues.delete(this.sessionId);
// expire cookie in browser
this.destroySession();
return promiseResolve<void>(null);
}
}
8 changes: 5 additions & 3 deletions src/test_helpers/init_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ import { ControllerTestData } from "../types";
import { HttpResponseStub } from "./http_response_stub";
import { HttpRequestStub } from "./http_request_stub";
import { Controller } from "../abstracts";
import { SessionManager } from "../utils";



export const initController = (controllerInstance: Controller, data?: ControllerTestData) => {
data = data || {};
const parsedCookies = data.cookieValue || {};
const headers = (data.request && data.request.headers) || {};
const session = new FORT_GLOBAL.sessionProvider();
const cookie = new CookieManager(parsedCookies);
session.cookie = cookie;
session.sessionId = parsedCookies[FORT_GLOBAL.appSessionIdentifier];
const session = new SessionManager(
cookie,
FORT_GLOBAL.sessionStore
);
controllerInstance['componentProp_'] = {
request: new HttpRequestStub(headers) as any,
response: new HttpResponseStub(headers) as any,
Expand Down
4 changes: 3 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IHttpResult } from '../interfaces';
import { IHttpResult, ISessonStore } from '../interfaces';

export * from './view_engine_data';
export * from './http_request';
Expand All @@ -16,3 +16,5 @@ export * from './view_read_option';
export * from './http_format_result';

export type ErrorResultMapper = (error: any) => IHttpResult;
type Class<I, Args extends any[] = any[]> = new (...args: Args) => I;
export type TSessionStore = Class<ISessonStore, [string]>;
3 changes: 2 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export * from './is_null_or_empty';
export * from './is_null';
export * from './is_array';
export * from './promise_resolve';
export * from './compare_string';
export * from './compare_string';
export * from './session_manager';
Loading

0 comments on commit 8bdeb90

Please sign in to comment.