Now Reading
Bun v0.5 | Bun Weblog

Bun v0.5 | Bun Weblog

2023-01-18 12:09:13

Bun v0.5 is full of new options together with npm workspaces, Bun.dns, and help for node:readline. There’s improved compatibility with node:tls and node:internet so a number of database drivers now work in Bun for the primary time, together with Postgres.js, mysql2, node-redis, and others. Bun additionally continues to get quicker and extra secure — Buffer instantiation is 10x quicker, crypto.createHasher() is 50x quicker, and bun set up bought dozens of bugfixes.

# Set up Bun
curl up | bash

# Improve to newest launch of Bun
bun improve

Bun now helps workspaces in bundle.json, and it is quick. Bun installs the Remix monorepo in about 500ms on Linux.

  • 28x quicker than npm set up
  • 12x quicker than yarn set up (v1)
  • 8x quicker than pnpm set up

What are workspaces?

Workspaces make it simple to develop advanced software program as a monorepo consisting of a number of impartial packages. To strive it, specify an inventory of sub-packages within the workspaces subject of your bundle.json; it is typical to put these sub-packages in a listing known as packages.

  "identify": "my-project",
  "model": "1.0.0",
  "workspaces": ["packages/a", "packages/b"]

Bun would not help globs for workspace names but, however that is coming quickly!

This has a pair main advantages.

  • Code could be break up into logical components. If one bundle depends on one other, you may merely add it as a dependency with bun add. If bundle b depends upon a, bun set up will symlink your native packages/a listing into the node_modules folder of b, as an alternative of attempting to obtain it from the npm registry.
  • Dependencies could be de-duplicated. If a and b share a standard dependency, it will likely be hoisted to the basis node_modules listing. This reduces redundant disk utilization and minimizes “dependency hell” points related to having a number of variations of a bundle put in concurrently.

Bun can now resolve domains utilizing the built-in Bun.dns API. In the intervening time, Bun.dns exposes a single perform: lookup.

import { dns } from "bun";

const information = await dns.lookup("", { household: 4 });
console.log(information); // [{ address: "" }]

We have additionally added a minimal implementation of Node.js’ node:dns that makes use of Bun.dns beneath the hood. It is powered by c-ares and non-blocking getaddrinfo on MacOS.

import { resolve4 } from "node:dns/guarantees";

const information = await resolve4("");
console.log(information); // [ "" ]

Bun now helps the creation of sockets utilizing net.connect() and tls.connect(). This unblocks a number of database driver libraries. A handful of consultant examples:

Hook up with Postgres in Bun utilizing Postgres.js by @porsager:

import postgres from "postgres";

const sql = postgres();
const [{ version }] = await sql`SELECT model()`;

console.log(model); // "PostgreSQL 14.2 ..."

Hook up with MySQL in Bun utilizing mysql2 shopper by @sidorares:

import { createConnection } from "mysql2/promise";

