<commit_before>var connect        = require('connect'),
    fs             = require('fs'),
    assert         = require('assert'),
    BrowserManager = require('vt').BrowserManager;

// I'm sure there's a better way to do this but not a priority now.
if (process.cwd().match(/examples\/?$/)) {
    process.chdir('test-server');
} else if (!process.cwd().match(/test-server\/?$/)) {
    process.chdir('examples/test-server');
}

var browsers = new BrowserManager();
var basePage = fs.readFileSync('base.html', 'utf8'); //cache the base page

var memory = new connect.session.MemoryStore({
    reapInterval: 60000
  , maxAge: 60000 * 2
});

var staticDir = __dirname + '/public';
console.log('Static directory: ' + staticDir);

var server = connect.createServer(
    connect.logger(),
    connect.cookieParser(),
    connect.session({ store: memory, secret: 'test' }),
    connect.static(staticDir),
    connect.router(app)
);

server.listen(3000);
console.log('Server listening on port 3000');

//NOTE: When client connects to socket.io socket, then it has rendered the DOM and gotten to onLoad();
var io = require('socket.io');
var socket = io.listen(server);
socket.on('connection', function (client) {
    var browser = undefined;
    var numMessages = 0;
    client.on('message', function (msg) {
        if (browser == undefined) {
            // msg should be the client's sessionID
            console.log('Socket.io client connected: ' + msg);
            assert.equal(numMessages, 0, "browser should only be undefined on first message");
            browsers.lookup(msg, function (browse) {
                browser = browse;
            });
            browser.initializeClient(client);
        } else {
            // otherwise, this was an event handler firing, but we haven't gotten there yet.
            throw new Error("Client shouldn't be triggering events yet.");
        }
    });
    client.on('disconnect', function (msg) {
        console.log('Client disconnected.');
        browser = undefined;
    });
});

// These will be for initial calls to a new resource
// TODO: add err as first param to callbacks
function app(app) {
    app.get('/local/:filename', function (req, res) {
        loadLocal({
            req: req,
            success : function (browser) {
                res.writeHead(200, {'Content-type': 'text/html'});
                res.end(basePage.replace(/:SESSION_ID:/, req.sessionID));<commit_msg>Fixed race condition in server.js<commit_after>var connect        = require('connect'),
    fs             = require('fs'),
    assert         = require('assert'),
    BrowserManager = require('vt').BrowserManager;

// I'm sure there's a better way to do this but not a priority now.
if (process.cwd().match(/examples\/?$/)) {
    process.chdir('test-server');
} else if (!process.cwd().match(/test-server\/?$/)) {
    process.chdir('examples/test-server');
}

var browsers = new BrowserManager();
var basePage = fs.readFileSync('base.html', 'utf8'); //cache the base page

var memory = new connect.session.MemoryStore({
    reapInterval: 60000
  , maxAge: 60000 * 2
});

var staticDir = __dirname + '/public';
console.log('Static directory: ' + staticDir);

var server = connect.createServer(
    connect.logger(),
    connect.cookieParser(),
    connect.session({ store: memory, secret: 'test' }),
    connect.static(staticDir),
    connect.router(app)
);

server.listen(3000);
console.log('Server listening on port 3000');

//NOTE: When client connects to socket.io socket, then it has rendered the DOM and gotten to onLoad();
var io = require('socket.io');
var socket = io.listen(server);
socket.on('connection', function (client) {
    var browser = undefined;
    var numMessages = 0;
    //TODO: can use client.once
    client.on('message', function (msg) {
        if (browser == undefined) {
            // msg should be the client's sessionID
            console.log('Socket.io client connected: ' + msg);
            assert.equal(numMessages, 0, "browser should only be undefined on first message");
            browsers.lookup(msg, function (browse) {
                browser = browse;
                browser.initializeClient(client);
            });
        } else {
            // otherwise, this was an event handler firing, but we haven't gotten there yet.
            throw new Error("Client shouldn't be triggering events yet.");
        }
    });
    client.on('disconnect', function (msg) {
        console.log('Client disconnected.');
        browser = undefined;
    });
});

// These will be for initial calls to a new resource
// TODO: add err as first param to callbacks
function app(app) {
    app.get('/local/:filename', function (req, res) {
        loadLocal({
            req: req,
            success : function (browser) {
                res.writeHead(200, {'Content-type': 'text/html'});
                res.end(basePage.replace(/:SESSION_ID:/, req.sessionID));
{'fraction_non_alphanumeric': 0.13999189627228525, 'fraction_numerical': 0.01012965964343598, 'mean_word_length': 2.720422004521477, 'pattern_counts': {'":': 0, '<': 3, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '99710', 'n_tokens_mistral': 1427, 'n_tokens_neox': 1349, 'n_words': 415}