From 9c11e8a1ca75c55b59c2794d3b3840f1df2e2a65 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Tue, 1 Nov 2011 23:42:45 +0100 Subject: [PATCH] net: implement Server.prototype.address() for pipes --- lib/net.js | 11 +++++++-- test/simple/test-pipe-address.js | 39 ++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 test/simple/test-pipe-address.js diff --git a/lib/net.js b/lib/net.js index ce1b8d72bb..348e25a2dc 100644 --- a/lib/net.js +++ b/lib/net.js @@ -743,7 +743,8 @@ Server.prototype.listen = function() { } else if (isPipeName(arguments[0])) { // UNIX socket or Windows pipe. - listen(self, arguments[0], -1, -1); + var pipeName = self._pipeName = arguments[0]; + listen(self, pipeName, -1, -1); } else if (typeof arguments[1] == 'undefined' || typeof arguments[1] == 'function') { @@ -764,7 +765,13 @@ Server.prototype.listen = function() { }; Server.prototype.address = function() { - return this._handle.getsockname(); + if (this._handle && this._handle.getsockname) { + return this._handle.getsockname(); + } else if (this._pipeName) { + return this._pipeName; + } else { + return null; + } }; function onconnection(clientHandle) { diff --git a/test/simple/test-pipe-address.js b/test/simple/test-pipe-address.js new file mode 100644 index 0000000000..6b29d2a2c4 --- /dev/null +++ b/test/simple/test-pipe-address.js @@ -0,0 +1,39 @@ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +var common = require('../common'); +var assert = require('assert'); +var net = require('net'); + +var address = null; + +var server = net.createServer(function() { + assert(false); // should not be called +}); + +server.listen(common.PIPE, function() { + address = server.address(); + server.close(); +}); + +process.on('exit', function() { + assert.equal(address, common.PIPE); +});