<commit_before>/**
 * tty.js
 */

process.title = 'tty.js';

/**
 * Modules
 */

var fs = require('fs')
  , express = require('express')
  , io = require('socket.io');

var pty = require('../build/Release/pty.node');

/**
 * Parameters
 */

var shellPath = process.env.SHELL || 'sh';

// linux console support is very stable, but
// the linux console doesn't have an alternate
// screen buffer.
// var termName = 'linux';

// xterm - more robust, but less tested.
var termName = 'xterm';

// easy to support, but no color.
// var termName = 'vt100';

/**
 * Terminal
 */

var Terminal = function(process, name, cols, rows) {
  process = process || 'sh';
  name = name || 'vt100';
  cols = cols || 80;
  rows = rows || 30;

  var term = pty.fork(process, name, cols, rows);

  this.input = fs.createWriteStream(null, { fd: term.fd });
  this.output = fs.createReadStream(null, { fd: term.fd });

  this.output.setEncoding('utf8');
  this.output._read();

  this.pid = term.pid;
  this.fd = term.fd;

  this.cols = cols;
  this.rows = rows;
};

Terminal.prototype.write = function(data) {
  return this.input.write(data);
};

Terminal.prototype.pipe = function(dest, options) {
  return this.output.pipe(dest, options);
};

Terminal.prototype.on = function(type, func) {
  this.output.on(type, func);
  return this;
};

Terminal.prototype.resize = function(cols, rows) {
  this.cols = cols;
  this.rows = rows;

  pty.resize(this.fd, cols, rows);
};

Terminal.prototype.destroy = function() {
  var self = this;

  this.input.writable = false;
  this.write = function() {};
  this.writable = false;

  // Need to close the read stream so
  // node stops reading a dead file descriptor.
  // Then we can safely SIGINT the shell
  // or whatever process was in the terminal.
  // SIGKILL makes libuv throw an UNKNOWN error.
  this.output.on('close', function() {
    process.kill(self.pid, 'SIGINT');
  });

  this.output.destroy();
};

/**
 * App
 */

var app = express.createServer();

app.use(function(req, res, next) {
  var setHeader = res.setHeader;
  res.setHeader = function(name) {
    switch (name) {
      case 'Cache-Control':
      case 'Last-Modified':
      case 'ETag':
        return;
    }
    return setHeader.apply(res, arguments);
  };
  next();
});

app.use(express.static(__dirname + '/../static'));

app.listen(8080);

/**
 * Sockets
 */

io = io.listen(app);

io.configure(function() {
  io.disable('log');
});

io.sockets.on('connection', function(socket) {
  var terms = [];

  socket.on('create', create);

  // can't use this right now
  // some error handling for a bad 'init'
  socket.on('init', function() {
    // need a new terminal
    if (!terms.length) return create();

    var i = terms.length
      , term = terms[0];

    term.output.removeAllListeners();
    term.on('data', function(data) {<commit_msg>use net.Socket instead of fs.ReadStream for pty fd<commit_after>/**
 * tty.js
 */

process.title = 'tty.js';

/**
 * Modules
 */

var fs = require('fs')
  , net = require('net')
  , express = require('express')
  , io = require('socket.io');

var pty = require('../build/Release/pty.node');

/**
 * Parameters
 */

var shellPath = process.env.SHELL || 'sh';

// linux console support is very stable, but
// the linux console doesn't have an alternate
// screen buffer.
// var termName = 'linux';

// xterm - more robust, but less tested.
var termName = 'xterm';

// easy to support, but no color.
// var termName = 'vt100';

/**
 * Terminal
 */

var Terminal = function(process, name, cols, rows) {
  process = process || 'sh';
  name = name || 'vt100';
  cols = cols || 80;
  rows = rows || 30;

  var term = pty.fork(process, name, cols, rows);

  this.socket = new net.Socket(term.fd);
  this.socket.setEncoding('utf8');
  this.socket.resume();

  this.pid = term.pid;
  this.fd = term.fd;

  this.cols = cols;
  this.rows = rows;
};

Terminal.prototype.write = function(data) {
  return this.socket.write(data);
};

Terminal.prototype.pipe = function(dest, options) {
  return this.socket.pipe(dest, options);
};

Terminal.prototype.on = function(type, func) {
  this.socket.on(type, func);
  return this;
};

Terminal.prototype.resize = function(cols, rows) {
  this.cols = cols;
  this.rows = rows;

  pty.resize(this.fd, cols, rows);
};

Terminal.prototype.destroy = function() {
  var self = this;

  this.socket.writable = false;
  this.write = function() {};
  this.writable = false;

  // Need to close the read stream so
  // node stops reading a dead file descriptor.
  // Then we can safely SIGINT the shell
  // or whatever process was in the terminal.
  // SIGKILL makes libuv throw an UNKNOWN error.
  this.socket.on('close', function() {
    process.kill(self.pid, 'SIGINT');
  });

  this.socket.destroy();
};

/**
 * App
 */

var app = express.createServer();

app.use(function(req, res, next) {
  var setHeader = res.setHeader;
  res.setHeader = function(name) {
    switch (name) {
      case 'Cache-Control':
      case 'Last-Modified':
      case 'ETag':
        return;
    }
    return setHeader.apply(res, arguments);
  };
  next();
});

app.use(express.static(__dirname + '/../static'));

app.listen(8080);

/**
 * Sockets
 */

io = io.listen(app);

io.configure(function() {
  io.disable('log');
});

io.sockets.on('connection', function(socket) {
  var terms = [];

  socket.on('create', create);

  // can't use this right now
  // some error handling for a bad 'init'
  socket.on('init', function() {
    // need a new terminal
    if (!terms.length) return create();

    var i = terms.length
      , term = terms[0];

    term.socket.removeAllListeners();
    term.on('data', function(data) {
{'fraction_non_alphanumeric': 0.16887241926945473, 'fraction_numerical': 0.005646726663137463, 'mean_word_length': 3.0601719197707737, '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': '7603069', 'n_tokens_mistral': 1973, 'n_tokens_neox': 1910, 'n_words': 498}