Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than f0b094d point at it.

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 1–11why, one step back
1import { Context } from '../../context'
2import type { Hono } from '../../hono'3import { HTTPException } from '../../http-exception'4import type { BlankSchema, Env, Input, MiddlewareHandler, Schema } from '../../types'56// Ref: https://github.com/cloudflare/workerd/blob/main/types/defines/pages.d.ts78// eslint-disable-next-line @typescript-eslint/no-explicit-any9type Params<P extends string = any> = Record<P, string | string[]>1011// eslint-disable-next-line @typescript-eslint/no-explicit-any

feat(adapters): add the seven adapters as workspace packages (#5463)Yusuke Wadabefore adoptionb877827

lines 12–14why, one step back
12/**
13 * @deprecated `hono/cloudflare-pages` will be removed in v5. Cloudflare recommends Workers with static assets; use `hono` on Workers instead.14 */

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 15–34why, one step back
15export type EventContext<Env = {}, P extends string = any, Data = Record<string, unknown>> = {
16 request: Request17 functionPath: string18 waitUntil: (promise: Promise<unknown>) => void19 passThroughOnException: () => void20 // eslint-disable-next-line @typescript-eslint/no-explicit-any21 props: any22 next: (input?: Request | string, init?: RequestInit) => Promise<Response>23 env: Env & { ASSETS: { fetch: typeof fetch } }24 params: Params<P>25 data: Data26}2728declare type PagesFunction<29 Env = unknown,30 // eslint-disable-next-line @typescript-eslint/no-explicit-any31 Params extends string = any,32 Data extends Record<string, unknown> = Record<string, unknown>,33> = (context: EventContext<Env, Params, Data>) => Response | Promise<Response>34

feat(adapters): add the seven adapters as workspace packages (#5463)Yusuke Wadabefore adoptionb877827

lines 35–37why, one step back
35/**
36 * @deprecated `hono/cloudflare-pages` will be removed in v5. Cloudflare recommends Workers with static assets; use `hono` on Workers instead.37 */

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 38–54why, one step back
38export const handle =
39 <E extends Env = Env, S extends Schema = BlankSchema, BasePath extends string = '/'>(40 app: Hono<E, S, BasePath>41 ): PagesFunction<E['Bindings']> =>42 (eventContext) => {43 return app.fetch(44 eventContext.request,45 { ...eventContext.env, eventContext },46 {47 waitUntil: eventContext.waitUntil,48 passThroughOnException: eventContext.passThroughOnException,49 props: {},50 }51 )52 }5354// eslint-disable-next-line @typescript-eslint/no-explicit-any

feat(adapters): add the seven adapters as workspace packages (#5463)Yusuke Wadabefore adoptionb877827

lines 55–57why, one step back
55/**
56 * @deprecated `hono/cloudflare-pages` will be removed in v5. Cloudflare recommends Workers with static assets; use `hono` on Workers instead.57 */

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 58–121why, one step back
58export function handleMiddleware<E extends Env = {}, P extends string = any, I extends Input = {}>(
59 middleware: MiddlewareHandler<60 E & {61 Bindings: {62 eventContext: EventContext63 }64 },65 P,66 I67 >68): PagesFunction<E['Bindings']> {69 return async (executionCtx) => {70 const context = new Context(executionCtx.request, {71 env: { ...executionCtx.env, eventContext: executionCtx },72 executionCtx,73 })7475 let response: Response | void = undefined7677 try {78 response = await middleware(context, async () => {79 try {80 context.res = await executionCtx.next()81 } catch (error) {82 if (error instanceof Error) {83 context.error = error84 } else {85 throw error86 }87 }88 })89 } catch (error) {90 if (error instanceof Error) {91 context.error = error92 } else {93 throw error94 }95 }9697 if (response) {98 return response99 }100101 if (context.error instanceof HTTPException) {102 return context.error.getResponse()103 }104105 if (context.error) {106 throw context.error107 }108109 return context.res110 }111}112113declare abstract class FetcherLike {114 fetch(input: RequestInfo, init?: RequestInit): Promise<Response>115}116117/**118 *119 * @description `serveStatic()` is for advanced mode:120 * https://developers.cloudflare.com/pages/platform/functions/advanced-mode/#set-up-a-function121 *

feat(adapters): add the seven adapters as workspace packages (#5463)Yusuke Wadabefore adoptionb877827

line 122why, one step back
122 * @deprecated `hono/cloudflare-pages` will be removed in v5. Cloudflare recommends Workers with static assets; use `hono` on Workers instead.

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 123–133why, one step back
123 */
124export const serveStatic = (): MiddlewareHandler => {125 return async (c) => {126 const env = c.env as { ASSETS: FetcherLike }127 const res = await env.ASSETS.fetch(c.req.raw)128 if (res.status === 404) {129 return c.notFound()130 }131 return res132 }133}