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

Method writeAsync

lib/internal/streams/iter/push.js:193–233  ·  view source on GitHub ↗

* Write chunks asynchronously. * If signal is provided, a write blocked on backpressure will reject * immediately when the signal fires. The cancelled write is removed from * pendingWrites so it does not occupy a slot. The queue itself is NOT put * into an error state - this is per-opera

(chunks, signal)

Source from the content-addressed store, hash-verified

191 * @returns {Promise<void>}
192 */
193 async writeAsync(chunks, signal) {
194 // Check writer state before signal (spec order: state, then signal)
195 if (this.#writerState === 'closed') {
196 throw new ERR_INVALID_STATE.TypeError('Writer is closed');
197 }
198 if (this.#writerState === 'closing') {
199 throw new ERR_INVALID_STATE.TypeError('Writer is closing');
200 }
201 if (this.#writerState === 'errored') {
202 throw this.#error;
203 }
204 if (this.#consumerState !== 'active') {
205 throw this.#consumerState === 'thrown' && this.#error ?
206 this.#error :
207 new ERR_INVALID_STATE.TypeError('Stream closed by consumer');
208 }
209
210 // Check for pre-aborted signal (after state checks per spec)
211 signal?.throwIfAborted();
212
213 // Try sync first
214 if (this.writeSync(chunks)) {
215 return;
216 }
217
218 // Buffer is full
219 switch (this.#backpressure) {
220 case 'strict':
221 if (this.#pendingWrites.length >= this.#highWaterMark) {
222 throw new ERR_INVALID_STATE.RangeError(
223 'Backpressure violation: too many pending writes. ' +
224 'Await each write() call to respect backpressure.');
225 }
226 return this.#createPendingWrite(chunks, signal);
227 case 'block':
228 return this.#createPendingWrite(chunks, signal);
229 default:
230 throw new ERR_INVALID_STATE(
231 'Unexpected: writeSync should have handled non-strict policy');
232 }
233 }
234
235 /**
236 * Create a pending write promise, optionally racing against a signal.

Callers 2

writeMethod · 0.80
writevMethod · 0.80

Calls 3

writeSyncMethod · 0.95
#createPendingWriteMethod · 0.95
throwIfAbortedMethod · 0.80

Tested by

no test coverage detected