yoho-session.js 2.58 KB
const config = global.yoho.config;
const memcachedSession = require('yoho-express-session');
const _ = require('lodash');
const uuid = require('uuid');

const cookieSession = require('client-sessions');
const memcached = require('connect-memcached');
const MemcachedStore = memcached(memcachedSession);

/**
 * 该中间件主要把 express-session 和 client-session 集中起来处理,如果 memcached 出错了,使用 cookie session
 * @param opts.backSession cookieSession 的键名
 * @returns {function(*=, *=, *)}
 */
function yohoSession(opts) {
    return (req, res, next) => {
        let notUseMemcached = _.get(req.app.locals.wap, 'session.removeMemcached', false);

        opts.backSession = opts.backSession || 'session2';

        if (req.session && !notUseMemcached) {
            req.sessionError = false;
        } else {
            // 重建 session
            res.emit('sessionError');
            req.sessionError = true;

            req.sessionID = uuid.v4();
            req.session = new memcachedSession.Session(req, req[opts.backSession].sessionBack);
            req.session.cookie = new memcachedSession.Cookie({
                domain: 'yohobuy.com',
                httpOnly: false
            });
        }

        Object.defineProperty(req.session, 'reset', {
            configurable: true,
            enumerable: false,
            value: function() {
                req.session.destroy();
                req[opts.backSession].reset();
            },
            writable: false
        });

        // 备份数据
        req[opts.backSession].sessionBack = req.session;

        next();
    };
}

module.exports = (app) => {
    app.use(memcachedSession({  // eslint-disable-line
        proxy: true,
        resave: false,
        saveUninitialized: true,
        unset: 'destroy',
        secret: '82dd7e724f2c6870472c89dfa43cf48d',
        name: 'yohobuy_session',
        genid() {
            return uuid.v4();
        },
        cookie: {
            domain: 'yohobuy.com',
            httpOnly: false
        },
        store: new MemcachedStore({
            hosts: config.memcache.session,
            prefix: 'yohobuy_session:',
            reconnect: 5000,
            timeout: 1000,
            retries: 0
        })
    }));

    app.use(cookieSession({ // eslint-disable-line
        requestKey: 'session2',
        cookieName: 'yohobuy_session_cookie',
        secret: '82dd7e724f2c6870472c89dfa43cf48d',
        cookie: {
            domain: 'yohobuy.com',
            ephemeral: true
        }
    }));

    app.use(yohoSession({
        backSession: 'session2'
    }));
};