[ SKILL_DOCUMENTATION ]
# 配置
## 脚本加载
js
// 内联
new Miniflare({ modules: true, script: `export default { ... }` });
// 基于文件
new Miniflare({ scriptPath: "worker.js" });
// 多模块
new Miniflare({
scriptPath: "src/index.js",
modules: true,
modulesRules: [
{ type: "ESModule", include: ["**/*.js"] },
{ type: "Text", include: ["**/*.txt"] },
],
});
## 兼容性
js
new Miniflare({
compatibilityDate: "2026-01-01", // 使用最近日期以获取最新特性
compatibilityFlags: [
"nodejs_compat", // Node.js API (process, Buffer 等)
"streams_enable_constructors", // 流构造函数
],
upstream: "https://example.com", // 未处理请求的回退地址
});
**关键:** 使用 `compatibilityDate: "2026-01-01"` 或最新日期以匹配生产环境运行时。旧日期会限制可用 API。
## HTTP 服务器 & Request.cf
js
new Miniflare({
port: 8787, // 默认: 8787
host: "127.0.0.1",
https: true, // 自签名证书
liveReload: true, // HTML 自动重载
cf: true, // 获取实时 Request.cf 数据 (缓存)
// cf: "./cf.json", // 或从文件加载
// cf: { colo: "DFW" }, // 或内联模拟
});
**注意:** 对于测试,请使用 `dispatchFetch()` (无端口冲突)。
## 存储绑定
js
new Miniflare({
// KV
kvNamespaces: ["TEST_NAMESPACE", "CACHE"],
kvPersist: "./kv-data", // 可选:持久化到磁盘
// R2
r2Buckets: ["BUCKET", "IMAGES"],
r2Persist: "./r2-data",
// Durable Objects
modules: true,
durableObjects: {
COUNTER: "Counter", // 类名
API_OBJECT: { className: "ApiObject", scriptName: "api-worker" },
},
durableObjectsPersist: "./do-data",
// D1
d1Databases: ["DB"],
d1Persist: "./d1-data",
// 缓存
cache: true, // 默认
cachePersist: "./cache-data",
});
## 绑定
js
new Miniflare({
// 环境变量
bindings: {
SECRET_KEY: "my-secret-value",
API_URL: "https://api.example.com",
DEBUG: true,
},
// 其他绑定
wasmBindings: { ADD_MODULE: "./add.wasm" },
textBlobBindings: { TEXT: "./data.txt" },
queueProducers: ["QUEUE"],
});
## 多个 Workers
js
new Miniflare({
workers: [
{
name: "main",
kvNamespaces: { DATA: "shared" },
serviceBindings: { API: "api-worker" },
script: `export default { ... }`,
},
{
name: "api-worker",
kvNamespaces: { DATA: "shared" }, // 共享存储