Skip to content

Commit

Permalink
refactor: body write
Browse files Browse the repository at this point in the history
  • Loading branch information
metcoder95 committed May 19, 2023
1 parent d58f33e commit 3e7435c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 79 deletions.
83 changes: 5 additions & 78 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1616,87 +1616,14 @@ function writeH2 (client, session, request) {
*/
stream = session.request(headers)

stream.once('continue', () => {
// TODO[H2]: Shall we trigger an event when 100-continue operations?
// console.log('this is the body:', body)
/** @type {import('node:http2').ClientHttp2Stream} */
stream = session.request(headers)
/* istanbul ignore else: assertion */
if (!body) {
if (contentLength === 0) {
headers['content-length'] = '0'
} else {
assert(contentLength == null, 'no body must not have content length')
// Indicate no more data will be written
stream.end()
}

request.onRequestSent()
} else if (util.isBuffer(body)) {
// console.log('as buffer')
assert(contentLength === body.byteLength, 'buffer body must have content length')

headers['content-length'] = `${contentLength}`
stream.end(body)
request.onBodySent(body)
request.onRequestSent()
} else if (util.isBlobLike(body)) {
// console.log('as blob')
if (typeof body.stream === 'function') {
// @ts-ignore
writeIterable({
client,
request,
contentLength,
h2stream: stream,
expectsPayload,
body: body.stream(),
socket: client[kSocket],
header: ''
})
} else {
writeBlob({
body,
client,
request,
contentLength,
expectsPayload,
h2stream: stream,
header: '',
socket: client[kSocket]
})
}
} else if (util.isStream(body)) {
// TODO: adapt the HTTP2Stream to API compatible with current undici
// expectations from a stream
writeStream({
body,
client,
request,
contentLength,
expectsPayload,
socket: client[kSocket],
h2Stream: stream,
header: ''
})
} else if (util.isIterable(body)) {
writeIterable({
body,
client,
request,
contentLength,
expectsPayload,
header: '',
h2stream: stream,
socket: client[kSocket]
})
} else {
assert(false)
}
})
stream.once('continue', writeBodyH2)
} else {
/** @type {import('node:http2').ClientHttp2Stream} */
stream = session.request(headers)
writeBodyH2()
}

function writeBodyH2 () {
/* istanbul ignore else: assertion */
if (!body) {
// Indicate no more data will be written
Expand Down
3 changes: 2 additions & 1 deletion test/http2.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ test('Should handle h2 continue', async t => {
const responseBody = []

server.on('request', (request, response) => {
t.equal(request.headers.expect, '100-continue')
t.equal(request.headers['x-my-header'], 'foo')
t.equal(request.headers[':method'], 'POST')

Expand All @@ -77,7 +78,7 @@ test('Should handle h2 continue', async t => {
response.end('hello h2!')
})

t.plan(8)
t.plan(7)

server.listen(0)
await once(server, 'listening')
Expand Down

0 comments on commit 3e7435c

Please sign in to comment.