diff --git a/packages/waku/src/lib/hono/runner.ts b/packages/waku/src/lib/hono/runner.ts index f0bbc4d72..c17836b6a 100644 --- a/packages/waku/src/lib/hono/runner.ts +++ b/packages/waku/src/lib/hono/runner.ts @@ -1,9 +1,10 @@ -import type { MiddlewareHandler } from 'hono'; +import type { Context, MiddlewareHandler } from 'hono'; +import { unstable_getCustomContext } from '../../server.js'; import { resolveConfig } from '../config.js'; import type { HandlerContext, MiddlewareOptions } from '../middleware/types.js'; -// Experimental Unstable API +// Internal context key const HONO_CONTEXT = '__hono_context'; const createEmptyReadableStream = () => @@ -13,6 +14,7 @@ const createEmptyReadableStream = () => }, }); +// Middleware runner (Is there a better name?) export const runner = (options: MiddlewareOptions): MiddlewareHandler => { const entriesPromise = options.cmd === 'start' @@ -68,3 +70,11 @@ export const runner = (options: MiddlewareOptions): MiddlewareHandler => { await next(); }; }; + +export const getHonoContext = () => { + const c = unstable_getCustomContext()[HONO_CONTEXT]; + if (!c) { + throw new Error('Hono context is not available'); + } + return c as C; +}; diff --git a/packages/waku/src/unstable_hono.ts b/packages/waku/src/unstable_hono.ts index c1e3292da..72c962efe 100644 --- a/packages/waku/src/unstable_hono.ts +++ b/packages/waku/src/unstable_hono.ts @@ -1,6 +1,6 @@ // These exports are for internal use only and subject to change without notice. -export { runner } from './lib/hono/runner.js'; +export { runner, getHonoContext } from './lib/hono/runner.js'; export const importHono = () => import('hono'); export const importHonoNodeServer: any = () => import('@hono/node-server');