MCPcopy Index your code
hub / github.com/nodejs/node / writeIterable

Function writeIterable

deps/undici/src/lib/dispatcher/client-h2.js:1391–1447  ·  view source on GitHub ↗
(abort, h2stream, body, client, request, socket, contentLength, expectsPayload)

Source from the content-addressed store, hash-verified

1389}
1390
1391async function writeIterable (abort, h2stream, body, client, request, socket, contentLength, expectsPayload) {
1392 assert(contentLength !== 0 || client[kRunning] === 0, 'iterator body cannot be pipelined')
1393
1394 let callback = null
1395 function onDrain () {
1396 if (callback) {
1397 const cb = callback
1398 callback = null
1399 cb()
1400 }
1401 }
1402
1403 const waitForDrain = () => new Promise((resolve, reject) => {
1404 assert(callback === null)
1405
1406 if (socket[kError]) {
1407 reject(socket[kError])
1408 } else {
1409 callback = resolve
1410 }
1411 })
1412
1413 h2stream
1414 .on('close', onDrain)
1415 .on('drain', onDrain)
1416
1417 try {
1418 // It's up to the user to somehow abort the async iterable.
1419 for await (const chunk of body) {
1420 if (socket[kError]) {
1421 throw socket[kError]
1422 }
1423
1424 const res = h2stream.write(chunk)
1425 request.onBodySent(chunk)
1426 if (!res) {
1427 await waitForDrain()
1428 }
1429 }
1430
1431 h2stream.end()
1432
1433 request.onRequestSent()
1434
1435 if (!expectsPayload) {
1436 socket[kReset] = true
1437 }
1438
1439 client[kResume]()
1440 } catch (err) {
1441 abort(err)
1442 } finally {
1443 h2stream
1444 .off('close', onDrain)
1445 .off('drain', onDrain)
1446 }
1447}
1448

Callers 1

writeBodyH2Function · 0.70

Calls 9

waitForDrainFunction · 0.70
abortFunction · 0.70
onBodySentMethod · 0.65
onRequestSentMethod · 0.65
assertFunction · 0.50
onMethod · 0.45
writeMethod · 0.45
endMethod · 0.45
offMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…