Skip to content

适配器辅助函数

适配器辅助函数提供了一种通过统一接口与各种平台无缝交互的方式。

导入

ts
import { Hono } from 'hono'
import { env, getRuntimeKey } from 'hono/adapter'

env()

env() 函数有助于在不同运行时中检索环境变量,而不仅仅是 Cloudflare Workers 的绑定。对于每个运行时,env(c) 可以检索的值可能不同。

ts
import { env } from 'hono/adapter'

app.get('/env', (c) => {
  // 在 Node.js 或 Bun 上,NAME 是 process.env.NAME
  // 在 Cloudflare 上,NAME 是 `wrangler.toml` 中写入的值
  const { NAME } = env<{ NAME: string }>(c)
  return c.text(NAME)
})

支持的运行时、无服务器平台和云服务:

指定运行时

您可以通过将运行时密钥作为第二个参数传递来指定获取环境变量的运行时。

ts
app.get('/env', (c) => {
  const { NAME } = env<{ NAME: string }>(c, 'workerd')
  return c.text(NAME)
})

getRuntimeKey()

getRuntimeKey() 函数返回当前运行时的标识符。

ts
app.get('/', (c) => {
  if (getRuntimeKey() === 'workerd') {
    return c.text('您在 Cloudflare 上')
  } else if (getRuntimeKey() === 'bun') {
    return c.text('您在 Bun 上')
  }
  ...
})

可用的运行时密钥

以下是可用的运行时密钥,不可用的运行时密钥运行时可能受支持并标记为 other,其中一些密钥的灵感来自 WinterCG 的运行时密钥

  • workerd - Cloudflare Workers
  • deno
  • bun
  • node
  • edge-light - Vercel Edge Functions
  • fastly - Fastly Compute
  • other - 其他未知的运行时密钥

Released under the MIT License.