UNPKG

it-reader

Version:

Read an exact number of bytes from a binary (async) iterable

33 lines 966 B
import { Uint8ArrayList } from 'uint8arraylist'; import type { Source } from 'it-stream-types'; /** * A specialized `AsyncGenerator` that lets you pass a number to the `.next` method which * will attempt to return only that many bytes. */ export interface Reader extends AsyncGenerator<Uint8ArrayList, void, any> { next: (...args: [] | [number | undefined]) => Promise<IteratorResult<Uint8ArrayList, void>>; } /** * Returns an `AsyncGenerator` that allows reading a set number of bytes from the passed source. * * @example * * ```javascript * import { reader } from 'it-reader' * * const stream = reader(source) * * // read 10 bytes from the stream * const { done, value } = await stream.next(10) * * if (done === true) { * // stream finished * } * * if (value != null) { * // do something with value * } * ``` */ export declare function reader(source: Source<Uint8Array | Uint8ArrayList>): Reader; //# sourceMappingURL=index.d.ts.map