stream-to-async-iterator
Version:
ES async interator wrapper for node streams
51 lines (50 loc) • 1.9 kB
TypeScript
/// <reference types="node" />
import { Readable } from "stream";
export declare type StreamToAsyncIteratorOptions = {
/** The size of each read from the stream for each iteration */
size?: number;
};
/**
* Wraps a stream into an object that can be used as an async iterator.
*
* This will keep a stream in a paused state, and will only read from the stream on each
* iteration. A size can be supplied to set an explicit call to `stream.read([size])` in
* the options for each iteration.
*/
export default class StreamToAsyncIterator<T = unknown> implements AsyncIterableIterator<T> {
/** The underlying readable stream */
private _stream;
/** Contains stream's error when stream has error'ed out */
private _error;
/** The current state of the iterator (not readable, readable, ended, errored) */
private _state;
private _size;
/** The rejections of promises to call when stream errors out */
private _rejections;
get closed(): boolean;
constructor(stream: Readable, { size }?: StreamToAsyncIteratorOptions);
[Symbol.asyncIterator](): this;
/**
* Returns the next iteration of data. Rejects if the stream errored out.
*/
next(): Promise<IteratorResult<T, void>>;
/**
* Waits until the stream is readable. Rejects if the stream errored out.
* @returns Promise when stream is readable
*/
private _untilReadable;
/**
* Waits until the stream is ended. Rejects if the stream errored out.
* @returns Promise when stream is finished
*/
private _untilEnd;
return(): Promise<IteratorResult<T, void>>;
throw(err?: Error): Promise<IteratorResult<T, void>>;
/**
* Destroy the stream
* @param err An optional error to pass to the stream for an error event
*/
close(err?: Error): void;
private _handleStreamError;
private _handleStreamEnd;
}