Bun

Bun v1.1.29


Jarred Sumner ยท September 20, 2024

Bun v1.1.29 is here! This release fixes 21 bugs (addressing 22 ๐Ÿ‘). Fixes issue with proxying requests with axios. N-API (napi) support in compiled C. Faster typed arrays in bun:ffi with 'buffer' type. Fixes issue with bundling imported .wasm files in bun build. Fixes regression with bun build from v1.1.28. Fixes snapshot regression in bun test. Fixes bug with argument validation in Bun.build plugins.

We're hiring systems engineers in San Francisco to build the future of JavaScript!

To install Bun

curl
npm
powershell
scoop
brew
docker
curl
curl -fsSL https://bun.sh/install | bash
npm
npm install -g bun
powershell
powershell -c "irm bun.sh/install.ps1|iex"
scoop
scoop install bun
brew
brew tap oven-sh/bun
brew install bun
docker
docker pull oven/bun
docker run --rm --init --ulimit memlock=-1:-1 oven/bun

To upgrade Bun

bun upgrade

N-API (napi) support in compiled C

In Bun v1.1.28 (previous version), we introduced support for compiling and running C code from JavaScript.

In this release, we've added N-API (napi) support to cc. This makes it easier to return JavaScript strings, objects, arrays and other non-primitive values from C code.

For example, this returns a JavaScript string "Hello, Napi!":

hello-napi.c
hello-napi.js
hello-napi.c
#include <node/node_api.h>

napi_value hello_napi(napi_env env) {
  napi_value result;
  napi_create_string_utf8(env, "Hello, Napi!", NAPI_AUTO_LENGTH, &result);
  return result;
}
hello-napi.js
import { cc } from "bun:ffi";
import source from "./hello-napi.c" with {type: "file"};
const hello_napi = cc({
  source,
  symbols: {
    hello_napi: {
      args: ["napi_env"],
      returns: "napi_value",
    },
  },
});

console.log(hello_napi());
// => "Hello, Napi!"

You can continue to use types like int, float, double to send & receive primitive values from C code, but now you can also use N-API types! Also, this works when using dlopen to load shared libraries with bun:ffi (such as Rust or C++ libraries with C ABI exports).

"buffer" type in bun:ffi

You can now use the "buffer" type in bun:ffi to send and receive JavaScript typed arrays (Uint8Array, Buffer, etc) in C code slightly faster and simpler.

The following example returns the last byte of a Uint8Array:

last-byte.c
last-byte.js
last-byte.c
uint8_t lastByte(uint8_t *arr, size_t len) {
  return arr[len - 1];
}
last-byte.js
import { cc } from "bun:ffi";
import source from "./last-byte.c" with {type: "file"};
const lastByte = cc({
  source,
  symbols: {
    lastByte: {
      args: ["buffer", "usize"],
      returns: "u8",
    },
  },
});

const arr = new Uint8Array([1, 2, 3, 4, 5]);
console.log(lastByte(arr, arr.length));
// => 5

In this microbenchmark, the hash function gets 30% faster by using the "buffer" type in bun:ffi instead of wrapping the Uint8Array in the ptr() function.

bun ./bench.js
cpu: Apple M3 Max
runtime: bun 1.1.29 (arm64-darwin)

benchmark          time (avg)             (min โ€ฆ max)       p75       p99      p995
----------------------------------------------------- -----------------------------
hash (ptr)      82.63 ns/iter   (79.6 ns โ€ฆ 172.82 ns)  81.65 ns 111.22 ns 118.77 ns
hash (buffer)   48.59 ns/iter    (45.04 ns โ€ฆ 94.9 ns)  50.09 ns  79.17 ns  81.93 ns

Benchmark code

Under the hood, we've made the conversion between JavaScript typed arrays and C uint8_t* happen inline without external function calls, which makes it faster than using the ptr function from bun:ffi.

Fixed: proxy with axios

A bug causing proxying requests with axios to not work properly is fixed.

Thanks to @cirospaciari for the fix!

Fixed: Bundling imported .wasm files in bun build

A bug that could cause a crash when bundling imported .wasm files in bun build is fixed.

Thanks to @dylan-conway for the fix!

Fixed: regression with bun build from v1.1.28

A regression where certain symbols were being re-ordered incorrectly in bun build is fixed.

Thanks to @paperdave for the fix!

Fixed: snapshot regression in bun test

A regression that caused bun test to always update snapshots is fixed.

Thanks to @dylan-conway for the fix!

Fixed: Argument validation issue in Bun.build plugins

A crash that could happen when passing an incorrect argument to the plugins option in Bun.build is fixed.

Thanks to @dylan-conway for the fix!

Fixed: cc missing include directories on Linux and macOS

A bug that caused cc to not find the default system include directories on Linux and macOS is fixed.

Thanks to 3 contributors!