Skip to content

Commit 275ce77

Browse files
committed
refactor: write h1/h2
1 parent 747507b commit 275ce77

File tree

1 file changed

+32
-20
lines changed

1 file changed

+32
-20
lines changed

lib/dispatcher/client.js

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,9 +1178,9 @@ async function connect (client) {
11781178

11791179
const isH2 = socket.alpnProtocol === 'h2'
11801180
if (isH2) {
1181-
await onHTTP2Connect(client, socket)
1181+
await connectH2(client, socket)
11821182
} else {
1183-
await onHTTP1Connect(client, socket)
1183+
await connectH1(client, socket)
11841184
}
11851185

11861186
addListener('close', onSocketClose)
@@ -1284,6 +1284,7 @@ function _resume (client, sync) {
12841284

12851285
const socket = client[kSocket]
12861286

1287+
// TODO (refactor): Avoid http specific code here...
12871288
if (socket && !socket.destroyed && socket.alpnProtocol !== 'h2') {
12881289
if (client[kSize] === 0) {
12891290
if (!socket[kNoRef] && socket.unref) {
@@ -1385,25 +1386,34 @@ function _resume (client, sync) {
13851386
return
13861387
}
13871388

1388-
if (!request.aborted && write(client, request)) {
1389+
write(client, request)
1390+
1391+
if (!request.aborted) {
13891392
client[kPendingIdx]++
13901393
} else {
13911394
client[kQueue].splice(client[kPendingIdx], 1)
13921395
}
13931396
}
13941397
}
13951398

1399+
function write (client, request) {
1400+
if (request.aborted) {
1401+
return
1402+
}
1403+
1404+
if (client[kHTTPConnVersion] === 'h2') {
1405+
writeH2(client, request)
1406+
} else {
1407+
writeH1(client, request)
1408+
}
1409+
}
1410+
13961411
// https://www.rfc-editor.org/rfc/rfc7230#section-3.3.2
13971412
function shouldSendContentLength (method) {
13981413
return method !== 'GET' && method !== 'HEAD' && method !== 'OPTIONS' && method !== 'TRACE' && method !== 'CONNECT'
13991414
}
14001415

1401-
function write (client, request) {
1402-
if (client[kHTTPConnVersion] === 'h2') {
1403-
writeH2(client, client[kHTTP2Session], request)
1404-
return
1405-
}
1406-
1416+
function writeH1 (client, request) {
14071417
const { method, path, host, upgrade, blocking, reset } = request
14081418

14091419
let { body, headers, contentLength } = request
@@ -1465,7 +1475,7 @@ function write (client, request) {
14651475
if (shouldSendContentLength(method) && contentLength > 0 && request.contentLength !== null && request.contentLength !== contentLength) {
14661476
if (client[kStrictContentLength]) {
14671477
errorRequest(client, request, new RequestContentLengthMismatchError())
1468-
return false
1478+
return
14691479
}
14701480

14711481
process.emitWarning(new RequestContentLengthMismatchError())
@@ -1489,7 +1499,7 @@ function write (client, request) {
14891499

14901500
if (request.aborted) {
14911501
util.destroy(body)
1492-
return false
1502+
return
14931503
}
14941504

14951505
if (method === 'HEAD') {
@@ -1577,24 +1587,26 @@ function write (client, request) {
15771587
} else {
15781588
assert(false)
15791589
}
1580-
1581-
return true
15821590
}
15831591

1584-
function writeH2 (client, session, request) {
1592+
function writeH2 (client, request) {
1593+
const session = client[kHTTP2Session]
15851594
const { body, method, path, host, upgrade, expectContinue, signal, headers: reqHeaders } = request
15861595

15871596
let headers
1588-
if (typeof reqHeaders === 'string') headers = Request[kHTTP2CopyHeaders](reqHeaders.trim())
1589-
else headers = reqHeaders
1597+
if (typeof reqHeaders === 'string') {
1598+
headers = Request[kHTTP2CopyHeaders](reqHeaders.trim())
1599+
} else {
1600+
headers = reqHeaders
1601+
}
15901602

15911603
if (upgrade) {
15921604
errorRequest(client, request, new Error('Upgrade not supported for H2'))
1593-
return false
1605+
return
15941606
}
15951607

15961608
if (request.aborted) {
1597-
return false
1609+
return
15981610
}
15991611

16001612
/** @type {import('node:http2').ClientHttp2Stream} */
@@ -2266,7 +2278,7 @@ function errorRequest (client, request, err) {
22662278
}
22672279
}
22682280

2269-
async function onHTTP1Connect (client, socket) {
2281+
async function connectH1 (client, socket) {
22702282
if (!llhttpInstance) {
22712283
llhttpInstance = await llhttpPromise
22722284
llhttpPromise = null
@@ -2327,7 +2339,7 @@ async function onHTTP1Connect (client, socket) {
23272339
})
23282340
}
23292341

2330-
async function onHTTP2Connect (client, socket) {
2342+
async function connectH2 (client, socket) {
23312343
if (!h2ExperimentalWarned) {
23322344
h2ExperimentalWarned = true
23332345
process.emitWarning('H2 support is experimental, expect them to change at any time.', {

0 commit comments

Comments
 (0)