@lunora/codegen 1.0.0-alpha.420 → 1.0.0-alpha.422

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- import{existsSync as d}from"node:fs";import{join as l}from"node:path";import{SyntaxKind as m,Node as n}from"ts-morph";import{diagnosticAt as g}from"./CodegenDiagnosticError-BUXlG3P-.mjs";import{a as u}from"./module-specifiers-BctA0m_p.mjs";import{p as h}from"./parse-validator-BisfC8bR.mjs";import{declarationLineOf as x,findObjectProperty as c}from"./isTestPath-Uwb5a6Kq.mjs";import{a as S}from"./attribution-Cwt30DDJ.mjs";import{m as f}from"./ip-reads-D2qhKEb6.mjs";const b="shapes.ts",y=r=>{const t=r.getSymbol();if(!t)return r.getText()==="defineShape";for(const e of t.getDeclarations())if(n.isImportSpecifier(e))return u(e.getImportDeclaration().getModuleSpecifierValue())?e.getNameNode().getText()==="defineShape":!1;return!1},I=r=>{const t=r.getSymbol();if(!t)return!1;for(const e of t.getDeclarations()){if(!n.isNamespaceImport(e))continue;const i=e.getFirstAncestorByKind(m.ImportDeclaration);return i!==void 0&&u(i.getModuleSpecifierValue())}return!1},N=r=>{if(n.isIdentifier(r))return y(r);if(n.isPropertyAccessExpression(r)){const t=r.getExpression();return r.getName()==="defineShape"&&n.isIdentifier(t)&&I(t)}return!1},A=r=>{const[t]=r.getArguments();if(!t||!n.isObjectLiteralExpression(t))return;const e=c(t,"table");if(!e||!n.isPropertyAssignment(e))return;const i=e.getInitializer();return i&&n.isStringLiteral(i)?i.getLiteralValue():void 0},E=r=>{const[t]=r.getArguments();if(!t||!n.isObjectLiteralExpression(t))return{};const e=c(t,"args");if(!e||!n.isPropertyAssignment(e))return{};const i=e.getInitializer();return!i||!n.isObjectLiteralExpression(i)?{}:h(i)},P=r=>{const[t]=r.getArguments(),e=n.isObjectLiteralExpression(t)?c(t,"where"):void 0,i=n.isPropertyAssignment(e)?e.getInitializer():void 0;if(e===void 0)return f(r);if(!n.isArrowFunction(i)&&!n.isFunctionExpression(i))return!0;const o=i.getParameters()[0]?.getNameNode();return n.isIdentifier(o)&&o.getText()!=="ctx"&&o.findReferencesAsNodes().length>0||f(r)},v=r=>{const t=[];for(const e of r.getVariableDeclarations()){const i=e.getInitializer();if(i?.getKind()!==m.CallExpression)continue;const o=i,s=o.getExpression();if(!N(s))continue;const a=e.getNameNode();if(!n.isIdentifier(a)){if(e.getVariableStatement()?.hasExportKeyword()!==!0)continue;throw g(a,"defineShape exports must be plain named exports (no destructuring)")}const p=S(e,"binding");p!==void 0&&t.push({args:E(o),exportName:p,filePath:"shapes",line:x(e),...P(o)?{readsIp:!0}:{},table:A(o)})}return t},K=(r,t)=>{const e=l(t,b);if(!d(e))return[];const i=r.getSourceFile(e)??r.addSourceFileAtPath(e),o=v(i);return o.sort((s,a)=>s.exportName.localeCompare(a.exportName)),o};export{b as SHAPES_FILENAME,K as discoverShapes};
1
+ import{existsSync as d}from"node:fs";import{join as l}from"node:path";import{SyntaxKind as m,Node as n}from"ts-morph";import{diagnosticAt as g}from"./CodegenDiagnosticError-BUXlG3P-.mjs";import{a as u}from"./module-specifiers-BctA0m_p.mjs";import{p as h}from"./parse-validator-BisfC8bR.mjs";import{declarationLineOf as x,findObjectProperty as c}from"./isTestPath-Uwb5a6Kq.mjs";import{a as S}from"./attribution-Cwt30DDJ.mjs";import{m as f}from"./ip-reads-By7tJdyu.mjs";const b="shapes.ts",y=r=>{const t=r.getSymbol();if(!t)return r.getText()==="defineShape";for(const e of t.getDeclarations())if(n.isImportSpecifier(e))return u(e.getImportDeclaration().getModuleSpecifierValue())?e.getNameNode().getText()==="defineShape":!1;return!1},I=r=>{const t=r.getSymbol();if(!t)return!1;for(const e of t.getDeclarations()){if(!n.isNamespaceImport(e))continue;const i=e.getFirstAncestorByKind(m.ImportDeclaration);return i!==void 0&&u(i.getModuleSpecifierValue())}return!1},N=r=>{if(n.isIdentifier(r))return y(r);if(n.isPropertyAccessExpression(r)){const t=r.getExpression();return r.getName()==="defineShape"&&n.isIdentifier(t)&&I(t)}return!1},A=r=>{const[t]=r.getArguments();if(!t||!n.isObjectLiteralExpression(t))return;const e=c(t,"table");if(!e||!n.isPropertyAssignment(e))return;const i=e.getInitializer();return i&&n.isStringLiteral(i)?i.getLiteralValue():void 0},E=r=>{const[t]=r.getArguments();if(!t||!n.isObjectLiteralExpression(t))return{};const e=c(t,"args");if(!e||!n.isPropertyAssignment(e))return{};const i=e.getInitializer();return!i||!n.isObjectLiteralExpression(i)?{}:h(i)},P=r=>{const[t]=r.getArguments(),e=n.isObjectLiteralExpression(t)?c(t,"where"):void 0,i=n.isPropertyAssignment(e)?e.getInitializer():void 0;if(e===void 0)return f(r);if(!n.isArrowFunction(i)&&!n.isFunctionExpression(i))return!0;const o=i.getParameters()[0]?.getNameNode();return n.isIdentifier(o)&&o.getText()!=="ctx"&&o.findReferencesAsNodes().length>0||f(r)},v=r=>{const t=[];for(const e of r.getVariableDeclarations()){const i=e.getInitializer();if(i?.getKind()!==m.CallExpression)continue;const o=i,s=o.getExpression();if(!N(s))continue;const a=e.getNameNode();if(!n.isIdentifier(a)){if(e.getVariableStatement()?.hasExportKeyword()!==!0)continue;throw g(a,"defineShape exports must be plain named exports (no destructuring)")}const p=S(e,"binding");p!==void 0&&t.push({args:E(o),exportName:p,filePath:"shapes",line:x(e),...P(o)?{readsIp:!0}:{},table:A(o)})}return t},K=(r,t)=>{const e=l(t,b);if(!d(e))return[];const i=r.getSourceFile(e)??r.addSourceFileAtPath(e),o=v(i);return o.sort((s,a)=>s.exportName.localeCompare(a.exportName)),o};export{b as SHAPES_FILENAME,K as discoverShapes};
@@ -1 +1 @@
1
- import"./declared-output-Iaar469J.mjs";import"./isTestPath-Uwb5a6Kq.mjs";import"./attribution-Cwt30DDJ.mjs";import"./unwrap-handler-return-DvbyiLjc.mjs";import"./ip-reads-D2qhKEb6.mjs";import"./classify-procedure-call-BmEirzc_.mjs";import{d as f}from"./index-DtJBMdZ4.mjs";import"./dispatch-namespace-DwTjD7BU.mjs";export{f as default};
1
+ import"./declared-output-Iaar469J.mjs";import"./isTestPath-Uwb5a6Kq.mjs";import"./attribution-Cwt30DDJ.mjs";import"./unwrap-handler-return-DvbyiLjc.mjs";import"./ip-reads-By7tJdyu.mjs";import"./classify-procedure-call-BmEirzc_.mjs";import{d as f}from"./index-M4HwQ4Jr.mjs";import"./dispatch-namespace-DwTjD7BU.mjs";export{f as default};
@@ -1 +1 @@
1
- import{LunoraError as g}from"@lunora/errors";import{Node as s,SyntaxKind as B}from"ts-morph";import{d as z}from"./declared-output-Iaar469J.mjs";import{findObjectProperty as h,bindingKeyName as H,listLunoraSourceFiles as M,lunoraRelativePath as W}from"./isTestPath-Uwb5a6Kq.mjs";import{e as K,i as O}from"./attribution-Cwt30DDJ.mjs";import{u as R,p as q,r as Y,c as w,a as G}from"./unwrap-handler-return-DvbyiLjc.mjs";import{m as J}from"./ip-reads-D2qhKEb6.mjs";import{b as L,s as X,c as Q}from"./classify-procedure-call-BmEirzc_.mjs";import{r as D,p as C,a as Z}from"./parse-validator-BisfC8bR.mjs";import{N as ee,l as te}from"./project-config-file-DR7xrrNB.mjs";import{s as ie}from"./dispatch-namespace-DwTjD7BU.mjs";const I=e=>e!==void 0&&(s.isArrowFunction(e)||s.isFunctionExpression(e))?e:void 0,ne=e=>{const i=e.getArguments()[0],t=I(i);if(t!==void 0)return t;if(i===void 0||!s.isObjectLiteralExpression(i))return;const n=h(i,"handler");return n!==void 0&&s.isPropertyAssignment(n)?I(n.getInitializer()):void 0},re=e=>{const i=e.getArguments()[0];if(!i||!s.isObjectLiteralExpression(i))return"unknown";const t=h(i,"handler");if(!t||!s.isPropertyAssignment(t))return"unknown";const n=t.getInitializer();return!n||!(s.isArrowFunction(n)||s.isFunctionExpression(n))?"unknown":R(n)},oe=e=>{const i=e.getArguments()[0];return!i||!(s.isArrowFunction(i)||s.isFunctionExpression(i))?"unknown":R(i)},se=e=>{let i={};for(const t of L(e)){if(t.name!=="input")continue;const n=t.call.getArguments()[0],o=n!==void 0&&s.isExpression(n)?D(n):void 0;o!==void 0&&(i={...C(o),...i})}return i},ae=e=>{const i=L(e).find(n=>n.name==="output");if(!i)return;const t=i.call.getArguments()[0];return t&&s.isExpression(t)?q(i.call,()=>Z(t)):void 0},E=(e,i)=>{const t=h(e,i);if(t===void 0||!s.isPropertyAssignment(t))return;const n=t.getInitializer();return n!==void 0&&s.isStringLiteral(n)?n.getLiteralValue():void 0},k=(e,i)=>{const t=h(e,i);if(t===void 0||!s.isPropertyAssignment(t))return;const n=t.getInitializer();return n!==void 0&&s.isNumericLiteral(n)?n.getLiteralValue():void 0},ce=e=>{const i=h(e,"cache");if(i===void 0||!s.isPropertyAssignment(i))return;const t=i.getInitializer();if(t===void 0||!s.isObjectLiteralExpression(t))return;const n=E(t,"scope"),o=k(t,"maxAge"),r=k(t,"staleWhileRevalidate"),a=E(t,"tag"),c=E(t,"vary"),l={...o===void 0?{}:{maxAge:o},...n==="private"||n==="public"?{scope:n}:{},...r===void 0?{}:{staleWhileRevalidate:r},...a===void 0?{}:{tag:a},...c===void 0?{}:{vary:c}};return Object.keys(l).length===0?void 0:l},le=e=>{if(e===void 0||!s.isObjectLiteralExpression(e))return{};const i=ce(e),t=h(e,"rest");return t!==void 0&&s.isPropertyAssignment(t)?{...i===void 0?{}:{cache:i},rest:t.getInitializer()?.getText()==="true"}:i===void 0?{}:{cache:i}},ue=e=>{const i=L(e).find(t=>t.name==="expose");return i?le(i.call.getArguments()[0]):void 0},de=e=>{const i=L(e).find(r=>r.name==="x402");if(!i)return;const t=i.call.getArguments()[0];if(t===void 0||!s.isObjectLiteralExpression(t))return{};const n=h(t,"price");if(n===void 0||!s.isPropertyAssignment(n))return{};const o=n.getInitializer();return o===void 0?{}:s.isStringLiteral(o)?{price:o.getLiteralValue()}:s.isNumericLiteral(o)?{price:o.getLiteralValue()}:{}},ge=e=>{const i=e.getArguments()[0];if(!i||!s.isObjectLiteralExpression(i))return{};const t=h(i,"args");if(!t||!s.isPropertyAssignment(t))return{};const n=t.getInitializer(),o=n===void 0?void 0:D(n);return o===void 0?{}:C(o)},f=(e,i)=>{throw new g("INTERNAL",`@lunora/codegen: lazy function registry needs literal ${i} in ${e.getSourceFile().getFilePath()}:${String(e.getStartLineNumber())}`)},S=(e,i,t)=>{if(!e||!s.isObjectLiteralExpression(e))return f(i,"metadata objects");const n=new Set;for(const o of e.getProperties()){if(!s.isPropertyAssignment(o))return f(o,"metadata fields without spreads, getters, or shorthand");const r=o.getNameNode(),a=s.isStringLiteral(r)?r.getLiteralValue():r.getText();if(!s.isIdentifier(r)&&!s.isStringLiteral(r)||!t.includes(a)||n.has(a))return f(o,"supported, unique metadata fields");n.add(a)}return e},y=(e,i)=>{const t=e.getProperty(i);return t&&s.isPropertyAssignment(t)?t.getInitializer():void 0},pe=e=>{const i=S(e,e,["credentialHeaders","maxAge","scope","staleWhileRevalidate","tag","vary"]),t={};for(const r of["maxAge","staleWhileRevalidate"]){const a=y(i,r);if(!(a===void 0&&r!=="maxAge")){if(!a||!s.isNumericLiteral(a))return f(e,`cache.${r}`);t[r]=a.getLiteralValue()}}const n=y(i,"scope");if(!n||!s.isStringLiteral(n)||!["private","public"].includes(n.getLiteralValue()))return f(e,"cache.scope");t.scope=n.getLiteralValue();for(const r of["tag","vary"]){const a=y(i,r);if(a!==void 0){if(!s.isStringLiteral(a))return f(a,`cache.${r}`);t[r]=a.getLiteralValue()}}const o=y(i,"credentialHeaders");if(o!==void 0){if(!s.isArrayLiteralExpression(o))return f(o,"cache.credentialHeaders");t.credentialHeaders=o.getElements().map(r=>s.isStringLiteral(r)?r.getLiteralValue():f(r,"cache.credentialHeaders string entries"))}return t},fe=e=>{const i=S(e.getArguments()[0],e,["rest","cache"]),t=y(i,"rest"),n=y(i,"cache");return t!==void 0&&!s.isTrueLiteral(t)&&!s.isFalseLiteral(t)?f(t,".expose({ rest })"):{...t?{rest:s.isTrueLiteral(t)}:{},...n?{cache:pe(n)}:{}}},me=e=>{const i=S(e.getArguments()[0],e,["price"]),t=y(i,"price");return!t||!s.isStringLiteral(t)&&!s.isNumericLiteral(t)?f(e,"`.x402({ price })` value"):{price:t.getLiteralValue()}},he=e=>{const i=e.map(o=>{const r=o.getArguments()[0];return r===void 0?ee:te(r)}),t=i.every(o=>o!==null&&typeof o=="object"&&!Array.isArray(o)),n=i.reduce((o,r)=>({...r,...o}),{});return t?n:"unloaded"},ye=e=>{const{factory:i,steps:t}=X(e);if(!i)return f(e,"metadata from a resolvable Lunora factory root");const n={visibility:i.startsWith("internal")?"internal":"public"};for(const{call:r,name:a}of t){if(!["expose","input","meta","output","use","x402"].includes(a))return f(r,`known builder modifiers, got .${a}()`);a==="expose"&&n.expose===void 0&&(n.expose=fe(r)),a==="x402"&&n.x402===void 0&&(n.x402=me(r))}const o=t.filter(r=>r.name==="meta").map(r=>r.call);return o.length===0?n:{...n,meta:he(o)}},ve=new Set(["action","mutation","query","stream"]),N=(e,i,t)=>{const n=e.getProperty(i)?.getTypeAtLocation(t);if(n===void 0)return;const o=n.isUnion()?n.getUnionTypes().filter(a=>!a.isUndefined()):[n],r=o.length===1?o[0]?.getLiteralValue():void 0;return typeof r=="string"?r:void 0},U=(e,i)=>{const t=N(e,"kind",i);return t!==void 0&&ve.has(t)&&e.getProperty("handler")!==void 0?t:void 0},be=e=>{const i=e.getParent();if(s.isPropertyAccessExpression(i)&&i.getExpression()===e)return i.getName()==="functions";if(s.isElementAccessExpression(i)&&i.getExpression()===e){const n=i.getArgumentExpression();return n!==void 0&&s.isStringLiteral(n)&&n.getLiteralValue()==="functions"}const t=s.isVariableDeclaration(i)&&i.getNameNode()!==e?i.getNameNode():void 0;return t!==void 0&&s.isObjectBindingPattern(t)&&t.getElements().some(n=>(n.getPropertyNameNode()?.getText()??n.getName())==="functions")},it=(e,i)=>{for(const t of e)for(const n of t.getVariableDeclarations()){const o=n.getNameNode();if(!n.isExported()||!s.isIdentifier(o))continue;const a=(n.getType().getProperty("functions")?.getTypeAtLocation(n)?.getProperties()??[]).filter(u=>U(u.getTypeAtLocation(n),n)!==void 0);if(a.length===0||o.findReferencesAsNodes().some(u=>be(u)))continue;const l=n.getName(),d=a.map(u=>u.getName()).join(", ");console.warn(`@lunora/codegen: ${i(t)}:${l} ships functions (${d}) that nothing re-exports, so none is in the generated api. Add \`export const { ${d} } = ${l}.functions;\` (or export your own wrappers of them).`)}},Ne=new Set(["connect","disconnect","init","reactor","whisper"]),Le={ArrayBuffer:"bytes",GeoPoint:"geoPoint"},Ae=8,P=(e,i)=>{const t=e.getText(i);return t===""?{kind:"from"}:{kind:"from",tsType:t}},Ee=new Set(["false","true"]),Pe=[[e=>e.isAny()||e.isUnknown(),"any"],[e=>e.isString(),"string"],[e=>e.isNumber(),"number"],[e=>e.isBoolean(),"boolean"],[e=>e.isNull(),"null"],[e=>e.isBigInt(),"bigint"]],Te=(e,i,t)=>{if(!e.isObject()||e.isIntersection()||e.getCallSignatures().length>0||e.isClass())return;const n=e.getProperties();if(n.some(r=>r.getTypeAtLocation(i).getCallSignatures().length>0))return;const o=e.getStringIndexType();if(o!==void 0&&n.length===0)return{keyType:{kind:"string"},kind:"record",valueType:b(o,i,t+1)};if(!(o!==void 0||n.length===0))return{kind:"object",shape:Object.fromEntries(n.map(r=>[r.getName(),$(r.getTypeAtLocation(i),r.isOptional(),i,t+1)]))}},b=(e,i,t)=>{if(t>Ae)return P(e,i);const n=Pe.find(([a])=>a(e));if(n!==void 0)return{kind:n[1]};const o=Le[(e.getSymbol()??e.getAliasSymbol())?.getName()??""];if(o!==void 0)return{kind:o};if(e.isStringLiteral()||e.isNumberLiteral()||e.isBooleanLiteral()||e.isBigIntLiteral())return{kind:"literal",literalValue:e.getText(i),sensitive:!0};const r=e.isIntersection()?N(e,"__table",i):void 0;if(r!==void 0)return{kind:"id",tableName:r};if(e.isUnion())return F(e.getUnionTypes(),i,t);if(e.isArray()){const a=e.getArrayElementType();return a===void 0?P(e,i):{inner:b(a,i,t+1),kind:"array"}}return Te(e,i,t)??P(e,i)},F=(e,i,t)=>{const o=new Set(e.filter(l=>l.isBooleanLiteral()).map(l=>l.getText(i))).size===Ee.size,r=e.filter(l=>!(o&&l.isBooleanLiteral())).map(l=>b(l,i,t+1)),a=o?[...r,{kind:"boolean"}]:r,[c]=a;return a.length===1&&c!==void 0?c:{kind:"union",members:a}},$=(e,i,t,n)=>{const o=e.isUnion()?e.getUnionTypes():[e],r=o.filter(l=>!l.isUndefined());if(!i&&r.length===o.length)return b(e,t,n);if(r.length===0)return{inner:{kind:"any"},kind:"optional"};const[a]=r;return{inner:r.length===1&&a!==void 0?b(a,t,n):F(r,t,n),kind:"optional"}},xe=(e,i)=>{const t=e.getProperty("__type")?.getTypeAtLocation(i);if(t===void 0)return{kind:"from"};const n=(e.getSymbol()??e.getAliasSymbol())?.getName();return n==="TimestampColumnValidator"?{kind:"timestamp"}:n==="ObjectColumnValidator"&&t.getProperties().length===0?{kind:"object",shape:{}}:$(t,!1,i,0)},Se=new Set(["AsyncGenerator","AsyncIterable","AsyncIterableIterator"]),Oe=(e,i)=>{const t=e.getProperty("handler")?.getTypeAtLocation(i).getCallSignatures()[0]?.getReturnType();if(t===void 0)return[];if(Se.has((t.getSymbol()??t.getAliasSymbol())?.getName()??"")){const[n]=t.getTypeArguments();return n===void 0?[]:[n]}return(t.isUnion()?t.getUnionTypes():[t]).map(n=>n.getSymbol()?.getName()==="Promise"?n.getTypeArguments()[0]??n:n)},we=(e,i)=>{const t=[...new Set(e.map(n=>Y(n,i)))];return t.length===0||t.includes("unknown")?"unknown":t.join(" | ")},Ie=(e,i,t,n)=>{const o=e.getType(),r=U(o,e);if(r===void 0)return;const a={...n?{description:n}:{},exportName:i,filePath:t,kind:r};if(o.getProperty("channel")?.isOptional()===!1)throw new g("PUBLISHED_FUNCTION_UNSUPPORTED",`${t}:${i} re-exports a room from a published package; its name is not in the declared type, so codegen cannot route it. Declare the room with \`defineRoom\` in this file instead.`);const c=N(o,"lifecycle",e),l=o.getProperty("lifecycle");if(c===void 0&&l!==void 0&&!l.isOptional())throw new g("PUBLISHED_FUNCTION_UNSUPPORTED",`${t}:${i} re-exports a lifecycle hook from a published package whose type does not say which moment it runs at. Declare it with its moment, e.g. \`RegisteredLifecycleHook & { readonly lifecycle: "disconnect" }\` (what \`onDisconnect\` returns).`);if(c!==void 0){if(!Ne.has(c))throw new g("PUBLISHED_FUNCTION_UNSUPPORTED",`${t}:${i} re-exports a "${c}" lifecycle hook from a published package; only a locally declared hook can carry its route. Declare it in this file instead.`);return{...a,args:{},lifecycle:c,returnType:"void",visibility:"internal"}}const d=N(o,"visibility",e);if(d!=="internal"&&d!=="public")throw new g("PUBLISHED_FUNCTION_VISIBILITY_UNKNOWN",`${t}:${i} re-exports a function from a published package whose type does not say whether it is internal or public. Declare it in that package as \`RegisteredQuery\` / \`RegisteredMutation\` / \`RegisteredAction\` (public) or their \`RegisteredInternal*\` counterparts, not the bare \`RegisteredFunction\`, so codegen does not list an internal function in the client \`api\`.`);if(d==="internal"&&o.getProperty("visibility")?.isOptional()!==!1)throw new g("PUBLISHED_FUNCTION_UNSUPPORTED",`${t}:${i} re-exports an internal function from a published package whose type makes \`visibility\` optional, so the value may not carry it. Declare it with a \`RegisteredInternal*\` type, which requires \`visibility: "internal"\`.`);const u=o.getProperty("args")?.getTypeAtLocation(e);if(u===void 0||u.isAny()||u.isUnknown()||u.getStringIndexType()!==void 0||u.getNumberIndexType()!==void 0)throw new g("PUBLISHED_FUNCTION_UNSUPPORTED",`${t}:${i} re-exports a function from a published package whose type does not list its arguments. Declare its validator map with one key per argument (an empty \`{}\` for none) so codegen can type the client call.`);const m=Object.fromEntries(u.getProperties().map(p=>[p.getName(),xe(p.getTypeAtLocation(e),e)]));return{...a,args:m,...r==="query"?{readsIp:!0}:{},returnType:we(Oe(o,e),e),typeDerivedArgs:!0,visibility:d}},ke=8,A=(e,i=0)=>{if(i>ke)return;if(s.isCallExpression(e))return{call:e};if(s.isParenthesizedExpression(e)||s.isAsExpression(e)||s.isSatisfiesExpression(e)||s.isNonNullExpression(e))return A(e.getExpression(),i+1);if(!s.isIdentifier(e)&&!s.isPropertyAccessExpression(e))return;const t=e.getSymbol()?.getValueDeclaration();return t?T(t,i+1):void 0},T=(e,i)=>{if(s.isVariableDeclaration(e)||s.isPropertyAssignment(e)){const t=e.getInitializer();return t?A(t,i):{deadEnd:e}}if(s.isShorthandPropertyAssignment(e)){const t=e.getValueSymbol()?.getValueDeclaration();return t?T(t,i+1):void 0}if(s.isBindingElement(e)){const t=H(e),r=e.getFirstAncestorByKind(B.VariableDeclaration)?.getInitializer()?.getType().getProperty(t)?.getValueDeclaration();return r?T(r,i+1):void 0}return{deadEnd:e}},x=e=>{const i=A(e);return i!==void 0&&"call"in i?i.call:void 0},_=e=>{const i=A(e);return i!==void 0&&"deadEnd"in i&&i.deadEnd.getSourceFile().isDeclarationFile()},_e=(e,i=()=>!0)=>{const t=e.getNameNode();if(s.isObjectBindingPattern(t)){const r=[];for(const a of t.getElements().filter(c=>i(c.getName()))){const c=x(a.getNameNode());c&&r.push([a.getName(),c])}return r}if(!i(e.getName()))return[];const n=e.getInitializer(),o=n&&(s.isCallExpression(n)?n:x(n));return o?[[e.getName(),o]]:[]},Re=(e,i)=>{const t=e.getNameNode();if(s.isObjectBindingPattern(t))return t.getElements().filter(o=>i(o.getName())&&_(o.getNameNode())).map(o=>[o.getName(),o.getNameNode()]);const n=e.getInitializer();return i(e.getName())&&n!==void 0&&_(n)?[[e.getName(),t]]:[]},De=e=>{const i=e.getArguments()[0],t=s.isObjectLiteralExpression(i)?i.getProperty("name"):void 0,n=s.isPropertyAssignment(t)?t.getInitializer():void 0;if(!n||!s.isStringLiteral(n))throw new g("ROOM_NAME_NOT_LITERAL",'defineRoom needs a string-literal `name` (e.g. `defineRoom({ name: "chat", … })`) so codegen can type its reference.');return n.getLiteralValue()},Ce=e=>e.startsWith("/")&&new URL(e,"https://lunora.invalid").pathname===e&&e!=="/_lunora"&&!e.startsWith("/_lunora/"),Ue=e=>{const i=e.getArguments()[0];if(!s.isObjectLiteralExpression(i))throw new g("SOCKET_PATH_NOT_LITERAL","defineWebSocket needs an object-literal options argument so codegen can read its `path` and `auth`.");const t=i.getProperty("path"),n=s.isPropertyAssignment(t)?t.getInitializer():void 0;if(!n||!s.isStringLiteral(n))throw new g("SOCKET_PATH_NOT_LITERAL",'defineWebSocket needs a string-literal `path` (e.g. `defineWebSocket({ path: "/chat", … })`) so codegen can route its upgrade.');const o=n.getLiteralValue();if(!Ce(o))throw new g("SOCKET_PATH_INVALID",`defineWebSocket path "${o}" must be a plain URL path starting with "/" (no "?", "#", ".." or unencoded characters) and not under "/_lunora".`);if(i.getProperties().some(l=>s.isSpreadAssignment(l)||s.isPropertyAssignment(l)&&s.isComputedPropertyName(l.getNameNode())))throw new g("SOCKET_AUTH_NOT_LITERAL","defineWebSocket cannot spread its options or use a computed key: codegen must read `auth` as a literal `auth: true` or `auth: false`.");const a=i.getProperty("auth");if(a===void 0)return{auth:!1,path:o};const c=s.isPropertyAssignment(a)?a.getInitializer():void 0;if(c===void 0||!(s.isTrueLiteral(c)||s.isFalseLiteral(c)))throw new g("SOCKET_AUTH_NOT_LITERAL","defineWebSocket needs a literal `auth: true` or `auth: false`, so codegen can read whether the route requires a session. A shorthand or a variable is not read.");return{auth:s.isTrueLiteral(c),path:o}},Fe=(e,i,t,n)=>{const o=n?ye(i):void 0,r=n?o?.expose:ue(i),a=n?o?.x402:de(i),c=w(()=>ae(i)),l=w(()=>oe(e)),d=z({kind:t.kind,output:c.value});return G(d?c.erased:l.erased),{args:se(i),...r?{expose:r}:{},...a?{x402:a}:{},...o?.meta===void 0?{}:{meta:o.meta},...t.adminOnly?{adminOnly:!0}:{},kind:t.kind,...c.value?{output:c.value}:{},returnType:l.value,visibility:o?.visibility==="internal"||t.visibility==="internal"?"internal":"public"}},$e=(e,i)=>{const t=Q(e,i);if(!t)return;if(t.channel)return{args:{},channel:{kind:t.channel,name:De(e)},kind:t.kind,returnType:"void",visibility:t.visibility};const{receiver:n}=t;return n?Fe(e,n,t,i):t.lifecycle?{args:{},kind:t.kind,lifecycle:t.lifecycle,returnType:"void",...t.lifecycle==="socket"?{socket:{...Ue(e),origin:`${e.getSourceFile().getFilePath()}@${String(e.getStart())}`}}:{},visibility:t.visibility}:{args:ge(e),kind:t.kind,returnType:re(e),visibility:t.visibility}},V=e=>{const i=e?.getJsDocs().at(-1)?.getDescription().trim();return i===""?void 0:i},Ve=e=>V(e.getFirstAncestor(i=>s.isVariableStatement(i))),je=e=>{const i=new Map;for(const t of e.getVariableStatements())if(t.isExported())for(const n of t.getDeclarations())i.has(n.getName())||i.set(n.getName(),t);return t=>V(i.get(t))},j=(e,i,t,n,o)=>{const r=$e(e,n);if(!r)return;const a=o??Ve(e);return{args:r.args,...a?{description:a}:{},exportName:i,filePath:t,kind:r.kind,returnType:r.returnType,...r.output?{output:r.output}:{},...r.meta===void 0?{}:{meta:r.meta},visibility:r.visibility,...r.adminOnly?{adminOnly:!0}:{},...r.kind==="query"&&(ne(e)===void 0||J(e))?{readsIp:!0}:{},...r.expose?{expose:r.expose}:{},...r.x402?{x402:r.x402}:{},...r.lifecycle?{lifecycle:r.lifecycle}:{},...r.channel?{channel:r.channel}:{},...r.socket?{socket:r.socket}:{}}},Be=(e,i,t)=>{const n=[];for(const o of e.getExportAssignments()){if(o.isExportEquals())continue;const r=x(o.getExpression()),a=r?j(r,"default",i,t):void 0;r&&a&&n.push({call:r,entry:a})}return n},ze=(e,i,t)=>{const n=K(e),o=je(e),r=new Map,a=(d,u)=>{if(d?.channel?.kind!=="room")return;const m=r.get(d.channel.name);if(m!==void 0&&m!==u)throw new g("DUPLICATE_ROOM_NAME",`Duplicate room name "${d.channel.name}" in ${i}: two defineRoom calls share it. Room names must be unique.`);r.set(d.channel.name,u)},c=e.getVariableStatements().flatMap(d=>d.getDeclarations()).flatMap(d=>_e(d,u=>n.has(u))).flatMap(([d,u])=>(n.get(d)??[]).filter(m=>O(m)).flatMap(m=>{const p=j(u,m,i,t,o(m));return a(p,u),p?[p]:[]})),l=new Set(c.map(d=>d.exportName));for(const d of e.getVariableStatements().flatMap(u=>u.getDeclarations()))for(const[u,m]of Re(d,p=>n.has(p)))for(const p of(n.get(u)??[]).filter(v=>O(v)&&!l.has(v))){const v=Ie(m,p,i,o(p));v&&(c.push(v),l.add(p))}for(const{call:d,entry:u}of Be(e,i,t))l.has(u.exportName)||(a(u,d),c.push(u));return c},He=e=>{const i=new Map;for(const t of e){if(t.channel?.kind!=="room")continue;const n=i.get(t.channel.name);if(n!==void 0&&n.file!==t.filePath)throw new g("DUPLICATE_ROOM_NAME",`Duplicate room name "${t.channel.name}": defined in ${n.origin} and ${t.filePath}:${t.exportName}. Room names must be unique across the project.`);n===void 0&&i.set(t.channel.name,{file:t.filePath,origin:`${t.filePath}:${t.exportName}`})}},Me=e=>{const i=new Map;for(const t of e){const n=ie(t.filePath),o=i.get(n);if(o&&o!==t.filePath)throw Object.assign(new Error(`Namespace collision: "${o}" and "${t.filePath}" both resolve to "${n}". Rename one of the files so the JS-identifier-sanitized names differ. (note: case-insensitive filesystems may also cause this — \`foo\` and \`FOO\` map to the same identifier)`),{code:"NAMESPACE_COLLISION",name:"LunoraError",paths:[o,t.filePath],status:500});i.set(n,t.filePath)}},nt=(e,i,t={})=>{const n=M(i),o=[];for(const r of n){const a=e.getSourceFile(r)??e.addSourceFileAtPath(r),c=W(i,r);o.push(...ze(a,c,t.lazyFunctions===!0))}return o.sort((r,a)=>`${r.filePath}:${r.exportName}`.localeCompare(`${a.filePath}:${a.exportName}`)),Me(o),He(o),o};export{nt as d,I as i,ne as p,it as w};
1
+ import{LunoraError as g}from"@lunora/errors";import{Node as s,SyntaxKind as B}from"ts-morph";import{d as z}from"./declared-output-Iaar469J.mjs";import{findObjectProperty as h,bindingKeyName as H,listLunoraSourceFiles as M,lunoraRelativePath as W}from"./isTestPath-Uwb5a6Kq.mjs";import{e as K,i as O}from"./attribution-Cwt30DDJ.mjs";import{u as R,p as q,r as Y,c as w,a as G}from"./unwrap-handler-return-DvbyiLjc.mjs";import{m as J}from"./ip-reads-By7tJdyu.mjs";import{b as L,s as X,c as Q}from"./classify-procedure-call-BmEirzc_.mjs";import{r as D,p as C,a as Z}from"./parse-validator-BisfC8bR.mjs";import{N as ee,l as te}from"./project-config-file-DR7xrrNB.mjs";import{s as ie}from"./dispatch-namespace-DwTjD7BU.mjs";const I=e=>e!==void 0&&(s.isArrowFunction(e)||s.isFunctionExpression(e))?e:void 0,ne=e=>{const i=e.getArguments()[0],t=I(i);if(t!==void 0)return t;if(i===void 0||!s.isObjectLiteralExpression(i))return;const n=h(i,"handler");return n!==void 0&&s.isPropertyAssignment(n)?I(n.getInitializer()):void 0},re=e=>{const i=e.getArguments()[0];if(!i||!s.isObjectLiteralExpression(i))return"unknown";const t=h(i,"handler");if(!t||!s.isPropertyAssignment(t))return"unknown";const n=t.getInitializer();return!n||!(s.isArrowFunction(n)||s.isFunctionExpression(n))?"unknown":R(n)},oe=e=>{const i=e.getArguments()[0];return!i||!(s.isArrowFunction(i)||s.isFunctionExpression(i))?"unknown":R(i)},se=e=>{let i={};for(const t of L(e)){if(t.name!=="input")continue;const n=t.call.getArguments()[0],o=n!==void 0&&s.isExpression(n)?D(n):void 0;o!==void 0&&(i={...C(o),...i})}return i},ae=e=>{const i=L(e).find(n=>n.name==="output");if(!i)return;const t=i.call.getArguments()[0];return t&&s.isExpression(t)?q(i.call,()=>Z(t)):void 0},E=(e,i)=>{const t=h(e,i);if(t===void 0||!s.isPropertyAssignment(t))return;const n=t.getInitializer();return n!==void 0&&s.isStringLiteral(n)?n.getLiteralValue():void 0},k=(e,i)=>{const t=h(e,i);if(t===void 0||!s.isPropertyAssignment(t))return;const n=t.getInitializer();return n!==void 0&&s.isNumericLiteral(n)?n.getLiteralValue():void 0},ce=e=>{const i=h(e,"cache");if(i===void 0||!s.isPropertyAssignment(i))return;const t=i.getInitializer();if(t===void 0||!s.isObjectLiteralExpression(t))return;const n=E(t,"scope"),o=k(t,"maxAge"),r=k(t,"staleWhileRevalidate"),a=E(t,"tag"),c=E(t,"vary"),l={...o===void 0?{}:{maxAge:o},...n==="private"||n==="public"?{scope:n}:{},...r===void 0?{}:{staleWhileRevalidate:r},...a===void 0?{}:{tag:a},...c===void 0?{}:{vary:c}};return Object.keys(l).length===0?void 0:l},le=e=>{if(e===void 0||!s.isObjectLiteralExpression(e))return{};const i=ce(e),t=h(e,"rest");return t!==void 0&&s.isPropertyAssignment(t)?{...i===void 0?{}:{cache:i},rest:t.getInitializer()?.getText()==="true"}:i===void 0?{}:{cache:i}},ue=e=>{const i=L(e).find(t=>t.name==="expose");return i?le(i.call.getArguments()[0]):void 0},de=e=>{const i=L(e).find(r=>r.name==="x402");if(!i)return;const t=i.call.getArguments()[0];if(t===void 0||!s.isObjectLiteralExpression(t))return{};const n=h(t,"price");if(n===void 0||!s.isPropertyAssignment(n))return{};const o=n.getInitializer();return o===void 0?{}:s.isStringLiteral(o)?{price:o.getLiteralValue()}:s.isNumericLiteral(o)?{price:o.getLiteralValue()}:{}},ge=e=>{const i=e.getArguments()[0];if(!i||!s.isObjectLiteralExpression(i))return{};const t=h(i,"args");if(!t||!s.isPropertyAssignment(t))return{};const n=t.getInitializer(),o=n===void 0?void 0:D(n);return o===void 0?{}:C(o)},f=(e,i)=>{throw new g("INTERNAL",`@lunora/codegen: lazy function registry needs literal ${i} in ${e.getSourceFile().getFilePath()}:${String(e.getStartLineNumber())}`)},S=(e,i,t)=>{if(!e||!s.isObjectLiteralExpression(e))return f(i,"metadata objects");const n=new Set;for(const o of e.getProperties()){if(!s.isPropertyAssignment(o))return f(o,"metadata fields without spreads, getters, or shorthand");const r=o.getNameNode(),a=s.isStringLiteral(r)?r.getLiteralValue():r.getText();if(!s.isIdentifier(r)&&!s.isStringLiteral(r)||!t.includes(a)||n.has(a))return f(o,"supported, unique metadata fields");n.add(a)}return e},y=(e,i)=>{const t=e.getProperty(i);return t&&s.isPropertyAssignment(t)?t.getInitializer():void 0},pe=e=>{const i=S(e,e,["credentialHeaders","maxAge","scope","staleWhileRevalidate","tag","vary"]),t={};for(const r of["maxAge","staleWhileRevalidate"]){const a=y(i,r);if(!(a===void 0&&r!=="maxAge")){if(!a||!s.isNumericLiteral(a))return f(e,`cache.${r}`);t[r]=a.getLiteralValue()}}const n=y(i,"scope");if(!n||!s.isStringLiteral(n)||!["private","public"].includes(n.getLiteralValue()))return f(e,"cache.scope");t.scope=n.getLiteralValue();for(const r of["tag","vary"]){const a=y(i,r);if(a!==void 0){if(!s.isStringLiteral(a))return f(a,`cache.${r}`);t[r]=a.getLiteralValue()}}const o=y(i,"credentialHeaders");if(o!==void 0){if(!s.isArrayLiteralExpression(o))return f(o,"cache.credentialHeaders");t.credentialHeaders=o.getElements().map(r=>s.isStringLiteral(r)?r.getLiteralValue():f(r,"cache.credentialHeaders string entries"))}return t},fe=e=>{const i=S(e.getArguments()[0],e,["rest","cache"]),t=y(i,"rest"),n=y(i,"cache");return t!==void 0&&!s.isTrueLiteral(t)&&!s.isFalseLiteral(t)?f(t,".expose({ rest })"):{...t?{rest:s.isTrueLiteral(t)}:{},...n?{cache:pe(n)}:{}}},me=e=>{const i=S(e.getArguments()[0],e,["price"]),t=y(i,"price");return!t||!s.isStringLiteral(t)&&!s.isNumericLiteral(t)?f(e,"`.x402({ price })` value"):{price:t.getLiteralValue()}},he=e=>{const i=e.map(o=>{const r=o.getArguments()[0];return r===void 0?ee:te(r)}),t=i.every(o=>o!==null&&typeof o=="object"&&!Array.isArray(o)),n=i.reduce((o,r)=>({...r,...o}),{});return t?n:"unloaded"},ye=e=>{const{factory:i,steps:t}=X(e);if(!i)return f(e,"metadata from a resolvable Lunora factory root");const n={visibility:i.startsWith("internal")?"internal":"public"};for(const{call:r,name:a}of t){if(!["expose","input","meta","output","use","x402"].includes(a))return f(r,`known builder modifiers, got .${a}()`);a==="expose"&&n.expose===void 0&&(n.expose=fe(r)),a==="x402"&&n.x402===void 0&&(n.x402=me(r))}const o=t.filter(r=>r.name==="meta").map(r=>r.call);return o.length===0?n:{...n,meta:he(o)}},ve=new Set(["action","mutation","query","stream"]),N=(e,i,t)=>{const n=e.getProperty(i)?.getTypeAtLocation(t);if(n===void 0)return;const o=n.isUnion()?n.getUnionTypes().filter(a=>!a.isUndefined()):[n],r=o.length===1?o[0]?.getLiteralValue():void 0;return typeof r=="string"?r:void 0},U=(e,i)=>{const t=N(e,"kind",i);return t!==void 0&&ve.has(t)&&e.getProperty("handler")!==void 0?t:void 0},be=e=>{const i=e.getParent();if(s.isPropertyAccessExpression(i)&&i.getExpression()===e)return i.getName()==="functions";if(s.isElementAccessExpression(i)&&i.getExpression()===e){const n=i.getArgumentExpression();return n!==void 0&&s.isStringLiteral(n)&&n.getLiteralValue()==="functions"}const t=s.isVariableDeclaration(i)&&i.getNameNode()!==e?i.getNameNode():void 0;return t!==void 0&&s.isObjectBindingPattern(t)&&t.getElements().some(n=>(n.getPropertyNameNode()?.getText()??n.getName())==="functions")},it=(e,i)=>{for(const t of e)for(const n of t.getVariableDeclarations()){const o=n.getNameNode();if(!n.isExported()||!s.isIdentifier(o))continue;const a=(n.getType().getProperty("functions")?.getTypeAtLocation(n)?.getProperties()??[]).filter(u=>U(u.getTypeAtLocation(n),n)!==void 0);if(a.length===0||o.findReferencesAsNodes().some(u=>be(u)))continue;const l=n.getName(),d=a.map(u=>u.getName()).join(", ");console.warn(`@lunora/codegen: ${i(t)}:${l} ships functions (${d}) that nothing re-exports, so none is in the generated api. Add \`export const { ${d} } = ${l}.functions;\` (or export your own wrappers of them).`)}},Ne=new Set(["connect","disconnect","init","reactor","whisper"]),Le={ArrayBuffer:"bytes",GeoPoint:"geoPoint"},Ae=8,P=(e,i)=>{const t=e.getText(i);return t===""?{kind:"from"}:{kind:"from",tsType:t}},Ee=new Set(["false","true"]),Pe=[[e=>e.isAny()||e.isUnknown(),"any"],[e=>e.isString(),"string"],[e=>e.isNumber(),"number"],[e=>e.isBoolean(),"boolean"],[e=>e.isNull(),"null"],[e=>e.isBigInt(),"bigint"]],Te=(e,i,t)=>{if(!e.isObject()||e.isIntersection()||e.getCallSignatures().length>0||e.isClass())return;const n=e.getProperties();if(n.some(r=>r.getTypeAtLocation(i).getCallSignatures().length>0))return;const o=e.getStringIndexType();if(o!==void 0&&n.length===0)return{keyType:{kind:"string"},kind:"record",valueType:b(o,i,t+1)};if(!(o!==void 0||n.length===0))return{kind:"object",shape:Object.fromEntries(n.map(r=>[r.getName(),$(r.getTypeAtLocation(i),r.isOptional(),i,t+1)]))}},b=(e,i,t)=>{if(t>Ae)return P(e,i);const n=Pe.find(([a])=>a(e));if(n!==void 0)return{kind:n[1]};const o=Le[(e.getSymbol()??e.getAliasSymbol())?.getName()??""];if(o!==void 0)return{kind:o};if(e.isStringLiteral()||e.isNumberLiteral()||e.isBooleanLiteral()||e.isBigIntLiteral())return{kind:"literal",literalValue:e.getText(i),sensitive:!0};const r=e.isIntersection()?N(e,"__table",i):void 0;if(r!==void 0)return{kind:"id",tableName:r};if(e.isUnion())return F(e.getUnionTypes(),i,t);if(e.isArray()){const a=e.getArrayElementType();return a===void 0?P(e,i):{inner:b(a,i,t+1),kind:"array"}}return Te(e,i,t)??P(e,i)},F=(e,i,t)=>{const o=new Set(e.filter(l=>l.isBooleanLiteral()).map(l=>l.getText(i))).size===Ee.size,r=e.filter(l=>!(o&&l.isBooleanLiteral())).map(l=>b(l,i,t+1)),a=o?[...r,{kind:"boolean"}]:r,[c]=a;return a.length===1&&c!==void 0?c:{kind:"union",members:a}},$=(e,i,t,n)=>{const o=e.isUnion()?e.getUnionTypes():[e],r=o.filter(l=>!l.isUndefined());if(!i&&r.length===o.length)return b(e,t,n);if(r.length===0)return{inner:{kind:"any"},kind:"optional"};const[a]=r;return{inner:r.length===1&&a!==void 0?b(a,t,n):F(r,t,n),kind:"optional"}},xe=(e,i)=>{const t=e.getProperty("__type")?.getTypeAtLocation(i);if(t===void 0)return{kind:"from"};const n=(e.getSymbol()??e.getAliasSymbol())?.getName();return n==="TimestampColumnValidator"?{kind:"timestamp"}:n==="ObjectColumnValidator"&&t.getProperties().length===0?{kind:"object",shape:{}}:$(t,!1,i,0)},Se=new Set(["AsyncGenerator","AsyncIterable","AsyncIterableIterator"]),Oe=(e,i)=>{const t=e.getProperty("handler")?.getTypeAtLocation(i).getCallSignatures()[0]?.getReturnType();if(t===void 0)return[];if(Se.has((t.getSymbol()??t.getAliasSymbol())?.getName()??"")){const[n]=t.getTypeArguments();return n===void 0?[]:[n]}return(t.isUnion()?t.getUnionTypes():[t]).map(n=>n.getSymbol()?.getName()==="Promise"?n.getTypeArguments()[0]??n:n)},we=(e,i)=>{const t=[...new Set(e.map(n=>Y(n,i)))];return t.length===0||t.includes("unknown")?"unknown":t.join(" | ")},Ie=(e,i,t,n)=>{const o=e.getType(),r=U(o,e);if(r===void 0)return;const a={...n?{description:n}:{},exportName:i,filePath:t,kind:r};if(o.getProperty("channel")?.isOptional()===!1)throw new g("PUBLISHED_FUNCTION_UNSUPPORTED",`${t}:${i} re-exports a room from a published package; its name is not in the declared type, so codegen cannot route it. Declare the room with \`defineRoom\` in this file instead.`);const c=N(o,"lifecycle",e),l=o.getProperty("lifecycle");if(c===void 0&&l!==void 0&&!l.isOptional())throw new g("PUBLISHED_FUNCTION_UNSUPPORTED",`${t}:${i} re-exports a lifecycle hook from a published package whose type does not say which moment it runs at. Declare it with its moment, e.g. \`RegisteredLifecycleHook & { readonly lifecycle: "disconnect" }\` (what \`onDisconnect\` returns).`);if(c!==void 0){if(!Ne.has(c))throw new g("PUBLISHED_FUNCTION_UNSUPPORTED",`${t}:${i} re-exports a "${c}" lifecycle hook from a published package; only a locally declared hook can carry its route. Declare it in this file instead.`);return{...a,args:{},lifecycle:c,returnType:"void",visibility:"internal"}}const d=N(o,"visibility",e);if(d!=="internal"&&d!=="public")throw new g("PUBLISHED_FUNCTION_VISIBILITY_UNKNOWN",`${t}:${i} re-exports a function from a published package whose type does not say whether it is internal or public. Declare it in that package as \`RegisteredQuery\` / \`RegisteredMutation\` / \`RegisteredAction\` (public) or their \`RegisteredInternal*\` counterparts, not the bare \`RegisteredFunction\`, so codegen does not list an internal function in the client \`api\`.`);if(d==="internal"&&o.getProperty("visibility")?.isOptional()!==!1)throw new g("PUBLISHED_FUNCTION_UNSUPPORTED",`${t}:${i} re-exports an internal function from a published package whose type makes \`visibility\` optional, so the value may not carry it. Declare it with a \`RegisteredInternal*\` type, which requires \`visibility: "internal"\`.`);const u=o.getProperty("args")?.getTypeAtLocation(e);if(u===void 0||u.isAny()||u.isUnknown()||u.getStringIndexType()!==void 0||u.getNumberIndexType()!==void 0)throw new g("PUBLISHED_FUNCTION_UNSUPPORTED",`${t}:${i} re-exports a function from a published package whose type does not list its arguments. Declare its validator map with one key per argument (an empty \`{}\` for none) so codegen can type the client call.`);const m=Object.fromEntries(u.getProperties().map(p=>[p.getName(),xe(p.getTypeAtLocation(e),e)]));return{...a,args:m,...r==="query"?{readsIp:!0}:{},returnType:we(Oe(o,e),e),typeDerivedArgs:!0,visibility:d}},ke=8,A=(e,i=0)=>{if(i>ke)return;if(s.isCallExpression(e))return{call:e};if(s.isParenthesizedExpression(e)||s.isAsExpression(e)||s.isSatisfiesExpression(e)||s.isNonNullExpression(e))return A(e.getExpression(),i+1);if(!s.isIdentifier(e)&&!s.isPropertyAccessExpression(e))return;const t=e.getSymbol()?.getValueDeclaration();return t?T(t,i+1):void 0},T=(e,i)=>{if(s.isVariableDeclaration(e)||s.isPropertyAssignment(e)){const t=e.getInitializer();return t?A(t,i):{deadEnd:e}}if(s.isShorthandPropertyAssignment(e)){const t=e.getValueSymbol()?.getValueDeclaration();return t?T(t,i+1):void 0}if(s.isBindingElement(e)){const t=H(e),r=e.getFirstAncestorByKind(B.VariableDeclaration)?.getInitializer()?.getType().getProperty(t)?.getValueDeclaration();return r?T(r,i+1):void 0}return{deadEnd:e}},x=e=>{const i=A(e);return i!==void 0&&"call"in i?i.call:void 0},_=e=>{const i=A(e);return i!==void 0&&"deadEnd"in i&&i.deadEnd.getSourceFile().isDeclarationFile()},_e=(e,i=()=>!0)=>{const t=e.getNameNode();if(s.isObjectBindingPattern(t)){const r=[];for(const a of t.getElements().filter(c=>i(c.getName()))){const c=x(a.getNameNode());c&&r.push([a.getName(),c])}return r}if(!i(e.getName()))return[];const n=e.getInitializer(),o=n&&(s.isCallExpression(n)?n:x(n));return o?[[e.getName(),o]]:[]},Re=(e,i)=>{const t=e.getNameNode();if(s.isObjectBindingPattern(t))return t.getElements().filter(o=>i(o.getName())&&_(o.getNameNode())).map(o=>[o.getName(),o.getNameNode()]);const n=e.getInitializer();return i(e.getName())&&n!==void 0&&_(n)?[[e.getName(),t]]:[]},De=e=>{const i=e.getArguments()[0],t=s.isObjectLiteralExpression(i)?i.getProperty("name"):void 0,n=s.isPropertyAssignment(t)?t.getInitializer():void 0;if(!n||!s.isStringLiteral(n))throw new g("ROOM_NAME_NOT_LITERAL",'defineRoom needs a string-literal `name` (e.g. `defineRoom({ name: "chat", … })`) so codegen can type its reference.');return n.getLiteralValue()},Ce=e=>e.startsWith("/")&&new URL(e,"https://lunora.invalid").pathname===e&&e!=="/_lunora"&&!e.startsWith("/_lunora/"),Ue=e=>{const i=e.getArguments()[0];if(!s.isObjectLiteralExpression(i))throw new g("SOCKET_PATH_NOT_LITERAL","defineWebSocket needs an object-literal options argument so codegen can read its `path` and `auth`.");const t=i.getProperty("path"),n=s.isPropertyAssignment(t)?t.getInitializer():void 0;if(!n||!s.isStringLiteral(n))throw new g("SOCKET_PATH_NOT_LITERAL",'defineWebSocket needs a string-literal `path` (e.g. `defineWebSocket({ path: "/chat", … })`) so codegen can route its upgrade.');const o=n.getLiteralValue();if(!Ce(o))throw new g("SOCKET_PATH_INVALID",`defineWebSocket path "${o}" must be a plain URL path starting with "/" (no "?", "#", ".." or unencoded characters) and not under "/_lunora".`);if(i.getProperties().some(l=>s.isSpreadAssignment(l)||s.isPropertyAssignment(l)&&s.isComputedPropertyName(l.getNameNode())))throw new g("SOCKET_AUTH_NOT_LITERAL","defineWebSocket cannot spread its options or use a computed key: codegen must read `auth` as a literal `auth: true` or `auth: false`.");const a=i.getProperty("auth");if(a===void 0)return{auth:!1,path:o};const c=s.isPropertyAssignment(a)?a.getInitializer():void 0;if(c===void 0||!(s.isTrueLiteral(c)||s.isFalseLiteral(c)))throw new g("SOCKET_AUTH_NOT_LITERAL","defineWebSocket needs a literal `auth: true` or `auth: false`, so codegen can read whether the route requires a session. A shorthand or a variable is not read.");return{auth:s.isTrueLiteral(c),path:o}},Fe=(e,i,t,n)=>{const o=n?ye(i):void 0,r=n?o?.expose:ue(i),a=n?o?.x402:de(i),c=w(()=>ae(i)),l=w(()=>oe(e)),d=z({kind:t.kind,output:c.value});return G(d?c.erased:l.erased),{args:se(i),...r?{expose:r}:{},...a?{x402:a}:{},...o?.meta===void 0?{}:{meta:o.meta},...t.adminOnly?{adminOnly:!0}:{},kind:t.kind,...c.value?{output:c.value}:{},returnType:l.value,visibility:o?.visibility==="internal"||t.visibility==="internal"?"internal":"public"}},$e=(e,i)=>{const t=Q(e,i);if(!t)return;if(t.channel)return{args:{},channel:{kind:t.channel,name:De(e)},kind:t.kind,returnType:"void",visibility:t.visibility};const{receiver:n}=t;return n?Fe(e,n,t,i):t.lifecycle?{args:{},kind:t.kind,lifecycle:t.lifecycle,returnType:"void",...t.lifecycle==="socket"?{socket:{...Ue(e),origin:`${e.getSourceFile().getFilePath()}@${String(e.getStart())}`}}:{},visibility:t.visibility}:{args:ge(e),kind:t.kind,returnType:re(e),visibility:t.visibility}},V=e=>{const i=e?.getJsDocs().at(-1)?.getDescription().trim();return i===""?void 0:i},Ve=e=>V(e.getFirstAncestor(i=>s.isVariableStatement(i))),je=e=>{const i=new Map;for(const t of e.getVariableStatements())if(t.isExported())for(const n of t.getDeclarations())i.has(n.getName())||i.set(n.getName(),t);return t=>V(i.get(t))},j=(e,i,t,n,o)=>{const r=$e(e,n);if(!r)return;const a=o??Ve(e);return{args:r.args,...a?{description:a}:{},exportName:i,filePath:t,kind:r.kind,returnType:r.returnType,...r.output?{output:r.output}:{},...r.meta===void 0?{}:{meta:r.meta},visibility:r.visibility,...r.adminOnly?{adminOnly:!0}:{},...r.kind==="query"&&(ne(e)===void 0||J(e))?{readsIp:!0}:{},...r.expose?{expose:r.expose}:{},...r.x402?{x402:r.x402}:{},...r.lifecycle?{lifecycle:r.lifecycle}:{},...r.channel?{channel:r.channel}:{},...r.socket?{socket:r.socket}:{}}},Be=(e,i,t)=>{const n=[];for(const o of e.getExportAssignments()){if(o.isExportEquals())continue;const r=x(o.getExpression()),a=r?j(r,"default",i,t):void 0;r&&a&&n.push({call:r,entry:a})}return n},ze=(e,i,t)=>{const n=K(e),o=je(e),r=new Map,a=(d,u)=>{if(d?.channel?.kind!=="room")return;const m=r.get(d.channel.name);if(m!==void 0&&m!==u)throw new g("DUPLICATE_ROOM_NAME",`Duplicate room name "${d.channel.name}" in ${i}: two defineRoom calls share it. Room names must be unique.`);r.set(d.channel.name,u)},c=e.getVariableStatements().flatMap(d=>d.getDeclarations()).flatMap(d=>_e(d,u=>n.has(u))).flatMap(([d,u])=>(n.get(d)??[]).filter(m=>O(m)).flatMap(m=>{const p=j(u,m,i,t,o(m));return a(p,u),p?[p]:[]})),l=new Set(c.map(d=>d.exportName));for(const d of e.getVariableStatements().flatMap(u=>u.getDeclarations()))for(const[u,m]of Re(d,p=>n.has(p)))for(const p of(n.get(u)??[]).filter(v=>O(v)&&!l.has(v))){const v=Ie(m,p,i,o(p));v&&(c.push(v),l.add(p))}for(const{call:d,entry:u}of Be(e,i,t))l.has(u.exportName)||(a(u,d),c.push(u));return c},He=e=>{const i=new Map;for(const t of e){if(t.channel?.kind!=="room")continue;const n=i.get(t.channel.name);if(n!==void 0&&n.file!==t.filePath)throw new g("DUPLICATE_ROOM_NAME",`Duplicate room name "${t.channel.name}": defined in ${n.origin} and ${t.filePath}:${t.exportName}. Room names must be unique across the project.`);n===void 0&&i.set(t.channel.name,{file:t.filePath,origin:`${t.filePath}:${t.exportName}`})}},Me=e=>{const i=new Map;for(const t of e){const n=ie(t.filePath),o=i.get(n);if(o&&o!==t.filePath)throw Object.assign(new Error(`Namespace collision: "${o}" and "${t.filePath}" both resolve to "${n}". Rename one of the files so the JS-identifier-sanitized names differ. (note: case-insensitive filesystems may also cause this — \`foo\` and \`FOO\` map to the same identifier)`),{code:"NAMESPACE_COLLISION",name:"LunoraError",paths:[o,t.filePath],status:500});i.set(n,t.filePath)}},nt=(e,i,t={})=>{const n=M(i),o=[];for(const r of n){const a=e.getSourceFile(r)??e.addSourceFileAtPath(r),c=W(i,r);o.push(...ze(a,c,t.lazyFunctions===!0))}return o.sort((r,a)=>`${r.filePath}:${r.exportName}`.localeCompare(`${a.filePath}:${a.exportName}`)),Me(o),He(o),o};export{nt as d,I as i,ne as p,it as w};
@@ -0,0 +1 @@
1
+ import{Node as n,SyntaxKind as d,VariableDeclarationKind as Be}from"ts-morph";import{isConstDeclaration as K,unwrapExpression as l,descendantsOfKind as Fe,bindingKeyName as ue,findObjectProperty as Ce}from"./isTestPath-Uwb5a6Kq.mjs";import{r as Le,b as V,d as p}from"./attribution-Cwt30DDJ.mjs";import{i as ae,r as j,c as m,a as ze}from"./context-root-BpDEhvNE.mjs";import{a as M,s as Z}from"./property-name-7cBlgK_O.mjs";const Re=e=>{const t=e.getText(),s=i=>{const r=i.getNameNode();return n.isIdentifier(r)&&r.getText()===t};for(const i of e.getAncestors())if(!(!n.isArrowFunction(i)&&!n.isFunctionExpression(i)&&!n.isFunctionDeclaration(i)))for(const r of i.getParameters()){const o=r.getDescendantsOfKind(d.BindingElement).find(a=>s(a));if(o!==void 0)return o}},ke=e=>{let t=e;for(;;){const s=t.getParent();if(!n.isObjectBindingPattern(s))return;const i=s.getParent();if(n.isBindingElement(i)){t=i;continue}return n.isParameterDeclaration(i)?ue(t):void 0}},Ke=e=>{const t=e.getText();if(!e.getAncestors().some(o=>{const a=n.isFunctionLikeDeclaration(o)||n.isArrowFunction(o)?o.getParameters()[1]:void 0,c=a?.getNameNode();return a!==void 0&&(n.isIdentifier(c)?c.getText()===t:a.getDescendantsOfKind(d.BindingElement).some(u=>u.getName()===t))&&ae(a)}))return!1;const i=p(e),r=n.isParameterDeclaration(i)?i:i?.getFirstAncestorByKind(d.Parameter);return r!==void 0&&(n.isParameterDeclaration(i)||n.isBindingElement(i))&&ae(r)},ce=(e,t)=>{const s=e.getParent();if(n.isPropertyAccessExpression(s)&&s.getNameNode()===e||n.isPropertyAssignment(s)&&s.getNameNode()===e)return!1;if(e.getText()===t)return!0;const i=Re(e);return i!==void 0&&ke(i)===t?!0:t==="args"&&Ke(e)},L=(e,t)=>n.isIdentifier(e)?ce(e,t):e.getDescendantsOfKind(d.Identifier).some(s=>ce(s,t)),Ve=e=>{let t=e;for(;n.isPropertyAccessExpression(t)||n.isElementAccessExpression(t)||n.isNonNullExpression(t);)t=t.getExpression();return n.isIdentifier(t)?t:void 0},f=e=>{const t=n.isIdentifier(e)?p(e):void 0;return n.isVariableDeclaration(t)&&n.isIdentifier(t.getNameNode())&&!V(t)?t.getInitializer():void 0},Me=(e,t)=>{const s=e.getParent();if(n.isVariableDeclarationList(s)&&s.getDeclarationKind()===Be.Var)return t;const i=s.getParent();return n.isVariableStatement(i)?i.getParent():i},We=(e,t)=>{const s=e.getNameNode();return n.isIdentifier(s)?s.getText()===t:s.getDescendantsOfKind(d.BindingElement).some(i=>{const r=i.getNameNode();return n.isIdentifier(r)&&r.getText()===t})},J=e=>e.getDescendantsOfKind(d.Identifier).filter(t=>{const s=t.getParent();return!(n.isPropertyAccessExpression(s)&&s.getNameNode()===t)&&!(n.isPropertyAssignment(s)&&s.getNameNode()===t)}),O=e=>n.isIdentifier(e)?[e]:J(e),T=e=>{let t=e;for(;;){const s=t.getParent();if(!((n.isPropertyAccessExpression(s)||n.isElementAccessExpression(s)||n.isNonNullExpression(s))&&s.getExpression()===t))return t;t=s}},_e=e=>{const t=e.getParent();return n.isCallExpression(t)&&t.getExpression()===e?t:void 0},je=new Set(["find","findFirst","findFirstOrThrow","findMany","findUnique","first","get","unique"]),U=new Set(["findFirst","findUnique","first","get","unique"]),W=e=>{const t=e.getExpression();return n.isPropertyAccessExpression(t)&&je.has(t.getName())&&m(t)?.[0]==="db"},I=(e,t)=>{for(let s=e,i=e.getParent();i!==void 0&&s!==t;s=i,i=i.getParent()){if(n.isConditionalExpression(i))return!0;if(n.isBinaryExpression(i)){const r=i.getOperatorToken().getKind();if(s===i.getRight()&&(r===d.QuestionQuestionToken||r===d.BarBarToken)||s===i.getLeft()&&r===d.AmpersandAmpersandToken)return!0}}return!1},ge=e=>O(e).some(t=>{if(I(t,e))return!1;if(m(T(t))?.[0]==="auth")return!0;const s=f(t);return s!==void 0&&O(s).some(i=>m(T(i))?.[0]==="auth"&&!I(i,s))}),qe=e=>W(e)&&e.getArguments().some(t=>ge(t)),fe=(e,t)=>{for(let s=e.getParent();s!==void 0;s=s.getParent()){if(n.isCallExpression(s)&&m(s.getExpression())?.[0]==="db")return!0;if(s===t)return!1}return!1},Ue=new Set(["_id","id"]),X=e=>{let t=e;for(let s=t.getParent();;s=t.getParent()){if(n.isAwaitExpression(s)||n.isParenthesizedExpression(s)||n.isNonNullExpression(s)||n.isAsExpression(s)||n.isSatisfiesExpression(s)||n.isTypeAssertion(s)){t=s;continue}if(n.isPropertyAccessExpression(s)&&s.getExpression()===t)return{field:s.getName(),read:s};if(n.isElementAccessExpression(s)&&s.getExpression()===t){const i=l(s.getArgumentExpression());return{field:n.isStringLiteral(i)||n.isNoSubstitutionTemplateLiteral(i)?i.getLiteralValue():void 0,read:s}}return{field:void 0,read:t}}},$="userId",y=e=>{let t=e;for(;;){const s=n.isNonNullExpression(t)||n.isAwaitExpression(t)?t.getExpression():l(t);if(s===void 0||s===t)return t;t=s}},G=e=>{const t=m(y(e));return t?.length===2&&t[0]==="auth"&&t[1]===$},ee=e=>n.isPropertyAssignment(e)?e.getInitializer():n.isShorthandPropertyAssignment(e)?e.getNameNode():void 0,b=e=>{let t=e;for(let s=t.getParent();;s=t.getParent()){if(!(n.isParenthesizedExpression(s)||n.isNonNullExpression(s)||n.isAsExpression(s)||n.isSatisfiesExpression(s)||n.isTypeAssertion(s)))return t;t=s}},pe=e=>{const t=p(e);return e.getSourceFile().getDescendantsOfKind(d.Identifier).filter(s=>s.getText()===e.getText()&&p(s)===t).filter(s=>{const i=s.getParent();return!((n.isVariableDeclaration(i)||n.isBindingElement(i))&&i.getNameNode()===s)})},le=e=>{const t=b(e).getParent();return n.isCallExpression(t)&&W(t)&&t.getArguments().includes(b(e))},Ge=e=>pe(e).every(t=>{if(le(t))return!0;const s=b(t),i=s.getParent(),r=i?.getParent();return(n.isPropertyAssignment(i)&&i.getNameNode()!==s&&M(i)==="where"||n.isShorthandPropertyAssignment(i)&&i.getName()==="where")&&r!==void 0&&le(r)}),z=e=>{const t=l(e);if(n.isObjectLiteralExpression(t))return t;const s=n.isIdentifier(t)&&Ge(t)?l(f(t)):void 0;return n.isObjectLiteralExpression(s)?s:void 0},He=e=>{const t=l(e)??e;if(!n.isObjectLiteralExpression(t))return G(e);const[s,...i]=t.getProperties(),r=i.length===0&&n.isPropertyAssignment(s)&&M(s)==="eq"?s.getInitializer():void 0;return r!==void 0&&G(r)},te=(e,t)=>{const s=e.getProperties(),i=n.isSpreadAssignment(t)?void 0:M(t);return s.slice(s.indexOf(t)+1).some(r=>{if(n.isSpreadAssignment(r))return!0;const o=Z(r.getNameNode());return o===void 0||o===i})},se=e=>e.getProperties().some(t=>n.isGetAccessorDeclaration(t)||n.isSetAccessorDeclaration(t)||n.isMethodDeclaration(t)),me=(e,t)=>!se(e)&&e.getProperties().some(s=>{const i=ee(s);if(i===void 0||n.isSpreadAssignment(s)||te(e,s))return!1;const r=Z(s.getNameNode());if(r===void 0)return!1;if(r==="AND"){const o=l(i);return n.isArrayLiteralExpression(o)&&o.getElements().some(a=>{const c=z(a);return c!==void 0&&me(c,t)})}return(t===void 0||r===t)&&He(i)}),Ee=(e,t)=>{const s=z(e),i=s===void 0?void 0:Ce(s,"where"),r=s===void 0||i===void 0||se(s)||te(s,i)?void 0:z(ee(i));return r!==void 0&&me(r,t)},ve=e=>e.getArguments().some(t=>G(t)||Ee(t,void 0)),Qe=(e,t)=>t===void 0?!1:Ue.has(t)?ve(e):e.getArguments().some(s=>Ee(s,t)),Ye=new Set(["_creationTime","_id"]),Ze=(e,t)=>Ye.has(t)||e.getArguments().every(s=>{const i=z(s);if(i===void 0)return!0;const r=i.getProperties(),o=r.findLast(c=>!n.isSpreadAssignment(c)&&M(c)==="select");if(se(i))return!1;if(o===void 0)return!r.some(c=>n.isSpreadAssignment(c)||Z(c.getNameNode())===void 0);if(te(i,o))return!1;const a=l(ee(o));return n.isArrayLiteralExpression(a)&&a.getElements().some(c=>(n.isStringLiteral(c)||n.isNoSubstitutionTemplateLiteral(c))&&c.getLiteralValue()===t)}),w=(e,t,s,i)=>{const r=e.getExpression(),o=n.isPropertyAccessExpression(r)&&(U.has(r.getName())||r.getName().endsWith("OrThrow"));return!qe(e)||!o||s!==void 0&&!Ze(e,s)?!1:i!==void 0&&l(t)===l(i)&&ve(e)||Qe(e,s)},ie=(e,t)=>{const s=t.getExpression(),i=n.isPropertyAccessExpression(s)&&s.getName().endsWith("OrThrow");return(n.isPropertyAccessExpression(e)||n.isElementAccessExpression(e))&&e.hasQuestionDotToken()&&!i},xe=(e,t)=>O(e).some(s=>{const i=T(s),r=m(i)?.[0];if(r==="auth")return!I(s,e)&&!fe(s,e);const o=r==="db"&&!I(s,e)?_e(i):void 0;if(o===void 0)return!1;const{field:a,read:c}=X(o);return!ie(c,o)&&w(o,c,a,t)}),H=e=>n.isArrowFunction(e)||n.isFunctionExpression(e)||n.isFunctionDeclaration(e)||n.isMethodDeclaration(e),Q=e=>{const t=e.getFirstAncestor(s=>H(s));return H(t)?t:void 0},Je=e=>{const t=l(e);return n.isAwaitExpression(t)?l(t.getExpression()):t},D=e=>{const t=[];let s=l(e);for(;s!==void 0;)if(n.isPropertyAccessExpression(s))t.unshift(s.getName()),s=l(s.getExpression());else if(n.isElementAccessExpression(s)){const i=s.getArgumentExpression();if(!n.isStringLiteral(i))return;t.unshift(i.getLiteralValue()),s=l(s.getExpression())}else return n.isIdentifier(s)?{path:t,root:s}:void 0},E=(e,t)=>p(e)?.compilerNode===t.compilerNode,Ae=e=>{if(!n.isIdentifier(e))return;const t=Le(e),s=t?.isAlias()===!0?t.getAliasedSymbol():t,i=s?.getValueDeclaration()??s?.getDeclarations()[0];if(i===void 0)return;const r=i.getSourceFile();if(r.isDeclarationFile()||r.isInNodeModules())return;if(n.isFunctionDeclaration(i))return i.getBody()===void 0||i.isGenerator()||V(i)?void 0:i;if(!K(i))return;const o=l(i.getInitializer());return n.isArrowFunction(o)||n.isFunctionExpression(o)&&!o.isGenerator()?o:void 0},Xe=e=>e>=d.FirstAssignment&&e<=d.LastAssignment,ne=e=>{const t=[],s=[];let i=l(e);for(;i!==void 0;)if(n.isCallExpression(i))t.push(i),i=l(i.getExpression());else if(n.isPropertyAccessExpression(i))s.unshift(i.getName()),i=l(i.getExpression());else if(n.isElementAccessExpression(i)){const r=i.getArgumentExpression();if(!n.isStringLiteral(r))return;s.unshift(r.getLiteralValue()),i=l(i.getExpression())}else return n.isIdentifier(i)&&t.length>0&&s.length>0?{calls:t,names:s,root:i}:void 0},re=e=>{const t=e.names[e.names.length-1]??"";if(U.has(t))return{selfEnforcing:!1};const s=t.endsWith("OrThrow")?t.slice(0,-7):void 0;return s!==void 0&&U.has(s)?{selfEnforcing:!0}:void 0},$e=e=>{const t=l(e),s=n.isIdentifier(t)?l(f(t))??t:t,i=D(s);if(i===void 0||i.path.length===0)return;const r=p(i.root);if(!K(r)||!n.isIdentifier(r.getNameNode()))return;const o=Je(r.getInitializer()),a=n.isCallExpression(o)&&m(o.getExpression())?.[0]==="db"?ne(o):void 0;return a!==void 0&&re(a)!==void 0?r:void 0},Ne=e=>e.calls.flatMap(t=>t.getArguments().flatMap(s=>[s,...s.getDescendants()])),et=(e,t)=>{const s=n.isIdentifier(e)?p(e):void 0;if(!n.isBindingElement(s)||s.getDotDotDotToken()!==void 0||s.getInitializer()!==void 0)return!1;const i=s.getParent(),r=n.isObjectBindingPattern(i)?i.getParent():void 0;if(!K(r))return!1;const o=D(r.getInitializer());return o?.path[0]==="auth"&&E(o.root,t)},tt=(e,t)=>{const s=D(e);if(s!==void 0&&s.path.length>0)return s.path[0]==="auth"&&E(s.root,t);const i=D(f(e));return i?.path[0]==="auth"&&E(i.root,t)?!0:et(e,t)},he=e=>{if(!n.isVariableStatement(e))return;const t=e.getDeclarations(),s=t.length===1?t[0]:void 0;return s!==void 0&&K(s)&&n.isIdentifier(s.getNameNode())?s:void 0},st=(e,t)=>{const s=l(e),i=a=>n.isIdentifier(a)&&E(a,t);if(n.isPrefixUnaryExpression(s))return s.getOperatorToken()===d.ExclamationToken&&i(l(s.getOperand()));if(!n.isBinaryExpression(s)||s.getOperatorToken().getKind()!==d.EqualsEqualsToken)return!1;const r=l(s.getLeft()),o=l(s.getRight());return i(r)&&n.isNullLiteral(o)||i(o)&&n.isNullLiteral(r)},it=(e,t)=>{const s=n.isBlock(e)?e.getStatements():void 0,i=s===void 0?e:s[s.length-1];return n.isThrowStatement(i)||!t&&n.isReturnStatement(i)},Pe=(e,t,s)=>n.isIfStatement(e)&&e.getElseStatement()===void 0&&st(e.getExpression(),t)&&it(e.getThenStatement(),s),nt=(e,t,s)=>{const i=e.getBody();if(!n.isBlock(i))return!1;const r=i.getStatements();return r.some((o,a)=>{const c=he(o),u=l(c?.getInitializer()),g=n.isAwaitExpression(u)?ne(u.getExpression()):void 0,A=g===void 0?void 0:re(g);if(c===void 0||g===void 0||A===void 0||g.names[0]!=="db"||!E(g.root,t))return!1;const v=Ne(g),h=v.some(x=>tt(x,t)),F=s.some(x=>v.some(oe=>n.isIdentifier(oe)&&E(oe,x)));if(!h||!F)return!1;const P=A.selfEnforcing?o:r.slice(a+1).find(x=>Pe(x,c,!0));return P!==void 0&&!e.getDescendantsOfKind(d.ReturnStatement).some(x=>Q(x)?.compilerNode===e.compilerNode&&x.getStart()<P.getEnd())})},rt=(e,t,s)=>{const i=Ae(e.getExpression()),r=e.getArguments();if(i===void 0||r.some(v=>n.isSpreadElement(v)))return!1;const o=r.flatMap((v,h)=>m(v)?.length===0?[h]:[]),a=o.length===1?o[0]:void 0,c=a===void 0?void 0:i.getParameters()[a];if(a===void 0||c===void 0||c.isRestParameter()||!n.isIdentifier(c.getNameNode()))return!1;const u=[];r.forEach((v,h)=>{const F=D(l(v)),P=h===a||F===void 0||!E(F.root,t)?void 0:i.getParameters()[h];P!==void 0&&n.isIdentifier(P.getNameNode())&&u.push(P)});const g=i.getReturnType(),A=i.isAsync()||g.isAny()||g.isUnknown()||g.getText().includes("Promise");return u.length===0||!s&&A?!1:nt(i,c,u)},ot=(e,t)=>{const s=he(e),i=l(s?.getInitializer()),r=n.isAwaitExpression(i)?ne(i.getExpression()):void 0,o=r===void 0?void 0:re(r);if(s===void 0||r===void 0||o===void 0||r.names[0]!=="db"||m(r.root)?.length!==0)return;const a=r.calls.some(u=>u.getArguments().some(g=>ge(g))),c=Ne(r).some(u=>n.isIdentifier(u)&&E(u,t));return a&&c?{declaration:s,selfEnforcing:o.selfEnforcing}:void 0},at=(e,t,s)=>{const i=(r,o)=>o.getStart()<=r.getStart()&&r.getEnd()<=o.getEnd();for(let r=e.getParent();r!==void 0&&r.compilerNode!==t.compilerNode;r=r.getParent()){if(H(r)||n.isIfStatement(r)||n.isIterationStatement(r)||n.isCaseClause(r)||n.isDefaultClause(r)||n.isCatchClause(r))return!1;if(n.isTryStatement(r)&&r.getCatchClause()!==void 0){const o=r.getTryBlock();if(!i(e,o)||!i(s,o))return!1}}return!0},ct=(e,t,s,i,r)=>e.slice(0,t).some((o,a)=>{if(!at(o,i,r))return!1;if(n.isExpressionStatement(o)){const u=l(o.getExpression()),g=n.isAwaitExpression(u),A=g?l(u.getExpression()):u;if(n.isCallExpression(A)&&rt(A,s,g))return!0}const c=ot(o,s);return c!==void 0&&(c.selfEnforcing||a+1<t&&Pe(e[a+1],c.declaration,!1))}),_=e=>{let t=l(e)??e;for(;n.isAwaitExpression(t);)t=l(t.getExpression())??t.getExpression();return t},C=e=>{const t=_(e);return n.isCallExpression(t)||n.isNewExpression(t)},lt=e=>{const t=_(e);return n.isCallExpression(t)&&m(t.getExpression())?.[0]==="db"},Ie=e=>{const t=_(e);return n.isCallExpression(t)&&W(t)},ye=e=>L(e,"args")||O(e).some(t=>{const s=f(t);return s!==void 0&&L(s,"args")}),R=(e,t)=>{const s=e?.getNameNode().getDescendantsOfKind(d.BindingElement).find(i=>n.isIdentifier(i.getNameNode())&&i.getNameNode().getText()===t);return s===void 0?[]:[s,...s.getAncestors().filter(i=>n.isBindingElement(i))].map(i=>n.isBindingElement(i)?i.getInitializer():void 0).filter(i=>i!==void 0)},De=e=>{const t=n.isIdentifier(e)?p(e):void 0;if(!n.isBindingElement(t)||V(t))return;const s=t.getFirstAncestorByKind(d.VariableDeclaration),i=s?.getInitializer();if(!R(s,e.getText()).some(r=>ye(r)))return i!==void 0&&Ie(i)?i:void 0},B=e=>{const t=_(e);return n.isCallExpression(t)&&W(t)?t:void 0},Se=e=>{if(!(!n.isBindingElement(e)||!n.isObjectBindingPattern(e.getParent())||e.getDotDotDotToken()!==void 0||n.isBindingElement(e.getParent().getParent())))return ue(e)},dt=e=>{let t=e,s=t.getParent();for(;n.isParenthesizedExpression(s)||n.isArrayLiteralExpression(s)||n.isObjectLiteralExpression(s)||n.isSpreadElement(s)||n.isSpreadAssignment(s)||n.isShorthandPropertyAssignment(s)||n.isPropertyAssignment(s)&&s.getNameNode()!==t;)t=s,s=t.getParent();if(n.isBinaryExpression(s)){const i=s.getOperatorToken().getKind();return s.getLeft()===t&&i>=d.FirstAssignment&&i<=d.LastAssignment}if(n.isForOfStatement(s)||n.isForInStatement(s))return s.getInitializer()===t;if(n.isPrefixUnaryExpression(s)||n.isPostfixUnaryExpression(s)){const i=s.getOperatorToken();return i===d.PlusPlusToken||i===d.MinusMinusToken}return n.isDeleteExpression(s)},Oe=e=>pe(e).every(t=>{const s=b(t),i=s.getParent();return n.isVariableDeclaration(i)&&n.isObjectBindingPattern(i.getNameNode())||n.isSpreadAssignment(i)?!0:(n.isPropertyAccessExpression(i)||n.isElementAccessExpression(i))&&i.getExpression()===s&&!dt(b(T(i)))}),de=(e,t)=>{const s=t!==void 0&&l(e)===l(t),i=f(e);if(i!==void 0){const a=B(i);if(a===void 0)return xe(i,s?i:void 0);if(n.isIdentifier(e)&&!Oe(e))return!1;const{field:c,read:u}=X(e);return ie(u,a)?!1:w(a,u,c,t)}const r=De(e),o=r===void 0?void 0:B(r);return o!==void 0&&n.isIdentifier(e)&&w(o,e,Se(p(e)),t)},S=(e,t=!0)=>{const s=y(e);if(n.isTemplateExpression(s))return s.getTemplateSpans().flatMap(o=>S(o.getExpression(),t));if(n.isBinaryExpression(s)&&s.getOperatorToken().getKind()===d.PlusToken)return[...S(s.getLeft(),t),...S(s.getRight(),t)];const i=t&&n.isIdentifier(s)?f(s):void 0,r=i===void 0?void 0:y(i);return r!==void 0&&(n.isTemplateExpression(r)||n.isBinaryExpression(r)&&r.getOperatorToken().getKind()===d.PlusToken)?S(r,!1):[s]},ut=e=>n.isStringLiteral(e)||n.isNoSubstitutionTemplateLiteral(e)||n.isNumericLiteral(e),gt=e=>{const t=y(e.getExpression()),s=n.isIdentifier(t)?f(t):void 0;if(n.isIdentifier(t)&&s!==void 0&&!Oe(t))return!1;const i=B(t)??(s===void 0?void 0:B(s));return i!==void 0&&!ie(e,i)&&w(i,e,X(e.getExpression()).field,void 0)},ft=e=>{const t=p(e),s=Se(t),i=n.isBindingElement(t)?t.getFirstAncestorByKind(d.VariableDeclaration)?.getInitializer():void 0,r=i===void 0?void 0:m(y(i));if(r!==void 0)return r.length===1&&r[0]==="auth"&&s===$;const o=De(e),a=o===void 0?void 0:B(o);return a!==void 0&&w(a,e,s,void 0)},Te=(e,t=!0)=>{const s=y(e),i=n.isIdentifier(s)?p(s):void 0;if((n.isVariableDeclaration(i)||n.isBindingElement(i))&&V(i))return!1;const r=m(s);if(r!==void 0)return r.length===2&&r[0]==="auth"&&r[1]===$;if(n.isPropertyAccessExpression(s)||n.isElementAccessExpression(s))return gt(s);if(!n.isIdentifier(s))return!1;const o=f(s);return o===void 0?ft(s):t&&Te(o,!1)},pt=(e,t)=>{const s=S(t),i=s.findIndex(a=>!ut(a)),r=s.findIndex(a=>a.getPos()<=e.getPos()&&e.getEnd()<=a.getEnd()),o=s[i];return o!==void 0&&i<r&&Te(o)},N=e=>L(e,"args"),k=e=>{if(!n.isIdentifier(e))return;const t=e.getText(),s=e.getFirstAncestor(a=>n.isArrowFunction(a)||n.isFunctionExpression(a)||n.isFunctionDeclaration(a));if(s===void 0)return;const i=e.getStart();let r,o=-1;for(const a of Fe(s,d.VariableDeclaration)){if(!We(a,t))continue;const c=Me(a,s);if(c===void 0||i<c.getStart()||i>=c.getEnd())continue;const u=a.getInitializer(),g=a.getStart();u!==void 0&&g<i&&g>o&&(r=a,o=g)}return r},Y=e=>k(e)?.getInitializer(),Bt=e=>{if(N(e))return!0;const t=s=>{const i=k(s),r=i?.getInitializer();return r!==void 0&&N(r)||R(i,s.getText()).some(o=>N(o))};return t(e)||J(e).some(s=>t(s))},Ft=e=>{if(C(e))return!1;if(R(k(e),e.getText()).some(i=>N(i)&&!C(i)))return!0;const t=Y(e);if(t!==void 0&&C(t)&&!(Ie(t)&&N(t)))return!1;const s=t??e;return N(s)?!0:J(s).some(i=>{if(i.getFirstAncestor(c=>c===s||n.isCallExpression(c)||n.isNewExpression(c))!==s)return!1;const o=k(i),a=c=>c!==void 0&&N(c)&&(!C(c)||lt(c));return a(o?.getInitializer())||R(o,i.getText()).some(c=>a(c))})},Ct=e=>{if(j(e))return!0;const t=f(e);if(t!==void 0&&j(t))return!0;const s=t??e;return(n.isIdentifier(s)?[s]:s.getDescendantsOfKind(d.Identifier)).some(r=>{const o=f(r);return o!==void 0&&j(o)})},Lt=e=>{const t=f(e)??e;if(O(t).some(r=>I(r,t)&&ye(T(r))&&!pt(r,t)))return!1;if(xe(e,e)||de(e,e))return!0;const s=f(e)??e;return(n.isIdentifier(s)?[s]:s.getDescendantsOfKind(d.Identifier)).some(r=>!I(r,s)&&!fe(r,s)&&de(r,s))},q=(e,t)=>L(e,t),zt=(e,t)=>{if(q(e,t))return!0;const s=Y(e);if(s!==void 0&&q(s,t))return!0;const i=Ve(e);if(i!==void 0){const r=Y(i);return r!==void 0&&q(r,t)}return!1},Rt=(e,t)=>{const s=Q(e),i=$e(t);if(s===void 0||i===void 0||Q(i)?.compilerNode!==s.compilerNode||s.getDescendantsOfKind(d.BinaryExpression).some(c=>{const u=D(c.getLeft());return Xe(c.getOperatorToken().getKind())&&u!==void 0&&E(u.root,i)}))return!1;let o=e,a=e.getParent();for(;a!==void 0&&a.compilerNode!==s.compilerNode;){if(n.isBlock(a)){const c=a.getStatements(),u=c.findIndex(g=>g.compilerNode===o.compilerNode);if(u>0&&ct(c,u,i,s,e))return!0}o=a,a=a.getParent()}return!1},mt=/rate|limit|\bip\b|geo|throttle/iu,Et=/^run[A-Z]/u,vt=e=>n.isIdentifier(e)?(e.getSymbol()?.getDeclarations()??[]).some(s=>s.getFirstAncestorByKind(d.ImportDeclaration)?.getModuleSpecifierValue().startsWith("@lunora/")===!0):!1,xt=e=>{const t=n.isCallExpression(e)?e.getExpression():e;let s;return n.isIdentifier(t)?s=t:n.isPropertyAccessExpression(t)&&(s=t.getNameNode()),s!==void 0&&vt(n.isPropertyAccessExpression(t)?t.getExpression():s)&&!mt.test(s.getText())},At=e=>{const t=n.isCallExpression(e)?e.getExpression():void 0;if(n.isElementAccessExpression(t)){const s=t.getArgumentExpression();return(n.isStringLiteral(s)||n.isNoSubstitutionTemplateLiteral(s))&&s.getLiteralText()==="use"}return n.isPropertyAccessExpression(t)&&t.getName()==="use"},Nt=e=>{const t=e.getParent();return n.isPropertyAccessExpression(t)&&t.getExpression()===e?!Et.test(t.getName()):n.isElementAccessExpression(t)&&t.getExpression()===e?n.isStringLiteral(t.getArgumentExpression())||n.isNoSubstitutionTemplateLiteral(t.getArgumentExpression()):n.isVariableDeclaration(t)&&t.getInitializer()?.getText()===e.getText()},ht=e=>{if(n.isPropertyAccessExpression(e))return e.getName()==="ip";if(n.isElementAccessExpression(e)){const t=e.getArgumentExpression();return(n.isStringLiteral(t)||n.isNoSubstitutionTemplateLiteral(t))&&t.getLiteralText()==="ip"}return n.isBindingElement(e)?e.getDotDotDotToken()!==void 0||(e.getPropertyNameNode()?.getText()??e.getName())==="ip":n.isShorthandPropertyAssignment(e)&&e.getName()==="ip"},Pt=(e,t)=>{const s=e.getParent();if(!n.isCallExpression(s)||s.getExpression()===e)return!1;const i=s.getArguments(),r=i.indexOf(e);if(i.slice(0,Math.max(r,0)).some(c=>n.isSpreadElement(c)))return!1;const o=Ae(s.getExpression()),a=r===-1||o===void 0?void 0:o.getParameters()[r];return o===void 0||a===void 0||a.isRestParameter()||!n.isIdentifier(a.getNameNode())?!1:t.has(a.compilerNode)?!0:(t.add(a.compilerNode),!we(o,a,t))},be=(e,t,s=new Set)=>{if(t===void 0||!n.isIdentifier(e))return!1;const i=p(e);if(i?.compilerNode===t.compilerNode)return!0;if(i===void 0||s.has(i.compilerNode))return!1;s.add(i.compilerNode);const r=n.isVariableDeclaration(i)&&n.isIdentifier(i.getNameNode())?i.getInitializer():void 0;return r!==void 0&&be(r,t,s)},It=e=>{const t=e.getParent();return n.isParameterDeclaration(t)||n.isBindingElement(t)||n.isVariableDeclaration(t)?t.getInitializer()!==e:!1},yt=e=>n.isArrowFunction(e)||n.isFunctionExpression(e)||n.isFunctionDeclaration(e)||n.isMethodDeclaration(e),Dt=(e,t)=>!n.isBindingElement(e)||e.getDotDotDotToken()===void 0?!1:e.getFirstAncestorByKind(d.Parameter)?.getParent()?.getAncestors().some(r=>r!==t&&yt(r)&&r.getAncestors().includes(t))===!0,we=(e,t,s)=>{for(const i of e.getDescendants())if(ht(i)&&!Dt(i,e)||At(i)&&!xt(i.getArguments()[0])||n.isIdentifier(i)&&(ze(i)||be(i,t))&&!Nt(i)&&!It(i)&&!Pt(i,s))return!0;return!1},kt=e=>we(e,void 0,new Set);export{Ct as a,Ft as b,Lt as c,Rt as d,zt as e,q as f,k as g,Bt as i,kt as m,N as r,Y as s};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lunora/codegen",
3
- "version": "1.0.0-alpha.420",
3
+ "version": "1.0.0-alpha.422",
4
4
  "description": "Code generator for Lunora: emits _generated/{api,server,dataModel}.ts from your schema",
