Skip to content

Instantly share code, notes, and snippets.

@suyash
Last active May 12, 2020 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suyash/7dc21cf7465274a4b97e9c9f51e46583 to your computer and use it in GitHub Desktop.
Save suyash/7dc21cf7465274a4b97e9c9f51e46583 to your computer and use it in GitHub Desktop.
MemoryStore + JFSStore
import { Store } from 'express-session';
import JFS from 'jfs';
export default class JFSStore extends Store {
constructor(location) {
super();
this.db = new JFS(location || 'data');
}
/**
* Get all active sessions.
*
* @param {function} callback
* @public
*/
all(callback) {
this.db.all(callback);
}
/**
* Clear all sessions.
*
* @param {function} callback
* @public
*/
clear(callback) {
const sessions = this.db.allSync();
// eslint-disable-next-line no-restricted-syntax
for (const sessionId of Object.keys(sessions)) {
this.db.deleteSync(sessionId);
}
if (callback) {
setImmediate(callback);
}
}
/**
* Destroy the session associated with the given session ID.
*
* @param {string} sessionId
* @public
*/
destroy(sessionId, callback) {
this.db.delete(sessionId, callback);
}
/**
* Fetch session by the given session ID.
*
* @param {string} sessionId
* @param {function} callback
* @public
*/
get(sessionId, callback) {
setImmediate(callback, null, this.getSession(sessionId));
}
/**
* Commit the given session associated with the given sessionId to the store.
*
* @param {string} sessionId
* @param {object} session
* @param {function} callback
* @public
*/
set(sessionId, session, callback) {
this.db.save(sessionId, session, callback);
}
/**
* Get number of active sessions.
*
* @param {function} callback
* @public
*/
length(callback) {
this.all((err, sessions) => {
if (err) {
return callback(err);
}
return callback(null, Object.keys(sessions).length);
});
}
/**
* Touch the given session object associated with the given session ID.
*
* @param {string} sessionId
* @param {object} session
* @param {function} callback
* @public
*/
touch(sessionId, session, callback) {
const currentSession = this.getSession(sessionId);
if (currentSession) {
// update expiration
currentSession.cookie = session.cookie;
this.db.save(sessionId, currentSession, callback);
}
}
/**
* Get session from the store.
* @private
*/
getSession(sessionId) {
const sess = this.db.getSync(sessionId);
if (!sess || sess instanceof Error) {
return null;
}
const expires = typeof sess.cookie.expires === 'string'
? new Date(sess.cookie.expires)
: sess.cookie.expires;
// destroy expired session
if (expires && expires <= Date.now()) {
this.db.deleteSync(sessionId);
return null;
}
return sess;
}
}
import { Store } from 'express-session';
export default class MemoryStore extends Store {
constructor() {
super();
this.sessions = Object.create(null);
}
/**
* Get all active sessions.
*
* @param {function} callback
* @public
*/
all(callback) {
const sessionIds = Object.keys(this.sessions);
const sessions = Object.create(null);
// eslint-disable-next-line
for (const sessionId of sessionIds) {
const session = this.getSession(sessionId);
if (session) {
sessions[sessionId] = session;
}
}
if (callback) {
setImmediate(callback, null, sessions);
}
}
/**
* Clear all sessions.
*
* @param {function} callback
* @public
*/
clear(callback) {
this.sessions = Object.create(null);
if (callback) {
setImmediate(callback);
}
}
/**
* Destroy the session associated with the given session ID.
*
* @param {string} sessionId
* @public
*/
destroy(sessionId, callback) {
delete this.sessions[sessionId];
if (callback) {
setImmediate(callback);
}
}
/**
* Fetch session by the given session ID.
*
* @param {string} sessionId
* @param {function} callback
* @public
*/
get(sessionId, callback) {
setImmediate(callback, null, this.getSession(sessionId));
}
/**
* Commit the given session associated with the given sessionId to the store.
*
* @param {string} sessionId
* @param {object} session
* @param {function} callback
* @public
*/
set(sessionId, session, callback) {
this.sessions[sessionId] = JSON.stringify(session);
if (callback) {
setImmediate(callback);
}
}
/**
* Get number of active sessions.
*
* @param {function} callback
* @public
*/
length(callback) {
this.all((err, sessions) => {
if (err) {
return callback(err);
}
return callback(null, Object.keys(sessions).length);
});
}
/**
* Touch the given session object associated with the given session ID.
*
* @param {string} sessionId
* @param {object} session
* @param {function} callback
* @public
*/
touch(sessionId, session, callback) {
const currentSession = this.getSession(sessionId);
if (currentSession) {
// update expiration
currentSession.cookie = session.cookie;
this.sessions[sessionId] = JSON.stringify(currentSession);
}
if (callback) {
setImmediate(callback);
}
}
/**
* Get session from the store.
* @private
*/
getSession(sessionId) {
let sess = this.sessions[sessionId];
if (!sess) {
return null;
}
// parse
sess = JSON.parse(sess);
const expires = typeof sess.cookie.expires === 'string'
? new Date(sess.cookie.expires)
: sess.cookie.expires;
// destroy expired session
if (expires && expires <= Date.now()) {
delete this.sessions[sessionId];
return null;
}
return sess;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment