it-buffer-stream
Version:
An async iterator that emits buffers containing bytes up to a certain length
45 lines • 1.35 kB
TypeScript
/**
* @packageDocumentation
*
* Generate a stream of buffers, useful for testing purposes.
*
* @example
*
* ```javascript
* import bufferStream from 'it-buffer-stream'
*
* const totalLength = //... a big number
*
* // all options are optional, defaults are shown
* const options = {
* chunkSize: 4096, // how many bytes will be in each buffer
* collector: (buffer) => {
* // will be called as each buffer is generated. the final buffer
* // may be smaller than `chunkSize`
* },
* generator: async (size) => {
* // return a promise that resolves to a buffer of length `size`
* //
* // if omitted, `Promise.resolve(crypto.randomBytes(size))` will be used
* }
* }
*
* let buffers = []
*
* for await (buf of bufferStream(totalLength, options)) {
* buffers.push(buf)
* }
*
* // `buffers` is an array of Buffers the combined length of which === totalLength
* ```
*/
export interface BufferStreamOptions {
chunkSize?: number;
collector?(arr: Uint8Array): void;
generator?(length: number): Uint8Array | Promise<Uint8Array>;
}
/**
* An async iterable that emits buffers containing bytes up to a certain length
*/
export default function bufferStream(limit: number, options?: BufferStreamOptions): AsyncGenerator<Uint8Array, void, unknown>;
//# sourceMappingURL=index.d.ts.map