@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 y}from"@lunora/errors";import{l as v,v as w}from"../packem_shared/admin-token-BKmc3AUm.mjs";import{u as _,s as N}from"../packem_shared/admin-url-Ca-KI3d_.mjs";import{i as $}from"../packem_shared/command-0l-ZPhIX.mjs";import{i as b}from"../packem_shared/resolve-target-CeiNrCAx.mjs";const h=(r,e="--args")=>{if(r===void 0||r.length===0)return{};try{return JSON.parse(r)}catch(o){const t=o instanceof Error?o.message:String(o);throw new y("INTERNAL",`failed to parse ${e} as JSON: ${t}`,{cause:o})}},P=/\/$/u,S="__lunora_admin__:runAs",A="__lunora_admin__:",O=async(r,e)=>{const o=await r.text();let t;try{t=JSON.parse(o)}catch{t=o}return e.info(JSON.stringify(t,void 0,2)),t},k=(r,e)=>{const o=e.body?.error?.code;e.runAs||e.status!==403||o!=="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:e,options:o,runAs:t})=>{const a=t?{args:{args:r,functionPath:o.functionPath,identity:e,userId:o.as},functionPath:S}:{args:r,functionPath:o.functionPath};return o.shard!==void 0&&(a.shardKey=o.shard),a},I=({cwd:r,logger:e,needsBearer:o,url:t})=>o?_(t,e,r):(b({cwd:r,url:t})??N(r)).replace(P,""),T=async r=>{const e=r.cwd??process.cwd(),o=r.as!==void 0&&r.as!=="",t=o||r.functionPath.startsWith(A),a=I({cwd:e,logger:r.logger,needsBearer:t,url:r.url});if(a===void 0)return{body:void 0,code:1,requestUrl:r.url??""};const s=`${a}/_lunora/rpc`,d=r.fetchImpl??globalThis.fetch;if(typeof d!="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(c){return r.logger.error(c instanceof Error?c.message:String(c)),{body:void 0,code:1,requestUrl:s}}const{source:f,token:i}=t?v({cwd:e,token:r.token,url:a}):{};if(t&&i===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};i!==void 0&&r.logger.debug?.(`admin bearer from ${w(f)}`);const m={"content-type":"application/json",...i===void 0?{}:{authorization:`Bearer ${i}`}},p=E({args:u,claims:l,options:r,runAs:o});r.logger.info(`POST ${s} -> ${r.functionPath}${o?` (as ${r.as??""})`:""}`);const n=await d(s,{body:JSON.stringify(p),headers:m,method:"POST"}),g=await O(n,r.logger);return k(r.logger,{body:g,runAs:o,status:n.status}),{body:g,code:n.ok?0:1,requestUrl:s}},L=$(({argument:r,cwd:e,logger:o,options:t})=>{const a=r[0];return a?T({args:t.args,as:t.as,claims:t.claims,cwd:e,functionPath:a,logger:o,shard:t.shard,token:t.token,url:t.url}):(o.error("missing function path. Usage: lunora run <functionPath> [--args <json>]"),{code:1})});export{L as execute,O as readAndLogBody,T as runRpcCommand};