Skip to main content

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 = true to 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.

PropertyTypeDescription
httpVersionMajornumber | undefinedHTTP major version. Defaults to 1.
httpVersionMinornumber | undefinedHTTP minor version. Defaults to 1.
methodstring | undefinedHTTP method. Defaults to 'GET'.
urlstring | undefinedRequest URL. Defaults to '/'.
headersRecord<string, any> | Headers | Map<string, any> | string[] | undefinedRequest headers.
trailersRecord<string, any> | Headers | Map<string, any> | string[] | undefinedTrailing headers.
paramsRecord<string, any> | undefinedPath parameters (attached as req.params).
cookiesRecord<string, any> | undefinedCookie values (attached as req.cookies).
bodystring | Buffer | Iterable<any> | AsyncIterable<any> | Object | undefinedRequest body. Objects are JSON-serialized automatically.
ipstring | undefinedRemote IP address.
ipsstring[] | undefinedProxy chain IP addresses.