handler.ts
This file
Path
src/adapter/cloudflare-pages/handler.ts
133 lines 路 4.0 KiB 路 TypeScript
Last changed by
feat(adapters): add the seven adapters as workspace packages (#5463)Yusuke Wadabefore adoptionb877827
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134import { Context } from '../../context'import type { Hono } from '../../hono'import { HTTPException } from '../../http-exception'import type { BlankSchema, Env, Input, MiddlewareHandler, Schema } from '../../types'
// Ref: https://github.com/cloudflare/workerd/blob/main/types/defines/pages.d.ts
// eslint-disable-next-line @typescript-eslint/no-explicit-anytype Params<P extends string = any> = Record<P, string | string[]>
// eslint-disable-next-line @typescript-eslint/no-explicit-any/** * @deprecated `hono/cloudflare-pages` will be removed in v5. Cloudflare recommends Workers with static assets; use `hono` on Workers instead. */export type EventContext<Env = {}, P extends string = any, Data = Record<string, unknown>> = { request: Request functionPath: string waitUntil: (promise: Promise<unknown>) => void passThroughOnException: () => void // eslint-disable-next-line @typescript-eslint/no-explicit-any props: any next: (input?: Request | string, init?: RequestInit) => Promise<Response> env: Env & { ASSETS: { fetch: typeof fetch } } params: Params<P> data: Data}
declare type PagesFunction< Env = unknown, // eslint-disable-next-line @typescript-eslint/no-explicit-any Params extends string = any, Data extends Record<string, unknown> = Record<string, unknown>,> = (context: EventContext<Env, Params, Data>) => Response | Promise<Response>
/** * @deprecated `hono/cloudflare-pages` will be removed in v5. Cloudflare recommends Workers with static assets; use `hono` on Workers instead. */export const handle = <E extends Env = Env, S extends Schema = BlankSchema, BasePath extends string = '/'>( app: Hono<E, S, BasePath> ): PagesFunction<E['Bindings']> => (eventContext) => { return app.fetch( eventContext.request, { ...eventContext.env, eventContext }, { waitUntil: eventContext.waitUntil, passThroughOnException: eventContext.passThroughOnException, props: {}, } ) }
// eslint-disable-next-line @typescript-eslint/no-explicit-any/** * @deprecated `hono/cloudflare-pages` will be removed in v5. Cloudflare recommends Workers with static assets; use `hono` on Workers instead. */export function handleMiddleware<E extends Env = {}, P extends string = any, I extends Input = {}>( middleware: MiddlewareHandler< E & { Bindings: { eventContext: EventContext } }, P, I >): PagesFunction<E['Bindings']> { return async (executionCtx) => { const context = new Context(executionCtx.request, { env: { ...executionCtx.env, eventContext: executionCtx }, executionCtx, })
let response: Response | void = undefined
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 }}
declare abstract class FetcherLike { fetch(input: RequestInfo, init?: RequestInit): Promise<Response>}
/** * * @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. */export const serveStatic = (): MiddlewareHandler => { return async (c) => { const env = c.env as { ASSETS: FetcherLike } const res = await env.ASSETS.fetch(c.req.raw) if (res.status === 404) { return c.notFound() } return res }}