<commit_before>/*jslint node: true, es5: true */
var cookie = require('ajncookie');

var sessions = {};

function generateSession() {
    var sessionId = new Date().getTime(),
        id = '',
        max = 10,
        min = 1,
        chars;
    chars = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
          'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
    chars = chars.concat(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
                         'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
                         'V', 'W', 'X', 'Y', 'Z']);
    chars = chars.concat([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
    while (max--) {
        id += chars[Math.floor(Math.random() * (chars.length - 1 - 1 + 1)) + min];
    }
    sessionId += id;
    sessions[sessionId] = {
        date: Date.now(),
        data: {}
    };
    return sessionId;
}

function getSession(sessionId) {
    return sessions[sessionId];
}

function updateSession(sessionId) {
    if (sessions[sessionId]) {
        sessions[sessionId].date = Date.now();
    } else {
        sessionId = generateSession();
    }
    return sessionId;
}

function createSession(req, res, sessionCookieName) {
    var sessionId = cookie.getCookie(req, sessionCookieName);
    if (!sessionId) {

        sessionId = generateSession();
    } else {
        sessionId = updateSession(sessionId);
    }
    cookie.setCookie(res, sessionCookieName, sessionId);
    return sessionId;
}

function invalidateSession(sessionId) {
    if (sessions[sessionId]) {
        sessions[sessionId] = null;
        delete sessions[sessionId];
        return true;
    }
    return false;
}

function delData(sessionId, name) {
    if (sessions[sessionId]) {
        delete sessions[sessionId].data[name];
    }
}

function setData(sessionId, name, value) {
    if (sessions[sessionId]) {
        sessions[sessionId].data[name] = value;
    }
}

function getData(sessionId, name) {
    if (sessions[sessionId]) {
        return sessions[sessionId].data[name];
    }
}


function Session(req, res, conf) {
    conf = conf || {};
    this.maxSize = conf.maxSize || Session.maxSize;
    this.sessionName = conf.sessionName || Session.sessionName;
    this.sessionId = createSession(req, res, this.sessionName);
}
Session.prototype.checkDataSize = function (value) {
    var sess = getSession(this.sessionId),
        size = 0;
    Object.keys(sess.data).forEach(function (name) {
        if (sess.data[name]) {
            size += Buffer.byteLength(sess.data[name]);
        }
    });
    return true
    // return (size + Buffer.byteLength(value)) <= this.maxSize;
};
Session.prototype.setData = function (name, value) {
    this.sessionId = updateSession(this.sessionId);
    if (value === null) {
        setData(this.sessionId, name, value);
    } else if (this.checkDataSize(value)) {
        setData(this.sessionId, name, value);
    } else {
        throw new RangeError('Can\'t set data, limit exceeded. ');
    }<commit_msg>support for object in sessions<commit_after>/*jslint node: true, es5: true */
var cookie = require('ajncookie');

var sessions = {};

function generateSession() {
    var sessionId = new Date().getTime(),
        id = '',
        max = 10,
        min = 1,
        chars;
    chars = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
          'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
    chars = chars.concat(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
                         'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
                         'V', 'W', 'X', 'Y', 'Z']);
    chars = chars.concat([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
    while (max--) {
        id += chars[Math.floor(Math.random() * (chars.length - 1 - 1 + 1)) + min];
    }
    sessionId += id;
    sessions[sessionId] = {
        date: Date.now(),
        data: {}
    };
    return sessionId;
}

function getSession(sessionId) {
    return sessions[sessionId];
}

function updateSession(sessionId) {
    if (sessions[sessionId]) {
        sessions[sessionId].date = Date.now();
    } else {
        sessionId = generateSession();
    }
    return sessionId;
}

function createSession(req, res, sessionCookieName) {
    var sessionId = cookie.getCookie(req, sessionCookieName);
    if (!sessionId) {

        sessionId = generateSession();
    } else {
        sessionId = updateSession(sessionId);
    }
    cookie.setCookie(res, sessionCookieName, sessionId);
    return sessionId;
}

function invalidateSession(sessionId) {
    if (sessions[sessionId]) {
        sessions[sessionId] = null;
        delete sessions[sessionId];
        return true;
    }
    return false;
}

function delData(sessionId, name) {
    if (sessions[sessionId]) {
        delete sessions[sessionId].data[name];
    }
}

function setData(sessionId, name, value) {
    if (sessions[sessionId]) {
        sessions[sessionId].data[name] = value;
    }
}

function getData(sessionId, name) {
    if (sessions[sessionId]) {
        return sessions[sessionId].data[name];
    }
}


function Session(req, res, conf) {
    conf = conf || {};
    this.maxSize = conf.maxSize || Session.maxSize;
    this.sessionName = conf.sessionName || Session.sessionName;
    this.sessionId = createSession(req, res, this.sessionName);
}
Session.prototype.checkDataSize = function (value) {
    var sess = getSession(this.sessionId),
        size = 0;
    Object.keys(sess.data).forEach(function (name) {
        if (sess.data[name]) {
            size += Buffer.byteLength(sess.data[name]);
        }
    });

    if (typeof value !== 'string') {
        value = JSON.stringify(value);
    }

    return (size + Buffer.byteLength(value)) <= this.maxSize;
};
Session.prototype.setData = function (name, value) {
    this.sessionId = updateSession(this.sessionId);
    if (value === null) {
        setData(this.sessionId, name, value);
    } else if (this.checkDataSize(value)) {
        setData(this.sessionId, name, value);
    } else {
        throw new RangeError('Can\'t set data, limit exceeded. ');
    }
{'fraction_non_alphanumeric': 0.17899214659685864, 'fraction_numerical': 0.0058900523560209425, 'mean_word_length': 2.6451997614788314, 'pattern_counts': {'":': 0, '<': 5, '<?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': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2413837', 'n_tokens_mistral': 2080, 'n_tokens_neox': 2028, 'n_words': 478}