All files / server/plugins/session index.js

71.42% Statements 10/14
60% Branches 6/10
75% Functions 3/4
76.92% Lines 10/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 5027x 27x 27x 27x     27x                                                       102x       102x   103x     102x 102x          
const modern = require('../../src/modern');
const session = require('express-session');
const RedisStore = require('connect-redis');
const Redis = require('ioredis');
 
let sessionMiddleware;
module.exports = {
  name: 'session',
  options: {
    __root: 'secret',
    resave: {
      default: false
    },
    saveUninitialized: {
      default: true
    },
    cookie: {
      default: {}
    },
    secret: {
      type: String,
      inherit: 'secret',
      env: 'SESSION_SECRET'
    },
    store: {
      env: false
    },
    redis: {
      type: String,
      inherit: true,
      env: 'REDIS_URL'
    }
  },
  init: ctx => {
    Iif (!ctx.options.session.store && ctx.options.session.redis) {
      const redisClient = new Redis(ctx.options.session.redis);
      ctx.options.session.store = new RedisStore({ client: redisClient });
    }
    sessionMiddleware = session(ctx.options.session);
  },
  before: ctx => modern(sessionMiddleware)(ctx),
  launch: ctx => {
    // Return early if the Socket plugin is not enabled
    Iif (!ctx.io || !ctx.io.use) return;
    ctx.io.use(function (socket, next) {
      sessionMiddleware(socket.request, socket.request.res || {}, next);
    });
  }
};