bun-types 1.1.37-canary.20241124T140524 → 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,23 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Detect when code is executed with Bun
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
The recommended way to conditionally detect when code is being executed with `bun` is to check for the existence of the `Bun` global.
|
|
6
|
+
|
|
7
|
+
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.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
if (typeof Bun !== "undefined") {
|
|
11
|
+
// this code will only run when the file is run with Bun
|
|
12
|
+
}
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
In TypeScript environments, the previous approach will result in a type error unless `bun-types` is globally installed. To avoid this, you can check `process.versions` instead.
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
if (process.versions.bun) {
|
|
21
|
+
// this code will only run when the file is run with Bun
|
|
22
|
+
}
|
|
23
|
+
```
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Check if the current file is the entrypoint
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Bun provides a handful of module-specific utilities on the [`import.meta`](https://bun.sh/docs/api/import-meta) object. Use `import.meta.main` to check if the current file is the entrypoint of the current process.
|
|
6
|
+
|
|
7
|
+
```ts#index.ts
|
|
8
|
+
if (import.meta.main) {
|
|
9
|
+
// this file is directly executed with `bun run`
|
|
10
|
+
} else {
|
|
11
|
+
// this file is being imported by another file
|
|
12
|
+
}
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
See [Docs > API > import.meta](https://bun.sh/docs/api/import-meta) for complete documentation.
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Escape an HTML string
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
The `Bun.escapeHTML()` utility can be used to escape HTML characters in a string. The following replacements are made.
|
|
6
|
+
|
|
7
|
+
- `"` becomes `"""`
|
|
8
|
+
- `&` becomes `"&"`
|
|
9
|
+
- `'` becomes `"'"`
|
|
10
|
+
- `<` becomes `"<"`
|
|
11
|
+
- `>` becomes `">"`
|
|
12
|
+
|
|
13
|
+
This function is optimized for large input. Non-string types will be converted to a string before escaping.
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
Bun.escapeHTML("<script>alert('Hello World!')</script>");
|
|
17
|
+
// <script>alert('Hello World!')</script>
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
See [Docs > API > Utils](https://bun.sh/docs/api/utils) for more useful utilities.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Convert a file URL to an absolute path
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Use `Bun.fileURLToPath()` to convert a `file://` URL to an absolute path.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
Bun.fileURLToPath("file:///path/to/file.txt");
|
|
9
|
+
// => "/path/to/file.txt"
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
See [Docs > API > Utils](https://bun.sh/docs/api/utils) for more useful utilities.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Compress and decompress data with gzip
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Use `Bun.gzipSync()` to compress a `Uint8Array` with gzip.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
const data = Buffer.from("Hello, world!");
|
|
9
|
+
const compressed = Bun.gzipSync(data);
|
|
10
|
+
// => Uint8Array
|
|
11
|
+
|
|
12
|
+
const decompressed = Bun.gunzipSync(compressed);
|
|
13
|
+
// => Uint8Array
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
See [Docs > API > Utils](https://bun.sh/docs/api/utils) for more useful utilities.
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Hash a password
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
The `Bun.password.hash()` function provides a fast, built-in mechanism for securely hashing passwords in Bun. No third-party dependencies are required.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
const password = "super-secure-pa$$word";
|
|
9
|
+
|
|
10
|
+
const hash = await Bun.password.hash(password);
|
|
11
|
+
// => $argon2id$v=19$m=65536,t=2,p=1$tFq+9AVr1bfPxQdh6E8DQRhEXg/M/...
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
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.
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
const password = "super-secure-pa$$word";
|
|
20
|
+
|
|
21
|
+
// use argon2 (default)
|
|
22
|
+
const argonHash = await Bun.password.hash(password, {
|
|
23
|
+
memoryCost: 4, // memory usage in kibibytes
|
|
24
|
+
timeCost: 3, // the number of iterations
|
|
25
|
+
});
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
Bun also implements the [bcrypt](https://en.wikipedia.org/wiki/Bcrypt) algorithm. Specify `algorithm: "bcrypt"` to use it.
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
// use bcrypt
|
|
34
|
+
const bcryptHash = await Bun.password.hash(password, {
|
|
35
|
+
algorithm: "bcrypt",
|
|
36
|
+
cost: 4, // number between 4-31
|
|
37
|
+
});
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
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.
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
const password = "super-secure-pa$$word";
|
|
46
|
+
const hash = await Bun.password.hash(password);
|
|
47
|
+
|
|
48
|
+
const isMatch = await Bun.password.verify(password, hash);
|
|
49
|
+
// => true
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
See [Docs > API > Hashing](https://bun.sh/docs/api/hashing#bun-password) for complete documentation.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Get the directory of the current file
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Bun provides a handful of module-specific utilities on the [`import.meta`](https://bun.sh/docs/api/import-meta) object.
|
|
6
|
+
|
|
7
|
+
```ts#/a/b/c.ts
|
|
8
|
+
import.meta.dir; // => "/a/b"
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
See [Docs > API > import.meta](https://bun.sh/docs/api/import-meta) for complete documentation.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Get the file name of the current file
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Bun provides a handful of module-specific utilities on the [`import.meta`](https://bun.sh/docs/api/import-meta) object. Use `import.meta.file` to retrieve the name of the current file.
|
|
6
|
+
|
|
7
|
+
```ts#/a/b/c.ts
|
|
8
|
+
import.meta.file; // => "c.ts"
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
See [Docs > API > import.meta](https://bun.sh/docs/api/import-meta) for complete documentation.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Get the absolute path of the current file
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Bun provides a handful of module-specific utilities on the [`import.meta`](https://bun.sh/docs/api/import-meta) object. Use `import.meta.path` to retrieve the absolute path of the current file.
|
|
6
|
+
|
|
7
|
+
```ts#/a/b/c.ts
|
|
8
|
+
import.meta.path; // => "/a/b/c.ts"
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
See [Docs > API > import.meta](https://bun.sh/docs/api/import-meta) for complete documentation.
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Get the absolute path to the current entrypoint
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
The `Bun.main` property contains the absolute path to the current entrypoint.
|
|
6
|
+
|
|
7
|
+
{% codetabs %}
|
|
8
|
+
|
|
9
|
+
```ts#foo.ts
|
|
10
|
+
console.log(Bun.main);
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```ts#index.ts
|
|
14
|
+
import "./foo.ts";
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
{% /codetabs %}
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
The printed path corresponds to the file that is executed with `bun run`.
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
$ bun run index.ts
|
|
25
|
+
/path/to/index.ts
|
|
26
|
+
$ bun run foo.ts
|
|
27
|
+
/path/to/foo.ts
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
See [Docs > API > Utils](https://bun.sh/docs/api/utils) for more useful utilities.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Convert an absolute path to a file URL
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Use `Bun.pathToFileURL()` to convert an absolute path to a `file://` URL.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
Bun.pathToFileURL("/path/to/file.txt");
|
|
9
|
+
// => "file:///path/to/file.txt"
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
See [Docs > API > Utils](https://bun.sh/docs/api/utils) for more useful utilities.
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Sleep for a fixed number of milliseconds
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
The `Bun.sleep` method provides a convenient way to create a void `Promise` that resolves in a fixed number of milliseconds.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
// sleep for 1 second
|
|
9
|
+
await Bun.sleep(1000);
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
Internally, this is equivalent to the following snippet that uses [`setTimeout`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout).
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
await new Promise(resolve => setTimeout(resolve, ms));
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
See [Docs > API > Utils](https://bun.sh/docs/api/utils) for more useful utilities.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Get the current Bun version
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Get the current version of Bun in a semver format.
|
|
6
|
+
|
|
7
|
+
```ts#index.ts
|
|
8
|
+
Bun.version; // => "0.6.15"
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
Get the exact `git` commit of [`oven-sh/bun`](https://github.com/oven-sh/bun) that was compiled to produce this Bun binary.
|
|
14
|
+
|
|
15
|
+
```ts#index.ts
|
|
16
|
+
Bun.revision; // => "49231b2cb9aa48497ab966fc0bb6b742dacc4994"
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
See [Docs > API > Utils](https://bun.sh/docs/api/utils) for more useful utilities.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Get the path to an executable bin file
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
`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.
|
|
6
|
+
|
|
7
|
+
```ts#foo.ts
|
|
8
|
+
Bun.which("sh"); // => "/bin/sh"
|
|
9
|
+
Bun.which("notfound"); // => null
|
|
10
|
+
Bun.which("bun"); // => "/home/user/.bun/bin/bun"
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
See [Docs > API > Utils](https://bun.sh/docs/api/utils#bun-which) for complete documentation.
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Enable compression for WebSocket messages
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
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.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
Bun.serve({
|
|
9
|
+
// ...
|
|
10
|
+
websocket: {
|
|
11
|
+
// enable compression
|
|
12
|
+
perMessageDeflate: true,
|
|
13
|
+
},
|
|
14
|
+
});
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
To enable compression for individual messages, pass `true` as the second parameter to `ws.send()`.
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
Bun.serve({
|
|
23
|
+
// ...
|
|
24
|
+
websocket: {
|
|
25
|
+
async message(ws, message) {
|
|
26
|
+
// send a compressed message
|
|
27
|
+
ws.send(message, true);
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
```
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Set per-socket contextual data on a WebSocket
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
When building a WebSocket server, it's typically necessary to store some identifying information or context associated with each connected client.
|
|
6
|
+
|
|
7
|
+
With [Bun.serve()](https://bun.sh/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.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
Bun.serve<{ socketId: number }>({
|
|
11
|
+
fetch(req, server) {
|
|
12
|
+
const success = server.upgrade(req, {
|
|
13
|
+
data: {
|
|
14
|
+
socketId: Math.random(),
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
if (success) return undefined;
|
|
18
|
+
|
|
19
|
+
// handle HTTP request normally
|
|
20
|
+
// ...
|
|
21
|
+
},
|
|
22
|
+
websocket: {
|
|
23
|
+
// define websocket handlers
|
|
24
|
+
async message(ws, message) {
|
|
25
|
+
// the contextual data is available as the `data` property
|
|
26
|
+
// on the WebSocket instance
|
|
27
|
+
console.log(`Received ${message} from ${ws.data.socketId}}`);
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
It's common to read cookies/headers from the incoming request to identify the connecting client.
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
type WebSocketData = {
|
|
39
|
+
createdAt: number;
|
|
40
|
+
token: string;
|
|
41
|
+
userId: string;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// TypeScript: specify the type of `data`
|
|
45
|
+
Bun.serve<WebSocketData>({
|
|
46
|
+
async fetch(req, server) {
|
|
47
|
+
// use a library to parse cookies
|
|
48
|
+
const cookies = parseCookies(req.headers.get("Cookie"));
|
|
49
|
+
const token = cookies["X-Token"];
|
|
50
|
+
const user = await getUserFromToken(token);
|
|
51
|
+
|
|
52
|
+
const upgraded = server.upgrade(req, {
|
|
53
|
+
data: {
|
|
54
|
+
createdAt: Date.now(),
|
|
55
|
+
token: cookies["X-Token"],
|
|
56
|
+
userId: user.id,
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
if (upgraded) return undefined;
|
|
61
|
+
},
|
|
62
|
+
websocket: {
|
|
63
|
+
async message(ws, message) {
|
|
64
|
+
// save the message to a database
|
|
65
|
+
await saveMessageToDatabase({
|
|
66
|
+
message: String(message),
|
|
67
|
+
userId: ws.data.userId,
|
|
68
|
+
});
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
```
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Build a publish-subscribe WebSocket server
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
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>)`.
|
|
6
|
+
|
|
7
|
+
This code snippet implements a simple single-channel chat server.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
const server = Bun.serve<{ username: string }>({
|
|
11
|
+
fetch(req, server) {
|
|
12
|
+
const cookies = req.headers.get("cookie");
|
|
13
|
+
const username = getUsernameFromCookies(cookies);
|
|
14
|
+
const success = server.upgrade(req, { data: { username } });
|
|
15
|
+
if (success) return undefined;
|
|
16
|
+
|
|
17
|
+
return new Response("Hello world");
|
|
18
|
+
},
|
|
19
|
+
websocket: {
|
|
20
|
+
open(ws) {
|
|
21
|
+
const msg = `${ws.data.username} has entered the chat`;
|
|
22
|
+
ws.subscribe("the-group-chat");
|
|
23
|
+
server.publish("the-group-chat", msg);
|
|
24
|
+
},
|
|
25
|
+
message(ws, message) {
|
|
26
|
+
// the server re-broadcasts incoming messages to everyone
|
|
27
|
+
server.publish("the-group-chat", `${ws.data.username}: ${message}`);
|
|
28
|
+
},
|
|
29
|
+
close(ws) {
|
|
30
|
+
const msg = `${ws.data.username} has left the chat`;
|
|
31
|
+
server.publish("the-group-chat", msg);
|
|
32
|
+
ws.unsubscribe("the-group-chat");
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
console.log(`Listening on ${server.hostname}:${server.port}`);
|
|
38
|
+
```
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Build a simple WebSocket server
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Start a simple WebSocket server using [`Bun.serve`](https://bun.sh/docs/api/http).
|
|
6
|
+
|
|
7
|
+
Inside `fetch`, we attempt to upgrade incoming `ws:` or `wss:` requests to WebSocket connections.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
const server = Bun.serve<{ authToken: string }>({
|
|
11
|
+
fetch(req, server) {
|
|
12
|
+
const success = server.upgrade(req);
|
|
13
|
+
if (success) {
|
|
14
|
+
// Bun automatically returns a 101 Switching Protocols
|
|
15
|
+
// if the upgrade succeeds
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// handle HTTP request normally
|
|
20
|
+
return new Response("Hello world!");
|
|
21
|
+
},
|
|
22
|
+
websocket: {
|
|
23
|
+
// this is called when a message is received
|
|
24
|
+
async message(ws, message) {
|
|
25
|
+
console.log(`Received ${message}`);
|
|
26
|
+
// send back a message
|
|
27
|
+
ws.send(`You said: ${message}`);
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
console.log(`Listening on ${server.hostname}:${server.port}`);
|
|
33
|
+
```
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Append content to a file
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Bun implements the `node:fs` module, which includes the `fs.appendFile` and `fs.appendFileSync` functions for appending content to files.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
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`.
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { appendFile } from "node:fs/promises";
|
|
13
|
+
|
|
14
|
+
await appendFile("message.txt", "data to append");
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
To use the non-`Promise` API:
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { appendFile } from "node:fs";
|
|
23
|
+
|
|
24
|
+
appendFile("message.txt", "data to append", err => {
|
|
25
|
+
if (err) throw err;
|
|
26
|
+
console.log('The "data to append" was appended to file!');
|
|
27
|
+
});
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
To specify the encoding of the content:
|
|
33
|
+
|
|
34
|
+
```js
|
|
35
|
+
import { appendFile } from "node:fs";
|
|
36
|
+
|
|
37
|
+
appendFile("message.txt", "data to append", "utf8", callback);
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
To append the data synchronously, use `fs.appendFileSync`:
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
import { appendFileSync } from "node:fs";
|
|
46
|
+
|
|
47
|
+
appendFileSync("message.txt", "data to append", "utf8");
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
See the [Node.js documentation](https://nodejs.org/api/fs.html#fspromisesappendfilepath-data-options) for more information.
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Write a string to a file
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
This code snippet writes a string to disk at a particular _absolute path_.
|
|
6
|
+
|
|
7
|
+
It uses the fast [`Bun.write()`](https://bun.sh/docs/api/file-io#writing-files-bun-write) API to efficiently write data to disk. The first argument is a _destination_; the second is the _data_ to write.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
const path = "/path/to/file.txt";
|
|
11
|
+
await Bun.write(path, "Lorem ipsum");
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
Any relative paths will be resolved relative to the project root (the nearest directory containing a `package.json` file).
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
const path = "./file.txt";
|
|
20
|
+
await Bun.write(path, "Lorem ipsum");
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
You can pass a `BunFile` as the destination. `Bun.write()` will write the data to its associated path.
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
const path = Bun.file("./file.txt");
|
|
29
|
+
await Bun.write(path, "Lorem ipsum");
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
---
|
|
33
|
+
|
|
34
|
+
`Bun.write()` returns the number of bytes written to disk.
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
const path = "./file.txt";
|
|
38
|
+
const bytes = await Bun.write(path, "Lorem ipsum");
|
|
39
|
+
// => 11
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
See [Docs > API > File I/O](https://bun.sh/docs/api/file-io#writing-files-bun-write) for complete documentation of `Bun.write()`.
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Write a Blob to a file
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
This code snippet writes a `Blob` to disk at a particular path.
|
|
6
|
+
|
|
7
|
+
It uses the fast [`Bun.write()`](https://bun.sh/docs/api/file-io#writing-files-bun-write) API to efficiently write data to disk. The first argument is a _destination_, like an absolute path or `BunFile` instance. The second argument is the _data_ to write.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
const path = "/path/to/file.txt";
|
|
11
|
+
await Bun.write(path, "Lorem ipsum");
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
The `BunFile` class extends `Blob`, so you can pass a `BunFile` directly into `Bun.write()` as well.
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
const path = "./out.txt";
|
|
20
|
+
const data = Bun.file("./in.txt");
|
|
21
|
+
|
|
22
|
+
// write the contents of ./in.txt to ./out.txt
|
|
23
|
+
await Bun.write(path, data);
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
See [Docs > API > File I/O](https://bun.sh/docs/api/file-io#writing-files-bun-write) for complete documentation of `Bun.write()`.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Write a file to stdout
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Bun exposes `stdout` as a `BunFile` with the `Bun.stdout` property. This can be used as a destination for [`Bun.write()`](https://bun.sh/docs/api/file-io#writing-files-bun-write).
|
|
6
|
+
|
|
7
|
+
This code writes a file to `stdout` similar to the `cat` command in Unix.
|
|
8
|
+
|
|
9
|
+
```ts#cat.ts
|
|
10
|
+
const path = "/path/to/file.txt";
|
|
11
|
+
const file = Bun.file(path);
|
|
12
|
+
await Bun.write(Bun.stdout, file);
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
See [Docs > API > File I/O](https://bun.sh/docs/api/file-io#writing-files-bun-write) for complete documentation of `Bun.write()`.
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Copy a file to another location
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
This code snippet copies a file to another location on disk.
|
|
6
|
+
|
|
7
|
+
It uses the fast [`Bun.write()`](https://bun.sh/docs/api/file-io#writing-files-bun-write) API to efficiently write data to disk. The first argument is a _destination_, like an absolute path or `BunFile` instance. The second argument is the _data_ to write.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
const file = Bun.file("/path/to/original.txt");
|
|
11
|
+
await Bun.write("/path/to/copy.txt", file);
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
See [Docs > API > File I/O](https://bun.sh/docs/api/file-io#writing-files-bun-write) for complete documentation of `Bun.write()`.
|