Skip to content

Commit

Permalink
refactor: try to reduce amount of http2 specific logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Feb 22, 2024
1 parent 5088696 commit 92ff0ef
Showing 1 changed file with 7 additions and 17 deletions.
24 changes: 7 additions & 17 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ const {
kHTTPConnVersion,
// HTTP2
kHost,
kHTTP2Session,
kHTTP2SessionState,
kHTTP2BuildRequest,
kHTTP2CopyHeaders,
Expand Down Expand Up @@ -279,7 +278,6 @@ class Client extends DispatcherBase {
this[kHTTPConnVersion] = 'h1'

// HTTP/2
this[kHTTP2Session] = null
this[kHTTP2SessionState] = !allowH2
? null
: {
Expand Down Expand Up @@ -397,11 +395,7 @@ class Client extends DispatcherBase {
resolve()
}

if (this[kHTTP2Session] != null) {
util.destroy(this[kHTTP2Session], err)
this[kHTTP2Session] = null
this[kHTTP2SessionState] = null
}
this[kHTTP2SessionState] = null

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

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

client[kSocket] = null
client[kHTTP2Session] = null
client[kQueue][client[kRunningIdx]++] = null
client.emit('disconnect', client[kUrl], [client], new InformationalError('upgrade'))

Expand Down Expand Up @@ -1230,17 +1222,14 @@ async function connect (client) {
})

client[kHTTPConnVersion] = 'h2'
session[kClient] = client
session[kSocket] = socket
session.on('error', onHttp2SessionError)
session.on('frameError', onHttp2FrameError)
session.on('end', onHttp2SessionEnd)
session.on('goaway', onHTTP2GoAway)
session.on('close', onSocketClose)
session.unref()

client[kHTTP2Session] = session
socket[kHTTP2Session] = session
client[kSocket] = session
} else {
if (!llhttpInstance) {
llhttpInstance = await llhttpPromise
Expand All @@ -1252,6 +1241,8 @@ async function connect (client) {
socket[kReset] = false
socket[kBlocking] = false
socket[kParser] = new Parser(client, socket, llhttpInstance)

client[kSocket] = socket
}

socket[kCounter] = 0
Expand All @@ -1265,7 +1256,6 @@ async function connect (client) {
.on('end', onSocketEnd)
.on('close', onSocketClose)

client[kSocket] = socket

Check failure on line 1259 in lib/client.js

View workflow job for this annotation

GitHub Actions / lint

More than 1 blank line not allowed

Check failure on line 1259 in lib/client.js

View workflow job for this annotation

GitHub Actions / lint

More than 1 blank line not allowed
if (channels.connected.hasSubscribers) {
channels.connected.publish({
Expand Down Expand Up @@ -1475,7 +1465,7 @@ function shouldSendContentLength (method) {

function write (client, request) {
if (client[kHTTPConnVersion] === 'h2') {
writeH2(client, client[kHTTP2Session], request)
writeH2(client, client[kSocket], request)
return
}

Expand Down Expand Up @@ -1853,7 +1843,7 @@ function writeH2 (client, session, request) {
})

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

if (client[kHTTP2Session] && !client[kHTTP2Session].destroyed && !this.closed && !this.destroyed) {
if (client[kSocket] && !client[kSocket].destroyed && !this.closed && !this.destroyed) {
h2State.streams -= 1
util.destroy(stream, err)
}
Expand Down

0 comments on commit 92ff0ef

Please sign in to comment.