* Write chunks asynchronously. * - 'strict': Queues if buffer full but rejects if too many pending writes (>= highWaterMark) * - 'block': Waits for buffer space (unbounded pending writes) * - 'drop-*': Always succeeds (handled by writeSync) * * If signal is provided, a write blocked o
(chunks: Uint8Array[], signal?: AbortSignal)
| 235 | * is per-operation cancellation, not terminal failure. |
| 236 | */ |
| 237 | async writeAsync(chunks: Uint8Array[], signal?: AbortSignal): Promise<void> { |
| 238 | // Check for pre-aborted signal |
| 239 | if (signal?.aborted) { |
| 240 | throw signal.reason ?? new DOMException('Aborted', 'AbortError'); |
| 241 | } |
| 242 | |
| 243 | // Check if write is possible |
| 244 | if (this.writerState !== 'open') { |
| 245 | throw new TypeError('Writer is closed'); |
| 246 | } |
| 247 | if (this.consumerState !== 'active') { |
| 248 | throw this.consumerState === 'thrown' && this.error |
| 249 | ? this.error |
| 250 | : new TypeError('Stream closed by consumer'); |
| 251 | } |
| 252 | |
| 253 | // Try sync first |
| 254 | if (this.writeSync(chunks)) { |
| 255 | return; |
| 256 | } |
| 257 | |
| 258 | // Buffer is full - handle based on policy |
| 259 | switch (this.backpressure) { |
| 260 | case 'strict': |
| 261 | // In strict mode, highWaterMark limits pendingWrites (the "hose") |
| 262 | // If too many writes are already pending, caller is ignoring backpressure |
| 263 | if (this.pendingWrites.length >= this.highWaterMark) { |
| 264 | throw new RangeError( |
| 265 | 'Backpressure violation: too many pending writes. ' + |
| 266 | 'Await each write() call to respect backpressure.' |
| 267 | ); |
| 268 | } |
| 269 | // Otherwise, queue this write and wait for space |
| 270 | return this.createPendingWrite(chunks, signal); |
| 271 | |
| 272 | case 'block': |
| 273 | // Wait for space (unbounded pending writes) |
| 274 | return this.createPendingWrite(chunks, signal); |
| 275 | |
| 276 | default: |
| 277 | // This shouldn't happen - writeSync handles drop-* policies |
| 278 | throw new Error('Unexpected: writeSync should have handled non-strict policy'); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Create a pending write promise, optionally racing against a signal. |
no test coverage detected