Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ability to reject connection with ECONNREFUSED #73

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,22 @@ Mitm.prototype.connect = function connect(orig, Socket, opts, done) {

this.emit("connection", server, opts)

// Ensure connect is emitted in next ticks, otherwise it would be impossible
// to listen to it after calling Net.connect or listening to it after the
// ClientRequest emits "socket".
setTimeout(client.emit.bind(client, "connect"))
setTimeout(server.emit.bind(server, "connect"))
if (client.rejected) {
const connectionRefusedError = new Error()
connectionRefusedError.address = "127.0.0.1"
connectionRefusedError.code = "ECONNREFUSED"
connectionRefusedError.errno = -4078
connectionRefusedError.port = 9090
connectionRefusedError.syscall = "connect"
setTimeout(client.emit.bind(client, "error", connectionRefusedError))
setTimeout(client.emit.bind(client, "close", true))
viglucci marked this conversation as resolved.
Show resolved Hide resolved
} else {
// Ensure connect is emitted in next ticks, otherwise it would be impossible
// to listen to it after calling Net.connect or listening to it after the
// ClientRequest emits "socket".
setTimeout(client.emit.bind(client, "connect"))
setTimeout(server.emit.bind(server, "connect"))
}

return client
}
Expand Down
4 changes: 4 additions & 0 deletions lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ Socket.prototype = Object.create(Net.Socket.prototype, {
Socket.prototype.bypass = function() {
this.bypassed = true
}

Socket.prototype.reject = function() {
this.rejected = true
}
Loading