你如何连接两个(管道标准输入和标准输出)Deno 子进程?

Mil*_*bit 4 typescript deno

API 文档来看,Deno 子进程( 的一个实例Deno.Process)可以接收四种标准输入类型之一,标准输出也是如此。但是,文档中没有提到如何将一个子进程的输出通过管道传输到另一个子进程的输入。我想要实现的是类似于基本的 UNIX 管道 ( oneProcess | another),然后读取管道中第二个进程的输出。简单地运行

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

失败并出现以下错误:

错误:未捕获的未找到:没有这样的文件或目录(操作系统错误 2)

因为第一个参数(字符串)应该是一个可执行文件。

那么,Deno 将如何实现这一点,我们是否可能需要"piped"(分别)设置为子进程的输出和输入,然后手动从一个进程读取和写入另一个进程的数据?

Mar*_*nde 5

你得到NotFound: no such file or directory是因为传递给的值cmd必须是二进制文件的路径。

第一个元素需要是二进制文件的路径

并且onProcess | another不是二进制文件。


要使用 unix 管道,|您可以运行bash然后写入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
Run Code Online (Sandbox Code Playgroud)