UNPKG

1.51 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.readableFrom = exports.readableCreate = void 0;
4const node_stream_1 = require("node:stream");
5/**
6 * Convenience function to create a Readable that can be pushed into (similar to RxJS Subject).
7 * Push `null` to it to complete (similar to RxJS `.complete()`).
8 *
9 * Difference from Readable.from() is that this readable is not "finished" yet and allows pushing more to it.
10 *
11 * Caution!
12 * The implementation of this Readable is not fully compliant,
13 * e.g the read() method doesn't return anything, so, it will hand the Node process (or cause it to process.exit(0))
14 * if read() will be called AFTER everything was pushed and Readable is closed (by pushing `null`).
15 * Beware of it when e.g doing unit testing! Jest prefers to hang (not exit-0).
16 *
17 * @deprecated because of the caution above
18 */
19function readableCreate(items = [], opt) {
20 const readable = new node_stream_1.Readable({
21 objectMode: true,
22 ...opt,
23 read() { }, // Caution, if this is called and Readable has not finished yet (null wasn't pushed) - it'll hang the process!
24 });
25 for (const item of items) {
26 readable.push(item);
27 }
28 return readable;
29}
30exports.readableCreate = readableCreate;
31/**
32 * Convenience type-safe wrapper around Readable.from() that infers the Type of input.
33 */
34function readableFrom(items, opt) {
35 return node_stream_1.Readable.from(items, opt);
36}
37exports.readableFrom = readableFrom;