【问题标题】:How to pass large amounts of data to process.stdin.write in Deno?如何将大量数据传递给 Deno 中的 process.stdin.write?
【发布时间】:2021-07-13 07:38:01
【问题描述】:

向标准输入传递大量数据会失败。如果你在 unix 下运行这个脚本,你只会在终端中得到网站的一部分输出:

const cat = Deno.run({
    cmd: ["cat"],
    stdin: "piped"
});
await cat.stdin.write(new Uint8Array(
    await (
            await fetch("https://languagelog.ldc.upenn.edu/nll/?feed=atom")
    ).arrayBuffer()
));
cat.stdin.close();
await cat.status();

sample feed 以</feed> 结尾,但管道会在中间吞下:

有没有办法绕过这个问题或者我发现了一个错误?

【问题讨论】:

    标签: javascript deno


    【解决方案1】:

    只有Ryan Dahl himself answered me:

    stdin.write 只是一个系统调用,它返回写入的字节数。如果你使用writeAll,我认为它会起作用。

    也就是说,理想情况下,您应该流式传输大数据,而不是缓冲它。

    import { readerFromStreamReader } from "https://deno.land/std@0.100.0/io/streams.ts";
    
    const cat = Deno.run({
      cmd: ["cat"],
      stdin: "piped",
    });
    const res = await fetch("https://languagelog.ldc.upenn.edu/nll/?feed=atom");
    let r = readerFromStreamReader(res.body.getReader());
    await Deno.copy(r, cat.stdin);
    cat.stdin.close();
    await cat.status();
    

    【讨论】:

      猜你喜欢
      • 2020-09-02
      • 2017-06-16
      • 2010-11-05
      • 2020-12-14
      • 2020-02-02
      • 1970-01-01
      • 1970-01-01
      • 2018-04-06
      • 2011-05-31
      相关资源
      最近更新 更多