UNPKG

@universal-middleware/compress

Version:
84 lines (80 loc) 2.39 kB
import { bindUniversal, universalSymbol, getAdapterRuntime } from './chunk-MMHRYIMR.js'; import { middleware_default } from './chunk-SBN5FRVL.js'; // ../adapter-hono/dist/index.js var contextSymbol = Symbol.for("unContext"); function getExecutionCtx(honoContext) { try { return honoContext.executionCtx; } catch { return; } } 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 response; } setContext(honoContext, response); return next(); } else { return next(); } }); }; } 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); 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 = void 0; 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 } ); } // virtual:universal-middleware:virtual:universal-middleware:hono:middleware:src/middleware.ts var middleware_default2 = createMiddleware(middleware_default); export { middleware_default2 as default };