<commit_before>function CollectionAPI(options) {

  var self = this;

  self._url = __meteor_bootstrap__.require('url');
  self._collections = {};
  self.options = {
    sslEnabled: false,
    listenPort: 3005,
    listenHost: undefined,
    privateKeyFile: 'privatekey.pem',
    certificateFile: 'certificate.pem'
  };
  _.extend(self.options, options || {});

  self.addCollection = function() {

  };

  self.start = function() {
    var httpServer, httpOptions;

    if (self.options.sslEnabled === true) {
      httpServer = __meteor_bootstrap__.require('https');
      var fs = __meteor_bootstrap__.require('fs');

      httpOptions = {
        key: fs.readFileSync(self.options.privateKeyFile),
        cert: fs.readFileSync(self.options.certificateFile)
      };
    } else {
      httpServer = __meteor_bootstrap__.require('http');
    }

    self._server = httpServer.createServer(httpOptions);
    self._server.addListener('request', function(request, response) { self._requestListener(request, response) });
    self._server.listen(self.options.listenPort, self.options.listenHost);

    var protocol = self.options.sslEnabled ? 'https://' : 'http://';
    var host = self.options.listenHost || 'localhost';
    console.log('Collection API running on ' +  protocol + host + ':' + self.options.listenPort);
  }

  self._requestListener = function(request, response) {
    self._request = request;
    self._response = response;

    // [1] should be the collection path variable name
    // [2] is the _id of that collection (optional)
    self._requestInfo = self._url.parse(self._request.url).pathname.split('/');
    
    // Make sure it looks like a variable name. I know this does not cover
    // ALL allowed variable names... but since I do an eval, I feel like
    // I have to check for at least something so it can be considered safe'ish.
    if (! self._requestInfo[1].match(/^[a-zA-Z_$][0-9a-zA-Z_$]*$/)) {
      return self._notFoundResponse('Invalid Collection Name');
    }

    // eval() is icky, but I don't know how else to accomplish this.
    try {
      self._requestCollection = eval(self._requestInfo[1]);
    } catch (e) {
      self._requestCollection = undefined;
    }

    // Do our best to ensure we have a Meteor.Collection object
    if (self._requestCollection === undefined || typeof(self._requestCollection) !== 'object' || self._requestCollection._collection === undefined) {
      return self._notFoundResponse('Collection Object Not Found');
    }

    return self._handleRequest();
  };

  self._handleRequest = function() {

    switch (self._request.method) {
      case 'GET':
        self._getRequest();
        break;
      case 'POST':
        self._postRequest();
        break;
      case 'PUT':
        self._putRequest();
        break;
      case 'DELETE':
        self._deleteRequest();
        break;
      default:
        self._notSupportedResponse();
    }

  }

  self._getRequest = function() {

    Fiber(function () {

      try {
        // TODO: A better way to do this?
        var collection_result = self._requestInfo[2] !== undefined
            ? self._requestCollection.find(self._requestInfo[2])
            : self._requestCollection.find();

        var records = [];
        collection_result.forEach(function (record) {<commit_msg>consistency is good<commit_after>function CollectionAPI(options) {

  var self = this;

  self._url = __meteor_bootstrap__.require('url');
  self._collections = {};
  self.options = {
    sslEnabled: false,
    listenPort: 3005,
    listenHost: undefined,
    privateKeyFile: 'privatekey.pem',
    certificateFile: 'certificate.pem'
  };
  _.extend(self.options, options || {});

  self.addCollection = function() {

  };

  self.start = function() {
    var httpServer, httpOptions;

    if (self.options.sslEnabled === true) {
      httpServer = __meteor_bootstrap__.require('https');
      var fs = __meteor_bootstrap__.require('fs');

      httpOptions = {
        key: fs.readFileSync(self.options.privateKeyFile),
        cert: fs.readFileSync(self.options.certificateFile)
      };
    } else {
      httpServer = __meteor_bootstrap__.require('http');
    }

    self._server = httpServer.createServer(httpOptions);
    self._server.addListener('request', function(request, response) { self._requestListener(request, response) });
    self._server.listen(self.options.listenPort, self.options.listenHost);

    var protocol = self.options.sslEnabled ? 'https://' : 'http://';
    var host = self.options.listenHost || 'localhost';
    console.log('Collection API running on ' +  protocol + host + ':' + self.options.listenPort);
  }

  self._requestListener = function(request, response) {
    self._request = request;
    self._response = response;

    // [1] should be the collection path variable name
    // [2] is the _id of that collection (optional)
    self._requestInfo = self._url.parse(self._request.url).pathname.split('/');
    
    // Make sure it looks like a variable name. I know this does not cover
    // ALL allowed variable names... but since I do an eval, I feel like
    // I have to check for at least something so it can be considered safe'ish.
    if (! self._requestInfo[1].match(/^[a-zA-Z_$][0-9a-zA-Z_$]*$/)) {
      return self._notFoundResponse('Invalid Collection Name');
    }

    // eval() is icky, but I don't know how else to accomplish this.
    try {
      self._requestCollection = eval(self._requestInfo[1]);
    } catch (e) {
      self._requestCollection = undefined;
    }

    // Do our best to ensure we have a Meteor.Collection object
    if (self._requestCollection === undefined || typeof(self._requestCollection) !== 'object' || self._requestCollection._collection === undefined) {
      return self._notFoundResponse('Collection Object Not Found');
    }

    return self._handleRequest();
  };

  self._handleRequest = function() {

    switch (self._request.method) {
      case 'GET':
        self._getRequest();
        break;
      case 'POST':
        self._postRequest();
        break;
      case 'PUT':
        self._putRequest();
        break;
      case 'DELETE':
        self._deleteRequest();
        break;
      default:
        self._notSupportedResponse();
    }

  }

  self._getRequest = function() {

    Fiber(function() {

      try {
        // TODO: A better way to do this?
        var collection_result = self._requestInfo[2] !== undefined
            ? self._requestCollection.find(self._requestInfo[2])
            : self._requestCollection.find();

        var records = [];
        collection_result.forEach(function(record) {
{'fraction_non_alphanumeric': 0.1291490645745323, 'fraction_numerical': 0.003621001810500905, 'mean_word_length': 2.847359257109692, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '3048848', 'n_tokens_mistral': 1923, 'n_tokens_neox': 1853, 'n_words': 455}