* Signal end of stream. Returns total bytes written. * @returns {number}
()
| 272 | * @returns {number} |
| 273 | */ |
| 274 | end() { |
| 275 | if (this.#writerState === 'errored') { |
| 276 | return -2; // Signal to reject with stored error |
| 277 | } |
| 278 | if (this.#writerState === 'closing') { |
| 279 | return -3; // Signal to PushWriter: wait for drain to complete |
| 280 | } |
| 281 | if (this.#writerState === 'closed') { |
| 282 | return this.#bytesWritten; // Idempotent |
| 283 | } |
| 284 | |
| 285 | this.#cleanup(); |
| 286 | this.#rejectPendingWrites( |
| 287 | new ERR_INVALID_STATE.TypeError('Writer closed')); |
| 288 | this.#resolvePendingDrains(false); |
| 289 | |
| 290 | // If buffer is empty, close immediately |
| 291 | if (this.#slots.length === 0) { |
| 292 | this.#writerState = 'closed'; |
| 293 | this.#resolvePendingReads(); |
| 294 | return this.#bytesWritten; |
| 295 | } |
| 296 | |
| 297 | // Buffer has data: transition to closing, defer completion until drained |
| 298 | this.#writerState = 'closing'; |
| 299 | return -3; // Signal to PushWriter: create deferred end promise |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Called by the read path when the consumer has drained all data while |
no test coverage detected