From 215d7f0324b3616dee3f2332ee958dcfbfd14311 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Veyret?= Date: Mon, 21 May 2018 10:25:13 +0200 Subject: [PATCH 1/2] express-session: add strictNullCheck --- .../express-session/express-session-tests.ts | 20 +++++----- types/express-session/index.d.ts | 38 +++++++++---------- types/express-session/tsconfig.json | 2 +- 3 files changed, 31 insertions(+), 29 deletions(-) diff --git a/types/express-session/express-session-tests.ts b/types/express-session/express-session-tests.ts index 6a619f162c..dc57e88f0d 100644 --- a/types/express-session/express-session-tests.ts +++ b/types/express-session/express-session-tests.ts @@ -11,7 +11,7 @@ app.use(session({ secret: 'keyboard cat', name: 'connect.sid', store: new session.MemoryStore(), - cookie: { path: '/', httpOnly: true, secure: false, maxAge: null }, + cookie: { path: '/', httpOnly: true, secure: false }, genid: (req: express.Request): string => '', rolling: false, resave: true, @@ -30,7 +30,7 @@ app.use((req, res, next) => { sess.views++; res.setHeader('Content-Type', 'text/html'); res.write(`

views: ${sess.views}

`); - res.write(`

expires in: ${(sess.cookie.maxAge / 1000)}s

`); + res.write(`

expires in: ${((sess.cookie.maxAge || 0) / 1000)}s

`); res.end(); } else { sess.views = 1; @@ -41,26 +41,28 @@ app.use((req, res, next) => { // Custom Session Store class MyStore extends session.Store { - private sessions: { [sid: string]: string }; + private sessions: { [sid: string]: string | undefined }; constructor() { super(); this.sessions = {}; } - get = (sid: string, callback: (err: any, session: Express.SessionData) => void): void => { - callback(null, JSON.parse(this.sessions[sid])); + get = (sid: string, callback: (err: any, session?: Express.SessionData | null) => void): void => { + const sessionString: string | undefined = this.sessions[sid]; + const sessionData: Express.SessionData | null = sessionString ? JSON.parse(sessionString) : null; + callback(null, sessionData); } - set = (sid: string, session: Express.Session, callback: (err: any) => void): void => { + set = (sid: string, session: Express.Session, callback?: (err?: any) => void): void => { this.sessions[sid] = JSON.stringify(session); - callback(null); + if (callback) callback(); } - destroy = (sid: string, callback: (err: any) => void): void => { + destroy = (sid: string, callback?: (err?: any) => void): void => { this.sessions[sid] = undefined; this.sessions = JSON.parse(JSON.stringify(this.sessions)); - callback(null); + if (callback) callback(); } } diff --git a/types/express-session/index.d.ts b/types/express-session/index.d.ts index d051f84df0..185753b0b6 100644 --- a/types/express-session/index.d.ts +++ b/types/express-session/index.d.ts @@ -67,35 +67,35 @@ declare namespace session { } interface BaseMemoryStore { - get: (sid: string, callback: (err: any, session: Express.SessionData) => void) => void; - set: (sid: string, session: Express.Session, callback: (err: any) => void) => void; - destroy: (sid: string, callback: (err: any) => void) => void; - length?: (callback: (err: any, length: number) => void) => void; - clear?: (callback: (err: any) => void) => void; + get: (sid: string, callback: (err: any, session?: Express.SessionData | null) => void) => void; + set: (sid: string, session: Express.Session, callback?: (err?: any) => void) => void; + destroy: (sid: string, callback?: (err?: any) => void) => void; + length?: (callback: (err: any, length?: number | null) => void) => void; + clear?: (callback?: (err?: any) => void) => void; } abstract class Store extends node.EventEmitter { constructor(config?: any); - regenerate: (req: express.Request, fn: (err: any) => any) => void; - load: (sid: string, fn: (err: any, session: Express.Session) => any) => void; + regenerate: (req: express.Request, fn: (err?: any) => any) => void; + load: (sid: string, fn: (err: any, session?: Express.Session | null) => any) => void; createSession: (req: express.Request, sess: Express.SessionData) => void; - get: (sid: string, callback: (err: any, session: Express.SessionData) => void) => void; - set: (sid: string, session: Express.Session, callback: (err: any) => void) => void; - destroy: (sid: string, callback: (err: any) => void) => void; - all: (callback: (err: any, obj: { [sid: string]: Express.SessionData; }) => void) => void; - length: (callback: (err: any, length: number) => void) => void; - clear: (callback: (err: any) => void) => void; + get: (sid: string, callback: (err: any, session?: Express.SessionData | null) => void) => void; + set: (sid: string, session: Express.Session, callback?: (err?: any) => void) => void; + destroy: (sid: string, callback?: (err?: any) => void) => void; + all: (callback: (err: any, obj?: { [sid: string]: Express.SessionData; } | null) => void) => void; + length: (callback: (err: any, length?: number | null) => void) => void; + clear: (callback?: (err?: any) => void) => void; } class MemoryStore implements BaseMemoryStore { - get: (sid: string, callback: (err: any, session: Express.SessionData) => void) => void; - set: (sid: string, session: Express.Session, callback: (err: any) => void) => void; - destroy: (sid: string, callback: (err: any) => void) => void; - all: (callback: (err: any, obj: { [sid: string]: Express.Session; }) => void) => void; - length: (callback: (err: any, length: number) => void) => void; - clear: (callback: (err: any) => void) => void; + get: (sid: string, callback: (err: any, session?: Express.SessionData | null) => void) => void; + set: (sid: string, session: Express.Session, callback?: (err?: any) => void) => void; + destroy: (sid: string, callback?: (err?: any) => void) => void; + all: (callback: (err: any, obj?: { [sid: string]: Express.Session; } | null) => void) => void; + length: (callback: (err: any, length?: number | null) => void) => void; + clear: (callback?: (err?: any) => void) => void; } } diff --git a/types/express-session/tsconfig.json b/types/express-session/tsconfig.json index 14c2e75bd5..dad933c3ea 100644 --- a/types/express-session/tsconfig.json +++ b/types/express-session/tsconfig.json @@ -6,7 +6,7 @@ ], "noImplicitAny": true, "noImplicitThis": true, - "strictNullChecks": false, + "strictNullChecks": true, "strictFunctionTypes": true, "baseUrl": "../", "typeRoots": [ From b0ac435e1cbd13911dcfdba3ddfe83261a71f457 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Veyret?= Date: Mon, 21 May 2018 10:30:03 +0200 Subject: [PATCH 2/2] express-session: add touch method definition --- types/express-session/index.d.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/types/express-session/index.d.ts b/types/express-session/index.d.ts index 185753b0b6..29d2b4fbbc 100644 --- a/types/express-session/index.d.ts +++ b/types/express-session/index.d.ts @@ -87,6 +87,7 @@ declare namespace session { all: (callback: (err: any, obj?: { [sid: string]: Express.SessionData; } | null) => void) => void; length: (callback: (err: any, length?: number | null) => void) => void; clear: (callback?: (err?: any) => void) => void; + touch: (sid: string, session: Express.Session, callback?: (err?: any) => void) => void; } class MemoryStore implements BaseMemoryStore { @@ -96,6 +97,7 @@ declare namespace session { all: (callback: (err: any, obj?: { [sid: string]: Express.Session; } | null) => void) => void; length: (callback: (err: any, length?: number | null) => void) => void; clear: (callback?: (err?: any) => void) => void; + touch: (sid: string, session: Express.Session, callback?: (err?: any) => void) => void; } }