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

Function clearBuffer

lib/internal/streams/writable.js:744–796  ·  view source on GitHub ↗
(stream, state)

Source from the content-addressed store, hash-verified

742
743// If there's something in the buffer waiting, then process it.
744function clearBuffer(stream, state) {
745 if ((state[kState] & (kDestroyed | kBufferProcessing | kCorked | kBuffered | kConstructed)) !==
746 (kBuffered | kConstructed)) {
747 return;
748 }
749
750 const objectMode = (state[kState] & kObjectMode) !== 0;
751 const { [kBufferedValue]: buffered, bufferedIndex } = state;
752 const bufferedLength = buffered.length - bufferedIndex;
753
754 if (!bufferedLength) {
755 return;
756 }
757
758 let i = bufferedIndex;
759
760 state[kState] |= kBufferProcessing;
761 if (bufferedLength > 1 && stream._writev) {
762 state.pendingcb -= bufferedLength - 1;
763
764 const callback = (state[kState] & kAllNoop) !== 0 ? nop : (err) => {
765 for (let n = i; n < buffered.length; ++n) {
766 buffered[n].callback(err);
767 }
768 };
769 // Make a copy of `buffered` if it's going to be used by `callback` above,
770 // since `doWrite` will mutate the array.
771 const chunks = (state[kState] & kAllNoop) !== 0 && i === 0 ?
772 buffered : ArrayPrototypeSlice(buffered, i);
773 chunks.allBuffers = (state[kState] & kAllBuffers) !== 0;
774
775 doWrite(stream, state, true, state.length, chunks, '', callback);
776
777 resetBuffer(state);
778 } else {
779 do {
780 const { chunk, encoding, callback } = buffered[i];
781 buffered[i++] = null;
782 const len = objectMode ? 1 : chunk.length;
783 doWrite(stream, state, false, len, chunk, encoding, callback);
784 } while (i < buffered.length && (state[kState] & kWriting) === 0);
785
786 if (i === buffered.length) {
787 resetBuffer(state);
788 } else if (i > 256) {
789 buffered.splice(0, i);
790 state.bufferedIndex = 0;
791 } else {
792 state.bufferedIndex = i;
793 }
794 }
795 state[kState] &= ~kBufferProcessing;
796}
797
798Writable.prototype._write = function(chunk, encoding, cb) {
799 if (this._writev) {

Callers 2

writable.jsFile · 0.85
onwriteFunction · 0.85

Calls 3

callbackMethod · 0.80
doWriteFunction · 0.70
resetBufferFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…