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

Function write_

lib/_http_outgoing.js:933–1018  ·  view source on GitHub ↗
(msg, chunk, encoding, callback, fromEnd)

Source from the content-addressed store, hash-verified

931}
932
933function 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;

Callers 1

_http_outgoing.jsFile · 0.85

Calls 9

isUint8ArrayFunction · 0.85
strictContentLengthFunction · 0.85
_implicitHeaderMethod · 0.80
corkMethod · 0.80
_sendMethod · 0.80
onErrorFunction · 0.70
debugFunction · 0.50
pushMethod · 0.45
toStringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…