node-analytics.js
function getSession(req, res, cb){

    const now = new Date();

    const cookies = getCookies(req.headers.cookie);

    // cookies.na_session  :: session._id
    // cookies.na_user     :: session.user

    // Establish session: new/old session? new/old user?
    if(cookies.na_session){
        if(opts.log_all)
            log('Session cookie found:', cookies.na_session);

        Session.findById(cookies.na_session, session_fields).lean().exec(function(err, session){
            if(err){
                log.error('getSession error', err);
                return cb(err);
            }

            if(!session){
                log.error('Session not found :: id[cookie]:', this.cookies.na_session);

                // send to check if user instead
                if(cookies.na_user)
                    userSession();
                else
                    newSession();
            }
            else {
                Update.session(session, {
                    $set: {
                        last: Date.now()
                    }
                }, (err, session) => {
                    if(err){
                        log.error('establish session / update error');
                        return cb(true);
                    }

                    session.continued = true;
                    cb(err, this.req, this.res, session)
                });
            }

        }.bind({
            cookies: cookies,
            req: req,
            res: res
        }))
    }
    else if(cookies.na_user)
        userSession();
    else
        newSession();

    // ====================

    function userSession(){

        // OLD USER, NEW SESSION

        cb(null, req, res, new Session({
            user: cookies.na_user,
            new_session: true
        }).toObject({ virtuals: true }));

        if(opts.log_all)
            log.timer('getSession 1', now);
    }
    function newSession(){

        // NEW USER, NEW SESSION
        // Initiate session to get _id
        let session = new Session();
        session.user = session._id.toString();
        session.new_user = true;

        session = session.toObject({ virtuals: true });

        cb(null, req, res, session);

        if(opts.log_all)
            log.timer('getSession 2', now);
    }
}
function getSession(req, res, cb){

    const now = new Date();

    const cookies = getCookies(req.headers.cookie);

    // cookies.na_session  :: session._id
    // cookies.na_user     :: session.user

    // Establish session: new/old session? new/old user?
    if(cookies.na_session){
        if(opts.log_all)
            log('Session cookie found:', cookies.na_session);

        Session.findById(cookies.na_session, session_fields).lean().exec(function(err, session){
            if(err){
                log.error('getSession error', err);
                return cb(err);
            }

            if(!session){
                log.error('Session not found :: id[cookie]:', this.cookies.na_session);

                // send to check if user instead
                if(cookies.na_user)
                    userSession();
                else
                    newSession();
            }
            else {
                Update.session(session, {
                    $set: {
                        last: Date.now()
                    }
                }, (err, session) => {
                    if(err){
                        log.error('establish session / update error');
                        return cb(true);
                    }

                    session.continued = true;
                    cb(err, this.req, this.res, session)
                });
            }

        }.bind({
            cookies: cookies,
            req: req,
            res: res
        }))
    }
    else if(cookies.na_user)
        userSession();
    else
        newSession();

    // ====================

    function userSession(){

        // OLD USER, NEW SESSION

        cb(null, req, res, new Session({
            user: cookies.na_user,
            new_session: true
        }).toObject({ virtuals: true }));

        if(opts.log_all)
            log.timer('getSession 1', now);
    }
    function newSession(){

        // NEW USER, NEW SESSION
        // Initiate session to get _id
        let session = new Session();
        session.user = session._id.toString();
        session.new_user = true;

        session = session.toObject({ virtuals: true });

        cb(null, req, res, session);

        if(opts.log_all)
            log.timer('getSession 2', now);
    }
}
===================== populate var session; returns boolean on whether newly formed