const connection = await createConnection({
  host: "localhost",
  consumer: "root",
  database: "take a look at",

const [rows] = await connection.execute("SELECT 1+2 AS rely");
console.log(rows); // [{ count: 3 }]

Hook up with Redis from Bun utilizing the official Node.js client:

import { createClient } from "redis";

const shopper = createClient();
await shopper.join();

await shopper.set("key", "Hiya!");
const worth = await shopper.get("key");

console.log(worth); // "Hiya!"

Bulding CLI instruments must be a lot simpler now that Bun helps the node:readline module.

import * as readline from "node:readline/guarantees";

const rl = readline.createInterface({
  enter: course of.stdin,
  output: course of.stdout,
  terminal: true,

const reply = await rl.query("How briskly is Bun from 1 to 10?n");
if (parseInt(reply) > 10) {
  console.log("Good reply!");

Operating this script yields:

$ bun readline.ts
How briskly is Bun from 1 to 10?
> 11
Good reply!

A long-standing function request on the WebSocket spec is the power to set customized headers when opening a WebSocket. Whereas this hasn’t but landed within the WebSocket commonplace, Bun now implements it. This permits customers to customise the headers used for the WebSocket shopper handshake request.

const ws = new WebSocket("ws://localhost/chat", {
  headers: {
    Authorization: "...",

Whereas bun wiptest remains to be a piece in progress, we proceed to extend Bun’s compatibility with Jest.


You need to use test.skip() to skip undesirable exams.

import { describe, take a look at, count on } from "bun:take a look at";

describe("fetch()", () => {
  take a look at.skip("can hook up with localhost", async () => {
    const response = await fetch("http://localhost");
    count on(response.okay).toBe(true);

  take a look at("can hook up with", async () => {
    const response = await fetch("");
    count on(response.okay).toBe(true);

If you skip a take a look at, it would seem as grayed out within the take a look at output.

bun wiptest output


You need to use expect(fn).toThrow() to catch anticipated errors.

import { take a look at, count on } from "bun:take a look at";

take a look at("catch error", async () => {
  count on(() => {
    throw new Error();

describe labels are included in the output

Beforehand, nested describe labels weren’t included within the take a look at runner output. Due to @ethanburrell, this has been fastened.

Earlier than:


✓ outer > inside > my take a look at

Check file:

See Also

import { describe, take a look at } from "bun:take a look at";

describe("outer", () => {
  describe("inside", () => {
    take a look at("my take a look at", () => {});

10x faster new Buffer()

Beforehand, the Buffer implementation in Bun was utilizing Object.setPrototypeOf() to create every new occasion. Eliminating this bottleneck makes it 10x quicker to instantiate a small Buffer in Bun.

10x faster Buffer

50x faster crypto.createHash()

Beforehand, Bun was utilizing a pure JavaScript implementation of crypto.createHash(). Now it is applied utilizing native code from BoringSSL, yielding a 50x pace enchancment.

Bun will now acknowledge the HTTPS_PROXY, HTTP_PROXY, and NO_PROXY enviroment variables when making outgoing HTTP requests, which incorporates fetch() and bun set up. These variables can help you specify a proxy to ahead, or not ahead, sure HTTP requests and are helpful when operating Bun inside a company firewall.

export HTTPS_PROXY=""
export NO_PROXY="localhost,"

If you wish to be taught extra about these variables, GitLab wrote a pleasant explainer.

There are two modifications to module decision which will influence a number of packages.

  1. Bun now not checks the browser property in bundle.json. It’s because some packages would disable Node.js performance, which isn’t what we wish for Bun.
  2. For higher Node.js & npm compatibility, Bun’s JavaScript runtime now reads the "node" export situation in bundle.json exports.

The order Bun’s JavaScript runtime reads bundle.json "exports" situations is:

["bun", "worker", "module", "node", "browser", "default"];

Because of this if a bundle has a "node" export situation, it will likely be used as an alternative of the "default" or "browser" export situation.

Whereas we proceed so as to add new options to Bun, we’re nonetheless targeted on enhancing stability and fixing bugs. This launch fixes plenty of points

Fixes to bun install

A number of bugs with Bun’s bundle supervisor are fastened on this launch, principally by @alexlamsl. Thanks Alex!

#1664 Beforehand, scoped and personal packages configured with bun set up would have a registry of localhost, which made little or no sense. We have fastened this and personal registries will now default to the default registry if not specified, which is normally
#1667 Sometimes, npm purchasers are speculated to cross the npm-auth-type header, however bun set up wasn’t. We have fastened this and now bun set up will cross the npm-auth-type header
a345efd In some CI environments, like Vercel, linking node_modules/.bin would fail as a result of the /proc filesystem (used to resolve absolute file paths) wasn’t mounted. We have fastened this by falling again to fchdir and getcwd when /proc/fd is just not obtainable
385c81d A crash generally occurred when operating bun add <bundle> if the "dependencies" checklist went from empty to not empty in a bundle.json
#1665 Use npm as default registry when scopes are configured
#1671 Repair logging verbosity in bun set up
#1799 Repair lifecycle script execution in bun set up

New APIs

Additional fixes

Thanks to everybody who contributed to Bun v0.5!

Source Link

What's Your Reaction?
In Love
Not Sure
View Comments (0)

Leave a Reply

Your email address will not be published.

2022 Blinking Robots.
WordPress by Doejo

Scroll To Top