Skip to main content
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

See all symbols in @std/io on

小贴士 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));

你找到了你需要的东西吗?

编辑此页面
隐私政策