bun-types 1.1.43 → 1.1.44-canary.20250111T140551

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 CHANGED
@@ -1057,6 +1057,10 @@ declare module "bun" {
1057
1057
  errors: number;
1058
1058
  totalCount: number;
1059
1059
  };
1060
+
1061
+ ADDRCONFIG: number;
1062
+ ALL: number;
1063
+ V4MAPPED: number;
1060
1064
  };
1061
1065
 
1062
1066
  interface DNSLookup {
@@ -1257,6 +1261,16 @@ declare module "bun" {
1257
1261
  * Deletes the file.
1258
1262
  */
1259
1263
  unlink(): Promise<void>;
1264
+
1265
+ /**
1266
+ * Deletes the file. ( same as unlink )
1267
+ */
1268
+ delete(): Promise<void>;
1269
+
1270
+ /**
1271
+ * Provides useful information about the file.
1272
+ */
1273
+ stat(): Promise<Stats>;
1260
1274
  }
1261
1275
  interface NetworkSink extends FileSink {
1262
1276
  /**
package/docs/api/ffi.md CHANGED
@@ -297,6 +297,20 @@ setTimeout(() => {
297
297
 
298
298
  When you're done with a JSCallback, you should call `close()` to free the memory.
299
299
 
300
+ ### Experimental thread-safe callbacks
301
+ `JSCallback` has experimental support for thread-safe callbacks. This will be needed if you pass a callback function into a different thread from it's instantiation context. You can enable it with the optional `threadsafe` option flag.
302
+ ```ts
303
+ const searchIterator = new JSCallback(
304
+ (ptr, length) => /hello/.test(new CString(ptr, length)),
305
+ {
306
+ returns: "bool",
307
+ args: ["ptr", "usize"],
308
+ threadsafe: true, // Optional. Defaults to `false`
309
+ },
310
+ );
311
+ ```
312
+ Be aware that there are still cases where this does not 100% work.
313
+
300
314
  {% callout %}
301
315
 
302
316
  **⚡️ Performance tip** — For a slight performance boost, directly pass `JSCallback.prototype.ptr` instead of the `JSCallback` object:
@@ -62,6 +62,14 @@ Bun.stdout;
62
62
  Bun.stderr;
63
63
  ```
64
64
 
65
+ ### Deleting files (`file.delete()`)
66
+
67
+ You can delete a file by calling the `.delete()` function.
68
+
69
+ ```ts
70
+ await Bun.file("logs.json").delete()
71
+ ```
72
+
65
73
  ## Writing files (`Bun.write()`)
66
74
 
67
75
  `Bun.write(destination, data): Promise<number>`
@@ -15,10 +15,10 @@ If you're using Ubuntu 20.04, here's how to install a [newer kernel](https://wik
15
15
 
16
16
  ```bash
17
17
  # If this returns a version >= 5.6, you don't need to do anything
18
- uname -r
18
+ $ uname -r
19
19
 
20
20
  # Install the official Ubuntu hardware enablement kernel
21
- sudo apt install --install-recommends linux-generic-hwe-20.04
21
+ $ sudo apt install --install-recommends linux-generic-hwe-20.04
22
22
  ```
23
23
 
24
24
  {% /details %}
@@ -229,7 +229,7 @@ jobs:
229
229
  - name: Checkout repo
230
230
  uses: actions/checkout@v4
231
231
  - name: Install bun
232
- uses: oven-sh/setup-bun@v1
232
+ uses: oven-sh/setup-bun@v2
233
233
  - name: Install dependencies
234
234
  run: bun install
235
235
  - name: Build app
@@ -4,18 +4,18 @@ name: Create a Discord bot
4
4
 
5
5
  Discord.js works out of the box with Bun. Let's write a simple bot. First create a directory and initialize it with `bun init`.
6
6
 
7
- ```bash
8
- mkdir my-bot
9
- cd my-bot
10
- bun init
7
+ ```sh
8
+ $ mkdir my-bot
9
+ $ cd my-bot
10
+ $ bun init
11
11
  ```
12
12
 
13
13
  ---
14
14
 
15
15
  Now install Discord.js.
16
16
 
17
- ```bash
18
- bun add discord.js
17
+ ```sh
18
+ $ bun add discord.js
19
19
  ```
20
20
 
21
21
  ---
@@ -67,7 +67,7 @@ client.login(process.env.DISCORD_TOKEN);
67
67
 
68
68
  Now we can run our bot with `bun run`. It may take a several seconds for the client to initialize the first time you run the file.
69
69
 
70
- ```bash
70
+ ```sh
71
71
  $ bun run bot.ts
72
72
  Ready! Logged in as my-bot#1234
73
73
  ```
@@ -17,7 +17,7 @@ export default app;
17
17
 
18
18
  Use `create-hono` to get started with one of Hono's project templates. Select `bun` when prompted for a template.
19
19
 
20
- ```bash
20
+ ```sh
21
21
  $ bun create hono myapp
22
22
  ✔ Which template do you want to use? › bun
23
23
  cloned honojs/starter#main to /path/to/myapp
@@ -30,7 +30,7 @@ $ bun install
30
30
 
31
31
  Then start the dev server and visit [localhost:3000](http://localhost:3000).
32
32
 
33
- ```bash
33
+ ```sh
34
34
  $ bun run dev
35
35
  ```
36
36
 
@@ -8,18 +8,18 @@ MongoDB and Mongoose work out of the box with Bun. This guide assumes you've alr
8
8
 
9
9
  Once MongoDB is running, create a directory and initialize it with `bun init`.
10
10
 
11
- ```bash
12
- mkdir mongoose-app
13
- cd mongoose-app
14
- bun init
11
+ ```sh
12
+ $ mkdir mongoose-app
13
+ $ cd mongoose-app
14
+ $ bun init
15
15
  ```
16
16
 
17
17
  ---
18
18
 
19
19
  Then add Mongoose as a dependency.
20
20
 
21
- ```bash
22
- bun add mongoose
21
+ ```sh
22
+ $ bun add mongoose
23
23
  ```
24
24
 
25
25
  ---
@@ -15,6 +15,8 @@ $ bun create next-app
15
15
  Creating a new Next.js app in /path/to/my-app.
16
16
  ```
17
17
 
18
+ ---
19
+
18
20
  You can specify a starter template using the `--example` flag.
19
21
 
20
22
  ```sh
@@ -16,17 +16,17 @@ As an example, let's deploy a simple Express HTTP server to Render.
16
16
 
17
17
  Create a new GitHub repo named `myapp`. Git clone it locally.
18
18
 
19
- ```bash
20
- git clone git@github.com:my-github-username/myapp.git
21
- cd myapp
19
+ ```sh
20
+ $ git clone git@github.com:my-github-username/myapp.git
21
+ $ cd myapp
22
22
  ```
23
23
 
24
24
  ---
25
25
 
26
26
  Add the Express library.
27
27
 
28
- ```bash
29
- bun add express
28
+ ```sh
29
+ $ bun add express
30
30
  ```
31
31
 
32
32
  ---
@@ -52,10 +52,10 @@ app.listen(port, () => {
52
52
 
53
53
  Commit your changes and push to GitHub.
54
54
 
55
- ```bash
56
- git add app.ts bun.lockb package.json
57
- git commit -m "Create simple Express app"
58
- git push origin main
55
+ ```sh
56
+ $ git add app.ts bun.lockb package.json
57
+ $ git commit -m "Create simple Express app"
58
+ $ git push origin main
59
59
  ```
60
60
 
61
61
  ---
@@ -10,8 +10,8 @@ Don't already have an account and Sentry project established? Head over to [sent
10
10
 
11
11
  To start using Sentry with Bun, first install the Sentry Bun SDK.
12
12
 
13
- ```bash
14
- bun add @sentry/bun
13
+ ```sh
14
+ $ bun add @sentry/bun
15
15
  ```
16
16
 
17
17
  ---
@@ -5,18 +5,22 @@ name: Hot reload an HTTP server
5
5
  Bun supports the [`--hot`](https://bun.sh/docs/runtime/hot#hot-mode) flag to run a file with hot reloading enabled. When any module or file changes, Bun re-runs the file.
6
6
 
7
7
  ```sh
8
- bun --hot run index.ts
8
+ $ bun --hot run index.ts
9
9
  ```
10
10
 
11
11
  ---
12
12
 
13
13
  Bun detects when you are running an HTTP server with `Bun.serve()`. It reloads your fetch handler when source files change, _without_ restarting the `bun` process. This makes hot reloads nearly instantaneous.
14
14
 
15
+ {% callout %}
16
+ Note that this doesn't reload the page on your browser.
17
+ {% /callout %}
18
+
15
19
  ```ts
16
20
  Bun.serve({
17
21
  port: 3000,
18
22
  fetch(req) {
19
- return new Response(`Hello world`);
23
+ return new Response("Hello world");
20
24
  },
21
25
  });
22
26
  ```
@@ -2,16 +2,43 @@
2
2
  name: Add a peer dependency
3
3
  ---
4
4
 
5
- To add an npm package as a peer dependency, directly modify the `peerDependencies` object in your package.json. Running `bun install` will install peer dependencies by default, unless marked optional in `peerDependenciesMeta`.
5
+
6
+ To add an npm package as a peer dependency, use the `--peer` flag.
7
+
8
+ ```sh
9
+ $ bun add @types/bun --peer
10
+ ```
11
+
12
+ ---
13
+
14
+ This will add the package to `peerDependencies` in `package.json`.
6
15
 
7
16
  ```json-diff
8
17
  {
9
18
  "peerDependencies": {
10
- + "zod": "^3.0.0"
19
+ + "@types/bun": "^1.0.0"
11
20
  }
12
21
  }
13
22
  ```
14
23
 
15
24
  ---
16
25
 
26
+ Running `bun install` will install peer dependencies by default, unless marked optional in `peerDependenciesMeta`.
27
+
28
+ ```json-diff
29
+ {
30
+ "peerDependencies": {
31
+ "@types/bun": "^1.0.0"
32
+ },
33
+ "peerDependenciesMeta": {
34
+ + "@types/bun": {
35
+ + "optional": true
36
+ + }
37
+ }
38
+
39
+ }
40
+ ```
41
+
42
+ ---
43
+
17
44
  See [Docs > Package manager](https://bun.sh/docs/cli/install) for complete documentation of Bun's package manager.
@@ -22,7 +22,7 @@ This will add the package to `dependencies` in `package.json`. By default, the `
22
22
 
23
23
  ---
24
24
 
25
- To "pin" to the `latest` version of the package, use `--exact`. This will add the package to `dependencies` without the `^`, pinning your project to the exact version you installed.
25
+ To "pin" to an exact version of the package, use `--exact`. This will add the package to `dependencies` without the `^`, pinning your project to the exact version you installed.
26
26
 
27
27
  ```sh
28
28
  $ bun add zod --exact
@@ -13,7 +13,7 @@ jobs:
13
13
  steps:
14
14
  # ...
15
15
  - uses: actions/checkout@v4
16
- + - uses: oven-sh/setup-bun@v1
16
+ + - uses: oven-sh/setup-bun@v2
17
17
 
18
18
  # run any `bun` or `bunx` command
19
19
  + - run: bun install
@@ -31,9 +31,9 @@ jobs:
31
31
  runs-on: ubuntu-latest
32
32
  steps:
33
33
  # ...
34
- - uses: oven-sh/setup-bun@v1
34
+ - uses: oven-sh/setup-bun@v2
35
35
  + with:
36
- + version: 0.7.0 # or "canary"
36
+ + version: "latest" # or "canary"
37
37
  ```
38
38
 
39
39
  ---
@@ -2,6 +2,12 @@
2
2
  name: Configure git to diff Bun's lockb lockfile
3
3
  ---
4
4
 
5
+ {% callout %}
6
+ Bun v1.1.39 introduced `bun.lock`, a JSONC formatted lockfile. `bun.lock` is human-readable and git-diffable without configuration, at no cost to performance. [**Learn more.**](https://bun.sh/docs/install/lockfile#text-based-lockfile)
7
+ {% /callout %}
8
+
9
+ ---
10
+
5
11
  To teach `git` how to generate a human-readable diff of Bun's binary lockfile format (`.lockb`), add the following to your local or global `.gitattributes` file:
6
12
 
7
13
  ```js
@@ -23,8 +23,6 @@ To allow Bun to execute lifecycle scripts for a specific package, add the packag
23
23
  Note that this only allows lifecycle scripts for the specific package listed in `trustedDependencies`, _not_ the dependencies of that dependency!
24
24
  {% /callout %}
25
25
 
26
- <!-- Bun maintains an allow-list of popular packages containing `postinstall` scripts that are known to be safe. To run lifecycle scripts for packages that aren't on this list, add the package to `trustedDependencies` in your package.json. -->
27
-
28
26
  ```json-diff
29
27
  {
30
28
  "name": "my-app",
@@ -2,6 +2,12 @@
2
2
  name: Generate a human-readable lockfile
3
3
  ---
4
4
 
5
+ {% callout %}
6
+ Bun v1.1.39 introduced `bun.lock`, a JSONC formatted lockfile. `bun.lock` is human-readable and git-diffable without configuration, at no cost to performance. [**Learn more.**](https://bun.sh/docs/install/lockfile#text-based-lockfile)
7
+ {% /callout %}
8
+
9
+ ---
10
+
5
11
  By default Bun generates a binary `bun.lockb` file when you run `bun install`. In some cases, it's preferable to generate a human-readable lockfile instead.
6
12
 
7
13
  ---
@@ -44,9 +44,11 @@ console.log(values);
44
44
  console.log(positionals);
45
45
  ```
46
46
 
47
+ ---
48
+
47
49
  then it outputs
48
50
 
49
- ```
51
+ ```sh
50
52
  $ bun run cli.ts --flag1 --flag2 value
51
53
  {
52
54
  flag1: true,
@@ -4,9 +4,9 @@ name: Define and replace static globals & constants
4
4
 
5
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
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
7
+ ```sh
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
10
  ```
11
11
 
12
12
  ---
@@ -25,12 +25,12 @@ if (process.env.NODE_ENV === "production") {
25
25
 
26
26
  Before the code reaches the JavaScript engine, Bun replaces `process.env.NODE_ENV` with `"production"`.
27
27
 
28
- ```ts
29
- if ("production" === "production") {
30
- console.log("Production mode");
31
- } else {
32
- console.log("Development mode");
33
- }
28
+ ```ts-diff
29
+ + if ("production" === "production") {
30
+ console.log("Production mode");
31
+ } else {
32
+ console.log("Development mode");
33
+ }
34
34
  ```
35
35
 
36
36
  ---
@@ -39,12 +39,12 @@ It doesn't stop there. Bun's optimizing transpiler is smart enough to do some ba
39
39
 
40
40
  Since `"production" === "production"` is always `true`, Bun replaces the entire expression with the `true` value.
41
41
 
42
- ```ts
43
- if (true) {
44
- console.log("Production mode");
45
- } else {
46
- console.log("Development mode");
47
- }
42
+ ```ts-diff
43
+ + if (true) {
44
+ console.log("Production mode");
45
+ } else {
46
+ console.log("Development mode");
47
+ }
48
48
  ```
49
49
 
50
50
  ---
@@ -2,22 +2,15 @@
2
2
  name: Delete a file
3
3
  ---
4
4
 
5
- To synchronously delete a file with Bun, use the `unlinkSync` function from the [`node:fs`](https://nodejs.org/api/fs.html#fs_fs_unlink_path_callback) module. (Currently, there is no `Bun` API for deleting files.)
5
+ The `Bun.file()` function accepts a path and returns a `BunFile` instance. Use the `.delete()` method to delete the file.
6
6
 
7
7
  ```ts
8
- import { unlinkSync } from "node:fs";
9
-
10
8
  const path = "/path/to/file.txt";
11
- unlinkSync(path);
9
+ const file = Bun.file(path);
10
+
11
+ await file.delete();
12
12
  ```
13
13
 
14
14
  ---
15
15
 
16
- To remove a file asynchronously, use the `unlink` function from the [`node:fs/promises`](https://nodejs.org/api/fs.html#fs_fspromises_unlink_path) module.
17
-
18
- ```ts
19
- import { unlink } from "node:fs/promises";
20
-
21
- const path = "/path/to/file.txt";
22
- await unlink(path);
23
- ```
16
+ See [Docs > API > File I/O](https://bun.sh/docs/api/file-io#reading-files-bun-file) for complete documentation of `Bun.file()`.
@@ -15,6 +15,8 @@ disable = false
15
15
  disableManifest = false
16
16
  ```
17
17
 
18
+ {% /details %}
19
+
18
20
  ## Minimizing re-downloads
19
21
 
20
22
  Bun strives to avoid re-downloading packages multiple times. When installing a package, if the cache already contains a version in the range specified by `package.json`, Bun will use the cached package instead of downloading it again.
@@ -33,14 +35,14 @@ Once a package is downloaded into the cache, Bun still needs to copy those files
33
35
 
34
36
  ## Saving disk space
35
37
 
36
- Since Bun uses hardlinks to "copy" a module into a project's `node_modules` directory on Linux, the contents of the package only exist in a single location on disk, greatly reducing the amount of disk space dedicated to `node_modules`.
38
+ Since Bun uses hardlinks to "copy" a module into a project's `node_modules` directory on Linux and Windows, the contents of the package only exist in a single location on disk, greatly reducing the amount of disk space dedicated to `node_modules`.
37
39
 
38
40
  This benefit also applies to macOS, but there are exceptions. It uses `clonefile` which is copy-on-write, meaning it will not occupy disk space, but it will count towards drive's limit. This behavior is useful if something attempts to patch `node_modules/*`, so it's impossible to affect other installations.
39
41
 
40
42
  {% details summary="Installation strategies" %}
41
43
  This behavior is configurable with the `--backend` flag, which is respected by all of Bun's package management commands.
42
44
 
43
- - **`hardlink`**: Default on Linux.
45
+ - **`hardlink`**: Default on Linux and Windows.
44
46
  - **`clonefile`** Default on macOS.
45
47
  - **`clonefile_each_dir`**: Similar to `clonefile`, except it clones each file individually per directory. It is only available on macOS and tends to perform slower than `clonefile`.
46
48
  - **`copyfile`**: The fallback used when any of the above fail. It is the slowest option. On macOS, it uses `fcopyfile()`; on Linux it uses `copy_file_range()`.
@@ -59,7 +59,7 @@ $ bun install --lockfile-only
59
59
 
60
60
  {% callout %}
61
61
  **Note** - using `--lockfile-only` will still populate the global install cache with registry metadata and git/tarball dependencies.
62
- {% endcallout %}
62
+ {% /callout %}
63
63
 
64
64
  #### Can I opt out?
65
65
 
@@ -30,9 +30,9 @@ process.env.FOO = "hello";
30
30
  Bun supports `--env-file` to override which specific `.env` file to load. You can use `--env-file` when running scripts in bun's runtime, or when running package.json scripts.
31
31
 
32
32
  ```sh
33
- bun --env-file=.env.1 src/index.ts
33
+ $ bun --env-file=.env.1 src/index.ts
34
34
 
35
- bun --env-file=.env.abc --env-file=.env.def run build
35
+ $ bun --env-file=.env.abc --env-file=.env.def run build
36
36
  ```
37
37
 
38
38
  ### Quotation marks
@@ -50,7 +50,7 @@ declare namespace HTMLRewriterTypes {
50
50
 
51
51
  interface Element {
52
52
  tagName: string;
53
- readonly attributes: IterableIterator<string[]>;
53
+ readonly attributes: IterableIterator<[string, string]>;
54
54
  readonly removed: boolean;
55
55
  /** Whether the element is explicitly self-closing, e.g. `<foo />` */
56
56
  readonly selfClosing: boolean;
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.1.43",
2
+ "version": "1.1.44-canary.20250111T140551",
3
3
  "name": "bun-types",
4
4
  "license": "MIT",
5
5
  "main": "",