Skip to content

在 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" },
    });
  },
});

Bun學習網由www.bunjs.com.cn整理維護