Files
SnowLuma/.pnpm-store/v11/files/97/6f7c9484cf47f84ca8f0e641fdaa1006b7082eefb3c481d5d52e21c25fbec33a6a9b9e0a3dc1b887ef2226b8faf4fdcbf2983acaa1fe884cf2baf1cd09bd3f
T
TanTan 2a69b23b47
Dev Build / cleanup (push) Waiting to run
Dev Build / typecheck (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

65 lines
1.9 KiB
Plaintext

import { HTTPException } from "../../http-exception.js";
import { Context } from "../../context.js";
//#region src/adapter/cloudflare-pages/handler.ts
/**
* @deprecated `hono/cloudflare-pages` will be removed in v5. Cloudflare recommends Workers with static assets; use `hono` on Workers instead.
*/
const handle = (app) => (eventContext) => {
return app.fetch(eventContext.request, {
...eventContext.env,
eventContext
}, {
waitUntil: eventContext.waitUntil,
passThroughOnException: eventContext.passThroughOnException,
props: {}
});
};
/**
* @deprecated `hono/cloudflare-pages` will be removed in v5. Cloudflare recommends Workers with static assets; use `hono` on Workers instead.
*/
function handleMiddleware(middleware) {
return async (executionCtx) => {
const context = new Context(executionCtx.request, {
env: {
...executionCtx.env,
eventContext: executionCtx
},
executionCtx
});
let response = void 0;
try {
response = await middleware(context, async () => {
try {
context.res = await executionCtx.next();
} catch (error) {
if (error instanceof Error) context.error = error;
else throw error;
}
});
} catch (error) {
if (error instanceof Error) context.error = error;
else throw error;
}
if (response) return response;
if (context.error instanceof HTTPException) return context.error.getResponse();
if (context.error) throw context.error;
return context.res;
};
}
/**
*
* @description `serveStatic()` is for advanced mode:
* https://developers.cloudflare.com/pages/platform/functions/advanced-mode/#set-up-a-function
*
* @deprecated `hono/cloudflare-pages` will be removed in v5. Cloudflare recommends Workers with static assets; use `hono` on Workers instead.
*/
const serveStatic = () => {
return async (c) => {
const res = await c.env.ASSETS.fetch(c.req.raw);
if (res.status === 404) return c.notFound();
return res;
};
};
//#endregion
export { handle, handleMiddleware, serveStatic };