bun-types 1.3.2-canary.20251106T140813 → 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/bundler/bytecode.mdx +465 -0
- package/docs/bundler/css.mdx +1024 -0
- package/docs/bundler/esbuild.mdx +253 -0
- package/docs/bundler/executables.mdx +535 -0
- package/docs/bundler/fullstack.mdx +1064 -0
- package/docs/bundler/hot-reloading.mdx +229 -0
- package/docs/bundler/html-static.mdx +386 -0
- package/docs/bundler/index.mdx +1499 -0
- package/docs/bundler/loaders.mdx +356 -0
- package/docs/bundler/macros.mdx +328 -0
- 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.mdx +29 -0
- package/docs/guides/binary/arraybuffer-to-blob.mdx +26 -0
- package/docs/guides/binary/arraybuffer-to-buffer.mdx +27 -0
- package/docs/guides/binary/arraybuffer-to-string.mdx +17 -0
- package/docs/guides/binary/arraybuffer-to-typedarray.mdx +41 -0
- package/docs/guides/binary/blob-to-arraybuffer.mdx +16 -0
- package/docs/guides/binary/blob-to-dataview.mdx +16 -0
- package/docs/guides/binary/blob-to-stream.mdx +16 -0
- package/docs/guides/binary/blob-to-string.mdx +17 -0
- package/docs/guides/binary/blob-to-typedarray.mdx +16 -0
- package/docs/guides/binary/buffer-to-arraybuffer.mdx +16 -0
- package/docs/guides/binary/buffer-to-blob.mdx +16 -0
- package/docs/guides/binary/buffer-to-readablestream.mdx +43 -0
- package/docs/guides/binary/buffer-to-string.mdx +27 -0
- package/docs/guides/binary/buffer-to-typedarray.mdx +16 -0
- package/docs/guides/binary/dataview-to-string.mdx +17 -0
- package/docs/guides/binary/typedarray-to-arraybuffer.mdx +27 -0
- package/docs/guides/binary/typedarray-to-blob.mdx +18 -0
- package/docs/guides/binary/typedarray-to-buffer.mdx +16 -0
- package/docs/guides/binary/typedarray-to-dataview.mdx +16 -0
- package/docs/guides/binary/typedarray-to-readablestream.mdx +43 -0
- package/docs/guides/binary/typedarray-to-string.mdx +18 -0
- 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.mdx +145 -0
- package/docs/guides/deployment/render.mdx +82 -0
- package/docs/guides/deployment/vercel.mdx +99 -0
- package/docs/guides/ecosystem/astro.mdx +82 -0
- package/docs/guides/ecosystem/discordjs.mdx +80 -0
- package/docs/guides/ecosystem/docker.mdx +151 -0
- package/docs/guides/ecosystem/drizzle.mdx +195 -0
- package/docs/guides/ecosystem/edgedb.mdx +257 -0
- package/docs/guides/ecosystem/elysia.mdx +31 -0
- package/docs/guides/ecosystem/express.mdx +43 -0
- package/docs/guides/ecosystem/hono.mdx +47 -0
- package/docs/guides/ecosystem/mongoose.mdx +92 -0
- package/docs/guides/ecosystem/neon-drizzle.mdx +234 -0
- package/docs/guides/ecosystem/neon-serverless-postgres.mdx +60 -0
- package/docs/guides/ecosystem/nextjs.mdx +57 -0
- package/docs/guides/ecosystem/nuxt.mdx +90 -0
- package/docs/guides/ecosystem/pm2.mdx +55 -0
- package/docs/guides/ecosystem/prisma-postgres.mdx +169 -0
- package/docs/guides/ecosystem/prisma.mdx +164 -0
- package/docs/guides/ecosystem/qwik.mdx +114 -0
- package/docs/guides/ecosystem/react.mdx +52 -0
- package/docs/guides/ecosystem/remix.mdx +97 -0
- package/docs/guides/ecosystem/sentry.mdx +54 -0
- package/docs/guides/ecosystem/solidstart.mdx +66 -0
- package/docs/guides/ecosystem/ssr-react.mdx +49 -0
- package/docs/guides/ecosystem/stric.mdx +54 -0
- package/docs/guides/ecosystem/sveltekit.mdx +138 -0
- package/docs/guides/ecosystem/systemd.mdx +114 -0
- package/docs/guides/ecosystem/upstash.mdx +87 -0
- package/docs/guides/ecosystem/vite.mdx +77 -0
- package/docs/guides/html-rewriter/extract-links.mdx +72 -0
- package/docs/guides/html-rewriter/extract-social-meta.mdx +97 -0
- package/docs/guides/http/cluster.mdx +69 -0
- package/docs/guides/http/fetch-unix.mdx +35 -0
- package/docs/guides/http/fetch.mdx +26 -0
- package/docs/guides/http/file-uploads.mdx +97 -0
- package/docs/guides/http/hot.mdx +28 -0
- package/docs/guides/http/proxy.mdx +26 -0
- package/docs/guides/http/server.mdx +48 -0
- package/docs/guides/http/simple.mdx +20 -0
- package/docs/guides/http/stream-file.mdx +50 -0
- package/docs/guides/http/stream-iterator.mdx +49 -0
- package/docs/guides/http/stream-node-streams-in-bun.mdx +22 -0
- package/docs/guides/http/tls.mdx +32 -0
- package/docs/guides/index.mdx +10 -0
- package/docs/guides/install/add-dev.mdx +28 -0
- package/docs/guides/install/add-git.mdx +38 -0
- package/docs/guides/install/add-optional.mdx +27 -0
- package/docs/guides/install/add-peer.mdx +45 -0
- package/docs/guides/install/add-tarball.mdx +35 -0
- package/docs/guides/install/add.mdx +44 -0
- package/docs/guides/install/azure-artifacts.mdx +76 -0
- package/docs/guides/install/cicd.mdx +43 -0
- package/docs/guides/install/custom-registry.mdx +32 -0
- package/docs/guides/install/from-npm-install-to-bun-install.mdx +230 -0
- package/docs/guides/install/git-diff-bun-lockfile.mdx +47 -0
- package/docs/guides/install/jfrog-artifactory.mdx +28 -0
- package/docs/guides/install/npm-alias.mdx +25 -0
- package/docs/guides/install/registry-scope.mdx +40 -0
- package/docs/guides/install/trusted.mdx +50 -0
- package/docs/guides/install/workspaces.mdx +70 -0
- package/docs/guides/install/yarnlock.mdx +50 -0
- package/docs/guides/process/argv.mdx +66 -0
- package/docs/guides/process/ctrl-c.mdx +18 -0
- package/docs/guides/process/ipc.mdx +69 -0
- package/docs/guides/process/nanoseconds.mdx +15 -0
- package/docs/guides/process/os-signals.mdx +41 -0
- package/docs/guides/process/spawn-stderr.mdx +34 -0
- package/docs/guides/process/spawn-stdout.mdx +28 -0
- package/docs/guides/process/spawn.mdx +43 -0
- package/docs/guides/process/stdin.mdx +62 -0
- package/docs/guides/read-file/arraybuffer.mdx +30 -0
- package/docs/guides/read-file/buffer.mdx +21 -0
- package/docs/guides/read-file/exists.mdx +18 -0
- package/docs/guides/read-file/json.mdx +19 -0
- package/docs/guides/read-file/mime.mdx +22 -0
- package/docs/guides/read-file/stream.mdx +28 -0
- package/docs/guides/read-file/string.mdx +24 -0
- package/docs/guides/read-file/uint8array.mdx +23 -0
- package/docs/guides/read-file/watch.mdx +66 -0
- package/docs/guides/runtime/build-time-constants.mdx +295 -0
- package/docs/guides/runtime/cicd.mdx +45 -0
- package/docs/guides/runtime/codesign-macos-executable.mdx +61 -0
- package/docs/guides/runtime/define-constant.mdx +149 -0
- package/docs/guides/runtime/delete-directory.mdx +39 -0
- package/docs/guides/runtime/delete-file.mdx +21 -0
- package/docs/guides/runtime/heap-snapshot.mdx +28 -0
- package/docs/guides/runtime/import-html.mdx +17 -0
- package/docs/guides/runtime/import-json.mdx +46 -0
- package/docs/guides/runtime/import-toml.mdx +32 -0
- package/docs/guides/runtime/import-yaml.mdx +104 -0
- package/docs/guides/runtime/read-env.mdx +37 -0
- package/docs/guides/runtime/set-env.mdx +51 -0
- package/docs/guides/runtime/shell.mdx +42 -0
- package/docs/guides/runtime/timezone.mdx +38 -0
- package/docs/guides/runtime/tsconfig-paths.mdx +31 -0
- package/docs/guides/runtime/typescript.mdx +51 -0
- package/docs/guides/runtime/vscode-debugger.mdx +48 -0
- package/docs/guides/runtime/web-debugger.mdx +103 -0
- package/docs/guides/streams/node-readable-to-arraybuffer.mdx +13 -0
- package/docs/guides/streams/node-readable-to-blob.mdx +13 -0
- package/docs/guides/streams/node-readable-to-json.mdx +14 -0
- package/docs/guides/streams/node-readable-to-string.mdx +14 -0
- package/docs/guides/streams/node-readable-to-uint8array.mdx +13 -0
- package/docs/guides/streams/to-array.mdx +16 -0
- package/docs/guides/streams/to-arraybuffer.mdx +16 -0
- package/docs/guides/streams/to-blob.mdx +16 -0
- package/docs/guides/streams/to-buffer.mdx +17 -0
- package/docs/guides/streams/to-json.mdx +16 -0
- package/docs/guides/streams/to-string.mdx +16 -0
- package/docs/guides/streams/to-typedarray.mdx +24 -0
- package/docs/guides/test/bail.mdx +24 -0
- package/docs/guides/test/coverage-threshold.mdx +67 -0
- package/docs/guides/test/coverage.mdx +49 -0
- package/docs/guides/test/happy-dom.mdx +73 -0
- package/docs/guides/test/migrate-from-jest.mdx +125 -0
- package/docs/guides/test/mock-clock.mdx +50 -0
- package/docs/guides/test/mock-functions.mdx +70 -0
- package/docs/guides/test/rerun-each.mdx +16 -0
- package/docs/guides/test/run-tests.mdx +116 -0
- package/docs/guides/test/skip-tests.mdx +43 -0
- package/docs/guides/test/snapshot.mdx +102 -0
- package/docs/guides/test/spy-on.mdx +49 -0
- package/docs/guides/test/svelte-test.mdx +113 -0
- package/docs/guides/test/testing-library.mdx +93 -0
- package/docs/guides/test/timeout.mdx +17 -0
- package/docs/guides/test/todo-tests.mdx +74 -0
- package/docs/guides/test/update-snapshots.mdx +49 -0
- package/docs/guides/test/watch-mode.mdx +24 -0
- package/docs/guides/util/base64.mdx +17 -0
- package/docs/guides/util/deep-equals.mdx +41 -0
- package/docs/guides/util/deflate.mdx +20 -0
- package/docs/guides/util/detect-bun.mdx +25 -0
- package/docs/guides/util/entrypoint.mdx +19 -0
- package/docs/guides/util/escape-html.mdx +24 -0
- package/docs/guides/util/file-url-to-path.mdx +16 -0
- package/docs/guides/util/gzip.mdx +20 -0
- package/docs/guides/util/hash-a-password.mdx +56 -0
- package/docs/guides/util/import-meta-dir.mdx +15 -0
- package/docs/guides/util/import-meta-file.mdx +15 -0
- package/docs/guides/util/import-meta-path.mdx +15 -0
- package/docs/guides/util/javascript-uuid.mdx +25 -0
- package/docs/guides/util/main.mdx +43 -0
- package/docs/guides/util/path-to-file-url.mdx +16 -0
- package/docs/guides/util/sleep.mdx +24 -0
- package/docs/guides/util/version.mdx +23 -0
- package/docs/guides/util/which-path-to-executable-bin.mdx +17 -0
- package/docs/guides/websocket/compression.mdx +33 -0
- package/docs/guides/websocket/context.mdx +74 -0
- package/docs/guides/websocket/pubsub.mdx +40 -0
- package/docs/guides/websocket/simple.mdx +35 -0
- package/docs/guides/write-file/append.mdx +54 -0
- package/docs/guides/write-file/basic.mdx +46 -0
- package/docs/guides/write-file/blob.mdx +30 -0
- package/docs/guides/write-file/cat.mdx +19 -0
- package/docs/guides/write-file/file-cp.mdx +18 -0
- package/docs/guides/write-file/filesink.mdx +54 -0
- package/docs/guides/write-file/response.mdx +19 -0
- package/docs/guides/write-file/stdout.mdx +23 -0
- package/docs/guides/write-file/stream.mdx +19 -0
- package/docs/guides/write-file/unlink.mdx +18 -0
- package/docs/index.mdx +133 -0
- package/docs/installation.mdx +365 -0
- package/docs/pm/bunx.mdx +83 -0
- package/docs/pm/catalogs.mdx +292 -0
- package/docs/pm/cli/add.mdx +179 -0
- package/docs/pm/cli/audit.mdx +60 -0
- package/docs/pm/cli/install.mdx +471 -0
- package/docs/pm/cli/link.mdx +48 -0
- package/docs/pm/cli/outdated.mdx +197 -0
- package/docs/pm/cli/patch.mdx +69 -0
- package/docs/pm/cli/pm.mdx +319 -0
- package/docs/pm/cli/publish.mdx +123 -0
- package/docs/pm/cli/remove.mdx +16 -0
- package/docs/pm/cli/update.mdx +140 -0
- package/docs/pm/cli/why.mdx +84 -0
- package/docs/pm/filter.mdx +102 -0
- package/docs/pm/global-cache.mdx +72 -0
- package/docs/pm/isolated-installs.mdx +205 -0
- package/docs/pm/lifecycle.mdx +57 -0
- package/docs/pm/lockfile.mdx +64 -0
- package/docs/pm/npmrc.mdx +111 -0
- package/docs/pm/overrides.mdx +83 -0
- package/docs/pm/scopes-registries.mdx +35 -0
- package/docs/pm/security-scanner-api.mdx +95 -0
- package/docs/pm/workspaces.mdx +109 -0
- package/docs/project/benchmarking.mdx +218 -0
- package/docs/project/bindgen.mdx +223 -0
- package/docs/project/building-windows.mdx +133 -0
- package/docs/project/contributing.mdx +343 -0
- 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/auto-install.mdx +97 -0
- package/docs/runtime/binary-data.mdx +846 -0
- package/docs/runtime/bun-apis.mdx +59 -0
- package/docs/runtime/bunfig.mdx +642 -0
- package/docs/runtime/c-compiler.mdx +204 -0
- package/docs/runtime/child-process.mdx +532 -0
- package/docs/runtime/color.mdx +267 -0
- package/docs/runtime/console.mdx +67 -0
- package/docs/runtime/cookies.mdx +454 -0
- package/docs/runtime/debugger.mdx +335 -0
- package/docs/runtime/environment-variables.mdx +214 -0
- package/docs/runtime/ffi.mdx +565 -0
- package/docs/runtime/file-io.mdx +306 -0
- package/docs/runtime/file-system-router.mdx +118 -0
- package/docs/runtime/file-types.mdx +354 -0
- package/docs/runtime/glob.mdx +181 -0
- package/docs/runtime/globals.mdx +72 -0
- package/docs/runtime/hashing.mdx +315 -0
- package/docs/runtime/html-rewriter.mdx +340 -0
- 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/runtime/http/websockets.mdx +404 -0
- package/docs/runtime/index.mdx +223 -0
- package/docs/runtime/jsx.mdx +115 -0
- package/docs/runtime/module-resolution.mdx +342 -0
- package/docs/runtime/networking/dns.mdx +111 -0
- package/docs/runtime/networking/fetch.mdx +468 -0
- package/docs/runtime/networking/tcp.mdx +239 -0
- package/docs/runtime/networking/udp.mdx +129 -0
- package/docs/runtime/node-api.mdx +19 -0
- package/docs/runtime/nodejs-compat.mdx +468 -0
- package/docs/runtime/plugins.mdx +405 -0
- package/docs/runtime/redis.mdx +582 -0
- package/docs/runtime/s3.mdx +863 -0
- package/docs/runtime/secrets.mdx +336 -0
- package/docs/runtime/semver.mdx +57 -0
- package/docs/runtime/shell.mdx +637 -0
- package/docs/runtime/sql.mdx +1404 -0
- package/docs/runtime/sqlite.mdx +699 -0
- package/docs/runtime/streams.mdx +232 -0
- package/docs/runtime/templating/create.mdx +269 -0
- package/docs/runtime/templating/init.mdx +58 -0
- package/docs/runtime/transpiler.mdx +288 -0
- package/docs/runtime/typescript.mdx +58 -0
- package/docs/runtime/utils.mdx +922 -0
- package/docs/runtime/watch-mode.mdx +161 -0
- package/docs/runtime/web-apis.mdx +29 -0
- package/docs/runtime/workers.mdx +328 -0
- package/docs/runtime/yaml.mdx +469 -0
- 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/dates-times.mdx +129 -0
- package/docs/test/discovery.mdx +90 -0
- package/docs/test/dom.mdx +226 -0
- package/docs/test/index.mdx +380 -0
- package/docs/test/lifecycle.mdx +348 -0
- package/docs/test/mocks.mdx +637 -0
- package/docs/test/reporters.mdx +117 -0
- 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
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Check if two objects are deeply equal
|
|
3
|
+
sidebarTitle: Deep equality
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
Check if two objects are deeply equal. This is used internally by `expect().toEqual()` in Bun's [test runner](https://bun.com/docs/test/writing).
|
|
8
|
+
|
|
9
|
+
```ts index.ts icon="/icons/typescript.svg"
|
|
10
|
+
const a = { a: 1, b: 2, c: { d: 3 } };
|
|
11
|
+
const b = { a: 1, b: 2, c: { d: 3 } };
|
|
12
|
+
|
|
13
|
+
Bun.deepEquals(a, b); // true
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
Pass `true` as a third argument to enable strict mode. This is used internally by `expect().toStrictEqual()` in Bun's [test runner](https://bun.com/docs/test/writing).
|
|
19
|
+
|
|
20
|
+
The following examples would return `true` in non-strict mode but `false` in strict mode.
|
|
21
|
+
|
|
22
|
+
```ts index.ts icon="/icons/typescript.svg"
|
|
23
|
+
// undefined values
|
|
24
|
+
Bun.deepEquals({}, { a: undefined }, true); // false
|
|
25
|
+
|
|
26
|
+
// undefined in arrays
|
|
27
|
+
Bun.deepEquals(["asdf"], ["asdf", undefined], true); // false
|
|
28
|
+
|
|
29
|
+
// sparse arrays
|
|
30
|
+
Bun.deepEquals([, 1], [undefined, 1], true); // false
|
|
31
|
+
|
|
32
|
+
// object literals vs instances w/ same properties
|
|
33
|
+
class Foo {
|
|
34
|
+
a = 1;
|
|
35
|
+
}
|
|
36
|
+
Bun.deepEquals(new Foo(), { a: 1 }, true); // false
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
See [Docs > API > Utils](https://bun.com/docs/api/utils) for more useful utilities.
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Compress and decompress data with DEFLATE
|
|
3
|
+
sidebarTitle: DEFLATE compression
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
Use `Bun.deflateSync()` to compress a `Uint8Array` with DEFLATE.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
const data = Buffer.from("Hello, world!");
|
|
11
|
+
const compressed = Bun.deflateSync("Hello, world!");
|
|
12
|
+
// => Uint8Array
|
|
13
|
+
|
|
14
|
+
const decompressed = Bun.inflateSync(compressed);
|
|
15
|
+
// => Uint8Array
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
See [Docs > API > Utils](https://bun.com/docs/api/utils) for more useful utilities.
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Detect when code is executed with Bun
|
|
3
|
+
sidebarTitle: Detect Bun
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
The recommended way to conditionally detect when code is being executed with `bun` is to check for the existence of the `Bun` global.
|
|
8
|
+
|
|
9
|
+
This is similar to how you'd check for the existence of the `window` variable to detect when code is being executed in a browser.
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
if (typeof Bun !== "undefined") {
|
|
13
|
+
// this code will only run when the file is run with Bun
|
|
14
|
+
}
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
In TypeScript environments, the previous approach will result in a type error unless `@types/bun` is installed. To avoid this, you can check `process.versions` instead.
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
if (process.versions.bun) {
|
|
23
|
+
// this code will only run when the file is run with Bun
|
|
24
|
+
}
|
|
25
|
+
```
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Check if the current file is the entrypoint
|
|
3
|
+
sidebarTitle: Check entrypoint
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
Bun provides a handful of module-specific utilities on the [`import.meta`](https://bun.com/docs/api/import-meta) object. Use `import.meta.main` to check if the current file is the entrypoint of the current process.
|
|
8
|
+
|
|
9
|
+
```ts index.ts icon="/icons/typescript.svg"
|
|
10
|
+
if (import.meta.main) {
|
|
11
|
+
// this file is directly executed with `bun run`
|
|
12
|
+
} else {
|
|
13
|
+
// this file is being imported by another file
|
|
14
|
+
}
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
See [Docs > API > import.meta](https://bun.com/docs/api/import-meta) for complete documentation.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Escape an HTML string
|
|
3
|
+
sidebarTitle: Escape HTML
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
The `Bun.escapeHTML()` utility can be used to escape HTML characters in a string. The following replacements are made.
|
|
8
|
+
|
|
9
|
+
- `"` becomes `"""`
|
|
10
|
+
- `&` becomes `"&"`
|
|
11
|
+
- `'` becomes `"'"`
|
|
12
|
+
- `<` becomes `"<"`
|
|
13
|
+
- `>` becomes `">"`
|
|
14
|
+
|
|
15
|
+
This function is optimized for large input. Non-string types will be converted to a string before escaping.
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
Bun.escapeHTML("<script>alert('Hello World!')</script>");
|
|
19
|
+
// <script>alert('Hello World!')</script>
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
See [Docs > API > Utils](https://bun.com/docs/api/utils) for more useful utilities.
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Convert a file URL to an absolute path
|
|
3
|
+
sidebarTitle: File URL to path
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
Use `Bun.fileURLToPath()` to convert a `file://` URL to an absolute path.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
Bun.fileURLToPath("file:///path/to/file.txt");
|
|
11
|
+
// => "/path/to/file.txt"
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
See [Docs > API > Utils](https://bun.com/docs/api/utils) for more useful utilities.
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Compress and decompress data with gzip
|
|
3
|
+
sidebarTitle: Gzip compression
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
Use `Bun.gzipSync()` to compress a `Uint8Array` with gzip.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
const data = Buffer.from("Hello, world!");
|
|
11
|
+
const compressed = Bun.gzipSync(data);
|
|
12
|
+
// => Uint8Array
|
|
13
|
+
|
|
14
|
+
const decompressed = Bun.gunzipSync(compressed);
|
|
15
|
+
// => Uint8Array
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
See [Docs > API > Utils](https://bun.com/docs/api/utils) for more useful utilities.
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Hash a password
|
|
3
|
+
sidebarTitle: Hash password
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
The `Bun.password.hash()` function provides a fast, built-in mechanism for securely hashing passwords in Bun. No third-party dependencies are required.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
const password = "super-secure-pa$$word";
|
|
11
|
+
|
|
12
|
+
const hash = await Bun.password.hash(password);
|
|
13
|
+
// => $argon2id$v=19$m=65536,t=2,p=1$tFq+9AVr1bfPxQdh6E8DQRhEXg/M/...
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
By default, this uses the [Argon2id](https://en.wikipedia.org/wiki/Argon2) algorithm. Pass a second argument to `Bun.password.hash()` to use a different algorithm or configure the hashing parameters.
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
const password = "super-secure-pa$$word";
|
|
22
|
+
|
|
23
|
+
// use argon2 (default)
|
|
24
|
+
const argonHash = await Bun.password.hash(password, {
|
|
25
|
+
memoryCost: 4, // memory usage in kibibytes
|
|
26
|
+
timeCost: 3, // the number of iterations
|
|
27
|
+
});
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
Bun also implements the [bcrypt](https://en.wikipedia.org/wiki/Bcrypt) algorithm. Specify `algorithm: "bcrypt"` to use it.
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
// use bcrypt
|
|
36
|
+
const bcryptHash = await Bun.password.hash(password, {
|
|
37
|
+
algorithm: "bcrypt",
|
|
38
|
+
cost: 4, // number between 4-31
|
|
39
|
+
});
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
Use `Bun.password.verify()` to verify a password. The algorithm and its parameters are stored in the hash itself, so re-specifying configuration is unnecessary.
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
const password = "super-secure-pa$$word";
|
|
48
|
+
const hash = await Bun.password.hash(password);
|
|
49
|
+
|
|
50
|
+
const isMatch = await Bun.password.verify(password, hash);
|
|
51
|
+
// => true
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
See [Docs > API > Hashing](https://bun.com/docs/api/hashing#bun-password) for complete documentation.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Get the directory of the current file
|
|
3
|
+
sidebarTitle: import.meta.dir
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
Bun provides a handful of module-specific utilities on the [`import.meta`](https://bun.com/docs/api/import-meta) object.
|
|
8
|
+
|
|
9
|
+
```ts /a/b/c.ts icon="/icons/typescript.svg"
|
|
10
|
+
import.meta.dir; // => "/a/b"
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
See [Docs > API > import.meta](https://bun.com/docs/api/import-meta) for complete documentation.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Get the file name of the current file
|
|
3
|
+
sidebarTitle: import.meta.file
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
Bun provides a handful of module-specific utilities on the [`import.meta`](https://bun.com/docs/api/import-meta) object. Use `import.meta.file` to retrieve the name of the current file.
|
|
8
|
+
|
|
9
|
+
```ts /a/b/c.ts icon="/icons/typescript.svg"
|
|
10
|
+
import.meta.file; // => "c.ts"
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
See [Docs > API > import.meta](https://bun.com/docs/api/import-meta) for complete documentation.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Get the absolute path of the current file
|
|
3
|
+
sidebarTitle: import.meta.path
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
Bun provides a handful of module-specific utilities on the [`import.meta`](https://bun.com/docs/api/import-meta) object. Use `import.meta.path` to retrieve the absolute path of the current file.
|
|
8
|
+
|
|
9
|
+
```ts /a/b/c.ts icon="/icons/typescript.svg"
|
|
10
|
+
import.meta.path; // => "/a/b/c.ts"
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
See [Docs > API > import.meta](https://bun.com/docs/api/import-meta) for complete documentation.
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Generate a UUID
|
|
3
|
+
sidebarTitle: Generate UUID
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
Use `crypto.randomUUID()` to generate a UUID v4. This API works in Bun, Node.js, and browsers. It requires no dependencies.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
crypto.randomUUID();
|
|
11
|
+
// => "123e4567-e89b-12d3-a456-426614174000"
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
In Bun, you can also use `Bun.randomUUIDv7()` to generate a [UUID v7](https://www.ietf.org/archive/id/draft-peabody-dispatch-new-uuid-format-01.html).
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
Bun.randomUUIDv7();
|
|
20
|
+
// => "0196a000-bb12-7000-905e-8039f5d5b206"
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
See [Docs > API > Utils](https://bun.com/docs/api/utils) for more useful utilities.
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Get the absolute path to the current entrypoint
|
|
3
|
+
sidebarTitle: Get entrypoint path
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
The `Bun.main` property contains the absolute path to the current entrypoint.
|
|
8
|
+
|
|
9
|
+
<CodeGroup>
|
|
10
|
+
|
|
11
|
+
```ts foo.ts icon="/icons/typescript.svg"
|
|
12
|
+
console.log(Bun.main);
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
```ts index.ts icon="/icons/typescript.svg"
|
|
16
|
+
import "./foo.ts";
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
</CodeGroup>
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
The printed path corresponds to the file that is executed with `bun run`.
|
|
24
|
+
|
|
25
|
+
```sh terminal icon="terminal"
|
|
26
|
+
bun run index.ts
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
```txt
|
|
30
|
+
/path/to/index.ts
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
```sh terminal icon="terminal"
|
|
34
|
+
bun run foo.ts
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
```txt
|
|
38
|
+
/path/to/foo.ts
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
See [Docs > API > Utils](https://bun.com/docs/api/utils) for more useful utilities.
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Convert an absolute path to a file URL
|
|
3
|
+
sidebarTitle: Path to file URL
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
Use `Bun.pathToFileURL()` to convert an absolute path to a `file://` URL.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
Bun.pathToFileURL("/path/to/file.txt");
|
|
11
|
+
// => "file:///path/to/file.txt"
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
See [Docs > API > Utils](https://bun.com/docs/api/utils) for more useful utilities.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Sleep for a fixed number of milliseconds
|
|
3
|
+
sidebarTitle: Sleep
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
The `Bun.sleep` method provides a convenient way to create a void `Promise` that resolves in a fixed number of milliseconds.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
// sleep for 1 second
|
|
11
|
+
await Bun.sleep(1000);
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
Internally, this is equivalent to the following snippet that uses [`setTimeout`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout).
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
await new Promise(resolve => setTimeout(resolve, ms));
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
See [Docs > API > Utils](https://bun.com/docs/api/utils) for more useful utilities.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Get the current Bun version
|
|
3
|
+
sidebarTitle: Get Bun version
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
Get the current version of Bun in a semver format.
|
|
8
|
+
|
|
9
|
+
```ts index.ts icon="/icons/typescript.svg"
|
|
10
|
+
Bun.version; // => "1.3.1"
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
Get the exact `git` commit of [`oven-sh/bun`](https://github.com/oven-sh/bun) that was compiled to produce this Bun binary.
|
|
16
|
+
|
|
17
|
+
```ts index.ts icon="/icons/typescript.svg"
|
|
18
|
+
Bun.revision; // => "49231b2cb9aa48497ab966fc0bb6b742dacc4994"
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
See [Docs > API > Utils](https://bun.com/docs/api/utils) for more useful utilities.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Get the path to an executable bin file
|
|
3
|
+
sidebarTitle: Find executable path
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
`Bun.which` is a utility function to find the absolute path of an executable file. It is similar to the `which` command in Unix-like systems.
|
|
8
|
+
|
|
9
|
+
```ts foo.ts icon="/icons/typescript.svg"
|
|
10
|
+
Bun.which("sh"); // => "/bin/sh"
|
|
11
|
+
Bun.which("notfound"); // => null
|
|
12
|
+
Bun.which("bun"); // => "/home/user/.bun/bin/bun"
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
See [Docs > API > Utils](https://bun.com/docs/api/utils#bun-which) for complete documentation.
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Enable compression for WebSocket messages
|
|
3
|
+
sidebarTitle: Enable compression
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
Per-message compression can be enabled with the `perMessageDeflate` parameter. When set, all messages will be compressed using the [permessage-deflate](https://tools.ietf.org/html/rfc7692) WebSocket extension.
|
|
8
|
+
|
|
9
|
+
```ts server.ts icon="/icons/typescript.svg"
|
|
10
|
+
Bun.serve({
|
|
11
|
+
// ...
|
|
12
|
+
websocket: {
|
|
13
|
+
// enable compression
|
|
14
|
+
perMessageDeflate: true,
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
To enable compression for individual messages, pass `true` as the second parameter to `ws.send()`.
|
|
22
|
+
|
|
23
|
+
```ts server.ts icon="/icons/typescript.svg"
|
|
24
|
+
Bun.serve({
|
|
25
|
+
// ...
|
|
26
|
+
websocket: {
|
|
27
|
+
async message(ws, message) {
|
|
28
|
+
// send a compressed message
|
|
29
|
+
ws.send(message, true);
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
```
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Set per-socket contextual data on a WebSocket
|
|
3
|
+
sidebarTitle: Contextual data
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
When building a WebSocket server, it's typically necessary to store some identifying information or context associated with each connected client.
|
|
8
|
+
|
|
9
|
+
With [Bun.serve()](https://bun.com/docs/api/websockets contextual-data), this "contextual data" is set when the connection is initially upgraded by passing a `data` parameter in the `server.upgrade()` call.
|
|
10
|
+
|
|
11
|
+
```ts server.ts icon="/icons/typescript.svg"
|
|
12
|
+
Bun.serve<{ socketId: number }>({
|
|
13
|
+
fetch(req, server) {
|
|
14
|
+
const success = server.upgrade(req, {
|
|
15
|
+
data: {
|
|
16
|
+
socketId: Math.random(),
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
if (success) return undefined;
|
|
20
|
+
|
|
21
|
+
// handle HTTP request normally
|
|
22
|
+
// ...
|
|
23
|
+
},
|
|
24
|
+
websocket: {
|
|
25
|
+
// define websocket handlers
|
|
26
|
+
async message(ws, message) {
|
|
27
|
+
// the contextual data is available as the `data` property
|
|
28
|
+
// on the WebSocket instance
|
|
29
|
+
console.log(`Received ${message} from ${ws.data.socketId}}`);
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
It's common to read cookies/headers from the incoming request to identify the connecting client.
|
|
38
|
+
|
|
39
|
+
```ts server.ts icon="/icons/typescript.svg"
|
|
40
|
+
type WebSocketData = {
|
|
41
|
+
createdAt: number;
|
|
42
|
+
token: string;
|
|
43
|
+
userId: string;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// TypeScript: specify the type of `data`
|
|
47
|
+
Bun.serve<WebSocketData>({
|
|
48
|
+
async fetch(req, server) {
|
|
49
|
+
// use a library to parse cookies
|
|
50
|
+
const cookies = parseCookies(req.headers.get("Cookie"));
|
|
51
|
+
const token = cookies["X-Token"];
|
|
52
|
+
const user = await getUserFromToken(token);
|
|
53
|
+
|
|
54
|
+
const upgraded = server.upgrade(req, {
|
|
55
|
+
data: {
|
|
56
|
+
createdAt: Date.now(),
|
|
57
|
+
token: cookies["X-Token"],
|
|
58
|
+
userId: user.id,
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
if (upgraded) return undefined;
|
|
63
|
+
},
|
|
64
|
+
websocket: {
|
|
65
|
+
async message(ws, message) {
|
|
66
|
+
// save the message to a database
|
|
67
|
+
await saveMessageToDatabase({
|
|
68
|
+
message: String(message),
|
|
69
|
+
userId: ws.data.userId,
|
|
70
|
+
});
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
```
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Build a publish-subscribe WebSocket server
|
|
3
|
+
sidebarTitle: Pub-sub server
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
Bun's server-side `WebSocket` API provides a native pub-sub API. Sockets can be subscribed to a set of named channels using `socket.subscribe(<name>)`; messages can be published to a channel using `socket.publish(<name>, <message>)`.
|
|
8
|
+
|
|
9
|
+
This code snippet implements a simple single-channel chat server.
|
|
10
|
+
|
|
11
|
+
```ts server.ts icon="/icons/typescript.svg"
|
|
12
|
+
const server = Bun.serve<{ username: string }>({
|
|
13
|
+
fetch(req, server) {
|
|
14
|
+
const cookies = req.headers.get("cookie");
|
|
15
|
+
const username = getUsernameFromCookies(cookies);
|
|
16
|
+
const success = server.upgrade(req, { data: { username } });
|
|
17
|
+
if (success) return undefined;
|
|
18
|
+
|
|
19
|
+
return new Response("Hello world");
|
|
20
|
+
},
|
|
21
|
+
websocket: {
|
|
22
|
+
open(ws) {
|
|
23
|
+
const msg = `${ws.data.username} has entered the chat`;
|
|
24
|
+
ws.subscribe("the-group-chat");
|
|
25
|
+
server.publish("the-group-chat", msg);
|
|
26
|
+
},
|
|
27
|
+
message(ws, message) {
|
|
28
|
+
// the server re-broadcasts incoming messages to everyone
|
|
29
|
+
server.publish("the-group-chat", `${ws.data.username}: ${message}`);
|
|
30
|
+
},
|
|
31
|
+
close(ws) {
|
|
32
|
+
const msg = `${ws.data.username} has left the chat`;
|
|
33
|
+
server.publish("the-group-chat", msg);
|
|
34
|
+
ws.unsubscribe("the-group-chat");
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
console.log(`Listening on ${server.hostname}:${server.port}`);
|
|
40
|
+
```
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Build a simple WebSocket server
|
|
3
|
+
sidebarTitle: Simple server
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
Start a simple WebSocket server using [`Bun.serve`](https://bun.com/docs/api/http).
|
|
8
|
+
|
|
9
|
+
Inside `fetch`, we attempt to upgrade incoming `ws:` or `wss:` requests to WebSocket connections.
|
|
10
|
+
|
|
11
|
+
```ts server.ts icon="/icons/typescript.svg"
|
|
12
|
+
const server = Bun.serve<{ authToken: string }>({
|
|
13
|
+
fetch(req, server) {
|
|
14
|
+
const success = server.upgrade(req);
|
|
15
|
+
if (success) {
|
|
16
|
+
// Bun automatically returns a 101 Switching Protocols
|
|
17
|
+
// if the upgrade succeeds
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// handle HTTP request normally
|
|
22
|
+
return new Response("Hello world!");
|
|
23
|
+
},
|
|
24
|
+
websocket: {
|
|
25
|
+
// this is called when a message is received
|
|
26
|
+
async message(ws, message) {
|
|
27
|
+
console.log(`Received ${message}`);
|
|
28
|
+
// send back a message
|
|
29
|
+
ws.send(`You said: ${message}`);
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
console.log(`Listening on ${server.hostname}:${server.port}`);
|
|
35
|
+
```
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Append content to a file
|
|
3
|
+
sidebarTitle: Append to file
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
Bun implements the `node:fs` module, which includes the `fs.appendFile` and `fs.appendFileSync` functions for appending content to files.
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
You can use `fs.appendFile` to asynchronously append data to a file, creating the file if it does not yet exist. The content can be a string or a `Buffer`.
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { appendFile } from "node:fs/promises";
|
|
15
|
+
|
|
16
|
+
await appendFile("message.txt", "data to append");
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
To use the non-`Promise` API:
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { appendFile } from "node:fs";
|
|
25
|
+
|
|
26
|
+
appendFile("message.txt", "data to append", err => {
|
|
27
|
+
if (err) throw err;
|
|
28
|
+
console.log('The "data to append" was appended to file!');
|
|
29
|
+
});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
---
|
|
33
|
+
|
|
34
|
+
To specify the encoding of the content:
|
|
35
|
+
|
|
36
|
+
```js
|
|
37
|
+
import { appendFile } from "node:fs";
|
|
38
|
+
|
|
39
|
+
appendFile("message.txt", "data to append", "utf8", callback);
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
To append the data synchronously, use `fs.appendFileSync`:
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
import { appendFileSync } from "node:fs";
|
|
48
|
+
|
|
49
|
+
appendFileSync("message.txt", "data to append", "utf8");
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
See the [Node.js documentation](https://nodejs.org/api/fs.html#fspromisesappendfilepath-data-options) for more information.
|