-
Notifications
You must be signed in to change notification settings - Fork 30.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
net,tls: add abort signal support to connect
Add documentation for net.connect AbortSignal, and add the support to tls.connect as well
- Loading branch information
Showing
6 changed files
with
384 additions
and
0 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
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,109 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
if (!common.hasCrypto) | ||
common.skip('missing crypto'); | ||
|
||
const assert = require('assert'); | ||
const https = require('https'); | ||
const Agent = https.Agent; | ||
const fixtures = require('../common/fixtures'); | ||
|
||
const { getEventListeners } = require('events'); | ||
const agent = new Agent(); | ||
|
||
const options = { | ||
key: fixtures.readKey('agent1-key.pem'), | ||
cert: fixtures.readKey('agent1-cert.pem') | ||
}; | ||
|
||
const server = https.createServer(options); | ||
|
||
server.listen(0, common.mustCall(async () => { | ||
const port = server.address().port; | ||
const host = 'localhost'; | ||
const options = { | ||
port: port, | ||
host: host, | ||
rejectUnauthorized: false, | ||
_agentKey: agent.getName({ port, host }) | ||
}; | ||
|
||
function postCreateConnection() { | ||
return new Promise((res) => { | ||
const ac = new AbortController(); | ||
const { signal } = ac; | ||
const connection = agent.createConnection({ ...options, signal }); | ||
connection.on('error', common.mustCall((err) => { | ||
assert(err); | ||
assert.strictEqual(err.name, 'AbortError'); | ||
res(); | ||
})); | ||
assert.strictEqual(getEventListeners(signal, 'abort').length, 1); | ||
ac.abort(); | ||
}); | ||
} | ||
|
||
function preCreateConnection() { | ||
return new Promise((res) => { | ||
const ac = new AbortController(); | ||
const { signal } = ac; | ||
ac.abort(); | ||
const connection = agent.createConnection({ ...options, signal }); | ||
connection.on('error', common.mustCall((err) => { | ||
assert(err); | ||
assert.strictEqual(err.name, 'AbortError'); | ||
res(); | ||
})); | ||
}); | ||
} | ||
|
||
// Blocked on https://github.com/nodejs/node/pull/37730 | ||
// function agentAsParam() { | ||
// return new Promise((res) => { | ||
// const ac = new AbortController(); | ||
// const { signal } = ac; | ||
// const request = https.get({ | ||
// port: server.address().port, | ||
// path: '/hello', | ||
// agent: agent, | ||
// signal, | ||
// }); | ||
// request.on('error', common.mustCall((err) => { | ||
// assert(err); | ||
// assert.strictEqual(err.name, 'AbortError'); | ||
// res(); | ||
// })); | ||
// assert.strictEqual(getEventListeners(signal, 'abort').length, 1); | ||
// ac.abort(); | ||
// }); | ||
// } | ||
|
||
// Blocked on https://github.com/nodejs/node/pull/37730 | ||
// function agentAsParamPreAbort() { | ||
// return new Promise((res) => { | ||
// const ac = new AbortController(); | ||
// const { signal } = ac; | ||
// ac.abort(); | ||
// const request = https.get({ | ||
// port: server.address().port, | ||
// path: '/hello', | ||
// agent: agent, | ||
// signal, | ||
// }); | ||
// request.on('error', common.mustCall((err) => { | ||
// assert(err); | ||
// assert.strictEqual(err.name, 'AbortError'); | ||
// res(); | ||
// })); | ||
// assert.strictEqual(getEventListeners(signal, 'abort').length, 0); | ||
// }); | ||
// } | ||
|
||
await postCreateConnection(); | ||
await preCreateConnection(); | ||
// Blocked on https://github.com/nodejs/node/pull/37730 | ||
// await agentAsParam(); | ||
// Blocked on https://github.com/nodejs/node/pull/37730 | ||
// await agentAsParamPreAbort(); | ||
server.close(); | ||
})); |
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,110 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const net = require('net'); | ||
const assert = require('assert'); | ||
const server = net.createServer(); | ||
const { getEventListeners } = require('events'); | ||
|
||
const liveConnections = new Set(); | ||
|
||
server.listen(0, common.mustCall(async () => { | ||
const port = server.address().port; | ||
const host = 'localhost'; | ||
const socketOptions = (signal) => ({ port, host, signal }); | ||
server.on('connection', (connection) => { | ||
liveConnections.add(connection); | ||
connection.on('close', () => { | ||
liveConnections.delete(connection); | ||
}); | ||
}); | ||
|
||
const attachHandlers = (socket, res) => { | ||
socket.on('error', common.mustCall((err) => { | ||
assert.strictEqual(err.name, 'AbortError'); | ||
})); | ||
socket.on('close', () => { | ||
res(); | ||
}); | ||
}; | ||
|
||
function postAbort() { | ||
return new Promise((res) => { | ||
const ac = new AbortController(); | ||
const { signal } = ac; | ||
const socket = net.connect(socketOptions(signal)); | ||
attachHandlers(socket, res); | ||
assert.strictEqual(getEventListeners(signal, 'abort').length, 1); | ||
ac.abort(); | ||
}); | ||
} | ||
|
||
function preAbort() { | ||
return new Promise((res) => { | ||
const ac = new AbortController(); | ||
const { signal } = ac; | ||
ac.abort(); | ||
const socket = net.connect(socketOptions(signal)); | ||
attachHandlers(socket, res); | ||
assert.strictEqual(getEventListeners(signal, 'abort').length, 0); | ||
}); | ||
} | ||
|
||
function tickAbort() { | ||
return new Promise((res) => { | ||
const ac = new AbortController(); | ||
const { signal } = ac; | ||
setImmediate(() => ac.abort()); | ||
const socket = net.connect(socketOptions(signal)); | ||
attachHandlers(socket, res); | ||
assert.strictEqual(getEventListeners(signal, 'abort').length, 1); | ||
}); | ||
} | ||
|
||
function testConstructor() { | ||
return new Promise((res) => { | ||
const ac = new AbortController(); | ||
const { signal } = ac; | ||
ac.abort(); | ||
const socket = new net.Socket(socketOptions(signal)); | ||
assert.strictEqual(getEventListeners(signal, 'abort').length, 0); | ||
attachHandlers(socket, res); | ||
}); | ||
} | ||
|
||
function testConstructorPost() { | ||
return new Promise((res) => { | ||
const ac = new AbortController(); | ||
const { signal } = ac; | ||
const socket = new net.Socket(socketOptions(signal)); | ||
attachHandlers(socket, res); | ||
assert.strictEqual(getEventListeners(signal, 'abort').length, 1); | ||
ac.abort(); | ||
}); | ||
} | ||
|
||
function testConstructorPostTick() { | ||
return new Promise((res) => { | ||
const ac = new AbortController(); | ||
const { signal } = ac; | ||
const socket = new net.Socket(socketOptions(signal)); | ||
attachHandlers(socket, res); | ||
assert.strictEqual(getEventListeners(signal, 'abort').length, 1); | ||
setImmediate(() => { | ||
ac.abort(); | ||
}); | ||
}); | ||
} | ||
|
||
await postAbort(); | ||
await preAbort(); | ||
await tickAbort(); | ||
await testConstructor(); | ||
await testConstructorPost(); | ||
await testConstructorPostTick(); | ||
|
||
// Killing the net.socket without connecting hangs the server. | ||
for (const connection of liveConnections) { | ||
connection.destroy(); | ||
} | ||
server.close(common.mustCall()); | ||
})); |
Oops, something went wrong.