Skip to content

BunDrainWaiter

Defined in: protocol/src/bun-drain-waiter.ts:43

Singleton drain waiter for Bun sockets.

Allows multiple concurrent requests to wait for drain with a simple callback-based pattern.

new BunDrainWaiter(): BunDrainWaiter

BunDrainWaiter

get needsDrain(): boolean

Defined in: protocol/src/bun-drain-waiter.ts:80

Check if drain is needed.

boolean

clear(): void

Defined in: protocol/src/bun-drain-waiter.ts:162

Clear all pending waiters and mark the socket closed. Call this on socket close/disconnect.

Pending waiters are REJECTED (matching Node’s DrainWaiter): a sender suspended on backpressure must not “succeed” against a dead socket.

void


markNeedsDrain(): void

Defined in: protocol/src/bun-drain-waiter.ts:73

Mark that drain is needed (socket.write returned false).

void


onDrain(): void

Defined in: protocol/src/bun-drain-waiter.ts:61

Call this from the socket’s drain handler. Resolves all pending waiters.

void


waitForDrain(): Promise<void>

Defined in: protocol/src/bun-drain-waiter.ts:91

Wait for socket drain.

Call markNeedsDrain() before calling this if socket.write returned false.

Promise<void>

If socket is closed while waiting


writeAll(socket, data): Promise<void>

Defined in: protocol/src/bun-drain-waiter.ts:124

Write a buffer FULLY to a Bun socket, honoring partial writes.

socket.write() returns how many bytes were written: a partial result (including 0) means the kernel buffer is full and the unwritten tail must be re-sent after the next drain event; -1 means the socket is closed. Treating the number as a boolean drops frame tails and corrupts the wire protocol under backpressure.

Calls on the same waiter are serialized in FIFO order so concurrent senders cannot interleave bytes inside one frame. Fast path (no backpressure, no contention) costs a single write() call plus one resolved-promise hop - no extra allocation.

BunWritableSocket

Buffer

Promise<void>

If the socket closes before all bytes are written