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 -fsSL https://bun.sh/install | bashnpm install -g bunpowershell -c "irm bun.sh/install.ps1|iex"scoop install bunbrew tap oven-sh/bunbrew install bundocker pull oven/bundocker run --rm --init --ulimit memlock=-1:-1 oven/bunTo upgrade Bun
bun upgradeN-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!":
#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;
}
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:
uint8_t lastByte(uint8_t *arr, size_t len) {
return arr[len - 1];
}
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.jscpu: 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 nsBenchmark 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 @paperclover 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.