Add duplex() and child() methods

This commit is contained in:
Sami Turcotte
2018-12-02 01:18:28 -05:00
parent 6201d7812f
commit c99b946e6b
4 changed files with 102 additions and 19 deletions

View File

@@ -1,3 +1,4 @@
import * as cp from "child_process";
import test from "ava";
import { expect } from "chai";
import { Readable } from "stream";
@@ -14,6 +15,8 @@ import {
collect,
concat,
merge,
duplex,
child,
} from ".";
test.cb("fromArray() streams array elements in flowing mode", t => {
@@ -921,3 +924,49 @@ test.cb("merge() merges an empty list of readable streams", t => {
.on("error", t.end)
.on("end", t.end);
});
test.cb(
"duplex() combines a writable and readable stream into a ReadWrite stream",
t => {
t.plan(1);
const source = new Readable();
const catProcess = cp.exec("cat");
let out = "";
source
.pipe(duplex(catProcess.stdin, catProcess.stdout))
.on("data", chunk => (out += chunk))
.on("error", t.end)
.on("end", () => {
expect(out).to.equal("abcdef");
t.pass();
t.end();
});
source.push("ab");
source.push("cd");
source.push("ef");
source.push(null);
},
);
test.cb(
"child() allows easily writing to child process stdin and reading from its stdout",
t => {
t.plan(1);
const source = new Readable();
const catProcess = cp.exec("cat");
let out = "";
source
.pipe(child(catProcess))
.on("data", chunk => (out += chunk))
.on("error", t.end)
.on("end", () => {
expect(out).to.equal("abcdef");
t.pass();
t.end();
});
source.push("ab");
source.push("cd");
source.push("ef");
source.push(null);
},
);