eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
4 lines • 2.19 kB
JavaScript
import{dirname,extname,join,relative,resolve}from"node:path";import{existsSync,readFileSync,statSync}from"node:fs";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 i=toPackageRelativePath(r.root,n);return findExistingModulePath(n)===void 0?[`Failed to load an installed package:`,` Package: ${r.name} (loaded outside the authored bundle)`,` Missing: ${i}`,` 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: ${i}`,` 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 n=`${e}${t}`;try{if(statSync(n).isFile())return n}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(e){let t=dirname(resolve(e));for(;;){let e=join(t,`package.json`);if(existsSync(e)&&isNodeModulesPath(e))try{let n=JSON.parse(readFileSync(e,`utf8`));if(typeof n.name==`string`&&n.name.length>0)return{name:n.name,root:t}}catch{return}let n=dirname(t);if(n===t)return;t=n}}function isNodeModulesPath(e){return e.replaceAll(`\\`,`/`).includes(`/node_modules/`)}export{createAuthoredModuleEvaluationError};