<commit_before>#!/usr/bin/node
// cycle.js - https://github.com/douglascrockford/JSON-js/blob/master/cycle.js
"function"!=typeof JSON.decycle&&(JSON.decycle=function(e){"use strict";var t=[],r=[];return function n(e,a){var o,i,f;if(!("object"!=typeof e||null===e||e instanceof Boolean||e instanceof Date||e instanceof Number||e instanceof RegExp||e instanceof String)){for(o=0;o<t.length;o+=1)if(t[o]===e)return{$ref:r[o]};if(t.push(e),r.push(a),"[object Array]"===Object.prototype.toString.apply(e))for(f=[],o=0;o<e.length;o+=1)f[o]=n(e[o],a+"["+o+"]");else{f={};for(i in e)Object.prototype.hasOwnProperty.call(e,i)&&(f[i]=n(e[i],a+"["+JSON.stringify(i)+"]"))}return f}return e}(e,"$")}),"function"!=typeof JSON.retrocycle&&(JSON.retrocycle=function retrocycle($){"use strict";var px=/^\$(?:\[(?:\d+|\"(?:[^\\\"\u0000-\u001f]|\\([\\\"\/bfnrt]|u[0-9a-zA-Z]{4}))*\")\])*$/;return function rez(value){var i,item,name,path;if(value&&"object"==typeof value)if("[object Array]"===Object.prototype.toString.apply(value))for(i=0;i<value.length;i+=1)item=value[i],item&&"object"==typeof item&&(path=item.$ref,"string"==typeof path&&px.test(path)?value[i]=eval(path):rez(item));else for(name in value)"object"==typeof value[name]&&(item=value[name],item&&(path=item.$ref,"string"==typeof path&&px.test(path)?value[name]=eval(path):rez(item)))}($),$});

/*
cgiNode2
Based on the work of <NAME> (<EMAIL>) - https://github.com/UeiRicho/cgi-node
*/
var vm = require('vm');
var fs = require('fs');
var url = require('url');
var Path = require('path');
var crypto = require('crypto');
var queryString = require('querystring');
var config = {
  version: '0.3',

  openTag: '<?',
  closeTag: '?>',

  scriptExtension: '.js', // Not being used yet.

  embededScriptExtensions: ['.jsml'], // Not being used yet.

  SessionCookie: 'NODE-SESSIONID',
  SessionTimeOut: 15 * 60, // 15 minutes
  SessionPath: process.env.CGI_NODE_SESSIONDIR || '/media/jason/PrimaryHDD/www/sessions/',
  logPath: '/media/jason/PrimaryHDD/www/jaszhix/'
};

function node(onFinished) {
  if (!(this instanceof node)) {
    return new node(onFinished);
  }
  //var this = this;
  node.prototype.node = this;
  /*
   This array contains all the scripts that have been included within the session.
   The script structure is: {id: <integer>, path: <string>, code: <string>, content: [<string>]};
  */
  node.prototype.__scripts = [];

  /*
   This object is created before the requested script is executed. It represents the HTTP request
   and contains all the request information. This includes the headers, URL, query string, post data
   and server information.
   
   See cgiHttpRequest for more details.
  */
  node.prototype.request = new cgiHttpRequest(onFinished);

  /*
   This object is created by the initially that implements header and write methods to send data back to the 
   client.
   
   See cgiHttpResponse for more details.
  */
  node.prototype.response = new cgiHttpResponse();

  /*
   This is the current user session object. It is automatically saved and loaded for each request.
   The session is sent through Cookies to the client which is automatically sent back and forth for every request.
   
   NOTE: this must be created after the request has been parsed.
  */
  node.prototype.session = new cgiHttpSession(this.request, this.response);

  /*
   This is an alias to the response.write method.
   Can be used directly within CGI script, example: write('Hello World')
  */
  node.prototype.write = this.response.write;
  node.prototype.json = this.response.json;
  node.prototype.ip = this.session.ipAddress;

  /*
   The node process object is made available to the scripts for any additional information they may require.
   See http://nodejs.org/documentation/api/ under "process" for more information.
  */
  node.prototype.process = process;
  node.prototype.require = require;

  /*
   Resolve the file path relative to the root of the website as defined within the configuration,
   or if not specified then the base script path.
  */
  node.prototype.mapPath = function (path) {
    var root = Path.dirname(this.request.server.path_translated.toString());
    return Path.resolve(root, path);
  };

  /*
   Executes the given file within the current context. 
   If the given file path is is a '.js' file, it is executed as is, otherwise it is assumed to be an ASP page and is parsed first.
  */
  node.prototype.include = (filePath, options)=>{
    // If options is not defined then assume UTF8 as including.
    if (options === undefined) {
      options = {
        encoding: 'utf8'
      };
    }

    // Resolve the script path.
    var path = this.mapPath(filePath);

    // Get the script file content.
    var content = fs.readFileSync(path, options);

    // If the file extension is not '.js' then parse out the different code and content sections.
    // TODO: use the configuration object to check if it is a script file or not.
    var script;
    if (Path.extname(filePath) !== config.scriptExtension) {
      script = cgiParser.script(this.__scripts.length, path, content.toString());
    }

    // Otherwise just create a new script object
    else {
      script = {
        id: this.__scripts.length,
        path: path,
        script: null,
        code: content,
        content: []
      };
    }

    // Push the script onto the global script array.
    this.__scripts.push(script);

    // Execute the script within the context.
    vm.runInNewContext(script.code, this, script.path);
  };
  /*
   This method is similar to PhpInfo(). It outputs all the HTTP request and server information and variables
   to the stream in HTML format.
  */
  node.prototype.info = ()=>{
    var drawObject = (title, object)=>{
      this.response.write('<tr><th colspan="2">' + title + '</th></tr>');
      for (var name in object) {
        var value = object[name];
        if (typeof (value) === 'function') {
          continue;
        } else if (typeof (value) === 'object') {
          var htmlValue = '<table class="NodeASPTable" border="0" style="margin: 0px">';
          for (var subName in value) {
            htmlValue += '<tr><td>' + subName + '</td><td>' + value[subName] + '</td></tr>';
          }
          value = htmlValue + '</table>';
        }

        process.stdout.write('<tr><td>' + name + '</td><td>' + value + '</td></tr>');
      }
    };

    this.response.write('<style>.Logo{ text-align: left; font-size: 36px !important; } .NodeASPTable{ font-family: arial; font-size: 12px; margin: auto; border-collapse: collapse; width: 600px} .NodeASPTable TH{ background-color: #303030; color: white; font-size: 14px; padding: 10px} .NodeASPTable TD{ padding: 5px; } .NodeASPTable TR TD:nth-child(1){ background: #d9ebb3; }</style>');
    this.response.write('<table class="NodeASPTable" border="1">');
    this.response.write('<tr><th colspan="2" class="Logo">CGI-NODE v' + config.version + '</th></tr>');

    var session = {
      id: this.session.id,
      path: this.session.path,
      ipAddress: this.session.ipAddress
    };

    drawObject('Node Versions', process.versions);
    drawObject('Configuration Variables', process.config.variables);
    drawObject('Environment', process.env);
    drawObject('Features', process.features);
    drawObject('Native Modules', process.moduleLoadList);
    drawObject('CGI Command Line Arguments', process.argv);
    drawObject('Server Variables', this.request.server);
    drawObject('HTTP Request Headers', this.request.headers);
    drawObject('HTTP Request Cookies', this.request.cookies);
    drawObject('Session', session);
    drawObject('Session Cookies', this.session.cookies);
    drawObject('Session Data', this.session.data);
    drawObject('URL Query String', this.request.query);
    drawObject('Post Form', this.request.post.form);
    drawObject('Post Files', this.request.post.files);
    drawObject('Post Parts', this.request.post.parts);

    this.response.write('</table>');
  };
  /*
  DOM Context Pass-through
  */
  node.prototype.script = (code)=>{
    return this.response.write(`<script>${code}</script>`);
  };
  node.prototype.console = {
    log: (value)=>{
      this.script(`console.log(${JSON.stringify(JSON.decycle(value))})`);
    },
    warn: (value)=>{
      this.script(`console.log(${value})`);
    }
  };<commit_msg>Added console.warn, console.error context methods<commit_after>#!/usr/bin/node
// cycle.js - https://github.com/douglascrockford/JSON-js/blob/master/cycle.js
"function"!=typeof JSON.decycle&&(JSON.decycle=function(e){"use strict";var t=[],r=[];return function n(e,a){var o,i,f;if(!("object"!=typeof e||null===e||e instanceof Boolean||e instanceof Date||e instanceof Number||e instanceof RegExp||e instanceof String)){for(o=0;o<t.length;o+=1)if(t[o]===e)return{$ref:r[o]};if(t.push(e),r.push(a),"[object Array]"===Object.prototype.toString.apply(e))for(f=[],o=0;o<e.length;o+=1)f[o]=n(e[o],a+"["+o+"]");else{f={};for(i in e)Object.prototype.hasOwnProperty.call(e,i)&&(f[i]=n(e[i],a+"["+JSON.stringify(i)+"]"))}return f}return e}(e,"$")}),"function"!=typeof JSON.retrocycle&&(JSON.retrocycle=function retrocycle($){"use strict";var px=/^\$(?:\[(?:\d+|\"(?:[^\\\"\u0000-\u001f]|\\([\\\"\/bfnrt]|u[0-9a-zA-Z]{4}))*\")\])*$/;return function rez(value){var i,item,name,path;if(value&&"object"==typeof value)if("[object Array]"===Object.prototype.toString.apply(value))for(i=0;i<value.length;i+=1)item=value[i],item&&"object"==typeof item&&(path=item.$ref,"string"==typeof path&&px.test(path)?value[i]=eval(path):rez(item));else for(name in value)"object"==typeof value[name]&&(item=value[name],item&&(path=item.$ref,"string"==typeof path&&px.test(path)?value[name]=eval(path):rez(item)))}($),$});

/*
cgiNode2
Based on the work of Uei Richo (<EMAIL>) - https://github.com/UeiRicho/cgi-node
*/
var vm = require('vm');
var fs = require('fs');
var url = require('url');
var Path = require('path');
var crypto = require('crypto');
var queryString = require('querystring');
var config = {
  version: '0.3',

  openTag: '<?',
  closeTag: '?>',

  scriptExtension: '.js', // Not being used yet.

  embededScriptExtensions: ['.jsml'], // Not being used yet.

  SessionCookie: 'NODE-SESSIONID',
  SessionTimeOut: 15 * 60, // 15 minutes
  SessionPath: process.env.CGI_NODE_SESSIONDIR || '/media/jason/PrimaryHDD/www/sessions/',
  logPath: '/media/jason/PrimaryHDD/www/jaszhix/'
};

function node(onFinished) {
  if (!(this instanceof node)) {
    return new node(onFinished);
  }
  //var this = this;
  node.prototype.node = this;
  /*
   This array contains all the scripts that have been included within the session.
   The script structure is: {id: <integer>, path: <string>, code: <string>, content: [<string>]};
  */
  node.prototype.__scripts = [];

  /*
   This object is created before the requested script is executed. It represents the HTTP request
   and contains all the request information. This includes the headers, URL, query string, post data
   and server information.
   
   See cgiHttpRequest for more details.
  */
  node.prototype.request = new cgiHttpRequest(onFinished);

  /*
   This object is created by the initially that implements header and write methods to send data back to the 
   client.
   
   See cgiHttpResponse for more details.
  */
  node.prototype.response = new cgiHttpResponse();

  /*
   This is the current user session object. It is automatically saved and loaded for each request.
   The session is sent through Cookies to the client which is automatically sent back and forth for every request.
   
   NOTE: this must be created after the request has been parsed.
  */
  node.prototype.session = new cgiHttpSession(this.request, this.response);

  /*
   This is an alias to the response.write method.
   Can be used directly within CGI script, example: write('Hello World')
  */
  node.prototype.write = this.response.write;
  node.prototype.json = this.response.json;
  node.prototype.ip = this.session.ipAddress;

  /*
   The node process object is made available to the scripts for any additional information they may require.
   See http://nodejs.org/documentation/api/ under "process" for more information.
  */
  node.prototype.process = process;
  node.prototype.require = require;

  /*
   Resolve the file path relative to the root of the website as defined within the configuration,
   or if not specified then the base script path.
  */
  node.prototype.mapPath = function (path) {
    var root = Path.dirname(this.request.server.path_translated.toString());
    return Path.resolve(root, path);
  };

  /*
   Executes the given file within the current context. 
   If the given file path is is a '.js' file, it is executed as is, otherwise it is assumed to be an ASP page and is parsed first.
  */
  node.prototype.include = (filePath, options)=>{
    // If options is not defined then assume UTF8 as including.
    if (options === undefined) {
      options = {
        encoding: 'utf8'
      };
    }

    // Resolve the script path.
    var path = this.mapPath(filePath);

    // Get the script file content.
    var content = fs.readFileSync(path, options);

    // If the file extension is not '.js' then parse out the different code and content sections.
    // TODO: use the configuration object to check if it is a script file or not.
    var script;
    if (Path.extname(filePath) !== config.scriptExtension) {
      script = cgiParser.script(this.__scripts.length, path, content.toString());
    }

    // Otherwise just create a new script object
    else {
      script = {
        id: this.__scripts.length,
        path: path,
        script: null,
        code: content,
        content: []
      };
    }

    // Push the script onto the global script array.
    this.__scripts.push(script);

    // Execute the script within the context.
    vm.runInNewContext(script.code, this, script.path);
  };
  /*
   This method is similar to PhpInfo(). It outputs all the HTTP request and server information and variables
   to the stream in HTML format.
  */
  node.prototype.info = ()=>{
    var drawObject = (title, object)=>{
      this.response.write('<tr><th colspan="2">' + title + '</th></tr>');
      for (var name in object) {
        var value = object[name];
        if (typeof (value) === 'function') {
          continue;
        } else if (typeof (value) === 'object') {
          var htmlValue = '<table class="NodeASPTable" border="0" style="margin: 0px">';
          for (var subName in value) {
            htmlValue += '<tr><td>' + subName + '</td><td>' + value[subName] + '</td></tr>';
          }
          value = htmlValue + '</table>';
        }

        process.stdout.write('<tr><td>' + name + '</td><td>' + value + '</td></tr>');
      }
    };

    this.response.write('<style>.Logo{ text-align: left; font-size: 36px !important; } .NodeASPTable{ font-family: arial; font-size: 12px; margin: auto; border-collapse: collapse; width: 600px} .NodeASPTable TH{ background-color: #303030; color: white; font-size: 14px; padding: 10px} .NodeASPTable TD{ padding: 5px; } .NodeASPTable TR TD:nth-child(1){ background: #d9ebb3; }</style>');
    this.response.write('<table class="NodeASPTable" border="1">');
    this.response.write('<tr><th colspan="2" class="Logo">CGI-NODE v' + config.version + '</th></tr>');

    var session = {
      id: this.session.id,
      path: this.session.path,
      ipAddress: this.session.ipAddress
    };

    drawObject('Node Versions', process.versions);
    drawObject('Configuration Variables', process.config.variables);
    drawObject('Environment', process.env);
    drawObject('Features', process.features);
    drawObject('Native Modules', process.moduleLoadList);
    drawObject('CGI Command Line Arguments', process.argv);
    drawObject('Server Variables', this.request.server);
    drawObject('HTTP Request Headers', this.request.headers);
    drawObject('HTTP Request Cookies', this.request.cookies);
    drawObject('Session', session);
    drawObject('Session Cookies', this.session.cookies);
    drawObject('Session Data', this.session.data);
    drawObject('URL Query String', this.request.query);
    drawObject('Post Form', this.request.post.form);
    drawObject('Post Files', this.request.post.files);
    drawObject('Post Parts', this.request.post.parts);

    this.response.write('</table>');
  };
  /*
  DOM Context Pass-through
  */
  node.prototype.script = (code)=>{
    return this.response.write(`<script>${code}</script>`);
  };
  node.prototype.console = {
    log: (value)=>{
      this.script(`console.log(${JSON.stringify(JSON.decycle(value))})`);
    },
    warn: (value)=>{
      this.script(`console.warn(${JSON.stringify(JSON.decycle(value))})`);
    },
    error: (value)=>{
      this.script(`console.error(${JSON.stringify(JSON.decycle(value))})`);
    }
  };
{'fraction_non_alphanumeric': 0.163390152640019, 'fraction_numerical': 0.0062956583714438435, 'mean_word_length': 3.7033519553072627, 'pattern_counts': {'":': 0, '<': 78, '<?xml version=': 0, '>': 85, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, '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': '5646587', 'n_tokens_mistral': 5323, 'n_tokens_neox': 5154, 'n_words': 1380}