Skip to content

Commit

Permalink
wrap net.Socket to workaround tty FD limitation. see #103.
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj committed Mar 7, 2015
1 parent 5bc72bc commit 2d45988
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 293 deletions.
68 changes: 26 additions & 42 deletions lib/pty.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,27 @@

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

/**
* Wrap net.Socket for a workaround
*/

function Socket(options) {
if (!(this instanceof Socket)) {
return new Socket(options);
}
var tty = process.binding('tty_wrap');
var guessHandleType = tty.guessHandleType;
tty.guessHandleType = function() {
return 'PIPE';
};
net.Socket.call(this, options);
tty.guessHandleType = guessHandleType;
}

Socket.prototype.__proto__ = net.Socket.prototype;

/**
* TTY Stream
Expand All @@ -16,54 +37,17 @@ function TTYStream(fd) {
var version = process.versions.node.split('.');

// // if (!require('tty').ReadStream) {
// if (+version[0] === 0 && +version[1] < 7) {
// var net = require('net');
// this.socket = new net.Socket(fd);
// return this;
// }
if (+version[0] === 0 && +version[1] < 7) {
this.socket = new net.Socket(fd);
return this;
}

if (+version[0] === 0 && +version[1] < 12) {
var tty = require('tty');
this.socket = new tty.ReadStream(fd);
return this;
}

var fs = require('fs');
var stream = require('./stream');

this.input = new fs.WriteStream(null, {
fd: fd
});
this.output = new stream.ReadStream(null, {
fd: fd,
autoClose: false,
stopEnd: false
});

// XXX Horrible workaround because fs.ReadStream stops reading.
// Cannot .unref() this.
// setInterval(function() {
// self.output._read(self.output._readableState.highWaterMark);
// }, 20);

this.readable = true;
this.writable = true;

this.input.on('finish', function() {
self.emit('close');
});

this.output.on('finish', function() {
self.emit('close');
});

this.input.on('end', function() {
self.emit('close');
});

this.output.on('end', function() {
self.emit('close');
});
this.socket = new Socket(fd);
}

TTYStream.prototype.write = function(data) {
Expand Down
251 changes: 0 additions & 251 deletions lib/stream.js

This file was deleted.

0 comments on commit 2d45988

Please sign in to comment.