Merge pull request #25900 from sveyret/express-session

express-session: add strictNullCheck
This commit is contained in:
Daniel Rosenwasser
2018-05-27 00:51:51 -07:00
committed by GitHub
3 changed files with 33 additions and 29 deletions

View File

@@ -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(`<p>views: ${sess.views}</p>`);
res.write(`<p>expires in: ${(sess.cookie.maxAge / 1000)}s</p>`);
res.write(`<p>expires in: ${((sess.cookie.maxAge || 0) / 1000)}s</p>`);
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();
}
}

View File

@@ -67,35 +67,37 @@ 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;
touch: (sid: string, session: Express.Session, 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;
touch: (sid: string, session: Express.Session, callback?: (err?: any) => void) => void;
}
}

View File

@@ -6,7 +6,7 @@
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": false,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [