<commit_before>/*
  Similar to meteor.methods
*/
var serverAPI = require('./server_api.js');

function MethodCall(message){
  this.id = message.id;
  this.ws = message.ws;
  this.name = message.name;
  this.data = message.data;
}

MethodCall.prototype.exec = function(){
  var that = this;
  var result;
  try{
      result = methods.list[this.name](this.data,this,function(e,result){
      if(e) return that.sendErr(e);
      if(result) return that.sendResult(result);
      console.log("no error, no result");
    });
  }catch(e){
    console.log('error in method ' + this.name);
    console.log(e.stack);
    return this.sendErr(e);
  }
  if(typeof result != "undefined")
    this.sendResult(result);
};

MethodCall.prototype.sendErr = function (e){
  process.send({cmd:"send",message:{
    id: this.id,
    ws: this.ws.id,
    error: e.toString(),
    data: null
  }});
};
MethodCall.prototype.sendResult = function (result){
  process.send({cmd:"send",message:{
    id: this.id,
    ws: this.ws.id,
    error: null,
    data: result
  }});
};


var methods = {};

// object of all methods
methods.list = {};
methods.users = {};


// function to add method to methods.list
methods.add = function (array) {

  for (var method in array) {
    methods.list[method] = array[method];
    process.send({cmd:"add",name:method});
  }

};

// execute method when called by client
methods.call = function(ws,message){
  var meth;
  try{
    meth = new MethodCall(ws,message);
  }catch(e){
    return console.log("error: "+e+", message: "+ JSON.stringify(message));
  }
  meth.exec();
};
var User = require(__dirname+"/ws_puppet.js");
process.on("message",function(message){
  if(!(message.ws in methods.users))
    methods.users[message.ws] = new User(message.ws);
  message.ws  = methods.users[message.ws];
  /*
  Commands:
    disconnect: Head is closed
    server api: return value for silk api method
  */
  if(!("cmd" in message)){
    var meth = new MethodCall(message);
    return meth.exec();
  }
  switch(message.cmd){
    case "disconnect": message.ws.emit("close"); break;
    case "close": break; //expected to close, will close forcfully in 5 seconds
    case "sleep": break; //Head is removed from the window manager so updates are impossible
    case "minimize": break; //Head is not removed but updates to the head will not be seen
    case "server api": Silk.api.done(message);
  }
});

// make global because it will be used in most files.
global.Silk = {};
Silk.methods = methods;
Silk.api = serverAPI;
global.methods = methods;

process.nextTick(function(){
  require(process.env.start);
});

process.send({cmd:"ready"});
<commit_msg>apps using fork framework have title set to app's name<commit_after>/*
  Similar to meteor.methods
*/
var serverAPI = require('./server_api.js');

function MethodCall(message){
  this.id = message.id;
  this.ws = message.ws;
  this.name = message.name;
  this.data = message.data;
}

MethodCall.prototype.exec = function(){
  var that = this;
  var result;
  try{
      result = methods.list[this.name](this.data,this,function(e,result){
      if(e) return that.sendErr(e);
      if(result) return that.sendResult(result);
      console.log("no error, no result");
    });
  }catch(e){
    console.log('error in method ' + this.name);
    console.log(e.stack);
    return this.sendErr(e);
  }
  if(typeof result != "undefined")
    this.sendResult(result);
};

MethodCall.prototype.sendErr = function (e){
  process.send({cmd:"send",message:{
    id: this.id,
    ws: this.ws.id,
    error: e.toString(),
    data: null
  }});
};
MethodCall.prototype.sendResult = function (result){
  process.send({cmd:"send",message:{
    id: this.id,
    ws: this.ws.id,
    error: null,
    data: result
  }});
};


var methods = {};

// object of all methods
methods.list = {};
methods.users = {};


// function to add method to methods.list
methods.add = function (array) {

  for (var method in array) {
    methods.list[method] = array[method];
    process.send({cmd:"add",name:method});
  }

};

// execute method when called by client
methods.call = function(ws,message){
  var meth;
  try{
    meth = new MethodCall(ws,message);
  }catch(e){
    return console.log("error: "+e+", message: "+ JSON.stringify(message));
  }
  meth.exec();
};
var User = require(__dirname+"/ws_puppet.js");
process.on("message",function(message){
  if(!(message.ws in methods.users))
    methods.users[message.ws] = new User(message.ws);
  message.ws  = methods.users[message.ws];
  /*
  Commands:
    disconnect: Head is closed
    server api: return value for silk api method
  */
  if(!("cmd" in message)){
    var meth = new MethodCall(message);
    return meth.exec();
  }
  switch(message.cmd){
    case "disconnect": message.ws.emit("close"); break;
    case "close": break; //expected to close, will close forcfully in 5 seconds
    case "sleep": break; //Head is removed from the window manager so updates are impossible
    case "minimize": break; //Head is not removed but updates to the head will not be seen
    case "server api": Silk.api.done(message);
  }
});

// make global because it will be used in most files.
global.Silk = {};
Silk.methods = methods;
Silk.api = serverAPI;
global.methods = methods;

process.nextTick(function(){
  process.title = process.env.app;
  require(process.env.start);
});

process.send({cmd:"ready"});

{'fraction_non_alphanumeric': 0.15785554728220402, 'fraction_numerical': 0.00037230081906180194, 'mean_word_length': 3.515126050420168, 'pattern_counts': {'":': 10, '<': 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': '2763432', 'n_tokens_mistral': 1756, 'n_tokens_neox': 1734, 'n_words': 459}