Skip to content

Commit b3d1e50

Browse files
committed
net: emit dns 'lookup' event before connect
net.connect() and net.createConnection() now emit a 'lookup' event after resolving the hostname but before connecting. Fixes #5418.
1 parent 6902f65 commit b3d1e50

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

doc/api/net.markdown

+9
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,15 @@ The amount of bytes sent.
450450

451451
`net.Socket` instances are [EventEmitter][] with the following events:
452452

453+
### Event: 'lookup'
454+
455+
Emitted after resolving the hostname but before connecting.
456+
Not applicable to UNIX sockets.
457+
458+
* `err` {Error | Null} The error object. See [dns.lookup()][].
459+
* `address` {String} The IP address.
460+
* `family` {String | Null} The address type. See [dns.lookup()][].
461+
453462
### Event: 'connect'
454463

455464
Emitted when a socket connection is successfully established.

lib/net.js

+2
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,8 @@ Socket.prototype.connect = function(options, cb) {
860860
var host = options.host;
861861
debug('connect: find host ' + host);
862862
require('dns').lookup(host, function(err, ip, addressType) {
863+
self.emit('lookup', err, ip, addressType);
864+
863865
// It's possible we were destroyed while looking this up.
864866
// XXX it would be great if we could cancel the promise returned by
865867
// the look up.

test/simple/test-net-dns-error.js

+6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ socket.on('error', function(err) {
4343
assert.equal(err.code, 'ENOTFOUND');
4444
actual_bad_connections++;
4545
});
46+
socket.on('lookup', function(err, ip, type) {
47+
assert(err instanceof Error);
48+
assert.equal(err.code, 'ENOTFOUND');
49+
assert.equal(ip, undefined);
50+
assert.equal(type, undefined);
51+
});
4652

4753
process.on('exit', function() {
4854
assert.equal(actual_bad_connections, expected_bad_connections);

test/simple/test-net-dns-lookup.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
var net = require('net');
25+
var ok = false;
26+
27+
var server = net.createServer(function(client) {
28+
client.end();
29+
server.close();
30+
});
31+
32+
server.listen(common.PORT, '127.0.0.1', function() {
33+
net.connect(common.PORT, '127.0.0.1').on('lookup', function(err, ip, type) {
34+
assert.equal(err, null);
35+
assert.equal(ip, '127.0.0.1');
36+
assert.equal(type, '4');
37+
ok = true;
38+
});
39+
});
40+
41+
process.on('exit', function() {
42+
assert(ok);
43+
});

0 commit comments

Comments
 (0)