主题
挂载
¥Mount
WinterCG 是一个 Web 互操作运行时的标准。Cloudflare、Deno、Vercel Edge Runtime、Netlify Function 以及其他各种工具均支持此功能,它允许 Web 服务器在使用 Web 标准定义(如 Fetch
、Request
和 Response
)的运行时之间互操作运行。
¥WinterCG is a standard for web-interoperable runtimes. Supported by Cloudflare, Deno, Vercel Edge Runtime, Netlify Function, and various others, it allows web servers to run interoperably across runtimes that use Web Standard definitions like Fetch
, Request
, and Response
.
Elysia 符合 WinterCG 标准。我们已针对 Bun 进行了优化,但如果可能的话,也会公开支持其他运行时。
¥Elysia is WinterCG compliant. We are optimized to run on Bun but also openly support other runtimes if possible.
理论上,这允许任何符合 WinterCG 标准的框架或代码一起运行,从而允许 Elysia、Hono、Remix、Itty Router 等框架在一个简单的函数中一起运行。
¥In theory, this allows any framework or code that is WinterCG compliant to be run together, allowing frameworks like Elysia, Hono, Remix, Itty Router to run together in a simple function.
秉承这一理念,我们为 Elysia 实现了相同的逻辑,引入了 .mount
方法,使其能够与任何兼容 WinterCG 的框架或代码一起运行。
¥Adhering to this, we implemented the same logic for Elysia by introducing .mount
method to run with any framework or code that is WinterCG compliant.
挂载
¥Mount
使用 .mount,只需传递 fetch
函数:
¥To use .mount, simply pass a fetch
function:
ts
import { Elysia } from 'elysia'
const app = new Elysia()
.get('/', () => 'Hello from Elysia')
.mount('/hono', hono.fetch)
Fetch 函数是一个接受 Web 标准请求并返回 Web 标准响应的函数,其定义如下:
¥A fetch function is a function that accepts a Web Standard Request and returns a Web Standard Response with the definition of:
ts
// Web Standard Request-like object
// Web Standard Response
type fetch = (request: RequestLike) => Response
默认情况下,此声明用于:
¥By default, this declaration is used by:
Bun
Deno
Vercel Edge 运行时
Cloudflare Worker
Netlify Edge 函数
混合函数处理程序
等等
这允许你在单个服务器环境中执行所有上述代码,从而可以与 Elysia 无缝交互。你还可以在单个部署中重用现有函数,从而无需使用反向代理来管理多个服务器。
¥This allows you to execute all the aforementioned code in a single server environment, making it possible to interact seamlessly with Elysia. You can also reuse existing functions within a single deployment, eliminating the need for a reverse proxy to manage multiple servers.
如果框架还支持 .mount 函数,则可以无限嵌套支持该函数的框架。
¥If the framework also supports a .mount function, you can deeply nest a framework that supports it.
ts
import { Elysia } from 'elysia'
import { Hono } from 'hono'
const elysia = new Elysia()
.get('/', () => 'Hello from Elysia inside Hono inside Elysia')
const hono = new Hono()
.get('/', (c) => c.text('Hello from Hono!'))
.mount('/elysia', elysia.fetch)
const main = new Elysia()
.get('/', () => 'Hello from Elysia')
.mount('/hono', hono.fetch)
.listen(3000)
复用 Elysia
¥Reusing Elysia
此外,你可以在服务器上复用多个现有的 Elysia 项目。
¥Moreover, you can re-use multiple existing Elysia projects on your server.
ts
import { Elysia } from 'elysia'
import A from 'project-a/elysia'
import B from 'project-b/elysia'
import C from 'project-c/elysia'
new Elysia()
.mount(A)
.mount(B)
.mount(C)
如果传递给 mount
的实例是 Elysia 实例,它将自动解析为 use
,默认提供类型安全并支持 Eden。
¥If an instance passed to mount
is an Elysia instance, it will be resolved with use
automatically, providing type-safety and support for Eden by default.
这使得可互操作的框架和运行时成为现实。
¥This makes the possibility of an interoperable framework and runtime a reality.