Skip to content

Commit

Permalink
[docs] Add a few more guides
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner committed Apr 19, 2024
1 parent b6aebb5 commit 2ce8395
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 0 deletions.
20 changes: 20 additions & 0 deletions docs/guides/http/stream-node-streams-in-bun.md
Original file line number Diff line number Diff line change
@@ -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" },
});
},
});
```
11 changes: 11 additions & 0 deletions docs/guides/streams/node-readable-to-arraybuffer.md
Original file line number Diff line number Diff line change
@@ -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();
```
11 changes: 11 additions & 0 deletions docs/guides/streams/node-readable-to-blob.md
Original file line number Diff line number Diff line change
@@ -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();
```
12 changes: 12 additions & 0 deletions docs/guides/streams/node-readable-to-json.md
Original file line number Diff line number Diff line change
@@ -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" }
```
12 changes: 12 additions & 0 deletions docs/guides/streams/node-readable-to-string.md
Original file line number Diff line number Diff line change
@@ -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!"
```

0 comments on commit 2ce8395

Please sign in to comment.