eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 2.44 kB
JavaScript
import{createRequire}from"node:module";import{createHash}from"node:crypto";import{readFile,realpath}from"node:fs/promises";import{isObject}from"#shared/guards.js";import{delimiter,join}from"node:path";import{execFile}from"node:child_process";import{promisify}from"node:util";import{homedir}from"node:os";async function readObject(e){try{let t=await readFile(e,`utf8`);if(Buffer.byteLength(t)>65536)return;let n=JSON.parse(t);return isObject(n)?n:void 0}catch{return}}async function readCliConfiguration(){let e=homedir(),t=process.platform===`darwin`?join(e,`Library`,`Application Support`):process.platform===`win32`?process.env.APPDATA??join(e,`AppData`,`Roaming`):process.env.XDG_DATA_HOME??join(e,`.local`,`share`);for(let n of[join(t,`com.vercel.cli`),join(e,`.now`),join(t,`now`)]){let e=await readObject(join(n,`config.json`));if(e===void 0)continue;let t=typeof e.currentTeam==`string`?e.currentTeam:void 0;return t?{directory:n,config:e,teamId:t}:void 0}}async function readVercelCliTeam(){return(await readCliConfiguration())?.teamId}async function readVercelCliConnection(){let e=await readCliConfiguration();if(!e)return;let{directory:t,config:n,teamId:r}=e;if(process.env.VERCEL_TOKEN)return{token:process.env.VERCEL_TOKEN,teamId:r};let i=process.env.VERCEL_TOKEN_STORAGE??n.credStorage??`file`,a=join(t,`auth.json`),o;if(i===`file`&&(o=await readObject(a)),!o&&(i===`keyring`||i===`auto`)){let e=`cli:${createHash(`sha256`).update(a).digest(`hex`).slice(0,16)}`;try{let t=await readCliKeyring(e),n=t&&t.length<=65536?JSON.parse(t):void 0;isObject(n)&&(o=n)}catch{if(i===`keyring`)return}!o&&i===`auto`&&(o=await readObject(a))}if(typeof o?.token==`string`&&o.token)return{token:o.token,teamId:r}}async function refreshVercelCliConnection(){try{await promisify(execFile)(`vercel`,[`whoami`],{timeout:15e3,maxBuffer:65536,windowsHide:!0})}catch{throw Error(`Vercel CLI access expired. Run /login to reconnect.`)}}async function readCliKeyring(e){for(let t of(process.env.PATH??``).split(delimiter))try{let n=await realpath(join(t,process.platform===`win32`?`vercel.cmd`:`vercel`)),r=createRequire(n);process.platform===`win32`&&(r=createRequire(r.resolve(`vercel/package.json`)));try{r=createRequire(r.resolve(`@vercel/cli-auth/package.json`))}catch{}return new(r("@napi-rs/keyring")).Entry(`com.vercel.vercel-cli`,e).getPassword()??void 0}catch{}}export{readVercelCliConnection,readVercelCliTeam,refreshVercelCliConnection};