@lunora/cli
Version:
The Lunora CLI: init, dev, deploy, codegen, migrate, seed, doctor, insights, logs, registry, and the rest of the project commands
2 lines (1 loc) • 2.85 kB
JavaScript
import{LunoraError as v}from"@lunora/errors";import{r as A,d as _}from"../packem_shared/admin-token-rxXZbVMZ.mjs";import{r as N,a as S}from"../packem_shared/admin-url-Cil3Or6N.mjs";import{d as b}from"../packem_shared/command-DwC11Cwe.mjs";import{r as P}from"../packem_shared/resolve-target-C_ZloTvd.mjs";const h=(r,t="--args")=>{if(r===void 0||r.length===0)return{};try{return JSON.parse(r)}catch(e){const a=e instanceof Error?e.message:String(e);throw new v("INTERNAL",`failed to parse ${t} as JSON: ${a}`,{cause:e})}},I=/\/$/u,O="__lunora_admin__:runAs",k="__lunora_admin__:",T=async(r,t)=>{const e=await r.text();let a;try{a=JSON.parse(e)}catch{a=e}return t.info(JSON.stringify(a,void 0,2)),a},$=(r,t)=>{const e=t.body?.error?.code;t.runAs||t.status!==403||e!=="FORBIDDEN_SHARD"||r.info("this app authorizes per shard, so an anonymous call is denied — retry with --as <userId> to run as an authenticated user")},E=({args:r,claims:t,options:e,runAs:a})=>{const n=a?{args:{args:r,functionPath:e.functionPath,identity:t,userId:e.as},functionPath:O}:{args:r,functionPath:e.functionPath};return e.shard!==void 0&&(n.shardKey=e.shard),n},R=({cwd:r,logger:t,needsBearer:e,url:a})=>e?N(a,t,r):(P({cwd:r,url:a})??S(r)).replace(I,""),U=async r=>{const t=r.cwd??process.cwd(),e=r.as!==void 0&&r.as!=="",a=e||r.functionPath.startsWith(k),n=R({cwd:t,logger:r.logger,needsBearer:a,url:r.url});if(n===void 0)return{body:void 0,code:1,requestUrl:r.url??""};const s=`${n}/_lunora/rpc`,i=r.fetchImpl??globalThis.fetch;if(typeof i!="function")throw new TypeError("no fetch implementation available — pass --fetch via dependency injection or run on Node >= 18");let u,l;try{u=h(r.args),l=r.claims===void 0?void 0:h(r.claims,"--claims")}catch(d){return r.logger.error(d instanceof Error?d.message:String(d)),{body:void 0,code:1,requestUrl:s}}const{source:g,token:o}=a?A({cwd:t,token:r.token,url:n}):{};if(a&&o===void 0)return r.logger.error("admin token required — pass --token, set LUNORA_ADMIN_TOKEN, or add it to .dev.vars (local targets only)"),{body:void 0,code:1,requestUrl:s};o!==void 0&&r.logger.debug?.(`admin bearer from ${_(g)}`);const m={"content-type":"application/json",...o===void 0?{}:{authorization:`Bearer ${o}`}},y=E({args:u,claims:l,options:r,runAs:e});r.logger.info(`POST ${s} -> ${r.functionPath}${e?` (as ${r.as??""})`:""}`);const c=await i(s,{body:JSON.stringify(y),headers:m,method:"POST"}),f=await T(c,r.logger);return $(r.logger,{body:f,runAs:e,status:c.status}),{body:f,code:c.ok?0:1,requestUrl:s}},L=b(({argument:r,cwd:t,logger:e,options:a})=>{const n=r[0];return n?U({args:a.args,as:a.as,claims:a.claims,cwd:t,functionPath:n,logger:e,shard:a.shard,token:a.token,url:a.url}):(e.error("missing function path. Usage: lunora run <functionPath> [--args <json>]"),{code:1})});export{L as execute,T as readAndLogBody,U as runRpcCommand};