diff options
author | 2023-02-24 16:33:53 -0800 | |
---|---|---|
committer | 2023-02-24 16:33:53 -0800 | |
commit | 18362505429f99662f4423264147896d23313dbe (patch) | |
tree | f3589c33ca01a3d1118836732f55fc05bf87f30c /docs/api | |
parent | 1d85b5efa83a28c46e50ebae041ac480b33fb116 (diff) | |
download | bun-18362505429f99662f4423264147896d23313dbe.tar.gz bun-18362505429f99662f4423264147896d23313dbe.tar.zst bun-18362505429f99662f4423264147896d23313dbe.zip |
Docs tweaks (#2160)
* Tweaks
* Add ecosystem. Add bunx. Flesh out install.
* Tweaks
* Add TS to installation
* Tweaks
* New readme
* Write new readme
* Tweak
* Center header
* Bun
* tweaks
* No dollar sign
* Fix links
* Update
* Tweak
Diffstat (limited to 'docs/api')
-rw-r--r-- | docs/api/file-io.md | 10 | ||||
-rw-r--r-- | docs/api/http.md | 27 | ||||
-rw-r--r-- | docs/api/websockets.md | 2 |
3 files changed, 21 insertions, 18 deletions
diff --git a/docs/api/file-io.md b/docs/api/file-io.md index a79f6ad80..0f1642ef3 100644 --- a/docs/api/file-io.md +++ b/docs/api/file-io.md @@ -1,5 +1,5 @@ {% callout %} -**Note** — Bun also provides an [almost complete](/docs/runtime/nodejs) implementation of the Node.js `fs` module, documented [here](https://nodejs.org/api/fs.html). This page only documents Bun-native APIs. +**Note** — The `Bun.file` and `Bun.write` APIs documented on this page are heavily optimized and represent the recommended way to perform file-system tasks using Bun. Existing Node.js projects may use Bun's [nearly complete](/docs/runtime/nodejs) implementation of the [`node:fs`](https://nodejs.org/api/fs.html) module. {% /callout %} Bun provides a set of optimized APIs for reading and writing files. @@ -234,13 +234,7 @@ interface Bun { write( destination: string | number | FileBlob | URL, - input: - | string - | Blob - | ArrayBuffer - | SharedArrayBuffer - | TypedArray - | Response, + input: string | Blob | ArrayBuffer | SharedArrayBuffer | TypedArray | Response, ): Promise<number>; } diff --git a/docs/api/http.md b/docs/api/http.md index d05691556..838c73516 100644 --- a/docs/api/http.md +++ b/docs/api/http.md @@ -1,10 +1,19 @@ {% callout %} -**Note** — Bun provides an [almost complete](/docs/runtime/nodejs#node_http) implementation of the Node.js [`http`](https://nodejs.org/api/http.html) and [`https`](https://nodejs.org/api/https.html) modules. This page only documents Bun-native APIs. +**Note** — This page documents the `Bun.serve` API. This API is heavily optimized and represents the recommended way to build HTTP servers in Bun. Existing Node.js projectes may use Bun's [nearly complete](/docs/runtime/nodejs#node_http) implementation of the Node.js [`http`](https://nodejs.org/api/http.html) and [`https`](https://nodejs.org/api/https.html) modules. {% /callout %} -## Start a server +## Send a request + +Bun implements the Web `fetch` API for making HTTP requests. The `fetch` function is available in the global scope. + +```ts +const response = await fetch("https://bun.sh/manifest.json"); +const result = (await response.json()) as any; +console.log(result.icons[0].src); +// => "/logo-square.jpg" +``` -`Bun.serve(options) => Server` +## Start a server Start an HTTP server in Bun with `Bun.serve`. @@ -41,7 +50,7 @@ Bun.serve({ }); ``` -## Error handling +### Error handling To activate development mode, set `development: true`. By default, development mode is _enabled_ unless `NODE_ENV` is `production`. @@ -91,7 +100,7 @@ const server = Bun.serve({ server.stop(); ``` -## TLS +### TLS Bun supports TLS out of the box, powered by [OpenSSL](https://www.openssl.org/). Enable TLS by passing in a value for `keyFile` and `certFile`; both are required to enable TLS. If needed, supply a `passphrase` to decrypt the `keyFile`. @@ -120,7 +129,7 @@ Bun.serve({ }); ``` -## Hot reloading +### Hot reloading Thus far, the examples on this page have used the explicit `Bun.serve` API. Bun also supports an alternate syntax. @@ -142,7 +151,7 @@ $ bun --hot server.ts It's possible to configure hot reloading while using the explicit `Bun.serve` API; for details refer to [Runtime > Hot reloading](/docs/runtime/hot). -## Streaming files +### Streaming files To stream a file, return a `Response` object with a `BunFile` object as the body. @@ -180,7 +189,7 @@ Bun.serve({ }); ``` -## Benchmarks +### Benchmarks Below are Bun and Node.js implementations of a simple HTTP server that responds `Bun!` to each incoming `Request`. @@ -223,7 +232,7 @@ The `Bun.serve` server can handle roughly 2.5x more requests per second than Nod {% image width="499" alt="image" src="https://user-images.githubusercontent.com/709451/162389032-fc302444-9d03-46be-ba87-c12bd8ce89a0.png" /%} -## Reference +### Reference {% details summary="See TypeScript definitions" %} diff --git a/docs/api/websockets.md b/docs/api/websockets.md index b362d4ee2..2220b9c8d 100644 --- a/docs/api/websockets.md +++ b/docs/api/websockets.md @@ -1,4 +1,4 @@ -As of Bun v0.2.1, `Bun.serve()` supports server-side WebSockets, with on-the-fly compression, TLS support, and a Bun-native pubsub API. +`Bun.serve()` supports server-side WebSockets, with on-the-fly compression, TLS support, and a Bun-native publish-subscribe API. {% callout %} |