<commit_before>/**
 * Copyright (c) 2012 <NAME>
 * MIT License
 */

(function () {
  "use strict";

  var util = require('util');
  var events = require('events');
  var child_process = require('child_process');

  var helpers = require('../helpers.js');
  var streams = helpers.core('streams');

  // predefine unattach helpers
  var supportUnattach = false; //helpers.version[0] > 1 || helpers.version[1] >= 7;
  var spawnUnattachHelper;

  // this constrcutor will spawn a new process using a option object
  function Process(settings) {
    // save arguments
    this.settings = settings;

    // build args
    this.args = [settings.file].concat(settings.args || []);

    // build exec
    this.exec = settings.exec || process.execPath;

    // build env
    this.env = helpers.mergeObject({}, settings.env || process.env);
    if (settings.options) {
      this.env.spawnOptions = JSON.stringify(settings.options);
    }

    // defaults properties
    this.closed = !!settings.close;
    this.suicide = false;
    this.pid = null;
    this.alive = false;

    // this streams will relay data from and to the process
    this.stderr = new streams.RelayStream({ paused: false });
    this.stdout = new streams.RelayStream({ paused: false });
    this.stdin = new streams.RelayStream({ paused: false });
  }
  util.inherits(Process, events.EventEmitter);
  module.exports = Process;

  // close all channels
  Process.prototype.close = function (callback) {
    this.closed = true;
    var progress = new helpers.ProgressTracker(callback);

    if (!this.process.stdin.destroyed) {
      progress.add('stdin');
      this.process.stdin.destroy();
      this.process.stdin.on('close', progress.set.bind(progress, 'stdin'));
    }

    if (!this.process.stdout.destroyed) {
      progress.add('stdout');
      this.process.stdout.destroy();
      this.process.stdout.on('close', progress.set.bind(progress, 'stdout'));
    }

    if (!this.process.stderr.destroyed) {
      progress.add('stderr');
      this.process.stderr.destroy();
      this.process.stderr.on('close', progress.set.bind(progress, 'stderr'));
    }
  };

  // pump output to write streams
  Process.prototype.pump = function (streams) {
    if (streams.stderr) {
      this.stderr.pipe(streams.stderr, {end: false});
    }

    if (streams.stdout) {
      this.stdout.pipe(streams.stdout, {end: false});
    }

    if (streams.stdin) {
      streams.stdin.pipe(this.stdin, {end: false});
    }
  };

  // spawn a new process
  Process.prototype.spawn = function (respawn) {
    var self = this;

    // use a normal spawn if unattach isn't required or supported by node
    if (!this.settings.deatached || supportUnattach) {
      this.process = child_process.spawn(this.exec, this.args, {
        env: this.env,
        unattach: !!this.settings.deatached
      });
    }

    // else we will need some layer to spawn
    else {
      this.process = spawnUnattachHelper(this);
    }

    // set PID and alive boolean
    this.pid = this.process.pid;
    this.alive = true;

    // handle process exit event
    this.process.on('exit', function () {
      var suicide = self.suicide;
      self.suicide = false;
      self.alive = false;
      self.emit('stop', suicide);
    });<commit_msg>[fix] pump death handling on windows<commit_after>/**
 * Copyright (c) 2012 <NAME>
 * MIT License
 */

(function () {
  "use strict";

  var util = require('util');
  var events = require('events');
  var child_process = require('child_process');

  var helpers = require('../helpers.js');
  var streams = helpers.core('streams');

  // predefine unattach helpers
  var supportUnattach = false; //helpers.version[0] > 1 || helpers.version[1] >= 7;
  var spawnUnattachHelper;

  // this constrcutor will spawn a new process using a option object
  function Process(settings) {
    // save arguments
    this.settings = settings;

    // build args
    this.args = [settings.file].concat(settings.args || []);

    // build exec
    this.exec = settings.exec || process.execPath;

    // build env
    this.env = helpers.mergeObject({}, settings.env || process.env);
    if (settings.options) {
      this.env.spawnOptions = JSON.stringify(settings.options);
    }

    // defaults properties
    this.closed = !!settings.close;
    this.suicide = false;
    this.pid = null;
    this.alive = false;

    // this streams will relay data from and to the process
    this.stderr = new streams.RelayStream({ paused: false });
    this.stdout = new streams.RelayStream({ paused: false });
    this.stdin = new streams.RelayStream({ paused: false });
  }
  util.inherits(Process, events.EventEmitter);
  module.exports = Process;

  // close all channels
  Process.prototype.close = function (callback) {
    this.closed = true;
    var progress = new helpers.ProgressTracker(callback);

    if (!this.process.stdin.destroyed) {
      progress.add('stdin');
      this.process.stdin.destroy();
      this.process.stdin.on('close', progress.set.bind(progress, 'stdin'));
    }

    if (!this.process.stdout.destroyed) {
      progress.add('stdout');
      this.process.stdout.destroy();
      this.process.stdout.on('close', progress.set.bind(progress, 'stdout'));
    }

    if (!this.process.stderr.destroyed) {
      progress.add('stderr');
      this.process.stderr.destroy();
      this.process.stderr.on('close', progress.set.bind(progress, 'stderr'));
    }
  };

  // pump output to write streams
  Process.prototype.pump = function (streams) {
    if (streams.stderr) {
      this.stderr.pipe(streams.stderr, {end: false});
    }

    if (streams.stdout) {
      this.stdout.pipe(streams.stdout, {end: false});
    }

    if (streams.stdin) {
      streams.stdin.pipe(this.stdin, {end: false});
    }
  };

  // spawn a new process
  Process.prototype.spawn = function (respawn) {
    var self = this;

    // use a normal spawn if unattach isn't required or supported by node
    if (!this.settings.deatached || supportUnattach) {
      this.process = child_process.spawn(this.exec, this.args, {
        env: this.env,
        unattach: !!this.settings.deatached
      });
    }

    // else we will need some layer to spawn
    else {
      this.process = spawnUnattachHelper(this);
    }

    // set PID and alive boolean
    this.pid = this.process.pid;
    this.alive = true;

    var forceClose = function (channel) {
      var stdout = setTimeout(function () {
        self.process[channel].destroy();
      }, 200);
      self.process[channel].once('close', function () {
        clearTimeout(stdout);
      });
    }

    // when the process dies (before the channels is closed)
    var internalHandle = this.process._internal.onexit;
    this.process._internal.onexit = function () {

      // force destroy channels after 200 ms, if they didn't close them self
      forceClose('stdin');
      forceClose('stdout');
      forceClose('stderr');

      return internalHandle.apply(self.process._internal, arguments);
    };

    // handle process exit event
    this.process.on('exit', function () {
      var suicide = self.suicide;
      self.suicide = false;
      self.alive = false;
      self.emit('stop', suicide);
    });
{'fraction_non_alphanumeric': 0.14105233930306815, 'fraction_numerical': 0.0030542829376648617, 'mean_word_length': 2.7717277486910996, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '765890', 'n_tokens_mistral': 2217, 'n_tokens_neox': 2157, 'n_words': 519}