• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3
4// This test checks that calling `net.connect` internally calls
5// `Socket.prototype.connect`.
6//
7// This is important for people who monkey-patch `Socket.prototype.connect`
8// since it's not possible to monkey-patch `net.connect` directly (as the core
9// `connect` function is called internally in Node instead of calling the
10// `exports.connect` function).
11//
12// Monkey-patching of `Socket.prototype.connect` is done by - among others -
13// most APM vendors, the async-listener module and the
14// continuation-local-storage module.
15//
16// Related:
17// - https://github.com/nodejs/node/pull/12342
18// - https://github.com/nodejs/node/pull/12852
19
20const net = require('net');
21const Socket = net.Socket;
22
23// Monkey patch Socket.prototype.connect to check that it's called.
24const orig = Socket.prototype.connect;
25Socket.prototype.connect = common.mustCall(function() {
26  return orig.apply(this, arguments);
27});
28
29const server = net.createServer();
30
31server.listen(common.mustCall(function() {
32  const port = server.address().port;
33  const client = net.connect({ port }, common.mustCall(function() {
34    client.end();
35  }));
36  client.on('end', common.mustCall(function() {
37    server.close();
38  }));
39}));
40