bun-types 1.3.2-canary.20251105T140650 → 1.3.2-canary.20251108T140624
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/bun.d.ts +102 -6
- package/docs/README.md +28 -0
- package/docs/bundler/bytecode.mdx +465 -0
- package/docs/bundler/{css.md → css.mdx} +97 -101
- package/docs/bundler/esbuild.mdx +253 -0
- package/docs/bundler/{executables.md → executables.mdx} +130 -221
- package/docs/bundler/fullstack.mdx +1064 -0
- package/docs/bundler/{hmr.md → hot-reloading.mdx} +74 -79
- package/docs/bundler/{html.md → html-static.mdx} +148 -111
- package/docs/bundler/index.mdx +1499 -0
- package/docs/bundler/loaders.mdx +356 -0
- package/docs/bundler/{macros.md → macros.mdx} +83 -84
- package/docs/bundler/minifier.mdx +1306 -0
- package/docs/bundler/plugins.mdx +411 -0
- package/docs/feedback.mdx +85 -0
- package/docs/guides/binary/{arraybuffer-to-array.md → arraybuffer-to-array.mdx} +3 -1
- package/docs/guides/binary/{arraybuffer-to-blob.md → arraybuffer-to-blob.mdx} +3 -1
- package/docs/guides/binary/{arraybuffer-to-buffer.md → arraybuffer-to-buffer.mdx} +3 -1
- package/docs/guides/binary/{arraybuffer-to-string.md → arraybuffer-to-string.mdx} +3 -1
- package/docs/guides/binary/{arraybuffer-to-typedarray.md → arraybuffer-to-typedarray.mdx} +3 -1
- package/docs/guides/binary/{blob-to-arraybuffer.md → blob-to-arraybuffer.mdx} +3 -1
- package/docs/guides/binary/{blob-to-dataview.md → blob-to-dataview.mdx} +3 -1
- package/docs/guides/binary/{blob-to-stream.md → blob-to-stream.mdx} +3 -1
- package/docs/guides/binary/{blob-to-string.md → blob-to-string.mdx} +3 -1
- package/docs/guides/binary/{blob-to-typedarray.md → blob-to-typedarray.mdx} +3 -1
- package/docs/guides/binary/{buffer-to-arraybuffer.md → buffer-to-arraybuffer.mdx} +3 -1
- package/docs/guides/binary/{buffer-to-blob.md → buffer-to-blob.mdx} +3 -1
- package/docs/guides/binary/{buffer-to-readablestream.md → buffer-to-readablestream.mdx} +3 -1
- package/docs/guides/binary/{buffer-to-string.md → buffer-to-string.mdx} +3 -1
- package/docs/guides/binary/{buffer-to-typedarray.md → buffer-to-typedarray.mdx} +3 -1
- package/docs/guides/binary/{dataview-to-string.md → dataview-to-string.mdx} +3 -1
- package/docs/guides/binary/{typedarray-to-arraybuffer.md → typedarray-to-arraybuffer.mdx} +3 -1
- package/docs/guides/binary/{typedarray-to-blob.md → typedarray-to-blob.mdx} +3 -1
- package/docs/guides/binary/{typedarray-to-buffer.md → typedarray-to-buffer.mdx} +3 -1
- package/docs/guides/binary/{typedarray-to-dataview.md → typedarray-to-dataview.mdx} +3 -1
- package/docs/guides/binary/{typedarray-to-readablestream.md → typedarray-to-readablestream.mdx} +3 -1
- package/docs/guides/binary/{typedarray-to-string.md → typedarray-to-string.mdx} +3 -1
- package/docs/guides/deployment/aws-lambda.mdx +204 -0
- package/docs/guides/deployment/digital-ocean.mdx +161 -0
- package/docs/guides/deployment/google-cloud-run.mdx +197 -0
- package/docs/guides/deployment/{railway.md → railway.mdx} +42 -54
- package/docs/guides/deployment/render.mdx +82 -0
- package/docs/guides/deployment/vercel.mdx +99 -0
- package/docs/guides/ecosystem/{astro.md → astro.mdx} +16 -6
- package/docs/guides/ecosystem/{discordjs.md → discordjs.mdx} +21 -18
- package/docs/guides/ecosystem/{docker.md → docker.mdx} +25 -14
- package/docs/guides/ecosystem/{drizzle.md → drizzle.mdx} +32 -22
- package/docs/guides/ecosystem/{edgedb.md → edgedb.mdx} +63 -34
- package/docs/guides/ecosystem/{elysia.md → elysia.mdx} +11 -11
- package/docs/guides/ecosystem/{express.md → express.mdx} +12 -9
- package/docs/guides/ecosystem/{hono.md → hono.mdx} +16 -8
- package/docs/guides/ecosystem/{mongoose.md → mongoose.mdx} +26 -21
- package/docs/guides/ecosystem/{neon-drizzle.md → neon-drizzle.mdx} +49 -35
- package/docs/guides/ecosystem/{neon-serverless-postgres.md → neon-serverless-postgres.mdx} +16 -11
- package/docs/guides/ecosystem/{nextjs.md → nextjs.mdx} +18 -10
- package/docs/guides/ecosystem/{nuxt.md → nuxt.mdx} +45 -11
- package/docs/guides/ecosystem/{pm2.md → pm2.mdx} +7 -9
- package/docs/guides/ecosystem/prisma-postgres.mdx +169 -0
- package/docs/guides/ecosystem/prisma.mdx +164 -0
- package/docs/guides/ecosystem/{qwik.md → qwik.mdx} +18 -11
- package/docs/guides/ecosystem/{react.md → react.mdx} +9 -7
- package/docs/guides/ecosystem/{remix.md → remix.mdx} +45 -26
- package/docs/guides/ecosystem/{sentry.md → sentry.mdx} +7 -5
- package/docs/guides/ecosystem/{solidstart.md → solidstart.mdx} +21 -13
- package/docs/guides/ecosystem/{ssr-react.md → ssr-react.mdx} +9 -11
- package/docs/guides/ecosystem/{stric.md → stric.mdx} +16 -17
- package/docs/guides/ecosystem/{sveltekit.md → sveltekit.mdx} +42 -29
- package/docs/guides/ecosystem/{systemd.md → systemd.mdx} +27 -26
- package/docs/guides/ecosystem/upstash.mdx +87 -0
- package/docs/guides/ecosystem/{vite.md → vite.mdx} +20 -14
- package/docs/guides/html-rewriter/{extract-links.md → extract-links.mdx} +11 -7
- package/docs/guides/html-rewriter/{extract-social-meta.md → extract-social-meta.mdx} +6 -2
- package/docs/guides/http/{cluster.md → cluster.mdx} +10 -7
- package/docs/guides/http/{fetch-unix.md → fetch-unix.mdx} +5 -3
- package/docs/guides/http/{fetch.md → fetch.mdx} +5 -3
- package/docs/guides/http/{file-uploads.md → file-uploads.mdx} +18 -15
- package/docs/guides/http/{hot.md → hot.mdx} +9 -7
- package/docs/guides/http/{proxy.md → proxy.mdx} +7 -5
- package/docs/guides/http/{server.md → server.mdx} +4 -2
- package/docs/guides/http/{simple.md → simple.mdx} +4 -2
- package/docs/guides/http/{stream-file.md → stream-file.mdx} +6 -4
- package/docs/guides/http/{stream-iterator.md → stream-iterator.mdx} +5 -3
- package/docs/guides/http/{stream-node-streams-in-bun.md → stream-node-streams-in-bun.mdx} +4 -2
- package/docs/guides/http/{tls.md → tls.mdx} +5 -3
- package/docs/guides/index.mdx +10 -0
- package/docs/guides/install/{add-dev.md → add-dev.mdx} +8 -6
- package/docs/guides/install/add-git.mdx +38 -0
- package/docs/guides/install/{add-optional.md → add-optional.mdx} +7 -5
- package/docs/guides/install/{add-peer.md → add-peer.mdx} +13 -11
- package/docs/guides/install/{add-tarball.md → add-tarball.mdx} +7 -5
- package/docs/guides/install/{add.md → add.mdx} +12 -10
- package/docs/guides/install/{azure-artifacts.md → azure-artifacts.mdx} +15 -12
- package/docs/guides/{runtime/cicd.md → install/cicd.mdx} +13 -13
- package/docs/guides/install/{custom-registry.md → custom-registry.mdx} +6 -4
- package/docs/guides/install/{from-npm-install-to-bun-install.md → from-npm-install-to-bun-install.mdx} +50 -34
- package/docs/guides/install/{git-diff-bun-lockfile.md → git-diff-bun-lockfile.mdx} +14 -11
- package/docs/guides/install/{jfrog-artifactory.md → jfrog-artifactory.mdx} +4 -4
- package/docs/guides/install/{npm-alias.md → npm-alias.mdx} +6 -4
- package/docs/guides/install/{registry-scope.md → registry-scope.mdx} +6 -4
- package/docs/guides/install/{trusted.md → trusted.mdx} +22 -18
- package/docs/guides/install/{workspaces.md → workspaces.mdx} +13 -13
- package/docs/guides/install/{yarnlock.md → yarnlock.mdx} +15 -9
- package/docs/guides/process/{argv.md → argv.mdx} +17 -9
- package/docs/guides/process/{ctrl-c.md → ctrl-c.mdx} +4 -2
- package/docs/guides/process/{ipc.md → ipc.mdx} +13 -10
- package/docs/guides/process/{nanoseconds.md → nanoseconds.mdx} +3 -1
- package/docs/guides/process/{os-signals.md → os-signals.mdx} +3 -1
- package/docs/guides/process/{spawn-stderr.md → spawn-stderr.mdx} +4 -1
- package/docs/guides/process/{spawn-stdout.md → spawn-stdout.mdx} +3 -1
- package/docs/guides/process/{spawn.md → spawn.mdx} +3 -1
- package/docs/guides/process/{stdin.md → stdin.mdx} +15 -7
- package/docs/guides/read-file/{arraybuffer.md → arraybuffer.mdx} +5 -3
- package/docs/guides/read-file/{buffer.md → buffer.mdx} +4 -2
- package/docs/guides/read-file/{exists.md → exists.mdx} +4 -2
- package/docs/guides/read-file/{json.md → json.mdx} +4 -2
- package/docs/guides/read-file/{mime.md → mime.mdx} +3 -1
- package/docs/guides/read-file/{stream.md → stream.mdx} +3 -1
- package/docs/guides/read-file/{string.md → string.mdx} +3 -1
- package/docs/guides/read-file/{uint8array.md → uint8array.mdx} +3 -1
- package/docs/guides/read-file/{watch.md → watch.mdx} +6 -8
- package/docs/guides/runtime/{build-time-constants.md → build-time-constants.mdx} +24 -22
- package/docs/guides/{install/cicd.md → runtime/cicd.mdx} +11 -7
- package/docs/guides/runtime/{codesign-macos-executable.md → codesign-macos-executable.mdx} +14 -9
- package/docs/guides/runtime/{define-constant.md → define-constant.mdx} +20 -16
- package/docs/guides/runtime/{delete-directory.md → delete-directory.mdx} +5 -3
- package/docs/guides/runtime/{delete-file.md → delete-file.mdx} +4 -2
- package/docs/guides/runtime/{heap-snapshot.md → heap-snapshot.mdx} +5 -3
- package/docs/guides/runtime/{import-html.md → import-html.mdx} +4 -2
- package/docs/guides/runtime/{import-json.md → import-json.mdx} +6 -4
- package/docs/guides/runtime/{import-toml.md → import-toml.mdx} +5 -3
- package/docs/guides/runtime/{import-yaml.md → import-yaml.mdx} +9 -7
- package/docs/guides/runtime/{read-env.md → read-env.mdx} +10 -5
- package/docs/guides/runtime/{set-env.md → set-env.mdx} +14 -10
- package/docs/guides/runtime/{shell.md → shell.mdx} +6 -4
- package/docs/guides/runtime/{timezone.md → timezone.mdx} +10 -7
- package/docs/guides/runtime/{tsconfig-paths.md → tsconfig-paths.mdx} +5 -3
- package/docs/guides/runtime/{typescript.md → typescript.mdx} +8 -6
- package/docs/guides/runtime/{vscode-debugger.md → vscode-debugger.mdx} +12 -11
- package/docs/guides/runtime/{web-debugger.md → web-debugger.mdx} +39 -18
- package/docs/guides/streams/{node-readable-to-arraybuffer.md → node-readable-to-arraybuffer.mdx} +3 -1
- package/docs/guides/streams/{node-readable-to-blob.md → node-readable-to-blob.mdx} +3 -1
- package/docs/guides/streams/{node-readable-to-json.md → node-readable-to-json.mdx} +3 -1
- package/docs/guides/streams/{node-readable-to-string.md → node-readable-to-string.mdx} +3 -1
- package/docs/guides/streams/{node-readable-to-uint8array.md → node-readable-to-uint8array.mdx} +3 -1
- package/docs/guides/streams/{to-array.md → to-array.mdx} +3 -1
- package/docs/guides/streams/{to-arraybuffer.md → to-arraybuffer.mdx} +3 -1
- package/docs/guides/streams/{to-blob.md → to-blob.mdx} +3 -1
- package/docs/guides/streams/{to-buffer.md → to-buffer.mdx} +3 -1
- package/docs/guides/streams/{to-json.md → to-json.mdx} +4 -2
- package/docs/guides/streams/{to-string.md → to-string.mdx} +3 -1
- package/docs/guides/streams/{to-typedarray.md → to-typedarray.mdx} +3 -1
- package/docs/guides/test/{bail.md → bail.mdx} +7 -5
- package/docs/guides/test/{coverage-threshold.md → coverage-threshold.mdx} +14 -7
- package/docs/guides/test/{coverage.md → coverage.mdx} +9 -4
- package/docs/guides/test/{happy-dom.md → happy-dom.mdx} +13 -8
- package/docs/guides/test/{migrate-from-jest.md → migrate-from-jest.mdx} +19 -37
- package/docs/guides/test/{mock-clock.md → mock-clock.mdx} +3 -1
- package/docs/guides/test/{mock-functions.md → mock-functions.mdx} +7 -5
- package/docs/guides/test/{rerun-each.md → rerun-each.mdx} +5 -3
- package/docs/guides/test/{run-tests.md → run-tests.mdx} +16 -11
- package/docs/guides/test/{skip-tests.md → skip-tests.mdx} +8 -4
- package/docs/guides/test/{snapshot.md → snapshot.mdx} +20 -13
- package/docs/guides/test/spy-on.mdx +49 -0
- package/docs/guides/test/{svelte-test.md → svelte-test.mdx} +16 -23
- package/docs/guides/test/{testing-library.md → testing-library.mdx} +18 -17
- package/docs/guides/test/{timeout.md → timeout.mdx} +5 -3
- package/docs/guides/test/{todo-tests.md → todo-tests.mdx} +13 -6
- package/docs/guides/test/{update-snapshots.md → update-snapshots.mdx} +9 -6
- package/docs/guides/test/{watch-mode.md → watch-mode.mdx} +9 -4
- package/docs/guides/util/{base64.md → base64.mdx} +3 -1
- package/docs/guides/util/{deep-equals.md → deep-equals.mdx} +5 -3
- package/docs/guides/util/{deflate.md → deflate.mdx} +3 -1
- package/docs/guides/util/{detect-bun.md → detect-bun.mdx} +3 -1
- package/docs/guides/util/{entrypoint.md → entrypoint.mdx} +4 -2
- package/docs/guides/util/{escape-html.md → escape-html.mdx} +3 -1
- package/docs/guides/util/{file-url-to-path.md → file-url-to-path.mdx} +3 -1
- package/docs/guides/util/{gzip.md → gzip.mdx} +3 -1
- package/docs/guides/util/{hash-a-password.md → hash-a-password.mdx} +3 -1
- package/docs/guides/util/{import-meta-dir.md → import-meta-dir.mdx} +4 -2
- package/docs/guides/util/{import-meta-file.md → import-meta-file.mdx} +4 -2
- package/docs/guides/util/{import-meta-path.md → import-meta-path.mdx} +4 -2
- package/docs/guides/util/{javascript-uuid.md → javascript-uuid.mdx} +3 -1
- package/docs/guides/util/{main.md → main.mdx} +19 -8
- package/docs/guides/util/{path-to-file-url.md → path-to-file-url.mdx} +3 -1
- package/docs/guides/util/{sleep.md → sleep.mdx} +3 -1
- package/docs/guides/util/{version.md → version.mdx} +6 -4
- package/docs/guides/util/{which-path-to-executable-bin.md → which-path-to-executable-bin.mdx} +4 -2
- package/docs/guides/websocket/{compression.md → compression.mdx} +5 -3
- package/docs/guides/websocket/{context.md → context.mdx} +9 -12
- package/docs/guides/websocket/{pubsub.md → pubsub.mdx} +5 -6
- package/docs/guides/websocket/{simple.md → simple.mdx} +5 -3
- package/docs/guides/write-file/{append.md → append.mdx} +3 -1
- package/docs/guides/write-file/{basic.md → basic.mdx} +3 -1
- package/docs/guides/write-file/{blob.md → blob.mdx} +3 -1
- package/docs/guides/write-file/{cat.md → cat.mdx} +4 -2
- package/docs/guides/write-file/{file-cp.md → file-cp.mdx} +3 -1
- package/docs/guides/write-file/{filesink.md → filesink.mdx} +3 -1
- package/docs/guides/write-file/{response.md → response.mdx} +3 -1
- package/docs/guides/write-file/{stdout.md → stdout.mdx} +3 -1
- package/docs/guides/write-file/{stream.md → stream.mdx} +3 -1
- package/docs/guides/write-file/{unlink.md → unlink.mdx} +3 -1
- package/docs/{index.md → index.mdx} +97 -41
- package/docs/installation.mdx +365 -0
- package/docs/{cli/bunx.md → pm/bunx.mdx} +25 -31
- package/docs/{install/catalogs.md → pm/catalogs.mdx} +18 -24
- package/docs/{cli/add.md → pm/cli/add.mdx} +61 -55
- package/docs/{install/audit.md → pm/cli/audit.mdx} +11 -8
- package/docs/{cli/install.md → pm/cli/install.mdx} +208 -81
- package/docs/{cli/link.md → pm/cli/link.mdx} +27 -19
- package/docs/{cli/outdated.md → pm/cli/outdated.mdx} +119 -12
- package/docs/{install/patch.md → pm/cli/patch.mdx} +25 -15
- package/docs/{cli/pm.md → pm/cli/pm.mdx} +96 -62
- package/docs/{cli/publish.md → pm/cli/publish.mdx} +38 -35
- package/docs/pm/cli/remove.mdx +16 -0
- package/docs/{cli/update.md → pm/cli/update.mdx} +26 -15
- package/docs/{cli/why.md → pm/cli/why.mdx} +27 -10
- package/docs/{cli/filter.md → pm/filter.mdx} +26 -14
- package/docs/{install/cache.md → pm/global-cache.mdx} +25 -12
- package/docs/{install/isolated.md → pm/isolated-installs.mdx} +32 -24
- package/docs/{install/lifecycle.md → pm/lifecycle.mdx} +20 -9
- package/docs/{install/lockfile.md → pm/lockfile.mdx} +18 -12
- package/docs/{install/npmrc.md → pm/npmrc.mdx} +30 -24
- package/docs/{install/overrides.md → pm/overrides.mdx} +47 -37
- package/docs/{install/registries.md → pm/scopes-registries.mdx} +8 -3
- package/docs/{install/security-scanner-api.md → pm/security-scanner-api.mdx} +22 -8
- package/docs/{install/workspaces.md → pm/workspaces.mdx} +17 -22
- package/docs/project/{benchmarking.md → benchmarking.mdx} +26 -11
- package/docs/project/{bindgen.md → bindgen.mdx} +12 -14
- package/docs/project/{building-windows.md → building-windows.mdx} +18 -47
- package/docs/project/{contributing.md → contributing.mdx} +56 -60
- package/docs/project/feedback.mdx +20 -0
- package/docs/project/license.mdx +78 -0
- package/docs/project/roadmap.mdx +8 -0
- package/docs/quickstart.mdx +240 -0
- package/docs/runtime/{autoimport.md → auto-install.mdx} +34 -31
- package/docs/{api/binary-data.md → runtime/binary-data.mdx} +77 -269
- package/docs/runtime/bun-apis.mdx +59 -0
- package/docs/runtime/{bunfig.md → bunfig.mdx} +95 -225
- package/docs/{api/cc.md → runtime/c-compiler.mdx} +16 -9
- package/docs/{api/spawn.md → runtime/child-process.mdx} +69 -119
- package/docs/{api/color.md → runtime/color.mdx} +6 -1
- package/docs/{api/console.md → runtime/console.mdx} +17 -7
- package/docs/{api/cookie.md → runtime/cookies.mdx} +24 -19
- package/docs/runtime/{debugger.md → debugger.mdx} +61 -51
- package/docs/runtime/environment-variables.mdx +214 -0
- package/docs/{api/ffi.md → runtime/ffi.mdx} +53 -45
- package/docs/{api/file-io.md → runtime/file-io.mdx} +41 -101
- package/docs/{api/file-system-router.md → runtime/file-system-router.mdx} +8 -2
- package/docs/{bundler/loaders.md → runtime/file-types.mdx} +56 -112
- package/docs/{api/glob.md → runtime/glob.mdx} +4 -1
- package/docs/runtime/globals.mdx +72 -0
- package/docs/{api/hashing.md → runtime/hashing.mdx} +18 -22
- package/docs/{api/html-rewriter.md → runtime/html-rewriter.mdx} +36 -30
- package/docs/runtime/http/cookies.mdx +79 -0
- package/docs/runtime/http/error-handling.mdx +40 -0
- package/docs/runtime/http/metrics.mdx +36 -0
- package/docs/runtime/http/routing.mdx +289 -0
- package/docs/runtime/http/server.mdx +647 -0
- package/docs/runtime/http/tls.mdx +101 -0
- package/docs/{api/websockets.md → runtime/http/websockets.mdx} +62 -231
- package/docs/{cli/run.md → runtime/index.mdx} +70 -108
- package/docs/runtime/jsx.mdx +115 -0
- package/docs/runtime/{modules.md → module-resolution.mdx} +83 -61
- package/docs/{api/dns.md → runtime/networking/dns.mdx} +23 -22
- package/docs/{api/fetch.md → runtime/networking/fetch.mdx} +10 -5
- package/docs/{api/tcp.md → runtime/networking/tcp.mdx} +59 -41
- package/docs/{api/udp.md → runtime/networking/udp.mdx} +14 -8
- package/docs/{api/node-api.md → runtime/node-api.mdx} +5 -0
- package/docs/runtime/{nodejs-apis.md → nodejs-compat.mdx} +9 -4
- package/docs/{bundler/plugins.md → runtime/plugins.mdx} +17 -61
- package/docs/{api/redis.md → runtime/redis.mdx} +65 -80
- package/docs/{api/s3.md → runtime/s3.mdx} +79 -66
- package/docs/{api/secrets.md → runtime/secrets.mdx} +27 -10
- package/docs/{api/semver.md → runtime/semver.mdx} +9 -4
- package/docs/runtime/{shell.md → shell.mdx} +52 -20
- package/docs/{api/sql.md → runtime/sql.mdx} +129 -99
- package/docs/{api/sqlite.md → runtime/sqlite.mdx} +162 -157
- package/docs/{api/streams.md → runtime/streams.mdx} +33 -31
- package/docs/{cli/bun-create.md → runtime/templating/create.mdx} +74 -143
- package/docs/{cli/init.md → runtime/templating/init.mdx} +24 -51
- package/docs/{api/transpiler.md → runtime/transpiler.mdx} +50 -38
- package/docs/{typescript.md → runtime/typescript.mdx} +7 -2
- package/docs/{api/utils.md → runtime/utils.mdx} +81 -50
- package/docs/runtime/{hot.md → watch-mode.mdx} +53 -37
- package/docs/runtime/web-apis.mdx +29 -0
- package/docs/{api/workers.md → runtime/workers.mdx} +28 -22
- package/docs/{api/yaml.md → runtime/yaml.mdx} +33 -232
- package/docs/snippets/cli/add.mdx +166 -0
- package/docs/snippets/cli/build.mdx +196 -0
- package/docs/snippets/cli/feedback.mdx +17 -0
- package/docs/snippets/cli/init.mdx +84 -0
- package/docs/snippets/cli/install.mdx +173 -0
- package/docs/snippets/cli/link.mdx +163 -0
- package/docs/snippets/cli/outdated.mdx +140 -0
- package/docs/snippets/cli/patch.mdx +171 -0
- package/docs/snippets/cli/publish.mdx +198 -0
- package/docs/snippets/cli/remove.mdx +146 -0
- package/docs/snippets/cli/run.mdx +293 -0
- package/docs/snippets/cli/test.mdx +100 -0
- package/docs/snippets/cli/update.mdx +144 -0
- package/docs/snippets/product-card.mdx +32 -0
- package/docs/snippets/product-tiles.mdx +94 -0
- package/docs/test/code-coverage.mdx +409 -0
- package/docs/test/configuration.mdx +467 -0
- package/docs/test/{time.md → dates-times.mdx} +23 -20
- package/docs/test/{discovery.md → discovery.mdx} +15 -10
- package/docs/test/dom.mdx +226 -0
- package/docs/{cli/test.md → test/index.mdx} +77 -94
- package/docs/test/lifecycle.mdx +348 -0
- package/docs/test/mocks.mdx +637 -0
- package/docs/test/{reporters.md → reporters.mdx} +15 -15
- package/docs/test/runtime-behavior.mdx +342 -0
- package/docs/test/snapshots.mdx +434 -0
- package/docs/test/writing-tests.mdx +635 -0
- package/docs/typescript.mdx +54 -0
- package/package.json +8 -6
- package/test.d.ts +2 -2
- package/docs/api/file.md +0 -19
- package/docs/api/globals.md +0 -387
- package/docs/api/http.md +0 -1408
- package/docs/api/import-meta.md +0 -69
- package/docs/benchmarks.md +0 -120
- package/docs/bun-flavored-toml.md +0 -42
- package/docs/bundler/css_modules.md +0 -145
- package/docs/bundler/fullstack.md +0 -418
- package/docs/bundler/index.md +0 -1735
- package/docs/bundler/intro.md +0 -75
- package/docs/bundler/vs-esbuild.md +0 -1127
- package/docs/cli/bun-completions.md +0 -3
- package/docs/cli/bun-install.md +0 -349
- package/docs/cli/bun-upgrade.md +0 -39
- package/docs/cli/info.md +0 -65
- package/docs/cli/patch-commit.md +0 -11
- package/docs/cli/remove.md +0 -7
- package/docs/cli/unlink.md +0 -9
- package/docs/contributing/upgrading-webkit.md +0 -57
- package/docs/ecosystem/elysia.md +0 -24
- package/docs/ecosystem/express.md +0 -37
- package/docs/ecosystem/hono.md +0 -18
- package/docs/ecosystem/react.md +0 -65
- package/docs/ecosystem/stric.md +0 -38
- package/docs/guides/ecosystem/prisma.md +0 -141
- package/docs/guides/ecosystem/render.md +0 -79
- package/docs/guides/install/add-git.md +0 -36
- package/docs/guides/test/spy-on.md +0 -46
- package/docs/install/index.md +0 -248
- package/docs/installation.md +0 -327
- package/docs/project/asan.md +0 -124
- package/docs/project/internals/build-process-for-ci.md +0 -75
- package/docs/project/licensing.md +0 -73
- package/docs/project/roadmap.md +0 -87
- package/docs/quickstart.md +0 -157
- package/docs/runtime/bun-apis.md +0 -207
- package/docs/runtime/env.md +0 -253
- package/docs/runtime/index.md +0 -312
- package/docs/runtime/jsx.md +0 -385
- package/docs/runtime/loaders.md +0 -130
- package/docs/runtime/plugins.md +0 -561
- package/docs/runtime/typescript.md +0 -139
- package/docs/runtime/web-apis.md +0 -128
- package/docs/test/configuration.md +0 -154
- package/docs/test/coverage.md +0 -142
- package/docs/test/dom.md +0 -75
- package/docs/test/examples/concurrent-test-glob.md +0 -132
- package/docs/test/hot.md +0 -15
- package/docs/test/lifecycle.md +0 -81
- package/docs/test/mocks.md +0 -313
- package/docs/test/runtime-behavior.md +0 -95
- package/docs/test/snapshots.md +0 -68
- package/docs/test/writing.md +0 -825
|
@@ -0,0 +1,647 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Server
|
|
3
|
+
description: Use `Bun.serve` to start a high-performance HTTP server in Bun
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
## Basic Setup
|
|
7
|
+
|
|
8
|
+
```ts title="index.ts" icon="/icons/typescript.svg"
|
|
9
|
+
const server = Bun.serve({
|
|
10
|
+
// `routes` requires Bun v1.2.3+
|
|
11
|
+
routes: {
|
|
12
|
+
// Static routes
|
|
13
|
+
"/api/status": new Response("OK"),
|
|
14
|
+
|
|
15
|
+
// Dynamic routes
|
|
16
|
+
"/users/:id": req => {
|
|
17
|
+
return new Response(`Hello User ${req.params.id}!`);
|
|
18
|
+
},
|
|
19
|
+
|
|
20
|
+
// Per-HTTP method handlers
|
|
21
|
+
"/api/posts": {
|
|
22
|
+
GET: () => new Response("List posts"),
|
|
23
|
+
POST: async req => {
|
|
24
|
+
const body = await req.json();
|
|
25
|
+
return Response.json({ created: true, ...body });
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
|
|
29
|
+
// Wildcard route for all routes that start with "/api/" and aren't otherwise matched
|
|
30
|
+
"/api/*": Response.json({ message: "Not found" }, { status: 404 }),
|
|
31
|
+
|
|
32
|
+
// Redirect from /blog/hello to /blog/hello/world
|
|
33
|
+
"/blog/hello": Response.redirect("/blog/hello/world"),
|
|
34
|
+
|
|
35
|
+
// Serve a file by buffering it in memory
|
|
36
|
+
"/favicon.ico": new Response(await Bun.file("./favicon.ico").bytes(), {
|
|
37
|
+
headers: {
|
|
38
|
+
"Content-Type": "image/x-icon",
|
|
39
|
+
},
|
|
40
|
+
}),
|
|
41
|
+
},
|
|
42
|
+
|
|
43
|
+
// (optional) fallback for unmatched routes:
|
|
44
|
+
// Required if Bun's version < 1.2.3
|
|
45
|
+
fetch(req) {
|
|
46
|
+
return new Response("Not Found", { status: 404 });
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
console.log(`Server running at ${server.url}`);
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
---
|
|
54
|
+
|
|
55
|
+
## HTML imports
|
|
56
|
+
|
|
57
|
+
Bun supports importing HTML files directly into your server code, enabling full-stack applications with both server-side and client-side code. HTML imports work in two modes:
|
|
58
|
+
|
|
59
|
+
**Development (`bun --hot`):** Assets are bundled on-demand at runtime, enabling hot module replacement (HMR) for a fast, iterative development experience. When you change your frontend code, the browser automatically updates without a full page reload.
|
|
60
|
+
|
|
61
|
+
**Production (`bun build`):** When building with `bun build --target=bun`, the `import index from "./index.html"` statement resolves to a pre-built manifest object containing all bundled client assets. `Bun.serve` consumes this manifest to serve optimized assets with zero runtime bundling overhead. This is ideal for deploying to production.
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
import myReactSinglePageApp from "./index.html";
|
|
65
|
+
|
|
66
|
+
Bun.serve({
|
|
67
|
+
routes: {
|
|
68
|
+
"/": myReactSinglePageApp,
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
HTML imports don't just serve HTML — it's a full-featured frontend bundler, transpiler, and toolkit built using Bun's [bundler](/bundler), JavaScript transpiler and CSS parser. You can use this to build full-featured frontends with React, TypeScript, Tailwind CSS, and more.
|
|
74
|
+
|
|
75
|
+
For a complete guide on building full-stack applications with HTML imports, including detailed examples and best practices, see [/docs/bundler/fullstack](/bundler/fullstack).
|
|
76
|
+
|
|
77
|
+
---
|
|
78
|
+
|
|
79
|
+
## Configuration
|
|
80
|
+
|
|
81
|
+
### Changing the `port` and `hostname`
|
|
82
|
+
|
|
83
|
+
To configure which port and hostname the server will listen on, set `port` and `hostname` in the options object.
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
Bun.serve({
|
|
87
|
+
port: 8080, // defaults to $BUN_PORT, $PORT, $NODE_PORT otherwise 3000 // [!code ++]
|
|
88
|
+
hostname: "mydomain.com", // defaults to "0.0.0.0" // [!code ++]
|
|
89
|
+
fetch(req) {
|
|
90
|
+
return new Response("404!");
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
To randomly select an available port, set `port` to `0`.
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
const server = Bun.serve({
|
|
99
|
+
port: 0, // random port // [!code ++]
|
|
100
|
+
fetch(req) {
|
|
101
|
+
return new Response("404!");
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// server.port is the randomly selected port
|
|
106
|
+
console.log(server.port);
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
You can view the chosen port by accessing the `port` property on the server object, or by accessing the `url` property.
|
|
110
|
+
|
|
111
|
+
```ts
|
|
112
|
+
console.log(server.port); // 3000
|
|
113
|
+
console.log(server.url); // http://localhost:3000
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### Configuring a default port
|
|
117
|
+
|
|
118
|
+
Bun supports several options and environment variables to configure the default port. The default port is used when the `port` option is not set.
|
|
119
|
+
|
|
120
|
+
- `--port` CLI flag
|
|
121
|
+
|
|
122
|
+
```sh
|
|
123
|
+
bun --port=4002 server.ts
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
- `BUN_PORT` environment variable
|
|
127
|
+
|
|
128
|
+
```sh
|
|
129
|
+
bun_PORT=4002 bun server.ts
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
- `PORT` environment variable
|
|
133
|
+
|
|
134
|
+
```sh terminal icon="terminal"
|
|
135
|
+
PORT=4002 bun server.ts
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
- `NODE_PORT` environment variable
|
|
139
|
+
|
|
140
|
+
```sh terminal icon="terminal"
|
|
141
|
+
NODE_PORT=4002 bun server.ts
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
---
|
|
145
|
+
|
|
146
|
+
## Unix domain sockets
|
|
147
|
+
|
|
148
|
+
To listen on a [unix domain socket](https://en.wikipedia.org/wiki/Unix_domain_socket), pass the `unix` option with the path to the socket.
|
|
149
|
+
|
|
150
|
+
```ts
|
|
151
|
+
Bun.serve({
|
|
152
|
+
unix: "/tmp/my-socket.sock", // path to socket
|
|
153
|
+
fetch(req) {
|
|
154
|
+
return new Response(`404!`);
|
|
155
|
+
},
|
|
156
|
+
});
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### Abstract namespace sockets
|
|
160
|
+
|
|
161
|
+
Bun supports Linux abstract namespace sockets. To use an abstract namespace socket, prefix the `unix` path with a null byte.
|
|
162
|
+
|
|
163
|
+
```ts
|
|
164
|
+
Bun.serve({
|
|
165
|
+
unix: "\0my-abstract-socket", // abstract namespace socket
|
|
166
|
+
fetch(req) {
|
|
167
|
+
return new Response(`404!`);
|
|
168
|
+
},
|
|
169
|
+
});
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
Unlike unix domain sockets, abstract namespace sockets are not bound to the filesystem and are automatically removed when the last reference to the socket is closed.
|
|
173
|
+
|
|
174
|
+
---
|
|
175
|
+
|
|
176
|
+
## idleTimeout
|
|
177
|
+
|
|
178
|
+
To configure the idle timeout, set the `idleTimeout` field in Bun.serve.
|
|
179
|
+
|
|
180
|
+
```ts
|
|
181
|
+
Bun.serve({
|
|
182
|
+
// 10 seconds:
|
|
183
|
+
idleTimeout: 10,
|
|
184
|
+
|
|
185
|
+
fetch(req) {
|
|
186
|
+
return new Response("Bun!");
|
|
187
|
+
},
|
|
188
|
+
});
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
This is the maximum amount of time a connection is allowed to be idle before the server closes it. A connection is idling if there is no data sent or received.
|
|
192
|
+
|
|
193
|
+
---
|
|
194
|
+
|
|
195
|
+
## export default syntax
|
|
196
|
+
|
|
197
|
+
Thus far, the examples on this page have used the explicit `Bun.serve` API. Bun also supports an alternate syntax.
|
|
198
|
+
|
|
199
|
+
```ts server.ts
|
|
200
|
+
import { type Serve } from "bun";
|
|
201
|
+
|
|
202
|
+
export default {
|
|
203
|
+
fetch(req) {
|
|
204
|
+
return new Response("Bun!");
|
|
205
|
+
},
|
|
206
|
+
} satisfies Serve;
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
Instead of passing the server options into `Bun.serve`, `export default` it. This file can be executed as-is; when Bun sees a file with a `default` export containing a `fetch` handler, it passes it into `Bun.serve` under the hood.
|
|
210
|
+
|
|
211
|
+
---
|
|
212
|
+
|
|
213
|
+
## Hot Route Reloading
|
|
214
|
+
|
|
215
|
+
Update routes without server restarts using `server.reload()`:
|
|
216
|
+
|
|
217
|
+
```ts
|
|
218
|
+
const server = Bun.serve({
|
|
219
|
+
routes: {
|
|
220
|
+
"/api/version": () => Response.json({ version: "1.0.0" }),
|
|
221
|
+
},
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
// Deploy new routes without downtime
|
|
225
|
+
server.reload({
|
|
226
|
+
routes: {
|
|
227
|
+
"/api/version": () => Response.json({ version: "2.0.0" }),
|
|
228
|
+
},
|
|
229
|
+
});
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
---
|
|
233
|
+
|
|
234
|
+
## Server Lifecycle Methods
|
|
235
|
+
|
|
236
|
+
### `server.stop()`
|
|
237
|
+
|
|
238
|
+
To stop the server from accepting new connections:
|
|
239
|
+
|
|
240
|
+
```ts
|
|
241
|
+
const server = Bun.serve({
|
|
242
|
+
fetch(req) {
|
|
243
|
+
return new Response("Hello!");
|
|
244
|
+
},
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
// Gracefully stop the server (waits for in-flight requests)
|
|
248
|
+
await server.stop();
|
|
249
|
+
|
|
250
|
+
// Force stop and close all active connections
|
|
251
|
+
await server.stop(true);
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
By default, `stop()` allows in-flight requests and WebSocket connections to complete. Pass `true` to immediately terminate all connections.
|
|
255
|
+
|
|
256
|
+
### `server.ref()` and `server.unref()`
|
|
257
|
+
|
|
258
|
+
Control whether the server keeps the Bun process alive:
|
|
259
|
+
|
|
260
|
+
```ts
|
|
261
|
+
// Don't keep process alive if server is the only thing running
|
|
262
|
+
server.unref();
|
|
263
|
+
|
|
264
|
+
// Restore default behavior - keep process alive
|
|
265
|
+
server.ref();
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
### `server.reload()`
|
|
269
|
+
|
|
270
|
+
Update the server's handlers without restarting:
|
|
271
|
+
|
|
272
|
+
```ts
|
|
273
|
+
const server = Bun.serve({
|
|
274
|
+
routes: {
|
|
275
|
+
"/api/version": Response.json({ version: "v1" }),
|
|
276
|
+
},
|
|
277
|
+
fetch(req) {
|
|
278
|
+
return new Response("v1");
|
|
279
|
+
},
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
// Update to new handler
|
|
283
|
+
server.reload({
|
|
284
|
+
routes: {
|
|
285
|
+
"/api/version": Response.json({ version: "v2" }),
|
|
286
|
+
},
|
|
287
|
+
fetch(req) {
|
|
288
|
+
return new Response("v2");
|
|
289
|
+
},
|
|
290
|
+
});
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
This is useful for development and hot reloading. Only `fetch`, `error`, and `routes` can be updated.
|
|
294
|
+
|
|
295
|
+
---
|
|
296
|
+
|
|
297
|
+
## Per-Request Controls
|
|
298
|
+
|
|
299
|
+
### `server.timeout(Request, seconds)`
|
|
300
|
+
|
|
301
|
+
Set a custom idle timeout for individual requests:
|
|
302
|
+
|
|
303
|
+
```ts
|
|
304
|
+
const server = Bun.serve({
|
|
305
|
+
async fetch(req, server) {
|
|
306
|
+
// Set 60 second timeout for this request
|
|
307
|
+
server.timeout(req, 60);
|
|
308
|
+
|
|
309
|
+
// If they take longer than 60 seconds to send the body, the request will be aborted
|
|
310
|
+
await req.text();
|
|
311
|
+
|
|
312
|
+
return new Response("Done!");
|
|
313
|
+
},
|
|
314
|
+
});
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
Pass `0` to disable the timeout for a request.
|
|
318
|
+
|
|
319
|
+
### `server.requestIP(Request)`
|
|
320
|
+
|
|
321
|
+
Get client IP and port information:
|
|
322
|
+
|
|
323
|
+
```ts
|
|
324
|
+
const server = Bun.serve({
|
|
325
|
+
fetch(req, server) {
|
|
326
|
+
const address = server.requestIP(req);
|
|
327
|
+
if (address) {
|
|
328
|
+
return new Response(`Client IP: ${address.address}, Port: ${address.port}`);
|
|
329
|
+
}
|
|
330
|
+
return new Response("Unknown client");
|
|
331
|
+
},
|
|
332
|
+
});
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
Returns `null` for closed requests or Unix domain sockets.
|
|
336
|
+
|
|
337
|
+
---
|
|
338
|
+
|
|
339
|
+
## Server Metrics
|
|
340
|
+
|
|
341
|
+
### `server.pendingRequests` and `server.pendingWebSockets`
|
|
342
|
+
|
|
343
|
+
Monitor server activity with built-in counters:
|
|
344
|
+
|
|
345
|
+
```ts
|
|
346
|
+
const server = Bun.serve({
|
|
347
|
+
fetch(req, server) {
|
|
348
|
+
return new Response(
|
|
349
|
+
`Active requests: ${server.pendingRequests}\n` + `Active WebSockets: ${server.pendingWebSockets}`,
|
|
350
|
+
);
|
|
351
|
+
},
|
|
352
|
+
});
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
### `server.subscriberCount(topic)`
|
|
356
|
+
|
|
357
|
+
Get count of subscribers for a WebSocket topic:
|
|
358
|
+
|
|
359
|
+
```ts
|
|
360
|
+
const server = Bun.serve({
|
|
361
|
+
fetch(req, server) {
|
|
362
|
+
const chatUsers = server.subscriberCount("chat");
|
|
363
|
+
return new Response(`${chatUsers} users in chat`);
|
|
364
|
+
},
|
|
365
|
+
websocket: {
|
|
366
|
+
message(ws) {
|
|
367
|
+
ws.subscribe("chat");
|
|
368
|
+
},
|
|
369
|
+
},
|
|
370
|
+
});
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
---
|
|
374
|
+
|
|
375
|
+
## Benchmarks
|
|
376
|
+
|
|
377
|
+
Below are Bun and Node.js implementations of a simple HTTP server that responds `Bun!` to each incoming `Request`.
|
|
378
|
+
|
|
379
|
+
```ts Bun
|
|
380
|
+
Bun.serve({
|
|
381
|
+
fetch(req: Request) {
|
|
382
|
+
return new Response("Bun!");
|
|
383
|
+
},
|
|
384
|
+
port: 3000,
|
|
385
|
+
});
|
|
386
|
+
```
|
|
387
|
+
|
|
388
|
+
```ts
|
|
389
|
+
require("http")
|
|
390
|
+
.createServer((req, res) => res.end("Bun!"))
|
|
391
|
+
.listen(8080);
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
The `Bun.serve` server can handle roughly 2.5x more requests per second than Node.js on Linux.
|
|
395
|
+
|
|
396
|
+
| Runtime | Requests per second |
|
|
397
|
+
| ------- | ------------------- |
|
|
398
|
+
| Node 16 | ~64,000 |
|
|
399
|
+
| Bun | ~160,000 |
|
|
400
|
+
|
|
401
|
+
<Frame>
|
|
402
|
+

|
|
403
|
+
</Frame>
|
|
404
|
+
|
|
405
|
+
---
|
|
406
|
+
|
|
407
|
+
## Practical example: REST API
|
|
408
|
+
|
|
409
|
+
Here's a basic database-backed REST API using Bun's router with zero dependencies:
|
|
410
|
+
|
|
411
|
+
<CodeGroup>
|
|
412
|
+
|
|
413
|
+
```ts server.ts expandable icon="file-code"
|
|
414
|
+
import type { Post } from "./types.ts";
|
|
415
|
+
import { Database } from "bun:sqlite";
|
|
416
|
+
|
|
417
|
+
const db = new Database("posts.db");
|
|
418
|
+
db.exec(`
|
|
419
|
+
CREATE TABLE IF NOT EXISTS posts (
|
|
420
|
+
id TEXT PRIMARY KEY,
|
|
421
|
+
title TEXT NOT NULL,
|
|
422
|
+
content TEXT NOT NULL,
|
|
423
|
+
created_at TEXT NOT NULL
|
|
424
|
+
)
|
|
425
|
+
`);
|
|
426
|
+
|
|
427
|
+
Bun.serve({
|
|
428
|
+
routes: {
|
|
429
|
+
// List posts
|
|
430
|
+
"/api/posts": {
|
|
431
|
+
GET: () => {
|
|
432
|
+
const posts = db.query("SELECT * FROM posts").all();
|
|
433
|
+
return Response.json(posts);
|
|
434
|
+
},
|
|
435
|
+
|
|
436
|
+
// Create post
|
|
437
|
+
POST: async req => {
|
|
438
|
+
const post: Omit<Post, "id" | "created_at"> = await req.json();
|
|
439
|
+
const id = crypto.randomUUID();
|
|
440
|
+
|
|
441
|
+
db.query(
|
|
442
|
+
`INSERT INTO posts (id, title, content, created_at)
|
|
443
|
+
VALUES (?, ?, ?, ?)`,
|
|
444
|
+
).run(id, post.title, post.content, new Date().toISOString());
|
|
445
|
+
|
|
446
|
+
return Response.json({ id, ...post }, { status: 201 });
|
|
447
|
+
},
|
|
448
|
+
},
|
|
449
|
+
|
|
450
|
+
// Get post by ID
|
|
451
|
+
"/api/posts/:id": req => {
|
|
452
|
+
const post = db.query("SELECT * FROM posts WHERE id = ?").get(req.params.id);
|
|
453
|
+
|
|
454
|
+
if (!post) {
|
|
455
|
+
return new Response("Not Found", { status: 404 });
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
return Response.json(post);
|
|
459
|
+
},
|
|
460
|
+
},
|
|
461
|
+
|
|
462
|
+
error(error) {
|
|
463
|
+
console.error(error);
|
|
464
|
+
return new Response("Internal Server Error", { status: 500 });
|
|
465
|
+
},
|
|
466
|
+
});
|
|
467
|
+
```
|
|
468
|
+
|
|
469
|
+
```ts types.ts icon="/icons/typescript.svg"
|
|
470
|
+
export interface Post {
|
|
471
|
+
id: string;
|
|
472
|
+
title: string;
|
|
473
|
+
content: string;
|
|
474
|
+
created_at: string;
|
|
475
|
+
}
|
|
476
|
+
```
|
|
477
|
+
|
|
478
|
+
</CodeGroup>
|
|
479
|
+
|
|
480
|
+
---
|
|
481
|
+
|
|
482
|
+
## Reference
|
|
483
|
+
|
|
484
|
+
```ts expandable See TypeScript Definitions
|
|
485
|
+
interface Server extends Disposable {
|
|
486
|
+
/**
|
|
487
|
+
* Stop the server from accepting new connections.
|
|
488
|
+
* @param closeActiveConnections If true, immediately terminates all connections
|
|
489
|
+
* @returns Promise that resolves when the server has stopped
|
|
490
|
+
*/
|
|
491
|
+
stop(closeActiveConnections?: boolean): Promise<void>;
|
|
492
|
+
|
|
493
|
+
/**
|
|
494
|
+
* Update handlers without restarting the server.
|
|
495
|
+
* Only fetch and error handlers can be updated.
|
|
496
|
+
*/
|
|
497
|
+
reload(options: Serve): void;
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* Make a request to the running server.
|
|
501
|
+
* Useful for testing or internal routing.
|
|
502
|
+
*/
|
|
503
|
+
fetch(request: Request | string): Response | Promise<Response>;
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* Upgrade an HTTP request to a WebSocket connection.
|
|
507
|
+
* @returns true if upgrade successful, false if failed
|
|
508
|
+
*/
|
|
509
|
+
upgrade<T = undefined>(
|
|
510
|
+
request: Request,
|
|
511
|
+
options?: {
|
|
512
|
+
headers?: Bun.HeadersInit;
|
|
513
|
+
data?: T;
|
|
514
|
+
},
|
|
515
|
+
): boolean;
|
|
516
|
+
|
|
517
|
+
/**
|
|
518
|
+
* Publish a message to all WebSocket clients subscribed to a topic.
|
|
519
|
+
* @returns Bytes sent, 0 if dropped, -1 if backpressure applied
|
|
520
|
+
*/
|
|
521
|
+
publish(
|
|
522
|
+
topic: string,
|
|
523
|
+
data: string | ArrayBufferView | ArrayBuffer | SharedArrayBuffer,
|
|
524
|
+
compress?: boolean,
|
|
525
|
+
): ServerWebSocketSendStatus;
|
|
526
|
+
|
|
527
|
+
/**
|
|
528
|
+
* Get count of WebSocket clients subscribed to a topic.
|
|
529
|
+
*/
|
|
530
|
+
subscriberCount(topic: string): number;
|
|
531
|
+
|
|
532
|
+
/**
|
|
533
|
+
* Get client IP address and port.
|
|
534
|
+
* @returns null for closed requests or Unix sockets
|
|
535
|
+
*/
|
|
536
|
+
requestIP(request: Request): SocketAddress | null;
|
|
537
|
+
|
|
538
|
+
/**
|
|
539
|
+
* Set custom idle timeout for a request.
|
|
540
|
+
* @param seconds Timeout in seconds, 0 to disable
|
|
541
|
+
*/
|
|
542
|
+
timeout(request: Request, seconds: number): void;
|
|
543
|
+
|
|
544
|
+
/**
|
|
545
|
+
* Keep process alive while server is running.
|
|
546
|
+
*/
|
|
547
|
+
ref(): void;
|
|
548
|
+
|
|
549
|
+
/**
|
|
550
|
+
* Allow process to exit if server is only thing running.
|
|
551
|
+
*/
|
|
552
|
+
unref(): void;
|
|
553
|
+
|
|
554
|
+
/** Number of in-flight HTTP requests */
|
|
555
|
+
readonly pendingRequests: number;
|
|
556
|
+
|
|
557
|
+
/** Number of active WebSocket connections */
|
|
558
|
+
readonly pendingWebSockets: number;
|
|
559
|
+
|
|
560
|
+
/** Server URL including protocol, hostname and port */
|
|
561
|
+
readonly url: URL;
|
|
562
|
+
|
|
563
|
+
/** Port server is listening on */
|
|
564
|
+
readonly port: number;
|
|
565
|
+
|
|
566
|
+
/** Hostname server is bound to */
|
|
567
|
+
readonly hostname: string;
|
|
568
|
+
|
|
569
|
+
/** Whether server is in development mode */
|
|
570
|
+
readonly development: boolean;
|
|
571
|
+
|
|
572
|
+
/** Server instance identifier */
|
|
573
|
+
readonly id: string;
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
interface WebSocketHandler<T = undefined> {
|
|
577
|
+
/** Maximum WebSocket message size in bytes */
|
|
578
|
+
maxPayloadLength?: number;
|
|
579
|
+
|
|
580
|
+
/** Bytes of queued messages before applying backpressure */
|
|
581
|
+
backpressureLimit?: number;
|
|
582
|
+
|
|
583
|
+
/** Whether to close connection when backpressure limit hit */
|
|
584
|
+
closeOnBackpressureLimit?: boolean;
|
|
585
|
+
|
|
586
|
+
/** Called when backpressure is relieved */
|
|
587
|
+
drain?(ws: ServerWebSocket<T>): void | Promise<void>;
|
|
588
|
+
|
|
589
|
+
/** Seconds before idle timeout */
|
|
590
|
+
idleTimeout?: number;
|
|
591
|
+
|
|
592
|
+
/** Enable per-message deflate compression */
|
|
593
|
+
perMessageDeflate?:
|
|
594
|
+
| boolean
|
|
595
|
+
| {
|
|
596
|
+
compress?: WebSocketCompressor | boolean;
|
|
597
|
+
decompress?: WebSocketCompressor | boolean;
|
|
598
|
+
};
|
|
599
|
+
|
|
600
|
+
/** Send ping frames to keep connection alive */
|
|
601
|
+
sendPings?: boolean;
|
|
602
|
+
|
|
603
|
+
/** Whether server receives its own published messages */
|
|
604
|
+
publishToSelf?: boolean;
|
|
605
|
+
|
|
606
|
+
/** Called when connection opened */
|
|
607
|
+
open?(ws: ServerWebSocket<T>): void | Promise<void>;
|
|
608
|
+
|
|
609
|
+
/** Called when message received */
|
|
610
|
+
message(ws: ServerWebSocket<T>, message: string | Buffer): void | Promise<void>;
|
|
611
|
+
|
|
612
|
+
/** Called when connection closed */
|
|
613
|
+
close?(ws: ServerWebSocket<T>, code: number, reason: string): void | Promise<void>;
|
|
614
|
+
|
|
615
|
+
/** Called when ping frame received */
|
|
616
|
+
ping?(ws: ServerWebSocket<T>, data: Buffer): void | Promise<void>;
|
|
617
|
+
|
|
618
|
+
/** Called when pong frame received */
|
|
619
|
+
pong?(ws: ServerWebSocket<T>, data: Buffer): void | Promise<void>;
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
interface TLSOptions {
|
|
623
|
+
/** Certificate authority chain */
|
|
624
|
+
ca?: string | Buffer | BunFile | Array<string | Buffer | BunFile>;
|
|
625
|
+
|
|
626
|
+
/** Server certificate */
|
|
627
|
+
cert?: string | Buffer | BunFile | Array<string | Buffer | BunFile>;
|
|
628
|
+
|
|
629
|
+
/** Path to DH parameters file */
|
|
630
|
+
dhParamsFile?: string;
|
|
631
|
+
|
|
632
|
+
/** Private key */
|
|
633
|
+
key?: string | Buffer | BunFile | Array<string | Buffer | BunFile>;
|
|
634
|
+
|
|
635
|
+
/** Reduce TLS memory usage */
|
|
636
|
+
lowMemoryMode?: boolean;
|
|
637
|
+
|
|
638
|
+
/** Private key passphrase */
|
|
639
|
+
passphrase?: string;
|
|
640
|
+
|
|
641
|
+
/** OpenSSL options flags */
|
|
642
|
+
secureOptions?: number;
|
|
643
|
+
|
|
644
|
+
/** Server name for SNI */
|
|
645
|
+
serverName?: string;
|
|
646
|
+
}
|
|
647
|
+
```
|