Skip to content

Commit 2277f6f

Browse files
committed
fix(pencil): Correct session extraction
1 parent fe45da5 commit 2277f6f

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

src/interceptors/http-server-interceptor.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export default class HttpServerInterceptor extends Interceptor implements IInter
4242
return true;
4343
}
4444
const snuid = v4();
45-
Logger.debug(`Url is: ${req.url}, method: ${req.method}`);
45+
Logger.debug(`Intercept request: ${req.url}, method: ${req.method}`);
4646
SessionManager.setSession(snuid, { req, res });
4747
const url = req.url;
4848
const clientIp = clientIpFromRequest(req);
@@ -98,7 +98,6 @@ export default class HttpServerInterceptor extends Interceptor implements IInter
9898
const risk = this.apiManager.risk.bind(this.apiManager);
9999
return function () {
100100
if (this && this.sn_finished) {
101-
Logger.debug(new Error()?.stack);
102101
SessionManager.cleanSession(this.req && this.req.sn_uid);
103102
return;
104103
}
@@ -109,7 +108,6 @@ export default class HttpServerInterceptor extends Interceptor implements IInter
109108
if (req && res) {
110109
risk({ event: EventType.RISK, context: { req: contextFromRequest(req), res: contextFromResponse(res) } });
111110
}
112-
Logger.debug(new Error().stack);
113111
SessionManager.cleanSession(this.req.sn_uid);
114112
}
115113
return original.apply(this, arguments);

src/session-manager.spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@ chai.use(chaiAsPromised);
66
const expect = chai.expect;
77

88
describe('SessionManager', () => {
9+
beforeEach(() => {
10+
SessionManager.cleanAllSessions();
11+
});
12+
913
it('Should have all public methods defined', () => {
1014
expect(SessionManager).to.have.property('getLastSession');
1115
expect(SessionManager).to.have.property('getSession');
1216
expect(SessionManager).to.have.property('setSession');
1317
expect(SessionManager).to.have.property('cleanSession');
18+
expect(SessionManager).to.have.property('cleanAllSessions');
1419
});
1520

1621
it('Should set and get sesssion', () => {
@@ -40,7 +45,7 @@ describe('SessionManager', () => {
4045

4146
it('Should set multiple sessions and get latest session', () => {
4247
const sessions = ['1111', '2222', '3333', '4444'];
43-
const [last] = sessions.slice(-1);
48+
const [last] = sessions;
4449

4550
sessions.forEach((id) => {
4651
SessionManager.setSession(id, { req: {}, res: {} });

src/session-manager.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,36 @@ export interface Session {
66
}
77

88
export default class SessionManager {
9-
private static lastSessionId = '';
10-
private static session: Map<string, Session> = new Map<string, Session>();
9+
private static stack: Array<Session> = [];
1110

1211
static getLastSession(): Session {
13-
const session = SessionManager.session.get(SessionManager.lastSessionId) || { req: null, res: null };
14-
Logger.debug(`[SessionManager] Getting last session by: ${SessionManager.lastSessionId}, is: ${session.req?.sn_uid}`);
12+
const [session = { req: null, res: null }] = SessionManager.stack;
1513
return session;
1614
}
1715

1816
static getSession(id: string): Session {
19-
return SessionManager.session.get(id) || { req: null, res: null };
17+
return SessionManager.stack.find((s) => s.req.sn_uid === id) || { req: null, res: null };
2018
}
2119

2220
static setSession(id: string, session: Session) {
2321
Logger.debug(`[SessionManager] Setting session: ${id}`);
2422
session.req.sn_uid = id;
2523
session.res.sn_uid = id;
26-
SessionManager.session.set(id, session);
27-
28-
//save last session
29-
SessionManager.lastSessionId = id;
24+
//add session
25+
SessionManager.stack.push(session);
3026
}
3127

3228
static cleanSession(id: string) {
3329
Logger.debug(`[SessionManager] Cleaning session: ${id}`);
34-
SessionManager.session.delete(id);
30+
// delete item
31+
const inx = SessionManager.stack.findIndex((s) => s.req.sn_uid === id);
32+
if (inx !== -1) {
33+
SessionManager.stack.splice(inx, 1);
34+
}
35+
}
36+
37+
static cleanAllSessions() {
38+
Logger.debug(`[SessionManager] Cleaning all sessions`);
39+
SessionManager.stack = [];
3540
}
3641
}

0 commit comments

Comments
 (0)