MCPcopy
hub / github.com/nodejs/undici / write

Method write

lib/dispatcher/client-h1.js:1632–1692  ·  view source on GitHub ↗

* @param {string|Uint8Array} chunk * @returns

(chunk)

Source from the content-addressed store, hash-verified

1630 * @returns
1631 */
1632 write (chunk) {
1633 const { socket, request, contentLength, client, bytesWritten, expectsPayload, header } = this
1634
1635 if (socket[kError]) {
1636 throw socket[kError]
1637 }
1638
1639 if (socket.destroyed) {
1640 return false
1641 }
1642
1643 const len = chunk instanceof Uint8Array ? chunk.byteLength : Buffer.byteLength(chunk)
1644 if (!len) {
1645 return true
1646 }
1647
1648 // We should defer writing chunks.
1649 if (contentLength !== null && bytesWritten + len > contentLength) {
1650 if (client[kStrictContentLength]) {
1651 throw new RequestContentLengthMismatchError()
1652 }
1653
1654 process.emitWarning(new RequestContentLengthMismatchError())
1655 }
1656
1657 socket.cork()
1658
1659 if (bytesWritten === 0) {
1660 if (!expectsPayload && request.reset !== false) {
1661 socket[kReset] = true
1662 }
1663
1664 if (contentLength === null) {
1665 socket.write(`${header}transfer-encoding: chunked\r\n`, 'latin1')
1666 } else {
1667 socket.write(`${header}content-length: ${contentLength}\r\n\r\n`, 'latin1')
1668 }
1669 }
1670
1671 if (contentLength === null) {
1672 socket.write(`\r\n${len.toString(16)}\r\n`, 'latin1')
1673 }
1674
1675 this.bytesWritten += len
1676
1677 const ret = socket.write(chunk)
1678
1679 socket.uncork()
1680
1681 request.onBodySent(chunk)
1682
1683 if (!ret) {
1684 if (socket[kParser].timeout && socket[kParser].timeoutType === TIMEOUT_HEADERS) {
1685 if (socket[kParser].timeout.refresh) {
1686 socket[kParser].timeout.refresh()
1687 }
1688 }
1689 }

Callers 15

writeIterableFunction · 0.95
onDataFunction · 0.45
writeBufferFunction · 0.45
writeBlobFunction · 0.45
endMethod · 0.45
_resumeFunction · 0.45
writeBufferFunction · 0.45
writeBlobFunction · 0.45
writeIterableFunction · 0.45
onResponseDataMethod · 0.45
onResponseDataMethod · 0.45
_transformMethod · 0.45

Calls 5

corkMethod · 0.80
uncorkMethod · 0.80
refreshMethod · 0.80
onBodySentMethod · 0.65
toStringMethod · 0.45

Tested by

no test coverage detected