<commit_before>var express = require('express'),
    Eye = require('./eye');

var eyeFlagNames = {};
Eye.flagNames.forEach(function (option) {
  eyeFlagNames[option.toLowerCase()] = option;
});

function EyeServer(options) {
  options = options || {};
  
  // dummy constructor to enable EyeServer construction without new
  function F() {};
  F.prototype = EyeServer.prototype;
  
  // create new EyeServer, inheriting from express.HTTPServer
  eyeServer = new F();
  eyeServer.constructor = EyeServer;
  express.HTTPServer.call(eyeServer, []);
  
  // intialize reasoner
  var eye = new Eye();
  
  // initialize server
  eyeServer.use(express.bodyParser());
  eyeServer.get (/^\/$/, handleEyeRequest);
  eyeServer.post(/^\/$/, handleEyeRequest);
  eyeServer.options(/^\/$/, handleOptions);
  
  function handleEyeRequest (req, res, next) {
    var reqParams = req.query,
        body = req.body || {},
        data = reqParams.data || [],
        query = reqParams.query || body.query,
        jsonpCallback = reqParams.callback,
        settings = {};
    
    // make sure data is an array
    if(typeof(data) === 'string')
      data = data.split(',');
    
    // add body data
    if(typeof(body.data) === 'string')
      data.push(body.data);
    else if(body.data instanceof Array)
      data.push.apply(data, body.data);
    
    // collect data and data URIs
    settings.data = [];
    // inspect all data parameters in request parameters
    data.forEach(function (item) {
      if(!item.match(/^https?:\/\//))
        // item is N3 data – push it
        settings.data.push(item);
      else
        // item is list of URIs – push each of them
        settings.data.push.apply(settings.data, item.split(','));
    });
    
    // do a reasoner pass by default
    settings.pass = true;
    
    // add query if present
    if(query) {
      settings.query = query;
      delete settings.pass;
    }
    
    // add boolean flags
    for(var param in reqParams) {
      var eyeFlagName = eyeFlagNames[param.replace(/-/g, '').toLowerCase()];
      if(eyeFlagName)
        settings[eyeFlagName] = !reqParams[param].match(/^0|false$/i);
    }

    // add debug information if requested
    if(options.debug)
      settings.originalUrl = req.originalUrl;

    // execute the reasoner and return result or error
    (options.eye || eye).execute(settings, function (error, result) {
      if(!jsonpCallback) {
        setHeaders(req, res);
        if(!error) {
          res.header('Content-Type', 'text/n3');
          res.send(result + '\n');
        }
        else {
          res.header('Content-Type', 'text/plain');
          res.send(error + '\n', 400);
        }
      }
      else {
        res.header('Content-Type', 'application/javascript');
        if(jsonpCallback.match(/^[\w\d-_]+$/i))
          res.send(jsonpCallback + '(' + JSON.stringify(error || result) + ')');
        else
          res.send('alert("Illegal callback name.")', 400);
      }
    });
  }
  
  function handleOptions(req, res, next) {
    setHeaders(req, res);
    res.header('Content-Type', 'text/plain');
    res.send('');
  }
  
  function setHeaders(req, res) {
    res.header('X-Powered-By', 'EYE Server');
    res.header('Access-Control-Allow-Origin', '*');
  }
  
  return eyeServer;
}

// inherit from express.HTTPServer
EyeServer.prototype.constructor = EyeServer;
EyeServer.prototype.__proto__ = express.HTTPServer.prototype;

module.exports = EyeServer;<commit_msg>Made EyeServer instance methods prototypal.<commit_after>var express = require('express'),
    Eye = require('./eye');

var eyeFlagNames = {};
Eye.flagNames.forEach(function (option) {
  eyeFlagNames[option.toLowerCase()] = option;
});

function EyeServer(options) {
  // dummy constructor to enable EyeServer construction without new
  function F() {};
  F.prototype = EyeServer.prototype;
  
  // create new EyeServer, inheriting from express.HTTPServer
  eyeServer = new F();
  eyeServer.constructor = EyeServer;
  express.HTTPServer.call(eyeServer, []);
  
  // apply settings and defaults
  eyeServer.settings = options || {};
  eyeServer.settings.eye = eyeServer.settings.eye || new Eye();
  
  // initialize server
  eyeServer.use(express.bodyParser());
  eyeServer.get (/^\/$/, proxy(eyeServer, eyeServer.handleEyeRequest));
  eyeServer.post(/^\/$/, proxy(eyeServer, eyeServer.handleEyeRequest));
  eyeServer.options(/^\/$/, proxy(eyeServer, eyeServer.handleEyeOptionsRequest));
  
  return eyeServer;
}

EyeServer.prototype = {
  constructor: EyeServer,
  
  // inherit from express.HTTPServer
  __proto__: express.HTTPServer.prototype,
  
  handleEyeRequest: function (req, res, next) {
    var self = this,
        reqParams = req.query,
        body = req.body || {},
        data = reqParams.data || [],
        query = reqParams.query || body.query,
        jsonpCallback = reqParams.callback,
        eyeParams = {};
    
    // make sure data is an array
    if(typeof(data) === 'string')
      data = data.split(',');
    
    // add body data
    if(typeof(body.data) === 'string')
      data.push(body.data);
    else if(body.data instanceof Array)
      data.push.apply(data, body.data);
    
    // collect data and data URIs
    eyeParams.data = [];
    // inspect all data parameters in request parameters
    data.forEach(function (item) {
      if(!item.match(/^https?:\/\//))
        // item is N3 data – push it
        eyeParams.data.push(item);
      else
        // item is list of URIs – push each of them
        eyeParams.data.push.apply(eyeParams.data, item.split(','));
    });
    
    // do a reasoner pass by default
    eyeParams.pass = true;
    
    // add query if present
    if(query) {
      eyeParams.query = query;
      delete eyeParams.pass;
    }
    
    // add boolean flags
    for(var param in reqParams) {
      var eyeFlagName = eyeFlagNames[param.replace(/-/g, '').toLowerCase()];
      if(eyeFlagName)
        eyeParams[eyeFlagName] = !reqParams[param].match(/^0|false$/i);
    }

    // add debug information if requested
    if(this.settings.debug)
      eyeParams.originalUrl = req.originalUrl;

    // execute the reasoner and return result or error
    this.settings.eye.execute(eyeParams, function (error, result) {
      if(!jsonpCallback) {
        self.setDefaultHeaders(req, res);
        if(!error) {
          res.header('Content-Type', 'text/n3');
          res.send(result + '\n');
        }
        else {
          res.header('Content-Type', 'text/plain');
          res.send(error + '\n', 400);
        }
      }
      else {
        res.header('Content-Type', 'application/javascript');
        if(jsonpCallback.match(/^[\w\d-_]+$/i))
          res.send(jsonpCallback + '(' + JSON.stringify(error || result) + ')');
        else
          res.send('alert("Illegal callback name.")', 400);
      }
    });
  },
  
  handleEyeOptionsRequest: function(req, res, next) {
    this.setDefaultHeaders(req, res);
    res.header('Content-Type', 'text/plain');
    res.send('');
  },
  
  setDefaultHeaders: function(req, res) {
    res.header('X-Powered-By', 'EYE Server');
    res.header('Access-Control-Allow-Origin', '*');
  }
}

function proxy(object, method) {
  return function() { method.apply(object, arguments); };
}

module.exports = EyeServer;
{'fraction_non_alphanumeric': 0.15139879319802524, 'fraction_numerical': 0.0024684585847504115, 'mean_word_length': 2.5908419497784343, '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': '169595', 'n_tokens_mistral': 2258, 'n_tokens_neox': 2212, 'n_words': 489}