handler.ts
This file
Path
adapters/service-worker/src/handler.ts
37 lines 路 941 bytes 路 TypeScript
Last changed by
feat(adapters): add the seven adapters as workspace packages (#5463)Yusuke Wadabefore adoptionb877827
History of this fileTake it
1234567891011121314151617181920212223242526272829303132333435363738/** * Handler for Service Worker * @module */
import type { Hono } from 'hono'import type { Env, Schema } from 'hono/types'import type { FetchEvent } from './types'
type Handler = (evt: FetchEvent) => voidexport type HandleOptions = { fetch?: typeof fetch}
/** * Adapter for Service Worker */export const handle = <E extends Env, S extends Schema, BasePath extends string>( app: Hono<E, S, BasePath>, opts: HandleOptions = { // To use `fetch` on a Service Worker correctly, bind it to `globalThis`. fetch: globalThis.fetch.bind(globalThis), }): Handler => { return (evt) => { evt.respondWith( (async () => { // @ts-expect-error Passing FetchEvent but app.fetch expects ExecutionContext const res = await app.fetch(evt.request, {}, evt) if (opts.fetch && res.status === 404) { return await opts.fetch(evt.request) } return res })() ) }}