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:
| Property | Type | Description |
|---|---|---|
capacitor | Capacitor | The underlying capacitor stream. Read back the full raw response via capacitor.createReadStream() after res.end(). |
complete | boolean | undefined | Set 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 = trueto 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.
| Property | Type | Description |
|---|---|---|
statusCode | number | undefined | HTTP status code. |
statusMessage | string | undefined | HTTP status message. |
headers | Record<string, any> | Headers | Map<string, any> | string[] | undefined | Response headers. |
chunkedEncoding | boolean | undefined | Whether to use chunked transfer encoding. |
sendDate | boolean | undefined | Whether to send the Date header automatically. |
strictContentLength | boolean | undefined | Enforce strict content-length validation. |
parsedUrl | URL | undefined | Pre-parsed URL for the paired request. |