diff --git a/doc/api/diagnostics_channel.md b/doc/api/diagnostics_channel.md index f2802486c15..e71c7590f21 100644 --- a/doc/api/diagnostics_channel.md +++ b/doc/api/diagnostics_channel.md @@ -1292,9 +1292,9 @@ Emitted when a `import()` throws an error. See [`error` event][]. `net.client.socket` -* `socket` {net.Socket} +* `socket` {net.Socket|tls.TLSSocket} -Emitted when a new TCP or pipe client socket is created. +Emitted when a new TCP or pipe client socket connection is created. `net.server.socket` diff --git a/lib/net.js b/lib/net.js index 7e0788556c8..4d9cc4dd2c4 100644 --- a/lib/net.js +++ b/lib/net.js @@ -238,11 +238,6 @@ function connect(...args) { debug('createConnection', normalized); const socket = new Socket(options); - if (netClientSocketChannel.hasSubscribers) { - netClientSocketChannel.publish({ - socket, - }); - } if (options.timeout) { socket.setTimeout(options.timeout); } @@ -1238,6 +1233,12 @@ Socket.prototype.connect = function(...args) { const options = normalized[0]; const cb = normalized[1]; + if (netClientSocketChannel.hasSubscribers) { + netClientSocketChannel.publish({ + socket: this, + }); + } + if (cb !== null) { this.once('connect', cb); } diff --git a/test/parallel/test-diagnostics-channel-net-client-socket-tls.js b/test/parallel/test-diagnostics-channel-net-client-socket-tls.js new file mode 100644 index 00000000000..c887376fd28 --- /dev/null +++ b/test/parallel/test-diagnostics-channel-net-client-socket-tls.js @@ -0,0 +1,32 @@ +'use strict'; +const common = require('../common'); +if (!common.hasCrypto) + common.skip('missing crypto'); + +// This test ensures that the 'net.client.socket' diagnostics channel publishes +// a message when tls.connect() is used to create a socket connection. + +const assert = require('assert'); +const dc = require('diagnostics_channel'); +const fixtures = require('../common/fixtures'); +const tls = require('tls'); + +const options = { + key: fixtures.readKey('agent1-key.pem'), + cert: fixtures.readKey('agent1-cert.pem'), + rejectUnauthorized: false, +}; + +dc.subscribe('net.client.socket', common.mustCall(({ socket }) => { + assert.strictEqual(socket instanceof tls.TLSSocket, true); +})); + +const server = tls.createServer(options, common.mustCall((socket) => { + socket.destroy(); + server.close(); +})); + +server.listen({ port: 0 }, common.mustCall(() => { + const { port } = server.address(); + tls.connect(port, options); +})); diff --git a/test/parallel/test-diagnostics-channel-net.js b/test/parallel/test-diagnostics-channel-net.js index dc84a5b4e1d..4a959ece277 100644 --- a/test/parallel/test-diagnostics-channel-net.js +++ b/test/parallel/test-diagnostics-channel-net.js @@ -1,5 +1,6 @@ 'use strict'; const common = require('../common'); +const Countdown = require('../common/countdown'); const assert = require('assert'); const net = require('net'); const dc = require('diagnostics_channel'); @@ -18,19 +19,23 @@ function testDiagnosticChannel(subscribers, test, after) { const testSuccessfulListen = common.mustCall(() => { let cb; - const server = net.createServer(common.mustCall((socket) => { - socket.destroy(); + const netClientSocketCount = 3; + const countdown = new Countdown(netClientSocketCount, () => { server.close(); cb(); - })); + }); + const server = net.createServer(common.mustCall((socket) => { + socket.destroy(); + countdown.dec(); + }, netClientSocketCount)); dc.subscribe('net.client.socket', common.mustCall(({ socket }) => { assert.strictEqual(isNetSocket(socket), true); - })); + }, netClientSocketCount)); dc.subscribe('net.server.socket', common.mustCall(({ socket }) => { assert.strictEqual(isNetSocket(socket), true); - })); + }, netClientSocketCount)); testDiagnosticChannel( { @@ -48,8 +53,13 @@ const testSuccessfulListen = common.mustCall(() => { common.mustCall((callback) => { cb = callback; server.listen({ port: 0, customOption: true }, () => { + // All supported ways of creating a net client socket connection. const { port } = server.address(); net.connect(port); + + net.createConnection(port); + + new net.Socket().connect(port); }); }), testFailingListen