swagger-express-middleware icon indicating copy to clipboard operation
swagger-express-middleware copied to clipboard

Session sensitive data store?

Open alex-dow opened this issue 8 years ago • 5 comments

So here is my problem, I am using this tool as part of my selenium testing. For each browser, I would like a separate data store so that actions performed by one browser do not affect the other.

I did not see anything in the documentation to support such a use case. If it's not supported out of the box, then I'd like to wonder out loud how it can be done.

Perhaps it's possible to assign a unique cookie, have all keys in the data store be prefixed by the value of that cookie (transparently)?

alex-dow avatar May 18 '16 14:05 alex-dow

Good idea!

I had never thought of that use-case before, but it shouldn't be too hard to create a session-specific data store class. I'll add it to the to-do list.

In the meantime though, you can probably whip-up something that meets your needs pretty easily by inheriting from the DataStore abstract class and implementing something similar to the MemoryStore class, but with a bit of extra logic to separate the data by session.

JamesMessinger avatar May 18 '16 15:05 JamesMessinger

well, browsing through the source, I spotted this:


      // Get the current DataStore (this can be changed at any time by third-party code)
      var db = util.isExpressApp(router) ? router.get('mock data store') || dataStore : dataStore;

I'm not sure this feature is documented anywhere, so I hope it's an intentional feature because it's allowed me to do this:

    app.use(function(req, res, next) {
        var cookie = req.cookies.__express_session_cookie;
        if (cookie == undefined) {
            console.log('CREATING NEW SESSION');
            var randomNumber = Math.random().toString();
            res.cookie('__express_session_cookie', randomNumber);
            console.log('creating new data store for SESSIONID:' + randomNumber);

            sessionDataStores[randomNumber] = prepareData();
            app.set('mock data store', sessionDataStores[randomNumber]);
        } else {
            console.log('USING EXISTING SESSION');
            if (!sessionDataStores[cookie]) {
                // eg: you restart the server but use the same browser which kept
                // the session cookie
                sessionDataStores[cookie] = prepareData();
            }   
            app.set('mock data store', sessionDataStores[cookie]);
        }   

        next();
    });

very very simple multi-session data storage. prepareData() is just returning a MemoryDataStore instance.

This is definitely better than messing around with key names.

alex-dow avatar May 19 '16 01:05 alex-dow

here is something cleaner:

function createSessionStore(req, res, next) {
    var cookie = req.cookies.__express_session_cookie;

    if (cookie == undefined) {
        var randomNumber = Math.random().toString();
        res.cookie('__express_session_cookie', randomNumber);

        sessionDataStores[randomNumber] = prepareDataStore();
        app.set('mock data store', sessionDataStores[randomNumber]);
    } else {
        if (!sessionDataStores[randomNumber]) {
            sessionDataStores[cookie] = prepareDataStore();
        }   
    }   
    next();
}

Then you can use it like so:

app.use(cookieParser());
app.use(createSessionStore);

alex-dow avatar May 19 '16 02:05 alex-dow

and my prepareDataStore() looks like:

function prepareDataStore() {
    var myDB = new swagger.MemoryDataStore();
    myDB.save(require('./dataEndpoint'));
    return myDB;
}

and dataEndpoint.js would look like:

var swagger = require('swagger-express-middleware');

module.exports = new swagger.Resource('/endpoint', {
    someKey: 'someValue'
});

alex-dow avatar May 19 '16 02:05 alex-dow

Actually my little function had a bug.

function createSessionStore(req, res, next) {
    var cookie = req.cookies.__express_session_cookie;

    if (cookie == undefined) {
        var randomNumber = Math.random().toString();
        res.cookie('__express_session_cookie', randomNumber);

        sessionDataStores[randomNumber] = prepareDataStore();
        app.set('mock data store', sessionDataStores[randomNumber]);
    } else {
        if (!sessionDataStores[cookie]) {
            sessionDataStores[cookie] = prepareDataStore();
        }
        app.set('mock data store', sessionDataStores[cookie]);
    }
    next();
}

This should be much better. I'll see if I can turn this into its own npm installable module (and of course, have some tests).

But I would need confirmation that the approach I'm taking is leveraging an intentional feature that will continue to exist.

alex-dow avatar May 19 '16 16:05 alex-dow