diff --git a/docs/guides/http/stream-node-streams-in-bun.md b/docs/guides/http/stream-node-streams-in-bun.md new file mode 100644 index 00000000000000..0b5e31d744f933 --- /dev/null +++ b/docs/guides/http/stream-node-streams-in-bun.md @@ -0,0 +1,20 @@ +--- +name: Streaming HTTP Server with Node.js Streams +--- + +In Bun, [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) objects can accept a Node.js [`Readable`](https://nodejs.org/api/stream.html#stream_readable_streams). + +This works because Bun's `Response` object allows any async iterable as its body. Node.js streams are async iterables, so you can pass them directly to `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" }, + }); + }, +}); +``` diff --git a/docs/guides/streams/node-readable-to-arraybuffer.md b/docs/guides/streams/node-readable-to-arraybuffer.md new file mode 100644 index 00000000000000..9f33c0eaf0fab5 --- /dev/null +++ b/docs/guides/streams/node-readable-to-arraybuffer.md @@ -0,0 +1,11 @@ +--- +name: Convert a Node.js Readable stream to an ArrayBuffer +--- + +To convert a Node.js `Readable` stream to an `ArrayBuffer` in Bun, you can create a new `Response` object with the stream as the body, then use `arrayBuffer()` to read the stream into an `ArrayBuffer`. + +```ts +import { Readable } from "stream"; +const stream = Readable.from(["Hello, ", "world!"]); +const buf = await new Response(stream).arrayBuffer(); +``` diff --git a/docs/guides/streams/node-readable-to-blob.md b/docs/guides/streams/node-readable-to-blob.md new file mode 100644 index 00000000000000..dc50eeba3736e4 --- /dev/null +++ b/docs/guides/streams/node-readable-to-blob.md @@ -0,0 +1,11 @@ +--- +name: Convert a Node.js Readable stream to a Blob +--- + +To convert a Node.js `Readable` stream to a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob) in Bun, you can create a new [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) object with the stream as the body, then use [`response.blob()`](https://developer.mozilla.org/en-US/docs/Web/API/Response/blob) to read the stream into a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob). + +```ts +import { Readable } from "stream"; +const stream = Readable.from(["Hello, ", "world!"]); +const blob = await new Response(stream).blob(); +``` diff --git a/docs/guides/streams/node-readable-to-json.md b/docs/guides/streams/node-readable-to-json.md new file mode 100644 index 00000000000000..3fb8bdc459c72f --- /dev/null +++ b/docs/guides/streams/node-readable-to-json.md @@ -0,0 +1,12 @@ +--- +name: Convert a Node.js Readable stream to JSON +--- + +To convert a Node.js `Readable` stream to a JSON object in Bun, you can create a new [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) object with the stream as the body, then use [`response.json()`](https://developer.mozilla.org/en-US/docs/Web/API/Response/json) to read the stream into a JSON object. + +```ts +import { Readable } from "stream"; +const stream = Readable.from([JSON.stringify({ hello: "world" })]); +const json = await new Response(stream).json(); +console.log(json); // { hello: "world" } +``` diff --git a/docs/guides/streams/node-readable-to-string.md b/docs/guides/streams/node-readable-to-string.md new file mode 100644 index 00000000000000..89efa33afe7d1d --- /dev/null +++ b/docs/guides/streams/node-readable-to-string.md @@ -0,0 +1,12 @@ +--- +name: Convert a Node.js Readable stream to a string +--- + +To convert a Node.js `Readable` stream to a string in Bun, you can create a new [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) object with the stream as the body, then use [`response.text()`](https://developer.mozilla.org/en-US/docs/Web/API/Response/text) to read the stream into a string. + +```ts +import { Readable } from "stream"; +const stream = Readable.from([Buffer.from("Hello, world!")]); +const text = await new Response(stream).text(); +console.log(text); // "Hello, world!" +```