跳到主要内容

1 篇博文 含有标签「readable」

查看所有标签

从 node-fetch 的响应中获取 readable

· 阅读需 1 分钟
1adybug
子虚伊人

node-fetchresponse.body 虽然类型标记为 ReadableStream,但实际上并不是,被 Readable.fromweb 调用时会报错,此时改为使用 Readable.from 即可成功。

import fetch from "node-fetch"

const response = await fetch("http://somewhere.com")

// ❌ 会报错
const readable = Readable.fromweb(response.body!)

// ✅ 不会报错
const readable = Readable.from(response.body!)