Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

net: proxy stdio streams in legacy net.Stream(fd) constructor call

  • Loading branch information...
commit 743ccb73283c05ec10325b356b797faf92257a03 1 parent 4527de8
@bnoordhuis authored
Showing with 39 additions and 10 deletions.
  1. +31 −8 lib/net_uv.js
  2. +8 −2 src/node.js
View
39 lib/net_uv.js
@@ -78,14 +78,10 @@ function Socket(options) {
stream.Stream.call(this);
if (typeof options == 'number') {
- // Legacy interface. Uncomment the following lines after
- // libuv backend is stable and API compatibile with legaacy.
- // console.error('Deprecated interface net.Socket(fd).');
- // console.trace();
- // Must support legacy interface. NPM depends on it.
- // https://github.com/isaacs/npm/blob/c7824f412f0cb59d6f55cf0bc220253c39e6029f/lib/utils/output.js#L110
- // TODO Before we can do this we need a way to open a uv_stream_t by fd.
- throw new Error("Not yet implemented")
+ console.error('Deprecated interface net.Socket(fd).');
+ console.trace();
+ wrapStdIO(this, options);
+ return;
}
// private
@@ -101,6 +97,33 @@ exports.Socket = Socket;
exports.Stream = Socket; // Legacy naming.
+function wrapStdIO(self, fd) {
+ var stream_ = [process.stdin, process.stdout, process.stderr][fd];
+ if (!stream_) {
+ throw new Error("net.Stream(fd) only supported " +
+ "for stdin, stdout and stderr");
+ }
+
+ // clean out object
+ for (var name in self.__proto__)
+ delete self[name];
+
+ // proxy stream properties
+ for (var name in stream_) {
+ (function(name) {
+ self.__defineGetter__(name, function() {
+ return stream_[name]
+ });
+
+ self.__defineSetter__(name, function(value) {
+ stream_[name] = value;
+ return value;
+ });
+ })(name);
+ }
+}
+
+
Socket.prototype.listen = function() {
var self = this;
self.on('connection', arguments[0]);
View
10 src/node.js
@@ -232,7 +232,10 @@
} else if (binding.isStdoutBlocking()) {
stdout = new fs.WriteStream(null, {fd: fd});
} else {
- stdout = new net.Stream(fd);
+ // net.Stream(fd) proxies process.stdout,
+ // don't call or you'll get unbounded recursion
+// stdout = new net.Stream(fd);
+ stdout = new tty.WriteStream(fd);
// FIXME Should probably have an option in net.Stream to create a
// stream from an existing fd which is writable only. But for now
// we'll just add this hack and set the `readable` member to false.
@@ -263,7 +266,10 @@
} else if (binding.isStdinBlocking()) {
stdin = new fs.ReadStream(null, {fd: fd});
} else {
- stdin = new net.Stream(fd);
+ // net.Stream(fd) proxies process.stdin,
+ // don't call or you'll get unbounded recursion
+// stdin = new net.Stream(fd);
+ stdin = new tty.ReadStream(fd);
stdin.readable = true;
}
Please sign in to comment.
Something went wrong with that request. Please try again.