在 Bun 中,YAML 是與 JSON 和 TOML 並列的一等公民。你可以:
- 使用
Bun.YAML.parse解析 YAML 字符串 - 在運行時將 YAML 文件作為模塊
import和require(包括熱重新加載和監聽模式支持) - 通過 Bun 的打包器在前端應用中
import和requireYAML 文件
一致性
Bun 的 YAML 解析器目前通過了官方 YAML 測試套件的 90% 以上。雖然我們正在積極努力達到 100% 一致性,但當前實現涵蓋了絕大多數實際用例。解析器用 Zig 編寫以獲得最佳性能,並持續改進。
運行時 API
Bun.YAML.parse()
將 YAML 字符串解析為 JavaScript 對象。
ts
import { YAML } from "bun";
const text = `
name: John Doe
age: 30
email: john@example.com
hobbies:
- reading
- coding
- hiking
`;
const data = YAML.parse(text);
console.log(data);
// {
// name: "John Doe",
// age: 30,
// email: "john@example.com",
// hobbies: ["reading", "coding", "hiking"]
// }多文檔 YAML
當解析包含多個文檔的 YAML(用 --- 分隔)時,Bun.YAML.parse() 返回一個數組:
ts
const multiDoc = `
---
name: Document 1
---
name: Document 2
---
name: Document 3
`;
const docs = Bun.YAML.parse(multiDoc);
console.log(docs);
// [
// { name: "Document 1" },
// { name: "Document 2" },
// { name: "Document 3" }
// ]支持的 YAML 功能
Bun 的 YAML 解析器支持完整的 YAML 1.2 規范,包括:
- 標量:字符串、數字、布爾值、null 值
- 集合:序列(數組)和映射(對象)
- 錨點和別名:使用
&和*的可重用節點 - 標簽:類型提示,如
!!str、!!int、!!float、!!bool、!!null - 多行字符串:字面量(
|)和折疊(>)標量 - 注釋:使用
# - 指令:
%YAML和%TAG
ts
const yaml = `
# Employee record
employee: &emp
name: Jane Smith
department: Engineering
skills:
- JavaScript
- TypeScript
- React
manager: *emp # Reference to employee
config: !!str 123 # Explicit string type
description: |
This is a multi-line
literal string that preserves
line breaks and spacing.
summary: >
This is a folded string
that joins lines with spaces
unless there are blank lines.
`;
const data = Bun.YAML.parse(yaml);錯誤處理
如果 YAML 無效,Bun.YAML.parse() 拋出 SyntaxError:
ts
try {
Bun.YAML.parse("invalid: yaml: content:");
} catch (error) {
console.error("Failed to parse YAML:", error.message);
}模塊導入
ES 模塊
你可以直接將 YAML 文件作為 ES 模塊導入。YAML 內容被解析並作為默認導出和命名導出提供:
yaml
database:
host: localhost
port: 5432
name: myapp
redis:
host: localhost
port: 6379
features:
auth: true
rateLimit: true
analytics: false默認導入
ts
import config from "./config.yaml";
console.log(config.database.host); // "localhost"
console.log(config.redis.port); // 6379命名導入
你可以將頂級 YAML 屬性解構為命名導入:
ts
import { database, redis, features } from "./config.yaml";
console.log(database.host); // "localhost"
console.log(redis.port); // 6379
console.log(features.auth); // true或組合兩者:
ts
import config, { database, features } from "./config.yaml";
// 使用完整的 config 對象
console.log(config);
// 或使用特定部分
if (features.rateLimit) {
setupRateLimiting(database);
}CommonJS
YAML 文件也可以在 CommonJS 中 require:
ts
const config = require("./config.yaml");
console.log(config.database.name); // "myapp"
// 解構也有效
const { database, redis } = require("./config.yaml");
console.log(database.port); // 5432YAML 熱重新加載
Bun 的 YAML 支持最強大的功能之一是熱重新加載。當你使用 bun --hot 運行應用程序時,對 YAML 文件的更改會自動檢測並重新加載,而無需關閉連接
配置熱重新加載
yaml
server:
port: 3000
host: localhost
features:
debug: true
verbose: falsets
import { server, features } from "./config.yaml";
console.log(`Starting server on ${server.host}:${server.port}`);
if (features.debug) {
console.log("Debug mode enabled");
}
// 你的服務器代碼在這裡
Bun.serve({
port: server.port,
hostname: server.host,
fetch(req) {
if (features.verbose) {
console.log(`${req.method} ${req.url}`);
}
return new Response("Hello World");
},
});使用熱重新加載運行:
bash
bun --hot server.ts現在當你修改 config.yaml 時,更改會立即反映在你運行的應用程序中。這非常適合:
- 在開發期間調整配置
- 無需重啟測試不同設置
- 使用配置更改實時調試
- 功能標志切換
配置管理
基於環境的配置
YAML 擅長管理不同環境的配置:
yaml
defaults: &defaults
timeout: 5000
retries: 3
cache:
enabled: true
ttl: 3600
development:
<<: *defaults
api:
url: http://localhost:4000
key: dev_key_12345
logging:
level: debug
pretty: true
staging:
<<: *defaults
api:
url: https://staging-api.example.com
key: ${STAGING_API_KEY}
logging:
level: info
pretty: false
production:
<<: *defaults
api:
url: https://api.example.com
key: ${PROD_API_KEY}
cache:
enabled: true
ttl: 86400
logging:
level: error
pretty: falsets
import configs from "./config.yaml";
const env = process.env.NODE_ENV || "development";
const config = configs[env];
// YAML 值中的環境變量可以插值
function interpolateEnvVars(obj: any): any {
if (typeof obj === "string") {
return obj.replace(/\${(\w+)}/g, (_, key) => process.env[key] || "");
}
if (typeof obj === "object") {
for (const key in obj) {
obj[key] = interpolateEnvVars(obj[key]);
}
}
return obj;
}
export default interpolateEnvVars(config);功能標志配置
yaml
features:
newDashboard:
enabled: true
rolloutPercentage: 50
allowedUsers:
- admin@example.com
- beta@example.com
experimentalAPI:
enabled: false
endpoints:
- /api/v2/experimental
- /api/v2/beta
darkMode:
enabled: true
default: auto # auto, light, darkts
import { features } from "./features.yaml";
export function isFeatureEnabled(featureName: string, userEmail?: string): boolean {
const feature = features[featureName];
if (!feature?.enabled) {
return false;
}
// 檢查 rollout 百分比
if (feature.rolloutPercentage < 100) {
const hash = hashCode(userEmail || "anonymous");
if (hash % 100 >= feature.rolloutPercentage) {
return false;
}
}
// 檢查允許的用戶
if (feature.allowedUsers && userEmail) {
return feature.allowedUsers.includes(userEmail);
}
return true;
}
// 與熱重新加載一起使用以實時切換功能
if (isFeatureEnabled("newDashboard", user.email)) {
renderNewDashboard();
} else {
renderLegacyDashboard();
}數據庫配置
yaml
connections:
primary:
type: postgres
host: ${DB_HOST:-localhost}
port: ${DB_PORT:-5432}
database: ${DB_NAME:-myapp}
username: ${DB_USER:-postgres}
password: ${DB_PASS}
pool:
min: 2
max: 10
idleTimeout: 30000
cache:
type: redis
host: ${REDIS_HOST:-localhost}
port: ${REDIS_PORT:-6379}
password: ${REDIS_PASS}
db: 0
analytics:
type: clickhouse
host: ${ANALYTICS_HOST:-localhost}
port: 8123
database: analytics
migrations:
autoRun: ${AUTO_MIGRATE:-false}
directory: ./migrations
seeds:
enabled: ${SEED_DB:-false}
directory: ./seedsts
import { connections, migrations } from "./database.yaml";
import { createConnection } from "./database-driver";
// 解析帶有默認值的環境變量
function parseConfig(config: any) {
return JSON.parse(
JSON.stringify(config).replace(
/\${([^:-]+)(?::([^}]+))?}/g,
(_, key, defaultValue) => process.env[key] || defaultValue || "",
),
);
}
const dbConfig = parseConfig(connections);
export const db = await createConnection(dbConfig.primary);
export const cache = await createConnection(dbConfig.cache);
export const analytics = await createConnection(dbConfig.analytics);
// 如果配置為自動運行遷移
if (parseConfig(migrations).autoRun === "true") {
await runMigrations(db, migrations.directory);
}打包器集成
當你在應用程序中導入 YAML 文件並使用 Bun 打包時,YAML 在構建時解析並作為 JavaScript 模塊包含:
bash
bun build app.ts --outdir=dist這意味著:
- 生產中零運行時 YAML 解析開銷
- 更小的打包大小
- 支持未使用配置的 tree-shaking(命名導入)
動態導入
YAML 文件可以動態導入,用於按需加載配置:
ts
const env = process.env.NODE_ENV || "development";
const config = await import(`./configs/${env}.yaml`);
// 加載用戶特定設置
async function loadUserSettings(userId: string) {
try {
const settings = await import(`./users/${userId}/settings.yaml`);
return settings.default;
} catch {
return await import("./users/default-settings.yaml");
}
}