Skip to content

Commit ec8d7c1

Browse files
committed
refactor: try to reduce amount of http2 specific logic
1 parent 5088696 commit ec8d7c1

File tree

1 file changed

+7
-18
lines changed

1 file changed

+7
-18
lines changed

lib/client.js

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ const {
7474
kHTTPConnVersion,
7575
// HTTP2
7676
kHost,
77-
kHTTP2Session,
7877
kHTTP2SessionState,
7978
kHTTP2BuildRequest,
8079
kHTTP2CopyHeaders,
@@ -279,7 +278,6 @@ class Client extends DispatcherBase {
279278
this[kHTTPConnVersion] = 'h1'
280279

281280
// HTTP/2
282-
this[kHTTP2Session] = null
283281
this[kHTTP2SessionState] = !allowH2
284282
? null
285283
: {
@@ -397,11 +395,7 @@ class Client extends DispatcherBase {
397395
resolve()
398396
}
399397

400-
if (this[kHTTP2Session] != null) {
401-
util.destroy(this[kHTTP2Session], err)
402-
this[kHTTP2Session] = null
403-
this[kHTTP2SessionState] = null
404-
}
398+
this[kHTTP2SessionState] = null
405399

406400
if (this[kSocket]) {
407401
util.destroy(this[kSocket].on('close', callback), err)
@@ -440,7 +434,6 @@ function onHTTP2GoAway (code) {
440434
const client = this[kClient]
441435
const err = new InformationalError(`HTTP/2: "GOAWAY" frame received with code ${code}`)
442436
client[kSocket] = null
443-
client[kHTTP2Session] = null
444437

445438
if (client.destroyed) {
446439
assert(this[kPending] === 0)
@@ -810,7 +803,6 @@ class Parser {
810803
.removeListener('close', onSocketClose)
811804

812805
client[kSocket] = null
813-
client[kHTTP2Session] = null
814806
client[kQueue][client[kRunningIdx]++] = null
815807
client.emit('disconnect', client[kUrl], [client], new InformationalError('upgrade'))
816808

@@ -1230,17 +1222,14 @@ async function connect (client) {
12301222
})
12311223

12321224
client[kHTTPConnVersion] = 'h2'
1233-
session[kClient] = client
1234-
session[kSocket] = socket
12351225
session.on('error', onHttp2SessionError)
12361226
session.on('frameError', onHttp2FrameError)
12371227
session.on('end', onHttp2SessionEnd)
12381228
session.on('goaway', onHTTP2GoAway)
12391229
session.on('close', onSocketClose)
12401230
session.unref()
12411231

1242-
client[kHTTP2Session] = session
1243-
socket[kHTTP2Session] = session
1232+
client[kSocket] = session
12441233
} else {
12451234
if (!llhttpInstance) {
12461235
llhttpInstance = await llhttpPromise
@@ -1252,6 +1241,8 @@ async function connect (client) {
12521241
socket[kReset] = false
12531242
socket[kBlocking] = false
12541243
socket[kParser] = new Parser(client, socket, llhttpInstance)
1244+
1245+
client[kSocket] = socket
12551246
}
12561247

12571248
socket[kCounter] = 0
@@ -1265,8 +1256,6 @@ async function connect (client) {
12651256
.on('end', onSocketEnd)
12661257
.on('close', onSocketClose)
12671258

1268-
client[kSocket] = socket
1269-
12701259
if (channels.connected.hasSubscribers) {
12711260
channels.connected.publish({
12721261
connectParams: {
@@ -1475,7 +1464,7 @@ function shouldSendContentLength (method) {
14751464

14761465
function write (client, request) {
14771466
if (client[kHTTPConnVersion] === 'h2') {
1478-
writeH2(client, client[kHTTP2Session], request)
1467+
writeH2(client, client[kSocket], request)
14791468
return
14801469
}
14811470

@@ -1853,7 +1842,7 @@ function writeH2 (client, session, request) {
18531842
})
18541843

18551844
stream.once('error', function (err) {
1856-
if (client[kHTTP2Session] && !client[kHTTP2Session].destroyed && !this.closed && !this.destroyed) {
1845+
if (client[kSocket] && !client[kSocket].destroyed && !this.closed && !this.destroyed) {
18571846
h2State.streams -= 1
18581847
util.destroy(stream, err)
18591848
}
@@ -1863,7 +1852,7 @@ function writeH2 (client, session, request) {
18631852
const err = new InformationalError(`HTTP/2: "frameError" received - type ${type}, code ${code}`)
18641853
errorRequest(client, request, err)
18651854

1866-
if (client[kHTTP2Session] && !client[kHTTP2Session].destroyed && !this.closed && !this.destroyed) {
1855+
if (client[kSocket] && !client[kSocket].destroyed && !this.closed && !this.destroyed) {
18671856
h2State.streams -= 1
18681857
util.destroy(stream, err)
18691858
}

0 commit comments

Comments
 (0)