How do you connect two (pipe stdin and stdout) Deno subprocesses?

Viewed 1600

Judging by the API docs, a Deno subprocess (an instance of Deno.Process) can receive one of four stdin types, the same goes for stdout. However, there's no mention in the documentation as to how to pipe the output from one subprocess to the input of another. What I'm trying to achieve is akin to the basic UNIX pipe (oneProcess | another) and then read the output of the second process in the pipeline. Simply running

const someProcess = Deno.run({
  cmd: ["oneProcess firstParameter | another 2ndParameter"]
});

fails with an error of:

error: Uncaught NotFound: No such file or directory (os error 2)

because the first argument (string) is expected to be an executable.

How would one achieve this is Deno then, are we required to perhaps set "piped" as both the output and input to the subprocesses (respectively) and then manually read and write data from one to another?

1 Answers

You're getting NotFound: no such file or directory because the value passed to cmd must be a path to the binary.

the first element needs to be a path to the binary

And onProcess | another is not a binary.


To use a unix pipe | you can run bash and then write to stdin.

const p = Deno.run({
  cmd: ["bash"],
  stdout: "piped",
  stdin: "piped"
});

const encoder = new TextEncoder();
const decoder = new TextDecoder();

const command = "echo -n yes | md5sum";
await p.stdin.write(encoder.encode(command));

await p.stdin.close();
const output = await p.output()
p.close();

console.log(decoder.decode(output)); // a6105c0a611b41b08f1209506350279e
Related