<commit_before>// sandbox.js - Rudimentary Javascript Sandbox

/*------------------------- INIT -------------------------*/
var fs = require( 'fs' )
  , path = require( 'path' )
  , spawn = require( 'child_process' ).spawn

/*------------------------- Sandbox -------------------------*/
function Sandbox( options ) {
  ( this.options = options || {} ).__proto__ = Sandbox.options

  this.run = function( code, hollaback ) {
    // Any vars in da house?
    var timer
      , stdout = ''
      , stderr = ''
      , child = spawn( this.options.node, [this.options.shovel] )
      , output = function( data ) {
          if ( !!data )
            stdout += data
        }
      , outputErr = function( data ) {
          if ( !!data )
            stderr += data
        }

    // Listen
    child.stdout.on( 'data', output )
    child.stderr.on( 'data', outputErr )

    child.on( 'exit', function( code ) {
      clearTimeout( timer )
      if (stderr) {
        var without_traces = stderr.replace(/\s{4}at(.*)(\n)*/ig, '');
        return hollaback.call( this, {
          '__error': new Error('The user script exited with an exception: \n ' + without_traces)
        });
      }
      var parsed;
      try {
        parsed = JSON.parse( stdout ) ;
      } catch (er) {
        return hollaback.call( this, {
          '__error': new Error(stdout + ' is not a valid JSON')
        });
      }
      hollaback.call( this, parsed );
    });

    // Go
    child.stdin.write( code )
    child.stdin.end()
    timer = setTimeout( function() {
      child.stdout.removeListener( 'output', output )
      stdout = JSON.stringify( { result: 'TimeoutError', console: [] } )
      child.kill( 'SIGKILL' )
    }, this.options.timeout )
  }
}

// Options
Sandbox.options =
  { timeout: 500
  , node: 'node'
  , shovel: path.join( __dirname, 'shovel.js' )
  }

// Info
fs.readFile( path.join( __dirname, '..', 'package.json' ), function( err, data ) {
  if ( err )
    throw err
  else
    Sandbox.info = JSON.parse( data )
})

/*------------------------- Export -------------------------*/
module.exports = Sandbox
<commit_msg>mitigate race condition between stdout flushing and process exit<commit_after>// sandbox.js - Rudimentary Javascript Sandbox

/*------------------------- INIT -------------------------*/
var fs = require( 'fs' )
  , path = require( 'path' )
  , spawn = require( 'child_process' ).spawn

/*------------------------- Sandbox -------------------------*/
function Sandbox( options ) {
  ( this.options = options || {} ).__proto__ = Sandbox.options

  this.run = function( code, hollaback ) {
    // Any vars in da house?
    var timer
      , stdout = ''
      , stderr = ''
      , child = spawn( this.options.node, [this.options.shovel] )
      , output = function( data ) {
          if ( !!data )
            stdout += data
        }
      , outputErr = function( data ) {
          if ( !!data )
            stderr += data
        }

    // Listen
    child.stdout.on( 'data', output )
    child.stderr.on( 'data', outputErr )

    child.on( 'close', function( code ) {
      clearTimeout( timer )
      if (stderr) {
        var without_traces = stderr.replace(/\s{4}at(.*)(\n)*/ig, '');
        return hollaback.call( this, {
          '__error': new Error('The user script exited with an exception: \n ' + without_traces)
        });
      }
      var parsed;
      try {
        parsed = JSON.parse( stdout ) ;
      } catch (er) {
        return hollaback.call( this, {
          '__error': new Error(stdout + ' is not a valid JSON')
        });
      }
      hollaback.call( this, parsed );
    });

    // Go
    child.stdin.write( code )
    child.stdin.end()
    timer = setTimeout( function() {
      child.stdout.removeListener( 'data', output )
      stdout = JSON.stringify( { result: 'TimeoutError', console: [] } )
      child.kill( 'SIGKILL' )
    }, this.options.timeout )
  }
}

// Options
Sandbox.options =
  { timeout: 500
  , node: 'node'
  , shovel: path.join( __dirname, 'shovel.js' )
  }

// Info
fs.readFile( path.join( __dirname, '..', 'package.json' ), function( err, data ) {
  if ( err )
    throw err
  else
    Sandbox.info = JSON.parse( data )
})

/*------------------------- Export -------------------------*/
module.exports = Sandbox

{'fraction_non_alphanumeric': 0.21222014925373134, 'fraction_numerical': 0.0018656716417910447, 'mean_word_length': 1.685660613650595, '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': '2075836', 'n_tokens_mistral': 1342, 'n_tokens_neox': 1309, 'n_words': 322}