UNPKG

@universal-middleware/hono

Version:
140 lines (138 loc) 3.63 kB
// src/common.ts import { bindUniversal, contextSymbol, getAdapterRuntime, universalSymbol } from "@universal-middleware/core"; function getExecutionCtx(honoContext) { try { return honoContext.executionCtx; } catch { return; } } function createHandler(handlerFactory) { return (...args) => { const handler = handlerFactory(...args); return bindUniversal(handler, async function universalHandlerHono(honoContext, next) { const context = initContext(honoContext); const response = await this[universalSymbol]( honoContext.req.raw, context, getRuntime(honoContext) ); if (response) { return maybeCloneResponse(response); } await next(); }); }; } function createMiddleware(middlewareFactory) { return (...args) => { const middleware = middlewareFactory(...args); return bindUniversal(middleware, async function universalMiddlewareHono(honoContext, next) { const context = initContext(honoContext); const response = await this[universalSymbol](honoContext.req.raw, context, getRuntime(honoContext)); if (typeof response === "function") { await next(); const res = await response(honoContext.res); if (res) { honoContext.res = res; } } else if (response !== null && typeof response === "object") { if (response instanceof Response) { return maybeCloneResponse(response); } setContext(honoContext, response); return next(); } else { return next(); } }); }; } function maybeCloneResponse(response) { const ownSymbols = Object.getOwnPropertySymbols(response).map((l) => l.toString()); if (ownSymbols.includes("Symbol(cache)")) { try { return response.clone(); } catch { } } return response; } function initContext(honoContext) { let ctx = getContext(honoContext); if (ctx === void 0) { ctx = {}; setContext(honoContext, ctx); } return ctx; } function setContext(honoContext, value) { honoContext.set(contextSymbol, value); if (honoContext.env) { honoContext.env[contextSymbol] = value; } if (honoContext.env?.eventContext?.env) { honoContext.env.eventContext.env[contextSymbol] = value; } } function getContext(honoContext) { return honoContext.get(contextSymbol) ?? honoContext.env?.[contextSymbol] ?? honoContext.env?.eventContext?.env[contextSymbol]; } function getRuntime(honoContext) { let params; const ctx = getExecutionCtx(honoContext); try { params = honoContext.req.param(); } catch { if (ctx) { params = ctx.params ?? void 0; } } return getAdapterRuntime( "hono", { params, hono: honoContext }, { env: honoContext.env, ctx, req: honoContext.env?.incoming, res: honoContext.env?.outgoing }, honoContext.req.raw ); } // src/router.ts import { apply as applyCore, getUniversal, UniversalRouter, universalSymbol as universalSymbol2 } from "@universal-middleware/core"; var UniversalHonoRouter = class extends UniversalRouter { #app; constructor(app) { super(false); this.#app = app; } use(middleware) { this.#app.use(createMiddleware(() => getUniversal(middleware))()); return this; } applyCatchAll() { this.#app.all("/*", createHandler(() => this[universalSymbol2])()); return this; } }; function apply(app, middlewares) { const router = new UniversalHonoRouter(app); applyCore(router, middlewares); } export { apply, createHandler, createMiddleware, getContext, getRuntime };