(msg, chunk, encoding, callback, fromEnd)
| 931 | } |
| 932 | |
| 933 | function write_(msg, chunk, encoding, callback, fromEnd) { |
| 934 | if (typeof callback !== 'function') |
| 935 | callback = nop; |
| 936 | |
| 937 | if (chunk === null) { |
| 938 | throw new ERR_STREAM_NULL_VALUES(); |
| 939 | } else if (typeof chunk !== 'string' && !isUint8Array(chunk)) { |
| 940 | throw new ERR_INVALID_ARG_TYPE( |
| 941 | 'chunk', ['string', 'Buffer', 'Uint8Array'], chunk); |
| 942 | } |
| 943 | |
| 944 | let err; |
| 945 | if (msg.finished) { |
| 946 | err = new ERR_STREAM_WRITE_AFTER_END(); |
| 947 | } else if (msg.destroyed) { |
| 948 | err = new ERR_STREAM_DESTROYED('write'); |
| 949 | } |
| 950 | |
| 951 | if (err) { |
| 952 | if (!msg.destroyed) { |
| 953 | onError(msg, err, callback); |
| 954 | } else { |
| 955 | process.nextTick(callback, err); |
| 956 | } |
| 957 | return false; |
| 958 | } |
| 959 | |
| 960 | let len; |
| 961 | |
| 962 | if (msg.strictContentLength) { |
| 963 | len ??= typeof chunk === 'string' ? Buffer.byteLength(chunk, encoding) : chunk.byteLength; |
| 964 | |
| 965 | if ( |
| 966 | strictContentLength(msg) && |
| 967 | (fromEnd ? msg[kBytesWritten] + len !== msg._contentLength : msg[kBytesWritten] + len > msg._contentLength) |
| 968 | ) { |
| 969 | throw new ERR_HTTP_CONTENT_LENGTH_MISMATCH(len + msg[kBytesWritten], msg._contentLength); |
| 970 | } |
| 971 | |
| 972 | msg[kBytesWritten] += len; |
| 973 | } |
| 974 | |
| 975 | if (!msg._header) { |
| 976 | if (fromEnd) { |
| 977 | len ??= typeof chunk === 'string' ? Buffer.byteLength(chunk, encoding) : chunk.byteLength; |
| 978 | msg._contentLength = len; |
| 979 | } |
| 980 | msg._implicitHeader(); |
| 981 | } |
| 982 | |
| 983 | if (!msg._hasBody) { |
| 984 | if (msg[kRejectNonStandardBodyWrites]) { |
| 985 | throw new ERR_HTTP_BODY_NOT_ALLOWED(); |
| 986 | } else { |
| 987 | debug('This type of response MUST NOT have a body. ' + |
| 988 | 'Ignoring write() calls.'); |
| 989 | process.nextTick(callback); |
| 990 | return true; |
no test coverage detected
searching dependent graphs…