Skip to content
Browse files

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

  • Loading branch information...
1 parent 4527de8 commit 2fcab858f219c7d23cc8872c782b9ef4074b6625 @bnoordhuis committed Sep 9, 2011
Showing with 37 additions and 8 deletions.
  1. +37 −8 lib/net_uv.js
View
45 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,39 @@ exports.Socket = Socket;
exports.Stream = Socket; // Legacy naming.
+function wrapStdIO(self, fd) {
+ if (fd < 0 || fd > 2) {
+ throw new Error("net.Stream(fd) is only supported " +
+ "for stdin, stdout and stderr");
+ }
+
+ var stream_ = require('net_legacy').Stream(fd);
+
+ if (fd == 0)
+ stream_._writeWatcher.stop();
+ else
+ stream_._readWatcher.stop();
+
+ // 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]);

0 comments on commit 2fcab85

Please sign in to comment.
Something went wrong with that request. Please try again.