UNPKG

@nestjs/common

Version:

Nest - modern, fast, powerful node.js web framework (@common)

38 lines (37 loc) • 1.72 kB
import { METHOD_METADATA, PATH_METADATA, SSE_METADATA, SSE_OPTIONS_METADATA, } from '../../constants.js'; import { RequestMethod } from '../../enums/request-method.enum.js'; /** * Declares this route as a Server-Sent-Events endpoint * * Events wait in memory for a client that is not reading them. Once more than * `maxPendingDataLength` characters of event data (4M by default) wait for a * client whose stream is backed up, its connection is dropped. An * `EventSource` then reconnects, sending the id of the last event it received. * Raise the limit for a route that streams bulk data faster than its clients * read, or set it to 0 or `Infinity` to lift it. * * @param path route path (default: `/`) * @param options `SseOptions` object specifying: * - `method` - HTTP request method of the route (default: `GET`). * - `maxPendingDataLength` - maximum number of characters of event data that * may wait for a client that is not reading them (default: 4M). 0 or * `Infinity` lifts the limit. * * @example * ```ts * @Sse('logs', { maxPendingDataLength: 64 * 1024 * 1024 }) * logs(): Observable<MessageEvent> { ... } * ``` * * @publicApi */ export function Sse(path, options = {}) { return (target, key, descriptor) => { path = path && path.length ? path : '/'; Reflect.defineMetadata(PATH_METADATA, path, descriptor.value); Reflect.defineMetadata(METHOD_METADATA, options[METHOD_METADATA] ?? RequestMethod.GET, descriptor.value); Reflect.defineMetadata(SSE_METADATA, true, descriptor.value); Reflect.defineMetadata(SSE_OPTIONS_METADATA, { maxPendingDataLength: options.maxPendingDataLength }, descriptor.value); return descriptor; }; }