Skip to content

Commit

Permalink
refactor: apply several changes
Browse files Browse the repository at this point in the history
  • Loading branch information
metcoder95 committed Aug 25, 2023
1 parent 718c3e9 commit e44b078
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
34 changes: 19 additions & 15 deletions lib/client.js
Expand Up @@ -1063,16 +1063,18 @@ function onSocketReadable () {
}

function onSocketError (err) {
const { [kParser]: parser } = this
const { [kClient]: client, [kParser]: parser } = this

assert(err.code !== 'ERR_TLS_CERT_ALTNAME_INVALID')

// On Mac OS, we get an ECONNRESET even if there is a full body to be forwarded
// to the user.
if (err.code === 'ECONNRESET' && parser && parser.statusCode && !parser.shouldKeepAlive) {
// We treat all incoming data so for as a valid response.
parser.onMessageComplete()
return
if (client[kHTTPConnVersion] !== 'h2') {
// On Mac OS, we get an ECONNRESET even if there is a full body to be forwarded
// to the user.
if (err.code === 'ECONNRESET' && parser.statusCode && !parser.shouldKeepAlive) {
// We treat all incoming data so for as a valid response.
parser.onMessageComplete()
return
}
}

this[kError] = err
Expand Down Expand Up @@ -1101,12 +1103,14 @@ function onError (client, err) {
}

function onSocketEnd () {
const { [kParser]: parser } = this
const { [kParser]: parser, [kClient]: client } = this

if (parser && parser.statusCode && !parser.shouldKeepAlive) {
// We treat all incoming data so far as a valid response.
parser.onMessageComplete()
return
if (client[kHTTPConnVersion] !== 'h2') {
if (parser.statusCode && !parser.shouldKeepAlive) {
// We treat all incoming data so far as a valid response.
parser.onMessageComplete()
return
}
}

util.destroy(this, new SocketError('other side closed', util.getSocketInfo(this)))
Expand Down Expand Up @@ -1895,7 +1899,7 @@ function writeH2 (client, session, request) {
function writeStream ({ h2stream, body, client, request, socket, contentLength, header, expectsPayload }) {
assert(contentLength !== 0 || client[kRunning] === 0, 'stream body cannot be pipelined')

if (socket.alpnProtocol === 'h2') {
if (client[kHTTPConnVersion] === 'h2') {
// For HTTP/2, is enough to pipe the stream
const pipe = pipeline(
body,
Expand Down Expand Up @@ -2006,7 +2010,7 @@ function writeStream ({ h2stream, body, client, request, socket, contentLength,
async function writeBlob ({ h2stream, body, client, request, socket, contentLength, header, expectsPayload }) {
assert(contentLength === body.size, 'blob body must have content length')

const isH2 = socket.alpnProtocol === 'h2'
const isH2 = client[kHTTPConnVersion] === 'h2'
try {
if (contentLength != null && contentLength !== body.size) {
throw new RequestContentLengthMismatchError()
Expand Down Expand Up @@ -2060,7 +2064,7 @@ async function writeIterable ({ h2stream, body, client, request, socket, content
}
})

if (socket.alpnProtocol === 'h2') {
if (client[kHTTPConnVersion] === 'h2') {
h2stream
.on('close', onDrain)
.on('drain', onDrain)
Expand Down
12 changes: 6 additions & 6 deletions test/fetch/encoding.js
Expand Up @@ -17,10 +17,10 @@ test('content-encoding header is case-iNsENsITIve', async (t) => {
res.setHeader('Content-Encoding', contentCodings)
res.setHeader('Content-Type', 'text/plain')

gzip.pipe(brotli).pipe(res)
brotli.pipe(gzip).pipe(res)

gzip.write(text)
gzip.end()
brotli.write(text)
brotli.end()
}).listen(0)

t.teardown(server.close.bind(server))
Expand All @@ -43,10 +43,10 @@ test('response decompression according to content-encoding should be handled in
res.setHeader('Content-Encoding', contentCodings)
res.setHeader('Content-Type', 'text/plain')

deflate.pipe(gzip).pipe(res)
gzip.pipe(deflate).pipe(res)

deflate.write(text)
deflate.end()
gzip.write(text)
gzip.end()
}).listen(0)

t.teardown(server.close.bind(server))
Expand Down
4 changes: 2 additions & 2 deletions test/http2.js
Expand Up @@ -655,11 +655,11 @@ test('Should handle h2 request with body (stream)', async t => {
':status': 200
})

stream.end('hello h2!')

for await (const chunk of stream) {
requestChunks.push(chunk)
}

stream.end('hello h2!')
})

t.plan(8)
Expand Down

0 comments on commit e44b078

Please sign in to comment.