Skip to content

Commit ecbbf35

Browse files
committed
modify
1 parent a50ed98 commit ecbbf35

5 files changed

Lines changed: 15 additions & 11 deletions

File tree

lib/core/connect.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function buildConnector ({ allowH2, maxCachedSessions, socketPath, timeout, sess
5252
const sessionCache = new SessionCache(maxCachedSessions == null ? 100 : maxCachedSessions)
5353
timeout = timeout == null ? 10e3 : timeout
5454
allowH2 = allowH2 != null ? allowH2 : false
55-
return function connect ({ hostname, host, protocol, port, servername, localAddress, httpSocket }, callback) {
55+
return function connect ({ hostname, host, protocol, port, servername, localAddress, httpSocket, signal }, callback) {
5656
let socket
5757
if (protocol === 'https:') {
5858
if (!tls) {
@@ -72,6 +72,7 @@ function buildConnector ({ allowH2, maxCachedSessions, socketPath, timeout, sess
7272
...options,
7373
servername,
7474
session,
75+
signal,
7576
localAddress,
7677
ALPNProtocols: allowH2 ? ['http/1.1', 'h2'] : ['http/1.1'],
7778
socket: httpSocket, // upgrade socket connection
@@ -92,6 +93,7 @@ function buildConnector ({ allowH2, maxCachedSessions, socketPath, timeout, sess
9293
socket = net.connect({
9394
highWaterMark: 64 * 1024, // Same as nodejs fs streams.
9495
...options,
96+
signal,
9597
localAddress,
9698
port,
9799
host: hostname
@@ -106,16 +108,11 @@ function buildConnector ({ allowH2, maxCachedSessions, socketPath, timeout, sess
106108

107109
const clearConnectTimeout = util.setupConnectTimeout(new WeakRef(socket), { timeout, hostname, port })
108110

109-
// Use close event for proper cleanup instead of monkey-patching destroy
110-
// The close event is the canonical cleanup event in Node.js and covers all scenarios
111-
socket.once('close', () => {
112-
if (clearConnectTimeout && typeof clearConnectTimeout === 'function') {
113-
clearConnectTimeout()
114-
}
115-
})
116-
117111
socket
118112
.setNoDelay(true)
113+
.once('close', function () {
114+
queueMicrotask(clearConnectTimeout)
115+
})
119116
.once(protocol === 'https:' ? 'secureConnect' : 'connect', function () {
120117
queueMicrotask(clearConnectTimeout)
121118

lib/core/request.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ class Request {
4343
expectContinue,
4444
servername,
4545
throwOnError,
46-
maxRedirections
46+
maxRedirections,
47+
signal
4748
}, handler) {
4849
if (typeof path !== 'string') {
4950
throw new InvalidArgumentError('path must be a string')
@@ -99,6 +100,8 @@ class Request {
99100

100101
this.abort = null
101102

103+
this.signal = signal instanceof AbortSignal ? signal : null
104+
102105
if (body == null) {
103106
this.body = null
104107
} else if (isStream(body)) {

lib/dispatcher/client.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ async function connect (client) {
424424
hostname,
425425
protocol,
426426
port,
427+
signal: client[kQueue]?.[0]?.signal,
427428
servername: client[kServerName],
428429
localAddress: client[kLocalAddress]
429430
}, (err, socket) => {

lib/web/fetch/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2057,6 +2057,7 @@ async function httpNetworkFetch (
20572057

20582058
return new Promise((resolve, reject) => agent.dispatch(
20592059
{
2060+
signal: request.signal,
20602061
path: url.pathname + url.search,
20612062
origin: url.origin,
20622063
method: request.method,

lib/web/fetch/request.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ class Request {
405405
// 26. If init["signal"] exists, then set signal to it.
406406
if (init.signal !== undefined) {
407407
signal = init.signal
408+
request.signal = signal
408409
}
409410

410411
// 27. Set this’s request to request.
@@ -922,7 +923,8 @@ function makeRequest (init) {
922923
url: init.urlList[0],
923924
headersList: init.headersList
924925
? new HeadersList(init.headersList)
925-
: new HeadersList()
926+
: new HeadersList(),
927+
signal: init.signal ?? null
926928
}
927929
}
928930

0 commit comments

Comments
 (0)