UNPKG

1.36 kBTypeScriptView Raw
1/// <reference types="node" />
2import { Transform } from 'stream';
3import { IncomingMessage, OutgoingHttpHeaders } from "http";
4interface Message {
5 data: string | object;
6 comment?: string;
7 event?: string;
8 id?: string;
9 retry?: number;
10}
11interface WriteHeaders {
12 writeHead?(statusCode: number, headers?: OutgoingHttpHeaders): WriteHeaders;
13 flushHeaders?(): void;
14}
15export declare type HeaderStream = NodeJS.WritableStream & WriteHeaders;
16/**
17 * Transforms "messages" to W3C event stream content.
18 * See https://html.spec.whatwg.org/multipage/server-sent-events.html
19 * A message is an object with one or more of the following properties:
20 * - data (String or object, which gets turned into JSON)
21 * - event
22 * - id
23 * - retry
24 * - comment
25 *
26 * If constructed with a HTTP Request, it will optimise the socket for streaming.
27 * If this stream is piped to an HTTP Response, it will set appropriate headers.
28 */
29export default class SseStream extends Transform {
30 constructor(req?: IncomingMessage);
31 pipe<T extends HeaderStream>(destination: T, options?: {
32 end?: boolean;
33 }): T;
34 _transform(message: Message, encoding: string, callback: (error?: (Error | null), data?: any) => void): void;
35 writeMessage(message: Message, encoding?: string, cb?: (error: Error | null | undefined) => void): boolean;
36}
37export {};