new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Bun · Runtime · all subjects

streams

23 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

ReadableStream basic creation with enqueue

Create a ReadableStream by passing an object with a start method that receives a controller. Call controller.enqueue() to add chunks and controller.close() to finish.

ReadableStream iteration with for await

Read a ReadableStream chunk-by-chunk using for await syntax: for await (const chunk of stream) { ... }

Direct ReadableStream type

Create an optimized ReadableStream that avoids queue management by setting type: 'direct' in the constructor options. With direct streams, use controller.write() instead of controller.enqueue() to write chunks directly without queueing.

Direct ReadableStream write returns bytes written

With a direct ReadableStream, controller.write() returns the number of bytes written, or a pending Promise<number> when the destination's internal buffer is full. The chunk is accepted either way. The promise resolves once the destination has drained.

Direct ReadableStream backpressure handling

To handle backpressure in a direct ReadableStream, await the result of controller.write() when it returns a Promise. Alternatively, use await controller.flush(true) after a write returns a Promise.

Direct ReadableStream example with backpressure

const stream = new ReadableStream({ type: 'direct', async pull(controller) { for (const chunk of chunks) { await controller.write(chunk); } controller.close(); }, });

Async generator streams for Response body

Use async generator functions as a source for Response and Request. Pass an async generator function or an object with Symbol.asyncIterator to create a ReadableStream that fetches data from an asynchronous source.

Async generator with Symbol.asyncIterator

const response = new Response({ [Symbol.asyncIterator]: async function* () { yield 'hello'; yield 'world'; }, });

Async generator yield returns controller

In an async generator used for a Response body, yield returns the direct ReadableStream controller. Use it to get control over the stream, for example to call controller.end() to close the stream early.

Async generator controller end example

const response = new Response({ [Symbol.asyncIterator]: async function* () { const controller = yield 'hello'; await controller.end(); }, }); await response.text(); // 'hello'

Bun.ArrayBufferSink basic usage

Bun.ArrayBufferSink is a fast incremental writer for constructing an ArrayBuffer of unknown size. Create an instance, call write() multiple times with strings or typed arrays, then call end() to get the ArrayBuffer.

Bun.ArrayBufferSink basic example

const sink = new Bun.ArrayBufferSink(); sink.write('h'); sink.write('e'); sink.write('l'); sink.write('l'); sink.write('o'); sink.end(); // ArrayBuffer(5) [ 104, 101, 108, 108, 111 ]

Bun.ArrayBufferSink start options

Call sink.start(options) to configure Bun.ArrayBufferSink. Options: asUint8Array (boolean) returns data as Uint8Array instead of ArrayBuffer; highWaterMark (number) preallocates internal buffer size in bytes; stream (boolean) enables flushing behavior instead of end-only.

Bun.ArrayBufferSink asUint8Array option

const sink = new Bun.ArrayBufferSink(); sink.start({ asUint8Array: true }); sink.write('hello'); sink.end(); // Uint8Array(5) [ 104, 101, 108, 108, 111 ]

Bun.ArrayBufferSink write method input types

The ArrayBufferSink.write() method supports strings, typed arrays (Uint8Array, etc.), ArrayBuffer, and SharedArrayBuffer.

Bun.ArrayBufferSink flush method

Call sink.flush() to return buffered data and clear the internal buffer. Only works when start() was called with stream: true. Returns ArrayBuffer by default or Uint8Array if asUint8Array: true was set.

Bun.ArrayBufferSink stream mode example

const sink = new Bun.ArrayBufferSink(); sink.start({ stream: true }); sink.write('h'); sink.write('e'); sink.write('l'); sink.flush(); // ArrayBuffer(3) [ 104, 101, 108 ] sink.write('l'); sink.write('o'); sink.flush(); // ArrayBuffer(2) [ 108, 111 ]

Bun.ArrayBufferSink highWaterMark option

Pass highWaterMark option to start() to manually set the size of the internal buffer in bytes. Improves performance when chunk size is small.

Bun.ArrayBufferSink highWaterMark example

const sink = new Bun.ArrayBufferSink(); sink.start({ highWaterMark: 1024 * 1024 }); // 1 MB

Web API ReadableStream and WritableStream

Bun implements the Web APIs ReadableStream and WritableStream for working with binary data without loading it all into memory at once.

Default ReadableStream applies backpressure automatically

For default (non-direct) ReadableStreams and async-generator response bodies, Bun applies backpressure automatically: it pauses the producer while the destination is backed up.

Direct ReadableStream chunks delivered as Uint8Array

When a direct ReadableStream is read from JavaScript, Bun buffers the writes and delivers them as Uint8Array chunks. Strings are UTF-8 encoded.

Streams support in Bun

Bun supports ReadableStream, WritableStream, TransformStream, ByteLengthQueuingStrategy, and CountQueuingStrategy.

Give your agent this brain