From 1513848f8801d7d1141c10c4960b6bfe47a53ed2 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Tue, 17 Jul 2012 15:16:23 +0200 Subject: [PATCH] net: fix Socket({ fd: 42 }) api Make the implementation match the documentation. This should work: var s = new net.Socket({ fd: 42, allowHalfOpen: true }; And now it does. --- lib/net.js | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/lib/net.js b/lib/net.js index b4934e3269c..7fe892f65dc 100644 --- a/lib/net.js +++ b/lib/net.js @@ -131,19 +131,25 @@ function Socket(options) { Stream.call(this); - if (typeof options == 'number') { - // Legacy interface. - var fd = options; - this._handle = createPipe(); - this._handle.open(fd); - this.readable = this.writable = true; - initSocketHandle(this); - } else { - // private - this._handle = options && options.handle; - initSocketHandle(this); - this.allowHalfOpen = options && options.allowHalfOpen; + switch (typeof options) { + case 'number': + options = { fd: options }; // Legacy interface. + break; + case 'undefined': + options = {}; + break; } + + if (typeof options.fd === 'undefined') { + this._handle = options && options.handle; // private + } else { + this._handle = createPipe(); + this._handle.open(options.fd); + this.readable = this.writable = true; + } + + initSocketHandle(this); + this.allowHalfOpen = options && options.allowHalfOpen; } util.inherits(Socket, Stream);