Code coverage report for thinkjs/lib/Lib/Util/Session.js

Statements: 100% (48 / 48)      Branches: 100% (22 / 22)      Functions: 100% (7 / 7)      Lines: 100% (48 / 48)      Ignored: none     

All files » thinkjs/lib/Lib/Util/ » Session.js
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 972           2   7 7 7 7               2   8 8 8               2   5 5     2   2   1 1 1         2   2   28 20   8   8 8 8 5 5 2     8 8 5 5 5 3     5 5   8 8 3 2 2 2     8   8         8   29   8  
var crypto = require('crypto');
/**
 * 生成uid
 * @param  int length
 * @return string
 */
var uid = function(length){
  'use strict';
  var ratio = Math.log(64) / Math.log(256);
  var numbytes = Math.ceil(length * ratio);
  var str = crypto.randomBytes(numbytes).toString('base64').slice(0, length);
  return str.replace(/\+/g, '_').replace(/\//g, '-');
};
/**
 * 生成cookie签名
 * @param  string val
 * @param  string secret
 * @return string
 */
var cookieSign = function(val, secret){
  'use strict';
  secret = crypto.createHmac('sha256', secret).update(val).digest('base64');
  secret = secret.replace(/\=+$/, '');
  return val + '.' + secret;
};
/**
 * 解析cookie签名
 * @param  {[type]} val
 * @param  {[type]} secret
 * @return {[type]}
 */
var cookieUnsign = function(val, secret){
  'use strict';
  var str = val.slice(0, val.lastIndexOf('.'));
  return cookieSign(str, secret) === val ? str : '';
};
 
var Session = module.exports = Cache(function(){
  'use strict';
  return {
    init: function(options){
      this.super_('init', options);
      this.key = this.options.cookie;
      this.updateExpire = true;
    }
  };
});
 
Session.uid = uid;
 
Session.start = function(http){
  'use strict';
  if (http.session) {
    return http.session;
  }
  var name = C('session_name');
  //是否使用签名
  var secret = C('session_sign');
  var cookie = http.cookie[name];
  if (cookie && secret) {
    cookie = cookieUnsign(cookie, secret);
    if (cookie) {
      http.cookie[name] = cookie;
    }
  }
  var session_cookie = cookie;
  if (!cookie) {
    cookie = uid(32);
    session_cookie = cookie;
    if (secret) {
      cookie = cookieSign(cookie, secret);
    }
    //将生成的cookie放在http.cookie对象上,方便程序内读取
    http.cookie[name] = cookie;
    http.setCookie(name, cookie, C('session_options'));
  }
  var type = C('session_type');
  if (!type) {
    if (APP_DEBUG || C('use_cluster')) {
      type = 'File';
      C('session_type', 'File');
      console.log("in debug or cluster mode, session can't use memory for storage, convert to File");
    }
  }
  name = type + 'Session';
  //session类
  var session = http.session = thinkRequire(name)({
    cookie: session_cookie,
    timeout: C('session_timeout')
  });
  //afterend时刷新缓存
  http.on('afterEnd', function(){
    //刷新session
    return session.flush && session.flush();
  })
  return cookie;
};