UNPKG

912 BPlain TextView Raw
1import { Readable, ReadableOptions } from 'stream'
2import { ReadableTyped } from '../stream.model'
3
4/**
5 * Convenience function to create a Readable that can be pushed into (similar to RxJS Subject).
6 * Push `null` to it to complete (similar to RxJS `.complete()`).
7 *
8 * Difference from Readable.from() is that this readable is not "finished" yet and allows pushing more to it.
9 */
10export function readableCreate<T>(
11 items: Iterable<T> = [],
12 opt?: ReadableOptions,
13): ReadableTyped<T> {
14 const readable = new Readable({
15 objectMode: true,
16 ...opt,
17 read() {},
18 })
19 for (const item of items) {
20 readable.push(item)
21 }
22 return readable
23}
24
25/**
26 * Convenience type-safe wrapper around Readable.from() that infers the Type of input.
27 */
28export function readableFrom<T>(
29 items: Iterable<T> | AsyncIterable<T>,
30 opt?: ReadableOptions,
31): ReadableTyped<T> {
32 return Readable.from(items, opt)
33}