-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b6aebb5
commit 2ce8395
Showing
5 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }, | ||
}); | ||
}, | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!" | ||
``` |