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

Function write

lib/internal/streams/iter/classic.js:578–618  ·  view source on GitHub ↗
(chunk)

Source from the content-addressed store, hash-verified

576 // Classic stream.Writable has no per-write abort signal support;
577 // cancellation should be handled at the pipeline level instead.
578 write(chunk) {
579 if (!isWritable()) {
580 return PromiseReject(new ERR_STREAM_WRITE_AFTER_END());
581 }
582
583 let bytes;
584 try {
585 bytes = toUint8Array(chunk);
586 } catch (err) {
587 return PromiseReject(err);
588 }
589
590 if (backpressure === 'strict' && isFull()) {
591 return PromiseReject(new ERR_INVALID_STATE.RangeError(
592 'Backpressure violation: buffer is full. ' +
593 'Await each write() call to respect backpressure.'));
594 }
595
596 if (backpressure === 'drop-newest' && isFull()) {
597 // Silently discard. Still count bytes for consistency with
598 // PushWriter, which counts dropped bytes in totalBytes.
599 totalBytes += TypedArrayPrototypeGetByteLength(bytes);
600 return PromiseResolve();
601 }
602
603 totalBytes += TypedArrayPrototypeGetByteLength(bytes);
604 const ok = writable.write(bytes);
605 if (ok) return PromiseResolve();
606
607 // backpressure === 'block' (or strict with room that filled on
608 // this write -- writable.write() accepted the data but returned
609 // false indicating the buffer is now at/over hwm).
610 if (backpressure === 'block') {
611 return waitForDrain();
612 }
613
614 // strict: the write was accepted (there was room before writing)
615 // but the buffer is now full. Resolve -- the *next* write will
616 // be rejected if the caller ignores backpressure.
617 return PromiseResolve();
618 },
619
620 writev(chunks) {
621 if (!ArrayIsArray(chunks)) {

Callers

nothing calls this directly

Calls 5

toUint8ArrayFunction · 0.85
isFullFunction · 0.85
isWritableFunction · 0.70
waitForDrainFunction · 0.70
writeMethod · 0.45

Tested by

no test coverage detected