Skip to main content
On this page

@std/io

不稳定

此 @std 包是实验性的,其 API 可能在未做大版本升级的情况下发生变化。

概览 Jump to heading

用于处理 Deno 的读取器、写入器和网络流的工具。

ReaderWriter 接口在 Deno 中已被废弃,因此许多此类工具函数也已被废弃。建议改用网络流。

import { toReadableStream, toWritableStream } from "@std/io";

await toReadableStream(Deno.stdin)
  .pipeTo(toWritableStream(Deno.stdout));

添加到您的项目 Jump to heading

deno add jsr:@std/io

查看 @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));

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

编辑此页面
隐私政策