5
5
  "keywords": [
6
6
  "cloudflare",
@@ -1 +0,0 @@
1
- import{Node as s,SyntaxKind as u,VariableDeclarationKind as re}from"ts-morph";import{isConstDeclaration as F,unwrapExpression as d,descendantsOfKind as oe,bindingKeyName as ae}from"./isTestPath-Uwb5a6Kq.mjs";import{r as ce,b as V,d as N}from"./attribution-Cwt30DDJ.mjs";import{i as _,r as C,c as m,a as le}from"./context-root-BpDEhvNE.mjs";const de=e=>{const t=e.getText(),i=n=>{const r=n.getNameNode();return s.isIdentifier(r)&&r.getText()===t};for(const n of e.getAncestors())if(!(!s.isArrowFunction(n)&&!s.isFunctionExpression(n)&&!s.isFunctionDeclaration(n)))for(const r of n.getParameters()){const o=r.getDescendantsOfKind(u.BindingElement).find(a=>i(a));if(o!==void 0)return o}},ue=e=>{let t=e;for(;;){const i=t.getParent();if(!s.isObjectBindingPattern(i))return;const n=i.getParent();if(s.isBindingElement(n)){t=n;continue}return s.isParameterDeclaration(n)?ae(t):void 0}},ge=e=>{const t=e.getText();if(!e.getAncestors().some(o=>{const a=s.isFunctionLikeDeclaration(o)||s.isArrowFunction(o)?o.getParameters()[1]:void 0,c=a?.getNameNode();return a!==void 0&&(s.isIdentifier(c)?c.getText()===t:a.getDescendantsOfKind(u.BindingElement).some(l=>l.getName()===t))&&_(a)}))return!1;const n=N(e),r=s.isParameterDeclaration(n)?n:n?.getFirstAncestorByKind(u.Parameter);return r!==void 0&&(s.isParameterDeclaration(n)||s.isBindingElement(n))&&_(r)},q=(e,t)=>{const i=e.getParent();if(s.isPropertyAccessExpression(i)&&i.getNameNode()===e||s.isPropertyAssignment(i)&&i.getNameNode()===e)return!1;if(e.getText()===t)return!0;const n=de(e);return n!==void 0&&ue(n)===t?!0:t==="args"&&ge(e)},T=(e,t)=>s.isIdentifier(e)?q(e,t):e.getDescendantsOfKind(u.Identifier).some(i=>q(i,t)),fe=e=>{let t=e;for(;s.isPropertyAccessExpression(t)||s.isElementAccessExpression(t)||s.isNonNullExpression(t);)t=t.getExpression();return s.isIdentifier(t)?t:void 0},f=e=>{const t=s.isIdentifier(e)?N(e):void 0;return s.isVariableDeclaration(t)&&s.isIdentifier(t.getNameNode())&&!V(t)?t.getInitializer():void 0},me=(e,t)=>{const i=e.getParent();if(s.isVariableDeclarationList(i)&&i.getDeclarationKind()===re.Var)return t;const n=i.getParent();return s.isVariableStatement(n)?n.getParent():n},pe=(e,t)=>{const i=e.getNameNode();return s.isIdentifier(i)?i.getText()===t:i.getDescendantsOfKind(u.BindingElement).some(n=>{const r=n.getNameNode();return s.isIdentifier(r)&&r.getText()===t})},M=e=>e.getDescendantsOfKind(u.Identifier).filter(t=>{const i=t.getParent();return!(s.isPropertyAccessExpression(i)&&i.getNameNode()===t)&&!(s.isPropertyAssignment(i)&&i.getNameNode()===t)}),y=e=>s.isIdentifier(e)?[e]:M(e),b=e=>{let t=e;for(;;){const i=t.getParent();if(!((s.isPropertyAccessExpression(i)||s.isElementAccessExpression(i)||s.isNonNullExpression(i))&&i.getExpression()===t))return t;t=i}},Ee=e=>{const t=e.getParent();return s.isCallExpression(t)&&t.getExpression()===e?t:void 0},ve=new Set(["find","findFirst","findFirstOrThrow","findMany","findUnique","first","get","unique"]),Z=e=>{const t=e.getExpression();return s.isPropertyAccessExpression(t)&&ve.has(t.getName())&&m(t)?.[0]==="db"},D=(e,t)=>{for(let i=e,n=e.getParent();n!==void 0&&i!==t;i=n,n=n.getParent()){if(s.isConditionalExpression(n))return!0;if(s.isBinaryExpression(n)){const r=n.getOperatorToken().getKind();if(i===n.getRight()&&(r===u.QuestionQuestionToken||r===u.BarBarToken)||i===n.getLeft()&&r===u.AmpersandAmpersandToken)return!0}}return!1},J=e=>y(e).some(t=>{if(D(t,e))return!1;if(m(b(t))?.[0]==="auth")return!0;const i=f(t);return i!==void 0&&y(i).some(n=>m(b(n))?.[0]==="auth"&&!D(n,i))}),xe=e=>Z(e)&&e.getArguments().some(t=>J(t)),X=(e,t)=>{for(let i=e.getParent();i!==void 0;i=i.getParent()){if(s.isCallExpression(i)&&m(i.getExpression())?.[0]==="db")return!0;if(i===t)return!1}return!1},R=e=>y(e).some(t=>{const i=b(t),n=m(i)?.[0];if(n==="auth")return!D(t,e)&&!X(t,e);const r=n==="db"&&!D(t,e)?Ee(i):void 0;return r!==void 0&&xe(r)}),k=e=>s.isArrowFunction(e)||s.isFunctionExpression(e)||s.isFunctionDeclaration(e)||s.isMethodDeclaration(e),K=e=>{const t=e.getFirstAncestor(i=>k(i));return k(t)?t:void 0},Ae=e=>{const t=d(e);return s.isAwaitExpression(t)?d(t.getExpression()):t},P=e=>{const t=[];let i=d(e);for(;i!==void 0;)if(s.isPropertyAccessExpression(i))t.unshift(i.getName()),i=d(i.getExpression());else if(s.isElementAccessExpression(i)){const n=i.getArgumentExpression();if(!s.isStringLiteral(n))return;t.unshift(n.getLiteralValue()),i=d(i.getExpression())}else return s.isIdentifier(i)?{path:t,root:i}:void 0},p=(e,t)=>N(e)?.compilerNode===t.compilerNode,Y=e=>{if(!s.isIdentifier(e))return;const t=ce(e),i=t?.isAlias()===!0?t.getAliasedSymbol():t,n=i?.getValueDeclaration()??i?.getDeclarations()[0];if(n===void 0)return;const r=n.getSourceFile();if(r.isDeclarationFile()||r.isInNodeModules())return;if(s.isFunctionDeclaration(n))return n.getBody()===void 0||n.isGenerator()||V(n)?void 0:n;if(!F(n))return;const o=d(n.getInitializer());return s.isArrowFunction(o)||s.isFunctionExpression(o)&&!o.isGenerator()?o:void 0},Ne=e=>e>=u.FirstAssignment&&e<=u.LastAssignment,H=e=>{const t=[],i=[];let n=d(e);for(;n!==void 0;)if(s.isCallExpression(n))t.push(n),n=d(n.getExpression());else if(s.isPropertyAccessExpression(n))i.unshift(n.getName()),n=d(n.getExpression());else if(s.isElementAccessExpression(n)){const r=n.getArgumentExpression();if(!s.isStringLiteral(r))return;i.unshift(r.getLiteralValue()),n=d(n.getExpression())}else return s.isIdentifier(n)&&t.length>0&&i.length>0?{calls:t,names:i,root:n}:void 0},j=new Set(["findFirst","findUnique","first","get","unique"]),U=e=>{const t=e.names[e.names.length-1]??"";if(j.has(t))return{selfEnforcing:!1};const i=t.endsWith("OrThrow")?t.slice(0,-7):void 0;return i!==void 0&&j.has(i)?{selfEnforcing:!0}:void 0},Ie=e=>{const t=d(e),i=s.isIdentifier(t)?d(f(t))??t:t,n=P(i);if(n===void 0||n.path.length===0)return;const r=N(n.root);if(!F(r)||!s.isIdentifier(r.getNameNode()))return;const o=Ae(r.getInitializer()),a=s.isCallExpression(o)&&m(o.getExpression())?.[0]==="db"?H(o):void 0;return a!==void 0&&U(a)!==void 0?r:void 0},$=e=>e.calls.flatMap(t=>t.getArguments().flatMap(i=>[i,...i.getDescendants()])),he=(e,t)=>{const i=s.isIdentifier(e)?N(e):void 0;if(!s.isBindingElement(i)||i.getDotDotDotToken()!==void 0||i.getInitializer()!==void 0)return!1;const n=i.getParent(),r=s.isObjectBindingPattern(n)?n.getParent():void 0;if(!F(r))return!1;const o=P(r.getInitializer());return o?.path[0]==="auth"&&p(o.root,t)},De=(e,t)=>{const i=P(e);if(i!==void 0&&i.path.length>0)return i.path[0]==="auth"&&p(i.root,t);const n=P(f(e));return n?.path[0]==="auth"&&p(n.root,t)?!0:he(e,t)},ee=e=>{if(!s.isVariableStatement(e))return;const t=e.getDeclarations(),i=t.length===1?t[0]:void 0;return i!==void 0&&F(i)&&s.isIdentifier(i.getNameNode())?i:void 0},Pe=(e,t)=>{const i=d(e),n=a=>s.isIdentifier(a)&&p(a,t);if(s.isPrefixUnaryExpression(i))return i.getOperatorToken()===u.ExclamationToken&&n(d(i.getOperand()));if(!s.isBinaryExpression(i)||i.getOperatorToken().getKind()!==u.EqualsEqualsToken)return!1;const r=d(i.getLeft()),o=d(i.getRight());return n(r)&&s.isNullLiteral(o)||n(o)&&s.isNullLiteral(r)},ye=(e,t)=>{const i=s.isBlock(e)?e.getStatements():void 0,n=i===void 0?e:i[i.length-1];return s.isThrowStatement(n)||!t&&s.isReturnStatement(n)},te=(e,t,i)=>s.isIfStatement(e)&&e.getElseStatement()===void 0&&Pe(e.getExpression(),t)&&ye(e.getThenStatement(),i),Se=(e,t,i)=>{const n=e.getBody();if(!s.isBlock(n))return!1;const r=n.getStatements();return r.some((o,a)=>{const c=ee(o),l=d(c?.getInitializer()),g=s.isAwaitExpression(l)?H(l.getExpression()):void 0,x=g===void 0?void 0:U(g);if(c===void 0||g===void 0||x===void 0||g.names[0]!=="db"||!p(g.root,t))return!1;const E=$(g),I=E.some(v=>De(v,t)),S=i.some(v=>E.some(W=>s.isIdentifier(W)&&p(W,v)));if(!I||!S)return!1;const h=x.selfEnforcing?o:r.slice(a+1).find(v=>te(v,c,!0));return h!==void 0&&!e.getDescendantsOfKind(u.ReturnStatement).some(v=>K(v)?.compilerNode===e.compilerNode&&v.getStart()<h.getEnd())})},Oe=(e,t,i)=>{const n=Y(e.getExpression()),r=e.getArguments();if(n===void 0||r.some(E=>s.isSpreadElement(E)))return!1;const o=r.flatMap((E,I)=>m(E)?.length===0?[I]:[]),a=o.length===1?o[0]:void 0,c=a===void 0?void 0:n.getParameters()[a];if(a===void 0||c===void 0||c.isRestParameter()||!s.isIdentifier(c.getNameNode()))return!1;const l=[];r.forEach((E,I)=>{const S=P(d(E)),h=I===a||S===void 0||!p(S.root,t)?void 0:n.getParameters()[I];h!==void 0&&s.isIdentifier(h.getNameNode())&&l.push(h)});const g=n.getReturnType(),x=n.isAsync()||g.isAny()||g.isUnknown()||g.getText().includes("Promise");return l.length===0||!i&&x?!1:Se(n,c,l)},Te=(e,t)=>{const i=ee(e),n=d(i?.getInitializer()),r=s.isAwaitExpression(n)?H(n.getExpression()):void 0,o=r===void 0?void 0:U(r);if(i===void 0||r===void 0||o===void 0||r.names[0]!=="db"||m(r.root)?.length!==0)return;const a=r.calls.some(l=>l.getArguments().some(g=>J(g))),c=$(r).some(l=>s.isIdentifier(l)&&p(l,t));return a&&c?{declaration:i,selfEnforcing:o.selfEnforcing}:void 0},be=(e,t,i)=>{const n=(r,o)=>o.getStart()<=r.getStart()&&r.getEnd()<=o.getEnd();for(let r=e.getParent();r!==void 0&&r.compilerNode!==t.compilerNode;r=r.getParent()){if(k(r)||s.isIfStatement(r)||s.isIterationStatement(r)||s.isCaseClause(r)||s.isDefaultClause(r)||s.isCatchClause(r))return!1;if(s.isTryStatement(r)&&r.getCatchClause()!==void 0){const o=r.getTryBlock();if(!n(e,o)||!n(i,o))return!1}}return!0},Be=(e,t,i,n,r)=>e.slice(0,t).some((o,a)=>{if(!be(o,n,r))return!1;if(s.isExpressionStatement(o)){const l=d(o.getExpression()),g=s.isAwaitExpression(l),x=g?d(l.getExpression()):l;if(s.isCallExpression(x)&&Oe(x,i,g))return!0}const c=Te(o,i);return c!==void 0&&(c.selfEnforcing||a+1<t&&te(e[a+1],c.declaration,!1))}),G=e=>{let t=d(e)??e;for(;s.isAwaitExpression(t);)t=d(t.getExpression())??t.getExpression();return t},O=e=>{const t=G(e);return s.isCallExpression(t)||s.isNewExpression(t)},we=e=>{const t=G(e);return s.isCallExpression(t)&&m(t.getExpression())?.[0]==="db"},ie=e=>{const t=G(e);return s.isCallExpression(t)&&Z(t)},ne=e=>T(e,"args")||y(e).some(t=>{const i=f(t);return i!==void 0&&T(i,"args")}),B=(e,t)=>{const i=e?.getNameNode().getDescendantsOfKind(u.BindingElement).find(n=>s.isIdentifier(n.getNameNode())&&n.getNameNode().getText()===t);return i===void 0?[]:[i,...i.getAncestors().filter(n=>s.isBindingElement(n))].map(n=>s.isBindingElement(n)?n.getInitializer():void 0).filter(n=>n!==void 0)},Q=e=>{const t=s.isIdentifier(e)?N(e):void 0;if(!s.isBindingElement(t)||V(t))return;const i=t.getFirstAncestorByKind(u.VariableDeclaration),n=i?.getInitializer();if(!B(i,e.getText()).some(r=>ne(r)))return n!==void 0&&ie(n)?n:void 0},A=e=>T(e,"args"),w=e=>{if(!s.isIdentifier(e))return;const t=e.getText(),i=e.getFirstAncestor(a=>s.isArrowFunction(a)||s.isFunctionExpression(a)||s.isFunctionDeclaration(a));if(i===void 0)return;const n=e.getStart();let r,o=-1;for(const a of oe(i,u.VariableDeclaration)){if(!pe(a,t))continue;const c=me(a,i);if(c===void 0||n<c.getStart()||n>=c.getEnd())continue;const l=a.getInitializer(),g=a.getStart();l!==void 0&&g<n&&g>o&&(r=a,o=g)}return r},L=e=>w(e)?.getInitializer(),je=e=>{if(A(e))return!0;const t=i=>{const n=w(i),r=n?.getInitializer();return r!==void 0&&A(r)||B(n,i.getText()).some(o=>A(o))};return t(e)||M(e).some(i=>t(i))},Qe=e=>{if(O(e))return!1;if(B(w(e),e.getText()).some(n=>A(n)&&!O(n)))return!0;const t=L(e);if(t!==void 0&&O(t)&&!(ie(t)&&A(t)))return!1;const i=t??e;return A(i)?!0:M(i).some(n=>{if(n.getFirstAncestor(c=>c===i||s.isCallExpression(c)||s.isNewExpression(c))!==i)return!1;const o=w(n),a=c=>c!==void 0&&A(c)&&(!O(c)||we(c));return a(o?.getInitializer())||B(o,n.getText()).some(c=>a(c))})},Ze=e=>{if(C(e))return!0;const t=f(e);if(t!==void 0&&C(t))return!0;const i=t??e;return(s.isIdentifier(i)?[i]:i.getDescendantsOfKind(u.Identifier)).some(r=>{const o=f(r);return o!==void 0&&C(o)})},Je=e=>{const t=f(e)??e;if(y(t).some(o=>D(o,t)&&ne(b(o))))return!1;if(R(e))return!0;const i=f(e)??Q(e);if(i!==void 0&&R(i))return!0;const n=i??e;return(s.isIdentifier(n)?[n]:n.getDescendantsOfKind(u.Identifier)).some(o=>{if(D(o,n)||X(o,n))return!1;const a=f(o)??Q(o);return a!==void 0&&R(a)})},z=(e,t)=>T(e,t),Xe=(e,t)=>{if(z(e,t))return!0;const i=L(e);if(i!==void 0&&z(i,t))return!0;const n=fe(e);if(n!==void 0){const r=L(n);return r!==void 0&&z(r,t)}return!1},Ye=(e,t)=>{const i=K(e),n=Ie(t);if(i===void 0||n===void 0||K(n)?.compilerNode!==i.compilerNode||i.getDescendantsOfKind(u.BinaryExpression).some(c=>{const l=P(c.getLeft());return Ne(c.getOperatorToken().getKind())&&l!==void 0&&p(l.root,n)}))return!1;let o=e,a=e.getParent();for(;a!==void 0&&a.compilerNode!==i.compilerNode;){if(s.isBlock(a)){const c=a.getStatements(),l=c.findIndex(g=>g.compilerNode===o.compilerNode);if(l>0&&Be(c,l,n,i,e))return!0}o=a,a=a.getParent()}return!1},Fe=/rate|limit|\bip\b|geo|throttle/iu,Ce=/^run[A-Z]/u,Re=e=>s.isIdentifier(e)?(e.getSymbol()?.getDeclarations()??[]).some(i=>i.getFirstAncestorByKind(u.ImportDeclaration)?.getModuleSpecifierValue().startsWith("@lunora/")===!0):!1,ze=e=>{const t=s.isCallExpression(e)?e.getExpression():e;let i;return s.isIdentifier(t)?i=t:s.isPropertyAccessExpression(t)&&(i=t.getNameNode()),i!==void 0&&Re(s.isPropertyAccessExpression(t)?t.getExpression():i)&&!Fe.test(i.getText())},ke=e=>{const t=s.isCallExpression(e)?e.getExpression():void 0;if(s.isElementAccessExpression(t)){const i=t.getArgumentExpression();return(s.isStringLiteral(i)||s.isNoSubstitutionTemplateLiteral(i))&&i.getLiteralText()==="use"}return s.isPropertyAccessExpression(t)&&t.getName()==="use"},Ke=e=>{const t=e.getParent();return s.isPropertyAccessExpression(t)&&t.getExpression()===e?!Ce.test(t.getName()):s.isElementAccessExpression(t)&&t.getExpression()===e?s.isStringLiteral(t.getArgumentExpression())||s.isNoSubstitutionTemplateLiteral(t.getArgumentExpression()):s.isVariableDeclaration(t)&&t.getInitializer()?.getText()===e.getText()},Le=e=>{if(s.isPropertyAccessExpression(e))return e.getName()==="ip";if(s.isElementAccessExpression(e)){const t=e.getArgumentExpression();return(s.isStringLiteral(t)||s.isNoSubstitutionTemplateLiteral(t))&&t.getLiteralText()==="ip"}return s.isBindingElement(e)?e.getDotDotDotToken()!==void 0||(e.getPropertyNameNode()?.getText()??e.getName())==="ip":s.isShorthandPropertyAssignment(e)&&e.getName()==="ip"},Ve=(e,t)=>{const i=e.getParent();if(!s.isCallExpression(i)||i.getExpression()===e)return!1;const n=i.getArguments().indexOf(e),r=Y(i.getExpression()),o=n===-1||r===void 0?void 0:r.getParameters()[n];return r===void 0||o===void 0||o.isRestParameter()||!s.isIdentifier(o.getNameNode())?!1:t.has(r.compilerNode)?!0:(t.add(r.compilerNode),!se(r,o,t))},Me=(e,t)=>t!==void 0&&s.isIdentifier(e)&&N(e)?.compilerNode===t.compilerNode,He=e=>s.isArrowFunction(e)||s.isFunctionExpression(e)||s.isFunctionDeclaration(e)||s.isMethodDeclaration(e),Ue=(e,t)=>!s.isBindingElement(e)||e.getDotDotDotToken()===void 0?!1:e.getFirstAncestorByKind(u.Parameter)?.getParent()?.getAncestors().some(r=>r!==t&&He(r)&&r.getAncestors().includes(t))===!0,se=(e,t,i)=>{for(const n of e.getDescendants()){if(Le(n)&&!Ue(n,e)||ke(n)&&!ze(n.getArguments()[0]))return!0;if(s.isIdentifier(n)&&(le(n)||Me(n,t))&&!Ke(n)){const r=n.getParent();if(!(s.isParameterDeclaration(r)||s.isBindingElement(r)||s.isVariableDeclaration(r))&&!Ve(n,i))return!0}}return!1},$e=e=>se(e,void 0,new Set);export{Ze as a,Qe as b,Je as c,Ye as d,Xe as e,z as f,w as g,je as i,$e as m,A as r,L as s};