Skip to main content

Class: BunResponse

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

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:108

Parameters

rawRequest

Request

Returns

BunResponse

Properties

isHead

isHead: boolean = false

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

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:106

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:96

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:123

Returns

boolean

Implementation of

HttpResponse.headersSent


statusCode

Get Signature

get statusCode(): number

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

Returns

number

Implementation of

HttpResponse.statusCode


writableEnded

Get Signature

get writableEnded(): boolean

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

Returns

boolean

Implementation of

HttpResponse.writableEnded

Methods

abortStream()

abortStream(err): void

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

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:146

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:178

Parameters

data?

any

Returns

void

Implementation of

HttpResponse.end


flushHeaders()

flushHeaders(): void

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

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:159

Parameters

key

string

Returns

string | string[] | undefined

Implementation of

HttpResponse.getHeader


json()

json(data): void

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

Parameters

data

any

Returns

void

Implementation of

HttpResponse.json


onAbort()

onAbort(callback): void

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

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:250

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:168

Parameters

data?

any

Returns

void

Implementation of

HttpResponse.send


setHeader()

setHeader(key, value): this

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

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:136

Parameters

code

number

Returns

this

Implementation of

HttpResponse.status


write()

write(data): void

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

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