@std/io
不稳定
此 @std 包是实验性的,其 API 可能在未做大版本升级的情况下发生变化。
概览 Jump to heading
用于处理 Deno 的读取器、写入器和网络流的工具。
Reader 和 Writer 接口在 Deno 中已被废弃,因此许多此类工具函数也已被废弃。建议改用网络流。
import { toReadableStream, toWritableStream } from "@std/io";
await toReadableStream(Deno.stdin)
.pipeTo(toWritableStream(Deno.stdout));
添加到您的项目 Jump to heading
deno add jsr:@std/io
小贴士 Jump to heading
- 新代码优先使用 Web Streams。
Reader/Writer助手主要为兼容性存在,正在逐步淘汰。 - 使用
toReadableStream/toWritableStream在不进行全部缓冲的情况下将 Deno 传统 IO 适配为流。 - 流天然支持背压;管道操作时避免手动读取循环。
- 对文本转换,可以结合使用
TextDecoderStream/TextEncoderStream。
示例 Jump to heading
import { toReadableStream, toWritableStream } from "@std/io";
await toReadableStream(Deno.stdin)
.pipeThrough(new TextDecoderStream())
.pipeThrough(
new TransformStream({
transform(chunk, ctl) {
ctl.enqueue(chunk.toUpperCase());
},
}),
)
.pipeThrough(new TextEncoderStream())
.pipeTo(toWritableStream(Deno.stdout));