如何在 Deno 中将大量数据传递给 process.stdin.write?
How to pass large amounts of data to process.stdin.write in Deno?
向标准输入传递大量数据将失败。如果你在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>
结尾,但是管道会在中间吞下去:
有没有办法避免这个问题,或者我发现了一个错误?
只有 Ryan Dahl himself answered me:
stdin.write is just one syscall, it returns the number of bytes written. If you use writeAll, I think it would work.
That said, ideally you'd stream large data, rather than buffer it up.
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();
向标准输入传递大量数据将失败。如果你在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>
结尾,但是管道会在中间吞下去:
有没有办法避免这个问题,或者我发现了一个错误?
只有 Ryan Dahl himself answered me:
stdin.write is just one syscall, it returns the number of bytes written. If you use writeAll, I think it would work.
That said, ideally you'd stream large data, rather than buffer it up.
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();