<commit_before>'use strict';
/*
 * Stores session data in the database
 * Source; https://github.com/edy-b/SciFlowWriter/blob/develop/available_plugins/ep_sciflowwriter/db/DirtyStore.js
 * This is not used for authors that are created via the API at current
 *
 * RPB: this module was not migrated to Promises, because it is only used via
 *      express-session, which can't actually use promises anyway.
 */

const DB = require('./DB');
const Store = require('express-session').Store;
const log4js = require('log4js');

const logger = log4js.getLogger('SessionStore');

module.exports = class SessionStore extends Store {
  get(sid, fn) {
    logger.debug(`GET ${sid}`);
    DB.db.get(`sessionstorage:${sid}`, (err, s) => {
      if (err != null) return fn(err);
      if (!s) return fn(null);
      if (typeof s.cookie.expires === 'string') s.cookie.expires = new Date(s.cookie.expires);
      if (s.cookie.expires && new Date() >= s.cookie.expires) return this.destroy(sid, fn);
      fn(null, s);
    });
  }

  set(sid, sess, fn) {
    logger.debug(`SET ${sid}`);
    DB.db.set(`sessionstorage:${sid}`, sess, fn);
  }

  destroy(sid, fn) {
    logger.debug(`DESTROY ${sid}`);
    DB.db.remove(`sessionstorage:${sid}`, fn);
  }
};<commit_msg>SessionStore: Promisify to the extent permitted by express-session<commit_after>'use strict';

const DB = require('./DB');
const Store = require('express-session').Store;
const log4js = require('log4js');
const util = require('util');

const logger = log4js.getLogger('SessionStore');

class SessionStore extends Store {
  async _get(sid) {
    logger.debug(`GET ${sid}`);
    const s = await DB.get(`sessionstorage:${sid}`);
    if (!s) return;
    if (typeof s.cookie.expires === 'string') s.cookie.expires = new Date(s.cookie.expires);
    if (s.cookie.expires && new Date() >= s.cookie.expires) {
      await this._destroy(sid);
      return;
    }
    return s;
  }

  async _set(sid, sess) {
    logger.debug(`SET ${sid}`);
    await DB.set(`sessionstorage:${sid}`, sess);
  }

  async _destroy(sid) {
    logger.debug(`DESTROY ${sid}`);
    await DB.remove(`sessionstorage:${sid}`);
  }
}

// express-session doesn't support Promise-based methods. This is where the callbackified versions
// used by express-session are defined.
for (const m of ['get', 'set', 'destroy']) {
  SessionStore.prototype[m] = util.callbackify(SessionStore.prototype[`_${m}`]);
}

module.exports = SessionStore;
{'fraction_non_alphanumeric': 0.1678951678951679, 'fraction_numerical': 0.002457002457002457, 'mean_word_length': 3.856858846918489, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 1, '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': '1851948', 'n_tokens_mistral': 801, 'n_tokens_neox': 798, 'n_words': 206}