Skip to main content

Class: BunResponse

Defined in: service-core/src/http/bun/BunAdapters.ts:84

Adapts a buffered/streamed set of response calls to the framework-agnostic HttpResponse interface, bridging them into a Fetch API Response that Bun.serve()'s fetch() handler resolves and returns.

Two modes:

  • Buffered (default): end()/json()/send() finalize the body and resolve responseReady with a single complete Response.
  • Streaming: flushHeaders()/write() construct a ReadableStream-backed Response and resolve responseReady immediately (before the body is fully known) so the client starts receiving bytes right away; further write() calls enqueue chunks, end() closes the stream.

Implements​

Indexable​

[key: string]: any

Allow arbitrary per-response properties.

Constructors​

Constructor​

new BunResponse(rawRequest): BunResponse

Defined in: service-core/src/http/bun/BunAdapters.ts:109

Parameters​

rawRequest​

Request

Returns​

BunResponse

Properties​

isHead​

isHead: boolean = false

Defined in: service-core/src/http/bun/BunAdapters.ts:95

Set to true for HEAD requests — body bytes must not be sent.


responseReady​

readonly responseReady: Promise<Response>

Defined in: service-core/src/http/bun/BunAdapters.ts:107

Resolves once headers are known — either the full buffered Response, or a streaming one.


result?​

optional result?: any

Defined in: service-core/src/http/bun/BunAdapters.ts:97

Intermediate result passed between middleware.

Implementation of​

HttpResponse.result

Accessors​

headersSent​

Get Signature​

get headersSent(): boolean

Defined in: service-core/src/http/bun/BunAdapters.ts:124

Returns​

boolean

Implementation of​

HttpResponse.headersSent


statusCode​

Get Signature​

get statusCode(): number

Defined in: service-core/src/http/bun/BunAdapters.ts:120

Returns​

number

Implementation of​

HttpResponse.statusCode


writableEnded​

Get Signature​

get writableEnded(): boolean

Defined in: service-core/src/http/bun/BunAdapters.ts:128

Returns​

boolean

Implementation of​

HttpResponse.writableEnded

Methods​

abortStream()​

abortStream(err): void

Defined in: service-core/src/http/bun/BunAdapters.ts:276

Errors the underlying stream if it's still open. Used when a middleware chain throws after streaming has already started and the Response has already been handed back to the client.

Parameters​

err​

any

Returns​

void


appendHeader()​

appendHeader(key, value): this

Defined in: service-core/src/http/bun/BunAdapters.ts:147

Adds a value for a header without clobbering any value(s) already set for the same key — the header is sent as multiple lines on the wire (e.g. multiple Set-Cookie headers).

Parameters​

key​

string

value​

string | number

Returns​

this

Implementation of​

HttpResponse.appendHeader


end()​

end(data?): void

Defined in: service-core/src/http/bun/BunAdapters.ts:179

Parameters​

data?​

any

Returns​

void

Implementation of​

HttpResponse.end


flushHeaders()​

flushHeaders(): void

Defined in: service-core/src/http/bun/BunAdapters.ts:209

Flushes status and headers to the wire immediately without ending the response, via a ReadableStream, and resolves responseReady right away so the client starts receiving bytes before the rest of the middleware chain finishes. Required before streaming data (e.g. SSE). Safe to call multiple times — only acts on the first call.

Returns​

void


getHeader()​

getHeader(key): string | string[] | undefined

Defined in: service-core/src/http/bun/BunAdapters.ts:160

Parameters​

key​

string

Returns​

string | string[] | undefined

Implementation of​

HttpResponse.getHeader


json()​

json(data): void

Defined in: service-core/src/http/bun/BunAdapters.ts:164

Parameters​

data​

any

Returns​

void

Implementation of​

HttpResponse.json


onAbort()​

onAbort(callback): void

Defined in: service-core/src/http/bun/BunAdapters.ts:241

Registers a callback to run when the client aborts the connection — either before any response was sent (via the request's AbortSignal) or mid-stream (via the ReadableStream's cancel callback).

Parameters​

callback​

() => void

Returns​

void


onFinish()​

onFinish(handler): void

Defined in: service-core/src/http/bun/BunAdapters.ts:251

Registers a callback fired exactly once when the response lifecycle ends — a normal end(), a client abort, or a streaming abort. Fires immediately if the response has already finished. Handlers run fire-and-forget (via a resolved microtask) so slow/async work (e.g. a Redis write) never delays or blocks the actual response flush.

Parameters​

handler​

() => void | Promise<void>

Returns​

void

Implementation of​

HttpResponse.onFinish


send()​

send(data?): void

Defined in: service-core/src/http/bun/BunAdapters.ts:169

Parameters​

data?​

any

Returns​

void

Implementation of​

HttpResponse.send


setHeader()​

setHeader(key, value): this

Defined in: service-core/src/http/bun/BunAdapters.ts:142

Sets a header, replacing any value(s) previously set for the same key.

Parameters​

key​

string

value​

string | number | string[]

Returns​

this

Implementation of​

HttpResponse.setHeader


status()​

status(code): this

Defined in: service-core/src/http/bun/BunAdapters.ts:137

Parameters​

code​

number

Returns​

this

Implementation of​

HttpResponse.status


write()​

write(data): void

Defined in: service-core/src/http/bun/BunAdapters.ts:230

Writes a chunk to the response without ending it (streaming / SSE). Flushes headers on the first call if they haven't been sent yet.

Parameters​

data​

string | Buffer<ArrayBufferLike>

Returns​

void