* @param {string|Uint8Array} chunk * @returns
(chunk)
| 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 | } |
no test coverage detected