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,20 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Get the MIME type of a file
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
The `Bun.file()` function accepts a path and returns a `BunFile` instance. The `BunFile` class extends `Blob`, so use the `.type` property to read the MIME type.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
const file = Bun.file("./package.json");
|
|
9
|
+
file.type; // application/json
|
|
10
|
+
|
|
11
|
+
const file = Bun.file("./index.html");
|
|
12
|
+
file.type; // text/html
|
|
13
|
+
|
|
14
|
+
const file = Bun.file("./image.png");
|
|
15
|
+
file.type; // image/png
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
Refer to [API > File I/O](https://bun.sh/docs/api/file-io) for more information on working with `BunFile`.
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Read a file as a ReadableStream
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
The `Bun.file()` function accepts a path and returns a `BunFile` instance. The `BunFile` class extends `Blob` and allows you to lazily read the file in a variety of formats. Use `.stream()` to consume the file incrementally as a `ReadableStream`.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
const path = "/path/to/package.json";
|
|
9
|
+
const file = Bun.file(path);
|
|
10
|
+
|
|
11
|
+
const stream = file.stream();
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
The chunks of the stream can be consumed as an [async iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols) using `for await`.
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
for await (const chunk of stream) {
|
|
20
|
+
chunk; // => Uint8Array
|
|
21
|
+
}
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
Refer to the [Streams](https://bun.sh/docs/api/streams) documentation for more information on working with streams in Bun.
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Read a file as a string
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
The `Bun.file()` function accepts a path and returns a `BunFile` instance. The `BunFile` class extends `Blob` and allows you to lazily read the file in a variety of formats. Use `.text()` to read the contents as a string.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
const path = "/path/to/file.txt";
|
|
9
|
+
const file = Bun.file(path);
|
|
10
|
+
|
|
11
|
+
const text = await file.text();
|
|
12
|
+
// string
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
Any relative paths will be resolved relative to the project root (the nearest directory containing a `package.json` file).
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
const path = "./file.txt";
|
|
21
|
+
const file = Bun.file(path);
|
|
22
|
+
```
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Read a file to a Uint8Array
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
The `Bun.file()` function accepts a path and returns a `BunFile` instance. The `BunFile` class extends `Blob` and allows you to lazily read the file in a variety of formats.
|
|
6
|
+
|
|
7
|
+
To read the file into a `Uint8Array` instance, retrieve the contents of the `BunFile` with `.bytes()`.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
const path = "/path/to/package.json";
|
|
11
|
+
const file = Bun.file(path);
|
|
12
|
+
|
|
13
|
+
const byteArray = await file.bytes();
|
|
14
|
+
|
|
15
|
+
byteArray[0]; // first byteArray
|
|
16
|
+
byteArray.length; // length of byteArray
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
Refer to [API > Binary data > Typed arrays](https://bun.sh/docs/api/binary-data#typedarray) for more information on working with `Uint8Array` and other binary data formats in Bun.
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Watch a directory for changes
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Bun implements the `node:fs` module, including the `fs.watch` function for listening for file system changes.
|
|
6
|
+
|
|
7
|
+
This code block listens for changes to files in the current directory. By default this operation is _shallow_, meaning that changes to files in subdirectories will not be detected.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { watch } from "fs";
|
|
11
|
+
|
|
12
|
+
const watcher = watch(import.meta.dir, (event, filename) => {
|
|
13
|
+
console.log(`Detected ${event} in ${filename}`);
|
|
14
|
+
});
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
To listen to changes in subdirectories, pass the `recursive: true` option to `fs.watch`.
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { watch } from "fs";
|
|
23
|
+
|
|
24
|
+
const watcher = watch(
|
|
25
|
+
import.meta.dir,
|
|
26
|
+
{ recursive: true },
|
|
27
|
+
(event, filename) => {
|
|
28
|
+
console.log(`Detected ${event} in ${filename}`);
|
|
29
|
+
},
|
|
30
|
+
);
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
Using the `node:fs/promises` module, you can listen for changes using `for await...of` instead of a callback.
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
import { watch } from "fs/promises";
|
|
39
|
+
|
|
40
|
+
const watcher = watch(import.meta.dir);
|
|
41
|
+
for await (const event of watcher) {
|
|
42
|
+
console.log(`Detected ${event.eventType} in ${event.filename}`);
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
To stop listening for changes, call `watcher.close()`. It's common to do this when the process receives a `SIGINT` signal, such as when the user presses Ctrl-C.
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
import { watch } from "fs";
|
|
52
|
+
|
|
53
|
+
const watcher = watch(import.meta.dir, (event, filename) => {
|
|
54
|
+
console.log(`Detected ${event} in ${filename}`);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
process.on("SIGINT", () => {
|
|
58
|
+
// close watcher when Ctrl-C is pressed
|
|
59
|
+
console.log("Closing watcher...");
|
|
60
|
+
watcher.close();
|
|
61
|
+
|
|
62
|
+
process.exit(0);
|
|
63
|
+
});
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
Refer to [API > Binary data > Typed arrays](https://bun.sh/docs/api/binary-data#typedarray) for more information on working with `Uint8Array` and other binary data formats in Bun.
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Install and run Bun in GitHub Actions
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Use the official [`setup-bun`](https://github.com/oven-sh/setup-bun) GitHub Action to install `bun` in your GitHub Actions runner.
|
|
6
|
+
|
|
7
|
+
```yaml-diff#workflow.yml
|
|
8
|
+
name: my-workflow
|
|
9
|
+
jobs:
|
|
10
|
+
my-job:
|
|
11
|
+
name: my-job
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
steps:
|
|
14
|
+
# ...
|
|
15
|
+
- uses: actions/checkout@v4
|
|
16
|
+
+ - uses: oven-sh/setup-bun@v2
|
|
17
|
+
|
|
18
|
+
# run any `bun` or `bunx` command
|
|
19
|
+
+ - run: bun install
|
|
20
|
+
+ - run: bun index.ts
|
|
21
|
+
+ - run: bun run build
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
To specify a version of Bun to install:
|
|
27
|
+
|
|
28
|
+
```yaml-diff#workflow.yml
|
|
29
|
+
name: my-workflow
|
|
30
|
+
jobs:
|
|
31
|
+
my-job:
|
|
32
|
+
name: my-job
|
|
33
|
+
runs-on: ubuntu-latest
|
|
34
|
+
steps:
|
|
35
|
+
# ...
|
|
36
|
+
- uses: oven-sh/setup-bun@v2
|
|
37
|
+
+ with:
|
|
38
|
+
+ bun-version: 1.0.11 # or "latest", "canary", <sha>
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
Refer to the [README.md](https://github.com/oven-sh/setup-bun) for complete documentation of the `setup-bun` GitHub Action.
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Define and replace static globals & constants
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
The `--define` flag lets you declare statically-analyzable constants and globals. It replace all usages of an identifier or property in a JavaScript or TypeScript file with a constant value. This feature is supported at runtime and also in `bun build`. This is sort of similar to `#define` in C/C++, except for JavaScript.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
bun --define process.env.NODE_ENV="'production'" src/index.ts # Runtime
|
|
9
|
+
bun build --define process.env.NODE_ENV="'production'" src/index.ts # Build
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
These statically-known values are used by Bun for dead code elimination and other optimizations.
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
if (process.env.NODE_ENV === "production") {
|
|
18
|
+
console.log("Production mode");
|
|
19
|
+
} else {
|
|
20
|
+
console.log("Development mode");
|
|
21
|
+
}
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
Before the code reaches the JavaScript engine, Bun replaces `process.env.NODE_ENV` with `"production"`.
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
if ("production" === "production") {
|
|
30
|
+
console.log("Production mode");
|
|
31
|
+
} else {
|
|
32
|
+
console.log("Development mode");
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
---
|
|
37
|
+
|
|
38
|
+
It doesn't stop there. Bun's optimizing transpiler is smart enough to do some basic constant folding.
|
|
39
|
+
|
|
40
|
+
Since `"production" === "production"` is always `true`, Bun replaces the entire expression with the `true` value.
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
if (true) {
|
|
44
|
+
console.log("Production mode");
|
|
45
|
+
} else {
|
|
46
|
+
console.log("Development mode");
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
And finally, Bun detects the `else` branch is not reachable, and eliminates it.
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
console.log("Production mode");
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
## What types of values are supported?
|
|
61
|
+
|
|
62
|
+
Values can be strings, identifiers, properties, or JSON.
|
|
63
|
+
|
|
64
|
+
### Replace global identifiers
|
|
65
|
+
|
|
66
|
+
To make all usages of `window` be `undefined`, you can use the following command.
|
|
67
|
+
|
|
68
|
+
```sh
|
|
69
|
+
bun --define window="undefined" src/index.ts
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
This can be useful when Server-Side Rendering (SSR) or when you want to make sure that the code doesn't depend on the `window` object.
|
|
73
|
+
|
|
74
|
+
```js
|
|
75
|
+
if (typeof window !== "undefined") {
|
|
76
|
+
console.log("Client-side code");
|
|
77
|
+
} else {
|
|
78
|
+
console.log("Server-side code");
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
You can also set the value to be another identifier. For example, to make all usages of `global` be `globalThis`, you can use the following command.
|
|
83
|
+
|
|
84
|
+
```sh
|
|
85
|
+
bun --define global="globalThis" src/index.ts
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
`global` is a global object in Node.js, but not in web browsers. So, you can use this to fix some cases where the code assumes that `global` is available.
|
|
89
|
+
|
|
90
|
+
### Replace values with JSON
|
|
91
|
+
|
|
92
|
+
`--define` can also be used to replace values with JSON objects and arrays.
|
|
93
|
+
|
|
94
|
+
To replace all usages of `AWS` with the JSON object `{"ACCESS_KEY":"abc","SECRET_KEY":"def"}`, you can use the following command.
|
|
95
|
+
|
|
96
|
+
```sh
|
|
97
|
+
# JSON
|
|
98
|
+
bun --define AWS='{"ACCESS_KEY":"abc","SECRET_KEY":"def"}' src/index.ts
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Those will be transformed into the equivalent JavaScript code.
|
|
102
|
+
|
|
103
|
+
From:
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
console.log(AWS.ACCESS_KEY); // => "abc"
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
To:
|
|
110
|
+
|
|
111
|
+
```ts
|
|
112
|
+
console.log("abc");
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Replace values with other properties
|
|
116
|
+
|
|
117
|
+
You can also pass properties to the `--define` flag.
|
|
118
|
+
|
|
119
|
+
For example, to replace all usages of `console.write` with `console.log`, you can use the following command (requires Bun v1.1.5 or later)
|
|
120
|
+
|
|
121
|
+
```sh
|
|
122
|
+
bun --define console.write=console.log src/index.ts
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
That transforms the following input:
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
console.write("Hello, world!");
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Into the following output:
|
|
132
|
+
|
|
133
|
+
```ts
|
|
134
|
+
console.log("Hello, world!");
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## How is this different than setting a variable?
|
|
138
|
+
|
|
139
|
+
You can also set `process.env.NODE_ENV` to `"production"` in your code, but that won't help with dead code elimination. In JavaScript, property accesses can have side effects. Getters & setters can be functions, and even dynamically defined (due to prototype chains and Proxy). Even if you set `process.env.NODE_ENV` to `"production"`, on the next line, it is not safe for static analysis tools to assume that `process.env.NODE_ENV`is`"production"`.
|
|
140
|
+
|
|
141
|
+
## How is this different than find-and-replace or string replacement?
|
|
142
|
+
|
|
143
|
+
The `--define` flag operates on the AST (Abstract Syntax Tree) level, not on the text level. It happens during the transpilation process, which means it can be used in optimizations like dead code elimination.
|
|
144
|
+
|
|
145
|
+
String replacement tools tend to have escaping issues and replace unintended parts of the code.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Import a HTML file as text
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
To import a `.html` file in Bun as a text file, use the `type: "text"` attribute in the import statement.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import html from "./file.html" with { type: "text" };
|
|
9
|
+
|
|
10
|
+
console.log(html); // <!DOCTYPE html><html><head>...
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
This can also be used with hot module reloading and/or watch mode to force Bun to reload whenever the `./file.html` file changes.
|
|
14
|
+
|
|
15
|
+
This feature was added in Bun v1.1.5.
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Import a JSON file
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Bun natively supports `.json` imports.
|
|
6
|
+
|
|
7
|
+
```json#package.json
|
|
8
|
+
{
|
|
9
|
+
"name": "bun",
|
|
10
|
+
"version": "1.0.0",
|
|
11
|
+
"author": {
|
|
12
|
+
"name": "John Dough",
|
|
13
|
+
"email": "john@dough.com"
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
Import the file like any other source file.
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import data from "./package.json";
|
|
24
|
+
|
|
25
|
+
data.name; // => "bun"
|
|
26
|
+
data.version; // => "1.0.0"
|
|
27
|
+
data.author.name; // => "John Dough"
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
Bun also supports [Import Attributes](https://github.com/tc39/proposal-import-attributes/) and [JSON modules](https://github.com/tc39/proposal-json-modules) syntax.
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import data from "./package.json" with { type: "json" };
|
|
36
|
+
|
|
37
|
+
data.name; // => "bun"
|
|
38
|
+
data.version; // => "1.0.0"
|
|
39
|
+
data.author.name; // => "John Dough"
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
See [Docs > Runtime > TypeScript](https://bun.sh/docs/runtime/typescript) for more information on using TypeScript with Bun.
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Import a TOML file
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Bun natively supports importing `.toml` files.
|
|
6
|
+
|
|
7
|
+
```toml#data.toml
|
|
8
|
+
name = "bun"
|
|
9
|
+
version = "1.0.0"
|
|
10
|
+
|
|
11
|
+
[author]
|
|
12
|
+
name = "John Dough"
|
|
13
|
+
email = "john@dough.com"
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
Import the file like any other source file.
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import data from "./data.toml";
|
|
22
|
+
|
|
23
|
+
data.name; // => "bun"
|
|
24
|
+
data.version; // => "1.0.0"
|
|
25
|
+
data.author.name; // => "John Dough"
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
See [Docs > Runtime > TypeScript](https://bun.sh/docs/runtime/typescript) for more information on using TypeScript with Bun.
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Read environment variables
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
The current environment variables can be accessed via `process.env`.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
process.env.API_TOKEN; // => "secret"
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
Bun also exposes these variables via `Bun.env`, which is a simple alias of `process.env`.
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
Bun.env.API_TOKEN; // => "secret"
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
To print all currently-set environment variables to the command line, run `bun --print process.env`. This is useful for debugging.
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
$ bun --print process.env
|
|
25
|
+
BAZ=stuff
|
|
26
|
+
FOOBAR=aaaaaa
|
|
27
|
+
<lots more lines>
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
See [Docs > Runtime > Environment variables](https://bun.sh/docs/runtime/env) for more information on using environment variables with Bun.
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Set environment variables
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
The current environment variables can be accessed via `process.env` or `Bun.env`.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
Bun.env.API_TOKEN; // => "secret"
|
|
9
|
+
process.env.API_TOKEN; // => "secret"
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
Set these variables in a `.env` file.
|
|
15
|
+
|
|
16
|
+
Bun reads the following files automatically (listed in order of increasing precedence).
|
|
17
|
+
|
|
18
|
+
- `.env`
|
|
19
|
+
- `.env.production` or `.env.development` (depending on value of `NODE_ENV`)
|
|
20
|
+
- `.env.local`
|
|
21
|
+
|
|
22
|
+
```txt#.env
|
|
23
|
+
FOO=hello
|
|
24
|
+
BAR=world
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
Variables can also be set via the command line.
|
|
30
|
+
|
|
31
|
+
```sh
|
|
32
|
+
$ FOO=helloworld bun run dev
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
See [Docs > Runtime > Environment variables](https://bun.sh/docs/runtime/env) for more information on using environment variables with Bun.
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Run a Shell Command
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Bun Shell is a cross-platform bash-like shell built in to Bun.
|
|
6
|
+
|
|
7
|
+
It provides a simple way to run shell commands in JavaScript and TypeScript. To get started, import the `$` function from the `bun` package and use it to run shell commands.
|
|
8
|
+
|
|
9
|
+
```ts#foo.ts
|
|
10
|
+
import { $ } from "bun";
|
|
11
|
+
|
|
12
|
+
await $`echo Hello, world!`; // => "Hello, world!"
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
The `$` function is a tagged template literal that runs the command and returns a promise that resolves with the command's output.
|
|
18
|
+
|
|
19
|
+
```ts#foo.ts
|
|
20
|
+
import { $ } from "bun";
|
|
21
|
+
|
|
22
|
+
const output = await $`ls -l`.text();
|
|
23
|
+
console.log(output);
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
To get each line of the output as an array, use the `lines` method.
|
|
29
|
+
|
|
30
|
+
```ts#foo.ts
|
|
31
|
+
import { $ } from "bun";
|
|
32
|
+
|
|
33
|
+
for await (const line of $`ls -l`.lines()) {
|
|
34
|
+
console.log(line);
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
See [Docs > API > Shell](https://bun.sh/docs/runtime/shell) for complete documentation.
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Set a time zone in Bun
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Bun supports programmatically setting a default time zone for the lifetime of the `bun` process. To do set, set the value of the `TZ` environment variable to a [valid timezone identifier](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones).
|
|
6
|
+
|
|
7
|
+
{% callout %}
|
|
8
|
+
When running a file with `bun`, the timezone defaults to your system's configured local time zone.
|
|
9
|
+
|
|
10
|
+
When running tests with `bun test`, the timezone is set to `UTC` to make tests more deterministic.
|
|
11
|
+
{% /callout %}
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
process.env.TZ = "America/New_York";
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
Alternatively, this can be set from the command line when running a Bun command.
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
$ TZ=America/New_York bun run dev
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
Once `TZ` is set, any `Date` instances will have that time zone. By default all dates use your system's configured time zone.
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
new Date().getHours(); // => 18
|
|
31
|
+
|
|
32
|
+
process.env.TZ = "America/New_York";
|
|
33
|
+
|
|
34
|
+
new Date().getHours(); // => 21
|
|
35
|
+
```
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Re-map import paths
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Bun reads the `paths` field in your `tsconfig.json` to re-write import paths. This is useful for aliasing package names or avoiding long relative paths.
|
|
6
|
+
|
|
7
|
+
```json
|
|
8
|
+
{
|
|
9
|
+
"compilerOptions": {
|
|
10
|
+
"paths": {
|
|
11
|
+
"my-custom-name": ["zod"],
|
|
12
|
+
"@components/*": ["./src/components/*"]
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
With the above `tsconfig.json`, the following imports will be re-written:
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { z } from "my-custom-name"; // imports from "zod"
|
|
24
|
+
import { Button } from "@components/Button"; // imports from "./src/components/Button"
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
See [Docs > Runtime > TypeScript](https://bun.sh/docs/runtime/typescript) for more information on using TypeScript with Bun.
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Install TypeScript declarations for Bun
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
To install TypeScript definitions for Bun's built-in APIs in your project, install `@types/bun`.
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
$ bun add -d @types/bun # dev dependency
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
Below is the full set of recommended `compilerOptions` for a Bun project. With this `tsconfig.json`, you can use top-level await, extensioned or extensionless imports, and JSX.
|
|
14
|
+
|
|
15
|
+
```jsonc
|
|
16
|
+
{
|
|
17
|
+
"compilerOptions": {
|
|
18
|
+
// Enable latest features
|
|
19
|
+
"lib": ["ESNext", "DOM"],
|
|
20
|
+
"target": "ESNext",
|
|
21
|
+
"module": "ESNext",
|
|
22
|
+
"moduleDetection": "force",
|
|
23
|
+
"jsx": "react-jsx",
|
|
24
|
+
"allowJs": true,
|
|
25
|
+
|
|
26
|
+
// Bundler mode
|
|
27
|
+
"moduleResolution": "bundler",
|
|
28
|
+
"allowImportingTsExtensions": true,
|
|
29
|
+
"verbatimModuleSyntax": true,
|
|
30
|
+
"noEmit": true,
|
|
31
|
+
|
|
32
|
+
// Best practices
|
|
33
|
+
"strict": true,
|
|
34
|
+
"skipLibCheck": true,
|
|
35
|
+
"noFallthroughCasesInSwitch": true,
|
|
36
|
+
|
|
37
|
+
// Some stricter flags
|
|
38
|
+
"noUnusedLocals": true,
|
|
39
|
+
"noUnusedParameters": true,
|
|
40
|
+
"noPropertyAccessFromIndexSignature": true,
|
|
41
|
+
},
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
Refer to [Ecosystem > TypeScript](https://bun.sh/docs/runtime/typescript) for a complete guide to TypeScript support in Bun.
|