aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-12-04 01:11:34 -0800
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-12-04 01:11:34 -0800
commitab020b2275b26cd83f13ed327bd0401ad2176fa9 (patch)
treedd7bcb4bcac90b478b8e2143c2560906bb52f0bd
parent0617896d7045e129abac8d7fd22df0e6626d92c8 (diff)
downloadbun-ab020b2275b26cd83f13ed327bd0401ad2176fa9.tar.gz
bun-ab020b2275b26cd83f13ed327bd0401ad2176fa9.tar.zst
bun-ab020b2275b26cd83f13ed327bd0401ad2176fa9.zip
Update README.md
Diffstat (limited to '')
-rw-r--r--README.md39
1 files changed, 33 insertions, 6 deletions
diff --git a/README.md b/README.md
index 2fec9861b..6d25153d8 100644
--- a/README.md
+++ b/README.md
@@ -2118,21 +2118,48 @@ Bun.serve({
});
```
-### Sending files with Bun.serve()
+### Streaming files with Bun.serve()
-`Bun.serve()` lets you send files fast.
+`Bun.serve()` lets you stream files fast.
-To send a file, return a `Response` object with a `Bun.file(pathOrFd)` object as the body.
+To stream a file, return a `Response` object with a `Bun.file(pathOrFd)` object as the body.
```ts
-Bun.serve({
+import { serve, file } from "bun";
+
+serve({
fetch(req) {
- return new Response(Bun.file("./hello.txt"));
+ return new Response(file("./hello.txt"));
},
});
```
-Under the hood, when TLS is not enabled, Bun automatically uses the sendfile(2) system call. This enables zero-copy file transfers, which is faster than reading the file into memory and sending it.
+Bun automatically uses the [`sendfile(2)`](https://man7.org/linux/man-pages/man2/sendfile.2.html) system call when possible, enabling zero-copy file transfers in the kernel &mdash; the fastest way to send files.
+
+Note: You can also read the files into memory and send it manually, but that is slower.
+
+**Sending only part of a file**
+
+<sup>Available in Bun v0.3.0</sup>
+
+`Bun.serve()` has builtin support for the `Content-Range` header
+
+To stream up to the first N bytes of a file, call [`slice(start, end)`](https://developer.mozilla.org/en-US/docs/Web/API/Blob/slice) on the `Bun.file` object:
+
+```ts
+import { serve, file } from "bun";
+
+serve({
+ fetch(req) {
+ const [start = 0, end = Infinity] =
+ req.headers.get("Range").split("=").at(-1).split("-") ?? [];
+
+ return new Response(
+ file("./my-big-video-to-stream.mp4").slice(Number(start), Number(end)),
+ );
+ },
+});
+```
### WebSockets with Bun.serve()