Bun 1.2 Improves Node Compatibility and Adds Postgres Client

The Bun team has unveiled Bun v1.2, marking a significant stride in enhancing compatibility with Node.js, a cornerstone of Bun’s appeal. This latest version introduces a native S3 object storage API alongside a built-in Postgres client, complementing the existing SQLite client.

Enhanced Node.js Compatibility

Designed as a seamless drop-in replacement for Node.js, Bun has made Node.js compatibility a primary focus. With the release of Bun 1.2, the team has implemented a rigorous approach to compatibility testing by executing the Node.js test suite with each code modification. Core Node modules such as path, os, string_decoder, puny_code, query_string, url, events, stream, and fs now successfully pass 90% of the Node.js test suite.

However, the team encountered challenges in running the Node.js test suite directly due to its inherent idiosyncrasies. For example, the suite verifies exact Node.js error message strings, which can vary between releases. Additionally, some tests depended on internal Node.js details. To address this, the Bun team adapted the Node test suite for Bun, substituting error strings with error codes, thereby allowing Bun to enrich error messages with more informative details.

New Features and Performance Improvements

Bun v1.2 also introduces support for the node:http2 module, enabling the creation of HTTP/2 servers with a claimed 2x speed enhancement compared to the equivalent module in Node.js. Furthermore, support for node:dgram (UDP socket management), node:cluster (spawning new Bun instances), and node:zlib has been added.

Built-in S3 Object Storage

A noteworthy feature of Bun 1.2 is the built-in support for S3, allowing developers to read, write, and delete files from S3 buckets using APIs that align with Web standards like Blob. The release notes highlight:

Decoupling storage from compute prevents an entire class of reliability issues: low disk space, high p95 response times from busy I/O, and security issues with shared file storage.

Recognized as the defacto-standard for cloud object storage, S3 APIs are utilized by various cloud services, including Amazon S3, Google Cloud Storage, and Cloudflare R2. The native S3 client in Bun boasts a 5x speed improvement for file downloads compared to packages like @aws-sdk/client-s3 in Node.js.

Expanded SQL Database Support

The release also broadens support for SQL databases, introducing Bun.sql, a built-in SQL client with Postgres capabilities. This new client complements the existing built-in SQLite client.

SQLite serves a diverse range of applications. As noted by Wesley Aptekar-Cassels on his blog:

On the whole, I think using SQLite is a good tradeoff for a lot of projects, including web apps that expect to have a potentially large number of users. As long as you don’t expect to need tens of thousands of small writes per second, thousands of large writes, or long-lived write transactions, it’s highly likely that SQLite will support your use case. It significantly reduces complexity and operational burden and eases testing, with the primary downside that it’s somewhat harder to get levels of availability and uptime that almost no one needs in the first place.

With Postgres support, Bun developers gain a robust option for scenarios where SQLite may not suffice. The Postgres client is crafted in native code, featuring optimizations such as automatic prepared statements, query pipelining, connection pooling, and structure caching. The release notes assert a potential 50% speed increase when reading rows compared to the most popular Postgres clients available for Node.js.

By integrating built-in support for widely-used data stores like S3 and Postgres, Bun aims to meet the evolving demands of production applications, offering scalable, cloud-native solutions with reduced reliance on external dependencies.

For a comprehensive overview of the features and enhancements in this substantial release, developers are encouraged to explore the full release notes.

Bun is developed in Zig and utilizes WebKit’s JavaScriptCore as its JavaScript engine. The inaugural version, Bun 1.0, was launched in September 2023.

About the Author

Bruno Couriol


Show moreShow less

Tech Optimizer
Bun 1.2 Improves Node Compatibility and Adds Postgres Client