<commit_before>var fs = require('fs');
var path = require('path');
var chalk = require('chalk');
var socket_io = require('socket.io');
var files = require('./files');
var logMsg = require('./utils').logMsg;
var relPathBuilder = require('./utils').relPathBuilder;
var virtualPath = require('./virtualPath');
var fsWatcher = require('./watch');
var hotStartCache = require('./resourceCache');

module.exports = function createServer(httpServer, options){
  var syncServer = socket_io(httpServer, { serveClient: false });
  var clientCount = 0;
  var normPath = relPathBuilder(options.base);

  function broadcast(type, data){
    syncServer.emit(type, data);
  }

  virtualPath.add('/basisjs-tools/fileSync.js', function(api){
    var socketIOClientfilename = require.resolve('socket.io-client/socket.io.js');
    var filename = __dirname + '/../assets/client.js';
    var mtime = new Date(Math.max(
      fs.statSync(socketIOClientfilename).mtime,
      fs.statSync(filename).mtime
    ));

    if (api.isContentModified(mtime))
    {
      var errHandler = function(err, filename){
        api.serverError('Can\'t read file (' + filename + '): ' + err);
      };

      files.readFile(filename, function(err, fileSyncData, fn){
        if (err)
          return errHandler(err, fn);

        files.readFile(socketIOClientfilename, function(err, data, fn){
          if (err)
            return errHandler(err, fn);

          api.logMsg(chalk.yellow('(read)'));

          api.responseToClient(data + fileSyncData, {
            contentType: 'application/javascript',
            mtime: mtime
          });
        });
      });
    }
    else
      api.logMsg(chalk.green('304'));
  });

  syncServer.sockets.on('connection', function(socket){
    logMsg('socket', 'client ' + chalk.yellow('connected'));
    clientCount++;

    socket.on('saveFile', require('./socket/saveFile.js')(options, broadcast));
    socket.on('createFile', require('./socket/createFile.js')(options, broadcast));
    socket.on('readFile', require('./socket/readFile.js')(options, broadcast));

    socket.on('openFile', require('./socket/openFile.js')(options, broadcast));
    socket.on('getFileGraph', require('./socket/getFileGraph.js')(options, broadcast));
    socket.on('handshake', require('./socket/handshake.js')(options, broadcast));
  });

  syncServer.sockets.on('connection', function(socket){
    logMsg('socket', 'client ' + chalk.yellow('disconnected'));
    clientCount--;
  });

  files.onRead(function(err, data, filename){
    if (err)
      return;

    var fileInfo = files.getInfo(filename, true);
    var fnKey = normPath(filename);

    if (hotStartCache.has(fnKey))
    {
      if (!clientCount || /^basisjs-tools:/.test(fnKey))
        return;

      logMsg('bcast', chalk.green('updateFile') + ' ' + fnKey);
      syncServer.emit('updateFile', {
        filename: fnKey,
        content: fileInfo.content
      });
    }
  });

  return syncServer;
};<commit_msg>server: fix file content update by sync server (typo)<commit_after>var fs = require('fs');
var path = require('path');
var chalk = require('chalk');
var socket_io = require('socket.io');
var files = require('./files');
var logMsg = require('./utils').logMsg;
var relPathBuilder = require('./utils').relPathBuilder;
var virtualPath = require('./virtualPath');
var fsWatcher = require('./watch');
var hotStartCache = require('./resourceCache');

module.exports = function createServer(httpServer, options){
  var syncServer = socket_io(httpServer, { serveClient: false });
  var clientCount = 0;
  var normPath = relPathBuilder(options.base);

  function broadcast(type, data){
    syncServer.emit(type, data);
  }

  virtualPath.add('/basisjs-tools/fileSync.js', function(api){
    var socketIOClientfilename = require.resolve('socket.io-client/socket.io.js');
    var filename = __dirname + '/../assets/client.js';
    var mtime = new Date(Math.max(
      fs.statSync(socketIOClientfilename).mtime,
      fs.statSync(filename).mtime
    ));

    if (api.isContentModified(mtime))
    {
      var errHandler = function(err, filename){
        api.serverError('Can\'t read file (' + filename + '): ' + err);
      };

      files.readFile(filename, function(err, fileSyncData, fn){
        if (err)
          return errHandler(err, fn);

        files.readFile(socketIOClientfilename, function(err, data, fn){
          if (err)
            return errHandler(err, fn);

          api.logMsg(chalk.yellow('(read)'));

          api.responseToClient(data + fileSyncData, {
            contentType: 'application/javascript',
            mtime: mtime
          });
        });
      });
    }
    else
      api.logMsg(chalk.green('304'));
  });

  syncServer.sockets.on('connection', function(socket){
    logMsg('socket', 'client ' + chalk.yellow('connected'));
    clientCount++;

    socket.on('saveFile', require('./socket/saveFile.js')(options, broadcast));
    socket.on('createFile', require('./socket/createFile.js')(options, broadcast));
    socket.on('readFile', require('./socket/readFile.js')(options, broadcast));

    socket.on('openFile', require('./socket/openFile.js')(options, broadcast));
    socket.on('getFileGraph', require('./socket/getFileGraph.js')(options, broadcast));
    socket.on('handshake', require('./socket/handshake.js')(options, broadcast));
  });

  syncServer.sockets.on('disconnect', function(socket){
    logMsg('socket', 'client ' + chalk.yellow('disconnected'));
    clientCount--;
  });

  files.onRead(function(err, data, filename){
    if (err)
      return;

    var fileInfo = files.getInfo(filename, true);
    var fnKey = normPath(filename);

    if (hotStartCache.has(fnKey))
    {
      if (!clientCount || /^basisjs-tools:/.test(fnKey))
        return;

      logMsg('bcast', chalk.green('updateFile') + ' ' + fnKey);
      syncServer.emit('updateFile', {
        filename: fnKey,
        content: fileInfo.content
      });
    }
  });

  return syncServer;
};
{'fraction_non_alphanumeric': 0.16850736278447123, 'fraction_numerical': 0.0013386880856760374, 'mean_word_length': 3.3916238060249815, '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': '3200581', 'n_tokens_mistral': 1818, 'n_tokens_neox': 1760, 'n_words': 332}