> ## 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 API

> Bun 全局对象和内置模块中可用的 Bun 原生 API 概述

Bun 在 `Bun` 全局对象以及多个内置模块中实现了一组原生 API。这些 API 经过了高度优化，代表了在实现某些常见功能时最为“Bun 原生”的规范方式。

Bun 力求尽可能实现标准 Web API。Bun 主要为服务器端任务引入新的 API，例如文件 I/O 和启动 HTTP 服务器等标准中尚不存在的任务。在此类情况下，Bun 的方法依然构建于诸如 `Blob`、`URL` 和 `Request` 等标准 API 之上。

```ts server.ts icon="https://mintcdn.com/bun-zhcndoc/cnUTwgMuf4cCrwC-/icons/typescript.svg?fit=max&auto=format&n=cnUTwgMuf4cCrwC-&q=85&s=e7767043c9e885c34f2d6c8fe2a95217" theme={"theme":{"light":"github-light","dark":"dracula"}}
Bun.serve({
  fetch(req: Request) {
    return new Response("成功！");
  },
});
```

点击右侧列表中的链接可跳转至相关文档。

| 主题               | API                                                                                                                                                                                                                                                                                                                    |
| ---------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| HTTP 服务器         | [`Bun.serve`](/runtime/http/server)                                                                                                                                                                                                                                                                                    |
| Shell            | [`$`](/runtime/shell)                                                                                                                                                                                                                                                                                                  |
| Bundler          | [`Bun.build`](/bundler)                                                                                                                                                                                                                                                                                                |
| 文件 I/O           | [`Bun.file`](/runtime/file-io#reading-files-bun-file), [`Bun.write`](/runtime/file-io#writing-files-bun-write), `Bun.stdin`, `Bun.stdout`, `Bun.stderr`                                                                                                                                                                |
| 子进程              | [`Bun.spawn`](/runtime/child-process#spawn-a-process-bun-spawn), [`Bun.spawnSync`](/runtime/child-process#blocking-api-bun-spawnsync)                                                                                                                                                                                  |
| TCP 套接字          | [`Bun.listen`](/runtime/networking/tcp#start-a-server-bun-listen), [`Bun.connect`](/runtime/networking/tcp#start-a-server-bun-listen)                                                                                                                                                                                  |
| UDP 套接字          | [`Bun.udpSocket`](/runtime/networking/udp)                                                                                                                                                                                                                                                                             |
| WebSocket        | `new WebSocket()`（客户端），[`Bun.serve`](/runtime/http/websockets)（服务器）                                                                                                                                                                                                                                                    |
| 转译器              | [`Bun.Transpiler`](/runtime/transpiler)                                                                                                                                                                                                                                                                                |
| 路由               | [`Bun.FileSystemRouter`](/runtime/file-system-router)                                                                                                                                                                                                                                                                  |
| 流式 HTML          | [`HTMLRewriter`](/runtime/html-rewriter)                                                                                                                                                                                                                                                                               |
| 无头浏览器            | [`Bun.WebView`](/runtime/webview)                                                                                                                                                                                                                                                                                      |
| 哈希               | [`Bun.password`](/runtime/hashing#bun-password), [`Bun.hash`](/runtime/hashing#bun-hash), [`Bun.CryptoHasher`](/runtime/hashing#bun-cryptohasher), `Bun.sha`                                                                                                                                                           |
| CSRF 保护          | [`Bun.CSRF.generate`](/runtime/csrf), [`Bun.CSRF.verify`](/runtime/csrf)                                                                                                                                                                                                                                               |
| SQLite           | [`bun:sqlite`](/runtime/sqlite)                                                                                                                                                                                                                                                                                        |
| PostgreSQL 客户端   | [`Bun.SQL`](/runtime/sql), `Bun.sql`                                                                                                                                                                                                                                                                                   |
| Redis（Valkey）客户端 | [`Bun.RedisClient`](/runtime/redis), `Bun.redis`                                                                                                                                                                                                                                                                       |
| FFI（外部函数接口）      | [`bun:ffi`](/runtime/ffi)                                                                                                                                                                                                                                                                                              |
| DNS              | [`Bun.dns.lookup`](/runtime/networking/dns), `Bun.dns.prefetch`, `Bun.dns.getCacheStats`                                                                                                                                                                                                                               |
| 测试               | [`bun:test`](/test)                                                                                                                                                                                                                                                                                                    |
| 工作线程             | [`new Worker()`](/runtime/workers)                                                                                                                                                                                                                                                                                     |
| 模块加载器            | [`Bun.plugin`](/bundler/plugins)                                                                                                                                                                                                                                                                                       |
| Glob             | [`Bun.Glob`](/runtime/glob)                                                                                                                                                                                                                                                                                            |
| Cookie           | [`Bun.Cookie`](/runtime/cookies), [`Bun.CookieMap`](/runtime/cookies)                                                                                                                                                                                                                                                  |
| Node-API         | [`Node-API`](/runtime/node-api)                                                                                                                                                                                                                                                                                        |
| `import.meta`    | [`import.meta`](/runtime/module-resolution#import-meta)                                                                                                                                                                                                                                                                |
| 工具               | [`Bun.version`](/runtime/utils#bun-version), [`Bun.revision`](/runtime/utils#bun-revision), [`Bun.env`](/runtime/utils#bun-env), [`Bun.main`](/runtime/utils#bun-main)                                                                                                                                                 |
| 睡眠与计时            | [`Bun.sleep()`](/runtime/utils#bun-sleep), [`Bun.sleepSync()`](/runtime/utils#bun-sleepsync), [`Bun.nanoseconds()`](/runtime/utils#bun-nanoseconds)                                                                                                                                                                    |
| 随机数与 UUID        | [`Bun.randomUUIDv7()`](/runtime/utils#bun-randomuuidv7)                                                                                                                                                                                                                                                                |
| 系统与环境            | [`Bun.which()`](/runtime/utils#bun-which)                                                                                                                                                                                                                                                                              |
| 比较与检查            | [`Bun.peek()`](/runtime/utils#bun-peek), [`Bun.deepEquals()`](/runtime/utils#bun-deepequals), `Bun.deepMatch`, [`Bun.inspect()`](/runtime/utils#bun-inspect)                                                                                                                                                           |
| 字符串与文本处理         | [`Bun.escapeHTML()`](/runtime/utils#bun-escapehtml), [`Bun.stringWidth()`](/runtime/utils#bun-stringwidth), `Bun.indexOfLine`                                                                                                                                                                                          |
| URL 与路径工具        | [`Bun.fileURLToPath()`](/runtime/utils#bun-fileurltopath), [`Bun.pathToFileURL()`](/runtime/utils#bun-pathtofileurl)                                                                                                                                                                                                   |
| 压缩               | [`Bun.gzipSync()`](/runtime/utils#bun-gzipsync), [`Bun.gunzipSync()`](/runtime/utils#bun-gunzipsync), [`Bun.deflateSync()`](/runtime/utils#bun-deflatesync), [`Bun.inflateSync()`](/runtime/utils#bun-inflatesync), `Bun.zstdCompressSync()`, `Bun.zstdDecompressSync()`, `Bun.zstdCompress()`, `Bun.zstdDecompress()` |
| 流处理              | [`Bun.readableStreamTo*()`](/runtime/utils#bun-readablestreamto), `Bun.readableStreamToBytes()`, `Bun.readableStreamToBlob()`, `Bun.readableStreamToFormData()`, `Bun.readableStreamToJSON()`, `Bun.readableStreamToArray()`                                                                                           |
| 内存与缓冲区管理         | `Bun.ArrayBufferSink`, `Bun.allocUnsafe`, `Bun.concatArrayBuffers`                                                                                                                                                                                                                                                     |
| 模块解析             | [`Bun.resolveSync()`](/runtime/utils#bun-resolvesync)                                                                                                                                                                                                                                                                  |
| 解析与格式化           | [`Bun.semver`](/runtime/semver), [`Bun.TOML.parse`](/runtime/toml), [`Bun.markdown`](/runtime/markdown), [`Bun.color`](/runtime/color), [`Bun.Image`](/runtime/image)                                                                                                                                                  |
| 底层 / 内部          | `Bun.mmap`, `Bun.gc`, `Bun.generateHeapSnapshot`, [`bun:jsc`](https://bun.com/reference/bun/jsc)                                                                                                                                                                                                                       |
