UNPKG

eve

Version:

Filesystem-first framework for durable backend AI agents that run anywhere.

1 lines 1.49 kB
import{access,cp,mkdir,readFile}from"node:fs/promises";import{dirname,join,posix,resolve,sep}from"node:path";function isRecord(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}const NEXT_MIDDLEWARE_PATH=`/_middleware`;function resolveMiddlewareFunctionDirectory(e,t){let n=posix.normalize(t.replace(/^\/+/,``));if(n.length===0||n===`.`||n===`..`||n.startsWith(`../`)||n.includes(`\\`)||n.includes(`\0`))throw Error(`Invalid Vercel middlewarePath: ${JSON.stringify(t)}.`);let r=resolve(e,`functions`),i=resolve(r,`${n}.func`);if(!i.startsWith(`${r}${sep}`))throw Error(`Vercel middlewarePath escapes the functions directory: ${t}.`);return i}async function copyHostMiddlewareFunctions(e){let t;try{t=await readFile(join(e.hostOutputDirectory,`config.json`),`utf8`)}catch(e){if(e instanceof Error&&`code`in e&&e.code===`ENOENT`)return;throw e}let n=JSON.parse(t);if(!isRecord(n))return;let r=new Set;if(Array.isArray(n.routes))for(let e of n.routes)isRecord(e)&&typeof e.middlewarePath==`string`&&r.add(e.middlewarePath);let i=resolveMiddlewareFunctionDirectory(e.hostOutputDirectory,NEXT_MIDDLEWARE_PATH);try{await access(i),r.add(NEXT_MIDDLEWARE_PATH)}catch(e){if(!(e instanceof Error&&`code`in e&&e.code===`ENOENT`))throw e}for(let t of r){let n=resolveMiddlewareFunctionDirectory(e.hostOutputDirectory,t),r=resolveMiddlewareFunctionDirectory(e.serviceOutputDirectory,t);await mkdir(dirname(r),{recursive:!0}),await cp(n,r,{force:!0,recursive:!0})}}export{copyHostMiddlewareFunctions};