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

Hono · all subjects

helpers/streaming

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

stream() helper for streaming responses

The stream() helper returns a simple streaming response as a Response object. It takes a context object and an async callback function that receives a stream object. The callback can call stream.write() to write a Uint8Array, stream.pipe() to pipe a readable stream, and stream.onAbort() to register a handler when the stream is aborted.

streamText() helper with chunked encoding

The streamText() helper returns a streaming response with Content-Type: text/plain, Transfer-Encoding: chunked, and X-Content-Type-Options: nosniff headers. The async callback function receives a stream object with methods: stream.writeln() to write text with a newline character, stream.write() to write text without a newline, and stream.sleep() to pause for a specified number of milliseconds.

streamSSE() for Server-Sent Events

The streamSSE() helper allows streaming Server-Sent Events. The async callback receives a stream object with stream.writeSSE() method that accepts an object with data, event, and id properties. The stream.aborted property indicates whether the stream has been aborted.

Streaming helper error handling with optional handler

All streaming helpers accept an optional third argument as an error handler function with signature (err, stream) => void. If the error handler is not specified, errors are output to the console. The stream is automatically closed after the callbacks are executed.

Streaming helper errors do not trigger onError hook

If the callback function of a streaming helper throws an error, the onError event of Hono will not be triggered. This is because onError is a hook to handle errors before the response is sent and overwrite the response, but when the callback is executed, the stream has already started and cannot be overwritten.

Import streaming helpers from hono/streaming

Streaming helpers are imported from 'hono/streaming': stream, streamText, and streamSSE.

streamSSE writeSSE() method structure

The stream.writeSSE() method accepts an object with three properties: data (the message content), event (the event name), and id (a string identifier for the event).

Give your agent this brain