Skip to content

feat: Add Bun runtime compatibility layer#5877

Closed
lalalune wants to merge 1 commit intocloudflare:mainfrom
lalalune:feat/bun-runtime-compat
Closed

feat: Add Bun runtime compatibility layer#5877
lalalune wants to merge 1 commit intocloudflare:mainfrom
lalalune:feat/bun-runtime-compat

Conversation

@lalalune
Copy link
Copy Markdown

Summary

This adds a Bun-compatible API layer for workerd, enabling Bun applications to run on Cloudflare Workers infrastructure.

Usage

pnpm run build:bun
// worker.js
import Bun from './bun-bundle.js'

export default {
  async fetch(request) {
    const hash = Bun.hash('hello world')
    await Bun.write('/tmp/data.txt', 'Hello from Bun')
    const file = Bun.file('/tmp/data.txt')
    const content = await file.text()
    
    return new Response(JSON.stringify({
      hash: hash.toString(16),
      content,
      bunVersion: Bun.version
    }))
  }
}

Features

  • File Operations: Uses node:fs (same filesystem limitations as nodejs_compat; typically /tmp with enable_web_file_system)
  • Password Hashing: PBKDF2 via WebCrypto
  • DNS: Real DNS-over-HTTPS (DoH) via Cloudflare/Google
  • SQLite: In-memory SQLite implementation
  • Hashing: wyhash, crc32, adler32, cityhash32/64, murmur32v3/64v2
  • Utilities: sleep, escapeHTML, stringWidth, deepEquals, inspect, streams

Samples

  • samples/bun-bundle/ - Demonstrates bundled compatibility layer
  • samples/bun-hello/ - Demonstrates native bun:* imports

Files Changed

  • src/bun/ - TypeScript implementation (14 files)
  • src/workerd/api/bun/ - C++ integration (2 files)
  • src/workerd/api/modules.h - Module registration
  • src/workerd/api/BUILD.bazel - Build dependencies
  • samples/bun-bundle/ and samples/bun-hello/ - Sample workers

Test Results

  • bun.test.ts: 180 tests passed
  • sqlite.test.ts: 26 tests passed
  • dns.test.ts: 15 tests passed

@lalalune lalalune requested review from a team as code owners January 14, 2026 04:55
@github-actions
Copy link
Copy Markdown


Thank you for your submission, we really appreciate it. Like many open-source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution. You can sign the CLA by just posting a Pull Request Comment same as the below format.


I have read the CLA Document and I hereby sign the CLA


You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot.

@lalalune lalalune requested review from a team as code owners January 14, 2026 04:56
@jasnell
Copy link
Copy Markdown
Collaborator

jasnell commented Jan 14, 2026

While we appreciate outside contributions, at this time we do not intend to impl (or add someone else's impl) of any bun-specific APIs to the Workers runtime. It is something we've discussed and have currently ruled out. There is just simply not enough ecosystem demand to justify the effort that would be needed to maintain it. It's conceivable that at some future point in time we might reconsider but it's unlikely to be any time soon.

(I left this comment in the original PR as well)

Adds a Bun-compatible API layer enabling Bun applications to run on workerd.

## Features
- Hash functions: wyhash, crc32, adler32, cityhash, murmur (JS implementations)
- Password hashing: PBKDF2 via WebCrypto
- File operations: node:fs with /tmp via nodejs_compat (lazy-loaded)
- DNS: DNS-over-HTTPS via Cloudflare/Google
- Utilities: deepEquals, escapeHTML, inspect, stringWidth, sleep
- Stream utilities: readableStreamToText/Array/ArrayBuffer/Blob/JSON
- ArrayBufferSink for buffer accumulation

## Not Supported
- bun:sqlite (throws error, directs to Cloudflare D1)
- bun:ffi (throws error, directs to WASM)
- bun:test (stubs only)

## Usage
```javascript
import Bun from './bun-bundle.js'

export default {
  async fetch(request) {
    const hash = Bun.hash('hello')
    await Bun.write('/tmp/data.txt', 'Hello')
    return new Response(await Bun.file('/tmp/data.txt').text())
  }
}
```

## Files
- src/bun/ - TypeScript implementation
- dist/bun/bun-bundle.js - Bundled compatibility layer
- samples/helloworld-bun/ - Sample worker

Tested: 198 unit tests pass, verified in real workerd
@lalalune lalalune force-pushed the feat/bun-runtime-compat branch from 0c9270d to c25f530 Compare January 14, 2026 08:43
@lalalune
Copy link
Copy Markdown
Author

okay makes sense, will close

@lalalune lalalune closed this Jan 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants