<commit_before>const Session   = plugins.require('sessions/Session');
const uniqid    = require('uniqid');

if (!Date.now)
{
    Date.now = function()
    {
        return new Date().getTime();
    };
}

class SessionsManager
{
    constructor(siteManager)
    {
        this.siteManager = siteManager;
        this.sessions = {};

        var _this = this;
        this.interval = setInterval(function()
        {
            _this.checkExpiredSessions();
        }, 5000);

        this.interval.unref();
    }

    getFromCookies(cookies)
    {
        if (typeof (cookies) !== 'object')
            return {};

        if (typeof (cookies.token) !== 'string' || cookies.token.length < 10 || cookies.token.length > 20 || cookies.token === '__global__') {
            cookies.token = uniqid();
            this.sessions[cookies.token] = new Session(this.siteManager, cookies.token);
            return this.sessions[cookies.token];
        }

        return this.getFromToken(cookies.token);
    }

    getFromToken(token, isNewToken)
    {
        try
        {
            if (this.sessions[token] === undefined) {
                this.sessions[token] = new Session(this.siteManager, token);
                
                if(!isNewToken) {
                    this.sessions[token].ready = false;
                }
            }

            return this.sessions[token];
        }
        catch (e)
        {
            console.error(e);
        }

        return null;
    }

    //-----------------------------------------------------

    checkExpiredSessions()
    {
        try
        {
            for (var token in this.sessions)
            {
                if (this.sessions[token].expirationTime <= Date.now())
                    delete this.sessions[token];
            }
        }
        catch (e)
        {
            console.error(e);
        }
    }

    //-----------------------------------------------------
    //Session socket

    handleSocket(socket)
    {
        try
        {
            var index = socket.url.lastIndexOf('?token=');
            if (index > 0)
            {
                var token = socket.url.substr(index + 7);
                index = token.indexOf('&');

                if (index != -1)
                    token = token.substr(0, index);

                if (token.length < 10 || token.length > 20 || token === '__global__')
                {
                    token = uniqid();
                    socket.write(JSON.stringify({cookies: {token: token}}));
                    socket.close();
                    return;
                }

                this.getFromToken(token).handleSocket(socket);
            }
        }
        catch (e)
        {
            console.error(e);
        }
    }

    broadcast(message, selector)
    {
        const sift = require('sift');

        try
        {
            for (var key in this.sessions)
            {
                if(selector) {
                    if(sift(selector, [ this.sessions[key].data ]).lengtth > 0)
                        this.sessions[key].broadcastSocketMessage(message);
                } else {
                    this.sessions[key].broadcastSocketMessage(message);
                }
            }
        }
        catch (e)
        {
            console.error(e);
        }
    }
}

module.exports = SessionsManager;<commit_msg>no message<commit_after>const Session   = plugins.require('sessions/Session');
const uniqid    = require('uniqid');

if (!Date.now)
{
    Date.now = function()
    {
        return new Date().getTime();
    };
}

class SessionsManager
{
    constructor(siteManager)
    {
        this.siteManager = siteManager;
        this.sessions = {};

        var _this = this;
        this.interval = setInterval(function()
        {
            _this.checkExpiredSessions();
        }, 5000);

        this.interval.unref();
    }

    getFromCookies(cookies)
    {
        if (typeof (cookies) !== 'object')
            return {};

        if (typeof (cookies.token) !== 'string' || cookies.token.length < 10 || cookies.token.length > 20 || cookies.token === '__global__') {
            cookies.token = uniqid();
            this.sessions[cookies.token] = new Session(this.siteManager, cookies.token);
            return this.sessions[cookies.token];
        }

        return this.getFromToken(cookies.token);
    }

    getFromToken(token, isNewToken)
    {
        try
        {
            if (this.sessions[token] === undefined) {
                this.sessions[token] = new Session(this.siteManager, token);
                
                if(!isNewToken) {
                    this.sessions[token].ready = false;
                }
            }

            return this.sessions[token];
        }
        catch (e)
        {
            console.error(e);
        }

        return null;
    }

    //-----------------------------------------------------

    checkExpiredSessions()
    {
        try
        {
            for (var token in this.sessions)
            {
                if (this.sessions[token].expirationTime <= Date.now())
                    delete this.sessions[token];
            }
        }
        catch (e)
        {
            console.error(e);
        }
    }

    //-----------------------------------------------------
    //Session socket

    handleSocket(socket)
    {
        try
        {
            var index = socket.url.lastIndexOf('?token=');
            if (index > 0)
            {
                var token = socket.url.substr(index + 7);
                index = token.indexOf('&');

                if (index != -1)
                    token = token.substr(0, index);

                if (token.length < 10 || token.length > 20 || token === '__global__')
                {
                    token = uniqid();
                    socket.write(JSON.stringify({cookies: {token: token}}));
                    socket.close();
                    return;
                }

                this.getFromToken(token).handleSocket(socket);
            }
        }
        catch (e)
        {
            console.error(e);
        }
    }

    broadcast(message, selector)
    {
        var sifter;
        
        if(selector) {
            const sift = require('sift');
            sifter = sift(selector);
        }

        try
        {
            for (var key in this.sessions)
            {
                if(selector) {
                    if(sifter(this.sessions[key].data))
                        this.sessions[key].broadcastSocketMessage(message);
                } else {
                    this.sessions[key].broadcastSocketMessage(message);
                }
            }
        }
        catch (e)
        {
            console.error(e);
        }
    }
}

module.exports = SessionsManager;
{'fraction_non_alphanumeric': 0.14784394250513347, 'fraction_numerical': 0.0048401290701085365, 'mean_word_length': 1.1531417745500474, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, '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': '5993344', 'n_tokens_mistral': 1905, 'n_tokens_neox': 1785, 'n_words': 306}