在 Bun 中,Response 对象可以接受 Node.js Readable 流。
这是可行的,因为 Bun 的 Response 对象允许任何异步可迭代对象作为其 body。Node.js 流是异步可迭代对象,所以你可以直接将它们传递给 Response。
ts
import { Readable } from "stream";
import { serve } from "bun";
serve({
port: 3000,
fetch(req) {
return new Response(Readable.from(["Hello, ", "world!"]), {
headers: { "Content-Type": "text/plain" },
});
},
});