diff --git a/lib/session-file-helpers.js b/lib/session-file-helpers.js index 67cc5d3..ac16f62 100644 --- a/lib/session-file-helpers.js +++ b/lib/session-file-helpers.js @@ -149,6 +149,56 @@ var helpers = { }); }, + /** + * Attempts to fetch all sessions from path + * + * @param {Object} options + * @param {Function} callback + * + * @api public + */ + getAll: function (options, callback) { + var promises = []; + + fs.readdirSync(options.path).forEach(function(file){ + if (path.extname(file) === options.fileExtension) { + promises.push(new Promise(function(accept, reject) { + const sessionId = path.basename(file).split('.')[0]; + helpers.get(sessionId, options, function (error, session) { + if (error) { + reject(error); + } + accept({ + session: session, + sessionId: sessionId, + }); + }) + })); + } + }); + + Promise.all(promises) + .then(function(result){ + console.dir({result}) + var resultObj = {}; + result.forEach(function(element) { + if (element.session) { + resultObj[element.sessionId] = element.session; + } + }); + callback(null, resultObj); + }) + .catch(function(err) { + if (Array.isArray(err)) { + callback(err.filter(function (error) { + return error !== null + })); + } else { + callback(err); + } + }); + }, + /** * Attempts to fetch session from a session file by the given `sessionId` * diff --git a/lib/session-file-store.js b/lib/session-file-store.js index 4e056e0..5789cf5 100644 --- a/lib/session-file-store.js +++ b/lib/session-file-store.js @@ -61,6 +61,17 @@ module.exports = function (session) { helpers.set(sessionId, session, this.options, callback); }; + /** + * Attempts to fetch all sessions. Sessions are returned in an array + * + * @param {Function} callback + * + * @api public + */ + FileStore.prototype.all = function (callback) { + helpers.getAll(this.options, callback); + }; + /** * Touch the given session object associated with the given `sessionId` * diff --git a/test/helpers.js b/test/helpers.js index c068253..8ea7475 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -369,6 +369,31 @@ describe('helpers', function () { }); }); + describe('#all', function () { + before(function (done) { + fs.emptyDir(SESSIONS_OPTIONS.path, done); + }); + + after(function (done) { + fs.remove(SESSIONS_OPTIONS.path, done); + }); + + it('should return one non-expired session', function (done) { + var session = clone(SESSION); + session.__lastAccess = 0; + helpers.set(SESSION_ID, session, SESSIONS_OPTIONS, function (err, json) { + helpers.getAll(FIXTURE_SESSIONS_OPTIONS, function (err, result) { + console.dir({err}) + expect(err).to.not.exist; + expect(result) + .to.be.ok + .and.has.property(SESSION_ID); + done(); + }); + }); + }); + }); + describe('#touch', function () { before(function (done) { fs.emptyDir(SESSIONS_OPTIONS.path, done);