IncomingMessageHost
IncomingMessageHost is a factory namespace for creating http.IncomingMessage instances in tests and adapters — without needing a real socket or live HTTP connection. It supports two creation modes: a synchronous plain-object initializer and an async raw-HTTP-bytes parser.
import { IncomingMessageHost } from '@opra/http';
IncomingMessageHost itself is a namespace, not a class. It produces real http.IncomingMessage objects that can be passed directly to Express, OPRA adapters, or upgraded to HttpRequest via HttpRequest.create().
Namespace functions
IncomingMessageHost.create(init?)
IncomingMessageHost.create(init?: IncomingMessageHost.Initiator): http.IncomingMessage
Synchronously creates a http.IncomingMessage from a plain object. The body (if provided) is pushed into the stream immediately and the stream is closed.
const req = IncomingMessageHost.create({
method: 'POST',
url: '/api/customers',
headers: { 'content-type': 'application/json' },
body: { name: 'Alice' },
});
IncomingMessageHost.from(init?, options?)
IncomingMessageHost.from(
init?:
| IncomingMessageHost.Initiator
| string
| Buffer
| NodeJS.ReadableStream
| Blob
| Iterable<any>
| AsyncIterable<any>
| Promise<any>,
options?: { waitForBody?: boolean }
): Promise<http.IncomingMessage>
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 request 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 rawRequest = `POST /api/customers HTTP/1.1\r\nContent-Type: application/json\r\n\r\n{"name":"Alice"}`;
const req = await IncomingMessageHost.from(rawRequest, { waitForBody: true });
// req.method === 'POST', headers are parsed, body stream is complete
Interfaces
IncomingMessageHost.Initiator
Used with create() to construct a message from a plain object.
| Property | Type | Description |
|---|---|---|
httpVersionMajor | number | undefined | HTTP major version. Defaults to 1. |
httpVersionMinor | number | undefined | HTTP minor version. Defaults to 1. |
method | string | undefined | HTTP method. Defaults to 'GET'. |
url | string | undefined | Request URL. Defaults to '/'. |
headers | Record<string, any> | Headers | Map<string, any> | string[] | undefined | Request headers. |
trailers | Record<string, any> | Headers | Map<string, any> | string[] | undefined | Trailing headers. |
params | Record<string, any> | undefined | Path parameters (attached as req.params). |
cookies | Record<string, any> | undefined | Cookie values (attached as req.cookies). |
body | string | Buffer | Iterable<any> | AsyncIterable<any> | Object | undefined | Request body. Objects are JSON-serialized automatically. |
ip | string | undefined | Remote IP address. |
ips | string[] | undefined | Proxy chain IP addresses. |