Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions lib/session-file-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove.

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`
*
Expand Down
11 changes: 11 additions & 0 deletions lib/session-file-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`
*
Expand Down
25 changes: 25 additions & 0 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove.

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);
Expand Down