UNPKG

@nestjs/core

Version:

The core of NestJS, a TypeScript framework for Node.js backends: modules, dependency injection and the request pipeline, on Express, Fastify or node:http.

64 lines (63 loc) • 3.19 kB
import { Readable } from 'stream'; /** * What a route handler can return to have it sent as it is produced, instead * of serialized: a web `Response`, a `ReadableStream`, or any other async * iterable (an async generator or a Node.js `Readable`, among others). */ export type StreamingBody = Response | ReadableStream<unknown> | AsyncIterable<unknown>; type BodySource = Exclude<StreamingBody, Response>; export interface StreamingBodyIterator { next(): Promise<IteratorResult<unknown>>; return(): Promise<unknown>; } export declare function isWebResponse(value: unknown): value is Response; /** * An async iterable that is also a thenable, like a Mongoose query, stands for * what it resolves to. Arrays and other sync iterables are not streams either. */ export declare function isStreamingBody(value: unknown): value is StreamingBody; /** * Whether something has read from the body already, as far as it can be told: * only an async generator itself knows that. A stream that failed instead is * left to report its own error when it is read. */ export declare function isStreamingBodyConsumed(body: StreamingBody): boolean; /** * Listens for the 'error' of a Node.js stream that the router holds before it * reads it, as a `StreamableFile` does for its own. A stream that fails in the * meantime, or once it is stopped unread (e.g. `fs.createReadStream()` of a * file that does not exist), would otherwise emit an 'error' that nothing * listens for, which ends the process. Whatever reads it still gets the error. */ export declare function listenForStreamError(value: unknown): void; /** * Opens the body for reading. Neither the async iterator of a `ReadableStream` * nor that of a `Readable` stops the stream until the read in progress settles, * which an upstream that went quiet may never do, so these are stopped directly. */ export declare function openStreamingBody(source: BodySource): StreamingBodyIterator; /** * Stops a body that will not be sent, so that what produces it (e.g. a paid * LLM call) stops as well. */ export declare function cancelStreamingBody(body: StreamingBody): Promise<void>; /** * Turns what a body yields into what a response can write: strings and binary * data as they are, and anything else as a line of JSON (NDJSON). */ export declare function toResponseChunk(value: unknown): string | Uint8Array; export declare function inferContentType(value: unknown): string; /** * The headers of a `Response` to send, as they are unless it came from * `fetch()`: that decodes the body, but leaves `Content-Encoding` and the * `Content-Length` of the encoded body in place. `Set-Cookie` is left out, as * only `getSetCookie()` tells its values apart in every implementation. */ export declare function getWebResponseHeaders(response: Response): [string, string][]; /** * A stream of what is left of the body, with the chunk that was already read * first. It reads a chunk only when the response can take one, and destroying * it (the adapters do when the client goes away) stops the body. */ export declare function toReadable(iterator: StreamingBodyIterator, firstChunk: string | Uint8Array): Readable; export {};