UNPKG

eve

Version:

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

4 lines 2.19 kB
import{existsSync,readFileSync,statSync}from"node:fs";import{dirname,extname,join,relative,resolve}from"node:path";import{fileURLToPath}from"node:url";function createAuthoredModuleEvaluationError(e,t){let n=describeInstalledPackageLoadFailure(t);return Error([`Failed to evaluate authored module:`,` ${e}`,...n===void 0?[]:[``,n]].join(` `),{cause:t})}function describeInstalledPackageLoadFailure(e){if(!(e instanceof Error))return;let t=e;if(t.code!==`ERR_MODULE_NOT_FOUND`||typeof t.url!=`string`)return;let n=filePathFromUrl(t.url);if(n===void 0||extname(n)!==``)return;let r=findInstalledPackage(n);if(r===void 0)return;let a=toPackageRelativePath(r.root,n);return findExistingModulePath(n)===void 0?[`Failed to load an installed package:`,` Package: ${r.name} (loaded outside the authored bundle)`,` Missing: ${a}`,` Reason: Package output may be incomplete, incorrectly installed, or incompatible with a standalone Node runtime.`,` Hint: Verify the installed package version and output, use a Node-compatible entrypoint, or report the missing module to the package publisher.`].join(` `):[`Failed to load an installed package:`,` Package: ${r.name} (loaded outside the authored bundle)`,` Import: ${a}`,` Reason: Node's ESM loader does not infer file extensions for relative imports.`,` Hint: Use a Node-compatible package or entrypoint, or report the extensionless import to the package publisher.`].join(` `)}function findExistingModulePath(e){for(let t of[`.js`,`.mjs`,`.cjs`]){let r=`${e}${t}`;try{if(statSync(r).isFile())return r}catch{}}}function toPackageRelativePath(e,t){return relative(e,t).replaceAll(`\\`,`/`)}function filePathFromUrl(e){try{let t=new URL(e);return t.protocol===`file:`?fileURLToPath(t):void 0}catch{return}}function findInstalledPackage(n){let i=dirname(resolve(n));for(;;){let n=join(i,`package.json`);if(existsSync(n)&&isNodeModulesPath(n))try{let e=JSON.parse(readFileSync(n,`utf8`));if(typeof e.name==`string`&&e.name.length>0)return{name:e.name,root:i}}catch{return}let o=dirname(i);if(o===i)return;i=o}}function isNodeModulesPath(e){return e.replaceAll(`\\`,`/`).includes(`/node_modules/`)}export{createAuthoredModuleEvaluationError};