All files / server/plugins/connect init.js

100% Statements 32/32
72.22% Branches 13/18
100% Functions 3/3
100% Lines 31/31
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 669x   9x   50x 50x 249x     50x     50x 50x 50x   50x 50x     50x 50x 50x       50x 50x     50x 50x 50x 50x     50x 50x 50x           50x 40x 40x     40x 34x                   50x 2x 2x      
const path = require('path');
 
module.exports = ctx => {
 
  const modern = ctx.utils.modern;
  const options = ctx.options.connect;
  const connect = ctx.plugins.filter(p => p.name === 'connect')[0];
 
  // TODO: fix it so this is not needed
  connect.before = [];
 
  // Inherit
  options.public = options.public || ctx.options.public;
  Eif (!path.isAbsolute(options.public)) {
    options.public = path.join(process.cwd(), options.public);
  }
  options.public = path.normalize(options.public);
  ctx.options.public = options.public;
 
  // Compress
  Eif (options.compress) {
    const compress = require('compression')(options.compress);
    connect.before.push(modern(compress));
  }
 
  // Public folder
  Eif (options.public) {
    connect.before.push(modern(ctx.express.static(options.public)));
  }
 
  Eif (options.session) {
    options.session.secret = options.session.secret || ctx.options.secret;
    const session = require('express-session')(options.session);
    connect.before.push(modern(session));
  }
 
  Eif (options.responseTime) {
    const responseTime = require('response-time')(options.responseTime);
    connect.before.push(modern(responseTime));
  }
 
  // // TODO: vhost: require('vhost')
  // // - DO IT WITH A ROUTER
 
  if (options.csrf) {
    const csrf = require('csurf')(options.csrf);
    connect.before.push(modern(csrf));
 
    // Set the csrf for render(): https://expressjs.com/en/api.html#res.locals
    connect.before.push(ctx => {
      ctx.res.locals.csrf = ctx.req.csrfToken();
    });
  }
 
  // ctx => {
  //   if (!ctx.options.middle) return;
  //   ?TODO: serveIndex: (opt, all) => require('serve-index')(all.public)
  // },
 
  // console.log(ctx.options.connect, ctx.options.connect.favicon);
  if (options.favicon) {
    const favicon = require('serve-favicon')(options.favicon);
    connect.before.push(modern(favicon));
  }
}