Skip to content

Commit c84d55c

Browse files
committed
fixup
1 parent ec8d7c1 commit c84d55c

File tree

1 file changed

+47
-70
lines changed

1 file changed

+47
-70
lines changed

lib/client.js

Lines changed: 47 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ const {
7373
kMaxResponseSize,
7474
kHTTPConnVersion,
7575
// HTTP2
76-
kHost,
7776
kHTTP2SessionState,
7877
kHTTP2BuildRequest,
7978
kHTTP2CopyHeaders,
@@ -285,7 +284,6 @@ class Client extends DispatcherBase {
285284
openStreams: 0, // Keep track of them to decide whether or not unref the session
286285
maxConcurrentStreams: maxConcurrentStreams != null ? maxConcurrentStreams : 100 // Max peerConcurrentStreams for a Node h2 server
287286
}
288-
this[kHost] = `${this[kUrl].hostname}${this[kUrl].port ? `:${this[kUrl].port}` : ''}`
289287

290288
// kQueue is built up of 3 sections separated by
291289
// the kRunningIdx and kPendingIdx indices.
@@ -395,8 +393,6 @@ class Client extends DispatcherBase {
395393
resolve()
396394
}
397395

398-
this[kHTTP2SessionState] = null
399-
400396
if (this[kSocket]) {
401397
util.destroy(this[kSocket].on('close', callback), err)
402398
} else {
@@ -408,63 +404,6 @@ class Client extends DispatcherBase {
408404
}
409405
}
410406

411-
function onHttp2SessionError (err) {
412-
assert(err.code !== 'ERR_TLS_CERT_ALTNAME_INVALID')
413-
414-
this[kSocket][kError] = err
415-
416-
onError(this[kClient], err)
417-
}
418-
419-
function onHttp2FrameError (type, code, id) {
420-
const err = new InformationalError(`HTTP/2: "frameError" received - type ${type}, code ${code}`)
421-
422-
if (id === 0) {
423-
this[kSocket][kError] = err
424-
onError(this[kClient], err)
425-
}
426-
}
427-
428-
function onHttp2SessionEnd () {
429-
util.destroy(this, new SocketError('other side closed'))
430-
util.destroy(this[kSocket], new SocketError('other side closed'))
431-
}
432-
433-
function onHTTP2GoAway (code) {
434-
const client = this[kClient]
435-
const err = new InformationalError(`HTTP/2: "GOAWAY" frame received with code ${code}`)
436-
client[kSocket] = null
437-
438-
if (client.destroyed) {
439-
assert(this[kPending] === 0)
440-
441-
// Fail entire queue.
442-
const requests = client[kQueue].splice(client[kRunningIdx])
443-
for (let i = 0; i < requests.length; i++) {
444-
const request = requests[i]
445-
errorRequest(this, request, err)
446-
}
447-
} else if (client[kRunning] > 0) {
448-
// Fail head of pipeline.
449-
const request = client[kQueue][client[kRunningIdx]]
450-
client[kQueue][client[kRunningIdx]++] = null
451-
452-
errorRequest(client, request, err)
453-
}
454-
455-
client[kPendingIdx] = client[kRunningIdx]
456-
457-
assert(client[kRunning] === 0)
458-
459-
client.emit('disconnect',
460-
client[kUrl],
461-
[client],
462-
err
463-
)
464-
465-
resume(client)
466-
}
467-
468407
const constants = require('./llhttp/constants')
469408
const createRedirectInterceptor = require('./interceptor/redirectInterceptor')
470409
const EMPTY_BUF = Buffer.alloc(0)
@@ -532,8 +471,7 @@ async function lazyllhttp () {
532471
}
533472

534473
let llhttpInstance = null
535-
let llhttpPromise = lazyllhttp()
536-
llhttpPromise.catch()
474+
let llhttpPromise = null
537475

538476
let currentParser = null
539477
let currentBufferRef = null
@@ -1222,18 +1160,55 @@ async function connect (client) {
12221160
})
12231161

12241162
client[kHTTPConnVersion] = 'h2'
1225-
session.on('error', onHttp2SessionError)
1226-
session.on('frameError', onHttp2FrameError)
1227-
session.on('end', onHttp2SessionEnd)
1228-
session.on('goaway', onHTTP2GoAway)
1163+
session.on('frameError', (type, code, id) => {
1164+
if (id === 0) {
1165+
const err = new InformationalError(`HTTP/2: "frameError" received - type ${type}, code ${code}`)
1166+
util.destroy(session, err)
1167+
util.destroy(socket, err)
1168+
}
1169+
})
1170+
session.on('end', () => {
1171+
const err = new SocketError('other side closed')
1172+
util.destroy(session, err)
1173+
util.destroy(socket, err)
1174+
})
1175+
session.on('goaway', (code) => {
1176+
const client = this[kClient]
1177+
const err = new InformationalError(`HTTP/2: "GOAWAY" frame received with code ${code}`)
1178+
client[kSocket] = null
1179+
1180+
if (client.destroyed) {
1181+
assert(this[kPending] === 0)
1182+
1183+
// Fail entire queue.
1184+
const requests = client[kQueue].splice(client[kRunningIdx])
1185+
for (let i = 0; i < requests.length; i++) {
1186+
const request = requests[i]
1187+
errorRequest(this, request, err)
1188+
}
1189+
} else if (client[kRunning] > 0) {
1190+
// Fail head of pipeline.
1191+
const request = client[kQueue][client[kRunningIdx]]
1192+
client[kQueue][client[kRunningIdx]++] = null
1193+
1194+
errorRequest(client, request, err)
1195+
}
1196+
1197+
client[kPendingIdx] = client[kRunningIdx]
1198+
1199+
assert(client[kRunning] === 0)
1200+
1201+
client.emit('disconnect', client[kUrl], [client], err)
1202+
1203+
resume(client)
1204+
})
12291205
session.on('close', onSocketClose)
12301206
session.unref()
12311207

12321208
client[kSocket] = session
12331209
} else {
12341210
if (!llhttpInstance) {
1235-
llhttpInstance = await llhttpPromise
1236-
llhttpPromise = null
1211+
llhttpInstance = await (llhttpPromise ??= lazyllhttp())
12371212
}
12381213

12391214
socket[kNoRef] = false
@@ -1665,7 +1640,9 @@ function writeH2 (client, session, request) {
16651640
let stream
16661641
const h2State = client[kHTTP2SessionState]
16671642

1668-
headers[HTTP2_HEADER_AUTHORITY] = host || client[kHost]
1643+
const { hostname, port } = client[kUrl]
1644+
1645+
headers[HTTP2_HEADER_AUTHORITY] = host || `${hostname}${port ? `:${port}` : ''}`
16691646
headers[HTTP2_HEADER_METHOD] = method
16701647

16711648
try {

0 commit comments

Comments
 (0)