<commit_before>module.exports = RedSess

var redis = require("redis")
RedSess.client = null

var Cookies = require('cookies')

// optional dependency
try { var KeyGrip = require('keygrip') } catch (e) {}

function RedSess (req, res, opt) {
  opt = opt || {}
  if (!RedSess.client && !opt.client) {
    console.error('RedSess: no client yet', req.url, req.headers)
    res.statusCode = 503
    res.setHeader('content-type', 'text/plain')
    res.setHeader('retry-after', '10')
    res.end('Redis client not yet loaded.\n')
    return
  }

  // if we got an array of strings rather than a keygrip, then
  // keygrip it up.
  if (opt.keys) {
    if (!KeyGrip)
      throw new Error('keys provided by KeyGrip not available')
    else if (Array.isArray(opt.keys))
      this.keys = new KeyGrip(opt.keys)
    else if (opt.keys instanceof KeyGrip)
      this.keys = opt.keys
    else
      throw new Error('invalid keys provided')
  }

  // set up the cookies thingie
  this.cookies = opt.cookies || new Cookies(req, res, this.keys)

  // set the s-cookie
  var name = opt && opt.cookieName || 's'
  var copt = { signed: !!this.keys }

  var s = this.cookies.get(name, copt)
  if (!s)
    s = require('crypto').randomBytes(30).toString('base64')

  this.cookies.set(name, s, copt)
  this.token = s

  if (!this.token)
    throw new Error('could not load session token')

  this.id = "session:" + this.token
  this.client = opt.client || RedSess.client
  this.request = req
  this.response = res

  // 2 week sessions by default.
  this.expire = opt.expire || 60 * 60 * 24 * 14
}

RedSess.createClient = function (conf) {
  conf = conf || {}
  RedSess.client = redis.createClient(conf.port, conf.host, conf)
  if (conf.auth)
    RedSess.client.auth(conf.auth)
  return RedSess.client
}

RedSess.quit = RedSess.close = RedSess.end = function (cb) {
  if (!RedSess.client)
    return cb && cb()
  if (cb)
    RedSess.client.once("end", cb)
  RedSess.client.quit()
}

RedSess.destroy = function () {
  RedSess.client.end()
}


<commit_msg>Set expire time to the cookie as well<commit_after>module.exports = RedSess

var redis = require("redis")
RedSess.client = null

var Cookies = require('cookies')

// optional dependency
try { var KeyGrip = require('keygrip') } catch (e) {}

function RedSess (req, res, opt) {
  opt = opt || {}
  if (!RedSess.client && !opt.client) {
    console.error('RedSess: no client yet', req.url, req.headers)
    res.statusCode = 503
    res.setHeader('content-type', 'text/plain')
    res.setHeader('retry-after', '10')
    res.end('Redis client not yet loaded.\n')
    return
  }

  // if we got an array of strings rather than a keygrip, then
  // keygrip it up.
  if (opt.keys) {
    if (!KeyGrip)
      throw new Error('keys provided by KeyGrip not available')
    else if (Array.isArray(opt.keys))
      this.keys = new KeyGrip(opt.keys)
    else if (opt.keys instanceof KeyGrip)
      this.keys = opt.keys
    else
      throw new Error('invalid keys provided')
  }

  // 2 week sessions by default.
  this.expire = opt.expire || 60 * 60 * 24 * 14

  // set up the cookies thingie
  this.cookies = opt.cookies || new Cookies(req, res, this.keys)

  // set the s-cookie
  var name = opt && opt.cookieName || 's'
  var expire_date = new Date(new Date().getTime()+(this.expire*1000))
  var copt = { expires: expire_date, signed: !!this.keys }

  var s = this.cookies.get(name, copt)
  if (!s)
    s = require('crypto').randomBytes(30).toString('base64')

  this.cookies.set(name, s, copt)
  this.token = s

  if (!this.token)
    throw new Error('could not load session token')

  this.id = "session:" + this.token
  this.client = opt.client || RedSess.client
  this.request = req
  this.response = res
}

RedSess.createClient = function (conf) {
  conf = conf || {}
  RedSess.client = redis.createClient(conf.port, conf.host, conf)
  if (conf.auth)
    RedSess.client.auth(conf.auth)
  return RedSess.client
}

RedSess.quit = RedSess.close = RedSess.end = function (cb) {
  if (!RedSess.client)
    return cb && cb()
  if (cb)
    RedSess.client.once("end", cb)
  RedSess.client.quit()
}

RedSess.destroy = function () {
  RedSess.client.end()
}



{'fraction_non_alphanumeric': 0.14453499520613614, 'fraction_numerical': 0.009587727708533078, 'mean_word_length': 3.083170254403131, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6757389', 'n_tokens_mistral': 1527, 'n_tokens_neox': 1466, 'n_words': 394}