-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
net: ensure net.connect calls Socket connect
It's important for people who monkey-patch `Socket.prototype.connect` that it's called by `net.connect` since it's not possible to monkey-patch `net.connect` directly (as the `connect` function is called directly by other parts of `lib/net.js` instead of calling the `exports.connect` function). Among the actors who monkey-patch `Socket.prototype.connect` are most APM vendors, the async-listener module and the continuation-local-storage module. Related: - #12342 - #12852 PR-URL: #12861 Reviewed-By: Sam Roberts <vieuxtech@gmail.com> Reviewed-By: Luca Maraschi <luca.maraschi@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jan Krems <jan.krems@gmail.com>
- Loading branch information
1 parent
65d6249
commit 212a7a6
Showing
2 changed files
with
57 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
|
||
// This test checks that calling `net.connect` internally calls | ||
// `Socket.prototype.connect`. | ||
// | ||
// This is important for people who monkey-patch `Socket.prototype.connect` | ||
// since it's not possible to monkey-patch `net.connect` directly (as the core | ||
// `connect` function is called internally in Node instead of calling the | ||
// `exports.connect` function). | ||
// | ||
// Monkey-patching of `Socket.prototype.connect` is done by - among others - | ||
// most APM vendors, the async-listener module and the | ||
// continuation-local-storage module. | ||
// | ||
// Related: | ||
// - https://github.com/nodejs/node/pull/12342 | ||
// - https://github.com/nodejs/node/pull/12852 | ||
|
||
const net = require('net'); | ||
const Socket = net.Socket; | ||
|
||
// Monkey patch Socket.prototype.connect to check that it's called. | ||
const orig = Socket.prototype.connect; | ||
Socket.prototype.connect = common.mustCall(function() { | ||
return orig.apply(this, arguments); | ||
}); | ||
|
||
const server = net.createServer(); | ||
|
||
server.listen(common.mustCall(function() { | ||
const port = server.address().port; | ||
const client = net.connect({port}, common.mustCall(function() { | ||
client.end(); | ||
})); | ||
client.on('end', common.mustCall(function() { | ||
server.close(); | ||
})); | ||
})); |