On this page
@std/io
Unstable
This @std package is experimental and its API may change without a major version bump.
Overview Jump to heading
Utilities for working with Deno's readers, writers, and web streams.
Reader and Writer interfaces are deprecated in Deno, and so many of these
utilities are also deprecated. Consider using web streams instead.
import { toReadableStream, toWritableStream } from "@std/io";
await toReadableStream(Deno.stdin)
.pipeTo(toWritableStream(Deno.stdout));
Add to your project 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));