import*as f from"workerpool";import{Hono as d}from"hono";import{importApiRequestHandler as l}from"../../web-server/handle-api-request.js";import{ResponseSizeLimitError as m}from"./errors.js";import{readStreamWithSizeLimit as S}from"./utils/read-stream-with-size-limit.js";import{enhanceContext as g}from"./utils/enhance-context.js";import{Logger as w}from"../../utils/reporter/logger.js";async function y(t,o=5){return t.body?await S({stream:t.body,maxSizeMB:o,onSizeExceededErrorHandler:()=>{throw new m(`Response size exceeded limit of ${o}MB`)}}):await t.arrayBuffer()}const n=new w;n.patchConsole();async function h(t){n.setStickyState(t.loggerStickyState);const o=new d,{requestHandlers:a}=await l(t.serverOutDir),s=a[t.requestHandlerId],u=(await s()).default;o.all(t.slug,async e=>{const r=await u(e.req.raw,g(e,t.ctxData),t.staticData);return r instanceof Response?r:typeof r=="string"?e.text(r):e.json(r)}),o.onError((e,r)=>{if(n.error(`[${t.requestHandlerId}] ${e.message} ${e.stack}`),e instanceof SyntaxError)return r.text(e.message,500);throw e});const i=await o.request(t.req.url,t.req),c=await y(i,t.maxResponseSizeMB);return{status:i.status,headers:Object.fromEntries(i.headers.entries()),body:c.toString(),loggerStickyState:n.getStickyState()}}f.worker({runHandler:h});
