Skip to main content

Documentation Index

Fetch the complete documentation index at: https://bun.zhcndoc.com/llms.txt

Use this file to discover all available pages before exploring further.

Bun 在 Bun 全局对象以及多个内置模块中实现了一组原生 API。这些 API 经过了高度优化,代表了在实现某些常见功能时最为“Bun 原生”的规范方式。 Bun 力求尽可能实现标准 Web API。Bun 主要为服务器端任务引入新的 API,例如文件 I/O 和启动 HTTP 服务器等标准中尚不存在的任务。在此类情况下,Bun 的方法依然构建于诸如 BlobURLRequest 等标准 API 之上。
https://mintcdn.com/bun-zhcndoc/cnUTwgMuf4cCrwC-/icons/typescript.svg?fit=max&auto=format&n=cnUTwgMuf4cCrwC-&q=85&s=e7767043c9e885c34f2d6c8fe2a95217server.ts
Bun.serve({
  fetch(req: Request) {
    return new Response("成功!");
  },
});
点击右侧列表中的链接可跳转至相关文档。
主题API
HTTP 服务器Bun.serve
Shell$
BundlerBun.build
文件 I/OBun.file, Bun.write, Bun.stdin, Bun.stdout, Bun.stderr
子进程Bun.spawn, Bun.spawnSync
TCP 套接字Bun.listen, Bun.connect
UDP 套接字Bun.udpSocket
WebSocketnew WebSocket()(客户端),Bun.serve(服务器)
转译器Bun.Transpiler
路由Bun.FileSystemRouter
流式 HTMLHTMLRewriter
无头浏览器Bun.WebView
哈希Bun.password, Bun.hash, Bun.CryptoHasher, Bun.sha
CSRF 保护Bun.CSRF.generate, Bun.CSRF.verify
SQLitebun:sqlite
PostgreSQL 客户端Bun.SQL, Bun.sql
Redis(Valkey)客户端Bun.RedisClient, Bun.redis
FFI(外部函数接口)bun:ffi
DNSBun.dns.lookup, Bun.dns.prefetch, Bun.dns.getCacheStats
测试bun:test
工作线程new Worker()
模块加载器Bun.plugin
GlobBun.Glob
CookieBun.Cookie, Bun.CookieMap
Node-APINode-API
import.metaimport.meta
工具Bun.version, Bun.revision, Bun.env, Bun.main
睡眠与计时Bun.sleep(), Bun.sleepSync(), Bun.nanoseconds()
随机数与 UUIDBun.randomUUIDv7()
系统与环境Bun.which()
比较与检查Bun.peek(), Bun.deepEquals(), Bun.deepMatch, Bun.inspect()
字符串与文本处理Bun.escapeHTML(), Bun.stringWidth(), Bun.indexOfLine
URL 与路径工具Bun.fileURLToPath(), Bun.pathToFileURL()
压缩Bun.gzipSync(), Bun.gunzipSync(), Bun.deflateSync(), Bun.inflateSync(), Bun.zstdCompressSync(), Bun.zstdDecompressSync(), Bun.zstdCompress(), Bun.zstdDecompress()
流处理Bun.readableStreamTo*(), Bun.readableStreamToBytes(), Bun.readableStreamToBlob(), Bun.readableStreamToFormData(), Bun.readableStreamToJSON(), Bun.readableStreamToArray()
内存与缓冲区管理Bun.ArrayBufferSink, Bun.allocUnsafe, Bun.concatArrayBuffers
模块解析Bun.resolveSync()
解析与格式化Bun.semver, Bun.TOML.parse, Bun.markdown, Bun.color, Bun.Image
底层 / 内部Bun.mmap, Bun.gc, Bun.generateHeapSnapshot, bun:jsc