Permalink
Browse files

use passthrough streams to simulate child process

  • Loading branch information...
1 parent b72331e commit ead0da90194ff8271e043b30a2feefa3990acd02 @polotek committed Aug 12, 2012
Showing with 30 additions and 2 deletions.
  1. +21 −0 pass-through-stream.js
  2. +9 −2 procstreams.js
@@ -0,0 +1,21 @@
+var Stream = require('stream')
+ , util = require('util');
+
+var PassThrough = function() {
+ Stream.apply(this, arguments);
+ this.readable = this.writable = true;
+}
+util.inherits(PassThrough, Stream);
+PassThrough.prototype.write = function(data) {
+ this.emit('data', data);
+}
+PassThrough.prototype.end = function(data) {
+ if(data) { this.write(data); }
+
+ this.readable = false;
+ this.writable = false;
+
+ this.emit('end');
+}
+
+module.exports = PassThrough;
View
@@ -7,6 +7,7 @@ var slice = Array.prototype.slice
, inherits = require('inherits')
, parse = require('shell-quote').parse
, utils = require('./protochains')
+ , PassThrough = require('./pass-through-stream')
, Collector = require('data-collector-stream');
var nop = function() {}
@@ -191,12 +192,18 @@ procStream.is = function(proc) {
procStream.enhanceStream = function(stream) {
var proc = procStream.enhance(new EventEmitter(), {
stdin: stream
- , stdout: stream
- , stderr: new Stream()
+ , stdout: new PassThrough()
+ , stderr: new PassThrough()
});
+ var opts = { end: false };
+ stream.pipe(proc.stdout, opts);
+
stream.once('end', function() {
proc.emit('exit', 0, null);
+
+ proc.stdout.end();
+ proc.stderr.end();
proc.emit('close', 0, null);
});
return proc;

0 comments on commit ead0da9

Please sign in to comment.