bun-types 1.1.37-canary.20241123T140655 → 1.1.37-canary.20241125T140601
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/api/binary-data.md +1028 -0
- package/docs/api/cc.md +197 -0
- package/docs/api/color.md +262 -0
- package/docs/api/console.md +38 -0
- package/docs/api/dns.md +113 -0
- package/docs/api/fetch.md +308 -0
- package/docs/api/ffi.md +536 -0
- package/docs/api/file-io.md +358 -0
- package/docs/api/file-system-router.md +112 -0
- package/docs/api/file.md +19 -0
- package/docs/api/glob.md +157 -0
- package/docs/api/globals.md +387 -0
- package/docs/api/hashing.md +314 -0
- package/docs/api/html-rewriter.md +31 -0
- package/docs/api/http.md +661 -0
- package/docs/api/import-meta.md +69 -0
- package/docs/api/node-api.md +16 -0
- package/docs/api/semver.md +52 -0
- package/docs/api/spawn.md +441 -0
- package/docs/api/sqlite.md +692 -0
- package/docs/api/streams.md +214 -0
- package/docs/api/tcp.md +221 -0
- package/docs/api/test.md +1 -0
- package/docs/api/transpiler.md +274 -0
- package/docs/api/udp.md +125 -0
- package/docs/api/utils.md +773 -0
- package/docs/api/websockets.md +547 -0
- package/docs/api/workers.md +230 -0
- package/docs/benchmarks.md +120 -0
- package/docs/bun-flavored-toml.md +42 -0
- package/docs/bundler/executables.md +291 -0
- package/docs/bundler/index.md +1543 -0
- package/docs/bundler/intro.md +75 -0
- package/docs/bundler/loaders.md +288 -0
- package/docs/bundler/macros.md +327 -0
- package/docs/bundler/plugins.md +30 -0
- package/docs/bundler/vs-esbuild.md +1127 -0
- package/docs/cli/add.md +163 -0
- package/docs/cli/bun-completions.md +3 -0
- package/docs/cli/bun-create.md +254 -0
- package/docs/cli/bun-dev.md +21 -0
- package/docs/cli/bun-install.md +255 -0
- package/docs/cli/bun-upgrade.md +39 -0
- package/docs/cli/bunx.md +80 -0
- package/docs/cli/filter.md +57 -0
- package/docs/cli/init.md +40 -0
- package/docs/cli/install.md +205 -0
- package/docs/cli/link.md +38 -0
- package/docs/cli/outdated.md +61 -0
- package/docs/cli/patch-commit.md +9 -0
- package/docs/cli/pm.md +150 -0
- package/docs/cli/publish.md +107 -0
- package/docs/cli/remove.md +5 -0
- package/docs/cli/run.md +196 -0
- package/docs/cli/test.md +247 -0
- package/docs/cli/unlink.md +7 -0
- package/docs/cli/update.md +34 -0
- package/docs/contributing/upgrading-webkit.md +57 -0
- package/docs/ecosystem/elysia.md +24 -0
- package/docs/ecosystem/express.md +37 -0
- package/docs/ecosystem/hono.md +18 -0
- package/docs/ecosystem/react.md +65 -0
- package/docs/ecosystem/stric.md +37 -0
- package/docs/guides/binary/arraybuffer-to-array.md +27 -0
- package/docs/guides/binary/arraybuffer-to-blob.md +24 -0
- package/docs/guides/binary/arraybuffer-to-buffer.md +25 -0
- package/docs/guides/binary/arraybuffer-to-string.md +15 -0
- package/docs/guides/binary/arraybuffer-to-typedarray.md +39 -0
- package/docs/guides/binary/blob-to-arraybuffer.md +14 -0
- package/docs/guides/binary/blob-to-dataview.md +14 -0
- package/docs/guides/binary/blob-to-stream.md +14 -0
- package/docs/guides/binary/blob-to-string.md +15 -0
- package/docs/guides/binary/blob-to-typedarray.md +14 -0
- package/docs/guides/binary/buffer-to-arraybuffer.md +14 -0
- package/docs/guides/binary/buffer-to-blob.md +14 -0
- package/docs/guides/binary/buffer-to-readablestream.md +41 -0
- package/docs/guides/binary/buffer-to-string.md +25 -0
- package/docs/guides/binary/buffer-to-typedarray.md +14 -0
- package/docs/guides/binary/dataview-to-string.md +15 -0
- package/docs/guides/binary/typedarray-to-arraybuffer.md +25 -0
- package/docs/guides/binary/typedarray-to-blob.md +16 -0
- package/docs/guides/binary/typedarray-to-buffer.md +14 -0
- package/docs/guides/binary/typedarray-to-dataview.md +14 -0
- package/docs/guides/binary/typedarray-to-readablestream.md +41 -0
- package/docs/guides/binary/typedarray-to-string.md +16 -0
- package/docs/guides/ecosystem/astro.md +72 -0
- package/docs/guides/ecosystem/discordjs.md +77 -0
- package/docs/guides/ecosystem/docker.md +140 -0
- package/docs/guides/ecosystem/drizzle.md +185 -0
- package/docs/guides/ecosystem/edgedb.md +228 -0
- package/docs/guides/ecosystem/elysia.md +31 -0
- package/docs/guides/ecosystem/express.md +40 -0
- package/docs/guides/ecosystem/hono.md +39 -0
- package/docs/guides/ecosystem/mongoose.md +87 -0
- package/docs/guides/ecosystem/neon-drizzle.md +220 -0
- package/docs/guides/ecosystem/neon-serverless-postgres.md +55 -0
- package/docs/guides/ecosystem/nextjs.md +38 -0
- package/docs/guides/ecosystem/nuxt.md +56 -0
- package/docs/guides/ecosystem/pm2.md +57 -0
- package/docs/guides/ecosystem/prisma.md +140 -0
- package/docs/guides/ecosystem/qwik.md +107 -0
- package/docs/guides/ecosystem/react.md +49 -0
- package/docs/guides/ecosystem/remix.md +78 -0
- package/docs/guides/ecosystem/render.md +79 -0
- package/docs/guides/ecosystem/sentry.md +52 -0
- package/docs/guides/ecosystem/solidstart.md +58 -0
- package/docs/guides/ecosystem/ssr-react.md +51 -0
- package/docs/guides/ecosystem/stric.md +55 -0
- package/docs/guides/ecosystem/sveltekit.md +125 -0
- package/docs/guides/ecosystem/systemd.md +113 -0
- package/docs/guides/ecosystem/vite.md +70 -0
- package/docs/guides/http/cluster.md +66 -0
- package/docs/guides/http/fetch-unix.md +33 -0
- package/docs/guides/http/fetch.md +24 -0
- package/docs/guides/http/file-uploads.md +94 -0
- package/docs/guides/http/hot.md +22 -0
- package/docs/guides/http/proxy.md +24 -0
- package/docs/guides/http/server.md +46 -0
- package/docs/guides/http/simple.md +18 -0
- package/docs/guides/http/stream-file.md +48 -0
- package/docs/guides/http/stream-iterator.md +47 -0
- package/docs/guides/http/stream-node-streams-in-bun.md +20 -0
- package/docs/guides/http/tls.md +30 -0
- package/docs/guides/install/add-dev.md +26 -0
- package/docs/guides/install/add-git.md +36 -0
- package/docs/guides/install/add-optional.md +25 -0
- package/docs/guides/install/add-peer.md +17 -0
- package/docs/guides/install/add-tarball.md +33 -0
- package/docs/guides/install/add.md +42 -0
- package/docs/guides/install/azure-artifacts.md +73 -0
- package/docs/guides/install/cicd.md +41 -0
- package/docs/guides/install/custom-registry.md +30 -0
- package/docs/guides/install/from-npm-install-to-bun-install.md +214 -0
- package/docs/guides/install/git-diff-bun-lockfile.md +38 -0
- package/docs/guides/install/jfrog-artifactory.md +28 -0
- package/docs/guides/install/npm-alias.md +23 -0
- package/docs/guides/install/registry-scope.md +36 -0
- package/docs/guides/install/trusted.md +48 -0
- package/docs/guides/install/workspaces.md +70 -0
- package/docs/guides/install/yarnlock.md +42 -0
- package/docs/guides/process/argv.md +57 -0
- package/docs/guides/process/ctrl-c.md +16 -0
- package/docs/guides/process/ipc.md +66 -0
- package/docs/guides/process/nanoseconds.md +13 -0
- package/docs/guides/process/os-signals.md +39 -0
- package/docs/guides/process/spawn-stderr.md +31 -0
- package/docs/guides/process/spawn-stdout.md +26 -0
- package/docs/guides/process/spawn.md +41 -0
- package/docs/guides/process/stdin.md +54 -0
- package/docs/guides/read-file/arraybuffer.md +28 -0
- package/docs/guides/read-file/buffer.md +19 -0
- package/docs/guides/read-file/exists.md +16 -0
- package/docs/guides/read-file/json.md +17 -0
- package/docs/guides/read-file/mime.md +20 -0
- package/docs/guides/read-file/stream.md +26 -0
- package/docs/guides/read-file/string.md +22 -0
- package/docs/guides/read-file/uint8array.md +21 -0
- package/docs/guides/read-file/watch.md +68 -0
- package/docs/guides/runtime/cicd.md +43 -0
- package/docs/guides/runtime/define-constant.md +145 -0
- package/docs/guides/runtime/import-html.md +15 -0
- package/docs/guides/runtime/import-json.md +44 -0
- package/docs/guides/runtime/import-toml.md +30 -0
- package/docs/guides/runtime/read-env.md +32 -0
- package/docs/guides/runtime/set-env.md +37 -0
- package/docs/guides/runtime/shell.md +40 -0
- package/docs/guides/runtime/timezone.md +35 -0
- package/docs/guides/runtime/tsconfig-paths.md +29 -0
- package/docs/guides/runtime/typescript.md +47 -0
- package/docs/guides/runtime/vscode-debugger.md +47 -0
- package/docs/guides/runtime/web-debugger.md +82 -0
- package/docs/guides/streams/node-readable-to-arraybuffer.md +11 -0
- package/docs/guides/streams/node-readable-to-blob.md +11 -0
- package/docs/guides/streams/node-readable-to-json.md +12 -0
- package/docs/guides/streams/node-readable-to-string.md +12 -0
- package/docs/guides/streams/node-readable-to-uint8array.md +11 -0
- package/docs/guides/streams/to-array.md +14 -0
- package/docs/guides/streams/to-arraybuffer.md +14 -0
- package/docs/guides/streams/to-blob.md +14 -0
- package/docs/guides/streams/to-buffer.md +15 -0
- package/docs/guides/streams/to-json.md +14 -0
- package/docs/guides/streams/to-string.md +14 -0
- package/docs/guides/streams/to-typedarray.md +22 -0
- package/docs/guides/test/bail.md +22 -0
- package/docs/guides/test/coverage-threshold.md +60 -0
- package/docs/guides/test/coverage.md +44 -0
- package/docs/guides/test/happy-dom.md +68 -0
- package/docs/guides/test/migrate-from-jest.md +110 -0
- package/docs/guides/test/mock-clock.md +48 -0
- package/docs/guides/test/mock-functions.md +68 -0
- package/docs/guides/test/rerun-each.md +14 -0
- package/docs/guides/test/run-tests.md +111 -0
- package/docs/guides/test/skip-tests.md +39 -0
- package/docs/guides/test/snapshot.md +99 -0
- package/docs/guides/test/spy-on.md +46 -0
- package/docs/guides/test/testing-library.md +87 -0
- package/docs/guides/test/timeout.md +15 -0
- package/docs/guides/test/todo-tests.md +67 -0
- package/docs/guides/test/update-snapshots.md +50 -0
- package/docs/guides/test/watch-mode.md +19 -0
- package/docs/guides/util/base64.md +15 -0
- package/docs/guides/util/deep-equals.md +39 -0
- package/docs/guides/util/deflate.md +18 -0
- package/docs/guides/util/detect-bun.md +23 -0
- package/docs/guides/util/entrypoint.md +17 -0
- package/docs/guides/util/escape-html.md +22 -0
- package/docs/guides/util/file-url-to-path.md +14 -0
- package/docs/guides/util/gzip.md +18 -0
- package/docs/guides/util/hash-a-password.md +54 -0
- package/docs/guides/util/import-meta-dir.md +13 -0
- package/docs/guides/util/import-meta-file.md +13 -0
- package/docs/guides/util/import-meta-path.md +13 -0
- package/docs/guides/util/main.md +32 -0
- package/docs/guides/util/path-to-file-url.md +14 -0
- package/docs/guides/util/sleep.md +22 -0
- package/docs/guides/util/version.md +21 -0
- package/docs/guides/util/which-path-to-executable-bin.md +15 -0
- package/docs/guides/websocket/compression.md +31 -0
- package/docs/guides/websocket/context.md +72 -0
- package/docs/guides/websocket/pubsub.md +38 -0
- package/docs/guides/websocket/simple.md +33 -0
- package/docs/guides/write-file/append.md +52 -0
- package/docs/guides/write-file/basic.md +44 -0
- package/docs/guides/write-file/blob.md +28 -0
- package/docs/guides/write-file/cat.md +17 -0
- package/docs/guides/write-file/file-cp.md +16 -0
- package/docs/guides/write-file/filesink.md +52 -0
- package/docs/guides/write-file/response.md +17 -0
- package/docs/guides/write-file/stdout.md +21 -0
- package/docs/guides/write-file/stream.md +17 -0
- package/docs/guides/write-file/unlink.md +23 -0
- package/docs/index.md +77 -0
- package/docs/install/cache.md +57 -0
- package/docs/install/index.md +202 -0
- package/docs/install/lifecycle.md +46 -0
- package/docs/install/lockfile.md +90 -0
- package/docs/install/npmrc.md +75 -0
- package/docs/install/overrides.md +73 -0
- package/docs/install/patch.md +57 -0
- package/docs/install/registries.md +30 -0
- package/docs/install/workspaces.md +70 -0
- package/docs/installation.md +289 -0
- package/docs/project/benchmarking.md +203 -0
- package/docs/project/building-windows.md +162 -0
- package/docs/project/internals/build-process-for-ci.md +75 -0
- package/docs/project/roadmap.md +87 -0
- package/docs/quickstart.md +144 -0
- package/docs/runtime/autoimport.md +94 -0
- package/docs/runtime/bun-apis.md +129 -0
- package/docs/runtime/bunfig.md +532 -0
- package/docs/runtime/debugger.md +325 -0
- package/docs/runtime/env.md +214 -0
- package/docs/runtime/hot.md +139 -0
- package/docs/runtime/index.md +309 -0
- package/docs/runtime/jsx.md +326 -0
- package/docs/runtime/loaders.md +127 -0
- package/docs/runtime/modules.md +298 -0
- package/docs/runtime/nodejs-apis.md +456 -0
- package/docs/runtime/plugins.md +605 -0
- package/docs/runtime/shell.md +537 -0
- package/docs/runtime/typescript.md +60 -0
- package/docs/runtime/web-apis.md +128 -0
- package/docs/test/coverage.md +91 -0
- package/docs/test/dom.md +75 -0
- package/docs/test/hot.md +15 -0
- package/docs/test/lifecycle.md +81 -0
- package/docs/test/mocks.md +236 -0
- package/docs/test/snapshots.md +15 -0
- package/docs/test/time.md +106 -0
- package/docs/test/writing.md +547 -0
- package/docs/typescript.md +51 -0
- package/package.json +5 -4
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Build an app with SvelteKit and Bun
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Use `sv create my-app` to create a SvelteKit project with SvelteKit CLI. Answer the prompts to select a template and set up your development environment.
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
$ bunx sv create my-app
|
|
9
|
+
┌ Welcome to the Svelte CLI! (v0.5.7)
|
|
10
|
+
│
|
|
11
|
+
◇ Which template would you like?
|
|
12
|
+
│ SvelteKit demo
|
|
13
|
+
│
|
|
14
|
+
◇ Add type checking with Typescript?
|
|
15
|
+
│ Yes, using Typescript syntax
|
|
16
|
+
│
|
|
17
|
+
◆ Project created
|
|
18
|
+
│
|
|
19
|
+
◇ What would you like to add to your project?
|
|
20
|
+
│ none
|
|
21
|
+
│
|
|
22
|
+
◇ Which package manager do you want to install dependencies with?
|
|
23
|
+
│ bun
|
|
24
|
+
│
|
|
25
|
+
◇ Successfully installed dependencies
|
|
26
|
+
│
|
|
27
|
+
◇ Project next steps ─────────────────────────────────────────────────────╮
|
|
28
|
+
│ │
|
|
29
|
+
│ 1: cd my-app │
|
|
30
|
+
│ 2: git init && git add -A && git commit -m "Initial commit" (optional) │
|
|
31
|
+
│ 3: bun run dev -- --open │
|
|
32
|
+
│ │
|
|
33
|
+
│ To close the dev server, hit Ctrl-C │
|
|
34
|
+
│ │
|
|
35
|
+
│ Stuck? Visit us at https://svelte.dev/chat │
|
|
36
|
+
│ │
|
|
37
|
+
├──────────────────────────────────────────────────────────────────────────╯
|
|
38
|
+
│
|
|
39
|
+
└ You're all set!
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
Once the project is initialized, `cd` into the new project. You don't need to run 'bun install' since the dependencies are already installed.
|
|
45
|
+
|
|
46
|
+
Then start the development server with `bun --bun run dev`.
|
|
47
|
+
|
|
48
|
+
To run the dev server with Node.js instead of Bun, you can omit the `--bun` flag.
|
|
49
|
+
|
|
50
|
+
```sh
|
|
51
|
+
$ cd my-app
|
|
52
|
+
$ bun --bun run dev
|
|
53
|
+
$ vite dev
|
|
54
|
+
Forced re-optimization of dependencies
|
|
55
|
+
|
|
56
|
+
VITE v5.4.10 ready in 424 ms
|
|
57
|
+
|
|
58
|
+
➜ Local: http://localhost:5173/
|
|
59
|
+
➜ Network: use --host to expose
|
|
60
|
+
➜ press h + enter to show help
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
---
|
|
64
|
+
|
|
65
|
+
Visit [http://localhost:5173](http://localhost:5173/) in a browser to see the template app.
|
|
66
|
+
|
|
67
|
+
{% image src="https://github.com/oven-sh/bun/assets/3084745/7c76eae8-78f9-44fa-9f15-1bd3ca1a47c0" /%}
|
|
68
|
+
|
|
69
|
+
---
|
|
70
|
+
|
|
71
|
+
If you edit and save `src/routes/+page.svelte`, you should see your changes hot-reloaded in the browser.
|
|
72
|
+
|
|
73
|
+
---
|
|
74
|
+
|
|
75
|
+
To build for production, you'll need to add the right SvelteKit adapter. Currently we recommend the
|
|
76
|
+
|
|
77
|
+
`bun add -D svelte-adapter-bun`.
|
|
78
|
+
|
|
79
|
+
Now, make the following changes to your `svelte.config.js`.
|
|
80
|
+
|
|
81
|
+
```ts-diff
|
|
82
|
+
- import adapter from "@sveltejs/adapter-auto";
|
|
83
|
+
+ import adapter from "svelte-adapter-bun";
|
|
84
|
+
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
|
|
85
|
+
|
|
86
|
+
/** @type {import('@sveltejs/kit').Config} */
|
|
87
|
+
const config = {
|
|
88
|
+
// Consult https://svelte.dev/docs/kit/integrations#preprocessors
|
|
89
|
+
// for more information about preprocessors
|
|
90
|
+
preprocess: vitePreprocess(),
|
|
91
|
+
|
|
92
|
+
kit: {
|
|
93
|
+
// adapter-auto only supports some environments, see https://svelte.dev/docs/kit/adapter-auto for a list.
|
|
94
|
+
// If your environment is not supported, or you settled on a specific environment, switch out the adapter.
|
|
95
|
+
// See https://svelte.dev/docs/kit/adapters for more information about adapters.
|
|
96
|
+
adapter: adapter()
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
export default config;
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
---
|
|
104
|
+
|
|
105
|
+
To build a production bundle:
|
|
106
|
+
|
|
107
|
+
```sh
|
|
108
|
+
$ bun --bun run build
|
|
109
|
+
$ vite build
|
|
110
|
+
vite v5.4.10 building SSR bundle for production...
|
|
111
|
+
"confetti" is imported from external module "@neoconfetti/svelte" but never used in "src/routes/sverdle/+page.svelte".
|
|
112
|
+
✓ 130 modules transformed.
|
|
113
|
+
vite v5.4.10 building for production...
|
|
114
|
+
✓ 148 modules transformed.
|
|
115
|
+
...
|
|
116
|
+
✓ built in 231ms
|
|
117
|
+
...
|
|
118
|
+
✓ built in 899ms
|
|
119
|
+
|
|
120
|
+
Run npm run preview to preview your production build locally.
|
|
121
|
+
|
|
122
|
+
> Using svelte-adapter-bun
|
|
123
|
+
✔ Start server with: bun ./build/index.js
|
|
124
|
+
✔ done
|
|
125
|
+
```
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Run Bun as a daemon with systemd
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
[systemd](https://systemd.io) is an init system and service manager for Linux operating systems that manages the startup and control of system processes and services.
|
|
6
|
+
|
|
7
|
+
<!-- systemd provides aggressive parallelization capabilities, uses socket and D-Bus activation for starting services, offers on-demand starting of daemons, keeps track of processes using Linux control groups, maintains mount and auto mount points, and implements an elaborate transactional dependency-based service control logic. systemd supports SysV and LSB init scripts and works as a replacement for sysvinit. -->
|
|
8
|
+
|
|
9
|
+
<!-- Other parts include a logging daemon, utilities to control basic system configuration like the hostname, date, locale, maintain a list of logged-in users and running containers and virtual machines, system accounts, runtime directories and settings, and daemons to manage simple network configuration, network time synchronization, log forwarding, and name resolution. -->
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
To run a Bun application as a daemon using **systemd** you'll need to create a _service file_ in `/lib/systemd/system/`.
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
$ cd /lib/systemd/system
|
|
17
|
+
$ touch my-app.service
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
Here is a typical service file that runs an application on system start. You can use this as a template for your own service. Replace `YOUR_USER` with the name of the user you want to run the application as. To run as `root`, replace `YOUR_USER` with `root`, though this is generally not recommended for security reasons.
|
|
23
|
+
|
|
24
|
+
Refer to the [systemd documentation](https://www.freedesktop.org/software/systemd/man/systemd.service.html) for more information on each setting.
|
|
25
|
+
|
|
26
|
+
```ini#my-app.service
|
|
27
|
+
[Unit]
|
|
28
|
+
# describe the app
|
|
29
|
+
Description=My App
|
|
30
|
+
# start the app after the network is available
|
|
31
|
+
After=network.target
|
|
32
|
+
|
|
33
|
+
[Service]
|
|
34
|
+
# usually you'll use 'simple'
|
|
35
|
+
# one of https://www.freedesktop.org/software/systemd/man/systemd.service.html#Type=
|
|
36
|
+
Type=simple
|
|
37
|
+
# which user to use when starting the app
|
|
38
|
+
User=YOUR_USER
|
|
39
|
+
# path to your application's root directory
|
|
40
|
+
WorkingDirectory=/home/YOUR_USER/path/to/my-app
|
|
41
|
+
# the command to start the app
|
|
42
|
+
# requires absolute paths
|
|
43
|
+
ExecStart=/home/YOUR_USER/.bun/bin/bun run index.ts
|
|
44
|
+
# restart policy
|
|
45
|
+
# one of {no|on-success|on-failure|on-abnormal|on-watchdog|on-abort|always}
|
|
46
|
+
Restart=always
|
|
47
|
+
|
|
48
|
+
[Install]
|
|
49
|
+
# start the app automatically
|
|
50
|
+
WantedBy=multi-user.target
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
---
|
|
54
|
+
|
|
55
|
+
If your application starts a webserver, note that non-`root` users are not able to listen on ports 80 or 443 by default. To permanently allow Bun to listen on these ports when executed by a non-`root` user, use the following command. This step isn't necessary when running as `root`.
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
$ sudo setcap CAP_NET_BIND_SERVICE=+eip ~/.bun/bin/bun
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
---
|
|
62
|
+
|
|
63
|
+
With the service file configured, you can now _enable_ the service. Once enabled, it will start automatically on reboot. This requires `sudo` permissions.
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
$ sudo systemctl enable my-app
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
---
|
|
70
|
+
|
|
71
|
+
To start the service without rebooting, you can manually _start_ it.
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
$ sudo systemctl start my-app
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
---
|
|
78
|
+
|
|
79
|
+
Check the status of your application with `systemctl status`. If you've started your app successfully, you should see something like this:
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
$ sudo systemctl status my-app
|
|
83
|
+
● my-app.service - My App
|
|
84
|
+
Loaded: loaded (/lib/systemd/system/my-app.service; enabled; preset: enabled)
|
|
85
|
+
Active: active (running) since Thu 2023-10-12 11:34:08 UTC; 1h 8min ago
|
|
86
|
+
Main PID: 309641 (bun)
|
|
87
|
+
Tasks: 3 (limit: 503)
|
|
88
|
+
Memory: 40.9M
|
|
89
|
+
CPU: 1.093s
|
|
90
|
+
CGroup: /system.slice/my-app.service
|
|
91
|
+
└─309641 /home/YOUR_USER/.bun/bin/bun run /home/YOUR_USER/application/index.ts
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
To update the service, edit the contents of the service file, then reload the daemon.
|
|
97
|
+
|
|
98
|
+
```bash
|
|
99
|
+
$ sudo systemctl daemon-reload
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
---
|
|
103
|
+
|
|
104
|
+
For a complete guide on the service unit configuration, you can check [this page](https://www.freedesktop.org/software/systemd/man/systemd.service.html). Or refer to this cheatsheet of common commands:
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
$ sudo systemctl daemon-reload # tell systemd that some files got changed
|
|
108
|
+
$ sudo systemctl enable my-app # enable the app (to allow auto-start)
|
|
109
|
+
$ sudo systemctl disable my-app # disable the app (turns off auto-start)
|
|
110
|
+
$ sudo systemctl start my-app # start the app if is stopped
|
|
111
|
+
$ sudo systemctl stop my-app # stop the app
|
|
112
|
+
$ sudo systemctl restart my-app # restart the app
|
|
113
|
+
```
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Build a frontend using Vite and Bun
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
{% callout %}
|
|
6
|
+
While Vite currently works with Bun, it has not been heavily optimized, nor has Vite been adapted to use Bun's bundler, module resolver, or transpiler.
|
|
7
|
+
{% /callout %}
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
Vite works out of the box with Bun. Get started with one of Vite's templates.
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
$ bun create vite my-app
|
|
15
|
+
✔ Select a framework: › React
|
|
16
|
+
✔ Select a variant: › TypeScript + SWC
|
|
17
|
+
Scaffolding project in /path/to/my-app...
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
Then `cd` into the project directory and install dependencies.
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
cd my-app
|
|
26
|
+
bun install
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
Start the development server with the `vite` CLI using `bunx`.
|
|
32
|
+
|
|
33
|
+
The `--bun` flag tells Bun to run Vite's CLI using `bun` instead of `node`; by default Bun respects Vite's `#!/usr/bin/env node` [shebang line](<https://en.wikipedia.org/wiki/Shebang_(Unix)>).
|
|
34
|
+
```bash
|
|
35
|
+
bunx --bun vite
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
To simplify this command, update the `"dev"` script in `package.json` to the following.
|
|
41
|
+
|
|
42
|
+
```json-diff#package.json
|
|
43
|
+
"scripts": {
|
|
44
|
+
- "dev": "vite",
|
|
45
|
+
+ "dev": "bunx --bun vite",
|
|
46
|
+
"build": "vite build",
|
|
47
|
+
"serve": "vite preview"
|
|
48
|
+
},
|
|
49
|
+
// ...
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
Now you can start the development server with `bun run dev`.
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
bun run dev
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
---
|
|
61
|
+
|
|
62
|
+
The following command will build your app for production.
|
|
63
|
+
|
|
64
|
+
```sh
|
|
65
|
+
$ bunx --bun vite build
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
70
|
+
This is a stripped down guide to get you started with Vite + Bun. For more information, see the [Vite documentation](https://vitejs.dev/guide/).
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Start a cluster of HTTP servers
|
|
3
|
+
description: Run multiple HTTP servers concurrently via the "reusePort" option to share the same port across multiple processes
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
To run multiple HTTP servers concurrently, use the `reusePort` option in `Bun.serve()` which shares the same port across multiple processes.
|
|
7
|
+
|
|
8
|
+
This automatically load balances incoming requests across multiple instances of Bun.
|
|
9
|
+
|
|
10
|
+
```ts#server.ts
|
|
11
|
+
import { serve } from "bun";
|
|
12
|
+
|
|
13
|
+
const id = Math.random().toString(36).slice(2);
|
|
14
|
+
|
|
15
|
+
serve({
|
|
16
|
+
port: process.env.PORT || 8080,
|
|
17
|
+
development: false,
|
|
18
|
+
|
|
19
|
+
// Share the same port across multiple processes
|
|
20
|
+
// This is the important part!
|
|
21
|
+
reusePort: true,
|
|
22
|
+
|
|
23
|
+
async fetch(request) {
|
|
24
|
+
return new Response("Hello from Bun #" + id + "!\n");
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
{% callout %}
|
|
32
|
+
**Linux only** — Windows and macOS ignore the `reusePort` option. This is an operating system limitation with `SO_REUSEPORT`, unfortunately.
|
|
33
|
+
{% /callout %}
|
|
34
|
+
|
|
35
|
+
After saving the file, start your servers on the same port.
|
|
36
|
+
|
|
37
|
+
Under the hood, this uses the Linux `SO_REUSEPORT` and `SO_REUSEADDR` socket options to ensure fair load balancing across multiple processes. [Learn more about `SO_REUSEPORT` and `SO_REUSEADDR`](https://lwn.net/Articles/542629/)
|
|
38
|
+
|
|
39
|
+
```ts#cluster.ts
|
|
40
|
+
import { spawn } from "bun";
|
|
41
|
+
|
|
42
|
+
const cpus = navigator.hardwareConcurrency; // Number of CPU cores
|
|
43
|
+
const buns = new Array(cpus);
|
|
44
|
+
|
|
45
|
+
for (let i = 0; i < cpus; i++) {
|
|
46
|
+
buns[i] = spawn({
|
|
47
|
+
cmd: ["bun", "./server.ts"],
|
|
48
|
+
stdout: "inherit",
|
|
49
|
+
stderr: "inherit",
|
|
50
|
+
stdin: "inherit",
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function kill() {
|
|
55
|
+
for (const bun of buns) {
|
|
56
|
+
bun.kill();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
process.on("SIGINT", kill);
|
|
61
|
+
process.on("exit", kill);
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
Bun has also implemented the `node:cluster` module, but this is a faster, simple, and limited alternative.
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: fetch with unix domain sockets in Bun
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
In Bun, the `unix` option in `fetch()` lets you send HTTP requests over a [unix domain socket](https://en.wikipedia.org/wiki/Unix_domain_socket).
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
const unix = "/var/run/docker.sock";
|
|
9
|
+
|
|
10
|
+
const response = await fetch("http://localhost/info", { unix });
|
|
11
|
+
|
|
12
|
+
const body = await response.json();
|
|
13
|
+
console.log(body); // { ... }
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
The `unix` option is a string that specifies the local file path to a unix domain socket. The `fetch()` function will use the socket to send the request to the server instead of using a TCP network connection. `https` is also supported by using the `https://` protocol in the URL instead of `http://`.
|
|
19
|
+
|
|
20
|
+
To send a `POST` request to an API endpoint over a unix domain socket:
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
const response = await fetch("https://hostname/a/path", {
|
|
24
|
+
unix: "/var/run/path/to/unix.sock",
|
|
25
|
+
method: "POST",
|
|
26
|
+
body: JSON.stringify({ message: "Hello from Bun!" }),
|
|
27
|
+
headers: {
|
|
28
|
+
"Content-Type": "application/json",
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const body = await response.json();
|
|
33
|
+
```
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Send an HTTP request using fetch
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Bun implements the Web-standard [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) API for sending HTTP requests. To send a simple `GET` request to a URL:
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
const response = await fetch("https://bun.sh");
|
|
9
|
+
const html = await response.text(); // HTML string
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
To send a `POST` request to an API endpoint.
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
const response = await fetch("https://bun.sh/api", {
|
|
18
|
+
method: "POST",
|
|
19
|
+
body: JSON.stringify({ message: "Hello from Bun!" }),
|
|
20
|
+
headers: { "Content-Type": "application/json" },
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const body = await response.json();
|
|
24
|
+
```
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Upload files via HTTP using FormData
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
To upload files via HTTP with Bun, use the [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData) API. Let's start with a HTTP server that serves a simple HTML web form.
|
|
6
|
+
|
|
7
|
+
```ts#index.ts
|
|
8
|
+
const server = Bun.serve({
|
|
9
|
+
port: 4000,
|
|
10
|
+
async fetch(req) {
|
|
11
|
+
const url = new URL(req.url);
|
|
12
|
+
|
|
13
|
+
// return index.html for root path
|
|
14
|
+
if (url.pathname === "/")
|
|
15
|
+
return new Response(Bun.file("index.html"), {
|
|
16
|
+
headers: {
|
|
17
|
+
"Content-Type": "text/html",
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
return new Response("Not Found", { status: 404 });
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
console.log(`Listening on http://localhost:${server.port}`);
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
We can define our HTML form in another file, `index.html`.
|
|
31
|
+
|
|
32
|
+
```html#index.html
|
|
33
|
+
<!DOCTYPE html>
|
|
34
|
+
<html>
|
|
35
|
+
<head>
|
|
36
|
+
<meta charset="utf-8" />
|
|
37
|
+
<title>Form</title>
|
|
38
|
+
</head>
|
|
39
|
+
<body>
|
|
40
|
+
<form action="/action" method="post" enctype="multipart/form-data">
|
|
41
|
+
<input type="text" name="name" placeholder="Name" />
|
|
42
|
+
<input type="file" name="profilePicture" />
|
|
43
|
+
<input type="submit" value="Submit" />
|
|
44
|
+
</form>
|
|
45
|
+
</body>
|
|
46
|
+
</html>
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
At this point, we can run the server and visit [`localhost:4000`](http://localhost:4000) to see our form.
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
$ bun run index.ts
|
|
55
|
+
Listening on http://localhost:4000
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
Our form will send a `POST` request to the `/action` endpoint with the form data. Let's handle that request in our server.
|
|
61
|
+
|
|
62
|
+
First we use the [`.formData()`](https://developer.mozilla.org/en-US/docs/Web/API/Request/formData) method on the incoming `Request` to asynchronously parse its contents to a `FormData` instance. Then we can use the [`.get()`](https://developer.mozilla.org/en-US/docs/Web/API/FormData/get) method to extract the value of the `name` and `profilePicture` fields. Here `name` corresponds to a `string` and `profilePicture` is a `Blob`.
|
|
63
|
+
|
|
64
|
+
Finally, we write the `Blob` to disk using [`Bun.write()`](https://bun.sh/docs/api/file-io#writing-files-bun-write).
|
|
65
|
+
|
|
66
|
+
```ts-diff#index.ts
|
|
67
|
+
const server = Bun.serve({
|
|
68
|
+
port: 4000,
|
|
69
|
+
async fetch(req) {
|
|
70
|
+
const url = new URL(req.url);
|
|
71
|
+
|
|
72
|
+
// return index.html for root path
|
|
73
|
+
if (url.pathname === "/")
|
|
74
|
+
return new Response(Bun.file("index.html"), {
|
|
75
|
+
headers: {
|
|
76
|
+
"Content-Type": "text/html",
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
+ // parse formdata at /action
|
|
81
|
+
+ if (url.pathname === '/action') {
|
|
82
|
+
+ const formdata = await req.formData();
|
|
83
|
+
+ const name = formdata.get('name');
|
|
84
|
+
+ const profilePicture = formdata.get('profilePicture');
|
|
85
|
+
+ if (!profilePicture) throw new Error('Must upload a profile picture.');
|
|
86
|
+
+ // write profilePicture to disk
|
|
87
|
+
+ await Bun.write('profilePicture.png', profilePicture);
|
|
88
|
+
+ return new Response("Success");
|
|
89
|
+
+ }
|
|
90
|
+
|
|
91
|
+
return new Response("Not Found", { status: 404 });
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
```
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Hot reload an HTTP server
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Bun supports the [`--hot`](https://bun.sh/docs/runtime/hot#hot-mode) flag to run a file with hot reloading enabled. When any module or file changes, Bun re-runs the file.
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
bun --hot run index.ts
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
Bun detects when you are running an HTTP server with `Bun.serve()`. It reloads your fetch handler when source files change, _without_ restarting the `bun` process. This makes hot reloads nearly instantaneous.
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
Bun.serve({
|
|
17
|
+
port: 3000,
|
|
18
|
+
fetch(req) {
|
|
19
|
+
return new Response(`Hello world`);
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
```
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Proxy HTTP requests using fetch()
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
In Bun, `fetch` supports sending requests through an HTTP or HTTPS proxy. This is useful on corporate networks or when you need to ensure a request is sent through a specific IP address.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
await fetch("https://example.com", {
|
|
9
|
+
// The URL of the proxy server
|
|
10
|
+
proxy: "https://username:password@proxy.example.com:8080",
|
|
11
|
+
});
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
The `proxy` option is a URL string that specifies the proxy server. It can include the username and password if the proxy requires authentication. It can be `http://` or `https://`.
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
You can also set the `$HTTP_PROXY` or `$HTTPS_PROXY` environment variable to the proxy URL. This is useful when you want to use the same proxy for all requests.
|
|
21
|
+
|
|
22
|
+
```sh
|
|
23
|
+
HTTPS_PROXY=https://username:password@proxy.example.com:8080 bun run index.ts
|
|
24
|
+
```
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Common HTTP server usage
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
This starts an HTTP server listening on port `3000`. It demonstrates basic routing with a number of common responses and also handles POST data from standard forms or as JSON.
|
|
6
|
+
|
|
7
|
+
See [`Bun.serve`](https://bun.sh/docs/api/http) for details.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
const server = Bun.serve({
|
|
11
|
+
async fetch(req) {
|
|
12
|
+
const path = new URL(req.url).pathname;
|
|
13
|
+
|
|
14
|
+
// respond with text/html
|
|
15
|
+
if (path === "/") return new Response("Welcome to Bun!");
|
|
16
|
+
|
|
17
|
+
// redirect
|
|
18
|
+
if (path === "/abc") return Response.redirect("/source", 301);
|
|
19
|
+
|
|
20
|
+
// send back a file (in this case, *this* file)
|
|
21
|
+
if (path === "/source") return new Response(Bun.file(import.meta.path));
|
|
22
|
+
|
|
23
|
+
// respond with JSON
|
|
24
|
+
if (path === "/api") return Response.json({ some: "buns", for: "you" });
|
|
25
|
+
|
|
26
|
+
// receive JSON data to a POST request
|
|
27
|
+
if (req.method === "POST" && path === "/api/post") {
|
|
28
|
+
const data = await req.json();
|
|
29
|
+
console.log("Received JSON:", data);
|
|
30
|
+
return Response.json({ success: true, data });
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// receive POST data from a form
|
|
34
|
+
if (req.method === "POST" && path === "/form") {
|
|
35
|
+
const data = await req.formData();
|
|
36
|
+
console.log(data.get("someField"));
|
|
37
|
+
return new Response("Success");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// 404s
|
|
41
|
+
return new Response("Page not found", { status: 404 });
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
console.log(`Listening on ${server.url}`);
|
|
46
|
+
```
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Write a simple HTTP server
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
This starts an HTTP server listening on port `3000`. It responds to all requests with a `Response` with status `200` and body `"Welcome to Bun!"`.
|
|
6
|
+
|
|
7
|
+
See [`Bun.serve`](https://bun.sh/docs/api/http) for details.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
const server = Bun.serve({
|
|
11
|
+
port: 3000,
|
|
12
|
+
fetch(request) {
|
|
13
|
+
return new Response("Welcome to Bun!");
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
console.log(`Listening on ${server.url}`);
|
|
18
|
+
```
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Stream a file as an HTTP Response
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
This snippet reads a file from disk using [`Bun.file()`](https://bun.sh/docs/api/file-io#reading-files-bun-file). This returns a `BunFile` instance, which can be passed directly into the `new Response` constructor.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
const path = "/path/to/file.txt";
|
|
9
|
+
const file = Bun.file(path);
|
|
10
|
+
const resp = new Response(file);
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
The `Content-Type` is read from the file and automatically set on the `Response`.
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
new Response(Bun.file("./package.json")).headers.get("Content-Type");
|
|
19
|
+
// => application/json;charset=utf-8
|
|
20
|
+
|
|
21
|
+
new Response(Bun.file("./test.txt")).headers.get("Content-Type");
|
|
22
|
+
// => text/plain;charset=utf-8
|
|
23
|
+
|
|
24
|
+
new Response(Bun.file("./index.tsx")).headers.get("Content-Type");
|
|
25
|
+
// => text/javascript;charset=utf-8
|
|
26
|
+
|
|
27
|
+
new Response(Bun.file("./img.png")).headers.get("Content-Type");
|
|
28
|
+
// => image/png
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
Putting it all together with [`Bun.serve()`](https://bun.sh/docs/api/http#bun-serve).
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
// static file server
|
|
37
|
+
Bun.serve({
|
|
38
|
+
async fetch(req) {
|
|
39
|
+
const path = new URL(req.url).pathname;
|
|
40
|
+
const file = Bun.file(path);
|
|
41
|
+
return new Response(file);
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
See [Docs > API > File I/O](https://bun.sh/docs/api/file-io#writing-files-bun-write) for complete documentation of `Bun.write()`.
|