Skip to main content

ServerResponseHost

ServerResponseHost is a factory namespace (and interface) for creating http.ServerResponse instances in tests and adapters — without a real socket. It buffers output into an in-memory capacitor stream so the full raw response can be read back after the response is finished.

import { ServerResponseHost } from '@opra/http';

ServerResponseHost produces real http.ServerResponse objects backed by a fs-capacitor writable stream. They can be passed to OPRA adapters or upgraded to HttpResponse via HttpResponse.create().


Interface properties

A ServerResponseHost instance extends http.ServerResponse with one additional property:

PropertyTypeDescription
capacitorCapacitorThe underlying capacitor stream. Read back the full raw response via capacitor.createReadStream() after res.end().
completeboolean | undefinedSet to true when the response is fully written.

Namespace functions

ServerResponseHost.create(req, init?)

ServerResponseHost.create(
req: http.IncomingMessage,
init?: ServerResponseHost.Initiator
): ServerResponseHost

Synchronously creates a ServerResponseHost for the given request. Status, headers, and other initial values can be set via init. Nothing is written to the socket until you call res.send() or res.end().

const req = IncomingMessageHost.create({ method: 'GET', url: '/' });
const res = ServerResponseHost.create(req, { statusCode: 200 });
res.setHeader('Content-Type', 'application/json');
res.end('{"ok":true}');

ServerResponseHost.from(req, init?, options?)

ServerResponseHost.from(
req: http.IncomingMessage,
init?:
| ServerResponseHost.Initiator
| string
| Buffer
| NodeJS.ReadableStream
| Blob
| Iterable<any>
| AsyncIterable<any>
| Promise<any>,
options?: { waitForBody?: boolean }
): Promise<ServerResponseHost>

Async factory. When init is a plain object or undefined, delegates to create(). When it is a string, Buffer, stream, or Blob, it is parsed as raw HTTP response bytes using HTTPParser.

  • By default resolves as soon as headers are parsed; the body continues streaming in the background.
  • Set options.waitForBody = true to wait until the full body has been received.
const rawResponse = `HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{"ok":true}`;

const res = await ServerResponseHost.from(req, rawResponse, { waitForBody: true });
// res.statusCode === 200, headers parsed, body written to capacitor

Interfaces

ServerResponseHost.Initiator

Used with create() to set initial response values from a plain object.

PropertyTypeDescription
statusCodenumber | undefinedHTTP status code.
statusMessagestring | undefinedHTTP status message.
headersRecord<string, any> | Headers | Map<string, any> | string[] | undefinedResponse headers.
chunkedEncodingboolean | undefinedWhether to use chunked transfer encoding.
sendDateboolean | undefinedWhether to send the Date header automatically.
strictContentLengthboolean | undefinedEnforce strict content-length validation.
parsedUrlURL | undefinedPre-parsed URL for the paired request.