@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) • 3.3 kB
JavaScript
import{LunoraError as l}from"@lunora/errors";import{r as y,d as v}from"../packem_shared/admin-token-VdUnvnKW.mjs";import{r as A,a as b}from"../packem_shared/admin-url-BhF5ufg1.mjs";import{d as N}from"../packem_shared/command-DwC11Cwe.mjs";import{a as _}from"../packem_shared/resolve-target-BRvw8Pf7.mjs";const f=(r,t="--args")=>{if(r===void 0||r.length===0)return{};let e;try{e=JSON.parse(r)}catch(a){const n=a instanceof Error?a.message:String(a);throw new l("INTERNAL",`failed to parse ${t} as JSON: ${n}`,{cause:a})}if(typeof e!="object"||e===null||Array.isArray(e))throw new l("INTERNAL",`${t} must be a JSON object (e.g. '{"id":1}'), got ${Array.isArray(e)?"an array":typeof e}`);return e},S=/\/$/u,P="__lunora_admin__:runAs",I="__lunora_admin__:",O=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")},R=({args:r,claims:t,options:e,runAs:a})=>{const n=a?{args:{args:r,functionPath:e.functionPath,identity:t,userId:e.as},functionPath:P}:{args:r,functionPath:e.functionPath};return e.shard!==void 0&&(n.shardKey=e.shard),n},T=({cwd:r,logger:t,needsBearer:e,url:a})=>e?A(a,t,r):(_({cwd:r,url:a})??b(r)).replace(S,""),k=({args:r,claims:t},e)=>{try{return{args:f(r),claims:t===void 0?void 0:f(t,"--claims")}}catch(a){e.error(a instanceof Error?a.message:String(a));return}},E=async r=>{const t=r.cwd??process.cwd(),e=r.as!==void 0&&r.as!=="";if(r.claims!==void 0&&!e)return r.logger.error("--claims requires --as <userId> — extra identity claims only travel inside the admin-gated `runAs` dispatch."),{body:void 0,code:1,requestUrl:r.url??""};const a=e||r.functionPath.startsWith(I),n=T({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");const c=k(r,r.logger);if(c===void 0)return{body:void 0,code:1,requestUrl:s};const{source:g,token:o}=a?y({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 ${v(g)}`);const h={"content-type":"application/json",...o===void 0?{}:{authorization:`Bearer ${o}`}},m=R({args:c.args,claims:c.claims,options:r,runAs:e});r.logger.info(`POST ${s} -> ${r.functionPath}${e?` (as ${r.as??""})`:""}`);const d=await i(s,{body:JSON.stringify(m),headers:h,method:"POST"}),u=await O(d,r.logger);return $(r.logger,{body:u,runAs:e,status:d.status}),{body:u,code:d.ok?0:1,requestUrl:s}},L=N(({argument:r,cwd:t,logger:e,options:a})=>{const n=r[0];return n?E({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,O as readAndLogBody,E as runRpcCommand};