UNPKG

eve

Version:

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

1 lines 3.01 kB
import{readFile}from"node:fs/promises";import{parseJsonObject}from"#shared/json.js";function optionalString(e,t){if(e!==void 0){if(typeof e!=`string`)throw Error(`${t} must be a string.`);return e}}function objectValue(e,t){try{return parseJsonObject(e)}catch{throw Error(`${t} must contain a JSON object.`)}}function parseDestination(e,t){if(e===void 0||typeof e==`string`)return e;let n;try{n=parseJsonObject(e)}catch{throw Error(`${t} must be a string or JSON object.`)}return{...n,service:optionalString(n.service,`${t}.service`),type:optionalString(n.type,`${t}.type`)}}function parseRoute(e,t){let n=objectValue(e,t);return{...n,destination:parseDestination(n.destination,`${t}.destination`),handle:optionalString(n.handle,`${t}.handle`),src:optionalString(n.src,`${t}.src`)}}function parseArray(e,t,n){if(e!==void 0){if(!Array.isArray(e))throw Error(`${t} must be an array.`);return e.map((e,r)=>n(e,`${t}[${r}]`))}}function parseMount(e,t){if(e===void 0||typeof e==`string`)return e;let n;try{n=parseJsonObject(e)}catch{throw Error(`${t} must be a string or JSON object.`)}return{...n,path:optionalString(n.path,`${t}.path`),subdomain:optionalString(n.subdomain,`${t}.subdomain`)}}function parseServiceConfig(e,t){let n=objectValue(e,t);return{...n,buildCommand:optionalString(n.buildCommand,`${t}.buildCommand`),entrypoint:optionalString(n.entrypoint,`${t}.entrypoint`),framework:optionalString(n.framework,`${t}.framework`),mount:parseMount(n.mount,`${t}.mount`),routes:parseArray(n.routes,`${t}.routes`,parseRoute),routePrefix:optionalString(n.routePrefix,`${t}.routePrefix`),root:optionalString(n.root,`${t}.root`),type:optionalString(n.type,`${t}.type`)}}function parseServices(e,t){if(e===void 0)return;if(Array.isArray(e))return e.map((e,n)=>{let r=`${t} services[${n}]`,i=objectValue(e,r),a=optionalString(i.name,`${r}.name`);if(a===void 0||a.trim().length===0)throw Error(`${r} must have a non-empty name.`);let{name:o,...s}=i;return{...parseServiceConfig(s,r),name:a}});let n;try{n=parseJsonObject(e)}catch{throw Error(`${t} services must be a JSON object or named service array.`)}return Object.fromEntries(Object.entries(n).map(([e,n])=>[e,parseServiceConfig(n,`${t} service ${JSON.stringify(e)}`)]))}function parseVercelServicesConfig(e,t){let n;try{n=parseJsonObject(e)}catch{throw Error(`${t} must contain a JSON object.`)}return{...n,routes:parseArray(n.routes,`${t} routes`,parseRoute),services:parseServices(n.services,t)}}function isNamedServiceArray(e){return Array.isArray(e)}function createServiceConfigRecord(e){return e===void 0?{}:isNamedServiceArray(e)?Object.fromEntries(e.map(({name:e,...t})=>[e,t])):e}function hasServices(e){return Object.keys(createServiceConfigRecord(e)).length>0}async function readVercelJsonFile(e){try{return parseVercelServicesConfig(JSON.parse(await readFile(e,`utf8`)),e)}catch(e){if(e instanceof Error&&`code`in e&&e.code===`ENOENT`)return{};throw e}}export{createServiceConfigRecord,hasServices,parseVercelServicesConfig,readVercelJsonFile};