UNPKG

6.57 kBTypeScriptView Raw
1export interface ReadableStreamEvents<T> {
2 /**
3 * The 'data' event is emitted whenever the stream is
4 * relinquishing ownership of a chunk of data to a consumer.
5 */
6 on(event: 'data', callback: (data: T) => void): void;
7 /**
8 * Emitted when any error occurs.
9 */
10 on(event: 'error', callback: (err: Error) => void): void;
11 /**
12 * The 'end' event is emitted when there is no more data
13 * to be consumed from the stream. The 'end' event will
14 * not be emitted unless the data is completely consumed.
15 */
16 on(event: 'end', callback: () => void): void;
17}
18/**
19 * A interface that emulates the API shape of a node.js readable
20 * stream for use in desktop and web environments.
21 */
22export interface ReadableStream<T> extends ReadableStreamEvents<T> {
23 /**
24 * Stops emitting any events until resume() is called.
25 */
26 pause(): void;
27 /**
28 * Starts emitting events again after pause() was called.
29 */
30 resume(): void;
31 /**
32 * Destroys the stream and stops emitting any event.
33 */
34 destroy(): void;
35 /**
36 * Allows to remove a listener that was previously added.
37 */
38 removeListener(event: string, callback: Function): void;
39}
40/**
41 * A interface that emulates the API shape of a node.js readable
42 * for use in desktop and web environments.
43 */
44export interface Readable<T> {
45 /**
46 * Read data from the underlying source. Will return
47 * null to indicate that no more data can be read.
48 */
49 read(): T | null;
50}
51export declare namespace Readable {
52 function fromString(value: string): Readable<string>;
53 function toString(readable: Readable<string>): string;
54}
55/**
56 * A interface that emulates the API shape of a node.js writeable
57 * stream for use in desktop and web environments.
58 */
59export interface WriteableStream<T> extends ReadableStream<T> {
60 /**
61 * Writing data to the stream will trigger the on('data')
62 * event listener if the stream is flowing and buffer the
63 * data otherwise until the stream is flowing.
64 *
65 * If a `highWaterMark` is configured and writing to the
66 * stream reaches this mark, a promise will be returned
67 * that should be awaited on before writing more data.
68 * Otherwise there is a risk of buffering a large number
69 * of data chunks without consumer.
70 */
71 write(data: T): void | Promise<void>;
72 /**
73 * Signals an error to the consumer of the stream via the
74 * on('error') handler if the stream is flowing.
75 */
76 error(error: Error): void;
77 /**
78 * Signals the end of the stream to the consumer. If the
79 * result is not an error, will trigger the on('data') event
80 * listener if the stream is flowing and buffer the data
81 * otherwise until the stream is flowing.
82 *
83 * In case of an error, the on('error') event will be used
84 * if the stream is flowing.
85 */
86 end(result?: T | Error): void;
87}
88/**
89 * A stream that has a buffer already read. Returns the original stream
90 * that was read as well as the chunks that got read.
91 *
92 * The `ended` flag indicates if the stream has been fully consumed.
93 */
94export interface ReadableBufferedStream<T> {
95 /**
96 * The original stream that is being read.
97 */
98 stream: ReadableStream<T>;
99 /**
100 * An array of chunks already read from this stream.
101 */
102 buffer: T[];
103 /**
104 * Signals if the stream has ended or not. If not, consumers
105 * should continue to read from the stream until consumed.
106 */
107 ended: boolean;
108}
109export declare function isReadableStream<T>(obj: unknown): obj is ReadableStream<T>;
110export declare function isReadableBufferedStream<T>(obj: unknown): obj is ReadableBufferedStream<T>;
111export interface Reducer<T> {
112 (data: T[]): T;
113}
114export interface DataTransformer<Original, Transformed> {
115 (data: Original): Transformed;
116}
117export interface ErrorTransformer {
118 (error: Error): Error;
119}
120export interface ITransformer<Original, Transformed> {
121 data: DataTransformer<Original, Transformed>;
122 error?: ErrorTransformer;
123}
124export declare function newWriteableStream<T>(reducer: Reducer<T>, options?: WriteableStreamOptions): WriteableStream<T>;
125export interface WriteableStreamOptions {
126 /**
127 * The number of objects to buffer before WriteableStream#write()
128 * signals back that the buffer is full. Can be used to reduce
129 * the memory pressure when the stream is not flowing.
130 */
131 highWaterMark?: number;
132}
133/**
134 * Helper to fully read a T readable into a T.
135 */
136export declare function consumeReadable<T>(readable: Readable<T>, reducer: Reducer<T>): T;
137/**
138 * Helper to read a T readable up to a maximum of chunks. If the limit is
139 * reached, will return a readable instead to ensure all data can still
140 * be read.
141 */
142export declare function consumeReadableWithLimit<T>(readable: Readable<T>, reducer: Reducer<T>, maxChunks: number): T | Readable<T>;
143/**
144 * Helper to read a T readable up to a maximum of chunks. If the limit is
145 * reached, will return a readable instead to ensure all data can still
146 * be read.
147 */
148export declare function peekReadable<T>(readable: Readable<T>, reducer: Reducer<T>, maxChunks: number): T | Readable<T>;
149/**
150 * Helper to fully read a T stream into a T.
151 */
152export declare function consumeStream<T>(stream: ReadableStream<T>, reducer: Reducer<T>): Promise<T>;
153/**
154 * Helper to peek up to `maxChunks` into a stream. The return type signals if
155 * the stream has ended or not. If not, caller needs to add a `data` listener
156 * to continue reading.
157 */
158export declare function peekStream<T>(stream: ReadableStream<T>, maxChunks: number): Promise<ReadableBufferedStream<T>>;
159/**
160 * Helper to read a T stream up to a maximum of chunks. If the limit is
161 * reached, will return a stream instead to ensure all data can still
162 * be read.
163 */
164export declare function consumeStreamWithLimit<T>(stream: ReadableStream<T>, reducer: Reducer<T>, maxChunks: number): Promise<T | ReadableStream<T>>;
165/**
166 * Helper to create a readable stream from an existing T.
167 */
168export declare function toStream<T>(t: T, reducer: Reducer<T>): ReadableStream<T>;
169/**
170 * Helper to convert a T into a Readable<T>.
171 */
172export declare function toReadable<T>(t: T): Readable<T>;
173/**
174 * Helper to transform a readable stream into another stream.
175 */
176export declare function transform<Original, Transformed>(stream: ReadableStreamEvents<Original>, transformer: ITransformer<Original, Transformed>, reducer: Reducer<Transformed>): ReadableStream<Transformed>;
177//# sourceMappingURL=stream.d.ts.map
\No newline at end of file