<commit_before>Cookies = require "js-cookie"
md5 = require "md5"

errors = require "./util/errors"
merge = require "./util/merge"
uniqueId = require "./util/uniqueid"


###*
 * Interface that all storage classes must implement. See Session class.
###
class ISessionStore
  ###*
   * Gets the value for the specified key from the storage.
   * @public
   * @param  {String} key
   * @param  {*}      defaultValue Value to return if the key does not exist.
   * @return {*}
  ###
  get: (key, defaultValue) ->
    dataObj = @__getData()
    unless dataObj.session_id?
      throw (errors.error "__getData must ensure session_id exists!", @)
    dataObj[key] or defaultValue

  ###*
   * Sets the value for the specified key.
   * @public
   * @param  {String} key
   * @param  {*}      value
   * @return {Object}       Current data.
  ###
  set: (key, value) ->
    dataObj = @__getData()
    dataObj[key] = value
    @__setData dataObj

  ###*
   * Deletes the specified key
   * @param  {String} key
   * @return {Object}     Current data.
  ###
  del: (key) ->
    dataObj = @__getData()
    delete dataObj[key]
    @__setData dataObj

  ###*
   * Returns the current data object and ensures that it contains a session_id
   * key.
   *
   * @protected
   * @return {Object} Current data.
  ###
  __getData: ->
    throw (errors.error "__getData() not implemented!", @)

  ###*
   * Sets the current data object.
   * @protected
   * @param  {Object} dataObj New data.
   * @return {Object}         New data.
  ###
  __setData: (dataObj) ->
    throw (errors.error "__setData(dataObj) not implemented!", @)

  ###*
   * Deletes the current data, including session_id.
   * @public
   * @return {*}
  ###
  clean: ->
    throw (errors.error "clean() not implemented!", @)

  ###*
   * Checks whether the session exists or not.
   * @return {Boolean} `true` if the session exists, `false` otherwise.
  ###
  exists: ->
    throw (errors.error "exists() not implemented!", @)


###*
 * Holds session data in a plain object.
###
class ObjectSessionStore extends ISessionStore
  ###*
   * @param  {Object} data Session data.
  ###
  constructor: (@data = {}) ->

  __getData: ->
    # ensure session id exists
    @data.session_id ?= uniqueId.generate.hash()
    @data

  __setData: (@data) ->

  clean: ->
    @data = {}

  exists: ->
    @data.session_id?


###*
 * Holds session data in a browser cookie.
###
class CookieSessionStore extends ISessionStore
  ###*
   * @param  {String} cookieName Name for the cookie.
   * @param  {Object} options    Options object.
   *                             - prefix: String to be appended to the cookie
   *                                 name.
   *                             - expiry: In days, 1 hour by default
  ###
  constructor: (cookieName, options = {}) ->
    defaults =
      prefix: ""
      expiry: 1 / 24
    options = merge defaults, (options or {})
    @cookieName = "#{options.prefix}#{cookieName}"
    @expiry = options.expiry
<commit_msg>added 24 hour expiry time<commit_after>Cookies = require "js-cookie"
md5 = require "md5"

errors = require "./util/errors"
merge = require "./util/merge"
uniqueId = require "./util/uniqueid"


###*
 * Interface that all storage classes must implement. See Session class.
###
class ISessionStore
  ###*
   * Gets the value for the specified key from the storage.
   * @public
   * @param  {String} key
   * @param  {*}      defaultValue Value to return if the key does not exist.
   * @return {*}
  ###
  get: (key, defaultValue) ->
    dataObj = @__getData()
    unless dataObj.session_id?
      throw (errors.error "__getData must ensure session_id exists!", @)
    dataObj[key] or defaultValue

  ###*
   * Sets the value for the specified key.
   * @public
   * @param  {String} key
   * @param  {*}      value
   * @return {Object}       Current data.
  ###
  set: (key, value) ->
    dataObj = @__getData()
    dataObj[key] = value
    @__setData dataObj

  ###*
   * Deletes the specified key
   * @param  {String} key
   * @return {Object}     Current data.
  ###
  del: (key) ->
    dataObj = @__getData()
    delete dataObj[key]
    @__setData dataObj

  ###*
   * Returns the current data object and ensures that it contains a session_id
   * key.
   *
   * @protected
   * @return {Object} Current data.
  ###
  __getData: ->
    throw (errors.error "__getData() not implemented!", @)

  ###*
   * Sets the current data object.
   * @protected
   * @param  {Object} dataObj New data.
   * @return {Object}         New data.
  ###
  __setData: (dataObj) ->
    throw (errors.error "__setData(dataObj) not implemented!", @)

  ###*
   * Deletes the current data, including session_id.
   * @public
   * @return {*}
  ###
  clean: ->
    throw (errors.error "clean() not implemented!", @)

  ###*
   * Checks whether the session exists or not.
   * @return {Boolean} `true` if the session exists, `false` otherwise.
  ###
  exists: ->
    throw (errors.error "exists() not implemented!", @)


###*
 * Holds session data in a plain object.
###
class ObjectSessionStore extends ISessionStore
  ###*
   * @param  {Object} data Session data.
  ###
  constructor: (@data = {}) ->

  __getData: ->
    # ensure session id exists
    @data.session_id ?= uniqueId.generate.hash()
    @data

  __setData: (@data) ->

  clean: ->
    @data = {}

  exists: ->
    @data.session_id?


###*
 * Holds session data in a browser cookie.
###
class CookieSessionStore extends ISessionStore
  ###*
   * @param  {String} cookieName Name for the cookie.
   * @param  {Object} options    Options object.
   *                             - prefix: String to be appended to the cookie
   *                                 name.
   *                             - expiry: In days, 1 hour by default
  ###
  constructor: (cookieName, options = {}) ->
    defaults =
      prefix: ""
      expiry: 1
    options = merge defaults, (options or {})
    @cookieName = "#{options.prefix}#{cookieName}"
    @expiry = options.expiry

{'fraction_non_alphanumeric': 0.13800566006325954, 'fraction_numerical': 0.001997669385716664, 'mean_word_length': 2.4608294930875574, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6094306', 'n_tokens_mistral': 1899, 'n_tokens_neox': 1871, 'n_words': 573}