Appearance
适配器助手
¥Adapter Helper
Adapter Helper 通过统一界面提供了一种与各种平台无缝交互的方式。
¥The Adapter Helper provides a seamless way to interact with various platforms through a unified interface.
导入
¥Import
ts
import { Hono } from 'hono'
import { env, getRuntimeKey } from 'hono/adapter'
env()
env()
函数有助于在不同的运行时检索环境变量,而不仅仅是 Cloudflare Workers 的绑定。使用 env(c)
可以检索的值可能因每个运行时而异。
¥The env()
function facilitates retrieving environment variables across different runtimes, extending beyond just Cloudflare Workers' Bindings. The value that can be retrieved with env(c)
may be different for each runtimes.
ts
import { env } from 'hono/adapter'
app.get('/env', (c) => {
// NAME is process.env.NAME on Node.js or Bun
// NAME is the value written in `wrangler.toml` on Cloudflare
const { NAME } = env<{ NAME: string }>(c)
return c.text(NAME)
})
支持的运行时、无服务器平台和云服务:
¥Supported Runtimes, Serverless Platforms and Cloud Services:
Cloudflare Workers
wrangler.toml
wrangler.jsonc
Deno
.env
文件¥
.env
file
Bun
process.env
Node.js
process.env
Vercel
AWS Lambda
Lambda 上的 Lambda@Edge 环境变量是 Lambda@Edge 的 不支持,你需要使用 Lamdba@Edge 事件 作为替代方案。
¥Lambda@Edge\ Environment Variables on Lambda are not supported by Lambda@Edge, you need to use Lamdba@Edge event as an alternative.
快速计算在 Fastly Compute 上,你可以使用 ConfigStore 来管理用户定义的数据。
¥Fastly Compute\ On Fastly Compute, you can use the ConfigStore to manage user-defined data.
Netlify 在 Netlify 上,你可以使用 Netlify 上下文 来管理用户定义的数据。
¥Netlify\ On Netlify, you can use the Netlify Contexts to manage user-defined data.
指定运行时
¥Specify the runtime
你可以通过将运行时键作为第二个参数传递来指定运行时以获取环境变量。
¥You can specify the runtime to get environment variables by passing the runtime key as the second argument.
ts
app.get('/env', (c) => {
const { NAME } = env<{ NAME: string }>(c, 'workerd')
return c.text(NAME)
})
getRuntimeKey()
getRuntimeKey()
函数返回当前运行时的标识符。
¥The getRuntimeKey()
function returns the identifier of the current runtime.
ts
app.get('/', (c) => {
if (getRuntimeKey() === 'workerd') {
return c.text('You are on Cloudflare')
} else if (getRuntimeKey() === 'bun') {
return c.text('You are on Bun')
}
...
})
可用运行时密钥
¥Available Runtimes Keys
以下是可用的运行时键,不可用的运行时键运行时可能受支持并标记为 other
,其中一些受 WinterCG 的运行时密钥 启发:
¥Here are the available runtimes keys, unavailable runtime key runtimes may be supported and labeled as other
, with some being inspired by WinterCG's Runtime Keys:
workerd
- Cloudflare Workersdeno
bun
node
edge-light
- Vercel Edge 函数¥
edge-light
- Vercel Edge Functionsfastly
- Fastly 计算¥
fastly
- Fastly Computeother
- 其他未知运行时键¥
other
- Other unknown runtimes keys