bun
约 3 分钟阅读
- oven-sh/bunGitHuboven-sh/bunoven-sh/bun · MIT, Zig · JavaScriptCore,tinycc,libiconv LGPLv2 · boringssl,libarchive,libiconv,lol-html,mimalloc,picohttp,tinycc,uSockets,zlib-cloudflare,libicu,uWebsockets笔记:bun
- MIT, Zig
- JavaScriptCore,tinycc,libiconv LGPLv2
- boringssl,libarchive,libiconv,lol-html,mimalloc,picohttp,tinycc,uSockets,zlib-cloudflare,libicu,uWebsockets
- @types/bun,
bun-types- typing
- 参考
- SaltyAom/bun-http-framework-benchmarkGitHubSaltyAom/bun-http-framework-benchmarkoven-sh/bun · MIT, Zig · JavaScriptCore,tinycc,libiconv LGPLv2 · boringssl,libarchive,libiconv,lol-html,mimalloc,picohttp,tinycc,uSockets,zlib-cloudflare,libicu,uWebsockets笔记:bun
- oven-sh/awesome-bunGitHuboven-sh/awesome-bunoven-sh/bun · MIT, Zig · JavaScriptCore,tinycc,libiconv LGPLv2 · boringssl,libarchive,libiconv,lol-html,mimalloc,picohttp,tinycc,uSockets,zlib-cloudflare,libicu,uWebsockets笔记:bun
- bun 还有很多不支持的 addon 库 #4290GitHub Issueoven-sh/bun#4290
- better-sqlite3
- Roadmap #159GitHub Issueoven-sh/bun#159
- bun 能力是独一无二的
- 可以为了 ffi 而使用 bun
- bun 目前还不适用于通用的生产环境
- 相对来说,bun 更适用于前端项目而不是后端项目,因为 后端 项目通常需要更多的 node 兼容模块
- bun 非常不建议直接用于后端
- 可能会遇到各式各样无法修复的错误
- 库的开发者一般也不会针对 bun 测试
- deploy bun#211GitHub Issueoven-sh/bun#211
- 未实现
dns,http2, https, child_process, vm, tls, async_hooks, worker_threads, inspector - 未实现 HTTP request body streaming
- Streaming upload with fetch #6017GitHub Issueoven-sh/bun#6017
- 兼容
- 支持 vitest #4145GitHub Issueoven-sh/bun#4145
- 支持 ReactNative #123GitHub Issueoven-sh/bun#123
- fetch 100 continue 问题 #7428GitHub Issueoven-sh/bun#7428
- S3 SDK, fetch 请求
支持 NestJS #1641GitHub Issueoven-sh/bun#1641
- bun 嵌入了 tinycc,有 jit 能力,因此 ffi 效率非常高
- bun 还处于非常早期开发阶段
- 使用 JavaScriptCore - 各有优缺点
- bun 一些点的性能很好,但整体应用性能比不上 node
# Manually Install
# =======================
# ~/.bun/bin/bun
curl -fsSL https://bun.com/install | bash
# macOS
curl -LO https://github.com/oven-sh/bun/releases/download/bun-v1.3.3/bun-darwin-x64.zip
unzip bun-darwin-x64.zip
mv bun-darwin-x64/bun ~/bin/
xattr -r -d com.apple.quarantine ~/bin/bun
# AlpineLinux
curl -LO https://github.com/oven-sh/bun/releases/download/bun-v1.3.3/bun-linux-x64-musl.zip
unzip bun-linux-x64-musl.zip
sudo mv bun-linux-x64-musl/bun /usr/local/bin/
# Linux
curl -LO https://github.com/oven-sh/bun/releases/download/bun-v1.3.3/bun-linux-x64.zip
unzip bun-linux-x64.zip
sudo mv bun-linux-x64/bun /usr/local/bin/
bun -v
bun upgrade # 升级
bun upgrade --canary # 升级到尚未发布版本 - bun 开发很快,可以提前尝试一些特性
docker run --rm -it wener/bun bun -v
docker run --rm --init --ulimit memlock=-1:-1 oven/bun # 官方镜像
docker run -it --rm -v $PWD:/host -w /host -p 3000:3000 --entrypoint bash oven/bun:debian
# 安装脚本
curl -fsSL https://bun.com/install | bashbun bun src/index.js
# https://github.com/oven-sh/bun/issues/211
./node_modules.bun > ./node_modules.jsnpm add -D @types/bunPackages@types/bunnpm Package@types/bunNPM
ffi
- 目前应该无法将 number 转换为 Pointer
// 注意转换为 CString 需要 \0 结尾
function ptrOfStr(s?: string) {
return typeof s === 'string' ? ptr(Buffer.from(s + '\0', 'utf8')) : null;
}- 64 bit 的 processor 使用52bit 的地址空间
- js 支持 53bit 的整数
- Wikipedia:64-bit computingWikipedia64-bit computing
- 不支持 struct
- oven-sh/bun#6139GitHub Issueoven-sh/bun#6139
- 使用 toArrayBuffer 和 read 来直接获取 ptr 数据
import { viewSource } from 'bun:ffi';
// 生成的 C 代码
console.log(
viewSource(
{
hello_world: {
returns: 'float',
args: ['float'],
},
},
false,
)[0],
);import { dlopen, FFIType } from 'bun:ffi';
// /lib/x86_64-linux-gnu/libc.so.6
// /usr/lib/x86_64-linux-gnu/libc.so.6
const { printf } = dlopen('libc.so.6', {
printf: { args: [FFIType.cstring, FFIType.f64] },
}).symbols;
printf(new TextEncoder().encode('%.17f\n'), 4);memory
- bun.sh/docs/project/benchmarking
- MIMALLOC_SHOW_STATS=1
Bun.gc(true); // synchronous
Bun.gc(false); // asynchronousmacro
- 类似 inline+预执行
import { random } from './random.ts' with { type: 'macro' };compile
# --sourcemap --minify --target bun-linux-x64-modern
# 90MB+
bun build --compile ./src/main.ts --outfile myapp --external={pg-query-stream,better-sqlite3,tedious,oracledb,mysql2,sqlite3,mysql} --external=@mikro-orm/{migrations,entity-generator,better-sqlite,sqlite,mariadb,mysql,mongodb,seeder}debug
bun --inspect server.tsFAQ
hono
Failed to find Response internal state key- 检查下是不是用了 node 的 serve 而不是 Bun.serve
- honojs/hono#2466GitHub Issuehonojs/hono#2466
Failed to open library. This is usually caused by a missing library or an invalid library path.
检查下是不是 arch 错了。
request timed out after 10 seconds. Pass idleTimeout to configure.
Bun.serve({
...app,
// timeout in 60s
idleTimeout: 60
})Bun.serve expects idleTimeout to be 255 or less
- idleTimeout 可以为 0
zig to rust
- 社区反应
- Bun support is now limited and deprecated
- yt-dlp/yt-dlp#16766GitHub Issueyt-dlp/yt-dlp#16766
- Bun support is now limited and deprecated
关联信息
反向链接、本文链接的其他页面和外部资料。
反向链接
- elysia笔记 · bun
elysiajs/elysia · 基于 bun 的 Web 框架 · 使用 @sinclair/typebox · 代码量很少,逻辑简单 · AOT · 生成代码来进行 route · route 变成一个大 switch · 与 hono 非常相似,但面向 bun 为主
References
GitHub14 条
- honojs/hono#2466github.com/honojs/hono/issues/2466
- oven-sh/awesome-bungithub.com/oven-sh/awesome-bun
- oven-sh/bungithub.com/oven-sh/bun
- oven-sh/bun#123github.com/oven-sh/bun/issues/123
- oven-sh/bun#159github.com/oven-sh/bun/issues/159
- oven-sh/bun#1641github.com/oven-sh/bun/issues/1641
- oven-sh/bun#211github.com/oven-sh/bun/issues/211
- oven-sh/bun#4145github.com/oven-sh/bun/issues/4145
- oven-sh/bun#4290github.com/oven-sh/bun/issues/4290
- oven-sh/bun#6017github.com/oven-sh/bun/issues/6017
- oven-sh/bun#6139github.com/oven-sh/bun/issues/6139
- oven-sh/bun#7428github.com/oven-sh/bun/issues/7428
- SaltyAom/bun-http-framework-benchmarkgithub.com/SaltyAom/bun-http-framework-benchmark
- yt-dlp/yt-dlp#16766github.com/yt-dlp/yt-dlp/issues/16766
Wikipedia1 条
- 64-bit computingWikipedia:64-bit computing
其他外链1 条
- bun.sh/docs/project/benchmarkingbun.sh/docs/project/benchmarking