* @param {string|Uint8Array} chunk * @returns
(chunk)
| 1607 | * @returns |
| 1608 | */ |
| 1609 | write (chunk) { |
| 1610 | const { socket, request, contentLength, client, bytesWritten, expectsPayload, header } = this |
| 1611 | |
| 1612 | if (socket[kError]) { |
| 1613 | throw socket[kError] |
| 1614 | } |
| 1615 | |
| 1616 | if (socket.destroyed) { |
| 1617 | return false |
| 1618 | } |
| 1619 | |
| 1620 | const len = chunk instanceof Uint8Array ? chunk.byteLength : Buffer.byteLength(chunk) |
| 1621 | if (!len) { |
| 1622 | return true |
| 1623 | } |
| 1624 | |
| 1625 | // We should defer writing chunks. |
| 1626 | if (contentLength !== null && bytesWritten + len > contentLength) { |
| 1627 | if (client[kStrictContentLength]) { |
| 1628 | throw new RequestContentLengthMismatchError() |
| 1629 | } |
| 1630 | |
| 1631 | process.emitWarning(new RequestContentLengthMismatchError()) |
| 1632 | } |
| 1633 | |
| 1634 | socket.cork() |
| 1635 | |
| 1636 | if (bytesWritten === 0) { |
| 1637 | if (!expectsPayload && request.reset !== false) { |
| 1638 | socket[kReset] = true |
| 1639 | } |
| 1640 | |
| 1641 | if (contentLength === null) { |
| 1642 | socket.write(`${header}transfer-encoding: chunked\r\n`, 'latin1') |
| 1643 | } else { |
| 1644 | socket.write(`${header}content-length: ${contentLength}\r\n\r\n`, 'latin1') |
| 1645 | } |
| 1646 | } |
| 1647 | |
| 1648 | if (contentLength === null) { |
| 1649 | socket.write(`\r\n${len.toString(16)}\r\n`, 'latin1') |
| 1650 | } |
| 1651 | |
| 1652 | this.bytesWritten += len |
| 1653 | |
| 1654 | const ret = socket.write(chunk) |
| 1655 | |
| 1656 | socket.uncork() |
| 1657 | |
| 1658 | request.onBodySent(chunk) |
| 1659 | |
| 1660 | if (!ret) { |
| 1661 | if (socket[kParser].timeout && socket[kParser].timeoutType === TIMEOUT_HEADERS) { |
| 1662 | if (socket[kParser].timeout.refresh) { |
| 1663 | socket[kParser].timeout.refresh() |
| 1664 | } |
| 1665 | } |
| 1666 | } |
no test coverage detected