Skip to content

Commit bb56dcc

Browse files
committed
tty/stdin: Refactor for streams2
1 parent 695abba commit bb56dcc

2 files changed

Lines changed: 43 additions & 29 deletions

File tree

‎lib/tty.js‎

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,42 +40,47 @@ exports.setRawMode = util.deprecate(function(flag) {
4040
}, 'tty.setRawMode: Use `process.stdin.setRawMode()` instead.');
4141

4242

43-
function ReadStream(fd) {
44-
if (!(this instanceof ReadStream)) return new ReadStream(fd);
45-
net.Socket.call(this, {
43+
function ReadStream(fd, options) {
44+
if (!(this instanceof ReadStream))
45+
return new ReadStream(fd, options);
46+
47+
options = util._extend({
48+
highWaterMark: 0,
49+
lowWaterMark: 0,
4650
handle: new TTY(fd, true)
47-
});
51+
}, options);
52+
53+
net.Socket.call(this, options);
4854

4955
this.readable = true;
5056
this.writable = false;
5157
this.isRaw = false;
58+
this.isTTY = true;
59+
60+
// this.read = function(orig) { return function(n) {
61+
// var ret = orig.apply(this, arguments);
62+
// console.trace('TTY read(' + n + ') -> ' + ret);
63+
// return ret;
64+
// } }(this.read);
5265
}
5366
inherits(ReadStream, net.Socket);
5467

5568
exports.ReadStream = ReadStream;
5669

57-
ReadStream.prototype.pause = function() {
58-
return net.Socket.prototype.pause.call(this);
59-
};
60-
61-
ReadStream.prototype.resume = function() {
62-
return net.Socket.prototype.resume.call(this);
63-
};
64-
6570
ReadStream.prototype.setRawMode = function(flag) {
6671
flag = !!flag;
6772
this._handle.setRawMode(flag);
6873
this.isRaw = flag;
6974
};
7075

71-
ReadStream.prototype.isTTY = true;
72-
7376

7477

7578
function WriteStream(fd) {
7679
if (!(this instanceof WriteStream)) return new WriteStream(fd);
7780
net.Socket.call(this, {
78-
handle: new TTY(fd, false)
81+
handle: new TTY(fd, false),
82+
readable: false,
83+
writable: true
7984
});
8085

8186
this.readable = false;

‎src/node.js‎

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@
140140

141141
} else {
142142
// Read all of stdin - execute it.
143-
process.stdin.resume();
144143
process.stdin.setEncoding('utf8');
145144

146145
var code = '';
@@ -497,17 +496,20 @@
497496
switch (tty_wrap.guessHandleType(fd)) {
498497
case 'TTY':
499498
var tty = NativeModule.require('tty');
500-
stdin = new tty.ReadStream(fd);
499+
stdin = new tty.ReadStream(fd, {
500+
highWaterMark: 0,
501+
lowWaterMark: 0
502+
});
501503
break;
502504

503505
case 'FILE':
504506
var fs = NativeModule.require('fs');
505-
stdin = new fs.ReadStream(null, {fd: fd});
507+
stdin = new fs.ReadStream(null, { fd: fd });
506508
break;
507509

508510
case 'PIPE':
509511
var net = NativeModule.require('net');
510-
stdin = new net.Stream(fd);
512+
stdin = new net.Stream({ fd: fd });
511513
stdin.readable = true;
512514
break;
513515

@@ -520,16 +522,23 @@
520522
stdin.fd = fd;
521523

522524
// stdin starts out life in a paused state, but node doesn't
523-
// know yet. Call pause() explicitly to unref() it.
524-
stdin.pause();
525-
526-
// when piping stdin to a destination stream,
527-
// let the data begin to flow.
528-
var pipe = stdin.pipe;
529-
stdin.pipe = function(dest, opts) {
530-
stdin.resume();
531-
return pipe.call(stdin, dest, opts);
532-
};
525+
// know yet. Explicitly to readStop() it to put it in the
526+
// not-reading state.
527+
if (stdin._handle && stdin._handle.readStop) {
528+
stdin._handle.reading = false;
529+
stdin._readableState.reading = false;
530+
stdin._handle.readStop();
531+
}
532+
533+
// if the user calls stdin.pause(), then we need to stop reading
534+
// immediately, so that the process can close down.
535+
stdin.on('pause', function() {
536+
if (!stdin._handle)
537+
return;
538+
stdin._readableState.reading = false;
539+
stdin._handle.reading = false;
540+
stdin._handle.readStop();
541+
});
533542

534543
return stdin;
535544
});

0 commit comments

Comments
 (0)