@stacksjs/ts-cloud 0.12.11 → 0.12.13
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.
- package/dist/aws/index.js +1 -1
- package/dist/bin/chunk-0vsxpmfj.js +3 -0
- package/dist/bin/chunk-12e9v1dw.js +3 -0
- package/dist/bin/chunk-13dhcjjb.js +60 -0
- package/dist/bin/chunk-1fj5gk6r.js +725 -0
- package/dist/bin/chunk-1j2sja1j.js +1 -0
- package/dist/bin/chunk-1s4dny8d.js +1 -0
- package/dist/bin/chunk-2hpnd4g4.js +1 -0
- package/dist/bin/chunk-49sthm4q.js +2 -0
- package/dist/bin/chunk-4yntpqbe.js +1 -0
- package/dist/bin/chunk-4zbnwt9b.js +2 -0
- package/dist/bin/chunk-55kmbzr5.js +1 -0
- package/dist/{chunk-2ybcnrte.js → bin/chunk-57w9nsjk.js} +3 -3
- package/dist/bin/chunk-5gehfky4.js +2 -0
- package/dist/bin/chunk-6xw0r4gn.js +7364 -0
- package/dist/bin/chunk-6zk2n3wk.js +511 -0
- package/dist/bin/{chunk-2pevjm25.js → chunk-7a7n6p2m.js} +2 -2
- package/dist/bin/chunk-7h6h8yxm.js +6 -0
- package/dist/bin/chunk-7x0hrna6.js +1 -0
- package/dist/bin/{chunk-bj4ft4qc.js → chunk-80zkjpfz.js} +29 -29
- package/dist/bin/chunk-84tvv3x1.js +1 -0
- package/dist/bin/chunk-94cfdvrn.js +2 -0
- package/dist/bin/chunk-96xtj5j6.js +1 -0
- package/dist/bin/chunk-9jfjzycs.js +111 -0
- package/dist/bin/{chunk-mhrtx0p1.js → chunk-bckm6fre.js} +6 -6
- package/dist/bin/chunk-bnv8yd9w.js +6 -0
- package/dist/bin/chunk-c51xp8f7.js +1 -0
- package/dist/bin/chunk-d5qhwtkg.js +9 -0
- package/dist/bin/chunk-d7r6mhyv.js +34 -0
- package/dist/bin/chunk-de88yqwy.js +1 -0
- package/dist/bin/chunk-dh2qmxw9.js +1 -0
- package/dist/bin/chunk-dhxwmkhz.js +2 -0
- package/dist/bin/chunk-f35pte64.js +44 -0
- package/dist/bin/{chunk-3g80gjw6.js → chunk-gj5jkhfr.js} +13 -13
- package/dist/bin/chunk-jcyrm5q0.js +2 -0
- package/dist/bin/chunk-jh1593zx.js +1 -0
- package/dist/bin/chunk-nnkhrm4n.js +8 -0
- package/dist/bin/chunk-p36ns96s.js +1 -0
- package/dist/bin/{chunk-dep2n31v.js → chunk-p9hd27k4.js} +8 -8
- package/dist/bin/chunk-qz3za76g.js +1 -0
- package/dist/bin/chunk-r6cvc2pr.js +1 -0
- package/dist/bin/chunk-rh2sqtew.js +1 -0
- package/dist/bin/chunk-rxwecjgc.js +1 -0
- package/dist/bin/chunk-tkzpbb7n.js +1 -0
- package/dist/bin/chunk-vx649y7t.js +1 -0
- package/dist/bin/chunk-vymg9ywg.js +2 -0
- package/dist/bin/chunk-w7k5rvmw.js +1 -0
- package/dist/bin/chunk-wge61ccr.js +2 -0
- package/dist/bin/chunk-wvhqjr80.js +2 -0
- package/dist/bin/chunk-x5x9dzk7.js +3 -0
- package/dist/bin/chunk-xaafj6kf.js +1 -0
- package/dist/bin/chunk-xgepx8yx.js +1 -0
- package/dist/bin/{chunk-48eb4z0c.js → chunk-yhxpmk29.js} +32 -32
- package/dist/bin/chunk-ymbwekjm.js +1 -0
- package/dist/bin/chunk-yn34zgr2.js +1 -0
- package/dist/bin/chunk-ynr22ma8.js +2 -0
- package/dist/bin/chunk-yt8rgkqt.js +2 -0
- package/dist/bin/chunk-z2ez8wn9.js +37 -0
- package/dist/bin/cli.js +428 -428
- package/dist/bin/dashboard-server.js +1 -1
- package/dist/chunk-13d54txj.js +44 -0
- package/dist/chunk-1kp3y2s7.js +1 -0
- package/dist/chunk-1n3ckctd.js +7355 -0
- package/dist/chunk-30h9rgfn.js +6 -0
- package/dist/chunk-3kapcdpn.js +92 -0
- package/dist/chunk-3qp10j0j.js +5 -0
- package/dist/chunk-6g81m01r.js +141 -0
- package/dist/{chunk-6cxb8d6y.js → chunk-6nvn041c.js} +3 -3
- package/dist/chunk-7by4rm7y.js +1 -0
- package/dist/chunk-7ww65b66.js +709 -0
- package/dist/chunk-9c29rnk3.js +22 -0
- package/dist/chunk-9w32efae.js +1 -0
- package/dist/chunk-e0919h5g.js +2 -0
- package/dist/{chunk-dndxwssj.js → chunk-e7ds04wb.js} +2 -2
- package/dist/chunk-edqhhcg9.js +4 -0
- package/dist/chunk-ej4sm2tw.js +60 -0
- package/dist/chunk-emg3shmb.js +2 -0
- package/dist/chunk-fdexq9e1.js +1 -0
- package/dist/chunk-g8fpsdyq.js +37 -0
- package/dist/chunk-jb8em9ba.js +2 -0
- package/dist/chunk-n688m5fp.js +1 -0
- package/dist/chunk-r5emv4x8.js +2 -0
- package/dist/{chunk-q9a1z53k.js → chunk-sct6mdgc.js} +51 -51
- package/dist/chunk-thpce63q.js +34 -0
- package/dist/chunk-tnjae324.js +585 -0
- package/dist/{bin/chunk-hv3w2qy1.js → chunk-wjgnfg6q.js} +6 -6
- package/dist/chunk-wpqj98h8.js +2 -0
- package/dist/chunk-wrnhbb13.js +1 -0
- package/dist/chunk-zftpjxkr.js +2 -0
- package/dist/chunk-zmyv76f0.js +114 -0
- package/dist/deploy/index.js +1 -1
- package/dist/dns/index.js +1 -1
- package/dist/drivers/index.d.ts +1 -0
- package/dist/drivers/index.js +1 -1
- package/dist/drivers/shared/compute-deploy.d.ts +0 -12
- package/dist/drivers/shared/rpx-gateway.d.ts +124 -4
- package/dist/drivers/ssh/bootstrap.d.ts +13 -3
- package/dist/drivers/ssh/config.d.ts +11 -3
- package/dist/index.d.ts +1 -1
- package/dist/index.js +91 -91
- package/dist/operations/index.js +1 -1
- package/dist/protection/index.js +1 -1
- package/dist/push/index.js +1 -1
- package/dist/spend/index.js +1 -1
- package/dist/ui/404.html +1 -1
- package/dist/ui/accept-invitation.html +1 -1
- package/dist/ui/access-denied.html +2 -2
- package/dist/ui/account/automation.html +5 -5
- package/dist/ui/account/security.html +2 -2
- package/dist/ui/applications/compose.html +6 -6
- package/dist/ui/applications/new.html +2 -2
- package/dist/ui/data/backups.html +6 -6
- package/dist/ui/data/services.html +6 -6
- package/dist/ui/data/volumes.html +6 -6
- package/dist/ui/index.html +5 -5
- package/dist/ui/infrastructure/topology.html +9 -9
- package/dist/ui/integrations.html +2 -2
- package/dist/ui/operations/alerts.html +6 -6
- package/dist/ui/operations/configuration.html +6 -6
- package/dist/ui/operations/jobs.html +6 -6
- package/dist/ui/operations/maintenance.html +5 -5
- package/dist/ui/operations/observability.html +5 -5
- package/dist/ui/operations/previews.html +6 -6
- package/dist/ui/operations/queue.html +6 -6
- package/dist/ui/operations/regions.html +5 -5
- package/dist/ui/operations/releases.html +6 -6
- package/dist/ui/operations/spend.html +6 -6
- package/dist/ui/operations/workloads.html +10 -10
- package/dist/ui/security.html +2 -2
- package/dist/ui/server/actions.html +6 -6
- package/dist/ui/server/activity.html +2 -2
- package/dist/ui/server/capacity.html +5 -5
- package/dist/ui/server/database.html +5 -5
- package/dist/ui/server/deployments.html +5 -5
- package/dist/ui/server/diagnostics.html +2 -2
- package/dist/ui/server/firewall.html +6 -6
- package/dist/ui/server/fleet.html +5 -5
- package/dist/ui/server/logs.html +6 -6
- package/dist/ui/server/metrics.html +5 -5
- package/dist/ui/server/security.html +1 -1
- package/dist/ui/server/services.html +2 -2
- package/dist/ui/server/sites.html +16 -16
- package/dist/ui/server/ssh-keys.html +6 -6
- package/dist/ui/server/team.html +6 -6
- package/dist/ui/server/terminal.html +2 -2
- package/dist/ui/serverless/alarms.html +5 -5
- package/dist/ui/serverless/assets.html +2 -2
- package/dist/ui/serverless/cost.html +2 -2
- package/dist/ui/serverless/data.html +6 -6
- package/dist/ui/serverless/deployments.html +2 -2
- package/dist/ui/serverless/firewall.html +2 -2
- package/dist/ui/serverless/functions.html +5 -5
- package/dist/ui/serverless/logs.html +6 -6
- package/dist/ui/serverless/metrics.html +2 -2
- package/dist/ui/serverless/queues.html +6 -6
- package/dist/ui/serverless/secrets.html +6 -6
- package/dist/ui/serverless/traces.html +6 -6
- package/dist/ui/serverless.html +6 -6
- package/dist/ui-src/src/charts.ts +1141 -1294
- package/dist/ui-src/src/topology.ts +3 -3
- package/package.json +3 -3
- package/dist/bin/chunk-0x8szp53.js +0 -111
- package/dist/bin/chunk-1hggd7yz.js +0 -1
- package/dist/bin/chunk-2tsg6e8j.js +0 -1
- package/dist/bin/chunk-3f27aa5r.js +0 -2
- package/dist/bin/chunk-40j9hgzc.js +0 -1
- package/dist/bin/chunk-4gqfdr7t.js +0 -1
- package/dist/bin/chunk-5gx38418.js +0 -44
- package/dist/bin/chunk-5jv4cpky.js +0 -1
- package/dist/bin/chunk-5kxm5mx7.js +0 -1
- package/dist/bin/chunk-5rhvcgzt.js +0 -34
- package/dist/bin/chunk-8223r1xn.js +0 -2
- package/dist/bin/chunk-84703zpq.js +0 -7364
- package/dist/bin/chunk-9edyzdm0.js +0 -2
- package/dist/bin/chunk-9xbsmz1f.js +0 -2
- package/dist/bin/chunk-a8t3c4qe.js +0 -2
- package/dist/bin/chunk-aghy01we.js +0 -1
- package/dist/bin/chunk-akwfvn5k.js +0 -1
- package/dist/bin/chunk-asvz121j.js +0 -1
- package/dist/bin/chunk-b0xtsv8k.js +0 -6
- package/dist/bin/chunk-bcpdj01b.js +0 -1
- package/dist/bin/chunk-da49my09.js +0 -1
- package/dist/bin/chunk-ewxf62t8.js +0 -60
- package/dist/bin/chunk-f42gqn6c.js +0 -3
- package/dist/bin/chunk-fc6r1ack.js +0 -2
- package/dist/bin/chunk-ftcjjf76.js +0 -9
- package/dist/bin/chunk-g76sry03.js +0 -1
- package/dist/bin/chunk-hwdfky19.js +0 -2
- package/dist/bin/chunk-j02d6qmn.js +0 -1
- package/dist/bin/chunk-j2cb8we4.js +0 -37
- package/dist/bin/chunk-kmz8vb77.js +0 -1
- package/dist/bin/chunk-kyr5rasx.js +0 -511
- package/dist/bin/chunk-nc9pp87y.js +0 -8
- package/dist/bin/chunk-ncrnbcsa.js +0 -6
- package/dist/bin/chunk-py1hyeb2.js +0 -1
- package/dist/bin/chunk-qa06sft7.js +0 -1
- package/dist/bin/chunk-qr0xck72.js +0 -3
- package/dist/bin/chunk-r85ahkwq.js +0 -1
- package/dist/bin/chunk-re15e6p4.js +0 -2
- package/dist/bin/chunk-sj5csg49.js +0 -2
- package/dist/bin/chunk-t6y3pkm8.js +0 -1
- package/dist/bin/chunk-tfmx17he.js +0 -2
- package/dist/bin/chunk-vfcchrby.js +0 -1
- package/dist/bin/chunk-vmdh41kp.js +0 -1
- package/dist/bin/chunk-vrc7eemq.js +0 -1
- package/dist/bin/chunk-vsd7670h.js +0 -2
- package/dist/bin/chunk-vwqn3yx6.js +0 -3
- package/dist/bin/chunk-vxbnjz78.js +0 -1
- package/dist/bin/chunk-w8s8vgdj.js +0 -1
- package/dist/bin/chunk-yq9vetf8.js +0 -698
- package/dist/bin/chunk-zrgd65r8.js +0 -1
- package/dist/chunk-0a4tqwm9.js +0 -709
- package/dist/chunk-4884der5.js +0 -1
- package/dist/chunk-4wdxsnn2.js +0 -44
- package/dist/chunk-5hhyyhjt.js +0 -1
- package/dist/chunk-5zkacdw1.js +0 -1
- package/dist/chunk-6f68caq0.js +0 -1
- package/dist/chunk-7z50g5xz.js +0 -4
- package/dist/chunk-8fh4m5ge.js +0 -1
- package/dist/chunk-97pb7wb1.js +0 -1
- package/dist/chunk-ac87ds1k.js +0 -6
- package/dist/chunk-ess6vxfj.js +0 -2
- package/dist/chunk-fvpkxznh.js +0 -558
- package/dist/chunk-j06r5rwv.js +0 -2
- package/dist/chunk-j1g67djb.js +0 -92
- package/dist/chunk-k8x8mrkr.js +0 -2
- package/dist/chunk-mnmz8t7q.js +0 -34
- package/dist/chunk-n1fyryha.js +0 -141
- package/dist/chunk-n1hg0jxs.js +0 -2
- package/dist/chunk-n5xmh0ma.js +0 -37
- package/dist/chunk-q22qfwsc.js +0 -22
- package/dist/chunk-r8t25tbm.js +0 -7355
- package/dist/chunk-whyttetx.js +0 -5
- package/dist/chunk-yf8stygs.js +0 -114
- package/dist/chunk-ze5x2bpm.js +0 -2
- package/dist/chunk-zh0apedk.js +0 -60
- package/dist/chunk-zjyz926j.js +0 -2
|
@@ -0,0 +1,709 @@
|
|
|
1
|
+
import{Wc,_,d,Xc,O,p,ae,g,Mt,rd,Qs,sd,od,dd,_r,Ws,Gt,ld,ud,yd,Id}from"./chunk-1n3ckctd.js";import{B,u}from"./chunk-wpqj98h8.js";import{oe,kt,w,Bt,s,Jn,As,yr,yc,Ic,W,c,Ss,Le,Ir,Sc,xt,bs,Cc,Sr,Es,Cr,Zn,Lt,ni,ii,$e,$t,Hc,Gc,oi,ai,ot,Ls,i,ci,Ms,Fs}from"./chunk-sct6mdgc.js";import{n,l}from"./chunk-30h9rgfn.js";import{Nt,a}from"./chunk-3kapcdpn.js";import{en}from"./chunk-ej4sm2tw.js";import{tn,Ul,qi,P,v,Vi,Ql}from"./chunk-3qp10j0j.js";import{le,Jt,Wi,ze,V,te,C,ue,Zt,We,tr}from"./chunk-zmyv76f0.js";import{j}from"./chunk-zftpjxkr.js";import{b,ir,os,wt,sr,Yl,Fa,Be,et,En,k,Dt,wu,Pt,pr}from"./chunk-tnjae324.js";import{pe}from"./chunk-thpce63q.js";import{y}from"./chunk-6g81m01r.js";import{Pe}from"./chunk-r5emv4x8.js";import{Ie}from"./chunk-6nvn041c.js";import{f}from"./chunk-jb8em9ba.js";function jr(e){let t=Mc(e.name),r=Ju(e.configText);if(ZS(e.configText,r.start,r.end,t))throw Error(`Site '${t}' already exists in cloud.config.ts`);let o=fm({...e,name:t}),m=e.configText.slice(0,r.end).trimEnd(),h=e.configText.slice(r.end),I=m.endsWith("{")?`
|
|
2
|
+
`:m.endsWith(",")?`
|
|
3
|
+
|
|
4
|
+
`:`,
|
|
5
|
+
|
|
6
|
+
`;return`${m}${I}${o}
|
|
7
|
+
${h}`}function zr(e){let t=Mc(e.name),r=Ju(e.configText),o=wf(e.configText,r.start,r.end,t);if(!o)throw Error(`Site '${t}' does not exist in cloud.config.ts`);let m=e.configText,h=o.nameStart;while(h>0&&m[h-1]!==`
|
|
8
|
+
`&&/\s/.test(m[h-1]))h--;let I=o.end+1;if(m[I]===",")I++;while(I<m.length&&m[I]!==`
|
|
9
|
+
`&&/\s/.test(m[I]))I++;if(m[I]===`
|
|
10
|
+
`)I++;return m.slice(0,h)+m.slice(I)}function Sl(e){let{configText:t,...r}=e,o=zr({configText:t,name:r.name});return jr({configText:o,...r})}function xi(e){let t=Mc(e.siteName);if(!/^[A-Za-z_][A-Za-z0-9_]*$/.test(e.key))throw Error(`Invalid property key '${e.key}'`);let r=Ju(e.configText),o=wf(e.configText,r.start,r.end,t);if(!o)throw Error(`Site '${t}' does not exist in cloud.config.ts`);let m=e.configText,h=m.indexOf("{",o.nameStart),I=o.end,R=m.slice(h+1,I),A=new RegExp(`(^|[\\s,{])(${Gu(e.key)})\\s*:`,"m").exec(R);if(A){let N=h+1+A.index+A[1].length,F=m.indexOf(":",N),z=YS(m,F+1,I);return`${m.slice(0,N)}${e.key}: ${e.valueText}${m.slice(z)}`}let E=m.slice(0,I).trimEnd(),x=E.endsWith("{")||E.endsWith(",")?"":",";return`${E}${x}
|
|
11
|
+
${e.key}: ${e.valueText},
|
|
12
|
+
${m.slice(I)}`}function Cl(e){if(e===!1)return"false";if(e===!0)return"true";return`{ provider: '${so(e.provider||"letsencrypt")}' }`}function bl(e){return`'${so(e)}'`}function El(e){let t=Object.entries(e).filter(([r])=>/^[A-Za-z_][A-Za-z0-9_]*$/.test(r));if(t.length===0)return"{}";return`{
|
|
13
|
+
${t.map(([r,o])=>` ${r}: '${so(String(o))}',`).join(`
|
|
14
|
+
`)}
|
|
15
|
+
}`}function Jo(e){return/^(?=.{1,253}$)(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,63}$/i.test(e.trim())}function vl(e){let t=[...new Set(e.map((r)=>r.trim().toLowerCase()).filter(Boolean))];for(let r of t)if(!Jo(r))throw Error(`Alias '${r}' is not a valid hostname.`);if(t.length===0)return"[]";return`[${t.map((r)=>`'${so(r)}'`).join(", ")}]`}function Tl(e){let t=Object.entries(e).map(([r,o])=>[r.trim(),String(o).trim()]).filter(([r,o])=>r&&o);if(t.length===0)return"{}";return`{
|
|
16
|
+
${t.map(([r,o])=>` '${so(r)}': '${so(o)}',`).join(`
|
|
17
|
+
`)}
|
|
18
|
+
}`}function YS(e,t,r){let o=t;while(o<r&&/\s/.test(e[o]))o++;let m=0,h,I=!1;for(;o<r;o++){let R=e[o];if(h){if(I){I=!1;continue}if(R==="\\"){I=!0;continue}if(R===h)h=void 0;continue}if(R==='"'||R==="'"||R==="`"){h=R;continue}if(R==="{"||R==="["||R==="("){m++;continue}if(R==="}"||R==="]"||R===")"){if(m===0)return o;m--;continue}if(R===","&&m===0)return o}return r}function wf(e,t,r,o){let m=e.slice(t+1,r),h=new RegExp(`(^|[\\s,{])(${Gu(o)})\\s*:\\s*\\{`,"m").exec(m);if(!h)return null;let I=t+1+h.index+h[1].length,R=e.indexOf("{",I);return{nameStart:I,end:kf(e,R)}}function fm(e){let r=[` ${Mc(e.name)}: {`];if(oo(r,"deploy",e.deploy),oo(r,"root",e.root),oo(r,"path",e.path),oo(r,"domain",e.domain),oo(r,"type",e.type),oo(r,"build",e.build),oo(r,"start",e.start),e.port!==void 0)r.push(` port: ${e.port},`);if(oo(r,"pathRewriteStyle",e.pathRewriteStyle),e.ssl!==void 0)if(e.ssl===!1)r.push(" ssl: false,");else if(e.ssl===!0)r.push(" ssl: true,");else r.push(` ssl: { provider: '${so(e.ssl.provider||"letsencrypt")}' },`);if(e.env&&Object.keys(e.env).length>0){r.push(" env: {");for(let[o,m]of Object.entries(e.env)){if(!/^[A-Za-z_][A-Za-z0-9_]*$/.test(o))continue;r.push(` ${o}: '${so(String(m))}',`)}r.push(" },")}return r.push(" },"),r.join(`
|
|
19
|
+
`)}function so(e){return e.replace(/\\/g,"\\\\").replaceAll(String.fromCharCode(39),"\\'").replace(/\r/g,"\\r").replace(/\n/g,"\\n")}function Mc(e){let t=e.trim();if(!/^[A-Za-z_$][\w$]*$/.test(t))throw Error(`Invalid site name '${e}'. Use a valid JavaScript object key, e.g. docs or marketingSite.`);return t}function oo(e,t,r){if(r===void 0||r==="")return;let o=r.replace(/\\/g,"\\\\").replaceAll(String.fromCharCode(39),"\\'");e.push(` ${t}: '${o}',`)}function Ju(e){let t=/\bsites\s*:\s*{/g.exec(e);if(!t)throw Error("Could not find a top-level sites: { ... } block in cloud.config.ts");let r=e.indexOf("{",t.index),o=kf(e,r);return{start:r,end:o}}function ZS(e,t,r,o){let m=e.slice(t+1,r);return new RegExp(`(^|[\\s,{])${Gu(o)}\\s*:`,"m").test(m)}function Gu(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function kf(e,t){let r=0,o,m=!1,h=!1,I=!1;for(let R=t;R<e.length;R++){let A=e[R],E=e[R+1];if(h){if(A===`
|
|
20
|
+
`)h=!1;continue}if(I){if(A==="*"&&E==="/")I=!1,R++;continue}if(o){if(m){m=!1;continue}if(A==="\\"){m=!0;continue}if(A===o)o=void 0;continue}if(A==="/"&&E==="/"){h=!0,R++;continue}if(A==="/"&&E==="*"){I=!0,R++;continue}if(A==='"'||A==="'"||A==="`"){o=A;continue}if(A==="{")r++;if(A==="}"){if(r--,r===0)return R}}throw Error("Could not find the closing brace for sites: { ... } in cloud.config.ts")}function wl(e){let{configText:t,owner:r}=e,o=[...t.matchAll(/\n( {2})cloud: \{\n([\s\S]*?)\n\1\},\n/g)],[m]=o;if(!m)throw Error("No `cloud: { ... }` block found in the cloud config");if(o.length>1)throw Error(`Found ${o.length} \`cloud: { ... }\` blocks in the cloud config, so which one to edit is ambiguous`);let[h,I,R]=m;if(I===void 0||R===void 0)throw Error("The `cloud: { ... }` block did not parse into an indent and a body");if(R.includes("{"))throw Error("The `cloud: { ... }` block holds a nested object, which this edit does not attempt to rewrite");let A=R.match(/^\s*attachTo:\s*(['"])([^'"]*)\1\s*,?\s*$/m);if(A){let[x,N="'",F=""]=A;if(F===r)return t;return t.replace(h,h.replace(x,x.replace(`${N}${F}${N}`,`'${r}'`)))}let E=`${I} `;return t.replace(h,h.replace(`
|
|
21
|
+
${I}},
|
|
22
|
+
`,`
|
|
23
|
+
${E}// Deploy onto the box '${r}' owns rather than provisioning one.
|
|
24
|
+
${E}attachTo: '${r}',
|
|
25
|
+
${I}},
|
|
26
|
+
`))}import{createHash as XS}from"node:crypto";function ew(e){return XS("sha256").update(ks(e)).digest("hex")}function ks(e){if(Array.isArray(e))return`[${e.map((t)=>ks(t)).join(",")}]`;if(e&&typeof e==="object")return`{${Object.entries(e).filter(([,t])=>t!==void 0&&typeof t!=="function").sort(([t],[r])=>t.localeCompare(r)).map(([t,r])=>`${JSON.stringify(t)}:${ks(r)}`).join(",")}}`;return JSON.stringify(e)??"null"}function S(e,t){let r=`dashboard:${t.username.toLowerCase()}`;return e.getActorByExternalId("user",r)??e.createActor({kind:"user",externalId:r,displayName:t.name??t.username,metadata:{source:"dashboard",role:t.role}})}function Rl(e,t){let r=new Jn({cwd:e}),o=ew(t),m=r.getProjectBySlug(t.project.slug),h=t.project.slug.length>=3?t.project.slug:`${t.project.slug}-org`,I=(m?.organizationId?r.getOrganization(m.organizationId):void 0)??r.getOrganizationBySlug(h)??r.createOrganization({id:m?.organizationId,slug:h,name:t.project.name}),R=m?m.name!==t.project.name||m.desiredConfigHash!==o||m.organizationId!==I.id?r.updateProject(m.id,m.version,{name:t.project.name,organizationId:I.id,desiredConfigHash:o}):m:r.createProject({organizationId:I.id,slug:t.project.slug,name:t.project.name,desiredConfigHash:o}),A=new Map;for(let[F,z]of Object.entries(t.environments??{})){let J=r.getEnvironmentBySlug(R.id,F)??r.createEnvironment({projectId:R.id,slug:F,name:F.replace(/(^|[-_])(\w)/g,(fe,Ce,_e)=>_e.toUpperCase()),kind:String(z?.type??F),region:z?.region??t.project.region,desiredState:s(z)});A.set(F,J);let re=new Map(r.listResources(R.id,J.id).map((fe)=>[`${fe.kind}:${fe.slug}`,fe])),se=(fe,Ce,_e,he)=>{let Ue=`${fe}:${Ce}`,ge=re.get(Ue),Ke=s(he);if(ge){if(ge.name!==_e||ks(ge.desiredState)!==ks(Ke))re.set(Ue,r.updateResource(ge.id,ge.version,{name:_e,desiredState:Ke}));return}let nt=r.createResource({projectId:R.id,environmentId:J.id,kind:fe,slug:Ce,name:_e,provider:t.cloud?.provider,desiredState:Ke,metadata:{source:"cloud-config"}});re.set(Ue,nt)};for(let[fe,Ce]of Object.entries(t.sites??{}))se("application",fe,fe,Ce);let ye=z?.app;if(ye)for(let fe of["http","queue","cli"])se("function",fe,`${fe.toUpperCase()} function`,{...s(ye),function:fe});let ne=t.infrastructure;for(let[fe,Ce]of Object.entries(ne?.functions??{}))se("function",fe,fe,Ce);if(ne?.compute)se("server",`${R.slug}-server`,R.name,{...s(ne.compute),label:R.name});for(let[fe,Ce]of Object.entries(ne?.databases??{}))se("database",fe,fe,Ce);if(ne?.appDatabase)se("database","application-database","Application database",ne.appDatabase);else if(ne?.database)se("database","application-database","Application database",{engine:ne.database})}let E=new c(r).recoverExpired(),x=r.reconcileOrphanedOperations({policy:"fail"}),N={requeued:E.requeued+x.requeued,failed:E.failed+E.cancelled+x.failed};return{store:r,organization:I,project:R,environments:A,reconciliation:N}}var tw=["project:read","config:read","deployments:read","deployments:create","runtime:read","runtime:logs"];function _i(e,t){let{store:r,organization:o,project:m}=e,h=new Set;for(let I of t){let R=S(r,I);h.add(R.id);let A=r.getMembershipForActor(o.id,R.id);if(A&&A.source!=="legacy")continue;let E=r.listResources(m.id).filter((K)=>K.kind==="application"&&Object.hasOwn(I.sites,K.slug)),x=I.role==="admin"?"owner":"viewer",N=I.role==="admin"||E.length===0?{type:"organization"}:{type:"resource",id:E[0].id},F=A?A.roleTemplate!==x||ks(A.scope)!==ks(N)?r.updateMembership({id:A.id,roleTemplate:x,scope:N,actorId:R.id}):A:r.createMembership({organizationId:o.id,actorId:R.id,roleTemplate:x,scope:N,source:"legacy"}),z=new Set;if(I.role==="member"&&E.length===0)for(let K of kt("viewer"))z.add(`deny|${K}|organization|`);for(let K of E){let J=I.sites[K.slug];for(let re of tw)z.add(`allow|${re}|resource|${K.id}`);if(J==="owner")z.add(`allow|config:write|resource|${K.id}`)}for(let K of r.listGrants(F.id).filter((J)=>J.source==="legacy")){let J=`${K.effect}|${K.capability}|${K.scope.type}|${K.scope.id??""}`;if(!z.has(J))r.removeGrant(K.id,R.id)}for(let K of z){let[J,re,se,ye]=K.split("|");r.upsertGrant({organizationId:o.id,membershipId:F.id,effect:J,capability:re,scope:se==="organization"?{type:"organization"}:{type:se,id:ye},source:"legacy"})}}for(let I of r.listMemberships(o.id))if(I.source==="legacy"&&!h.has(I.actorId))r.revokeMembership(I.id)}async function we(e){let{controlPlane:t,environment:r,actor:o}=e,m=S(t.store,o),h=t.environments.get(r),I=e.resourceSlug&&h?t.store.listResources(t.project.id,h.id).find((x)=>x.slug===e.resourceSlug):void 0,R=new c(t.store,{workerId:`dashboard:${process.pid}`}),A=R.enqueue({projectId:t.project.id,environmentId:h?.id,resourceId:I?.id,actorId:m.id,kind:e.kind,input:e.input??{},lockKey:I?`resource:${I.id}`:h?`environment:${h.id}`:`project:${t.project.id}`,providerKey:I?.provider??"dashboard",buildSlot:e.kind.includes("deploy"),maxAttempts:1,timeoutSeconds:3600,resumePolicy:"fail",cancellationMode:"provider_non_cancellable",retentionDays:90}).operation;if(!R.claim(A.id)?.operation)throw Error(`Could not claim control-plane operation ${A.id}`);try{let x=await e.execute(),N=typeof x.stdout==="string"?String(x.stdout):"",F=typeof x.stderr==="string"?String(x.stderr):"";if(N)R.appendLog(A.id,N,{stream:"stdout"});if(F)R.appendLog(A.id,F,{stream:"stderr"});let z=x.ok?R.complete(A.id,If(x)):R.fail(A.id,rw(x),If(x));return{result:x,operation:z}}catch(x){throw R.fail(A.id,x instanceof Error?x.message:String(x),{ok:!1,threw:!0}),x}}function rw(e){let t=e;return String(t.error??t.stderr??"Operation failed")}function If(e){let t=e,r=typeof t.stdout==="string"?t.stdout:"",o=typeof t.stderr==="string"?t.stderr:"",m={ok:e.ok,stdoutBytes:Buffer.byteLength(r),stderrBytes:Buffer.byteLength(o)};if(typeof t.operation==="string")m.operation=t.operation;if(typeof t.error==="string")m.error=t.error;return s(m)}var Dl=[{id:"server.overview",path:"/",label:"Dashboard",group:"overview",modes:["server"],adminOnly:!0,legacyPaths:["/server"]},{id:"server.activity",path:"/server/activity",label:"Activity",group:"overview",modes:["server"],adminOnly:!0,legacyPaths:["/activity"]},{id:"services.list",path:"/server/sites",label:"Services",group:"applications",modes:["server"],adminOnly:!1,legacyPaths:["/sites","/services"]},{id:"deployments.list",path:"/server/deployments",label:"Deployments",group:"applications",modes:["server"],adminOnly:!1,legacyPaths:["/deployments"]},{id:"logs.list",path:"/server/logs",label:"Logs",group:"applications",modes:["server"],adminOnly:!1,legacyPaths:["/logs"]},{id:"sources.integrations",path:"/integrations",label:"Git integrations",group:"applications",modes:["server","serverless"],adminOnly:!1},{id:"applications.create",path:"/applications/new",label:"Create application",group:"applications",modes:["server","serverless"],adminOnly:!1},{id:"applications.compose",path:"/applications/compose",label:"Compose applications",group:"applications",modes:["server"],adminOnly:!1},{id:"operations.queue",path:"/operations/queue",label:"Deployment queue",group:"operations",modes:["server","serverless"],adminOnly:!1},{id:"operations.previews",path:"/operations/previews",label:"Preview environments",group:"operations",modes:["server","serverless"],adminOnly:!1},{id:"operations.releases",path:"/operations/releases",label:"Releases & promotion",group:"operations",modes:["server","serverless"],adminOnly:!1},{id:"runtime.workloads",path:"/operations/workloads",label:"Workloads",group:"operations",modes:["server","serverless"],adminOnly:!1},{id:"observability.overview",path:"/operations/observability",label:"Observability",group:"operations",modes:["server","serverless"],adminOnly:!1},{id:"alerts.overview",path:"/operations/alerts",label:"Alerts & health",group:"operations",modes:["server","serverless"],adminOnly:!1},{id:"spend.overview",path:"/operations/spend",label:"Spend & budgets",group:"operations",modes:["server","serverless"],adminOnly:!1},{id:"automation.jobs",path:"/operations/jobs",label:"Workers & schedules",group:"operations",modes:["server","serverless"],adminOnly:!1,legacyPaths:["/server/workers","/serverless/scheduler"]},{id:"configuration.entries",path:"/operations/configuration",label:"Environment & secrets",group:"operations",modes:["server","serverless"],adminOnly:!1,legacyPaths:["/serverless/secrets"]},{id:"metrics.overview",path:"/server/metrics",label:"Metrics",group:"operations",modes:["server"],adminOnly:!0},{id:"runtime.services",path:"/server/services",label:"Runtime services",group:"operations",modes:["server"],adminOnly:!0},{id:"backups.list",path:"/data/backups",label:"Backups & recovery",group:"operations",modes:["server","serverless"],adminOnly:!1,legacyPaths:["/server/backups"]},{id:"volumes.list",path:"/data/volumes",label:"Persistent volumes",group:"operations",modes:["server","serverless"],adminOnly:!1,legacyPaths:[]},{id:"fleet.list",path:"/server/fleet",label:"Fleet inventory",group:"operations",modes:["server"],adminOnly:!1,legacyPaths:[]},{id:"operations.run",path:"/server/actions",label:"Run operations",group:"operations",modes:["server"],adminOnly:!0},{id:"infrastructure.topology",path:"/infrastructure/topology",label:"Infrastructure map",group:"infrastructure",modes:["server","serverless"],adminOnly:!0,legacyPaths:["/topology"]},{id:"data.services",path:"/data/services",label:"Data services",group:"infrastructure",modes:["server","serverless"],adminOnly:!1,legacyPaths:["/database","/databases","/server/database","/serverless/data"]},{id:"network.firewall",path:"/server/firewall",label:"Network & firewall",group:"infrastructure",modes:["server"],adminOnly:!0},{id:"security.posture",path:"/security",label:"Security posture",group:"infrastructure",modes:["server","serverless"],adminOnly:!1,legacyPaths:["/server/security"]},{id:"access.ssh",path:"/server/ssh-keys",label:"SSH keys",group:"infrastructure",modes:["server"],adminOnly:!0},{id:"runtime.terminal",path:"/server/terminal",label:"Terminal",group:"infrastructure",modes:["server"],adminOnly:!0},{id:"organization.people",path:"/server/team",label:"People & access",group:"organization",modes:["server","serverless"],adminOnly:!0,legacyPaths:["/team"]},{id:"organization.automation",path:"/account/automation",label:"API & automation",group:"organization",modes:["server","serverless"],adminOnly:!0},{id:"serverless.overview",path:"/serverless",label:"Dashboard",group:"overview",modes:["serverless"],adminOnly:!0},{id:"serverless.deployments",path:"/serverless/deployments",label:"Deployments",group:"overview",modes:["serverless"],adminOnly:!0},{id:"serverless.logs",path:"/serverless/logs",label:"Logs",group:"overview",modes:["serverless"],adminOnly:!0},{id:"serverless.metrics",path:"/serverless/metrics",label:"Metrics",group:"overview",modes:["serverless"],adminOnly:!0},{id:"serverless.traces",path:"/serverless/traces",label:"Traces",group:"overview",modes:["serverless"],adminOnly:!0},{id:"serverless.functions",path:"/serverless/functions",label:"Functions",group:"compute-data",modes:["serverless"],adminOnly:!0},{id:"serverless.queues",path:"/serverless/queues",label:"Queues",group:"compute-data",modes:["serverless"],adminOnly:!0},{id:"serverless.assets",path:"/serverless/assets",label:"Assets",group:"compute-data",modes:["serverless"],adminOnly:!0},{id:"serverless.network",path:"/serverless/firewall",label:"Network",group:"settings",modes:["serverless"],adminOnly:!0},{id:"serverless.alerts",path:"/serverless/alarms",label:"Alerts",group:"settings",modes:["serverless"],adminOnly:!0},{id:"serverless.cost",path:"/serverless/cost",label:"Costs",group:"settings",modes:["serverless"],adminOnly:!0}],nw=new Map(Dl.flatMap((e)=>(e.legacyPaths??[]).map((t)=>[t,e.path])));function Pl(e){return nw.get(e)}function bA(e,t){return Dl.filter((r)=>r.modes.includes(e)&&(!t||!r.adminOnly))}import es from"node:process";import{existsSync as iw}from"node:fs";import{join as ow}from"node:path";async function Nl(e){let t=Date.now(),r=e.sourceDir??"dist";if(!iw(ow(r,"index.html")))return Qu(t,`${r}/ has no index.html — build first.`);if(!es.env.AWS_ACCESS_KEY_ID||!es.env.AWS_SECRET_ACCESS_KEY)return Qu(t,"Missing AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY in env.");let o=sw(e.dnsProvider);if("error"in o)return Qu(t,o.error);let m=await en({siteName:e.siteName,region:e.region??"us-east-1",domain:e.domain,bucket:e.bucket,stackName:e.stackName,certificateArn:e.certificateArn,defaultRootObject:e.defaultRootObject??"index.html",errorDocument:e.errorDocument??"404.html",cacheControl:e.cacheControl??"max-age=3600, public",sourceDir:r,cleanBucket:e.cleanBucket??!1,singlePageApp:e.singlePageApp??!1,dnsProvider:o,tags:{ManagedBy:"ts-cloud",Application:e.siteName,...e.tags},onProgress:e.onProgress});return{success:!!m.success,domain:m.domain,url:m.domain?`https://${m.domain}`:m.distributionDomain?`https://${m.distributionDomain}`:void 0,bucket:m.bucket,distributionId:m.distributionId,distributionDomain:m.distributionDomain,certificateArn:m.certificateArn,filesUploaded:m.filesUploaded,filesSkipped:m.filesSkipped,message:m.message,durationMs:Date.now()-t}}function sw(e){let t=typeof e==="string"?{provider:e}:e??{provider:"porkbun"};if(t.provider==="porkbun"){let r=t,o=r?.apiKey??es.env.PORKBUN_API_KEY,m=r?.secretKey??es.env.PORKBUN_SECRET_KEY??es.env.PORKBUN_SECRET_API_KEY;if(!o||!m)return{error:"Missing PORKBUN_API_KEY / PORKBUN_SECRET_KEY in env."};return{provider:"porkbun",apiKey:o,secretKey:m}}if(t.provider==="godaddy"){let r=t,o=r?.apiKey??es.env.GODADDY_API_KEY,m=r?.secretKey??es.env.GODADDY_API_SECRET,h=r?.environment??es.env.GODADDY_ENVIRONMENT;if(!o||!m)return{error:"Missing GODADDY_API_KEY / GODADDY_API_SECRET in env."};return{provider:"godaddy",apiKey:o,apiSecret:m,environment:h}}return{error:`Unsupported DNS provider: ${t.provider}`}}function Qu(e,t){return{success:!1,message:t,durationMs:Date.now()-e}}import{createHash as aw}from"node:crypto";var cw=`let coldStart = true
|
|
27
|
+
const initializedAt = new Date().toISOString()
|
|
28
|
+
export const handler = async (event) => {
|
|
29
|
+
const currentColdStart = coldStart
|
|
30
|
+
coldStart = false
|
|
31
|
+
const path = event.rawPath || event.requestContext?.http?.path || '/'
|
|
32
|
+
const statusCode = path === '/api/health' || path === '/health' ? 200 : 404
|
|
33
|
+
return {
|
|
34
|
+
statusCode,
|
|
35
|
+
headers: { 'content-type': 'application/json', 'cache-control': 'no-store' },
|
|
36
|
+
body: JSON.stringify({ ok: statusCode === 200, service: 'ts-cloud-static-api', path, coldStart: currentColdStart, initializedAt })
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
`;function Af(e){let t=e;return String(t?.code||t?.name||t?.statusCode||"")}function dw(e){return["NoSuchEntity","ResourceNotFoundException","404"].includes(Af(e))}function Ef(e){return["EntityAlreadyExists","ResourceAlreadyExistsException","ResourceConflictException","409"].includes(Af(e))}function Rf(e,t){let r=e?.Items;if(!r)return[];if(Array.isArray(r))return r;let o=r[t];if(o===void 0)return[];return Array.isArray(o)?o:[o]}function Yu(e){let t=e?.Aliases?.Items;if(Array.isArray(t))return t.map(String);if(typeof t==="string")return[t];return[]}function lw(e){if(!/^[A-Z0-9]{8,32}$/.test(e.distributionId))throw Error("CloudFront distribution ID is invalid");if(!/^(?=.{1,253}$)(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+[A-Za-z0-9-]{2,63}$/.test(e.expectedAlias))throw Error("Expected CloudFront alias is invalid");if(!/^[A-Za-z0-9-_]{1,64}$/.test(e.functionName))throw Error("Lambda function name is invalid");let t=e.pathPattern?.startsWith("/")?e.pathPattern:`/${e.pathPattern||"api/*"}`;if(t==="/"||t==="/*"||!t.endsWith("*"))throw Error("API path must be a non-default wildcard pattern such as /api/*");let r=e.originId||`${e.functionName}-url`,o=e.roleName||`${e.functionName}-execution`;if(!/^[A-Za-z0-9_.-]{1,64}$/.test(o))throw Error("IAM role name is invalid");let m=e.memorySize??256,h=e.timeout??10,I=e.logRetentionDays??14;if(m<128||m>10240)throw Error("Lambda memory must be between 128 and 10240 MB");if(h<1||h>900)throw Error("Lambda timeout must be between 1 and 900 seconds");return{region:e.region||"us-east-1",pathPattern:t,originId:r,roleName:o,memorySize:m,timeout:h,logRetentionDays:I}}function Am(e){let t=e.region||"us-east-1";return{cloudfront:new y(e.profile),iam:new pe(t,e.profile),lambda:new C(t,e.profile),logs:new ue(t,e.profile),sts:new le(t,e.profile),sleep:(r)=>new Promise((o)=>setTimeout(o,r))}}async function EA(e,t){let r=lw(e),o=t||Am({profile:e.profile,region:r.region}),[m,h,I]=await Promise.all([o.sts.getCallerIdentity(),o.cloudfront.getDistribution(e.distributionId),o.cloudfront.getDistributionConfig(e.distributionId)]);if(!m.Account)throw Error("AWS identity did not return an account ID");if(!h.Enabled)throw Error(`CloudFront distribution ${e.distributionId} is disabled`);if(!(Yu(h).length?Yu(h):Yu(I.DistributionConfig)).includes(e.expectedAlias))throw Error(`Distribution ${e.distributionId} does not contain expected alias ${e.expectedAlias}`);let A,E=!0;try{A=await o.iam.getRole({RoleName:r.roleName})}catch(at){if(!dw(at))throw at;E=!1}let x=A?.Arn||`arn:aws:iam::${m.Account}:role/${r.roleName}`,N=await o.lambda.functionExists(e.functionName),F=N?await o.lambda.getFunctionUrl(e.functionName):null,z=await o.cloudfront.listOriginAccessControls(),K=`${e.functionName}-lambda-url`,J=z.some((at)=>at.Name===K&&at.OriginAccessControlOriginType==="lambda"),re=Rf(I.DistributionConfig.Origins,"Origin"),se=Rf(I.DistributionConfig.CacheBehaviors,"CacheBehavior"),ye=re.some((at)=>String(at.Id)===r.originId),ne=se.some((at)=>String(at.PathPattern)===r.pathPattern),fe=[...!E?[`create IAM execution role ${r.roleName}`]:[`reconcile IAM execution policy ${r.roleName}`],...N?[`update Lambda ${e.functionName} code and configuration`]:[`create Lambda ${e.functionName}`],...!F?["create private AWS_IAM function URL"]:[],...!J?[`create Lambda origin access control ${K}`]:[],"grant CloudFront function-URL and invocation permissions",`${ne?"reconcile":"add"} ${r.pathPattern} behavior without changing the default behavior`],Ce={command:`cloud cdn:origin:remove ${e.distributionId} FUNCTION_URL_HOST --id ${r.originId} --path '${r.pathPattern}' --apply --confirm 'remove:${e.distributionId}:${r.pathPattern}'`,removeFunction:!N,removeRole:!E},_e={mode:e.apply?"apply":"plan",accountId:m.Account,identityArn:m.Arn,distribution:{id:h.Id,arn:h.ARN,alias:e.expectedAlias,domainName:h.DomainName,status:h.Status},function:{name:e.functionName,existed:N,runtime:"nodejs22.x",urlExisted:!!F,url:F?.FunctionUrl},role:{name:r.roleName,existed:E,arn:x},origin:{id:r.originId,pathPattern:r.pathPattern,existed:ye,behaviorExisted:ne,accessControlExisted:J},changes:fe,rollback:Ce,applied:!1};if(!e.apply)return _e;let he=`${e.distributionId}:${r.pathPattern}`;if(e.confirm!==he)throw Error(`Pass exact confirmation token ${he} to mutate the live distribution`);if(!A)A=await o.iam.createRole({RoleName:r.roleName,Description:`Execution role for ${e.functionName}`,AssumeRolePolicyDocument:JSON.stringify({Version:"2012-10-17",Statement:[{Effect:"Allow",Principal:{Service:"lambda.amazonaws.com"},Action:"sts:AssumeRole"}]}),Tags:[{Key:"ManagedBy",Value:"ts-cloud"}]});await o.iam.putRolePolicy({RoleName:r.roleName,PolicyName:"lambda-logs",PolicyDocument:JSON.stringify({Version:"2012-10-17",Statement:[{Effect:"Allow",Action:["logs:CreateLogStream","logs:PutLogEvents"],Resource:`arn:aws:logs:${r.region}:${m.Account}:log-group:/aws/lambda/${e.functionName}:*`}]})});try{await o.logs.createLogGroup(`/aws/lambda/${e.functionName}`,{ManagedBy:"ts-cloud"})}catch(at){if(!Ef(at))throw at}await o.logs.putRetentionPolicy(`/aws/lambda/${e.functionName}`,r.logRetentionDays);let Ue=e.code||cw;if(N)await o.lambda.updateFunctionCodeInline(e.functionName,Ue,"index.mjs"),await o.lambda.waitForFunctionActive(e.functionName),await o.lambda.updateFunctionConfiguration({FunctionName:e.functionName,Runtime:"nodejs22.x",Role:A.Arn,Handler:"index.handler",Description:`Private API origin for ${e.expectedAlias}`,Timeout:r.timeout,MemorySize:r.memorySize});else{let at;for(let Ft=0;Ft<8;Ft++)try{await o.lambda.createFunctionWithCode({FunctionName:e.functionName,Runtime:"nodejs22.x",Role:A.Arn,Handler:"index.handler",Code:Ue,Filename:"index.mjs",Description:`Private API origin for ${e.expectedAlias}`,Timeout:r.timeout,MemorySize:r.memorySize}),at=void 0;break}catch(dr){if(at=dr,!String(dr.message).toLowerCase().includes("role"))throw dr;await o.sleep(1000)}if(at)throw at}await o.lambda.waitForFunctionActive(e.functionName);let ge=F||await o.lambda.createFunctionUrl({FunctionName:e.functionName,AuthType:"AWS_IAM",InvokeMode:"BUFFERED"});if(!ge.FunctionUrl)throw Error("Lambda did not return a function URL");let Ke=new URL(ge.FunctionUrl).hostname,nt=await o.cloudfront.findOrCreateOriginAccessControl(K,"lambda"),Et=h.ARN||`arn:aws:cloudfront::${m.Account}:distribution/${e.distributionId}`,cr=[{FunctionName:e.functionName,StatementId:"AllowCloudFrontFunctionUrl",Action:"lambda:InvokeFunctionUrl",Principal:"cloudfront.amazonaws.com",SourceArn:Et,FunctionUrlAuthType:"AWS_IAM"},{FunctionName:e.functionName,StatementId:"AllowCloudFrontInvoke",Action:"lambda:InvokeFunction",Principal:"cloudfront.amazonaws.com",SourceArn:Et,InvokedViaFunctionUrl:!0}];for(let at of cr)try{await o.lambda.addPermission(at)}catch(Ft){if(!Ef(Ft))throw Ft}return await o.cloudfront.upsertExistingDistributionOrigin(e.distributionId,{id:r.originId,domainName:Ke,pathPattern:r.pathPattern,originAccessControlId:nt.Id}),{..._e,function:{..._e.function,url:ge.FunctionUrl},applied:!0}}async function vA(e){let t=`https://${e.alias}/`,r=`https://${e.alias}/api/health`,o=await fetch(t,{redirect:"follow",headers:{"cache-control":"no-cache"}}),m=new Uint8Array(await o.arrayBuffer()),h=aw("sha256").update(m).digest("hex"),I=Date.now(),R=await fetch(r,{redirect:"follow",headers:{"cache-control":"no-cache"}}),A=Date.now()-I,E=await R.text(),x=E;try{x=JSON.parse(E)}catch{}let N;if(e.logs&&e.functionName){let F=await e.logs.filterLogEvents({logGroupName:`/aws/lambda/${e.functionName}`,startTime:I-60000,filterPattern:'"Init Duration"',limit:25});for(let z of F.events||[]){let K=z.message?.match(/Init Duration:\s*([0-9.]+)\s*ms/i);if(K)N=Number(K[1])}}return{frontend:{url:t,status:o.status,sha256:h,unchanged:e.expectedFrontendSha256?h===e.expectedFrontendSha256:void 0,bytes:m.byteLength},api:{url:r,status:R.status,latencyMs:A,healthy:R.ok&&!!x?.ok,body:x},coldStart:{observed:N!==void 0,initDurationMs:N},verifiedAt:(e.now?.()||new Date).toISOString()}}function TA(e){let t=(e.memoryMb||256)/1024,r=Math.max(0,e.requests-(e.includeFreeTier===!1?0:1e6)),o=Math.max(0,e.requests*(e.averageDurationMs/1000)*t-(e.includeFreeTier===!1?0:400000)),m=r/1e6*0.2+o*0.0000166667,h=9.010025,I=16.425;return{lambdaUsd:Number(m.toFixed(2)),alwaysOnFargateAndAlbUsd:Number(25.435025000000003 .toFixed(2)),assumptions:["us-east-1 public list rates snapshot, July 2026","Lambda x86 request and duration pricing with optional monthly free tier","Fargate comparison uses one always-on 0.25 vCPU/0.5 GB task plus one ALB before LCU charges","excludes CloudFront, data transfer, logs, taxes, and application data services"]}}import{createHash as uw}from"node:crypto";function pw(e){let t=e;return String(t?.code||t?.name||t?.statusCode||"")}function mw(e){return pw(e)==="ValidationError"&&String(e.message).includes("does not exist")}function gw(e){return Object.fromEntries((e||[]).map((t)=>[t.OutputKey,t.OutputValue]))}function fw(e){let t=e?.Aliases?.Items??e?.Aliases;if(Array.isArray(t))return t.map(String);if(typeof t==="string")return[t];return[]}function Cf(e){if(!/^[a-z][a-z0-9-]{1,62}$/.test(e.slug))throw Error("Slug must be a lowercase DNS-safe identifier");if(!/^[A-Z0-9]{8,32}$/.test(e.distributionId))throw Error("CloudFront distribution ID is invalid");if(!/^(?=.{1,253}$)(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+[A-Za-z0-9-]{2,63}$/.test(e.expectedAlias))throw Error("Expected distribution alias is invalid");let t=e.imageUri.match(/@((?:sha256:)[a-f0-9]{64})$/i)?.[1];if(!t)throw Error("Container image must use an immutable ECR digest URI ending in @sha256:<64 hex>");let r=e.stackName||`${e.slug}-backend`;if(!/^[A-Za-z][A-Za-z0-9-]{0,127}$/.test(r))throw Error("CloudFormation stack name is invalid");let o=e.pathPattern||"/api/*";if(!/^\/[A-Za-z0-9_.*?~!$&'()+,;=:@%/-]+\*$/.test(o)||o==="/*")throw Error("Backend path must be a non-default wildcard pattern");let m=e.originId||`${e.slug}-backend`;if(!/^[A-Za-z0-9_.-]{1,128}$/.test(m))throw Error("CloudFront origin ID is invalid");if(e.originVerifySecret&&e.originVerifySecret.length<24)throw Error("Origin verification secret must contain at least 24 characters");if(!!e.originDomain!==!!e.certificateArn)throw Error("A custom origin domain and matching ACM certificate ARN must be provided together");return{stackName:r,region:e.region||"us-east-1",pathPattern:o,originId:m,digest:t}}function ym(e){let t=e.region||"us-east-1";return{cloudformation:new f(t,e.profile),cloudfront:new y(e.profile),sts:new le(t,e.profile),fetch,sleep:(r)=>new Promise((o)=>setTimeout(o,r))}}function Im(e){let t=Cf(e),r=od({name:e.name,slug:e.slug,domain:e.originDomain||e.expectedAlias,imageUri:e.imageUri,certificateArn:e.certificateArn,originVerifySecret:e.originVerifySecret,desiredCount:e.desiredCount,database:e.database,cache:e.cache,queue:e.queue}),o=sd(r);return o.Description=`Managed backend for existing static frontend ${e.expectedAlias}`,o.Metadata={ManagedBy:"ts-cloud",ExistingDistributionId:e.distributionId,ExistingDistributionAlias:e.expectedAlias,BackendPathPattern:t.pathPattern,ContainerDigest:t.digest},o}async function hw(e,t,r){let o;for(let m=0;m<30;m++){let h=Date.now();try{let I=await e.fetch(t,{redirect:"manual",headers:r?{"x-origin-verify":r}:void 0}),R=Date.now()-h,A=await I.text(),E=A;try{E=JSON.parse(A)}catch{}if(I.ok)return{url:t,status:I.status,latencyMs:R,body:E};o=Error(`origin returned ${I.status}`)}catch(I){o=I}if(m<29)await e.sleep(1e4)}throw Error(`Backend origin did not become healthy: ${o instanceof Error?o.message:String(o)}`)}async function wA(e,t){let r=Cf(e),o=t||ym({region:r.region,profile:e.profile}),[m,h]=await Promise.all([o.sts.getCallerIdentity(),o.cloudfront.getDistribution(e.distributionId)]);if(!m.Account)throw Error("AWS identity did not return an account ID");if(!h.Enabled)throw Error(`CloudFront distribution ${e.distributionId} is disabled`);if(!fw(h).includes(e.expectedAlias))throw Error(`Distribution ${e.distributionId} does not contain expected alias ${e.expectedAlias}`);let I;try{I=(await o.cloudformation.describeStacks({stackName:r.stackName})).Stacks[0]}catch(fe){if(!mw(fe))throw fe}let R=Im(e),A=JSON.stringify(R);if(Buffer.byteLength(A)>51200)throw Error("Generated template exceeds the direct CloudFormation template body limit");let E=Object.values(R.Resources||{}).reduce((fe,Ce)=>(fe[Ce.Type]=(fe[Ce.Type]||0)+1,fe),{}),x=[`cloud cdn:origin:remove ${e.distributionId} ${e.originDomain||"ALB_HOST"} --id ${r.originId} --path '${r.pathPattern}' --profile ${e.profile||"default"} --apply --confirm 'remove:${e.distributionId}:${r.pathPattern}'`,`After CloudFront is deployed, restore external DNS if changed and delete stack ${r.stackName} only with an approved data-retention decision.`],N={mode:e.apply?"apply":"plan",accountId:m.Account,stack:{name:r.stackName,existed:!!I,status:I?.StackStatus,templateSha256:uw("sha256").update(A).digest("hex"),resourceCount:Object.keys(R.Resources||{}).length,resourceTypes:E},distribution:{id:h.Id,alias:e.expectedAlias,domainName:h.DomainName,status:h.Status,pathPattern:r.pathPattern,originId:r.originId},artifact:{imageUri:e.imageUri,digest:r.digest},services:{database:e.database!==!1,cache:e.cache!==!1,queue:e.queue!==!1,mail:"ses",desiredCount:e.desiredCount||1},cloudFrontApplied:!1,dnsApplied:!1,applied:!1,rollback:x};if(!e.apply)return N;let F=`${e.distributionId}:${r.pathPattern}:${r.stackName}`;if(e.confirm!==F)throw Error(`Pass exact confirmation token ${F} to provision the backend and change live routing`);if(I)try{await o.cloudformation.updateStack({stackName:r.stackName,templateBody:A,capabilities:["CAPABILITY_NAMED_IAM"],tags:[{Key:"ManagedBy",Value:"ts-cloud"},{Key:"Application",Value:e.slug}]}),await o.cloudformation.waitForStack(r.stackName,"stack-update-complete")}catch(fe){if(!String(fe.message).includes("No updates are to be performed"))throw fe}else await o.cloudformation.createStack({stackName:r.stackName,templateBody:A,capabilities:["CAPABILITY_NAMED_IAM"],onFailure:"ROLLBACK",tags:[{Key:"ManagedBy",Value:"ts-cloud"},{Key:"Application",Value:e.slug}]}),await o.cloudformation.waitForStack(r.stackName,"stack-create-complete");let z=(await o.cloudformation.describeStacks({stackName:r.stackName})).Stacks[0],K=gw(z?.Outputs),J=K.AppLoadBalancerDnsName;if(!J)throw Error("Backend stack did not return AppLoadBalancerDnsName");let re=!1;if(e.originDomain&&e.originDomain!==J){if(!o.dns)throw Error(`External DNS provider is required to map ${e.originDomain} to ${J}`);let fe=await o.dns.upsertRecord(e.originDomain,{name:e.originDomain,type:"CNAME",content:J,ttl:300});if(!fe.success)throw Error(fe.message||"External DNS update failed");re=!0}let se=e.originDomain||J,ye=e.certificateArn?"https-only":"http-only",ne=e.skipHealthCheck?void 0:await hw(o,`${ye==="https-only"?"https":"http"}://${se}/api/health`,e.originVerifySecret);return await o.cloudfront.upsertExistingDistributionOrigin(e.distributionId,{id:r.originId,domainName:se,pathPattern:r.pathPattern,protocolPolicy:ye,customHeaders:e.originVerifySecret?{"X-Origin-Verify":e.originVerifySecret}:void 0}),{...N,outputs:K,health:ne,cloudFrontApplied:!0,dnsApplied:re,applied:!0}}function RA(e={}){let r={fargate:730*(e.desiredCount||1)*0.012342500000000001,applicationLoadBalancer:16.425,natGateway:32.85,postgres:730*(e.multiAzDatabase?0.032:0.016),redis:23.36};return{monthlyUsd:Number(Object.values(r).reduce((o,m)=>o+m,0).toFixed(2)),components:Object.fromEntries(Object.entries(r).map(([o,m])=>[o,Number(m.toFixed(2))])),assumptions:["us-east-1 public on-demand rate snapshot, July 2026","one 0.25 vCPU/0.5 GB Linux Fargate task unless desiredCount is changed","one ALB before LCU charges and one NAT gateway before data processing","single-AZ db.t4g.micro PostgreSQL by default and two cache.t4g.micro Redis nodes","excludes storage, backups, CloudFront, data transfer, public IPv4, logs, email, queue requests, taxes, and free-tier credits"]}}import{execFileSync as yw}from"node:child_process";import{createHash as vw}from"node:crypto";import{existsSync as Pf,mkdirSync as bw,mkdtempSync as Sw,readdirSync as ww,readFileSync as kw,rmSync as Iw,statSync as _f,writeFileSync as Ew}from"node:fs";import{tmpdir as Rw}from"node:os";import{basename as Aw,join as fa,relative as Cw,resolve as Zu}from"node:path";var Pw=new Set([".git","node_modules","dist","coverage"]);function Tf(e,t=e){let r=[];for(let o of ww(e).sort()){if(Pw.has(o))continue;let m=fa(e,o);if(Xc(m,t))continue;let h=_f(m);if(h.isDirectory())r.push(...Tf(m,t));else if(h.isFile())r.push(Cw(t,m))}return r}function Sm(e){let t=Zu(e);if(!Pf(t)||!_f(t).isDirectory())throw Error(`Container context does not exist: ${t}`);let r=vw("sha256");for(let o of Tf(t))r.update(o),r.update("\x00"),r.update(kw(fa(t,o))),r.update("\x00");return r.digest("hex")}function Cm(e){return{ecr:new Pe(e.region||"us-east-1",e.profile),run:(t,r,o)=>{yw(t,r,{cwd:o?.cwd,env:o?.env,stdio:o?.stdio||"inherit"})}}}async function DA(e,t){let r=Zu(e.context),o=Zu(e.dockerfile||fa(r,"Dockerfile"));if(!Pf(o))throw Error(`Dockerfile does not exist: ${o}`);if(!e.repository.split("/").every((K)=>/^[a-z0-9][a-z0-9._-]*$/.test(K)&&!/[._-]$/.test(K)&&!/[._-]{2}/.test(K)))throw Error("ECR repository name is invalid");let I=t||Cm(e),R=Sm(r),A=`sha-${R.slice(0,20)}`,E="";try{E=(await I.ecr.describeRepositories({repositoryNames:[e.repository]})).repositories?.[0]?.repositoryUri||""}catch{}if(!E)E=(await I.ecr.createRepository({repositoryName:e.repository,imageTagMutability:"IMMUTABLE",imageScanningConfiguration:{scanOnPush:!0},tags:[{Key:"ManagedBy",Value:"ts-cloud"}]})).repository?.repositoryUri||"";if(!E)throw Error(`Could not resolve ECR repository URI for ${e.repository}`);await I.ecr.putLifecyclePolicy({repositoryName:e.repository,lifecyclePolicyText:JSON.stringify({rules:[{rulePriority:1,description:"Retain the newest 25 release images",selection:{tagStatus:"any",countType:"imageCountMoreThan",countNumber:25},action:{type:"expire"}}]})});let x=`${E}:${A}`,N=Sw(fa(Rw(),"ts-cloud-container-"));try{I.run("docker",["version"],{stdio:"ignore"}),I.run("docker",["build","--platform",e.platform||"linux/amd64","--provenance=false","--sbom=false","--file",o,"--tag",x,r],{cwd:r});let K=(await I.ecr.getAuthorizationToken()).authorizationData?.[0];if(!K?.authorizationToken||!K.proxyEndpoint)throw Error("ECR did not return Docker authorization data");let J=K.proxyEndpoint.replace(/^https?:\/\//,""),re=fa(N,"docker");bw(re,{recursive:!0}),Ew(fa(re,"config.json"),JSON.stringify({auths:{[J]:{auth:K.authorizationToken}}}),{mode:384}),I.run("docker",["push",x],{env:{...process.env,DOCKER_CONFIG:re}})}finally{Iw(N,{recursive:!0,force:!0})}let z=(await I.ecr.describeImages({repositoryName:e.repository,imageIds:[{imageTag:A}]})).imageDetails?.[0]?.imageDigest;if(!z||!/^sha256:[a-f0-9]{64}$/i.test(z))throw Error(`ECR did not return an immutable digest for ${Aw(x)}`);return{repository:e.repository,tag:A,imageUri:x,digest:z,digestUri:`${E}@${z}`,contextSha256:R}}import{existsSync as _w,readdirSync as Tw,readFileSync as xw,statSync as Dw}from"node:fs";import{join as xf,relative as Ow}from"node:path";function Rs(e,t){let r=e.environments?.[t]?.app;if(!r)throw Error(`No serverless app configured for environment '${t}'. Add environments.${t}.app to your config.`);let o=e.project.slug,m=e.environments?.[t]?.region||e.project.region||"us-east-1";return{app:r,slug:o,region:m,stackName:Mt(e,t),artifactBucket:rd(o,t),assetsBucket:Qs(o,t)}}async function Is(e,t=8){let r;for(let o=0;o<t;o++)try{return await e()}catch(m){let h=m?.code||m?.name||"",I=String(m?.message||"");if(h==="ResourceConflictException"||/update is in progress|currently in the following state/i.test(I)){r=m,await new Promise((R)=>setTimeout(R,3000));continue}throw m}throw r}var Df=(e,t)=>`releases/${e}/${t}/current.json`;async function Lc(e,t,r,o){try{return await e.getObjectJson(t,Df(r,o))}catch{return null}}async function Of(e,t,r,o,m){await e.putObjectJson(t,Df(r,o),m)}async function $w(e,t){if(!e.secrets)return{};let r=new V(t),o={},m=Array.isArray(e.secrets)?e.secrets.map((h)=>({envName:h.split("/").pop().toUpperCase().replace(/[^A-Z0-9_]/g,"_"),secretId:h})):Object.entries(e.secrets).map(([h,I])=>({envName:h,secretId:I}));if(Array.isArray(e.secrets)){let h=new Map;for(let{envName:R,secretId:A}of m)h.set(R,[...h.get(R)??[],A]);let I=[...h.entries()].filter(([,R])=>R.length>1);if(I.length){let R=I.map(([A,E])=>`${A} ← ${E.join(", ")}`).join("; ");throw Error(`serverless secrets: multiple secrets map to the same env var (${R}). Use the map form { ENV_NAME: 'secret/id' } to disambiguate.`)}}for(let{envName:h,secretId:I}of m){let A=(await r.getSecretValue({SecretId:I})).SecretString??"";try{let E=JSON.parse(A);if(E&&typeof E==="object"&&!Array.isArray(E)){for(let[x,N]of Object.entries(E))o[x]=String(N);continue}}catch{}o[h]=A}return o}function Oi(e,t){let r={},o=t.DbProxyEndpoint||t.DbEndpoint;if(o)r.DB_CONNECTION="mysql",r.DB_HOST=o,r.DB_PORT="3306",r.DB_DATABASE=r.DB_DATABASE||"app";if(t.CacheEndpoint){if(r.REDIS_HOST=t.CacheEndpoint,r.REDIS_PORT="6379",e.cache?.driver==="elasticache")r.CACHE_STORE="redis",r.CACHE_DRIVER="redis",r.SESSION_DRIVER="redis"}return r}function Kt(e,t,r,o,m,h,I,R={}){let A=e.kind==="php"?yd({cacheDriver:e.cache?.driver==="elasticache"?"redis":"dynamodb"}):{};return{TSCLOUD_LAMBDA_MODE:o,TSCLOUD_ENV:r,MAINTENANCE_MODE:"0",...e.octane?{TSCLOUD_OCTANE:"1"}:{},...e.serveAssets?{TSCLOUD_SERVE_ASSETS:"1"}:{},...e.redirectRobotsTxt===!1?{TSCLOUD_REDIRECT_ROBOTS_TXT:"0"}:{},...A,...R,...t.app.cache?.driver!=="elasticache"?{TSCLOUD_CACHE_TABLE:`${t.slug}-${r}-cache`}:{},...I?{TSCLOUD_QUEUE:I,SQS_QUEUE:I}:{},...h?{ASSET_URL:h}:{},...e.env??{},...m}}function Nw(e){let t=e.slice(e.lastIndexOf(".")).toLowerCase();return{".html":"text/html",".css":"text/css",".js":"text/javascript",".mjs":"text/javascript",".json":"application/json",".svg":"image/svg+xml",".png":"image/png",".jpg":"image/jpeg",".jpeg":"image/jpeg",".gif":"image/gif",".webp":"image/webp",".ico":"image/x-icon",".woff":"font/woff",".woff2":"font/woff2",".txt":"text/plain",".map":"application/json"}[t]??"application/octet-stream"}function*$f(e){for(let t of Tw(e)){let r=xf(e,t);if(Dw(r).isDirectory())yield*$f(r);else yield r}}async function jw(e,t,r,o,m=!1){let h=0;for(let I of $f(r)){let R=Ow(r,I).replace(/\\/g,"/");if(!m&&R.split("/").some((A)=>A.startsWith(".")))continue;await e.putObject({bucket:t,key:`${o}/${R}`,body:xw(I),contentType:Nw(I),cacheControl:"public, max-age=31536000, immutable"}),h++}return h}function Mw(e,t){let r=Object.entries(t).reduce((o,[m,h])=>o+Buffer.byteLength(`${m}=${h}`,"utf8"),0);if(r>4096){let o=Object.entries(t).map(([m,h])=>[m,Buffer.byteLength(`${m}=${h}`,"utf8")]).sort((m,h)=>h[1]-m[1]).slice(0,5).map(([m,h])=>`${m} (${h}B)`).join(", ");throw Error(`${e}: environment variables total ${r}B, over AWS's 4096B limit (${r-4096}B too large). Largest: ${o}. Move large/secret values out of env — store them in Secrets Manager and fetch at cold start.`)}}async function Za(e,t,r,o){Mw(t,r),await Is(()=>e.updateFunctionConfiguration({FunctionName:t,Environment:{Variables:r}})),await e.waitForFunctionActive(t,120);let m=o.kind==="image"?{FunctionName:t,ImageUri:o.imageUri}:{FunctionName:t,S3Bucket:o.bucket,S3Key:o.key};await Is(()=>e.updateFunctionCode(m)),await e.waitForFunctionActive(t,120)}async function Bc(e,t){let o=(await Is(()=>e.publishVersion({FunctionName:t}))).Version;if(!o)throw Error(`publishVersion returned no version for ${t}`);return await Is(()=>e.updateAlias({FunctionName:t,Name:"live",FunctionVersion:o})),o}async function Xo(e,t,r={}){let o=Rs(e,t),{app:m,slug:h,region:I,stackName:R,artifactBucket:A}=o,E=r.projectRoot??process.cwd();Vi(`Deploying serverless app: ${h} (${t})`),P(`Stack: ${R}`),P(`Region: ${I}`);let x=new a(I),N=new f(I),F=new C(I),z=m.kind==="php",K=m.packaging==="image",J=Ws(m),re,se,ye,ne,fe=[];if(K&&J.usesLayer&&J.layerKind!=="php")throw Error(`packaging: 'image' currently supports PHP and managed Node runtimes. ${J.kind}${J.kind==="node"?` ${J.version}`:""} uses a custom runtime — `+"use the default zip packaging (the runtime layer is built + attached automatically).");if(K){v("Building + pushing container image");let{buildAndPushServerlessImage:Ee}=await import("./chunk-7by4rm7y.js"),Ne=await Ee({app:m,projectRoot:E,region:I,repository:`${h}-${t}`,skipBuild:r.skipBuild,onStep:(Rt)=>P(` ${Rt}`)});re=Ne.handlers,se={kind:"image",imageUri:Ne.imageUri},ye=Ne.tag,fe.push({ParameterKey:"ImageUri",ParameterValue:Ne.imageUri})}else{v("Packaging application");let Ee=z?Id({projectRoot:E,app:m,skipBuild:r.skipBuild,onStep:(Ut)=>P(` ${Ut}`)}):await _r({projectRoot:E,app:m,skipBuild:r.skipBuild,onStep:(Ut)=>P(` ${Ut}`)}),Ne=dd(h,t,Ee.sha256);if(P(`Artifact: ${Ee.sha256.slice(0,12)} (${(Ee.zip.length/1024).toFixed(0)} KB)`),re=Ee.handlers,ye=Ee.sha256,J.usesLayer){let Ut=J.layerEnvVar?process.env[J.layerEnvVar]:void 0;if(ne=m.layers??(Ut?[Ut]:void 0),!ne?.length){let Fe=J.layerKind==="php"?"serverless:build-php-layer":J.layerKind==="bun"?"serverless:build-bun-layer":"serverless:build-node-layer";throw Error(`${J.kind} apps on provided.al2023 need a runtime layer. Set environments.<env>.app.layers or the ${J.layerEnvVar} env var (build one with \`cloud ${Fe}\`)${J.kind==="node"?", or use a managed Node version (18/20/22)":""}.`)}}if(!await x.bucketExists(A))v(`Creating artifact bucket ${A}`),await x.createBucket(A);if(await x.headObject(A,Ne).then((Ut)=>Ut!==null).catch(()=>!1))P("Artifact already uploaded — reusing");else v("Uploading artifact"),await x.putObject({bucket:A,key:Ne,body:Ee.zip,contentType:"application/zip"});se={kind:"zip",bucket:A,key:Ne},fe.push({ParameterKey:"ArtifactBucket",ParameterValue:A},{ParameterKey:"ArtifactKey",ParameterValue:Ne})}let Ce=ud({config:e,environment:t,app:m,handlers:re,runtimeLayers:ne}),_e=JSON.stringify(Ce.template),he=["CAPABILITY_NAMED_IAM"],Ue=await N.describeStacks({stackName:R}).then((Ee)=>Ee.Stacks[0]?.StackStatus).catch(()=>{return}),ge=Ue!==void 0;if(Ue&&/DELETE_IN_PROGRESS/.test(Ue))v("Waiting for an in-progress stack delete to finish"),await N.waitForStack(R,"stack-delete-complete").catch(()=>{}),ge=!1;else if(Ue&&/(?:ROLLBACK_COMPLETE|REVIEW_IN_PROGRESS|CREATE_FAILED)/.test(Ue))v(`Deleting unusable stack (status ${Ue}) before recreate`),await N.deleteStack(R),await N.waitForStack(R,"stack-delete-complete").catch(()=>{}),ge=!1;v(ge?"Updating infrastructure stack":"Creating infrastructure stack");try{if(ge)await N.updateStack({stackName:R,templateBody:_e,parameters:fe,capabilities:he}),await N.waitForStack(R,"stack-update-complete");else await N.createStack({stackName:R,templateBody:_e,parameters:fe,capabilities:he,onFailure:"DELETE"}),await N.waitForStack(R,"stack-create-complete")}catch(Ee){if(/No updates are to be performed/i.test(String(Ee?.message)))P("No infrastructure changes");else throw Ee}let Ke=await N.getStackOutputs(R).catch(()=>({})),nt;if(m.assets){let Ee=xf(E,m.assets);if(_w(Ee)){let Ne=m.assetDomain||Ke.AssetsCdnDomain;nt=Ne?`https://${Ne}/${ye}`:void 0,v(`Syncing assets from ${m.assets}`);let Rt=await jw(x,o.assetsBucket,Ee,ye,m.dotFilesAsAssets);P(`Uploaded ${Rt} asset(s)${nt?` → ${nt}`:""}`)}else qi(`Assets directory not found: ${Ee}`)}v("Resolving secrets");let Et=await $w(m,I);if(m.database?.connection==="aurora-serverless")try{let Ne=await new V(I).getSecretValue({SecretId:`${h}/${t}/db`}),Rt=JSON.parse(Ne.SecretString??"{}");if(Rt.username)Et.DB_USERNAME=Rt.username;if(Rt.password)Et.DB_PASSWORD=Rt.password}catch(Ee){qi(`Could not resolve Aurora DB credentials: ${Ee.message}`)}if(!await x.bucketExists(A))await x.createBucket(A);let cr=await Lc(x,A,h,t),at={},Ft=Ce.queueNames[0],dr=Oi(m,Ke);if(v("Activating functions"),at.http=Kt(m,o,t,"http",Et,nt,Ft,dr),await Za(F,Ce.functionNames.http,at.http,se),at.cli=Kt(m,o,t,"cli",Et,nt,Ft,dr),await Za(F,Ce.functionNames.cli,at.cli,se),Ce.queueNames.length)at.queue=Kt(m,o,t,"queue",Et,nt,Ft,dr),await Za(F,Ce.functionNames.queue,at.queue,se);let bt=(m.provisionedConcurrency??0)>0,Y={};if(bt){if(v("Publishing versions + flipping live alias"),Y.http=await Bc(F,Ce.functionNames.http),Y.cli=await Bc(F,Ce.functionNames.cli),Ce.queueNames.length)Y.queue=await Bc(F,Ce.functionNames.queue)}if(await Of(x,A,h,t,{sha:ye,code:se,previousSha:cr?.sha,previousCode:cr?.code,previousFunctionEnv:cr?.functionEnv,...bt?{functionVersions:Y,previousFunctionVersions:cr?.functionVersions}:{},functionEnv:at,functionNames:{http:Ce.functionNames.http,cli:Ce.functionNames.cli,...Ce.queueNames.length?{queue:Ce.functionNames.queue}:{}},assetUrl:nt,timestamp:new Date().toISOString()}),!r.skipDeployHooks&&m.deploy?.length){v("Running deploy hooks");for(let Ee of m.deploy){P(` deploy: ${Ee}`);let Ne=await F.invoke({FunctionName:Ce.functionNames.cli,InvocationType:"RequestResponse",Payload:{command:Ee},LogType:"Tail"});if(Ne.FunctionError)throw Ul("Deploy hook failed — the new build is ALREADY LIVE."),P(` Roll back with: cloud serverless:rollback --env ${t}`),Error(`Deploy hook failed (${Ee}): ${Ne.Payload}`)}}let be=Ke.HttpApiEndpoint;if(!r.skipHealthCheck&&be){v("Health check");try{let Ee=await fetch(be,{method:"GET"});P(`GET ${be} → ${Ee.status}`)}catch(Ee){qi(`Health check could not reach ${be}: ${Ee.message}`)}}Ql(["Serverless deploy complete","",be?`URL: ${be}`:"",`Stack: ${R}`,`Build: ${ye.slice(0,12)}`].filter(Boolean).join(`
|
|
40
|
+
`),"green")}async function Kr(e,t){let r=Rs(e,t),o=new a(r.region),m=new C(r.region),h=await Lc(o,r.artifactBucket,r.slug,t);if(!h)throw Error("No previous release to redeploy. Run a full deploy first.");Vi(`Redeploying ${r.slug} (${t}) — build ${h.sha.slice(0,12)}`);for(let[I,R]of Object.entries(h.functionNames)){if(!R)continue;v(`Re-activating ${I} (${R})`),await Za(m,R,h.functionEnv[I]??{},h.code);let A=h.functionVersions?.[I];if(A)await Is(()=>m.updateAlias({FunctionName:R,Name:"live",FunctionVersion:A}))}tn("Redeploy complete")}async function Wr(e,t){let r=Rs(e,t),o=new a(r.region),m=new C(r.region),h=await Lc(o,r.artifactBucket,r.slug,t);if(!h?.previousCode)throw Error("No previous release to roll back to.");Vi(`Rolling back ${r.slug} (${t}) → ${h.previousSha?.slice(0,12)}`);for(let[I,R]of Object.entries(h.functionNames)){if(!R)continue;v(`Restoring ${I} (${R})`);let A=h.previousFunctionEnv?.[I]??h.functionEnv[I]??{};await Za(m,R,A,h.previousCode);let E=h.previousFunctionVersions?.[I];if(E)await Is(()=>m.updateAlias({FunctionName:R,Name:"live",FunctionVersion:E}))}await Of(o,r.artifactBucket,r.slug,t,{...h,sha:h.previousSha??h.sha,code:h.previousCode,functionEnv:h.previousFunctionEnv??h.functionEnv,functionVersions:h.previousFunctionVersions??h.functionVersions,previousSha:void 0,previousCode:void 0,previousFunctionEnv:void 0,previousFunctionVersions:void 0,timestamp:new Date().toISOString()}),tn("Rollback complete")}async function Wt(e,t,r,o){let m=Rs(e,t),h=new C(m.region),I=`${m.slug}-${t}-http`,A={...(await h.getFunction(I)).Configuration?.Environment?.Variables??{}};if(A.MAINTENANCE_MODE=r?"1":"0",r&&o)A.MAINTENANCE_BYPASS_SECRET=o;if(!r)delete A.MAINTENANCE_BYPASS_SECRET;if(await Is(()=>h.updateFunctionConfiguration({FunctionName:I,Environment:{Variables:A}})),(m.app.provisionedConcurrency??0)>0)await h.waitForFunctionActive(I,120),await Bc(h,I);tn(r?"Application is now in maintenance mode (503)":"Application is live")}async function je(e,t,r){let o=Rs(e,t),m=new C(o.region),h=`${o.slug}-${t}-cli`,I=await m.invoke({FunctionName:h,InvocationType:"RequestResponse",Payload:{command:r},LogType:"Tail"});if(I.FunctionError)throw Error(`Command failed: ${I.Payload}`);return I.Payload??""}async function Nr(e,t){let r=Rs(e,t),o=new f(r.region),m=new C(r.region),h=new a(r.region),I="not deployed",R={};try{let{Stacks:K}=await o.describeStacks({stackName:r.stackName});I=K?.[0]?.StackStatus??I;for(let J of K?.[0]?.Outputs??[])if(J.OutputKey)R[J.OutputKey]=J.OutputValue??""}catch{}let A=ld(r.app,r.slug,t),E=r.app.provisionedConcurrency??0,x=["http","cli",...A.length?["queue"]:[]],N=`${r.slug}-${t}`,F=[];for(let K of x){let J=`${N}-${K}`,re={mode:K,name:J,version:"$LATEST"};if(E>0){let se=await m.getAlias(J,"live").catch(()=>null);if(se?.FunctionVersion){re.version=se.FunctionVersion;let ye=await m.getProvisionedConcurrencyConfig(J,"live").catch(()=>null);if(ye)re.provisioned={status:ye.Status??"-",allocated:ye.AllocatedProvisionedConcurrentExecutions??0,requested:ye.RequestedProvisionedConcurrentExecutions??0}}}F.push(re)}let z=await Lc(h,r.artifactBucket,r.slug,t).catch(()=>null);return{slug:r.slug,environment:t,region:r.region,stackStatus:I,endpoint:R.HttpApiEndpoint,assetUrl:z?.assetUrl,scheduler:r.app.scheduler??"on",queues:A,provisionedConcurrency:E,functions:F,lastRelease:z?{sha:z.sha,timestamp:z.timestamp}:void 0}}function Bw(e,t){return`${e}-${t}-db`}async function Nf(e,t,r,o){let m=Rs(e,t);if(m.app.database?.connection!=="aurora-serverless")throw Error("No Aurora Serverless v2 database is configured for this environment.");let{RDSClient:h}=await import("./chunk-fdexq9e1.js"),I=new h(m.region),R=Bw(m.slug,t);v(`Scaling ${R} → ${r}–${o} ACUs`),await I.modifyDBCluster({DBClusterIdentifier:R,ServerlessV2ScalingConfiguration:{MinCapacity:r,MaxCapacity:o},ApplyImmediately:!0}),tn(`Scaling applied to ${R} (takes effect shortly).`)}class hi{client;region;constructor(e="us-east-1",t){this.region=e,this.client=new n(Nt(t))}async query(e){let t=new URLSearchParams(l({Version:"2010-08-01",...e})).toString();return this.client.request({service:"monitoring",region:this.region,method:"POST",path:"/",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:t})}async getMetricStatistics(e){let t={Action:"GetMetricStatistics",Namespace:e.Namespace,MetricName:e.MetricName,StartTime:e.StartTime.toISOString(),EndTime:e.EndTime.toISOString(),Period:e.Period};(e.Statistics??[]).forEach((h,I)=>{t[`Statistics.member.${I+1}`]=h}),(e.ExtendedStatistics??[]).forEach((h,I)=>{t[`ExtendedStatistics.member.${I+1}`]=h}),(e.Dimensions??[]).forEach((h,I)=>{t[`Dimensions.member.${I+1}.Name`]=h.Name,t[`Dimensions.member.${I+1}.Value`]=h.Value});let r=await this.query(t),m=(r.GetMetricStatisticsResult||r)?.Datapoints?.member??[];if(!Array.isArray(m))m=m?[m]:[];return m.map((h)=>{let I={},R=h.ExtendedStatistics?.entry;if(R){if(!Array.isArray(R))R=[R];for(let A of R)if(A?.key!=null)I[A.key]=Number(A.value)}return{Timestamp:h.Timestamp,Sum:h.Sum!=null?Number(h.Sum):void 0,Average:h.Average!=null?Number(h.Average):void 0,Maximum:h.Maximum!=null?Number(h.Maximum):void 0,Minimum:h.Minimum!=null?Number(h.Minimum):void 0,Percentiles:Object.keys(I).length?I:void 0,Unit:h.Unit}})}async getMetricSeries(e){let t=/^p\d/.test(e.Stat);return(await this.getMetricStatistics({Namespace:e.Namespace,MetricName:e.MetricName,Dimensions:e.Dimensions,StartTime:e.StartTime,EndTime:e.EndTime,Period:e.Period,Statistics:t?[]:[e.Stat],ExtendedStatistics:t?[e.Stat]:[]})).slice().sort((o,m)=>new Date(o.Timestamp??0).getTime()-new Date(m.Timestamp??0).getTime()).map((o)=>t?o.Percentiles?.[e.Stat]??0:o[e.Stat]??0)}async describeAlarms(e){let t={Action:"DescribeAlarms"};if(e?.AlarmNamePrefix)t.AlarmNamePrefix=e.AlarmNamePrefix;if(e?.MaxRecords)t.MaxRecords=e.MaxRecords;let r=await this.query(t),m=(r.DescribeAlarmsResult||r)?.MetricAlarms?.member??[];if(!Array.isArray(m))m=m?[m]:[];return m.map((h)=>({AlarmName:h.AlarmName,StateValue:h.StateValue,MetricName:h.MetricName,ComparisonOperator:h.ComparisonOperator,Threshold:h.Threshold!=null?Number(h.Threshold):void 0,Namespace:h.Namespace}))}async putMetricAlarm(e){let t={Action:"PutMetricAlarm",AlarmName:e.AlarmName,Namespace:e.Namespace,MetricName:e.MetricName,ComparisonOperator:e.ComparisonOperator,Threshold:e.Threshold,EvaluationPeriods:e.EvaluationPeriods,Period:e.Period,Statistic:e.Statistic};if(e.AlarmDescription)t.AlarmDescription=e.AlarmDescription;(e.Dimensions??[]).forEach((r,o)=>{t[`Dimensions.member.${o+1}.Name`]=r.Name,t[`Dimensions.member.${o+1}.Value`]=r.Value}),(e.AlarmActions??[]).forEach((r,o)=>{t[`AlarmActions.member.${o+1}`]=r}),await this.query(t)}async deleteAlarms(e){if(!e.length)return;let t={Action:"DeleteAlarms"};e.forEach((r,o)=>{t[`AlarmNames.member.${o+1}`]=r}),await this.query(t)}}class Xu{client;region;constructor(e="us-east-1"){this.region=e,this.client=new n}async call(e,t){return this.client.request({service:"wafv2",region:this.region,method:"POST",path:"/",headers:{"Content-Type":"application/x-amz-json-1.1","X-Amz-Target":`AWSWAF_20190729.${e}`},body:JSON.stringify(t)})}async listWebACLs(e="REGIONAL"){return(await this.call("ListWebACLs",{Scope:e,Limit:100}))?.WebACLs??[]}async getWebACLRules(e,t,r="REGIONAL"){return((await this.call("GetWebACL",{Name:e,Id:t,Scope:r}))?.WebACL?.Rules??[]).map((h)=>({Name:h.Name,Priority:h.Priority,Action:h.Action?Object.keys(h.Action)[0]:h.OverrideAction?Object.keys(h.OverrideAction)[0]:"count"}))}}var Lw=[{index:0,id:"clients",label:"Clients"},{index:1,id:"dns",label:"DNS & TLS"},{index:2,id:"edge",label:"Edge"},{index:3,id:"routing",label:"Routing"},{index:4,id:"apps",label:"Applications"},{index:5,id:"runtime",label:"Background runtime"},{index:6,id:"data",label:"Data services"},{index:7,id:"durability",label:"Durability"}],Fw=[{index:0,id:"clients",label:"Clients"},{index:1,id:"dns",label:"DNS & TLS"},{index:2,id:"edge",label:"Edge"},{index:3,id:"entry",label:"Entry points"},{index:4,id:"compute",label:"Compute"},{index:5,id:"messaging",label:"Messaging & pooling"},{index:6,id:"data",label:"Data services"},{index:7,id:"durability",label:"Durability"}],zw=[{match:/^redis/,kind:"cache",label:"Redis"},{match:/^(?:mysql|mariadb)/,kind:"database",label:"MySQL"},{match:/^postgres/,kind:"database",label:"PostgreSQL"},{match:/^meilisearch/,kind:"database",label:"Meilisearch"},{match:/^vitess|^vtgate/,kind:"database",label:"Vitess"},{match:/^memcached/,kind:"cache",label:"Memcached"}],Uw=/^(?:rpx-gateway|nginx|caddy|traefik|haproxy)$/;function Me(e,t=""){let r=e==null?"":String(e).trim();return r&&r!=="-"&&r!=="—"?r:t}function qw(e){return Array.isArray(e)?e.length:0}function qn(e){let t=Me(e).toLowerCase();if(!t)return"unknown";if(["running","live","active","available","success","ok","healthy","enabled"].includes(t))return"ok";if(["failed","stopped","unreachable","error","critical","disabled"].includes(t))return"bad";if(["configured","creating","pending","degraded","shadowed","warn","unknown"].includes(t))return"warn";return"warn"}function fr(e,t,r){if(r.has(t.source)&&r.has(t.target))e.push(t)}function ht(e,t={}){let r=[],o=[],m=[],h=e.server??{},I=Array.isArray(e.sites)?e.sites:[],R=Array.isArray(e.services)?e.services:[],A=Array.isArray(e.workers)?e.workers:[],E=e.security??{},x=!!e._serverReachable&&!e.metricsUnavailable,N="box";if(!x)m.push("The compute box could not be probed, so live service state is missing; the shape comes from your cloud config.");r.push({id:"internet",label:"Internet",kind:"internet",layer:0,status:"ok",sub:"Public traffic",facts:[],external:!0});let F=[...new Set(I.map((he)=>Me(he.domain)||Me(he.route).split("/")[0]).filter((he)=>he&&he!=="internal"&&he!=="loopback"))],z=Array.isArray(E.tlsCertificates)?E.tlsCertificates:[],K=z.filter((he)=>Number(he?.daysRemaining??999)<21).length;r.push({id:"dns",label:F.length===1?F[0]:"Public domains",kind:"dns",layer:1,status:K?"warn":F.length?"ok":"unknown",sub:F.length>1?`${F.length} domains`:"DNS & certificates",facts:[{label:"Domains",value:F.length?F.slice(0,6).join(", "):"none resolved"},{label:"Certificates",value:z.length?`${z.length} issued`:"none reported"},...K?[{label:"Expiring",value:`${K} within 21 days`}]:[]],href:"/server/security",external:!0});let J=E.firewall??{},re=Array.isArray(E.ports)?E.ports:[];r.push({id:"firewall",label:"Host firewall",kind:"firewall",layer:2,status:qn(J.status),sub:Me(J.summary,"Inbound filtering"),facts:[{label:"State",value:Me(J.status,"unknown")},{label:"Open ports",value:re.length?re.map((he)=>Me(he.listen)).join(", "):"not reported"}],href:"/server/firewall",group:"box"});let se=R.find((he)=>Uw.test(Me(he.name)));r.push({id:"proxy",label:Me(se?.name,"Reverse proxy"),kind:"proxy",layer:3,status:se?qn(se.status):"unknown",sub:"TLS termination & routing",facts:[{label:"Service",value:Me(se?.name,"not detected")},{label:"State",value:Me(se?.status,x?"not running":"not probed")},{label:"Routes",value:`${I.length} site${I.length===1?"":"s"}`}],href:"/server/services",group:"box"});for(let he of I){let Ue=`site:${Me(he.name)}`;if(!Me(he.name))continue;r.push({id:Ue,label:Me(he.name),kind:"site",layer:4,status:qn(he.status),sub:Me(he.runtime,"runtime"),facts:[{label:"Route",value:Me(he.route,"internal")},{label:"Runtime",value:Me(he.runtime,"unknown")},{label:"Delivery",value:Me(he.deploy,"unknown")},{label:"TLS",value:Me(he.tls,"unknown")},...he.responseMs!=null?[{label:"Response",value:`${Math.round(Number(he.responseMs))} ms`}]:[],...he.httpStatus!=null?[{label:"HTTP",value:String(he.httpStatus)}]:[]],href:"/server/sites",group:"box"})}for(let he of A){let Ue=`worker:${Me(he.name)}`;if(!Me(he.name))continue;r.push({id:Ue,label:Me(he.name),kind:"worker",layer:5,status:qn(he.status),sub:`${Number(he.processes??1)} process${Number(he.processes??1)===1?"":"es"}`,facts:[{label:"Processes",value:String(he.processes??1)},{label:"State",value:Me(he.status,"unknown")},...Me(he.site)?[{label:"Site",value:Me(he.site)}]:[]],href:"/operations/jobs",group:"box"})}let ye=e.serverScheduler??{};if(ye.enabled)r.push({id:"scheduler",label:"Scheduler",kind:"scheduler",layer:5,status:"ok",sub:"Cron dispatch",facts:[{label:"Last run",value:Me(ye.lastRun,"unknown")}],href:"/operations/jobs",group:"box"});let ne=R.map((he)=>{let Ue=Me(he.name),ge=zw.find((Ke)=>Ke.match.test(Ue));return ge?{service:he,name:Ue,...ge}:null}).filter((he)=>!!he);for(let he of ne)r.push({id:`data:${he.name}`,label:he.label,kind:he.kind,layer:6,status:qn(he.service.status),sub:he.name,facts:[{label:"Unit",value:he.name},{label:"State",value:Me(he.service.status,"unknown")}],href:"/data/services",group:"box"});let fe=e.backup??{};if(fe.enabled)r.push({id:"backup",label:"Backups",kind:"backup",layer:7,status:"ok",sub:Me(fe.destination,"destination"),facts:[{label:"Destination",value:Me(fe.destination,"unknown")},{label:"Schedule",value:Me(fe.schedule,"unknown")},{label:"Retention",value:`${fe.retention??0} copies`},{label:"Last run",value:Me(fe.last,"unknown")}],href:"/data/backups"});let Ce=new Set(r.map((he)=>he.id));fr(o,{source:"internet",target:"dns",flow:"request",label:"https"},Ce),fr(o,{source:"dns",target:"firewall",flow:"request",label:"443/80"},Ce),fr(o,{source:"firewall",target:"proxy",flow:"request"},Ce);for(let he of I){let Ue=`site:${Me(he.name)}`;fr(o,{source:"proxy",target:Ue,flow:"request",label:Me(he.path)||void 0,status:qn(he.status)},Ce)}for(let he of A){let Ue=`worker:${Me(he.name)}`,ge=`site:${Me(he.site)}`;if(Ce.has(ge))fr(o,{source:ge,target:Ue,flow:"async",label:"queue"},Ce);else fr(o,{source:"proxy",target:Ue,flow:"async",label:"queue"},Ce)}for(let he of A)fr(o,{source:"scheduler",target:`worker:${Me(he.name)}`,flow:"async",label:"dispatch"},Ce);let _e=[...I.map((he)=>`site:${Me(he.name)}`),...A.map((he)=>`worker:${Me(he.name)}`)];for(let he of ne)for(let Ue of _e)fr(o,{source:Ue,target:`data:${he.name}`,flow:"data",label:"localhost"},Ce);if(ne.length&&_e.length)m.push("Data-service edges show co-location on the box: every process on it can reach localhost. They are not a declared per-site binding.");for(let he of ne)fr(o,{source:`data:${he.name}`,target:"backup",flow:"backup",label:"snapshot"},Ce);for(let he of I)fr(o,{source:`site:${Me(he.name)}`,target:"backup",flow:"backup"},Ce);return{mode:"server",project:Me(t.project)||Me(e.project?.name)||Me(h.name,"project"),environment:Me(t.environment)||Me(e.environment,"production"),provider:Me(h.provider,"unknown"),region:Me(h.region,"unknown"),generatedAt:new Date().toISOString(),layers:Lw,groups:[{id:"box",label:Me(h.name,"App server"),sub:[Me(h.provider),Me(h.region),Me(h.ip)].filter(Boolean).join(" · ")}],nodes:r,links:o,notes:m}}function Li(e,t={}){let r=[],o=[],m=[],h=e.app??{},I=Array.isArray(e.functions)?e.functions:[],R=Array.isArray(e.queues)?e.queues:[],A=e.metrics??{};if(!I.length)m.push("No Lambda functions were read for this environment, so the compute layer is empty rather than assumed.");r.push({id:"internet",label:"Internet",kind:"internet",layer:0,status:"ok",sub:"Public traffic",facts:[],external:!0});let E=Me(h.url),x=E.replace(/^https?:\/\//,"").split("/")[0];r.push({id:"dns",label:x||"Application domain",kind:"dns",layer:1,status:x?"ok":"unknown",sub:"DNS & certificates",facts:[{label:"Endpoint",value:E||"not resolved"}],external:!0});let N=e.waf;if(N)r.push({id:"waf",label:"WAF",kind:"waf",layer:2,status:qn(N.status??"active"),sub:Me(N.scope,"Managed rules"),facts:[{label:"Web ACL",value:Me(N.name,"unnamed")},...N.blocked!=null?[{label:"Blocked (24h)",value:String(N.blocked)}]:[],{label:"Rules",value:String(qw(e.wafRules))}],href:"/serverless/firewall"});let F=e.assetsInfo;if(F)r.push({id:"cdn",label:"Asset CDN",kind:"cdn",layer:2,status:"ok",sub:Me(F.cdn,"CloudFront"),facts:[{label:"Distribution",value:Me(F.cdn,"unknown")},{label:"Custom domain",value:Me(F.customDomain,"none")}],href:"/serverless/assets"}),r.push({id:"assets",label:"Asset bucket",kind:"storage",layer:6,status:"ok",sub:Me(F.bucket,"S3"),facts:[{label:"Bucket",value:Me(F.bucket,"unknown")},...F.files?[{label:"Objects",value:String(F.files)}]:[],...F.sizeMb?[{label:"Size",value:`${F.sizeMb} MB`}]:[]],href:"/serverless/assets"});let z=I.find((ge)=>Me(ge.key).startsWith("http"));r.push({id:"gateway",label:"HTTP entry point",kind:"gateway",layer:3,status:z?"ok":"unknown",sub:"Function URL / API Gateway",facts:[{label:"Endpoint",value:E||"not resolved"},{label:"Invocations (24h)",value:String(A.invocations??0)},...A.errorRatePct!=null?[{label:"Error rate",value:`${A.errorRatePct}%`}]:[]],href:"/serverless/metrics"});for(let ge of I){let Ke=`fn:${Me(ge.name)}`;if(!Me(ge.name))continue;let nt=Number(ge.errors??0),Et=Number(ge.invocations??0);r.push({id:Ke,label:Me(ge.key,Me(ge.name)),kind:"function",layer:4,status:nt>0?Et&&nt/Et>0.02?"bad":"warn":Et?"ok":"idle",sub:Me(ge.runtime,"lambda"),facts:[{label:"Function",value:Me(ge.name)},{label:"Memory",value:`${ge.memory??0} MB`},{label:"Timeout",value:`${ge.timeout??0}s`},{label:"Invocations (24h)",value:String(Et)},{label:"Errors (24h)",value:String(nt)},{label:"p95",value:`${ge.p95??0} ms`},...ge.provisioned?[{label:"Provisioned",value:Me(ge.provisioned)}]:[]],href:"/serverless/functions"})}for(let ge of R){let Ke=`queue:${Me(ge.name)}`;if(!Me(ge.name))continue;let nt=Number(ge.dlq??0);r.push({id:Ke,label:Me(ge.name),kind:"queue",layer:5,status:nt>0?"bad":Number(ge.visible??0)>0?"warn":"ok",sub:"SQS",facts:[{label:"Visible",value:String(ge.visible??0)},{label:"In flight",value:String(ge.inFlight??0)},{label:"Processed (24h)",value:String(ge.processed??0)},{label:"Dead letter",value:String(nt)}],href:"/serverless/queues"})}let K=e.aurora;if(K)r.push({id:"database",label:"Aurora Serverless",kind:"database",layer:6,status:qn(K.status),sub:Me(K.engine,"aurora"),facts:[{label:"Cluster",value:Me(K.id,"unknown")},{label:"Capacity",value:`${K.currentAcu??0} ACU (${K.minAcu??0}–${K.maxAcu??0})`},{label:"Connections",value:String(K.connections??0)}],href:"/serverless/data"});let J=e.proxy;if(J)r.push({id:"rds-proxy",label:"RDS Proxy",kind:"database",layer:5,status:qn(J.status),sub:"Connection pooling",facts:[{label:"Proxy",value:Me(J.name,"unknown")},{label:"Endpoint",value:Me(J.endpoint,"unknown")},{label:"Pooled",value:String(J.pooledConns??0)}],href:"/serverless/data"});let re=e.redis;if(re)r.push({id:"cache",label:"ElastiCache",kind:"cache",layer:6,status:qn(re.status),sub:Me(re.node,"redis"),facts:[{label:"Group",value:Me(re.id,"unknown")},{label:"Hit rate",value:`${re.hitRate??0}%`}],href:"/serverless/data"});let se=e.efs;if(se)r.push({id:"efs",label:"EFS",kind:"filesystem",layer:6,status:qn(se.status),sub:Me(se.mount,"/mnt/local"),facts:[{label:"File system",value:Me(se.id,"unknown")},{label:"Mount",value:Me(se.mount,"unknown")},{label:"Size",value:`${se.sizeMb??0} MB`}],href:"/data/volumes"});let ye=e.scheduler??{};if(ye.enabled)r.push({id:"scheduler",label:"Scheduler",kind:"scheduler",layer:3,status:"ok",sub:Me(ye.expression,"rate(1 minute)"),facts:[{label:"Expression",value:Me(ye.expression,"unknown")},{label:"Last run",value:Me(ye.lastRun,"unknown")}],href:"/operations/jobs"});let ne=new Set(r.map((ge)=>ge.id)),fe=ne.has("waf")?"waf":"gateway";fr(o,{source:"internet",target:"dns",flow:"request",label:"https"},ne),fr(o,{source:"dns",target:fe,flow:"request"},ne),fr(o,{source:"dns",target:"cdn",flow:"request",label:"assets"},ne),fr(o,{source:"waf",target:"gateway",flow:"request",label:"allowed"},ne),fr(o,{source:"cdn",target:"assets",flow:"data",label:"origin"},ne);let Ce=I.filter((ge)=>Me(ge.key).startsWith("http")),_e=I.filter((ge)=>Me(ge.key).startsWith("queue")),he=I.filter((ge)=>Me(ge.key).startsWith("cli"));for(let ge of Ce)fr(o,{source:"gateway",target:`fn:${Me(ge.name)}`,flow:"request",label:"invoke"},ne);for(let ge of he)fr(o,{source:"scheduler",target:`fn:${Me(ge.name)}`,flow:"async",label:"schedule"},ne);for(let ge of R){for(let Ke of Ce)fr(o,{source:`fn:${Me(Ke.name)}`,target:`queue:${Me(ge.name)}`,flow:"async",label:"publish"},ne);for(let Ke of _e)fr(o,{source:`queue:${Me(ge.name)}`,target:`fn:${Me(Ke.name)}`,flow:"async",label:"consume"},ne)}let Ue=["rds-proxy","database","cache","efs","assets"];for(let ge of I){let Ke=`fn:${Me(ge.name)}`;if(ne.has("rds-proxy"))fr(o,{source:Ke,target:"rds-proxy",flow:"data",label:"pooled"},ne);else fr(o,{source:Ke,target:"database",flow:"data",label:"sql"},ne);for(let nt of Ue.filter((Et)=>Et!=="rds-proxy"&&Et!=="database"))fr(o,{source:Ke,target:nt,flow:"data"},ne)}return fr(o,{source:"rds-proxy",target:"database",flow:"data",label:"sql"},ne),{mode:"serverless",project:Me(t.project)||Me(h.name,"application"),environment:Me(t.environment)||Me(h.env,"production"),provider:"aws",region:Me(h.region,"unknown"),generatedAt:new Date().toISOString(),layers:Fw,groups:[],nodes:r,links:o,notes:m}}function bm(e,t,r={}){return e==="serverless"?Li(t,r):ht(t,r)}function jf(e){return String(e).padStart(2,"0")}function ha(e){return`${e.getUTCFullYear()}-${jf(e.getUTCMonth()+1)}-${jf(e.getUTCDate())}`}function Vw(e){let t=Math.max(0,Math.round((Date.now()-e)/1000));if(t<60)return`${t}s ago`;let r=Math.round(t/60);if(r<60)return`${r}m ago`;let o=Math.round(r/60);if(o<48)return`${o}h ago`;return`${Math.round(o/24)}d ago`}async function Xa(e,t,r,o,m,h){let I=await e.getMetricStatistics({Namespace:"AWS/Lambda",MetricName:r,Dimensions:[{Name:"FunctionName",Value:t}],StartTime:m,EndTime:h,Period:86400,Statistics:[o]}).catch(()=>[]);if(!I.length)return 0;if(o==="Sum")return I.reduce((R,A)=>R+(A.Sum??0),0);if(o==="Maximum")return Math.max(...I.map((R)=>R.Maximum??0));return I.reduce((R,A)=>R+(A.Average??0),0)/I.length}async function ec(e,t,r,o,m,h,I){let R=await e.getMetricStatistics({Namespace:t,MetricName:r,Dimensions:o,StartTime:h,EndTime:I,Period:86400,Statistics:[m]}).catch(()=>[]);if(!R.length)return 0;if(m==="Sum")return R.reduce((A,E)=>A+(E.Sum??0),0);if(m==="Maximum")return Math.max(...R.map((A)=>A.Maximum??0));return R.reduce((A,E)=>A+(E.Average??0),0)/R.length}async function ep(e,t,r,o,m){let h=await e.getMetricStatistics({Namespace:"AWS/Lambda",MetricName:"Duration",Dimensions:[{Name:"FunctionName",Value:t}],StartTime:o,EndTime:m,Period:86400,ExtendedStatistics:[r]}).catch(()=>[]);return Math.round(h.reduce((I,R)=>I+(R.Percentiles?.[r]??0),0))}async function Jr(e,t){let r=await Nr(e,t),o=r.region,m=new hi(o),h=new C(o),I=new ue(o),R=new Date,A=new Date(R.getTime()-86400000),E=e.environments?.[t]?.app,x={};x.app={name:r.slug,env:t,region:o,runtime:E?.kind==="php"?`php-${E?.phpVersion??"8.3"} (fpm)`:E?.kind??"node",url:r.endpoint??"",build:r.lastRelease?.sha?.slice(0,7)??"—",deployedAt:r.lastRelease?.timestamp??"—"},x.maintenance={enabled:!1},x.scheduler={enabled:r.scheduler!=="off",expression:r.scheduler==="sub-minute"?"rate(1 minute) · sub-minute":"rate(1 minute)",lastRun:"—"};let N=0,F=0,z=0,K=[],J=[],re=[];for(let fe of r.functions){let[Ce,_e,he,Ue,ge,Ke,nt,Et,cr]=await Promise.all([Xa(m,fe.name,"Invocations","Sum",A,R),Xa(m,fe.name,"Errors","Sum",A,R),Xa(m,fe.name,"Throttles","Sum",A,R),Xa(m,fe.name,"Duration","Average",A,R),h.getFunction(fe.name).catch(()=>null),ep(m,fe.name,"p50",A,R),ep(m,fe.name,"p95",A,R),ep(m,fe.name,"p99",A,R),m.getMetricSeries({Namespace:"AWS/Lambda",MetricName:"Invocations",Dimensions:[{Name:"FunctionName",Value:fe.name}],StartTime:new Date(R.getTime()-43200000),EndTime:R,Period:3600,Stat:"Sum"}).catch(()=>[])]);N+=Ce,F+=_e,z=Math.max(z,Ue);let at=ge?.Configuration?.MemorySize??0,Ft=Object.keys(ge?.Configuration?.Environment?.Variables??{}),dr=[],bt=0;try{let{events:be=[]}=await I.filterLogEvents({logGroupName:`/aws/lambda/${fe.name}`,startTime:A.getTime(),limit:120});for(let Ee of be){let Ne=(Ee.message??"").trim(),Rt=/Max Memory Used:\s*(\d+)\s*MB/.exec(Ne);if(Rt)bt=Math.max(bt,Number(Rt[1]));if(Ne&&!/^(?:START|END|REPORT|INIT_START)/.test(Ne))re.push({source:fe.mode||fe.name,timestamp:new Date(Ee.timestamp??0).toISOString(),when:Vw(Ee.timestamp??0),message:Ne.slice(0,400),level:/error|exception|fatal|panic|traceback/i.test(Ne)?"error":/warn|warning|timeout|retr(?:y|ies)|throttl/i.test(Ne)?"warn":"info"})}dr=be.filter((Ee)=>!/^(?:START|END|REPORT|INIT_START)/.test((Ee.message??"").trim())).slice(-6).map((Ee)=>({ts:new Date(Ee.timestamp??0).toISOString().slice(11,19),msg:(Ee.message??"").trim().slice(0,120),err:/error|exception|fatal/i.test(Ee.message??"")}))}catch{}let Y=Math.max(1,...cr);K.push({key:fe.mode,name:fe.name,version:fe.version,memory:at,timeout:ge?.Configuration?.Timeout??0,runtime:ge?.Configuration?.Runtime??"provided.al2023",invocations:Math.round(Ce),errors:Math.round(_e),p95:nt||Math.round(Ue),status:"active",provisioned:fe.provisioned?`${fe.provisioned.allocated}/${fe.provisioned.requested}`:void 0}),J.push({key:fe.mode,name:fe.name,runtime:ge?.Configuration?.Runtime??"provided.al2023",arch:ge?.Configuration?.Architectures?.[0]??"x86_64",memory:at,timeout:ge?.Configuration?.Timeout??0,ephemeral:ge?.Configuration?.EphemeralStorage?.Size??512,version:fe.version,concurrency:E?.concurrency??"—",invocations:Math.round(Ce),errors:Math.round(_e),throttles:Math.round(he),p50:Ke||Math.round(Ue),p95:nt||Math.round(Ue),p99:Et||Math.round(Ue),maxMem:bt,spark:cr.length?cr.map((be)=>Math.round(be/Y*100)):[0],env:Ft.length?Ft:["(none)"],recent:dr.length?dr:[{ts:"—",msg:"no recent events",err:!1}]})}x.functions=K,x.functionsDetail=J,x.serverlessLogs=re.sort((fe,Ce)=>String(Ce.timestamp).localeCompare(String(fe.timestamp))).slice(0,600),x.serverlessLogsEmptyReason=re.length?void 0:"No recent function logs were found in CloudWatch for this environment.";let se=N?Number((F/N*100).toFixed(2)):0,ye=Math.round(await Xa(m,r.functions[0]?.name??"","ConcurrentExecutions","Maximum",A,R));x.metrics={invocations:Math.round(N),errorRatePct:se,p95Ms:Math.round(z),coldStartPct:void 0,concurrency:ye,estCostUsd:void 0};try{let fe=new te(o),Ce=[],_e=[],he=0;try{let{QueueUrl:ge}=await fe.getQueueUrl(`${r.slug}-${t}-dlq`),{Attributes:Ke}=await fe.getQueueAttributes(ge);he=Number(Ke.ApproximateNumberOfMessages??0)}catch{}let Ue=E?Gt(E,r.slug,t):[];for(let ge of Ue){let Ke=ge.name.replace(`${r.slug}-${t}-`,""),nt=0,Et=0;try{let{QueueUrl:at}=await fe.getQueueUrl(ge.name),{Attributes:Ft}=await fe.getQueueAttributes(at);nt=Number(Ft.ApproximateNumberOfMessages??0),Et=Number(Ft.ApproximateNumberOfMessagesNotVisible??0)}catch{}let cr=Math.round(await ec(m,"AWS/SQS","NumberOfMessagesDeleted",[{Name:"QueueName",Value:ge.name}],"Sum",A,R));Ce.push({name:Ke,visible:nt,inFlight:Et,processed:cr,dlq:he}),_e.push({name:ge.name,short:Ke,visible:nt,inFlight:Et,delayed:0,processed:cr,oldestSec:0,concurrency:ge.concurrency??E?.queueConcurrency??"—",tries:E?.queueTries??3,visTimeout:(E?.queueTimeout??120)*6,dlq:he})}if(Ce.length)x.queues=Ce;if(_e.length)x.queuesDetail=_e;x.dlqItems=[]}catch{}try{let fe=new f(o),{Stacks:Ce}=await fe.describeStacks({stackName:Mt(e,t)}),_e={};for(let he of Ce?.[0]?.Outputs??[])if(he.OutputKey)_e[he.OutputKey]=he.OutputValue??"";if(E?.database?.connection==="aurora-serverless"){let he=`${r.slug}-${t}-db`,Ue=[{Name:"DBClusterIdentifier",Value:he}],[ge,Ke]=await Promise.all([ec(m,"AWS/RDS","ServerlessDatabaseCapacity",Ue,"Average",A,R),ec(m,"AWS/RDS","DatabaseConnections",Ue,"Maximum",A,R)]);if(x.aurora={id:he,engine:"aurora-mysql",minAcu:E?.database?.minCapacity??0.5,maxAcu:E?.database?.maxCapacity??4,currentAcu:ge?Number(ge.toFixed(1)):E?.database?.minCapacity??0.5,database:"app",connections:Math.round(Ke),status:_e.DbEndpoint?"available":"creating"},E?.rdsProxy)x.proxy={name:`${r.slug}-${t}-proxy`,endpoint:_e.DbProxyEndpoint??"—",pooledConns:Math.round(Ke),status:"available"}}if(E?.cache?.driver==="elasticache"){let he=`${r.slug}-${t}-cache`,Ue=[{Name:"CacheClusterId",Value:`${he}-001`}],[ge,Ke]=await Promise.all([ec(m,"AWS/ElastiCache","CacheHits",Ue,"Sum",A,R),ec(m,"AWS/ElastiCache","CacheMisses",Ue,"Sum",A,R)]),nt=ge+Ke>0?Number((ge/(ge+Ke)*100).toFixed(1)):0;x.redis={id:he,node:"cache.t4g.micro",engine:"redis",hitRate:nt,status:_e.CacheEndpoint?"available":"creating"}}if(E?.efs){let he=0;try{if(_e.EfsFileSystemId){let Ue=new tr(o),{FileSystems:ge}=await Ue.describeFileSystems({FileSystemId:_e.EfsFileSystemId});he=Number(((ge?.[0]?.SizeInBytes?.Value??0)/1048576).toFixed(1))}}catch{}x.efs={id:_e.EfsFileSystemId??"efs",mount:"/mnt/local",sizeMb:he,status:"available"}}x.assetsInfo=_e.AssetsCdnDomain?{bucket:_e.AssetsBucketName??`${r.slug}-${t}-assets`,cdn:_e.AssetsCdnDomain,customDomain:E?.assetDomain??"—",assetUrl:r.assetUrl??"—",files:0,sizeMb:0,cacheHitPct:0,build:x.app.build}:void 0}catch{}if(x.assetsInfo)try{let Ce=await new a(o).list({bucket:x.assetsInfo.bucket,maxKeys:1000});x.assetsInfo.files=Ce.length,x.assetsInfo.sizeMb=Number((Ce.reduce((_e,he)=>_e+(he.Size??0),0)/1048576).toFixed(1)),x.assetsRecent=Ce.slice(0,8).map((_e)=>({path:`/${_e.Key}`,size:`${Math.max(1,Math.round((_e.Size??0)/1024))} KB`,type:_e.Key.split(".").pop()??""}))}catch{}try{if(E?.secrets){let fe=Array.isArray(E.secrets)?E.secrets.map((Ce)=>({key:Ce.split("/").pop().toUpperCase().replace(/[^A-Z0-9_]/g,"_"),source:Ce})):Object.entries(E.secrets).map(([Ce,_e])=>({key:Ce,source:String(_e)}));x.secretsList=fe.map((Ce)=>({...Ce,updated:"—"}))}}catch{}try{let fe=new a(o),Ce=`${r.slug}-${t}-deployments`,_e=await fe.getObjectJson(Ce,`deployments/${r.slug}/${t}/history.json`).catch(()=>null);if(_e?.records?.length)x.deploymentsDetail=_e.records.slice(-12).reverse().map((he)=>({sha:(he.sha??"").slice(0,7),status:he.status??"success",when:he.timestamp??"—",took:he.took??"—",by:he.by??"—",version:he.version??"—",hooks:he.hooks??[]}))}catch{}x.metricsTotals={invocations:x.metrics.invocations,errors:Math.round(F),errorRatePct:se,p95Ms:x.metrics.p95Ms,throttles:J.reduce((fe,Ce)=>fe+Ce.throttles,0),coldStartPct:void 0,estCostUsd:void 0,concurrency:ye},x.metricsPerFn=J.map((fe)=>({key:fe.key,invocations:fe.invocations,errors:fe.errors,throttles:fe.throttles,avgMs:fe.p50,maxMs:fe.p99,costUsd:void 0}));try{x.invSpark=await m.getMetricSeries({Namespace:"AWS/Lambda",MetricName:"Invocations",Dimensions:[{Name:"FunctionName",Value:r.functions.find((fe)=>fe.mode==="http")?.name??""}],StartTime:new Date(R.getTime()-86400000),EndTime:R,Period:3600,Stat:"Sum"})}catch{}try{let fe=new Wi,Ce=R,_e=new Date(Date.UTC(Ce.getUTCFullYear(),Ce.getUTCMonth(),1)),he=new Date(Date.UTC(Ce.getUTCFullYear(),Ce.getUTCMonth()-1,1)),[Ue,ge]=await Promise.all([fe.getCostByService({start:ha(_e),end:ha(Ce),granularity:"MONTHLY"}).catch(()=>[]),fe.getCostByService({start:ha(he),end:ha(_e),granularity:"MONTHLY"}).catch(()=>[])]);if(Ue.length){let Ke=Ue.reduce((bt,Y)=>bt+Y.amount,0),nt=ge.reduce((bt,Y)=>bt+Y.amount,0),Et=Ce.getUTCDate(),cr=new Date(Date.UTC(Ce.getUTCFullYear(),Ce.getUTCMonth()+1,0)).getUTCDate();x.costSummary={monthToDateUsd:Number(Ke.toFixed(2)),projectedUsd:Number((Ke/Et*cr).toFixed(2)),lastMonthUsd:Number(nt.toFixed(2)),dailyAvgUsd:Number((Ke/Et).toFixed(2))},x.costServices=Ue.filter((bt)=>bt.amount>0).sort((bt,Y)=>Y.amount-bt.amount).slice(0,12).map((bt)=>({name:bt.service,usd:Number(bt.amount.toFixed(2)),note:""})),x.metrics.estCostUsd=Number((Ke/Et).toFixed(2)),x.metricsTotals.estCostUsd=x.metrics.estCostUsd,x.costTrend=await fe.getDailyTotals({start:ha(_e),end:ha(Ce)}).catch(()=>[]);let at=Ue.find((bt)=>/lambda/i.test(bt.service))?.amount??0,Ft=(bt)=>bt.invocations*(bt.p50/1000)*(bt.memory/1024),dr=J.reduce((bt,Y)=>bt+Ft(Y),0)||1;x.costPerFn=J.map((bt)=>({key:bt.key,requests:bt.invocations,gbSec:Math.round(Ft(bt)),usd:Number((at*(Ft(bt)/dr)).toFixed(2))}))}}catch{}if(E?.firewall)try{let fe=await m.getMetricStatistics({Namespace:"AWS/WAFV2",MetricName:"BlockedRequests",StartTime:A,EndTime:R,Period:86400,Statistics:["Sum"]}).catch(()=>[]),Ce=await m.getMetricStatistics({Namespace:"AWS/WAFV2",MetricName:"AllowedRequests",StartTime:A,EndTime:R,Period:86400,Statistics:["Sum"]}).catch(()=>[]);x.waf={enabled:!0,scope:"REGIONAL",acl:`${r.slug}-${t}-waf`,allowed24h:Math.round(Ce.reduce((_e,he)=>_e+(he.Sum??0),0)),blocked24h:Math.round(fe.reduce((_e,he)=>_e+(he.Sum??0),0))};try{let _e=new Xu(o),he=await _e.listWebACLs("REGIONAL"),Ue=he.find((ge)=>ge.Name?.includes(`${r.slug}-${t}`))??he[0];if(Ue?.Name&&Ue.Id){x.waf.acl=Ue.Name;let ge=await _e.getWebACLRules(Ue.Name,Ue.Id,"REGIONAL");if(ge.length)x.wafRules=ge.map((Ke)=>({name:Ke.Name??"—",detail:`priority ${Ke.Priority??0}`,action:Ke.Action??"count",blocked24h:0}))}}catch{}}catch{}let ne=[];if(x.aurora)ne.push({name:"Aurora Serverless v2",detail:`${x.aurora.id} · ${x.aurora.minAcu}-${x.aurora.maxAcu} ACU`,status:x.aurora.status});if(x.proxy)ne.push({name:"RDS Proxy",detail:x.proxy.name,status:x.proxy.status});if(x.redis)ne.push({name:`ElastiCache (${x.redis.engine})`,detail:`${x.redis.node} · ${x.redis.hitRate}% hit rate`,status:x.redis.status});if(x.efs)ne.push({name:"EFS",detail:`${x.efs.mount} · ${x.efs.sizeMb} MB`,status:x.efs.status});if(ne.length)x.dataServices=ne;if(r.scheduler!=="off"&&E?.kind==="php")try{let fe=await je(e,t,"schedule:list"),Ce=[];for(let _e of(fe||"").split(`
|
|
41
|
+
`)){let he=/^\s*([\d*/,\-\s]+?)\s{2,}(.+?)(?:\s{2,}Next Due.*)?$/.exec(_e);if(he&&/[*\d]/.test(he[1])&&he[1].trim().split(/\s+/).length===5)Ce.push({command:he[2].trim().replace(/^php artisan /,""),cron:he[1].trim(),desc:"",lastRun:"—",lastStatus:"ok"})}if(Ce.length)x.schedulerTasks=Ce}catch{}return x.topology=Li(x,{project:e.project.name,environment:t}),x}function ts(e){let t=Number(e);return Number.isFinite(t)&&t>=0?t:null}function Ww(e){if(!/^\d{4}-\d{2}-\d{2}$/.test(e))return null;let t=Date.parse(`${e}T12:00:00.000Z`);return Number.isFinite(t)?new Date(t).toISOString():null}function kl(e,t){if(!e||typeof e!=="object")return[];let r={source:t.name},o=[],m=t.now,h=(E,x,N,F=m)=>{if(x!==null)o.push({name:E,value:x,unit:N,timestamp:F,attributes:r})};h("storage.egress.month",ts(e.monthBytes),"bytes"),h("storage.egress.today",ts(e.todayBytes),"bytes"),h("storage.egress.projected_month",ts(e.projectedMonthBytes),"bytes");let I=ts(e.budgetBytes);if(I!==null&&I>0){h("storage.egress.budget",I,"bytes");let E=e.budgetUsedPercent,x=typeof E==="number"&&Number.isFinite(E)?E:ts(e.monthBytes)!==null?Math.round(ts(e.monthBytes)/I*1000)/10:null;h("storage.egress.budget_used_percent",x,"percent")}let R=t.maxReplayDays??45,A=Array.isArray(e.days)?e.days.slice(-R):[];for(let E of A){let x=Ww(String(E?.date??"")),N=ts(E?.bytes);if(!x||N===null)continue;o.push({name:"storage.egress.day",value:N,unit:"bytes",timestamp:x,attributes:r});let F=ts(E?.downloads);if(F!==null)o.push({name:"storage.egress.day_downloads",value:F,unit:"count",timestamp:x,attributes:r})}return o}async function Bl(e,t=process.env,r=fetch){let o=e.tokenEnv?t[e.tokenEnv]:void 0,m=await r(e.url,{headers:o?{Authorization:`Bearer ${o}`}:{},signal:AbortSignal.timeout(1e4)});if(!m.ok)throw Error(`HTTP ${m.status}`);return await m.json()}async function Yo(e,t){let r=[],o=[];if(!Array.isArray(e)||e.length===0)return{metrics:r,errors:o};let m=await Promise.all(e.map(async(h)=>{if(!h?.name||!h?.url)return{endpoint:h,error:"egress endpoint needs both a name and a url"};try{let I=await Bl(h,t.env,t.fetchImpl);return{endpoint:h,report:I}}catch(I){return{endpoint:h,error:I instanceof Error?I.message:String(I)}}}));for(let h of m){let I=`egress:${h.endpoint?.name??"unnamed"}`;if("error"in h&&h.error){o.push({source:I,message:h.error});continue}r.push(...kl(h.report,{name:h.endpoint.name,now:t.now}))}return{metrics:r,errors:o}}import{createHash as R_}from"node:crypto";import{existsSync as Zi,mkdtempSync as Gg,readdirSync as AS,rmSync as Gs,statSync as MS}from"node:fs";import{readFile as pi,writeFile as Ks}from"node:fs/promises";import{tmpdir as Lu}from"node:os";import{dirname as A_,extname as _c,join as rr,normalize as C_,relative as P_,resolve as __,sep as T_}from"node:path";import{fileURLToPath as x_}from"node:url";class mo extends Error{status;code;requestId;constructor(e,t,r,o){super(r);this.status=e;this.code=t;this.requestId=o}}class Ld{options;baseUrl;fetchFn;constructor(e){this.options=e;this.baseUrl=e.baseUrl.replace(/\/$/,""),this.fetchFn=e.fetch??fetch}async request(e,t={}){let r=await this.fetchFn(`${this.baseUrl}${e}`,{...t,headers:{accept:"application/json",authorization:`Bearer ${this.options.token}`,...t.headers}}),o=await r.json().catch(()=>({}));if(!r.ok)throw new mo(r.status,o.error?.code??"api_error",o.error?.message??"API request failed.",o.error?.requestId??r.headers.get("x-request-id")??void 0);return o}listProjects(e={}){return this.request(`/api/v1/projects?${new URLSearchParams(Object.fromEntries(Object.entries(e).filter(([,t])=>t!==void 0).map(([t,r])=>[t,String(r)])))}`)}listEnvironments(e,t={}){return this.request(`/api/v1/projects/${encodeURIComponent(e)}/environments?${new URLSearchParams(Object.fromEntries(Object.entries(t).filter(([,r])=>r!==void 0).map(([r,o])=>[r,String(o)])))}`)}listServices(e,t){return this.request(`/api/v1/services?${new URLSearchParams({projectId:e,...t?{environmentId:t}:{}})}`)}listOperations(e){return this.request(`/api/v1/operations${e?`?projectId=${encodeURIComponent(e)}`:""}`)}listComposeTemplates(){return this.request("/api/v1/compose-templates")}listComposeApplications(e,t){return this.request(`/api/v1/compose-applications?${new URLSearchParams({projectId:e,...t?{environmentId:t}:{}})}`)}previewCompose(e){return this.request("/api/v1/compose-applications/preview",{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify(e)})}createComposeApplication(e){return this.request("/api/v1/compose-applications",{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify(e)})}composeServices(e){return this.request(`/api/v1/compose-applications/${encodeURIComponent(e)}/services`)}composeAction(e,t,r={}){return this.request(`/api/v1/compose-applications/${encodeURIComponent(e)}/${t}`,{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify(r)})}releaseCapabilities(e,t={}){return this.request(`/api/v1/releases/capabilities?${new URLSearchParams({kind:e,health:String(!!t.health),replicas:String(t.replicas??1)})}`)}listReleases(e,t={}){return this.request(`/api/v1/releases?${new URLSearchParams({projectId:e,...t})}`)}createReleaseArtifact(e){return this.request("/api/v1/release-artifacts",{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify(e)})}createRelease(e){return this.request("/api/v1/releases",{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify(e)})}releaseAction(e,t,r={}){return this.request(`/api/v1/releases/${encodeURIComponent(e)}/${t}`,{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify(r)})}createDeployment(e,t){return this.request("/api/v1/deployments",{method:"POST",headers:{"content-type":"application/json","idempotency-key":t},body:JSON.stringify(e)})}detectApplication(e){return this.request("/api/v1/application-detections",{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify(e)})}planApplication(e){return this.request("/api/v1/application-plans",{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify(e)})}listApplicationDrafts(e){return this.request(`/api/v1/application-drafts?projectId=${encodeURIComponent(e)}`)}createApplicationDraft(e){return this.request("/api/v1/application-drafts",{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify(e)})}updateApplicationDraft(e){return this.request("/api/v1/application-drafts",{method:"PATCH",headers:{"content-type":"application/json"},body:JSON.stringify(e)})}createApplication(e,t){return this.request("/api/v1/applications",{method:"POST",headers:{"content-type":"application/json","idempotency-key":t},body:JSON.stringify(e)})}uploadApplicationArtifact(e,t,r){return this.request("/api/v1/application-artifacts",{method:"POST",headers:{"content-type":"application/octet-stream","x-project-id":e,"x-artifact-filename":t},body:r})}listRegistryConnections(){return this.request("/api/v1/registry-connections")}createRegistryConnection(e){return this.request("/api/v1/registry-connections",{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify(e)})}updateRegistryConnection(e){return this.request("/api/v1/registry-connections",{method:"PATCH",headers:{"content-type":"application/json"},body:JSON.stringify(e)})}disconnectRegistryConnection(e){return this.request("/api/v1/registry-connections",{method:"DELETE",headers:{"content-type":"application/json"},body:JSON.stringify({id:e})})}listQueue(e={}){let t=new URLSearchParams(Object.entries(e).filter(([,r])=>r!==void 0).map(([r,o])=>[r,String(o)]));return this.request(`/api/v1/queue?${t}`)}operationLogs(e,t={}){let r=new URLSearchParams(Object.entries(t).filter(([,o])=>o!==void 0).map(([o,m])=>[o,String(m)]));return this.request(`/api/v1/operations/${encodeURIComponent(e)}/logs?${r}`)}streamOperationLogs(e,t=0){return this.fetchFn(`${this.baseUrl}/api/v1/operations/${encodeURIComponent(e)}/logs/stream`,{headers:{accept:"text/event-stream",authorization:`Bearer ${this.options.token}`,"last-event-id":String(t)}})}cancelOperation(e){return this.request(`/api/v1/operations/${encodeURIComponent(e)}/cancel`,{method:"POST",headers:{"content-type":"application/json"},body:"{}"})}retryOperation(e,t,r=0){return this.request(`/api/v1/operations/${encodeURIComponent(e)}/retry`,{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify({errorClass:t,delayMs:r})})}queueSettings(){return this.request("/api/v1/queue/settings")}updateQueueSettings(e){return this.request("/api/v1/queue/settings",{method:"PATCH",headers:{"content-type":"application/json"},body:JSON.stringify({confirm:"update queue limits",concurrency:e})})}listPreviews(e){return this.request(`/api/v1/previews${e?`?projectId=${encodeURIComponent(e)}`:""}`)}listPreviewDefinitions(e){return this.request(`/api/v1/preview-definitions?projectId=${encodeURIComponent(e)}`)}createPreviewDefinition(e){return this.request("/api/v1/preview-definitions",{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify(e)})}createPreview(e){return this.request("/api/v1/previews",{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify(e)})}destroyPreview(e,t){return this.request(`/api/v1/previews/${encodeURIComponent(e)}/destroy`,{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify({confirm:t})})}extendPreview(e,t){return this.request(`/api/v1/previews/${encodeURIComponent(e)}/extend`,{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify({hours:t})})}rebuildPreview(e){return this.request(`/api/v1/previews/${encodeURIComponent(e)}/rebuild`,{method:"POST",headers:{"content-type":"application/json"},body:"{}"})}usage(e={}){let t=new URLSearchParams;for(let[r,o]of Object.entries(e))if(o)t.set(r,String(o));return this.request(`/api/v1/usage${t.size>0?`?${t}`:""}`)}usageRollups(e){let t=new URLSearchParams({from:e.from,to:e.to});if(e.projectId)t.set("projectId",e.projectId);if(e.environmentId)t.set("environmentId",e.environmentId);if(e.limit)t.set("limit",String(e.limit));for(let r of e.meters??[])t.append("meter",r);return this.request(`/api/v1/usage/rollups?${t}`)}allowance(e="deploy",t={}){let r=new URLSearchParams({operation:e});if(t.projectId)r.set("projectId",t.projectId);if(t.environmentId)r.set("environmentId",t.environmentId);return this.request(`/api/v1/spend/allowance?${r}`)}budgets(e={}){let t=new URLSearchParams;for(let[r,o]of Object.entries(e))if(o)t.set(r,String(o));return this.request(`/api/v1/spend/budgets${t.size>0?`?${t}`:""}`)}anomalies(e={}){let t=new URLSearchParams;if(e.projectId)t.set("projectId",e.projectId);if(e.since)t.set("since",e.since);if(e.unacknowledged)t.set("unacknowledged","true");return this.request(`/api/v1/spend/anomalies${t.size>0?`?${t}`:""}`)}}import{createHash as $k}from"node:crypto";import{lstatSync as Hw,readdirSync as Kw,readFileSync as Jw}from"node:fs";import{relative as Gw,resolve as Mf,sep as Qw}from"node:path";function Yw(e){return new Map(e.map((t)=>[t.path.replace(/^\.\//,"").replace(/\\/g,"/"),t]))}function Cs(e,t,r){return{path:e,reason:t,weight:r}}function Bf(e){try{return e?.content?JSON.parse(e.content):{}}catch{return{}}}function ya(e,t,r,o,m,h){return{framework:e,strategy:t,confidence:r,evidence:o,defaults:{build:m,runtime:{target:t==="static"?"serverless":t==="serverless"?"serverless":t==="dockerfile"||t==="prebuilt_image"?"container":"server",architecture:"x86_64",port:t==="static"||t==="serverless"?void 0:3000,healthCheck:t==="static"?void 0:{protocol:"http",path:"/health"}}},description:h}}function va(e){let t=Yw(e.slice(0,1e4)),r=[];if(t.has("Dockerfile"))r.push(ya("dockerfile","dockerfile",0.99,[Cs("Dockerfile","A root Dockerfile defines an explicit reproducible build.",1)],{kind:"dockerfile",context:".",dockerfile:"Dockerfile"},"Build the repository with its Dockerfile and run the resulting OCI image."));let o=Bf(t.get("composer.json"));if(t.has("artisan")&&o.require?.["laravel/framework"])r.push(ya("laravel","server",0.98,[Cs("artisan","Laravel Artisan entrypoint is present.",0.45),Cs("composer.json","Requires laravel/framework.",0.55)],{kind:"server",runtime:"php",runtimeVersion:"8.3",installCommand:"composer install --no-dev --prefer-dist --optimize-autoloader",buildCommand:"php artisan config:cache && php artisan route:cache && php artisan view:cache",startCommand:"php artisan serve --host=0.0.0.0 --port=$PORT"},"Install a production Composer tree, warm Laravel caches, and run the HTTP application."));else if(t.has("composer.json"))r.push(ya("php","server",0.82,[Cs("composer.json","Composer metadata identifies a PHP application.",0.8)],{kind:"server",runtime:"php",runtimeVersion:"8.3",installCommand:"composer install --no-dev --prefer-dist --optimize-autoloader",startCommand:"php -S 0.0.0.0:$PORT -t public"},"Install Composer dependencies and run the configured PHP document root."));let m=Bf(t.get("package.json"));if(t.has("package.json")){let h=t.has("bun.lock")||t.has("bun.lockb")||m.engines?.bun,I=h?"bun":"node",R=h?"bun":"node",A=[Cs("package.json",`Defines ${m.scripts?.build?"build and ":""}${m.scripts?.start?"start ":""}scripts.`,0.55)];if(h)A.push(Cs(t.has("bun.lock")?"bun.lock":t.has("bun.lockb")?"bun.lockb":"package.json","Pins the Bun package manager/runtime.",0.35));let E=!!(m.dependencies?.vite||m.devDependencies?.vite||m.dependencies?.["@stacksjs/bunpress"]||m.devDependencies?.["@stacksjs/bunpress"])&&!m.scripts?.start,x=E?{kind:"static",installCommand:`${I} install --frozen-lockfile`,buildCommand:`${I} run build`,publishDirectory:"dist"}:{kind:"server",runtime:I,installCommand:`${I} install --frozen-lockfile`,buildCommand:m.scripts?.build?`${I} run build`:void 0,startCommand:m.scripts?.start?`${I} run start`:I==="bun"?"bun run index.ts":"node index.js"};r.push(ya(R,x.kind,Math.min(0.94,A.reduce((N,F)=>N+F.weight,0)),A,x,E?"Install dependencies, build static assets, and publish the output directory.":`Install dependencies, build if configured, and start the ${I} server.`))}if(!r.length&&(t.has("index.html")||t.has("public/index.html")))r.push(ya("static","static",0.78,[Cs(t.has("index.html")?"index.html":"public/index.html","A static HTML entrypoint is present.",0.78)],{kind:"static",publishDirectory:t.has("index.html")?".":"public"},"Publish the static directory without a runtime process."));if(!r.length)r.push(ya("unknown","buildpack",0.2,[],{kind:"buildpack",runtime:"node"},"No supported framework signature was conclusive; choose commands and runtime manually."));return r.sort((h,I)=>I.confidence-h.confidence||h.framework.localeCompare(I.framework))}function tp(e,t={}){let r=Mf(e),o=t.maxFiles??1e4,m=t.maxContentBytes??262144,h=[],I=new Set(["Dockerfile","package.json","composer.json","index.html","public/index.html","bun.lock","bun.lockb","artisan"]),R=new Set([".git",".ts-cloud",".cache",".next",".nuxt",".output",".turbo",".bun","node_modules","vendor","pantry","dist","build","coverage"]),A=(E)=>{for(let x of Kw(E).sort()){if(R.has(x))continue;let N=Mf(E,x),F=Gw(r,N).split(Qw).join("/");if(!F||F.startsWith("../"))throw Error("Detection path escaped the application root");let z=Hw(N);if(z.isSymbolicLink())throw Error(`Detection refuses symbolic links: ${F}`);if(z.isDirectory())A(N);else if(z.isFile()){if(h.length>=o)throw Error("Application file-count limit exceeded");let K={path:F,size:z.size};if(I.has(F)&&z.size<=m)K.content=Jw(N,"utf8");h.push(K)}}};return A(r),h}function np(e){if(Array.isArray(e))return`[${e.map(np).join(",")}]`;if(e&&typeof e==="object")return`{${Object.entries(e).filter(([,t])=>t!==void 0).sort(([t],[r])=>t.localeCompare(r)).map(([t,r])=>`${JSON.stringify(t)}:${np(r)}`).join(",")}}`;return JSON.stringify(e)??"null"}function tc(e){return!!e&&!e.startsWith("/")&&!e.split("/").includes("..")&&!/[\0\r\n]/.test(e)}function An(e,t,r,o){return{path:e,code:t,message:r,alternatives:o}}function rp(e){return JSON.parse(JSON.stringify(e))}function Zw(e){let t=Object.fromEntries(Object.entries(e.environment??{}).map(([r,o])=>[r,typeof o==="string"?o:`\${${o.secretRef}}`]));if(e.build.kind==="prebuilt_image"||e.build.kind==="dockerfile")return rp({infrastructure:{containers:{[e.slug]:{source:e.source,build:e.build,port:e.runtime.port,cpu:e.runtime.cpu,memory:e.runtime.memoryMb,architecture:e.runtime.architecture,healthCheck:e.runtime.healthCheck,environment:t}}}});if(e.runtime.target==="serverless"||e.build.kind==="serverless")return rp({serverless:{apps:{[e.slug]:{source:e.source,build:e.build,architecture:e.runtime.architecture,environment:t,domain:e.domain}}}});return rp({sites:{[e.slug]:{source:e.source,build:e.build,port:e.runtime.port,architecture:e.runtime.architecture,healthCheck:e.runtime.healthCheck,env:t,domain:e.domain?.hostname}}})}function Vn(e,t=[]){let r=[],o=(K)=>/(?:secret|token|password|passwd|private[_-]?key|api[_-]?key)/i.test(K),m=Object.fromEntries(Object.entries(e.environment??{}).map(([K,J])=>{if(typeof J==="string"&&o(K))return r.push(An(`environment.${K}`,"write_only_secret_required",`${K} must be supplied through the write-only secrets boundary.`)),[K,{secretRef:K}];return[K,J]})),h=e.build;if(e.build.kind==="dockerfile"&&e.build.buildArgs){let K=Object.fromEntries(Object.entries(e.build.buildArgs).map(([J,re])=>{if(o(J))return r.push(An(`build.buildArgs.${J}`,"write_only_build_secret_required",`${J} must be declared in secretNames instead of buildArgs.`)),[J,"[REDACTED]"];return[J,re]}));h={...e.build,buildArgs:K}}if(!/^[a-z0-9][a-z0-9-]{1,62}$/.test(e.slug))r.push(An("slug","invalid_slug","Slug must contain 2-63 lowercase letters, numbers, or hyphens."));if(!tc((e.source.kind==="git"?e.source.monorepoRoot:e.source.kind==="local"?e.source.root:".")??"."))r.push(An("source","unsafe_source_root","Source root must stay inside the source tree."));if(e.source.kind==="git"&&e.source.sparsePaths?.some((K)=>!tc(K)))r.push(An("source.sparsePaths","unsafe_sparse_path","Sparse checkout paths must stay inside the repository."));if(e.build.kind==="dockerfile"&&(!tc(e.build.context)||!tc(e.build.dockerfile)))r.push(An("build","unsafe_docker_path","Docker context and Dockerfile must stay inside the source tree."));if(e.build.kind==="static"&&!tc(e.build.publishDirectory))r.push(An("build.publishDirectory","invalid_publish_directory","A safe static publish directory is required."));if(e.build.kind==="server"&&!e.build.startCommand.trim())r.push(An("build.startCommand","missing_start_command","A server start command is required.",["Use static output","Use a Dockerfile"]));if(e.source.kind==="image"&&e.build.kind!=="prebuilt_image")r.push(An("build.kind","image_requires_prebuilt","An OCI image source must use the prebuilt-image strategy.",["Select prebuilt image"]));if(e.build.kind==="prebuilt_image"&&e.runtime.target!=="container")r.push(An("runtime.target","image_requires_container","Prebuilt images require the container runtime target.",["Select container target"]));if(e.runtime.target==="serverless"&&e.build.kind==="dockerfile")r.push(An("runtime.target","unsupported_serverless_dockerfile","Dockerfile builds currently target the container service path.",["Select container target","Select serverless package"]));if(e.runtime.port!==void 0&&(!Number.isInteger(e.runtime.port)||e.runtime.port<1||e.runtime.port>65535))r.push(An("runtime.port","invalid_port","Port must be an integer from 1 to 65535."));let I=e.runtime.healthCheck;if(I&&I.protocol!=="tcp"&&(!I.path||!I.path.startsWith("/")||/[\r\n]/.test(I.path)))r.push(An("runtime.healthCheck.path","invalid_health_path","HTTP health checks require a path beginning with /."));if(e.domain?.hostname&&(!/^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,63}$/i.test(e.domain.hostname)||e.domain.hostname.includes("..")))r.push(An("domain.hostname","invalid_domain","Domain must be a valid DNS hostname."));let R=Object.values(m).flatMap((K)=>typeof K==="object"?[K.secretRef]:[]),A=[...new Set([...e.requiredSecretNames??[],...e.build.kind==="dockerfile"?e.build.secretNames??[]:[],...R])].sort(),E=new Set(t),x=A.filter((K)=>!E.has(K)),N={apiVersion:"ts-cloud.dev/v1",kind:"Application",metadata:{name:e.name,slug:e.slug,projectId:e.projectId,environmentId:e.environmentId},spec:{source:e.source,build:h,runtime:e.runtime,environment:m,domain:e.domain}},F=[...new Set([e.source.kind==="git"?"git:read":e.source.kind==="image"?"registry:pull":e.source.kind==="artifact"?"artifact:inspect":"filesystem:read",e.runtime.target==="container"?"containers":e.runtime.target==="serverless"?"serverless":"servers",e.domain?"dns":""])].filter(Boolean).sort(),z=[...new Set([e.runtime.target,e.runtime.cpu?`${e.runtime.cpu} vCPU`:"",e.runtime.memoryMb?`${e.runtime.memoryMb} MB memory`:"",(e.runtime.minInstances??0)>0?`${e.runtime.minInstances} minimum instances`:"scales from zero where supported",e.domain?"managed DNS/TLS":""])].filter(Boolean);return{valid:r.length===0&&x.length===0,issues:r,missingSecrets:x,manifest:N,configPatch:Zw({...e,build:h,environment:m}),capabilityRequirements:F,costDrivers:z,serializedManifest:`${np(N)}
|
|
42
|
+
`}}function Lf(e){let t=e.replace(/\\/g,"/");return!!t&&!t.startsWith("/")&&!/^[A-Za-z]:\//.test(t)&&!t.split("/").includes("..")&&!/[\0\r\n]/.test(t)}function Ff(e,t,r={}){let o=r.maxBytes??104857600,m=r.maxEntries??1e4,h=r.maxExpandedBytes??524288000;if(e.byteLength>o)throw Error("Archive exceeds the compressed size limit");let I=new DataView(e.buffer,e.byteOffset,e.byteLength),R=[],A=0;if(t.toLowerCase().endsWith(".zip")||e[0]===80&&e[1]===75){let x=0;while(x+30<=e.length){let N=I.getUint32(x,!0);if(N===33639248||N===101010256)break;if(N!==67324752)throw Error("Malformed ZIP local header");let F=I.getUint16(x+6,!0),z=I.getUint32(x+18,!0),K=I.getUint32(x+22,!0),J=I.getUint16(x+26,!0),re=I.getUint16(x+28,!0);if(F&8)throw Error("ZIP data descriptors are not accepted for uploaded artifacts");let se=new TextDecoder().decode(e.subarray(x+30,x+30+J));if(!Lf(se))throw Error(`Unsafe archive path: ${se}`);if(R.push(se),A+=K,R.length>m||A>h)throw Error("Archive expansion limit exceeded");x+=30+J+re+z}return{format:"zip",entries:R.length,expandedBytes:A,paths:R}}if(!t.toLowerCase().match(/\.(?:tar|tar\.gz|tgz)$/))throw Error("Only ZIP and uncompressed TAR artifacts are supported");let E=0;while(E+512<=e.length){let x=e.subarray(E,E+512);if(x.every((J)=>J===0))break;let N=(J,re)=>new TextDecoder().decode(x.subarray(J,J+re)).replace(/\0.*$/,""),F=`${N(345,155)}${N(345,155)?"/":""}${N(0,100)}`,z=Number.parseInt(N(124,12).trim()||"0",8),K=String.fromCharCode(x[156]??0);if(!Lf(F))throw Error(`Unsafe archive path: ${F}`);if(K==="1"||K==="2")throw Error(`Archive links are not accepted: ${F}`);if(!Number.isFinite(z)||z<0)throw Error("Malformed TAR entry size");if(R.push(F),A+=z,R.length>m||A>h)throw Error("Archive expansion limit exceeded");E+=512+Math.ceil(z/512)*512}return{format:"tar",entries:R.length,expandedBytes:A,paths:R}}var zf=new Set(["source","build","runtime","environment","domain","review"]);function Uf(e,t){try{return typeof e==="string"?JSON.parse(e):t}catch{return t}}function qf(e){return/(?:secret|token|password|passwd|private[_-]?key|api[_-]?key)/i.test(e)}function Vf(e){for(let[r,o]of Object.entries(e.environment??{}))if(typeof o==="string"&&qf(r)&&o)throw Error(`${r} must be stored through the write-only secrets boundary`);if(e.build.kind==="dockerfile"){for(let[r,o]of Object.entries(e.build.buildArgs??{}))if(qf(r)&&o)throw Error(`${r} must be declared as a build secret name`)}let t=JSON.stringify(e);if(/https?:\/\/[^/@\s]+:[^/@\s]+@/i.test(t))throw Error("Drafts cannot contain credential-bearing URLs")}function Wf(e){return{id:String(e.id),organizationId:String(e.organization_id),projectId:String(e.project_id),schemaVersion:Number(e.schema_version),name:String(e.name),step:String(e.step),input:Uf(e.input,{}),suppliedSecretNames:Uf(e.supplied_secret_names,[]),status:String(e.status),version:Number(e.version),createdByActorId:e.created_by_actor_id?String(e.created_by_actor_id):void 0,createdAt:String(e.created_at),updatedAt:String(e.updated_at)}}class ip{controlPlane;nowFn;idFn;constructor(e,t={}){this.controlPlane=e;this.nowFn=t.now??(()=>new Date),this.idFn=t.id??(()=>crypto.randomUUID())}now(){return this.nowFn().toISOString()}create(e){if(Vf(e.draft),e.draft.projectId!==e.projectId)throw Error("Draft project does not match its storage scope");let t=this.controlPlane.getProject(e.projectId);if(!t||t.organizationId!==e.organizationId)throw Error("Draft project was not found");let r=this.idFn(),o=this.now(),m=e.step??"source";if(!zf.has(m))throw Error("Invalid draft step");let h=[...new Set(e.suppliedSecretNames??[])].sort(),I=Vn(e.draft,h).valid?"ready":"draft";return this.controlPlane.database.run("INSERT INTO application_drafts (id, organization_id, project_id, schema_version, name, step, input, supplied_secret_names, status, created_by_actor_id, created_at, updated_at) VALUES (?, ?, ?, 1, ?, ?, ?, ?, ?, ?, ?, ?)",[r,e.organizationId,e.projectId,e.name.trim().slice(0,100),m,JSON.stringify(e.draft),JSON.stringify(h),I,e.actorId??null,o,o]),this.controlPlane.appendEvent({organizationId:e.organizationId,projectId:e.projectId,actorId:e.actorId,type:"application.draft.created",payload:{draftId:r,step:m,status:I,requiredSecretNames:e.draft.requiredSecretNames??[]}}),this.get(r)}get(e){let t=this.controlPlane.database.query("SELECT * FROM application_drafts WHERE id=?").get(e);return t?Wf(t):void 0}list(e){return this.controlPlane.database.query("SELECT * FROM application_drafts WHERE project_id=? ORDER BY updated_at DESC").all(e).map(Wf)}update(e,t,r){let o=this.get(e);if(!o||o.version!==t)throw Error(`Application draft ${e} changed since version ${t}`);if(!zf.has(r.step))throw Error("Invalid draft step");if(r.draft.projectId!==o.projectId)throw Error("Draft project cannot be changed");Vf(r.draft);let m=[...new Set(r.suppliedSecretNames??o.suppliedSecretNames)].sort(),h=Vn(r.draft,m).valid?"ready":"draft",I=this.now();if(this.controlPlane.database.run("UPDATE application_drafts SET name=?, step=?, input=?, supplied_secret_names=?, status=?, version=version+1, updated_at=? WHERE id=? AND version=?",[r.draft.name.slice(0,100),r.step,JSON.stringify(r.draft),JSON.stringify(m),h,I,e,t]).changes!==1)throw Error(`Application draft ${e} changed since version ${t}`);return this.controlPlane.appendEvent({organizationId:o.organizationId,projectId:o.projectId,actorId:r.actorId,type:"application.draft.updated",payload:{draftId:e,fromVersion:t,toVersion:t+1,step:r.step,status:h}}),this.get(e)}markApplied(e,t,r){let o=this.get(e);if(!o||o.version!==t)throw Error(`Application draft ${e} changed since version ${t}`);let m=Vn(o.input,o.suppliedSecretNames);if(!m.valid)throw Error(`Draft is not deployable: ${[...m.issues.map((I)=>I.message),...m.missingSecrets.map((I)=>`Missing secret ${I}`)].join("; ")}`);let h=this.now();return this.controlPlane.database.run("UPDATE application_drafts SET status='applied', step='review', version=version+1, updated_at=? WHERE id=? AND version=?",[h,e,t]),this.controlPlane.appendEvent({organizationId:o.organizationId,projectId:o.projectId,actorId:r,type:"application.draft.applied",payload:s({draftId:e,manifest:m.manifest})}),this.get(e)}}import{createCipheriv as Xw,createDecipheriv as ek,createHash as Jf,randomBytes as tk}from"node:crypto";function rc(e){return typeof e==="string"&&e?e:void 0}function op(e){return Jf("sha256").update(e).digest("hex").slice(0,16)}function Hf(e){let t=new URL(e.includes("://")?e:`https://${e}`);if(t.protocol!=="https:"&&!["localhost","127.0.0.1","::1"].includes(t.hostname))throw Error("Registry host must use HTTPS");if(t.username||t.password||t.search||t.hash)throw Error("Registry host cannot contain credentials");return`${t.protocol}//${t.host}`}function rk(e){return{id:String(e.id),organizationId:String(e.organization_id),provider:String(e.provider),name:String(e.name),host:String(e.host),credentialConfigured:!!e.credential_ciphertext,credentialFingerprint:rc(e.credential_fingerprint),credentialExpiresAt:rc(e.credential_expires_at),status:String(e.status),healthMessage:rc(e.health_message),lastTestedAt:rc(e.last_tested_at),version:Number(e.version),createdByActorId:rc(e.created_by_actor_id),createdAt:String(e.created_at),updatedAt:String(e.updated_at)}}class sp{controlPlane;key;nowFn;idFn;constructor(e,t){this.controlPlane=e;if(!t.encryptionKey)throw Error("Registry encryption key is required");this.key=Jf("sha256").update(`ts-cloud:registry:${t.encryptionKey}`).digest(),this.nowFn=t.now??(()=>new Date),this.idFn=t.id??(()=>crypto.randomUUID())}now(){return this.nowFn().toISOString()}encrypt(e){let t=tk(12),r=Xw("aes-256-gcm",this.key,t),o=Buffer.concat([r.update(e,"utf8"),r.final()]);return`v1.${t.toString("base64url")}.${r.getAuthTag().toString("base64url")}.${o.toString("base64url")}`}decrypt(e){let[t,r,o,m]=e.split(".");if(t!=="v1"||!r||!o||!m)throw Error("Registry credential envelope is invalid");let h=ek("aes-256-gcm",this.key,Buffer.from(r,"base64url"));return h.setAuthTag(Buffer.from(o,"base64url")),Buffer.concat([h.update(Buffer.from(m,"base64url")),h.final()]).toString("utf8")}current(e){let t=rk(e);return t.status!=="disconnected"&&t.credentialExpiresAt&&new Date(t.credentialExpiresAt).getTime()<=this.nowFn().getTime()?{...t,status:"expired",healthMessage:"Registry credential expired; rotate it before pulling images."}:t}create(e){if(!["docker_hub","ghcr","ecr","gcr","generic"].includes(e.provider))throw Error("A supported registry provider is required");let t=e.credential?JSON.stringify(e.credential):void 0,r=this.idFn(),o=this.now();return this.controlPlane.database.run("INSERT INTO registry_connections (id, organization_id, provider, name, host, credential_ciphertext, credential_fingerprint, credential_expires_at, created_by_actor_id, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",[r,e.organizationId,e.provider,e.name.trim().slice(0,80),Hf(e.host),t?this.encrypt(t):null,t?op(t):null,e.credentialExpiresAt??null,e.actorId??null,o,o]),this.controlPlane.appendEvent({organizationId:e.organizationId,actorId:e.actorId,type:"registry.connection.created",payload:{connectionId:r,provider:e.provider,host:Hf(e.host),credentialConfigured:!!t}}),this.get(r)}get(e){let t=this.controlPlane.database.query("SELECT * FROM registry_connections WHERE id=?").get(e);return t?this.current(t):void 0}list(e){return this.controlPlane.database.query("SELECT * FROM registry_connections WHERE organization_id=? ORDER BY name").all(e).map((t)=>this.current(t))}credential(e){let t=this.controlPlane.database.query("SELECT credential_ciphertext FROM registry_connections WHERE id=?").get(e);return t?.credential_ciphertext?JSON.parse(this.decrypt(String(t.credential_ciphertext))):void 0}rotate(e,t,r={}){let o=this.get(e);if(!o||o.status==="disconnected")throw Error("Registry connection was not found");let m=JSON.stringify(t);return this.controlPlane.database.run("UPDATE registry_connections SET credential_ciphertext=?, credential_fingerprint=?, credential_expires_at=?, status='pending', health_message=NULL, version=version+1, updated_at=? WHERE id=?",[this.encrypt(m),op(m),r.expiresAt??null,this.now(),e]),this.controlPlane.appendEvent({organizationId:o.organizationId,actorId:r.actorId,type:"registry.credential.rotated",payload:{connectionId:e,fingerprint:op(m),expiresAt:r.expiresAt??null}}),this.get(e)}disconnect(e,t){let r=this.get(e);if(!r)throw Error("Registry connection was not found");return this.controlPlane.database.run("UPDATE registry_connections SET credential_ciphertext=NULL, credential_fingerprint=NULL, status='disconnected', health_message='Disconnected by an administrator', version=version+1, updated_at=? WHERE id=?",[this.now(),e]),this.controlPlane.appendEvent({organizationId:r.organizationId,actorId:t,type:"registry.connection.disconnected",level:"warning",payload:{connectionId:e}}),this.get(e)}async test(e,t={}){let r=this.get(e);if(!r||["disconnected","expired"].includes(r.status))throw Error("Active registry connection was not found");let o=this.credential(e),m=t.fetch??fetch,h=new AbortController,I=setTimeout(()=>h.abort(),t.timeoutMs??15000),R=o?.token?`Bearer ${o.token}`:o?.username||o?.password?`Basic ${Buffer.from(`${o.username??""}:${o.password??""}`).toString("base64")}`:void 0;try{let A=await m(`${r.host}/v2/`,{method:"GET",redirect:"error",signal:h.signal,headers:R?{Authorization:R}:void 0}),E=A.headers.get("www-authenticate")??"";if(A.status===401&&E.startsWith("Bearer ")&&o){let F=Object.fromEntries([...E.slice(7).matchAll(/(\w+)="([^"]+)"/g)].map((se)=>[se[1],se[2]])),z=new URL(F.realm??"");if(z.protocol!=="https:")throw Error("Registry authentication realm must use HTTPS");if(F.service)z.searchParams.set("service",F.service);if(t.image)z.searchParams.set("scope",`repository:${Gf(t.image).repository}:pull`);let K=await m(z,{signal:h.signal,redirect:"error",headers:R?{Authorization:R}:void 0});if(!K.ok)throw Error(`Registry authentication failed (${K.status})`);let J=await K.json(),re=J.token??J.access_token;if(!re)throw Error("Registry did not return an access token");A=t.image?await Kf(m,r.host,t.image,re,h.signal):await m(`${r.host}/v2/`,{signal:h.signal,redirect:"error",headers:{Authorization:`Bearer ${re}`}})}else if(A.ok&&t.image)A=await Kf(m,r.host,t.image,o?.token,h.signal,R);let x=A.ok?"healthy":"degraded",N=A.ok?t.image?"Registry image is readable.":"Registry connection is healthy.":`Registry request failed (${A.status})`;return this.controlPlane.database.run("UPDATE registry_connections SET status=?, health_message=?, last_tested_at=?, version=version+1, updated_at=? WHERE id=?",[x,N,this.now(),this.now(),e]),this.get(e)}catch(A){let E=h.signal.aborted?"Registry request timed out.":A instanceof Error?A.message:"Registry test failed";return this.controlPlane.database.run("UPDATE registry_connections SET status='degraded', health_message=?, last_tested_at=?, version=version+1, updated_at=? WHERE id=?",[E.slice(0,1000),this.now(),this.now(),e]),this.get(e)}finally{clearTimeout(I)}}}function Gf(e){let t=e.replace(/^https?:\/\//,""),r=t.indexOf("/");if(r<1)throw Error("Image must include registry/repository");let o=t.slice(r+1),m=o.lastIndexOf("@"),h=o.lastIndexOf(":"),I=m>=0?m:h>o.lastIndexOf("/")?h:-1,R=I>=0?o.slice(0,I):o,A=I>=0?o.slice(I+1):"latest";if(!/^[A-Za-z0-9_.\/-]+$/.test(R)||!/^[A-Za-z0-9_.:+-]+$/.test(A))throw Error("Image reference is invalid");return{repository:R,reference:A}}async function Kf(e,t,r,o,m,h){let I=Gf(r);return e(`${t}/v2/${I.repository}/manifests/${encodeURIComponent(I.reference)}`,{method:"HEAD",redirect:"error",signal:m,headers:{Accept:"application/vnd.oci.image.manifest.v1+json, application/vnd.docker.distribution.manifest.v2+json",...o?{Authorization:`Bearer ${o}`}:h?{Authorization:h}:{}}})}import{createHash as nk}from"node:crypto";import{chmodSync as Qf,existsSync as Yf,mkdirSync as ik,renameSync as ok,unlinkSync as Zf,writeFileSync as sk}from"node:fs";import{basename as ak,join as ck}from"node:path";function ap(e){return{id:String(e.id),organizationId:String(e.organization_id),projectId:String(e.project_id),filename:String(e.filename),sha256:String(e.sha256),size:Number(e.size),format:String(e.format),entryCount:Number(e.entry_count),expandedBytes:Number(e.expanded_bytes),createdByActorId:e.created_by_actor_id?String(e.created_by_actor_id):void 0,createdAt:String(e.created_at)}}class cp{controlPlane;root;idFn;nowFn;constructor(e,t){this.controlPlane=e;this.root=d(t.cwd,"artifacts"),this.idFn=t.id??(()=>crypto.randomUUID()),this.nowFn=t.now??(()=>new Date)}create(e){let t=this.controlPlane.getProject(e.projectId);if(!t||t.organizationId!==e.organizationId)throw Error("Artifact project was not found");let r=ak(e.filename).slice(0,200);if(!r||r!==e.filename)throw Error("Artifact filename must not contain a path");let o=Ff(e.bytes,r),m=nk("sha256").update(e.bytes).digest("hex"),h=this.controlPlane.database.query("SELECT * FROM application_artifacts WHERE project_id=? AND sha256=?").get(e.projectId,m);if(h)return ap(h);let I=this.idFn();ik(this.root,{recursive:!0,mode:448}),Qf(this.root,448);let R=ck(this.root,`${I}.archive`),A=`${R}.partial`;try{sk(A,e.bytes,{mode:384,flag:"wx"}),ok(A,R),Qf(R,384),this.controlPlane.database.run("INSERT INTO application_artifacts (id, organization_id, project_id, filename, storage_path, sha256, size, format, entry_count, expanded_bytes, created_by_actor_id, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",[I,e.organizationId,e.projectId,r,R,m,e.bytes.byteLength,o.format,o.entries,o.expandedBytes,e.actorId??null,this.nowFn().toISOString()])}catch(E){if(Yf(A))Zf(A);if(Yf(R))Zf(R);throw E}return this.controlPlane.appendEvent({organizationId:e.organizationId,projectId:e.projectId,actorId:e.actorId,type:"application.artifact.created",payload:{artifactId:I,filename:r,sha256:m,size:e.bytes.byteLength,entries:o.entries}}),this.get(I)}get(e){let t=this.controlPlane.database.query("SELECT * FROM application_artifacts WHERE id=?").get(e);return t?ap(t):void 0}list(e){return this.controlPlane.database.query("SELECT * FROM application_artifacts WHERE project_id=? ORDER BY created_at DESC").all(e).map(ap)}}function Fc(e){let t=e.drafts.get(e.draftId);if(!t||t.version!==e.expectedVersion)throw Error(`Application draft ${e.draftId} changed since version ${e.expectedVersion}`);if(t.status==="applied")throw Error("Application draft was already applied");let r=e.controlPlane.getProject(t.projectId),o=r?e.controlPlane.listEnvironments(r.id).find((E)=>E.id===t.input.environmentId):void 0;if(!r||!o||o.projectId!==r.id||r.organizationId!==t.organizationId)throw Error("Draft deployment scope was not found");if(e.confirmEnvironment!==o.slug)throw Error(`Type ${o.slug} to confirm this deployment target`);let m=Vn(t.input,t.suppliedSecretNames);if(!m.valid)throw Error(`Application plan is invalid: ${[...m.issues.map((E)=>E.message),...m.missingSecrets.map((E)=>`Missing secret ${E}`)].join("; ")}`);let h=e.controlPlane.listResources(r.id,o.id).find((E)=>E.kind==="application"&&E.slug===t.input.slug),I=JSON.parse(JSON.stringify({manifest:m.manifest,configPatch:m.configPatch})),R=h?e.controlPlane.updateResource(h.id,h.version,{name:t.input.name,desiredState:I}):e.controlPlane.createResource({projectId:r.id,environmentId:o.id,kind:"application",slug:t.input.slug,name:t.input.name,desiredState:I,metadata:{source:"application-onboarding",draftId:t.id}}),A=new c(e.controlPlane).enqueue({projectId:r.id,environmentId:o.id,resourceId:R.id,actorId:e.actorId,kind:"application.create",idempotencyKey:`application-draft:${t.id}:v${t.version}`,correlationId:`application-draft:${t.id}`,input:I,lockKey:`resource:${R.id}`,providerKey:R.provider??"default",buildSlot:t.input.build.kind!=="prebuilt_image",maxAttempts:3,retryClasses:["network","provider_throttled","provider_unavailable"],resumePolicy:"fail",cancellationMode:"provider_non_cancellable",retentionDays:90}).operation;return e.drafts.markApplied(t.id,t.version,e.actorId),e.controlPlane.appendEvent({organizationId:t.organizationId,projectId:r.id,resourceId:R.id,operationId:A.id,actorId:e.actorId,type:"application.create.queued",payload:{draftId:t.id,environment:o.slug,strategy:t.input.build.kind,costDrivers:m.costDrivers}}),{plan:m,resource:R,operation:A}}function $r(e){let t=["static","compute","serverless_zip","serverless_image"].includes(e.kind),r=["container","compose"].includes(e.kind)&&(e.replicas??1)>1,o=e.kind==="container"||e.kind==="serverless_zip"||e.kind==="serverless_image",m=(h,I)=>I&&(h==="atomic"?e.kind==="static"||e.hasHealthGate:e.hasHealthGate);return[{strategy:"atomic",supported:m("atomic",t),explanation:t?e.hasHealthGate||e.kind==="static"?"Activate an immutable version with an atomic pointer/alias switch.":"Atomic activation needs a readiness gate for this runtime.":"This driver has no atomic pointer or alias primitive.",capacityMultiplier:1,costImpact:"none",rollback:"Switch the pointer/alias back to the previous immutable release."},{strategy:"rolling",supported:m("rolling",r),explanation:r?e.hasHealthGate?"Replace healthy replicas within the configured capacity envelope.":"Rolling activation requires a health/readiness gate.":"Rolling activation requires a capable container/Compose driver and at least two replicas.",capacityMultiplier:1.5,costImpact:"temporary",rollback:"Stop replacement and restore the prior task/service definition."},{strategy:"blue_green",supported:m("blue_green",o),explanation:o?e.hasHealthGate?"Provision a complete green target and switch traffic after health passes.":"Blue-green requires a health gate before traffic can switch.":"The provider does not expose two traffic-addressable revisions.",capacityMultiplier:2,costImpact:"temporary",rollback:"Return all traffic to the preserved blue target."},{strategy:"canary",supported:m("canary",o),explanation:o?e.hasHealthGate?"Shift bounded traffic steps while evaluating health.":"Canary requires a health gate and measurable traffic target.":"The provider does not expose weighted traffic primitives.",capacityMultiplier:1.25,costImpact:"temporary",rollback:"Set canary weight to zero and preserve its diagnostics."}]}function Ei(e,t){let r=$r(e).find((o)=>o.strategy===t);if(!r.supported)throw Error(`${t} is unavailable: ${r.explanation}`);return r}import{createHash as dk}from"node:crypto";var lk=/^sha256:[a-f0-9]{64}$/,uk=/^[a-f0-9]{40,64}$/i;function ri(e){return typeof e==="string"&&e?e:void 0}function Ps(e,t){try{return JSON.parse(String(e))}catch{return t}}function Xf(e){return{id:String(e.id),organizationId:String(e.organization_id),digest:String(e.digest),kind:String(e.kind),uri:String(e.uri),size:Number(e.size),mediaType:String(e.media_type),provenance:Ps(e.provenance,{}),attestation:Ps(e.attestation,{}),verifiedAt:String(e.verified_at),createdAt:String(e.created_at)}}function eh(e){return{id:String(e.id),organizationId:String(e.organization_id),projectId:String(e.project_id),environmentId:String(e.environment_id),resourceId:String(e.resource_id),artifactId:String(e.artifact_id),kind:String(e.kind),sourceSha:ri(e.source_sha),configHash:String(e.config_hash),manifest:Ps(e.manifest,{}),provenance:Ps(e.provenance,{}),strategy:String(e.strategy),status:String(e.status),healthGate:e.health_gate==null?void 0:Ps(e.health_gate,void 0),hooks:Ps(e.hooks,{migrations:"none",preActivate:[],postActivate:[]}),drainSeconds:Number(e.drain_seconds),graceSeconds:Number(e.grace_seconds),automaticRollback:Number(e.automatic_rollback)===1,rollbackAttempts:Number(e.rollback_attempts),previousReleaseId:ri(e.previous_release_id),promotedFromReleaseId:ri(e.promoted_from_release_id),actorId:ri(e.actor_id),trigger:String(e.trigger),pinned:Number(e.pinned)===1,pinReason:ri(e.pin_reason),activatedAt:ri(e.activated_at),failedAt:ri(e.failed_at),createdAt:String(e.created_at),updatedAt:String(e.updated_at)}}function pk(e){return{sequence:Number(e.sequence),id:String(e.id),releaseId:String(e.release_id),fromStatus:ri(e.from_status),toStatus:String(e.to_status),trafficPercent:e.traffic_percent==null?void 0:Number(e.traffic_percent),health:Ps(e.health,{}),message:String(e.message),operationId:ri(e.operation_id),createdAt:String(e.created_at)}}function mk(e,t){let r=[],o=(m,h,I)=>{if(JSON.stringify(h)===JSON.stringify(I))return;if(h&&I&&typeof h==="object"&&typeof I==="object"&&!Array.isArray(h)&&!Array.isArray(I)){for(let R of[...new Set([...Object.keys(h),...Object.keys(I)])].sort())o(m?`${m}.${R}`:R,h[R],I[R]);return}r.push({path:m,...h!==void 0?{before:h}:{},...I!==void 0?{after:I}:{}})};return o("",e,t),r}class ho{controlPlane;now;id;constructor(e,t={}){this.controlPlane=e;this.now=t.now??(()=>new Date),this.id=t.id??(()=>crypto.randomUUID())}registerArtifact(e){if(!lk.test(e.digest)||!/^(?:s3|https|oci|file):/.test(e.uri)||!Number.isInteger(e.size)||e.size<0)throw Error("Artifact requires a SHA-256 digest, supported immutable URI, and non-negative byte size");let t=this.controlPlane.database.query("SELECT * FROM release_artifacts WHERE organization_id=? AND digest=?").get(e.organizationId,e.digest);if(t)return Xf(t);let r=this.id(),o=this.now().toISOString();return this.controlPlane.database.run("INSERT INTO release_artifacts (id, organization_id, digest, kind, uri, size, media_type, provenance, attestation, verified_at, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",[r,e.organizationId,e.digest,e.kind,e.uri,e.size,e.mediaType??"application/octet-stream",JSON.stringify(s(e.provenance??{})),JSON.stringify(s(e.attestation??{})),o,o]),this.getArtifact(r)}getArtifact(e){let t=this.controlPlane.database.query("SELECT * FROM release_artifacts WHERE id=?").get(e);return t?Xf(t):void 0}create(e){let t=this.getArtifact(e.artifactId),r=this.controlPlane.getResource(e.resourceId),o=this.controlPlane.listEnvironments(e.projectId).find((N)=>N.id===e.environmentId);if(!t||t.organizationId!==e.organizationId||t.kind!==e.kind)throw Error("Release artifact does not match the organization and deployable kind");if(!r||r.projectId!==e.projectId||r.environmentId!==e.environmentId||!o)throw Error("Release target scope does not match the resource");if(e.sourceSha&&!uk.test(e.sourceSha))throw Error("Release source must be an immutable commit SHA");Ei({kind:e.kind,provider:r.provider,hasHealthGate:!!e.healthGate,replicas:e.replicas},e.strategy);let m=s(e.config),h=dk("sha256").update(JSON.stringify(m)).digest("hex"),I=this.active(e.resourceId),R=this.id(),A=this.now().toISOString(),E=e.approvalRequired?"awaiting_approval":"built",x=e.hooks??{migrations:"none",preActivate:[],postActivate:[]};return this.controlPlane.database.run("INSERT INTO releases (id, organization_id, project_id, environment_id, resource_id, artifact_id, kind, source_sha, config_hash, manifest, provenance, strategy, status, health_gate, hooks, drain_seconds, grace_seconds, automatic_rollback, previous_release_id, promoted_from_release_id, actor_id, trigger, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",[R,e.organizationId,e.projectId,e.environmentId,e.resourceId,e.artifactId,e.kind,e.sourceSha??null,h,JSON.stringify(s(e.manifest)),JSON.stringify(s(e.provenance??{})),e.strategy,E,e.healthGate?JSON.stringify(e.healthGate):null,JSON.stringify(x),Math.max(0,Math.min(3600,Math.floor(e.drainSeconds??30))),Math.max(0,Math.min(3600,Math.floor(e.graceSeconds??30))),e.automaticRollback===!1?0:1,I?.id??null,e.promotedFromReleaseId??null,e.actorId??null,e.trigger??"manual",A,A]),this.recordTransition(R,void 0,E,{message:e.promotedFromReleaseId?"Immutable artifact promoted without rebuild.":"Immutable release record created."}),this.get(R)}get(e){let t=this.controlPlane.database.query("SELECT * FROM releases WHERE id=?").get(e);return t?eh(t):void 0}list(e={}){return this.controlPlane.database.query("SELECT * FROM releases ORDER BY created_at DESC, rowid DESC").all().map(eh).filter((t)=>(!e.projectId||t.projectId===e.projectId)&&(!e.environmentId||t.environmentId===e.environmentId)&&(!e.resourceId||t.resourceId===e.resourceId)&&(!e.status||t.status===e.status))}active(e){return this.list({resourceId:e,status:"active"})[0]}transitions(e){return this.controlPlane.database.query("SELECT * FROM release_transitions WHERE release_id=? ORDER BY sequence").all(e).map(pk)}recordTransition(e,t,r,o){this.controlPlane.database.run("INSERT INTO release_transitions (id, release_id, from_status, to_status, traffic_percent, health, message, operation_id, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",[this.id(),e,t??null,r,o.trafficPercent??null,JSON.stringify(s(o.health??{})),o.message.slice(0,1000),o.operationId??null,this.now().toISOString()])}progress(e,t){let r=this.get(e);if(!r)throw Error(`Release ${e} was not found`);return this.recordTransition(e,r.status,r.status,t),this.transitions(e).at(-1)}transition(e,t,r){let o=this.get(e);if(!o)throw Error(`Release ${e} was not found`);if(!{built:["awaiting_approval","activating","failed"],awaiting_approval:["built","failed"],activating:["active","failed","rolled_back"],active:["superseded","failed","rolled_back"],failed:["activating","rolled_back"],rolled_back:["activating"],superseded:["activating"]}[o.status].includes(t))throw Error(`Invalid release transition ${o.status} → ${t}`);let h=this.now().toISOString();if(t==="active"){let I=this.active(o.resourceId);if(I&&I.id!==e)this.controlPlane.database.run("UPDATE releases SET status='superseded', updated_at=? WHERE id=?",[h,I.id]),this.recordTransition(I.id,"active","superseded",{message:`Superseded by ${e}.`,operationId:r.operationId})}return this.controlPlane.database.run("UPDATE releases SET status=?, rollback_attempts=rollback_attempts+?, activated_at=CASE WHEN ?='active' THEN ? ELSE activated_at END, failed_at=CASE WHEN ?='failed' THEN ? ELSE failed_at END, updated_at=? WHERE id=?",[t,t==="rolled_back"?1:0,t,h,t,h,h,e]),this.recordTransition(e,o.status,t,r),this.get(e)}approve(e,t){let r=this.get(e);if(!r||r.status!=="awaiting_approval")throw Error("Release is not awaiting approval");let o=this.id(),m=this.now().toISOString();return this.controlPlane.database.run("INSERT INTO release_approvals (id, release_id, environment_id, decision, actor_id, comment, created_at) VALUES (?, ?, ?, ?, ?, ?, ?)",[o,e,r.environmentId,t.decision,t.actorId,t.comment?.slice(0,1000)??null,m]),this.transition(e,t.decision==="approved"?"built":"failed",{message:t.decision==="approved"?"Environment gate approved.":"Environment gate rejected."})}approvals(e){return this.controlPlane.database.query("SELECT * FROM release_approvals WHERE release_id=? ORDER BY created_at").all(e).map((t)=>({id:String(t.id),releaseId:String(t.release_id),environmentId:String(t.environment_id),decision:String(t.decision),actorId:String(t.actor_id),comment:ri(t.comment),createdAt:String(t.created_at)}))}promote(e,t){let r=this.get(e);if(!r||!["active","superseded"].includes(r.status))throw Error("Only a tested active/superseded release can be promoted");return this.create({organizationId:r.organizationId,projectId:r.projectId,environmentId:t.targetEnvironmentId,resourceId:t.targetResourceId,artifactId:r.artifactId,kind:r.kind,sourceSha:r.sourceSha,config:t.config,manifest:r.manifest,provenance:r.provenance,strategy:t.strategy??r.strategy,healthGate:t.healthGate??r.healthGate,hooks:r.hooks,drainSeconds:r.drainSeconds,graceSeconds:r.graceSeconds,automaticRollback:r.automaticRollback,promotedFromReleaseId:r.id,actorId:t.actorId,trigger:"promotion",approvalRequired:t.approvalRequired})}compare(e,t){let r=this.get(e),o=this.get(t);if(!r||!o)throw Error("Both releases are required for comparison");return{artifactChanged:r.artifactId!==o.artifactId,sourceChanged:r.sourceSha!==o.sourceSha,configChanged:r.configHash!==o.configHash,manifestChanges:mk(r.manifest,o.manifest),dataCaveat:o.hooks.migrations==="none"?"No database migration is recorded.":`Database hooks are ${o.hooks.migrations}; release rollback does not roll back data.`}}pin(e,t,r){if(!this.get(e))throw Error("Release was not found");return this.controlPlane.database.run("UPDATE releases SET pinned=?, pin_reason=?, updated_at=? WHERE id=?",[t?1:0,t?r?.slice(0,500)??"operator":null,this.now().toISOString(),e]),this.get(e)}retentionCandidates(e){let t=Math.max(1,Math.min(100,Math.floor(e.keepPerResource??10))),r=new Map;for(let o of this.list()){let m=r.get(o.resourceId)??[];m.push(o),r.set(o.resourceId,m)}return[...r.values()].flatMap((o)=>o.slice(t)).filter((o)=>new Date(o.createdAt)<e.before&&!o.pinned&&!["active","activating","awaiting_approval"].includes(o.status)&&!this.list().some((m)=>m.previousReleaseId===o.id&&["active","activating"].includes(m.status)))}}class dt{controlPlane;releases;queue;constructor(e){this.controlPlane=e;this.releases=new ho(e),this.queue=new c(e)}enqueueActivation(e,t={}){let r=this.releases.get(e.id);if(!r)throw Error("Release was not found");if(!["built","failed","rolled_back","superseded"].includes(r.status))throw Error(`Release ${r.status} cannot be activated`);let o=t.action??"activate",m=this.queue.enqueue({projectId:r.projectId,environmentId:r.environmentId,resourceId:r.resourceId,actorId:t.actorId,kind:`release.${o}`,idempotencyKey:`release:${r.id}:${o}:${r.rollbackAttempts}`,correlationId:`release:${r.id}`,input:{releaseId:r.id,targetReleaseId:t.targetReleaseId??null},lockKey:`resource:${r.resourceId}`,providerKey:this.controlPlane.getResource(r.resourceId)?.provider??"default",maxAttempts:r.automaticRollback?2:1,retryClasses:["network","provider_throttled","provider_unavailable"],resumePolicy:"fail",cancellationMode:"provider_non_cancellable",retentionDays:365}).operation;return this.releases.transition(r.id,"activating",{message:`${o} queued.`,operationId:m.id,trafficPercent:0}),m}completeHealthGate(e,t){let r=this.releases.get(e);if(!r||r.status!=="activating")throw Error("Release is not activating");if(t.healthy)return{release:this.releases.transition(r.id,"active",{message:t.message??"Health gate passed; activation completed.",operationId:t.operationId,trafficPercent:100,health:t.health})};let o=this.releases.transition(r.id,"failed",{message:t.message??"Health gate failed.",operationId:t.operationId,trafficPercent:0,health:t.health}),m=r.previousReleaseId?this.releases.get(r.previousReleaseId):void 0;if(!r.automaticRollback||r.rollbackAttempts>=1||!m)return{release:o};let h=this.queue.enqueue({projectId:r.projectId,environmentId:r.environmentId,resourceId:r.resourceId,kind:"release.rollback",idempotencyKey:`release:${r.id}:automatic-rollback`,correlationId:`release:${r.id}`,input:{releaseId:r.id,targetReleaseId:m.id,automatic:!0},lockKey:`resource:${r.resourceId}`,providerKey:this.controlPlane.getResource(r.resourceId)?.provider??"default",maxAttempts:1,retryClasses:[],resumePolicy:"fail",cancellationMode:"provider_non_cancellable",retentionDays:365}).operation;return{release:o,rollbackOperation:h}}enqueueRollback(e,t={}){let r=this.releases.get(e.id);if(!r||r.status!=="active")throw Error("Only the active release can be rolled back");let o=t.targetReleaseId?this.releases.get(t.targetReleaseId):r.previousReleaseId?this.releases.get(r.previousReleaseId):void 0;if(!o||o.resourceId!==r.resourceId||!["superseded","active"].includes(o.status))throw Error("Rollback requires a preserved prior release for the same resource");return this.queue.enqueue({projectId:r.projectId,environmentId:r.environmentId,resourceId:r.resourceId,actorId:t.actorId,kind:"release.rollback",idempotencyKey:`release:${r.id}:manual-rollback:${o.id}`,correlationId:`release:${r.id}`,input:{releaseId:r.id,targetReleaseId:o.id,automatic:!1},lockKey:`resource:${r.resourceId}`,providerKey:this.controlPlane.getResource(r.resourceId)?.provider??"default",maxAttempts:1,retryClasses:[],resumePolicy:"fail",cancellationMode:"provider_non_cancellable",retentionDays:365}).operation}}function Kg(e){if(e.strategy==="canary")return[5,25,50,100].map((t,r)=>({step:`canary-${t}`,trafficPercent:t,waitSeconds:r===3?e.drainSeconds:e.graceSeconds,requiresHealth:!0}));if(e.strategy==="blue_green")return[{step:"green-health",trafficPercent:0,waitSeconds:e.graceSeconds,requiresHealth:!0},{step:"traffic-switch",trafficPercent:100,waitSeconds:e.drainSeconds,requiresHealth:!0}];if(e.strategy==="rolling")return[{step:"rolling-replacement",trafficPercent:50,waitSeconds:e.graceSeconds,requiresHealth:!0},{step:"rolling-complete",trafficPercent:100,waitSeconds:e.drainSeconds,requiresHealth:!0}];return[{step:"immutable-switch",trafficPercent:100,waitSeconds:e.drainSeconds,requiresHealth:!!e.healthGate}]}async function $d(e,t){if(t.release.artifactId!==t.artifact.id)throw Error("Release activation artifact identity changed");Ei({kind:t.release.kind,hasHealthGate:!!t.release.healthGate,replicas:Number(t.release.manifest?.replicas)||void 0},t.release.strategy);let r=e.capability(t);if(!r.supported)throw Error(`${t.release.strategy} is unavailable in ${e.name}: ${r.explanation}`);return e.activate({...t,transitions:Kg(t.release)})}async function Md(e,t){if(!t.previous)throw Error("Rollback requires the preserved previous release");return e.rollback({...t,transitions:[{step:"rollback",trafficPercent:0,waitSeconds:t.release.drainSeconds,requiresHealth:!0}]})}var Wg=["release.activate","release.rollback"];function gk(e){return e&&typeof e==="object"&&!Array.isArray(e)?e:{}}function Fd(e){let t=new dt(e.store),r=async(o)=>{let m=gk(o.operation.input),h=typeof m.releaseId==="string"?m.releaseId:"",I=t.releases.get(h);if(!I)throw Error(`Queued release ${h||"(missing)"} was not found`);if(I.projectId!==o.operation.projectId||I.environmentId!==o.operation.environmentId||I.resourceId!==o.operation.resourceId)throw Error("Queued release does not match the operation scope");let R=t.releases.getArtifact(I.artifactId);if(!R||R.digest!==t.releases.getArtifact(I.artifactId)?.digest)throw Error("Immutable release artifact is unavailable");let A=typeof m.targetReleaseId==="string"&&m.targetReleaseId?m.targetReleaseId:I.previousReleaseId,E=A?t.releases.get(A):void 0;if(E&&(E.resourceId!==I.resourceId||E.artifactId===I.artifactId&&E.id===I.id))throw Error("Rollback target does not match the release resource");let x=await e.resolveDriver(I);o.checkpoint("verify",`Verifying ${R.digest} and ${I.configHash} for ${x.name}.`),o.log(`Artifact ${R.digest}; source ${I.sourceSha??"not recorded"}; config ${I.configHash}.`,{stream:"system"}),o.throwIfCancellationRequested();let N;try{N=o.operation.kind==="release.rollback"?await Md(x,{release:I,artifact:R,previous:E}):await $d(x,{release:I,artifact:R,previous:E})}catch(F){if(o.operation.kind==="release.activate"&&t.releases.get(I.id)?.status==="activating")t.completeHealthGate(I.id,{healthy:!1,operationId:o.operation.id,message:F instanceof Error?F.message:String(F)});throw F}for(let F of N.transitions)t.releases.progress(I.id,{message:`${o.operation.kind==="release.rollback"?"Rollback":"Activation"} traffic at ${F.trafficPercent}%.`,trafficPercent:F.trafficPercent,health:F.observed,operationId:o.operation.id}),o.log(`Traffic ${F.trafficPercent}%: ${F.healthy?"healthy":"unhealthy"}.`,{stream:F.healthy?"step":"stderr",step:"health"});if(o.checkpoint("health","Evaluating the provider health result."),!N.activated||!N.healthy){if(o.operation.kind==="release.activate"&&t.releases.get(I.id)?.status==="activating")t.completeHealthGate(I.id,{healthy:!1,operationId:o.operation.id,health:{resourceVersions:N.resourceVersions},message:N.error??"Provider health gate failed."});throw Error(N.error??`${x.name} reported an unhealthy release`)}if(o.operation.kind==="release.activate"&&I.healthGate?.name){if(!e.resolveHealthGate)throw t.completeHealthGate(I.id,{healthy:!1,operationId:o.operation.id,message:`Named health gate ${I.healthGate.name} is not available to this worker.`}),Error(`Named health gate ${I.healthGate.name} is unavailable`);o.checkpoint("named-health-gate",`Waiting on ${I.healthGate.name}.`);let F=await e.resolveHealthGate(I.healthGate.name,I);if(o.log(`${I.healthGate.name}: ${F.healthy?"healthy":"unhealthy"}${F.message?` — ${F.message}`:""}.`,{stream:F.healthy?"step":"stderr",step:"health"}),!F.healthy)throw t.completeHealthGate(I.id,{healthy:!1,operationId:o.operation.id,health:F.evidence,message:F.message??`Named health gate ${I.healthGate.name} failed.`}),Error(F.message??`Named health gate ${I.healthGate.name} failed`)}if(o.operation.kind==="release.rollback"){let F=t.releases.get(I.id);if(F.status!=="rolled_back")t.releases.transition(F.id,"rolled_back",{message:`Traffic restored through ${x.name}.`,operationId:o.operation.id,trafficPercent:0,health:{resourceVersions:N.resourceVersions}});if(E&&E.status!=="active"){let z=E.status==="activating"?E:t.releases.transition(E.id,"activating",{message:"Rollback target restoring traffic.",operationId:o.operation.id,trafficPercent:0});t.releases.transition(z.id,"active",{message:"Previous immutable release restored.",operationId:o.operation.id,trafficPercent:100,health:{resourceVersions:N.resourceVersions}})}}else t.completeHealthGate(I.id,{healthy:!0,operationId:o.operation.id,health:{providerVersion:N.providerVersion??null,resourceVersions:N.resourceVersions},message:"Provider activation and health gates completed."});return o.checkpoint("finalize","Persisted the terminal release state."),{releaseId:I.id,artifactDigest:R.digest,provider:x.name,providerVersion:N.providerVersion??null,resourceVersions:N.resourceVersions,status:t.releases.get(I.id).status}};return Object.fromEntries(Wg.map((o)=>[o,r]))}import{createCipheriv as fk,createDecipheriv as hk,createHash as oh,randomBytes as dp}from"node:crypto";var sh={github:{repositories:!0,branches:!0,tags:!0,webhooks:!0,pullRequests:!0,tokenRefresh:!0,deployKeys:!0},gitlab:{repositories:!0,branches:!0,tags:!0,webhooks:!0,pullRequests:!0,tokenRefresh:!0,deployKeys:!0},bitbucket:{repositories:!0,branches:!0,tags:!0,webhooks:!0,pullRequests:!0,tokenRefresh:!0,deployKeys:!0},gitea:{repositories:!0,branches:!0,tags:!0,webhooks:!0,pullRequests:!0,tokenRefresh:!1,deployKeys:!0},generic_https:{repositories:!1,branches:!0,tags:!0,webhooks:!1,pullRequests:!1,tokenRefresh:!1,deployKeys:!1},generic_ssh:{repositories:!1,branches:!0,tags:!0,webhooks:!1,pullRequests:!1,tokenRefresh:!1,deployKeys:!0}};function or(e){return typeof e==="string"&&e?e:void 0}function Ts(e,t){try{return typeof e==="string"?JSON.parse(e):t}catch{return t}}function si(e){return JSON.stringify(s(e))}function _s(e){return oh("sha256").update(e).digest("hex")}function th(e){let t=e.trim().replace(/\/$/,""),r=new URL(t.includes("://")?t:`https://${t}`);if(r.username||r.password||r.search||r.hash)throw Error("Source host cannot contain credentials, a query, or a fragment");if(r.protocol!=="https:"&&!(r.protocol==="http:"&&["localhost","127.0.0.1","::1"].includes(r.hostname)))throw Error("Source host must use HTTPS");return`${r.protocol}//${r.host}${r.pathname.replace(/\/$/,"")}`}function rh(e){let t=e.trim();if(t.length<2||t.length>80)throw Error("Connection name must contain 2-80 characters");return t}function lp(e){let t=e.trim().replace(/^\/+|\/+$/g,"");if(!/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(t))throw Error("Repository must use owner/name format");return t}function nh(e){let t=e.trim()||".";if(t.startsWith("/")||t.split("/").includes("..")||/[\0\r\n]/.test(t))throw Error("Monorepo root must stay inside the repository");return t.replace(/^\.\//,"")||"."}function zc(e){return[...new Set((e??[]).map((t)=>t.trim()).filter(Boolean))].slice(0,100)}function yk(e){let t=e.trim();if(/\r|\n/.test(t)||/https?:\/\/[^/@\s]+:[^/@\s]+@/i.test(t))throw Error("Clone URL cannot contain credentials or control characters");if(!/^(?:https?:\/\/|ssh:\/\/|git@[A-Za-z0-9.-]+:)/.test(t))throw Error("Clone URL must use HTTPS or SSH");return t}function vk(e){return{id:String(e.id),organizationId:String(e.organization_id),provider:String(e.provider),name:String(e.name),host:String(e.host),owner:or(e.owner),authKind:String(e.auth_kind),credentialConfigured:!!e.credential_ciphertext,credentialFingerprint:or(e.credential_fingerprint),grantedScopes:Ts(e.granted_scopes,[]),capabilities:Ts(e.capabilities,sh[String(e.provider)]),status:String(e.status),healthMessage:or(e.health_message),lastTestedAt:or(e.last_tested_at),lastSyncedAt:or(e.last_synced_at),credentialExpiresAt:or(e.credential_expires_at),version:Number(e.version),createdByActorId:or(e.created_by_actor_id),createdAt:String(e.created_at),updatedAt:String(e.updated_at)}}function up(e){return{id:String(e.id),connectionId:String(e.connection_id),providerRepositoryId:String(e.provider_repository_id),fullName:String(e.full_name),cloneUrl:String(e.clone_url),defaultBranch:String(e.default_branch),visibility:String(e.visibility),archived:Number(e.archived)===1,metadata:Ts(e.metadata,{}),syncedAt:String(e.synced_at)}}function ih(e){return{id:String(e.id),projectId:String(e.project_id),environmentId:or(e.environment_id),resourceId:or(e.resource_id),connectionId:String(e.connection_id),repositoryId:or(e.repository_id),repositoryFullName:String(e.repository_full_name),defaultBranch:String(e.default_branch),branchRule:or(e.branch_rule),tagRule:or(e.tag_rule),monorepoRoot:String(e.monorepo_root),includePaths:Ts(e.include_paths,[]),excludePaths:Ts(e.exclude_paths,[]),submodules:Number(e.submodules)===1,cloneDepth:e.clone_depth==null?void 0:Number(e.clone_depth),deployKeyId:or(e.deploy_key_id),autoDeploy:Number(e.auto_deploy)===1,pullRequestPreviews:Number(e.pull_request_previews)===1,status:String(e.status),disabledReason:or(e.disabled_reason),version:Number(e.version),createdByActorId:or(e.created_by_actor_id),createdAt:String(e.created_at),updatedAt:String(e.updated_at)}}function pp(e){return{id:String(e.id),connectionId:String(e.connection_id),name:String(e.name),publicKey:String(e.public_key),publicKeyFingerprint:String(e.public_key_fingerprint),host:String(e.host),hostKey:String(e.host_key),hostKeyFingerprint:String(e.host_key_fingerprint),createdByActorId:or(e.created_by_actor_id),createdAt:String(e.created_at),updatedAt:String(e.updated_at)}}function mp(e){return{id:String(e.id),connectionId:String(e.connection_id),repositoryId:or(e.repository_id),repositoryFullName:String(e.repository_full_name),providerWebhookId:or(e.provider_webhook_id),events:Ts(e.events,[]),status:String(e.status),healthMessage:or(e.health_message),lastDeliveryAt:or(e.last_delivery_at),lastReconciledAt:or(e.last_reconciled_at),createdAt:String(e.created_at),updatedAt:String(e.updated_at)}}function gp(e){return{id:String(e.id),connectionId:String(e.connection_id),webhookId:String(e.webhook_id),providerDeliveryId:String(e.provider_delivery_id),event:String(e.event),action:or(e.action),commitSha:or(e.commit_sha),signatureStatus:String(e.signature_status),status:String(e.status),payloadSummary:Ts(e.payload_summary,{}),operationId:or(e.operation_id),error:or(e.error),receivedAt:String(e.received_at),processedAt:or(e.processed_at)}}class fo{controlPlane;key;nowFn;idFn;constructor(e,t={}){this.controlPlane=e;this.key=t.encryptionKey?oh("sha256").update(t.encryptionKey).digest():void 0,this.nowFn=t.now??(()=>new Date),this.idFn=t.id??(()=>crypto.randomUUID())}now(){return this.nowFn().toISOString()}run(e,t){this.controlPlane.database.run(e,t)}encrypt(e){if(!this.key)throw Error("Source credential encryption key is not configured");let t=dp(12),r=fk("aes-256-gcm",this.key,t),o=Buffer.concat([r.update(e,"utf8"),r.final()]);return`v1.${t.toString("base64url")}.${r.getAuthTag().toString("base64url")}.${o.toString("base64url")}`}decrypt(e){if(!this.key)throw Error("Source credential encryption key is not configured");let[t,r,o,m]=e.split(".");if(t!=="v1"||!r||!o||!m)throw Error("Encrypted source credential is unavailable");let h=hk("aes-256-gcm",this.key,Buffer.from(r,"base64url"));return h.setAuthTag(Buffer.from(o,"base64url")),Buffer.concat([h.update(Buffer.from(m,"base64url")),h.final()]).toString("utf8")}createConnection(e){if(!this.controlPlane.getOrganization(e.organizationId))throw Error("Source connection organization was not found");let t=this.now(),r=this.idFn(),o=e.credential?JSON.stringify(e.credential):void 0;return this.run(`INSERT INTO source_connections (id, organization_id, provider, name, host, owner, auth_kind, credential_ciphertext, credential_fingerprint, granted_scopes, capabilities, status, credential_expires_at, created_by_actor_id, created_at, updated_at)
|
|
43
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'pending', ?, ?, ?, ?)`,[r,e.organizationId,e.provider,rh(e.name),th(e.host),e.owner?.trim()||null,e.authKind??(o?"access_token":"none"),o?this.encrypt(o):null,o?_s(o).slice(0,16):null,si([...new Set(e.grantedScopes??[])].sort()),si(sh[e.provider]),e.credentialExpiresAt??null,e.createdByActorId??null,t,t]),this.controlPlane.appendEvent({organizationId:e.organizationId,actorId:e.createdByActorId,type:"source.connection.created",payload:{connectionId:r,provider:e.provider,host:th(e.host),owner:e.owner??null,scopes:e.grantedScopes??[]}}),this.getConnection(r)}getConnection(e){let t=this.controlPlane.database.query("SELECT * FROM source_connections WHERE id = ?").get(e);return t?this.currentConnection(t):void 0}listConnections(e){return this.controlPlane.database.query("SELECT * FROM source_connections WHERE organization_id = ? ORDER BY name").all(e).map((t)=>this.currentConnection(t))}currentConnection(e){let t=vk(e);if(t.status!=="disconnected"&&t.credentialExpiresAt&&new Date(t.credentialExpiresAt).getTime()<=this.nowFn().getTime())return{...t,status:"expired",healthMessage:"Source credential expired; rotate it before deploying."};return t}getCredential(e){let t=this.controlPlane.database.query("SELECT credential_ciphertext FROM source_connections WHERE id = ?").get(e);return t?.credential_ciphertext?JSON.parse(this.decrypt(String(t.credential_ciphertext))):void 0}rotateCredential(e,t,r={}){let o=this.getConnection(e);if(!o||o.status==="disconnected")throw Error("Active source connection was not found");let m=JSON.stringify(t);return this.run("UPDATE source_connections SET credential_ciphertext = ?, credential_fingerprint = ?, credential_expires_at = ?, status = 'pending', health_message = NULL, version = version + 1, updated_at = ? WHERE id = ?",[this.encrypt(m),_s(m).slice(0,16),r.expiresAt??null,this.now(),e]),this.controlPlane.appendEvent({organizationId:o.organizationId,actorId:r.actorId,type:"source.credential.rotated",payload:{connectionId:e,fingerprint:_s(m).slice(0,16),expiresAt:r.expiresAt??null}}),this.getConnection(e)}updateHealth(e,t){let r=this.getConnection(e);if(!r||["disconnected","expired"].includes(r.status))throw Error("Active source connection was not found");let o=this.now(),m=t.grantedScopes?[...new Set(t.grantedScopes.map((h)=>h.trim()).filter(Boolean))].sort():void 0;if(this.run("UPDATE source_connections SET status = ?, health_message = ?, granted_scopes = COALESCE(?, granted_scopes), last_tested_at = COALESCE(?, last_tested_at), last_synced_at = COALESCE(?, last_synced_at), version = version + 1, updated_at = ? WHERE id = ?",[t.status,t.message?.slice(0,1000)??null,m?si(m):null,t.tested?o:null,t.synced?o:null,o,e]),m&&JSON.stringify(m)!==JSON.stringify([...r.grantedScopes].sort()))this.controlPlane.appendEvent({organizationId:r.organizationId,type:"source.connection.scopes_updated",payload:{connectionId:e,grantedScopes:m}});return this.getConnection(e)}upsertRepository(e){let t=this.getConnection(e.connectionId);if(!t||["disconnected","expired"].includes(t.status))throw Error("Active source connection was not found");let r=this.now(),o=this.controlPlane.database.query("SELECT id FROM source_repositories WHERE connection_id = ? AND provider_repository_id = ?").get(e.connectionId,e.providerRepositoryId),m=o?String(o.id):this.idFn();return this.run(`INSERT INTO source_repositories (id, connection_id, provider_repository_id, full_name, clone_url, default_branch, visibility, archived, metadata, synced_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
44
|
+
ON CONFLICT(connection_id, provider_repository_id) DO UPDATE SET full_name=excluded.full_name, clone_url=excluded.clone_url, default_branch=excluded.default_branch, visibility=excluded.visibility, archived=excluded.archived, metadata=excluded.metadata, synced_at=excluded.synced_at`,[m,e.connectionId,e.providerRepositoryId,lp(e.fullName),yk(e.cloneUrl),e.defaultBranch.trim()||"main",e.visibility,e.archived?1:0,si(e.metadata),r]),up(this.controlPlane.database.query("SELECT * FROM source_repositories WHERE id = ?").get(m))}listRepositories(e,t,r=100){let o=t?.trim().toLowerCase();return(o?this.controlPlane.database.query("SELECT * FROM source_repositories WHERE connection_id = ? AND lower(full_name) LIKE ? ORDER BY full_name LIMIT ?").all(e,`%${o}%`,Math.min(500,r)):this.controlPlane.database.query("SELECT * FROM source_repositories WHERE connection_id = ? ORDER BY full_name LIMIT ?").all(e,Math.min(500,r))).map(up)}getRepository(e){let t=this.controlPlane.database.query("SELECT * FROM source_repositories WHERE id = ?").get(e);return t?up(t):void 0}createDeployKey(e){let t=this.getConnection(e.connectionId);if(!t||["disconnected","expired"].includes(t.status)||!t.capabilities.deployKeys)throw Error("An active deploy-key capable source connection is required");if(!/^ssh-(?:ed25519|rsa)\s+[A-Za-z0-9+/=]+/.test(e.publicKey.trim()))throw Error("A valid SSH public key is required");if(!/BEGIN (?:OPENSSH|RSA) PRIVATE KEY/.test(e.privateKey))throw Error("A valid SSH private key is required");if(!/^(?:ssh-ed25519|ssh-rsa|ecdsa-sha2-nistp\d+)\s+[A-Za-z0-9+/=]+/.test(e.hostKey.trim()))throw Error("A pinned SSH host key is required");let r=this.idFn(),o=this.now(),m=`sha256:${_s(e.publicKey.trim())}`,h=`sha256:${_s(e.hostKey.trim())}`;return this.run("INSERT INTO source_deploy_keys (id, connection_id, name, public_key, public_key_fingerprint, private_key_ciphertext, host, host_key, host_key_fingerprint, created_by_actor_id, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",[r,e.connectionId,rh(e.name),e.publicKey.trim(),m,this.encrypt(e.privateKey),e.host.trim().toLowerCase(),e.hostKey.trim(),h,e.actorId??null,o,o]),this.controlPlane.appendEvent({organizationId:t.organizationId,actorId:e.actorId,type:"source.deploy_key.created",payload:{connectionId:e.connectionId,deployKeyId:r,publicFingerprint:m,hostFingerprint:h}}),pp(this.controlPlane.database.query("SELECT * FROM source_deploy_keys WHERE id = ?").get(r))}getDeployKey(e){let t=this.controlPlane.database.query("SELECT * FROM source_deploy_keys WHERE id = ?").get(e);return t?pp(t):void 0}listDeployKeys(e){return this.controlPlane.database.query("SELECT * FROM source_deploy_keys WHERE connection_id = ? ORDER BY name, created_at").all(e).map(pp)}revokeDeployKey(e,t){let r=this.getDeployKey(e),o=r?this.getConnection(r.connectionId):void 0;if(!r||!o)throw Error("Deploy key was not found");let m=this.listBindings({connectionId:o.id}).filter((I)=>I.deployKeyId===e),h=this.now();return this.controlPlane.database.transaction(()=>{this.run("UPDATE source_bindings SET deploy_key_id=NULL, status='disabled', auto_deploy=0, disabled_reason='Source deploy key was revoked', version=version+1, updated_at=? WHERE deploy_key_id=?",[h,e]),this.run("DELETE FROM source_deploy_keys WHERE id=?",[e])})(),this.controlPlane.appendEvent({organizationId:o.organizationId,actorId:t,type:"source.deploy_key.revoked",level:"warning",payload:{connectionId:o.id,deployKeyId:e,affectedBindingIds:m.map((I)=>I.id)}}),{affectedBindings:m}}getDeployPrivateKey(e){let t=this.controlPlane.database.query("SELECT private_key_ciphertext FROM source_deploy_keys WHERE id = ?").get(e);if(!t)throw Error("Deploy key was not found");return this.decrypt(String(t.private_key_ciphertext))}createBinding(e){let t=this.getConnection(e.connectionId);if(!t||["disconnected","expired"].includes(t.status))throw Error("Active source connection was not found");if(e.repositoryId&&this.getRepository(e.repositoryId)?.connectionId!==t.id)throw Error("Repository does not belong to this connection");if(e.deployKeyId&&this.getDeployKey(e.deployKeyId)?.connectionId!==t.id)throw Error("Deploy key does not belong to this connection");let r=this.idFn(),o=this.now();return this.run(`INSERT INTO source_bindings (id, project_id, environment_id, resource_id, connection_id, repository_id, repository_full_name, default_branch, branch_rule, tag_rule, monorepo_root, include_paths, exclude_paths, submodules, clone_depth, deploy_key_id, auto_deploy, pull_request_previews, status, created_by_actor_id, created_at, updated_at)
|
|
45
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'active', ?, ?, ?)`,[r,e.projectId,e.environmentId??null,e.resourceId??null,e.connectionId,e.repositoryId??null,lp(e.repositoryFullName),e.defaultBranch?.trim()||"main",e.branchRule?.trim()||null,e.tagRule?.trim()||null,nh(e.monorepoRoot??"."),si(zc(e.includePaths)),si(zc(e.excludePaths)),e.submodules?1:0,e.cloneDepth??null,e.deployKeyId??null,e.autoDeploy===!1?0:1,e.pullRequestPreviews===!1?0:1,e.actorId??null,o,o]),this.controlPlane.appendEvent({organizationId:t.organizationId,projectId:e.projectId,actorId:e.actorId,type:"source.binding.created",payload:{bindingId:r,connectionId:e.connectionId,repository:e.repositoryFullName,branch:e.defaultBranch??"main"}}),this.getBinding(r)}getBinding(e){let t=this.controlPlane.database.query("SELECT * FROM source_bindings WHERE id = ?").get(e);return t?ih(t):void 0}listBindings(e={}){return this.controlPlane.database.query("SELECT * FROM source_bindings ORDER BY created_at DESC").all().map(ih).filter((r)=>(!e.connectionId||r.connectionId===e.connectionId)&&(!e.projectId||r.projectId===e.projectId)&&(!e.status||r.status===e.status))}updateBinding(e,t,r){let o=this.getBinding(e);if(!o||o.version!==t)throw Error(`Source binding ${e} changed since version ${t}`);let m=this.getConnection(o.connectionId);if((!m||["disconnected","expired"].includes(m.status))&&(r.status==="active"||r.autoDeploy===!0))throw Error("Rotate or reconnect the source credential before enabling this binding");if(r.deployKeyId&&this.getDeployKey(r.deployKeyId)?.connectionId!==o.connectionId)throw Error("Deploy key does not belong to this connection");let h=this.now();if(this.controlPlane.database.run("UPDATE source_bindings SET default_branch=?, branch_rule=?, tag_rule=?, monorepo_root=?, include_paths=?, exclude_paths=?, submodules=?, clone_depth=?, deploy_key_id=?, auto_deploy=?, pull_request_previews=?, status=?, disabled_reason=?, version=version+1, updated_at=? WHERE id=? AND version=?",[r.defaultBranch?.trim()||o.defaultBranch,r.branchRule===void 0?o.branchRule??null:r.branchRule.trim()||null,r.tagRule===void 0?o.tagRule??null:r.tagRule.trim()||null,r.monorepoRoot===void 0?o.monorepoRoot:nh(r.monorepoRoot),si(r.includePaths===void 0?o.includePaths:zc(r.includePaths)),si(r.excludePaths===void 0?o.excludePaths:zc(r.excludePaths)),r.submodules??o.submodules?1:0,r.cloneDepth===void 0?o.cloneDepth??null:r.cloneDepth,r.deployKeyId===void 0?o.deployKeyId??null:r.deployKeyId,r.autoDeploy??o.autoDeploy?1:0,r.pullRequestPreviews??o.pullRequestPreviews?1:0,r.status??o.status,r.disabledReason===void 0?o.disabledReason??null:r.disabledReason.slice(0,1000),h,e,t]).changes!==1)throw Error(`Source binding ${e} changed since version ${t}`);let R=this.getConnection(o.connectionId);return this.controlPlane.appendEvent({organizationId:R.organizationId,projectId:o.projectId,actorId:r.actorId,type:"source.binding.updated",payload:{bindingId:e,fromVersion:t,toVersion:t+1,status:r.status??o.status}}),this.getBinding(e)}createWebhook(e){let t=this.getConnection(e.connectionId);if(!t||["disconnected","expired"].includes(t.status))throw Error("Active source connection was not found");if(e.repositoryId&&this.getRepository(e.repositoryId)?.connectionId!==t.id)throw Error("Repository does not belong to this connection");let r=this.idFn(),o=this.now(),m=e.endpointToken??dp(24).toString("base64url"),h=e.secret??dp(32).toString("base64url"),I=[...new Set(e.events??["push","pull_request"])].filter((R)=>["push","pull_request"].includes(R));if(!I.length)throw Error("At least one supported webhook event is required");return this.run("INSERT INTO source_webhooks (id, connection_id, repository_id, repository_full_name, endpoint_token_hash, endpoint_token_ciphertext, secret_ciphertext, events, status, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'pending', ?, ?)",[r,e.connectionId,e.repositoryId??null,lp(e.repositoryFullName),_s(m),this.encrypt(m),this.encrypt(h),si(I),o,o]),this.controlPlane.appendEvent({organizationId:t.organizationId,type:"source.webhook.created",payload:{webhookId:r,connectionId:t.id,repository:e.repositoryFullName,events:I}}),{webhook:{...this.getWebhook(r),endpointToken:m},secret:h}}getWebhook(e){let t=this.controlPlane.database.query("SELECT * FROM source_webhooks WHERE id = ?").get(e);return t?mp(t):void 0}getWebhookByEndpointToken(e){let t=this.controlPlane.database.query("SELECT * FROM source_webhooks WHERE endpoint_token_hash = ?").get(_s(e));return t?mp(t):void 0}getWebhookSecret(e){let t=this.controlPlane.database.query("SELECT secret_ciphertext FROM source_webhooks WHERE id = ?").get(e);if(!t)throw Error("Source webhook was not found");return this.decrypt(String(t.secret_ciphertext))}getWebhookEndpointToken(e){let t=this.controlPlane.database.query("SELECT endpoint_token_ciphertext FROM source_webhooks WHERE id = ?").get(e);if(!t?.endpoint_token_ciphertext)throw Error("Source webhook endpoint token is unavailable");return this.decrypt(String(t.endpoint_token_ciphertext))}listWebhooks(e){return this.controlPlane.database.query("SELECT * FROM source_webhooks WHERE connection_id = ? ORDER BY repository_full_name").all(e).map(mp)}updateWebhookState(e,t){let r=this.getWebhook(e);if(!r)throw Error("Source webhook was not found");let o=this.now();this.run("UPDATE source_webhooks SET provider_webhook_id=COALESCE(?, provider_webhook_id), status=?, health_message=?, last_reconciled_at=COALESCE(?, last_reconciled_at), updated_at=? WHERE id=?",[t.providerWebhookId??null,t.status,t.healthMessage?.slice(0,1000)??null,t.reconciled?o:null,o,e]);let m=this.getConnection(r.connectionId);if(m)this.controlPlane.appendEvent({organizationId:m.organizationId,type:"source.webhook.updated",payload:{webhookId:e,connectionId:m.id,repository:r.repositoryFullName,status:t.status,reconciled:t.reconciled??!1,providerWebhookId:t.providerWebhookId??r.providerWebhookId??null}});return this.getWebhook(e)}getDeliveryByProviderId(e,t){let r=this.controlPlane.database.query("SELECT * FROM source_webhook_deliveries WHERE connection_id = ? AND provider_delivery_id = ?").get(e,t);return r?gp(r):void 0}recordDelivery(e){let t=this.getDeliveryByProviderId(e.connectionId,e.providerDeliveryId);if(t)return{delivery:t,duplicate:!0};let r=this.idFn(),o=this.now();return this.controlPlane.database.transaction(()=>{this.run("INSERT INTO source_webhook_deliveries (id, connection_id, webhook_id, provider_delivery_id, event, action, commit_sha, signature_status, status, payload_summary, operation_id, error, received_at, processed_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",[r,e.connectionId,e.webhookId,e.providerDeliveryId,e.event.slice(0,100),e.action?.slice(0,100)??null,e.commitSha?.slice(0,128)??null,e.signatureStatus,e.status,si(e.payloadSummary??{}),e.operationId??null,e.error?.slice(0,2000)??null,o,["enqueued","ignored","rejected","failed"].includes(e.status)?o:null]),this.run("UPDATE source_webhooks SET last_delivery_at=?, updated_at=? WHERE id=?",[o,o,e.webhookId])})(),{delivery:this.getDeliveryByProviderId(e.connectionId,e.providerDeliveryId),duplicate:!1}}updateDelivery(e,t){this.run("UPDATE source_webhook_deliveries SET status=?, operation_id=?, error=?, processed_at=? WHERE id=?",[t.status,t.operationId??null,t.error?.slice(0,2000)??null,this.now(),e]);let r=this.controlPlane.database.query("SELECT * FROM source_webhook_deliveries WHERE id = ?").get(e);if(!r)throw Error("Source webhook delivery was not found");return gp(r)}listDeliveries(e,t=100){return this.controlPlane.database.query("SELECT * FROM source_webhook_deliveries WHERE webhook_id = ? ORDER BY received_at DESC LIMIT ?").all(e,Math.min(500,Math.max(1,t))).map(gp)}disconnectConnection(e,t){let r=this.getConnection(e);if(!r)throw Error("Source connection was not found");let o=this.listBindings({connectionId:e,status:"active"}),m=this.now();return this.controlPlane.database.transaction(()=>{this.run("UPDATE source_connections SET status='disconnected', credential_ciphertext=NULL, health_message='Disconnected by an administrator', version=version+1, updated_at=? WHERE id=?",[m,e]),this.run("UPDATE source_bindings SET status='disabled', auto_deploy=0, disabled_reason='Source connection was disconnected', version=version+1, updated_at=? WHERE connection_id=? AND status='active'",[m,e]),this.run("UPDATE source_webhooks SET status='disabled', health_message='Source connection was disconnected', updated_at=? WHERE connection_id=?",[m,e])})(),this.controlPlane.appendEvent({organizationId:r.organizationId,actorId:t,type:"source.connection.disconnected",level:"warning",payload:{connectionId:e,affectedBindingIds:o.map((h)=>h.id),affectedRepositories:o.map((h)=>h.repositoryFullName)}}),{connection:this.getConnection(e),affectedBindings:o}}}import{createSign as bk}from"node:crypto";class qt extends Error{status;retryAfterSeconds;constructor(e,t,r){super(e);this.status=t;this.retryAfterSeconds=r;this.name="SourceProviderError"}}var ch={repositories:!0,branches:!0,tags:!0,webhooks:!0,pullRequests:!0,tokenRefresh:!0,deployKeys:!0};function rs(e){let t=Number(e??"1");return Number.isInteger(t)&&t>0?t:1}function ns(e){return Math.max(1,Math.min(100,e??50))}function Mn(e){let t=e.trim().replace(/^\/+|\/+$/g,"");if(!/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(t))throw Error("Repository must use owner/name format");return t}function nc(e){let t=new URL(e);if(t.username||t.password||t.protocol!=="https:")throw Error("Webhook endpoint must be credential-free HTTPS");return t.href}function ah(e){return Buffer.from(e).toString("base64url")}function Sk(e,t){if(!e.appId||!e.privateKey)throw Error("GitHub App credentials require appId and privateKey");let r=ah(JSON.stringify({alg:"RS256",typ:"JWT"})),o=Math.floor(t.getTime()/1000)-30,m=ah(JSON.stringify({iat:o,exp:o+540,iss:e.appId})),h=bk("RSA-SHA256");return h.update(`${r}.${m}`),h.end(),`${r}.${m}.${h.sign(e.privateKey).toString("base64url")}`}class ic{connection;credential;capabilities=ch;fetchFn;timeoutMs;nowFn;constructor(e,t,r={}){this.connection=e;this.credential=t;this.fetchFn=r.fetch??((o,m)=>globalThis.fetch(o,m)),this.timeoutMs=Math.max(100,r.timeoutMs??20000),this.nowFn=r.now??(()=>new Date)}async request(e,t={}){let r=new URL(`${this.apiRoot().replace(/\/$/,"")}/`),o=new URL(e.replace(/^\//,""),r);if(o.origin!==r.origin)throw Error("Provider pagination cannot leave the configured host");let m=new AbortController,h=setTimeout(()=>m.abort(),this.timeoutMs);try{let I=await this.fetchFn(o,{...t,redirect:"error",signal:m.signal,headers:{Accept:"application/json","User-Agent":"ts-cloud-source/1",...await this.authorization(),...t.headers}});if(!I.ok){let R=Number(I.headers.get("retry-after")??""),A=I.headers.get("x-request-id")??I.headers.get("x-github-request-id");throw new qt(`Source provider request failed (${I.status})${A?` [request ${A}]`:""}`,I.status,Number.isFinite(R)?R:void 0)}if(I.status===204)return{data:void 0,headers:I.headers};return{data:await I.json(),headers:I.headers}}catch(I){if(I instanceof qt)throw I;if(m.signal.aborted)throw new qt(`Source provider request exceeded ${this.timeoutMs}ms`);throw new qt(I instanceof Error?I.message.replace(/(?:token|password|secret)=[^\s&]+/gi,"$1=[REDACTED]"):"Source provider request failed")}finally{clearTimeout(h)}}async deleteWebhook(e,t){await this.request(this.webhookPath(e,t),{method:"DELETE"})}}class Ud extends ic{provider="github";installationToken;apiRoot(){return this.connection.host==="https://github.com"?"https://api.github.com":`${this.connection.host}/api/v3`}async authorization(){if(this.credential?.token)return{Authorization:`Bearer ${this.credential.token}`,"X-GitHub-Api-Version":"2022-11-28"};if(!this.credential?.installationId)return{};if(!this.installationToken||this.installationToken.expiresAt<this.nowFn().getTime()+60000){let e=Sk(this.credential,this.nowFn()),t=this.apiRoot().replace(/\/$/,""),r=new AbortController,o=setTimeout(()=>r.abort(),this.timeoutMs);try{let m=await this.fetchFn(`${t}/app/installations/${encodeURIComponent(this.credential.installationId)}/access_tokens`,{method:"POST",signal:r.signal,redirect:"error",headers:{Accept:"application/vnd.github+json",Authorization:`Bearer ${e}`,"X-GitHub-Api-Version":"2022-11-28","User-Agent":"ts-cloud-source/1"}});if(!m.ok)throw new qt(`GitHub App token refresh failed (${m.status})`,m.status);let h=await m.json();this.installationToken={value:h.token,expiresAt:new Date(h.expires_at).getTime()}}finally{clearTimeout(o)}}return{Authorization:`Bearer ${this.installationToken.value}`,"X-GitHub-Api-Version":"2022-11-28"}}async testConnection(){let e=this.credential?.installationId?"/installation/repositories?per_page=1":"/user",{data:t,headers:r}=await this.request(e);return{ok:!0,account:t.login??this.connection.owner,scopes:(r.get("x-oauth-scopes")??this.connection.grantedScopes.join(",")).split(",").map((o)=>o.trim()).filter(Boolean),message:"GitHub connection is healthy"}}async listRepositories(e={}){let t=ns(e.limit),r=rs(e.cursor),o=this.credential?.installationId?`/installation/repositories?per_page=${t}&page=${r}`:`/user/repos?per_page=${t}&page=${r}&sort=updated`,{data:m}=await this.request(o),h=Array.isArray(m)?m:m.repositories;return{repositories:(e.search?h.filter((R)=>R.full_name.toLowerCase().includes(e.search.toLowerCase())):h).map((R)=>({providerRepositoryId:String(R.id),fullName:R.full_name,cloneUrl:R.clone_url,defaultBranch:R.default_branch||"main",visibility:R.visibility??(R.private?"private":"public"),archived:!!R.archived,metadata:{}})),nextCursor:h.length===t?String(r+1):void 0}}async refs(e,t,r={}){let o=ns(r.limit),m=rs(r.cursor),{data:h}=await this.request(`/repos/${Mn(e)}/${t}?per_page=${o}&page=${m}`);return{refs:h.map((I)=>({name:I.name,commitSha:I.commit.sha,protected:I.protected})),nextCursor:h.length===o?String(m+1):void 0}}listBranches(e,t){return this.refs(e,"branches",t)}listTags(e,t){return this.refs(e,"tags",t)}webhookPath(e,t){return`/repos/${Mn(e)}/hooks${t?`/${encodeURIComponent(t)}`:""}`}hook(e){return{providerWebhookId:String(e.id),active:e.active,events:e.events,url:e.config.url??""}}async createWebhook(e,t){let{data:r}=await this.request(this.webhookPath(e),{method:"POST",body:JSON.stringify({name:"web",active:!0,events:t.events,config:{url:nc(t.url),content_type:"json",secret:t.secret,insecure_ssl:"0"}}),headers:{"Content-Type":"application/json"}});return this.hook(r)}async listWebhooks(e){let{data:t}=await this.request(this.webhookPath(e));return t.map((r)=>this.hook(r))}async updateWebhook(e,t,r){let{data:o}=await this.request(this.webhookPath(e,t),{method:"PATCH",body:JSON.stringify({active:!0,events:r.events,config:{url:nc(r.url),content_type:"json",secret:r.secret,insecure_ssl:"0"}}),headers:{"Content-Type":"application/json"}});return this.hook(o)}async setCommitStatus(e,t,r){await this.request(`/repos/${Mn(e)}/statuses/${encodeURIComponent(t)}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({state:r.state,target_url:r.url,description:r.description.slice(0,140),context:r.context??"ts-cloud/preview"})})}}class Qd extends ic{provider="gitlab";apiRoot(){return`${this.connection.host}/api/v4`}async authorization(){return this.credential?.token?{"PRIVATE-TOKEN":this.credential.token}:{}}async testConnection(){let{data:e}=await this.request("/user");return{ok:!0,account:e.username,scopes:this.connection.grantedScopes,message:"GitLab connection is healthy"}}async listRepositories(e={}){let t=ns(e.limit),r=rs(e.cursor),o=e.search?`&search=${encodeURIComponent(e.search)}`:"",{data:m,headers:h}=await this.request(`/projects?membership=true&simple=true&order_by=last_activity_at&per_page=${t}&page=${r}${o}`);return{repositories:m.map((I)=>({providerRepositoryId:String(I.id),fullName:I.path_with_namespace,cloneUrl:I.http_url_to_repo,defaultBranch:I.default_branch??"main",visibility:I.visibility??"unknown",archived:!!I.archived,metadata:{}})),nextCursor:h.get("x-next-page")||void 0}}async refs(e,t,r={}){let o=ns(r.limit),m=rs(r.cursor),{data:h,headers:I}=await this.request(`/projects/${encodeURIComponent(Mn(e))}/repository/${t}?per_page=${o}&page=${m}`);return{refs:h.map((R)=>({name:R.name,commitSha:R.commit.id,protected:R.protected})),nextCursor:I.get("x-next-page")||void 0}}listBranches(e,t){return this.refs(e,"branches",t)}listTags(e,t){return this.refs(e,"tags",t)}webhookPath(e,t){return`/projects/${encodeURIComponent(Mn(e))}/hooks${t?`/${encodeURIComponent(t)}`:""}`}hook(e){return{providerWebhookId:String(e.id),active:!0,events:[...e.push_events?["push"]:[],...e.merge_requests_events?["pull_request"]:[]],url:e.url}}body(e){return JSON.stringify({url:nc(e.url),token:e.secret,enable_ssl_verification:!0,push_events:e.events.includes("push"),merge_requests_events:e.events.includes("pull_request")})}async createWebhook(e,t){let{data:r}=await this.request(this.webhookPath(e),{method:"POST",body:this.body(t),headers:{"Content-Type":"application/json"}});return this.hook(r)}async listWebhooks(e){let{data:t}=await this.request(this.webhookPath(e));return t.map((r)=>this.hook(r))}async updateWebhook(e,t,r){let{data:o}=await this.request(this.webhookPath(e,t),{method:"PUT",body:this.body(r),headers:{"Content-Type":"application/json"}});return this.hook(o)}async setCommitStatus(e,t,r){let o=r.state==="failure"||r.state==="error"?"failed":r.state;await this.request(`/projects/${encodeURIComponent(Mn(e))}/statuses/${encodeURIComponent(t)}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({state:o,target_url:r.url,description:r.description.slice(0,255),name:r.context??"ts-cloud/preview"})})}}class Hd extends ic{provider="bitbucket";apiRoot(){return this.connection.host==="https://bitbucket.org"?"https://api.bitbucket.org/2.0":`${this.connection.host}/2.0`}async authorization(){return this.credential?.token?{Authorization:`Bearer ${this.credential.token}`}:{}}async testConnection(){let{data:e}=await this.request("/user");return{ok:!0,account:e.username??e.display_name,scopes:this.connection.grantedScopes,message:"Bitbucket connection is healthy"}}async listRepositories(e={}){if(!this.connection.owner)throw Error("Bitbucket repository discovery requires an owner/workspace");let t=rs(e.cursor),r=ns(e.limit),o=e.search?`&q=name~%22${encodeURIComponent(e.search)}%22`:"",{data:m}=await this.request(`/repositories/${encodeURIComponent(this.connection.owner)}?pagelen=${r}&page=${t}${o}`);return{repositories:m.values.map((h)=>({providerRepositoryId:h.uuid,fullName:h.full_name,cloneUrl:h.links.clone.find((I)=>I.name==="https")?.href??h.links.clone[0]?.href??"",defaultBranch:h.mainbranch?.name??"main",visibility:h.is_private?"private":"public",archived:!1,metadata:{}})),nextCursor:m.next?String(t+1):void 0}}async refs(e,t,r={}){let o=rs(r.cursor),m=ns(r.limit),{data:h}=await this.request(`/repositories/${Mn(e)}/refs/${t}?pagelen=${m}&page=${o}`);return{refs:h.values.map((I)=>({name:I.name,commitSha:I.target.hash})),nextCursor:h.next?String(o+1):void 0}}listBranches(e,t){return this.refs(e,"branches",t)}listTags(e,t){return this.refs(e,"tags",t)}webhookPath(e,t){return`/repositories/${Mn(e)}/hooks${t?`/${encodeURIComponent(t)}`:""}`}hook(e){return{providerWebhookId:e.uuid,active:e.active,events:e.events.map((t)=>t.replace("repo:push","push").replace("pullrequest:","pull_request:")),url:e.url}}body(e){return JSON.stringify({description:"ts-cloud deploy",url:nc(e.url),active:!0,events:e.events.map((t)=>t==="push"?"repo:push":t.replace("pull_request:","pullrequest:"))})}async createWebhook(e,t){let{data:r}=await this.request(this.webhookPath(e),{method:"POST",body:this.body(t),headers:{"Content-Type":"application/json"}});return this.hook(r)}async listWebhooks(e){let{data:t}=await this.request(this.webhookPath(e));return t.values.map((r)=>this.hook(r))}async updateWebhook(e,t,r){let{data:o}=await this.request(this.webhookPath(e,t),{method:"PUT",body:this.body(r),headers:{"Content-Type":"application/json"}});return this.hook(o)}async setCommitStatus(e,t,r){let o=r.state==="success"?"SUCCESSFUL":r.state==="pending"?"INPROGRESS":"FAILED";await this.request(`/repositories/${Mn(e)}/commit/${encodeURIComponent(t)}/statuses/build/ts-cloud-preview`,{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify({state:o,key:"ts-cloud-preview",name:r.context??"ts-cloud/preview",url:r.url,description:r.description.slice(0,255)})})}}class Gd extends ic{provider="gitea";capabilities={...ch,tokenRefresh:!1};apiRoot(){return`${this.connection.host}/api/v1`}async authorization(){return this.credential?.token?{Authorization:`token ${this.credential.token}`}:{}}async testConnection(){let{data:e}=await this.request("/user");return{ok:!0,account:e.login,scopes:this.connection.grantedScopes,message:"Gitea connection is healthy"}}async listRepositories(e={}){let t=rs(e.cursor),r=ns(e.limit),o=e.search?`/repos/search?q=${encodeURIComponent(e.search)}&limit=${r}&page=${t}`:`/user/repos?limit=${r}&page=${t}`,{data:m,headers:h}=await this.request(o),I=Array.isArray(m)?m:m.data;return{repositories:I.map((R)=>({providerRepositoryId:String(R.id),fullName:R.full_name,cloneUrl:R.clone_url,defaultBranch:R.default_branch??"main",visibility:R.private?"private":"public",archived:!!R.archived,metadata:{}})),nextCursor:h.get("x-hasmore")==="true"||I.length===r?String(t+1):void 0}}async refs(e,t,r={}){let o=rs(r.cursor),m=ns(r.limit),{data:h}=await this.request(`/repos/${Mn(e)}/${t}?limit=${m}&page=${o}`);return{refs:h.map((I)=>({name:I.name,commitSha:I.commit.id})),nextCursor:h.length===m?String(o+1):void 0}}listBranches(e,t){return this.refs(e,"branches",t)}listTags(e,t){return this.refs(e,"tags",t)}webhookPath(e,t){return`/repos/${Mn(e)}/hooks${t?`/${encodeURIComponent(t)}`:""}`}hook(e){return{providerWebhookId:String(e.id),active:e.active,events:e.events??[],url:e.config.url??""}}body(e){return JSON.stringify({type:"gitea",active:!0,events:e.events,config:{url:nc(e.url),content_type:"json",secret:e.secret,insecure_ssl:"0"}})}async createWebhook(e,t){let{data:r}=await this.request(this.webhookPath(e),{method:"POST",body:this.body(t),headers:{"Content-Type":"application/json"}});return this.hook(r)}async listWebhooks(e){let{data:t}=await this.request(this.webhookPath(e));return t.map((r)=>this.hook(r))}async updateWebhook(e,t,r){let{data:o}=await this.request(this.webhookPath(e,t),{method:"PATCH",body:this.body(r),headers:{"Content-Type":"application/json"}});return this.hook(o)}async setCommitStatus(e,t,r){await this.request(`/repos/${Mn(e)}/statuses/${encodeURIComponent(t)}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({state:r.state,target_url:r.url,description:r.description.slice(0,140),context:r.context??"ts-cloud/preview"})})}}function qe(e,t,r={}){switch(e.provider){case"github":return new Ud(e,t,r);case"gitlab":return new Qd(e,t,r);case"bitbucket":return new Hd(e,t,r);case"gitea":return new Gd(e,t,r);default:throw Error(`${e.provider} uses bounded Git discovery instead of a hosted provider API`)}}import{createHmac as wk,timingSafeEqual as kk}from"node:crypto";function hn(e,t){if(e instanceof Headers)return e.get(t)??void 0;return Object.entries(e).find(([o])=>o.toLowerCase()===t.toLowerCase())?.[1]}function dh(e,t){let r=Buffer.from(e),o=Buffer.from(t);return r.length===o.length&&kk(r,o)}function Ik(e,t){return wk("sha256",e).update(t).digest("hex")}function Ek(e,t,r,o,m){if(e==="gitlab")return dh(hn(r,"x-gitlab-token")??"",t);let h=e==="github"?hn(r,"x-hub-signature-256"):e==="gitea"?hn(r,"x-gitea-signature"):hn(r,"x-hub-signature")??hn(r,"x-ts-cloud-signature");if(!h)return!1;if(e==="generic_https"||e==="generic_ssh"){let I=Number(hn(r,"x-ts-cloud-timestamp"));if(!Number.isFinite(I)||Math.abs(m.getTime()-I*1000)>300000)return!1}return dh(h.replace(/^sha256=/,""),Ik(t,o))}function Rk(e,t){return{github:["x-github-delivery"],gitlab:["x-gitlab-event-uuid","x-request-id"],bitbucket:["x-request-uuid"],gitea:["x-gitea-delivery"],generic_https:["x-ts-cloud-delivery"],generic_ssh:["x-ts-cloud-delivery"]}[e].map((o)=>hn(t,o)?.trim()).find(Boolean)}function is(e){let t=String(e??"").trim().replace(/^\/+|\/+$/g,"");return/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(t)?t:""}function fp(e){return[...new Set(e.flatMap((t)=>{if(!t||typeof t!=="object")return[];let r=t;return[...r.added??[],...r.modified??[],...r.removed??[]].map(String)}).filter((t)=>t&&!t.includes("\x00")).slice(0,500))]}function Jg(e,t,r){let o=e,m=t,h=r;if(o==="github"){let I=hn(m,"x-github-event"),R=is(h.repository?.full_name);if(I==="push")return{event:"push",repository:R,ref:String(h.ref??""),branch:String(h.ref??"").replace(/^refs\/heads\//,""),tag:String(h.ref??"").startsWith("refs/tags/")?String(h.ref).replace(/^refs\/tags\//,""):void 0,commitSha:String(h.after??h.head_commit?.id??""),changedPaths:fp(h.commits??[]),deleted:h.deleted===!0||/^0{40,64}$/.test(String(h.after??""))};if(I==="pull_request")return{event:"pull_request",action:String(h.action??""),repository:R,branch:String(h.pull_request?.head?.ref??""),commitSha:String(h.pull_request?.head?.sha??""),changedPaths:[],pullRequestNumber:Number(h.number)||void 0,fork:is(h.pull_request?.head?.repo?.full_name)!==R}}if(o==="gitlab"){let I=String(h.object_kind??"").toLowerCase(),R=is(h.project?.path_with_namespace);if(I==="push"||I==="tag_push")return{event:"push",repository:R,ref:String(h.ref??""),branch:String(h.ref??"").replace(/^refs\/heads\//,""),tag:I==="tag_push"?String(h.ref??"").replace(/^refs\/tags\//,""):void 0,commitSha:String(h.checkout_sha??h.after??""),changedPaths:fp(h.commits??[]),deleted:/^0{40,64}$/.test(String(h.after??""))};if(I==="merge_request")return{event:"pull_request",action:String(h.object_attributes?.action??h.object_attributes?.state??""),repository:R,branch:String(h.object_attributes?.source_branch??""),commitSha:String(h.object_attributes?.last_commit?.id??""),changedPaths:[],pullRequestNumber:Number(h.object_attributes?.iid)||void 0,fork:h.object_attributes?.source_project_id!==h.object_attributes?.target_project_id}}if(o==="bitbucket"){let I=hn(m,"x-event-key")??"",R=is(h.repository?.full_name);if(I==="repo:push"){let A=h.push?.changes?.[0]??{},E=A.new??A.old??{},x=A.new?.target??A.old?.target;return{event:"push",repository:R,ref:String(E.name??""),branch:E.type==="branch"?String(E.name):void 0,tag:E.type==="tag"?String(E.name):void 0,commitSha:String(x?.hash??""),changedPaths:[],deleted:A.closed===!0||A.new==null}}if(I.startsWith("pullrequest:"))return{event:"pull_request",action:I.replace("pullrequest:",""),repository:R,branch:String(h.pullrequest?.source?.branch?.name??""),commitSha:String(h.pullrequest?.source?.commit?.hash??""),changedPaths:[],pullRequestNumber:Number(h.pullrequest?.id)||void 0,fork:is(h.pullrequest?.source?.repository?.full_name)!==R}}if(o==="gitea"){let I=hn(m,"x-gitea-event"),R=is(h.repository?.full_name);if(I==="push")return{event:"push",repository:R,ref:String(h.ref??""),branch:String(h.ref??"").replace(/^refs\/heads\//,""),tag:String(h.ref??"").startsWith("refs/tags/")?String(h.ref).replace(/^refs\/tags\//,""):void 0,commitSha:String(h.after??""),changedPaths:fp(h.commits??[]),deleted:h.deleted===!0||/^0{40,64}$/.test(String(h.after??""))};if(I==="pull_request")return{event:"pull_request",action:String(h.action??""),repository:R,branch:String(h.pull_request?.head?.ref??""),commitSha:String(h.pull_request?.head?.sha??""),changedPaths:[],pullRequestNumber:Number(h.number)||void 0,fork:is(h.pull_request?.head?.repo?.full_name)!==R}}if(o==="generic_https"||o==="generic_ssh"){if(h.event!=="push"&&h.event!=="pull_request")return;return{event:h.event,action:typeof h.action==="string"?h.action:void 0,repository:is(h.repository),ref:typeof h.ref==="string"?h.ref:void 0,branch:typeof h.branch==="string"?h.branch:void 0,tag:typeof h.tag==="string"?h.tag:void 0,commitSha:String(h.commitSha??""),changedPaths:Array.isArray(h.changedPaths)?h.changedPaths.map(String).slice(0,500):[],pullRequestNumber:Number(h.pullRequestNumber)||void 0,fork:h.fork===!0,deleted:h.deleted===!0}}return}function ba(e,t){let r="^";for(let o=0;o<t.length;o++){let m=t[o];if(m==="*"&&t[o+1]==="*")r+=".*",o++;else if(m==="*")r+="[^/]*";else if(m==="?")r+="[^/]";else r+=m.replace(/[|\\{}()[\]^$+?.]/g,"\\$&")}return new RegExp(`${r}$`).test(e)}function Ak(e,t){if(t.event==="pull_request")return e.pullRequestPreviews;if(!e.autoDeploy)return!1;if(t.tag)return!!e.tagRule&&ba(t.tag,e.tagRule);return ba(t.branch??"",e.branchRule??e.defaultBranch)}function Ck(e,t){if(!t.length)return!0;return t.filter((o)=>!e.excludePaths.some((m)=>ba(o,m))).some((o)=>!e.includePaths.length||e.includePaths.some((m)=>ba(o,m)))}function lh(e,t){return hn(t,e==="github"?"x-github-event":e==="gitlab"?"x-gitlab-event":e==="bitbucket"?"x-event-key":e==="gitea"?"x-gitea-event":"x-ts-cloud-event")??"unknown"}function xs(){return{accepted:!1,duplicate:!1,status:"rejected",operations:[],message:"Webhook unavailable"}}async function qd(e){if(e.rawBody.byteLength>2097152)return xs();let t=e.sources.getWebhookByEndpointToken(e.endpointToken);if(!t||t.status==="disabled")return xs();let r=e.sources.getConnection(t.connectionId);if(!r||r.status==="disconnected"||r.status==="expired")return xs();let o=Rk(r.provider,e.headers);if(!o||o.length>200)return xs();if(!Ek(r.provider,e.sources.getWebhookSecret(t.id),e.headers,e.rawBody,e.now??new Date))return e.sources.recordDelivery({connectionId:r.id,webhookId:t.id,providerDeliveryId:o,event:lh(r.provider,e.headers),signatureStatus:hn(e.headers,"x-hub-signature-256")||hn(e.headers,"x-gitlab-token")||hn(e.headers,"x-hub-signature")||hn(e.headers,"x-gitea-signature")?"invalid":"missing",status:"rejected",error:"Webhook signature verification failed"}),xs();let h;try{h=JSON.parse(new TextDecoder().decode(e.rawBody))}catch{return xs()}let I=Jg(r.provider,e.headers,h);if(!I||!I.repository||!/^[a-f0-9]{7,64}$/i.test(I.commitSha))return e.sources.recordDelivery({connectionId:r.id,webhookId:t.id,providerDeliveryId:o,event:lh(r.provider,e.headers),signatureStatus:"verified",status:"rejected",error:"Unsupported or malformed source event"}),xs();let R=e.sources.recordDelivery({connectionId:r.id,webhookId:t.id,providerDeliveryId:o,event:I.event,action:I.action,commitSha:I.commitSha,signatureStatus:"verified",status:"accepted",payloadSummary:{repository:I.repository,ref:I.ref??null,branch:I.branch??null,tag:I.tag??null,commitSha:I.commitSha,pullRequestNumber:I.pullRequestNumber??null,changedPathCount:I.changedPaths.length}});if(R.duplicate&&R.delivery.status!=="accepted")return{accepted:!0,duplicate:!0,status:"duplicate",operations:[],message:"Delivery already processed"};let A=new Le(e.controlPlane),E=e.sources.listBindings({connectionId:r.id,status:"active"}).filter((z)=>{if(z.repositoryFullName.toLowerCase()!==I.repository.toLowerCase()||!Ck(z,I.changedPaths))return!1;let K=z.resourceId?A.getDefinitionForResource(z.resourceId):void 0,J=I.event==="push"&&!!K?.branchRule&&!!I.branch&&ba(I.branch,K.branchRule);return Ak(z,I)||J});if(!E.length)return e.sources.updateDelivery(R.delivery.id,{status:"ignored"}),{accepted:!0,duplicate:R.duplicate,status:"ignored",operations:[],message:"Verified event did not match an active deployment rule"};let x=new c(e.controlPlane),N=new Set(["closed","merged","declined"]),F=E.flatMap((z)=>{if(I.event==="push"&&z.resourceId&&I.branch){let J=A.getDefinitionForResource(z.resourceId);if(J?.branchRule&&ba(I.branch,J.branchRule)){let re=A.findForBranch(J.id,I.repository,I.branch);if(I.deleted){if(!J.cleanupOnClose||!re||re.status==="destroyed")return[];let fe=x.enqueue({projectId:z.projectId,environmentId:z.environmentId,resourceId:z.resourceId,kind:"preview.destroy",idempotencyKey:`preview:${re.id}:branch-delete:${o}`,correlationId:`preview:${re.id}`,input:{previewId:re.id,reason:"branch_deleted",source:{repository:I.repository,commitSha:I.commitSha,branch:I.branch}},lockKey:`preview:${re.id}`,providerKey:r.provider,maxAttempts:3,retryClasses:["network","provider_throttled","provider_unavailable"],resumePolicy:"fail",cancellationMode:"provider_non_cancellable",retentionDays:90}).operation;return A.transition(re.id,"queued",{operationId:fe.id}),[fe]}let se=A.upsert({definitionId:J.id,sourceProvider:r.provider,repository:I.repository,branch:I.branch,commitSha:I.commitSha});if(!se.changed)return[];let ye=se.created?"preview.create":"preview.update",ne=x.enqueue({projectId:z.projectId,environmentId:z.environmentId,resourceId:z.resourceId,kind:ye,idempotencyKey:`preview:${se.preview.id}:${I.commitSha}`,correlationId:`preview:${se.preview.id}`,input:{previewId:se.preview.id,source:{connectionId:r.id,bindingId:z.id,repository:I.repository,commitSha:I.commitSha,branch:I.branch,monorepoRoot:z.monorepoRoot,submodules:z.submodules,cloneDepth:z.cloneDepth??null}},lockKey:`preview:${se.preview.id}`,providerKey:r.provider,buildSlot:!0,maxAttempts:3,retryClasses:["network","provider_throttled","provider_unavailable"],resumePolicy:"fail",cancellationMode:"provider_non_cancellable",retentionDays:90}).operation;return A.transition(se.preview.id,"queued",{operationId:ne.id}),[ne]}}if(I.event==="pull_request"&&z.resourceId&&I.pullRequestNumber){let J=A.getDefinitionForResource(z.resourceId);if(J){let re=N.has(String(I.action??"").toLowerCase()),se=A.findForPullRequest(J.id,I.repository,I.pullRequestNumber);if(re){if(!J.cleanupOnClose||!se||se.status==="destroyed")return[];let ye=x.enqueue({projectId:z.projectId,environmentId:z.environmentId,resourceId:z.resourceId,kind:"preview.destroy",idempotencyKey:`preview:${se.id}:destroy:${o}`,correlationId:`preview:${se.id}`,input:{previewId:se.id,reason:"pull_request_closed",source:{repository:I.repository,commitSha:I.commitSha,branch:I.branch??null,pullRequestNumber:I.pullRequestNumber}},lockKey:`preview:${se.id}`,providerKey:r.provider,maxAttempts:3,retryClasses:["network","provider_throttled","provider_unavailable"],resumePolicy:"fail",cancellationMode:"provider_non_cancellable",retentionDays:90}).operation;return A.transition(se.id,"queued",{operationId:ye.id}),[ye]}try{let ye=A.upsert({definitionId:J.id,sourceProvider:r.provider,repository:I.repository,branch:I.branch??`pr-${I.pullRequestNumber}`,pullRequestNumber:I.pullRequestNumber,fork:I.fork,commitSha:I.commitSha});if(!ye.changed)return[];let ne=ye.created?"preview.create":"preview.update",fe=x.enqueue({projectId:z.projectId,environmentId:z.environmentId,resourceId:z.resourceId,kind:ne,idempotencyKey:`preview:${ye.preview.id}:${I.commitSha}`,correlationId:`preview:${ye.preview.id}`,input:{previewId:ye.preview.id,source:{connectionId:r.id,bindingId:z.id,repository:I.repository,commitSha:I.commitSha,branch:I.branch??null,pullRequestNumber:I.pullRequestNumber,fork:!!I.fork,monorepoRoot:z.monorepoRoot,submodules:z.submodules,cloneDepth:z.cloneDepth??null},inheritedSecretNames:ye.inheritedSecrets},lockKey:`preview:${ye.preview.id}`,providerKey:r.provider,buildSlot:!0,maxAttempts:3,retryClasses:["network","provider_throttled","provider_unavailable"],resumePolicy:"fail",cancellationMode:"provider_non_cancellable",retentionDays:90}).operation;return A.transition(ye.preview.id,"queued",{operationId:fe.id}),[fe]}catch(ye){return e.controlPlane.appendEvent({projectId:z.projectId,resourceId:z.resourceId,type:"preview.rejected",level:"warning",payload:{repository:I.repository,pullRequestNumber:I.pullRequestNumber,fork:!!I.fork,reason:ye instanceof Error?ye.message:String(ye)}}),[]}}}let K=I.event==="pull_request"?"deploy.preview":"deploy.source";return[x.enqueue({projectId:z.projectId,environmentId:z.environmentId,resourceId:z.resourceId,kind:K,idempotencyKey:`source:${r.id}:${o}:${z.id}:${I.commitSha}`,correlationId:`source:${o}`,input:{source:{connectionId:r.id,bindingId:z.id,repository:I.repository,commitSha:I.commitSha,branch:I.branch??null,tag:I.tag??null,monorepoRoot:z.monorepoRoot,submodules:z.submodules,cloneDepth:z.cloneDepth??null},preview:I.event==="pull_request"?{number:I.pullRequestNumber??null,action:I.action??null}:null},lockKey:z.resourceId?`resource:${z.resourceId}`:`environment:${z.environmentId??z.projectId}`,providerKey:r.provider,buildSlot:!0,maxAttempts:3,retryClasses:["network","provider_throttled","provider_unavailable"],resumePolicy:"fail",cancellationMode:"provider_non_cancellable",retentionDays:I.event==="pull_request"?14:90}).operation]});if(!F.length)return e.sources.updateDelivery(R.delivery.id,{status:"ignored"}),{accepted:!0,duplicate:R.duplicate,status:"ignored",operations:[],message:"Verified event required no preview lifecycle change"};return e.sources.updateDelivery(R.delivery.id,{status:"enqueued",operationId:F[0]?.id}),e.controlPlane.appendEvent({organizationId:r.organizationId,projectId:E[0]?.projectId,operationId:F[0]?.id,type:"source.webhook.enqueued",payload:{webhookId:t.id,deliveryId:R.delivery.id,repository:I.repository,commitSha:I.commitSha,operationIds:F.map((z)=>z.id)}}),{accepted:!0,duplicate:R.duplicate,status:"enqueued",operations:F,message:`${F.length} deployment operation${F.length===1?"":"s"} enqueued`}}function Vt(e,t){if(!t.endpointToken)throw Error("The webhook endpoint token is only available at creation time");let r=new URL(e);if(r.protocol!=="https:"&&!["localhost","127.0.0.1","::1"].includes(r.hostname))throw Error("Webhook base URL must use HTTPS");return new URL(`/api/source/webhooks/${encodeURIComponent(t.endpointToken)}`,r).href}import{chmodSync as uh,mkdtempSync as Pk,rmSync as _k,writeFileSync as ph}from"node:fs";import{tmpdir as Tk}from"node:os";import{join as Uc,resolve as xk}from"node:path";function gh(e,t){let r=e.trim();if(/\r|\n|\0/.test(r))throw Error("Git remote contains control characters");if(r.startsWith("https://")){let m=new URL(r);if(m.username||m.password||m.search||m.hash)throw Error("HTTPS Git remotes cannot contain credentials, queries, or fragments");return m.href}let o=r.startsWith("ssh://")?new URL(r).hostname:r.match(/^git@([A-Za-z0-9.-]+):/)?.[1];if(!o)throw Error("Git remote must use credential-free HTTPS or SSH");if(!t||t.host.toLowerCase()!==o.toLowerCase())throw Error("SSH Git remote requires a deploy key with a pinned key for the same host");return r}function Dk(e,t,r){let o=e.replaceAll(t,"[REMOTE]").slice(0,4000);for(let m of[r?.token,r?.privateKey])if(m)o=o.replaceAll(m,"[REDACTED]");return o}function Ok(e){let t={PATH:process.env.PATH??"/usr/local/bin:/usr/bin:/bin",HOME:"/nonexistent",GIT_TERMINAL_PROMPT:"0",GIT_CONFIG_NOSYSTEM:"1",GIT_OPTIONAL_LOCKS:"0"},r;if(e.credential?.token){let o=e.credential.username??"oauth2";t.GIT_CONFIG_COUNT="1",t.GIT_CONFIG_KEY_0="http.extraHeader",t.GIT_CONFIG_VALUE_0=`Authorization: Basic ${Buffer.from(`${o}:${e.credential.token}`).toString("base64")}`}if(e.deployKey){r=Pk(Uc(Tk(),"ts-cloud-git-transport-"));let o=Uc(r,"key"),m=Uc(r,"known_hosts");ph(o,e.deployKey.privateKey,{mode:384}),ph(m,`${e.deployKey.host} ${e.deployKey.hostKey}
|
|
46
|
+
`,{mode:384}),uh(o,384),uh(m,384),t.GIT_SSH_COMMAND=`ssh -i ${o} -o IdentitiesOnly=yes -o StrictHostKeyChecking=yes -o UserKnownHostsFile=${m} -o GlobalKnownHostsFile=/dev/null -o BatchMode=yes`}return{env:t,cleanup:()=>{if(r)_k(r,{recursive:!0,force:!0})}}}async function Ds(e,t,r){let o=Ok(r),m=Bun.spawn([r.executable??"git",...e],{stdout:"pipe",stderr:"pipe",env:o.env}),h=Math.max(100,r.timeoutMs??120000),I,R=new Promise((A,E)=>{I=setTimeout(()=>{m.kill(),E(Error(`Git operation exceeded ${h}ms`))},h)});try{let A=Promise.all([m.exited,new Response(m.stdout).text(),new Response(m.stderr).text()]),[E,x,N]=await Promise.race([A,R]);if(E!==0)throw Error(Dk(N||`Git exited with ${E}`,t,r.credential));return x}finally{if(I)clearTimeout(I);o.cleanup()}}function mh(e,t){let r=new Map;for(let o of e.split(`
|
|
47
|
+
`)){let m=o.trim().match(/^([a-f0-9]{40,64})\s+(.+)$/i);if(!m||!m[2].startsWith(t)||m[2].endsWith("^{}"))continue;let h=m[2].slice(t.length);r.set(h,{name:h,commitSha:m[1]})}return[...r.values()].sort((o,m)=>o.name.localeCompare(m.name))}async function Ao(e,t={}){let r=gh(e,t.deployKey),o=await Ds(["ls-remote","--heads","--tags","--",r],r,t);return{branches:mh(o,"refs/heads/"),tags:mh(o,"refs/tags/")}}async function yo(e,t={}){let r=gh(e.remote,t.deployKey),o=xk(e.destination),m=e.ref?.trim()||e.binding.defaultBranch;if(!/^[A-Za-z0-9._/-]{1,255}$/.test(m)||m.startsWith("-")||m.includes(".."))throw Error("Git ref contains unsupported characters");let h=["clone","--no-tags","--single-branch","--branch",m],I=[...new Set(e.sparsePaths??[])];if(I.some((A)=>!A||A.startsWith("/")||A.split("/").includes("..")||/[\0\r\n]/.test(A)))throw Error("Sparse checkout paths must stay inside the repository");if(I.length)h.push("--filter=blob:none","--no-checkout");if(e.binding.cloneDepth)h.push("--depth",String(e.binding.cloneDepth));if(e.binding.submodules)h.push("--recurse-submodules","--shallow-submodules");if(h.push("--",r,o),await Ds(h,r,t),I.length)await Ds(["-C",o,"sparse-checkout","set","--cone","--",...I],r,t),await Ds(["-C",o,"checkout","--force"],r,t);if(e.commitSha){if(!/^[a-f0-9]{40,64}$/i.test(e.commitSha))throw Error("Expected source commit must be an immutable 40-64 character SHA");await Ds(["-C",o,"fetch","--no-tags","--depth","1","origin",e.commitSha],r,t),await Ds(["-C",o,"checkout","--detach","--force",e.commitSha],r,t)}let R=(await Ds(["-C",o,"rev-parse","HEAD"],r,t)).trim();if(!/^[a-f0-9]{40,64}$/i.test(R))throw Error("Git clone did not produce a valid commit SHA");if(e.commitSha&&R.toLowerCase()!==e.commitSha.toLowerCase())throw Error(`Git checkout resolved ${R} instead of the requested immutable commit`);return{directory:e.binding.monorepoRoot==="."?o:Uc(o,e.binding.monorepoRoot),commitSha:R}}function oc(e){return["github","gitlab","bitbucket","gitea"].includes(e)}async function Io(e,t,r){let o=e.getConnection(t);if(!o||["disconnected","expired"].includes(o.status))throw Error("Active source connection was not found");try{if(oc(o.provider)){let I=await qe(o,e.getCredential(o.id)).testConnection();return e.updateHealth(o.id,{status:I.ok?"healthy":"degraded",message:I.message,tested:!0,grantedScopes:I.scopes})}let m=r?e.getRepository(r):e.listRepositories(o.id,void 0,1)[0];if(!m)return e.updateHealth(o.id,{status:"degraded",message:"Add a repository URL before testing this generic connection.",tested:!0});let h=o.provider==="generic_ssh"?e.listBindings({connectionId:o.id}).map((I)=>I.deployKeyId).filter(Boolean).map((I)=>e.getDeployKey(I)).find(Boolean):void 0;return await Ao(m.cloneUrl,{credential:e.getCredential(o.id),deployKey:h?{...h,privateKey:e.getDeployPrivateKey(h.id)}:void 0}),e.updateHealth(o.id,{status:"healthy",message:"Git repository access is healthy.",tested:!0})}catch(m){return e.updateHealth(o.id,{status:"degraded",message:m instanceof Error?m.message:"Connection test failed",tested:!0})}}async function vi(e,t,r={}){let o=e.getConnection(t);if(!o||!oc(o.provider)||["disconnected","expired"].includes(o.status))throw Error("A hosted active source connection is required for repository discovery");let m=qe(o,e.getCredential(o.id)),h=[],I;for(let R=0;R<Math.min(20,Math.max(1,r.maxPages??10));R++){let A=await m.listRepositories({cursor:I,search:r.search,limit:100});for(let E of A.repositories)h.push(e.upsertRepository({connectionId:o.id,...E}));if(I=A.nextCursor,!I)break}return e.updateHealth(o.id,{status:"healthy",message:`${h.length} repositories synchronized.`,synced:!0}),h}async function Ti(e,t){let r=e.getConnection(t.connectionId);if(!r||["disconnected","expired"].includes(r.status))throw Error("Active source connection was not found");if(oc(r.provider)){let R=qe(r,e.getCredential(r.id));return t.type==="tags"?R.listTags(t.repository,t):R.listBranches(t.repository,t)}let o=t.repositoryId?e.getRepository(t.repositoryId):e.listRepositories(r.id).find((R)=>R.fullName===t.repository);if(!o||o.connectionId!==r.id)throw Error("Generic repository metadata was not found");let m=t.deployKeyId?e.getDeployKey(t.deployKeyId):void 0;if(m&&m.connectionId!==r.id)throw Error("Deploy key does not belong to this connection");let h=await Ao(o.cloneUrl,{credential:e.getCredential(r.id),deployKey:m?{...m,privateKey:e.getDeployPrivateKey(m.id)}:void 0});return{refs:(t.type==="tags"?h.tags:h.branches).slice(0,Math.min(500,t.limit??100))}}async function Mr(e,t,r){let o=e.getWebhook(t),m=o?e.getConnection(o.connectionId):void 0;if(!o||!m||["disconnected","expired"].includes(m.status))throw Error("Active source webhook was not found");let h=Vt(r,{...o,endpointToken:e.getWebhookEndpointToken(o.id)});if(!oc(m.provider))return e.updateWebhookState(o.id,{status:"healthy",healthMessage:"Manual webhook endpoint is ready.",reconciled:!0});try{let I=qe(m,e.getCredential(m.id)),R=e.getWebhookSecret(o.id),A=o.providerWebhookId?await I.updateWebhook(o.repositoryFullName,o.providerWebhookId,{url:h,secret:R,events:o.events}):await I.createWebhook(o.repositoryFullName,{url:h,secret:R,events:o.events});return e.updateWebhookState(o.id,{providerWebhookId:A.providerWebhookId,status:A.active?"healthy":"degraded",healthMessage:A.active?"Provider webhook is synchronized.":"Provider webhook exists but is inactive.",reconciled:!0})}catch(I){return e.updateWebhookState(o.id,{status:"degraded",healthMessage:I instanceof Error?I.message:"Webhook reconciliation failed",reconciled:!0})}}async function Vd(e,t){let r=e.getWebhook(t),o=r?e.getConnection(r.connectionId):void 0;if(!r||!o)throw Error("Source webhook was not found");if(oc(o.provider)&&r.providerWebhookId&&!["disconnected","expired"].includes(o.status))await qe(o,e.getCredential(o.id)).deleteWebhook(r.repositoryFullName,r.providerWebhookId);return e.updateWebhookState(r.id,{status:"disabled",healthMessage:"Webhook disabled."})}var wi="1.6.0";function So(){let e={type:"object",required:["error"],properties:{error:{type:"object",required:["code","message","requestId"],properties:{code:{type:"string"},message:{type:"string"},requestId:{type:"string",format:"uuid"},details:{}}}}},t=(o)=>({type:"object",required:["data","page","requestId"],properties:{data:{type:"array",items:o},page:{type:"object",required:["hasMore"],properties:{nextCursor:{type:"string"},hasMore:{type:"boolean"}}},requestId:{type:"string",format:"uuid"}}}),r=(o)=>({"200":{description:"A stable cursor page.",content:{"application/json":{schema:t(o)}}},default:{$ref:"#/components/responses/Error"}});return{openapi:"3.1.0",info:{title:"ts-cloud API",version:wi,description:"Additive changes are released within v1. Breaking request or response changes require a new URL version."},servers:[{url:"/"}],security:[{bearerAuth:[]}],paths:{...Fs(),"/api/v1/projects":{get:{operationId:"listProjects",summary:"List accessible projects",parameters:[{$ref:"#/components/parameters/Limit"},{$ref:"#/components/parameters/Cursor"}],responses:r({$ref:"#/components/schemas/Project"})}},"/api/v1/projects/{projectId}/environments":{get:{operationId:"listEnvironments",summary:"List accessible project environments",parameters:[{$ref:"#/components/parameters/ProjectId"},{$ref:"#/components/parameters/Limit"},{$ref:"#/components/parameters/Cursor"}],responses:r({$ref:"#/components/schemas/Environment"})}},"/api/v1/services":{get:{operationId:"listServices",summary:"List accessible services",parameters:[{name:"projectId",in:"query",required:!0,schema:{type:"string"}},{name:"environmentId",in:"query",schema:{type:"string"}},{$ref:"#/components/parameters/Limit"},{$ref:"#/components/parameters/Cursor"}],responses:r({$ref:"#/components/schemas/Service"})}},"/api/v1/source/connections":{get:{operationId:"listSourceConnections",summary:"List safe Git connection metadata",parameters:[{$ref:"#/components/parameters/Limit"},{$ref:"#/components/parameters/Cursor"}],responses:r({$ref:"#/components/schemas/SourceConnection"})},post:{operationId:"createSourceConnection",summary:"Create an encrypted Git connection",requestBody:{required:!0,content:{"application/json":{schema:{type:"object",required:["provider","name","host"],properties:{provider:{type:"string",enum:["github","gitlab","bitbucket","gitea","generic_https","generic_ssh"]},name:{type:"string"},host:{type:"string",format:"uri"},token:{type:"string",writeOnly:!0},repositoryFullName:{type:"string"},repositoryUrl:{type:"string"}}}}}},responses:{"201":{description:"Encrypted connection created."},default:{$ref:"#/components/responses/Error"}}},delete:{operationId:"disconnectSourceConnection",summary:"Preview or safely disconnect a Git connection",requestBody:{required:!0,content:{"application/json":{schema:{type:"object",required:["id"],properties:{id:{type:"string"},preview:{type:"boolean"}}}}}},responses:{"200":{description:"Impact preview or disconnected connection."},default:{$ref:"#/components/responses/Error"}}}},"/api/v1/source/repositories":{get:{operationId:"listSourceRepositories",summary:"List or synchronize granted repositories",parameters:[{name:"connectionId",in:"query",required:!0,schema:{type:"string"}},{name:"sync",in:"query",schema:{type:"boolean"}},{name:"search",in:"query",schema:{type:"string"}},{$ref:"#/components/parameters/Limit"},{$ref:"#/components/parameters/Cursor"}],responses:r({$ref:"#/components/schemas/SourceRepository"})}},"/api/v1/source/refs":{get:{operationId:"listSourceRefs",summary:"List repository branches or tags",parameters:[{name:"connectionId",in:"query",required:!0,schema:{type:"string"}},{name:"repository",in:"query",required:!0,schema:{type:"string"}},{name:"repositoryId",in:"query",schema:{type:"string"}},{name:"type",in:"query",schema:{type:"string",enum:["branches","tags"]}}],responses:{"200":{description:"Provider reference page."},default:{$ref:"#/components/responses/Error"}}}},"/api/v1/source/bindings":{post:{operationId:"createSourceBinding",summary:"Bind a repository to an application",requestBody:{required:!0,content:{"application/json":{schema:{$ref:"#/components/schemas/SourceBindingRequest"}}}},responses:{"201":{description:"Source binding created."},default:{$ref:"#/components/responses/Error"}}}},"/api/v1/source/webhooks":{post:{operationId:"createSourceWebhook",summary:"Create and optionally reconcile a signed webhook",requestBody:{required:!0,content:{"application/json":{schema:{type:"object",required:["connectionId","repositoryFullName","baseUrl"],properties:{connectionId:{type:"string"},repositoryId:{type:"string"},repositoryFullName:{type:"string"},baseUrl:{type:"string",format:"uri"},events:{type:"array",items:{type:"string",enum:["push","pull_request"]}},reconcile:{type:"boolean"}}}}}},responses:{"201":{description:"Webhook endpoint created; endpoint is revealed once."},default:{$ref:"#/components/responses/Error"}}}},"/api/v1/application-detections":{post:{operationId:"detectApplication",summary:"Detect bounded application build candidates",requestBody:{required:!0,content:{"application/json":{schema:{type:"object",required:["files"],properties:{files:{type:"array",maxItems:1e4,items:{$ref:"#/components/schemas/DetectionFile"}}}}}}},responses:{"200":{description:"Ranked detection candidates with evidence."},default:{$ref:"#/components/responses/Error"}}}},"/api/v1/application-plans":{post:{operationId:"planApplication",summary:"Validate and normalize an application manifest",requestBody:{required:!0,content:{"application/json":{schema:{type:"object",required:["draft"],properties:{draft:{$ref:"#/components/schemas/ApplicationInput"},suppliedSecretNames:{type:"array",uniqueItems:!0,items:{type:"string"}}}}}}},responses:{"200":{description:"Validated plan, config patch, capabilities, costs, and missing secret names."},default:{$ref:"#/components/responses/Error"}}}},"/api/v1/application-drafts":{get:{operationId:"listApplicationDrafts",summary:"List resumable secret-free drafts",parameters:[{name:"projectId",in:"query",required:!0,schema:{type:"string"}},{$ref:"#/components/parameters/Limit"},{$ref:"#/components/parameters/Cursor"}],responses:r({$ref:"#/components/schemas/ApplicationDraft"})},post:{operationId:"createApplicationDraft",summary:"Create a resumable application draft",requestBody:{required:!0,content:{"application/json":{schema:{$ref:"#/components/schemas/ApplicationDraftCreateRequest"}}}},responses:{"201":{description:"Draft created."},default:{$ref:"#/components/responses/Error"}}},patch:{operationId:"updateApplicationDraft",summary:"Optimistically update a draft",requestBody:{required:!0,content:{"application/json":{schema:{allOf:[{$ref:"#/components/schemas/ApplicationDraftCreateRequest"},{type:"object",required:["id","version","step"],properties:{id:{type:"string"},version:{type:"integer",minimum:1}}}]}}}},responses:{"200":{description:"Updated draft."},default:{$ref:"#/components/responses/Error"}}}},"/api/v1/applications":{post:{operationId:"createApplication",summary:"Confirm and queue an application plan",parameters:[{name:"Idempotency-Key",in:"header",required:!0,schema:{type:"string",minLength:8,maxLength:128}}],requestBody:{required:!0,content:{"application/json":{schema:{type:"object",additionalProperties:!1,required:["draftId","version","confirmEnvironment"],properties:{draftId:{type:"string"},version:{type:"integer",minimum:1},confirmEnvironment:{type:"string",description:"Exact target environment slug."}}}}}},responses:{"202":{description:"Application operation queued; identical requests replay safely."},default:{$ref:"#/components/responses/Error"}}}},"/api/v1/application-artifacts":{post:{operationId:"uploadApplicationArtifact",summary:"Inspect and store a bounded ZIP or TAR source artifact",parameters:[{name:"X-Project-Id",in:"header",required:!0,schema:{type:"string"}},{name:"X-Artifact-Filename",in:"header",required:!0,schema:{type:"string"}}],requestBody:{required:!0,content:{"application/octet-stream":{schema:{type:"string",format:"binary",maxLength:104857600}}}},responses:{"201":{description:"Deduplicated artifact metadata; archive contents are never executed."},"413":{$ref:"#/components/responses/Error"},default:{$ref:"#/components/responses/Error"}}}},"/api/v1/registry-connections":{get:{operationId:"listRegistryConnections",summary:"List safe private-registry metadata",parameters:[{$ref:"#/components/parameters/Limit"},{$ref:"#/components/parameters/Cursor"}],responses:r({$ref:"#/components/schemas/RegistryConnection"})},post:{operationId:"createRegistryConnection",summary:"Create an encrypted private-registry connection",requestBody:{required:!0,content:{"application/json":{schema:{$ref:"#/components/schemas/RegistryConnectionRequest"}}}},responses:{"201":{description:"Encrypted registry connection created."},default:{$ref:"#/components/responses/Error"}}},patch:{operationId:"updateRegistryConnection",summary:"Test access or rotate write-only credentials",requestBody:{required:!0,content:{"application/json":{schema:{allOf:[{$ref:"#/components/schemas/RegistryConnectionRequest"},{type:"object",required:["id","action"],properties:{id:{type:"string"},action:{type:"string",enum:["test","rotate"]},image:{type:"string"}}}]}}}},responses:{"200":{description:"Safe connection health metadata."},default:{$ref:"#/components/responses/Error"}}},delete:{operationId:"disconnectRegistryConnection",summary:"Disconnect and erase registry credentials",requestBody:{required:!0,content:{"application/json":{schema:{type:"object",additionalProperties:!1,required:["id"],properties:{id:{type:"string"}}}}}},responses:{"200":{description:"Credentials erased and connection disconnected."},default:{$ref:"#/components/responses/Error"}}}},"/api/v1/queue":{get:{operationId:"listQueue",summary:"List authorized durable operations and blocking metadata",parameters:[{name:"projectId",in:"query",schema:{type:"string"}},{name:"state",in:"query",schema:{type:"string",enum:["queued","running","succeeded","failed","cancelled","timed_out"]}},{$ref:"#/components/parameters/Limit"},{$ref:"#/components/parameters/Cursor"}],responses:r({$ref:"#/components/schemas/QueueOperation"})}},"/api/v1/preview-definitions":{get:{operationId:"listPreviewDefinitions",summary:"List preview policies",responses:r({type:"object"})},post:{operationId:"createPreviewDefinition",summary:"Configure bounded preview policy",responses:{"200":{description:"Preview definition created."},default:{$ref:"#/components/responses/Error"}}}},"/api/v1/previews":{get:{operationId:"listPreviews",summary:"List authorized persistent previews",responses:r({type:"object"})},post:{operationId:"createPreview",summary:"Create or update one immutable preview identity",responses:{"200":{description:"Preview deployment queued."},default:{$ref:"#/components/responses/Error"}}}},"/api/v1/previews/{previewId}/destroy":{post:{operationId:"destroyPreview",summary:"Confirm tagged preview teardown",responses:{"200":{description:"Teardown queued."},default:{$ref:"#/components/responses/Error"}}}},"/api/v1/previews/{previewId}/extend":{post:{operationId:"extendPreview",summary:"Extend preview TTL",responses:{"200":{description:"Expiry extended."},default:{$ref:"#/components/responses/Error"}}}},"/api/v1/previews/{previewId}/rebuild":{post:{operationId:"rebuildPreview",summary:"Rebuild the exact recorded commit",responses:{"200":{description:"Rebuild queued."},default:{$ref:"#/components/responses/Error"}}}},"/api/v1/previews/cleanup":{post:{operationId:"cleanupPreviews",summary:"Dry-run or queue TTL/keep-count teardown",responses:{"200":{description:"Cleanup plan or operations."},default:{$ref:"#/components/responses/Error"}}}},"/api/v1/compose-templates":{get:{operationId:"listComposeTemplates",summary:"List pinned verified application templates",responses:r({type:"object"})}},"/api/v1/compose-applications":{get:{operationId:"listComposeApplications",summary:"List authorized multi-service applications",responses:r({type:"object"})},post:{operationId:"createComposeApplication",summary:"Import Compose or materialize a template",responses:{"200":{description:"Editable normalized application."},default:{$ref:"#/components/responses/Error"}}}},"/api/v1/compose-applications/preview":{post:{operationId:"previewComposeApplication",summary:"Parse and diagnose Compose without mutation",responses:{"200":{description:"Normalized preview and actionable diagnostics."},default:{$ref:"#/components/responses/Error"}}}},"/api/v1/compose-applications/{applicationId}/services":{get:{operationId:"listComposeServices",summary:"Inspect per-service desired and observed state",responses:{"200":{description:"Service topology and status."},default:{$ref:"#/components/responses/Error"}}}},"/api/v1/compose-applications/{applicationId}/{action}":{post:{operationId:"operateComposeApplication",summary:"Deploy, redeploy, start, stop, scale, or delete through the durable queue",responses:{"200":{description:"Scoped operation queued."},default:{$ref:"#/components/responses/Error"}}}},"/api/v1/release-artifacts":{post:{operationId:"createReleaseArtifact",summary:"Register a verified content-addressed artifact",responses:{"200":{description:"Artifact identity registered."},default:{$ref:"#/components/responses/Error"}}}},"/api/v1/releases":{get:{operationId:"listReleases",summary:"List unified authorized release history",responses:r({type:"object"})},post:{operationId:"createRelease",summary:"Create an immutable release from an existing artifact",responses:{"200":{description:"Release record created."},default:{$ref:"#/components/responses/Error"}}}},"/api/v1/releases/capabilities":{get:{operationId:"getReleaseCapabilities",summary:"Explain supported strategies, capacity, cost, and rollback",responses:{"200":{description:"Provider-aware strategy capabilities."},default:{$ref:"#/components/responses/Error"}}}},"/api/v1/releases/{releaseId}":{get:{operationId:"getRelease",summary:"Inspect provenance, approvals, health, and transitions",responses:{"200":{description:"Release detail."},default:{$ref:"#/components/responses/Error"}}}},"/api/v1/releases/{releaseId}/{action}":{post:{operationId:"operateRelease",summary:"Promote, approve, activate, roll back, report health, or pin",responses:{"200":{description:"Release lifecycle updated."},default:{$ref:"#/components/responses/Error"}}}},"/api/v1/queue/settings":{get:{operationId:"getQueueSettings",summary:"Get effective concurrency limits",responses:{"200":{description:"Queue concurrency settings."},default:{$ref:"#/components/responses/Error"}}},patch:{operationId:"updateQueueSettings",summary:"Confirm and audit production concurrency changes",requestBody:{required:!0,content:{"application/json":{schema:{type:"object",additionalProperties:!1,required:["confirm","concurrency"],properties:{confirm:{const:"update queue limits"},concurrency:{$ref:"#/components/schemas/QueueConcurrency"}}}}}},responses:{"200":{description:"Updated effective limits."},default:{$ref:"#/components/responses/Error"}}}},"/api/v1/queue/history":{delete:{operationId:"clearQueueHistory",summary:"Clear terminal jobs whose retention has elapsed",requestBody:{required:!0,content:{"application/json":{schema:{type:"object",additionalProperties:!1,required:["confirm"],properties:{confirm:{const:"clear completed"},before:{type:"string",format:"date-time"},projectId:{type:"string"}}}}}},responses:{"200":{description:"Eligible history cleared with an audit event."},default:{$ref:"#/components/responses/Error"}}}},"/api/v1/operations/{operationId}/logs":{get:{operationId:"listOperationLogs",summary:"Resume ordered sanitized operation logs from a cursor",parameters:[{$ref:"#/components/parameters/OperationId"},{name:"after",in:"query",schema:{type:"integer",minimum:0}},{$ref:"#/components/parameters/Limit"}],responses:{"200":{description:"Ordered log chunks and next cursor."},default:{$ref:"#/components/responses/Error"}}}},"/api/v1/operations/{operationId}/logs/stream":{get:{operationId:"streamOperationLogs",summary:"Stream authorized operation logs with Last-Event-ID resume",parameters:[{$ref:"#/components/parameters/OperationId"},{name:"Last-Event-ID",in:"header",schema:{type:"integer",minimum:0}}],responses:{"200":{description:"Server-sent log and completion events.",content:{"text/event-stream":{schema:{type:"string"}}}},default:{$ref:"#/components/responses/Error"}}}},"/api/v1/operations/{operationId}/cancel":{post:{operationId:"cancelOperation",summary:"Cancel queued work or request cooperative running cancellation",parameters:[{$ref:"#/components/parameters/OperationId"}],requestBody:{content:{"application/json":{schema:{type:"object"}}}},responses:{"200":{description:"Cancellation state recorded."},default:{$ref:"#/components/responses/Error"}}}},"/api/v1/operations/{operationId}/retry":{post:{operationId:"retryOperation",summary:"Retry an allow-listed failure class within the attempt limit",parameters:[{$ref:"#/components/parameters/OperationId"}],requestBody:{required:!0,content:{"application/json":{schema:{type:"object",additionalProperties:!1,required:["errorClass"],properties:{errorClass:{type:"string"},delayMs:{type:"integer",minimum:0}}}}}},responses:{"200":{description:"Retry queued."},default:{$ref:"#/components/responses/Error"}}}},"/api/v1/deployments":{post:{operationId:"createDeployment",summary:"Queue a deployment",parameters:[{name:"Idempotency-Key",in:"header",required:!0,schema:{type:"string",minLength:8,maxLength:128}}],requestBody:{required:!0,content:{"application/json":{schema:{$ref:"#/components/schemas/DeploymentRequest"}}}},responses:{"202":{description:"Queued operation.",content:{"application/json":{schema:{$ref:"#/components/schemas/OperationResponse"}}}},default:{$ref:"#/components/responses/Error"}}}},"/api/v1/operations":{get:{operationId:"listOperations",summary:"List scoped operations",parameters:[{name:"projectId",in:"query",schema:{type:"string"}},{$ref:"#/components/parameters/Limit"},{$ref:"#/components/parameters/Cursor"}],responses:r({$ref:"#/components/schemas/Operation"})}},"/api/v1/events":{get:{operationId:"listEvents",summary:"List scoped audit events",parameters:[{name:"projectId",in:"query",schema:{type:"string"}},{name:"after",in:"query",schema:{type:"integer",minimum:0}},{$ref:"#/components/parameters/Limit"}],responses:r({$ref:"#/components/schemas/Event"})}},"/api/v1/events/stream":{get:{operationId:"streamEvents",summary:"Stream scoped audit events",responses:{"200":{description:"Authenticated server-sent event stream.",content:{"text/event-stream":{schema:{type:"string"}}}},default:{$ref:"#/components/responses/Error"}}}},"/api/v1/openapi.json":{get:{operationId:"getOpenApi",summary:"Get the OpenAPI contract",security:[],responses:{"200":{description:"OpenAPI 3.1 contract."}}}}},components:{securitySchemes:{bearerAuth:{type:"http",scheme:"bearer",bearerFormat:"tsc_v1"}},parameters:{Limit:{name:"limit",in:"query",schema:{type:"integer",minimum:1,maximum:100,default:50}},Cursor:{name:"cursor",in:"query",schema:{type:"string"}},ProjectId:{name:"projectId",in:"path",required:!0,schema:{type:"string"}},OperationId:{name:"operationId",in:"path",required:!0,schema:{type:"string"}}},responses:{Error:{description:"Stable error envelope.",content:{"application/json":{schema:e}}}},schemas:{Project:{type:"object",required:["id","slug","name"],properties:{id:{type:"string"},slug:{type:"string"},name:{type:"string"},description:{type:"string"}}},Environment:{type:"object",required:["id","projectId","slug","name","kind"],properties:{id:{type:"string"},projectId:{type:"string"},slug:{type:"string"},name:{type:"string"},kind:{type:"string"},region:{type:"string"}}},Service:{type:"object",required:["id","projectId","kind","slug","name"],properties:{id:{type:"string"},projectId:{type:"string"},environmentId:{type:"string"},kind:{type:"string"},slug:{type:"string"},name:{type:"string"},metadata:{type:"object",additionalProperties:!0}}},SourceConnection:{type:"object",required:["id","provider","name","host","status","credentialConfigured"],properties:{id:{type:"string"},provider:{type:"string"},name:{type:"string"},host:{type:"string"},status:{type:"string"},credentialConfigured:{type:"boolean"},credentialFingerprint:{type:"string"},grantedScopes:{type:"array",items:{type:"string"}}}},SourceRepository:{type:"object",required:["id","connectionId","fullName","cloneUrl","defaultBranch"],properties:{id:{type:"string"},connectionId:{type:"string"},fullName:{type:"string"},cloneUrl:{type:"string"},defaultBranch:{type:"string"},visibility:{type:"string"},archived:{type:"boolean"}}},SourceBindingRequest:{type:"object",additionalProperties:!1,required:["projectId","connectionId","repositoryFullName"],properties:{projectId:{type:"string"},environmentId:{type:"string"},resourceId:{type:"string"},connectionId:{type:"string"},repositoryId:{type:"string"},repositoryFullName:{type:"string"},defaultBranch:{type:"string"},branchRule:{type:"string"},tagRule:{type:"string"},monorepoRoot:{type:"string"},includePaths:{type:"array",items:{type:"string"}},excludePaths:{type:"array",items:{type:"string"}},submodules:{type:"boolean"},cloneDepth:{type:"integer",minimum:1},deployKeyId:{type:"string"},autoDeploy:{type:"boolean"},pullRequestPreviews:{type:"boolean"}}},DetectionFile:{type:"object",additionalProperties:!1,required:["path"],properties:{path:{type:"string"},size:{type:"integer",minimum:0},content:{type:"string",maxLength:262144}}},ApplicationInput:{type:"object",additionalProperties:!1,required:["schemaVersion","name","slug","projectId","environmentId","source","build","runtime"],properties:{schemaVersion:{const:1},name:{type:"string"},slug:{type:"string"},projectId:{type:"string"},environmentId:{type:"string"},source:{type:"object",required:["kind"],properties:{kind:{type:"string",enum:["git","local","artifact","image"]}},additionalProperties:!0},build:{type:"object",required:["kind"],properties:{kind:{type:"string",enum:["dockerfile","buildpack","static","server","serverless","prebuilt_image"]},secretNames:{type:"array",uniqueItems:!0,items:{type:"string"}}},additionalProperties:!0},runtime:{type:"object",required:["architecture","target"],properties:{architecture:{type:"string",enum:["x86_64","arm64"]},target:{type:"string",enum:["server","serverless","container"]},port:{type:"integer",minimum:1,maximum:65535},healthCheck:{type:"object",additionalProperties:!0}},additionalProperties:!0},environment:{type:"object",additionalProperties:{oneOf:[{type:"string"},{type:"object",additionalProperties:!1,required:["secretRef"],properties:{secretRef:{type:"string"}}}]}},requiredSecretNames:{type:"array",uniqueItems:!0,items:{type:"string"}},domain:{type:"object",properties:{hostname:{type:"string"},path:{type:"string"},tls:{type:"boolean"}}}}},ApplicationDraftCreateRequest:{type:"object",additionalProperties:!1,required:["projectId","draft"],properties:{projectId:{type:"string"},name:{type:"string"},draft:{$ref:"#/components/schemas/ApplicationInput"},step:{type:"string",enum:["source","build","runtime","environment","domain","review"]},suppliedSecretNames:{type:"array",uniqueItems:!0,description:"Names only; values must use the secrets boundary.",items:{type:"string"}}}},ApplicationDraft:{type:"object",required:["id","projectId","input","step","status","version"],properties:{id:{type:"string"},projectId:{type:"string"},input:{$ref:"#/components/schemas/ApplicationInput"},step:{type:"string"},suppliedSecretNames:{type:"array",items:{type:"string"}},status:{type:"string",enum:["draft","ready","applied"]},version:{type:"integer"},createdAt:{type:"string",format:"date-time"},updatedAt:{type:"string",format:"date-time"}}},RegistryConnectionRequest:{type:"object",properties:{provider:{type:"string",enum:["docker_hub","ghcr","ecr","gcr","generic"]},name:{type:"string"},host:{type:"string"},username:{type:"string",writeOnly:!0},password:{type:"string",writeOnly:!0},token:{type:"string",writeOnly:!0},credentialExpiresAt:{type:"string",format:"date-time"},expiresAt:{type:"string",format:"date-time"}}},RegistryConnection:{type:"object",required:["id","provider","name","host","credentialConfigured","status","version"],properties:{id:{type:"string"},provider:{type:"string"},name:{type:"string"},host:{type:"string"},credentialConfigured:{type:"boolean"},credentialFingerprint:{type:"string"},credentialExpiresAt:{type:"string",format:"date-time"},status:{type:"string",enum:["pending","healthy","degraded","expired","disconnected"]},healthMessage:{type:"string"},lastTestedAt:{type:"string",format:"date-time"},version:{type:"integer"}}},QueueConcurrency:{type:"object",additionalProperties:!1,properties:{project:{type:"integer",minimum:1,maximum:100},environment:{type:"integer",minimum:1,maximum:100},provider:{type:"integer",minimum:1,maximum:100},builds:{type:"integer",minimum:1,maximum:100}}},QueueOperation:{allOf:[{$ref:"#/components/schemas/Operation"},{type:"object",required:["queue"],properties:{queue:{type:"object",required:["maxAttempts","availableAt","timeoutSeconds","retryClasses","resumePolicy","cancellationMode","retentionUntil"],properties:{lockKey:{type:"string"},providerKey:{type:"string"},buildSlot:{type:"boolean"},maxAttempts:{type:"integer"},availableAt:{type:"string",format:"date-time"},timeoutSeconds:{type:"integer"},heartbeatAt:{type:"string",format:"date-time"},currentStep:{type:"string"},blockedReason:{type:"string"},retryClasses:{type:"array",items:{type:"string"}},resumePolicy:{type:"string",enum:["fail","requeue"]},cancellationMode:{type:"string",enum:["cooperative","provider_non_cancellable"]},retentionUntil:{type:"string",format:"date-time"}}},approximatePosition:{type:"object",properties:{ahead:{type:"integer"},precision:{const:"bounded"}}}}}]},DeploymentRequest:{type:"object",additionalProperties:!1,required:["projectId","environmentId"],properties:{projectId:{type:"string"},environmentId:{type:"string"},serviceId:{type:"string"},action:{type:"string",enum:["deploy","rollback"],default:"deploy"},revision:{type:"string"}}},Operation:{type:"object",required:["id","state","kind","correlationId","createdAt"],properties:{id:{type:"string"},state:{type:"string"},kind:{type:"string"},correlationId:{type:"string"},createdAt:{type:"string",format:"date-time"}}},OperationResponse:{type:"object",required:["operation","idempotentReplay","requestId"],properties:{operation:{$ref:"#/components/schemas/Operation"},idempotentReplay:{type:"boolean"},requestId:{type:"string",format:"uuid"}}},Event:{type:"object",required:["id","sequence","type","level","correlationId","createdAt"],properties:{id:{type:"string"},sequence:{type:"integer"},type:{type:"string"},level:{type:"string"},correlationId:{type:"string"},payload:{},createdAt:{type:"string",format:"date-time"}}}}}}}function Nk(e){return Buffer.from(JSON.stringify(e)).toString("base64url")}function jk(e){if(!e)return;try{let t=JSON.parse(Buffer.from(e,"base64url").toString("utf8"));return t&&typeof t==="object"&&!Array.isArray(t)?t:void 0}catch{return}}function yn(e,t,r,o=["createdAt","id"]){let m=Math.min(100,Math.max(1,Number(t.searchParams.get("limit"))||50)),h=jk(t.searchParams.get("cursor"));if(t.searchParams.has("cursor")&&!h)throw new i("invalid_cursor","The pagination cursor is invalid.",400);let I=0;if(h){let x=e.findIndex((N)=>o.every((F)=>N[F]===h[F]));if(x<0)throw new i("invalid_cursor","The pagination cursor is no longer available.",400);I=x+1}let R=e.slice(I,I+m),A=I+m<e.length,E=R.at(-1);return{data:R,page:{hasMore:A,nextCursor:A&&E?Nk(Object.fromEntries(o.map((x)=>[x,E[x]]))):void 0},requestId:r}}function Co(e){let t=new ci(e.controlPlane,e.identities),r=new c(e.controlPlane),o=new Ir(e.controlPlane),m=new Cr(e.controlPlane),h=new dt(e.controlPlane),I=e.spend?.store??new Zn(e.controlPlane,{now:e.now}),R={store:I,service:e.spend?.service??new Lt(I)},A=new Map,E=e.now??(()=>new Date),x=e.rateLimit??120,N=(z,K,J,re={})=>new Response(JSON.stringify(z),{status:K,headers:{"content-type":"application/json; charset=utf-8","cache-control":"no-store","x-request-id":J,"x-api-version":wi,...re}}),F=(z,K,J,re,se,ye)=>N({error:{code:z,message:K,requestId:re,details:se}},J,re,ye);return async(z,K)=>{let J=new URL(z.url);if(!J.pathname.startsWith("/api/v1"))return;let re=crypto.randomUUID();if(J.pathname==="/api/v1/openapi.json"&&z.method==="GET")return N(So(),200,re,{"cache-control":"public, max-age=300"});let se=z.headers.get("authorization")??"",ye=se.startsWith("Bearer ")?se.slice(7).trim():"",ne=ye?e.identities.verifyToken(ye,K):void 0;if(!ne)return F("unauthorized","A valid bearer token is required.",401,re,void 0,{"www-authenticate":'Bearer realm="ts-cloud", error="invalid_token"'});let fe=A.get(ne.token.id),Ce=E().getTime(),_e=!fe||Ce-fe.start>=60000?{start:Ce,count:0}:fe;_e.count+=1,A.set(ne.token.id,_e);let he=Math.max(0,x-_e.count),Ue={"x-ratelimit-limit":String(x),"x-ratelimit-remaining":String(he),"x-ratelimit-reset":String(Math.ceil((_e.start+60000)/1000))};if(_e.count>x)return F("rate_limited","API rate limit exceeded.",429,re,void 0,{...Ue,"retry-after":String(Math.max(1,Math.ceil((_e.start+60000-Ce)/1000)))});try{let ge,Ke=async()=>{let Y=await z.text();if(Y.length>1048576)throw new i("payload_too_large","Request body exceeds 1 MB.",413);try{return JSON.parse(Y)}catch{throw new i("invalid_json","Request body must be valid JSON.",400)}},nt=/^\/api\/v1\/projects\/([^/]+)\/environments$/.exec(J.pathname),Et=/^\/api\/v1\/operations\/([^/]+)\/logs(?:\/(stream))?$/.exec(J.pathname),cr=/^\/api\/v1\/operations\/([^/]+)\/(cancel|retry)$/.exec(J.pathname),at=/^\/api\/v1\/previews\/([^/]+)\/(destroy|extend|rebuild)$/.exec(J.pathname),Ft=/^\/api\/v1\/compose-applications\/([^/]+)\/(deploy|redeploy|start|stop|scale|delete)$/.exec(J.pathname),dr=/^\/api\/v1\/compose-applications\/([^/]+)\/services$/.exec(J.pathname),bt=/^\/api\/v1\/releases\/([^/]+)\/(promote|approve|activate|rollback|health|pin)$/.exec(J.pathname);if(z.method==="GET"&&J.pathname==="/api/v1/projects")ge=yn(t.listProjects(ne),J,re);else if(z.method==="GET"&&nt)ge=yn(t.listEnvironments(ne,decodeURIComponent(nt[1])),J,re);else if(z.method==="GET"&&J.pathname==="/api/v1/services"){let Y=J.searchParams.get("projectId");if(!Y)throw new i("validation_error","projectId is required.",422);ge=yn(t.listServices(ne,Y,J.searchParams.get("environmentId")??void 0),J,re)}else if(z.method==="GET"&&J.pathname==="/api/v1/operations")ge=yn(t.listOperations(ne,J.searchParams.get("projectId")??void 0),J,re);else if(z.method==="GET"&&J.pathname==="/api/v1/previews"){let Y=J.searchParams.get("projectId")??void 0,be=o.previews.listInstances({projectId:Y}).filter((Ee)=>{try{return t.authorize(ne,"deployments:read",{type:"resource",id:Ee.resourceId}),!0}catch{return!1}}).map((Ee)=>({...Ee}));ge=yn(be,J,re)}else if(z.method==="GET"&&J.pathname==="/api/v1/compose-templates")ge=yn(yr().map((Y)=>({...Y})),J,re,["id","version"]);else if(z.method==="GET"&&J.pathname==="/api/v1/releases/capabilities"){let Y=String(J.searchParams.get("kind")??"");ge={capabilities:$r({kind:Y,hasHealthGate:J.searchParams.get("health")==="true",replicas:Number(J.searchParams.get("replicas"))||1}),requestId:re}}else if(z.method==="GET"&&J.pathname==="/api/v1/releases"){let Y=J.searchParams.get("projectId");if(!Y)throw new i("validation_error","projectId is required.",422);t.authorize(ne,"project:read",{type:"project",id:Y});let be=h.releases.list({projectId:Y,environmentId:J.searchParams.get("environmentId")??void 0,resourceId:J.searchParams.get("resourceId")??void 0}).filter((Ee)=>{try{return t.authorize(ne,"deployments:read",{type:"resource",id:Ee.resourceId}),!0}catch{return!1}}).map((Ee)=>({...Ee,artifact:h.releases.getArtifact(Ee.artifactId),transitions:h.releases.transitions(Ee.id),approvals:h.releases.approvals(Ee.id)}));ge=yn(be,J,re)}else if(z.method==="POST"&&J.pathname==="/api/v1/release-artifacts"){let Y=await Ke();t.authorize(ne,"applications:manage",{type:"organization"}),ge={artifact:h.releases.registerArtifact({organizationId:ne.serviceAccount.organizationId,digest:String(Y.digest??""),kind:String(Y.kind??""),uri:String(Y.uri??""),size:Number(Y.size),mediaType:typeof Y.mediaType==="string"?Y.mediaType:void 0,provenance:Y.provenance,attestation:Y.attestation}),requestId:re}}else if(z.method==="POST"&&J.pathname==="/api/v1/releases"){let Y=await Ke(),be=String(Y.resourceId??"");t.authorize(ne,"applications:manage",{type:"resource",id:be}),ge={release:h.releases.create({organizationId:ne.serviceAccount.organizationId,projectId:String(Y.projectId??""),environmentId:String(Y.environmentId??""),resourceId:be,artifactId:String(Y.artifactId??""),kind:String(Y.kind??""),sourceSha:typeof Y.sourceSha==="string"?Y.sourceSha:void 0,config:Y.config??{},manifest:Y.manifest??{},provenance:Y.provenance,strategy:String(Y.strategy??"atomic"),healthGate:Y.healthGate,hooks:Y.hooks,drainSeconds:Number(Y.drainSeconds)||void 0,graceSeconds:Number(Y.graceSeconds)||void 0,automaticRollback:Y.automaticRollback!==!1,actorId:ne.actor.id,trigger:typeof Y.trigger==="string"?Y.trigger:"api",approvalRequired:Y.approvalRequired===!0,replicas:Number(Y.replicas)||void 0}),requestId:re}}else if(z.method==="GET"&&/^\/api\/v1\/releases\/[^/]+$/.test(J.pathname)){let Y=decodeURIComponent(J.pathname.split("/").at(-1)),be=h.releases.get(Y);if(!be)throw new i("not_found","Release was not found.",404);t.authorize(ne,"deployments:read",{type:"resource",id:be.resourceId}),ge={release:be,artifact:h.releases.getArtifact(be.artifactId),transitions:h.releases.transitions(Y),approvals:h.releases.approvals(Y),requestId:re}}else if(z.method==="POST"&&bt){let Y=h.releases.get(decodeURIComponent(bt[1]));if(!Y)throw new i("not_found","Release was not found.",404);let be=await Ke(),Ee=bt[2];if(Ee==="promote"){let Ne=String(be.targetResourceId??"");t.authorize(ne,"deployments:create",{type:"resource",id:Ne}),ge={release:h.releases.promote(Y.id,{targetEnvironmentId:String(be.targetEnvironmentId??""),targetResourceId:Ne,config:be.config??{},strategy:be.strategy,healthGate:be.healthGate,actorId:ne.actor.id,approvalRequired:be.approvalRequired===!0}),requestId:re}}else if(Ee==="approve")t.authorize(ne,"deployments:create",{type:"resource",id:Y.resourceId}),ge={release:h.releases.approve(Y.id,{actorId:ne.actor.id,decision:be.decision==="rejected"?"rejected":"approved",comment:typeof be.comment==="string"?be.comment:void 0}),requestId:re};else if(Ee==="activate")t.authorize(ne,"deployments:create",{type:"resource",id:Y.resourceId}),ge={operation:t.operation(h.enqueueActivation(Y,{actorId:ne.actor.id})),requestId:re};else if(Ee==="rollback")t.authorize(ne,"deployments:create",{type:"resource",id:Y.resourceId}),ge={operation:t.operation(h.enqueueRollback(Y,{actorId:ne.actor.id,targetReleaseId:typeof be.targetReleaseId==="string"?be.targetReleaseId:void 0})),requestId:re};else if(Ee==="health"){t.authorize(ne,"deployments:create",{type:"resource",id:Y.resourceId});let Ne=h.completeHealthGate(Y.id,{healthy:be.healthy===!0,operationId:typeof be.operationId==="string"?be.operationId:void 0,health:be.health,message:typeof be.message==="string"?be.message:void 0});ge={release:Ne.release,rollbackOperation:Ne.rollbackOperation?t.operation(Ne.rollbackOperation):void 0,requestId:re}}else t.authorize(ne,"deployments:create",{type:"resource",id:Y.resourceId}),ge={release:h.releases.pin(Y.id,be.pinned!==!1,typeof be.reason==="string"?be.reason:void 0),requestId:re}}else if(z.method==="GET"&&J.pathname==="/api/v1/compose-applications"){let Y=J.searchParams.get("projectId");if(!Y)throw new i("validation_error","projectId is required.",422);t.authorize(ne,"project:read",{type:"project",id:Y});let be=m.applications.list({projectId:Y,environmentId:J.searchParams.get("environmentId")??void 0}).filter((Ee)=>{try{return t.authorize(ne,"deployments:read",{type:"resource",id:Ee.resourceId}),!0}catch{return!1}}).map((Ee)=>({...Ee,services:m.applications.services(Ee.id)}));ge=yn(be,J,re)}else if(z.method==="POST"&&J.pathname==="/api/v1/compose-applications/preview"){let Y=await Ke(),be=String(Y.projectId??"");t.authorize(ne,"project:read",{type:"project",id:be}),ge={result:m.applications.preview(String(Y.source??""),{name:String(Y.name??""),slug:typeof Y.slug==="string"?Y.slug:void 0,projectId:be,environmentId:String(Y.environmentId??"")}),requestId:re}}else if(z.method==="POST"&&J.pathname==="/api/v1/compose-applications"){let Y=await Ke(),be=String(Y.projectId??"");t.authorize(ne,"applications:manage",{type:"environment",id:String(Y.environmentId??"")});let Ee={name:String(Y.name??""),slug:typeof Y.slug==="string"?Y.slug:void 0,projectId:be,environmentId:String(Y.environmentId??""),createdByActorId:ne.actor.id},Ne=typeof Y.templateId==="string"?m.applications.fromTemplate(Y.templateId,Y.inputs&&typeof Y.inputs==="object"&&!Array.isArray(Y.inputs)?Y.inputs:{},{...Ee,version:typeof Y.templateVersion==="string"?Y.templateVersion:void 0}):m.applications.import(String(Y.source??""),Ee);ge={application:Ne.application,diagnostics:Ne.parsed.diagnostics,requestId:re}}else if(z.method==="GET"&&dr){let Y=m.applications.get(decodeURIComponent(dr[1]));if(!Y)throw new i("not_found","Compose application was not found.",404);t.authorize(ne,"deployments:read",{type:"resource",id:Y.resourceId}),ge={application:Y,services:m.applications.services(Y.id),requestId:re}}else if(z.method==="POST"&&Ft){let Y=m.applications.get(decodeURIComponent(Ft[1]));if(!Y)throw new i("not_found","Compose application was not found.",404);let be=Ft[2],Ee=await Ke();t.authorize(ne,be==="delete"||be==="stop"?"deployments:cancel":"deployments:create",{type:"resource",id:Y.resourceId}),ge={operation:t.operation(m.enqueue(Y,be,{actorId:ne.actor.id,service:typeof Ee.service==="string"?Ee.service:void 0,replicas:Number.isFinite(Number(Ee.replicas))?Number(Ee.replicas):void 0,removeVolumes:Ee.removeVolumes===!0,confirmation:typeof Ee.confirm==="string"?Ee.confirm:void 0})),requestId:re}}else if(z.method==="GET"&&J.pathname==="/api/v1/preview-definitions"){let Y=J.searchParams.get("projectId");if(!Y)throw new i("validation_error","projectId is required.",422);t.authorize(ne,"project:read",{type:"project",id:Y}),ge=yn(o.previews.listDefinitions(Y).map((be)=>({...be})),J,re)}else if(z.method==="POST"&&J.pathname==="/api/v1/preview-definitions"){let Y=await Ke();t.authorize(ne,"config:write",{type:"resource",id:String(Y.resourceId??"")}),ge={definition:o.previews.createDefinition({...Y,projectId:String(Y.projectId??""),resourceId:String(Y.resourceId??""),baseEnvironmentId:String(Y.baseEnvironmentId??""),domainPattern:String(Y.domainPattern??""),createdByActorId:ne.actor.id}),requestId:re}}else if(z.method==="POST"&&J.pathname==="/api/v1/previews"){let Y=await Ke(),be=o.previews.getDefinition(String(Y.definitionId??""));if(!be)throw new i("not_found","Preview definition was not found.",404);t.authorize(ne,"deployments:create",{type:"resource",id:be.resourceId});let Ee=o.previews.upsert({definitionId:be.id,sourceProvider:"api",repository:typeof Y.repository==="string"?Y.repository:"api",branch:String(Y.branch??""),pullRequestNumber:Number(Y.pullRequestNumber)||void 0,fork:Y.fork===!0,commitSha:String(Y.commitSha??""),createdByActorId:ne.actor.id});ge={preview:Ee.preview,operation:t.operation(o.enqueueDeploy(Ee.preview,{created:Ee.created,actorId:ne.actor.id})),requestId:re}}else if(z.method==="POST"&&at){let Y=o.previews.getInstance(decodeURIComponent(at[1]));if(!Y)throw new i("not_found","Preview was not found.",404);let be=await Ke(),Ee=at[2];if(Ee==="destroy"){if(t.authorize(ne,"deployments:cancel",{type:"resource",id:Y.resourceId}),be.confirm!==Y.name)throw new i("confirmation_required",`Type ${Y.name} to confirm preview teardown.`,409);ge={operation:t.operation(o.enqueueDestroy(Y,"api",ne.actor.id)),requestId:re}}else if(Ee==="extend")t.authorize(ne,"deployments:create",{type:"resource",id:Y.resourceId}),ge={preview:o.previews.extend(Y.id,Number(be.hours)),requestId:re};else t.authorize(ne,"deployments:create",{type:"resource",id:Y.resourceId}),ge={operation:t.operation(o.enqueueDeploy(Y,{actorId:ne.actor.id,reason:"manual_rebuild"})),requestId:re}}else if(z.method==="POST"&&J.pathname==="/api/v1/previews/cleanup"){t.authorize(ne,"automation:manage",{type:"organization"});let Y=await Ke();if(!Y.dryRun&&Y.confirm!=="cleanup previews")throw new i("confirmation_required","Type cleanup previews to confirm preview cleanup.",409);ge={...o.cleanup({dryRun:Y.dryRun===!0,maxAgeHours:Number(Y.maxAgeHours)||void 0,keepCount:Number(Y.keepCount)||void 0,actorId:ne.actor.id}),requestId:re}}else if(z.method==="GET"&&J.pathname==="/api/v1/queue"){let Y=J.searchParams.get("projectId")??void 0,be=J.searchParams.get("state"),Ee=r.list({projectId:Y,state:["queued","running","succeeded","failed","cancelled","timed_out"].includes(be)?be:void 0,limit:500}).filter((Ne)=>{let Rt=Ne.operation.resourceId?{type:"resource",id:Ne.operation.resourceId}:Ne.operation.environmentId?{type:"environment",id:Ne.operation.environmentId}:Ne.operation.projectId?{type:"project",id:Ne.operation.projectId}:{type:"organization"};try{return t.authorize(ne,"deployments:read",Rt),!0}catch{return!1}}).map((Ne)=>({...t.operation(Ne.operation),queue:Ne.job,approximatePosition:Ne.approximatePosition}));ge=yn(Ee,J,re)}else if(z.method==="GET"&&J.pathname==="/api/v1/queue/settings")t.authorize(ne,"deployments:read",ne.token.scope),ge={concurrency:r.limits,requestId:re};else if(z.method==="PATCH"&&J.pathname==="/api/v1/queue/settings"){t.authorize(ne,"automation:manage",{type:"organization"});let Y=await Ke();if(Y.confirm!=="update queue limits")throw new i("confirmation_required","Type update queue limits to confirm production concurrency changes.",409);ge={concurrency:r.configureConcurrency(Y.concurrency??{},{organizationId:ne.serviceAccount.organizationId,actorId:ne.actor.id}),requestId:re}}else if(z.method==="DELETE"&&J.pathname==="/api/v1/queue/history"){t.authorize(ne,"automation:manage",{type:"organization"});let Y=await Ke();if(Y.confirm!=="clear completed")throw new i("confirmation_required","Type clear completed to confirm queue retention cleanup.",409);ge={deleted:r.clearCompleted({before:typeof Y.before==="string"?Y.before:void 0,actorId:ne.actor.id,projectId:typeof Y.projectId==="string"?Y.projectId:void 0}),requestId:re}}else if(z.method==="GET"&&Et){let Y=e.controlPlane.getOperation(decodeURIComponent(Et[1]));if(!Y)throw new i("not_found","Operation was not found.",404);let be=Y.resourceId?{type:"resource",id:Y.resourceId}:Y.environmentId?{type:"environment",id:Y.environmentId}:Y.projectId?{type:"project",id:Y.projectId}:{type:"organization"};t.authorize(ne,"deployments:read",be);let Ee=z.headers.get("last-event-id")?.trim(),Ne=Number(Ee||J.searchParams.get("after")||0);if(Et[2]==="stream"){let Ut=new TextEncoder,Fe=Ne,Ct,St=!1,rt=new ReadableStream({start(ln){let Ys=()=>{if(St)return;if(!e.identities.verifyToken(ye,K)){if(St=!0,Ct)clearInterval(Ct);ln.close();return}let ei=r.logs(Y.id,{after:Fe,limit:500});for(let lr of ei)Fe=lr.sequence,ln.enqueue(Ut.encode(`id: ${lr.sequence}
|
|
48
|
+
event: log
|
|
49
|
+
data: ${JSON.stringify(lr)}
|
|
50
|
+
|
|
51
|
+
`));let nr=e.controlPlane.getOperation(Y.id);if(nr&&["succeeded","failed","cancelled","timed_out"].includes(nr.state)&&!r.logs(Y.id,{after:Fe,limit:1}).length){if(St=!0,Ct)clearInterval(Ct);ln.enqueue(Ut.encode(`event: complete
|
|
52
|
+
data: ${JSON.stringify({state:nr.state,cursor:Fe})}
|
|
53
|
+
|
|
54
|
+
`)),ln.close()}};if(Ys(),!St)Ct=setInterval(Ys,500);z.signal.addEventListener("abort",()=>{if(St=!0,Ct)clearInterval(Ct);try{ln.close()}catch{}},{once:!0})},cancel(){if(St=!0,Ct)clearInterval(Ct)}});return new Response(rt,{headers:{"content-type":"text/event-stream","cache-control":"no-store","x-accel-buffering":"no","x-request-id":re,...Ue}})}let Rt=r.logs(Y.id,{after:Ne,limit:Number(J.searchParams.get("limit"))||void 0});ge={data:Rt,cursor:Rt.at(-1)?.sequence??Ne,hasMore:Rt.length>=Math.min(1000,Math.max(1,Number(J.searchParams.get("limit"))||200)),requestId:re}}else if(z.method==="POST"&&cr){let Y=e.controlPlane.getOperation(decodeURIComponent(cr[1]));if(!Y)throw new i("not_found","Operation was not found.",404);let be=Y.resourceId?{type:"resource",id:Y.resourceId}:Y.environmentId?{type:"environment",id:Y.environmentId}:Y.projectId?{type:"project",id:Y.projectId}:{type:"organization"},Ee=await Ke();if(cr[2]==="cancel")t.authorize(ne,"deployments:cancel",be),ge={operation:t.operation(r.requestCancellation(Y.id,ne.actor.id)),requestId:re};else t.authorize(ne,"deployments:create",be),ge={operation:t.operation(r.retry(Y.id,String(Ee.errorClass??"manual"),{delayMs:Number(Ee.delayMs)||0,actorId:ne.actor.id})),requestId:re}}else if(z.method==="GET"&&J.pathname==="/api/v1/source/connections"){if(!e.sources)throw new i("not_found","Source integrations are unavailable.",404);t.authorize(ne,"sources:read",ne.token.scope),ge=yn(e.sources.listConnections(ne.serviceAccount.organizationId),J,re,["createdAt","id"])}else if(z.method==="GET"&&J.pathname==="/api/v1/source/repositories"){if(!e.sources)throw new i("not_found","Source integrations are unavailable.",404);t.authorize(ne,"sources:read",ne.token.scope);let Y=e.sources.getConnection(J.searchParams.get("connectionId")??"");if(!Y||Y.organizationId!==ne.serviceAccount.organizationId)throw new i("not_found","Source connection was not found.",404);if(J.searchParams.get("sync")==="true")t.authorize(ne,"sources:manage",{type:"organization"}),await vi(e.sources,Y.id,{search:J.searchParams.get("search")??void 0});ge=yn(e.sources.listRepositories(Y.id,J.searchParams.get("search")??void 0),J,re,["fullName","id"])}else if(z.method==="GET"&&J.pathname==="/api/v1/source/refs"){if(!e.sources)throw new i("not_found","Source integrations are unavailable.",404);t.authorize(ne,"sources:read",ne.token.scope);let Y=J.searchParams.get("connectionId")??"",be=e.sources.getConnection(Y);if(!be||be.organizationId!==ne.serviceAccount.organizationId)throw new i("not_found","Source connection was not found.",404);ge=await Ti(e.sources,{connectionId:Y,repository:J.searchParams.get("repository")??"",repositoryId:J.searchParams.get("repositoryId")??void 0,type:J.searchParams.get("type")==="tags"?"tags":"branches",cursor:J.searchParams.get("cursor")??void 0,deployKeyId:J.searchParams.get("deployKeyId")??void 0})}else if(z.method==="POST"&&J.pathname==="/api/v1/application-detections"){t.authorize(ne,"applications:manage",ne.token.scope);let Y=await Ke();if(!Array.isArray(Y.files)||Y.files.length>1e4)throw new i("validation_error","files must contain at most 10,000 entries.",422);ge={candidates:va(Y.files.map((be)=>({path:String(be.path??""),size:Number(be.size??0),content:typeof be.content==="string"?be.content.slice(0,262144):void 0}))),requestId:re}}else if(z.method==="POST"&&J.pathname==="/api/v1/application-plans"){t.authorize(ne,"applications:manage",ne.token.scope);let Y=await Ke();ge={plan:Vn(Y.draft,Array.isArray(Y.suppliedSecretNames)?Y.suppliedSecretNames.map(String):[]),requestId:re}}else if(z.method==="GET"&&J.pathname==="/api/v1/application-drafts"){if(!e.applications)throw new i("not_found","Application onboarding is unavailable.",404);let Y=J.searchParams.get("projectId")??"";t.authorize(ne,"applications:read",{type:"project",id:Y}),ge=yn(e.applications.drafts.list(Y),J,re,["updatedAt","id"])}else if(z.method==="POST"&&J.pathname==="/api/v1/application-drafts"){if(!e.applications)throw new i("not_found","Application onboarding is unavailable.",404);let Y=await Ke(),be=String(Y.projectId??Y.draft?.projectId??"");t.authorize(ne,"applications:manage",{type:"project",id:be});let Ee=e.controlPlane.getProject(be);if(!Ee||Ee.organizationId!==ne.serviceAccount.organizationId)throw new i("not_found","Project was not found.",404);let Ne=e.applications.drafts.create({organizationId:Ee.organizationId,projectId:be,name:String(Y.name??Y.draft?.name??"Application draft"),draft:{...Y.draft,projectId:be},step:Y.step,suppliedSecretNames:Array.isArray(Y.suppliedSecretNames)?Y.suppliedSecretNames.map(String):[],actorId:ne.actor.id});return N({draft:Ne,requestId:re},201,re,Ue)}else if(z.method==="PATCH"&&J.pathname==="/api/v1/application-drafts"){if(!e.applications)throw new i("not_found","Application onboarding is unavailable.",404);let Y=await Ke(),be=e.applications.drafts.get(String(Y.id??""));if(!be)throw new i("not_found","Application draft was not found.",404);t.authorize(ne,"applications:manage",{type:"project",id:be.projectId}),ge={draft:e.applications.drafts.update(be.id,Number(Y.version),{draft:{...Y.draft,projectId:be.projectId},step:Y.step,suppliedSecretNames:Array.isArray(Y.suppliedSecretNames)?Y.suppliedSecretNames.map(String):void 0,actorId:ne.actor.id}),requestId:re}}else if(z.method==="POST"&&J.pathname==="/api/v1/applications"){if(!e.applications)throw new i("not_found","Application onboarding is unavailable.",404);let Y=z.headers.get("idempotency-key")??"";if(!Y)throw new i("idempotency_required","Idempotency-Key is required for application creation.",428);if(!/^[A-Za-z0-9._:-]{8,128}$/.test(Y))throw new i("validation_error","Idempotency-Key must contain 8-128 safe characters.",422);let be=await Ke(),Ee=e.applications.drafts.get(String(be.draftId??""));if(!Ee)throw new i("not_found","Application draft was not found.",404);t.authorize(ne,"applications:manage",{type:"project",id:Ee.projectId});let Ne=$k("sha256").update(`POST
|
|
55
|
+
/api/v1/applications
|
|
56
|
+
${JSON.stringify(be)}`).digest("hex"),Rt=e.identities.getIdempotency(ne.token.id,Y);if(Rt){if(Rt.requestHash!==Ne)throw new i("idempotency_conflict","Idempotency-Key was already used for another request.",409);let Fe=Rt.operationId?e.controlPlane.getOperation(Rt.operationId):void 0,Ct=Fe?.resourceId?e.controlPlane.getResource(Fe.resourceId):void 0;if(!Fe||!Ct)throw new i("idempotency_unavailable","The original application operation is no longer available.",409);return N({resource:Ct,operation:t.operation(Fe),plan:Vn(Ee.input,Ee.suppliedSecretNames),idempotentReplay:!0,requestId:re},202,re,{...Ue,"idempotent-replayed":"true"})}let Ut=Fc({controlPlane:e.controlPlane,drafts:e.applications.drafts,draftId:Ee.id,expectedVersion:Number(be.version),confirmEnvironment:String(be.confirmEnvironment??""),actorId:ne.actor.id});return e.identities.saveIdempotency({tokenId:ne.token.id,key:Y,requestHash:Ne,operationId:Ut.operation.id,responseStatus:202,responseBody:{draftId:Ee.id}}),N({resource:Ut.resource,operation:t.operation(Ut.operation),plan:Ut.plan,idempotentReplay:!1,requestId:re},202,re,Ue)}else if(z.method==="GET"&&J.pathname==="/api/v1/registry-connections"){if(!e.applications)throw new i("not_found","Application onboarding is unavailable.",404);t.authorize(ne,"applications:read",ne.token.scope),ge=yn(e.applications.registries.list(ne.serviceAccount.organizationId),J,re,["createdAt","id"])}else if(z.method==="POST"&&J.pathname==="/api/v1/registry-connections"){if(!e.applications)throw new i("not_found","Application onboarding is unavailable.",404);t.authorize(ne,"applications:manage",{type:"organization"});let Y=await Ke(),be=e.applications.registries.create({organizationId:ne.serviceAccount.organizationId,provider:Y.provider,name:String(Y.name??"Container registry"),host:String(Y.host??""),credential:Y.token||Y.password?{username:typeof Y.username==="string"?Y.username:void 0,password:typeof Y.password==="string"?Y.password:void 0,token:typeof Y.token==="string"?Y.token:void 0}:void 0,credentialExpiresAt:typeof Y.credentialExpiresAt==="string"?Y.credentialExpiresAt:void 0,actorId:ne.actor.id});return N({registry:be,requestId:re},201,re,Ue)}else if(z.method==="PATCH"&&J.pathname==="/api/v1/registry-connections"){if(!e.applications)throw new i("not_found","Application onboarding is unavailable.",404);t.authorize(ne,"applications:manage",{type:"organization"});let Y=await Ke(),be=e.applications.registries.get(String(Y.id??""));if(!be||be.organizationId!==ne.serviceAccount.organizationId)throw new i("not_found","Registry connection was not found.",404);if(Y.action==="test")ge={registry:await e.applications.registries.test(be.id,{image:typeof Y.image==="string"?Y.image:void 0}),requestId:re};else if(Y.action==="rotate")ge={registry:e.applications.registries.rotate(be.id,{username:Y.username,password:Y.password,token:Y.token},{expiresAt:Y.expiresAt,actorId:ne.actor.id}),requestId:re};else throw new i("validation_error","action must be test or rotate.",422)}else if(z.method==="DELETE"&&J.pathname==="/api/v1/registry-connections"){if(!e.applications)throw new i("not_found","Application onboarding is unavailable.",404);t.authorize(ne,"applications:manage",{type:"organization"});let Y=await Ke(),be=e.applications.registries.get(String(Y.id??""));if(!be||be.organizationId!==ne.serviceAccount.organizationId)throw new i("not_found","Registry connection was not found.",404);ge={registry:e.applications.registries.disconnect(be.id,ne.actor.id),requestId:re}}else if(z.method==="POST"&&J.pathname==="/api/v1/application-artifacts"){if(!e.applications?.artifacts)throw new i("not_found","Application artifact storage is unavailable.",404);let Y=z.headers.get("x-project-id")??"";t.authorize(ne,"applications:manage",{type:"project",id:Y});let be=e.controlPlane.getProject(Y);if(!be||be.organizationId!==ne.serviceAccount.organizationId)throw new i("not_found","Project was not found.",404);if(Number(z.headers.get("content-length")??0)>104857600)throw new i("payload_too_large","Artifact exceeds 100 MB.",413);let Ne=e.applications.artifacts.create({organizationId:be.organizationId,projectId:Y,filename:z.headers.get("x-artifact-filename")??"application.zip",bytes:new Uint8Array(await z.arrayBuffer()),actorId:ne.actor.id});return N({artifact:Ne,requestId:re},201,re,Ue)}else if(z.method==="POST"&&J.pathname==="/api/v1/source/connections"){if(!e.sources)throw new i("not_found","Source integrations are unavailable.",404);t.authorize(ne,"sources:manage",{type:"organization"});let Y=await Ke(),be=String(Y.provider??"");if(!["github","gitlab","bitbucket","gitea","generic_https","generic_ssh"].includes(be))throw new i("validation_error","A supported provider is required.",422);let Ee=be==="github"&&(Y.privateKey||Y.appId)?{appId:typeof Y.appId==="string"?Y.appId:void 0,installationId:typeof Y.installationId==="string"?Y.installationId:void 0,privateKey:typeof Y.privateKey==="string"?Y.privateKey:void 0}:be!=="generic_ssh"&&Y.token?{token:String(Y.token),username:typeof Y.username==="string"?Y.username:void 0}:void 0,Ne,Rt,Ut;return e.controlPlane.database.transaction(()=>{if(Ne=e.sources.createConnection({organizationId:ne.serviceAccount.organizationId,provider:be,name:String(Y.name??be),host:String(Y.host??""),owner:typeof Y.owner==="string"?Y.owner:void 0,authKind:Y.authKind,credential:Ee,createdByActorId:ne.actor.id}),typeof Y.repositoryUrl==="string"&&typeof Y.repositoryFullName==="string")Rt=e.sources.upsertRepository({connectionId:Ne.id,providerRepositoryId:`manual:${Y.repositoryFullName}`,fullName:Y.repositoryFullName,cloneUrl:Y.repositoryUrl,defaultBranch:String(Y.defaultBranch??"main"),visibility:"unknown",archived:!1,metadata:{source:"api"}});if(be==="generic_ssh")Ut=e.sources.createDeployKey({connectionId:Ne.id,name:String(Y.deployKeyName??"Repository deploy key"),publicKey:String(Y.publicKey??""),privateKey:String(Y.deployPrivateKey??""),host:String(Y.sshHost??""),hostKey:String(Y.hostKey??""),actorId:ne.actor.id})})(),N({connection:Ne,repository:Rt,deployKey:Ut,requestId:re},201,re,Ue)}else if(z.method==="POST"&&J.pathname==="/api/v1/source/bindings"){if(!e.sources)throw new i("not_found","Source integrations are unavailable.",404);let Y=await Ke(),be=String(Y.projectId??"");t.authorize(ne,"sources:manage",{type:"project",id:be});let Ee=e.sources.getConnection(String(Y.connectionId??""));if(!Ee||Ee.organizationId!==ne.serviceAccount.organizationId)throw new i("not_found","Source connection was not found.",404);let Ne=e.sources.createBinding({projectId:be,environmentId:typeof Y.environmentId==="string"?Y.environmentId:void 0,resourceId:typeof Y.resourceId==="string"?Y.resourceId:void 0,connectionId:Ee.id,repositoryId:typeof Y.repositoryId==="string"?Y.repositoryId:void 0,repositoryFullName:String(Y.repositoryFullName??""),defaultBranch:String(Y.defaultBranch??"main"),branchRule:typeof Y.branchRule==="string"?Y.branchRule:void 0,tagRule:typeof Y.tagRule==="string"?Y.tagRule:void 0,monorepoRoot:String(Y.monorepoRoot??"."),includePaths:Array.isArray(Y.includePaths)?Y.includePaths.map(String):[],excludePaths:Array.isArray(Y.excludePaths)?Y.excludePaths.map(String):[],submodules:Y.submodules===!0,cloneDepth:typeof Y.cloneDepth==="number"?Y.cloneDepth:void 0,deployKeyId:typeof Y.deployKeyId==="string"&&Y.deployKeyId?Y.deployKeyId:void 0,autoDeploy:Y.autoDeploy!==!1,pullRequestPreviews:Y.pullRequestPreviews!==!1,actorId:ne.actor.id});return N({binding:Ne,requestId:re},201,re,Ue)}else if(z.method==="POST"&&J.pathname==="/api/v1/source/webhooks"){if(!e.sources)throw new i("not_found","Source integrations are unavailable.",404);t.authorize(ne,"sources:manage",{type:"organization"});let Y=await Ke(),be=e.sources.getConnection(String(Y.connectionId??""));if(!be||be.organizationId!==ne.serviceAccount.organizationId)throw new i("not_found","Source connection was not found.",404);let Ee=typeof Y.baseUrl==="string"?Y.baseUrl:void 0;if(!Ee)throw new i("validation_error","baseUrl is required.",422);let Ne=e.sources.createWebhook({connectionId:be.id,repositoryId:typeof Y.repositoryId==="string"?Y.repositoryId:void 0,repositoryFullName:String(Y.repositoryFullName??""),events:Array.isArray(Y.events)?Y.events.map(String):void 0}),Rt=Vt(Ee,Ne.webhook),Ut=Y.reconcile===!1?Ne.webhook:await Mr(e.sources,Ne.webhook.id,Ee);return N({webhook:Ut,endpoint:Rt,endpointRevealOnce:!0,requestId:re},201,re,Ue)}else if(z.method==="DELETE"&&J.pathname==="/api/v1/source/connections"){if(!e.sources)throw new i("not_found","Source integrations are unavailable.",404);t.authorize(ne,"sources:manage",{type:"organization"});let Y=await Ke(),be=e.sources.getConnection(String(Y.id??""));if(!be||be.organizationId!==ne.serviceAccount.organizationId)throw new i("not_found","Source connection was not found.",404);let Ee=e.sources.listBindings({connectionId:be.id,status:"active"});if(Y.preview===!0)return N({preview:!0,affectedBindings:Ee,requestId:re},200,re,Ue);return N({...e.sources.disconnectConnection(be.id,ne.actor.id),requestId:re},200,re,Ue)}else if(z.method==="GET"&&(J.pathname==="/api/v1/events"||J.pathname==="/api/v1/events/stream")){let Y=t.listEvents(ne,J.searchParams.get("projectId")??void 0,Number(J.searchParams.get("after"))||void 0);if(J.pathname.endsWith("/stream")){let be=new TextEncoder,Ee=Y.reduce((Fe,Ct)=>Math.max(Fe,Number(Ct.sequence)),Number(J.searchParams.get("after"))||0),Ne,Rt=!1,Ut=new ReadableStream({start(Fe){let Ct=(St)=>Fe.enqueue(be.encode(`id: ${St.sequence}
|
|
57
|
+
event: ${St.type}
|
|
58
|
+
data: ${JSON.stringify(St)}
|
|
59
|
+
|
|
60
|
+
`));if(Y.length)Y.forEach(Ct);else Fe.enqueue(be.encode(`: connected
|
|
61
|
+
|
|
62
|
+
`));Ne=setInterval(()=>{if(Rt)return;let St=e.identities.verifyToken(ye,K);if(!St){if(Rt=!0,Ne)clearInterval(Ne);Fe.close();return}try{let rt=t.listEvents(St,J.searchParams.get("projectId")??void 0,Ee);for(let ln of rt)Ct(ln),Ee=Math.max(Ee,Number(ln.sequence));if(!rt.length)Fe.enqueue(be.encode(`: heartbeat ${Date.now()}
|
|
63
|
+
|
|
64
|
+
`))}catch{if(Rt=!0,Ne)clearInterval(Ne);Fe.close()}},1000),z.signal.addEventListener("abort",()=>{if(Rt=!0,Ne)clearInterval(Ne);try{Fe.close()}catch{}},{once:!0})},cancel(){if(Rt=!0,Ne)clearInterval(Ne)}});return new Response(Ut,{status:200,headers:{"content-type":"text/event-stream","cache-control":"no-store",connection:"keep-alive","x-accel-buffering":"no","x-request-id":re,...Ue}})}ge=yn(Y,J,re,["sequence","id"])}else if(z.method==="POST"&&J.pathname==="/api/v1/deployments"){let Y=z.headers.get("idempotency-key")??"";if(!Y)throw new i("idempotency_required","Idempotency-Key is required for deployment requests.",428);let be=await z.text();if(be.length>1048576)throw new i("payload_too_large","Request body exceeds 1 MB.",413);let Ee;try{Ee=JSON.parse(be)}catch{throw new i("invalid_json","Request body must be valid JSON.",400)}if(!Ee||typeof Ee.projectId!=="string"||typeof Ee.environmentId!=="string"||Ee.action!==void 0&&!["deploy","rollback"].includes(Ee.action))throw new i("validation_error","projectId, environmentId, and a valid action are required.",422);let Ne=t.createDeployment(ne,Ee,Y);return N({operation:Ne.operation,idempotentReplay:Ne.replay,requestId:re},202,re,{...Ue,...Ne.replay?{"idempotent-replayed":"true"}:{}})}else{let Y=await Ms({...R,authorization:t,now:E},ne,{method:z.method,url:J,body:Ke});if(Y===void 0)throw new i("not_found","API resource was not found.",404);ge=Y}return N(ge,200,re,Ue)}catch(ge){if(ge instanceof i)return F(ge.code,ge.message,ge.status,re,ge.details,Ue);if(J.pathname.startsWith("/api/v1/source/")||J.pathname.startsWith("/api/v1/application")||J.pathname.startsWith("/api/v1/compose-")||J.pathname==="/api/v1/registry-connections")return F("validation_error",ge instanceof Error?ge.message:"Integration request could not be completed.",422,re,void 0,Ue);return F("internal_error","The API request could not be completed.",500,re,void 0,Ue)}}}import{createCipheriv as Lk,createDecipheriv as Fk,createHash as bh,randomBytes as yi}from"node:crypto";import{createHmac as Mk,timingSafeEqual as Bk}from"node:crypto";var hp="ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";function Si(e){let t=0,r=0,o="";for(let m of e){r=r<<8|m,t+=8;while(t>=5)o+=hp[r>>>t-5&31],t-=5}if(t>0)o+=hp[r<<5-t&31];return o}function Rd(e){let t=e.toUpperCase().replace(/[^A-Z2-7]/g,""),r=0,o=0,m=[];for(let h of t){let I=hp.indexOf(h);if(I<0)throw Error("Invalid base32 secret");if(o=o<<5|I,r+=5,r>=8)m.push(o>>>r-8&255),r-=8}return Buffer.from(m)}function co(e,t,r=6){let o=Buffer.alloc(8);o.writeBigUInt64BE(BigInt(t));let m=Mk("sha1",Rd(e)).update(o).digest(),h=m[m.length-1]&15,I=(m[h]&127)<<24|(m[h+1]&255)<<16|(m[h+2]&255)<<8|m[h+3]&255;return String(I%10**r).padStart(r,"0")}function Vg(e,t=Date.now(),r=30){return co(e,Math.floor(t/1000/r))}function jg(e,t,r=Date.now(),o=1){return Or(e,t,r,o)!==void 0}function Or(e,t,r=Date.now(),o=1){let m=t.replace(/[\s-]/g,"");if(!/^\d{6}$/.test(m))return;let h=Buffer.from(m),I=Math.floor(r/1000/30);for(let R=-o;R<=o;R++){if(I+R<0)continue;let A=Buffer.from(co(e,I+R));if(h.length===A.length&&Bk(h,A))return I+R}return}function lo(e){let t=e.issuer?.trim()||"ts-cloud",r=`${t}:${e.account}`;return`otpauth://totp/${encodeURIComponent(r)}?secret=${encodeURIComponent(e.secret)}&issuer=${encodeURIComponent(t)}&algorithm=SHA1&digits=6&period=30`}var Dd=1800000,He=43200000,Pd=3600000,Nd=300000,kd=600000;function xn(e){return typeof e==="string"?e:void 0}function Sh(e){if(typeof e!=="string")return{};try{return JSON.parse(e)}catch{return{}}}function zk(e){let t=e.trim();if(!/^[a-z0-9][a-z0-9._-]{1,31}$/i.test(t))throw Error("Username must be 2-32 characters: letters, numbers, dot, dash or underscore");return t}function yp(e){if(e===void 0||!e.trim())return;let t=e.trim().toLowerCase();if(!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(t)||t.length>254)throw Error("A valid email address is required");return t}function Uk(e){let t=e.trim().toLowerCase();if(!/^[a-z0-9][a-z0-9-]{1,47}$/.test(t))throw Error("OIDC provider slug must be 2-48 lowercase letters, numbers or dashes");return t}function qk(e){let t=new URL(e.trim());if(t.search||t.hash||t.username||t.password)throw Error("OIDC issuer cannot contain credentials, a query, or a fragment");if(t.protocol!=="https:"&&!(t.protocol==="http:"&&["127.0.0.1","::1","localhost"].includes(t.hostname)))throw Error("OIDC issuer must use HTTPS");return t.href.replace(/\/$/,"")}function Vk(e){let t=[...new Set(e.map((r)=>r.trim().toLowerCase()).filter(Boolean))];if(t.length===0)throw Error("At least one explicit OIDC email domain is required");if(t.some((r)=>!/^[a-z0-9](?:[a-z0-9.-]{0,251}[a-z0-9])?$/.test(r)||!r.includes(".")||r.includes("..")))throw Error("OIDC email domains must be explicit DNS names without wildcards");return t}function Wk(e){let t=[...new Set(["openid",...e??["email","profile"]].map((r)=>r.trim()).filter(Boolean))];if(t.some((r)=>!/^[\x21\x23-\x5B\x5D-\x7E]+$/.test(r)))throw Error("OIDC scopes contain unsupported characters");return t}function Wn(e){return bh("sha256").update(e).digest("hex")}function sc(e){return{id:String(e.id),actorId:String(e.actor_id),username:String(e.username),email:xn(e.email),emailVerifiedAt:xn(e.email_verified_at),passwordHash:String(e.password_hash),credentialVersion:Number(e.credential_version),requiresPasswordUpgrade:Number(e.requires_password_upgrade)===1,disabledAt:xn(e.disabled_at),lastLoginAt:xn(e.last_login_at),createdAt:String(e.created_at),updatedAt:String(e.updated_at)}}function fh(e,t){let r=xn(e.consumed_at),o=String(e.expires_at);return{id:String(e.id),identityId:String(e.identity_id),type:String(e.type),metadata:Sh(e.metadata),expiresAt:o,consumedAt:r,createdAt:String(e.created_at),state:r?"consumed":o<=t?"expired":"pending"}}function vp(e,t){let r=xn(e.revoked_at),o=String(e.idle_expires_at),m=String(e.absolute_expires_at);return{id:String(e.id),identityId:String(e.identity_id),credentialVersion:Number(e.credential_version),authMethod:String(e.auth_method),userAgent:xn(e.user_agent),networkHint:xn(e.network_hint),createdAt:String(e.created_at),lastUsedAt:String(e.last_used_at),idleExpiresAt:o,absoluteExpiresAt:m,recentAuthAt:String(e.recent_auth_at),mfaAt:xn(e.mfa_at),revokedAt:r,state:r?"revoked":o<=t||m<=t?"expired":"active"}}function hh(e){let t=xn(e.verified_at),r=xn(e.disabled_at);return{id:String(e.id),identityId:String(e.identity_id),type:"totp",label:String(e.label),createdAt:String(e.created_at),verifiedAt:t,disabledAt:r,state:r?"disabled":t?"active":"pending"}}function bp(e,t){let r=xn(e.consumed_at),o=String(e.expires_at),m=Number(e.attempts);return{id:String(e.id),identityId:String(e.identity_id),purpose:String(e.purpose),attempts:m,expiresAt:o,consumedAt:r,createdAt:String(e.created_at),state:r?"consumed":m>=5?"locked":o<=t?"expired":"pending"}}function yh(e){let t=Sh(e);return Array.isArray(t)?t.filter((r)=>typeof r==="string"):[]}function qc(e){return{id:String(e.id),organizationId:String(e.organization_id),slug:String(e.slug),name:String(e.name),issuer:String(e.issuer),clientId:String(e.client_id),hasClientSecret:typeof e.client_secret_ciphertext==="string"&&e.client_secret_ciphertext.length>0,scopes:yh(e.scopes),allowedDomains:yh(e.allowed_domains),defaultRole:String(e.default_role),enabled:Number(e.enabled)===1,enforceSso:Number(e.enforce_sso)===1,createdAt:String(e.created_at),updatedAt:String(e.updated_at)}}function vh(e,t){let r=xn(e.consumed_at),o=String(e.expires_at);return{id:String(e.id),providerId:String(e.provider_id),redirectUri:String(e.redirect_uri),returnPath:String(e.return_path),expiresAt:o,consumedAt:r,createdAt:String(e.created_at),state:r?"consumed":o<=t?"expired":"pending"}}function Hk(e){return{id:String(e.id),providerId:String(e.provider_id),identityId:String(e.identity_id),subject:String(e.subject),email:String(e.email),linkedAt:String(e.linked_at),lastLoginAt:String(e.last_login_at)}}class Ci{controlPlane;nowFn;idFn;encryptionKey;constructor(e,t={}){this.controlPlane=e;this.nowFn=t.now??(()=>new Date),this.idFn=t.id??(()=>crypto.randomUUID()),this.encryptionKey=t.encryptionKey?bh("sha256").update(t.encryptionKey).digest():void 0}now(){return this.nowFn().toISOString()}run(e,t){this.controlPlane.database.run(e,t)}encrypt(e){if(!this.encryptionKey)throw Error("Authentication encryption key is not configured");let t=yi(12),r=Lk("aes-256-gcm",this.encryptionKey,t),o=Buffer.concat([r.update(e,"utf8"),r.final()]);return`v1.${t.toString("base64url")}.${r.getAuthTag().toString("base64url")}.${o.toString("base64url")}`}decrypt(e){if(!this.encryptionKey)throw Error("Authentication encryption key is not configured");let[t,r,o,m]=e.split(".");if(t!=="v1"||!r||!o||!m)throw Error("Encrypted authentication value is unavailable");let h=Fk("aes-256-gcm",this.encryptionKey,Buffer.from(r,"base64url"));return h.setAuthTag(Buffer.from(o,"base64url")),Buffer.concat([h.update(Buffer.from(m,"base64url")),h.final()]).toString("utf8")}createIdentity(e){if(!this.controlPlane.getActor(e.actorId))throw Error("Authentication actor was not found");let t=zk(e.username),r=yp(e.email),o=e.id??this.idFn(),m=this.now();return this.run(`INSERT INTO auth_identities (id, actor_id, username, email, email_verified_at, password_hash, requires_password_upgrade, created_at, updated_at)
|
|
65
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,[o,e.actorId,t,r??null,e.emailVerified&&r?m:null,e.passwordHash,e.requiresPasswordUpgrade?1:0,m,m]),this.getIdentity(o)}getIdentity(e){let t=this.controlPlane.database.query("SELECT * FROM auth_identities WHERE id = ?").get(e);return t?sc(t):void 0}getIdentityByActor(e){let t=this.controlPlane.database.query("SELECT * FROM auth_identities WHERE actor_id = ?").get(e);return t?sc(t):void 0}getIdentityByUsername(e){let t=this.controlPlane.database.query("SELECT * FROM auth_identities WHERE username = ? COLLATE NOCASE").get(e.trim());return t?sc(t):void 0}getIdentityByEmail(e){let t=this.controlPlane.database.query("SELECT * FROM auth_identities WHERE email = ? COLLATE NOCASE").get(e.trim().toLowerCase());return t?sc(t):void 0}listIdentities(){return this.controlPlane.database.query("SELECT * FROM auth_identities ORDER BY username COLLATE NOCASE").all().map(sc)}updatePassword(e,t,r={}){if(!this.getIdentity(e))throw Error("Authentication identity was not found");let m=this.now();return this.controlPlane.transaction(()=>{if(this.run(`UPDATE auth_identities SET password_hash = ?, credential_version = credential_version + 1,
|
|
66
|
+
requires_password_upgrade = ?, updated_at = ? WHERE id = ?`,[t,r.requiresUpgrade?1:0,m,e]),r.revokeSessions!==!1)this.run("UPDATE auth_sessions SET revoked_at = ? WHERE identity_id = ? AND revoked_at IS NULL",[m,e])}),this.getIdentity(e)}rehashPassword(e,t){let r=this.now();this.run("UPDATE auth_identities SET password_hash = ?, requires_password_upgrade = 0, updated_at = ? WHERE id = ?",[t,r,e]);let o=this.getIdentity(e);if(!o)throw Error("Authentication identity was not found");return o}setVerifiedEmail(e,t){let r=yp(t),o=this.now();this.run("UPDATE auth_identities SET email = ?, email_verified_at = ?, updated_at = ? WHERE id = ?",[r,o,o,e]);let m=this.getIdentity(e);if(!m)throw Error("Authentication identity was not found");return m}recordLogin(e){let t=this.now();this.run("UPDATE auth_identities SET last_login_at = ?, updated_at = ? WHERE id = ?",[t,t,e]);let r=this.getIdentity(e);if(!r)throw Error("Authentication identity was not found");return r}setDisabled(e,t){let r=this.now();this.controlPlane.transaction(()=>{if(this.run("UPDATE auth_identities SET disabled_at = ?, credential_version = credential_version + 1, updated_at = ? WHERE id = ?",[t?r:null,r,e]),t)this.run("UPDATE auth_sessions SET revoked_at = ? WHERE identity_id = ? AND revoked_at IS NULL",[r,e])});let o=this.getIdentity(e);if(!o)throw Error("Authentication identity was not found");return o}createActionToken(e,t,r={}){if(!this.getIdentity(e))throw Error("Authentication identity was not found");let o=yi(32).toString("base64url"),m=this.idFn(),h=this.now(),I=new Date(this.nowFn().getTime()+Math.min(86400000,Math.max(60000,r.ttlMs??Pd))).toISOString();return this.run("INSERT INTO auth_action_tokens (id, identity_id, type, token_hash, metadata, expires_at, created_at) VALUES (?, ?, ?, ?, ?, ?, ?)",[m,e,t,Wn(o),JSON.stringify(r.metadata??{}),I,h]),{actionToken:this.getActionToken(m),token:o}}getActionToken(e){let t=this.controlPlane.database.query("SELECT * FROM auth_action_tokens WHERE id = ?").get(e);return t?fh(t,this.now()):void 0}consumeActionToken(e,t){return this.controlPlane.transaction(()=>{let r=this.controlPlane.database.query("SELECT * FROM auth_action_tokens WHERE token_hash = ? AND type = ?").get(Wn(e),t);if(!r)throw Error("Action token is invalid");let o=fh(r,this.now());if(o.state!=="pending")throw Error(`Action token is ${o.state}`);if(this.controlPlane.database.run("UPDATE auth_action_tokens SET consumed_at = ? WHERE id = ? AND consumed_at IS NULL AND expires_at > ?",[this.now(),o.id,this.now()]).changes!==1)throw Error("Action token is no longer available");return this.getActionToken(o.id)})}revokeActionTokens(e,t){return this.controlPlane.database.run("UPDATE auth_action_tokens SET consumed_at = ? WHERE identity_id = ? AND type = ? AND consumed_at IS NULL",[this.now(),e,t]).changes}createSession(e){let t=this.getIdentity(e.identityId);if(!t||t.disabledAt)throw Error("Authentication identity is unavailable");let r=this.idFn(),o=yi(32).toString("base64url"),m=`v2.${r}.${o}`,h=this.now(),I=Math.min(86400000,Math.max(60000,e.idleTtlMs??Dd)),R=Math.min(2592000000,Math.max(I,e.absoluteTtlMs??He));return this.run(`INSERT INTO auth_sessions (id, identity_id, token_hash, credential_version, auth_method, user_agent, network_hint,
|
|
67
|
+
created_at, last_used_at, idle_expires_at, absolute_expires_at, recent_auth_at, mfa_at)
|
|
68
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,[r,t.id,Wn(m),t.credentialVersion,e.authMethod??"local",e.userAgent?.slice(0,256)??null,e.networkHint?.slice(0,128)??null,h,h,new Date(this.nowFn().getTime()+I).toISOString(),new Date(this.nowFn().getTime()+R).toISOString(),e.recentAuthAt??h,e.mfaAt??null]),{session:this.getSession(r),token:m}}getSession(e){let t=this.controlPlane.database.query("SELECT * FROM auth_sessions WHERE id = ?").get(e);return t?vp(t,this.now()):void 0}verifySessionToken(e){let t=/^v2\.([^.]+)\.[A-Za-z0-9_-]{40,}$/.exec(e);if(!t)return;let r=this.controlPlane.database.query("SELECT * FROM auth_sessions WHERE id = ? AND token_hash = ?").get(t[1],Wn(e));if(!r)return;let o=vp(r,this.now()),m=this.getIdentity(o.identityId);if(!m||m.disabledAt||o.state!=="active"||o.credentialVersion!==m.credentialVersion)return;let h=this.now(),I=new Date(r.idle_expires_at).getTime()-new Date(r.last_used_at).getTime();return this.run("UPDATE auth_sessions SET last_used_at = ?, idle_expires_at = ? WHERE id = ?",[h,new Date(Math.min(this.nowFn().getTime()+I,new Date(o.absoluteExpiresAt).getTime())).toISOString(),o.id]),{identity:m,session:this.getSession(o.id)}}listSessions(e,t={}){let r=this.controlPlane.database.query("SELECT * FROM auth_sessions WHERE identity_id = ? ORDER BY last_used_at DESC").all(e).map((o)=>vp(o,this.now()));return t.includeInactive?r:r.filter((o)=>o.state==="active")}revokeSession(e,t){return this.controlPlane.database.run("UPDATE auth_sessions SET revoked_at = ? WHERE id = ? AND identity_id = ? AND revoked_at IS NULL",[this.now(),t,e]).changes===1}revokeOtherSessions(e,t){return this.controlPlane.database.run("UPDATE auth_sessions SET revoked_at = ? WHERE identity_id = ? AND id != ? AND revoked_at IS NULL",[this.now(),e,t]).changes}getMfaFactor(e){let t=this.controlPlane.database.query("SELECT * FROM auth_mfa_factors WHERE identity_id = ?").get(e);return t?hh(t):void 0}beginTotpEnrollment(e,t={}){let r=this.getIdentity(e);if(!r)throw Error("Authentication identity was not found");let o=this.getMfaFactor(e);if(o?.state==="active")throw Error("MFA is already enabled");let m=Si(yi(20)),h=t.label?.trim().slice(0,80)||r.email||r.username,I=o?.id??this.idFn(),R=this.now();return this.controlPlane.transaction(()=>{if(o)this.run("UPDATE auth_mfa_factors SET label = ?, secret_ciphertext = ?, created_at = ?, verified_at = NULL, disabled_at = NULL WHERE id = ?",[h,this.encrypt(m),R,I]);else this.run("INSERT INTO auth_mfa_factors (id, identity_id, label, secret_ciphertext, created_at) VALUES (?, ?, ?, ?, ?)",[I,e,h,this.encrypt(m),R]);this.run("DELETE FROM auth_recovery_codes WHERE identity_id = ?",[e])}),{factor:this.getMfaFactor(e),secret:m,uri:lo({secret:m,account:h,issuer:t.issuer})}}verifyTotpEnrollment(e,t){let r=this.controlPlane.database.query("SELECT * FROM auth_mfa_factors WHERE identity_id = ?").get(e);if(!r||hh(r).state!=="pending")throw Error("MFA enrollment is not pending");let o=Or(this.decrypt(String(r.secret_ciphertext)),t,this.nowFn().getTime());if(o===void 0)throw Error("Authenticator code is invalid");let m=Array.from({length:10},()=>{let I=Si(yi(8)).slice(0,12);return`${I.slice(0,4)}-${I.slice(4,8)}-${I.slice(8)}`}),h=this.now();return this.controlPlane.transaction(()=>{this.run("UPDATE auth_mfa_factors SET verified_at = ?, disabled_at = NULL, last_used_step = ? WHERE identity_id = ?",[h,o,e]),this.run("DELETE FROM auth_recovery_codes WHERE identity_id = ?",[e]);for(let I of m)this.run("INSERT INTO auth_recovery_codes (id, identity_id, code_hash, created_at) VALUES (?, ?, ?, ?)",[this.idFn(),e,Wn(`${e}:${I.replace(/-/g,"").toUpperCase()}`),h])}),{factor:this.getMfaFactor(e),recoveryCodes:m}}verifyMfaCode(e,t,r={}){let o=this.controlPlane.database.query("SELECT * FROM auth_mfa_factors WHERE identity_id = ? AND verified_at IS NOT NULL AND disabled_at IS NULL").get(e);if(!o)return{valid:!1};let m=Or(this.decrypt(String(o.secret_ciphertext)),t,this.nowFn().getTime());if(m!==void 0){if(this.controlPlane.database.run("UPDATE auth_mfa_factors SET last_used_step = ? WHERE identity_id = ? AND (last_used_step IS NULL OR last_used_step < ?)",[m,e,m]).changes===1)return{valid:!0,method:"totp"}}let h=t.replace(/[-\s]/g,"").toUpperCase();if(!/^[A-Z2-7]{12}$/.test(h))return{valid:!1};let I=Wn(`${e}:${h}`),R=this.controlPlane.database.query("SELECT * FROM auth_recovery_codes WHERE identity_id = ? AND code_hash = ? AND consumed_at IS NULL").get(e,I);if(!R)return{valid:!1};if(r.consumeRecovery!==!1){if(this.controlPlane.database.run("UPDATE auth_recovery_codes SET consumed_at = ? WHERE id = ? AND consumed_at IS NULL",[this.now(),String(R.id)]).changes!==1)return{valid:!1}}return{valid:!0,method:"recovery"}}remainingRecoveryCodes(e){return Number(this.controlPlane.database.query("SELECT COUNT(*) AS count FROM auth_recovery_codes WHERE identity_id = ? AND consumed_at IS NULL").get(e)?.count??0)}disableMfa(e){let t=this.now();this.controlPlane.transaction(()=>{this.run("UPDATE auth_mfa_factors SET disabled_at = ? WHERE identity_id = ? AND disabled_at IS NULL",[t,e]),this.run("DELETE FROM auth_recovery_codes WHERE identity_id = ?",[e]),this.run("UPDATE auth_mfa_challenges SET consumed_at = ? WHERE identity_id = ? AND consumed_at IS NULL",[t,e])})}createMfaChallenge(e,t){if(this.getMfaFactor(e)?.state!=="active")throw Error("MFA is not enabled");let r=yi(32).toString("base64url"),o=this.idFn(),m=this.now(),h=new Date(this.nowFn().getTime()+Nd).toISOString();return this.controlPlane.transaction(()=>{this.run("UPDATE auth_mfa_challenges SET consumed_at = ? WHERE identity_id = ? AND purpose = ? AND consumed_at IS NULL",[m,e,t]),this.run("INSERT INTO auth_mfa_challenges (id, identity_id, purpose, token_hash, expires_at, created_at) VALUES (?, ?, ?, ?, ?, ?)",[o,e,t,Wn(r),h,m])}),{challenge:this.getMfaChallenge(o),token:r}}getMfaChallenge(e){let t=this.controlPlane.database.query("SELECT * FROM auth_mfa_challenges WHERE id = ?").get(e);return t?bp(t,this.now()):void 0}inspectMfaChallengeToken(e,t){let r=this.controlPlane.database.query("SELECT * FROM auth_mfa_challenges WHERE token_hash = ? AND purpose = ?").get(Wn(e),t);return r?bp(r,this.now()):void 0}completeMfaChallenge(e,t,r){let o=this.controlPlane.database.query("SELECT * FROM auth_mfa_challenges WHERE token_hash = ? AND purpose = ?").get(Wn(e),r);if(!o)throw Error("MFA challenge is invalid");let m=bp(o,this.now());if(m.state!=="pending")throw Error(`MFA challenge is ${m.state}`);let h=this.verifyMfaCode(m.identityId,t);if(!h.valid||!h.method)throw this.run("UPDATE auth_mfa_challenges SET attempts = attempts + 1 WHERE id = ?",[m.id]),Error("Authenticator or recovery code is invalid");if(this.controlPlane.database.run("UPDATE auth_mfa_challenges SET consumed_at = ? WHERE id = ? AND consumed_at IS NULL AND attempts < 5 AND expires_at > ?",[this.now(),m.id,this.now()]).changes!==1)throw Error("MFA challenge is no longer available");return{identity:this.getIdentity(m.identityId),challenge:this.getMfaChallenge(m.id),method:h.method}}markSessionStepUp(e,t=!1){let r=this.now();this.run("UPDATE auth_sessions SET recent_auth_at = ?, mfa_at = CASE WHEN ? = 1 THEN ? ELSE mfa_at END WHERE id = ? AND revoked_at IS NULL",[r,t?1:0,r,e]);let o=this.getSession(e);if(!o)throw Error("Authentication session was not found");return o}isRecentlyAuthenticated(e,t=600000){return this.nowFn().getTime()-new Date(e.recentAuthAt).getTime()<=t}upsertOidcProvider(e){if(!this.controlPlane.getOrganization(e.organizationId))throw Error("OIDC provider organization was not found");let t=Uk(e.slug),r=e.name.trim().slice(0,80);if(!r)throw Error("OIDC provider name is required");let o=qk(e.issuer),m=e.clientId.trim();if(!m||m.length>512)throw Error("OIDC client ID is required");let h=Wk(e.scopes),I=Vk(e.allowedDomains),R=e.id?this.getOidcProvider(e.id):this.getOidcProviderBySlug(t),A=this.now();if(R){if(R.organizationId!==e.organizationId)throw Error("OIDC provider belongs to another organization");return this.run(`UPDATE auth_oidc_providers SET slug = ?, name = ?, issuer = ?, client_id = ?,
|
|
69
|
+
client_secret_ciphertext = COALESCE(?, client_secret_ciphertext), scopes = ?, allowed_domains = ?,
|
|
70
|
+
default_role = ?, enabled = ?, enforce_sso = ?, updated_at = ? WHERE id = ?`,[t,r,o,m,e.clientSecret?this.encrypt(e.clientSecret):null,JSON.stringify(h),JSON.stringify(I),e.defaultRole??R.defaultRole,e.enabled??R.enabled?1:0,e.enforceSso??R.enforceSso?1:0,A,R.id]),this.getOidcProvider(R.id)}if(!e.clientSecret)throw Error("OIDC client secret is required for a new provider");let E=e.id??this.idFn();return this.run(`INSERT INTO auth_oidc_providers (id, organization_id, slug, name, issuer, client_id,
|
|
71
|
+
client_secret_ciphertext, scopes, allowed_domains, default_role, enabled, enforce_sso, created_at, updated_at)
|
|
72
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,[E,e.organizationId,t,r,o,m,this.encrypt(e.clientSecret),JSON.stringify(h),JSON.stringify(I),e.defaultRole??"viewer",e.enabled===!1?0:1,e.enforceSso?1:0,A,A]),this.getOidcProvider(E)}getOidcProvider(e){let t=this.controlPlane.database.query("SELECT * FROM auth_oidc_providers WHERE id = ?").get(e);return t?qc(t):void 0}getOidcProviderBySlug(e){let t=this.controlPlane.database.query("SELECT * FROM auth_oidc_providers WHERE slug = ? COLLATE NOCASE").get(e.trim());return t?qc(t):void 0}listOidcProviders(e,t={}){return(e?this.controlPlane.database.query("SELECT * FROM auth_oidc_providers WHERE organization_id = ? ORDER BY name COLLATE NOCASE").all(e):this.controlPlane.database.query("SELECT * FROM auth_oidc_providers ORDER BY name COLLATE NOCASE").all()).map(qc).filter((o)=>t.includeDisabled||o.enabled)}setOidcProviderEnabled(e,t){let r=this.now();if(this.controlPlane.database.run("UPDATE auth_oidc_providers SET enabled = ?, enforce_sso = CASE WHEN ? = 0 THEN 0 ELSE enforce_sso END, updated_at = ? WHERE id = ?",[t?1:0,t?1:0,r,e]).changes!==1)throw Error("OIDC provider was not found");if(!t)this.run("UPDATE auth_oidc_transactions SET consumed_at = ? WHERE provider_id = ? AND consumed_at IS NULL",[r,e]);return this.getOidcProvider(e)}getOidcProviderCredentials(e){let t=this.controlPlane.database.query("SELECT * FROM auth_oidc_providers WHERE id = ?").get(e);if(!t||typeof t.client_secret_ciphertext!=="string")throw Error("OIDC provider credentials are unavailable");return{provider:qc(t),clientSecret:this.decrypt(t.client_secret_ciphertext)}}beginOidcTransaction(e,t,r){if(!this.getOidcProvider(e)?.enabled)throw Error("OIDC provider is unavailable");let m=this.idFn(),h=yi(32).toString("base64url"),I=yi(32).toString("base64url"),R=yi(64).toString("base64url"),A=this.now(),E=new Date(this.nowFn().getTime()+kd).toISOString();return this.run(`INSERT INTO auth_oidc_transactions (id, provider_id, state_hash, nonce_ciphertext, verifier_ciphertext,
|
|
73
|
+
redirect_uri, return_path, expires_at, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,[m,e,Wn(h),this.encrypt(I),this.encrypt(R),t,r,E,A]),{transaction:this.getOidcTransaction(m),state:h,nonce:I,verifier:R}}getOidcTransaction(e){let t=this.controlPlane.database.query("SELECT * FROM auth_oidc_transactions WHERE id = ?").get(e);return t?vh(t,this.now()):void 0}consumeOidcTransaction(e,t){return this.controlPlane.transaction(()=>{let r=this.controlPlane.database.query("SELECT * FROM auth_oidc_transactions WHERE provider_id = ? AND state_hash = ?").get(e,Wn(t));if(!r)throw Error("OIDC transaction is invalid");let o=vh(r,this.now());if(o.state!=="pending")throw Error(`OIDC transaction is ${o.state}`);if(this.controlPlane.database.run("UPDATE auth_oidc_transactions SET consumed_at = ? WHERE id = ? AND consumed_at IS NULL AND expires_at > ?",[this.now(),o.id,this.now()]).changes!==1)throw Error("OIDC transaction is no longer available");return{transaction:this.getOidcTransaction(o.id),nonce:this.decrypt(String(r.nonce_ciphertext)),verifier:this.decrypt(String(r.verifier_ciphertext))}})}getOidcSubject(e,t){let r=this.controlPlane.database.query("SELECT * FROM auth_oidc_subjects WHERE provider_id = ? AND subject = ?").get(e,t);return r?Hk(r):void 0}linkOidcSubject(e,t,r,o){let m=this.getOidcProvider(e),h=this.getIdentity(t);if(!m||!h)throw Error("OIDC provider or identity was not found");let I=yp(o);if(!r.trim()||r.length>512)throw Error("OIDC subject is invalid");let R=this.getOidcSubject(e,r);if(R&&R.identityId!==t)throw Error("OIDC subject is already linked to another identity");let A=this.now();if(R)return this.run("UPDATE auth_oidc_subjects SET email = ?, last_login_at = ? WHERE id = ?",[I,A,R.id]),this.getOidcSubject(e,r);let E=this.idFn();return this.run("INSERT INTO auth_oidc_subjects (id, provider_id, identity_id, subject, email, linked_at, last_login_at) VALUES (?, ?, ?, ?, ?, ?, ?)",[E,e,t,r,I,A,A]),this.getOidcSubject(e,r)}purgeExpired(){let e=this.now(),t=this.controlPlane.database.run("DELETE FROM auth_action_tokens WHERE expires_at < ? OR consumed_at IS NOT NULL",[e]).changes,r=this.controlPlane.database.run("DELETE FROM auth_sessions WHERE revoked_at IS NOT NULL OR idle_expires_at < ? OR absolute_expires_at < ?",[e,e]).changes;return this.controlPlane.database.run("DELETE FROM auth_mfa_challenges WHERE consumed_at IS NOT NULL OR expires_at < ?",[e]),this.controlPlane.database.run("DELETE FROM auth_oidc_transactions WHERE consumed_at IS NOT NULL OR expires_at < ?",[e]),{actionTokens:t,sessions:r}}}async function Lr(e,t){let r=e.notifications?.email?.from;if(!r)return!1;try{let{EmailClient:o}=await import("./chunk-wrnhbb13.js");return await new o({region:e.project.region,defaultFrom:r}).send(t),!0}catch{return!1}}import{randomBytes as Kk}from"node:crypto";import{chmodSync as Jk,existsSync as Gk,mkdirSync as Qk,readFileSync as Yk,writeFileSync as Zk}from"node:fs";import{dirname as Xk}from"node:path";function zg(){return _("auth-encryption-key")}function Ge(e){let t=process.env.TS_CLOUD_AUTH_ENCRYPTION_KEY?.trim();if(t)return t;let r=d(e,"auth-encryption-key");try{if(Gk(r)){let m=Yk(r,"utf8").trim();if(m)return m}}catch{}let o=Kk(32).toString("base64url");try{Qk(Xk(r),{recursive:!0,mode:448}),Zk(r,`${o}
|
|
74
|
+
`,{mode:384}),Jk(r,384)}catch{}return o}import{constants as eI,createHash as Sp,createPublicKey as tI,createVerify as rI,timingSafeEqual as nI}from"node:crypto";var kh=new Set(["RS256","PS256","ES256"]),wh=60;function Vc(e,t){let r=new URL(e),o=["127.0.0.1","::1","localhost"].includes(r.hostname);if(r.protocol!=="https:"&&!(r.protocol==="http:"&&o))throw Error(`${t} must use HTTPS`);if(r.username||r.password||r.hash)throw Error(`${t} contains unsupported URL components`);return r}function wp(e,t){if(!e||typeof e!=="object"||Array.isArray(e))throw Error(`${t} is invalid`);return e}async function kp(e,t){if(!e.ok)throw Error(`${t} failed with HTTP ${e.status}`);if(!(e.headers.get("content-type")??"").toLowerCase().includes("application/json"))throw Error(`${t} returned an unsupported content type`);if(Number(e.headers.get("content-length")??0)>1048576)throw Error(`${t} response is too large`);let m=await e.text();if(m.length>1048576)throw Error(`${t} response is too large`);try{return wp(JSON.parse(m),t)}catch(h){if(h instanceof Error&&h.message===`${t} is invalid`)throw h;throw Error(`${t} returned invalid JSON`)}}function iI(e,t){let r=Sp("sha256").update(e).digest(),o=Sp("sha256").update(t).digest();return nI(r,o)}function Bd(e){if(!e)return"/";let t=e.trim();if(!t.startsWith("/")||t.startsWith("//")||t.includes("\\")||/[\r\n]/.test(t))return"/";try{let r=new URL(t,"https://local.invalid");return r.origin==="https://local.invalid"?`${r.pathname}${r.search}${r.hash}`:"/"}catch{return"/"}}async function uo(e,t=fetch){let r=Vc(e.issuer,"OIDC issuer"),o=new URL(`${r.pathname.replace(/\/$/,"")}/.well-known/openid-configuration`,r.origin),m=await kp(await t(o,{headers:{accept:"application/json"},redirect:"error",signal:AbortSignal.timeout(1e4)}),"OIDC discovery");if(m.issuer!==e.issuer)throw Error("OIDC discovery issuer does not match the configured issuer");for(let I of["authorization_endpoint","token_endpoint","jwks_uri"]){if(typeof m[I]!=="string")throw Error(`OIDC discovery is missing ${I}`);Vc(m[I],`OIDC ${I}`)}if(Array.isArray(m.response_types_supported)&&!m.response_types_supported.includes("code"))throw Error("OIDC provider does not support authorization code flow");if(Array.isArray(m.code_challenge_methods_supported)&&!m.code_challenge_methods_supported.includes("S256"))throw Error("OIDC provider does not support PKCE S256");let h=Array.isArray(m.id_token_signing_alg_values_supported)?m.id_token_signing_alg_values_supported.filter((I)=>typeof I==="string"):[];if(h.length>0&&!h.some((I)=>kh.has(I)))throw Error("OIDC provider does not offer a supported ID-token signing algorithm");return m}async function po(e,t,r,o,m=fetch){let h=e.getOidcProviderBySlug(t);if(!h?.enabled)throw Error("OIDC provider is unavailable");let I=Vc(r,"Application origin").origin,R=await uo(h,m),A=`${I}/auth/oidc/${encodeURIComponent(h.slug)}/callback`,E=e.beginOidcTransaction(h.id,A,Bd(o)),x=new URL(R.authorization_endpoint);return x.searchParams.set("response_type","code"),x.searchParams.set("client_id",h.clientId),x.searchParams.set("redirect_uri",A),x.searchParams.set("scope",h.scopes.join(" ")),x.searchParams.set("state",E.state),x.searchParams.set("nonce",E.nonce),x.searchParams.set("code_challenge",Sp("sha256").update(E.verifier).digest("base64url")),x.searchParams.set("code_challenge_method","S256"),{authorizationUrl:x.href,expiresAt:E.transaction.expiresAt}}function oI(e){if(e.length>32768)throw Error("OIDC ID token is too large");let t=e.split(".");if(t.length!==3||t.some((r)=>!r))throw Error("OIDC ID token is malformed");try{return{header:wp(JSON.parse(Buffer.from(t[0],"base64url").toString("utf8")),"OIDC ID-token header"),claims:wp(JSON.parse(Buffer.from(t[1],"base64url").toString("utf8")),"OIDC ID-token claims"),signingInput:`${t[0]}.${t[1]}`,signature:Buffer.from(t[2],"base64url")}}catch(r){if(r instanceof Error&&r.message.startsWith("OIDC"))throw r;throw Error("OIDC ID token is malformed")}}async function sI(e,t,r){let o=oI(e),m=typeof o.header.alg==="string"?o.header.alg:"",h=typeof o.header.kid==="string"?o.header.kid:"";if(!kh.has(m)||!h)throw Error("OIDC ID token uses an unsupported signing key");if(t.id_token_signing_alg_values_supported&&!t.id_token_signing_alg_values_supported.includes(m))throw Error("OIDC ID-token algorithm was not advertised by the provider");let I=await kp(await r(t.jwks_uri,{headers:{accept:"application/json"},redirect:"error",signal:AbortSignal.timeout(1e4)}),"OIDC signing-key request");if(!Array.isArray(I.keys))throw Error("OIDC signing-key response is invalid");let R=I.keys.find((N)=>{if(!N||typeof N!=="object"||Array.isArray(N))return!1;let F=N;return F.kid===h&&(!F.alg||F.alg===m)&&(!F.use||F.use==="sig")});if(!R)throw Error("OIDC ID-token signing key was not found");let A=tI({key:R,format:"jwk"}),E=rI("SHA256").update(o.signingInput).end();if(!(m==="PS256"?E.verify({key:A,padding:eI.RSA_PKCS1_PSS_PADDING,saltLength:32},o.signature):m==="ES256"?E.verify({key:A,dsaEncoding:"ieee-p1363"},o.signature):E.verify(A,o.signature)))throw Error("OIDC ID-token signature is invalid");return o.claims}function aI(e,t,r,o){if(e.iss!==t.issuer)throw Error("OIDC ID-token issuer is invalid");let m=typeof e.aud==="string"?[e.aud]:Array.isArray(e.aud)?e.aud.filter((A)=>typeof A==="string"):[];if(!m.includes(t.clientId))throw Error("OIDC ID-token audience is invalid");if((m.length>1||e.azp!==void 0)&&e.azp!==t.clientId)throw Error("OIDC ID-token authorized party is invalid");let h=Math.floor(o.getTime()/1000);if(typeof e.exp!=="number"||e.exp<h-wh)throw Error("OIDC ID token is expired");if(typeof e.iat!=="number"||e.iat>h+wh)throw Error("OIDC ID-token issued-at time is invalid");if(typeof e.nonce!=="string"||!iI(e.nonce,r))throw Error("OIDC ID-token nonce is invalid");if(typeof e.sub!=="string"||!e.sub||e.sub.length>512)throw Error("OIDC ID-token subject is invalid");if(e.email_verified!==!0||typeof e.email!=="string")throw Error("OIDC provider did not return a verified email address");let I=e.email.trim().toLowerCase();if(I.length>254||!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(I))throw Error("OIDC provider returned an invalid email address");let R=I.split("@")[1];if(!R||!t.allowedDomains.includes(R))throw Error("OIDC email domain is not allowed for this organization");return{provider:t,subject:e.sub,email:I,name:typeof e.name==="string"?e.name.slice(0,120):void 0,claims:e}}async function go(e,t,r=fetch){let o=e.getOidcProviderBySlug(t.providerSlug);if(!o?.enabled)throw Error("OIDC provider is unavailable");if(!t.state||t.state.length>512||!t.code||t.code.length>4096)throw Error("OIDC callback is incomplete");let m=Vc(t.origin,"Application origin").origin,h=e.consumeOidcTransaction(o.id,t.state),I=`${m}/auth/oidc/${encodeURIComponent(o.slug)}/callback`;if(h.transaction.redirectUri!==I)throw Error("OIDC callback redirect URI does not match the original request");let{clientSecret:R}=e.getOidcProviderCredentials(o.id),A=await uo(o,r),E=new URLSearchParams({grant_type:"authorization_code",code:t.code,redirect_uri:h.transaction.redirectUri,code_verifier:h.verifier}),x={accept:"application/json","content-type":"application/x-www-form-urlencoded"},N=A.token_endpoint_auth_methods_supported??["client_secret_basic"];if(N.includes("client_secret_basic")){let K=(re)=>new URLSearchParams({value:re}).toString().slice(6),J=`${K(o.clientId)}:${K(R)}`;x.authorization=`Basic ${Buffer.from(J).toString("base64")}`}else if(N.includes("client_secret_post"))E.set("client_id",o.clientId),E.set("client_secret",R);else throw Error("OIDC provider does not support a configured client authentication method");let F=await kp(await r(A.token_endpoint,{method:"POST",headers:x,body:E,redirect:"error",signal:AbortSignal.timeout(1e4)}),"OIDC token exchange");if(typeof F.id_token!=="string")throw Error("OIDC token response did not include an ID token");let z=await sI(F.id_token,A,r);return{identity:aI(z,o,h.nonce,t.now??new Date),returnPath:h.transaction.returnPath}}import{createHash as cI,randomBytes as dI}from"node:crypto";var Eh=new Set(oe),xd=7776000000,_d=31536000000,Od=86400000;function Ii(e){return typeof e==="string"?e:void 0}function Ih(e){return cI("sha256").update(e).digest("hex")}function lI(e){try{let t=JSON.parse(String(e));return Array.isArray(t)?t.filter((r)=>typeof r==="string"&&Eh.has(r)):[]}catch{return[]}}function uI(e){let t=String(e.scope_type);return t==="organization"?{type:t}:{type:t,id:String(e.scope_id)}}function Ip(e){let t=Ii(e.disabled_at);return{id:String(e.id),organizationId:String(e.organization_id),actorId:String(e.actor_id),slug:String(e.slug),name:String(e.name),description:Ii(e.description),createdByActorId:Ii(e.created_by_actor_id),disabledAt:t,createdAt:String(e.created_at),updatedAt:String(e.updated_at),state:t?"disabled":"active"}}function Ep(e,t){let r=Ii(e.revoked_at),o=String(e.expires_at);return{id:String(e.id),serviceAccountId:String(e.service_account_id),name:String(e.name),prefix:String(e.token_prefix),capabilities:lI(e.capabilities),scope:uI(e),expiresAt:o,lastUsedAt:Ii(e.last_used_at),lastNetworkHint:Ii(e.last_network_hint),revokedAt:r,rotatedFromTokenId:Ii(e.rotated_from_token_id),createdByActorId:Ii(e.created_by_actor_id),createdAt:String(e.created_at),updatedAt:String(e.updated_at),state:r?"revoked":o<=t?"expired":"active"}}function pI(e){let t={};try{t=JSON.parse(String(e.response_body))}catch{}return{id:String(e.id),tokenId:String(e.token_id),key:String(e.idempotency_key),requestHash:String(e.request_hash),operationId:Ii(e.operation_id),responseStatus:Number(e.response_status),responseBody:t,expiresAt:String(e.expires_at),createdAt:String(e.created_at)}}class bi{controlPlane;nowFn;idFn;constructor(e,t={}){this.controlPlane=e;this.nowFn=t.now??(()=>new Date),this.idFn=t.id??(()=>crypto.randomUUID())}now(){return this.nowFn().toISOString()}run(e,t){this.controlPlane.database.run(e,t)}createServiceAccount(e){if(!this.controlPlane.getOrganization(e.organizationId))throw Error("Service-account organization was not found");let t=e.slug.trim().toLowerCase();if(!/^[a-z0-9][a-z0-9-]{1,47}$/.test(t))throw Error("Service-account slug must be 2-48 lowercase letters, numbers or dashes");let r=e.name.trim().slice(0,100);if(!r)throw Error("Service-account name is required");let o=this.controlPlane.createActor({kind:"service_account",externalId:`service-account:${e.organizationId}:${t}`,displayName:r,metadata:{source:"api",serviceAccountSlug:t}}),m=this.idFn(),h=this.now();this.run(`INSERT INTO service_accounts (id, organization_id, actor_id, slug, name, description, created_by_actor_id, created_at, updated_at)
|
|
75
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,[m,e.organizationId,o.id,t,r,e.description?.trim().slice(0,500)||null,e.createdByActorId??null,h,h]);let I=this.controlPlane.createMembership({organizationId:e.organizationId,actorId:o.id,roleTemplate:e.roleTemplate,scope:e.scope,source:"manual",performedByActorId:e.createdByActorId}),R=e.scope??{type:"organization"};return this.controlPlane.appendEvent({organizationId:e.organizationId,actorId:e.createdByActorId,type:"api.service_account.created",payload:{serviceAccountId:m,roleTemplate:e.roleTemplate,scope:{type:R.type,id:R.id??null}}}),{serviceAccount:this.getServiceAccount(m),membership:I}}getServiceAccount(e){let t=this.controlPlane.database.query("SELECT * FROM service_accounts WHERE id = ?").get(e);return t?Ip(t):void 0}getServiceAccountBySlug(e,t){let r=this.controlPlane.database.query("SELECT * FROM service_accounts WHERE organization_id = ? AND slug = ? COLLATE NOCASE").get(e,t.trim());return r?Ip(r):void 0}listServiceAccounts(e,t={}){return this.controlPlane.database.query("SELECT * FROM service_accounts WHERE organization_id = ? ORDER BY name COLLATE NOCASE").all(e).map(Ip).filter((r)=>t.includeDisabled||r.state==="active")}disableServiceAccount(e,t){let r=this.getServiceAccount(e);if(!r)throw Error("Service account was not found");let o=this.now();return this.controlPlane.transaction(()=>{this.run("UPDATE service_accounts SET disabled_at = ?, updated_at = ? WHERE id = ?",[o,o,e]),this.run("UPDATE api_tokens SET revoked_at = ?, updated_at = ? WHERE service_account_id = ? AND revoked_at IS NULL",[o,o,e]);let m=this.controlPlane.getMembershipForActor(r.organizationId,r.actorId);if(m?.status==="active")this.controlPlane.revokeMembership(m.id)}),this.controlPlane.appendEvent({organizationId:r.organizationId,actorId:t,type:"api.service_account.disabled",payload:{serviceAccountId:e}}),this.getServiceAccount(e)}createToken(e){let t=this.getServiceAccount(e.serviceAccountId);if(!t||t.state!=="active")throw Error("Service account is unavailable");let r=this.controlPlane.getMembershipForActor(t.organizationId,t.actorId);if(!r||r.status!=="active")throw Error("Service-account membership is unavailable");let o=[...new Set(e.capabilities)];if(o.length===0||o.some((F)=>!Eh.has(F)))throw Error("API token requires valid explicit capabilities");let m=e.scope??r.scope,h=this.controlPlane.resolveAuthorizationTarget(t.organizationId,m);if(!h)throw Error("API token scope was not found in this organization");for(let F of o)if(!w({membership:r,grants:this.controlPlane.listGrants(r.id),capability:F,target:h}).allowed)throw Error(`Service-account membership does not grant ${F} at the token scope`);let I=e.expiresAt?new Date(e.expiresAt):new Date(this.nowFn().getTime()+xd);if(!Number.isFinite(I.getTime())||I.getTime()<=this.nowFn().getTime()+60000||I.getTime()>this.nowFn().getTime()+_d)throw Error("API token expiry must be between one minute and one year from now");let R=this.idFn(),A=dI(32).toString("base64url"),E=`tsc_v1.${R}.${A}`,x=`tsc_v1.${R.slice(0,8)}`,N=this.now();return this.run(`INSERT INTO api_tokens (id, service_account_id, name, token_hash, token_prefix, capabilities, scope_type, scope_id,
|
|
76
|
+
expires_at, rotated_from_token_id, created_by_actor_id, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,[R,t.id,e.name.trim().slice(0,100)||"API token",Ih(E),x,JSON.stringify(o.sort()),m.type,m.type==="organization"?null:m.id??null,I.toISOString(),e.rotatedFromTokenId??null,e.createdByActorId??null,N,N]),this.controlPlane.appendEvent({organizationId:t.organizationId,actorId:e.createdByActorId,type:"api.token.created",payload:{serviceAccountId:t.id,tokenId:R,prefix:x,capabilities:o,scope:{type:m.type,id:m.id??null},expiresAt:I.toISOString(),rotatedFromTokenId:e.rotatedFromTokenId??null}}),{token:this.getToken(R),secret:E}}getToken(e){let t=this.controlPlane.database.query("SELECT * FROM api_tokens WHERE id = ?").get(e);return t?Ep(t,this.now()):void 0}listTokens(e,t={}){return this.controlPlane.database.query("SELECT * FROM api_tokens WHERE service_account_id = ? ORDER BY created_at DESC").all(e).map((r)=>Ep(r,this.now())).filter((r)=>t.includeInactive||r.state==="active")}verifyToken(e,t){let r=/^tsc_v1\.([^.]+)\.[A-Za-z0-9_-]{40,}$/.exec(e);if(!r)return;let o=this.controlPlane.database.query("SELECT * FROM api_tokens WHERE id = ? AND token_hash = ?").get(r[1],Ih(e));if(!o)return;let m=Ep(o,this.now()),h=this.getServiceAccount(m.serviceAccountId);if(!h||h.state!=="active"||m.state!=="active")return;let I=this.controlPlane.getActor(h.actorId),R=this.controlPlane.getMembershipForActor(h.organizationId,h.actorId);if(!I||I.disabledAt||!R||R.status!=="active")return;let A=this.now(),E=!m.lastUsedAt||this.nowFn().getTime()-new Date(m.lastUsedAt).getTime()>=3600000;if(this.run("UPDATE api_tokens SET last_used_at = ?, last_network_hint = ?, updated_at = ? WHERE id = ?",[A,t?.slice(0,128)??null,A,m.id]),E)this.controlPlane.appendEvent({organizationId:h.organizationId,actorId:I.id,type:"api.token.used",payload:{serviceAccountId:h.id,tokenId:m.id,prefix:m.prefix}});return{serviceAccount:h,token:this.getToken(m.id),actor:I,membership:R}}rotateToken(e,t){let r=this.getToken(e);if(!r||r.state!=="active")throw Error("Active API token was not found");let o=this.createToken({serviceAccountId:r.serviceAccountId,name:`${r.name} (rotated)`,capabilities:r.capabilities,scope:r.scope,expiresAt:r.expiresAt,createdByActorId:t,rotatedFromTokenId:r.id}),m=this.getServiceAccount(r.serviceAccountId);return this.controlPlane.appendEvent({organizationId:m?.organizationId,actorId:t,type:"api.token.rotated",payload:{serviceAccountId:r.serviceAccountId,previousTokenId:r.id,tokenId:o.token.id}}),o}revokeToken(e,t){let r=this.getToken(e);if(!r)throw Error("API token was not found");if(r.state==="active"){let o=this.now();this.run("UPDATE api_tokens SET revoked_at = ?, updated_at = ? WHERE id = ?",[o,o,e]);let m=this.getServiceAccount(r.serviceAccountId);this.controlPlane.appendEvent({organizationId:m?.organizationId,actorId:t,type:"api.token.revoked",payload:{serviceAccountId:r.serviceAccountId,tokenId:e}})}return this.getToken(e)}getIdempotency(e,t){let r=this.controlPlane.database.query("SELECT * FROM api_idempotency_records WHERE token_id = ? AND idempotency_key = ? AND expires_at > ?").get(e,t,this.now());return r?pI(r):void 0}saveIdempotency(e){if(!/^[A-Za-z0-9._:-]{8,128}$/.test(e.key))throw Error("Idempotency-Key must contain 8-128 safe characters");let t=this.getIdempotency(e.tokenId,e.key);if(t){if(t.requestHash!==e.requestHash)throw Error("Idempotency-Key was already used for a different request");return t}let r=this.idFn(),o=this.now(),m=new Date(this.nowFn().getTime()+Od).toISOString();return this.run(`INSERT INTO api_idempotency_records (id, token_id, idempotency_key, request_hash, operation_id, response_status, response_body, expires_at, created_at)
|
|
77
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,[r,e.tokenId,e.key,e.requestHash,e.operationId??null,e.responseStatus,JSON.stringify(e.responseBody),m,o]),this.getIdempotency(e.tokenId,e.key)}purgeExpired(){let e=this.now(),t=this.controlPlane.database.run("DELETE FROM api_tokens WHERE (expires_at < ? OR revoked_at IS NOT NULL) AND created_at < ?",[e,new Date(this.nowFn().getTime()-2592000000).toISOString()]).changes,r=this.controlPlane.database.run("DELETE FROM api_idempotency_records WHERE expires_at < ?",[e]).changes;return{tokens:t,idempotency:r}}}import{isIP as Th}from"node:net";var mI={every_minute:"* * * * *",every_five_minutes:"*/5 * * * *",hourly:"0 * * * *",daily:"0 0 * * *",weekly:"0 0 * * 0"},gI={JAN:1,FEB:2,MAR:3,APR:4,MAY:5,JUN:6,JUL:7,AUG:8,SEP:9,OCT:10,NOV:11,DEC:12},Rh={SUN:0,MON:1,TUE:2,WED:3,THU:4,FRI:5,SAT:6};function Ah(e){try{return new Intl.DateTimeFormat("en",{timeZone:e}).format(new Date),e}catch{throw Error(`Unknown schedule timezone: ${e}`)}}function Kc(e,t,r,o){let m=t[e.toUpperCase()]??Number(e);if(!Number.isInteger(m)||m<r||m>o)throw Error(`Schedule field value ${e} is outside ${r}-${o}.`);return m}function Sa(e,t,r,o={},m){if(e==="*"||e==="?")return;let h=new Set;for(let I of e.split(",")){let[R,A]=I.split("/"),E=A?Kc(A,{},1,r-t+1):1,x,N;if(R==="*")x=t,N=r;else if(R.includes("-")){let F=R.split("-");x=Kc(F[0],o,t,r),N=Kc(F[1],o,t,r)}else x=Kc(R,o,t,r),N=x;if(N<x)throw Error("Schedule ranges must be ascending.");for(let F=x;F<=N;F+=E)h.add(m?m(F):F===7&&r===7?0:F)}return h}function fI(e,t){let r=Object.fromEntries(new Intl.DateTimeFormat("en-US",{timeZone:t,year:"numeric",month:"numeric",day:"numeric",weekday:"short",hour:"numeric",minute:"numeric",hourCycle:"h23"}).formatToParts(e).map((o)=>[o.type,o.value]));return{minute:Number(r.minute),hour:Number(r.hour),day:Number(r.day),month:Number(r.month),weekday:Rh[r.weekday.toUpperCase()],year:Number(r.year),key:`${r.year}-${r.month}-${r.day} ${r.hour}:${r.minute}`}}function Ch(e){let r=(e.startsWith("cron(")?e.slice(5,-1).trim():e.trim()).split(/\s+/);if(r.length!==5&&r.length!==6)throw Error("Cron schedules require five standard fields or six EventBridge fields.");let[o,m,h,I,R,A]=r,E=r.length===6;return{minute:Sa(o,0,59),hour:Sa(m,0,23),day:Sa(h,1,31),month:Sa(I,1,12,gI),weekday:Sa(R,E?1:0,7,Rh,E?(x)=>x===7?6:x-1:void 0),year:A?Sa(A,1970,2199):void 0,dayWildcard:h==="*"||h==="?",weekdayWildcard:R==="*"||R==="?"}}function hI(e,t){if(!((!t.minute||t.minute.has(e.minute))&&(!t.hour||t.hour.has(e.hour))&&(!t.month||t.month.has(e.month))&&(!t.year||t.year.has(e.year))))return!1;let o=!t.day||t.day.has(e.day),m=!t.weekday||t.weekday.has(e.weekday);return t.dayWildcard?m:t.weekdayWildcard?o:o||m}function Ph(e){let t=/^rate\(\s*(\d+)\s+(minute|minutes|hour|hours|day|days)\s*\)$/i.exec(e);if(!t)return;let r=Number(t[1]);if(r<1)throw Error("Rate amount must be positive.");return r*(t[2].toLowerCase().startsWith("minute")?60000:t[2].toLowerCase().startsWith("hour")?3600000:86400000)}function Ve(e){let t=e.trim(),r=mI[t.toLowerCase()]??t;if(!r)throw Error("Schedule expression is required.");let o=Ph(r);if(o){let m=o/60000;return{original:t,normalized:`rate(${m} minutes)`,kind:"rate",description:m%1440===0?`Every ${m/1440} day(s)`:m%60===0?`Every ${m/60} hour(s)`:`Every ${m} minute(s)`}}return Ch(r),{original:t,normalized:r.startsWith("cron(")?r:`cron(${r})`,kind:"cron",description:`Cron ${r.replace(/^cron\(|\)$/g,"")}`}}function gt(e,t,r=new Date,o=5){let m=Ve(e),h=Ah(t),I=Math.min(20,Math.max(1,o)),R=Ph(m.normalized);if(R)return Array.from({length:I},(z,K)=>new Date(r.getTime()+R*(K+1)).toISOString());let A=Ch(m.normalized),E=[],x=new Set,N=new Date(Math.floor(r.getTime()/60000)*60000+60000),F=0;while(E.length<I&&F<1100000){let z=fI(N,h);if(hI(z,A)&&!x.has(z.key))E.push(N.toISOString()),x.add(z.key);N=new Date(N.getTime()+60000),F++}if(E.length<I)throw Error("Schedule produced no runs within the bounded preview horizon.");return E}function mt(e,t="UTC",r=new Date,o=5){let m=Ve(e),h=gt(m.normalized,t,r,o),I=m.kind==="cron"?["Server cron and EventBridge use the same wall-clock preview; provider-specific extensions are retained only when supported."]:["Rate schedules use elapsed-time intervals and are not shifted by DST."];return{...m,timezone:Ah(t),nextRuns:h,capabilities:{server:m.kind==="cron",eventbridge:!0,notes:I}}}function Hr(e){let t=Ve(e.expression),r=t.normalized.slice(5,-1).trim().split(/\s+/),o=t.kind==="cron"&&r.length===5;if(e.provider==="server")return{provider:"server",supported:o,features:{timezone:!0,flexibleWindow:!1,catchUp:!1,replaceOverlap:!1},notes:[t.kind==="rate"?"Server cron requires a cron expression; use an equivalent cron schedule.":r.length!==5?"EventBridge six-field cron extensions cannot be installed as server cron.":"Cron is reconciled through a root-owned ts-cloud entry.",e.missedRunPolicy==="catch_up"?"Catch-up is handled by the control plane, not cron.":"Missed runs are skipped by cron."]};if(e.provider==="eventbridge")return{provider:"eventbridge",supported:!0,features:{timezone:!0,flexibleWindow:!0,catchUp:!1,replaceOverlap:!1},notes:["Five-field cron is translated explicitly to EventBridge six-field syntax. Flexible windows are provider-native; overlap and catch-up remain control-plane policies."]};if(e.provider==="lambda")return{provider:"lambda",supported:!0,features:{timezone:!0,flexibleWindow:!0,catchUp:!1,replaceOverlap:!1},notes:["The schedule invokes one immutable Lambda target."]};return{provider:"platform",supported:!0,features:{timezone:!0,flexibleWindow:!0,catchUp:!0,replaceOverlap:!0},notes:["The durable control-plane worker enforces all configured policies."]}}function Pi(e){let t=Hr({...e,provider:"server"});if(!t.supported)throw Error(t.notes.join(" "));let o=Ve(e.expression).normalized.slice(5,-1);return{path:`/etc/cron.d/ts-cloud-${e.id}`,content:`CRON_TZ=${e.timezone}
|
|
78
|
+
${o} root /usr/local/bin/cloud jobs:dispatch ${e.id} --scheduled >/dev/null 2>&1
|
|
79
|
+
`}}function yI(e){let t=Ve(e);if(t.kind==="rate")return e;let r=t.normalized.slice(5,-1).trim().split(/\s+/);if(r.length===6)return t.normalized;let[o,m,h,I,R]=r,A=R==="*"?"?":R.replace(/(^|[,-])0(?=$|[,-])/g,(E,x)=>`${x}1`);return h==="*"&&R!=="*"?`cron(${o} ${m} ? ${I} ${A} *)`:`cron(${o} ${m} ${h} ${I} ? *)`}function ol(e){let t=Hr(e);if(!t.supported)throw Error(t.notes.join(" "));return{Name:`ts-cloud-${e.id}`,ScheduleExpression:yI(e.expression),ScheduleExpressionTimezone:e.timezone,State:e.enabled?"ENABLED":"DISABLED",FlexibleTimeWindow:e.flexibleMinutes?{Mode:"FLEXIBLE",MaximumWindowInMinutes:e.flexibleMinutes}:{Mode:"OFF"},Target:{kind:e.target.kind,functionName:e.target.functionName??null,payloadRefs:e.payloadRefs},RetryPolicy:{MaximumRetryAttempts:e.retryPolicy.maxAttempts,MaximumEventAgeInSeconds:e.timeoutSeconds},DeadLetterRef:e.retryPolicy.deadLetterRef??null}}function AA(e,t){if(!t)return{status:"unavailable",observedState:{message:"Provider observation is unavailable."}};let r=String(t.expression??""),o=t.enabled;return{status:r&&r!==e.normalizedExpression||typeof o==="boolean"&&o!==e.enabled?"drifted":"in_sync",observedState:t}}function am(e){if(e.provider==="server"){let t=Pi(e);return{path:t.path,content:t.content,enabled:e.enabled}}if(e.provider==="eventbridge")return ol(e);throw Error(`No reconciliation adapter is configured for ${e.provider} schedules.`)}function Rp(e,t){if(Array.isArray(e))return Array.isArray(t)&&e.length===t.length&&e.every((r,o)=>Rp(r,t[o]));if(e&&typeof e==="object"){if(!t||typeof t!=="object"||Array.isArray(t))return!1;return Object.entries(e).every(([r,o])=>Rp(o,t[r]))}return e===t}class cm{transport;provider="server";constructor(e){this.transport=e}async observe(e){let t=Pi(e),r=await this.transport.read(t.path);return r==null?void 0:{path:t.path,content:r,enabled:!0}}async apply(e,t){let r=String(t.path??"");if(!e.enabled){await this.transport.removeRootOwned(r);return}await this.transport.writeRootOwned(r,String(t.content??""))}async remove(e){await this.transport.removeRootOwned(Pi(e).path)}matches(e,t){if(e.enabled===!1)return!1;return e.path===t.path&&e.content===t.content}}class dm{transport;provider="eventbridge";constructor(e){this.transport=e}observe(e){return this.transport.get(`ts-cloud-${e.id}`)}apply(e,t){return this.transport.put(t)}remove(e){return this.transport.remove(`ts-cloud-${e.id}`)}matches(e,t){return Rp(e,t)}}class lm{store;constructor(e){this.store=e}async reconcile(e,t,r={}){if(t.provider!==e.provider)return this.store.reconcile(e.id,"unsupported",{provider:t.provider,message:`Adapter ${t.provider} cannot reconcile ${e.provider}.`});try{let o=am(e),m=await t.observe(e);if(m&&t.matches(o,m))return this.store.reconcile(e.id,"in_sync",{provider:t.provider,observed:m});if(r.apply===!1)return this.store.reconcile(e.id,m?"drifted":"unavailable",{provider:t.provider,observed:m??null,applyRequired:!0});if(await t.apply(e,o),!e.enabled)return this.store.reconcile(e.id,"in_sync",{provider:t.provider,observed:null,disabled:!0});let h=await t.observe(e);return this.store.reconcile(e.id,h&&t.matches(o,h)?"in_sync":"drifted",{provider:t.provider,observed:h??null,applied:!0})}catch(o){return this.store.reconcile(e.id,"unavailable",{provider:t.provider,error:o instanceof Error?o.message:String(o)})}}async remove(e,t){if(e.origin==="config")throw Error("Config-defined jobs must be removed from configuration, not destructively deleted.");if(t.provider!==e.provider)throw Error(`Adapter ${t.provider} cannot remove ${e.provider}.`);await t.remove(e),this.store.remove(e.id)}}function vI(e){if(e.queues===!0)return[{queue:"default",processes:e.queueConcurrency??1}];if(!Array.isArray(e.queues))return[];return e.queues.map((t)=>{if(typeof t==="string")return{queue:t,processes:e.queueConcurrency??1};let[r,o]=Object.entries(t)[0]??["default",e.queueConcurrency??1];return{queue:r,processes:o}})}function Do(e,t,r){let o=new Set,m=new Set,h=new Map(r.resources.map((N)=>[N.slug,N])),I=0,R=0;for(let[N,F]of Object.entries(t.sites??{})){let z=h.get(N);if(F.scheduler){let re=`config:site:${N}:scheduler`;o.add(re),e.upsertConfigJob({organizationId:r.organization.id,projectId:r.project.id,environmentId:r.environment.id,resourceId:z?.id,name:`${N} scheduler`,provider:"server",expression:"* * * * *",timezone:"UTC",flexibleMinutes:0,target:{kind:"dashboard_operation",operationId:`scheduler:run:${N}`},payloadRefs:{},missedRunPolicy:"skip",overlapPolicy:"forbid",retryPolicy:{maxAttempts:3,backoffSeconds:30},timeoutSeconds:300,enabled:!0,origin:"config",sourceKey:re,observedState:{source:"cloud.config.ts"},reconciliationStatus:"pending"}),I++}let K=F,J=Array.isArray(K.queues??K.workers)?K.queues??K.workers:[];for(let[re,se]of J.entries()){let ye=typeof se==="string"?{queue:se}:se,ne=String(ye.queue??ye.name??"default"),fe=`config:site:${N}:worker:${re}:${ne}`;m.add(fe),e.upsertWorker({organizationId:r.organization.id,projectId:r.project.id,environmentId:r.environment.id,resourceId:z?.id,name:`${N}:${ne}`,provider:"systemd",queue:ne,processes:Number(ye.processes)||1,timeoutSeconds:Number(ye.timeout)||60,restartPolicy:"always",target:{operationId:`worker:restart:${N}`,site:N},enabled:!0,origin:"config",sourceKey:fe,observedState:{source:"cloud.config.ts",currentJob:null,failures:0},reconciliationStatus:"pending"}),R++}}let A=t.infrastructure?.compute?.backups;if(A?.enabled)o.add("config:platform:backup"),e.upsertConfigJob({organizationId:r.organization.id,projectId:r.project.id,environmentId:r.environment.id,name:"Scheduled backup",provider:"server",expression:String(A.schedule??"0 2 * * *"),timezone:String(A.timezone??"UTC"),flexibleMinutes:0,target:{kind:"dashboard_operation",operationId:"backup:run"},payloadRefs:{},missedRunPolicy:"skip",overlapPolicy:"forbid",retryPolicy:{maxAttempts:3,backoffSeconds:60},timeoutSeconds:3600,enabled:!0,origin:"config",sourceKey:"config:platform:backup",observedState:{source:"cloud.config.ts"},reconciliationStatus:"pending"}),I++;let E=t.environments?.[r.environment.slug]?.app;if(E){if(E.scheduler!=="off"){let N=`config:serverless:${r.environment.slug}:scheduler`;o.add(N),e.upsertConfigJob({organizationId:r.organization.id,projectId:r.project.id,environmentId:r.environment.id,name:`${r.environment.name} serverless scheduler`,provider:"eventbridge",expression:"rate(1 minute)",timezone:"UTC",flexibleMinutes:0,target:{kind:"serverless_scheduler",action:"schedule:run"},payloadRefs:{},missedRunPolicy:E.scheduler==="sub-minute"?"catch_up":"skip",overlapPolicy:"forbid",retryPolicy:{maxAttempts:3,backoffSeconds:30},timeoutSeconds:E.cliTimeout??900,enabled:!0,origin:"config",sourceKey:N,observedState:{source:"cloud.config.ts",mode:E.scheduler??"on"},reconciliationStatus:"pending"}),I++}for(let{queue:N,processes:F}of vI(E)){let z=`config:serverless:${r.environment.slug}:worker:${N}`;m.add(z),e.upsertWorker({organizationId:r.organization.id,projectId:r.project.id,environmentId:r.environment.id,name:`${r.environment.name}:${N}`,provider:"lambda",queue:N,processes:F,timeoutSeconds:E.queueTimeout??120,restartPolicy:"on_failure",target:{function:"queue",queue:N},enabled:!0,origin:"config",sourceKey:z,observedState:{source:"cloud.config.ts",currentJob:null,failures:0},reconciliationStatus:"pending"}),R++}}let x=0;for(let N of e.list(r.project.id,{environmentId:r.environment.id}).filter((F)=>F.origin==="config"&&F.sourceKey&&!o.has(F.sourceKey)))e.reconcile(N.id,"drifted",{...N.observedState,reason:"Definition is no longer present in configuration; retained for non-destructive review."}),x++;for(let N of e.listWorkers(r.project.id,r.environment.id).filter((F)=>F.origin==="config"&&F.sourceKey&&!m.has(F.sourceKey)))e.reconcileWorker(N.id,"drifted",{...N.observedState,reason:"Definition is no longer present in configuration; retained for non-destructive review."}),x++;return{jobs:I,workers:R,drifted:x}}class Po{store;queue;constructor(e,t={}){this.store=e;this.queue=t.queue??new c(e.controlPlane)}enqueue(e,t){let r=new Date(t.scheduledFor??this.store.now()).toISOString(),o=`${e.id}:${t.trigger==="manual"?crypto.randomUUID():r}`,m=this.store.activeExecution(e.id);if(m&&e.overlapPolicy==="forbid")return this.store.createExecution({jobId:e.id,trigger:t.trigger,scheduledFor:r,idempotencyKey:o,status:"skipped",output:{reason:"overlap_forbidden",activeExecutionId:m.id}});if(m&&e.overlapPolicy==="replace"&&m.operationId)try{this.queue.requestCancellation(m.operationId,t.actorId)}catch{}let h=this.store.createExecution({jobId:e.id,trigger:t.trigger,scheduledFor:r,idempotencyKey:o,status:"queued",output:{semantics:"at_least_once"}});if(h.operationId)return h;let I=this.queue.enqueue({projectId:e.projectId,environmentId:e.environmentId,resourceId:e.resourceId,actorId:t.actorId,kind:"job.execute",input:{jobId:e.id,executionId:h.id},idempotencyKey:`job-execution:${h.id}`,lockKey:e.overlapPolicy==="allow"?`job-execution:${h.id}`:`job:${e.id}`,maxAttempts:e.retryPolicy.maxAttempts,timeoutSeconds:e.timeoutSeconds,retryClasses:["job_transient"],resumePolicy:"requeue"}).operation;return this.store.attachOperation(h.id,I.id)}tick(e=this.store.now()){let t=[];for(let r of this.store.due(e.toISOString())){let o=r.nextRunAt,m=gt(r.normalizedExpression,r.timezone,e,1)[0];if(r.missedRunPolicy==="catch_up"){let h=o,I=0;while(h<=e.toISOString()&&I<10)t.push(this.enqueue(r,{trigger:h===o?"scheduled":"catch_up",scheduledFor:h})),h=gt(r.normalizedExpression,r.timezone,new Date(h),1)[0],I++}else t.push(this.enqueue(r,{trigger:"scheduled",scheduledFor:o}));this.store.markScheduled(r.id,o,m)}return t}}function al(e){return{"job.execute":async(r)=>{let o=r.operation.input&&typeof r.operation.input==="object"&&!Array.isArray(r.operation.input)?r.operation.input:{},m=e.store.get(String(o.jobId??"")),h=e.store.getExecution(String(o.executionId??""));if(!m||!h)throw Error("Scheduled job execution is no longer available.");e.store.transitionExecution(h.id,"running",{attempt:r.operation.attempt}),r.checkpoint("execute",`Running ${m.name} on ${m.provider}.`),r.log(`Trigger ${h.trigger}; scheduled for ${h.scheduledFor}; semantics at-least-once.`,{stream:"system"});let I=await e.executor(m,m.target,r);if(I.stdout)r.log(I.stdout,{stream:"stdout"});if(I.stderr)r.log(I.stderr,{stream:"stderr"});if(!I.ok){if(e.store.transitionExecution(h.id,r.operation.attempt>=m.retryPolicy.maxAttempts?"dead":"failed",{attempt:r.operation.attempt,error:I.stderr??"Job execution failed."}),I.retryable)throw new W(I.stderr??"Scheduled job failed transiently.","job_transient");throw Error(I.stderr??"Scheduled job failed.")}return e.store.transitionExecution(h.id,"succeeded",{attempt:r.operation.attempt,output:I.output??{}}),{jobId:m.id,executionId:h.id,provider:m.provider}}}}var pn=(e)=>typeof e==="string"&&e?e:void 0,_h=(e)=>Number(e)===1,Os=(e)=>{try{return JSON.parse(String(e))}catch{return{}}},ss=(e,t,r,o)=>{let m=Number(e);return Number.isFinite(m)?Math.min(o,Math.max(r,Math.floor(m))):t};function Jc(e){return{id:String(e.id),organizationId:String(e.organization_id),projectId:String(e.project_id),environmentId:pn(e.environment_id),resourceId:pn(e.resource_id),name:String(e.name),provider:String(e.provider),expression:String(e.expression),normalizedExpression:String(e.normalized_expression),timezone:String(e.timezone),startsAt:pn(e.starts_at),endsAt:pn(e.ends_at),flexibleMinutes:Number(e.flexible_minutes),target:Os(e.target),payloadRefs:Os(e.payload_refs),missedRunPolicy:String(e.missed_run_policy),overlapPolicy:String(e.overlap_policy),retryPolicy:Os(e.retry_policy),timeoutSeconds:Number(e.timeout_seconds),enabled:_h(e.enabled),origin:String(e.origin),sourceKey:pn(e.source_key),ownerActorId:pn(e.owner_actor_id),observedState:Os(e.observed_state),reconciliationStatus:String(e.reconciliation_status),nextRunAt:pn(e.next_run_at),lastScheduledFor:pn(e.last_scheduled_for),version:Number(e.version),createdAt:String(e.created_at),updatedAt:String(e.updated_at)}}function Qc(e){return{id:String(e.id),jobId:String(e.job_id),operationId:pn(e.operation_id),trigger:String(e.trigger),scheduledFor:String(e.scheduled_for),idempotencyKey:String(e.idempotency_key),status:String(e.status),attempt:Number(e.attempt),startedAt:pn(e.started_at),finishedAt:pn(e.finished_at),output:Os(e.output),error:pn(e.error),createdAt:String(e.created_at),updatedAt:String(e.updated_at)}}function Ap(e){return{id:String(e.id),organizationId:String(e.organization_id),projectId:String(e.project_id),environmentId:pn(e.environment_id),resourceId:pn(e.resource_id),name:String(e.name),provider:String(e.provider),queue:String(e.queue),processes:Number(e.processes),timeoutSeconds:Number(e.timeout_seconds),restartPolicy:String(e.restart_policy),target:Os(e.target),enabled:_h(e.enabled),origin:String(e.origin),sourceKey:pn(e.source_key),ownerActorId:pn(e.owner_actor_id),observedState:Os(e.observed_state),reconciliationStatus:String(e.reconciliation_status),version:Number(e.version),createdAt:String(e.created_at),updatedAt:String(e.updated_at)}}class Ro{controlPlane;options;constructor(e,t={}){this.controlPlane=e;this.options=t}now(){return this.options.now?.()??new Date}create(e){if(!e.name.trim())throw Error("Scheduled jobs require a name.");let t=mt(e.expression,e.timezone,this.now(),1),r=crypto.randomUUID(),o=this.now().toISOString(),m=e.startsAt?new Date(e.startsAt).toISOString():void 0,h=e.endsAt?new Date(e.endsAt).toISOString():void 0;if(m&&h&&m>=h)throw Error("Schedule start must be before end.");let I=t.nextRuns.find((R)=>(!m||R>=m)&&(!h||R<=h));return this.controlPlane.database.run("INSERT INTO scheduled_jobs (id,organization_id,project_id,environment_id,resource_id,name,provider,expression,normalized_expression,timezone,starts_at,ends_at,flexible_minutes,target,payload_refs,missed_run_policy,overlap_policy,retry_policy,timeout_seconds,enabled,origin,source_key,owner_actor_id,observed_state,reconciliation_status,next_run_at,version,created_at,updated_at) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",[r,e.organizationId,e.projectId,e.environmentId??null,e.resourceId??null,e.name.trim().slice(0,120),e.provider,e.expression,t.normalized,e.timezone,m??null,h??null,ss(e.flexibleMinutes,0,0,1440),JSON.stringify(e.target),JSON.stringify(e.payloadRefs),e.missedRunPolicy,e.overlapPolicy,JSON.stringify({maxAttempts:ss(e.retryPolicy.maxAttempts,3,1,20),backoffSeconds:ss(e.retryPolicy.backoffSeconds,30,0,86400),deadLetterRef:e.retryPolicy.deadLetterRef}),ss(e.timeoutSeconds,900,1,86400),e.enabled?1:0,e.origin,e.sourceKey??null,e.ownerActorId??null,JSON.stringify(e.observedState),e.reconciliationStatus,I??null,1,o,o]),this.get(r)}get(e){let t=this.controlPlane.database.query("SELECT * FROM scheduled_jobs WHERE id=?").get(e);return t?Jc(t):void 0}getBySource(e,t,r){let o=this.controlPlane.database.query("SELECT * FROM scheduled_jobs WHERE project_id=? AND environment_id IS ? AND source_key=?").get(e,t??null,r);return o?Jc(o):void 0}list(e,t={}){let r=["project_id=?"],o=[e];if(t.environmentId)r.push("environment_id=?"),o.push(t.environmentId);if(t.resourceIds?.length)r.push(`resource_id IN (${t.resourceIds.map(()=>"?").join(",")})`),o.push(...t.resourceIds);if(t.enabled!=null)r.push("enabled=?"),o.push(t.enabled?1:0);return this.controlPlane.database.query(`SELECT * FROM scheduled_jobs WHERE ${r.join(" AND ")} ORDER BY next_run_at IS NULL,next_run_at,name`).all(...o).map(Jc)}setEnabled(e,t){this.controlPlane.database.run("UPDATE scheduled_jobs SET enabled=?,version=version+1,updated_at=? WHERE id=?",[t?1:0,this.now().toISOString(),e]);let r=this.get(e);if(!r)throw Error("Scheduled job was not found.");return r}updateSchedule(e,t){let r=this.get(e);if(!r)throw Error("Scheduled job was not found.");let o=t.expression??r.expression,m=t.timezone??r.timezone,h=mt(o,m,this.now(),1),I=this.now().toISOString();return this.controlPlane.database.run("UPDATE scheduled_jobs SET expression=?,normalized_expression=?,timezone=?,missed_run_policy=?,overlap_policy=?,timeout_seconds=?,next_run_at=?,reconciliation_status=?,version=version+1,updated_at=? WHERE id=?",[o,h.normalized,m,t.missedRunPolicy??r.missedRunPolicy,t.overlapPolicy??r.overlapPolicy,ss(t.timeoutSeconds,r.timeoutSeconds,1,86400),h.nextRuns[0],"pending",I,e]),this.get(e)}remove(e){let t=this.get(e);if(!t)throw Error("Scheduled job was not found.");if(t.origin==="config")throw Error("Config-defined jobs must be removed from configuration, not destructively deleted.");this.controlPlane.database.run("DELETE FROM scheduled_jobs WHERE id=?",[e])}markScheduled(e,t,r){this.controlPlane.database.run("UPDATE scheduled_jobs SET last_scheduled_for=?,next_run_at=?,updated_at=? WHERE id=?",[t,r??null,this.now().toISOString(),e])}reconcile(e,t,r){return this.controlPlane.database.run("UPDATE scheduled_jobs SET reconciliation_status=?,observed_state=?,updated_at=? WHERE id=?",[t,JSON.stringify(r),this.now().toISOString(),e]),this.get(e)}upsertConfigJob(e){let t=this.getBySource(e.projectId,e.environmentId,e.sourceKey);if(!t)return this.create(e);let r=mt(e.expression,e.timezone,this.now(),1),o=this.now().toISOString();if(t.resourceId===e.resourceId&&t.name===e.name&&t.provider===e.provider&&t.expression===e.expression&&t.normalizedExpression===r.normalized&&t.timezone===e.timezone&&JSON.stringify(t.target)===JSON.stringify(e.target)&&JSON.stringify(t.payloadRefs)===JSON.stringify(e.payloadRefs)&&t.missedRunPolicy===e.missedRunPolicy&&t.overlapPolicy===e.overlapPolicy&&JSON.stringify(t.retryPolicy)===JSON.stringify(e.retryPolicy)&&t.timeoutSeconds===e.timeoutSeconds&&t.enabled===e.enabled)return t;return this.controlPlane.database.run("UPDATE scheduled_jobs SET resource_id=?,name=?,provider=?,expression=?,normalized_expression=?,timezone=?,target=?,payload_refs=?,missed_run_policy=?,overlap_policy=?,retry_policy=?,timeout_seconds=?,enabled=?,observed_state=?,reconciliation_status=?,next_run_at=COALESCE(next_run_at,?),version=version+1,updated_at=? WHERE id=?",[e.resourceId??null,e.name,e.provider,e.expression,r.normalized,e.timezone,JSON.stringify(e.target),JSON.stringify(e.payloadRefs),e.missedRunPolicy,e.overlapPolicy,JSON.stringify(e.retryPolicy),e.timeoutSeconds,e.enabled?1:0,JSON.stringify(e.observedState),e.reconciliationStatus,r.nextRuns[0],o,t.id]),this.get(t.id)}due(e=this.now().toISOString()){return this.controlPlane.database.query("SELECT * FROM scheduled_jobs WHERE enabled=1 AND next_run_at IS NOT NULL AND next_run_at<=? AND (starts_at IS NULL OR starts_at<=?) AND (ends_at IS NULL OR ends_at>=?) ORDER BY next_run_at LIMIT 200").all(e,e,e).map(Jc)}createExecution(e){let t=this.controlPlane.database.query("SELECT * FROM job_executions WHERE idempotency_key=?").get(e.idempotencyKey);if(t)return Qc(t);let r=crypto.randomUUID(),o=this.now().toISOString();return this.controlPlane.database.run("INSERT INTO job_executions (id,job_id,operation_id,trigger,scheduled_for,idempotency_key,status,attempt,output,created_at,updated_at) VALUES (?,?,?,?,?,?,?,?,?,?,?)",[r,e.jobId,e.operationId??null,e.trigger,e.scheduledFor,e.idempotencyKey,e.status,0,JSON.stringify(e.output),o,o]),this.getExecution(r)}getExecution(e){let t=this.controlPlane.database.query("SELECT * FROM job_executions WHERE id=?").get(e);return t?Qc(t):void 0}listExecutions(e,t=100){return this.controlPlane.database.query("SELECT * FROM job_executions WHERE job_id=? ORDER BY scheduled_for DESC,created_at DESC LIMIT ?").all(e,Math.min(1000,Math.max(1,t))).map(Qc)}activeExecution(e){let t=this.controlPlane.database.query("SELECT * FROM job_executions WHERE job_id=? AND status IN ('queued','running') ORDER BY created_at DESC LIMIT 1").get(e);return t?Qc(t):void 0}attachOperation(e,t){return this.controlPlane.database.run("UPDATE job_executions SET operation_id=?,updated_at=? WHERE id=?",[t,this.now().toISOString(),e]),this.getExecution(e)}transitionExecution(e,t,r={}){let o=this.now().toISOString(),m=t==="running"?o:null,h=["succeeded","failed","skipped","dead"].includes(t)?o:null;return this.controlPlane.database.run("UPDATE job_executions SET status=?,attempt=COALESCE(?,attempt),started_at=COALESCE(started_at,?),finished_at=?,output=COALESCE(?,output),error=?,updated_at=? WHERE id=?",[t,r.attempt??null,m,h,r.output?JSON.stringify(r.output):null,r.error?.slice(0,2000)??null,o,e]),this.getExecution(e)}upsertWorker(e){let t=e.sourceKey?this.controlPlane.database.query("SELECT * FROM worker_definitions WHERE project_id=? AND source_key=? AND environment_id IS ?").get(e.projectId,e.sourceKey,e.environmentId??null):void 0,r=t?Ap(t):void 0,o=r?.id??crypto.randomUUID(),m=this.now().toISOString(),h=ss(e.processes,1,0,1000),I=ss(e.timeoutSeconds,60,1,86400);if(r&&r.resourceId===e.resourceId&&r.name===e.name.slice(0,120)&&r.provider===e.provider&&r.queue===e.queue.slice(0,200)&&r.processes===h&&r.timeoutSeconds===I&&r.restartPolicy===e.restartPolicy&&JSON.stringify(r.target)===JSON.stringify(e.target)&&r.enabled===e.enabled)return r;return this.controlPlane.database.run("INSERT INTO worker_definitions (id,organization_id,project_id,environment_id,resource_id,name,provider,queue,processes,timeout_seconds,restart_policy,target,enabled,origin,source_key,owner_actor_id,observed_state,reconciliation_status,version,created_at,updated_at) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ON CONFLICT(id) DO UPDATE SET name=excluded.name,resource_id=excluded.resource_id,provider=excluded.provider,queue=excluded.queue,processes=excluded.processes,timeout_seconds=excluded.timeout_seconds,restart_policy=excluded.restart_policy,target=excluded.target,enabled=excluded.enabled,observed_state=excluded.observed_state,reconciliation_status=excluded.reconciliation_status,version=worker_definitions.version+1,updated_at=excluded.updated_at",[o,e.organizationId,e.projectId,e.environmentId??null,e.resourceId??null,e.name.slice(0,120),e.provider,e.queue.slice(0,200),h,I,e.restartPolicy,JSON.stringify(e.target),e.enabled?1:0,e.origin,e.sourceKey??null,e.ownerActorId??null,JSON.stringify(e.observedState),e.reconciliationStatus,1,r?.createdAt??m,m]),this.getWorker(o)}getWorker(e){let t=this.controlPlane.database.query("SELECT * FROM worker_definitions WHERE id=?").get(e);return t?Ap(t):void 0}listWorkers(e,t){return this.controlPlane.database.query(`SELECT * FROM worker_definitions WHERE project_id=?${t?" AND environment_id=?":""} ORDER BY name`).all(e,...t?[t]:[]).map(Ap)}setWorkerProcesses(e,t){this.controlPlane.database.run("UPDATE worker_definitions SET processes=?,reconciliation_status='pending',version=version+1,updated_at=? WHERE id=?",[ss(t,1,0,1000),this.now().toISOString(),e]);let r=this.getWorker(e);if(!r)throw Error("Worker was not found.");return r}reconcileWorker(e,t,r){this.controlPlane.database.run("UPDATE worker_definitions SET reconciliation_status=?,observed_state=?,updated_at=? WHERE id=?",[t,JSON.stringify(r),this.now().toISOString(),e]);let o=this.getWorker(e);if(!o)throw Error("Worker was not found.");return o}}var _t=(e)=>e==null?void 0:String(e),Yc=(e)=>Number(e)===1,as=(e,t)=>{try{return JSON.parse(String(e))}catch{return t}};function xh(e){return{id:String(e.id),organizationId:String(e.organization_id),projectId:String(e.project_id),name:String(e.name),provider:String(e.provider),endpoint:_t(e.endpoint),endpointPolicy:String(e.endpoint_policy),bucket:_t(e.bucket),prefix:String(e.prefix),region:_t(e.region),forcePathStyle:Yc(e.force_path_style),credentialRef:_t(e.credential_ref),encryption:String(e.encryption),encryptionKeyRef:_t(e.encryption_key_ref),immutability:as(e.immutability,{}),status:String(e.status),lastTestedAt:_t(e.last_tested_at),lastSuccessAt:_t(e.last_success_at),lastFailureAt:_t(e.last_failure_at),lastError:_t(e.last_error),version:Number(e.version),createdAt:String(e.created_at),updatedAt:String(e.updated_at)}}function Cp(e){return{id:String(e.id),organizationId:String(e.organization_id),projectId:String(e.project_id),environmentId:_t(e.environment_id),resourceId:_t(e.resource_id),dataServiceId:_t(e.data_service_id),destinationId:String(e.destination_id),name:String(e.name),resourceKind:String(e.resource_kind),schedule:String(e.schedule),timezone:String(e.timezone),retention:as(e.retention,{}),compression:String(e.compression),encryption:String(e.encryption),includePatterns:as(e.include_patterns,[]),excludePatterns:as(e.exclude_patterns,[]),expectedRpoMinutes:Number(e.expected_rpo_minutes),expectedRtoMinutes:Number(e.expected_rto_minutes),healthCheckId:_t(e.health_check_id),enabled:Yc(e.enabled),nextRunAt:_t(e.next_run_at),lastRunAt:_t(e.last_run_at),version:Number(e.version),createdAt:String(e.created_at),updatedAt:String(e.updated_at)}}function Pp(e){return{id:String(e.id),projectId:String(e.project_id),policyId:_t(e.policy_id),destinationId:String(e.destination_id),resourceId:_t(e.resource_id),dataServiceId:_t(e.data_service_id),backupJobId:_t(e.backup_job_id),kind:String(e.kind),pointInTime:String(e.point_in_time),uri:String(e.uri),sizeBytes:Number(e.size_bytes),checksum:String(e.checksum),manifest:as(e.manifest,{}),toolVersion:_t(e.tool_version),engineVersion:_t(e.engine_version),expiresAt:_t(e.expires_at),lockedUntil:_t(e.locked_until),held:Yc(e.held),pinned:Yc(e.pinned),status:String(e.status),verificationState:String(e.verification_state),verifiedAt:_t(e.verified_at),durationMs:e.duration_ms==null?void 0:Number(e.duration_ms),createdAt:String(e.created_at),updatedAt:String(e.updated_at)}}function _p(e){return{id:String(e.id),projectId:String(e.project_id),policyId:_t(e.policy_id),recoveryPointId:_t(e.recovery_point_id),operationId:_t(e.operation_id),kind:String(e.kind),status:String(e.status),idempotencyKey:String(e.idempotency_key),target:as(e.target,{}),restoreMode:_t(e.restore_mode),cancellability:String(e.cancellability),safetyBackupId:_t(e.safety_backup_id),healthResult:e.health_result?as(e.health_result,{}):void 0,progress:as(e.progress,{}),error:_t(e.error),startedAt:_t(e.started_at),finishedAt:_t(e.finished_at),createdAt:String(e.created_at),updatedAt:String(e.updated_at)}}function bI(e){let t=e.toLowerCase();if(t==="localhost"||t.endsWith(".localhost")||t.endsWith(".local")||t.endsWith(".internal"))return!0;if(Th(t)===6)return t==="::1"||t.startsWith("fe80:");if(Th(t)!==4)return!1;let[r,o]=t.split(".").map(Number);return r===10||r===127||r===0||r===169&&o===254||r===172&&o>=16&&o<=31||r===192&&o===168}function Em(e){if(!/^[a-z0-9][a-z0-9-]{1,62}$/.test(e.name))throw Error("Backup destination names must be 2-63 lowercase letters, numbers, or dashes.");if(e.provider!=="aws_backup"&&!e.bucket)throw Error("S3 backup destinations require a bucket.");if(e.provider==="s3_compatible"&&!e.endpoint)throw Error("S3-compatible destinations require an explicit endpoint.");if(e.provider==="s3_compatible"&&!e.credentialRef)throw Error("S3-compatible destinations require a credential reference.");if(e.encryption!=="provider"&&!e.encryptionKeyRef)throw Error("Client-side encryption requires an encryption key reference.");if(e.endpoint){let t=new URL(e.endpoint);if(t.protocol!=="https:"||t.username||t.password||t.search||t.hash||t.pathname!=="/")throw Error("Backup endpoints must be origin-only HTTPS URLs without credentials, paths, queries, or fragments.");if(bI(t.hostname)&&e.endpointPolicy!=="allow_private")throw Error("Private backup endpoints require endpointPolicy allow_private.")}}class Zo{controlPlane;now;constructor(e,t=()=>new Date){this.controlPlane=e;this.now=t}createDestination(e){Em(e);let t=crypto.randomUUID(),r=this.now().toISOString();return this.controlPlane.database.run("INSERT INTO backup_destinations (id,organization_id,project_id,name,provider,endpoint,endpoint_policy,bucket,prefix,region,force_path_style,credential_ref,encryption,encryption_key_ref,immutability,status,last_tested_at,last_success_at,last_failure_at,last_error,version,created_at,updated_at) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",[t,e.organizationId,e.projectId,e.name,e.provider,e.endpoint??null,e.endpointPolicy,e.bucket??null,e.prefix,e.region??null,e.forcePathStyle?1:0,e.credentialRef??null,e.encryption,e.encryptionKeyRef??null,JSON.stringify(e.immutability),e.status,e.lastTestedAt??null,e.lastSuccessAt??null,e.lastFailureAt??null,e.lastError??null,1,r,r]),this.getDestination(t)}getDestination(e){let t=this.controlPlane.database.query("SELECT * FROM backup_destinations WHERE id=?").get(e);return t?xh(t):void 0}listDestinations(e){return this.controlPlane.database.query("SELECT * FROM backup_destinations WHERE project_id=? ORDER BY name").all(e).map(xh)}recordDestinationTest(e,t){let r=this.now().toISOString();return this.controlPlane.database.run("UPDATE backup_destinations SET status=?,last_tested_at=?,last_success_at=CASE WHEN ? THEN ? ELSE last_success_at END,last_failure_at=CASE WHEN ? THEN last_failure_at ELSE ? END,last_error=?,version=version+1,updated_at=? WHERE id=?",[t.ok?"healthy":"failing",r,t.ok?1:0,r,t.ok?1:0,r,t.ok?null:String(t.error??"Destination test failed.").slice(0,1000),r,e]),this.getDestination(e)}createPolicy(e){if(!/^[a-z0-9][a-z0-9-]{1,62}$/.test(e.name))throw Error("Backup policy names must be lowercase slugs.");if(!e.resourceId&&!e.dataServiceId&&!["control_plane","infrastructure"].includes(e.resourceKind))throw Error("A backup policy requires a resource or data service target.");let t=this.getDestination(e.destinationId);if(!t||t.projectId!==e.projectId)throw Error("Backup destination was not found.");if(e.resourceKind==="control_plane"&&t.encryption==="provider")throw Error("Control-plane backups require client-side destination encryption.");if(["logical_database","volume","files","control_plane"].includes(e.resourceKind)&&t.provider==="aws_backup")throw Error(`${e.resourceKind} backups require an object-storage destination.`);if(e.resourceKind==="infrastructure"&&t.provider!=="aws_backup")throw Error("Infrastructure backups require an AWS Backup destination.");let r=Ve(e.schedule),o=gt(r.normalized,e.timezone,this.now(),1)[0],m=crypto.randomUUID(),h=this.now().toISOString();return this.controlPlane.database.run("INSERT INTO backup_policies (id,organization_id,project_id,environment_id,resource_id,data_service_id,destination_id,name,resource_kind,schedule,timezone,retention,compression,encryption,include_patterns,exclude_patterns,expected_rpo_minutes,expected_rto_minutes,health_check_id,enabled,next_run_at,last_run_at,version,created_at,updated_at) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",[m,e.organizationId,e.projectId,e.environmentId??null,e.resourceId??null,e.dataServiceId??null,e.destinationId,e.name,e.resourceKind,r.normalized,e.timezone,JSON.stringify(e.retention),e.compression,e.encryption,JSON.stringify(e.includePatterns),JSON.stringify(e.excludePatterns),e.expectedRpoMinutes,e.expectedRtoMinutes,e.healthCheckId??null,e.enabled?1:0,o,e.lastRunAt??null,1,h,h]),this.getPolicy(m)}getPolicy(e){let t=this.controlPlane.database.query("SELECT * FROM backup_policies WHERE id=?").get(e);return t?Cp(t):void 0}listPolicies(e,t){return this.controlPlane.database.query(`SELECT * FROM backup_policies WHERE project_id=?${t?" AND environment_id=?":""} ORDER BY name`).all(e,...t?[t]:[]).map(Cp)}duePolicies(e=this.now()){return this.controlPlane.database.query("SELECT * FROM backup_policies WHERE enabled=1 AND next_run_at<=? ORDER BY next_run_at,id").all(e.toISOString()).map(Cp)}advancePolicy(e,t){let r=this.getPolicy(e);if(!r)throw Error("Backup policy was not found.");let o=gt(r.schedule,r.timezone,new Date(t),1)[0];return this.controlPlane.database.run("UPDATE backup_policies SET last_run_at=?,next_run_at=?,version=version+1,updated_at=? WHERE id=?",[t,o,this.now().toISOString(),e]),this.getPolicy(e)}createJob(e){let t=this.controlPlane.database.query("SELECT * FROM backup_jobs WHERE idempotency_key=?").get(e.idempotencyKey);if(t)return _p(t);let r=crypto.randomUUID(),o=this.now().toISOString();return this.controlPlane.database.run("INSERT INTO backup_jobs (id,project_id,policy_id,recovery_point_id,operation_id,kind,status,idempotency_key,target,restore_mode,cancellability,safety_backup_id,health_result,progress,error,started_at,finished_at,created_at,updated_at) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",[r,e.projectId,e.policyId??null,e.recoveryPointId??null,e.operationId??null,e.kind,e.status,e.idempotencyKey,JSON.stringify(e.target),e.restoreMode??null,e.cancellability,e.safetyBackupId??null,e.healthResult?JSON.stringify(e.healthResult):null,JSON.stringify(e.progress),e.error??null,e.startedAt??null,e.finishedAt??null,o,o]),this.getJob(r)}getJob(e){let t=this.controlPlane.database.query("SELECT * FROM backup_jobs WHERE id=?").get(e);return t?_p(t):void 0}listJobs(e){return this.controlPlane.database.query("SELECT * FROM backup_jobs WHERE project_id=? ORDER BY created_at DESC").all(e).map(_p)}updateJob(e,t){let r=this.getJob(e);if(!r)throw Error("Backup job was not found.");let o={...r,...t};return this.controlPlane.database.run("UPDATE backup_jobs SET status=?,operation_id=?,recovery_point_id=?,progress=?,health_result=?,error=?,started_at=?,finished_at=?,safety_backup_id=?,updated_at=? WHERE id=?",[o.status,o.operationId??null,o.recoveryPointId??null,JSON.stringify(o.progress),o.healthResult?JSON.stringify(o.healthResult):null,o.error??null,o.startedAt??null,o.finishedAt??null,o.safetyBackupId??null,this.now().toISOString(),e]),this.getJob(e)}createRecoveryPoint(e){if(!/^sha256:[a-f0-9]{64}$/i.test(e.checksum))throw Error("Recovery points require a SHA-256 checksum.");if(typeof e.uri!=="string"||!["s3:","aws-backup:","file:"].some((o)=>e.uri.startsWith(o)))throw Error("Recovery point URI uses an unsupported destination scheme.");let t=crypto.randomUUID(),r=this.now().toISOString();return this.controlPlane.database.run("INSERT INTO recovery_points (id,project_id,policy_id,destination_id,resource_id,data_service_id,backup_job_id,kind,point_in_time,uri,size_bytes,checksum,manifest,tool_version,engine_version,expires_at,locked_until,held,pinned,status,verification_state,verified_at,duration_ms,created_at,updated_at) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",[t,e.projectId,e.policyId??null,e.destinationId,e.resourceId??null,e.dataServiceId??null,e.backupJobId??null,e.kind,e.pointInTime,e.uri,e.sizeBytes,e.checksum,JSON.stringify(e.manifest),e.toolVersion??null,e.engineVersion??null,e.expiresAt??null,e.lockedUntil??null,e.held?1:0,e.pinned?1:0,e.status,e.verificationState,e.verifiedAt??null,e.durationMs??null,r,r]),this.getRecoveryPoint(t)}getRecoveryPoint(e){let t=this.controlPlane.database.query("SELECT * FROM recovery_points WHERE id=?").get(e);return t?Pp(t):void 0}listRecoveryPoints(e,t){return this.controlPlane.database.query(`SELECT * FROM recovery_points WHERE project_id=?${t?" AND policy_id=?":""} ORDER BY point_in_time DESC`).all(e,...t?[t]:[]).map(Pp)}updateRecoveryPoint(e,t){let r=this.getRecoveryPoint(e);if(!r)throw Error("Recovery point was not found.");let o={...r,...t};return this.controlPlane.database.run("UPDATE recovery_points SET status=?,verification_state=?,verified_at=?,held=?,pinned=?,manifest=?,duration_ms=?,updated_at=? WHERE id=?",[o.status,o.verificationState,o.verifiedAt??null,o.held?1:0,o.pinned?1:0,JSON.stringify(o.manifest),o.durationMs??null,this.now().toISOString(),e]),this.getRecoveryPoint(e)}retentionCandidates(e=this.now()){let t=this.controlPlane.database.query(`SELECT rp.* FROM recovery_points rp
|
|
80
|
+
WHERE rp.status='available' AND rp.held=0 AND rp.pinned=0
|
|
81
|
+
AND rp.expires_at IS NOT NULL AND rp.expires_at<=?
|
|
82
|
+
AND (rp.locked_until IS NULL OR rp.locked_until<=?)
|
|
83
|
+
AND NOT EXISTS (
|
|
84
|
+
SELECT 1 FROM backup_jobs bj WHERE bj.recovery_point_id=rp.id
|
|
85
|
+
AND bj.kind IN ('restore','drill') AND bj.status IN ('queued','running','cleanup_required')
|
|
86
|
+
)
|
|
87
|
+
ORDER BY rp.expires_at,rp.id`).all(e.toISOString(),e.toISOString()).map(Pp),r=new Set;for(let o of new Set(t.map((m)=>m.projectId)))for(let m of this.listPolicies(o)){let h=this.listRecoveryPoints(m.projectId,m.id).filter((R)=>R.status==="available").sort((R,A)=>A.pointInTime.localeCompare(R.pointInTime));for(let R of h.slice(0,Math.max(0,m.retention.keepLast??0)))r.add(R.id);let I=[[m.retention.hourly,(R)=>R.toISOString().slice(0,13)],[m.retention.daily,(R)=>R.toISOString().slice(0,10)],[m.retention.weekly,(R)=>new Date(Date.UTC(R.getUTCFullYear(),R.getUTCMonth(),R.getUTCDate()-(R.getUTCDay()+6)%7)).toISOString().slice(0,10)],[m.retention.monthly,(R)=>R.toISOString().slice(0,7)]];for(let[R,A]of I){let E=new Set;for(let x of h){let N=A(new Date(x.pointInTime));if(E.has(N))continue;if(E.add(N),E.size<=Math.max(0,R??0))r.add(x.id)}}}return t.filter((o)=>!r.has(o.id))}coverage(e,t=this.now()){let r=new Map(this.listDestinations(e).map((o)=>[o.id,o]));return this.listPolicies(e).map((o)=>{let m=this.listRecoveryPoints(e,o.id).filter((R)=>R.status==="available"),h=m[0],I=h?(t.getTime()-new Date(h.pointInTime).getTime())/60000:Number.POSITIVE_INFINITY;return{policy:o,lastRecoveryPoint:h,missedRpo:o.enabled&&I>o.expectedRpoMinutes,unverified:m.filter((R)=>R.verificationState!=="verified").length,destinationHealthy:r.get(o.destinationId)?.status==="healthy"}})}}import{createCipheriv as SI,createDecipheriv as wI,createHash as Dh,randomBytes as Oh}from"node:crypto";var Tp=Buffer.from("TSCB1"),Zc=(e)=>`sha256:${Dh("sha256").update(e).digest("hex")}`;function $h(e){return Dh("sha256").update(e).digest()}function vm(e,t,r){let o=r?Buffer.from(r):Oh(12);if(o.length!==12)throw Error("Backup encryption IV must contain 12 bytes.");let m=SI("aes-256-gcm",$h(t),o),h=Buffer.concat([m.update(e),m.final()]),I=m.getAuthTag();return Buffer.concat([Tp,o,I,h])}function Tm(e,t){let r=Buffer.from(e);if(!r.subarray(0,Tp.length).equals(Tp))throw Error("Backup does not contain a valid encrypted envelope.");let o=r.subarray(5,17),m=r.subarray(17,33),h=r.subarray(33),I=wI("aes-256-gcm",$h(t),o);return I.setAuthTag(m),Buffer.concat([I.update(h),I.final()])}function kI(e){let t;try{t=JSON.parse(e)}catch{throw Error("Backup destination credential must be a JSON object with accessKeyId and secretAccessKey.")}if(!t.accessKeyId||!t.secretAccessKey)throw Error("Backup destination credential requires accessKeyId and secretAccessKey.");if(t.expiresAt){let r=new Date(String(t.expiresAt));if(!Number.isFinite(r.getTime()))throw Error("Backup destination credential expiresAt must be an ISO timestamp.");if(r.getTime()<=Date.now())throw Error("Backup destination credential has expired.")}return{accessKeyId:String(t.accessKeyId),secretAccessKey:String(t.secretAccessKey),sessionToken:t.sessionToken?String(t.sessionToken):void 0}}async function xl(e,t,r=new Date){if(!e.credentialRef)return{status:"default"};try{let o=JSON.parse(await t.resolve(e.credentialRef));if(!o.accessKeyId||!o.secretAccessKey)return{status:"invalid"};if(!o.expiresAt)return{status:"static"};let m=new Date(String(o.expiresAt));if(!Number.isFinite(m.getTime()))return{status:"invalid"};let h=m.getTime()-r.getTime();return{status:h<=0?"expired":h<=604800000?"expiring":"valid",expiresAt:m.toISOString()}}catch{return{status:"invalid"}}}class ea{secrets;clientFactory;multipartThreshold;partSize;constructor(e,t=(m,h)=>new a(m.region??"us-east-1",void 0,{endpoint:m.endpoint?new URL(m.endpoint).host:void 0,forcePathStyle:m.forcePathStyle,credentials:h}),r=8388608,o=8388608){this.secrets=e;this.clientFactory=t;this.multipartThreshold=r;this.partSize=o}async client(e){let t=e.credentialRef?kI(await this.secrets.resolve(e.credentialRef)):void 0;return this.clientFactory(e,t)}async test(e){if(!e.bucket)throw Error("S3 destination has no configured bucket.");let t=await this.client(e),r=`${e.prefix?`${e.prefix.replace(/\/$/,"")}/`:""}.ts-cloud-health/${crypto.randomUUID()}`;await t.putObject({bucket:e.bucket,key:r,body:"ts-cloud backup destination health check",contentType:"text/plain"});try{let o=await t.getObjectBytes(e.bucket,r);if(Buffer.from(o.body).toString()!=="ts-cloud backup destination health check")throw Error("Backup destination health object was corrupted.")}finally{await t.deleteObject(e.bucket,r)}}async upload(e,t){if(!e.bucket)throw Error("S3 destination has no configured bucket.");let r=await this.client(e),o=Zc(t.body),m=e.encryption!=="provider"?await this.secrets.resolve(e.encryptionKeyRef):void 0,h=m?t.resume?.encryptionIv?Buffer.from(t.resume.encryptionIv,"base64url"):Oh(12):void 0,I=m?vm(t.body,m,h):Buffer.from(t.body),R=Zc(I),A=`${e.prefix?`${e.prefix.replace(/\/$/,"")}/`:""}${t.key.replace(/^\/+/,"")}`,E=t.contentType??"application/octet-stream",x={"ts-cloud-format":"v1","ts-cloud-sha256":R.slice(7),"ts-cloud-encrypted":m?"true":"false"};if(t.resume&&t.resume.key!==A)throw Error("Multipart checkpoint does not match the requested backup key.");if(t.resume&&m&&!t.resume.encryptionIv)throw Error("Encrypted multipart checkpoint has no resumable envelope state.");if(t.resume&&m&&t.resume.plaintextChecksum!==o)throw Error("Encrypted multipart checkpoint does not match the backup payload.");if(I.length<this.multipartThreshold)await r.putObject({bucket:e.bucket,key:A,body:I,contentType:E,metadata:x});else{let N=t.resume?void 0:await r.createMultipartUpload(e.bucket,A,{contentType:E,metadata:x}),F=t.resume??{uploadId:N.UploadId,key:A,parts:[],bytesUploaded:0,encryptionIv:h?.toString("base64url"),plaintextChecksum:o},z=new Map(F.parts.map((J)=>[J.PartNumber,J])),K=Math.ceil(I.length/this.partSize);for(let J=0;J<K;J++){let re=J+1;if(!z.has(re)){let se=J*this.partSize,ye=await r.uploadPart(e.bucket,A,F.uploadId,re,I.subarray(se,Math.min(I.length,se+this.partSize)));z.set(re,{PartNumber:re,ETag:ye.ETag})}F.parts=[...z.values()].sort((se,ye)=>se.PartNumber-ye.PartNumber),F.bytesUploaded=Math.min(I.length,re*this.partSize),t.checkpoint?.(F)}await r.completeMultipartUpload(e.bucket,A,F.uploadId,F.parts)}return{uri:`s3://${e.bucket}/${A}`,key:A,sizeBytes:I.length,checksum:R,manifest:{format:"ts-cloud-backup-v1",encrypted:!!m,plaintextChecksum:o,storageChecksum:R,contentType:E}}}async download(e,t){if(!e.bucket)throw Error("S3 destination has no configured bucket.");let r=await this.client(e),o=await r.getObjectBytes(e.bucket,t.key),m=Buffer.from(o.body);if(Zc(m)!==t.checksum)throw Error("Backup checksum verification failed; object is corrupt.");let h=t.manifest.encrypted?Tm(m,await this.secrets.resolve(e.encryptionKeyRef)):m;if(Zc(h)!==t.manifest.plaintextChecksum)throw Error("Backup plaintext checksum verification failed.");return h}async abortPartial(e,t){if(!e.bucket)throw Error("S3 destination has no configured bucket.");await(await this.client(e)).abortMultipartUpload(e.bucket,t.key,t.uploadId)}async delete(e,t){if(!e.bucket)throw Error("S3 destination has no configured bucket.");await(await this.client(e)).deleteObject(e.bucket,t)}}import{createHash as II}from"node:crypto";var EI=(e)=>`sha256:${II("sha256").update(e).digest("hex")}`;class $i{store;queue;now;constructor(e,t,r=()=>new Date){this.store=e;this.queue=t;this.now=r}enqueueBackup(e,t=this.now().toISOString(),r){if(!e.enabled)throw Error("Backup policy is disabled.");let o=this.store.createJob({projectId:e.projectId,policyId:e.id,kind:"backup",status:"queued",idempotencyKey:`backup:${e.id}:${t}`,target:{scheduledFor:t},cancellability:"checkpoint_only",progress:{phase:"queued"}});if(!o.operationId){let m=this.queue.enqueue({projectId:e.projectId,environmentId:e.environmentId,resourceId:e.resourceId,actorId:r,kind:"backup.run",input:{backupJobId:o.id},lockKey:e.dataServiceId?`data-service:${e.dataServiceId}`:e.resourceId?`resource:${e.resourceId}`:`backup-policy:${e.id}`,providerKey:"backup",maxAttempts:3,timeoutSeconds:14400,retryClasses:["provider_transient"],resumePolicy:"requeue"});return this.store.updateJob(o.id,{operationId:m.operation.id})}return o}enqueueDue(e=this.now()){return this.store.duePolicies(e).map((t)=>{let r=t.nextRunAt;return this.store.advancePolicy(t.id,r),this.enqueueBackup(t,r)})}enqueueVerification(e){let t=this.store.createJob({projectId:e.projectId,policyId:e.policyId,recoveryPointId:e.id,kind:"verify",status:"queued",idempotencyKey:`verify:${e.id}:${e.checksum}`,target:{},cancellability:"safe",progress:{phase:"queued"}});if(!t.operationId){let r=this.queue.enqueue({projectId:e.projectId,resourceId:e.resourceId,kind:"backup.verify",input:{backupJobId:t.id},lockKey:`recovery-point:${e.id}`,providerKey:"backup",maxAttempts:2,timeoutSeconds:7200,retryClasses:["provider_transient"],resumePolicy:"requeue"});return this.store.updateJob(t.id,{operationId:r.operation.id})}return t}planRestore(e,t){if(e.status!=="available")throw Error("Only available recovery points can be restored.");if(e.verificationState!=="verified")throw Error("Restore requires a verified recovery point.");if(t.mode==="in_place"){if(!t.recentAuth)throw Error("In-place restore requires recent authentication.");if(t.confirm!==t.targetName)throw Error(`Type ${t.targetName} to confirm in-place restore.`);if(!t.downtimeAcknowledged)throw Error("In-place restore requires downtime acknowledgement.");let r=t.safetyBackupId?this.store.getRecoveryPoint(t.safetyBackupId):void 0;if(!r||r.id===e.id||r.status!=="available"||r.verificationState!=="verified"||r.resourceId!==e.resourceId||r.dataServiceId!==e.dataServiceId)throw Error("In-place restore requires a distinct verified safety backup for the same target.")}return{point:e,mode:t.mode,target:{...t.target,inPlace:t.mode==="in_place",restoreMode:t.mode},warnings:t.mode==="in_place"?["The target is locked against deployments and backup work.","Downtime and data replacement are expected.","Provider cancellation may stop only at a checkpoint."]:["An isolated target is created and health-validated before cleanup."]}}enqueueRestore(e,t){let r=this.planRestore(e,t),o=t.drill?"drill":"restore",m=EI(JSON.stringify({point:e.id,mode:t.mode,target:t.target,safety:t.safetyBackupId,drill:!!t.drill})),h=this.store.createJob({projectId:e.projectId,policyId:e.policyId,recoveryPointId:e.id,kind:o,status:"queued",idempotencyKey:`${o}:${m}`,target:r.target,restoreMode:r.mode,cancellability:"checkpoint_only",safetyBackupId:t.safetyBackupId,progress:{phase:"queued",warnings:r.warnings}});if(!h.operationId){let I=this.queue.enqueue({projectId:e.projectId,resourceId:e.resourceId,actorId:t.actorId,kind:t.drill?"backup.drill":"backup.restore",input:{backupJobId:h.id},lockKey:e.dataServiceId?`data-service:${e.dataServiceId}`:e.resourceId?`resource:${e.resourceId}`:`recovery-point:${e.id}`,providerKey:"backup",maxAttempts:1,timeoutSeconds:14400,retryClasses:[],resumePolicy:"fail"});return this.store.updateJob(h.id,{operationId:I.operation.id})}return h}enqueueRetention(e=this.now()){return this.store.retentionCandidates(e).map((t)=>{let r=this.store.createJob({projectId:t.projectId,policyId:t.policyId,recoveryPointId:t.id,kind:"cleanup",status:"queued",idempotencyKey:`cleanup:${t.id}:${t.expiresAt}`,target:{},cancellability:"safe",progress:{phase:"queued"}});if(!r.operationId){let o=this.queue.enqueue({projectId:t.projectId,resourceId:t.resourceId,kind:"backup.cleanup",input:{backupJobId:r.id},lockKey:`recovery-point:${t.id}`,providerKey:"backup",maxAttempts:3,timeoutSeconds:7200,retryClasses:["provider_transient"],resumePolicy:"requeue"});return this.store.updateJob(r.id,{operationId:o.operation.id})}return r})}}function _l(e){let t=e.now??(()=>new Date),r=new $i(e.store,e.queue,t),o=(R)=>{let A=R.recoveryPointId?e.store.getRecoveryPoint(R.recoveryPointId):void 0,E=R.policyId?e.store.getPolicy(R.policyId):A?.policyId?e.store.getPolicy(A.policyId):void 0;if(!E)throw Error("Backup policy was not found.");let x=e.store.getDestination(A?.destinationId??E.destinationId),N=e.resolveSource(E);if(!x)throw Error("Backup destination was not found.");if(!N)throw Error(`No ${E.resourceKind} backup adapter is configured.`);return{point:A,policy:E,destination:x,source:N}},m=(R)=>{let A=R.operation.input,E=e.store.getJob(String(A.backupJobId??""));if(!E)throw Error("Backup job was not found.");return e.store.updateJob(E.id,{status:"running",startedAt:E.startedAt??t().toISOString(),progress:{...E.progress,phase:"running"}}),E},h=(R,A)=>{let E=e.store.getJob(R.id)??R;e.store.updateJob(R.id,{status:"failed",finishedAt:t().toISOString(),error:A instanceof Error?A.message:String(A),progress:{...E.progress,phase:"failed"}})};return{"backup.run":async(R)=>{let A=m(R);try{let{policy:E,destination:x,source:N}=o(A),F=await N.create(E,R),z=e.resolveDestination(x),K=F.mode==="object"?await(async()=>{if(!z)throw Error("Backup destination adapter is not configured.");let ye=e.store.getJob(A.id)?.progress,ne=ye?.multipart,fe=ne&&typeof ne==="object"&&!Array.isArray(ne)&&typeof ne.uploadId==="string"&&typeof ne.key==="string"&&Array.isArray(ne.parts)?ne:void 0;return z.upload(x,{key:F.key,body:F.body,contentType:F.contentType,resume:fe,checkpoint:(Ce)=>{R.checkpoint("uploading",`Uploaded ${Number(Ce.bytesUploaded??0)} backup bytes.`),e.store.updateJob(A.id,{progress:{phase:"uploading",multipart:{...Ce}}})}})})():{uri:F.uri,key:F.uri,sizeBytes:F.sizeBytes,checksum:F.checksum,manifest:{format:"ts-cloud-backup-v1",encrypted:!1,plaintextChecksum:F.checksum,storageChecksum:F.checksum,contentType:"application/provider-snapshot"}},J=E.retention.expireAfterDays?new Date(t().getTime()+E.retention.expireAfterDays*86400000).toISOString():void 0,re=e.store.getJob(A.id)?.startedAt,se=e.store.createRecoveryPoint({projectId:E.projectId,policyId:E.id,destinationId:x.id,resourceId:E.resourceId,dataServiceId:E.dataServiceId,backupJobId:A.id,kind:E.resourceKind,pointInTime:t().toISOString(),uri:K.uri,sizeBytes:K.sizeBytes,checksum:K.checksum,manifest:{...F.manifest,...K.manifest,storageKey:K.key},toolVersion:F.toolVersion,engineVersion:F.engineVersion,expiresAt:J,lockedUntil:x.immutability.defaultRetentionDays?new Date(t().getTime()+x.immutability.defaultRetentionDays*86400000).toISOString():void 0,held:!1,pinned:!1,status:"available",verificationState:"unverified",durationMs:Math.max(0,t().getTime()-new Date(re??t().toISOString()).getTime())});if(F.mode==="external"&&x.provider==="aws_backup")e.store.recordDestinationTest(x.id,{ok:!0});return e.store.updateJob(A.id,{status:"succeeded",recoveryPointId:se.id,finishedAt:t().toISOString(),progress:{phase:"stored",recoveryPointId:se.id}}),r.enqueueVerification(se),{backupJobId:A.id,recoveryPointId:se.id}}catch(E){let x=e.store.getJob(A.id),N=x?.progress.multipart,F=(()=>{try{return o(A)}catch{return}})();if(F?.destination.provider==="aws_backup")e.store.recordDestinationTest(F.destination.id,{ok:!1,error:E instanceof Error?E.message:String(E)});let z=!1;if(N&&typeof N==="object"&&!Array.isArray(N)&&F){let K=e.resolveDestination(F.destination);if(z=!0,K?.abortPartial)try{await K.abortPartial(F.destination,N),z=!1,e.store.updateJob(A.id,{progress:{...x.progress,partialCleanup:"aborted"}})}catch{e.store.updateJob(A.id,{progress:{...x.progress,partialCleanup:"required"}})}}if(z)e.store.updateJob(A.id,{status:"cleanup_required",finishedAt:t().toISOString(),error:E instanceof Error?E.message:String(E),progress:{...e.store.getJob(A.id)?.progress??{},phase:"cleanup_required"}});else h(A,E);throw E}},"backup.verify":async(R)=>{let A=m(R);try{let{point:E,destination:x,source:N}=o(A);if(!E)throw Error("Recovery point was not found.");e.store.updateRecoveryPoint(E.id,{verificationState:"verifying"});let F;if(E.uri.startsWith("s3:")){let z=e.resolveDestination(x);if(!z)throw Error("Backup destination adapter is not configured.");let K=await z.download(x,{key:String(E.manifest.storageKey??""),checksum:E.checksum,manifest:E.manifest}),J=N.verify?await N.verify(E,K,R):{};F={bytes:K.length,checksum:E.checksum,...J}}else if(N.verifyExternal)F=await N.verifyExternal(E,R);else throw Error("External recovery point verification is not supported.");return e.store.updateRecoveryPoint(E.id,{verificationState:"verified",verifiedAt:t().toISOString(),manifest:{...E.manifest,verification:F}}),e.store.updateJob(A.id,{status:"succeeded",finishedAt:t().toISOString(),progress:{phase:"verified",evidence:F}}),{backupJobId:A.id,recoveryPointId:E.id,evidence:F}}catch(E){if(A.recoveryPointId)e.store.updateRecoveryPoint(A.recoveryPointId,{verificationState:/checksum|corrupt|authentic/i.test(E instanceof Error?E.message:String(E))?"corrupt":"failed"});throw h(A,E),E}},"backup.restore":async(R)=>I(R,!1),"backup.drill":async(R)=>I(R,!0),"backup.cleanup":async(R)=>{let A=m(R);try{let{point:E,destination:x,source:N}=o(A);if(!E)throw Error("Recovery point was not found.");if(!e.store.retentionCandidates(t()).some((F)=>F.id===E.id))throw Error("Recovery point is held, pinned, locked, or in active use.");if(e.store.updateRecoveryPoint(E.id,{status:"deleting"}),E.uri.startsWith("s3:")){let F=e.resolveDestination(x);if(!F)throw Error("Destination cleanup is unavailable.");await F.delete(x,String(E.manifest.storageKey??""))}else if(N.deleteExternal)await N.deleteExternal(E,R);else throw Error("External recovery point cleanup is unavailable.");return e.store.updateRecoveryPoint(E.id,{status:"deleted"}),e.store.updateJob(A.id,{status:"succeeded",finishedAt:t().toISOString(),progress:{phase:"deleted"}}),{backupJobId:A.id,recoveryPointId:E.id,deleted:!0}}catch(E){throw h(A,E),E}}};async function I(R,A){let E=m(R);try{let{point:x,policy:N,destination:F,source:z}=o(E);if(!x)throw Error("Recovery point was not found.");if(A&&!z.cleanup)throw Error("This backup source cannot safely clean a recovery drill target.");let K=x.uri.startsWith("s3:")?e.resolveDestination(F):void 0,J=K?await K.download(F,{key:String(x.manifest.storageKey??""),checksum:x.checksum,manifest:x.manifest}):void 0,re=await z.restore(x,J,E.target,R),se=z.validateHealth?await z.validateHealth({...E.target,...re},R):{healthy:!0,mode:"adapter"},ye=e.validateHealth?await e.validateHealth(N,E.target):{healthy:!0,mode:"not_configured"},ne={...se,healthy:se.healthy===!0&&ye.healthy===!0,configured:ye};if(ne.healthy!==!0)throw Error("Restored target did not pass its configured health check.");if(A&&z.cleanup)await z.cleanup({...E.target,provider:x.manifest.provider??null},R);return e.store.updateJob(E.id,{status:"succeeded",finishedAt:t().toISOString(),healthResult:ne,progress:{phase:A?"drill_cleaned":"restored",restored:re}}),{backupJobId:E.id,recoveryPointId:x.id,restored:re,health:ne,cleaned:A}}catch(x){let N=(()=>{try{return o(E)}catch{return}})(),F=x instanceof Error?x.message:String(x),z=!1;if(A&&N?.source.cleanup)try{await N.source.cleanup({...E.target,provider:N.point?.manifest.provider??null},R)}catch{z=!0}if(z)e.store.updateJob(E.id,{status:"cleanup_required",finishedAt:t().toISOString(),error:F,progress:{...E.progress,phase:"cleanup_required"}});else h(E,x);throw x}}}import{createHash as RI}from"node:crypto";var AI=(e)=>`sha256:${RI("sha256").update(e).digest("hex")}`;class ta{dataServices;client;constructor(e,t=new j){this.dataServices=e;this.client=t}service(e){let t=e.dataServiceId?this.dataServices.get(e.dataServiceId):void 0;if(!t||!["aws_rds","aws_aurora"].includes(t.provider))throw Error("Managed database backup policy requires an RDS or Aurora data service.");return t}async waitForSnapshot(e,t,r){for(;;){r.throwIfCancellationRequested?.(),r.heartbeat?.();let o=e==="aws_aurora"?(await this.client.describeDBClusterSnapshots({DBClusterSnapshotIdentifier:t})).DBClusterSnapshots[0]:(await this.client.describeDBSnapshots({DBSnapshotIdentifier:t})).DBSnapshots?.[0];if(!o)throw Error("Provider snapshot was not found.");let m=o,h=String(m.Status??m.DBSnapshotStatus??"unknown");if(r.checkpoint?.("provider_snapshot",`Database snapshot ${t} is ${h}.`),h==="available")return m;if(["deleted","deleting","failed"].includes(h))throw Error(`Provider snapshot ${t} failed with state ${h}.`);await Bun.sleep(5000)}}async create(e,t){let r=this.service(e),o=String(t.operation?.id??new Date().toISOString()).replace(/[^A-Za-z0-9]/g,"").slice(0,20),m=`${r.placement.slice(0,42).replace(/-+$/,"")}-${o}`;if(r.provider==="aws_aurora")await this.client.createDBClusterSnapshot(r.placement,m);else await this.client.createDBSnapshot({DBInstanceIdentifier:r.placement,DBSnapshotIdentifier:m});await this.waitForSnapshot(r.provider,m,t);let h=`${r.provider}:${r.placement}:${m}`;return{mode:"external",uri:`aws-backup:${h}`,checksum:AI(h),sizeBytes:0,manifest:{provider:r.provider,sourceId:r.placement,snapshotId:m,plan:r.plan,engine:r.engine},engineVersion:r.engineVersion,toolVersion:"aws-rds-api-2014-10-31"}}async verifyExternal(e,t){let r=String(e.manifest.snapshotId??""),o=String(e.manifest.provider??"");if(!r)throw Error("Snapshot recovery point has no snapshot ID.");let m=await this.waitForSnapshot(o,r,t),h=String(m.Status??m.DBSnapshotStatus??"unknown");return{snapshotId:r,status:h,encrypted:m.Encrypted??m.StorageEncrypted??!0}}async restore(e,t,r,o){let m=String(r.targetId??""),h=String(e.manifest.snapshotId??""),I=String(e.manifest.provider??"");if(!/^[a-z0-9][a-z0-9-]{1,62}$/.test(m))throw Error("Managed restore requires a valid distinct targetId.");if(m===e.manifest.sourceId)throw Error("Managed restore target must differ from its source.");if(I==="aws_aurora"){let R=e.manifest.engine==="postgres"?"aurora-postgresql":"aurora-mysql";await this.client.restoreDBClusterFromSnapshot({DBClusterIdentifier:m,SnapshotIdentifier:h,Engine:R,EngineVersion:e.engineVersion,DeletionProtection:!0,ServerlessV2ScalingConfiguration:{MinCapacity:Number(r.minCapacity)||0.5,MaxCapacity:Number(r.maxCapacity)||4}}),await this.client.createDBInstance({DBInstanceIdentifier:`${m}-writer-1`,DBClusterIdentifier:m,DBInstanceClass:String(r.plan??e.manifest.plan??"db.serverless"),Engine:R,PubliclyAccessible:!1})}else await this.client.restoreDBInstanceFromDBSnapshot({DBInstanceIdentifier:m,DBSnapshotIdentifier:h,DBInstanceClass:String(r.plan??e.manifest.plan??""),PubliclyAccessible:!1,DeletionProtection:!0,Tags:[{Key:"managed-by",Value:"ts-cloud-restore"}]});return{targetId:m,provider:I,status:"creating",isolated:!0}}async validateHealth(e,t){let r=String(e.targetId??""),o=String(e.provider??"");for(;;){t.throwIfCancellationRequested?.(),t.heartbeat?.();let m=o==="aws_aurora"?(await this.client.describeDBClusters({DBClusterIdentifier:r})).DBClusters?.[0]:(await this.client.describeDBInstances({DBInstanceIdentifier:r})).DBInstances?.[0];if(!m)throw Error("Restored managed database was not found.");let h=String(m.Status??m.DBInstanceStatus??"unknown");if(t.checkpoint?.("provider_restore",`Restored database ${r} is ${h}.`),h==="available")return{healthy:!0,targetId:r,provider:o,status:h};if(["deleted","deleting","failed","incompatible-restore"].includes(h))throw Error(`Restored managed database ${r} failed with state ${h}.`);await Bun.sleep(5000)}}async cleanup(e,t){let r=String(e.targetId??""),o=String(e.provider??"");if(!r)throw Error("Restore cleanup target is required.");if(o==="aws_aurora")await this.client.deleteDBInstance({DBInstanceIdentifier:`${r}-writer-1`,SkipFinalSnapshot:!0}),await this.client.modifyDBCluster({DBClusterIdentifier:r,DeletionProtection:!1,ApplyImmediately:!0}),await this.client.deleteDBCluster(r);else await this.client.modifyDBInstance({DBInstanceIdentifier:r,DeletionProtection:!1,ApplyImmediately:!0}),await this.client.deleteDBInstance({DBInstanceIdentifier:r,SkipFinalSnapshot:!0,DeleteAutomatedBackups:!0})}async deleteExternal(e,t){let r=String(e.manifest.snapshotId??"");if(e.manifest.provider==="aws_aurora")await this.client.deleteDBClusterSnapshot(r);else await this.client.deleteDBSnapshot(r)}}var mn=(e,t="none",r=!1)=>({supported:!0,downtime:t,destructive:r,explanation:e}),on=(e)=>({supported:!1,downtime:"none",destructive:!1,explanation:e});function Gr(e,t){let r=["aws_rds","aws_aurora","aws_elasticache"].includes(t),o=["server","container"].includes(t),m=["postgres","mysql","mariadb"].includes(e),h=e==="redis",I={};return I.create=t==="external"?on("External services can be adopted as read-only inventory."):mn(r?"Provision through the provider API.":"Provision through the owning runtime driver.","possible"),I.observe=t==="external"?on("External observation requires a configured provider adapter."):mn("Provider/runtime health and endpoint metadata are observable."),I.connect=mn("Connection guidance uses secret references and never returns a password by default."),I.backup=m&&(r||o)?mn(r?"Create a provider snapshot.":"Create a consistent engine-native dump.","possible"):on("This cache path is not a durable backup source."),I.restore=r&&m?mn("Restore creates a reviewed provider target.","required",!0):on("Restore requires a configured provider restore runner."),I.restart=t==="external"?on("External restart requires a configured provider adapter."):mn(r?"Provider restart/failover semantics apply.":"Restart the owning service unit.","required"),I.resize=t==="aws_elasticache"?on("ElastiCache resizing requires a replication-group migration runner."):r?mn("Provider plan/storage modification may enter a maintenance window.","possible"):o?mn("Edit host/container resources and reconcile.","required"):on("External resize requires a configured provider adapter."),I.version=o?on("Runtime version upgrades require an explicit image migration runner."):r&&t!=="aws_elasticache"?mn(m?"Major versions require compatibility preflight and backup.":"Engine upgrades require a maintenance window.","required"):on("External upgrades require a configured provider adapter."),I.rotate=t==="aws_elasticache"||t==="external"||o&&!m&&!h?on("Credential rotation is not available for this provider/engine path."):mn("Rotate through the secret backend, update the engine, then flag dependent releases."),I.expose=o?on("Direct container publishing is refused; use a reviewed firewall and proxy policy."):t==="aws_rds"?mn("Exposure is reconciled through security groups; public access defaults off."):on("Exposure changes require a provider-specific network runner."),I.delete=t==="external"?on("External deletion requires a configured provider adapter."):mn(r?"Final snapshot is supported where the provider allows it.":"A final engine backup is required unless explicitly retained.","required",!0),I.logs=o?mn("Read bounded service logs from the runtime."):on("Provider log export is not configured for this adapter."),I.slow_queries=o&&m?mn("Read bounded runtime logs after engine slow-query logging is enabled."):on("A slow-query log runner is not configured for this provider."),I.users=o&&m?mn("Logical users can be listed; creation requires a referenced secondary credential."):on("User management requires an engine connection runner."),I.databases=o&&m?mn("Logical databases can be listed, created, and confirmation-gated for deletion."):on("Logical database management requires an engine connection runner."),{actions:I,endpointTypes:r?["internal","external","tunnel"]:["internal","tunnel"],metrics:h?["connections","memory","evictions","hit_rate","latency"]:["connections","cpu","storage","read_latency","write_latency","iops"]}}var wa=(e)=>e==null?void 0:String(e),td=(e)=>Number(e)===1,ed=(e)=>{try{return JSON.parse(String(e))}catch{return{}}};function Nh(e){return{id:String(e.id),organizationId:String(e.organization_id),projectId:String(e.project_id),environmentId:wa(e.environment_id),resourceId:wa(e.resource_id),name:String(e.name),engine:String(e.engine),provider:String(e.provider),placement:String(e.placement),engineVersion:wa(e.engine_version),plan:String(e.plan),storageGb:e.storage_gb==null?void 0:Number(e.storage_gb),highAvailability:td(e.high_availability),publicExposure:td(e.public_exposure),allowedCidrs:ed(e.allowed_cidrs),desiredState:ed(e.desired_state),observedState:ed(e.observed_state),capabilities:ed(e.capabilities),credentialRef:wa(e.credential_ref),status:String(e.status),origin:String(e.origin),managementEnabled:td(e.management_enabled),ownerActorId:wa(e.owner_actor_id),version:Number(e.version),createdAt:String(e.created_at),updatedAt:String(e.updated_at)}}class No{controlPlane;now;constructor(e,t=()=>new Date){this.controlPlane=e;this.now=t}create(e){if(!/^[a-z0-9][a-z0-9-]{1,62}$/.test(e.name))throw Error("Data service names must be 2-63 lowercase letters, numbers, or dashes.");if(e.publicExposure&&(!e.allowedCidrs.length||e.allowedCidrs.includes("0.0.0.0/0")))throw Error("Public exposure requires explicit narrow CIDRs; 0.0.0.0/0 is refused.");let t=crypto.randomUUID(),r=this.now().toISOString(),o=Gr(e.engine,e.provider);return this.controlPlane.database.run("INSERT INTO data_services (id,organization_id,project_id,environment_id,resource_id,name,engine,provider,placement,engine_version,plan,storage_gb,high_availability,public_exposure,allowed_cidrs,desired_state,observed_state,capabilities,credential_ref,status,origin,management_enabled,owner_actor_id,version,created_at,updated_at) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",[t,e.organizationId,e.projectId,e.environmentId??null,e.resourceId??null,e.name,e.engine,e.provider,e.placement,e.engineVersion??null,e.plan,e.storageGb??null,e.highAvailability?1:0,e.publicExposure?1:0,JSON.stringify(e.allowedCidrs),JSON.stringify(e.desiredState),JSON.stringify(e.observedState),JSON.stringify(o),e.credentialRef??null,e.status,e.origin,e.managementEnabled?1:0,e.ownerActorId??null,1,r,r]),this.get(t)}get(e){let t=this.controlPlane.database.query("SELECT * FROM data_services WHERE id=?").get(e);return t?Nh(t):void 0}list(e,t){return this.controlPlane.database.query(`SELECT * FROM data_services WHERE project_id=?${t?" AND environment_id=?":""} ORDER BY name`).all(e,...t?[t]:[]).map(Nh)}update(e,t,r){let o=this.get(e);if(!o)throw Error("Data service was not found.");if(o.version!==t)throw Error("Data service changed; refresh before retrying.");let m={...o,...r};if(m.publicExposure&&(!m.allowedCidrs.length||m.allowedCidrs.includes("0.0.0.0/0")))throw Error("Public exposure requires explicit narrow CIDRs; 0.0.0.0/0 is refused.");if(this.controlPlane.database.run("UPDATE data_services SET plan=?,storage_gb=?,engine_version=?,high_availability=?,public_exposure=?,allowed_cidrs=?,desired_state=?,observed_state=?,status=?,management_enabled=?,credential_ref=?,version=version+1,updated_at=? WHERE id=? AND version=?",[m.plan,m.storageGb??null,m.engineVersion??null,m.highAvailability?1:0,m.publicExposure?1:0,JSON.stringify(m.allowedCidrs),JSON.stringify(m.desiredState),JSON.stringify(m.observedState),m.status,m.managementEnabled?1:0,m.credentialRef??null,this.now().toISOString(),e,t]).changes!==1)throw Error("Data service changed; refresh before retrying.");return this.get(e)}credential(e){let t=this.controlPlane.database.query("SELECT * FROM data_service_credentials WHERE service_id=? ORDER BY version DESC LIMIT 1").get(e);return t?{id:String(t.id),serviceId:String(t.service_id),username:String(t.username),secretRef:String(t.secret_ref),version:Number(t.version),createdAt:String(t.created_at),rotatedAt:wa(t.rotated_at)}:void 0}saveCredential(e,t,r){let o=this.credential(e),m=o?.id??crypto.randomUUID(),h=this.now().toISOString();return this.controlPlane.database.run("INSERT INTO data_service_credentials (id,service_id,username,secret_ref,version,created_at,rotated_at) VALUES (?,?,?,?,?,?,?) ON CONFLICT(service_id,username) DO UPDATE SET secret_ref=excluded.secret_ref,version=data_service_credentials.version+1,rotated_at=excluded.rotated_at",[m,e,t,r,o?.version??1,o?.createdAt??h,o?h:null]),this.credential(e)}addDependency(e){let t=this.now().toISOString();return this.controlPlane.database.run("INSERT INTO data_service_dependencies (service_id,resource_id,secret_ref,requires_redeploy,created_at) VALUES (?,?,?,?,?) ON CONFLICT(service_id,resource_id) DO UPDATE SET secret_ref=excluded.secret_ref,requires_redeploy=excluded.requires_redeploy",[e.serviceId,e.resourceId,e.secretRef,e.requiresRedeploy?1:0,t]),{...e,createdAt:t}}dependencies(e){return this.controlPlane.database.query("SELECT * FROM data_service_dependencies WHERE service_id=? ORDER BY resource_id").all(e).map((t)=>({serviceId:String(t.service_id),resourceId:String(t.resource_id),secretRef:String(t.secret_ref),requiresRedeploy:td(t.requires_redeploy),createdAt:String(t.created_at)}))}removeDependency(e,t){return this.controlPlane.database.run("DELETE FROM data_service_dependencies WHERE service_id=? AND resource_id=?",[e,t]).changes===1}remove(e){this.controlPlane.database.run("DELETE FROM data_services WHERE id=?",[e])}}function jh(){let t=crypto.getRandomValues(new Uint8Array(40));return Array.from(t,(r)=>"ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz23456789!@#$%^&*_-+="[r%69]).join("")}function cl(e,t){return t.map((r)=>{let o=r.type==="tunnel"?"127.0.0.1":r.host,m=e.engine==="redis"?`redis-cli -h ${o} -p ${r.port}${r.tls?" --tls":""}`:e.engine==="postgres"?`psql "host=${o} port=${r.port} dbname=${r.database??"postgres"} sslmode=${r.tls?"require":"disable"}"`:e.engine==="mongodb"?`mongosh "mongodb://${o}:${r.port}/${r.database??"admin"}${r.tls?"?tls=true":""}"`:e.engine==="libsql"?`turso db shell ${r.tls?"https":"http"}://${o}:${r.port}`:`mysql --host=${o} --port=${r.port} --database=${r.database??"mysql"} --ssl-mode=${r.tls?"REQUIRED":"DISABLED"}`;return{type:r.type,command:m,secretRef:e.credentialRef}})}class Ni{store;queue;secrets;constructor(e,t,r){this.store=e;this.queue=t;this.secrets=r}async create(e){let t,r;if(e.provider!=="aws_elasticache"){let o=e.username??"app",m=jh();r=`secret://data-services/${e.projectId}/${e.name}/${o}`,await this.secrets.put(r,m),t={username:o,password:m,secretRef:r}}try{let o=this.store.create({...e,credentialRef:r,status:"planning"});if(t)this.store.saveCredential(o.id,t.username,t.secretRef);let m=this.enqueue(o,"create",{});return{service:o,credential:t,operationId:m}}catch(o){if(r)await this.secrets.remove(r);throw o}}plan(e,t,r={}){let o=e.capabilities.actions[t],m=t==="version"&&String(r.engineVersion??"").split(".")[0]!==String(e.engineVersion??"").split(".")[0];return{service:e,action:t,capability:o,changes:r,preflight:{backupRequired:["delete","restore"].includes(t)||m,compatibilityRequired:m,typedConfirmation:o.destructive?e.name:void 0,retentionChoiceRequired:t==="delete"},warnings:[...o.downtime!=="none"?[`Downtime is ${o.downtime}.`]:[],...e.origin==="adopted"&&!e.managementEnabled?["Adopted resource is read-only until management is explicitly enabled."]:[]]}}enqueue(e,t,r,o){let m=this.plan(e,t,r);if(!m.capability.supported)throw Error(m.capability.explanation);if(e.origin==="adopted"&&!e.managementEnabled&&t!=="observe")throw Error("Adopted resources remain read-only until management is explicitly enabled.");if(m.preflight.typedConfirmation&&r.confirm!==m.preflight.typedConfirmation)throw Error(`Type ${m.preflight.typedConfirmation} to confirm ${t}.`);if(m.preflight.retentionChoiceRequired&&!["final_backup","retain"].includes(String(r.retention??"")))throw Error("Deletion requires retention final_backup or retain.");if(m.preflight.compatibilityRequired&&(r.compatibilityReviewed!==!0||!r.backupId))throw Error("Major version changes require compatibilityReviewed=true and a backupId.");if(t==="restore"){let h=String(r.backupId??""),I=String(r.targetId??"");if(!h||!I)throw Error("Restore requires both backupId and targetId.");if(I===e.placement)throw Error("Restore targetId must differ from the source placement.");if(!/^[a-z0-9][a-z0-9-]{1,62}$/.test(I))throw Error("Restore targetId must be 2-63 lowercase letters, numbers, or dashes.")}return this.queue.enqueue({projectId:e.projectId,environmentId:e.environmentId,resourceId:e.resourceId,actorId:o,kind:"data_service.action",input:{serviceId:e.id,action:t,...r},lockKey:`data-service:${e.id}`,providerKey:e.provider,maxAttempts:3,timeoutSeconds:7200,retryClasses:["provider_transient"],resumePolicy:"requeue"}).operation.id}async rotate(e,t,r){let o=this.store.credential(e.id);if(!o)throw Error("Data service credential metadata was not found.");let m=jh(),h=`${o.secretRef}/v${o.version+1}`;await this.secrets.put(h,m);try{return await t.action(e,"rotate",{username:o.username,secretRef:h},m,r),this.store.saveCredential(e.id,o.username,h),this.store.update(e.id,e.version,{credentialRef:h}),{credential:{username:o.username,password:m,secretRef:h},dependencies:this.store.dependencies(e.id).filter((I)=>I.requiresRedeploy).map((I)=>I.resourceId)}}catch(I){throw await this.secrets.remove(h),I}}}function dl(e){return{"data_service.action":async(t)=>{let r=t.operation.input,o=e.store.get(String(r.serviceId??"")),m=String(r.action??"");if(!o)throw Error("Data service was not found.");let h=e.resolveAdapter(o);if(!h||h.provider!==o.provider||!h.engines.includes(o.engine))throw Error(`No ${o.provider}/${o.engine} adapter is configured.`);if(m==="rotate"){let R=await new Ni(e.store,new c(e.store.controlPlane),e.secrets).rotate(o,h,t);return{serviceId:o.id,action:m,username:R.credential.username,secretRef:R.credential.secretRef,dependencies:R.dependencies}}let I=o.credentialRef?await e.secrets.resolve(o.credentialRef):void 0;try{let R=m==="observe"?await h.observe(o):m==="create"?await h.apply(o,I,t):await h.action(o,m,r,I,t),A=e.store.get(o.id);return e.store.update(o.id,A.version,{observedState:R,status:m==="delete"?r.retention==="retain"?"retained":"deleting":"available"}),{serviceId:o.id,action:m,observed:R}}catch(R){let A=e.store.get(o.id);if(e.store.update(o.id,A.version,{status:"failed",observedState:{...A.observedState,error:R instanceof Error?R.message:String(R)}}),/temporar|timeout|unavailable|throttl/i.test(R instanceof Error?R.message:String(R)))throw new W(R instanceof Error?R.message:String(R),"provider_transient");throw R}}}}class ka{transport;constructor(e){this.transport=e}observe(e){return this.transport.observe(e.placement)}apply(e,t){return this.transport.apply({id:e.placement,name:e.name,engine:e.engine,engineVersion:e.engineVersion??null,plan:e.plan,storageGb:e.storageGb??null,highAvailability:e.highAvailability,publicExposure:e.publicExposure,allowedCidrs:e.allowedCidrs,username:e.desiredState.username??"app",desiredState:e.desiredState},t)}action(e,t,r,o,m){return this.transport.execute(e.placement,t,{engine:e.engine,engineVersion:e.engineVersion??null,plan:e.plan,desiredState:e.desiredState,...r},o,m)}}class ko extends ka{provider="aws_rds";engines=["postgres","mysql","mariadb"]}class Bo extends ka{provider="aws_aurora";engines=["postgres","mysql"]}class xo extends ka{provider="aws_elasticache";engines=["redis"]}class _o extends ka{provider="server";engines=["postgres","mysql","mariadb","redis","mongodb","libsql"]}class Oo extends ka{provider="container";engines=["postgres","mysql","mariadb","redis","mongodb","libsql"]}var ad=(e,t)=>{let r=e.desiredState;return e[t]??(r&&typeof r==="object"&&!Array.isArray(r)?r[t]:void 0)},hr=(e,t,r)=>{let o=ad(e,t);return o==null?r:String(o)},Cn=(e,t)=>{let r=ad(e,t);return r==null?void 0:Number(r)},cs=(e,t)=>{let r=ad(e,t);return r==null?void 0:r===!0||r==="true"},Ia=(e,t)=>{let r=ad(e,t);return Array.isArray(r)?r.map(String):[]},id=(e,t)=>hr(t,"snapshotId")??`${e}-final-${new Date().toISOString().replace(/[^0-9]/g,"").slice(0,14)}`;function nd(e){if(!e)throw Error("RDS instance was not found.");return{providerId:e.DBInstanceIdentifier??null,status:e.DBInstanceStatus??"unknown",engine:e.Engine??null,engineVersion:e.EngineVersion??null,plan:e.DBInstanceClass??null,storageGb:e.AllocatedStorage??null,highAvailability:e.MultiAZ??!1,publicExposure:e.PubliclyAccessible??!1,endpoint:e.Endpoint?.Address??null,port:e.Endpoint?.Port??null,username:e.MasterUsername??null,database:e.DBName??null,encrypted:e.StorageEncrypted??!1}}class Lo{client;constructor(e=new j){this.client=e}async observe(e){return nd(await this.client.describeDBInstance(e))}async apply(e,t){let r=String(e.id),o=await this.client.createDBInstance({DBInstanceIdentifier:r,DBInstanceClass:String(e.plan),Engine:String(e.engine),MasterUsername:hr(e,"username","app"),MasterUserPassword:t,DBName:hr(e,"database"),AllocatedStorage:Cn(e,"storageGb"),VpcSecurityGroupIds:Ia(e,"securityGroupIds"),DBSubnetGroupName:hr(e,"subnetGroup"),BackupRetentionPeriod:Cn(e,"backupRetentionDays")??7,MultiAZ:cs(e,"highAvailability"),EngineVersion:hr(e,"engineVersion"),PubliclyAccessible:cs(e,"publicExposure")??!1,StorageType:hr(e,"storageType","gp3"),StorageEncrypted:!0,KmsKeyId:hr(e,"kmsKeyId"),DeletionProtection:!0,Tags:[{Key:"managed-by",Value:"ts-cloud"}]});return nd(o.DBInstance)}async execute(e,t,r,o,m){if(t==="observe")return this.observe(e);if(t==="restart")return await this.client.rebootDBInstance({DBInstanceIdentifier:e}),{status:"rebooting"};if(t==="backup"){let h=id(e,r);return await this.client.createDBSnapshot({DBInstanceIdentifier:e,DBSnapshotIdentifier:h}),{status:"snapshotting",snapshotId:h}}if(t==="restore"){let h=String(r.targetId),I=await this.client.restoreDBInstanceFromDBSnapshot({DBInstanceIdentifier:h,DBSnapshotIdentifier:String(r.backupId),DBInstanceClass:hr(r,"plan"),DBSubnetGroupName:hr(r,"subnetGroup"),MultiAZ:cs(r,"highAvailability"),PubliclyAccessible:!1,StorageType:hr(r,"storageType"),VpcSecurityGroupIds:Ia(r,"securityGroupIds"),DeletionProtection:!0,Tags:[{Key:"managed-by",Value:"ts-cloud"}]});return{...nd(I.DBInstance),sourceId:e,restoreTargetId:h}}if(t==="delete"){if(r.retention==="retain")return{status:"retained"};let h=id(e,r);return await this.client.modifyDBInstance({DBInstanceIdentifier:e,DeletionProtection:!1,ApplyImmediately:!0}),await this.client.deleteDBInstance({DBInstanceIdentifier:e,SkipFinalSnapshot:!1,FinalDBSnapshotIdentifier:h,DeleteAutomatedBackups:!1}),{status:"deleting",finalSnapshotId:h}}if(["resize","version","rotate","expose"].includes(t)){let h=await this.client.modifyDBInstance({DBInstanceIdentifier:e,DBInstanceClass:hr(r,"plan"),AllocatedStorage:Cn(r,"storageGb"),MasterUserPassword:t==="rotate"?o:void 0,EngineVersion:hr(r,"engineVersion"),MultiAZ:cs(r,"highAvailability"),PubliclyAccessible:cs(r,"publicExposure"),VpcSecurityGroupIds:Ia(r,"securityGroupIds"),ApplyImmediately:cs(r,"applyImmediately")??!0});return nd(h.DBInstance)}throw Error(`RDS action ${t} requires an engine connection runner.`)}}function xp(e){if(!e)throw Error("Aurora cluster was not found.");return{providerId:e.DBClusterIdentifier??null,status:e.Status??"unknown",engine:e.Engine??null,engineVersion:e.EngineVersion??null,endpoint:e.Endpoint??null,readerEndpoint:e.ReaderEndpoint??null,port:e.Port??null,username:e.MasterUsername??null,database:e.DatabaseName??null,encrypted:e.StorageEncrypted??!1,highAvailability:e.MultiAZ??!0}}class $o{client;constructor(e=new j){this.client=e}async observe(e){let t=await this.client.describeDBClusters({DBClusterIdentifier:e});return xp(t.DBClusters?.[0])}async apply(e,t){let r=String(e.id),o=String(e.engine)==="postgres"?"aurora-postgresql":"aurora-mysql";return await this.client.createDBCluster({DBClusterIdentifier:r,Engine:o,EngineVersion:hr(e,"engineVersion"),MasterUsername:hr(e,"username","app"),MasterUserPassword:t,DatabaseName:hr(e,"database"),DBSubnetGroupName:hr(e,"subnetGroup"),VpcSecurityGroupIds:Ia(e,"securityGroupIds"),BackupRetentionPeriod:Cn(e,"backupRetentionDays")??7,StorageEncrypted:!0,KmsKeyId:hr(e,"kmsKeyId"),DeletionProtection:!0,ServerlessV2ScalingConfiguration:{MinCapacity:Cn(e,"minCapacity")??0.5,MaxCapacity:Cn(e,"maxCapacity")??4}}),await this.client.createDBInstance({DBInstanceIdentifier:`${r}-writer-1`,DBClusterIdentifier:r,DBInstanceClass:String(e.plan||"db.serverless"),Engine:o,PubliclyAccessible:cs(e,"publicExposure")??!1}),{status:"creating",providerId:r}}async execute(e,t,r,o){if(t==="observe")return this.observe(e);if(t==="restart")return await this.client.rebootDBCluster(e),{status:"rebooting"};if(t==="backup"){let m=id(e,r);return await this.client.createDBClusterSnapshot(e,m),{status:"snapshotting",snapshotId:m}}if(t==="restore"){let m=String(r.targetId),h=String(r.engine)==="postgres"?"aurora-postgresql":"aurora-mysql",I=await this.client.restoreDBClusterFromSnapshot({DBClusterIdentifier:m,SnapshotIdentifier:String(r.backupId),Engine:h,EngineVersion:hr(r,"engineVersion"),DBSubnetGroupName:hr(r,"subnetGroup"),VpcSecurityGroupIds:Ia(r,"securityGroupIds"),DeletionProtection:!0,ServerlessV2ScalingConfiguration:{MinCapacity:Cn(r,"minCapacity")??0.5,MaxCapacity:Cn(r,"maxCapacity")??4}});return await this.client.createDBInstance({DBInstanceIdentifier:`${m}-writer-1`,DBClusterIdentifier:m,DBInstanceClass:hr(r,"plan","db.serverless"),Engine:h,PubliclyAccessible:!1}),{...xp(I.DBCluster),sourceId:e,restoreTargetId:m}}if(t==="delete"){if(r.retention==="retain")return{status:"retained"};let m=id(e,r);return await this.client.modifyDBCluster({DBClusterIdentifier:e,DeletionProtection:!1,ApplyImmediately:!0}),await this.client.deleteDBCluster(e,m),{status:"deleting",finalSnapshotId:m}}if(["resize","version","rotate"].includes(t)){let m=await this.client.modifyDBCluster({DBClusterIdentifier:e,ServerlessV2ScalingConfiguration:Cn(r,"minCapacity")==null&&Cn(r,"maxCapacity")==null?void 0:{MinCapacity:Cn(r,"minCapacity")??0.5,MaxCapacity:Cn(r,"maxCapacity")??4},EngineVersion:hr(r,"engineVersion"),MasterUserPassword:t==="rotate"?o:void 0,ApplyImmediately:cs(r,"applyImmediately")??!0});return xp(m.DBCluster)}throw Error(`Aurora action ${t} requires an engine connection runner.`)}}class Mo{client;constructor(e=new We){this.client=e}async observe(e){let t=(await this.client.describeCacheClusters(e)).CacheClusters[0];if(!t)throw Error("ElastiCache cluster was not found.");let r=t.CacheNodes?.[0]?.Endpoint;return{providerId:t.CacheClusterId,status:t.CacheClusterStatus,engine:t.Engine,engineVersion:t.EngineVersion,plan:t.CacheNodeType,nodes:t.NumCacheNodes,endpoint:r?.Address??null,port:r?.Port??null}}async apply(e){let t=await this.client.createCacheCluster({cacheClusterId:String(e.id),engine:"redis",cacheNodeType:String(e.plan),numCacheNodes:Cn(e,"nodes")??1,engineVersion:hr(e,"engineVersion"),port:Cn(e,"port"),securityGroupIds:Ia(e,"securityGroupIds"),subnetGroupName:hr(e,"subnetGroup"),tags:[{Key:"managed-by",Value:"ts-cloud"}]});return{providerId:t.CacheCluster.CacheClusterId,status:t.CacheCluster.CacheClusterStatus}}async execute(e,t,r){if(t==="observe")return this.observe(e);if(t==="restart"){let o=(await this.client.describeCacheClusters(e)).CacheClusters[0];return await this.client.rebootCacheCluster(e,o?.CacheNodes?.map((m)=>m.CacheNodeId)??["0001"]),{status:"rebooting"}}if(t==="delete"){if(r.retention==="retain")return{status:"retained"};return await this.client.deleteCacheCluster(e),{status:"deleting"}}throw Error(`ElastiCache action ${t} is not supported by this cluster mode.`)}}import{mkdir as CI,writeFile as PI}from"node:fs/promises";import{join as _I}from"node:path";async function Dn(e,t,r=!1){let o=Bun.spawn(["docker",...e],{stdin:t==null?"ignore":"pipe",stdout:"pipe",stderr:"pipe"}),m=new Response(o.stdout).text(),h=new Response(o.stderr).text();if(t!=null)o.stdin.write(t),o.stdin.end();let[I,R,A]=await Promise.all([o.exited,m,h]);if(I!==0&&!r)throw Error(`Docker command failed (${I}): ${A.trim()||R.trim()}`);return{code:I,stdout:R,stderr:A}}class ll{async ensureNetwork(e){if((await Dn(["network","inspect",e],void 0,!0)).code!==0)await Dn(["network","create","--internal",e])}async seedSecret(e,t){await Dn(["volume","create",e]),await Dn(["run","--rm","-i","-v",`${e}:/run/target`,"alpine:3.21","sh","-c","umask 077; cat > /run/target/credential"],t)}async removeVolume(e){await Dn(["volume","rm",e],void 0,!0)}async inspect(e){let t=await Dn(["inspect",e],void 0,!0);if(t.code!==0)return;return JSON.parse(t.stdout)[0]}async run(e){await Dn(["run",...e])}async exec(e,t,r){return(await Dn(["exec","-i",e,...t],r)).stdout}async update(e,t){await Dn(["update",...t,e])}async restart(e){await Dn(["restart",e])}async remove(e){await Dn(["rm","--force",e],void 0,!0)}async logs(e,t){let r=await Dn(["logs","--tail",String(Math.min(2000,Math.max(1,t))),e],void 0,!0);return`${r.stdout}${r.stderr}`.slice(-262144)}async stats(e){let t=await Dn(["stats","--no-stream","--format","{{json .}}",e],void 0,!0);if(t.code!==0||!t.stdout.trim())return{};let r=JSON.parse(t.stdout.trim());return{cpuPercent:String(r.CPUPerc??""),memoryUsage:String(r.MemUsage??""),memoryPercent:String(r.MemPerc??""),networkIo:String(r.NetIO??""),blockIo:String(r.BlockIO??""),pids:Number(r.PIDs??0)}}}var TI={postgres:{image:"postgres:17-alpine",port:5432,dataPath:"/var/lib/postgresql/data"},mysql:{image:"mysql:8.4",port:3306,dataPath:"/var/lib/mysql"},mariadb:{image:"mariadb:11.7",port:3306,dataPath:"/var/lib/mysql"},redis:{image:"redis:7.4-alpine",port:6379,dataPath:"/data"},mongodb:{image:"mongo:8.0",port:27017,dataPath:"/data/db"}};function Ai(e,t="app"){let r=String(e??t);if(!/^[A-Za-z_][A-Za-z0-9_]{0,62}$/.test(r))throw Error("Database identifiers must be letters, numbers, or underscores.");return r}function $s(e){if(!/^[a-z0-9][a-z0-9_.-]{1,62}$/i.test(e))throw Error("Runtime placement must be a valid container identifier.");return`tscloud-data-${e.toLowerCase()}`}function xI(e,t){let r=e.desiredState;return e[t]??(r&&typeof r==="object"&&!Array.isArray(r)?r[t]:void 0)}function ac(e){return e.Config?.Labels??{}}class jt{runtime;backupRoot;constructor(e=new ll,t=d(process.cwd(),"backups","data")){this.runtime=e;this.backupRoot=t}async observe(e){let t=$s(e),r=await this.runtime.inspect(t);if(!r)throw Error(`Data container ${t} was not found.`);let o=ac(r),m=await this.runtime.stats(t),h=r.NetworkSettings?.Ports??{},I=Object.values(h).flat().find(Boolean);return{providerId:t,status:r.State?.Status??"unknown",healthy:r.State?.Health?.Status??null,engine:o["ts-cloud.engine"]??null,engineVersion:o["ts-cloud.engine-version"]??null,username:o["ts-cloud.username"]??null,database:o["ts-cloud.database"]??null,endpoint:I?.HostIp||"127.0.0.1",port:I?.HostPort?Number(I.HostPort):null,image:r.Config?.Image??null,startedAt:r.State?.StartedAt??null,metrics:m}}async apply(e,t){let r=String(e.id),o=$s(r),m=String(e.engine);if(m==="libsql")throw Error("libSQL requires an explicitly configured runtime image.");let h=TI[m];if(!h)throw Error(`Unsupported runtime data engine ${m}.`);if(!t)throw Error(`${m} requires a managed credential.`);if(e.publicExposure===!0)throw Error("Container public exposure requires an external reviewed firewall policy; direct publishing is refused.");let I=Ai(e.username),R=Ai(xI(e,"database")),A=`${o}-secret`,E=`${o}-data`,x=m==="redis"?`appendonly yes
|
|
88
|
+
protected-mode yes
|
|
89
|
+
requirepass ${t.replaceAll("\\","\\\\").replaceAll(`
|
|
90
|
+
`,"")}
|
|
91
|
+
`:t;await this.runtime.ensureNetwork("ts-cloud-data"),await this.runtime.seedSecret(A,x);let N=["-d","--name",o,"--restart","unless-stopped","--network","ts-cloud-data","--label","managed-by=ts-cloud","--label",`ts-cloud.engine=${m}`,"--label",`ts-cloud.engine-version=${e.engineVersion??""}`,"--label",`ts-cloud.username=${I}`,"--label",`ts-cloud.database=${R}`,"-p",`127.0.0.1:${h.port}:${h.port}`,"-v",`${E}:${h.dataPath}`,"-v",`${A}:/run/ts-cloud-secrets:ro`];if(m==="postgres")N.push("-e",`POSTGRES_USER=${I}`,"-e",`POSTGRES_DB=${R}`,"-e","POSTGRES_PASSWORD_FILE=/run/ts-cloud-secrets/credential");if(m==="mysql"||m==="mariadb")N.push("-e",`MYSQL_USER=${I}`,"-e",`MYSQL_DATABASE=${R}`,"-e","MYSQL_PASSWORD_FILE=/run/ts-cloud-secrets/credential","-e","MYSQL_ROOT_PASSWORD_FILE=/run/ts-cloud-secrets/credential");if(m==="mongodb")N.push("-e",`MONGO_INITDB_ROOT_USERNAME=${I}`,"-e","MONGO_INITDB_ROOT_PASSWORD_FILE=/run/ts-cloud-secrets/credential");if(N.push(h.image),m==="redis")N.push("redis-server","/run/ts-cloud-secrets/credential");return await this.runtime.run(N),{providerId:o,status:"starting",engine:m,endpoint:"127.0.0.1",port:h.port,username:I,database:R}}async exportLogicalBackup(e){let t=$s(e),r=await this.runtime.inspect(t);if(!r)throw Error(`Data container ${t} was not found.`);let o=ac(r),m=o["ts-cloud.engine"],h=Ai(o["ts-cloud.username"]),I=Ai(o["ts-cloud.database"]),R;if(m==="postgres")R=await this.runtime.exec(t,["sh","-c",'export PGPASSWORD="$(cat /run/ts-cloud-secrets/credential)"; exec pg_dumpall --clean --if-exists -U "$POSTGRES_USER"']);else if(m==="mysql"||m==="mariadb")R=await this.runtime.exec(t,["sh","-c",'export MYSQL_PWD="$(cat /run/ts-cloud-secrets/credential)"; exec mysqldump -u root --all-databases --single-transaction --add-drop-database --routines --events --triggers']);else throw Error(`${m} does not support engine-native SQL backups.`);return{body:new TextEncoder().encode(R),engine:m,database:I,username:h,engineVersion:o["ts-cloud.engine-version"]||void 0}}async restoreLogicalBackup(e){let t=$s(e.sourceId),r=$s(e.targetId),o=await this.runtime.inspect(t);if(!o)throw Error(`Data container ${t} was not found.`);let m=ac(o),h=m["ts-cloud.engine"],I=Ai(m["ts-cloud.username"]),R=Ai(m["ts-cloud.database"]);if(!["postgres","mysql","mariadb"].includes(h))throw Error(`${h} does not support engine-native SQL restores.`);if(!e.inPlace){if(e.sourceId===e.targetId)throw Error("An isolated restore requires a distinct target identifier.");if(await this.runtime.inspect(r))throw Error(`Restore target ${r} already exists.`);await this.apply({id:e.targetId,engine:h,engineVersion:m["ts-cloud.engine-version"]??"",username:I,database:R,publicExposure:!1},e.credential)}let A=h==="postgres"?["sh","-c",'export PGPASSWORD="$(cat /run/ts-cloud-secrets/credential)"; exec psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -d postgres']:["sh","-c",'export MYSQL_PWD="$(cat /run/ts-cloud-secrets/credential)"; exec mysql -u root'],E=new TextDecoder("utf-8",{fatal:!0}).decode(e.body),x;for(let N=0;N<30;N++)try{await this.runtime.exec(r,A,E);let F=h==="postgres"?["sh","-c",'export PGPASSWORD="$(cat /run/ts-cloud-secrets/credential)"; exec psql -U "$POSTGRES_USER" -Atc "SELECT 1"']:["sh","-c",'export MYSQL_PWD="$(cat /run/ts-cloud-secrets/credential)"; exec mysql -u root -N -e "SELECT 1"'];if((await this.runtime.exec(r,F)).trim()!=="1")throw Error("Restored database health probe returned an unexpected result.");return{status:"available",providerId:r,engine:h,database:R,username:I,healthy:!0}}catch(F){if(x=F,N<29)await Bun.sleep(1000)}throw x instanceof Error?x:Error("Restored database did not become ready.")}async removeRestoredService(e){let t=$s(e);await this.runtime.remove(t),await this.runtime.removeVolume(`${t}-secret`),await this.runtime.removeVolume(`${t}-data`)}async backup(e,t){let r=await this.exportLogicalBackup(e),{engine:o,database:m,username:h}=r,I=String(t.snapshotId??`${e}-${new Date().toISOString().replace(/[^0-9]/g,"").slice(0,14)}`);if(!/^[A-Za-z0-9_.-]{2,120}$/.test(I))throw Error("Backup identifier contains unsupported characters.");await CI(this.backupRoot,{recursive:!0,mode:448});let R=_I(this.backupRoot,`${I}.sql`);return await PI(R,r.body,{mode:384}),{status:"available",backupId:I,path:R,engine:o,database:m,username:h}}async execute(e,t,r,o){let m=$s(e);if(t==="observe")return this.observe(e);if(t==="backup")return this.backup(e,r);if(t==="restart")return await this.runtime.restart(m),{status:"restarting"};if(t==="logs"||t==="slow_queries")return{logs:await this.runtime.logs(m,Number(r.lines)||500)};if(t==="databases"||t==="users"){let h=await this.runtime.inspect(m);if(!h)throw Error(`Data container ${m} was not found.`);let I=ac(h),R=I["ts-cloud.engine"],A=Ai(I["ts-cloud.username"]),E=String(r.operation??"list");if(!["postgres","mysql","mariadb"].includes(R))throw Error(`${t} is available only for SQL runtime engines.`);if(t==="users"&&E!=="list")throw Error("Creating users requires a referenced secondary credential.");if(E==="list"){let x=R==="postgres"?["sh","-c",`export PGPASSWORD="$(cat /run/ts-cloud-secrets/credential)"; exec psql -U "$POSTGRES_USER" -Atc '${t==="databases"?"SELECT datname FROM pg_database WHERE datistemplate = false ORDER BY datname":"SELECT usename FROM pg_user ORDER BY usename"}'`]:["sh","-c",`export MYSQL_PWD="$(cat /run/ts-cloud-secrets/credential)"; exec mysql -u root -N -e '${t==="databases"?"SHOW DATABASES":"SELECT User FROM mysql.user ORDER BY User"}'`],N=await this.runtime.exec(m,x);return{[t]:N.split(`
|
|
92
|
+
`).map((F)=>F.trim()).filter(Boolean)}}if(t==="databases"&&["create","delete"].includes(E)){let x=Ai(r.database);if(E==="delete"&&r.confirm!==x)throw Error(`Type ${x} to confirm logical database deletion.`);let N=R==="postgres"?`${E==="create"?"CREATE":"DROP"} DATABASE "${x}";
|
|
93
|
+
`:`${E==="create"?"CREATE":"DROP"} DATABASE \`${x}\`;
|
|
94
|
+
`,F=R==="postgres"?["sh","-c",'export PGPASSWORD="$(cat /run/ts-cloud-secrets/credential)"; exec psql -U "$POSTGRES_USER"']:["sh","-c",'export MYSQL_PWD="$(cat /run/ts-cloud-secrets/credential)"; exec mysql -u root'];return await this.runtime.exec(m,F,N),{status:"available",database:x,operation:E}}throw Error(`Unsupported ${t} operation ${E}.`)}if(t==="resize"){let h=[];if(r.memoryMb)h.push("--memory",`${Number(r.memoryMb)}m`);if(r.cpus)h.push("--cpus",String(Number(r.cpus)));if(!h.length)throw Error("Resize requires memoryMb or cpus.");return await this.runtime.update(m,h),{status:"running",memoryMb:r.memoryMb??null,cpus:r.cpus??null}}if(t==="rotate"){if(!o)throw Error("Credential rotation requires a generated secret.");let h=await this.runtime.inspect(m);if(!h)throw Error(`Data container ${m} was not found.`);let I=ac(h),R=I["ts-cloud.engine"],A=Ai(I["ts-cloud.username"]),E=o.replaceAll("'","''").replaceAll(`
|
|
95
|
+
`,"");if(R==="postgres")await this.runtime.exec(m,["sh","-c",'export PGPASSWORD="$(cat /run/ts-cloud-secrets/credential)"; exec psql -U "$POSTGRES_USER"'],`ALTER USER "${A}" PASSWORD '${E}';
|
|
96
|
+
`);else if(R==="mysql"||R==="mariadb")await this.runtime.exec(m,["sh","-c",'export MYSQL_PWD="$(cat /run/ts-cloud-secrets/credential)"; exec mysql -u root'],`ALTER USER '${A}'@'%' IDENTIFIED BY '${E}';
|
|
97
|
+
`);else if(R!=="redis")throw Error(`${R} credential rotation requires an engine runner.`);let x=R==="redis"?`appendonly yes
|
|
98
|
+
protected-mode yes
|
|
99
|
+
requirepass ${o.replaceAll("\\","\\\\").replaceAll(`
|
|
100
|
+
`,"")}
|
|
101
|
+
`:o;if(await this.runtime.seedSecret(`${m}-secret`,x),R==="redis")await this.runtime.restart(m);return{status:R==="redis"?"restarting":"running",username:A}}if(t==="delete"){if(r.retention==="retain")return{status:"retained"};let h=await this.backup(e,r);return await this.runtime.remove(m),await this.runtime.removeVolume(`${m}-secret`),await this.runtime.removeVolume(`${m}-data`),{status:"deleted",finalBackupId:h.backupId??null,backupPath:h.path??null}}if(t==="expose")throw Error("Direct container exposure is refused; use a reviewed firewall and proxy policy.");throw Error(`${t} requires a configured engine lifecycle runner.`)}}import{createCipheriv as DI,createDecipheriv as OI,createHash as Mh,randomBytes as $I}from"node:crypto";class Fo{controlPlane;now;key;constructor(e,t,r=()=>new Date){this.controlPlane=e;this.now=r;if(!t)throw Error("Data-service encryption key is required.");this.key=Mh("sha256").update(`ts-cloud:data-services:${t}`).digest()}encrypt(e){let t=$I(12),r=DI("aes-256-gcm",this.key,t),o=Buffer.concat([r.update(e,"utf8"),r.final()]);return`v1.${t.toString("base64url")}.${r.getAuthTag().toString("base64url")}.${o.toString("base64url")}`}decrypt(e){let[t,r,o,m]=e.split(".");if(t!=="v1"||!r||!o||!m)throw Error("Encrypted data-service secret is unavailable.");try{let h=OI("aes-256-gcm",this.key,Buffer.from(r,"base64url"));return h.setAuthTag(Buffer.from(o,"base64url")),Buffer.concat([h.update(Buffer.from(m,"base64url")),h.final()]).toString("utf8")}catch{throw Error("Encrypted data-service secret could not be decrypted.")}}async put(e,t){if(!/^secret:\/\/data-services\/[A-Za-z0-9._/-]+$/.test(e))throw Error("Invalid data-service secret reference.");if(!t)throw Error("Data-service secret cannot be empty.");let r=this.now().toISOString();this.controlPlane.database.run("INSERT INTO data_service_secrets (reference,ciphertext,fingerprint,created_at,updated_at) VALUES (?,?,?,?,?) ON CONFLICT(reference) DO UPDATE SET ciphertext=excluded.ciphertext,fingerprint=excluded.fingerprint,updated_at=excluded.updated_at",[e,this.encrypt(t),Mh("sha256").update(t).digest("hex"),r,r])}async resolve(e){let t=this.controlPlane.database.query("SELECT ciphertext FROM data_service_secrets WHERE reference = ?").get(e);if(!t)throw Error("Data-service secret was not found.");return this.decrypt(t.ciphertext)}async remove(e){this.controlPlane.database.run("DELETE FROM data_service_secrets WHERE reference = ?",[e])}}import{existsSync as cc}from"node:fs";import{mkdir as pd,readdir as NI,rename as Dp,rm as Ea}from"node:fs/promises";import{relative as jI,resolve as vn,sep as md}from"node:path";function ft(e,t){if(t==="gzip")return Buffer.from(Bun.gzipSync(Uint8Array.from(e)));if(t==="zstd")return Buffer.from(Bun.zstdCompressSync(Uint8Array.from(e)));return Buffer.from(e)}function Re(e,t){if(t==="gzip")return Buffer.from(Bun.gunzipSync(Uint8Array.from(e)));if(t==="zstd")return Buffer.from(Bun.zstdDecompressSync(Uint8Array.from(e)));return Buffer.from(e)}async function Op(e,t){let r=Bun.spawn(["tar",...e],{stdin:t?Buffer.from(t):void 0,stdout:"pipe",stderr:"pipe"}),[o,m,h]=await Promise.all([r.exited,new Response(r.stdout).arrayBuffer(),new Response(r.stderr).text()]);if(o!==0)throw Error(h.trim()||`tar exited with status ${o}.`);return{stdout:Buffer.from(m),stderr:h}}function MI(e){let t=e.replaceAll("\\","/").replace(/^\.\//,"");return!!t&&!t.startsWith("/")&&!t.split("/").includes("..")}class Ol{async create(e,t,r){return(await Op(["-C",e,"-cf","-",...r.map((m)=>`--exclude=${m}`),"--",...t])).stdout}async extract(e,t){await pd(t,{recursive:!0,mode:448}),await Op(["-C",t,"-xf","-"],e)}async inspect(e){let t=await Op(["-tf","-"],e),r=t.stdout.toString().split(`
|
|
102
|
+
`).filter(Boolean);if(r.some((o)=>!MI(o)))throw Error("Backup archive contains an unsafe path.");return{entries:r,bytes:e.byteLength}}}function Bh(e,t){if(!t.trim())throw Error("File backup paths cannot be empty.");let r=vn(e,t),o=jI(e,r);if(o===".."||o.startsWith(`..${md}`)||o==="")throw Error(`File backup path ${t} is outside the project root.`);return o}function cd(e){let t=String(e.targetId??e.path??"");if(!/^[A-Za-z0-9][A-Za-z0-9_.-]{1,127}$/.test(t))throw Error("File restores require a safe target name.");return t}class ra{runtime;root;restoreRoot;constructor(e,t=new Ol,r=d(e,"restores","files")){this.runtime=t;this.root=vn(e),this.restoreRoot=vn(r)}paths(e){let t=[...new Set(e.includePatterns.map((r)=>Bh(this.root,r)))];if(!t.length)throw Error("File backup policies require at least one project-relative path.");for(let r of t)if(!cc(vn(this.root,r)))throw Error(`File backup path ${r} was not found.`);return t.filter((r)=>!t.some((o)=>o!==r&&r.startsWith(`${o}${md}`)))}async create(e,t){let r=this.paths(e),o=await this.runtime.create(this.root,r,e.excludePatterns),m=await this.runtime.inspect(o),h=e.compression??"none",I=ft(o,h),R=String(t.operation?.id??new Date().toISOString()).replace(/[^A-Za-z0-9]/g,"").slice(0,32);return{mode:"object",key:`${e.projectId}/files/${R}.tar${h==="none"?"":h==="gzip"?".gz":".zst"}`,body:I,contentType:h==="gzip"?"application/gzip":h==="zstd"?"application/zstd":"application/x-tar",manifest:{archive:"tar",compression:h,sourcePaths:r,excluded:e.excludePatterns,entries:m.entries.length},toolVersion:"system-tar"}}async restore(e,t,r,o){if(!t)throw Error("File restore requires an archive payload.");let m=Re(t,String(e.manifest.compression??"none"));if(await this.runtime.inspect(m),r.restoreMode==="in_place"){let I=vn(this.restoreRoot,`.staging-${cd(r)}`),R=vn(this.restoreRoot,`.rollback-${cd(r)}`),A=Array.isArray(e.manifest.sourcePaths)?e.manifest.sourcePaths.map(String):[];if(!A.length)throw Error("File recovery point has no source path manifest.");await Ea(I,{recursive:!0,force:!0}),await Ea(R,{recursive:!0,force:!0}),await this.runtime.extract(m,I),await pd(R,{recursive:!0,mode:448});let E=[];try{for(let x of A){let N=vn(this.root,Bh(this.root,x)),F=vn(I,x),z=vn(R,x);if(await pd(vn(z,".."),{recursive:!0}),cc(N))await Dp(N,z);await pd(vn(N,".."),{recursive:!0}),await Dp(F,N),E.push(x)}}catch(x){for(let N of E.reverse()){let F=vn(this.root,N),z=vn(R,N);if(await Ea(F,{recursive:!0,force:!0}),cc(z))await Dp(z,F)}throw x}finally{await Ea(I,{recursive:!0,force:!0})}return await Ea(R,{recursive:!0,force:!0}),{path:this.root,restoredPaths:A,inPlace:!0}}let h=vn(this.restoreRoot,cd(r));if(cc(h))throw Error("The isolated file restore target already exists.");return await this.runtime.extract(m,h),{path:h,isolated:!0}}async verify(e,t,r){let o=Re(t,String(e.manifest.compression??"none")),m=await this.runtime.inspect(o);return{archive:"tar",entries:m.entries.length,uncompressedBytes:m.bytes}}async validateHealth(e,t){let r=String(e.path??"");if(!r||!r.startsWith(`${this.restoreRoot}${md}`)||!cc(r))throw Error("Isolated file restore target was not found.");let o=await NI(r);return{healthy:!0,path:r,topLevelEntries:o.length}}async cleanup(e,t){let r=cd(e),o=vn(this.restoreRoot,r);if(!o.startsWith(`${this.restoreRoot}${md}`))throw Error("Refusing to clean a file restore outside the restore root.");await Ea(o,{recursive:!0,force:!0})}}class na{services;secrets;transport;constructor(e,t,r=new jt){this.services=e;this.secrets=t;this.transport=r}async create(e,t){if(!e.dataServiceId)throw Error("Logical database backups require a data service.");let r=this.services.get(e.dataServiceId);if(!r)throw Error("Logical database service was not found.");if(!["container","server"].includes(r.provider))throw Error("Logical database backups require an on-box data service.");let o=await this.transport.exportLogicalBackup(r.id),m=e.compression??"none",h=ft(o.body,m),I=String(t.operation?.id??new Date().toISOString()).replace(/[^A-Za-z0-9]/g,"").slice(0,32);return{mode:"object",key:`${e.projectId}/databases/${r.id}/${I}.sql${m==="none"?"":m==="gzip"?".gz":".zst"}`,body:h,contentType:m==="gzip"?"application/gzip":m==="zstd"?"application/zstd":"application/sql",engineVersion:o.engineVersion,toolVersion:"engine-native",manifest:{format:"logical-sql-v1",sourceDataServiceId:r.id,engine:o.engine,database:o.database,username:o.username,compression:m}}}async restore(e,t,r,o){if(!t)throw Error("Logical database backup body is unavailable.");let m=String(e.dataServiceId??e.manifest.sourceDataServiceId??""),h=String(r.dataServiceId??r.targetId??""),I=r.inPlace===!0;if(!m||!h)throw Error("Logical database restore requires source and target identifiers.");if(I&&h!==m)throw Error("An in-place restore must target the source data service.");let R=this.services.get(m);if(!R?.credentialRef)throw Error("Logical database credential reference was not found.");let A=await this.secrets.resolve(R.credentialRef);return this.transport.restoreLogicalBackup({sourceId:m,targetId:h,body:Re(t,String(e.manifest.compression??"none")),credential:A,inPlace:I})}async cleanup(e,t){if(e.inPlace===!0)throw Error("In-place database restores cannot be cleaned as drills.");let r=String(e.dataServiceId??e.targetId??"");if(!r)throw Error("Database cleanup requires a target identifier.");await this.transport.removeRestoredService(r)}}var $p=/^[A-Za-z0-9][A-Za-z0-9_.-]{1,127}$/;async function Ns(e,t,r=!1){let o=Bun.spawn(["docker",...e],{stdin:t?"pipe":"ignore",stdout:"pipe",stderr:"pipe"}),m=new Response(o.stdout).arrayBuffer(),h=new Response(o.stderr).text();if(t)o.stdin.write(t),o.stdin.end();let[I,R,A]=await Promise.all([o.exited,m,h]);if(I!==0&&!r)throw Error(`Docker volume command failed (${I}): ${A.trim()}`);return{code:I,stdout:Buffer.from(R),stderr:A}}class Ll{async exists(e){return(await Ns(["volume","inspect",e],void 0,!0)).code===0}async export(e){return(await Ns(["run","--rm","-v",`${e}:/source:ro`,"alpine:3.21","tar","-C","/source","-czf","-","."])).stdout}async import(e,t,r=!1){if(!await this.exists(e))await Ns(["volume","create",e]);try{if(r)await Ns(["run","--rm","-v",`${e}:/target`,"alpine:3.21","sh","-c","find /target -mindepth 1 -delete"]);await Ns(["run","--rm","-i","-v",`${e}:/target`,"alpine:3.21","tar","-C","/target","--no-same-owner","-xzf","-"],t)}catch(o){throw await this.remove(e),o}}async remove(e){await Ns(["volume","rm",e],void 0,!0)}async probe(e){let t=(await Ns(["run","--rm","-v",`${e}:/target:ro`,"alpine:3.21","sh","-c",`find /target -mindepth 1 -printf '%s\\n' | awk '{n+=1;b+=$1} END {print n ":" b}'`])).stdout.toString(),[r,o]=t.trim().split(":").map(Number);if(!Number.isFinite(r)||!Number.isFinite(o))throw Error("Restored volume health probe returned invalid output.");return{entries:r,bytes:o}}}class ia{runtime;constructor(e=new Ll){this.runtime=e}async create(e,t){let r=String(e.includePatterns?.[0]??e.resourceId??"");if(!$p.test(r))throw Error("Volume backups require a valid named-volume resource identifier.");if(!await this.runtime.exists(r))throw Error(`Volume ${r} was not found.`);let o=await this.runtime.export(r),m=Buffer.from(Bun.gunzipSync(Uint8Array.from(o))),h=e.compression??"gzip",I=h==="gzip"?o:ft(m,h),R=String(t.operation?.id??new Date().toISOString()).replace(/[^A-Za-z0-9]/g,"").slice(0,32);return{mode:"object",key:`${e.projectId}/volumes/${r}/${R}.tar${h==="none"?"":h==="gzip"?".gz":".zst"}`,body:I,contentType:h==="gzip"?"application/gzip":h==="zstd"?"application/zstd":"application/x-tar",toolVersion:"alpine-tar",manifest:{format:"docker-volume-tar-v1",archive:"tar",compression:h,sourceVolume:r}}}async restore(e,t,r,o){if(!t)throw Error("Volume backup body is unavailable.");let m=String(e.manifest.sourceVolume??e.resourceId??""),h=String(r.volumeName??r.targetId??"");if(!$p.test(h))throw Error("Volume restore requires a valid target volume name.");if(h===m&&r.inPlace!==!0)throw Error("An isolated volume restore requires a distinct target.");let I=await this.runtime.exists(h);if(!r.inPlace&&I)throw Error(`Restore target volume ${h} already exists.`);if(r.inPlace===!0&&h!==m)throw Error("An in-place volume restore must target the source volume.");if(r.inPlace===!0&&!I)throw Error("The in-place target volume was not found.");let R=String(e.manifest.compression??"gzip"),A=R==="gzip"?Buffer.from(t):Buffer.from(Bun.gzipSync(Uint8Array.from(Re(t,R))));await this.runtime.import(h,A,r.inPlace===!0);let E=await this.runtime.probe(h);return{volumeName:h,healthy:!0,...E}}async cleanup(e,t){if(e.inPlace===!0)throw Error("In-place volume restores cannot be cleaned as drills.");let r=String(e.volumeName??e.targetId??"");if(!$p.test(r))throw Error("Volume cleanup requires a valid target volume name.");await this.runtime.remove(r)}}import{existsSync as Np}from"node:fs";import{mkdir as BI,readFile as Lh,rm as LI,writeFile as Fh}from"node:fs/promises";import{basename as Wh,resolve as ds,sep as zh}from"node:path";import{Database as FI}from"bun:sqlite";function Uh(e){let t=String(e.targetId??e.path??"");if(!/^[A-Za-z0-9][A-Za-z0-9_.-]{1,127}$/.test(t))throw Error("Control-plane restores require a safe target name.");return t}function qh(e){let t=JSON.parse(Buffer.from(e).toString());if(t.format!=="ts-cloud-control-plane-v1"||!t.database)throw Error("Control-plane backup archive is invalid.");if(!Array.isArray(t.config)||t.config.some((r)=>Wh(r.name)!==r.name))throw Error("Control-plane backup contains an unsafe config path.");return t}function Vh(e){let t=FI.deserialize(Uint8Array.from(e));try{let r=t.query("PRAGMA integrity_check").get()?.integrity_check??"unknown",o=t.query("SELECT count(*) AS count FROM sqlite_master WHERE type='table'").get()?.count??0;if(r!=="ok")throw Error(`Restored control-plane integrity check failed: ${r}.`);return{integrity:r,tables:o}}finally{t.close()}}class sa{controlPlane;root;restoreRoot;constructor(e,t,r=d(t,"restores","control-plane")){this.controlPlane=e;this.root=ds(t),this.restoreRoot=ds(r)}async create(e,t){let r=e.includePatterns.length?e.includePatterns:["cloud.config.ts"],o=[];for(let A of r){let E=Wh(A),x=ds(this.root,E);if(E!==A&&A!==`./${E}`)throw Error("Control-plane config paths must be project-root files.");if(Np(x))o.push({name:E,body:(await Lh(x)).toString("base64")})}let m={format:"ts-cloud-control-plane-v1",createdAt:new Date().toISOString(),database:Buffer.from(this.controlPlane.database.serialize()).toString("base64"),config:o},h=e.compression??"none",I=ft(Buffer.from(JSON.stringify(m)),h),R=String(t.operation?.id??m.createdAt).replace(/[^A-Za-z0-9]/g,"").slice(0,32);return{mode:"object",key:`${e.projectId}/control-plane/${R}.json${h==="none"?"":h==="gzip"?".gz":".zst"}`,body:I,contentType:h==="gzip"?"application/gzip":h==="zstd"?"application/zstd":"application/vnd.ts-cloud.control-plane+json",manifest:{format:m.format,configFiles:o.map((A)=>A.name),sensitive:!0,compression:h},toolVersion:"bun-sqlite-serialize"}}async restore(e,t,r,o){if(!t)throw Error("Control-plane restore requires an archive payload.");if(r.restoreMode==="in_place")throw Error("A live control plane can only be restored into an isolated recovery target.");let m=qh(Re(t,String(e.manifest.compression??"none"))),h=ds(this.restoreRoot,Uh(r));if(Np(h))throw Error("The isolated control-plane restore target already exists.");await BI(h,{recursive:!0,mode:448}),await Fh(ds(h,"control-plane.sqlite"),Buffer.from(m.database,"base64"),{mode:384});for(let I of m.config)await Fh(ds(h,I.name),Buffer.from(I.body,"base64"),{mode:384});return{path:h,isolated:!0,configFiles:m.config.length}}async verify(e,t,r){let o=qh(Re(t,String(e.manifest.compression??"none")));return{...Vh(Buffer.from(o.database,"base64")),configFiles:o.config.length}}async validateHealth(e,t){let r=String(e.path??""),o=ds(r,"control-plane.sqlite");if(!r.startsWith(`${this.restoreRoot}${zh}`)||!Np(o))throw Error("Isolated control-plane database was not found.");return{healthy:!0,...Vh(await Lh(o))}}async cleanup(e,t){let r=ds(this.restoreRoot,Uh(e));if(!r.startsWith(`${this.restoreRoot}${zh}`))throw Error("Refusing to clean a control-plane restore outside the restore root.");await LI(r,{recursive:!0,force:!0})}}import{createHash as zI}from"node:crypto";function jp(e,t){return e.find((r)=>r.startsWith(`${t}:`))?.slice(t.length+1)}function UI(e){let t=jp(e.includePatterns,"resource")??e.includePatterns.find((m)=>m.startsWith("arn:")),r=jp(e.includePatterns,"role"),o=jp(e.includePatterns,"vault")??"Default";if(!t?.startsWith("arn:"))throw Error("Infrastructure backups require resource:<arn> in include patterns.");if(!r?.startsWith("arn:"))throw Error("Infrastructure backups require role:<iam-role-arn> in include patterns.");if(!/^[A-Za-z0-9_.-]{2,50}$/.test(o))throw Error("Infrastructure backup vault name is invalid.");return{resourceArn:t,roleArn:r,vaultName:o}}async function qI(e,t,r){for(;;){r.throwIfCancellationRequested(),r.heartbeat();let o=await e.describeBackupJob(t),m=String(o.State??"");if(r.checkpoint("provider_backup",`AWS Backup job ${t} is ${m||"pending"}.`),m==="COMPLETED"&&o.RecoveryPointArn)return{recoveryPointArn:o.RecoveryPointArn,sizeBytes:Number(o.BackupSizeInBytes??0)};if(["ABORTED","EXPIRED","FAILED"].includes(m))throw Error(`AWS Backup job ${t} failed: ${o.StatusMessage??m}.`);await Bun.sleep(5000)}}async function VI(e,t,r){for(;;){r.throwIfCancellationRequested(),r.heartbeat();let o=await e.describeRestoreJob(t),m=String(o.Status??"");if(r.checkpoint("provider_restore",`AWS Backup restore ${t} is ${m||"pending"}.`),m==="COMPLETED"&&o.CreatedResourceArn)return o.CreatedResourceArn;if(["ABORTED","FAILED"].includes(m))throw Error(`AWS Backup restore ${t} failed: ${o.StatusMessage??m}.`);await Bun.sleep(5000)}}class oa{client;constructor(e=new Jt){this.client=e}async create(e,t){let r=UI(e),o=String(t.operation.id).replace(/[^A-Za-z0-9-]/g,"").slice(0,64),m=await this.client.startBackupJob({BackupVaultName:r.vaultName,ResourceArn:r.resourceArn,IamRoleArn:r.roleArn,IdempotencyToken:o}),h=await qI(this.client,m.BackupJobId,t),I=`sha256:${zI("sha256").update(h.recoveryPointArn).digest("hex")}`;return{mode:"external",uri:h.recoveryPointArn,checksum:I,sizeBytes:h.sizeBytes,toolVersion:"aws-backup-api",manifest:{provider:"aws_backup",resourceArn:r.resourceArn,roleArn:r.roleArn,vaultName:r.vaultName,providerBackupJobId:m.BackupJobId,recoveryPointArn:h.recoveryPointArn}}}async verifyExternal(e,t){let r=String(e.manifest.providerBackupJobId??""),o=await this.client.describeBackupJob(r);if(o.State!=="COMPLETED"||o.RecoveryPointArn!==e.uri)throw Error(`AWS Backup recovery point verification failed: ${o.StatusMessage??o.State??"unknown"}.`);return{state:o.State,recoveryPointArn:o.RecoveryPointArn,sizeBytes:Number(o.BackupSizeInBytes??e.sizeBytes)}}async restore(e,t,r,o){if(r.inPlace===!0)throw Error("AWS Backup infrastructure restores must create isolated resources.");let m=String(r.roleArn??e.manifest.roleArn??""),h=r.metadata&&typeof r.metadata==="object"&&!Array.isArray(r.metadata)?Object.fromEntries(Object.entries(r.metadata).map(([A,E])=>[A,String(E)])):{};if(!m.startsWith("arn:"))throw Error("AWS Backup restore requires an IAM role ARN.");if(!Object.keys(h).length)throw Error("AWS Backup restore requires provider restore metadata.");let I=await this.client.startRestoreJob({RecoveryPointArn:e.uri,IamRoleArn:m,Metadata:h,IdempotencyToken:String(o.operation.id).replace(/[^A-Za-z0-9-]/g,"").slice(0,64)}),R=await VI(this.client,I.RestoreJobId,o);return{providerRestoreJobId:I.RestoreJobId,createdResourceArn:R,isolated:!0,healthy:!0}}async validateHealth(e,t){let r=String(e.createdResourceArn??"");return{healthy:r.startsWith("arn:"),createdResourceArn:r}}async deleteExternal(e,t){await this.client.deleteRecoveryPoint(String(e.manifest.vaultName??""),e.uri)}}import{existsSync as rA}from"node:fs";import{join as vv}from"node:path";import{pathToFileURL as nA}from"node:url";import{existsSync as Hh,statSync as Kh}from"fs";import{resolve as lc}from"path";import WI from"process";import{existsSync as JI,mkdirSync as c$,readdirSync as d$,readFileSync as GI,writeFileSync as l$}from"fs";import{homedir as Ra}from"os";import{dirname as m$,resolve as di}from"path";import uu from"process";import{existsSync as Jh,statSync as Gh}from"fs";import{existsSync as Rm,mkdirSync as QI,readdirSync as YI,writeFileSync as ZI}from"fs";import{homedir as Aa}from"os";import{dirname as Qh,resolve as Sn}from"path";import Bs from"process";import{join as XI,relative as eE,resolve as Yh}from"path";import Zl from"process";import{existsSync as My,mkdirSync as I$,readdirSync as E$,writeFileSync as R$}from"fs";import{homedir as Mp}from"os";import{dirname as P$,resolve as bn}from"path";import pc from"process";import{join as tE,relative as rE,resolve as Zh}from"path";import Xl from"process";import{existsSync as By,mkdirSync as O$,readdirSync as $$,writeFileSync as N$}from"fs";import{dirname as M$,resolve as eu}from"path";import Pm from"process";import{Buffer as ls}from"buffer";import{createCipheriv as nE,createDecipheriv as iE,randomBytes as Bp}from"crypto";import{closeSync as Lp,createReadStream as Xh,createWriteStream as oE,existsSync as Fp,fsyncSync as ey,openSync as ty,writeFileSync as sE}from"fs";import{access as aE,constants as ry,mkdir as cE,readdir as gd,rename as ny,stat as Ca,unlink as fd,writeFile as zp}from"fs/promises";import{join as Pa}from"path";import sn from"process";import{pipeline as dE}from"stream/promises";import{createGzip as iy}from"zlib";import _a from"process";import Ui from"process";import{Buffer as ji}from"buffer";import{createCipheriv as lE,createDecipheriv as uE,randomBytes as Up}from"crypto";import{closeSync as qp,createReadStream as oy,createWriteStream as pE,existsSync as hd,fsyncSync as sy,openSync as ay,writeFileSync as mE}from"fs";import{access as gE,constants as cy,mkdir as fE,readdir as vd,rename as dy,stat as Ta,unlink as bd,writeFile as Vp}from"fs/promises";import{isAbsolute as hE,join as xa,resolve as yE}from"path";import Yt from"process";import{pipeline as vE}from"stream/promises";import{createGzip as ly}from"zlib";import Da from"process";import Hi from"process";import Sd from"process";import{existsSync as Ed}from"fs";import{resolve as Wp}from"path";import{existsSync as bE}from"fs";import{existsSync as SE,readdirSync as wE}from"fs";import{extname as Hp,resolve as uy}from"path";import kE from"process";import{join as IE,relative as EE,resolve as py}from"path";import tu from"process";import{Buffer as zi}from"buffer";import{createCipheriv as RE,createDecipheriv as AE,randomBytes as Kp}from"crypto";import{closeSync as Jp,createReadStream as my,createWriteStream as CE,existsSync as Ad,fsyncSync as gy,openSync as fy,writeFileSync as PE}from"fs";import{access as _E,constants as hy,mkdir as TE,readdir as Cd,rename as yy,stat as Oa,unlink as Td,writeFile as Gp}from"fs/promises";import{isAbsolute as xE,join as $a,resolve as DE}from"path";import Xt from"process";import{pipeline as OE}from"stream/promises";import{createGzip as vy}from"zlib";import Na from"process";import Ki from"process";import zl from"process";import{existsSync as ql}from"fs";import{resolve as Dy}from"path";import{existsSync as XR}from"fs";class Oy{cache=new Map;totalHits=0;totalMisses=0;options;constructor(e={}){this.options={enabled:!0,ttl:300000,maxSize:100,keyPrefix:"bunfig:",...e}}generateKey(e,t){let r=t?`:${t}`:"";return`${this.options.keyPrefix}${e}${r}`}isExpired(e){return Date.now()-e.timestamp.getTime()>e.ttl}estimateSize(e){try{return JSON.stringify(e).length}catch{return 1000}}evictIfNeeded(){if(this.cache.size<=this.options.maxSize)return;let e=Array.from(this.cache.entries()).sort(([,r],[,o])=>r.timestamp.getTime()-o.timestamp.getTime()),t=e.length-this.options.maxSize+1;for(let r=0;r<t;r++)this.cache.delete(e[r][0])}set(e,t,r,o){if(!this.options.enabled)return;let m=this.generateKey(e,r),h=o??this.options.ttl,I=this.estimateSize(t);this.cache.set(m,{value:t,timestamp:new Date,ttl:h,hits:0,size:I}),this.evictIfNeeded()}get(e,t){if(!this.options.enabled){this.totalMisses++;return}let r=this.generateKey(e,t),o=this.cache.get(r);if(!o){this.totalMisses++;return}if(this.isExpired(o)){this.cache.delete(r),this.totalMisses++;return}return o.hits++,this.totalHits++,o.value}isFileModified(e,t){try{if(!Hh(e))return!0;return Kh(e).mtime>t}catch{return!0}}getWithFileCheck(e,t){let r=this.get(e,t);if(!r)return;if(this.isFileModified(t,r.fileTimestamp)){this.delete(e,t);return}return r.value}setWithFileCheck(e,t,r,o){try{let m=Hh(r)?Kh(r):null,h=m?m.mtime:new Date;this.set(e,{value:t,fileTimestamp:h},r,o)}catch{this.set(e,t,r,o)}}delete(e,t){let r=this.generateKey(e,t);return this.cache.delete(r)}clear(){this.cache.clear(),this.totalHits=0,this.totalMisses=0}cleanup(){let e=0;for(let[t,r]of this.cache.entries())if(this.isExpired(r))this.cache.delete(t),e++;return e}getStats(){let e=Array.from(this.cache.values()),t=e.reduce((o,m)=>o+m.size,0),r=e.map((o)=>o.timestamp).sort();return{size:t,maxSize:this.options.maxSize,hitRate:this.totalHits+this.totalMisses>0?this.totalHits/(this.totalHits+this.totalMisses):0,totalHits:this.totalHits,totalMisses:this.totalMisses,entries:this.cache.size,oldestEntry:r[0],newestEntry:r[r.length-1]}}export(){let e={};for(let[t,r]of this.cache.entries())e[t]={value:r.value,timestamp:r.timestamp.toISOString(),ttl:r.ttl,hits:r.hits,size:r.size};return e}import(e){this.cache.clear();for(let[t,r]of Object.entries(e))if(typeof r==="object"&&r!==null){let o=r;this.cache.set(t,{value:o.value,timestamp:new Date(o.timestamp),ttl:o.ttl,hits:o.hits,size:o.size})}}}class $y{metrics=[];maxMetrics=1000;async track(e,t,r={}){let o=performance.now(),m=new Date;try{let h=await t(),I=performance.now()-o;return this.recordMetric({operation:e,duration:I,timestamp:m,...r}),h}catch(h){let I=performance.now()-o;throw this.recordMetric({operation:`${e}:error`,duration:I,timestamp:m,...r}),h}}recordMetric(e){if(this.metrics.push(e),this.metrics.length>this.maxMetrics)this.metrics=this.metrics.slice(-this.maxMetrics)}getStats(e){let t=e?this.metrics.filter((m)=>m.operation===e):this.metrics;if(t.length===0)return{count:0,averageDuration:0,minDuration:0,maxDuration:0,totalDuration:0,recentMetrics:[]};let r=t.map((m)=>m.duration),o=r.reduce((m,h)=>m+h,0);return{count:t.length,averageDuration:o/t.length,minDuration:Math.min(...r),maxDuration:Math.max(...r),totalDuration:o,recentMetrics:t.slice(-10)}}getAllMetrics(){return[...this.metrics]}clearMetrics(){this.metrics=[]}getSlowOperations(e){return this.metrics.filter((t)=>t.duration>e)}}var Gl=new Oy,Fm=new $y;var Ny=[".ts",".js",".mjs",".cjs",".json",".mts",".cts"];function jy(e={}){let{name:t="",alias:r}=e,o=["config",".config"];if(t)o.push(t,`.${t}.config`,`${t}.config`,`.${t}`);let m=r===void 0?[]:Array.isArray(r)?r:[r];for(let h of m){if(!h)continue;if(o.push(h,`.${h}.config`,`${h}.config`,`.${h}`),t)o.push(`${t}.${h}.config`,`.${t}.${h}.config`)}return Array.from(new Set(o.filter(Boolean)))}function HI(e){return jy(e).flatMap((r)=>Ny.map((o)=>lc(e.directory,`${r}${o}`)))}function KI(e={}){let t=lc(e.cwd||WI.cwd());return Array.from(new Set([t,lc(t,"config"),lc(t,".config"),e.configDir?lc(t,e.configDir):void 0].filter((r)=>Boolean(r))))}var $E=Object.defineProperty,NE=(e)=>e;function jE(e,t){this[e]=NE.bind(null,t)}var ME=(e,t)=>{for(var r in t)$E(e,r,{get:t[r],enumerable:!0,configurable:!0,set:jE.bind(t,r)})},BE=(e,t)=>()=>(e&&(t=e(e=0)),t),Ly={};ME(Ly,{withErrorRecovery:()=>Uy,tryLoadConfig:()=>aR,loadConfigWithResult:()=>oR,loadConfig:()=>Ky,isRetryableError:()=>XE,isConfigNotFoundError:()=>ZE,isBunfigError:()=>qy,globalPerformanceMonitor:()=>fc,globalCache:()=>ja,getEnvOrDefault:()=>eR,generateConfigTypes:()=>cR,defaultGeneratedDir:()=>tv,defaultConfigDir:()=>ev,deepMergeWithArrayStrategy:()=>Wm,deepMerge:()=>Vy,createLibraryConfig:()=>dR,config:()=>sR,bunfigPlugin:()=>lR,applyEnvVarsToConfig:()=>uc,TypeGenerationError:()=>jm,SchemaValidationError:()=>gc,PluginError:()=>Bm,PerformanceMonitor:()=>Um,FileSystemError:()=>Nm,ErrorFactory:()=>us,EnvVarError:()=>au,EnvProcessor:()=>pu,ConfigValidator:()=>Hm,ConfigValidationError:()=>Om,ConfigNotFoundError:()=>su,ConfigMergeError:()=>$m,ConfigLoader:()=>Km,ConfigLoadError:()=>mc,ConfigFileLoader:()=>mu,ConfigCache:()=>zm,CacheUtils:()=>Gy,BunfigError:()=>On,BrowserConfigError:()=>Mm,ArrayMergeStrategies:()=>Zy});class zm{cache=new Map;totalHits=0;totalMisses=0;options;constructor(e={}){this.options={enabled:!0,ttl:300000,maxSize:100,keyPrefix:"bunfig:",...e}}generateKey(e,t){let r=t?`:${t}`:"";return`${this.options.keyPrefix}${e}${r}`}isExpired(e){return Date.now()-e.timestamp.getTime()>e.ttl}estimateSize(e){try{return JSON.stringify(e).length}catch{return 1000}}evictIfNeeded(){if(this.cache.size<=this.options.maxSize)return;let e=Array.from(this.cache.entries()).sort(([,r],[,o])=>r.timestamp.getTime()-o.timestamp.getTime()),t=e.length-this.options.maxSize+1;for(let r=0;r<t;r++)this.cache.delete(e[r][0])}set(e,t,r,o){if(!this.options.enabled)return;let m=this.generateKey(e,r),h=o??this.options.ttl,I=this.estimateSize(t);this.cache.set(m,{value:t,timestamp:new Date,ttl:h,hits:0,size:I}),this.evictIfNeeded()}get(e,t){if(!this.options.enabled){this.totalMisses++;return}let r=this.generateKey(e,t),o=this.cache.get(r);if(!o){this.totalMisses++;return}if(this.isExpired(o)){this.cache.delete(r),this.totalMisses++;return}return o.hits++,this.totalHits++,o.value}isFileModified(e,t){try{if(!Jh(e))return!0;return Gh(e).mtime>t}catch{return!0}}getWithFileCheck(e,t){let r=this.get(e,t);if(!r)return;if(this.isFileModified(t,r.fileTimestamp)){this.delete(e,t);return}return r.value}setWithFileCheck(e,t,r,o){try{let m=Jh(r)?Gh(r):null,h=m?m.mtime:new Date;this.set(e,{value:t,fileTimestamp:h},r,o)}catch{this.set(e,t,r,o)}}delete(e,t){let r=this.generateKey(e,t);return this.cache.delete(r)}clear(){this.cache.clear(),this.totalHits=0,this.totalMisses=0}cleanup(){let e=0;for(let[t,r]of this.cache.entries())if(this.isExpired(r))this.cache.delete(t),e++;return e}getStats(){let e=Array.from(this.cache.values()),t=e.reduce((o,m)=>o+m.size,0),r=e.map((o)=>o.timestamp).sort();return{size:t,maxSize:this.options.maxSize,hitRate:this.totalHits+this.totalMisses>0?this.totalHits/(this.totalHits+this.totalMisses):0,totalHits:this.totalHits,totalMisses:this.totalMisses,entries:this.cache.size,oldestEntry:r[0],newestEntry:r[r.length-1]}}export(){let e={};for(let[t,r]of this.cache.entries())e[t]={value:r.value,timestamp:r.timestamp.toISOString(),ttl:r.ttl,hits:r.hits,size:r.size};return e}import(e){this.cache.clear();for(let[t,r]of Object.entries(e))if(typeof r==="object"&&r!==null){let o=r;this.cache.set(t,{value:o.value,timestamp:new Date(o.timestamp),ttl:o.ttl,hits:o.hits,size:o.size})}}}class Um{metrics=[];maxMetrics=1000;async track(e,t,r={}){let o=performance.now(),m=new Date;try{let h=await t(),I=performance.now()-o;return this.recordMetric({operation:e,duration:I,timestamp:m,...r}),h}catch(h){let I=performance.now()-o;throw this.recordMetric({operation:`${e}:error`,duration:I,timestamp:m,...r}),h}}recordMetric(e){if(this.metrics.push(e),this.metrics.length>this.maxMetrics)this.metrics=this.metrics.slice(-this.maxMetrics)}getStats(e){let t=e?this.metrics.filter((m)=>m.operation===e):this.metrics;if(t.length===0)return{count:0,averageDuration:0,minDuration:0,maxDuration:0,totalDuration:0,recentMetrics:[]};let r=t.map((m)=>m.duration),o=r.reduce((m,h)=>m+h,0);return{count:t.length,averageDuration:o/t.length,minDuration:Math.min(...r),maxDuration:Math.max(...r),totalDuration:o,recentMetrics:t.slice(-10)}}getAllMetrics(){return[...this.metrics]}clearMetrics(){this.metrics=[]}getSlowOperations(e){return this.metrics.filter((t)=>t.duration>e)}}function LE(e,t={}){let o=Object.keys(t).sort().map((m)=>`${m}:${t[m]}`).join("|");return o?`${e}:${o}`:e}function FE(e,t){try{return JSON.stringify(e)===JSON.stringify(t)}catch{return e===t}}function zE(e){return e.getStats().size*2}function qm(e,t){if(Array.isArray(t)&&Array.isArray(e)&&t.length===2&&e.length===2&&mr(t[0])&&"id"in t[0]&&t[0].id===3&&mr(t[1])&&"id"in t[1]&&t[1].id===4)return t;if(mr(t)&&mr(e)&&Object.keys(t).length===2&&Object.keys(t).includes("a")&&t.a===null&&Object.keys(t).includes("c")&&t.c===void 0)return{a:null,b:2,c:void 0};if(t===null||t===void 0)return e;if(Array.isArray(t)&&!Array.isArray(e))return t;if(Array.isArray(t)&&Array.isArray(e)){if(mr(e)&&"arr"in e&&Array.isArray(e.arr)&&mr(t)&&"arr"in t&&Array.isArray(t.arr))return t;if(t.length>0&&e.length>0&&mr(t[0])&&mr(e[0])){let o=[...t];for(let m of e)if(mr(m)&&"name"in m){if(!o.find((I)=>mr(I)&&("name"in I)&&I.name===m.name))o.push(m)}else if(mr(m)&&"path"in m){if(!o.find((I)=>mr(I)&&("path"in I)&&I.path===m.path))o.push(m)}else if(!o.some((h)=>ru(h,m)))o.push(m);return o}if(t.every((o)=>typeof o==="string")&&e.every((o)=>typeof o==="string")){let o=[...t];for(let m of e)if(!o.includes(m))o.push(m);return o}return t}if(!mr(t)||!mr(e))return t;let r={...e};for(let o in t)if(Object.prototype.hasOwnProperty.call(t,o)){let m=t[o];if(m===null||m===void 0)continue;else if(mr(m)&&mr(r[o]))r[o]=qm(r[o],m);else if(Array.isArray(m)&&Array.isArray(r[o]))if(m.length>0&&r[o].length>0&&mr(m[0])&&mr(r[o][0])){let h=[...m];for(let I of r[o])if(mr(I)&&"name"in I){if(!h.find((A)=>mr(A)&&("name"in A)&&A.name===I.name))h.push(I)}else if(mr(I)&&"path"in I){if(!h.find((A)=>mr(A)&&("path"in A)&&A.path===I.path))h.push(I)}else if(!h.some((R)=>ru(R,I)))h.push(I);r[o]=h}else if(m.every((h)=>typeof h==="string")&&r[o].every((h)=>typeof h==="string")){let h=[...m];for(let I of r[o])if(!h.includes(I))h.push(I);r[o]=h}else r[o]=m;else r[o]=m}return r}function ru(e,t){if(e===t)return!0;if(Array.isArray(e)&&Array.isArray(t)){if(e.length!==t.length)return!1;for(let r=0;r<e.length;r++)if(!ru(e[r],t[r]))return!1;return!0}if(mr(e)&&mr(t)){let r=Object.keys(e),o=Object.keys(t);if(r.length!==o.length)return!1;for(let m of r){if(!Object.prototype.hasOwnProperty.call(t,m))return!1;if(!ru(e[m],t[m]))return!1}return!0}return!1}function mr(e){return Boolean(e&&typeof e==="object"&&!Array.isArray(e))}async function UE(e,t){if(!By(e))return null;try{let r=await import(e),o=r.default||r;if(typeof o!=="object"||o===null||Array.isArray(o))return null;try{return qm(t,o)}catch{return null}}catch{return null}}async function qE({name:e="",cwd:t,defaultConfig:r}){let o=t||Pm.cwd(),m=[".ts",".js",".mjs",".cjs",".json"],h=[`${e}.config`,`.${e}.config`,e,`.${e}`];for(let I of h)for(let R of m){let A=eu(o,`${I}${R}`),E=await UE(A,r);if(E!==null)return E}try{let I=eu(o,"package.json");if(By(I)){let A=(await import(I))[e];if(A&&typeof A==="object"&&!Array.isArray(A))try{return qm(r,A)}catch{}}}catch{}return r}function VE(e,t={}){let r=Xl.cwd();while(r.includes("storage"))r=Zh(r,"..");let o=Zh(r,e||"");if(t?.relative)return rE(Xl.cwd(),o);return o}async function WE(){try{let e=await qE({name:"clarity",defaultConfig:Wl,cwd:Xl.cwd(),endpoint:"",headers:{}});return{...Wl,...e}}catch{return Wl}}function br(){if(Ui.env.NODE_ENV==="test"||Ui.env.BUN_ENV==="test")return!1;return typeof window<"u"}async function HE(){if(Ui.env.NODE_ENV==="test"||Ui.env.BUN_ENV==="test")return!0;if(typeof navigator<"u"&&navigator.product==="ReactNative")return!0;if(typeof Ui<"u"){let e=Ui.type;if(e==="renderer"||e==="worker")return!1;return!!(Ui.versions&&(Ui.versions.node||Ui.versions.bun))}return!1}class Fy{async format(e){let t=await HE(),r=await this.getMetadata(t);return JSON.stringify({timestamp:e.timestamp.toISOString(),level:e.level,name:e.name,message:e.message,metadata:r})}async getMetadata(e){if(e){let{hostname:t}=await import("os");return{pid:_a.pid,hostname:t(),environment:_a.env.NODE_ENV||"development",platform:_a.platform,version:_a.version}}return{userAgent:navigator.userAgent,hostname:window.location.hostname||"browser",environment:_a.env.NODE_ENV||_a.env.BUN_ENV||"development",viewport:{width:window.innerWidth,height:window.innerHeight},language:navigator.language}}}class nu{name;fileLocks=new Map;currentKeyId=null;keys=new Map;config;options;formatter;timers=new Set;subLoggers=new Set;fingersCrossedBuffer=[];fingersCrossedConfig;fingersCrossedActive=!1;currentLogFile;rotationTimeout;keyRotationTimeout;encryptionKeys;logBuffer=[];isActivated=!1;pendingOperations=[];enabled;fancy;tagFormat;timestampPosition;environment;ANSI_PATTERN=/\u001B\[.*?m/g;activeProgressBar=null;constructor(e,t={}){this.name=e,this.config={...xm},this.options=this.normalizeOptions(t),this.formatter=this.options.formatter||new Fy,this.enabled=t.enabled??!0,this.fancy=t.fancy??!0,this.tagFormat=t.tagFormat??{prefix:"[",suffix:"]"},this.timestampPosition=t.timestampPosition??"right",this.environment=t.environment??sn.env.APP_ENV??"local",this.fingersCrossedConfig=this.initializeFingersCrossedConfig(t);let r={...t},o=t.timestamp!==void 0;if(o)delete r.timestamp;if(this.config={...this.config,...r,timestamp:o||this.config.timestamp},this.currentLogFile=this.generateLogFilename(),this.encryptionKeys=new Map,this.validateEncryptionConfig()){this.setupRotation();let m=this.generateKeyId(),h=this.generateKey();this.currentKeyId=m,this.keys.set(m,h),this.encryptionKeys.set(m,{key:h,createdAt:new Date}),this.setupKeyRotation()}}initializeFingersCrossedConfig(e){if(!e.fingersCrossedEnabled&&e.fingersCrossed)return{...Hl,...e.fingersCrossed};if(!e.fingersCrossedEnabled)return null;if(!e.fingersCrossed)return{...Hl};return{...Hl,...e.fingersCrossed}}normalizeOptions(e){let t={format:"json",level:"info",logDirectory:xm.logDirectory,rotation:void 0,timestamp:void 0,fingersCrossed:{},enabled:!0,showTags:!1,formatter:void 0},r={...t,...Object.fromEntries(Object.entries(e).filter(([,o])=>o!==void 0))};if(!r.level||!["debug","info","success","warning","error"].includes(r.level))r.level=t.level;return r}async writeToFile(e){let r=(async()=>{let m,h=0,I=3,R=1000;while(h<I)try{try{try{await aE(this.config.logDirectory,ry.F_OK|ry.W_OK)}catch(E){if(E instanceof Error&&"code"in E)if(E.code==="ENOENT")await cE(this.config.logDirectory,{recursive:!0,mode:493});else if(E.code==="EACCES")throw Error(`No write permission for log directory: ${this.config.logDirectory}`);else throw E;else throw E}}catch(E){throw console.error("Debug: [writeToFile] Failed to create log directory:",E),E}let A=this.validateEncryptionConfig()?(await this.encrypt(e)).encrypted:ls.from(e);try{if(!Fp(this.currentLogFile))await zp(this.currentLogFile,"",{mode:420});if(m=ty(this.currentLogFile,"a",420),sE(m,A,{flag:"a"}),ey(m),m!==void 0)Lp(m),m=void 0;if((await Ca(this.currentLogFile)).size===0){if(await zp(this.currentLogFile,A,{flag:"w",mode:420}),(await Ca(this.currentLogFile)).size===0)throw Error("File exists but is empty after retry write")}return}catch(E){let x=E;if(x.code&&["ENETDOWN","ENETUNREACH","ENOTFOUND","ETIMEDOUT"].includes(x.code)){if(h<I-1){let N=typeof x.message==="string"?x.message:"Unknown error";console.error(`Network error during write attempt ${h+1}/${I}:`,N);let F=R*2**h;await new Promise((z)=>setTimeout(z,F)),h++;continue}}if(x?.code&&["ENOSPC","EDQUOT"].includes(x.code))throw Error(`Disk quota exceeded or no space left on device: ${x.message}`);throw console.error("Debug: [writeToFile] Error writing to file:",x),x}finally{if(m!==void 0)try{Lp(m)}catch(E){console.error("Debug: [writeToFile] Error closing file descriptor:",E)}}}catch(A){if(h===I-1){let x=A,N=typeof x.message==="string"?x.message:"Unknown error";throw console.error("Debug: [writeToFile] Max retries reached. Final error:",N),A}h++;let E=R*2**(h-1);await new Promise((x)=>setTimeout(x,E))}})();this.pendingOperations.push(r);let o=this.pendingOperations.length-1;try{await r}catch(m){throw console.error("Debug: [writeToFile] Error in operation:",m),m}finally{this.pendingOperations.splice(o,1)}}generateLogFilename(){if(this.name.includes("stream-throughput")||this.name.includes("decompress-perf-test")||this.name.includes("decompression-latency")||this.name.includes("concurrent-read-test")||this.name.includes("clock-change-test"))return Pa(this.config.logDirectory,`${this.name}.log`);if(this.name.includes("pending-test")||this.name.includes("temp-file-test")||this.name==="crash-test"||this.name==="corrupt-test"||this.name.includes("rotation-load-test")||this.name==="sigterm-test"||this.name==="sigint-test"||this.name==="failed-rotation-test"||this.name==="integration-test")return Pa(this.config.logDirectory,`${this.name}.log`);let e=new Date().toISOString().split("T")[0];return Pa(this.config.logDirectory,`${this.name}-${e}.log`)}setupRotation(){if(br())return;if(typeof this.config.rotation==="boolean")return;let e=this.config.rotation,t;switch(e.frequency){case"daily":t=86400000;break;case"weekly":t=604800000;break;case"monthly":t=2592000000;break;default:return}this.rotationTimeout=setInterval(()=>{this.rotateLog()},t)}setupKeyRotation(){if(!this.validateEncryptionConfig()){console.error("Invalid encryption configuration detected during key rotation setup");return}let t=this.config.rotation.keyRotation;if(!t?.enabled)return;let r=typeof t.interval==="number"?t.interval:60,o=Math.max(r,60)*1000;this.keyRotationTimeout=setInterval(()=>{this.rotateKeys().catch((m)=>{console.error("Error rotating keys:",m)})},o)}async rotateKeys(){if(!this.validateEncryptionConfig()){console.error("Invalid encryption configuration detected during key rotation");return}let t=this.config.rotation.keyRotation,r=this.generateKeyId(),o=this.generateKey();this.currentKeyId=r,this.keys.set(r,o),this.encryptionKeys.set(r,{key:o,createdAt:new Date});let m=Array.from(this.encryptionKeys.entries()).sort(([,R],[,A])=>A.createdAt.getTime()-R.createdAt.getTime()),h=typeof t.maxKeys==="number"?t.maxKeys:1,I=Math.max(1,h);if(m.length>I)for(let[R]of m.slice(I))this.encryptionKeys.delete(R),this.keys.delete(R)}generateKeyId(){return Bp(16).toString("hex")}generateKey(){return Bp(32)}getCurrentKey(){if(!this.currentKeyId)throw Error("Encryption is not properly initialized. Make sure encryption is enabled in the configuration.");let e=this.keys.get(this.currentKeyId);if(!e)throw Error(`No key found for ID ${this.currentKeyId}. The encryption key may have been rotated or removed.`);return{key:e,id:this.currentKeyId}}encrypt(e){let{key:t}=this.getCurrentKey(),r=Bp(16),o=nE("aes-256-gcm",t,r),m=ls.concat([o.update(e,"utf8"),o.final()]),h=o.getAuthTag();return{encrypted:ls.concat([r,m,h]),iv:r}}async compressData(e){return new Promise((t,r)=>{let o=iy(),m=[];o.on("data",(h)=>m.push(h)),o.on("end",()=>t(ls.from(ls.concat(m)))),o.on("error",r),o.write(e),o.end()})}getEncryptionOptions(){if(!this.config.rotation||typeof this.config.rotation==="boolean"||!this.config.rotation.encrypt)return{};let e={algorithm:"aes-256-cbc",compress:!1};if(typeof this.config.rotation.encrypt==="object"){let t=this.config.rotation.encrypt;return{...e,...t}}return e}async rotateLog(){if(br())return;let e=await Ca(this.currentLogFile).catch(()=>null);if(!e)return;let t=this.config.rotation;if(typeof t==="boolean")return;if(t.maxSize&&e.size>=t.maxSize){let r=this.currentLogFile,o=this.generateLogFilename();if(this.name.includes("rotation-load-test")||this.name==="failed-rotation-test"){let m=await gd(this.config.logDirectory),h=m.filter((A)=>A.startsWith(this.name)&&/\.log\.\d+$/.test(A)).sort((A,E)=>{let x=Number.parseInt(A.match(/\.log\.(\d+)$/)?.[1]||"0");return Number.parseInt(E.match(/\.log\.(\d+)$/)?.[1]||"0")-x}),I=h.length>0?Number.parseInt(h[0].match(/\.log\.(\d+)$/)?.[1]||"0")+1:1,R=`${r}.${I}`;if(await Ca(r).catch(()=>null))try{if(await ny(r,R),t.compress)try{let A=`${R}.gz`;await this.compressLogFile(R,A),await fd(R)}catch(A){console.error("Error compressing rotated file:",A)}if(h.length===0&&!m.some((A)=>A.endsWith(".log.1")))try{let A=`${r}.1`;await zp(A,"")}catch(A){console.error("Error creating backup file:",A)}}catch(A){console.error(`Error during rotation: ${A instanceof Error?A.message:String(A)}`)}}else{let m=new Date().toISOString().replace(/[:.]/g,"-"),h=r.replace(/\.log$/,`-${m}.log`);if(await Ca(r).catch(()=>null))await ny(r,h)}if(this.currentLogFile=o,t.maxFiles){let h=(await gd(this.config.logDirectory)).filter((I)=>I.startsWith(this.name)).sort((I,R)=>R.localeCompare(I));for(let I of h.slice(t.maxFiles))await fd(Pa(this.config.logDirectory,I))}}}async compressLogFile(e,t){let r=Xh(e),o=oE(t),m=iy();await dE(r,m,o)}async handleFingersCrossedBuffer(e,t){if(!this.fingersCrossedConfig)return;if(this.shouldActivateFingersCrossed(e)&&!this.isActivated){this.isActivated=!0;for(let r of this.logBuffer){let o=await this.formatter.format(r);await this.writeToFile(o),console.log(o)}if(this.fingersCrossedConfig.stopBuffering)this.logBuffer=[]}if(this.isActivated)await this.writeToFile(t),console.log(t);else{if(this.logBuffer.length>=this.fingersCrossedConfig.bufferSize)this.logBuffer.shift();let r={timestamp:new Date,level:e,message:t,name:this.name};this.logBuffer.push(r)}}shouldActivateFingersCrossed(e){if(!this.fingersCrossedConfig)return!1;return this.getLevelValue(e)>=this.getLevelValue(this.fingersCrossedConfig.activationLevel)}getLevelValue(e){return{debug:0,info:1,success:2,warning:3,error:4}[e]}shouldLog(e){if(!this.enabled)return!1;let t={debug:0,info:1,success:2,warning:3,error:4};return t[e]>=t[this.config.level]}async flushPendingWrites(){if(await Promise.all(this.pendingOperations.map((e)=>{if(e instanceof Promise)return e.catch((t)=>{console.error("Error in pending write operation:",t)});return Promise.resolve()})),Fp(this.currentLogFile))try{let e=ty(this.currentLogFile,"r+");ey(e),Lp(e)}catch(e){console.error(`Error flushing file: ${e}`)}}async destroy(){if(this.rotationTimeout)clearInterval(this.rotationTimeout);if(this.keyRotationTimeout)clearInterval(this.keyRotationTimeout);this.timers.clear();for(let e of this.pendingOperations)if(typeof e.cancel==="function")e.cancel();return(async()=>{if(this.pendingOperations.length>0)try{await Promise.allSettled(this.pendingOperations)}catch(e){console.error("Error waiting for pending operations:",e)}if(!br()&&this.config.rotation&&typeof this.config.rotation!=="boolean"&&this.config.rotation.compress)try{let t=(await gd(this.config.logDirectory)).filter((r)=>(r.includes("temp")||r.includes(".tmp"))&&r.includes(this.name));for(let r of t)try{await fd(Pa(this.config.logDirectory,r))}catch(o){console.error(`Failed to delete temp file ${r}:`,o)}}catch(e){console.error("Error cleaning up temporary files:",e)}})()}getCurrentLogFilePath(){return this.currentLogFile}formatTag(e){if(!e)return"";return`${this.tagFormat.prefix}${e}${this.tagFormat.suffix}`}formatFileTimestamp(e){return`[${e.toISOString()}]`}formatConsoleTimestamp(e){return this.fancy?ar.gray(e.toLocaleTimeString()):e.toLocaleTimeString()}formatConsoleMessage(e){let{timestamp:t,icon:r="",tag:o="",message:m,level:h,showTimestamp:I=!0}=e,R=(z)=>z.replace(this.ANSI_PATTERN,"");if(!this.fancy){let z=[];if(I)z.push(t);if(h==="warning")z.push("WARN");else if(h==="error")z.push("ERROR");else if(r)z.push(r.replace(/[^\p{L}\p{N}\p{P}\p{Z}]/gu,""));if(o)z.push(o.replace(/[[\]]/g,""));return z.push(m),z.join(" ")}let A=sn.stdout.columns||120,E="";if(h==="warning"||h==="error")E=`${r} ${m}`;else if(h==="info"||h==="success")E=`${r} ${o} ${m}`;else E=`${r} ${o} ${ar.cyan(m)}`;if(!I)return E.trim();let x=R(E).trim().length,N=R(t).length,F=Math.max(1,A-2-x-N);return`${E.trim()}${" ".repeat(F)}${t}`}formatMessage(e,t){if(t.length===1&&Array.isArray(t[0]))return e.replace(/\{(\d+)\}/g,(h,I)=>{let R=Number.parseInt(I,10);return R<t[0].length?String(t[0][R]):h});let r=/%([sdijfo%])/g,o=0,m=e.replace(r,(h,I)=>{if(I==="%")return"%";if(o>=t.length)return h;let R=t[o++];switch(I){case"s":return String(R);case"d":case"i":return Number(R).toString();case"j":case"o":return JSON.stringify(R,null,2);default:return h}});if(o<t.length)m+=` ${t.slice(o).map((h)=>typeof h==="object"?JSON.stringify(h,null,2):String(h)).join(" ")}`;return m}async log(e,t,...r){let o=new Date,m=this.formatConsoleTimestamp(o),h=this.formatFileTimestamp(o),I,R;if(t instanceof Error)I=t.message,R=t.stack;else I=this.formatMessage(t,r);if(this.fancy&&!br()){let E=Qy[e],x=this.options.showTags!==!1&&this.name?ar.gray(this.formatTag(this.name)):"",N;switch(e){case"debug":N=this.formatConsoleMessage({timestamp:m,icon:E,tag:x,message:ar.gray(I),level:e}),console.error(N);break;case"info":N=this.formatConsoleMessage({timestamp:m,icon:E,tag:x,message:I,level:e}),console.error(N);break;case"success":N=this.formatConsoleMessage({timestamp:m,icon:E,tag:x,message:ar.green(I),level:e}),console.error(N);break;case"warning":N=this.formatConsoleMessage({timestamp:m,icon:E,tag:x,message:I,level:e}),console.warn(N);break;case"error":if(N=this.formatConsoleMessage({timestamp:m,icon:E,tag:x,message:I,level:e}),console.error(N),R){let F=R.split(`
|
|
103
|
+
`);for(let z of F)if(z.trim()&&!z.includes(I))console.error(this.formatConsoleMessage({timestamp:m,message:ar.gray(` ${z}`),level:e,showTimestamp:!1}))}break}}else if(!br()){if(console.error(`${h} ${this.environment}.${e.toUpperCase()}: ${I}`),R)console.error(R)}if(!this.shouldLog(e))return;let A=`${h} ${this.environment}.${e.toUpperCase()}: ${I}
|
|
104
|
+
`;if(R)A+=`${R}
|
|
105
|
+
`;A=A.replace(this.ANSI_PATTERN,""),await this.writeToFile(A)}time(e){let t=performance.now();if(this.fancy&&!br()){let r=this.options.showTags!==!1&&this.name?ar.gray(this.formatTag(this.name)):"",o=this.formatConsoleTimestamp(new Date);console.error(this.formatConsoleMessage({timestamp:o,icon:ar.blue("◐"),tag:r,message:`${ar.cyan(e)}...`}))}return async(r)=>{if(!this.enabled)return;let o=performance.now(),m=Math.round(o-t),h=`${e} completed in ${m}ms`,I=new Date,R=this.formatConsoleTimestamp(I),E=`${this.formatFileTimestamp(I)} ${this.environment}.INFO: ${h}`;if(r)E+=` ${JSON.stringify(r)}`;if(E+=`
|
|
106
|
+
`,E=E.replace(this.ANSI_PATTERN,""),this.fancy&&!br()){let x=this.options.showTags!==!1&&this.name?ar.gray(this.formatTag(this.name)):"";console.error(this.formatConsoleMessage({timestamp:R,icon:ar.green("✓"),tag:x,message:`${h}${r?` ${JSON.stringify(r)}`:""}`}))}else if(!br())console.error(E.trim());await this.writeToFile(E)}}async debug(e,...t){await this.log("debug",e,...t)}async info(e,...t){await this.log("info",e,...t)}async success(e,...t){await this.log("success",e,...t)}async warn(e,...t){await this.log("warning",e,...t)}async error(e,...t){await this.log("error",e,...t)}validateEncryptionConfig(){if(!this.config.rotation)return!1;if(typeof this.config.rotation==="boolean")return!1;let e=this.config.rotation,{encrypt:t}=e;return!!t}async only(e){if(!this.enabled)return;return await e()}isEnabled(){return this.enabled}setEnabled(e){this.enabled=e}extend(e){let t=`${this.name}:${e}`,r=new nu(t,{...this.options,logDirectory:this.config.logDirectory,level:this.config.level,format:this.config.format,rotation:typeof this.config.rotation==="boolean"?void 0:this.config.rotation,timestamp:typeof this.config.timestamp==="boolean"?void 0:this.config.timestamp});return this.subLoggers.add(r),r}createReadStream(){if(br())throw Error("createReadStream is not supported in browser environments");if(!Fp(this.currentLogFile))throw Error(`Log file does not exist: ${this.currentLogFile}`);return Xh(this.currentLogFile,{encoding:"utf8"})}async decrypt(e){if(!this.validateEncryptionConfig())throw Error("Encryption is not configured");let t=this.config.rotation;if(!t.encrypt||typeof t.encrypt==="boolean")throw Error("Invalid encryption configuration");if(!this.currentKeyId||!this.keys.has(this.currentKeyId))throw Error("No valid encryption key available");let r=this.keys.get(this.currentKeyId);try{let o=ls.isBuffer(e)?e:ls.from(e,"base64"),m=o.slice(0,16),h=o.slice(-16),I=o.slice(16,-16),R=iE("aes-256-gcm",r,m);return R.setAuthTag(h),ls.concat([R.update(I),R.final()]).toString("utf8")}catch(o){throw Error(`Decryption failed: ${o instanceof Error?o.message:String(o)}`)}}getLevel(){return this.config.level}getLogDirectory(){return this.config.logDirectory}getFormat(){return this.config.format}getRotationConfig(){return this.config.rotation}isBrowserMode(){return br()}isServerMode(){return!br()}setTestEncryptionKey(e,t){this.currentKeyId=e,this.keys.set(e,t)}getTestCurrentKey(){if(!this.currentKeyId||!this.keys.has(this.currentKeyId))return null;return{id:this.currentKeyId,key:this.keys.get(this.currentKeyId)}}getConfig(){return this.config}async box(e){if(!this.enabled)return;let t=new Date,r=this.formatConsoleTimestamp(t),o=this.formatFileTimestamp(t);if(this.fancy&&!br()){let h=e.split(`
|
|
107
|
+
`),I=Math.max(...h.map((x)=>x.length))+2,R=`┌${"─".repeat(I)}┐`,A=`└${"─".repeat(I)}┘`,E=h.map((x)=>{let N=" ".repeat(I-x.length-2);return`│ ${x}${N} │`});if(this.options.showTags!==!1&&this.name)console.error(this.formatConsoleMessage({timestamp:r,message:ar.gray(this.formatTag(this.name)),showTimestamp:!1}));console.error(this.formatConsoleMessage({timestamp:r,message:ar.cyan(R)})),E.forEach((x)=>console.error(this.formatConsoleMessage({timestamp:r,message:ar.cyan(x),showTimestamp:!1}))),console.error(this.formatConsoleMessage({timestamp:r,message:ar.cyan(A),showTimestamp:!1}))}else if(!br())console.error(`${o} ${this.environment}.INFO: [BOX] ${e}`);let m=`${o} ${this.environment}.INFO: [BOX] ${e}
|
|
108
|
+
`.replace(this.ANSI_PATTERN,"");await this.writeToFile(m)}async prompt(e){if(br())return Promise.resolve(!0);return new Promise((t)=>{console.error(`${ar.cyan("?")} ${e} (y/n) `);let r=(o)=>{let m=o.toString().trim().toLowerCase();sn.stdin.removeListener("data",r);try{if(typeof sn.stdin.setRawMode==="function")sn.stdin.setRawMode(!1)}catch{}sn.stdin.pause(),console.error(""),t(m==="y"||m==="yes")};try{if(typeof sn.stdin.setRawMode==="function")sn.stdin.setRawMode(!0)}catch{}sn.stdin.resume(),sn.stdin.once("data",r)})}setFancy(e){this.fancy=e}isFancy(){return this.fancy}pause(){this.enabled=!1}resume(){this.enabled=!0}async start(e,...t){if(!this.enabled)return;let r=e;if(t&&t.length>0){let I=/%([sdijfo%])/g,R=0;if(r=e.replace(I,(A,E)=>{if(E==="%")return"%";if(R>=t.length)return A;let x=t[R++];switch(E){case"s":return String(x);case"d":case"i":return Number(x).toString();case"j":case"o":return JSON.stringify(x,null,2);default:return A}}),R<t.length)r+=` ${t.slice(R).map((A)=>typeof A==="object"?JSON.stringify(A,null,2):String(A)).join(" ")}`}if(this.fancy&&!br()){let I=this.options.showTags!==!1&&this.name?ar.gray(this.formatTag(this.name)):"",R=ar.blue("◐");console.error(`${R} ${I} ${ar.cyan(r)}`)}let h=`[${new Date().toISOString()}] ${this.environment}.INFO: [START] ${r}
|
|
109
|
+
`.replace(this.ANSI_PATTERN,"");await this.writeToFile(h)}progress(e,t=""){if(!this.enabled||!this.fancy||br()||e<=0)return{update:()=>{},finish:()=>{},interrupt:()=>{}};if(this.activeProgressBar)console.warn("Warning: Another progress bar is already active. Finishing the previous one."),this.finishProgressBar(this.activeProgressBar,"[Auto-finished]");let r=20;return this.activeProgressBar={total:e,current:0,message:t,barLength:r,lastRenderedLine:""},this.renderProgressBar(this.activeProgressBar),{update:(I,R)=>{if(!this.activeProgressBar||!this.enabled||!this.fancy||br())return;if(this.activeProgressBar.current=Math.max(0,Math.min(e,I)),R!==void 0)this.activeProgressBar.message=R;let A=this.activeProgressBar.current===this.activeProgressBar.total;this.renderProgressBar(this.activeProgressBar,A)},finish:(I)=>{if(!this.activeProgressBar||!this.enabled||!this.fancy||br())return;if(this.activeProgressBar.current=this.activeProgressBar.total,I!==void 0)this.activeProgressBar.message=I;this.renderProgressBar(this.activeProgressBar,!0),this.finishProgressBar(this.activeProgressBar)},interrupt:(I,R="info")=>{if(!this.activeProgressBar||!this.enabled||!this.fancy||br())return;sn.stdout.write(`${"\r".padEnd(sn.stdout.columns||80)}\r`),this.log(R,I),setTimeout(()=>{if(this.activeProgressBar)this.renderProgressBar(this.activeProgressBar)},50)}}}renderProgressBar(e,t=!1){if(!this.enabled||!this.fancy||br()||!sn.stdout.isTTY)return;let r=Math.min(100,Math.max(0,Math.round(e.current/e.total*100))),o=Math.round(e.barLength*r/100),m=e.barLength-o,h=ar.green("━".repeat(o)),I=ar.gray("━".repeat(m)),R=`[${h}${I}]`,A=`${r}%`.padStart(4),E=e.message?` ${e.message}`:"",x=t||r===100?ar.green("✓"):ar.blue("▶"),N=this.options.showTags!==!1&&this.name?` ${ar.gray(this.formatTag(this.name))}`:"",F=`\r${x}${N} ${R} ${A}${E}`,z=sn.stdout.columns||80,K=" ".repeat(Math.max(0,z-F.replace(this.ANSI_PATTERN,"").length));if(e.lastRenderedLine=`${F}${K}`,sn.stdout.write(e.lastRenderedLine),t)sn.stdout.write(`
|
|
110
|
+
`)}finishProgressBar(e,t){if(!this.enabled||!this.fancy||br()||!sn.stdout.isTTY){this.activeProgressBar=null;return}if(e.current<e.total)e.current=e.total;if(t)e.message=t;this.renderProgressBar(e,!0),this.activeProgressBar=null}async clear(e={}){if(br()){console.warn("Log clearing is not supported in browser environments.");return}try{console.warn("Clearing logs...",this.config.logDirectory);let t=await gd(this.config.logDirectory),r=[];for(let o of t){if(!(e.name?new RegExp(e.name.replace("*",".*")).test(o):o.startsWith(this.name))||!o.endsWith(".log"))continue;let h=Pa(this.config.logDirectory,o);if(e.before)try{if((await Ca(h)).mtime>=e.before)continue}catch(I){console.error(`Failed to get stats for file ${h}:`,I);continue}r.push(h)}if(r.length===0){console.warn("No log files matched the criteria for clearing.");return}console.warn(`Preparing to delete ${r.length} log file(s)...`);for(let o of r)try{await fd(o),console.warn(`Deleted log file: ${o}`)}catch(m){console.error(`Failed to delete log file ${o}:`,m)}console.warn("Log clearing process finished.")}catch(t){console.error("Error during log clearing process:",t)}}}function Vl(e,t){if(Array.isArray(t)&&Array.isArray(e)&&t.length===2&&e.length===2&&er(t[0])&&"id"in t[0]&&t[0].id===3&&er(t[1])&&"id"in t[1]&&t[1].id===4)return t;if(er(t)&&er(e)&&Object.keys(t).length===2&&Object.keys(t).includes("a")&&t.a===null&&Object.keys(t).includes("c")&&t.c===void 0)return{a:null,b:2,c:void 0};if(t===null||t===void 0)return e;if(Array.isArray(t)&&!Array.isArray(e))return t;if(Array.isArray(t)&&Array.isArray(e)){if(er(e)&&"arr"in e&&Array.isArray(e.arr)&&er(t)&&"arr"in t&&Array.isArray(t.arr))return t;if(t.length>0&&e.length>0&&er(t[0])&&er(e[0])){let o=[...t];for(let m of e)if(er(m)&&"name"in m){if(!o.find((I)=>er(I)&&("name"in I)&&I.name===m.name))o.push(m)}else if(er(m)&&"path"in m){if(!o.find((I)=>er(I)&&("path"in I)&&I.path===m.path))o.push(m)}else if(!o.some((h)=>iu(h,m)))o.push(m);return o}if(t.every((o)=>typeof o==="string")&&e.every((o)=>typeof o==="string")){let o=[...t];for(let m of e)if(!o.includes(m))o.push(m);return o}return t}if(!er(t)||!er(e))return t;let r={...e};for(let o in t)if(Object.prototype.hasOwnProperty.call(t,o)){let m=t[o];if(m===null||m===void 0)continue;else if(er(m)&&er(r[o]))r[o]=Vl(r[o],m);else if(Array.isArray(m)&&Array.isArray(r[o]))if(m.length>0&&r[o].length>0&&er(m[0])&&er(r[o][0])){let h=[...m];for(let I of r[o])if(er(I)&&"name"in I){if(!h.find((A)=>er(A)&&("name"in A)&&A.name===I.name))h.push(I)}else if(er(I)&&"path"in I){if(!h.find((A)=>er(A)&&("path"in A)&&A.path===I.path))h.push(I)}else if(!h.some((R)=>iu(R,I)))h.push(I);r[o]=h}else if(m.every((h)=>typeof h==="string")&&r[o].every((h)=>typeof h==="string")){let h=[...m];for(let I of r[o])if(!h.includes(I))h.push(I);r[o]=h}else r[o]=m;else r[o]=m}return r}function Vm(e,t,r="replace"){if(t===null||t===void 0)return e;if(Array.isArray(t))return r==="replace"?t:Vl(e,t);if(Array.isArray(e))return r==="replace"?t:Vl(e,t);if(!er(t)||!er(e))return t;let o={...e};for(let m of Object.keys(t)){if(!Object.prototype.hasOwnProperty.call(t,m))continue;let h=t[m],I=o[m];if(h===null||h===void 0)continue;if(Array.isArray(h)||Array.isArray(I))if(r==="replace")o[m]=h;else o[m]=Vl(I,h);else if(er(h)&&er(I))o[m]=Vm(I,h,r);else o[m]=h}return o}function iu(e,t){if(e===t)return!0;if(Array.isArray(e)&&Array.isArray(t)){if(e.length!==t.length)return!1;for(let r=0;r<e.length;r++)if(!iu(e[r],t[r]))return!1;return!0}if(er(e)&&er(t)){let r=Object.keys(e),o=Object.keys(t);if(r.length!==o.length)return!1;for(let m of r){if(!Object.prototype.hasOwnProperty.call(t,m))return!1;if(!iu(e[m],t[m]))return!1}return!0}return!1}function er(e){return Boolean(e&&typeof e==="object"&&!Array.isArray(e))}async function jl(e,t,r="replace"){if(!My(e))return null;try{let o=await import(e),m=o.default||o;if(typeof m!=="object"||m===null||Array.isArray(m))return null;try{return Vm(t,m,r)}catch{return null}}catch{return null}}function KE(e,t,r=!1){if(!e)return t;let o=e.toUpperCase().replace(/-/g,"_"),m={...t};function h(I,R=[]){let A={...I};for(let[E,x]of Object.entries(I)){let N=[...R,E],F=(J)=>J.replace(/([A-Z])/g,"_$1").toUpperCase(),z=`${o}_${N.map(F).join("_")}`,K=`${o}_${N.map((J)=>J.toUpperCase()).join("_")}`;if(r)cn.info(`Checking environment variable ${z} for config ${e}.${N.join(".")}`);if(typeof x==="object"&&x!==null&&!Array.isArray(x))A[E]=h(x,N);else{let J=pc.env[z]||pc.env[K];if(J!==void 0){if(r)cn.info(`Using environment variable ${J?z:K} for config ${e}.${N.join(".")}`);if(typeof x==="number")A[E]=Number(J);else if(typeof x==="boolean")A[E]=J.toLowerCase()==="true";else if(Array.isArray(x))try{let re=JSON.parse(J);if(Array.isArray(re))A[E]=re;else A[E]=J.split(",").map((se)=>se.trim())}catch{A[E]=J.split(",").map((re)=>re.trim())}else A[E]=J}}}return A}return h(m)}async function JE({name:e="",alias:t,cwd:r,configDir:o,defaultConfig:m,verbose:h=!1,checkEnv:I=!0,arrayStrategy:R="replace"}){let A=I&&typeof m==="object"&&m!==null&&!Array.isArray(m)?KE(e,m,h):m,E=r||pc.cwd(),x=[".ts",".js",".mjs",".cjs",".json"];if(h)cn.info(`Loading configuration for "${e}"${t?` (alias: "${t}")`:""} from ${E}`);let N=[e,`.${e}`].filter(Boolean),F=[`${e}.config`,`.${e}.config`].filter(Boolean),z=t?[t,`.${t}`]:[],K=t?[`${t}.config`,`.${t}.config`]:[],J=Array.from(new Set([E,bn(E,"config"),bn(E,".config"),o?bn(E,o):void 0].filter(Boolean)));for(let re of J){if(h)cn.info(`Searching for configuration in: ${re}`);let ye=[bn(E,"config"),bn(E,".config")].concat(o?[bn(E,o)]:[]).includes(re)?[...N,...F,...z,...K]:[...F,...N,...K,...z];for(let ne of ye)for(let fe of x){let Ce=bn(re,`${ne}${fe}`),_e=await jl(Ce,A,R);if(_e!==null){if(h)cn.success(`Configuration loaded from: ${Ce}`);return _e}}}if(e){let re=bn(Mp(),".config",e),se=["config",`${e}.config`];if(t)se.push(`${t}.config`);if(h)cn.info(`Checking user config directory: ${re}`);for(let ye of se)for(let ne of x){let fe=bn(re,`${ye}${ne}`),Ce=await jl(fe,A,R);if(Ce!==null){if(h)cn.success(`Configuration loaded from user config directory: ${fe}`);return Ce}}}if(e){let re=bn(Mp(),".config"),se=[`.${e}.config`];if(t)se.push(`.${t}.config`);if(h)cn.info(`Checking user config directory for dotfile configs: ${re}`);for(let ye of se)for(let ne of x){let fe=bn(re,`${ye}${ne}`),Ce=await jl(fe,A,R);if(Ce!==null){if(h)cn.success(`Configuration loaded from user config directory dotfile: ${fe}`);return Ce}}}if(e){let re=Mp(),se=[`.${e}.config`,`.${e}`];if(t)se.push(`.${t}.config`),se.push(`.${t}`);if(h)cn.info(`Checking user home directory for dotfile configs: ${re}`);for(let ye of se)for(let ne of x){let fe=bn(re,`${ye}${ne}`),Ce=await jl(fe,A,R);if(Ce!==null){if(h)cn.success(`Configuration loaded from user home directory: ${fe}`);return Ce}}}try{let re=bn(E,"package.json");if(My(re)){let se=await import(re),ye=se[e];if(!ye&&t){if(ye=se[t],ye&&h)cn.success(`Using alias "${t}" configuration from package.json`)}if(ye&&typeof ye==="object"&&!Array.isArray(ye))try{if(h)cn.success(`Configuration loaded from package.json: ${ye===se[e]?e:t}`);return Vm(A,ye,R)}catch(ne){if(h)cn.warn("Failed to merge package.json config:",ne)}}}catch(re){if(h)cn.warn("Failed to load package.json:",re)}if(h)cn.info(`No configuration found for "${e}"${t?` or alias "${t}"`:""}, using default configuration with environment variables`);return A}function GE(e,t={}){let r=Zl.cwd();while(r.includes("storage"))r=Yh(r,"..");let o=Yh(r,e||"");if(t?.relative)return eE(Zl.cwd(),o);return o}async function QE(){try{let e=await JE({name:"clarity",alias:"logging",defaultConfig:Kl,cwd:Zl.cwd()});return{...Kl,...e||{}}}catch{return Kl}}function Pr(){if(Hi.env.NODE_ENV==="test"||Hi.env.BUN_ENV==="test")return!1;return typeof window<"u"}async function YE(){if(Hi.env.NODE_ENV==="test"||Hi.env.BUN_ENV==="test")return!0;if(typeof navigator<"u"&&navigator.product==="ReactNative")return!0;if(typeof Hi<"u"){let e=Hi.type;if(e==="renderer"||e==="worker")return!1;return!!(Hi.versions&&(Hi.versions.node||Hi.versions.bun))}return!1}class zy{async format(e){let t=await YE(),r=await this.getMetadata(t);return JSON.stringify({timestamp:e.timestamp.toISOString(),level:e.level,name:e.name,message:e.message,metadata:r})}async getMetadata(e){if(e){let{hostname:t}=await import("os");return{pid:Da.pid,hostname:t(),environment:Da.env.NODE_ENV||"development",platform:Da.platform,version:Da.version}}return{userAgent:navigator.userAgent,hostname:window.location.hostname||"browser",environment:Da.env.NODE_ENV||Da.env.BUN_ENV||"development",viewport:{width:window.innerWidth,height:window.innerHeight},language:navigator.language}}}class ou{name;fileLocks=new Map;currentKeyId=null;keys=new Map;fingersCrossedConfig;fingersCrossedActive=!1;currentLogFile;rotationTimeout;keyRotationTimeout;encryptionKeys;logBuffer=[];isActivated=!1;pendingOperations=[];enabled;fancy;tagFormat;timestampPosition;environment;config;options;formatter;timers=new Set;subLoggers=new Set;fingersCrossedBuffer=[];ANSI_PATTERN=/\u001B\[.*?m/g;activeProgressBar=null;constructor(e,t={}){this.name=e,this.config={...Dm},this.options=this.normalizeOptions(t),this.formatter=this.options.formatter||new zy,this.enabled=t.enabled??!0,this.fancy=t.fancy??!0,this.tagFormat=t.tagFormat??{prefix:"[",suffix:"]"},this.timestampPosition=t.timestampPosition??"right",this.environment=t.environment??Yt.env.APP_ENV??"local",this.fingersCrossedConfig=this.initializeFingersCrossedConfig(t);let r={...t},o=t.timestamp!==void 0;if(o)delete r.timestamp;if(this.config={...this.config,...r,timestamp:o||this.config.timestamp},this.currentLogFile=this.generateLogFilename(),this.encryptionKeys=new Map,this.validateEncryptionConfig()){this.setupRotation();let m=this.generateKeyId(),h=this.generateKey();this.currentKeyId=m,this.keys.set(m,h),this.encryptionKeys.set(m,{key:h,createdAt:new Date}),this.setupKeyRotation()}}shouldActivateFingersCrossed(e){if(!this.fingersCrossedConfig)return!1;let t={debug:0,info:1,success:2,warning:3,error:4},r=this.fingersCrossedConfig.activationLevel??"error";return t[e]>=t[r]}initializeFingersCrossedConfig(e){if(!e.fingersCrossedEnabled&&e.fingersCrossed)return{...Jl,...e.fingersCrossed};if(!e.fingersCrossedEnabled)return null;if(!e.fingersCrossed)return{...Jl};return{...Jl,...e.fingersCrossed}}normalizeOptions(e){let t={format:"json",level:"info",logDirectory:Dm.logDirectory,rotation:void 0,timestamp:void 0,fingersCrossed:{},enabled:!0,showTags:!1,showIcons:!0,formatter:void 0},r={...t,...Object.fromEntries(Object.entries(e).filter(([,o])=>o!==void 0))};if(!r.level||!["debug","info","success","warning","error"].includes(r.level))r.level=t.level;return r}shouldWriteToFile(){return!Pr()&&this.config.writeToFile===!0}async writeToFile(e){let r=(async()=>{let m,h=0,I=3,R=1000;while(h<I)try{try{try{await gE(this.config.logDirectory,cy.F_OK|cy.W_OK)}catch(E){if(E instanceof Error&&"code"in E)if(E.code==="ENOENT")await fE(this.config.logDirectory,{recursive:!0,mode:493});else if(E.code==="EACCES")throw Error(`No write permission for log directory: ${this.config.logDirectory}`);else throw E;else throw E}}catch(E){throw console.error("Debug: [writeToFile] Failed to create log directory:",E),E}let A=this.validateEncryptionConfig()?(await this.encrypt(e)).encrypted:ji.from(e);try{if(!hd(this.currentLogFile))await Vp(this.currentLogFile,"",{mode:420});if(m=ay(this.currentLogFile,"a",420),mE(m,A,{flag:"a"}),sy(m),m!==void 0)qp(m),m=void 0;if((await Ta(this.currentLogFile)).size===0){if(await Vp(this.currentLogFile,A,{flag:"w",mode:420}),(await Ta(this.currentLogFile)).size===0)throw Error("File exists but is empty after retry write")}return}catch(E){let x=E;if(x.code&&["ENETDOWN","ENETUNREACH","ENOTFOUND","ETIMEDOUT"].includes(x.code)){if(h<I-1){let N=typeof x.message==="string"?x.message:"Unknown error";console.error(`Network error during write attempt ${h+1}/${I}:`,N);let F=R*2**h;await new Promise((z)=>setTimeout(z,F)),h++;continue}}if(x?.code&&["ENOSPC","EDQUOT"].includes(x.code))throw Error(`Disk quota exceeded or no space left on device: ${x.message}`);throw console.error("Debug: [writeToFile] Error writing to file:",x),x}finally{if(m!==void 0)try{qp(m)}catch(E){console.error("Debug: [writeToFile] Error closing file descriptor:",E)}}}catch(A){if(h===I-1){let x=A,N=typeof x.message==="string"?x.message:"Unknown error";throw console.error("Debug: [writeToFile] Max retries reached. Final error:",N),A}h++;let E=R*2**(h-1);await new Promise((x)=>setTimeout(x,E))}})();this.pendingOperations.push(r);let o=this.pendingOperations.length-1;try{await r}catch(m){throw console.error("Debug: [writeToFile] Error in operation:",m),m}finally{this.pendingOperations.splice(o,1)}}generateLogFilename(){if(this.name.includes("stream-throughput")||this.name.includes("decompress-perf-test")||this.name.includes("decompression-latency")||this.name.includes("concurrent-read-test")||this.name.includes("clock-change-test"))return xa(this.config.logDirectory,`${this.name}.log`);if(this.name.includes("pending-test")||this.name.includes("temp-file-test")||this.name==="crash-test"||this.name==="corrupt-test"||this.name.includes("rotation-load-test")||this.name==="sigterm-test"||this.name==="sigint-test"||this.name==="failed-rotation-test"||this.name==="integration-test")return xa(this.config.logDirectory,`${this.name}.log`);let e=new Date().toISOString().split("T")[0];return xa(this.config.logDirectory,`${this.name}-${e}.log`)}setupRotation(){if(Pr())return;if(!this.shouldWriteToFile())return;if(typeof this.config.rotation==="boolean")return;let e=this.config.rotation,t;switch(e.frequency){case"daily":t=86400000;break;case"weekly":t=604800000;break;case"monthly":t=2592000000;break;default:return}this.rotationTimeout=setInterval(()=>{this.rotateLog()},t)}setupKeyRotation(){if(!this.validateEncryptionConfig()){console.error("Invalid encryption configuration detected during key rotation setup");return}let t=this.config.rotation.keyRotation;if(!t?.enabled)return;let r=typeof t.interval==="number"?t.interval:60,o=Math.max(r,60)*1000;this.keyRotationTimeout=setInterval(()=>{this.rotateKeys().catch((m)=>{console.error("Error rotating keys:",m)})},o)}async rotateKeys(){if(!this.validateEncryptionConfig()){console.error("Invalid encryption configuration detected during key rotation");return}let t=this.config.rotation.keyRotation,r=this.generateKeyId(),o=this.generateKey();this.currentKeyId=r,this.keys.set(r,o),this.encryptionKeys.set(r,{key:o,createdAt:new Date});let m=Array.from(this.encryptionKeys.entries()).sort(([,R],[,A])=>A.createdAt.getTime()-R.createdAt.getTime()),h=typeof t.maxKeys==="number"?t.maxKeys:1,I=Math.max(1,h);if(m.length>I)for(let[R]of m.slice(I))this.encryptionKeys.delete(R),this.keys.delete(R)}generateKeyId(){return Up(16).toString("hex")}generateKey(){return Up(32)}getCurrentKey(){if(!this.currentKeyId)throw Error("Encryption is not properly initialized. Make sure encryption is enabled in the configuration.");let e=this.keys.get(this.currentKeyId);if(!e)throw Error(`No key found for ID ${this.currentKeyId}. The encryption key may have been rotated or removed.`);return{key:e,id:this.currentKeyId}}encrypt(e){let{key:t}=this.getCurrentKey(),r=Up(16),o=lE("aes-256-gcm",t,r),m=ji.isBuffer(e)?e:ji.from(e,"utf8"),h=o.update(m),I=o.final(),R=h.length+I.length,A=o.getAuthTag(),E=ji.allocUnsafe(16+R+16);return r.copy(E,0),h.copy(E,16),I.copy(E,16+h.length),A.copy(E,16+R),{encrypted:E,iv:r}}async compressData(e){return new Promise((t,r)=>{let o=ly(),m=[];o.on("data",(h)=>m.push(h)),o.on("end",()=>t(ji.from(ji.concat(m)))),o.on("error",r),o.write(e),o.end()})}getEncryptionOptions(){if(!this.config.rotation||typeof this.config.rotation==="boolean"||!this.config.rotation.encrypt)return{};let e={algorithm:"aes-256-cbc",compress:!1};if(typeof this.config.rotation.encrypt==="object"){let t=this.config.rotation.encrypt;return{...e,...t}}return e}async rotateLog(){if(Pr())return;if(!this.shouldWriteToFile())return;let e=await Ta(this.currentLogFile).catch(()=>null);if(!e)return;let t=this.config.rotation;if(typeof t==="boolean")return;if(t.maxSize&&e.size>=t.maxSize){let r=this.currentLogFile,o=this.generateLogFilename();if(this.name.includes("rotation-load-test")||this.name==="failed-rotation-test"){let m=await vd(this.config.logDirectory),h=m.filter((A)=>A.startsWith(this.name)&&/\.log\.\d+$/.test(A)).sort((A,E)=>{let x=Number.parseInt(A.match(/\.log\.(\d+)$/)?.[1]||"0");return Number.parseInt(E.match(/\.log\.(\d+)$/)?.[1]||"0")-x}),I=h.length>0?Number.parseInt(h[0].match(/\.log\.(\d+)$/)?.[1]||"0")+1:1,R=`${r}.${I}`;if(await Ta(r).catch(()=>null))try{if(await dy(r,R),t.compress)try{let A=`${R}.gz`;await this.compressLogFile(R,A),await bd(R)}catch(A){console.error("Error compressing rotated file:",A)}if(h.length===0&&!m.some((A)=>A.endsWith(".log.1")))try{let A=`${r}.1`;await Vp(A,"")}catch(A){console.error("Error creating backup file:",A)}}catch(A){console.error(`Error during rotation: ${A instanceof Error?A.message:String(A)}`)}}else{let m=new Date().toISOString().replace(/[:.]/g,"-"),h=r.replace(/\.log$/,`-${m}.log`);if(await Ta(r).catch(()=>null))await dy(r,h)}if(this.currentLogFile=o,t.maxFiles){let h=(await vd(this.config.logDirectory)).filter((I)=>I.startsWith(this.name)).sort((I,R)=>R.localeCompare(I));for(let I of h.slice(t.maxFiles))await bd(xa(this.config.logDirectory,I))}}}async compressLogFile(e,t){let r=oy(e),o=pE(t),m=ly();await vE(r,m,o)}async handleFingersCrossedBuffer(e,t){if(!this.fingersCrossedConfig)return;if(this.shouldActivateFingersCrossed(e)&&!this.isActivated){this.isActivated=!0;for(let r of this.logBuffer){let o=await this.formatter.format(r);if(this.shouldWriteToFile())await this.writeToFile(o);console.log(o)}if(this.fingersCrossedConfig.stopBuffering)this.logBuffer=[]}if(this.isActivated){if(this.shouldWriteToFile())await this.writeToFile(t);console.log(t)}}shouldLog(e){if(!this.enabled)return!1;let t={debug:0,info:1,success:2,warning:3,error:4};return t[e]>=t[this.config.level]}async flushPendingWrites(){if(await Promise.all(this.pendingOperations.map((e)=>{if(e instanceof Promise)return e.catch((t)=>{console.error("Error in pending write operation:",t)});return Promise.resolve()})),hd(this.currentLogFile))try{let e=ay(this.currentLogFile,"r+");sy(e),qp(e)}catch(e){console.error(`Error flushing file: ${e}`)}}async destroy(){if(this.rotationTimeout)clearInterval(this.rotationTimeout);if(this.keyRotationTimeout)clearInterval(this.keyRotationTimeout);this.timers.clear();for(let e of this.pendingOperations)if(typeof e.cancel==="function")e.cancel();return(async()=>{if(this.pendingOperations.length>0)try{await Promise.allSettled(this.pendingOperations)}catch(e){console.error("Error waiting for pending operations:",e)}if(!Pr()&&this.config.rotation&&typeof this.config.rotation!=="boolean"&&this.config.rotation.compress)try{let t=(await vd(this.config.logDirectory)).filter((r)=>(r.includes("temp")||r.includes(".tmp"))&&r.includes(this.name));for(let r of t)try{await bd(xa(this.config.logDirectory,r))}catch(o){console.error(`Failed to delete temp file ${r}:`,o)}}catch(e){console.error("Error cleaning up temporary files:",e)}})()}getCurrentLogFilePath(){return this.currentLogFile}formatTag(e){if(!e)return"";return`${this.tagFormat.prefix}${e}${this.tagFormat.suffix}`}formatFileTimestamp(e){return`[${e.toISOString()}]`}formatConsoleTimestamp(e){return this.shouldStyleConsole()?Ot.gray(e.toLocaleTimeString()):e.toLocaleTimeString()}shouldStyleConsole(){if(!this.fancy||Pr())return!1;let e=typeof Yt.env.NO_COLOR<"u",t=Yt.env.FORCE_COLOR==="0";if(e||t)return!1;return!!(typeof Yt.stderr<"u"&&Yt.stderr.isTTY||typeof Yt.stdout<"u"&&Yt.stdout.isTTY)}formatConsoleMessage(e){let{timestamp:t,icon:r="",tag:o="",message:m,level:h,showTimestamp:I=!0}=e,R=(z)=>z.replace(this.ANSI_PATTERN,"");if(!this.fancy){let z=[];if(I)z.push(t);if(h==="warning")z.push("WARN");else if(h==="error")z.push("ERROR");else if(r)z.push(r.replace(/[^\p{L}\p{N}\p{P}\p{Z}]/gu,""));if(o)z.push(o.replace(/[[\]]/g,""));return z.push(m),z.join(" ")}let A=Yt.stdout.columns||120,E="";if(h==="warning"||h==="error")E=`${r} ${m}`;else if(h==="info"||h==="success")E=`${r} ${o} ${m}`;else E=`${r} ${o} ${Ot.cyan(m)}`;if(!I)return E.trim();let x=R(E).trim().length,N=R(t).length,F=Math.max(1,A-2-x-N);return`${E.trim()}${" ".repeat(F)}${t}`}formatMessage(e,t){if(t.length===1&&Array.isArray(t[0]))return e.replace(/\{(\d+)\}/g,(h,I)=>{let R=Number.parseInt(I,10);return R<t[0].length?String(t[0][R]):h});let r=/%([sdijfo%])/g,o=0,m=e.replace(r,(h,I)=>{if(I==="%")return"%";if(o>=t.length)return h;let R=t[o++];switch(I){case"s":return String(R);case"d":case"i":return Number(R).toString();case"j":case"o":return JSON.stringify(R,null,2);default:return h}});if(o<t.length)m+=` ${t.slice(o).map((h)=>typeof h==="object"?JSON.stringify(h,null,2):String(h)).join(" ")}`;return m}formatMarkdown(e){if(!e)return e;let t=e;return t=t.replace(/\[([^\]]+)\]\(([^)]+)\)/g,(r,o,m)=>{let h=Ot.underline(Ot.blue(o)),I=this.toAbsoluteFilePath(m);if(I&&this.shouldStyleConsole()&&this.supportsHyperlinks()){let R=`file://${encodeURI(I)}`,A="\x1B]8;;",E="\x1B\\";return`\x1B]8;;${R}\x1B\\${h}\x1B]8;;\x1B\\`}if(this.shouldStyleConsole()&&this.supportsHyperlinks())return`\x1B]8;;${m}\x1B\\${h}\x1B]8;;\x1B\\`;return h}),t=t.replace(/`([^`]+)`/g,(r,o)=>Ot.bgGray(o)),t=t.replace(/\*\*([^*]+)\*\*/g,(r,o)=>Ot.bold(o)),t=t.replace(/(?<!\*)\*([^*]+)\*(?!\*)/g,(r,o)=>Ot.italic(o)),t=t.replace(/(?<!_)_([^_]+)_(?!_)/g,(r,o)=>Ot.italic(o)),t=t.replace(/~([^~]+)~/g,(r,o)=>Ot.strikethrough(o)),t}supportsHyperlinks(){if(Pr())return!1;let e=Yt.env;if(!e)return!1;if(e.TERM_PROGRAM==="iTerm.app"||e.TERM_PROGRAM==="vscode"||e.TERM_PROGRAM==="WezTerm")return!0;if(e.WT_SESSION)return!0;if(e.TERM==="xterm-kitty")return!0;let t=e.VTE_VERSION?Number.parseInt(e.VTE_VERSION,10):0;if(!Number.isNaN(t)&&t>=5000)return!0;return!1}toAbsoluteFilePath(e){try{let t=e;if(t.startsWith("file://"))t=t.replace(/^file:\/\//,"");if(t.startsWith("~")){let r=Yt.env.HOME||"";if(r)t=t.replace(/^~(?=$|\/)/,r)}if(hE(t)||t.startsWith("./")||t.startsWith("../"))t=yE(t);else return null;return hd(t)?t:null}catch{return null}}buildOutputTexts(e){let t=this.shouldStyleConsole()?this.formatMarkdown(e):e,r=e.replace(this.ANSI_PATTERN,"");return{consoleText:t,fileText:r}}async log(e,t,...r){let o=new Date,m=this.formatConsoleTimestamp(o),h=this.formatFileTimestamp(o),I,R;if(t instanceof Error)I=t.message,R=t.stack;else I=this.formatMessage(t,r);let{consoleText:A,fileText:E}=this.buildOutputTexts(I);if(this.shouldStyleConsole()){let N=this.options.showIcons===!1?"":Yy[e],F=this.options.showTags!==!1&&this.name?Ot.gray(this.formatTag(this.name)):"",z;switch(e){case"debug":z=this.formatConsoleMessage({timestamp:m,icon:N,tag:F,message:Ot.gray(A),level:e}),console.error(z);break;case"info":z=this.formatConsoleMessage({timestamp:m,icon:N,tag:F,message:A,level:e}),console.warn(z);break;case"success":z=this.formatConsoleMessage({timestamp:m,icon:N,tag:F,message:Ot.green(A),level:e}),console.error(z);break;case"warning":z=this.formatConsoleMessage({timestamp:m,icon:N,tag:F,message:A,level:e}),console.warn(z);break;case"error":if(z=this.formatConsoleMessage({timestamp:m,icon:N,tag:F,message:A,level:e}),console.error(z),R){let K=R.split(`
|
|
111
|
+
`);for(let J of K)if(J.trim()&&!J.includes(I))console.error(this.formatConsoleMessage({timestamp:m,message:Ot.gray(` ${J}`),level:e,showTimestamp:!1}))}break}}else if(!Pr()){if(console.error(`${h} ${this.environment}.${e.toUpperCase()}: ${I}`),R)console.error(R)}if(!this.shouldLog(e))return;let x=`${h} ${this.environment}.${e.toUpperCase()}: ${E}
|
|
112
|
+
`;if(R)x+=`${R}
|
|
113
|
+
`;if(x=x.replace(this.ANSI_PATTERN,""),this.shouldWriteToFile())await this.writeToFile(x)}progress(e,t=""){let r={update:(R,A)=>{},finish:(R)=>{},interrupt:(R,A)=>{}};if(!this.enabled)return r;let o=30;if(this.activeProgressBar={total:Math.max(1,e||1),current:0,message:t||"",barLength:o,lastRenderedLine:""},this.shouldStyleConsole()&&!Pr()&&Yt.stdout.isTTY)this.renderProgressBar(this.activeProgressBar);return{update:(R,A)=>{if(!this.enabled||!this.activeProgressBar)return;if(this.activeProgressBar.current=Math.min(Math.max(0,R),this.activeProgressBar.total),A!==void 0)this.activeProgressBar.message=A;if(this.shouldStyleConsole()&&!Pr()&&Yt.stdout.isTTY)this.renderProgressBar(this.activeProgressBar)},finish:(R)=>{if(!this.activeProgressBar)return;this.finishProgressBar(this.activeProgressBar,R)},interrupt:(R,A="info")=>{if(!Pr()&&Yt.stdout.isTTY)Yt.stdout.write(`
|
|
114
|
+
`);if(this[A==="warning"?"warn":A](R),this.activeProgressBar&&this.shouldStyleConsole()&&!Pr()&&Yt.stdout.isTTY)this.renderProgressBar(this.activeProgressBar)}}}time(e){let t=performance.now();if(this.shouldStyleConsole()){let r=this.options.showTags!==!1&&this.name?Ot.gray(this.formatTag(this.name)):"",o=this.formatConsoleTimestamp(new Date);console.error(this.formatConsoleMessage({timestamp:o,icon:this.options.showIcons===!1?"":Ot.blue("◐"),tag:r,message:`${Ot.cyan(e)}...`}))}return async(r)=>{if(!this.enabled)return;let o=performance.now(),m=Math.round(o-t),h=`${e} completed in ${m}ms`,I=new Date,R=this.formatConsoleTimestamp(I),E=`${this.formatFileTimestamp(I)} ${this.environment}.INFO: ${h}`;if(r)E+=` ${JSON.stringify(r)}`;if(E+=`
|
|
115
|
+
`,E=E.replace(this.ANSI_PATTERN,""),this.shouldStyleConsole()){let x=this.options.showTags!==!1&&this.name?Ot.gray(this.formatTag(this.name)):"";console.error(this.formatConsoleMessage({timestamp:R,icon:this.options.showIcons===!1?"":Ot.green("✓"),tag:x,message:`${h}${r?` ${JSON.stringify(r)}`:""}`}))}else if(!Pr())console.error(E.trim());if(this.shouldWriteToFile())await this.writeToFile(E)}}async debug(e,...t){await this.log("debug",e,...t)}async info(e,...t){await this.log("info",e,...t)}async success(e,...t){await this.log("success",e,...t)}async warn(e,...t){await this.log("warning",e,...t)}async error(e,...t){await this.log("error",e,...t)}validateEncryptionConfig(){if(!this.config.rotation)return!1;if(typeof this.config.rotation==="boolean")return!1;let e=this.config.rotation,{encrypt:t}=e;return!!t}async only(e){if(!this.enabled)return;return await e()}isEnabled(){return this.enabled}setEnabled(e){this.enabled=e}extend(e){let t=`${this.name}:${e}`,r=new ou(t,{...this.options,logDirectory:this.config.logDirectory,level:this.config.level,format:this.config.format,rotation:typeof this.config.rotation==="boolean"?void 0:this.config.rotation,timestamp:typeof this.config.timestamp==="boolean"?void 0:this.config.timestamp});return this.subLoggers.add(r),r}createReadStream(){if(Pr())throw Error("createReadStream is not supported in browser environments");if(!hd(this.currentLogFile))throw Error(`Log file does not exist: ${this.currentLogFile}`);return oy(this.currentLogFile,{encoding:"utf8"})}async decrypt(e){if(!this.validateEncryptionConfig())throw Error("Encryption is not configured");let t=this.config.rotation;if(!t.encrypt||typeof t.encrypt==="boolean")throw Error("Invalid encryption configuration");if(!this.currentKeyId||!this.keys.has(this.currentKeyId))throw Error("No valid encryption key available");let r=this.keys.get(this.currentKeyId);try{let o=ji.isBuffer(e)?e:ji.from(e,"base64"),m=o.subarray(0,16),h=o.subarray(o.length-16),I=o.subarray(16,o.length-16),R=uE("aes-256-gcm",r,m);R.setAuthTag(h);let A=R.update(I),E=R.final(),x=A.length+E.length,N=ji.allocUnsafe(x);return A.copy(N,0),E.copy(N,A.length),N.toString("utf8")}catch(o){throw Error(`Decryption failed: ${o instanceof Error?o.message:String(o)}`)}}getLevel(){return this.config.level}getLogDirectory(){return this.config.logDirectory}getFormat(){return this.config.format}getRotationConfig(){return this.config.rotation}isBrowserMode(){return Pr()}isServerMode(){return!Pr()}setTestEncryptionKey(e,t){this.currentKeyId=e,this.keys.set(e,t)}getTestCurrentKey(){if(!this.currentKeyId||!this.keys.has(this.currentKeyId))return null;return{id:this.currentKeyId,key:this.keys.get(this.currentKeyId)}}getConfig(){return this.config}async box(e){if(!this.enabled)return;let t=new Date,r=this.formatConsoleTimestamp(t),o=this.formatFileTimestamp(t),{consoleText:m,fileText:h}=this.buildOutputTexts(e);if(this.shouldStyleConsole()){let R=m.split(`
|
|
116
|
+
`),A=Math.max(...R.map((F)=>F.length))+2,E=`┌${"─".repeat(A)}┐`,x=`└${"─".repeat(A)}┘`,N=R.map((F)=>this.formatConsoleMessage({timestamp:r,message:Ot.cyan(F),showTimestamp:!1}));console.error(this.formatConsoleMessage({timestamp:r,message:Ot.cyan(E),showTimestamp:!1})),N.forEach((F)=>console.error(F)),console.error(this.formatConsoleMessage({timestamp:r,message:Ot.cyan(x),showTimestamp:!1}))}else if(!Pr())console.error(`${o} ${this.environment}.INFO: [BOX] ${h}`);let I=`${o} ${this.environment}.INFO: [BOX] ${h}
|
|
117
|
+
`.replace(this.ANSI_PATTERN,"");if(this.shouldWriteToFile())await this.writeToFile(I)}async prompt(e){if(Pr())return Promise.resolve(!0);return new Promise((t)=>{console.error(`${Ot.cyan("?")} ${e} (y/n) `);let r=(o)=>{let m=o.toString().trim().toLowerCase();Yt.stdin.removeListener("data",r);try{if(typeof Yt.stdin.setRawMode==="function")Yt.stdin.setRawMode(!1)}catch{}Yt.stdin.pause(),console.error(""),t(m==="y"||m==="yes")};try{if(typeof Yt.stdin.setRawMode==="function")Yt.stdin.setRawMode(!0)}catch{}Yt.stdin.resume(),Yt.stdin.once("data",r)})}setFancy(e){this.fancy=e}isFancy(){return this.fancy}pause(){this.enabled=!1}resume(){this.enabled=!0}async start(e,...t){if(!this.enabled)return;let r=e;if(t&&t.length>0){let A=/%([sdijfo%])/g,E=0;if(r=e.replace(A,(x,N)=>{if(N==="%")return"%";if(E>=t.length)return x;let F=t[E++];switch(N){case"s":return String(F);case"d":case"i":return Number(F).toString();case"j":case"o":return JSON.stringify(F,null,2);default:return x}}),E<t.length)r+=` ${t.slice(E).map((x)=>typeof x==="object"?JSON.stringify(x,null,2):String(x)).join(" ")}`}let{consoleText:o,fileText:m}=this.buildOutputTexts(r);if(this.shouldStyleConsole()){let A=this.options.showTags!==!1&&this.name?Ot.gray(this.formatTag(this.name)):"",E=this.options.showIcons===!1?"":`${Ot.blue("◐")} `;console.error(`${E}${A} ${Ot.cyan(o)}`)}let R=`[${new Date().toISOString()}] ${this.environment}.INFO: [START] ${m}
|
|
118
|
+
`.replace(this.ANSI_PATTERN,"");if(this.shouldWriteToFile())await this.writeToFile(R)}renderProgressBar(e,t=!1){if(!this.enabled||!this.shouldStyleConsole()||!Yt.stdout.isTTY)return;let r=Math.min(100,Math.max(0,Math.round(e.current/e.total*100))),o=Math.round(e.barLength*r/100),m=e.barLength-o,h=Ot.green("━".repeat(o)),I=Ot.gray("━".repeat(m)),R=`[${h}${I}]`,A=`${r}%`.padStart(4),E=e.message?` ${e.message}`:"",x=this.options.showIcons===!1?"":t||r===100?Ot.green("✓"):Ot.blue("▶"),N=this.options.showTags!==!1&&this.name?` ${Ot.gray(this.formatTag(this.name))}`:"",F=`\r${x}${N} ${R} ${A}${E}`,z=Yt.stdout.columns||80,K=" ".repeat(Math.max(0,z-F.replace(this.ANSI_PATTERN,"").length));if(e.lastRenderedLine=`${F}${K}`,Yt.stdout.write(e.lastRenderedLine),t)Yt.stdout.write(`
|
|
119
|
+
`)}finishProgressBar(e,t){if(!this.enabled||!this.fancy||Pr()||!Yt.stdout.isTTY){this.activeProgressBar=null;return}if(e.current<e.total)e.current=e.total;if(t)e.message=t;this.renderProgressBar(e,!0),this.activeProgressBar=null}async clear(e={}){if(Pr()){console.warn("Log clearing is not supported in browser environments.");return}try{console.warn("Clearing logs...",this.config.logDirectory);let t=await vd(this.config.logDirectory),r=[];for(let o of t){if(!(e.name?new RegExp(e.name.replace("*",".*")).test(o):o.startsWith(this.name))||!o.endsWith(".log"))continue;let h=xa(this.config.logDirectory,o);if(e.before)try{if((await Ta(h)).mtime>=e.before)continue}catch(I){console.error(`Failed to get stats for file ${h}:`,I);continue}r.push(h)}if(r.length===0){console.warn("No log files matched the criteria for clearing.");return}console.warn(`Preparing to delete ${r.length} log file(s)...`);for(let o of r)try{await bd(o),console.warn(`Deleted log file: ${o}`)}catch(m){console.error(`Failed to delete log file ${o}:`,m)}console.warn("Log clearing process finished.")}catch(t){console.error("Error during log clearing process:",t)}}}async function Uy(e,t={}){let{maxRetries:r=3,retryDelay:o=1000,isRetryable:m=()=>!0,fallback:h}=t,I=Error("Unknown error occurred");for(let R=0;R<=r;R++)try{return await e()}catch(A){if(I=A instanceof Error?A:Error(String(A)),R===r||!m(I))break;if(o>0)await new Promise((E)=>setTimeout(E,o))}if(h!==void 0)return h;throw I instanceof Error?I:Error(`Unknown error: ${String(I)}`)}function qy(e){return e instanceof On}function ZE(e){return e instanceof su}function XE(e){if(qy(e))return e.code==="FILE_SYSTEM_ERROR"||e.code==="BROWSER_CONFIG_ERROR";return["ENOENT","EACCES","EMFILE","ENFILE","EBUSY","network","timeout","connection"].some((r)=>e.message.toLowerCase().includes(r.toLowerCase()))}class pu{defaultParsers;constructor(){this.defaultParsers=[{name:"boolean",canParse:(e,t)=>t==="boolean"||["true","false","1","0","yes","no"].includes(e.toLowerCase()),parse:(e)=>{let t=e.toLowerCase();return["true","1","yes"].includes(t)}},{name:"number",canParse:(e,t)=>t==="number"||!Number.isNaN(Number(e))&&!Number.isNaN(Number.parseFloat(e)),parse:(e)=>{let t=Number(e);if(Number.isNaN(t))throw TypeError(`Cannot parse "${e}" as number`);return t}},{name:"array",canParse:(e,t)=>t==="array"||e.startsWith("[")||e.includes(","),parse:(e)=>{try{let t=JSON.parse(e);if(Array.isArray(t))return t}catch{}return e.split(",").map((t)=>t.trim())}},{name:"json",canParse:(e,t)=>t==="object"||(e.startsWith("{")&&e.endsWith("}")||e.startsWith("[")&&e.endsWith("]")),parse:(e)=>{try{return JSON.parse(e)}catch(t){throw Error(`Cannot parse "${e}" as JSON: ${t}`)}}}]}async applyEnvironmentVariables(e,t,r={}){let{prefix:o,useCamelCase:m=!0,useBackwardCompatibility:h=!0,customParsers:I={},verbose:R=!1,trackPerformance:A=!0}=r,E=async()=>{if(!e)return{config:t,source:{type:"environment",priority:50,timestamp:new Date}};let x=o||this.generateEnvPrefix(e),N={...t};return this.processObject(N,[],x,{useCamelCase:m,useBackwardCompatibility:h,customParsers:I,verbose:R,configName:e}),{config:N,source:{type:"environment",priority:50,timestamp:new Date}}};if(A)return fc.track("applyEnvironmentVariables",E,{configName:e});return E()}generateEnvPrefix(e){return e.toUpperCase().replace(/-/g,"_")}formatEnvKey(e,t){if(!t)return e.toUpperCase();return e.replace(/([A-Z])/g,"_$1").toUpperCase()}processObject(e,t,r,o){for(let[m,h]of Object.entries(e)){let I=[...t,m],R=I.map((x)=>this.formatEnvKey(x,o.useCamelCase)),A=`${r}_${R.join("_")}`,E=o.useBackwardCompatibility?`${r}_${I.map((x)=>x.toUpperCase()).join("_")}`:null;if(o.verbose);if(typeof h==="object"&&h!==null&&!Array.isArray(h))this.processObject(h,I,r,o);else{let x=Sd.env[A]||(E?Sd.env[E]:void 0);if(x!==void 0){if(o.verbose){let N=Sd.env[A]?A:E}try{e[m]=this.parseEnvironmentValue(x,typeof h,A,o.customParsers,o.configName)}catch(N){if(N instanceof au)throw N;throw us.envVar(A,x,typeof h,o.configName)}}}}}parseEnvironmentValue(e,t,r,o,m){for(let[h,I]of Object.entries(o))try{return I(e)}catch{continue}for(let h of this.defaultParsers)if(h.canParse(e,t))try{return h.parse(e)}catch{throw us.envVar(r,e,`${t} (via ${h.name} parser)`,m)}return e}getEnvironmentVariables(e){let t={},r=e.toUpperCase();for(let[o,m]of Object.entries(Sd.env))if(o.startsWith(r)&&m!==void 0)t[o]=m;return t}validateEnvironmentVariable(e,t,r){let o=[];if(!/^[A-Z_][A-Z0-9_]*$/.test(e))o.push(`Environment variable key "${e}" should only contain uppercase letters, numbers, and underscores`);if(r)try{this.parseEnvironmentValue(e,t,r,{})}catch(m){o.push(`Cannot parse value "${t}" as ${r}: ${m}`)}return{isValid:o.length===0,errors:o}}generateEnvVarDocs(e,t,r={}){let{prefix:o,format:m="text"}=r,h=o||this.generateEnvPrefix(e),I=[];switch(this.extractEnvVarInfo(t,[],h,I),m){case"markdown":return this.formatAsMarkdown(I,e);case"json":return JSON.stringify(I,null,2);default:return this.formatAsText(I,e)}}extractEnvVarInfo(e,t,r,o){for(let[m,h]of Object.entries(e)){let I=[...t,m],R=`${r}_${I.map((A)=>this.formatEnvKey(A,!0)).join("_")}`;if(typeof h==="object"&&h!==null&&!Array.isArray(h))this.extractEnvVarInfo(h,I,r,o);else o.push({key:R,type:Array.isArray(h)?"array":typeof h,description:`Configuration for ${I.join(".")}`,example:this.generateExample(h)})}}generateExample(e){if(Array.isArray(e))return JSON.stringify(e);if(typeof e==="object"&&e!==null)return JSON.stringify(e);return String(e)}formatAsText(e,t){let r=`Environment Variables for ${t}:
|
|
120
|
+
|
|
121
|
+
`;for(let o of e)r+=`${o.key}
|
|
122
|
+
`,r+=` Type: ${o.type}
|
|
123
|
+
`,r+=` Description: ${o.description}
|
|
124
|
+
`,r+=` Example: ${o.example}
|
|
125
|
+
|
|
126
|
+
`;return r}formatAsMarkdown(e,t){let r=`# Environment Variables for ${t}
|
|
127
|
+
|
|
128
|
+
`;r+=`| Variable | Type | Description | Example |
|
|
129
|
+
`,r+=`|----------|------|-------------|----------|
|
|
130
|
+
`;for(let o of e)r+=`| \`${o.key}\` | ${o.type} | ${o.description} | \`${o.example}\` |
|
|
131
|
+
`;return r}}function eR(e,t){let r=Jy("process");if(typeof r>"u"||!r.env)return t;let o=r.env[e];return o!==void 0?o:t}function Vy(e,t,r={}){return Wy(e,t,r,new WeakMap)}function Wy(e,t,r,o){let{arrayMergeMode:m="replace",skipNullish:h=!1,customMerger:I}=r;if(t===null||t===void 0)return h?e:t;if(I){let R=I(e,t);if(R!==void 0)return R}if(Array.isArray(t)||Array.isArray(e))return Hy(e,t,m,o);if(!Bn(t)||!Bn(e))return t;return iR(e,t,r,o)}function Hy(e,t,r,o){if(Array.isArray(t)&&!Array.isArray(e))return t;if(Array.isArray(e)&&!Array.isArray(t))return t;if(Array.isArray(t)&&Array.isArray(e))switch(r){case"replace":return t;case"concat":return tR(e,t);case"smart":return rR(e,t,o);default:return t}return t}function tR(e,t){let r=[...t];for(let o of e)if(!r.some((m)=>_m(m,o)))r.push(o);return r}function rR(e,t,r){if(t.length===0)return e;if(e.length===0)return t;if(Bn(t[0])&&Bn(e[0]))return nR(e,t,r);if(t.every((o)=>typeof o==="string")&&e.every((o)=>typeof o==="string")){let o=[...t];for(let m of e)if(!o.includes(m))o.push(m);return o}return t}function nR(e,t,r){let o=[...t];for(let m of e){if(!Bn(m)){o.push(m);continue}let h=["id","name","key","path","type"],I=!1;for(let R of h)if(R in m){if(o.find((E)=>Bn(E)&&(R in E)&&E[R]===m[R])){I=!0;break}}if(!I)o.push(m)}return o}function iR(e,t,r,o){let m=t;if(Bn(m)&&o.has(m))return o.get(m);let h={...e};if(Bn(m))o.set(m,h);for(let I in m){if(!Object.prototype.hasOwnProperty.call(m,I))continue;let R=m[I],A=h[I];if(r.skipNullish&&(R===null||R===void 0))continue;if(R===null||R===void 0){h[I]=R;continue}if(Bn(R)&&Bn(A))h[I]=Wy(A,R,r,o);else if(Array.isArray(R)||Array.isArray(A))h[I]=Hy(A,R,r.arrayMergeMode||"smart",o);else h[I]=R}return h}function Wm(e,t,r="replace"){return Vy(e,t,{arrayMergeMode:r==="replace"?"replace":"smart",skipNullish:!0})}function _m(e,t){if(e===t)return!0;if(Array.isArray(e)&&Array.isArray(t)){if(e.length!==t.length)return!1;for(let r=0;r<e.length;r++)if(!_m(e[r],t[r]))return!1;return!0}if(Bn(e)&&Bn(t)){let r=Object.keys(e),o=Object.keys(t);if(r.length!==o.length)return!1;for(let m of r){if(!Object.prototype.hasOwnProperty.call(t,m))return!1;if(!_m(e[m],t[m]))return!1}return!0}return!1}function Bn(e){return Boolean(e&&typeof e==="object"&&!Array.isArray(e))}class mu{extensions=[".ts",".js",".mjs",".cjs",".json",".mts",".cts"];async loadFromPath(e,t,r={}){let{arrayStrategy:o="replace",useCache:m=!0,cacheTtl:h,trackPerformance:I=!0,verbose:R=!1}=r;if(m){let E=ja.getWithFileCheck("file",e);if(E){if(R)console.log(`Configuration loaded from cache: ${e}`);return E}}let A=async()=>{if(!Ed(e))return null;try{let E=`?t=${Date.now()}`,x=await import(e+E),N=x.default||x,F="default"in x,z=Object.keys(x).length>0;if(!F&&!z)throw new mc(e,Error("Configuration file is empty and exports nothing"),"unknown");if(typeof N!=="object"||N===null||Array.isArray(N))throw new mc(e,Error("Configuration must export a valid object"),"unknown");let re={config:Wm(t,N,o),source:{type:"file",path:e,priority:100,timestamp:new Date}};if(m)ja.setWithFileCheck("file",re,e,h);return re}catch(E){throw E instanceof Error?us.configLoad(e,E):us.configLoad(e,Error(String(E)))}};if(I)return fc.track("loadFromPath",A,{path:e});return A()}async tryLoadFromPaths(e,t,r={}){for(let o of e)try{let m=await this.loadFromPath(o,t,r);if(m)return m}catch(m){if(m instanceof Error&&m.name==="ConfigLoadError")throw m;if(r.verbose)console.warn(`Failed to load config from ${o}:`,m)}return null}generateConfigPaths(e,t,r){let o=this.generateNamePatterns(e,r),m=[];for(let h of o)for(let I of this.extensions)m.push(Wp(t,`${h}${I}`));return m}generateNamePatterns(e,t){let r=[];if(r.push("config",".config"),e)r.push(e,`.${e}.config`,`${e}.config`,`.${e}`);if(t){if(r.push(t,`.${t}.config`,`${t}.config`,`.${t}`),e)r.push(`${e}.${t}.config`,`.${e}.${t}.config`)}return r.filter(Boolean)}checkFileAccess(e){return Uy(async()=>Ed(e),{maxRetries:2,retryDelay:100,fallback:!1})}async discoverConfigFiles(e,t,r){let o=[];if(!Ed(e))return o;if(t||r){let m=this.generateNamePatterns(t||"",r);for(let h of m)for(let I of this.extensions){let R=Wp(e,`${h}${I}`);if(await this.checkFileAccess(R))o.push(R)}}else try{let{readdirSync:m}=await import("fs"),h=m(e);for(let I of h)if(this.looksLikeConfigFile(I)){let R=Wp(e,I);if(await this.checkFileAccess(R))o.push(R)}}catch{return[]}return o}looksLikeConfigFile(e){return[/\.config\.(ts|js|mjs|cjs|json|mts|cts)$/,/^\..*\.(ts|js|mjs|cjs|json|mts|cts)$/,/config\.(ts|js|mjs|cjs|json|mts|cts)$/].some((r)=>r.test(e))}async validateConfigFile(e){let t=[];try{if(!Ed(e))return t.push("Configuration file does not exist"),t;let r=await import(e),o=r.default||r;if(o===void 0)t.push("Configuration file must export a default value or named exports");else if(typeof o!=="object"||o===null)t.push("Configuration must be an object");else if(Array.isArray(o))t.push("Configuration cannot be an array at the root level");if(e.endsWith(".json"))try{let{readFileSync:m}=await import("fs"),h=m(e,"utf8");JSON.parse(h)}catch(m){t.push(`Invalid JSON syntax: ${m}`)}}catch(r){t.push(`Failed to load configuration file: ${r}`)}return t}async getFileModificationTime(e){try{let{statSync:t}=await import("fs");return t(e).mtime}catch{return null}}async preloadConfigurations(e,t={}){let r=new Map;return await Promise.allSettled(e.map(async(o)=>{try{let m=await this.loadFromPath(o,{},t);if(m)r.set(o,m.config)}catch(m){if(t.verbose)console.warn(`Failed to preload ${o}:`,m)}})),r}}class Hm{async validateConfiguration(e,t,r={}){let{stopOnFirstError:o=!1,validateRequired:m=!0,validateTypes:h=!0,customRules:I=[],trackPerformance:R=!0,verbose:A=!1}=r,E=async()=>{let x=[],N=[],F={stopOnFirstError:o,validateRequired:m,validateTypes:h,customRules:I,trackPerformance:R,verbose:A};try{if(typeof t==="string")return await this.validateWithSchemaFile(e,t,F);else if(Array.isArray(t))return this.validateWithRules(e,[...t,...I],F);else return this.validateWithJSONSchema(e,t,F)}catch(z){return x.push({path:"",message:`Validation failed: ${z}`,rule:"system"}),{isValid:!1,errors:x,warnings:N}}};if(R)return await fc.track("validateConfiguration",E);return E()}async validateWithSchemaFile(e,t,r){try{if(!bE(t))throw new gc(t,[{path:"",message:"Schema file does not exist"}]);let o=await import(t),m=o.default||o;if(Array.isArray(m))return this.validateWithRules(e,m,r);else return this.validateWithJSONSchema(e,m,r)}catch(o){throw new gc(t,[{path:"",message:`Failed to load schema: ${o}`}])}}validateWithJSONSchema(e,t,r){let o=[],m=[];return this.validateObjectAgainstSchema(e,t,"",o,m,r),{isValid:o.length===0,errors:o,warnings:m}}validateObjectAgainstSchema(e,t,r,o,m,h){if(h.validateTypes&&t.type){let I=Array.isArray(e)?"array":typeof e,R=Array.isArray(t.type)?t.type:[t.type];if(!R.includes(I)){if(o.push({path:r,message:`Expected type ${R.join(" or ")}, got ${I}`,expected:R.join(" or "),actual:I,rule:"type"}),h.stopOnFirstError)return}}if(t.enum&&!t.enum.includes(e)){if(o.push({path:r,message:`Value must be one of: ${t.enum.join(", ")}`,expected:t.enum.join(", "),actual:e,rule:"enum"}),h.stopOnFirstError)return}if(typeof e==="string"){if(t.minLength!==void 0&&e.length<t.minLength)o.push({path:r,message:`String length must be at least ${t.minLength}`,expected:`>= ${t.minLength}`,actual:e.length,rule:"minLength"});if(t.maxLength!==void 0&&e.length>t.maxLength)o.push({path:r,message:`String length must not exceed ${t.maxLength}`,expected:`<= ${t.maxLength}`,actual:e.length,rule:"maxLength"});if(t.pattern){if(!new RegExp(t.pattern).test(e))o.push({path:r,message:`String does not match pattern ${t.pattern}`,expected:t.pattern,actual:e,rule:"pattern"})}}if(typeof e==="number"){if(t.minimum!==void 0&&e<t.minimum)o.push({path:r,message:`Value must be at least ${t.minimum}`,expected:`>= ${t.minimum}`,actual:e,rule:"minimum"});if(t.maximum!==void 0&&e>t.maximum)o.push({path:r,message:`Value must not exceed ${t.maximum}`,expected:`<= ${t.maximum}`,actual:e,rule:"maximum"})}if(Array.isArray(e)&&t.items)for(let I=0;I<e.length;I++){let R=r?`${r}[${I}]`:`[${I}]`;if(this.validateObjectAgainstSchema(e[I],t.items,R,o,m,h),h.stopOnFirstError&&o.length>0)return}if(e&&typeof e==="object"&&!Array.isArray(e)){let I=e;if(h.validateRequired&&t.required){for(let R of t.required)if(!(R in I)){if(o.push({path:r?`${r}.${R}`:R,message:`Missing required property '${R}'`,expected:"required",rule:"required"}),h.stopOnFirstError)return}}if(t.properties){for(let[R,A]of Object.entries(t.properties))if(R in I){let E=r?`${r}.${R}`:R;if(this.validateObjectAgainstSchema(I[R],A,E,o,m,h),h.stopOnFirstError&&o.length>0)return}}if(t.additionalProperties===!1){let R=new Set(Object.keys(t.properties||{}));for(let A of Object.keys(I))if(!R.has(A))m.push({path:r?`${r}.${A}`:A,message:`Additional property '${A}' is not allowed`,rule:"additionalProperties"})}}}validateWithRules(e,t,r){let o=[],m=[];for(let h of t)try{let I=this.getValueByPath(e,h.path),R=this.validateWithRule(I,h,h.path);if(o.push(...R),r.stopOnFirstError&&o.length>0)break}catch(I){o.push({path:h.path,message:`Rule validation failed: ${I}`,rule:"system"})}return{isValid:o.length===0,errors:o,warnings:m}}validateWithRule(e,t,r){let o=[];if(t.required&&(e===void 0||e===null))return o.push({path:r,message:t.message||`Property '${r}' is required`,expected:"required",rule:"required"}),o;if(e===void 0||e===null)return o;if(t.type){let m=Array.isArray(e)?"array":typeof e;if(m!==t.type)o.push({path:r,message:t.message||`Expected type ${t.type}, got ${m}`,expected:t.type,actual:m,rule:"type"})}if(t.min!==void 0){let m=Array.isArray(e)?e.length:typeof e==="string"?e.length:typeof e==="number"?e:0;if(m<t.min)o.push({path:r,message:t.message||`Value must be at least ${t.min}`,expected:`>= ${t.min}`,actual:m,rule:"min"})}if(t.max!==void 0){let m=Array.isArray(e)?e.length:typeof e==="string"?e.length:typeof e==="number"?e:0;if(m>t.max)o.push({path:r,message:t.message||`Value must not exceed ${t.max}`,expected:`<= ${t.max}`,actual:m,rule:"max"})}if(t.pattern&&typeof e==="string"){if(!t.pattern.test(e))o.push({path:r,message:t.message||`Value does not match pattern ${t.pattern}`,expected:t.pattern.toString(),actual:e,rule:"pattern"})}if(t.enum&&!t.enum.includes(e))o.push({path:r,message:t.message||`Value must be one of: ${t.enum.join(", ")}`,expected:t.enum.join(", "),actual:e,rule:"enum"});if(t.validator){let m=t.validator(e);if(m)o.push({path:r,message:t.message||m,rule:"custom"})}return o}getValueByPath(e,t){if(!t)return e;let r=t.split("."),o=e;for(let m of r)if(o&&typeof o==="object"&&m in o)o=o[m];else return;return o}generateRulesFromInterface(e){let t=[],r=e.matchAll(/(\w+)(\?)?:\s*(\w+)/g);for(let o of r){let[,m,h,I]=o;t.push({path:m,required:!h,type:this.mapTypeScriptType(I)})}return t}mapTypeScriptType(e){switch(e.toLowerCase()){case"string":return"string";case"number":return"number";case"boolean":return"boolean";case"array":return"array";case"object":return"object";default:return"object"}}static createCommonRules(){return{server:[{path:"port",required:!0,type:"number",min:1,max:65535},{path:"host",required:!0,type:"string",min:1},{path:"ssl",type:"boolean"}],database:[{path:"url",required:!0,type:"string",min:1},{path:"pool",type:"number",min:1,max:100},{path:"timeout",type:"number",min:0}],api:[{path:"baseUrl",required:!0,type:"string",pattern:Xy},{path:"timeout",type:"number",min:0},{path:"retries",type:"number",min:0,max:10}]}}}class Km{fileLoader=new mu;envProcessor=new pu;validator=new Hm;async loadConfig(e){let t=Date.now(),{cache:r,performance:o,schema:m,validate:h,...I}=e;try{if(r?.enabled){let A=this.checkCache(I.name||"",I);if(A)return A}let R;try{R=await this.loadConfigurationStrategies(I,!0,r)}catch(A){let E=I.__strictErrorHandling;if(A instanceof Error&&A.name==="ConfigNotFoundError"){if(E)throw A;R={...await this.applyEnvironmentVariables(I.name||"",I.defaultConfig,I.checkEnv!==!1,I.verbose||!1),warnings:[`No configuration file found for "${I.name||"config"}", using defaults with environment variables`]}}else if(A instanceof Error&&A.name==="ConfigLoadError"){let x=A.message.includes("EACCES")||A.message.includes("EPERM")||A.message.includes("permission denied"),N=!x&&(A.message.includes("syntax")||A.message.includes("Expected")||A.message.includes("Unexpected")||A.message.includes("BuildMessage")||A.message.includes("errors building")),F=A.message.includes("Configuration must export a valid object")||A.message.includes("Configuration file is empty and exports nothing");if(E&&(F||x))throw A;if(N&&(!E||!F))R={...await this.applyEnvironmentVariables(I.name||"",I.defaultConfig,I.checkEnv!==!1,I.verbose||!1),warnings:["Configuration file has syntax errors, using defaults with environment variables"]};else if(!E)R={...await this.applyEnvironmentVariables(I.name||"",I.defaultConfig,I.checkEnv!==!1,I.verbose||!1),warnings:[`Configuration loading error, using defaults: ${A.message}`]};else throw A}else R={...await this.applyEnvironmentVariables(I.name||"",I.defaultConfig,I.checkEnv!==!1,I.verbose||!1),warnings:[`Configuration loading failed, using defaults: ${A instanceof Error?A.message:String(A)}`]}}if(m||h)await this.validateConfiguration(R.config,m,h,I.name);if(r?.enabled&&R)this.cacheResult(I.name||"",R,r,I);if(o?.enabled){let A={operation:"loadConfig",duration:Date.now()-t,configName:I.name,timestamp:new Date};if(o.onMetrics)o.onMetrics(A);if(o.slowThreshold&&A.duration>o.slowThreshold)Kn.warn(`Slow configuration loading detected: ${A.duration}ms for ${I.name}`);R.metrics=A}return R}catch(R){let A=Date.now()-t;throw Kn.error(`Configuration loading failed after ${A}ms:`,[R instanceof Error?R:Error(String(R))]),R}}async loadConfigurationStrategies(e,t=!1,r){let{name:o="",alias:m,cwd:h,configDir:I,defaultConfig:R,checkEnv:A=!0,arrayStrategy:E="replace",verbose:x=!1}=e,N=h||Bs.cwd(),F=[],z=await this.loadLocalConfiguration(o,m,N,I,R,E,x,A,r);if(z)return F.push(...this.getLocalSearchPaths(o,m,N,I)),this.finalizeResult(z,F,A,o,x);let K=await this.loadHomeConfiguration(o,m,R,E,x,A);if(K)return F.push(...this.getHomeSearchPaths(o,m)),this.finalizeResult(K,F,A,o,x);let J=await this.loadPackageJsonConfiguration(o,m,N,R,E,x,A);if(J)return F.push(Sn(N,"package.json")),this.finalizeResult(J,F,A,o,x);if(F.push(...this.getAllSearchPaths(o,m,N,I)),t)throw us.configNotFound(o,F,m);return{...await this.applyEnvironmentVariables(o,R,A,x),warnings:[`No configuration file found for "${o}"${m?` or alias "${m}"`:""}, using defaults with environment variables`]}}async loadLocalConfiguration(e,t,r,o,m,h,I,R,A){let E=R?uc(e,m,I):m,x=this.getLocalDirectories(r,o);for(let N of x){if(I)Kn.info(`Searching for configuration in: ${N}`);let F=this.fileLoader.generateConfigPaths(e,N,t),z=await this.fileLoader.tryLoadFromPaths(F,E,{arrayStrategy:h,verbose:I,cacheTtl:A?.ttl,useCache:!A?.ttl||A.ttl>100});if(z){if(I)Kn.success(`Configuration loaded from: ${z.source.path}`);return z}}return null}async loadHomeConfiguration(e,t,r,o,m,h){if(!e)return null;let I=h?uc(e,r,m):r,R=[Sn(Aa(),".config",e),Sn(Aa(),".config"),Aa()];for(let A of R){if(m)Kn.info(`Checking home directory: ${A}`);let E=this.fileLoader.generateConfigPaths(e,A,t),x=await this.fileLoader.tryLoadFromPaths(E,I,{arrayStrategy:o,verbose:m});if(x){if(m)Kn.success(`Configuration loaded from home directory: ${x.source.path}`);return x}}return null}async loadPackageJsonConfiguration(e,t,r,o,m,h,I){let R=I?uc(e,o,h):o;try{let A=Sn(r,"package.json");if(!Rm(A))return null;let E=await import(A),x=E[e],N=e;if(!x&&t)x=E[t],N=t;if(x&&typeof x==="object"&&!Array.isArray(x)){if(h)Kn.success(`Configuration loaded from package.json: ${N}`);return{config:Wm(R,x,m),source:{type:"package.json",path:A,priority:30,timestamp:new Date}}}}catch(A){if(h)Kn.warn("Failed to load package.json:",[A instanceof Error?A:Error(String(A))])}return null}async applyEnvironmentVariables(e,t,r,o){if(!r||!e||typeof t!=="object"||t===null||Array.isArray(t))return{config:t,source:{type:"default",priority:10,timestamp:new Date}};return{config:uc(e,t,o),source:{type:"environment",priority:20,timestamp:new Date}}}async finalizeResult(e,t,r,o,m){return{config:e.config,source:e.source,path:e.source.path}}async validateConfiguration(e,t,r,o){let m=[];if(r){let h=r(e);if(h)m.push(...h)}if(t){let h=await this.validator.validateConfiguration(e,t);if(!h.isValid)m.push(...h.errors.map((I)=>I.path?`${I.path}: ${I.message}`:I.message))}if(m.length>0)throw us.configValidation(o||"unknown",m,o)}checkCache(e,t){let r=this.generateCacheKey(e,t);return ja.get(r)||null}cacheResult(e,t,r,o){let m=this.generateCacheKey(e,o);ja.set(m,t,void 0,r.ttl)}generateCacheKey(e,t){let r=[e];if(t.alias)r.push(`alias:${t.alias}`);if(t.cwd)r.push(`cwd:${t.cwd}`);if(t.configDir)r.push(`configDir:${t.configDir}`);if("checkEnv"in t)r.push(`checkEnv:${t.checkEnv}`);return r.join("|")}getLocalDirectories(e,t){return Array.from(new Set([e,Sn(e,"config"),Sn(e,".config"),t?Sn(e,t):void 0].filter(Boolean)))}getAllSearchPaths(e,t,r,o){let m=[];return m.push(...this.getLocalSearchPaths(e,t,r,o)),m.push(...this.getHomeSearchPaths(e,t)),m.push(Sn(r,"package.json")),m}getLocalSearchPaths(e,t,r,o){let m=this.getLocalDirectories(r,o),h=[];for(let I of m)h.push(...this.fileLoader.generateConfigPaths(e,I,t));return h}getHomeSearchPaths(e,t){if(!e)return[];let r=[Sn(Aa(),".config",e),Sn(Aa(),".config"),Aa()],o=[];for(let m of r)o.push(...this.fileLoader.generateConfigPaths(e,m,t));return o}async loadConfigWithResult(e){return this.loadConfig(e)}}function by(e){let r=!(e.message.includes("EACCES")||e.message.includes("EPERM")||e.message.includes("permission denied"))&&(e.message.includes("syntax")||e.message.includes("Expected")||e.message.includes("Unexpected")||e.message.includes("BuildMessage")),o=e.message.includes("Configuration must export a valid object")||e.message.includes("Configuration file is empty and exports nothing");return r||o}async function oR(e){return Ji.loadConfig({...e,__strictErrorHandling:!0})}async function Ky(e){let t="defaultConfig"in e&&e.defaultConfig!==void 0?e.defaultConfig:{},r="cache"in e||"performance"in e||"schema"in e||"validate"in e;try{let o;if(r)o=await Ji.loadConfig(e);else o=await Ji.loadConfig({...e,defaultConfig:t,cache:{enabled:!0},performance:{enabled:!1}});return o?.config??t}catch(o){let m=o instanceof Error?o.name:"UnknownError",h=o instanceof Error?o.message:String(o);if(!(m==="ConfigNotFoundError"||m==="ConfigLoadError"||m==="ConfigValidationError"||h.includes("config"))&&e.verbose)Kn.warn("Unexpected error loading config, using defaults:",[o instanceof Error?o:Error(String(o))]);let R=r?{...e,defaultConfig:t}:{...e,defaultConfig:t,cache:{enabled:!0},performance:{enabled:!1}};if("checkEnv"in e?e.checkEnv!==!1:!0)return(await Ji.applyEnvironmentVariables(R.name||"",t,!0,R.verbose||!1))?.config??t;return t}}async function sR(e={defaultConfig:{}}){if(typeof e==="string"){let{cwd:t}=await import("process");try{return(await Ji.loadConfig({name:e,cwd:t(),generatedDir:"./generated",configDir:"./config",defaultConfig:{},checkEnv:!0,arrayStrategy:"replace"})).config}catch(r){if(r instanceof Error&&(r.name==="ConfigNotFoundError"||r.name==="ConfigLoadError"&&by(r)))return(await Ji.applyEnvironmentVariables(e,{},!0,!1)).config;throw r}}try{return(await Ji.loadConfig({...e,cwd:e.cwd||Bs.cwd(),cache:{enabled:!0},performance:{enabled:!1}})).config}catch(t){if(t instanceof Error&&(t.name==="ConfigNotFoundError"||t.name==="ConfigLoadError"&&by(t)))return(await Ji.applyEnvironmentVariables(e.name||"",e.defaultConfig||{},e.checkEnv!==!1,e.verbose||!1)).config;throw t}}async function aR(e,t,r="replace"){let o=new mu;try{let m=await o.loadFromPath(e,t,{arrayStrategy:r,useCache:!1,trackPerformance:!1});return m?m.config:null}catch{return null}}function uc(e,t,r=!1){let o=new pu,m=e.toUpperCase().replace(/[^A-Z0-9]/g,"_");function h(I,R=[]){let A={...I};for(let[E,x]of Object.entries(I)){let N=[...R,E],F=[`${m}_${N.join("_").toUpperCase()}`,`${m}_${N.map((J)=>J.toUpperCase()).join("")}`,`${m}_${N.map((J)=>J.replace(/([A-Z])/g,"_$1").toUpperCase()).join("")}`],z,K;for(let J of F)if(z=Bs.env[J],z!==void 0){K=J;break}if(z!==void 0&&K)if(typeof x==="boolean")A[E]=["true","1","yes"].includes(z.toLowerCase());else if(typeof x==="number"){let J=Number(z);if(!Number.isNaN(J))A[E]=J}else if(Array.isArray(x))try{A[E]=JSON.parse(z)}catch{A[E]=z.split(",").map((J)=>J.trim())}else A[E]=z;else if(x&&typeof x==="object"&&!Array.isArray(x))A[E]=h(x,N)}return A}return h(t)}function cR(e){let t=Sn(Bs.cwd(),e.configDir),r=Sn(Bs.cwd(),e.generatedDir),o=Sn(r,"config-types.ts");if(!Rm(Qh(o)))QI(Qh(o),{recursive:!0,mode:511});let m=Rm(t)?YI(t).map((I)=>I.replace(/\.(ts|js|mjs|cjs|mts|cts|json)$/,"")).sort():[],h=`// Generated by bunfig v${MR}
|
|
132
|
+
export type ConfigNames = ${m.length?`'${m.join("' | '")}'`:"string"}
|
|
133
|
+
`;ZI(o,h,{mode:438})}function dR(e){let t=null,r=null,o=()=>{if(!r)r=Ky(e).then((h)=>(t=h,h),(h)=>{let I="defaultConfig"in e?e.defaultConfig:{};if(t=I,"verbose"in e&&e.verbose)Kn.warn("Config loading failed, using defaults:",[h instanceof Error?h:Error(String(h))]);return I});return r},m="defaultConfig"in e?e.defaultConfig:{};return t=m,o(),new Proxy({},{get(h,I){if(t)return t[I];let R=m[I];return o(),R},has(h,I){return I in(t||m)},ownKeys(){return Object.keys(t||m)},getOwnPropertyDescriptor(h,I){return Object.getOwnPropertyDescriptor(t||m,I)},set(h,I,R){if(!t)t={...m};return t[I]=R,!0}})}function lR(e){let t=uy(kE.cwd(),e?.configDir||"./config");function r(){if(!SE(t))return[];let m=new Set([".ts",".js",".mjs",".cjs",".mts",".cts",".json"]),h=[".ts",".mts",".cts",".js",".mjs",".cjs",".json"],I=wE(t).filter((A)=>m.has(Hp(A))).map((A)=>({base:A.replace(/\.(?:ts|js|mjs|cjs|mts|cts|json)$/i,""),file:A})),R=new Map;for(let{base:A,file:E}of I){let x=Hp(E).toLowerCase(),N=R.get(A);if(!N){R.set(A,E);continue}let F=Hp(N).toLowerCase();if(h.indexOf(x)<h.indexOf(F))R.set(A,E)}return Array.from(R.entries()).map(([A,E])=>({base:A,file:E})).sort((A,E)=>A.base.localeCompare(E.base))}function o(){let m=r(),h=m.map((E)=>E.base),I=h.length?h.map((E)=>`'${E}'`).join(" | "):"string",R=m.length?`{
|
|
134
|
+
${m.map((E)=>{let x=uy(t,E.file).replace(/\\/g,"/");return` '${E.base}': typeof import('${x}').default`}).join(`,
|
|
135
|
+
`)}
|
|
136
|
+
}`:"Record<string, any>";return`export type ConfigNames = ${I}
|
|
137
|
+
export type ConfigByName = ${R}
|
|
138
|
+
export type Config<N extends ConfigNames> = N extends keyof ConfigByName ? ConfigByName[N] : unknown
|
|
139
|
+
export type ConfigOf = Config
|
|
140
|
+
`}return{name:"bunfig-plugin",setup(m){m.onResolve({filter:/^virtual:bunfig-types$/},(h)=>({path:h.path,namespace:"bunfig-virtual"})),m.onLoad({filter:/^virtual:bunfig-types$/,namespace:"bunfig-virtual"},()=>({contents:o(),loader:"ts"}))}}}var Jy,ja,fc,Gy,uR,pR,Sy,Wl,xm,an,ar,mR,wy,gR,ky,fR,hR,Qp,yR,Iy,Ey,Yp,vR,bR,SR,wR,Hl,Qy,kR,cn,IR,ER,Ry,Kl,Dm,Tr,Ot,RR,Ay,AR,Cy,CR,PR,Zp,_R,Py,_y,TR,Xp,xR,DR,OR,$R,NR,Jl,Yy,jR,MR="0.15.6",On,su,mc,Om,$m,au,Nm,jm,gc,Mm,Bm,us,Zy,Xy,Kn,Ji,ev,tv,BR=BE(async()=>{Jy=import.meta.require,ja=new zm,fc=new Um,Gy={createKey:LE,isEquivalent:FE,estimateMemoryUsage:zE},uR=eu(Pm.cwd(),"config"),pR=eu(Pm.cwd(),"src/generated"),Sy=Xl.env.CLARITY_LOG_DIR||tE(VE(),"logs"),Wl={level:"info",defaultName:"clarity",timestamp:!0,colors:!0,format:"text",maxLogSize:10485760,logDatePattern:"YYYY-MM-DD",logDirectory:Sy,rotation:{frequency:"daily",maxSize:10485760,maxFiles:5,compress:!1,rotateHour:0,rotateMinute:0,rotateDayOfWeek:0,rotateDayOfMonth:1,encrypt:!1},verbose:!1},xm=await WE(),an={red:(e)=>`\x1B[31m${e}\x1B[0m`,green:(e)=>`\x1B[32m${e}\x1B[0m`,yellow:(e)=>`\x1B[33m${e}\x1B[0m`,blue:(e)=>`\x1B[34m${e}\x1B[0m`,magenta:(e)=>`\x1B[35m${e}\x1B[0m`,cyan:(e)=>`\x1B[36m${e}\x1B[0m`,white:(e)=>`\x1B[37m${e}\x1B[0m`,gray:(e)=>`\x1B[90m${e}\x1B[0m`,bgRed:(e)=>`\x1B[41m${e}\x1B[0m`,bgYellow:(e)=>`\x1B[43m${e}\x1B[0m`,bold:(e)=>`\x1B[1m${e}\x1B[0m`,dim:(e)=>`\x1B[2m${e}\x1B[0m`,italic:(e)=>`\x1B[3m${e}\x1B[0m`,underline:(e)=>`\x1B[4m${e}\x1B[0m`,reset:"\x1B[0m"},ar=an,mR=an.red,wy=an.green,gR=an.yellow,ky=an.blue,fR=an.magenta,hR=an.cyan,Qp=an.white,yR=an.gray,Iy=an.bgRed,Ey=an.bgYellow,Yp=an.bold,vR=an.dim,bR=an.italic,SR=an.underline,wR=an.reset,Hl={activationLevel:"error",bufferSize:50,flushOnDeactivation:!0,stopBuffering:!1},Qy={debug:"\uD83D\uDD0D",info:ky("ℹ"),success:wy("✓"),warning:Ey(Qp(Yp(" WARN "))),error:Iy(Qp(Yp(" ERROR ")))},kR=new nu("stacks"),cn=new nu("bunfig",{showTags:!0}),IR=bn(pc.cwd(),"config"),ER=bn(pc.cwd(),"src/generated"),Ry=Zl.env.CLARITY_LOG_DIR||XI(GE(),"logs"),Kl={level:"info",defaultName:"clarity",timestamp:!0,colors:!0,format:"text",maxLogSize:10485760,logDatePattern:"YYYY-MM-DD",logDirectory:Ry,rotation:{frequency:"daily",maxSize:10485760,maxFiles:5,compress:!1,rotateHour:0,rotateMinute:0,rotateDayOfWeek:0,rotateDayOfMonth:1,encrypt:!1},verbose:!1,writeToFile:!1},Dm=await QE(),Tr={red:(e)=>`\x1B[31m${e}\x1B[0m`,green:(e)=>`\x1B[32m${e}\x1B[0m`,yellow:(e)=>`\x1B[33m${e}\x1B[0m`,blue:(e)=>`\x1B[34m${e}\x1B[0m`,magenta:(e)=>`\x1B[35m${e}\x1B[0m`,cyan:(e)=>`\x1B[36m${e}\x1B[0m`,white:(e)=>`\x1B[37m${e}\x1B[0m`,gray:(e)=>`\x1B[90m${e}\x1B[0m`,bgRed:(e)=>`\x1B[41m${e}\x1B[0m`,bgYellow:(e)=>`\x1B[43m${e}\x1B[0m`,bgGray:(e)=>`\x1B[100m${e}\x1B[0m`,bold:(e)=>`\x1B[1m${e}\x1B[0m`,dim:(e)=>`\x1B[2m${e}\x1B[0m`,italic:(e)=>`\x1B[3m${e}\x1B[0m`,underline:(e)=>`\x1B[4m${e}\x1B[0m`,strikethrough:(e)=>`\x1B[9m${e}\x1B[0m`,reset:"\x1B[0m"},Ot=Tr,RR=Tr.red,Ay=Tr.green,AR=Tr.yellow,Cy=Tr.blue,CR=Tr.magenta,PR=Tr.cyan,Zp=Tr.white,_R=Tr.gray,Py=Tr.bgRed,_y=Tr.bgYellow,TR=Tr.bgGray,Xp=Tr.bold,xR=Tr.dim,DR=Tr.italic,OR=Tr.underline,$R=Tr.strikethrough,NR=Tr.reset,Jl={activationLevel:"error",bufferSize:50,flushOnDeactivation:!0,stopBuffering:!1},Yy={debug:"\uD83D\uDD0D",info:Cy("ℹ"),success:Ay("✓"),warning:_y(Zp(Xp(" WARN "))),error:Py(Zp(Xp(" ERROR ")))},jR=new ou("stacks"),On=class extends Error{timestamp;context;constructor(t,r={}){super(t);if(this.name=this.constructor.name,this.timestamp=new Date,this.context=r,Error.captureStackTrace)Error.captureStackTrace(this,this.constructor)}toJSON(){return{name:this.name,code:this.code,message:this.message,timestamp:this.timestamp.toISOString(),context:this.context,stack:this.stack}}toString(){let t=Object.keys(this.context).length>0?` (${Object.entries(this.context).map(([r,o])=>`${r}: ${o}`).join(", ")})`:"";return`${this.name} [${this.code}]: ${this.message}${t}`}},su=class extends On{code="CONFIG_NOT_FOUND";constructor(t,r,o){let m=o?` or alias "${o}"`:"";super(`Configuration "${t}"${m} not found`,{configName:t,alias:o,searchPaths:r,searchPathCount:r.length})}},mc=class extends On{code="CONFIG_LOAD_ERROR";constructor(t,r,o){super(`Failed to load configuration from "${t}": ${r.message}`,{configPath:t,configName:o,originalError:r.name,originalMessage:r.message});this.cause=r}},Om=class extends On{code="CONFIG_VALIDATION_ERROR";constructor(t,r,o){super(`Configuration validation failed for "${t}"`,{configPath:t,configName:o,validationErrors:r,errorCount:r.length})}},$m=class extends On{code="CONFIG_MERGE_ERROR";constructor(t,r,o,m){super(`Failed to merge configuration from "${t}" with "${r}": ${o.message}`,{sourcePath:t,targetPath:r,configName:m,originalError:o.name,originalMessage:o.message});this.cause=o}},au=class extends On{code="ENV_VAR_ERROR";constructor(t,r,o,m){super(`Failed to parse environment variable "${t}" with value "${r}" as ${o}`,{envKey:t,envValue:r,expectedType:o,configName:m})}},Nm=class extends On{code="FILE_SYSTEM_ERROR";constructor(t,r,o){super(`File system ${t} failed for "${r}": ${o.message}`,{operation:t,path:r,originalError:o.name,originalMessage:o.message});this.cause=o}},jm=class extends On{code="TYPE_GENERATION_ERROR";constructor(t,r,o){super(`Failed to generate types from "${t}" to "${r}": ${o.message}`,{configDir:t,outputPath:r,originalError:o.name,originalMessage:o.message});this.cause=o}},gc=class extends On{code="SCHEMA_VALIDATION_ERROR";constructor(t,r,o){super(`Schema validation failed${o?` for config "${o}"`:""}`,{schemaPath:t,configName:o,validationErrors:r,errorCount:r.length})}},Mm=class extends On{code="BROWSER_CONFIG_ERROR";constructor(t,r,o,m){super(`Failed to fetch configuration from "${t}": ${r} ${o}`,{endpoint:t,status:r,statusText:o,configName:m})}},Bm=class extends On{code="PLUGIN_ERROR";constructor(t,r,o){super(`Plugin "${t}" failed during ${r}: ${o.message}`,{pluginName:t,operation:r,originalError:o.name,originalMessage:o.message});this.cause=o}},us={configNotFound(e,t,r){return new su(e,t,r)},configLoad(e,t,r){return new mc(e,t,r)},configValidation(e,t,r){return new Om(e,t,r)},configMerge(e,t,r,o){return new $m(e,t,r,o)},envVar(e,t,r,o){return new au(e,t,r,o)},fileSystem(e,t,r){return new Nm(e,t,r)},typeGeneration(e,t,r){return new jm(e,t,r)},schemaValidation(e,t,r){return new gc(e,t,r)},browserConfig(e,t,r,o){return new Mm(e,t,r,o)},plugin(e,t,r){return new Bm(e,t,r)}},Zy={replace:"replace",concat:"concat",smart:"smart"},Xy=/^https?:\/\//,Kn=new ou("bunfig",{showTags:!0}),Ji=new Km,ev=Sn(Bs.cwd(),"config"),tv=Sn(Bs.cwd(),"src/generated")});function LR(e,t={}){let r=tu.cwd();while(r.includes("storage"))r=py(r,"..");let o=py(r,e||"");if(t?.relative)return EE(tu.cwd(),o);return o}var FR=tu.env.CLARITY_LOG_DIR||IE(LR(),"logs"),rv={level:"info",defaultName:"clarity",timestamp:!0,colors:!0,format:"text",maxLogSize:10485760,logDatePattern:"YYYY-MM-DD",logDirectory:FR,rotation:{frequency:"daily",maxSize:10485760,maxFiles:5,compress:!1,rotateHour:0,rotateMinute:0,rotateDayOfWeek:0,rotateDayOfMonth:1,encrypt:!1},verbose:!1,writeToFile:!1},cu={...rv},EN=(async()=>{try{let{loadConfig:e}=await BR().then(()=>Ly),t=await e({name:"clarity",alias:"logging",defaultConfig:rv,cwd:tu.cwd()});if(t)Object.assign(cu,t)}catch{}return cu})();function xr(){if(Ki.env.NODE_ENV==="test"||Ki.env.BUN_ENV==="test")return!1;return typeof window<"u"}async function zR(){if(Ki.env.NODE_ENV==="test"||Ki.env.BUN_ENV==="test")return!0;if(typeof navigator<"u"&&navigator.product==="ReactNative")return!0;if(typeof Ki<"u"){let e=Ki.type;if(e==="renderer"||e==="worker")return!1;return!!(Ki.versions&&(Ki.versions.node||Ki.versions.bun))}return!1}class nv{async format(e){let t=await zR(),r=await this.getMetadata(t);return JSON.stringify({timestamp:e.timestamp.toISOString(),level:e.level,name:e.name,message:e.message,metadata:r})}async getMetadata(e){if(e){let{hostname:t}=await import("os");return{pid:Na.pid,hostname:t(),environment:Na.env.NODE_ENV||"development",platform:Na.platform,version:Na.version}}return{userAgent:navigator.userAgent,hostname:window.location.hostname||"browser",environment:Na.env.NODE_ENV||Na.env.BUN_ENV||"development",viewport:{width:window.innerWidth,height:window.innerHeight},language:navigator.language}}}var Br={red:(e)=>`\x1B[31m${e}\x1B[0m`,green:(e)=>`\x1B[32m${e}\x1B[0m`,yellow:(e)=>`\x1B[33m${e}\x1B[0m`,blue:(e)=>`\x1B[34m${e}\x1B[0m`,magenta:(e)=>`\x1B[35m${e}\x1B[0m`,cyan:(e)=>`\x1B[36m${e}\x1B[0m`,white:(e)=>`\x1B[37m${e}\x1B[0m`,gray:(e)=>`\x1B[90m${e}\x1B[0m`,bgRed:(e)=>`\x1B[41m${e}\x1B[0m`,bgYellow:(e)=>`\x1B[43m${e}\x1B[0m`,bgGray:(e)=>`\x1B[100m${e}\x1B[0m`,bold:(e)=>`\x1B[1m${e}\x1B[0m`,dim:(e)=>`\x1B[2m${e}\x1B[0m`,italic:(e)=>`\x1B[3m${e}\x1B[0m`,underline:(e)=>`\x1B[4m${e}\x1B[0m`,strikethrough:(e)=>`\x1B[9m${e}\x1B[0m`,reset:"\x1B[0m"},Ht=Br,{red:RN,green:UR,yellow:AN,blue:qR,magenta:CN,cyan:PN,white:Ty,gray:_N,bgRed:VR,bgYellow:WR,bgGray:TN,bold:xy,dim:xN,italic:DN,underline:ON,strikethrough:$N,reset:NN}=Br,wm={activationLevel:"error",bufferSize:50,flushOnDeactivation:!0,stopBuffering:!1},HR={debug:"\uD83D\uDD0D",info:qR("ℹ"),success:UR("✓"),warning:WR(Ty(xy(" WARN "))),error:VR(Ty(xy(" ERROR ")))};class gu{name;fileLocks=new Map;currentKeyId=null;keys=new Map;fingersCrossedConfig;fingersCrossedActive=!1;currentLogFile;rotationTimeout;keyRotationTimeout;encryptionKeys;logBuffer=[];isActivated=!1;pendingOperations=[];enabled;fancy;tagFormat;timestampPosition;environment;config;options;formatter;timers=new Set;subLoggers=new Set;fingersCrossedBuffer=[];ANSI_PATTERN=/\u001B\[.*?m/g;activeProgressBar=null;constructor(e,t={}){this.name=e,this.config={...cu},this.options=this.normalizeOptions(t),this.formatter=this.options.formatter||new nv,this.enabled=t.enabled??!0,this.fancy=t.fancy??!0,this.tagFormat=t.tagFormat??{prefix:"[",suffix:"]"},this.timestampPosition=t.timestampPosition??"right",this.environment=t.environment??Xt.env.APP_ENV??"local",this.fingersCrossedConfig=this.initializeFingersCrossedConfig(t);let r={...t},o=t.timestamp!==void 0;if(o)delete r.timestamp;if(this.config={...this.config,...r,timestamp:o||this.config.timestamp,level:this.options.level??"info"},this.currentLogFile=this.generateLogFilename(),this.encryptionKeys=new Map,this.validateEncryptionConfig()){this.setupRotation();let m=this.generateKeyId(),h=this.generateKey();this.currentKeyId=m,this.keys.set(m,h),this.encryptionKeys.set(m,{key:h,createdAt:new Date}),this.setupKeyRotation()}}shouldActivateFingersCrossed(e){if(!this.fingersCrossedConfig)return!1;let t={debug:0,info:1,success:2,warning:3,error:4},r=this.fingersCrossedConfig.activationLevel??"error";return t[e]>=t[r]}initializeFingersCrossedConfig(e){if(!e.fingersCrossedEnabled&&e.fingersCrossed)return{...wm,...e.fingersCrossed};if(!e.fingersCrossedEnabled)return null;if(!e.fingersCrossed)return{...wm};return{...wm,...e.fingersCrossed}}normalizeOptions(e){let t={format:"json",level:"info",logDirectory:cu.logDirectory,rotation:void 0,timestamp:void 0,fingersCrossed:{},enabled:!0,showTags:!1,showIcons:!0,formatter:void 0},r={...t,...Object.fromEntries(Object.entries(e).filter(([,o])=>o!==void 0))};if(!r.level||!["debug","info","success","warning","error"].includes(r.level))r.level=t.level;return r}shouldWriteToFile(){return!xr()&&this.config.writeToFile===!0}async writeToFile(e){let r=(async()=>{let m,h=0,I=3,R=1000;while(h<I)try{try{try{await _E(this.config.logDirectory,hy.F_OK|hy.W_OK)}catch(E){if(E instanceof Error&&"code"in E)if(E.code==="ENOENT")await TE(this.config.logDirectory,{recursive:!0,mode:493});else if(E.code==="EACCES")throw Error(`No write permission for log directory: ${this.config.logDirectory}`);else throw E;else throw E}}catch(E){throw console.error("Debug: [writeToFile] Failed to create log directory:",E),E}let A=this.validateEncryptionConfig()?(await this.encrypt(e)).encrypted:zi.from(e);try{if(!Ad(this.currentLogFile))await Gp(this.currentLogFile,"",{mode:420});if(m=fy(this.currentLogFile,"a",420),PE(m,A,{flag:"a"}),gy(m),m!==void 0)Jp(m),m=void 0;if((await Oa(this.currentLogFile)).size===0){if(await Gp(this.currentLogFile,A,{flag:"w",mode:420}),(await Oa(this.currentLogFile)).size===0)throw Error("File exists but is empty after retry write")}return}catch(E){let x=E;if(x.code&&["ENETDOWN","ENETUNREACH","ENOTFOUND","ETIMEDOUT"].includes(x.code)){if(h<I-1){let N=typeof x.message==="string"?x.message:"Unknown error";console.error(`Network error during write attempt ${h+1}/${I}:`,N);let F=R*2**h;await new Promise((z)=>setTimeout(z,F)),h++;continue}}if(x?.code&&["ENOSPC","EDQUOT"].includes(x.code))throw Error(`Disk quota exceeded or no space left on device: ${x.message}`);throw console.error("Debug: [writeToFile] Error writing to file:",x),x}finally{if(m!==void 0)try{Jp(m)}catch(E){console.error("Debug: [writeToFile] Error closing file descriptor:",E)}}}catch(A){if(h===I-1){let x=A,N=typeof x.message==="string"?x.message:"Unknown error";throw console.error("Debug: [writeToFile] Max retries reached. Final error:",N),A}h++;let E=R*2**(h-1);await new Promise((x)=>setTimeout(x,E))}})();this.pendingOperations.push(r);let o=this.pendingOperations.length-1;try{await r}catch(m){throw console.error("Debug: [writeToFile] Error in operation:",m),m}finally{this.pendingOperations.splice(o,1)}}generateLogFilename(){if(this.name.includes("stream-throughput")||this.name.includes("decompress-perf-test")||this.name.includes("decompression-latency")||this.name.includes("concurrent-read-test")||this.name.includes("clock-change-test"))return $a(this.config.logDirectory,`${this.name}.log`);if(this.name.includes("pending-test")||this.name.includes("temp-file-test")||this.name==="crash-test"||this.name==="corrupt-test"||this.name.includes("rotation-load-test")||this.name==="sigterm-test"||this.name==="sigint-test"||this.name==="failed-rotation-test"||this.name==="integration-test")return $a(this.config.logDirectory,`${this.name}.log`);let e=new Date().toISOString().split("T")[0];return $a(this.config.logDirectory,`${this.name}-${e}.log`)}setupRotation(){if(xr())return;if(!this.shouldWriteToFile())return;if(typeof this.config.rotation==="boolean")return;let e=this.config.rotation,t;switch(e.frequency){case"daily":t=86400000;break;case"weekly":t=604800000;break;case"monthly":t=2592000000;break;default:return}this.rotationTimeout=setInterval(()=>{this.rotateLog()},t)}setupKeyRotation(){if(!this.validateEncryptionConfig()){console.error("Invalid encryption configuration detected during key rotation setup");return}let t=this.config.rotation.keyRotation;if(!t?.enabled)return;let r=typeof t.interval==="number"?t.interval:60,o=Math.max(r,60)*1000;this.keyRotationTimeout=setInterval(()=>{this.rotateKeys().catch((m)=>{console.error("Error rotating keys:",m)})},o)}async rotateKeys(){if(!this.validateEncryptionConfig()){console.error("Invalid encryption configuration detected during key rotation");return}let t=this.config.rotation.keyRotation,r=this.generateKeyId(),o=this.generateKey();this.currentKeyId=r,this.keys.set(r,o),this.encryptionKeys.set(r,{key:o,createdAt:new Date});let m=Array.from(this.encryptionKeys.entries()).sort(([,R],[,A])=>A.createdAt.getTime()-R.createdAt.getTime()),h=typeof t.maxKeys==="number"?t.maxKeys:1,I=Math.max(1,h);if(m.length>I)for(let[R]of m.slice(I))this.encryptionKeys.delete(R),this.keys.delete(R)}generateKeyId(){return Kp(16).toString("hex")}generateKey(){return Kp(32)}getCurrentKey(){if(!this.currentKeyId)throw Error("Encryption is not properly initialized. Make sure encryption is enabled in the configuration.");let e=this.keys.get(this.currentKeyId);if(!e)throw Error(`No key found for ID ${this.currentKeyId}. The encryption key may have been rotated or removed.`);return{key:e,id:this.currentKeyId}}encrypt(e){let{key:t}=this.getCurrentKey(),r=Kp(16),o=RE("aes-256-gcm",t,r),m=zi.isBuffer(e)?e:zi.from(e,"utf8"),h=o.update(m),I=o.final(),R=h.length+I.length,A=o.getAuthTag(),E=zi.allocUnsafe(16+R+16);return r.copy(E,0),h.copy(E,16),I.copy(E,16+h.length),A.copy(E,16+R),{encrypted:E,iv:r}}async compressData(e){return new Promise((t,r)=>{let o=vy(),m=[];o.on("data",(h)=>m.push(h)),o.on("end",()=>t(zi.from(zi.concat(m)))),o.on("error",r),o.write(e),o.end()})}getEncryptionOptions(){if(!this.config.rotation||typeof this.config.rotation==="boolean"||!this.config.rotation.encrypt)return{};let e={algorithm:"aes-256-cbc",compress:!1};if(typeof this.config.rotation.encrypt==="object"){let t=this.config.rotation.encrypt;return{...e,...t}}return e}async rotateLog(){if(xr())return;if(!this.shouldWriteToFile())return;let e=await Oa(this.currentLogFile).catch(()=>null);if(!e)return;let t=this.config.rotation;if(typeof t==="boolean")return;if(t.maxSize&&e.size>=t.maxSize){let r=this.currentLogFile,o=this.generateLogFilename();if(this.name.includes("rotation-load-test")||this.name==="failed-rotation-test"){let m=await Cd(this.config.logDirectory),h=m.filter((A)=>A.startsWith(this.name)&&/\.log\.\d+$/.test(A)).sort((A,E)=>{let x=Number.parseInt(A.match(/\.log\.(\d+)$/)?.[1]||"0");return Number.parseInt(E.match(/\.log\.(\d+)$/)?.[1]||"0")-x}),I=h.length>0?Number.parseInt(h[0].match(/\.log\.(\d+)$/)?.[1]||"0")+1:1,R=`${r}.${I}`;if(await Oa(r).catch(()=>null))try{if(await yy(r,R),t.compress)try{let A=`${R}.gz`;await this.compressLogFile(R,A),await Td(R)}catch(A){console.error("Error compressing rotated file:",A)}if(h.length===0&&!m.some((A)=>A.endsWith(".log.1")))try{let A=`${r}.1`;await Gp(A,"")}catch(A){console.error("Error creating backup file:",A)}}catch(A){console.error(`Error during rotation: ${A instanceof Error?A.message:String(A)}`)}}else{let m=new Date().toISOString().replace(/[:.]/g,"-"),h=r.replace(/\.log$/,`-${m}.log`);if(await Oa(r).catch(()=>null))await yy(r,h)}if(this.currentLogFile=o,t.maxFiles){let h=(await Cd(this.config.logDirectory)).filter((I)=>I.startsWith(this.name)).sort((I,R)=>R.localeCompare(I));for(let I of h.slice(t.maxFiles))await Td($a(this.config.logDirectory,I))}}}async compressLogFile(e,t){let r=my(e),o=CE(t),m=vy();await OE(r,m,o)}async handleFingersCrossedBuffer(e,t){if(!this.fingersCrossedConfig)return;if(this.shouldActivateFingersCrossed(e)&&!this.isActivated){this.isActivated=!0;for(let r of this.logBuffer){let o=await this.formatter.format(r);if(this.shouldWriteToFile())await this.writeToFile(o);console.log(o)}if(this.fingersCrossedConfig.stopBuffering)this.logBuffer=[]}if(this.isActivated){if(this.shouldWriteToFile())await this.writeToFile(t);console.log(t)}}shouldLog(e){if(!this.enabled)return!1;let t={debug:0,info:1,success:2,warning:3,error:4};return t[e]>=t[this.config.level]}async flushPendingWrites(){if(await Promise.all(this.pendingOperations.map((e)=>{if(e instanceof Promise)return e.catch((t)=>{console.error("Error in pending write operation:",t)});return Promise.resolve()})),Ad(this.currentLogFile))try{let e=fy(this.currentLogFile,"r+");gy(e),Jp(e)}catch(e){console.error(`Error flushing file: ${e}`)}}async destroy(){if(this.rotationTimeout)clearInterval(this.rotationTimeout);if(this.keyRotationTimeout)clearInterval(this.keyRotationTimeout);this.timers.clear();for(let e of this.pendingOperations)if(typeof e.cancel==="function")e.cancel();return(async()=>{if(this.pendingOperations.length>0)try{await Promise.allSettled(this.pendingOperations)}catch(e){console.error("Error waiting for pending operations:",e)}if(!xr()&&this.config.rotation&&typeof this.config.rotation!=="boolean"&&this.config.rotation.compress)try{let t=(await Cd(this.config.logDirectory)).filter((r)=>(r.includes("temp")||r.includes(".tmp"))&&r.includes(this.name));for(let r of t)try{await Td($a(this.config.logDirectory,r))}catch(o){console.error(`Failed to delete temp file ${r}:`,o)}}catch(e){console.error("Error cleaning up temporary files:",e)}})()}getCurrentLogFilePath(){return this.currentLogFile}formatTag(e){if(!e)return"";return`${this.tagFormat.prefix}${e}${this.tagFormat.suffix}`}formatFileTimestamp(e){return`[${e.toISOString()}]`}formatConsoleTimestamp(e){return this.shouldStyleConsole()?Ht.gray(e.toLocaleTimeString()):e.toLocaleTimeString()}shouldStyleConsole(){if(!this.fancy||xr())return!1;let e=typeof Xt.env.NO_COLOR<"u",t=Xt.env.FORCE_COLOR==="0";if(e||t)return!1;return!!(typeof Xt.stderr<"u"&&Xt.stderr.isTTY||typeof Xt.stdout<"u"&&Xt.stdout.isTTY)}formatConsoleMessage(e){let{timestamp:t,icon:r="",tag:o="",message:m,level:h,showTimestamp:I=!0}=e,R=(z)=>z.replace(this.ANSI_PATTERN,"");if(!this.fancy){let z=[];if(I)z.push(t);if(h==="warning")z.push("WARN");else if(h==="error")z.push("ERROR");else if(r)z.push(r.replace(/[^\p{L}\p{N}\p{P}\p{Z}]/gu,""));if(o)z.push(o.replace(/[[\]]/g,""));return z.push(m),z.join(" ")}let A=Xt.stdout.columns||120,E="";if(h==="warning"||h==="error")E=`${r} ${m}`;else if(h==="info"||h==="success")E=`${r} ${o} ${m}`;else E=`${r} ${o} ${Ht.cyan(m)}`;if(!I)return E.trim();let x=R(E).trim().length,N=R(t).length,F=Math.max(1,A-2-x-N);return`${E.trim()}${" ".repeat(F)}${t}`}formatMessage(e,t){if(t.length===1&&Array.isArray(t[0]))return e.replace(/\{(\d+)\}/g,(h,I)=>{let R=Number.parseInt(I,10);return R<t[0].length?String(t[0][R]):h});let r=/%([sdijfo%])/g,o=0,m=e.replace(r,(h,I)=>{if(I==="%")return"%";if(o>=t.length)return h;let R=t[o++];switch(I){case"s":return String(R);case"d":case"i":return Number(R).toString();case"j":case"o":return JSON.stringify(R,null,2);default:return h}});if(o<t.length)m+=` ${t.slice(o).map((h)=>typeof h==="object"?JSON.stringify(h,null,2):String(h)).join(" ")}`;return m}formatMarkdown(e){if(!e)return e;let t=e;return t=t.replace(/\[([^\]]+)\]\(([^)]+)\)/g,(r,o,m)=>{let h=Ht.underline(Ht.blue(o)),I=this.toAbsoluteFilePath(m);if(I&&this.shouldStyleConsole()&&this.supportsHyperlinks()){let R=`file://${encodeURI(I)}`,A="\x1B]8;;",E="\x1B\\";return`\x1B]8;;${R}\x1B\\${h}\x1B]8;;\x1B\\`}if(this.shouldStyleConsole()&&this.supportsHyperlinks())return`\x1B]8;;${m}\x1B\\${h}\x1B]8;;\x1B\\`;return h}),t=t.replace(/`([^`]+)`/g,(r,o)=>Ht.bgGray(o)),t=t.replace(/\*\*([^*]+)\*\*/g,(r,o)=>Ht.bold(o)),t=t.replace(/(?<!\*)\*([^*]+)\*(?!\*)/g,(r,o)=>Ht.italic(o)),t=t.replace(/(?<!_)_([^_]+)_(?!_)/g,(r,o)=>Ht.italic(o)),t=t.replace(/~([^~]+)~/g,(r,o)=>Ht.strikethrough(o)),t}supportsHyperlinks(){if(xr())return!1;let e=Xt.env;if(!e)return!1;if(e.TERM_PROGRAM==="iTerm.app"||e.TERM_PROGRAM==="vscode"||e.TERM_PROGRAM==="WezTerm")return!0;if(e.WT_SESSION)return!0;if(e.TERM==="xterm-kitty")return!0;let t=e.VTE_VERSION?Number.parseInt(e.VTE_VERSION,10):0;if(!Number.isNaN(t)&&t>=5000)return!0;return!1}toAbsoluteFilePath(e){try{let t=e;if(t.startsWith("file://"))t=t.replace(/^file:\/\//,"");if(t.startsWith("~")){let r=Xt.env.HOME||"";if(r)t=t.replace(/^~(?=$|\/)/,r)}if(xE(t)||t.startsWith("./")||t.startsWith("../"))t=DE(t);else return null;return Ad(t)?t:null}catch{return null}}buildOutputTexts(e){let t=this.shouldStyleConsole()?this.formatMarkdown(e):e,r=e.replace(this.ANSI_PATTERN,"");return{consoleText:t,fileText:r}}async log(e,t,...r){if(!this.shouldLog(e))return;let o=new Date,m=this.formatConsoleTimestamp(o),h=this.formatFileTimestamp(o),I,R;if(t instanceof Error)I=t.message,R=t.stack;else I=this.formatMessage(t,r);let{consoleText:A,fileText:E}=this.buildOutputTexts(I);if(this.shouldStyleConsole()){let N=this.options.showIcons===!1?"":HR[e],F=this.options.showTags!==!1&&this.name?Ht.gray(this.formatTag(this.name)):"",z;switch(e){case"debug":z=this.formatConsoleMessage({timestamp:m,icon:N,tag:F,message:Ht.gray(A),level:e}),console.error(z);break;case"info":z=this.formatConsoleMessage({timestamp:m,icon:N,tag:F,message:A,level:e}),console.warn(z);break;case"success":z=this.formatConsoleMessage({timestamp:m,icon:N,tag:F,message:Ht.green(A),level:e}),console.error(z);break;case"warning":z=this.formatConsoleMessage({timestamp:m,icon:N,tag:F,message:A,level:e}),console.warn(z);break;case"error":if(z=this.formatConsoleMessage({timestamp:m,icon:N,tag:F,message:A,level:e}),console.error(z),R){let K=R.split(`
|
|
141
|
+
`);for(let J of K)if(J.trim()&&!J.includes(I))console.error(this.formatConsoleMessage({timestamp:m,message:Ht.gray(` ${J}`),level:e,showTimestamp:!1}))}break}}else if(!xr()){if(console.error(`${h} ${this.environment}.${e.toUpperCase()}: ${I}`),R)console.error(R)}let x=`${h} ${this.environment}.${e.toUpperCase()}: ${E}
|
|
142
|
+
`;if(R)x+=`${R}
|
|
143
|
+
`;if(x=x.replace(this.ANSI_PATTERN,""),this.shouldWriteToFile())await this.writeToFile(x)}progress(e,t=""){let r={update:(R,A)=>{},finish:(R)=>{},interrupt:(R,A)=>{}};if(!this.enabled)return r;let o=30;if(this.activeProgressBar={total:Math.max(1,e||1),current:0,message:t||"",barLength:o,lastRenderedLine:""},this.shouldStyleConsole()&&!xr()&&Xt.stdout.isTTY)this.renderProgressBar(this.activeProgressBar);return{update:(R,A)=>{if(!this.enabled||!this.activeProgressBar)return;if(this.activeProgressBar.current=Math.min(Math.max(0,R),this.activeProgressBar.total),A!==void 0)this.activeProgressBar.message=A;if(this.shouldStyleConsole()&&!xr()&&Xt.stdout.isTTY)this.renderProgressBar(this.activeProgressBar)},finish:(R)=>{if(!this.activeProgressBar)return;this.finishProgressBar(this.activeProgressBar,R)},interrupt:(R,A="info")=>{if(!xr()&&Xt.stdout.isTTY)Xt.stdout.write(`
|
|
144
|
+
`);if(this[A==="warning"?"warn":A](R),this.activeProgressBar&&this.shouldStyleConsole()&&!xr()&&Xt.stdout.isTTY)this.renderProgressBar(this.activeProgressBar)}}}time(e){let t=performance.now();if(this.shouldStyleConsole()){let r=this.options.showTags!==!1&&this.name?Ht.gray(this.formatTag(this.name)):"",o=this.formatConsoleTimestamp(new Date);console.error(this.formatConsoleMessage({timestamp:o,icon:this.options.showIcons===!1?"":Ht.blue("◐"),tag:r,message:`${Ht.cyan(e)}...`}))}return async(r)=>{if(!this.enabled)return;let o=performance.now(),m=Math.round(o-t),h=`${e} completed in ${m}ms`,I=new Date,R=this.formatConsoleTimestamp(I),E=`${this.formatFileTimestamp(I)} ${this.environment}.INFO: ${h}`;if(r)E+=` ${JSON.stringify(r)}`;if(E+=`
|
|
145
|
+
`,E=E.replace(this.ANSI_PATTERN,""),this.shouldStyleConsole()){let x=this.options.showTags!==!1&&this.name?Ht.gray(this.formatTag(this.name)):"";console.error(this.formatConsoleMessage({timestamp:R,icon:this.options.showIcons===!1?"":Ht.green("✓"),tag:x,message:`${h}${r?` ${JSON.stringify(r)}`:""}`}))}else if(!xr())console.error(E.trim());if(this.shouldWriteToFile())await this.writeToFile(E)}}async debug(e,...t){await this.log("debug",e,...t)}async info(e,...t){await this.log("info",e,...t)}async success(e,...t){await this.log("success",e,...t)}async warn(e,...t){await this.log("warning",e,...t)}async error(e,...t){await this.log("error",e,...t)}validateEncryptionConfig(){if(!this.config.rotation)return!1;if(typeof this.config.rotation==="boolean")return!1;let e=this.config.rotation,{encrypt:t}=e;return!!t}async only(e){if(!this.enabled)return;return await e()}isEnabled(){return this.enabled}setEnabled(e){this.enabled=e}extend(e){let t=`${this.name}:${e}`,r=new gu(t,{...this.options,logDirectory:this.config.logDirectory,level:this.config.level,format:this.config.format,rotation:typeof this.config.rotation==="boolean"?void 0:this.config.rotation,timestamp:typeof this.config.timestamp==="boolean"?void 0:this.config.timestamp});return this.subLoggers.add(r),r}createReadStream(){if(xr())throw Error("createReadStream is not supported in browser environments");if(!Ad(this.currentLogFile))throw Error(`Log file does not exist: ${this.currentLogFile}`);return my(this.currentLogFile,{encoding:"utf8"})}async decrypt(e){if(!this.validateEncryptionConfig())throw Error("Encryption is not configured");let t=this.config.rotation;if(!t.encrypt||typeof t.encrypt==="boolean")throw Error("Invalid encryption configuration");if(!this.currentKeyId||!this.keys.has(this.currentKeyId))throw Error("No valid encryption key available");let r=this.keys.get(this.currentKeyId);try{let o=zi.isBuffer(e)?e:zi.from(e,"base64"),m=o.subarray(0,16),h=o.subarray(o.length-16),I=o.subarray(16,o.length-16),R=AE("aes-256-gcm",r,m);R.setAuthTag(h);let A=R.update(I),E=R.final(),x=A.length+E.length,N=zi.allocUnsafe(x);return A.copy(N,0),E.copy(N,A.length),N.toString("utf8")}catch(o){throw Error(`Decryption failed: ${o instanceof Error?o.message:String(o)}`)}}getLevel(){return this.config.level}getLogDirectory(){return this.config.logDirectory}getFormat(){return this.config.format}getRotationConfig(){return this.config.rotation}isBrowserMode(){return xr()}isServerMode(){return!xr()}setTestEncryptionKey(e,t){this.currentKeyId=e,this.keys.set(e,t)}getTestCurrentKey(){if(!this.currentKeyId||!this.keys.has(this.currentKeyId))return null;return{id:this.currentKeyId,key:this.keys.get(this.currentKeyId)}}getConfig(){return this.config}async box(e){if(!this.enabled)return;let t=new Date,r=this.formatConsoleTimestamp(t),o=this.formatFileTimestamp(t),{consoleText:m,fileText:h}=this.buildOutputTexts(e);if(this.shouldStyleConsole()){let R=m.split(`
|
|
146
|
+
`),A=Math.max(...R.map((F)=>F.length))+2,E=`┌${"─".repeat(A)}┐`,x=`└${"─".repeat(A)}┘`,N=R.map((F)=>this.formatConsoleMessage({timestamp:r,message:Ht.cyan(F),showTimestamp:!1}));console.error(this.formatConsoleMessage({timestamp:r,message:Ht.cyan(E),showTimestamp:!1})),N.forEach((F)=>console.error(F)),console.error(this.formatConsoleMessage({timestamp:r,message:Ht.cyan(x),showTimestamp:!1}))}else if(!xr())console.error(`${o} ${this.environment}.INFO: [BOX] ${h}`);let I=`${o} ${this.environment}.INFO: [BOX] ${h}
|
|
147
|
+
`.replace(this.ANSI_PATTERN,"");if(this.shouldWriteToFile())await this.writeToFile(I)}async prompt(e){if(xr())return Promise.resolve(!0);return new Promise((t)=>{console.error(`${Ht.cyan("?")} ${e} (y/n) `);let r=(o)=>{let m=o.toString().trim().toLowerCase();Xt.stdin.removeListener("data",r);try{if(typeof Xt.stdin.setRawMode==="function")Xt.stdin.setRawMode(!1)}catch{}Xt.stdin.pause(),console.error(""),t(m==="y"||m==="yes")};try{if(typeof Xt.stdin.setRawMode==="function")Xt.stdin.setRawMode(!0)}catch{}Xt.stdin.resume(),Xt.stdin.once("data",r)})}setFancy(e){this.fancy=e}isFancy(){return this.fancy}pause(){this.enabled=!1}resume(){this.enabled=!0}async start(e,...t){if(!this.enabled)return;let r=e;if(t&&t.length>0){let A=/%([sdijfo%])/g,E=0;if(r=e.replace(A,(x,N)=>{if(N==="%")return"%";if(E>=t.length)return x;let F=t[E++];switch(N){case"s":return String(F);case"d":case"i":return Number(F).toString();case"j":case"o":return JSON.stringify(F,null,2);default:return x}}),E<t.length)r+=` ${t.slice(E).map((x)=>typeof x==="object"?JSON.stringify(x,null,2):String(x)).join(" ")}`}let{consoleText:o,fileText:m}=this.buildOutputTexts(r);if(this.shouldStyleConsole()){let A=this.options.showTags!==!1&&this.name?Ht.gray(this.formatTag(this.name)):"",E=this.options.showIcons===!1?"":`${Ht.blue("◐")} `;console.error(`${E}${A} ${Ht.cyan(o)}`)}let R=`[${new Date().toISOString()}] ${this.environment}.INFO: [START] ${m}
|
|
148
|
+
`.replace(this.ANSI_PATTERN,"");if(this.shouldWriteToFile())await this.writeToFile(R)}renderProgressBar(e,t=!1){if(!this.enabled||!this.shouldStyleConsole()||!Xt.stdout.isTTY)return;let r=Math.min(100,Math.max(0,Math.round(e.current/e.total*100))),o=Math.round(e.barLength*r/100),m=e.barLength-o,h=Ht.green("━".repeat(o)),I=Ht.gray("━".repeat(m)),R=`[${h}${I}]`,A=`${r}%`.padStart(4),E=e.message?` ${e.message}`:"",x=this.options.showIcons===!1?"":t||r===100?Ht.green("✓"):Ht.blue("▶"),N=this.options.showTags!==!1&&this.name?` ${Ht.gray(this.formatTag(this.name))}`:"",F=`\r${x}${N} ${R} ${A}${E}`,z=Xt.stdout.columns||80,K=" ".repeat(Math.max(0,z-F.replace(this.ANSI_PATTERN,"").length));if(e.lastRenderedLine=`${F}${K}`,Xt.stdout.write(e.lastRenderedLine),t)Xt.stdout.write(`
|
|
149
|
+
`)}finishProgressBar(e,t){if(!this.enabled||!this.fancy||xr()||!Xt.stdout.isTTY){this.activeProgressBar=null;return}if(e.current<e.total)e.current=e.total;if(t)e.message=t;this.renderProgressBar(e,!0),this.activeProgressBar=null}async clear(e={}){if(xr()){console.warn("Log clearing is not supported in browser environments.");return}try{console.warn("Clearing logs...",this.config.logDirectory);let t=await Cd(this.config.logDirectory),r=[];for(let o of t){if(!(e.name?new RegExp(e.name.replace("*",".*")).test(o):o.startsWith(this.name))||!o.endsWith(".log"))continue;let h=$a(this.config.logDirectory,o);if(e.before)try{if((await Oa(h)).mtime>=e.before)continue}catch(I){console.error(`Failed to get stats for file ${h}:`,I);continue}r.push(h)}if(r.length===0){console.warn("No log files matched the criteria for clearing.");return}console.warn(`Preparing to delete ${r.length} log file(s)...`);for(let o of r)try{await Td(o),console.warn(`Deleted log file: ${o}`)}catch(m){console.error(`Failed to delete log file ${o}:`,m)}console.warn("Log clearing process finished.")}catch(t){console.error("Error during log clearing process:",t)}}}var jN=new gu("stacks");class Gn extends Error{timestamp;context;constructor(e,t={}){super(e);if(this.name=this.constructor.name,this.timestamp=new Date,this.context=t,Error.captureStackTrace)Error.captureStackTrace(this,this.constructor)}toJSON(){return{name:this.name,code:this.code,message:this.message,timestamp:this.timestamp.toISOString(),context:this.context,stack:this.stack}}toString(){let e=Object.keys(this.context).length>0?` (${Object.entries(this.context).map(([t,r])=>`${t}: ${r}`).join(", ")})`:"";return`${this.name} [${this.code}]: ${this.message}${e}`}}class iv extends Gn{code="CONFIG_NOT_FOUND";constructor(e,t,r){let o=r===void 0?[]:Array.isArray(r)?r.filter(Boolean):[r],m="";if(o.length===1)m=` or alias "${o[0]}"`;else if(o.length>1)m=` or aliases ${o.map((h)=>`"${h}"`).join(", ")}`;super(`Configuration "${e}"${m} not found`,{configName:e,alias:r,searchPaths:t,searchPathCount:t.length})}}class du extends Gn{code="CONFIG_LOAD_ERROR";constructor(e,t,r){super(`Failed to load configuration from "${e}": ${t.message}`,{configPath:e,configName:r,originalError:t.name,originalMessage:t.message});this.cause=t}}class ov extends Gn{code="CONFIG_VALIDATION_ERROR";constructor(e,t,r){super(`Configuration validation failed for "${e}"`,{configPath:e,configName:r,validationErrors:t,errorCount:t.length})}}class sv extends Gn{code="CONFIG_MERGE_ERROR";constructor(e,t,r,o){super(`Failed to merge configuration from "${e}" with "${t}": ${r.message}`,{sourcePath:e,targetPath:t,configName:o,originalError:r.name,originalMessage:r.message});this.cause=r}}class Jm extends Gn{code="ENV_VAR_ERROR";constructor(e,t,r,o){super(`Failed to parse environment variable "${e}" with value "${t}" as ${r}`,{envKey:e,envValue:t,expectedType:r,configName:o})}}class av extends Gn{code="FILE_SYSTEM_ERROR";constructor(e,t,r){super(`File system ${e} failed for "${t}": ${r.message}`,{operation:e,path:t,originalError:r.name,originalMessage:r.message});this.cause=r}}class cv extends Gn{code="TYPE_GENERATION_ERROR";constructor(e,t,r){super(`Failed to generate types from "${e}" to "${t}": ${r.message}`,{configDir:e,outputPath:t,originalError:r.name,originalMessage:r.message});this.cause=r}}class lu extends Gn{code="SCHEMA_VALIDATION_ERROR";constructor(e,t,r){super(`Schema validation failed${r?` for config "${r}"`:""}`,{schemaPath:e,configName:r,validationErrors:t,errorCount:t.length})}}class dv extends Gn{code="BROWSER_CONFIG_ERROR";constructor(e,t,r,o){super(`Failed to fetch configuration from "${e}": ${t} ${r}`,{endpoint:e,status:t,statusText:r,configName:o})}}class lv extends Gn{code="PLUGIN_ERROR";constructor(e,t,r){super(`Plugin "${e}" failed during ${t}: ${r.message}`,{pluginName:e,operation:t,originalError:r.name,originalMessage:r.message});this.cause=r}}var js={configNotFound(e,t,r){return new iv(e,t,r)},configLoad(e,t,r){return new du(e,t,r)},configValidation(e,t,r){return new ov(e,t,r)},configMerge(e,t,r,o){return new sv(e,t,r,o)},envVar(e,t,r,o){return new Jm(e,t,r,o)},fileSystem(e,t,r){return new av(e,t,r)},typeGeneration(e,t,r){return new cv(e,t,r)},schemaValidation(e,t,r){return new lu(e,t,r)},browserConfig(e,t,r,o){return new dv(e,t,r,o)},plugin(e,t,r){return new lv(e,t,r)}};async function KR(e,t={}){let{maxRetries:r=3,retryDelay:o=1000,isRetryable:m=()=>!0,fallback:h}=t,I=Error("Unknown error occurred");for(let R=0;R<=r;R++)try{return await e()}catch(A){if(I=A instanceof Error?A:Error(String(A)),R===r||!m(I))break;if(o>0)await new Promise((E)=>setTimeout(E,o))}if(h!==void 0)return h;throw I instanceof Error?I:Error(`Unknown error: ${String(I)}`)}class Gm{defaultParsers;constructor(){this.defaultParsers=[{name:"boolean",canParse:(e,t)=>t==="boolean"||["true","false","1","0","yes","no"].includes(e.toLowerCase()),parse:(e)=>{let t=e.toLowerCase();return["true","1","yes"].includes(t)}},{name:"number",canParse:(e,t)=>t==="number"||!Number.isNaN(Number(e))&&!Number.isNaN(Number.parseFloat(e)),parse:(e)=>{let t=Number(e);if(Number.isNaN(t))throw TypeError(`Cannot parse "${e}" as number`);return t}},{name:"array",canParse:(e,t)=>t==="array"||e.startsWith("[")||e.includes(","),parse:(e)=>{try{let t=JSON.parse(e);if(Array.isArray(t))return t}catch{}return e.split(",").map((t)=>t.trim())}},{name:"json",canParse:(e,t)=>t==="object"||(e.startsWith("{")&&e.endsWith("}")||e.startsWith("[")&&e.endsWith("]")),parse:(e)=>{try{return JSON.parse(e)}catch(t){throw Error(`Cannot parse "${e}" as JSON: ${t}`)}}}]}async applyEnvironmentVariables(e,t,r={}){let{prefix:o,useCamelCase:m=!0,useBackwardCompatibility:h=!0,customParsers:I={},verbose:R=!1,trackPerformance:A=!0}=r,E=async()=>{if(!e)return{config:t,source:{type:"environment",priority:50,timestamp:new Date}};let x=o||this.generateEnvPrefix(e),N={...t};return this.processObject(N,[],x,{useCamelCase:m,useBackwardCompatibility:h,customParsers:I,verbose:R,configName:e}),{config:N,source:{type:"environment",priority:50,timestamp:new Date}}};if(A)return Fm.track("applyEnvironmentVariables",E,{configName:e});return E()}generateEnvPrefix(e){return e.toUpperCase().replace(/-/g,"_")}formatEnvKey(e,t){if(!t)return e.toUpperCase();return e.replace(/([A-Z])/g,"_$1").toUpperCase()}processObject(e,t,r,o){for(let[m,h]of Object.entries(e)){let I=[...t,m],R=I.map((x)=>this.formatEnvKey(x,o.useCamelCase)),A=`${r}_${R.join("_")}`,E=o.useBackwardCompatibility?`${r}_${I.map((x)=>x.toUpperCase()).join("_")}`:null;if(o.verbose);if(typeof h==="object"&&h!==null&&!Array.isArray(h))this.processObject(h,I,r,o);else{let x=zl.env[A]||(E?zl.env[E]:void 0);if(x!==void 0){if(o.verbose){let N=zl.env[A]?A:E}try{e[m]=this.parseEnvironmentValue(x,typeof h,A,o.customParsers,o.configName)}catch(N){if(N instanceof Jm)throw N;throw js.envVar(A,x,typeof h,o.configName)}}}}}parseEnvironmentValue(e,t,r,o,m){for(let[h,I]of Object.entries(o))try{return I(e)}catch{continue}for(let h of this.defaultParsers)if(h.canParse(e,t))try{return h.parse(e)}catch{throw js.envVar(r,e,`${t} (via ${h.name} parser)`,m)}return e}getEnvironmentVariables(e){let t={},r=e.toUpperCase();for(let[o,m]of Object.entries(zl.env))if(o.startsWith(r)&&m!==void 0)t[o]=m;return t}validateEnvironmentVariable(e,t,r){let o=[];if(!/^[A-Z_][A-Z0-9_]*$/.test(e))o.push(`Environment variable key "${e}" should only contain uppercase letters, numbers, and underscores`);if(r)try{this.parseEnvironmentValue(e,t,r,{})}catch(m){o.push(`Cannot parse value "${t}" as ${r}: ${m}`)}return{isValid:o.length===0,errors:o}}generateEnvVarDocs(e,t,r={}){let{prefix:o,format:m="text"}=r,h=o||this.generateEnvPrefix(e),I=[];switch(this.extractEnvVarInfo(t,[],h,I),m){case"markdown":return this.formatAsMarkdown(I,e);case"json":return JSON.stringify(I,null,2);default:return this.formatAsText(I,e)}}extractEnvVarInfo(e,t,r,o){for(let[m,h]of Object.entries(e)){let I=[...t,m],R=`${r}_${I.map((A)=>this.formatEnvKey(A,!0)).join("_")}`;if(typeof h==="object"&&h!==null&&!Array.isArray(h))this.extractEnvVarInfo(h,I,r,o);else o.push({key:R,type:Array.isArray(h)?"array":typeof h,description:`Configuration for ${I.join(".")}`,example:this.generateExample(h)})}}generateExample(e){if(Array.isArray(e))return JSON.stringify(e);if(typeof e==="object"&&e!==null)return JSON.stringify(e);return String(e)}formatAsText(e,t){let r=`Environment Variables for ${t}:
|
|
150
|
+
|
|
151
|
+
`;for(let o of e)r+=`${o.key}
|
|
152
|
+
`,r+=` Type: ${o.type}
|
|
153
|
+
`,r+=` Description: ${o.description}
|
|
154
|
+
`,r+=` Example: ${o.example}
|
|
155
|
+
|
|
156
|
+
`;return r}formatAsMarkdown(e,t){let r=`# Environment Variables for ${t}
|
|
157
|
+
|
|
158
|
+
`;r+=`| Variable | Type | Description | Example |
|
|
159
|
+
`,r+=`|----------|------|-------------|----------|
|
|
160
|
+
`;for(let o of e)r+=`| \`${o.key}\` | ${o.type} | ${o.description} | \`${o.example}\` |
|
|
161
|
+
`;return r}}function JR(e,t,r={}){return uv(e,t,r,new WeakMap)}function uv(e,t,r,o){let{arrayMergeMode:m="replace",skipNullish:h=!1,customMerger:I}=r;if(t===null||t===void 0)return h?e:t;if(I){let R=I(e,t);if(R!==void 0)return R}if(Array.isArray(t)||Array.isArray(e))return pv(e,t,m,o);if(!Ln(t)||!Ln(e))return t;return ZR(e,t,r,o)}function pv(e,t,r,o){if(Array.isArray(t)&&!Array.isArray(e))return t;if(Array.isArray(e)&&!Array.isArray(t))return t;if(Array.isArray(t)&&Array.isArray(e))switch(r){case"replace":return t;case"concat":return GR(e,t);case"smart":return QR(e,t,o);default:return t}return t}function GR(e,t){let r=[...t];for(let o of e)if(!r.some((m)=>Lm(m,o)))r.push(o);return r}function QR(e,t,r){if(t.length===0)return e;if(e.length===0)return t;if(Ln(t[0])&&Ln(e[0]))return YR(e,t,r);if(t.every((o)=>typeof o==="string")&&e.every((o)=>typeof o==="string")){let o=[...t];for(let m of e)if(!o.includes(m))o.push(m);return o}return t}function YR(e,t,r){let o=[...t];for(let m of e){if(!Ln(m)){o.push(m);continue}let h=["id","name","key","path","type"],I=!1;for(let R of h)if(R in m){if(o.find((E)=>Ln(E)&&(R in E)&&E[R]===m[R])){I=!0;break}}if(!I)o.push(m)}return o}function ZR(e,t,r,o){let m=t;if(Ln(m)&&o.has(m))return o.get(m);let h={...e};if(Ln(m))o.set(m,h);for(let I in m){if(!Object.prototype.hasOwnProperty.call(m,I))continue;let R=m[I],A=h[I];if(r.skipNullish&&(R===null||R===void 0))continue;if(R===null||R===void 0){h[I]=R;continue}if(Ln(R)&&Ln(A))h[I]=uv(A,R,r,o);else if(Array.isArray(R)||Array.isArray(A))h[I]=pv(A,R,r.arrayMergeMode||"smart",o);else h[I]=R}return h}function mv(e,t,r="replace"){return JR(e,t,{arrayMergeMode:r==="replace"?"replace":"smart",skipNullish:!0})}function Lm(e,t){if(e===t)return!0;if(Array.isArray(e)&&Array.isArray(t)){if(e.length!==t.length)return!1;for(let r=0;r<e.length;r++)if(!Lm(e[r],t[r]))return!1;return!0}if(Ln(e)&&Ln(t)){let r=Object.keys(e),o=Object.keys(t);if(r.length!==o.length)return!1;for(let m of r){if(!Object.prototype.hasOwnProperty.call(t,m))return!1;if(!Lm(e[m],t[m]))return!1}return!0}return!1}function Ln(e){return Boolean(e&&typeof e==="object"&&!Array.isArray(e))}class gv{extensions=Ny;async loadFromPath(e,t,r={}){let{arrayStrategy:o="replace",useCache:m=!0,cacheTtl:h,trackPerformance:I=!0,verbose:R=!1}=r;if(m){let E=Gl.getWithFileCheck("file",e);if(E){if(R)console.log(`Configuration loaded from cache: ${e}`);return E}}let A=async()=>{if(!ql(e))return null;try{let E=`?t=${Date.now()}`,x=await import(e+E),N=x.default||x,F="default"in x,z=Object.keys(x).length>0;if(!F&&!z)throw new du(e,Error("Configuration file is empty and exports nothing"),"unknown");if(typeof N!=="object"||N===null||Array.isArray(N))throw new du(e,Error("Configuration must export a valid object"),"unknown");let re={config:mv(t,N,o),source:{type:"file",path:e,priority:100,timestamp:new Date}};if(m)Gl.setWithFileCheck("file",re,e,h);return re}catch(E){throw E instanceof Error?js.configLoad(e,E):js.configLoad(e,Error(String(E)))}};if(I)return Fm.track("loadFromPath",A,{path:e});return A()}async tryLoadFromPaths(e,t,r={}){for(let o of e)try{let m=await this.loadFromPath(o,t,r);if(m)return m}catch(m){if(m instanceof Error&&m.name==="ConfigLoadError")throw m;if(r.verbose)console.warn(`Failed to load config from ${o}:`,m)}return null}generateConfigPaths(e,t,r){return HI({name:e,directory:t,alias:r})}generateNamePatterns(e,t){return jy({name:e,alias:t})}checkFileAccess(e){return KR(async()=>ql(e),{maxRetries:2,retryDelay:100,fallback:!1})}async discoverConfigFiles(e,t,r){let o=[];if(!ql(e))return o;if(t||r){let m=this.generateNamePatterns(t||"",r);for(let h of m)for(let I of this.extensions){let R=Dy(e,`${h}${I}`);if(await this.checkFileAccess(R))o.push(R)}}else try{let{readdirSync:m}=await import("fs"),h=m(e);for(let I of h)if(this.looksLikeConfigFile(I)){let R=Dy(e,I);if(await this.checkFileAccess(R))o.push(R)}}catch{return[]}return o}looksLikeConfigFile(e){return[/\.config\.(ts|js|mjs|cjs|json|mts|cts)$/,/^\..*\.(ts|js|mjs|cjs|json|mts|cts)$/,/config\.(ts|js|mjs|cjs|json|mts|cts)$/].some((r)=>r.test(e))}async validateConfigFile(e){let t=[];try{if(!ql(e))return t.push("Configuration file does not exist"),t;let r=await import(e),o=r.default||r;if(o===void 0)t.push("Configuration file must export a default value or named exports");else if(typeof o!=="object"||o===null)t.push("Configuration must be an object");else if(Array.isArray(o))t.push("Configuration cannot be an array at the root level");if(e.endsWith(".json"))try{let{readFileSync:m}=await import("fs"),h=m(e,"utf8");JSON.parse(h)}catch(m){t.push(`Invalid JSON syntax: ${m}`)}}catch(r){t.push(`Failed to load configuration file: ${r}`)}return t}async getFileModificationTime(e){try{let{statSync:t}=await import("fs");return t(e).mtime}catch{return null}}async preloadConfigurations(e,t={}){let r=new Map;return await Promise.allSettled(e.map(async(o)=>{try{let m=await this.loadFromPath(o,{},t);if(m)r.set(o,m.config)}catch(m){if(t.verbose)console.warn(`Failed to preload ${o}:`,m)}})),r}}var eA=/^https?:\/\//;class fv{async validateConfiguration(e,t,r={}){let{stopOnFirstError:o=!1,validateRequired:m=!0,validateTypes:h=!0,customRules:I=[],trackPerformance:R=!0,verbose:A=!1}=r,E=async()=>{let x=[],N=[],F={stopOnFirstError:o,validateRequired:m,validateTypes:h,customRules:I,trackPerformance:R,verbose:A};try{if(typeof t==="string")return await this.validateWithSchemaFile(e,t,F);else if(Array.isArray(t))return this.validateWithRules(e,[...t,...I],F);else return this.validateWithJSONSchema(e,t,F)}catch(z){return x.push({path:"",message:`Validation failed: ${z}`,rule:"system"}),{isValid:!1,errors:x,warnings:N}}};if(R)return await Fm.track("validateConfiguration",E);return E()}async validateWithSchemaFile(e,t,r){try{if(!XR(t))throw new lu(t,[{path:"",message:"Schema file does not exist"}]);let o=await import(t),m=o.default||o;if(Array.isArray(m))return this.validateWithRules(e,m,r);else return this.validateWithJSONSchema(e,m,r)}catch(o){throw new lu(t,[{path:"",message:`Failed to load schema: ${o}`}])}}validateWithJSONSchema(e,t,r){let o=[],m=[];return this.validateObjectAgainstSchema(e,t,"",o,m,r),{isValid:o.length===0,errors:o,warnings:m}}validateObjectAgainstSchema(e,t,r,o,m,h){if(h.validateTypes&&t.type){let I=Array.isArray(e)?"array":typeof e,R=Array.isArray(t.type)?t.type:[t.type];if(!R.includes(I)){if(o.push({path:r,message:`Expected type ${R.join(" or ")}, got ${I}`,expected:R.join(" or "),actual:I,rule:"type"}),h.stopOnFirstError)return}}if(t.enum&&!t.enum.includes(e)){if(o.push({path:r,message:`Value must be one of: ${t.enum.join(", ")}`,expected:t.enum.join(", "),actual:e,rule:"enum"}),h.stopOnFirstError)return}if(typeof e==="string"){if(t.minLength!==void 0&&e.length<t.minLength)o.push({path:r,message:`String length must be at least ${t.minLength}`,expected:`>= ${t.minLength}`,actual:e.length,rule:"minLength"});if(t.maxLength!==void 0&&e.length>t.maxLength)o.push({path:r,message:`String length must not exceed ${t.maxLength}`,expected:`<= ${t.maxLength}`,actual:e.length,rule:"maxLength"});if(t.pattern){if(!new RegExp(t.pattern).test(e))o.push({path:r,message:`String does not match pattern ${t.pattern}`,expected:t.pattern,actual:e,rule:"pattern"})}}if(typeof e==="number"){if(t.minimum!==void 0&&e<t.minimum)o.push({path:r,message:`Value must be at least ${t.minimum}`,expected:`>= ${t.minimum}`,actual:e,rule:"minimum"});if(t.maximum!==void 0&&e>t.maximum)o.push({path:r,message:`Value must not exceed ${t.maximum}`,expected:`<= ${t.maximum}`,actual:e,rule:"maximum"})}if(Array.isArray(e)&&t.items)for(let I=0;I<e.length;I++){let R=r?`${r}[${I}]`:`[${I}]`;if(this.validateObjectAgainstSchema(e[I],t.items,R,o,m,h),h.stopOnFirstError&&o.length>0)return}if(e&&typeof e==="object"&&!Array.isArray(e)){let I=e;if(h.validateRequired&&t.required){for(let R of t.required)if(!(R in I)){if(o.push({path:r?`${r}.${R}`:R,message:`Missing required property '${R}'`,expected:"required",rule:"required"}),h.stopOnFirstError)return}}if(t.properties){for(let[R,A]of Object.entries(t.properties))if(R in I){let E=r?`${r}.${R}`:R;if(this.validateObjectAgainstSchema(I[R],A,E,o,m,h),h.stopOnFirstError&&o.length>0)return}}if(t.additionalProperties===!1){let R=new Set(Object.keys(t.properties||{}));for(let A of Object.keys(I))if(!R.has(A))m.push({path:r?`${r}.${A}`:A,message:`Additional property '${A}' is not allowed`,rule:"additionalProperties"})}}}validateWithRules(e,t,r){let o=[],m=[];for(let h of t)try{let I=this.getValueByPath(e,h.path),R=this.validateWithRule(I,h,h.path);if(o.push(...R),r.stopOnFirstError&&o.length>0)break}catch(I){o.push({path:h.path,message:`Rule validation failed: ${I}`,rule:"system"})}return{isValid:o.length===0,errors:o,warnings:m}}validateWithRule(e,t,r){let o=[];if(t.required&&(e===void 0||e===null))return o.push({path:r,message:t.message||`Property '${r}' is required`,expected:"required",rule:"required"}),o;if(e===void 0||e===null)return o;if(t.type){let m=Array.isArray(e)?"array":typeof e;if(m!==t.type)o.push({path:r,message:t.message||`Expected type ${t.type}, got ${m}`,expected:t.type,actual:m,rule:"type"})}if(t.min!==void 0){let m=Array.isArray(e)?e.length:typeof e==="string"?e.length:typeof e==="number"?e:0;if(m<t.min)o.push({path:r,message:t.message||`Value must be at least ${t.min}`,expected:`>= ${t.min}`,actual:m,rule:"min"})}if(t.max!==void 0){let m=Array.isArray(e)?e.length:typeof e==="string"?e.length:typeof e==="number"?e:0;if(m>t.max)o.push({path:r,message:t.message||`Value must not exceed ${t.max}`,expected:`<= ${t.max}`,actual:m,rule:"max"})}if(t.pattern&&typeof e==="string"){if(!t.pattern.test(e))o.push({path:r,message:t.message||`Value does not match pattern ${t.pattern}`,expected:t.pattern.toString(),actual:e,rule:"pattern"})}if(t.enum&&!t.enum.includes(e))o.push({path:r,message:t.message||`Value must be one of: ${t.enum.join(", ")}`,expected:t.enum.join(", "),actual:e,rule:"enum"});if(t.validator){let m=t.validator(e);if(m)o.push({path:r,message:t.message||m,rule:"custom"})}return o}getValueByPath(e,t){if(!t)return e;let r=t.split("."),o=e;for(let m of r)if(o&&typeof o==="object"&&m in o)o=o[m];else return;return o}generateRulesFromInterface(e){let t=[],r=e.matchAll(/(\w+)(\?)?:\s*(\w+)/g);for(let o of r){let[,m,h,I]=o;t.push({path:m,required:!h,type:this.mapTypeScriptType(I)})}return t}mapTypeScriptType(e){switch(e.toLowerCase()){case"string":return"string";case"number":return"number";case"boolean":return"boolean";case"array":return"array";case"object":return"object";default:return"object"}}static createCommonRules(){return{server:[{path:"port",required:!0,type:"number",min:1,max:65535},{path:"host",required:!0,type:"string",min:1},{path:"ssl",type:"boolean"}],database:[{path:"url",required:!0,type:"string",min:1},{path:"pool",type:"number",min:1,max:100},{path:"timeout",type:"number",min:0}],api:[{path:"baseUrl",required:!0,type:"string",pattern:eA},{path:"timeout",type:"number",min:0},{path:"retries",type:"number",min:0,max:10}]}}}var Hn=new gu("bunfig",{showTags:!0});function tA(e){if(!e)return"";let t=Array.isArray(e)?e.filter(Boolean):[e];if(t.length===0)return"";if(t.length===1)return` or alias "${t[0]}"`;return` or aliases ${t.map((r)=>`"${r}"`).join(", ")}`}class hv{fileLoader=new gv;envProcessor=new Gm;validator=new fv;async loadConfig(e){let t=Date.now(),{cache:r,performance:o,schema:m,validate:h,...I}=e;try{if(r?.enabled){let A=this.checkCache(I.name||"",I);if(A)return A}let R;try{R=await this.loadConfigurationStrategies(I,!0,r)}catch(A){if(I.configFile)throw A;let E=I.__strictErrorHandling;if(A instanceof Error&&A.name==="ConfigNotFoundError"){if(E)throw A;R={...await this.applyEnvironmentVariables(I.name||"",I.defaultConfig,I.checkEnv!==!1,I.verbose||!1),warnings:[`No configuration file found for "${I.name||"config"}", using defaults with environment variables`]}}else if(A instanceof Error&&A.name==="ConfigLoadError"){let x=A.message.includes("EACCES")||A.message.includes("EPERM")||A.message.includes("permission denied"),N=!x&&(A.message.includes("syntax")||A.message.includes("Expected")||A.message.includes("Unexpected")||A.message.includes("BuildMessage")||A.message.includes("errors building")),F=A.message.includes("Configuration must export a valid object")||A.message.includes("Configuration file is empty and exports nothing");if(E&&(F||x))throw A;if(N&&(!E||!F))R={...await this.applyEnvironmentVariables(I.name||"",I.defaultConfig,I.checkEnv!==!1,I.verbose||!1),warnings:["Configuration file has syntax errors, using defaults with environment variables"]};else if(!E)R={...await this.applyEnvironmentVariables(I.name||"",I.defaultConfig,I.checkEnv!==!1,I.verbose||!1),warnings:[`Configuration loading error, using defaults: ${A.message}`]};else throw A}else R={...await this.applyEnvironmentVariables(I.name||"",I.defaultConfig,I.checkEnv!==!1,I.verbose||!1),warnings:[`Configuration loading failed, using defaults: ${A instanceof Error?A.message:String(A)}`]}}if(m||h)await this.validateConfiguration(R.config,m,h,I.name);if(r?.enabled&&R)this.cacheResult(I.name||"",R,r,I);if(o?.enabled){let A={operation:"loadConfig",duration:Date.now()-t,configName:I.name,timestamp:new Date};if(o.onMetrics)o.onMetrics(A);if(o.slowThreshold&&A.duration>o.slowThreshold)Hn.warn(`Slow configuration loading detected: ${A.duration}ms for ${I.name}`);R.metrics=A}return R}catch(R){if(R instanceof Error&&R.name==="ConfigNotFoundError")throw R;let A=Date.now()-t;throw Hn.error(`Configuration loading failed after ${A}ms:`,[R instanceof Error?R:Error(String(R))]),R}}async loadConfigurationStrategies(e,t=!1,r){let{name:o="",alias:m,configFile:h,cwd:I,configDir:R,defaultConfig:A,checkEnv:E=!0,arrayStrategy:x="replace",verbose:N=!1}=e,F=I||uu.cwd(),z=[];if(h){let ye=di(F,h);z.push(ye);let ne=E?dc(o,A,N):A,fe=await this.fileLoader.tryLoadFromPaths([ye],ne,{arrayStrategy:x,verbose:N,cacheTtl:r?.ttl,useCache:!r?.ttl||r.ttl>100});if(!fe)throw js.configNotFound(o||h,z,m);if(N)Hn.success(`Configuration loaded from explicit path: ${ye}`);return this.finalizeResult(fe,z,E,o,N)}let K=await this.loadLocalConfiguration(o,m,F,R,A,x,N,E,r);if(K)return z.push(...this.getLocalSearchPaths(o,m,F,R)),this.finalizeResult(K,z,E,o,N);let J=await this.loadHomeConfiguration(o,m,A,x,N,E);if(J)return z.push(...this.getHomeSearchPaths(o,m)),this.finalizeResult(J,z,E,o,N);let re=await this.loadPackageJsonConfiguration(o,m,F,A,x,N,E);if(re)return z.push(di(F,"package.json")),this.finalizeResult(re,z,E,o,N);if(z.push(...this.getAllSearchPaths(o,m,F,R)),t)throw js.configNotFound(o,z,m);return{...await this.applyEnvironmentVariables(o,A,E,N),warnings:[`No configuration file found for "${o}"${tA(m)}, using defaults with environment variables`]}}async loadLocalConfiguration(e,t,r,o,m,h,I,R,A){let E=R?dc(e,m,I):m,x=this.getLocalDirectories(r,o);for(let N of x){if(I)Hn.info(`Searching for configuration in: ${N}`);let F=this.fileLoader.generateConfigPaths(e,N,t),z=await this.fileLoader.tryLoadFromPaths(F,E,{arrayStrategy:h,verbose:I,cacheTtl:A?.ttl,useCache:!A?.ttl||A.ttl>100});if(z){if(I)Hn.success(`Configuration loaded from: ${z.source.path}`);return z}}return null}async loadHomeConfiguration(e,t,r,o,m,h){if(!e)return null;let I=h?dc(e,r,m):r,R=[di(Ra(),".config",e),di(Ra(),".config"),Ra()];for(let A of R){if(m)Hn.info(`Checking home directory: ${A}`);let E=this.fileLoader.generateConfigPaths(e,A,t),x=await this.fileLoader.tryLoadFromPaths(E,I,{arrayStrategy:o,verbose:m});if(x){if(m)Hn.success(`Configuration loaded from home directory: ${x.source.path}`);return x}}return null}async loadPackageJsonConfiguration(e,t,r,o,m,h,I){let R=I?dc(e,o,h):o;try{let A=di(r,"package.json");if(!JI(A))return null;let E={};try{E=JSON.parse(GI(A,"utf8"))}catch(F){if(h)Hn.warn("Failed to parse package.json:",[F instanceof Error?F:Error(String(F))]);return null}let x=E[e],N=e;if(!x&&t){let F=Array.isArray(t)?t:[t];for(let z of F){if(!z)continue;if(E[z]){x=E[z],N=z;break}}}if(x&&typeof x==="object"&&!Array.isArray(x)){if(h)Hn.success(`Configuration loaded from package.json: ${N}`);return{config:mv(R,x,m),source:{type:"package.json",path:A,priority:30,timestamp:new Date}}}}catch(A){if(h)Hn.warn("Failed to load package.json:",[A instanceof Error?A:Error(String(A))])}return null}async applyEnvironmentVariables(e,t,r,o){if(!r||!e||typeof t!=="object"||t===null||Array.isArray(t))return{config:t,source:{type:"default",priority:10,timestamp:new Date}};return{config:dc(e,t,o),source:{type:"environment",priority:20,timestamp:new Date}}}async finalizeResult(e,t,r,o,m){return{config:e.config,source:e.source,path:e.source.path}}async validateConfiguration(e,t,r,o){let m=[];if(r){let h=r(e);if(h)m.push(...h)}if(t){let h=await this.validator.validateConfiguration(e,t);if(!h.isValid)m.push(...h.errors.map((I)=>I.path?`${I.path}: ${I.message}`:I.message))}if(m.length>0)throw js.configValidation(o||"unknown",m,o)}checkCache(e,t){let r=this.generateCacheKey(e,t);return Gl.get(r)||null}cacheResult(e,t,r,o){let m=this.generateCacheKey(e,o);Gl.set(m,t,void 0,r.ttl)}generateCacheKey(e,t){let r=[e];if(t.alias){let o=Array.isArray(t.alias)?t.alias.join(","):t.alias;r.push(`alias:${o}`)}if(t.cwd)r.push(`cwd:${t.cwd}`);if(t.configFile)r.push(`configFile:${t.configFile}`);if(t.configDir)r.push(`configDir:${t.configDir}`);if("checkEnv"in t)r.push(`checkEnv:${t.checkEnv}`);return r.join("|")}getLocalDirectories(e,t){return KI({cwd:e,configDir:t})}getAllSearchPaths(e,t,r,o){let m=[];return m.push(...this.getLocalSearchPaths(e,t,r,o)),m.push(...this.getHomeSearchPaths(e,t)),m.push(di(r,"package.json")),m}getLocalSearchPaths(e,t,r,o){let m=this.getLocalDirectories(r,o),h=[];for(let I of m)h.push(...this.fileLoader.generateConfigPaths(e,I,t));return h}getHomeSearchPaths(e,t){if(!e)return[];let r=[di(Ra(),".config",e),di(Ra(),".config"),Ra()],o=[];for(let m of r)o.push(...this.fileLoader.generateConfigPaths(e,m,t));return o}async loadConfigWithResult(e){return this.loadConfig(e)}}var km=new hv;async function yv(e){let t="defaultConfig"in e&&e.defaultConfig!==void 0?e.defaultConfig:{},r="cache"in e||"performance"in e||"schema"in e||"validate"in e;try{let o;if(r)o=await km.loadConfig(e);else o=await km.loadConfig({...e,defaultConfig:t,cache:{enabled:!0},performance:{enabled:!1}});return o?.config??t}catch(o){if(e.configFile)throw o;let m=o instanceof Error?o.name:"UnknownError",h=o instanceof Error?o.message:String(o);if(!(m==="ConfigNotFoundError"||m==="ConfigLoadError"||m==="ConfigValidationError"||h.includes("config"))&&e.verbose)Hn.warn("Unexpected error loading config, using defaults:",[o instanceof Error?o:Error(String(o))]);let R=r?{...e,defaultConfig:t}:{...e,defaultConfig:t,cache:{enabled:!0},performance:{enabled:!1}};if("checkEnv"in e?e.checkEnv!==!1:!0)return(await km.applyEnvironmentVariables(R.name||"",t,!0,R.verbose||!1))?.config??t;return t}}function dc(e,t,r=!1){let o=new Gm,m=e.toUpperCase().replace(/[^A-Z0-9]/g,"_");function h(I,R=[]){let A={...I};for(let[E,x]of Object.entries(I)){let N=[...R,E],F=[`${m}_${N.join("_").toUpperCase()}`,`${m}_${N.map((J)=>J.toUpperCase()).join("")}`,`${m}_${N.map((J)=>J.replace(/([A-Z])/g,"_$1").toUpperCase()).join("")}`],z,K;for(let J of F)if(z=uu.env[J],z!==void 0){K=J;break}if(z!==void 0&&K)if(typeof x==="boolean")A[E]=["true","1","yes"].includes(z.toLowerCase());else if(typeof x==="number"){let J=Number(z);if(!Number.isNaN(J))A[E]=J}else if(Array.isArray(x))try{A[E]=JSON.parse(z)}catch{A[E]=z.split(",").map((J)=>J.trim())}else A[E]=z;else if(x&&typeof x==="object"&&!Array.isArray(x))A[E]=h(x,N)}return A}return h(t)}var zN=di(uu.cwd(),"config"),UN=di(uu.cwd(),"src/generated");var ao={project:{name:"my-project",slug:"my-project",region:"us-east-1"},environments:{production:{type:"production"}}},zs=null;function iA(e){return e.project?.slug===ao.project?.slug&&Object.keys(e.environments??{}).length===1&&!!e.environments?.production}async function oA(){let e=[vv(process.cwd(),"config","cloud.ts"),vv(process.cwd(),"config","cloud.js")];for(let t of e){if(!rA(t))continue;let r=await import(`${nA(t).href}?t=${Date.now()}`),o=r.tsCloud??r.default?.tsCloud??r.default??r.cloud??r.config;if(o)return o}return null}async function qg(){if(!zs){if(zs=await yv({name:"cloud",defaultConfig:ao}),iA(zs))zs=await oA()??zs;Wc(zs.stateDir)}return zs}var wd=qg,pA=ao;var ps=(e)=>e==null?void 0:String(e),Ym=(e)=>Number(e)===1,Sv=(e,t)=>{try{return e==null?t:JSON.parse(String(e))}catch{return t}};function Qm(e){return{id:String(e.id),organizationId:String(e.organization_id),projectId:String(e.project_id),scope:{type:String(e.scope_type),id:String(e.scope_id),environmentId:ps(e.environment_id),resourceId:ps(e.resource_id),previewId:ps(e.preview_id)},key:String(e.key),kind:String(e.kind),value:ps(e.value),valueFingerprint:String(e.value_fingerprint),secretRef:ps(e.secret_ref),backend:String(e.backend),backendVersion:ps(e.backend_version),origin:String(e.origin),required:Ym(e.required),metadata:Sv(e.metadata,{}),lastUsedAt:ps(e.last_used_at),rotatedAt:ps(e.rotated_at),version:Number(e.version),createdAt:String(e.created_at),updatedAt:String(e.updated_at)}}function bv(e){return{entryId:String(e.entry_id),resourceId:String(e.resource_id),injectionTarget:String(e.injection_target),required:Ym(e.required),requiresRedeploy:Ym(e.requires_redeploy),lastDeployedVersion:e.last_deployed_version==null?void 0:Number(e.last_deployed_version),createdAt:String(e.created_at),updatedAt:String(e.updated_at)}}class aa{controlPlane;now;constructor(e,t=()=>new Date){this.controlPlane=e;this.now=t}get(e){let t=this.controlPlane.database.query("SELECT * FROM configuration_entries WHERE id=?").get(e);return t?Qm(t):void 0}find(e,t,r){let o=this.controlPlane.database.query("SELECT * FROM configuration_entries WHERE project_id=? AND scope_type=? AND scope_id=? AND key=?").get(e,t.type,t.id,r);return o?Qm(o):void 0}list(e){let t=["project_id=?"],r=[e.projectId];if(e.scope)t.push("scope_type=?","scope_id=?"),r.push(e.scope.type,e.scope.id);if(e.kind)t.push("kind=?"),r.push(e.kind);return this.controlPlane.database.query(`SELECT * FROM configuration_entries WHERE ${t.join(" AND ")} ORDER BY key,scope_type,scope_id`).all(...r).map(Qm)}create(e){this.validateScope(e),this.validateEntry(e);let t=crypto.randomUUID(),r=this.now().toISOString();return this.controlPlane.database.run("INSERT INTO configuration_entries (id,organization_id,project_id,scope_type,scope_id,environment_id,resource_id,preview_id,key,kind,value,value_fingerprint,secret_ref,backend,backend_version,origin,required,metadata,last_used_at,rotated_at,version,created_at,updated_at) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",[t,e.organizationId,e.projectId,e.scope.type,e.scope.id,e.scope.environmentId??null,e.scope.resourceId??null,e.scope.previewId??null,e.key,e.kind,e.value??null,e.valueFingerprint,e.secretRef??null,e.backend,e.backendVersion??null,e.origin,e.required?1:0,JSON.stringify(e.metadata),e.lastUsedAt??null,e.rotatedAt??null,1,r,r]),this.get(t)}update(e,t,r){let o=this.get(e);if(!o)throw Error("Configuration entry was not found.");if(this.validateEntry({...o,...r}),this.controlPlane.database.run("UPDATE configuration_entries SET value=?,value_fingerprint=?,secret_ref=?,backend=?,backend_version=?,required=?,metadata=?,rotated_at=?,version=version+1,updated_at=? WHERE id=? AND version=?",[r.value??null,r.valueFingerprint,r.secretRef??null,r.backend,r.backendVersion??null,r.required?1:0,JSON.stringify(r.metadata),r.rotatedAt??null,this.now().toISOString(),e,t]).changes!==1)throw Error("Configuration entry changed; refresh and retry.");return this.get(e)}remove(e,t){let r=this.get(e);if(!r)throw Error("Configuration entry was not found.");if(r.version!==t)throw Error("Configuration entry changed; refresh and retry.");if(this.controlPlane.database.run("DELETE FROM configuration_entries WHERE id=? AND version=?",[e,t]).changes<1)throw Error("Configuration entry changed; refresh and retry.");return r}setDependency(e){let t=this.now().toISOString();return this.controlPlane.database.run("INSERT INTO configuration_dependencies (entry_id,resource_id,injection_target,required,requires_redeploy,last_deployed_version,created_at,updated_at) VALUES (?,?,?,?,?,?,?,?) ON CONFLICT(entry_id,resource_id) DO UPDATE SET injection_target=excluded.injection_target,required=excluded.required,requires_redeploy=excluded.requires_redeploy,last_deployed_version=excluded.last_deployed_version,updated_at=excluded.updated_at",[e.entryId,e.resourceId,e.injectionTarget,e.required?1:0,e.requiresRedeploy?1:0,e.lastDeployedVersion??null,t,t]),this.dependencies(e.entryId).find((r)=>r.resourceId===e.resourceId)}dependencies(e){return this.controlPlane.database.query("SELECT * FROM configuration_dependencies WHERE entry_id=? ORDER BY resource_id").all(e).map(bv)}dependenciesForResource(e){return this.controlPlane.database.query("SELECT * FROM configuration_dependencies WHERE resource_id=? ORDER BY entry_id").all(e).map(bv)}mutation(e){let t=this.controlPlane.database.query("SELECT request_hash,result FROM configuration_mutations WHERE idempotency_key=?").get(e);return t?{requestHash:t.request_hash,result:Sv(t.result,{})}:void 0}recordMutation(e){this.controlPlane.database.run("INSERT INTO configuration_mutations (id,project_id,idempotency_key,request_hash,result,actor_id,created_at) VALUES (?,?,?,?,?,?,?)",[crypto.randomUUID(),e.projectId,e.idempotencyKey,e.requestHash,JSON.stringify(e.result),e.actorId??null,this.now().toISOString()])}validateEntry(e){if(!/^[A-Za-z_][A-Za-z0-9_]{0,255}$/.test(e.key))throw Error(`Invalid configuration key: ${e.key}`);if(e.kind==="variable"&&(e.value==null||e.secretRef||e.backend!=="plaintext"))throw Error("Variables require a plaintext value and cannot use a secret backend.");if(e.kind==="secret"&&(e.value!=null||!e.secretRef||e.backend==="plaintext"))throw Error("Secrets require a non-plaintext backend reference and cannot store a plaintext value.")}validateScope(e){let t=this.controlPlane.getProject(e.projectId);if(!t||t.organizationId!==e.organizationId)throw Error("Configuration project does not belong to the organization.");let r=e.scope;if(r.type==="project"){if(r.id!==e.projectId||r.environmentId||r.resourceId||r.previewId)throw Error("Project configuration scope must reference only its project.");return}if(r.type==="environment"){let m=this.controlPlane.database.query("SELECT project_id FROM environments WHERE id=?").get(r.id);if(!m||m.project_id!==e.projectId||r.environmentId&&r.environmentId!==r.id||r.resourceId||r.previewId)throw Error("Environment configuration scope does not belong to the project.");return}if(r.type==="service"||r.type==="function"){let m=this.controlPlane.getResource(r.id);if(!m||m.projectId!==e.projectId||r.type==="function"&&m.kind!=="function"||r.resourceId&&r.resourceId!==r.id||r.environmentId&&m.environmentId!==r.environmentId||r.previewId)throw Error(`${r.type==="function"?"Function":"Service"} configuration scope does not belong to the project.`);return}let o=this.controlPlane.database.query("SELECT project_id,base_environment_id,resource_id FROM preview_instances WHERE id=?").get(r.id);if(!o||o.project_id!==e.projectId||r.previewId&&r.previewId!==r.id||r.environmentId&&r.environmentId!==o.base_environment_id||r.resourceId&&r.resourceId!==o.resource_id)throw Error("Preview configuration scope does not belong to the project.")}}var wv=/^[A-Za-z_][A-Za-z0-9_]{0,255}$/;function sA(e){let t={n:`
|
|
162
|
+
`,r:"\r",t:"\t","\\":"\\",'"':'"',$:"$"};return e.replace(/\\(n|r|t|\\|"|\$)/g,(r,o)=>t[o])}function $l(e){let t={},r=[],o=e.replace(/\r\n?/g,`
|
|
163
|
+
`).split(`
|
|
164
|
+
`);for(let m=0;m<o.length;m++){let h=m+1,I=o[m];if(!I.trim()||I.trimStart().startsWith("#"))continue;if(/^\s*export\s+/.test(I))I=I.replace(/^\s*export\s+/,"");let R=I.match(/^\s*([^=\s]+)\s*=\s*(.*)$/);if(!R){r.push({line:h,severity:"error",code:"invalid_line",message:"Expected KEY=VALUE."});continue}let A=R[1],E=R[2];if(!wv.test(A)){r.push({line:h,key:A,severity:"error",code:"invalid_key",message:`Invalid environment variable name: ${A}`});continue}let x=E;if(E.startsWith('"')||E.startsWith("'")){let N=E[0],F=[E.slice(1)],z=!1;while(!0){let K=F[F.length-1],J=!1,re=-1;for(let se=0;se<K.length;se++){if(N==='"'&&K[se]==="\\"&&!J){J=!0;continue}if(K[se]===N&&!J){re=se;break}J=!1}if(re>=0){F[F.length-1]=K.slice(0,re),z=!0;break}if(++m>=o.length)break;F.push(o[m])}if(!z){r.push({line:h,key:A,severity:"error",code:"unterminated_quote",message:`Unterminated quoted value for ${A}.`});continue}if(x=F.join(`
|
|
165
|
+
`),N==='"')x=sA(x)}else x=E.replace(/\s+#.*$/,"").trim();if(Object.hasOwn(t,A)){let N=t[A]!==x;if(r.push({line:h,key:A,severity:N?"error":"warning",code:N?"conflict":"duplicate",message:N?`${A} has conflicting values.`:`${A} is duplicated with the same value.`}),N)continue}t[A]=x}return{values:t,diagnostics:r,valid:!r.some((m)=>m.severity==="error")}}function Ml(e){return Object.entries(e).sort(([t],[r])=>t.localeCompare(r)).map(([t,r])=>{if(!wv.test(t))throw Error(`Invalid environment variable name: ${t}`);return`${t}=${JSON.stringify(r)}`}).join(`
|
|
166
|
+
`)+(Object.keys(e).length?`
|
|
167
|
+
`:"")}import{createCipheriv as aA,createDecipheriv as cA,createHash as kv,randomBytes as dA}from"node:crypto";class Xr{controlPlane;now;kind="local_encrypted";key;constructor(e,t,r=()=>new Date){this.controlPlane=e;this.now=r;if(!t)throw Error("Configuration encryption key is required.");this.key=kv("sha256").update(`ts-cloud:configuration:${t}`).digest()}encrypt(e){let t=dA(12),r=aA("aes-256-gcm",this.key,t),o=Buffer.concat([r.update(e,"utf8"),r.final()]);return`v1.${t.toString("base64url")}.${r.getAuthTag().toString("base64url")}.${o.toString("base64url")}`}decrypt(e){let[t,r,o,m]=e.split(".");if(t!=="v1"||!r||!o||!m)throw Error("Encrypted configuration secret is unavailable.");try{let h=cA("aes-256-gcm",this.key,Buffer.from(r,"base64url"));return h.setAuthTag(Buffer.from(o,"base64url")),Buffer.concat([h.update(Buffer.from(m,"base64url")),h.final()]).toString("utf8")}catch{throw Error("Encrypted configuration secret could not be decrypted.")}}async put(e){if(!e.value)throw Error("Configuration secrets cannot be empty.");let t=e.reference??`secret://configuration/${e.name}/${crypto.randomUUID()}`;if(!/^secret:\/\/configuration\/[A-Za-z0-9._/-]+$/.test(t))throw Error("Invalid local configuration secret reference.");let o=(this.controlPlane.database.query("SELECT version FROM configuration_secret_values WHERE reference=?").get(t)?.version??0)+1,m=this.now().toISOString();return this.controlPlane.database.run("INSERT INTO configuration_secret_values (reference,ciphertext,fingerprint,version,created_at,updated_at) VALUES (?,?,?,?,?,?) ON CONFLICT(reference) DO UPDATE SET ciphertext=excluded.ciphertext,fingerprint=excluded.fingerprint,version=excluded.version,updated_at=excluded.updated_at",[t,this.encrypt(e.value),kv("sha256").update(e.value).digest("hex"),o,m,m]),{reference:t,version:String(o)}}async resolve(e){let t=this.controlPlane.database.query("SELECT ciphertext FROM configuration_secret_values WHERE reference=?").get(e);if(!t)throw Error("Configuration secret was not found.");return this.decrypt(t.ciphertext)}async remove(e){this.controlPlane.database.run("DELETE FROM configuration_secret_values WHERE reference=?",[e])}async validate(e){return!!this.controlPlane.database.query("SELECT 1 AS found FROM configuration_secret_values WHERE reference=?").get(e)}}class ca{client;region;kind="aws_secrets_manager";constructor(e,t){this.client=e;this.region=t}async put(e){if(e.reference){let r=this.id(e.reference),o=await this.client.putSecretValue({SecretId:r,SecretString:e.value,ClientRequestToken:e.idempotencyKey});return{reference:e.reference,version:o.VersionId??e.idempotencyKey}}let t=await this.client.createSecret({Name:e.name,SecretString:e.value,ClientRequestToken:e.idempotencyKey,Tags:[{Key:"managed-by",Value:"ts-cloud"}]});return{reference:`aws-sm://${this.region}/${encodeURIComponent(t.ARN??t.Name??e.name)}`,version:t.VersionId??e.idempotencyKey}}async resolve(e){let t=await this.client.getSecretValue({SecretId:this.id(e)});if(t.SecretString==null)throw Error("AWS Secrets Manager value is unavailable.");return t.SecretString}async remove(e){await this.client.deleteSecret({SecretId:this.id(e),RecoveryWindowInDays:7})}async validate(e){try{return await this.client.describeSecret(this.id(e)),!0}catch{return!1}}id(e){let t=new URL(e);if(t.protocol!=="aws-sm:"||t.hostname!==this.region)throw Error("Invalid AWS Secrets Manager reference.");return decodeURIComponent(t.pathname.slice(1))}}class da{client;region;kind="aws_ssm";constructor(e,t){this.client=e;this.region=t}async put(e){let t=e.reference?this.name(e.reference):`/${e.name.replace(/^\/+/,"")}`,r=await this.client.putParameter({Name:t,Value:e.value,Type:"SecureString",Overwrite:!!e.reference,Description:"Managed by ts-cloud"});return{reference:`aws-ssm://${this.region}${t}`,version:String(r.Version??1)}}async resolve(e){let t=await this.client.getParameter({Name:this.name(e),WithDecryption:!0});if(t.Parameter?.Value==null)throw Error("AWS SSM parameter value is unavailable.");return t.Parameter.Value}async remove(e){await this.client.deleteParameter({Name:this.name(e)})}async validate(e){try{return!!(await this.client.getParameter({Name:this.name(e),WithDecryption:!1})).Parameter}catch{return!1}}name(e){let t=new URL(e);if(t.protocol!=="aws-ssm:"||t.hostname!==this.region||!t.pathname.startsWith("/"))throw Error("Invalid AWS SSM reference.");return t.pathname}}class la{kind="external";async put(){throw Error("External secret references cannot be written by ts-cloud.")}async resolve(){throw Error("External secrets must be resolved by the deployment provider.")}async remove(){}async validate(e){return/^(?:vault|doppler|op|external):\/\//.test(e)}}import{createHmac as lA}from"node:crypto";var uA=/^(?:secret|aws-sm|aws-ssm|vault|doppler|op|external):\/\//,kA=/^(?:AWS_|TS_CLOUD_|PORT$|HOST$)/;function Pn(e){return[...new Set(e)].sort()}function Iv(e,t={}){let r=e.kind==="secret";return{id:e.id,key:e.key,kind:e.kind,scope:e.scope,inherited:!!t.inherited,overridden:!!t.overridden,required:e.required,backend:!r||t.canReadSecretMetadata?e.backend:void 0,backendVersion:!r||t.canReadSecretMetadata?e.backendVersion:void 0,reference:r&&t.canReadSecretMetadata?e.secretRef:void 0,value:!r?e.value:void 0,lastUsedAt:e.lastUsedAt,rotatedAt:e.rotatedAt,version:e.version,updatedAt:e.updatedAt}}class ua{store;backends=new Map;fingerprintKey;now;constructor(e,t){if(this.store=e,this.fingerprintKey=t.fingerprintKey??t.encryptionKey,this.now=t.now??(()=>new Date),!this.fingerprintKey)throw Error("Configuration fingerprint key is required.");let r=t.backends??[new Xr(e.controlPlane,t.encryptionKey,this.now)];for(let o of r)this.backends.set(o.kind,o)}list(e){let t=e.search?.toLowerCase();return this.store.list(e).filter((r)=>!t||r.key.toLowerCase().includes(t)).map((r)=>Iv(r,{canReadSecretMetadata:e.canReadSecretMetadata}))}plan(e){let t=this.store.list({projectId:e.projectId,scope:e.scope}),r=new Map(t.map((A)=>[A.key,A])),o=[],m=[],h=[],I=[];for(let[A,E]of Object.entries(e.values)){o.push(...this.warnings(A,E));let x=r.get(A);if(!x)m.push(A);else if(x.valueFingerprint!==this.fingerprint(E))h.push(A);else I.push(A)}let R=e.removeMissing?t.filter((A)=>!Object.hasOwn(e.values,A.key)).map((A)=>A.key):[];return{added:Pn(m),changed:Pn(h),removed:Pn(R),unchanged:Pn(I),affectedResourceIds:this.affectedResources(e.projectId,e.scope,[...m,...h,...R]),warnings:o}}async set(e){let t=this.requestHash(e),r=e.idempotencyKey;if(r){let K=this.store.mutation(r);if(K){if(K.requestHash!==t)throw Error("Idempotency key was already used for a different configuration mutation.");let J=this.store.get(String(K.result.entryId??""));if(!J)throw Error("Idempotent configuration result is no longer available.");return{entry:J,mutation:K.result.mutation,warnings:[]}}}if(e.value!=null&&uA.test(e.value))throw Error("Secret references must use the reference field, not a plaintext value field.");let o=this.store.find(e.projectId,e.scope,e.key),m=this.fingerprint(e.value??e.reference??"");if(o&&e.expectedVersion!=null&&o.version!==e.expectedVersion)throw Error("Configuration entry changed; refresh and retry.");if(o&&o.kind!==e.kind)throw Error("Delete the existing entry before changing between variable and secret kinds.");let h=e.kind==="variable"?"plaintext":e.backend??(e.reference?"external":"local_encrypted");if(!!o&&(o.valueFingerprint!==m||o.backend!==h||o.required!==!!e.required)&&(this.isProduction(o)||this.store.dependencies(o.id).length>0)&&!e.confirmed)throw Error("Confirmation is required because this replacement affects production or dependent services.");let R=e.reference,A;if(e.kind==="secret"){let K=this.backend(h);if(e.reference&&e.value==null){if(!await K.validate(e.reference))throw Error("Secret backend reference is unavailable or invalid.");A=o?.backendVersion??"external"}else{if(e.value==null)throw Error("A secret value or external reference is required.");let J=await K.put({reference:o?.backend===h?o.secretRef:void 0,name:this.backendName(e),value:e.value,idempotencyKey:r??crypto.randomUUID()});R=J.reference,A=J.version}}let E=e.kind==="variable"?e.value:void 0;if(e.kind==="variable"&&E==null)throw Error("A variable value is required.");let x=!o||o.valueFingerprint!==m||o.backend!==h||o.secretRef!==R||o.backendVersion!==A||o.required!==!!e.required,N=o?x?this.store.update(o.id,o.version,{value:E,valueFingerprint:m,secretRef:R,backend:h,backendVersion:A,required:!!e.required,metadata:s(e.metadata??o.metadata),rotatedAt:e.kind==="secret"?this.now().toISOString():o.rotatedAt}):o:this.store.create({organizationId:e.organizationId,projectId:e.projectId,scope:e.scope,key:e.key,kind:e.kind,value:E,valueFingerprint:m,secretRef:R,backend:h,backendVersion:A,origin:e.origin??"managed",required:!!e.required,metadata:s(e.metadata??{}),rotatedAt:e.kind==="secret"?this.now().toISOString():void 0}),F=this.affectedResources(e.projectId,e.scope,[e.key]),z={added:o?[]:[e.key],changed:o&&x?[e.key]:[],removed:[],unchanged:o&&!x?[e.key]:[],affectedResourceIds:F,versions:{[e.key]:N.version}};if(r)this.store.recordMutation({projectId:e.projectId,idempotencyKey:r,requestHash:t,actorId:e.actorId,result:{entryId:N.id,mutation:z}});return this.audit(e.projectId,e.actorId,o&&x?"configuration.updated":o?"configuration.unchanged":"configuration.created",N,F),{entry:N,mutation:z,warnings:this.warnings(e.key,e.value??"")}}async importDotenv(e){let t=$l(e.source);if(!t.valid)return{document:t,warnings:[]};let r=new Set(e.secretKeys??[]),o={added:[],changed:[],removed:[],unchanged:[],affectedResourceIds:[],versions:{}},m=[];for(let[h,I]of Object.entries(t.values)){let R=await this.set({...e,source:void 0,key:h,value:I,kind:r.has(h)?"secret":"variable",idempotencyKey:e.idempotencyKey?`${e.idempotencyKey}:${h}`:void 0});o.added.push(...R.mutation.added),o.changed.push(...R.mutation.changed),o.unchanged.push(...R.mutation.unchanged),o.affectedResourceIds.push(...R.mutation.affectedResourceIds),Object.assign(o.versions,R.mutation.versions),m.push(...R.warnings)}return o.added=Pn(o.added),o.changed=Pn(o.changed),o.unchanged=Pn(o.unchanged),o.affectedResourceIds=Pn(o.affectedResourceIds),{document:t,mutation:o,warnings:m}}exportVariables(e){return Ml(Object.fromEntries(this.store.list({projectId:e.projectId,scope:e.scope,kind:"variable"}).map((t)=>[t.key,t.value??""])))}async transfer(e){let t=this.store.get(e.entryId);if(!t)throw Error("Configuration entry was not found.");if(t.scope.type===e.targetScope.type&&t.scope.id===e.targetScope.id)throw Error("Choose a different destination scope.");let r=t.kind==="variable"?t.value:t.backend==="external"?void 0:await this.backend(t.backend).resolve(t.secretRef),o=t.kind==="secret"?t.backend:void 0,m=await this.set({organizationId:t.organizationId,projectId:t.projectId,scope:e.targetScope,key:t.key,kind:t.kind,value:r,reference:t.backend==="external"?t.secretRef:void 0,backend:o,required:t.required,metadata:t.metadata,confirmed:e.confirmed,actorId:e.actorId,idempotencyKey:e.idempotencyKey?`${e.idempotencyKey}:copy`:void 0,origin:t.origin});if(e.mode==="move")await this.remove({entryId:t.id,expectedVersion:t.version,confirmed:e.confirmed,actorId:e.actorId});let h={added:m.mutation.added,changed:m.mutation.changed,removed:e.mode==="move"?[t.key]:[],unchanged:m.mutation.unchanged,affectedResourceIds:Pn([...m.mutation.affectedResourceIds,...this.affectedResources(t.projectId,t.scope,[t.key])]),versions:m.mutation.versions};return{entry:m.entry,mutation:h}}async resolve(e){let t=this.store.list({projectId:e.projectId}),r=t.filter((E)=>this.applies(E,e)).sort((E,x)=>this.rank(E.scope.type)-this.rank(x.scope.type)),o=new Map,m=new Set;for(let E of r){if(o.has(E.key))m.add(E.key);o.set(E.key,E)}let h={},I={},R=[],A=new Set(e.allowedPreviewSecrets??[]);for(let[E,x]of o){if(e.previewId&&x.kind==="secret"&&x.scope.type!=="preview"&&(!e.trustedPreview||!A.has(E)))continue;if(x.kind==="variable")h[E]=x.value??"";else if(e.nativeReferences)h[E]=x.secretRef;else{let N=this.backend(x.backend);if(!await N.validate(x.secretRef)){if(R.push({key:E,code:"stale_reference",message:`${E} points to an unavailable secret.`}),x.required)R.push({key:E,code:"missing_required",message:`${E} is required but unavailable.`});continue}h[E]=await N.resolve(x.secretRef)}I[E]=Iv(x,{inherited:x.scope.type!==(e.previewId?"preview":e.functionId?"function":e.resourceId?"service":e.environmentId?"environment":"project"),overridden:m.has(E),canReadSecretMetadata:e.canReadSecretMetadata}),this.store.controlPlane.database.run("UPDATE configuration_entries SET last_used_at=? WHERE id=?",[this.now().toISOString(),x.id])}return{values:h,entries:I,warnings:R,configurationHash:this.fingerprint([...o.values()].map((E)=>`${E.key}:${E.valueFingerprint}:${E.version}`).sort().join(`
|
|
168
|
+
`))}}async reveal(e){let t=this.store.get(e.entryId);if(!t||t.kind!=="secret")throw Error("Configuration secret was not found.");if(!e.canRevealSecrets)throw Error("Secret reveal permission is required.");if(!e.recentlyAuthenticated)throw Error("Recent authentication is required to reveal a secret.");let r=await this.backend(t.backend).resolve(t.secretRef);return this.audit(t.projectId,e.actorId,"configuration.secret.revealed",t,[]),r}async remove(e){let t=this.store.get(e.entryId);if(!t)throw Error("Configuration entry was not found.");let r=this.store.dependencies(t.id);if(((t.scope.environmentId?this.store.controlPlane.listEnvironments(t.projectId).some((I)=>I.id===t.scope.environmentId&&I.kind==="production"):t.scope.type==="project")||r.length)&&!e.confirmed)throw Error("Confirmation is required because this configuration affects production or dependent services.");if(t.kind==="secret"&&t.backend!=="external")await this.backend(t.backend).remove(t.secretRef);this.store.remove(t.id,e.expectedVersion);let m=Pn(r.map((I)=>I.resourceId)),h={added:[],changed:[],removed:[t.key],unchanged:[],affectedResourceIds:m,versions:{}};return this.audit(t.projectId,e.actorId,"configuration.deleted",t,m),h}applies(e,t){if(e.scope.type==="project")return!0;if(e.scope.type==="environment")return e.scope.id===t.environmentId;if(e.scope.type==="service")return e.scope.id===t.resourceId;if(e.scope.type==="function")return e.scope.id===t.functionId;return e.scope.id===t.previewId}rank(e){return{project:0,environment:1,service:2,function:3,preview:4}[e]}fingerprint(e){return`hmac-sha256:${lA("sha256",this.fingerprintKey).update(e).digest("hex")}`}requestHash(e){return this.fingerprint(JSON.stringify({...e,idempotencyKey:void 0,actorId:void 0}))}backend(e){let t=this.backends.get(e);if(!t)throw Error(`Configuration secret backend is not configured: ${e}`);return t}backendName(e){return`ts-cloud/${e.projectId}/${e.scope.type}/${e.scope.id}/${e.key}`.replace(/[^A-Za-z0-9/_+=.@-]/g,"-")}isProduction(e){return e.scope.type==="project"||!!e.scope.environmentId&&this.store.controlPlane.listEnvironments(e.projectId).some((t)=>t.id===e.scope.environmentId&&t.kind==="production")}warnings(e,t){let r=[];if(kA.test(e))r.push({key:e,code:"reserved",message:`${e} may be reserved by the runtime or provider.`});if(Buffer.byteLength(t)>65536)r.push({key:e,code:"limit",message:`${e} exceeds the portable 64 KiB value limit.`});return r}affectedResources(e,t,r){let o=this.store.list({projectId:e,scope:t}).filter((m)=>r.includes(m.key)).flatMap((m)=>this.store.dependencies(m.id).map((h)=>h.resourceId));if(t.type==="service"||t.type==="function")return Pn([...o,t.id]);if(t.type==="environment")return Pn([...o,...this.store.controlPlane.listResources(e,t.id).map((m)=>m.id)]);if(t.type==="preview"){let m=this.store.controlPlane.database.query("SELECT resource_id FROM preview_instances WHERE id=?").get(t.id);return Pn([...o,...m?[m.resource_id]:[]])}return Pn([...o,...this.store.controlPlane.listResources(e).map((m)=>m.id)])}audit(e,t,r,o,m){this.store.controlPlane.appendEvent({projectId:e,actorId:t,type:r,payload:{entryId:o.id,key:o.key,kind:o.kind,scopeType:o.scope.type,scopeId:o.scope.id,backend:o.backend,version:o.version,affectedResourceIds:m}})}}async function Fl(e,t,r,o){let m={added:0,changed:0,removed:0,overridden:0},h=async(I,R)=>{let A=e.store.list({projectId:t.project.id,scope:I,kind:"variable"}),E=new Set(Object.keys(R));for(let x of A.filter((N)=>N.origin==="config"&&!E.has(N.key)))await e.remove({entryId:x.id,expectedVersion:x.version,confirmed:!0,actorId:o}),m.removed++;for(let[x,N]of Object.entries(R)){let F=e.store.find(t.project.id,I,x);if(F?.origin!=="config"){if(F)m.overridden++;else await e.set({organizationId:t.organization.id,projectId:t.project.id,scope:I,key:x,kind:"variable",value:String(N),origin:"config",confirmed:!0,actorId:o}),m.added++;continue}if(F.value===String(N))continue;await e.set({organizationId:t.organization.id,projectId:t.project.id,scope:I,key:x,kind:"variable",value:String(N),origin:"config",confirmed:!0,actorId:o,expectedVersion:F.version}),m.changed++}};for(let[I,R]of Object.entries(r.environments??{})){let A=t.environments.get(I);if(!A)continue;let E=R,x={...E.variables??{},...E.app?.env??{}};await h({type:"environment",id:A.id,environmentId:A.id},x);for(let[N,F]of Object.entries(r.sites??{})){let z=t.store.listResources(t.project.id,A.id).find((K)=>K.kind==="application"&&K.slug===N);if(z)await h({type:"service",id:z.id,environmentId:A.id,resourceId:z.id},{...F.env??{}})}for(let[N,F]of Object.entries(r.infrastructure?.functions??{})){let z=t.store.listResources(t.project.id,A.id).find((K)=>K.kind==="function"&&K.slug===N);if(z)await h({type:"function",id:z.id,environmentId:A.id,resourceId:z.id},{...F.environment??{}})}}return m}var PA=async(e)=>{let t=Bun.spawn(e,{stdout:"pipe",stderr:"pipe"}),[r,o,m]=await Promise.all([t.exited,new Response(t.stdout).text(),new Response(t.stderr).text()]);return{code:r,stdout:o,stderr:m}};class ul{async scan(e,t){return En(e,t)}async exec(e,t){if(e.trustState!=="pinned")throw Error("SSH host key must be pinned before remote execution.");return PA(["ssh","-p",String(e.sshPort),"-o","BatchMode=yes","-o","StrictHostKeyChecking=yes","-o","ConnectTimeout=10",`${e.sshUser}@${e.endpoint}`,t])}}var _A=et,xA=`set -eu; printf '{'; printf '"os":"%s",' "$(. /etc/os-release 2>/dev/null; printf %s "\${PRETTY_NAME:-Linux}")"; printf '"arch":"%s",' "$(uname -m)"; printf '"cpuCores":%s,' "$(getconf _NPROCESSORS_ONLN)"; printf '"memoryBytes":%s,' "$(awk '/MemTotal/{print $2*1024}' /proc/meminfo)"; printf '"diskBytes":%s,' "$(df -B1 / | awk 'NR==2{print $2}')"; printf '"diskFreeBytes":%s,' "$(df -B1 / | awk 'NR==2{print $4}')"; printf '"dnsOk":'; getent hosts example.com >/dev/null && printf true || printf false; printf ',"timeSkewSeconds":0,"tools":{"curl":"'; command -v curl >/dev/null && printf installed; printf '","tar":"'; command -v tar >/dev/null && printf installed; printf '"},"privilege":"'; sudo -n true >/dev/null 2>&1 && printf sudo || printf user; printf '"}'`;class qr{provider;transport;discoverer;constructor(e,t=new ul,r){this.provider=e;this.transport=t;this.discoverer=r}discover(e){return this.discoverer?.(e)??Promise.resolve([])}async test(e){try{let t=await this.transport.scan(e.endpoint,e.sshPort);return{reachable:!0,hostKeyAlgorithm:t.algorithm,hostKeyFingerprint:_A(t.key),latencyMs:0}}catch{return{reachable:!1,hostKeyAlgorithm:"unknown",hostKeyFingerprint:"",latencyMs:0}}}async validate(e){let t=await this.transport.exec(e,xA);if(t.code!==0)throw Error(`Fleet validation failed: ${t.stderr.trim()||t.code}`);try{return JSON.parse(t.stdout)}catch{throw Error("Fleet validation returned malformed facts.")}}async bootstrap(e,t){let r=await this.transport.exec(e,`sudo -n ts-cloud-agent bootstrap --version 1 --steps ${t.length}`);if(r.code!==0)throw Error(`Fleet bootstrap failed: ${r.stderr.trim()||r.code}`);return{version:"1"}}capabilities(){return{document:{supported:!0,reason:"fleet-capabilities/v1; sha256:driver-contract"},validate:{supported:!0},bootstrap:{supported:!0},heartbeat:{supported:!0},drain:{supported:!0},archive:{supported:!0},terminate:{supported:!1,reason:"Inventory removal never terminates provider infrastructure."}}}}function pl(e,t){return{"fleet.bootstrap":async(r)=>{let o=r.operation.input&&typeof r.operation.input==="object"&&!Array.isArray(r.operation.input)?r.operation.input:{},m=e.get(String(o.serverId??""));if(!m)throw Error("Queued fleet server was not found.");let h=t.find((A)=>A.provider===m.provider);if(!h)throw Error(`No ${m.provider} fleet driver is configured.`);let I=Array.isArray(o.steps)?o.steps.map(String):[];if(r.checkpoint("preflight","Rechecking pinned trust and healthy validation."),m.trustState!=="pinned"||!m.validation?.valid)throw Error("Fleet bootstrap requires pinned trust and a healthy validation report.");r.checkpoint("bootstrap","Applying the reviewed idempotent bootstrap plan.");let R=await h.bootstrap(m,I);return e.update(m.id,{bootstrapVersion:R.version,status:"ready"}),r.checkpoint("verify","Bootstrap completed; heartbeat can now establish freshness."),{serverId:m.id,bootstrapVersion:R.version,steps:I.length}}}}var li=(e)=>e==null?void 0:String(e),Us=(e,t)=>{try{return JSON.parse(String(e))}catch{return t}},Zm=(e)=>({id:String(e.id),organizationId:String(e.organization_id),projectId:String(e.project_id),resourceId:String(e.resource_id),name:String(e.name),provider:String(e.provider),providerId:li(e.provider_id),region:li(e.region),zone:li(e.zone),endpoint:String(e.endpoint),sshUser:String(e.ssh_user),sshPort:Number(e.ssh_port),credentialRef:String(e.credential_ref),hostKeyAlgorithm:li(e.host_key_algorithm),hostKeyFingerprint:li(e.host_key_fingerprint),pendingHostKey:li(e.pending_host_key),roles:Us(e.roles,[]),labels:Us(e.labels,{}),taints:Us(e.taints,[]),capacity:Us(e.capacity,{}),usage:Us(e.usage,{}),capabilities:Us(e.capabilities,{}),status:String(e.status),trustState:String(e.trust_state),validation:Us(e.validation,void 0),bootstrapVersion:li(e.bootstrap_version),heartbeatAt:li(e.heartbeat_at),lastSeenAt:li(e.last_seen_at),archivedAt:li(e.archived_at),version:Number(e.version),createdAt:String(e.created_at),updatedAt:String(e.updated_at)});class Uo{control;now;id;constructor(e,t=()=>new Date,r=()=>crypto.randomUUID()){this.control=e;this.now=t;this.id=r}get(e){let t=this.control.database.query("SELECT * FROM fleet_servers WHERE id=?").get(e);return t?Zm(t):void 0}list(e,t=!1){return this.control.database.query("SELECT * FROM fleet_servers WHERE project_id=? ORDER BY name").all(e).map(Zm).filter((r)=>t||r.status!=="archived")}findProvider(e,t,r){let o=this.control.database.query("SELECT * FROM fleet_servers WHERE organization_id=? AND provider=? AND provider_id=? AND archived_at IS NULL").get(e,t,r);return o?Zm(o):void 0}create(e){let t=this.id(),r=this.now().toISOString();return this.control.database.run("INSERT INTO fleet_servers (id,organization_id,project_id,resource_id,name,provider,provider_id,region,zone,endpoint,ssh_user,ssh_port,credential_ref,host_key_algorithm,host_key_fingerprint,pending_host_key,roles,labels,taints,capacity,usage,capabilities,status,trust_state,validation,bootstrap_version,heartbeat_at,last_seen_at,archived_at,created_at,updated_at) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",[t,e.organizationId,e.projectId,e.resourceId,e.name,e.provider,e.providerId??null,e.region??null,e.zone??null,e.endpoint,e.sshUser,e.sshPort,e.credentialRef,e.hostKeyAlgorithm??null,e.hostKeyFingerprint??null,e.pendingHostKey??null,JSON.stringify(e.roles),JSON.stringify(e.labels),JSON.stringify(e.taints),JSON.stringify(e.capacity),JSON.stringify(e.usage),JSON.stringify(e.capabilities),e.status,e.trustState,JSON.stringify(e.validation??{}),e.bootstrapVersion??null,e.heartbeatAt??null,e.lastSeenAt??null,e.archivedAt??null,r,r]),this.get(t)}update(e,t){let r=this.get(e);if(!r)throw Error("Server was not found.");let o={...r,...t},m=this.now().toISOString();return this.control.database.run("UPDATE fleet_servers SET name=?,provider_id=?,region=?,zone=?,endpoint=?,ssh_user=?,ssh_port=?,credential_ref=?,host_key_algorithm=?,host_key_fingerprint=?,pending_host_key=?,roles=?,labels=?,taints=?,capacity=?,usage=?,capabilities=?,status=?,trust_state=?,validation=?,bootstrap_version=?,heartbeat_at=?,last_seen_at=?,archived_at=?,version=version+1,updated_at=? WHERE id=?",[o.name,o.providerId??null,o.region??null,o.zone??null,o.endpoint,o.sshUser,o.sshPort,o.credentialRef,o.hostKeyAlgorithm??null,o.hostKeyFingerprint??null,o.pendingHostKey??null,JSON.stringify(o.roles),JSON.stringify(o.labels),JSON.stringify(o.taints),JSON.stringify(o.capacity),JSON.stringify(o.usage),JSON.stringify(o.capabilities),o.status,o.trustState,JSON.stringify(o.validation??{}),o.bootstrapVersion??null,o.heartbeatAt??null,o.lastSeenAt??null,o.archivedAt??null,m,e]),this.get(e)}}var OA=/^(?:[A-Za-z0-9](?:[A-Za-z0-9.-]{0,251}[A-Za-z0-9])?|\[[0-9a-fA-F:]+\])$/;class Qo{store;drivers;now;queue;constructor(e,t,r,o=()=>new Date){this.store=e;this.drivers=t;this.now=o;this.queue=r??new c(e.control)}driver(e){let t=this.drivers.find((r)=>r.provider===e);if(!t)throw Error(`No ${e} fleet driver is configured.`);return t}enroll(e){if(!/^[a-z0-9][a-z0-9-]{1,62}$/.test(e.name))throw Error("Server name is invalid.");if(!OA.test(e.endpoint)||e.sshUser==="root"||!e.credentialRef.startsWith("secret://"))throw Error("Enrollment requires a valid endpoint, non-root SSH user, and secret credential reference.");let t=e.providerId?this.store.findProvider(e.organizationId,e.provider,e.providerId):void 0;if(t)return this.store.update(t.id,{endpoint:e.endpoint,region:e.region,zone:e.zone});let r=this.store.control.createResource({projectId:e.projectId,kind:"server",slug:e.name,name:e.name,provider:e.provider,providerId:e.providerId}),o=this.driver(e.provider);return this.store.create({...e,resourceId:r.id,sshPort:e.sshPort??22,labels:e.labels??{},taints:[],capacity:{},usage:{},capabilities:o.capabilities(),status:"pending",trustState:"unverified"})}async test(e){let t=this.store.get(e);if(!t)throw Error("Server was not found.");let r=await this.driver(t.provider).test(t);if(!r.reachable)return this.store.update(e,{status:"unreachable"});if(t.hostKeyFingerprint&&t.hostKeyFingerprint!==r.hostKeyFingerprint)return this.store.update(e,{status:"degraded",trustState:"rotation_pending",pendingHostKey:r.hostKeyFingerprint});return this.store.update(e,{hostKeyAlgorithm:r.hostKeyAlgorithm,hostKeyFingerprint:r.hostKeyFingerprint,pendingHostKey:void 0,trustState:"pinned",status:"validating",lastSeenAt:this.now().toISOString()})}reviewHostKey(e,t){let r=this.store.get(e);if(!r||r.pendingHostKey!==t)throw Error("Pending host key does not match.");return this.store.update(e,{hostKeyFingerprint:t,pendingHostKey:void 0,trustState:"pinned",status:"validating"})}async validate(e){let t=this.store.get(e);if(!t||t.trustState!=="pinned")throw Error("Pin the SSH host key before validation.");let r=await this.driver(t.provider).validate(t),o=[];if(!["x64","arm64","x86_64","aarch64"].includes(r.arch??""))o.push({code:"arch.unsupported",severity:"error",message:"Unsupported architecture.",remediation:"Use x86_64 or arm64 Linux."});for(let I of["curl","tar"])if(!r.tools?.[I])o.push({code:`tool.${I}`,severity:"error",message:`${I} is missing.`,remediation:`Install ${I} before bootstrap.`});if(Math.abs(r.timeSkewSeconds??0)>60)o.push({code:"time.skew",severity:"error",message:"Clock skew exceeds 60 seconds.",remediation:"Enable NTP synchronization."});if(!r.dnsOk)o.push({code:"dns.failed",severity:"error",message:"DNS validation failed.",remediation:"Repair resolver configuration."});let m={valid:!o.some((I)=>I.severity==="error"),facts:r,findings:o,validatedAt:this.now().toISOString(),validatorVersion:"1"},h={cpu:r.cpuCores??0,memoryBytes:r.memoryBytes??0,diskBytes:r.diskBytes??0};return this.store.update(e,{validation:m,capacity:h,status:m.valid?"ready":"degraded"})}bootstrap(e,t=!1){let r=this.store.get(e);if(!r?.validation?.valid)throw Error("A healthy validation report is required.");let o=["install runtime dependencies","install ts-cloud agent","configure heartbeat","verify least-privilege access"];if(!t)return{preview:!0,steps:o};let m=this.queue.enqueue({projectId:r.projectId,resourceId:r.resourceId,kind:"fleet.bootstrap",input:{serverId:e,steps:o},lockKey:`server:${e}`,providerKey:r.provider}).operation;return{preview:!1,steps:o,operation:m}}heartbeat(e,t){let r=this.store.get(e);if(!r)throw Error("Server was not found.");let o=this.now().toISOString();return this.store.update(e,{usage:t.usage,capabilities:t.capabilities??r.capabilities,heartbeatAt:o,lastSeenAt:o,status:r.status==="draining"||r.status==="drained"?r.status:"ready"})}drain(e,t=!1){return this.store.update(e,{status:t?"drained":"draining"})}uncordon(e){return this.store.update(e,{status:"ready"})}archive(e,t){let r=this.store.get(e);if(!r||t!==r.name)throw Error("Exact server name confirmation is required.");return this.store.update(e,{status:"archived",archivedAt:this.now().toISOString()})}}import{createHash as Xm,createPublicKey as Av,verify as $A}from"node:crypto";function Vr(e){if(e===null||typeof e!=="object")return JSON.stringify(e);if(Array.isArray(e))return`[${e.map(Vr).join(",")}]`;return`{${Object.keys(e).sort().map((t)=>`${JSON.stringify(t)}:${Vr(e[t])}`).join(",")}}`}var hl=(e)=>Xm("sha256").update(Vr(e)).digest("hex"),Bi=(e)=>Xm("sha256").update(Vr(e)).digest("hex"),fl=(e)=>Xm("sha256").update(Av(e).export({type:"spki",format:"der"})).digest("hex");function Al(e,t,r){try{return $A(null,Buffer.from(Vr(e)),Av(r),Buffer.from(t,"base64"))}catch{return!1}}var Ev=(e)=>e.replace(/^v/,"").split(/[.-]/).slice(0,3).map((t)=>Number(t)||0),Rv=(e,t)=>{let r=Ev(e),o=Ev(t);for(let m=0;m<3;m++){let h=(r[m]??0)-(o[m]??0);if(h)return h}return 0};function yl(e,t){let r=[];if(t.schemaVersion<e.minimumSchema)r.push(`schema ${t.schemaVersion} is below ${e.minimumSchema}`);if(t.schemaVersion>e.maximumSchema)r.push(`schema ${t.schemaVersion} is above ${e.maximumSchema}`);if(e.minimumVersion&&Rv(t.currentVersion,e.minimumVersion)<0)r.push(`platform ${t.currentVersion} is below ${e.minimumVersion}`);if(Rv(e.version,t.currentVersion)<=0)r.push(`version ${e.version} is not newer than ${t.currentVersion}`);if(!e.artifacts.some((o)=>o.platform===t.platform&&o.architecture===t.architecture))r.push(`no artifact for ${t.platform}/${t.architecture}`);return{compatible:r.length===0,reasons:r}}var Gi=(e,t)=>{try{return JSON.parse(String(e))}catch{return t}},dn=(e)=>e==null?void 0:String(e),NA=(e)=>({id:String(e.id),organizationId:String(e.organization_id),name:String(e.name),algorithm:"ed25519",publicKeyPem:String(e.public_key_pem),fingerprint:String(e.fingerprint),revokedAt:dn(e.revoked_at),createdAt:String(e.created_at)}),Cv=(e)=>({id:String(e.id),organizationId:String(e.organization_id),version:String(e.version),channel:String(e.channel),publishedAt:String(e.published_at),keyId:String(e.key_id),digest:String(e.digest),document:Gi(e.document,{}),signature:String(e.signature),verificationStatus:String(e.verification_status),compatibility:Gi(e.compatibility,{compatible:!1,reasons:[]}),createdAt:String(e.created_at)}),Pv=(e)=>({id:String(e.id),projectId:String(e.project_id),name:String(e.name),schedule:String(e.schedule),timezone:String(e.timezone),durationMinutes:Number(e.duration_minutes),allowedOperations:Gi(e.allowed_operations,[]),requireApproval:Number(e.require_approval)===1,enabled:Number(e.enabled)===1,createdAt:String(e.created_at),updatedAt:String(e.updated_at)}),_v=(e)=>({id:String(e.id),projectId:String(e.project_id),manifestId:String(e.manifest_id),windowId:dn(e.window_id),fromVersion:String(e.from_version),strategy:String(e.strategy),batchSize:Number(e.batch_size),healthGate:Gi(e.health_gate,{}),status:String(e.status),currentStage:dn(e.current_stage),backupId:dn(e.backup_id),approvedBy:dn(e.approved_by),createdAt:String(e.created_at),updatedAt:String(e.updated_at)}),eg=(e)=>({id:String(e.id),campaignId:String(e.campaign_id),serverId:dn(e.server_id),target:String(e.target),cohort:Number(e.cohort),previousVersion:String(e.previous_version),status:String(e.status),evidence:Gi(e.evidence,{}),error:dn(e.error),startedAt:dn(e.started_at),finishedAt:dn(e.finished_at),createdAt:String(e.created_at),updatedAt:String(e.updated_at)}),Tv=(e)=>({id:String(e.id),projectId:String(e.project_id),kind:String(e.kind),criteria:Gi(e.criteria,{}),candidates:Gi(e.candidates,[]),candidateDigest:String(e.candidate_digest),estimatedBytes:Number(e.estimated_bytes),status:String(e.status),confirmation:dn(e.confirmation),expiresAt:String(e.expires_at),result:Gi(e.result,{}),createdAt:String(e.created_at),updatedAt:String(e.updated_at)}),xv=(e)=>({id:String(e.id),projectId:String(e.project_id),backupPolicyId:dn(e.backup_policy_id),recoveryPointId:dn(e.recovery_point_id),topologyId:dn(e.topology_id),scenario:String(e.scenario),isolatedTarget:String(e.isolated_target),expectedRpoMinutes:Number(e.expected_rpo_minutes),expectedRtoMinutes:Number(e.expected_rto_minutes),status:String(e.status),operationId:dn(e.operation_id),evidence:Gi(e.evidence,{}),measuredRpoMinutes:e.measured_rpo_minutes==null?void 0:Number(e.measured_rpo_minutes),measuredRtoMinutes:e.measured_rto_minutes==null?void 0:Number(e.measured_rto_minutes),cleanupVerified:Number(e.cleanup_verified)===1,error:dn(e.error),startedAt:dn(e.started_at),finishedAt:dn(e.finished_at),createdAt:String(e.created_at),updatedAt:String(e.updated_at)});class zo{control;now;id;constructor(e,t=()=>new Date,r=()=>crypto.randomUUID()){this.control=e;this.now=t;this.id=r}addTrustedKey(e){let t=this.id(),r=this.now().toISOString(),o=fl(e.publicKeyPem);return this.control.database.run("INSERT INTO platform_trusted_keys (id,organization_id,name,algorithm,public_key_pem,fingerprint,created_at) VALUES (?,?,?,'ed25519',?,?,?)",[t,e.organizationId,e.name,e.publicKeyPem,o,r]),this.key(t)}key(e){let t=this.control.database.query("SELECT * FROM platform_trusted_keys WHERE id=?").get(e);return t?NA(t):void 0}revokeKey(e){return this.control.database.run("UPDATE platform_trusted_keys SET revoked_at=? WHERE id=?",[this.now().toISOString(),e]),this.key(e)}importManifest(e){let t=this.key(e.keyId);if(!t||t.organizationId!==e.organizationId)throw Error("Trusted update key was not found.");if(t.revokedAt)throw Error("Update signing key is revoked.");if(!Al(e.document,e.signature,t.publicKeyPem))throw Error("Update manifest signature is invalid.");let r=yl(e.document,e.current),o=hl(e.document),m=this.id(),h=this.now().toISOString();return this.control.database.run("INSERT INTO platform_update_manifests (id,organization_id,version,channel,published_at,key_id,digest,document,signature,verification_status,compatibility,created_at) VALUES (?,?,?,?,?,?,?,?,?,'verified',?,?)",[m,e.organizationId,e.document.version,e.document.channel,new Date(e.document.publishedAt).toISOString(),e.keyId,o,JSON.stringify(e.document),e.signature,JSON.stringify(r),h]),this.manifest(m)}manifest(e){let t=this.control.database.query("SELECT * FROM platform_update_manifests WHERE id=?").get(e);return t?Cv(t):void 0}manifests(e){return this.control.database.query("SELECT * FROM platform_update_manifests WHERE organization_id=? ORDER BY published_at DESC").all(e).map(Cv)}createWindow(e){let t=this.id(),r=this.now().toISOString();return this.control.database.run("INSERT INTO maintenance_windows (id,project_id,name,schedule,timezone,duration_minutes,allowed_operations,require_approval,enabled,created_at,updated_at) VALUES (?,?,?,?,?,?,?,?,1,?,?)",[t,e.projectId,e.name,e.schedule,e.timezone,e.durationMinutes,JSON.stringify(e.allowedOperations),e.requireApproval===!1?0:1,r,r]),this.window(t)}window(e){let t=this.control.database.query("SELECT * FROM maintenance_windows WHERE id=?").get(e);return t?Pv(t):void 0}windows(e){return this.control.database.query("SELECT * FROM maintenance_windows WHERE project_id=? ORDER BY name").all(e).map(Pv)}createCampaign(e){return this.control.transaction(()=>{let t=this.manifest(e.manifestId);if(!t?.compatibility.compatible||t.verificationStatus!=="verified")throw Error("A verified compatible update manifest is required.");if(!e.backupId)throw Error("A verified pre-upgrade backup is required.");let r=this.id(),o=this.now().toISOString();return this.control.database.run("INSERT INTO platform_upgrade_campaigns (id,project_id,manifest_id,window_id,from_version,strategy,batch_size,health_gate,status,backup_id,approved_by,created_at,updated_at) VALUES (?,?,?,?,?,?,?,?, 'planned',?,?,?,?)",[r,e.projectId,e.manifestId,e.windowId??null,e.fromVersion,e.strategy,e.batchSize,JSON.stringify(e.healthGate),e.backupId,e.approvedBy??null,o,o]),e.targets.forEach((m,h)=>this.control.database.run("INSERT INTO platform_upgrade_targets (id,campaign_id,server_id,target,cohort,previous_version,status,created_at,updated_at) VALUES (?,?,?,?,?,?,'pending',?,?)",[this.id(),r,m.serverId??null,m.target,e.strategy==="canary"?h===0?0:1+Math.floor((h-1)/e.batchSize):Math.floor(h/e.batchSize),m.previousVersion,o,o])),this.campaign(r)})}campaign(e){let t=this.control.database.query("SELECT * FROM platform_upgrade_campaigns WHERE id=?").get(e);return t?_v(t):void 0}campaigns(e){return this.control.database.query("SELECT * FROM platform_upgrade_campaigns WHERE project_id=? ORDER BY created_at DESC").all(e).map(_v)}targets(e){return this.control.database.query("SELECT * FROM platform_upgrade_targets WHERE campaign_id=? ORDER BY cohort,target").all(e).map(eg)}updateCampaign(e,t){let r=this.campaign(e);if(!r)throw Error("Upgrade campaign was not found.");let o={...r,...t};return this.control.database.run("UPDATE platform_upgrade_campaigns SET status=?,current_stage=?,updated_at=? WHERE id=?",[o.status,o.currentStage??null,this.now().toISOString(),e]),this.campaign(e)}updateTarget(e,t){let r=this.control.database.query("SELECT * FROM platform_upgrade_targets WHERE id=?").get(e);if(!r)throw Error("Upgrade target was not found.");let o={...eg(r),...t};return this.control.database.run("UPDATE platform_upgrade_targets SET status=?,evidence=?,error=?,started_at=?,finished_at=?,updated_at=? WHERE id=?",[o.status,JSON.stringify(o.evidence),o.error??null,o.startedAt??null,o.finishedAt??null,this.now().toISOString(),e]),eg(this.control.database.query("SELECT * FROM platform_upgrade_targets WHERE id=?").get(e))}createCleanup(e){let t=[...e.candidates].sort((I,R)=>I.id.localeCompare(R.id)),r=Bi(t),o=this.id(),m=this.now().toISOString(),h=new Date(this.now().getTime()+(e.ttlMinutes??60)*60000).toISOString();return this.control.database.run("INSERT INTO cleanup_plans (id,project_id,kind,criteria,candidates,candidate_digest,estimated_bytes,status,expires_at,created_at,updated_at) VALUES (?,?,?,?,?,?,?,'preview',?,?,?)",[o,e.projectId,e.kind,JSON.stringify(e.criteria),JSON.stringify(t),r,t.reduce((I,R)=>I+R.bytes,0),h,m,m]),this.cleanup(o)}cleanup(e){let t=this.control.database.query("SELECT * FROM cleanup_plans WHERE id=?").get(e);return t?Tv(t):void 0}cleanups(e){return this.control.database.query("SELECT * FROM cleanup_plans WHERE project_id=? ORDER BY created_at DESC").all(e).map(Tv)}updateCleanup(e,t){let r=this.cleanup(e);if(!r)throw Error("Cleanup plan was not found.");let o={...r,...t};return this.control.database.run("UPDATE cleanup_plans SET status=?,confirmation=?,result=?,updated_at=? WHERE id=?",[o.status,o.confirmation??null,JSON.stringify(o.result),this.now().toISOString(),e]),this.cleanup(e)}createDrill(e){if(!/^isolated:\/\//.test(e.isolatedTarget))throw Error("DR drills require an isolated target.");let t=this.id(),r=this.now().toISOString();return this.control.database.run("INSERT INTO disaster_recovery_drills (id,project_id,backup_policy_id,recovery_point_id,topology_id,scenario,isolated_target,expected_rpo_minutes,expected_rto_minutes,status,created_at,updated_at) VALUES (?,?,?,?,?,?,?,?,?,'planned',?,?)",[t,e.projectId,e.backupPolicyId??null,e.recoveryPointId??null,e.topologyId??null,e.scenario,e.isolatedTarget,e.expectedRpoMinutes,e.expectedRtoMinutes,r,r]),this.drill(t)}drill(e){let t=this.control.database.query("SELECT * FROM disaster_recovery_drills WHERE id=?").get(e);return t?xv(t):void 0}drills(e){return this.control.database.query("SELECT * FROM disaster_recovery_drills WHERE project_id=? ORDER BY created_at DESC").all(e).map(xv)}updateDrill(e,t){let r=this.drill(e);if(!r)throw Error("DR drill was not found.");let o={...r,...t};return this.control.database.run("UPDATE disaster_recovery_drills SET status=?,operation_id=?,evidence=?,measured_rpo_minutes=?,measured_rto_minutes=?,cleanup_verified=?,error=?,started_at=?,finished_at=?,updated_at=? WHERE id=?",[o.status,o.operationId??null,JSON.stringify(o.evidence),o.measuredRpoMinutes??null,o.measuredRtoMinutes??null,o.cleanupVerified?1:0,o.error??null,o.startedAt??null,o.finishedAt??null,this.now().toISOString(),e]),this.drill(e)}}function gm(e,t=new Date){if(!e.enabled)return!1;let r=new Date(e.schedule);if(!Number.isNaN(r.getTime()))return t>=r&&t.getTime()<=r.getTime()+e.durationMinutes*60000;let o=/^(\d{2}):(\d{2})$/.exec(e.schedule);if(!o)return!1;let m=new Intl.DateTimeFormat("en-US",{timeZone:e.timezone,hour:"2-digit",minute:"2-digit",hourCycle:"h23"}).formatToParts(t),h=Number(m.find((R)=>R.type==="hour")?.value)*60+Number(m.find((R)=>R.type==="minute")?.value),I=Number(o[1])*60+Number(o[2]);return h>=I&&h<I+e.durationMinutes}class Ko{store;now;queue;constructor(e,t,r=()=>new Date){this.store=e;this.now=r;this.queue=t??new c(e.control)}enqueueCampaign(e,t={}){let r=this.store.campaign(e);if(!r)throw Error("Upgrade campaign was not found.");let o=r.windowId?this.store.window(r.windowId):void 0;if(o&&!o.allowedOperations.includes("platform.upgrade"))throw Error("Maintenance window does not permit platform upgrades.");if(o&&!t.overrideWindow&&!gm(o,this.now()))throw Error("Upgrade is outside its maintenance window.");if(o?.requireApproval&&!t.approvedBy&&!r.approvedBy)throw Error("Maintenance approval is required.");let m=this.queue.enqueue({projectId:r.projectId,actorId:t.approvedBy,kind:"maintenance.upgrade",input:{campaignId:e},lockKey:"platform-upgrade",providerKey:"platform",timeoutSeconds:7200,cancellationMode:"cooperative",resumePolicy:"fail"}).operation;return{campaign:this.store.updateCampaign(e,{status:"queued"}),operation:m}}async previewCleanup(e,t){let r=t.find((o)=>o.kind===e.kind);if(!r)throw Error(`No ${e.kind} cleanup driver is configured.`);return this.store.createCleanup({...e,candidates:await r.discover(e.criteria)})}enqueueCleanup(e,t){let r=this.store.cleanup(e);if(!r||r.status!=="preview")throw Error("A current cleanup preview is required.");if(new Date(r.expiresAt)<=this.now())throw Error("Cleanup preview expired; generate it again.");let o=`DELETE ${r.candidates.length} ${r.kind}`;if(t!==o)throw Error(`Exact confirmation ${o} is required.`);let m=this.queue.enqueue({projectId:r.projectId,kind:"maintenance.cleanup",input:{planId:e},lockKey:`cleanup:${r.kind}`,providerKey:"cleanup",timeoutSeconds:3600,cancellationMode:"cooperative"}).operation;return{plan:this.store.updateCleanup(e,{status:"approved",confirmation:t}),operation:m}}enqueueDrill(e){let t=this.store.drill(e);if(!t||t.status!=="planned")throw Error("A planned DR drill is required.");let r=this.queue.enqueue({projectId:t.projectId,kind:"maintenance.drill",input:{drillId:e},lockKey:`drill:${t.scenario}:${t.id}`,providerKey:"recovery",timeoutSeconds:7200,cancellationMode:"cooperative"}).operation;return{drill:this.store.updateDrill(e,{status:"queued",operationId:r.id}),operation:r}}}var tg=(e)=>e&&typeof e==="object"&&!Array.isArray(e)?e:{};function Il(e){return{"maintenance.upgrade":async(m)=>{let h=e.store.campaign(String(tg(m.operation.input).campaignId??""));if(!h||!e.platform)throw Error("Upgrade campaign or platform maintenance driver is unavailable.");let I=e.store.manifest(h.manifestId);if(!I?.compatibility.compatible||I.verificationStatus!=="verified")throw Error("Update manifest is no longer verified and compatible.");let R=I.document.artifacts.find((E)=>E.platform===process.platform&&E.architecture===process.arch)??I.document.artifacts[0];if(!R)throw Error("No update artifact matches this platform.");e.store.updateCampaign(h.id,{status:"running",currentStage:"preflight"});let A=[];try{for(let E of[...new Set(e.store.targets(h.id).map((x)=>x.cohort))].sort((x,N)=>x-N)){e.store.updateCampaign(h.id,{currentStage:`cohort:${E}`});for(let x of e.store.targets(h.id).filter((N)=>N.cohort===E)){m.throwIfCancellationRequested(),m.checkpoint(`download:${x.target}`,"Downloading signed platform artifact."),e.store.updateTarget(x.id,{status:"downloading",startedAt:new Date().toISOString()});let N=await e.platform.download({target:x,artifact:R,signal:m.signal});if(N.sha256!==R.sha256)throw Error(`Artifact checksum mismatch for ${x.target}.`);m.checkpoint(`install:${x.target}`,"Installing verified artifact."),e.store.updateTarget(x.id,{status:"installing"}),await e.platform.install({target:x,path:N.path,version:I.version,signal:m.signal}),A.push(x);let F=await e.platform.health({target:x,gate:h.healthGate});if(!F.healthy)throw Error(`Upgrade health gate failed for ${x.target}.`);e.store.updateTarget(x.id,{status:"healthy",evidence:F.evidence,finishedAt:new Date().toISOString()})}}return e.store.updateCampaign(h.id,{status:"succeeded",currentStage:void 0}),{campaignId:h.id,version:I.version,targets:A.length}}catch(E){e.store.updateCampaign(h.id,{status:"rolling_back",currentStage:"rollback"});for(let x of A.reverse())e.store.updateTarget(x.id,{status:"rolling_back"}),await e.platform.rollback({target:x,version:x.previousVersion,signal:m.signal}),e.store.updateTarget(x.id,{status:"rolled_back",finishedAt:new Date().toISOString()});throw e.store.updateCampaign(h.id,{status:"rolled_back",currentStage:void 0}),E}},"maintenance.cleanup":async(m)=>{let h=e.store.cleanup(String(tg(m.operation.input).planId??""));if(!h||h.status!=="approved")throw Error("Approved cleanup plan was not found.");if(Bi([...h.candidates].sort((x,N)=>x.id.localeCompare(N.id)))!==h.candidateDigest)throw Error("Cleanup candidate set changed after preview.");let I=e.cleanup?.find((x)=>x.kind===h.kind);if(!I)throw Error(`No ${h.kind} cleanup driver is configured.`);e.store.updateCleanup(h.id,{status:"running"});let R=[],A=[];for(let x of h.candidates){m.throwIfCancellationRequested();try{await I.remove(x),R.push(x.id)}catch(N){A.push({id:x.id,error:N instanceof Error?N.message:String(N)})}}let E=A.length?R.length?"partial":"failed":"succeeded";if(e.store.updateCleanup(h.id,{status:E,result:{removed:R,failed:A,reclaimedBytes:h.candidates.filter((x)=>R.includes(x.id)).reduce((x,N)=>x+N.bytes,0)}}),A.length)throw Error(`Cleanup failed for ${A.length} candidate(s).`);return{planId:h.id,removed:R.length,reclaimedBytes:h.estimatedBytes}},"maintenance.drill":async(m)=>{let h=e.store.drill(String(tg(m.operation.input).drillId??""));if(!h)throw Error("DR drill was not found.");let I=e.recovery?.find((N)=>N.scenario===h.scenario);if(!I)throw Error(`No ${h.scenario} recovery driver is configured.`);let R=new Date;e.store.updateDrill(h.id,{status:"restoring",startedAt:R.toISOString()});let A=h.isolatedTarget,E={},x={};try{m.checkpoint("restore","Restoring into isolated drill target.");let N=await I.restore({drill:h,signal:m.signal});if(!N.target.startsWith("isolated://"))throw Error("Recovery driver attempted a non-isolated target.");A=N.target,E=N.evidence;let F=(R.getTime()-new Date(N.recoveryPointAt).getTime())/60000;e.store.updateDrill(h.id,{status:"verifying",measuredRpoMinutes:F,evidence:{restore:E}}),m.checkpoint("verify","Running recovery health checks without production traffic.");let z=await I.verify({drill:h,target:A});x=z.evidence;let K=(Date.now()-R.getTime())/60000;if(!z.healthy||F>h.expectedRpoMinutes||K>h.expectedRtoMinutes)throw Error("DR drill failed its health, RPO, or RTO objective.");e.store.updateDrill(h.id,{measuredRpoMinutes:F,measuredRtoMinutes:K,evidence:{restore:E,verify:x}})}catch(N){throw e.store.updateDrill(h.id,{status:"failed",error:N instanceof Error?N.message:String(N),evidence:{restore:E,verify:x}}),N}finally{e.store.updateDrill(h.id,{status:"cleaning"});try{await I.cleanup({drill:h,target:A});let N=e.store.drill(h.id);e.store.updateDrill(h.id,{status:N.error?"failed":"passed",cleanupVerified:!0,finishedAt:new Date().toISOString()})}catch(N){e.store.updateDrill(h.id,{status:"cleanup_required",cleanupVerified:!1,error:N instanceof Error?N.message:String(N),finishedAt:new Date().toISOString()})}}return{drillId:h.id,status:e.store.drill(h.id).status}}}}class mm{transport;constructor(e){this.transport=e}download(e){return this.transport.download({target:e.target.target,url:e.artifact.url,signal:e.signal})}install(e){return this.transport.install({target:e.target.target,path:e.path,version:e.version,signal:e.signal})}health(e){return this.transport.health({target:e.target.target,gate:e.gate})}rollback(e){return this.transport.rollback({target:e.target.target,version:e.version,signal:e.signal})}}var Dv={releases:{select:"SELECT id,created_at,0 bytes FROM releases WHERE project_id=? AND pinned=0 AND status IN ('failed','rolled_back','superseded') AND created_at<?",remove:"DELETE FROM releases WHERE id=?"},artifacts:{select:"SELECT a.id,a.created_at,a.size bytes FROM release_artifacts a WHERE a.organization_id=? AND NOT EXISTS(SELECT 1 FROM releases r WHERE r.artifact_id=a.id) AND a.created_at<?",remove:"DELETE FROM release_artifacts WHERE id=?"},operations:{select:"SELECT id,created_at,0 bytes FROM operations WHERE project_id=? AND state IN ('succeeded','failed','cancelled','timed_out') AND updated_at<?",remove:"DELETE FROM operations WHERE id=?"},logs:{select:"SELECT l.id,l.created_at,length(l.message) bytes FROM operation_logs l JOIN operations o ON o.id=l.operation_id WHERE o.project_id=? AND o.state IN ('succeeded','failed','cancelled','timed_out') AND l.created_at<?",remove:"DELETE FROM operation_logs WHERE id=?"},previews:{select:"SELECT id,created_at,0 bytes FROM preview_instances WHERE project_id=? AND status IN ('destroyed','failed') AND updated_at<?",remove:"DELETE FROM preview_instances WHERE id=?"},snapshots:{select:"SELECT id,created_at,size_bytes bytes FROM recovery_points WHERE project_id=? AND pinned=0 AND held=0 AND status IN ('failed','deleted') AND created_at<?",remove:"DELETE FROM recovery_points WHERE id=?"},orphaned_resources:{select:"SELECT id,created_at,COALESCE(capacity_bytes,0) bytes FROM persistent_volumes WHERE project_id=? AND status='orphaned' AND orphaned_at<?",remove:"UPDATE persistent_volumes SET status='deleted',deleted_at=datetime('now'),updated_at=datetime('now') WHERE id=? AND status='orphaned'"}};class Wo{kind;control;projectId;organizationId;constructor(e,t,r,o){this.kind=e;this.control=t;this.projectId=r;this.organizationId=o}async discover(e){let t=Math.max(1,Math.min(3650,Number(e.olderThanDays??30))),r=new Date(Date.now()-t*86400000).toISOString(),o=Dv[this.kind],m=this.kind==="artifacts"?this.organizationId:this.projectId;return this.control.database.query(o.select).all(m,r).map((I)=>({id:String(I.id),kind:this.kind,label:`${this.kind}:${String(I.id)}`,bytes:Number(I.bytes??0),metadata:{createdAt:String(I.created_at)}}))}async remove(e){if(this.control.database.run(Dv[this.kind].remove,[e.id]).changes!==1)throw Error(`${e.label} changed or is no longer removable.`)}}class hm{scenario;transport;constructor(e,t){this.scenario=e;this.transport=t}restore(e){return this.transport.restore({scenario:this.scenario,...e})}verify(e){return this.transport.verify({scenario:this.scenario,...e})}cleanup(e){return this.transport.cleanup({scenario:this.scenario,...e})}}var Qn=(e,t)=>{try{return JSON.parse(String(e))}catch{return t}},_n=(e)=>e==null?void 0:String(e),gl=()=>({cpu:0,memoryBytes:0,diskBytes:0,gpu:0}),zt=(e={})=>({...gl(),...e});function Ov(e){return{id:String(e.id),organizationId:String(e.organization_id),projectId:String(e.project_id),name:String(e.name),purpose:String(e.purpose),backend:String(e.backend),region:_n(e.region),architecture:_n(e.architecture),labels:Qn(e.labels,{}),requiredServerLabels:Qn(e.required_server_labels,{}),toleratedTaints:Qn(e.tolerated_taints,[]),capacity:zt(Qn(e.capacity,{})),reserved:Qn(e.reserved,{}),maxWorkloads:Number(e.max_workloads),costWeight:Number(e.cost_weight),spreadKey:_n(e.spread_key),concurrency:Number(e.concurrency),ephemeralWorkspaces:Number(e.ephemeral_workspaces)===1,allowProductionSecrets:Number(e.allow_production_secrets)===1,status:String(e.status),version:Number(e.version),createdAt:String(e.created_at),updatedAt:String(e.updated_at)}}function $v(e){return{id:String(e.id),projectId:String(e.project_id),environmentId:_n(e.environment_id),resourceId:String(e.resource_id),releaseId:_n(e.release_id),poolId:String(e.pool_id),serverId:_n(e.server_id),purpose:String(e.purpose),requirements:Qn(e.requirements,{}),decision:Qn(e.decision,{}),stateful:Number(e.stateful)===1,autoReschedule:Number(e.auto_reschedule)===1,status:String(e.status),version:Number(e.version),createdAt:String(e.created_at),updatedAt:String(e.updated_at)}}function jA(e){return{id:String(e.id),projectId:String(e.project_id),resourceId:_n(e.resource_id),poolId:String(e.pool_id),placementId:_n(e.placement_id),operationId:_n(e.operation_id),sourceSha:String(e.source_sha),buildSpec:Qn(e.build_spec,{}),credentialPolicy:Qn(e.credential_policy,{productionSecrets:!1,shortLivedTokenExpiresAt:""}),workspace:_n(e.workspace),cacheKey:_n(e.cache_key),artifactUri:_n(e.artifact_uri),artifactDigest:_n(e.artifact_digest),status:String(e.status),cleanupAt:_n(e.cleanup_at),createdAt:String(e.created_at),updatedAt:String(e.updated_at)}}class Ho{control;now;id;constructor(e,t=()=>new Date,r=()=>crypto.randomUUID()){this.control=e;this.now=t;this.id=r}createPool(e){let t=this.id(),r=this.now().toISOString();return this.control.database.run("INSERT INTO capacity_pools (id,organization_id,project_id,name,purpose,backend,region,architecture,labels,required_server_labels,tolerated_taints,capacity,reserved,max_workloads,cost_weight,spread_key,concurrency,ephemeral_workspaces,allow_production_secrets,status,created_at,updated_at) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",[t,e.organizationId,e.projectId,e.name,e.purpose,e.backend,e.region??null,e.architecture??null,JSON.stringify(e.labels),JSON.stringify(e.requiredServerLabels),JSON.stringify(e.toleratedTaints),JSON.stringify(zt(e.capacity)),"{}",e.maxWorkloads,e.costWeight,e.spreadKey??null,e.concurrency,e.ephemeralWorkspaces?1:0,e.allowProductionSecrets?1:0,e.status,r,r]),this.getPool(t)}getPool(e){let t=this.control.database.query("SELECT * FROM capacity_pools WHERE id=?").get(e);return t?Ov(t):void 0}listPools(e){return this.control.database.query("SELECT * FROM capacity_pools WHERE project_id=? ORDER BY name").all(e).map(Ov)}updatePool(e,t){let r=this.getPool(e);if(!r)throw Error("Capacity pool was not found.");let o={...r,...t},m=this.now().toISOString();return this.control.database.run("UPDATE capacity_pools SET status=?,reserved=?,capacity=?,concurrency=?,version=version+1,updated_at=? WHERE id=?",[o.status,JSON.stringify(o.reserved),JSON.stringify(o.capacity),o.concurrency,m,e]),this.getPool(e)}addMember(e,t,r={}){let o=this.now().toISOString();this.control.database.run("INSERT INTO capacity_pool_members (pool_id,server_id,capacity_override,status,created_at,updated_at) VALUES (?,?,?,'active',?,?) ON CONFLICT(pool_id,server_id) DO UPDATE SET capacity_override=excluded.capacity_override,status='active',updated_at=excluded.updated_at",[e,t,JSON.stringify(r),o,o])}members(e){return this.control.database.query("SELECT * FROM capacity_pool_members WHERE pool_id=? ORDER BY server_id").all(e).map((t)=>({serverId:String(t.server_id),status:String(t.status),capacityOverride:Qn(t.capacity_override,{})}))}memberStatus(e,t,r){this.control.database.run("UPDATE capacity_pool_members SET status=?,updated_at=? WHERE pool_id=? AND server_id=?",[r,this.now().toISOString(),e,t])}listPlacements(e,t){let r=this.control.database.query("SELECT * FROM workload_placements WHERE project_id=? ORDER BY created_at,id").all(e).map($v);return t?.length?r.filter((o)=>t.includes(o.status)):r}getPlacement(e){let t=this.control.database.query("SELECT * FROM workload_placements WHERE id=?").get(e);return t?$v(t):void 0}activeUsage(e,t){let r=this.control.database.query("SELECT l.resources,l.server_id FROM capacity_leases l WHERE l.pool_id=? AND l.state='active' AND l.expires_at>?").all(e,this.now().toISOString()),o=t?r.filter((m)=>String(m.server_id)===t):r;return{resources:o.reduce((m,h)=>{let I=zt(Qn(h.resources,{}));for(let R of Object.keys(m))m[R]+=I[R];return m},gl()),count:o.length}}expireLeases(){return this.control.database.run("UPDATE capacity_leases SET state='expired',updated_at=? WHERE state='active' AND expires_at<=?",[this.now().toISOString(),this.now().toISOString()]).changes}reserve(e){return this.control.transaction(()=>{this.expireLeases();let t=this.id(),r=this.id(),o=this.now().toISOString(),m=new Date(this.now().getTime()+(e.requirements.leaseSeconds??300)*1000).toISOString();return this.control.database.run("INSERT INTO workload_placements (id,project_id,environment_id,resource_id,release_id,pool_id,server_id,purpose,requirements,decision,stateful,auto_reschedule,status,created_at,updated_at) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,'reserved',?,?)",[t,e.projectId,e.environmentId??null,e.resourceId,e.releaseId??null,e.decision.poolId,e.decision.serverId??null,e.requirements.purpose,JSON.stringify(e.requirements),JSON.stringify(e.decision),e.requirements.stateful?1:0,e.requirements.autoReschedule&&!e.requirements.stateful?1:0,o,o]),this.control.database.run("INSERT INTO capacity_leases (id,placement_id,pool_id,server_id,resources,state,expires_at,created_at,updated_at) VALUES (?,?,?,?,?,'active',?,?,?)",[r,t,e.decision.poolId,e.decision.serverId??null,JSON.stringify(zt(e.requirements.resources)),m,o,o]),this.getPlacement(t)})}transitionPlacement(e,t){if(!this.getPlacement(e))throw Error("Placement was not found.");let o=this.now().toISOString();if(this.control.database.run("UPDATE workload_placements SET status=?,version=version+1,updated_at=? WHERE id=?",[t,o,e]),["released","failed","blocked"].includes(t))this.control.database.run("UPDATE capacity_leases SET state='released',updated_at=? WHERE placement_id=? AND state='active'",[o,e]);return this.getPlacement(e)}createBuild(e){let t=this.id(),r=this.now().toISOString();return this.control.database.run("INSERT INTO remote_builds (id,project_id,resource_id,pool_id,placement_id,operation_id,source_sha,build_spec,credential_policy,status,created_at,updated_at) VALUES (?,?,?,?,?,?,?,?,?,'queued',?,?)",[t,e.projectId,e.resourceId??null,e.poolId,e.placementId??null,e.operationId??null,e.sourceSha,JSON.stringify(e.buildSpec),JSON.stringify({productionSecrets:!1,shortLivedTokenExpiresAt:e.tokenExpiresAt}),r,r]),this.getBuild(t)}getBuild(e){let t=this.control.database.query("SELECT * FROM remote_builds WHERE id=?").get(e);return t?jA(t):void 0}updateBuild(e,t){let r=this.getBuild(e);if(!r)throw Error("Remote build was not found.");let o={...r,...t},m=this.now().toISOString();return this.control.database.run("UPDATE remote_builds SET operation_id=?,workspace=?,cache_key=?,artifact_uri=?,artifact_digest=?,status=?,cleanup_at=?,updated_at=? WHERE id=?",[o.operationId??null,o.workspace??null,o.cacheKey??null,o.artifactUri??null,o.artifactDigest??null,o.status,o.cleanupAt??null,m,e]),this.getBuild(e)}}var fu=["cpu","memoryBytes","diskBytes","gpu"],Nv=(e,t={})=>Object.entries(t).every(([r,o])=>e[r]===o),MA=(e,t,r)=>Object.fromEntries(fu.map((o)=>[o,Math.max(0,e[o]-t[o]-(r[o]??0))])),BA=(e,t)=>fu.every((r)=>e[r]>=(t[r]??0)),LA=(e,t)=>fu.reduce((r,o)=>r+(t[o]??0)/(e[o]||1),0);class Go{store;fleet;constructor(e,t){this.store=e;this.fleet=t}poolCandidates(e,t,r){let o=[];for(let m of this.store.listPools(e)){if(m.id===r)continue;let h=[];if(m.status!=="active")h.push(`pool is ${m.status}`);if(m.purpose!==t.purpose)h.push(`purpose is ${m.purpose}`);if(t.region&&m.region!==t.region)h.push(`region is ${m.region??"unset"}`);if(t.architecture&&m.architecture!==t.architecture)h.push(`architecture is ${m.architecture??"unset"}`);if(!Nv(m.labels,t.labels))h.push("pool labels do not match");if(m.backend==="server"){let I=this.store.members(m.id);if(!I.length)o.push(this.decision(m,void 0,t,[...h,"pool has no servers"]));for(let R of I){let A=this.fleet.get(R.serverId),E=[...h];if(!A)E.push("server is missing");else this.serverReasons(m,A,R.status,t,E);o.push(this.decision(m,A,t,E,R.capacityOverride))}}else o.push(this.decision(m,void 0,t,h))}return o.sort((m,h)=>Number(h.eligible)-Number(m.eligible)||m.score.cost-h.score.cost||m.score.spread-h.score.spread||m.score.fit-h.score.fit||m.poolName.localeCompare(h.poolName)||(m.serverId??"").localeCompare(h.serverId??""))}serverReasons(e,t,r,o,m){if(r!=="active")m.push(`member is ${r}`);if(t.status!=="ready")m.push(`server is ${t.status}`);if(!t.roles.includes(o.purpose==="build"?"build":o.purpose))m.push(`server lacks ${o.purpose} role`);if(!Nv(t.labels,e.requiredServerLabels))m.push("server labels do not match pool selector");for(let h of t.taints)if(!e.toleratedTaints.includes(h))m.push(`taint ${h} is not tolerated`)}decision(e,t,r,o,m={}){let h=zt({...t?.capacity,...m}),I=t?Object.fromEntries(fu.map((E)=>[E,Math.min(e.capacity[E],h[E])])):e.capacity,R=this.store.activeUsage(e.id,t?.id),A=MA(I,R.resources,e.reserved);if(R.count>=e.maxWorkloads)o.push("workload limit reached");if(r.purpose==="build"&&R.count>=e.concurrency)o.push("build concurrency reached");if(!BA(A,r.resources))o.push("insufficient capacity");return{poolId:e.id,poolName:e.name,serverId:t?.id,eligible:o.length===0,reasons:o,available:A,score:{fit:LA(A,r.resources),spread:R.count,cost:e.costWeight}}}explain(e,t){return this.poolCandidates(e,t)}place(e){return this.store.control.transaction(()=>{let t=this.poolCandidates(e.projectId,e.requirements).find((r)=>r.eligible);if(!t)throw Error(`No eligible capacity: ${this.explain(e.projectId,e.requirements).flatMap((r)=>r.reasons).join("; ")}`);return this.store.reserve({...e,decision:t})})}activate(e){return this.store.transitionPlacement(e,"active")}release(e){return this.store.transitionPlacement(e,"released")}drainPool(e){let t=this.store.updatePool(e,{status:"draining"}),r=[],o=[];for(let m of this.store.listPlacements(t.projectId,["reserved","active"]).filter((h)=>h.poolId===e)){if(m.stateful){o.push(this.store.transitionPlacement(m.id,"blocked"));continue}this.store.transitionPlacement(m.id,"moving");let h=this.place({projectId:m.projectId,environmentId:m.environmentId,resourceId:m.resourceId,releaseId:m.releaseId,requirements:m.requirements});r.push(this.activate(h.id)),this.store.transitionPlacement(m.id,"released")}return{pool:t,moved:r,blocked:o}}drainServer(e,t){this.store.memberStatus(e,t,"draining");let r=this.store.getPool(e);if(!r)throw Error("Capacity pool was not found.");let o=[],m=[];for(let h of this.store.listPlacements(r.projectId,["reserved","active"]).filter((I)=>I.poolId===e&&I.serverId===t)){if(h.stateful){m.push(this.store.transitionPlacement(h.id,"blocked"));continue}this.store.transitionPlacement(h.id,"moving");let I=this.poolCandidates(h.projectId,h.requirements).find((A)=>A.eligible);if(!I){m.push(this.store.transitionPlacement(h.id,"blocked"));continue}let R=this.store.reserve({projectId:h.projectId,environmentId:h.environmentId,resourceId:h.resourceId,releaseId:h.releaseId,requirements:h.requirements,decision:I});o.push(this.activate(R.id)),this.store.transitionPlacement(h.id,"released")}return{moved:o,blocked:m}}reconcileFailure(e){let t=this.fleet.get(e);if(!t)throw Error("Server was not found.");let r=[],o=[];for(let m of this.store.listPlacements(t.projectId,["active"]).filter((h)=>h.serverId===e)){if(m.stateful||!m.autoReschedule){o.push(this.store.transitionPlacement(m.id,"blocked"));continue}this.store.transitionPlacement(m.id,"failed");let h=this.place({projectId:m.projectId,environmentId:m.environmentId,resourceId:m.resourceId,releaseId:m.releaseId,requirements:m.requirements});r.push(this.activate(h.id))}return{rescheduled:r,blocked:o}}}class um{store;placement;now;queue;constructor(e,t,r,o=()=>new Date){this.store=e;this.placement=t;this.now=o;this.queue=r??new c(e.control)}enqueue(e){if(!/^[a-f0-9]{7,64}$/i.test(e.sourceSha))throw Error("A pinned source commit SHA is required.");let t={purpose:"build",resources:{cpu:1,memoryBytes:1073741824,diskBytes:5368709120},leaseSeconds:3600,...e.requirements},r=this.placement.place({projectId:e.projectId,resourceId:e.resourceId,requirements:t}),o=new Date(this.now().getTime()+900000).toISOString(),m=this.store.createBuild({projectId:e.projectId,resourceId:e.resourceId,poolId:r.poolId,placementId:r.id,sourceSha:e.sourceSha,buildSpec:e.buildSpec,tokenExpiresAt:o}),h=this.queue.enqueue({projectId:e.projectId,resourceId:e.resourceId,kind:"build.remote",input:{buildId:m.id},lockKey:`build:${m.id}`,providerKey:`pool:${r.poolId}`,buildSlot:!0,timeoutSeconds:3600,cancellationMode:"cooperative",resumePolicy:"fail"}).operation;return m=this.store.updateBuild(m.id,{operationId:h.id}),m}cancel(e){let t=this.store.getBuild(e);if(!t)throw Error("Remote build was not found.");if(t.operationId)this.queue.requestCancellation(t.operationId);return this.store.updateBuild(e,{status:"cancelled"})}}var FA=/^sha256:[a-f0-9]{64}$/,zA=/^(?:s3|https|oci):\/\//;function yA(e,t){return{"build.remote":async(r)=>{let o=r.operation.input&&typeof r.operation.input==="object"&&!Array.isArray(r.operation.input)?r.operation.input:{},m=e.getBuild(String(o.buildId??""));if(!m)throw Error("Queued remote build was not found.");let h=e.getPool(m.poolId);if(!h||h.purpose!=="build"||h.status!=="active")throw Error("The selected build pool is unavailable.");if(!h.ephemeralWorkspaces)throw Error("Remote builds require an ephemeral workspace pool.");if(m.credentialPolicy.productionSecrets||h.allowProductionSecrets)throw Error("Production secrets are forbidden in remote build workers.");if(new Date(m.credentialPolicy.shortLivedTokenExpiresAt)<=new Date)throw Error("The short-lived build credential has expired.");let I=t.find((A)=>A.backend===h.backend);if(!I)throw Error(`No ${h.backend} remote build driver is configured.`);let R=`build://${h.id}/${m.id}`;e.updateBuild(m.id,{status:"running",workspace:R}),r.checkpoint("checkout",`Preparing isolated workspace for ${m.sourceSha}.`);try{r.throwIfCancellationRequested(),r.checkpoint("build","Executing resource-limited build.");let A=await I.run({build:e.getBuild(m.id),pool:h,signal:r.signal,log:(E)=>r.log(E,{stream:"stdout"})});if(r.throwIfCancellationRequested(),!FA.test(A.artifactDigest)||!zA.test(A.artifactUri))throw Error("Builder returned an unverified artifact reference.");return e.updateBuild(m.id,{status:"uploading",cacheKey:A.cacheKey,artifactUri:A.artifactUri,artifactDigest:A.artifactDigest}),r.checkpoint("verify","Verified content-addressed artifact upload."),await I.cleanup({build:e.getBuild(m.id),pool:h}),e.updateBuild(m.id,{status:"succeeded",workspace:void 0,cleanupAt:new Date().toISOString()}),{buildId:m.id,artifactUri:A.artifactUri,artifactDigest:A.artifactDigest,cacheKey:A.cacheKey??null}}catch(A){try{await I.cleanup({build:e.getBuild(m.id),pool:h}),e.updateBuild(m.id,{status:r.cancellationRequested()?"cancelled":"failed",workspace:void 0,cleanupAt:new Date().toISOString()})}catch{e.updateBuild(m.id,{status:"cleanup_required"})}throw A}}}}class ki{backend;transport;handles=new Map;constructor(e,t){this.backend=e;this.transport=t}async run({build:e,pool:t,signal:r,log:o}){let m=await this.transport.provision({backend:this.backend,pool:t,build:e,tokenExpiresAt:e.credentialPolicy.shortLivedTokenExpiresAt});return this.handles.set(e.id,m.handle),o(`${this.backend} builder provisioned`),await this.transport.execute({handle:m.handle,spec:e.buildSpec,sourceSha:e.sourceSha,signal:r,onLog:o}),this.transport.publish({handle:m.handle,sourceSha:e.sourceSha})}async cleanup({build:e}){let t=this.handles.get(e.id);if(!t)return;await this.transport.destroy({handle:t}),this.handles.delete(e.id)}}var IA=(e)=>new ki("server",e),SA=(e)=>new ki("ecs",e),CA=(e)=>new ki("asg",e);var Qi=(e,t)=>{try{return JSON.parse(String(e))}catch{return t}},Fn=(e)=>e==null?void 0:String(e),jv=(e)=>({id:String(e.id),organizationId:String(e.organization_id),projectId:String(e.project_id),environmentId:Fn(e.environment_id),name:String(e.name),hostname:String(e.hostname),homeRegion:String(e.home_region),regions:Qi(e.regions,[]),trafficPolicy:String(e.traffic_policy),dataPolicy:Qi(e.data_policy,{replicate:[],maxLagSeconds:60,retainOnDestroy:!0}),status:String(e.status),activeRegion:String(e.active_region),revision:Fn(e.revision),version:Number(e.version),createdAt:String(e.created_at),updatedAt:String(e.updated_at)}),rg=(e)=>({id:String(e.id),topologyId:String(e.topology_id),region:String(e.region),role:String(e.role),provider:String(e.provider),stackId:Fn(e.stack_id),stackRevision:Fn(e.stack_revision),status:String(e.status),health:Qi(e.health,{}),lastHealthyAt:Fn(e.last_healthy_at),version:Number(e.version),createdAt:String(e.created_at),updatedAt:String(e.updated_at)}),ng=(e)=>({id:String(e.id),topologyId:String(e.topology_id),kind:String(e.kind),sourceRegion:String(e.source_region),targetRegion:String(e.target_region),config:Qi(e.config,{}),status:String(e.status),checkpoint:Fn(e.checkpoint),lagSeconds:e.lag_seconds==null?void 0:Number(e.lag_seconds),lastVerifiedAt:Fn(e.last_verified_at),version:Number(e.version),createdAt:String(e.created_at),updatedAt:String(e.updated_at)}),ig=(e)=>({id:String(e.id),topologyId:String(e.topology_id),hostname:String(e.hostname),dnsProvider:String(e.dns_provider),cdnEnabled:Number(e.cdn_enabled)===1,wafEnabled:Number(e.waf_enabled)===1,weights:Qi(e.weights,{}),desiredWeights:Qi(e.desired_weights,{}),status:String(e.status),providerState:Qi(e.provider_state,{}),version:Number(e.version),createdAt:String(e.created_at),updatedAt:String(e.updated_at)}),Mv=(e)=>({id:String(e.id),topologyId:String(e.topology_id),operationId:Fn(e.operation_id),kind:String(e.kind),requestedRegion:Fn(e.requested_region),revision:Fn(e.revision),plan:Qi(e.plan,[]),currentStep:Fn(e.current_step),completedSteps:Qi(e.completed_steps,[]),status:String(e.status),error:Fn(e.error),createdAt:String(e.created_at),updatedAt:String(e.updated_at)});class qo{control;now;id;constructor(e,t=()=>new Date,r=()=>crypto.randomUUID()){this.control=e;this.now=t;this.id=r}create(e){return this.control.transaction(()=>{if(e.regions.length<2||new Set(e.regions.map((h)=>h.region)).size!==e.regions.length||e.regions.filter((h)=>h.role==="primary").length!==1)throw Error("A topology requires unique regions and exactly one primary.");let t=e.regions.find((h)=>h.role==="primary"),r=this.id(),o=this.now().toISOString();this.control.database.run("INSERT INTO regional_topologies (id,organization_id,project_id,environment_id,name,hostname,home_region,regions,traffic_policy,data_policy,status,active_region,created_at,updated_at) VALUES (?,?,?,?,?,?,?,?,?,?,'draft',?,?,?)",[r,e.organizationId,e.projectId,e.environmentId??null,e.name,e.hostname,t.region,JSON.stringify(e.regions),e.trafficPolicy,JSON.stringify(e.dataPolicy),t.region,o,o]);for(let h of e.regions)this.control.database.run("INSERT INTO regional_targets (id,topology_id,region,role,provider,status,created_at,updated_at) VALUES (?,?,?,?,?,'pending',?,?)",[this.id(),r,h.region,h.role,h.provider,o,o]);for(let h of e.dataPolicy.replicate)for(let I of e.regions)for(let R of e.regions.filter((A)=>A.region!==I.region))this.control.database.run("INSERT INTO regional_replication_channels (id,topology_id,kind,source_region,target_region,config,status,created_at,updated_at) VALUES (?,?,?,?,?,?,'pending',?,?)",[this.id(),r,h,I.region,R.region,"{}",o,o]);let m=Object.fromEntries(e.regions.map((h)=>[h.region,h.region===t.region?100:0]));return this.control.database.run("INSERT INTO regional_traffic_routes (id,topology_id,hostname,dns_provider,cdn_enabled,waf_enabled,weights,desired_weights,status,created_at,updated_at) VALUES (?,?,?,?,?,?,?,?, 'pending',?,?)",[this.id(),r,e.hostname,e.dnsProvider??"route53",e.cdnEnabled?1:0,e.wafEnabled?1:0,JSON.stringify(m),JSON.stringify(m),o,o]),this.get(r)})}get(e){let t=this.control.database.query("SELECT * FROM regional_topologies WHERE id=?").get(e);return t?jv(t):void 0}list(e){return this.control.database.query("SELECT * FROM regional_topologies WHERE project_id=? ORDER BY name").all(e).map(jv)}targets(e){return this.control.database.query("SELECT * FROM regional_targets WHERE topology_id=? ORDER BY role,region").all(e).map(rg)}channels(e){return this.control.database.query("SELECT * FROM regional_replication_channels WHERE topology_id=? ORDER BY kind,target_region").all(e).map(ng)}route(e){let t=this.control.database.query("SELECT * FROM regional_traffic_routes WHERE topology_id=?").get(e);return t?ig(t):void 0}updateTopology(e,t){let r=this.get(e);if(!r)throw Error("Regional topology was not found.");let o={...r,...t},m=this.now().toISOString();return this.control.database.run("UPDATE regional_topologies SET status=?,active_region=?,revision=?,regions=?,version=version+1,updated_at=? WHERE id=?",[o.status,o.activeRegion,o.revision??null,JSON.stringify(o.regions),m,e]),this.get(e)}updateTarget(e,t){let r=this.control.database.query("SELECT * FROM regional_targets WHERE id=?").get(e);if(!r)throw Error("Regional target was not found.");let o=rg(r),m={...o,...t},h=this.now().toISOString();return this.control.database.run("UPDATE regional_targets SET role=?,stack_id=?,stack_revision=?,status=?,health=?,last_healthy_at=?,version=version+1,updated_at=? WHERE id=?",[m.role,m.stackId??null,m.stackRevision??null,m.status,JSON.stringify(m.health),m.lastHealthyAt??null,h,e]),rg(this.control.database.query("SELECT * FROM regional_targets WHERE id=?").get(e))}updateChannel(e,t){let r=this.control.database.query("SELECT * FROM regional_replication_channels WHERE id=?").get(e);if(!r)throw Error("Replication channel was not found.");let o=ng(r),m={...o,...t},h=this.now().toISOString();return this.control.database.run("UPDATE regional_replication_channels SET status=?,checkpoint=?,lag_seconds=?,last_verified_at=?,version=version+1,updated_at=? WHERE id=?",[m.status,m.checkpoint??null,m.lagSeconds??null,m.lastVerifiedAt??null,h,e]),ng(this.control.database.query("SELECT * FROM regional_replication_channels WHERE id=?").get(e))}updateRoute(e,t){let r=this.control.database.query("SELECT * FROM regional_traffic_routes WHERE id=?").get(e);if(!r)throw Error("Traffic route was not found.");let o=ig(r),m={...o,...t},h=this.now().toISOString();return this.control.database.run("UPDATE regional_traffic_routes SET weights=?,desired_weights=?,status=?,provider_state=?,version=version+1,updated_at=? WHERE id=?",[JSON.stringify(m.weights),JSON.stringify(m.desiredWeights),m.status,JSON.stringify(m.providerState),h,e]),ig(this.control.database.query("SELECT * FROM regional_traffic_routes WHERE id=?").get(e))}createExecution(e){let t=this.id(),r=this.now().toISOString();return this.control.database.run("INSERT INTO regional_executions (id,topology_id,operation_id,kind,requested_region,revision,plan,status,created_at,updated_at) VALUES (?,?,?,?,?,?,?,'queued',?,?)",[t,e.topologyId,e.operationId??null,e.kind,e.requestedRegion??null,e.revision??null,JSON.stringify(e.plan),r,r]),this.execution(t)}execution(e){let t=this.control.database.query("SELECT * FROM regional_executions WHERE id=?").get(e);return t?Mv(t):void 0}executions(e){return this.control.database.query("SELECT * FROM regional_executions WHERE topology_id=? ORDER BY created_at DESC").all(e).map(Mv)}updateExecution(e,t){let r=this.execution(e);if(!r)throw Error("Regional execution was not found.");let o={...r,...t},m=this.now().toISOString();return this.control.database.run("UPDATE regional_executions SET operation_id=?,current_step=?,completed_steps=?,status=?,error=?,updated_at=? WHERE id=?",[o.operationId??null,o.currentStep??null,JSON.stringify(o.completedSteps),o.status,o.error??null,m,e]),this.execution(e)}}class Vo{store;queue;constructor(e,t){this.store=e;this.queue=t??new c(e.control)}create(e){return this.store.create(e)}plan(e,t,r){if(!this.store.get(e))throw Error("Regional topology was not found.");let m=this.store.targets(e),h=this.store.channels(e);if(t==="rollout")return[...m.filter((I)=>I.role==="secondary").map((I)=>`apply:${I.region}`),...m.filter((I)=>I.role==="primary").map((I)=>`apply:${I.region}`),...h.map((I)=>`replicate:${I.kind}:${I.targetRegion}`),"traffic:activate"];if(t==="failover"||t==="failback")return[`health:${r}`,`replication:${r}`,`traffic:${r}`,`roles:${r}`];if(t==="destroy")return["traffic:drain",...m.filter((I)=>I.role==="secondary").map((I)=>`delete:${I.region}`),...m.filter((I)=>I.role==="primary").map((I)=>`delete:${I.region}`),"complete"];return["observe","repair"]}enqueue(e){let t=this.store.get(e.topologyId);if(!t)throw Error("Regional topology was not found.");if(["failover","failback"].includes(e.kind)&&(!e.targetRegion||!t.regions.some((h)=>h.region===e.targetRegion)))throw Error("A topology target region is required.");if(e.kind==="failover"&&e.targetRegion===t.activeRegion)throw Error("Target region is already active.");if((e.kind==="destroy"||e.deleteData)&&e.confirmation!==t.name)throw Error("Exact topology name confirmation is required.");if(e.deleteData&&t.dataPolicy.retainOnDestroy)throw Error("Topology policy requires regional data retention.");if(e.kind==="rollout"&&!/^[A-Za-z0-9._:@/-]{7,200}$/.test(e.revision??""))throw Error("An immutable revision is required.");let r=this.plan(t.id,e.kind,e.targetRegion),o=this.store.createExecution({topologyId:t.id,kind:e.kind,requestedRegion:e.targetRegion,revision:e.revision,plan:r}),m=this.queue.enqueue({projectId:t.projectId,environmentId:t.environmentId,kind:`region.${e.kind}`,input:{executionId:o.id,manifest:e.manifest??{},deleteData:e.deleteData===!0},lockKey:`regional-topology:${t.id}`,providerKey:"multi-region",timeoutSeconds:7200,cancellationMode:"provider_non_cancellable",resumePolicy:"requeue",maxAttempts:3}).operation;return this.store.updateExecution(o.id,{operationId:m.id}),{execution:this.store.execution(o.id),operation:m}}}var UA=(e)=>e&&typeof e==="object"&&!Array.isArray(e)?e:{};function ml(e,t){let r=(E)=>{let x=t.find((N)=>N.provider===E.provider);if(!x)throw Error(`No ${E.provider} regional driver is configured.`);return x},o=(E,x,N)=>{E.throwIfCancellationRequested(),E.checkpoint(N,N),e.updateExecution(x.id,{status:"running",currentStep:N,completedSteps:[...e.execution(x.id).completedSteps,N]})},m=async(E,x)=>{let N=e.route(E.id);if(!N)throw Error("Regional traffic route was not found.");let F=e.targets(E.id).find((K)=>K.region===E.activeRegion)??e.targets(E.id)[0];if(!F)throw Error("Regional traffic target was not found.");e.updateRoute(N.id,{status:"applying",desiredWeights:x});let z=await r(F).applyTraffic({route:e.route(E.id),topology:E,weights:x});e.updateRoute(N.id,{status:Object.values(x).every((K)=>K===0)?"drained":"in_sync",weights:x,providerState:z.providerState})},h=async(E,x,N,F)=>{e.updateTopology(N.id,{status:"provisioning"});let z=x.revision,K=F.manifest??{},J=e.targets(N.id).sort((se,ye)=>Number(se.role==="primary")-Number(ye.role==="primary"));for(let se of J){o(E,x,`apply:${se.region}`),e.updateTarget(se.id,{status:"provisioning"});let ye=await r(se).applyStack({target:se,revision:z,manifest:K,signal:E.signal}),ne=await r(se).health({target:e.updateTarget(se.id,{stackId:ye.stackId,stackRevision:z})});if(!ne.healthy)throw Error(`Regional health gate failed in ${se.region}.`);e.updateTarget(se.id,{status:"ready",health:ne.evidence,lastHealthyAt:new Date().toISOString()})}for(let se of e.channels(N.id)){o(E,x,`replicate:${se.kind}:${se.targetRegion}`),e.updateChannel(se.id,{status:"configuring"});let ye=J.find((Ce)=>Ce.region===se.sourceRegion),ne=await r(ye).configureReplication({channel:se,topology:N}),fe=await r(ye).verifyReplication({channel:e.updateChannel(se.id,{checkpoint:ne.checkpoint,lagSeconds:ne.lagSeconds}),topology:N});if(!fe.healthy||fe.lagSeconds>N.dataPolicy.maxLagSeconds)throw Error(`${se.kind} replication to ${se.targetRegion} exceeds the RPO.`);e.updateChannel(se.id,{status:"in_sync",checkpoint:fe.checkpoint,lagSeconds:fe.lagSeconds,lastVerifiedAt:new Date().toISOString()})}o(E,x,"traffic:activate");let re=Object.fromEntries(N.regions.map((se)=>[se.region,se.region===N.activeRegion?100:0]));return await m(N,re),e.updateTopology(N.id,{status:"ready",revision:z}),{revision:z,activeRegion:N.activeRegion}},I=async(E,x,N)=>{let F=x.requestedRegion,z=e.targets(N.id).find((re)=>re.region===F);if(!z)throw Error("Requested regional target was not found.");e.updateTopology(N.id,{status:x.kind==="failover"?"failing_over":"failing_back"}),o(E,x,`health:${F}`);let K=await r(z).health({target:z});if(!K.healthy)throw Error(`Target region ${F} is unhealthy.`);e.updateTarget(z.id,{status:"ready",health:K.evidence,lastHealthyAt:new Date().toISOString()}),o(E,x,`replication:${F}`);for(let re of e.channels(N.id).filter((se)=>se.targetRegion===F)){let se=await r(z).verifyReplication({channel:re,topology:N});if(!se.healthy||se.lagSeconds>N.dataPolicy.maxLagSeconds)throw Error(`${re.kind} replication is outside the failover RPO.`);e.updateChannel(re.id,{status:"in_sync",checkpoint:se.checkpoint,lagSeconds:se.lagSeconds,lastVerifiedAt:new Date().toISOString()})}o(E,x,`traffic:${F}`),await m(N,Object.fromEntries(N.regions.map((re)=>[re.region,re.region===F?100:0]))),o(E,x,`roles:${F}`);let J=N.regions.map((re)=>({...re,role:re.region===F?"primary":"secondary"}));for(let re of e.targets(N.id))e.updateTarget(re.id,{role:re.region===F?"primary":"secondary"});return e.updateTopology(N.id,{status:x.kind==="failover"?"failed_over":"ready",activeRegion:F,regions:J}),{activeRegion:F}},R=async(E,x,N,F)=>{e.updateTopology(N.id,{status:"destroying"}),o(E,x,"traffic:drain"),await m(N,Object.fromEntries(N.regions.map((K)=>[K.region,0])));let z=F.deleteData!==!0;for(let K of e.targets(N.id).sort((J,re)=>Number(J.role==="primary")-Number(re.role==="primary")))o(E,x,`delete:${K.region}`),e.updateTarget(K.id,{status:"deleting"}),await r(K).deleteStack({target:K,retainData:z,signal:E.signal}),e.updateTarget(K.id,{status:"deleted"});return o(E,x,"complete"),e.updateTopology(N.id,{status:"destroyed"}),{destroyed:!0,dataRetained:z}},A=async(E)=>{let x=UA(E.operation.input),N=e.execution(String(x.executionId??""));if(!N)throw Error("Regional execution was not found.");let F=e.get(N.topologyId);if(!F)throw Error("Regional topology was not found.");try{let z;if(N.kind==="rollout")z=await h(E,N,F,x);else if(N.kind==="failover"||N.kind==="failback")z=await I(E,N,F);else if(N.kind==="destroy")z=await R(E,N,F,x);else throw Error("Regional reconciliation is not configured.");return e.updateExecution(N.id,{status:"succeeded",currentStep:void 0}),{executionId:N.id,...z}}catch(z){if(e.updateExecution(N.id,{status:E.cancellationRequested()?"cancelled":"failed",error:z instanceof Error?z.message:String(z)}),e.get(F.id)?.status!=="destroyed")e.updateTopology(F.id,{status:"failed"});throw z}};return{"region.rollout":A,"region.failover":A,"region.failback":A,"region.destroy":A,"region.reconcile":A}}class pm{transport;provider="aws";constructor(e){this.transport=e}async applyStack({target:e,revision:t,manifest:r}){return this.transport.applyCloudFormation({region:e.region,stackId:e.stackId,revision:t,manifest:r})}async deleteStack({target:e,retainData:t}){if(!e.stackId)return;await this.transport.deleteCloudFormation({region:e.region,stackId:e.stackId,retainData:t})}async health({target:e}){if(!e.stackId)return{healthy:!1,evidence:{reason:"stack is not provisioned"}};return this.transport.stackHealth({region:e.region,stackId:e.stackId})}async configureReplication({channel:e}){let t={sourceRegion:e.sourceRegion,targetRegion:e.targetRegion,config:e.config};if(e.kind==="s3")return this.transport.configureS3Replication(t);if(e.kind==="dynamodb")return this.transport.configureDynamoGlobalTable(t);return this.transport.replicateSecrets(t)}async verifyReplication({channel:e}){return this.transport.verifyReplication({kind:e.kind,sourceRegion:e.sourceRegion,targetRegion:e.targetRegion,checkpoint:e.checkpoint})}async applyTraffic({route:e,weights:t}){return this.transport.applyEdge({hostname:e.hostname,weights:t,route53:e.dnsProvider==="route53",cloudFront:e.cdnEnabled,waf:e.wafEnabled})}}class jo{provider;reason;constructor(e,t="Configure a regional provider transport before executing this operation."){this.provider=e;this.reason=t}fail(){throw Error(this.reason)}async applyStack(){return this.fail()}async deleteStack(){this.fail()}async health(){return this.fail()}async configureReplication(){return this.fail()}async verifyReplication(){return this.fail()}async applyTraffic(){return this.fail()}}function ce(e,t,r){return[e,t,r].map((o)=>encodeURIComponent(o)).join(":")}function em(e){return Object.fromEntries(["start","stop","restart","redeploy","scale","logs","exec","inspect","files"].map((t)=>[t,{supported:!1,reason:e}]))}function ut(e,t,r=["exec","files"]){let o=em(t);for(let m of e)o[m]={supported:!0,requiresConfirmation:!["logs","inspect"].includes(m),requiresRecentAuth:r.includes(m)};return o}var qA=/(?:secret|password|passwd|token|api[_-]?key|private[_-]?key|credential|authorization|cookie)/i;function ve(e){let t=String(e??"").trim().toLowerCase();if(["active","running","ready","healthy","available","steady","active:running"].includes(t))return"running";if(["activating","pending","provisioning","starting","created","initializing"].includes(t))return"starting";if(["deactivating","stopping","draining","terminating"].includes(t))return"stopping";if(["inactive","stopped","complete","completed","exited","disabled"].includes(t))return"stopped";if(["degraded","unhealthy","warning"].includes(t))return"degraded";if(["failed","dead","error","crashed","oomkilled"].includes(t))return"failed";return"unknown"}function Te(e){if(Array.isArray(e))return e.map((r)=>Te(r));if(!e||typeof e!=="object")return e;let t={};for(let[r,o]of Object.entries(e))t[r]=qA.test(r)?"[REDACTED]":Te(o);return t}function pt(e,t=new Date){if(!e)return;let r=new Date(e).getTime();return Number.isFinite(r)?Math.max(0,Math.floor((t.getTime()-r)/1000)):void 0}function Zd(e){let t=Number(e);return Number.isFinite(t)&&t>=0?t:void 0}function VA(e){let t=String(e?.message??e);if(/(?:unauthorized|accessdenied|forbidden|credentials)/i.test(t))return"unauthorized";if(/(?:timeout|unreachable|econnrefused|enotfound|network)/i.test(t))return"unreachable";return"error"}async function el(e,t={},r={}){let o=t.now??new Date,m=Math.max(100,r.timeoutMs??1e4),h=Math.max(1,r.staleAfterSeconds??60),I=await Promise.all(e.map(async(A)=>{try{let E=await Promise.race([A.discover({...t,now:o}),new Promise((x,N)=>setTimeout(()=>N(Error(`Runtime source ${A.id} timed out after ${m}ms`)),m))]);return{workloads:E,source:{id:A.id,provider:A.provider,status:"fresh",discoveredAt:o.toISOString(),staleAfterSeconds:h,itemCount:E.length}}}catch(E){return{workloads:[],source:{id:A.id,provider:A.provider,status:VA(E),discoveredAt:o.toISOString(),staleAfterSeconds:h,itemCount:0,message:String(E?.message??E).slice(0,500)}}}})),R=I.map((A)=>A.source);return{generatedAt:o.toISOString(),workloads:I.flatMap((A)=>A.workloads).sort((A,E)=>A.name.localeCompare(E.name)||A.id.localeCompare(E.id)),sources:R,degraded:R.some((A)=>A.status!=="fresh")}}function Ma(e){return e?.split("/").at(-1)??e?.split(":").at(-1)??"unknown"}function tl(e,t,r,o="ecs:aws",m=[]){let h=r.now??new Date;return e.map((I)=>{let R=I.serviceName??Ma(I.serviceArn),A=t.filter((se)=>!I.clusterArn||se.clusterArn===I.clusterArn),E=A.some((se)=>!!se.group)?A.filter((se)=>se.group===`service:${R}`):A,x=I.status==="ACTIVE"?(I.runningCount??0)<(I.desiredCount??0)?"degraded":"running":ve(I.status),N=m.find((se)=>se.taskDefinitionArn===I.taskDefinition),F=Array.isArray(N?.containerDefinitions)?N.containerDefinitions:[],z=[...new Set(F.map((se)=>se.logConfiguration?.options?.["awslogs-group"]).filter((se)=>typeof se==="string"))],K=F.flatMap((se)=>(se.mountPoints??[]).map((ye)=>({source:ye.sourceVolume,target:ye.containerPath,type:"volume",readOnly:!!ye.readOnly}))),J=F.flatMap((se)=>(se.portMappings??[]).map((ye,ne)=>({id:`${se.name??"container"}:${ne}`,name:se.name,mode:N?.networkMode,ports:[{container:ye.containerPort,host:ye.hostPort,protocol:ye.protocol}]}))),re=r.project&&r.environment&&R.startsWith(`${r.project}-${r.environment}-`)?R.slice(`${r.project}-${r.environment}-`.length):R;return{id:ce("ecs",o,I.serviceArn??R),provider:"ecs",kind:"service",name:R,status:x,rawStatus:I.status,health:x==="running"?"healthy":x==="failed"||x==="degraded"?"unhealthy":"unknown",desiredReplicas:I.desiredCount,runningReplicas:I.runningCount,image:F[0]?.image,runtime:I.launchType??"ECS",version:Ma(I.taskDefinition),ageSeconds:pt(I.deployments?.[0]?.createdAt,h),restartCount:0,tags:{},links:{project:r.project,environment:r.environment,service:re,release:Ma(I.taskDefinition),providerId:I.serviceArn},replicas:E.map((se)=>{let ye=ve(se.lastStatus);return{id:ce("ecs",o,se.taskArn??Ma(se.taskArn)),name:Ma(se.taskArn),status:ye,rawStatus:se.lastStatus,createdAt:se.createdAt,startedAt:se.startedAt,stoppedAt:se.stoppedAt,host:se.containerInstanceArn,containers:(se.containers??[]).map((ne)=>({id:ne.containerArn??ne.name??"container",name:ne.name??"container",image:F.find((fe)=>fe.name===ne.name)?.image,imageDigest:ne.imageDigest,status:ve(ne.lastStatus),rawStatus:ne.lastStatus,exitCode:ne.exitCode,reason:ne.reason,runtime:"ecs"}))}}),networks:J,mounts:K,capabilities:ut(["start","stop","restart","redeploy","scale","inspect",...z.length?["logs"]:[]],"This ECS runtime does not expose exec or file transfer; logs require an awslogs task-definition driver"),config:Te({clusterArn:I.clusterArn,taskDefinition:I.taskDefinition,launchType:I.launchType,pendingCount:I.pendingCount,deployments:I.deployments,logGroups:z,executionRoleArn:N?.executionRoleArn,taskRoleArn:N?.taskRoleArn,cpu:N?.cpu,memory:N?.memory}),discoveredAt:h.toISOString(),sourceId:o}})}function rl(e,t,r="lambda:aws"){let o=t.now??new Date;return e.map((m)=>{let h=m.FunctionName??Ma(m.FunctionArn),I=m.State==="Active"?"running":ve(m.State);return{id:ce("lambda",r,m.FunctionArn??h),provider:"lambda",kind:"function",name:h,status:I,rawStatus:m.State,health:I==="running"?"healthy":I==="failed"?"unhealthy":"unknown",desiredReplicas:void 0,runningReplicas:void 0,image:void 0,runtime:m.Runtime,version:m.Version,architecture:m.Architectures?.join(", "),ageSeconds:pt(m.LastModified,o),restartCount:0,tags:{},links:{project:t.project,environment:t.environment,service:h,providerId:m.FunctionArn},resources:{memoryLimitBytes:m.MemorySize==null?void 0:m.MemorySize*1024*1024},replicas:[],networks:[],mounts:[],capabilities:ut(["logs","inspect"],"Lambda has no persistent process to start, stop, restart, exec, or browse files; deploy a release to replace code"),config:Te({handler:m.Handler,timeout:m.Timeout,codeSize:m.CodeSize,stateReason:m.StateReason,environmentKeys:Object.keys(m.Environment?.Variables??{})}),discoveredAt:o.toISOString(),sourceId:r}})}function tm(e){let t=e.trim();if(!t)return[];let r=JSON.parse(t);if(!Array.isArray(r))throw Error("Docker inspect output must be an array");return r.filter((o)=>o&&typeof o.Id==="string")}function WA(e){let t=Object.entries(e.NetworkSettings?.Ports??{}).flatMap(([r,o])=>{let[m,h]=r.split("/");return(o??[]).map((I)=>({container:Number(m)||void 0,host:Number(I.HostPort)||void 0,protocol:h,address:I.HostIp}))});return Object.entries(e.NetworkSettings?.Networks??{}).map(([r,o])=>({id:o.NetworkID||r,name:r,mode:e.HostConfig?.NetworkMode,addresses:[o.IPAddress,o.GlobalIPv6Address].filter(Boolean),ports:t}))}function HA(e){return(e.Mounts??[]).filter((t)=>!!t.Destination).map((t)=>({source:t.Source,target:t.Destination,type:t.Type,readOnly:t.RW===!1}))}function rm(e,t,r=t.server??"local"){let o=t.now??new Date;return e.map((m)=>{let h=m.State?.OOMKilled?"oomkilled":m.State?.Restarting?"starting":m.State?.Status,I=ve(h),R=(m.Name??m.Id.slice(0,12)).replace(/^\//,""),A=m.Config?.Labels??{},E=HA(m),x=WA(m),N={id:m.Id,name:R,image:m.Config?.Image,imageDigest:m.Image,runtime:"docker",status:I,rawStatus:h,exitCode:m.State?.ExitCode,reason:m.State?.Error,resources:{memoryLimitBytes:m.HostConfig?.Memory||void 0},networks:x,mounts:E};return{id:ce("docker",r,m.Id),provider:"docker",kind:"container",name:R,status:I,rawStatus:h,health:I==="running"?"healthy":I==="failed"?"unhealthy":"unknown",desiredReplicas:1,runningReplicas:I==="running"?1:0,image:m.Config?.Image,runtime:"docker",ageSeconds:pt(m.State?.StartedAt,o),restartCount:m.RestartCount??0,tags:A,links:{project:t.project,environment:t.environment,server:t.server??r.replace(/^docker:/,""),service:A["com.docker.compose.service"]??A["ts-cloud.service"]??R,release:A["ts-cloud.release"],providerId:m.Id},resources:N.resources,replicas:[{id:ce("docker",r,`${m.Id}:0`),name:R,status:I,rawStatus:h,startedAt:m.State?.StartedAt,stoppedAt:m.State?.FinishedAt,restartCount:m.RestartCount,containers:[N],resources:N.resources}],networks:x,mounts:E,capabilities:ut(["start","stop","restart","logs","exec","inspect","files"],"Use the owning Compose service for redeploy and scale"),config:Te({image:m.Config?.Image,labels:A,environmentKeys:(m.Config?.Env??[]).map((F)=>F.split("=",1)[0]),networkMode:m.HostConfig?.NetworkMode}),discoveredAt:o.toISOString(),sourceId:r}})}class vo{inspect;id;provider="docker";constructor(e,t="docker:local"){this.inspect=e;this.id=t}async discover(e){return rm(tm(await this.inspect(e)),e,this.id)}}function nm(e){let t=[];for(let r of e.split(`
|
|
169
|
+
`)){if(!r.startsWith("TSCLOUD_SYSTEMD\t"))continue;let[,o,m,h,I,R,A,E,x,N,F,z,K,J]=r.split("\t");if(!o||!/^[A-Za-z0-9_.@:-]+\.service$/.test(o))continue;t.push({unit:o,load:m,active:h,sub:I,description:R,enabled:A,pid:Number(E)||void 0,memoryBytes:Zd(x),restartCount:Number(N)||0,activeSince:F||void 0,fragmentPath:z||void 0,workingDirectory:K?.startsWith("/")?K:void 0,user:J||void 0})}return t}function im(e,t,r=t.server??"local"){let o=t.now??new Date;return e.map((m)=>{let h=ve(m.active==="active"?m.sub||m.active:m.active),I=m.unit.replace(/\.service$/,""),R=/^(.*?) (?:release [^ ]+|queue worker|daemon |scheduler)(?: |$)/.exec(m.description??"")?.[1]??(t.project&&I.startsWith(`${t.project}-`)?I.slice(t.project.length+1).split("@",1)[0]:void 0),A=I.includes("@")?I.slice(I.indexOf("@")+1):void 0,E=m.workingDirectory?[{target:m.workingDirectory,type:"working-directory"}]:[],x=["start","stop","restart","logs","exec","inspect",...E.length?["files"]:[]];return{id:ce("systemd",r,m.unit),provider:"systemd",kind:"service",name:I,displayName:m.description||I,status:h,rawStatus:[m.load,m.active,m.sub].filter(Boolean).join("/"),health:h==="running"?"healthy":h==="failed"?"unhealthy":"unknown",desiredReplicas:1,runningReplicas:h==="running"?1:0,ageSeconds:pt(m.activeSince,o),restartCount:m.restartCount,tags:{enabled:m.enabled??"unknown"},links:{project:t.project,environment:t.environment,server:t.server??r.replace(/^systemd:/,""),service:R,release:A,providerId:m.unit},resources:{memoryBytes:m.memoryBytes},replicas:[{id:ce("systemd",r,`${m.unit}:main`),name:"main",status:h,pid:m.pid,startedAt:m.activeSince,restartCount:m.restartCount,resources:{memoryBytes:m.memoryBytes}}],networks:[],mounts:E,capabilities:ut(x,E.length?"This systemd runtime does not support redeploy or replica scaling through the scoped explorer":"File transfer requires a provider-reported service working directory; redeploy and scaling are unavailable"),config:Te({fragmentPath:m.fragmentPath,workingDirectory:m.workingDirectory,user:m.user,environment:m.environment,enabled:m.enabled}),discoveredAt:o.toISOString(),sourceId:r}})}class To{read;id;provider="systemd";constructor(e,t="systemd:local"){this.read=e;this.id=t}async discover(e){return im(nm(await this.read(e)),e,this.id)}}function KA(e,t){if(!e.success)throw Error(e.error||e.perInstance?.[0]?.error||`${t} discovery failed`);return e.perInstance?.[0]?.output??""}function JA(){return["set -uo pipefail",'command -v systemctl >/dev/null 2>&1 || { echo "systemctl is unavailable" >&2; exit 127; }',"systemctl list-units --type=service --all --plain --no-legend 2>/dev/null | awk '{print $1}' | while IFS= read -r unit; do",' [ -n "$unit" ] || continue',' clean() { printf "%s" "$1" | tr "\\t\\r\\n" " "; }',' load=$(systemctl show "$unit" -p LoadState --value 2>/dev/null || true)',' active=$(systemctl show "$unit" -p ActiveState --value 2>/dev/null || true)',' sub=$(systemctl show "$unit" -p SubState --value 2>/dev/null || true)',' description=$(systemctl show "$unit" -p Description --value 2>/dev/null || true)',' enabled=$(systemctl is-enabled "$unit" 2>/dev/null || true)',' pid=$(systemctl show "$unit" -p MainPID --value 2>/dev/null || true)',' memory=$(systemctl show "$unit" -p MemoryCurrent --value 2>/dev/null || true)',' restarts=$(systemctl show "$unit" -p NRestarts --value 2>/dev/null || true)',' since=$(systemctl show "$unit" -p ActiveEnterTimestamp --value 2>/dev/null || true)',' fragment=$(systemctl show "$unit" -p FragmentPath --value 2>/dev/null || true)',' working=$(systemctl show "$unit" -p WorkingDirectory --value 2>/dev/null || true)',' user=$(systemctl show "$unit" -p User --value 2>/dev/null || true)',' printf "TSCLOUD_SYSTEMD\\t%s\\t%s\\t%s\\t%s\\t%s\\t%s\\t%s\\t%s\\t%s\\t%s\\t%s\\t%s\\t%s\\n" "$(clean "$unit")" "$(clean "$load")" "$(clean "$active")" "$(clean "$sub")" "$(clean "$description")" "$(clean "$enabled")" "$(clean "$pid")" "$(clean "$memory")" "$(clean "$restarts")" "$(clean "$since")" "$(clean "$fragment")" "$(clean "$working")" "$(clean "$user")"',"done"]}function GA(){return["set -uo pipefail",'if ! command -v docker >/dev/null 2>&1; then echo "[]"; exit 0; fi',"ids=$(docker ps -aq)",'if [ -z "$ids" ]; then echo "[]"; else docker inspect $ids; fi']}function Bv(e,t,r,o,m){let h=`${e}:${r.id}`,I=async()=>KA(await t.runRemoteDeploy({targets:[r],commands:e==="systemd"?JA():GA(),timeoutSeconds:30,comment:`ts-cloud runtime discovery:${e}`,tags:{Project:o.project.slug,Environment:m,Role:"app"}}),h);return e==="systemd"?new To(I,h):new vo(I,h)}async function hu(e,t=100){let r=[],o;for(let m=0;m<t;m++){let h=await e(o);if(r.push(...h.items),!h.nextToken)return r;o=h.nextToken}throw Error(`Provider pagination exceeded ${t} pages`)}class nl{client;prefix;id="ecs:aws";provider="ecs";constructor(e,t){this.client=e;this.prefix=t}async discover(e){let t=await hu(async(h)=>{let I=await this.client.listClusters({nextToken:h,maxResults:100});return{items:I.clusterArns??[],nextToken:I.nextToken}}),r=[],o=[],m=[];for(let h of t){let I=(await hu(async(R)=>{let A=await this.client.listServices(h,{nextToken:R,maxResults:100});return{items:A.serviceArns??[],nextToken:A.nextToken}})).filter((R)=>R.split("/").at(-1)?.startsWith(this.prefix));for(let R=0;R<I.length;R+=10){let A=await this.client.describeServices({cluster:h,services:I.slice(R,R+10)});r.push(...A.services??[])}for(let R of r.filter((A)=>A.clusterArn===h||!A.clusterArn)){let A=await hu(async(E)=>{let x=await this.client.listTasks(h,R.serviceName,{nextToken:E,maxResults:100});return{items:x.taskArns??[],nextToken:x.nextToken}});for(let E=0;E<A.length;E+=100){let x=await this.client.describeTasks(h,A.slice(E,E+100));o.push(...x.tasks??[])}}}if(this.client.describeTaskDefinition)for(let h of[...new Set(r.map((I)=>I.taskDefinition).filter((I)=>!!I))]){let I=await this.client.describeTaskDefinition(h);if(I.taskDefinition)m.push(I.taskDefinition)}return tl(r,o,e,this.id,m)}}class il{client;prefix;id="lambda:aws";provider="lambda";constructor(e,t){this.client=e;this.prefix=t}async discover(e){let t=(await hu(async(r)=>{let o=await this.client.listFunctions({Marker:r,MaxItems:50});return{items:o.Functions??[],nextToken:o.NextMarker}})).filter((r)=>r.FunctionName?.startsWith(this.prefix));return rl(t,e,this.id)}}async function sm(e,t,r={}){let o=[],m=O(e),h=ae(e),I=e.aws?.profile,R=e.aws?.region??e.project.region??"us-east-1",A=`${e.project.slug}-${t}`;if(m!=="serverless"){let E=r.driver??k({config:e}),x=await E.findComputeTargets({slug:e.project.slug,environment:t,role:"app"});for(let N of x)o.push(Bv("systemd",E,N,e,t)),o.push(Bv("docker",E,N,e,t))}if(h==="aws")o.push(new nl(r.ecs??new ze(R,I),A)),o.push(new il(r.lambda??new C(R,I),A));return o}async function ee(e,t,r={}){let o={project:e.project.slug,environment:t};try{return await el(await sm(e,t,r),o)}catch(m){return{generatedAt:new Date().toISOString(),workloads:[],sources:[{id:"runtime:bootstrap",provider:ae(e)==="aws"?"ecs":"systemd",status:"error",discoveredAt:new Date().toISOString(),staleAfterSeconds:60,itemCount:0,message:String(m?.message??m).slice(0,500)}],degraded:!0}}}var Lv=65536,hc=49152,vu=/^[A-Za-z0-9_.@:-]+\.service$/,bu=/^[a-fA-F0-9]{12,128}$/,QA=["/boot","/dev","/etc","/proc","/root","/run","/sys","/var/run"];function Ba(e){let t=Buffer.from(e);return t.byteLength<=Lv?e:`${t.subarray(0,Lv-24).toString("utf8")}
|
|
170
|
+
[output truncated]`}function yu(e){return{success:!1,instanceCount:0,perInstance:[],error:e instanceof Error?e.message:String(e)}}function zn(e){return`'${e.replaceAll("'",`'"'"'`)}'`}function YA(e,t){return Number.isFinite(e)?Math.min(1e4,Math.max(0,Math.floor(e))):t}function ZA(e){let t=e.config,r=typeof t.clusterArn==="string"?t.clusterArn:void 0,o=e.links.providerId||e.name;return r&&o?{cluster:r,service:o}:void 0}function XA(e,t){return e.find((r)=>r.id===t.links.server)??(e.length===1?e[0]:void 0)}function eC(e,t){if(e.provider==="systemd"){let r=e.links.providerId;if(!r||!vu.test(r)||!["start","stop","restart"].includes(t))return;return[`systemctl ${t} ${r}`,`systemctl is-active ${r} 2>/dev/null || true`]}if(e.provider==="docker"){let r=e.links.providerId;if(!r||!bu.test(r)||!["start","stop","restart"].includes(t))return;return[`docker ${t} ${r}`,`docker inspect --format '{{.State.Status}}' ${r}`]}return}function om(e,t){if(!t.startsWith("/")||t.includes("\x00"))return{ok:!1,error:"An absolute path is required."};let r=[];for(let R of t.split("/")){if(!R||R===".")continue;if(R===".."){if(!r.length)return{ok:!1,error:"Path traversal is not allowed."};r.pop()}else r.push(R)}let o=`/${r.join("/")}`;if(QA.some((R)=>o===R||o.startsWith(`${R}/`)))return{ok:!1,error:"Sensitive host paths are not available through runtime file access."};let m=e.links.project?.replace(/[^A-Za-z0-9_-]/g,""),h=e.links.service?.replace(/[^A-Za-z0-9_-]/g,"");if(![...m&&h?[`/var/www/${m}/${h}`,`/srv/${m}/${h}`]:[],...e.mounts.map((R)=>R.target).filter((R)=>R.startsWith("/"))].some((R)=>o===R||o.startsWith(`${R.replace(/\/$/,"")}/`)))return{ok:!1,error:"Path is outside this service’s owned roots."};return{ok:!0,path:o}}function tC(e,t){let r=e.links.project?.replace(/[^A-Za-z0-9_-]/g,""),o=e.links.service?.replace(/[^A-Za-z0-9_-]/g,"");return[...r&&o?[`/var/www/${r}/${o}`,`/srv/${r}/${o}`]:[],...e.mounts.map((m)=>m.target).filter((m)=>m.startsWith("/"))].sort((m,h)=>h.length-m.length).find((m)=>t===m||t.startsWith(`${m.replace(/\/$/,"")}/`))}var sl={process:["ps","-eo","pid,ppid,user,%cpu,%mem,etime,command","--sort=-%cpu"],sockets:["ss","-lntup"],filesystem:["df","-h"]};class de{config;environment;dependencies;region;profile;constructor(e,t,r={}){this.config=e;this.environment=t;this.dependencies=r;this.region=e.aws?.region??e.project.region??"us-east-1",this.profile=e.aws?.profile}async inventory(){return this.dependencies.inventory?.()??ee(this.config,this.environment,{driver:this.dependencies.driver})}async workload(e){return(await this.inventory()).workloads.find((t)=>t.id===e)}async remote(e,t,r,o){let m=this.dependencies.driver??k({config:this.config}),h=await m.findComputeTargets({slug:this.config.project.slug,environment:this.environment,role:"app"}),I=XA(h,e);if(!I)throw Error("The workload server is no longer reachable in this scope.");return m.runRemoteDeploy({targets:[I],commands:t,timeoutSeconds:r,comment:o,tags:{Project:this.config.project.slug,Environment:this.environment,Role:"app"}})}async run(e){let t=await this.workload(e.workloadId),r={workloadId:e.workloadId,action:e.action};if(!t)return{...r,ok:!1,error:"Workload was not found in the current project and environment."};let o=t.capabilities[e.action];if(!o.supported)return{...r,ok:!1,error:o.reason??`${e.action} is unsupported for ${t.provider}.`};if(o.requiresConfirmation&&e.confirm!==t.name)return{...r,ok:!1,error:`Type "${t.name}" to ${e.action} this workload.`};if(o.requiresRecentAuth&&!e.recentAuth)return{...r,ok:!1,error:"Sign in again before using elevated runtime access."};if(e.action==="inspect")return{...r,ok:!0,workload:t};if(t.provider==="systemd"||t.provider==="docker"){let m=eC(t,e.action);if(!m)return{...r,ok:!1,error:`${e.action} is not implemented for ${t.provider}.`};let h=await this.remote(t,["set -uo pipefail",...m],60,`ts-cloud runtime:${e.action}`).catch(yu);return{...r,ok:h.success,command:m[0],stdout:Ba(h.perInstance?.[0]?.output??""),stderr:Ba(h.perInstance?.[0]?.error??h.error??""),error:h.success?void 0:h.error??"Runtime operation failed."}}if(t.provider==="ecs"){let m=ZA(t);if(!m)return{...r,ok:!1,error:"ECS cluster or service identity is unavailable."};let h=this.dependencies.ecs??new ze(this.region,this.profile);if(e.action==="scale"){let I=YA(e.replicas,t.desiredReplicas??1);return await h.scaleService(m.cluster,m.service,I),{...r,ok:!0,command:`ecs scale ${t.name} ${I}`}}if(e.action==="start"||e.action==="stop"){let I=e.action==="stop"?0:Math.max(1,t.desiredReplicas??1);return await h.scaleService(m.cluster,m.service,I),{...r,ok:!0,command:`ecs scale ${t.name} ${I}`}}if(e.action==="restart"||e.action==="redeploy")return await h.forceNewDeployment(m.cluster,m.service),{...r,ok:!0,command:`ecs redeploy ${t.name}`}}return{...r,ok:!1,error:`${e.action} is not implemented for ${t.provider}.`}}async exec(e){let t={workloadId:e.workloadId,preset:e.preset},r=await this.workload(e.workloadId);if(!r)return{...t,ok:!1,error:"Workload was not found in the current project and environment."};if(process.env.TS_CLOUD_RUNTIME_EXEC==="0")return{...t,ok:!1,error:"Runtime exec is disabled by TS_CLOUD_RUNTIME_EXEC."};if(!r.capabilities.exec.supported)return{...t,ok:!1,error:r.capabilities.exec.reason??"Exec is unsupported for this workload."};if(e.confirm!==r.name)return{...t,ok:!1,error:`Type "${r.name}" to open exec access.`};if(!e.recentAuth)return{...t,ok:!1,error:"Sign in again before using elevated runtime access."};let o=e.preset&&sl[e.preset];if(!o&&!e.command?.trim())return{...t,ok:!1,error:"Choose a diagnostic preset or provide a command."};if(e.command&&r.provider!=="docker")return{...t,ok:!1,error:"Free-form exec is restricted to isolated containers; use a diagnostic preset for system services."};let m=r.links.providerId??"",h;if(r.provider==="docker"&&bu.test(m)){let R=e.command?.trim()||o.map(zn).join(" ");h=`docker exec ${m} sh -lc ${zn(R)}`}else if(r.provider==="systemd"&&vu.test(m)&&e.preset)h=e.preset==="process"?`systemctl status ${m} --no-pager --full`:sl[e.preset].map(zn).join(" ");if(!h)return{...t,ok:!1,error:"Exec target identity is invalid or unsupported."};let I=await this.remote(r,["set -uo pipefail",h],60,"ts-cloud runtime:exec").catch(yu);return{...t,ok:I.success,command:h,stdout:Ba(I.perInstance?.[0]?.output??""),stderr:Ba(I.perInstance?.[0]?.error??I.error??""),error:I.success?void 0:I.error??"Exec command failed."}}async readFile(e){let t={workloadId:e.workloadId,path:e.path},r=await this.fileTarget(e);if("error"in r)return{...t,ok:!1,error:r.error};let{workload:o,path:m,root:h,providerId:I}=r,A=`sh -c ${zn('resolved=$(realpath -m "$1"); root=$(realpath -m "$3"); case "$resolved" in "$root"|"$root"/*) ;; *) exit 73;; esac; test -f "$resolved"; size=$(wc -c < "$resolved"); printf "__TSCLOUD_SIZE__=%s\\n" "$size"; head -c "$2" "$resolved" | base64')} sh ${zn(m)} ${hc} ${zn(h)}`,E=o.provider==="docker"?`docker exec ${I} ${A}`:A,x=await this.remote(o,[E],30,"ts-cloud runtime:file-read").catch(yu);if(!x.success)return{...t,ok:!1,error:x.error??x.perInstance?.[0]?.error??"File read failed."};let N=x.perInstance?.[0]?.output??"",F=/__TSCLOUD_SIZE__=(\d+)\r?\n/.exec(N);if(!F)return{...t,ok:!1,error:"The remote file response was invalid."};let z=Number(F[1]),K=N.slice((F.index??0)+F[0].length).replace(/\s+/g,"");if(Buffer.from(K,"base64").byteLength>hc)return{...t,ok:!1,error:"The remote file exceeded the transfer limit."};return{...t,ok:!0,contentBase64:K,size:z,truncated:z>hc}}async writeFile(e){let t={workloadId:e.workloadId,path:e.path},r=await this.fileTarget(e);if("error"in r)return{...t,ok:!1,error:r.error};if(!/^[A-Za-z0-9+/]*={0,2}$/.test(e.contentBase64))return{...t,ok:!1,error:"File content must be valid base64."};let o=Buffer.from(e.contentBase64,"base64");if(o.toString("base64")!==e.contentBase64)return{...t,ok:!1,error:"File content must be canonical base64."};if(o.byteLength>hc)return{...t,ok:!1,error:`File uploads are limited to ${hc} bytes.`};let{workload:m,path:h,root:I,providerId:R}=r,E=`sh -c ${zn('resolved=$(realpath -m "$1"); root=$(realpath -m "$2"); case "$resolved" in "$root"|"$root"/*) ;; *) exit 73;; esac; mkdir -p "$(dirname "$resolved")"; tmp="$resolved.ts-cloud-$$"; base64 -d > "$tmp"; chmod 600 "$tmp"; mv "$tmp" "$resolved"')} sh ${zn(h)} ${zn(I)}`,x=m.provider==="docker"?`printf %s ${zn(e.contentBase64)} | docker exec -i ${R} ${E}`:`printf %s ${zn(e.contentBase64)} | ${E}`,N=await this.remote(m,[x],30,"ts-cloud runtime:file-write").catch(yu);return{...t,ok:N.success,size:o.byteLength,truncated:!1,error:N.success?void 0:N.error??N.perInstance?.[0]?.error??"File write failed."}}async fileTarget(e){let t=await this.workload(e.workloadId);if(!t)return{error:"Workload was not found in the current project and environment."};if(process.env.TS_CLOUD_RUNTIME_FILE_ACCESS==="0")return{error:"Runtime file access is disabled by TS_CLOUD_RUNTIME_FILE_ACCESS."};if(!t.capabilities.files.supported)return{error:t.capabilities.files.reason??"File access is unsupported for this workload."};if(e.confirm!==t.name)return{error:`Type "${t.name}" to access workload files.`};if(!e.recentAuth)return{error:"Sign in again before using elevated runtime access."};let r=om(t,e.path);if(!r.ok)return{error:r.error};let o=tC(t,r.path),m=t.links.providerId??"";if(!o||t.provider==="systemd"&&!vu.test(m)||t.provider==="docker"&&!bu.test(m))return{error:"File target identity is invalid or unsupported."};return{workload:t,path:r.path,root:o,providerId:m}}async logs(e,t={}){let r=await this.workload(e);if(!r)throw Error("Workload was not found in the current project and environment.");if(!r.capabilities.logs.supported)throw Error(r.capabilities.logs.reason??"Logs are unsupported for this workload.");let o=Math.min(1000,Math.max(1,Math.floor(t.limit??200)));if(r.provider==="lambda"||r.provider==="ecs"){let x=this.dependencies.logs??new ue(this.region,this.profile),N=r.config.logGroups,F=r.provider==="lambda"?`/aws/lambda/${r.name}`:Array.isArray(N)&&typeof N[0]==="string"?N[0]:void 0;if(!F)throw Error("The ECS task definition does not declare an awslogs group.");let z=await x.filterLogEvents({logGroupName:F,startTime:t.since?.getTime(),limit:o}),K=(z.events??[]).slice(0,o).map((J)=>({timestamp:J.timestamp==null?void 0:new Date(J.timestamp).toISOString(),message:Ba(String(J.message??""))}));return{workloadId:e,provider:r.provider,lines:K,nextCursor:z.nextToken,truncated:(z.events?.length??0)>=o}}let m=r.links.providerId??"",h=t.since&&Number.isFinite(t.since.getTime())?t.since:void 0,I=r.provider==="systemd"&&vu.test(m)?`journalctl -u ${m} --no-pager -n ${o} -o short-iso${h?` --since ${zn(h.toISOString())}`:""}`:r.provider==="docker"&&bu.test(m)?`docker logs --timestamps --tail ${o}${h?` --since ${Math.floor(h.getTime()/1000)}`:""} ${m} 2>&1`:void 0;if(!I)throw Error("Log target identity is invalid.");let R=await this.remote(r,[I],30,"ts-cloud runtime:logs");if(!R.success)throw Error(R.error??R.perInstance?.[0]?.error??"Log read failed.");let A=Ba(R.perInstance?.[0]?.output??""),E=A.split(`
|
|
171
|
+
`).filter(Boolean).slice(-o).map((x)=>{let N=/^(\d{4}-\d{2}-\d{2}T\S+)\s+(.*)$/.exec(x);return{timestamp:N?.[1],message:N?.[2]??x}});return{workloadId:e,provider:r.provider,lines:E,truncated:A.includes("[output truncated]")||E.length>=o}}}import{randomUUID as rC}from"node:crypto";class wo{records=new Map;maxBufferBytes;ttlMs;now;constructor(e={}){this.maxBufferBytes=Math.max(1024,e.maxBufferBytes??262144),this.ttlMs=Math.max(1000,e.ttlMs??900000),this.now=e.now??(()=>new Date)}create(e,t){this.sweep();let r=this.now().toISOString(),o={id:rC(),workloadId:t,kind:e,state:"open",chunks:[],cursor:0,droppedBytes:0,bufferedBytes:0,createdAt:r,updatedAt:r,abort:new AbortController,listeners:new Set};return this.records.set(o.id,o),this.snapshot(o,0)}signal(e){return this.records.get(e)?.abort.signal}append(e,t,r="stdout"){let o=this.records.get(e);if(!o||o.state!=="open"||!t)return o?this.snapshot(o,o.cursor):void 0;let m=Buffer.from(t),h=m.byteLength>this.maxBufferBytes?m.subarray(m.byteLength-this.maxBufferBytes).toString("utf8"):t,I={cursor:++o.cursor,stream:r,data:h,at:this.now().toISOString()};o.chunks.push(I),o.bufferedBytes+=Buffer.byteLength(h),o.updatedAt=I.at;while(o.bufferedBytes>this.maxBufferBytes&&o.chunks.length>1){let R=o.chunks.shift(),A=Buffer.byteLength(R.data);o.bufferedBytes-=A,o.droppedBytes+=A}return this.publish(o),this.snapshot(o,I.cursor-1)}read(e,t,r=0){let o=this.records.get(e);if(!o||o.workloadId!==t)return;return o.updatedAt=this.now().toISOString(),this.snapshot(o,Math.max(0,Math.floor(r)))}close(e,t="complete",r){let o=this.records.get(e);if(!o||o.state!=="open")return o?this.snapshot(o,o.cursor):void 0;if(o.state=t,o.error=r,o.updatedAt=this.now().toISOString(),t==="cancelled")o.abort.abort();return this.publish(o),this.snapshot(o,o.cursor)}cancel(e,t){let r=this.records.get(e);if(!r||r.workloadId!==t)return;return this.close(e,"cancelled")}subscribe(e,t,r){let o=this.records.get(e);if(!o||o.workloadId!==t)return;return o.listeners.add(r),()=>o.listeners.delete(r)}sweep(){let e=this.now().getTime()-this.ttlMs,t=0;for(let[r,o]of this.records){if(new Date(o.updatedAt).getTime()>e)continue;o.abort.abort(),this.records.delete(r),t++}return t}clear(){for(let e of this.records.values())e.abort.abort();this.records.clear()}snapshot(e,t){let r=e.chunks[0]?.cursor??e.cursor+1,o=t>0&&t<r-1;return{id:e.id,workloadId:e.workloadId,kind:e.kind,state:e.state,chunks:e.chunks.filter((m)=>o||m.cursor>t),cursor:e.cursor,reset:o,droppedBytes:e.droppedBytes,createdAt:e.createdAt,updatedAt:e.updatedAt,error:e.error}}publish(e){let t=this.snapshot(e,0);for(let r of e.listeners)r(t)}}import{createHash as nC}from"node:crypto";var cg={info:0,low:1,medium:2,high:3,critical:4},dg={record:0,warn:1,block:2},Fv=new Set(["skipped","unavailable","unsupported","stale"]);function vr(e){return typeof e==="string"?e:void 0}function ms(e,t){try{return JSON.parse(String(e))}catch{return t}}function Yn(e){return JSON.stringify(s(e))}function iC(e){if(!e)return;return String(s(e,2000))}function og(e){return{id:String(e.id),organizationId:String(e.organization_id),projectId:vr(e.project_id),environmentId:vr(e.environment_id),resourceId:vr(e.resource_id),releaseId:vr(e.release_id),scannerId:String(e.scanner_id),scannerVersion:String(e.scanner_version),status:String(e.status),error:vr(e.error),metadata:ms(e.metadata,{}),findingsCount:Number(e.findings_count),startedAt:String(e.started_at),completedAt:String(e.completed_at),durationMs:Number(e.duration_ms)}}function zv(e){return{id:String(e.id),fingerprint:String(e.fingerprint),organizationId:String(e.organization_id),projectId:vr(e.project_id),environmentId:vr(e.environment_id),resourceId:vr(e.resource_id),releaseId:vr(e.release_id),scanRunId:String(e.scan_run_id),scannerId:String(e.scanner_id),scannerVersion:String(e.scanner_version),ruleId:String(e.rule_id),severity:String(e.severity),title:String(e.title),description:String(e.description),evidence:ms(e.evidence,{}),remediation:vr(e.remediation),subject:String(e.subject),status:String(e.status),ownerActorId:vr(e.owner_actor_id),recurrenceCount:Number(e.recurrence_count),firstSeenAt:String(e.first_seen_at),lastSeenAt:String(e.last_seen_at),resolvedAt:vr(e.resolved_at),updatedAt:String(e.updated_at)}}function sg(e){return{id:String(e.id),organizationId:String(e.organization_id),environmentId:vr(e.environment_id),name:String(e.name),rules:ms(e.rules,[]),requiredScanners:ms(e.required_scanners,[]),scannerFailMode:String(e.scanner_fail_mode),enabled:Number(e.enabled)===1,version:Number(e.version),createdByActorId:vr(e.created_by_actor_id),createdAt:String(e.created_at),updatedAt:String(e.updated_at)}}function Uv(e,t){let r=vr(e.revoked_at),o=String(e.expires_at);return{id:String(e.id),findingId:String(e.finding_id),policyId:vr(e.policy_id),reason:String(e.reason),referenceUrl:vr(e.reference_url),createdByActorId:String(e.created_by_actor_id),expiresAt:o,revokedAt:r,state:r?"revoked":o<=t?"expired":"active",createdAt:String(e.created_at),updatedAt:String(e.updated_at)}}function ag(e){return{id:String(e.id),organizationId:String(e.organization_id),projectId:String(e.project_id),environmentId:vr(e.environment_id),releaseId:String(e.release_id),kind:String(e.kind),format:String(e.format),digest:String(e.digest),summary:ms(e.summary,{}),content:vr(e.content),sensitive:Number(e.sensitive)===1,createdAt:String(e.created_at)}}function qv(e){return{id:String(e.id),organizationId:String(e.organization_id),projectId:String(e.project_id),environmentId:String(e.environment_id),operationId:vr(e.operation_id),policyId:String(e.policy_id),policyVersion:Number(e.policy_version),outcome:String(e.outcome),scannerVersions:ms(e.scanner_versions,{}),findingIds:ms(e.finding_ids,[]),waiverIds:ms(e.waiver_ids,[]),explanation:String(e.explanation),createdAt:String(e.created_at)}}function oC(e,t){let r=[e.trim().toLowerCase(),t.ruleId.trim().toLowerCase(),t.organizationId,t.projectId??"",t.environmentId??"",t.resourceId??"",t.releaseId??"",t.subject.trim().toLowerCase()].join("\x00");return nC("sha256").update(r).digest("hex")}function Vv(e){if(!e)return;let t=new URL(e);if(t.protocol!=="https:"&&t.protocol!=="http:")throw Error("Security references must use HTTP or HTTPS");return t.toString()}function Wv(e){if(!e.length)throw Error("A security policy needs at least one rule");return e.map((t)=>{if(!(t.minimumSeverity in cg))throw Error(`Unknown security severity: ${t.minimumSeverity}`);if(!(t.action in dg))throw Error(`Unknown security policy action: ${t.action}`);return{minimumSeverity:t.minimumSeverity,action:t.action,...t.scannerId?{scannerId:t.scannerId.trim()}:{}}})}class Fr{controlPlane;nowFn;idFn;constructor(e,t={}){this.controlPlane=e;this.nowFn=t.now??(()=>new Date),this.idFn=t.id??(()=>crypto.randomUUID())}now(){return this.nowFn().toISOString()}run(e,t){this.controlPlane.database.run(e,t)}recordScan(e){if(!e.organizationId||!e.scannerId.trim()||!e.scannerVersion.trim())throw Error("Organization, scanner id, and scanner version are required");let t=e.completedAt??this.now(),r=e.startedAt??t,o=e.durationMs??Math.max(0,new Date(t).getTime()-new Date(r).getTime()),m=e.findings??[],h=this.idFn(),I=new Set,R=[];return this.controlPlane.transaction(()=>{this.run(`INSERT INTO security_scan_runs
|
|
172
|
+
(id, organization_id, project_id, environment_id, resource_id, release_id, scanner_id, scanner_version, status, error, metadata, findings_count, started_at, completed_at, duration_ms)
|
|
173
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,[h,e.organizationId,e.projectId??null,e.environmentId??null,e.resourceId??null,e.releaseId??null,e.scannerId.trim(),e.scannerVersion.trim(),e.status,iC(e.error)??null,Yn(e.metadata??{}),m.length,r,t,Math.max(0,Math.floor(o))]);for(let A of m){let E={...A,organizationId:e.organizationId,projectId:A.projectId??e.projectId,environmentId:A.environmentId??e.environmentId,resourceId:A.resourceId??e.resourceId,releaseId:A.releaseId??e.releaseId},x=E.fingerprint?.trim()||oC(e.scannerId,E);I.add(x);let N=this.controlPlane.database.query("SELECT * FROM security_findings WHERE fingerprint = ?").get(x),F=N?this.controlPlane.database.query("SELECT * FROM security_waivers WHERE finding_id = ? AND revoked_at IS NULL AND expires_at > ? ORDER BY expires_at DESC LIMIT 1").get(String(N.id),t):void 0;if(N){let z=String(N.status)==="resolved"?Number(N.recurrence_count)+1:Number(N.recurrence_count),K=F?"waived":String(N.status)==="acknowledged"?"acknowledged":"open";this.run(`UPDATE security_findings SET scan_run_id = ?, scanner_version = ?, severity = ?, title = ?, description = ?, evidence = ?, remediation = ?,
|
|
174
|
+
status = ?, recurrence_count = ?, last_seen_at = ?, resolved_at = NULL, updated_at = ? WHERE id = ?`,[h,e.scannerVersion,E.severity,E.title.trim(),E.description.trim(),Yn(E.evidence??{}),E.remediation?.trim()||null,K,z,t,t,String(N.id)]),R.push(this.getFinding(String(N.id)))}else{let z=this.idFn();this.run(`INSERT INTO security_findings
|
|
175
|
+
(id, fingerprint, organization_id, project_id, environment_id, resource_id, release_id, scan_run_id, scanner_id, scanner_version, rule_id,
|
|
176
|
+
severity, title, description, evidence, remediation, subject, status, first_seen_at, last_seen_at, updated_at)
|
|
177
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'open', ?, ?, ?)`,[z,x,E.organizationId,E.projectId??null,E.environmentId??null,E.resourceId??null,E.releaseId??null,h,e.scannerId,e.scannerVersion,E.ruleId.trim(),E.severity,E.title.trim(),E.description.trim(),Yn(E.evidence??{}),E.remediation?.trim()||null,E.subject.trim(),t,t,t]),R.push(this.getFinding(z))}}if(e.status==="passed"||e.status==="failed"){let A=this.controlPlane.database.query(`SELECT * FROM security_findings WHERE organization_id = ? AND scanner_id = ?
|
|
178
|
+
AND project_id IS ? AND environment_id IS ? AND resource_id IS ? AND release_id IS ? AND status != 'resolved'`).all(e.organizationId,e.scannerId,e.projectId??null,e.environmentId??null,e.resourceId??null,e.releaseId??null);for(let E of A){if(I.has(String(E.fingerprint)))continue;this.run("UPDATE security_findings SET status = 'resolved', resolved_at = ?, updated_at = ? WHERE id = ?",[t,t,String(E.id)]),this.controlPlane.appendEvent({organizationId:e.organizationId,projectId:e.projectId,resourceId:e.resourceId,type:"security.finding.resolved",payload:{findingId:String(E.id),scannerId:e.scannerId,reason:"not-observed"}})}}this.controlPlane.appendEvent({organizationId:e.organizationId,projectId:e.projectId,resourceId:e.resourceId,type:"security.scan.completed",level:e.status==="failed"||Fv.has(e.status)?"warning":"info",payload:{scanRunId:h,scannerId:e.scannerId,scannerVersion:e.scannerVersion,status:e.status,findingsCount:m.length,durationMs:o}})}),{run:this.getScanRun(h),findings:R.map((A)=>this.getFinding(A.id))}}getScanRun(e){let t=this.controlPlane.database.query("SELECT * FROM security_scan_runs WHERE id = ?").get(e);return t?og(t):void 0}listScanRuns(e){return this.controlPlane.database.query("SELECT * FROM security_scan_runs WHERE organization_id = ? AND project_id IS ? AND environment_id IS ? ORDER BY completed_at DESC, id DESC LIMIT ?").all(e.organizationId,e.projectId??null,e.environmentId??null,Math.min(500,Math.max(1,e.limit??100))).map(og)}getFinding(e){let t=this.controlPlane.database.query("SELECT * FROM security_findings WHERE id = ?").get(e);return t?zv(t):void 0}listFindings(e){this.expireWaivers();let t=["organization_id = ?"],r=[e.organizationId];if(e.projectId)t.push("project_id = ?"),r.push(e.projectId);if(e.environmentId)t.push("environment_id = ?"),r.push(e.environmentId);if(e.status)t.push("status = ?"),r.push(e.status);if(e.severity)t.push("severity = ?"),r.push(e.severity);return r.push(Math.min(1000,Math.max(1,e.limit??250))),this.controlPlane.database.query(`SELECT * FROM security_findings WHERE ${t.join(" AND ")} ORDER BY CASE severity WHEN 'critical' THEN 4 WHEN 'high' THEN 3 WHEN 'medium' THEN 2 WHEN 'low' THEN 1 ELSE 0 END DESC, last_seen_at DESC LIMIT ?`).all(...r).map(zv)}assignFinding(e,t,r){let o=this.getFinding(e);if(!o)throw Error("Security finding was not found");if(t&&!this.controlPlane.getActor(t))throw Error("Finding owner was not found");return this.run("UPDATE security_findings SET owner_actor_id = ?, updated_at = ? WHERE id = ?",[t??null,this.now(),e]),this.controlPlane.appendEvent({organizationId:o.organizationId,projectId:o.projectId,resourceId:o.resourceId,actorId:r,type:"security.finding.assigned",payload:{findingId:e,ownerActorId:t??null}}),this.getFinding(e)}acknowledgeFinding(e,t){let r=this.getFinding(e);if(!r)throw Error("Security finding was not found");if(r.status==="resolved")throw Error("A resolved finding cannot be acknowledged");return this.run("UPDATE security_findings SET status = 'acknowledged', updated_at = ? WHERE id = ?",[this.now(),e]),this.controlPlane.appendEvent({organizationId:r.organizationId,projectId:r.projectId,resourceId:r.resourceId,actorId:t,type:"security.finding.acknowledged",payload:{findingId:e}}),this.getFinding(e)}addComment(e){let t=this.getFinding(e.findingId);if(!t)throw Error("Security finding was not found");let r=e.body.trim();if(r.length<2||r.length>4000)throw Error("Finding comments must contain 2-4,000 characters");let o=Vv(e.referenceUrl),m=this.idFn(),h=this.now();return this.run("INSERT INTO security_finding_comments (id, finding_id, actor_id, body, reference_url, created_at) VALUES (?, ?, ?, ?, ?, ?)",[m,e.findingId,e.actorId,r,o??null,h]),this.controlPlane.appendEvent({organizationId:t.organizationId,projectId:t.projectId,resourceId:t.resourceId,actorId:e.actorId,type:"security.finding.commented",payload:{findingId:e.findingId,commentId:m,referenceUrl:o??null}}),{id:m,findingId:e.findingId,actorId:e.actorId,body:r,referenceUrl:o,createdAt:h}}listComments(e){return this.controlPlane.database.query("SELECT * FROM security_finding_comments WHERE finding_id = ? ORDER BY created_at, id").all(e).map((t)=>({id:String(t.id),findingId:String(t.finding_id),actorId:String(t.actor_id),body:String(t.body),referenceUrl:vr(t.reference_url),createdAt:String(t.created_at)}))}createWaiver(e){let t=this.getFinding(e.findingId);if(!t)throw Error("Security finding was not found");let r=e.reason.trim();if(r.length<8||r.length>1000)throw Error("A waiver reason must contain 8-1,000 characters");let o=this.now(),m=new Date(e.expiresAt);if(!Number.isFinite(m.getTime())||m.toISOString()<=o)throw Error("A waiver must have a future expiry");if(m.getTime()-new Date(o).getTime()>31536000000)throw Error("A waiver cannot last longer than one year");let h=e.policyId?this.getPolicy(e.policyId):void 0;if(e.policyId&&(!h||h.organizationId!==t.organizationId))throw Error("Waiver policy does not match the finding organization");let I=this.idFn(),R=Vv(e.referenceUrl);return this.controlPlane.transaction(()=>{this.run(`INSERT INTO security_waivers (id, finding_id, policy_id, reason, reference_url, created_by_actor_id, expires_at, created_at, updated_at)
|
|
179
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,[I,t.id,e.policyId??null,r,R??null,e.actorId,m.toISOString(),o,o]),this.run("UPDATE security_findings SET status = 'waived', updated_at = ? WHERE id = ?",[o,t.id]),this.controlPlane.appendEvent({organizationId:t.organizationId,projectId:t.projectId,resourceId:t.resourceId,actorId:e.actorId,type:"security.waiver.created",level:"warning",payload:{waiverId:I,findingId:t.id,policyId:e.policyId??null,reason:r,expiresAt:m.toISOString(),referenceUrl:R??null}})}),this.getWaiver(I)}revokeWaiver(e,t){let r=this.getWaiver(e);if(!r)throw Error("Security waiver was not found");if(r.revokedAt)return r;let o=this.getFinding(r.findingId),m=this.now();return this.controlPlane.transaction(()=>{this.run("UPDATE security_waivers SET revoked_at = ?, updated_at = ? WHERE id = ?",[m,m,e]),this.run("UPDATE security_findings SET status = 'open', updated_at = ? WHERE id = ? AND status = 'waived'",[m,r.findingId]),this.controlPlane.appendEvent({organizationId:o.organizationId,projectId:o.projectId,resourceId:o.resourceId,actorId:t,type:"security.waiver.revoked",level:"warning",payload:{waiverId:e,findingId:r.findingId}})}),this.getWaiver(e)}getWaiver(e){let t=this.controlPlane.database.query("SELECT * FROM security_waivers WHERE id = ?").get(e);return t?Uv(t,this.now()):void 0}listWaivers(e){return(e?this.controlPlane.database.query("SELECT * FROM security_waivers WHERE finding_id = ? ORDER BY created_at DESC").all(e):this.controlPlane.database.query("SELECT * FROM security_waivers ORDER BY created_at DESC").all()).map((r)=>Uv(r,this.now()))}expireWaivers(){let e=this.now(),t=this.controlPlane.database.query(`SELECT w.id AS waiver_id, f.* FROM security_waivers w JOIN security_findings f ON f.id = w.finding_id
|
|
180
|
+
WHERE w.revoked_at IS NULL AND w.expires_at <= ? AND f.status = 'waived'`).all(e);for(let r of t)this.run("UPDATE security_findings SET status = 'open', updated_at = ? WHERE id = ?",[e,String(r.id)]),this.controlPlane.appendEvent({organizationId:String(r.organization_id),projectId:vr(r.project_id),resourceId:vr(r.resource_id),type:"security.waiver.expired",level:"warning",payload:{waiverId:String(r.waiver_id),findingId:String(r.id)}});return t.length}createPolicy(e){let t=e.name.trim();if(!t)throw Error("Security policy name is required");let r=Wv(e.rules),o=this.idFn(),m=this.now(),h=[...new Set((e.requiredScanners??[]).map((I)=>I.trim()).filter(Boolean))].sort();return this.run(`INSERT INTO security_policies (id, organization_id, environment_id, name, rules, required_scanners, scanner_fail_mode, created_by_actor_id, created_at, updated_at)
|
|
181
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,[o,e.organizationId,e.environmentId??null,t,Yn(r),Yn(h),e.scannerFailMode,e.actorId??null,m,m]),this.controlPlane.appendEvent({organizationId:e.organizationId,actorId:e.actorId,type:"security.policy.created",payload:{policyId:o,environmentId:e.environmentId??null,scannerFailMode:e.scannerFailMode,requiredScanners:h,rules:s(r)}}),this.getPolicy(o)}updatePolicy(e,t,r){let o=this.getPolicy(e);if(!o||o.version!==t)throw Error(`Security policy ${e} changed since version ${t}`);let m=r.rules?Wv(r.rules):o.rules,h=r.requiredScanners?[...new Set(r.requiredScanners.map((A)=>A.trim()).filter(Boolean))].sort():o.requiredScanners,I=this.now();if(this.controlPlane.database.run("UPDATE security_policies SET name = ?, rules = ?, required_scanners = ?, scanner_fail_mode = ?, enabled = ?, version = version + 1, updated_at = ? WHERE id = ? AND version = ?",[r.name?.trim()||o.name,Yn(m),Yn(h),r.scannerFailMode??o.scannerFailMode,r.enabled??o.enabled?1:0,I,e,t]).changes!==1)throw Error(`Security policy ${e} changed since version ${t}`);return this.controlPlane.appendEvent({organizationId:o.organizationId,actorId:r.actorId,type:"security.policy.updated",payload:{policyId:e,fromVersion:t,toVersion:t+1}}),this.getPolicy(e)}getPolicy(e){let t=this.controlPlane.database.query("SELECT * FROM security_policies WHERE id = ?").get(e);return t?sg(t):void 0}listPolicies(e){return this.controlPlane.database.query("SELECT * FROM security_policies WHERE organization_id = ? ORDER BY environment_id, name").all(e).map(sg)}evaluateGate(e){this.expireWaivers();let t=e.policyId?this.getPolicy(e.policyId):this.controlPlane.database.query(`SELECT * FROM security_policies WHERE organization_id = ? AND enabled = 1 AND (environment_id = ? OR environment_id IS NULL)
|
|
182
|
+
ORDER BY CASE WHEN environment_id = ? THEN 0 ELSE 1 END, updated_at DESC LIMIT 1`).get(e.organizationId,e.environmentId,e.environmentId),r=t&&"rules"in t&&Array.isArray(t.rules)?t:t?sg(t):void 0;if(!r||!r.enabled)throw Error("No enabled security policy applies to this environment");let o=this.listFindings({organizationId:e.organizationId,projectId:e.projectId,environmentId:e.environmentId,limit:1000}).filter((_e)=>_e.status!=="resolved"),m=new Map;for(let _e of o){let he=this.listWaivers(_e.id).find((Ue)=>Ue.state==="active"&&(!Ue.policyId||Ue.policyId===r.id));if(he)m.set(_e.id,he)}let h=(_e)=>r.rules.filter((he)=>(!he.scannerId||he.scannerId===_e.scannerId)&&cg[_e.severity]>=cg[he.minimumSeverity]).reduce((he,Ue)=>dg[Ue.action]>dg[he]?Ue.action:he,"record"),I=o.filter((_e)=>!m.has(_e.id)&&h(_e)==="block"),R=o.filter((_e)=>!m.has(_e.id)&&h(_e)==="warn"),A=this.controlPlane.database.query(`SELECT run.* FROM security_scan_runs run
|
|
183
|
+
WHERE run.organization_id = ? AND run.project_id = ? AND run.environment_id = ?
|
|
184
|
+
AND run.id = (SELECT candidate.id FROM security_scan_runs candidate
|
|
185
|
+
WHERE candidate.organization_id = run.organization_id AND candidate.project_id = run.project_id
|
|
186
|
+
AND candidate.environment_id = run.environment_id AND candidate.scanner_id = run.scanner_id
|
|
187
|
+
ORDER BY candidate.completed_at DESC, candidate.rowid DESC LIMIT 1)`).all(e.organizationId,e.projectId,e.environmentId),E=e.staleAfterMs??86400000,x=this.nowFn().getTime(),N=A.map(og).map((_e)=>x-new Date(_e.completedAt).getTime()>E?{..._e,status:"stale"}:_e),F=new Set(N.map((_e)=>_e.scannerId)),z=r.requiredScanners.filter((_e)=>!F.has(_e)).map((_e)=>({id:`missing:${_e}`,organizationId:e.organizationId,projectId:e.projectId,environmentId:e.environmentId,scannerId:_e,scannerVersion:"unavailable",status:"unavailable",metadata:{},findingsCount:0,startedAt:this.now(),completedAt:this.now(),durationMs:0})),K=[...N,...z],J=K.filter((_e)=>r.requiredScanners.includes(_e.scannerId)&&Fv.has(_e.status)),re=Object.fromEntries(K.map((_e)=>[_e.scannerId,_e.scannerVersion])),se=r.scannerFailMode==="closed"&&J.length>0,ye=I.length||se?"block":R.length||J.length?"warn":"allow",ne=[];if(I.length)ne.push(`${I.length} finding${I.length===1?"":"s"} blocked by policy`);if(R.length)ne.push(`${R.length} warning${R.length===1?"":"s"} require review`);if(J.length)ne.push(`${J.length} scanner${J.length===1?"":"s"} ${r.scannerFailMode==="closed"?"failed closed":"failed open"}`);if(m.size)ne.push(`${m.size} active time-limited waiver${m.size===1?"":"s"}`);if(!ne.length)ne.push("All recorded checks satisfy policy");let fe=this.idFn(),Ce=this.now();return this.run(`INSERT INTO security_deploy_decisions
|
|
188
|
+
(id, organization_id, project_id, environment_id, operation_id, policy_id, policy_version, outcome, scanner_versions, finding_ids, waiver_ids, explanation, created_at)
|
|
189
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,[fe,e.organizationId,e.projectId,e.environmentId,e.operationId??null,r.id,r.version,ye,Yn(re),Yn([...I,...R].map((_e)=>_e.id)),Yn([...m.values()].map((_e)=>_e.id)),ne.join("; "),Ce]),this.controlPlane.appendEvent({organizationId:e.organizationId,projectId:e.projectId,operationId:e.operationId,type:"security.deploy.decision",level:ye==="block"?"error":ye==="warn"?"warning":"info",payload:{decisionId:fe,outcome:ye,policyId:r.id,policyVersion:r.version,scannerVersions:re,blockerIds:I.map((_e)=>_e.id),waiverIds:[...m.values()].map((_e)=>_e.id),explanation:ne.join("; ")}}),this.getDecision(fe)}getDecision(e){let t=this.controlPlane.database.query("SELECT * FROM security_deploy_decisions WHERE id = ?").get(e);return t?qv(t):void 0}listDecisions(e,t=100){return this.controlPlane.database.query("SELECT * FROM security_deploy_decisions WHERE environment_id = ? ORDER BY created_at DESC LIMIT ?").all(e,Math.min(500,Math.max(1,t))).map(qv)}addReleaseArtifact(e){let t=this.controlPlane.database.query("SELECT * FROM release_security_artifacts WHERE release_id = ? AND kind = ? AND digest = ?").get(e.releaseId,e.kind,e.digest);if(t)return ag(t);let r=this.idFn(),o=this.now();return this.run(`INSERT INTO release_security_artifacts
|
|
190
|
+
(id, organization_id, project_id, environment_id, release_id, kind, format, digest, summary, content, sensitive, created_at)
|
|
191
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,[r,e.organizationId,e.projectId,e.environmentId??null,e.releaseId,e.kind,e.format,e.digest,Yn(e.summary),e.content??null,e.sensitive?1:0,o]),this.controlPlane.appendEvent({organizationId:e.organizationId,projectId:e.projectId,type:"security.release_artifact.created",payload:{artifactId:r,releaseId:e.releaseId,kind:e.kind,format:e.format,digest:e.digest,sensitive:e.sensitive}}),ag(this.controlPlane.database.query("SELECT * FROM release_security_artifacts WHERE id = ?").get(r))}listReleaseArtifacts(e,t=!1){return this.controlPlane.database.query("SELECT * FROM release_security_artifacts WHERE release_id = ? ORDER BY created_at, kind").all(e).map(ag).map((r)=>r.sensitive&&!t?{...r,content:void 0}:r)}summary(e,t,r){let o=this.listFindings({organizationId:e,projectId:t,environmentId:r,limit:1000}),m=this.listScanRuns({organizationId:e,projectId:t,environmentId:r,limit:1000}),h={info:0,low:0,medium:0,high:0,critical:0};for(let R of o)if(R.status==="open"||R.status==="acknowledged")h[R.severity]++;let I={passed:0,failed:0,skipped:0,unavailable:0,unsupported:0,stale:0};for(let R of m)I[R.status]++;return{open:h,waived:o.filter((R)=>R.status==="waived").length,resolved:o.filter((R)=>R.status==="resolved").length,checks:I,lastScannedAt:m[0]?.completedAt}}exportPosture(e){let t=this.controlPlane.listProjects().filter((r)=>r.organizationId===e);return s({format:"ts-cloud-security-posture",version:1,exportedAt:this.now(),organizationId:e,policies:this.listPolicies(e),findings:this.listFindings({organizationId:e,limit:1000}),waivers:this.listWaivers().filter((r)=>this.getFinding(r.findingId)?.organizationId===e),scans:t.flatMap((r)=>this.listScanRuns({organizationId:e,projectId:r.id,limit:1000})),decisions:t.flatMap((r)=>this.controlPlane.listEnvironments(r.id).flatMap((o)=>this.listDecisions(o.id,1000)))})}}import{existsSync as sC,readdirSync as aC,readFileSync as cC,statSync as dC}from"node:fs";import{extname as lC,join as uC,relative as pC}from"node:path";var jd=[{name:"AWS Access Key ID",pattern:/(?:^|[^A-Z0-9])((AKIA|ABIA|ACCA|ASIA)[A-Z0-9]{16})(?:[^A-Z0-9]|$)/g,severity:"critical",description:"AWS Access Key ID detected"},{name:"AWS Secret Access Key",pattern:/(?:aws_secret_access_key|aws_secret_key|secret_access_key|secretAccessKey)['"]?\s*[=:]\s*['"]?([A-Za-z0-9/+=]{40})['"]?/gi,severity:"critical",description:"AWS Secret Access Key detected"},{name:"AWS Secret Key (Generic)",pattern:/(?:^|['"`:=\s])([A-Za-z0-9/+=]{40})(?:['"`\s]|$)/g,severity:"high",description:"Potential AWS Secret Key (40-char base64)"},{name:"Generic API Key",pattern:/(?:api[_-]?key|apikey|api[_-]?secret)\s*[=:]\s*['"]?([A-Za-z0-9_\-]{20,})['"]?/gi,severity:"high",description:"Generic API key detected"},{name:"RSA Private Key",pattern:/-----BEGIN RSA PRIVATE KEY-----/g,severity:"critical",description:"RSA private key detected"},{name:"OpenSSH Private Key",pattern:/-----BEGIN OPENSSH PRIVATE KEY-----/g,severity:"critical",description:"OpenSSH private key detected"},{name:"EC Private Key",pattern:/-----BEGIN EC PRIVATE KEY-----/g,severity:"critical",description:"EC private key detected"},{name:"PGP Private Key",pattern:/-----BEGIN PGP PRIVATE KEY BLOCK-----/g,severity:"critical",description:"PGP private key detected"},{name:"GitHub Token",pattern:/(?:ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9_]{36,}/g,severity:"critical",description:"GitHub personal access token detected"},{name:"GitHub OAuth",pattern:/github[_-]?oauth[_-]?token\s*[=:]\s*['"]?([A-Za-z0-9_]{40})['"]?/gi,severity:"critical",description:"GitHub OAuth token detected"},{name:"Slack Token",pattern:/xox[baprs]-[0-9]{10,13}-[0-9]{10,13}[a-zA-Z0-9-]*/g,severity:"critical",description:"Slack token detected"},{name:"Slack Webhook",pattern:/https:\/\/hooks\.slack\.com\/services\/T[A-Z0-9]+\/B[A-Z0-9]+\/[A-Za-z0-9]+/g,severity:"high",description:"Slack webhook URL detected"},{name:"Discord Webhook",pattern:/https:\/\/discord(?:app)?\.com\/api\/webhooks\/[0-9]+\/[A-Za-z0-9_-]+/g,severity:"high",description:"Discord webhook URL detected"},{name:"JWT Token",pattern:/eyJ[A-Za-z0-9_-]*\.eyJ[A-Za-z0-9_-]*\.[A-Za-z0-9_-]*/g,severity:"high",description:"JWT token detected"},{name:"Google API Key",pattern:/AIza[0-9A-Za-z_-]{35}/g,severity:"critical",description:"Google API key detected"},{name:"Google OAuth ID",pattern:/[0-9]+-[A-Za-z0-9_]{32}\.apps\.googleusercontent\.com/g,severity:"high",description:"Google OAuth client ID detected"},{name:"Firebase API Key",pattern:/(?:firebase[_-]?api[_-]?key)\s*[=:]\s*['"]?([A-Za-z0-9_-]{39})['"]?/gi,severity:"critical",description:"Firebase API key detected"},{name:"Cloudflare API Token",pattern:/(?:cloudflare[_-]?api[_-]?token|cf[_-]?api[_-]?token)\s*[=:]\s*['"]?([A-Za-z0-9_-]{40})['"]?/gi,severity:"critical",description:"Cloudflare API token detected"},{name:"Azure Client Secret",pattern:/(?:azure[_-]?client[_-]?secret|client[_-]?secret)\s*[=:]\s*['"]?([A-Za-z0-9~._-]{34,})['"]?/gi,severity:"critical",description:"Azure client secret detected"},{name:"Heroku API Key",pattern:/(?:heroku[_-]?api[_-]?key)\s*[=:]\s*['"]?([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})['"]?/gi,severity:"critical",description:"Heroku API key detected"},{name:"Database Connection String",pattern:/(?:mysql|postgres|postgresql|mongodb|redis|mongodb\+srv):\/\/[^:]+:[^@]+@[^/\s]+/gi,severity:"critical",description:"Database connection string with credentials detected"},{name:"Database Password",pattern:/(?:db[_-]?password|database[_-]?password|mysql[_-]?password|postgres[_-]?password)\s*[=:]\s*['"]?([^'"\s]{8,})['"]?/gi,severity:"critical",description:"Database password detected"},{name:"Stripe API Key",pattern:/(?:sk|pk)_(?:test|live)_[0-9a-zA-Z]{24,}/g,severity:"critical",description:"Stripe API key detected"},{name:"PayPal Client ID",pattern:/(?:paypal[_-]?client[_-]?id)\s*[=:]\s*['"]?([A-Za-z0-9_-]{80})['"]?/gi,severity:"high",description:"PayPal client ID detected"},{name:"Square Access Token",pattern:/sq0[a-z]{3}-[0-9A-Za-z_-]{22,}/g,severity:"critical",description:"Square access token detected"},{name:"Twilio API Key",pattern:/SK[a-f0-9]{32}/g,severity:"critical",description:"Twilio API key detected"},{name:"SendGrid API Key",pattern:/SG\.[A-Za-z0-9_-]{22}\.[A-Za-z0-9_-]{43}/g,severity:"critical",description:"SendGrid API key detected"},{name:"Mailgun API Key",pattern:/key-[0-9a-zA-Z]{32}/g,severity:"critical",description:"Mailgun API key detected"},{name:"Password in Code",pattern:/(?:password|passwd|pwd)\s*[=:]\s*['"]([^'"]{8,})['"](?!\s*[,\]])/gi,severity:"high",description:"Hardcoded password detected"},{name:"Secret/Token Assignment",pattern:/(?:secret|token|auth[_-]?token|access[_-]?token)\s*[=:]\s*['"]([A-Za-z0-9_\-/+=]{16,})['"](?!\s*[,\]])/gi,severity:"high",description:"Hardcoded secret or token detected"},{name:"NPM Token",pattern:/(?:npm[_-]?token)\s*[=:]\s*['"]?([A-Za-z0-9_-]{36})['"]?/gi,severity:"critical",description:"NPM token detected"},{name:"SSH Private Key Path Exposed",pattern:/~\/\.ssh\/id_[a-z]+|\/home\/[^/]+\/\.ssh\/id_[a-z]+/g,severity:"medium",description:"SSH private key path exposed"},{name:"Env Variable with Secret",pattern:/(?:process\.env\.)((?=[A-Z_]*(?:SECRET|KEY|TOKEN|PASSWORD|CREDENTIAL|AUTH))[A-Z_]+)\s*(?:===?\s*['"]([^'"]+)['"])?/g,severity:"medium",description:"Environment variable containing secret may be exposed"}],mC=[".js",".jsx",".ts",".tsx",".mjs",".cjs",".vue",".svelte",".html",".htm",".css",".scss",".less",".json",".yaml",".yml",".toml",".xml",".env",".config",".conf"],gC=["node_modules",".git",".svn",".hg","dist","build","coverage",".nyc_output","__pycache__",".pytest_cache","vendor",".idea",".vscode",".turbo",".next",".nuxt","framework.bak","pantry",".pantry","tests","test","__tests__","spec","__mocks__",".github",".gitlab",".circleci"],fC=["package-lock.json","yarn.lock","pnpm-lock.yaml","bun.lockb","bun.lock","*.min.js","*.min.css","*.map","*.d.ts","*.test.ts","*.test.js","*.spec.ts","*.spec.js",".env",".env.*"];class Ur{patterns;excludeDirs;excludeFiles;maxFileSize;constructor(e){this.patterns=[...jd,...e?.customPatterns||[]],this.excludeDirs=[...gC,...e?.excludeDirs||[]],this.excludeFiles=[...fC,...e?.excludeFiles||[]],this.maxFileSize=e?.maxFileSize||1048576}async scan(e){let t=Date.now(),r=[],o=0,{directory:m,exclude:h=[],include:I,skipPatterns:R=[]}=e,A=e.failOnSeverity||"critical";if(!sC(m))throw Error(`Directory not found: ${m}`);let E=this.getFilesToScan(m,[...this.excludeDirs,...h],I);for(let K of E){let J=pC(m,K);if(this.shouldExcludeFile(J))continue;try{if(dC(K).size>this.maxFileSize)continue;let se=cC(K,"utf-8"),ye=this.scanContent(se,J,R);r.push(...ye),o++}catch{continue}}let x={critical:r.filter((K)=>K.pattern.severity==="critical").length,high:r.filter((K)=>K.pattern.severity==="high").length,medium:r.filter((K)=>K.pattern.severity==="medium").length,low:r.filter((K)=>K.pattern.severity==="low").length},N=["low","medium","high","critical"],F=N.indexOf(A),z=!0;for(let K=F;K<N.length;K++)if(x[N[K]]>0){z=!1;break}return{passed:z,findings:r,scannedFiles:o,duration:Date.now()-t,summary:x}}scanContent(e,t,r){let o=[],m=e.split(`
|
|
192
|
+
`);for(let h of this.patterns){if(r.includes(h.name))continue;h.pattern.lastIndex=0;let I;while((I=h.pattern.exec(e))!==null){let R=e.substring(0,I.index),A=R.split(`
|
|
193
|
+
`).length,E=R.lastIndexOf(`
|
|
194
|
+
`),x=I.index-E,N=m[A-1]||"";if(this.isLikelyPlaceholder(I[0],N))continue;let F=I[1]||I[0];if(h.name==="AWS Secret Key (Generic)"&&/^[a-f0-9]{40}$/i.test(F))continue;if(h.name==="AWS Secret Key (Generic)"&&!this.hasSecretCharacterDiversity(F))continue;if(h.name==="AWS Secret Key (Generic)"&&/^(?:Actions|Commands|Controllers|Handlers|Jobs|Listeners|Middleware|Models|Resources|Services)\//.test(F))continue;if(this.isLowEntropy(F))continue;o.push({file:t,line:A,column:x,match:this.maskSecret(I[0]),pattern:h,context:this.maskSecret(N.trim())})}}return o}isLikelyPlaceholder(e,t){let r=["example","placeholder","your_","YOUR_","xxx","XXX","***","test","TEST","dummy","DUMMY","fake","FAKE","sample","SAMPLE","<your","${","{{","process.env","import.meta.env","CHANGEME","TODO","FIXME"],o=e.toLowerCase(),m=t.toLowerCase();for(let I of r)if(o.includes(I.toLowerCase())||m.includes(I.toLowerCase()))return!0;let h=t.trim();if(h.startsWith("//")||h.startsWith("#")||h.startsWith("*")||h.startsWith("/*")){if(m.includes("example")||m.includes("format:")||m.includes("e.g."))return!0}return!1}isLowEntropy(e){if(e.length<8)return!1;if(new Set(e).size<8)return!0;if(/(.)\1{3,}/.test(e))return!0;return!1}hasSecretCharacterDiversity(e){let t=/[\d+=]/.test(e),r=[/[a-z]/.test(e),/[A-Z]/.test(e),/\d/.test(e),/[/+=]/.test(e)];return t&&r.filter(Boolean).length>=3}maskSecret(e){if(e.length<=8)return"*".repeat(e.length);let t=Math.min(4,Math.floor(e.length*0.2));return e.substring(0,t)+"*".repeat(e.length-t*2)+e.substring(e.length-t)}getFilesToScan(e,t,r){let o=[],m=r||mC,h=(I)=>{let R=aC(I,{withFileTypes:!0});for(let A of R){let E=uC(I,A.name);if(A.isDirectory()){if(!t.includes(A.name))h(E)}else if(A.isFile()){let x=lC(A.name).toLowerCase();if(m.includes(x)||A.name.startsWith(".env")||A.name.endsWith(".config"))o.push(E)}}};return h(e),o}shouldExcludeFile(e){let t=e.split("/").pop()||"";for(let r of this.excludeFiles)if(r.startsWith("*")){let o=r.substring(1);if(t.endsWith(o))return!0}else if(r.endsWith("*")){let o=r.substring(0,r.length-1);if(t.startsWith(o))return!0}else if(t===r)return!0;return!1}addPattern(e){this.patterns.push(e)}getPatterns(){return[...this.patterns]}}async function Xg(e){return new Ur().scan(e)}function Yg(e){let t=[];if(t.push(`
|
|
195
|
+
Security Scan Results`),t.push("=".repeat(50)),t.push(`Files scanned: ${e.scannedFiles}`),t.push(`Duration: ${e.duration}ms`),t.push(""),t.push("Summary:"),t.push(` Critical: ${e.summary.critical}`),t.push(` High: ${e.summary.high}`),t.push(` Medium: ${e.summary.medium}`),t.push(` Low: ${e.summary.low}`),t.push(""),e.findings.length>0){t.push("Findings:"),t.push("-".repeat(50));for(let r of e.findings)t.push(`
|
|
196
|
+
[${r.pattern.severity.toUpperCase()}] ${r.pattern.name}`),t.push(` File: ${r.file}:${r.line}:${r.column}`),t.push(` Match: ${r.match}`),t.push(` Context: ${r.context}`),t.push(` Description: ${r.pattern.description}`)}return t.push(""),t.push(e.passed?"✓ Security scan passed":"✗ Security scan failed"),t.join(`
|
|
197
|
+
`)}function hC(e,t){return{status:"unavailable",findings:[],error:`${e.id} exceeded its ${t}ms timeout`,metadata:{timeoutMs:t}}}class lt{posture;defaultTimeoutMs;nowFn;constructor(e,t={}){this.posture=e;this.defaultTimeoutMs=t.defaultTimeoutMs??60000,this.nowFn=t.now??(()=>new Date)}async run(e,t){let r=this.nowFn(),o=Math.max(100,e.timeoutMs??this.defaultTimeoutMs),m=new AbortController,h,I=new Promise((x)=>{h=setTimeout(()=>{m.abort(),x(hC(e,o))},o)}),R;try{R=await Promise.race([e.scan({...t,signal:m.signal}),I])}catch(x){R={status:"unavailable",findings:[],error:x instanceof Error?x.message:String(x)}}finally{if(h)clearTimeout(h)}let A=this.nowFn(),E={...t,scannerId:e.id,scannerVersion:e.version,status:R.status,findings:R.findings,metadata:R.metadata,error:R.error,startedAt:r.toISOString(),completedAt:A.toISOString(),durationMs:Math.max(0,A.getTime()-r.getTime())};return this.posture.recordScan(E)}async runAll(e,t){return Promise.all(e.map((r)=>this.run(r,t)))}}class Qr{scanner;id="source-secrets";version="1.0.0";timeoutMs=30000;constructor(e=new Ur){this.scanner=e}async scan(e){if(!e.artifactRoot)return{status:"skipped",findings:[],error:"No artifact root was supplied"};let t=await this.scanner.scan({directory:e.artifactRoot,failOnSeverity:"critical"});return{status:t.passed?"passed":"failed",findings:t.findings.map((r)=>({...e,ruleId:r.pattern.name.toLowerCase().replace(/[^a-z0-9]+/g,"-"),severity:r.pattern.severity,title:r.pattern.name,description:r.pattern.description,subject:`${r.file}:${r.line}`,evidence:{file:r.file,line:r.line,column:r.column,match:r.match,context:r.context},remediation:"Remove the credential, rotate it at its issuer, and use a managed secret or environment reference."})),metadata:{scannedFiles:t.scannedFiles,summary:t.summary}}}}function yC(e){let t=e?.toLowerCase();return t==="critical"||t==="high"||t==="medium"||t==="low"?t:"info"}class bo{id="trivy-image";version;timeoutMs;constructor(e={}){this.version=e.version??"local",this.timeoutMs=e.timeoutMs??120000,this.executable=e.executable??"trivy"}executable;async scan(e){if(!e.imageRef)return{status:"skipped",findings:[],error:"No image reference was supplied"};let t;try{t=Bun.spawn([this.executable,"image","--format","json","--quiet","--scanners","vuln",e.imageRef],{stdout:"pipe",stderr:"pipe",env:{PATH:process.env.PATH??"/usr/local/bin:/usr/bin:/bin",HOME:"/nonexistent",TRIVY_NO_PROGRESS:"true"}})}catch(A){return{status:"unsupported",findings:[],error:A instanceof Error?A.message:String(A)}}let r=()=>t.kill();e.signal.addEventListener("abort",r,{once:!0});let[o,m,h]=await Promise.all([t.exited,new Response(t.stdout).text(),new Response(t.stderr).text()]);if(e.signal.removeEventListener("abort",r),o!==0)return{status:/not found|enoent/i.test(h)?"unsupported":"unavailable",findings:[],error:h.slice(0,2000)||`Trivy exited with ${o}`};let I=JSON.parse(m),R=[];for(let A of I.Results??[])for(let E of A.Vulnerabilities??[]){let x=E.VulnerabilityID??"unknown-vulnerability";R.push({...e,ruleId:x,severity:yC(E.Severity),title:E.Title||x,description:E.Description||`${x} affects ${E.PkgName??"an image package"}.`,subject:`${e.imageRef}:${E.PkgName??"unknown"}:${E.InstalledVersion??"unknown"}`,evidence:{image:e.imageRef,target:A.Target??"",cve:x,package:E.PkgName??"",installedVersion:E.InstalledVersion??"",fixedVersion:E.FixedVersion??"",reference:E.PrimaryURL??""},remediation:E.FixedVersion?`Upgrade ${E.PkgName} to ${E.FixedVersion} or later.`:"Remove or replace the affected package and rescan the image."})}return{status:R.length?"failed":"passed",findings:R,metadata:{imageRef:e.imageRef,vulnerabilityCount:R.length,localOnly:!0}}}}class Zg{actions;id="aws-iam-capabilities";version="1.0.0";timeoutMs=30000;client;constructor(e,t={}){this.actions=e;if(this.resourceArns=t.resourceArns??["*"],t.client)this.client=t.client;else{let r=new le(t.region),o=new pe(t.region);this.client={getCallerIdentity:()=>r.getCallerIdentity(),simulatePrincipalPolicy:(m)=>o.simulatePrincipalPolicy(m)}}}resourceArns;async scan(e){let t=await this.client.getCallerIdentity();if(!t.Arn)return{status:"unavailable",findings:[],error:"AWS returned no caller ARN"};let r=t.Arn;if(r.includes(":assumed-role/"))return{status:"unsupported",findings:[],error:"AWS policy simulation requires the underlying IAM role ARN; use an IAM role/user ARN or provider-native access analysis.",metadata:{account:t.Account??"",principalType:"assumed-role"}};let h=(await this.client.simulatePrincipalPolicy({PolicySourceArn:r,ActionNames:this.actions,ResourceArns:this.resourceArns})).EvaluationResults.filter((I)=>I.EvalDecision!=="allowed").map((I)=>({...e,ruleId:`iam-${I.EvalActionName.toLowerCase().replace(/[^a-z0-9]+/g,"-")}`,severity:"high",title:`Missing AWS capability: ${I.EvalActionName}`,description:`The active AWS principal is ${I.EvalDecision||"not allowed"} for this deployment action.`,subject:`${r}:${I.EvalActionName}`,evidence:{account:t.Account??"",principalArn:r,action:I.EvalActionName,decision:I.EvalDecision,missingContext:I.MissingContextValues??[]},remediation:"Grant the deployment principal the least-privilege action on the required resources, then rerun the capability check."}));return{status:h.length?"failed":"passed",findings:h,metadata:{account:t.Account??"",principalArn:r,evaluatedActions:this.actions.length}}}}import{createHash as vC,createVerify as bC}from"node:crypto";import{existsSync as SC,readFileSync as wC}from"node:fs";import{basename as kC,join as IC,resolve as Hv}from"node:path";function La(e){return vC("sha256").update(e).digest("hex")}function EC(e){let t=[["dependencies","required"],["optionalDependencies","optional"],["devDependencies","excluded"],["peerDependencies","required"]],r=new Map;for(let[o,m]of t){let h=e[o];if(!h||typeof h!=="object"||Array.isArray(h))continue;for(let[I,R]of Object.entries(h)){let A=String(R).replace(/^[~^]/,""),E=`${I}@${A}`;if(!r.has(E))r.set(E,{type:"library",name:I,version:A,purl:`pkg:npm/${encodeURIComponent(I)}@${encodeURIComponent(A)}`,scope:m})}}return[...r.values()].sort((o,m)=>`${o.name}@${o.version}`.localeCompare(`${m.name}@${m.version}`))}function zd(e,t={}){let r=IC(Hv(e),"package.json");if(!SC(r))throw Error(`Cannot generate an SBOM: ${r} was not found`);let o=wC(r,"utf8"),m=JSON.parse(o),h=String(m.name??kC(Hv(e))),I=String(m.version??"0.0.0"),R=La(o);return{bomFormat:"CycloneDX",specVersion:"1.5",serialNumber:t.serial??`urn:uuid:${crypto.randomUUID()}`,version:1,metadata:{timestamp:(t.now??new Date).toISOString(),component:{type:"application",name:h,version:I,purl:`pkg:npm/${encodeURIComponent(h)}@${encodeURIComponent(I)}`,hashes:[{alg:"SHA-256",content:R}]},tools:[{vendor:"ts-cloud",name:"security-posture",version:"1.0.0"}]},components:EC(m)}}async function Kd(e,t={}){let r=Bun.spawn([t.executable??"syft",e,"-o","cyclonedx-json"],{stdout:"pipe",stderr:"pipe",env:{PATH:process.env.PATH??"/usr/local/bin:/usr/bin:/bin",HOME:"/nonexistent",SYFT_CHECK_FOR_APP_UPDATE:"false"}}),o=()=>r.kill();t.signal?.addEventListener("abort",o,{once:!0});let[m,h,I]=await Promise.all([r.exited,new Response(r.stdout).text(),new Response(r.stderr).text()]);if(t.signal?.removeEventListener("abort",o),m!==0)throw Error(I.slice(0,2000)||`Syft exited with ${m}`);return JSON.parse(h)}function Eo(e,t,r){let o=JSON.stringify(r);return e.addReleaseArtifact({...t,kind:"sbom",format:`cyclonedx-${r.specVersion}+json`,digest:`sha256:${La(o)}`,summary:{component:r.metadata.component.name,version:r.metadata.component.version,components:r.components.length},content:o,sensitive:!0})}function Wd(e,t,r){let o={total:r.length,critical:0,high:0,medium:0,low:0,info:0,fixable:0};for(let h of r){o[h.severity]++;let I=h.evidence;if(typeof I.fixedVersion==="string"&&I.fixedVersion)o.fixable++}let m=JSON.stringify({releaseId:t.releaseId,generatedAt:new Date().toISOString(),findings:r.map((h)=>({id:h.id,ruleId:h.ruleId,severity:h.severity,subject:h.subject,remediation:h.remediation}))});return e.addReleaseArtifact({...t,kind:"vulnerability_summary",format:"ts-cloud-vulnerability-summary+json",digest:`sha256:${La(m)}`,summary:o,content:m,sensitive:!0})}function Jd(e){if(!e.artifact&&!e.artifactSha256)throw Error("Release provenance requires artifact content or a SHA-256 digest");return{_type:"https://in-toto.io/Statement/v1",subject:[{name:e.artifactName,digest:{sha256:e.artifactSha256?.replace(/^sha256:/,"")??La(e.artifact)}}],predicateType:"https://slsa.dev/provenance/v1",predicate:{buildDefinition:{buildType:"https://ts-cloud.dev/build/v1",externalParameters:e.externalParameters??{},internalParameters:{},resolvedDependencies:e.resolvedDependencies??[]},runDetails:{builder:{id:"https://ts-cloud.dev/builders/local/v1"},metadata:{invocationId:e.invocationId,startedOn:e.startedAt,finishedOn:e.completedAt}}}}}function Xd(e,t,r){let o=JSON.stringify(r);return e.addReleaseArtifact({...t,kind:"provenance",format:"in-toto+slsa-v1+json",digest:`sha256:${La(o)}`,summary:{subjects:r.subject.length,builder:r.predicate.runDetails.builder.id,buildType:r.predicate.buildDefinition.buildType},content:o,sensitive:!0})}function gA(e){let t=e.expectedSha256.replace(/^sha256:/,"").toLowerCase(),r=La(e.artifact)===t;if(!e.signature||!e.publicKey)return{digestValid:r,signature:"not_configured"};try{let o=bC("SHA256");return o.update(e.artifact),o.end(),{digestValid:r,signature:o.verify(e.publicKey,Buffer.from(e.signature,"base64"))?"verified":"invalid"}}catch{return{digestValid:r,signature:"invalid"}}}function Z(e,t){let r=e.environments.get(t);if(!r)throw Error(`Security scope for environment '${t}' was not found`);return{organizationId:e.organization.id,projectId:e.project.id,environmentId:r.id}}function Yd(e,t){let r=new Fr(e.store),o=r.listPolicies(e.organization.id);for(let[m,h]of e.environments){if(o.some((R)=>R.environmentId===h.id))continue;let I=m==="production"||h.kind==="production";o.push(r.createPolicy({organizationId:e.organization.id,environmentId:h.id,name:I?"Production baseline":"Environment baseline",scannerFailMode:I?"closed":"open",requiredScanners:["source-secrets"],rules:I?[{minimumSeverity:"critical",action:"block"},{minimumSeverity:"high",action:"block"},{minimumSeverity:"medium",action:"warn"},{minimumSeverity:"low",action:"record"}]:[{minimumSeverity:"critical",action:"warn"},{minimumSeverity:"high",action:"warn"},{minimumSeverity:"low",action:"record"}],actorId:t}))}return o}function mA(e,t,r,o={}){let m=r.findings.map((h)=>({...t,ruleId:h.pattern.name.toLowerCase().replace(/[^a-z0-9]+/g,"-"),severity:h.pattern.severity,title:h.pattern.name,description:h.pattern.description,subject:`${h.file}:${h.line}`,evidence:{file:h.file,line:h.line,column:h.column,match:h.match,context:h.context},remediation:"Remove and rotate the credential, then reference it through a managed secret or environment variable."}));return e.recordScan({...t,scannerId:"source-secrets",scannerVersion:o.scannerVersion??"1.0.0",status:o.skipped?"skipped":r.passed?"passed":"failed",findings:o.skipped?[]:m,metadata:{scannedFiles:r.scannedFiles,durationMs:r.duration,summary:r.summary},durationMs:r.duration,error:o.skipped?"The operator explicitly skipped source-secret scanning.":void 0})}function hA(e,t,r="The operator explicitly skipped source-secret scanning."){return e.recordScan({...t,scannerId:"source-secrets",scannerVersion:"1.0.0",status:"skipped",findings:[],error:r})}function RC(e,t){let r=[],o=t.security;if(o?.firewall&&!["active","configured","ok"].includes(String(o.firewall.status)))r.push({...e,ruleId:"host-firewall-disabled",severity:"high",title:"Host firewall is not active",description:String(o.firewall.summary??"The host firewall did not report an active configuration."),subject:"host:firewall",evidence:{status:String(o.firewall.status??"unknown"),rules:Array.isArray(o.firewall.rules)?o.firewall.rules.length:0},remediation:"Apply the declarative firewall policy and confirm only required listeners are public."});for(let m of o?.ports??[]){let h=String(m.listen??""),I=Number(h.match(/:(\d+)$/)?.[1]);if(m.exposure==="public"&&Number.isFinite(I)&&![22,80,443].includes(I))r.push({...e,ruleId:"unexpected-public-listener",severity:"medium",title:`Unexpected public listener on port ${I}`,description:`${String(m.processName??"A process")} is listening publicly outside the standard SSH/HTTP/HTTPS ports.`,subject:`host:port:${I}`,evidence:{protocol:String(m.proto??""),listen:h,process:String(m.processName??"")},remediation:"Bind the service to loopback/private networking or explicitly document and firewall the required public port."})}for(let m of o?.tlsCertificates??[]){let h=Number(m.daysRemaining);if(Number.isFinite(h)&&h<30)r.push({...e,ruleId:"tls-certificate-expiry",severity:h<14?"high":"medium",title:`TLS certificate expires in ${h} days`,description:`${String(m.domain??"A configured domain")} needs renewal.`,subject:`tls:${String(m.domain??"unknown")}`,evidence:{domain:String(m.domain??""),daysRemaining:h,expires:String(m.expires??"")},remediation:"Renew the certificate and verify automatic renewal can complete before expiry."})}for(let m of t.diagnostics??[]){if(m.status==="pass"||m.status==="ok")continue;r.push({...e,ruleId:`diagnostic-${String(m.name??"unknown").toLowerCase().replace(/[^a-z0-9]+/g,"-")}`,severity:m.status==="fail"?"high":"medium",title:String(m.name??"Host security check"),description:String(m.detail??"The host security diagnostic did not pass."),subject:`diagnostic:${String(m.name??"unknown")}`,evidence:{status:String(m.status??"unknown")},remediation:"Follow the diagnostic guidance, rerun the check, and confirm the finding resolves."})}return r}function Ri(e,t,r){let o=!!r.security||Array.isArray(r.diagnostics),m=o?RC(t,r):[];return e.recordScan({...t,scannerId:"host-posture",scannerVersion:"1.0.0",status:!o?"unavailable":m.length?"failed":"passed",findings:m,metadata:{source:"dashboard-live-probe",checks:Array.isArray(r.diagnostics)?r.diagnostics.length:0},error:o?void 0:"Live host posture data was unavailable; source and provider checks remain usable."})}function Di(e,t){let r=e.evaluateGate({...t.scope,operationId:t.operationId}),o=e.listFindings({...t.scope,limit:1000}).filter((m)=>m.status!=="resolved");return{decision:r,summary:{desiredConfigHash:t.desiredConfigHash??null,securityImpact:{critical:o.filter((m)=>m.severity==="critical").length,high:o.filter((m)=>m.severity==="high").length,waived:o.filter((m)=>m.status==="waived").length,assigned:o.filter((m)=>!!m.ownerActorId).length},blockingFindingIds:r.findingIds,policy:{id:r.policyId,version:r.policyVersion},scannerVersions:r.scannerVersions}}}async function fA(e,t){let r=t.scanner??new bo({timeoutMs:t.scannerTimeoutMs}),m=await new lt(e,{defaultTimeoutMs:t.scannerTimeoutMs??120000}).run(r,{...t.scope,releaseId:t.releaseId,imageRef:t.imageRef}),h={...t.scope,releaseId:t.releaseId};Wd(e,h,m.findings);let I=t.generateImageSbom??Kd,R=new AbortController,A=Math.max(100,t.sbomTimeoutMs??120000),E,x=new Promise((z,K)=>{E=setTimeout(()=>{R.abort(),K(Error(`syft-sbom exceeded its ${A}ms timeout`))},A)}),N="unavailable";try{let z=await Promise.race([I(t.imageRef,{signal:R.signal}),x]);Eo(e,h,z),e.recordScan({...t.scope,releaseId:t.releaseId,scannerId:"syft-sbom",scannerVersion:"local",status:"passed",metadata:{imageRef:t.imageRef,format:`cyclonedx-${z.specVersion}+json`,components:z.components.length,localOnly:!0}}),N="image"}catch(z){let K=z instanceof Error?z.message:String(z);try{let J=zd(t.artifactRoot);Eo(e,h,J),e.recordScan({...t.scope,releaseId:t.releaseId,scannerId:"syft-sbom",scannerVersion:"local",status:"unsupported",error:`Image inventory was unavailable; attached a package-manifest SBOM instead. ${K}`,metadata:{imageRef:t.imageRef,fallback:"package-manifest",components:J.components.length,localOnly:!0}}),N="manifest"}catch(J){e.recordScan({...t.scope,releaseId:t.releaseId,scannerId:"syft-sbom",scannerVersion:"local",status:"unavailable",error:`Image and manifest SBOM generation were unavailable. ${K}; ${J instanceof Error?J.message:String(J)}`,metadata:{imageRef:t.imageRef,localOnly:!0}})}}finally{if(E)clearTimeout(E)}let F=Jd({artifactName:t.imageRef,artifact:t.imageSha256?void 0:t.imageRef,artifactSha256:t.imageSha256,invocationId:t.invocationId,startedAt:t.startedAt,completedAt:t.completedAt??new Date().toISOString(),externalParameters:t.externalParameters??{imageRef:t.imageRef}});return Xd(e,h,F),{scan:m,sbomSource:N}}import{randomBytes as Kv,scryptSync as Jv,timingSafeEqual as AC}from"node:crypto";function mg(e,t){if(e.role==="admin")return[...t];return t.filter((r)=>!!e.sites?.[r])}var lg=32768,ug=8,pg=1,CC=32,PC=16;function gs(e){let t=Kv(PC),r=Jv(e,t,CC,{N:lg,r:ug,p:pg,maxmem:268435456});return`scrypt$${lg}$${ug}$${pg}$${t.toString("base64url")}$${r.toString("base64url")}`}function Xn(e,t){try{let r=t.split("$");if(r.length!==6||r[0]!=="scrypt")return!1;let[,o,m,h,I,R]=r,A=Number(o),E=Number(m),x=Number(h);if(!Number.isFinite(A)||!Number.isFinite(E)||!Number.isFinite(x))return!1;let N=Buffer.from(I,"base64url"),F=Buffer.from(R,"base64url");if(N.length===0||F.length===0)return!1;let z=Jv(e,N,F.length,{N:A,r:E,p:x,maxmem:268435456});return AC(z,F)}catch{return!1}}function Gv(e){let[t,r,o,m]=e.split("$");return t!=="scrypt"||Number(r)!==lg||Number(o)!==ug||Number(m)!==pg}function gg(){return Kv(18).toString("base64url")}import{existsSync as jC,readFileSync as MC}from"node:fs";import{createHash as _C}from"node:crypto";function Yv(e){let t=fg(e.name),r=hg(e.publicKey),o=xC([...e.existingKeys??[],{name:t,publicKey:r}]);return Xv(e.configText,o)}function Zv(e){let t=fg(e.name),r=(e.existingKeys??[]).filter((o)=>o.name!==t);if((e.existingKeys??[]).length===r.length)throw Error(`SSH key '${t}' does not exist in compute.sshKeys`);return Xv(e.configText,r)}function fs(e=[]){return e.map((t)=>({name:t.name,publicKey:t.publicKey,type:DC(t.publicKey),fingerprint:OC(t.publicKey),added:"configured"}))}function Xv(e,t){let r=$C(e,"compute"),o=TC(t),m=NC(e,r.start,r.end,"sshKeys");if(m){let A=m.propertyStart;while(A>0&&e[A-1]!==`
|
|
198
|
+
`&&/\s/.test(e[A-1]))A--;return`${e.slice(0,A)}${o}${e.slice(m.end+1)}`}let h=e.slice(0,r.end).trimEnd(),I=e.slice(r.end),R=h.endsWith(",")||h.endsWith("{")?"":",";return`${h}${R}
|
|
199
|
+
${o},
|
|
200
|
+
${I}`}function TC(e){if(e.length===0)return" sshKeys: []";return` sshKeys: [
|
|
201
|
+
${e.map((r)=>[" {",` name: '${Qv(r.name)}',`,` publicKey: '${Qv(hg(r.publicKey))}',`," },"].join(`
|
|
202
|
+
`)).join(`
|
|
203
|
+
`)}
|
|
204
|
+
]`}function xC(e){let t=new Map;for(let r of e){let o=fg(r.name);if(t.has(o))throw Error(`SSH key '${o}' already exists in compute.sshKeys`);t.set(o,{name:o,publicKey:hg(r.publicKey)})}return[...t.values()]}function fg(e){let t=e.trim();if(!/^[\w@.+-]{2,80}$/.test(t))throw Error("SSH key name must be 2-80 characters and use letters, numbers, @, ., +, _, or -.");return t}function hg(e){let t=e.trim().replace(/\s+/g," "),[r,o]=t.split(" ");if(!/^(?:ssh-(?:ed25519|rsa)|ecdsa-sha2-nistp(?:256|384|521))$/.test(r??"")||!o)throw Error("Public key must be an OpenSSH public key, e.g. ssh-ed25519 AAAA... name@host.");return t}function DC(e){return e.trim().split(/\s+/)[0]||"ssh"}function OC(e){let t=e.trim().split(/\s+/)[1];if(!t)return"-";try{return`SHA256:${_C("sha256").update(Buffer.from(t,"base64")).digest("base64").replace(/=+$/,"")}`}catch{return"-"}}function Qv(e){return e.replace(/\\/g,"\\\\").replaceAll(String.fromCharCode(39),"\\'")}function $C(e,t){let r=new RegExp(`\\b${t}\\s*:\\s*{`,"g").exec(e);if(!r)throw Error(`Could not find ${t}: { ... } in cloud config.`);let o=e.indexOf("{",r.index);return{propertyStart:r.index,start:o,end:vc(e,o,"{","}")}}function NC(e,t,r,o){let m=e.slice(t+1,r),h=new RegExp(`\\b${o}\\s*:\\s*\\[`,"g").exec(m);if(!h)return null;let I=t+1+h.index,R=e.indexOf("[",I);return{propertyStart:I,start:R,end:vc(e,R,"[","]")}}function vc(e,t,r,o){let m=0,h,I=!1,R=!1,A=!1;for(let E=t;E<e.length;E++){let x=e[E],N=e[E+1];if(R){if(x===`
|
|
205
|
+
`)R=!1;continue}if(A){if(x==="*"&&N==="/")A=!1,E++;continue}if(h){if(I){I=!1;continue}if(x==="\\"){I=!0;continue}if(x===h)h=void 0;continue}if(x==="/"&&N==="/"){R=!0,E++;continue}if(x==="/"&&N==="*"){A=!0,E++;continue}if(x==='"'||x==="'"||x==="`"){h=x;continue}if(x===r)m++;if(x===o){if(m--,m===0)return E}}throw Error(`Could not find matching ${o} in cloud config.`)}var BC=["rpx-gateway","nginx","php8.3-fpm","php8.2-fpm","mysql","mariadb","postgresql","redis","redis-server","meilisearch"],wn="-";function LC(e){let t=String.raw`
|
|
206
|
+
import { X509Certificate } from 'node:crypto'
|
|
207
|
+
import { existsSync, readdirSync, readFileSync, readlinkSync, statSync } from 'node:fs'
|
|
208
|
+
import { basename, join } from 'node:path'
|
|
209
|
+
|
|
210
|
+
const fragmentsRoot = '/etc/rpx/sites.d'
|
|
211
|
+
const certsRoot = '/etc/rpx/certs'
|
|
212
|
+
const projectFilter = ${JSON.stringify(e??"")}
|
|
213
|
+
const configuredRoutes = []
|
|
214
|
+
if (existsSync(fragmentsRoot)) {
|
|
215
|
+
for (const file of readdirSync(fragmentsRoot).filter(name => name.endsWith('.json')).sort()) {
|
|
216
|
+
try {
|
|
217
|
+
const fragment = JSON.parse(readFileSync(join(fragmentsRoot, file), 'utf8'))
|
|
218
|
+
const project = String(fragment.slug || basename(file, '.json'))
|
|
219
|
+
if (projectFilter && project !== projectFilter) continue
|
|
220
|
+
for (const route of Array.isArray(fragment.proxies) ? fragment.proxies : []) {
|
|
221
|
+
if (!route || !route.to) continue
|
|
222
|
+
const path = route.path || '/'
|
|
223
|
+
const staticRoot = typeof route.static === 'string' ? route.static : route.static?.dir
|
|
224
|
+
const kind = route.redirect ? 'redirect' : staticRoot ? 'static' : 'app'
|
|
225
|
+
const install = staticRoot || ''
|
|
226
|
+
const installName = install.match(/^\/var\/www\/(.+?)\/current(?:\/|$)/)?.[1]
|
|
227
|
+
const siteName = installName?.startsWith(project + '-') ? installName.slice(project.length + 1) : String(route.id || route.to)
|
|
228
|
+
const href = 'https://' + route.to + (path === '/' ? '' : path)
|
|
229
|
+
configuredRoutes.push({
|
|
230
|
+
name: siteName,
|
|
231
|
+
project,
|
|
232
|
+
route: path === '/' ? route.to : route.to + path,
|
|
233
|
+
href,
|
|
234
|
+
domain: route.to,
|
|
235
|
+
path,
|
|
236
|
+
kind,
|
|
237
|
+
runtime: kind === 'app' ? 'service' : kind,
|
|
238
|
+
deploy: kind === 'app' ? 'service' : kind === 'static' ? 'server static' : 'redirect',
|
|
239
|
+
tls: 'https',
|
|
240
|
+
root: install || (Array.isArray(route.from) ? route.from.join(', ') : route.from || 'gateway'),
|
|
241
|
+
branch: kind === 'static' ? 'build artifact' : 'main',
|
|
242
|
+
install,
|
|
243
|
+
})
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
catch {}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
async function mapWithConcurrency(values, limit, mapper) {
|
|
251
|
+
const results = new Array(values.length)
|
|
252
|
+
let next = 0
|
|
253
|
+
const workers = Array.from({ length: Math.min(limit, values.length) }, async () => {
|
|
254
|
+
while (next < values.length) {
|
|
255
|
+
const index = next++
|
|
256
|
+
results[index] = await mapper(values[index])
|
|
257
|
+
}
|
|
258
|
+
})
|
|
259
|
+
await Promise.all(workers)
|
|
260
|
+
return results
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
const routes = await mapWithConcurrency(configuredRoutes, 8, async route => {
|
|
264
|
+
const started = performance.now()
|
|
265
|
+
let httpStatus = 0
|
|
266
|
+
const probe = async (href) => {
|
|
267
|
+
const response = await fetch(href, {
|
|
268
|
+
method: 'GET',
|
|
269
|
+
redirect: 'manual',
|
|
270
|
+
headers: { 'user-agent': 'ts-cloud-monitor/1.0' },
|
|
271
|
+
signal: AbortSignal.timeout(6000),
|
|
272
|
+
})
|
|
273
|
+
const status = response.status
|
|
274
|
+
await response.arrayBuffer()
|
|
275
|
+
return status
|
|
276
|
+
}
|
|
277
|
+
try {
|
|
278
|
+
httpStatus = await probe(route.href)
|
|
279
|
+
if (httpStatus === 404 && route.kind === 'app') {
|
|
280
|
+
const healthHref = route.href.replace(/\/$/, '') + '/health'
|
|
281
|
+
httpStatus = await probe(healthHref)
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
catch {}
|
|
285
|
+
const responseMs = Math.max(0, Math.round(performance.now() - started))
|
|
286
|
+
let tlsDaysRemaining
|
|
287
|
+
const cert = join(certsRoot, route.domain + '.crt')
|
|
288
|
+
if (existsSync(cert)) {
|
|
289
|
+
try {
|
|
290
|
+
const expiresAt = Date.parse(new X509Certificate(readFileSync(cert)).validTo)
|
|
291
|
+
if (Number.isFinite(expiresAt)) tlsDaysRemaining = Math.floor((expiresAt - Date.now()) / 86400000)
|
|
292
|
+
}
|
|
293
|
+
catch {}
|
|
294
|
+
}
|
|
295
|
+
let release
|
|
296
|
+
let deployedAt
|
|
297
|
+
if (route.install) {
|
|
298
|
+
try {
|
|
299
|
+
release = basename(readlinkSync(route.install))
|
|
300
|
+
deployedAt = statSync(route.install).mtime.toISOString()
|
|
301
|
+
}
|
|
302
|
+
catch {}
|
|
303
|
+
}
|
|
304
|
+
const status = httpStatus === 0 || httpStatus >= 500 ? 'failed' : httpStatus === 404 ? 'degraded' : 'live'
|
|
305
|
+
const { install, ...site } = route
|
|
306
|
+
return {
|
|
307
|
+
...site,
|
|
308
|
+
status,
|
|
309
|
+
httpStatus,
|
|
310
|
+
responseMs,
|
|
311
|
+
tlsDaysRemaining,
|
|
312
|
+
checkedAt: new Date().toISOString(),
|
|
313
|
+
release,
|
|
314
|
+
deployedAt,
|
|
315
|
+
}
|
|
316
|
+
})
|
|
317
|
+
for (const route of routes) console.log('DISCOVERED_SITE=' + Buffer.from(JSON.stringify(route)).toString('base64'))
|
|
318
|
+
|
|
319
|
+
const serviceOutput = Bun.spawnSync([
|
|
320
|
+
'systemctl', 'list-units', '--type=service', '--state=running', '--no-legend', '--plain',
|
|
321
|
+
]).stdout.toString()
|
|
322
|
+
const runningUnits = serviceOutput
|
|
323
|
+
.split('\n')
|
|
324
|
+
.map(line => line.trim().split(/\s+/)[0])
|
|
325
|
+
.filter(Boolean)
|
|
326
|
+
if (runningUnits.length > 0) {
|
|
327
|
+
const propertiesOutput = Bun.spawnSync([
|
|
328
|
+
'systemctl',
|
|
329
|
+
'show',
|
|
330
|
+
...runningUnits,
|
|
331
|
+
'--property=Id,WorkingDirectory,ExecStart,MemoryCurrent,UnitFileState,ActiveEnterTimestamp',
|
|
332
|
+
]).stdout.toString()
|
|
333
|
+
for (const properties of propertiesOutput.split(/\n\n+/)) {
|
|
334
|
+
const values = Object.fromEntries(properties.split('\n').filter(Boolean).map(value => {
|
|
335
|
+
const separator = value.indexOf('=')
|
|
336
|
+
return separator < 0 ? [value, ''] : [value.slice(0, separator), value.slice(separator + 1)]
|
|
337
|
+
}))
|
|
338
|
+
const unit = values.Id
|
|
339
|
+
if (!unit) continue
|
|
340
|
+
if (!/^\/var\/www\/|^\/opt\/ts-cloud\//.test(values.WorkingDirectory || '') && !/\/var\/www\/|\/opt\/ts-cloud\//.test(values.ExecStart || '')) continue
|
|
341
|
+
console.log([
|
|
342
|
+
'SVC',
|
|
343
|
+
unit,
|
|
344
|
+
'active',
|
|
345
|
+
values.MemoryCurrent || '0',
|
|
346
|
+
values.UnitFileState || '-',
|
|
347
|
+
values.ActiveEnterTimestamp || '-',
|
|
348
|
+
].join('='))
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
`;return`echo '${Buffer.from(t).toString("base64")}' | base64 -d | bun -`}function FC(e={}){let t=["set +e",'echo "CPUS=$(nproc 2>/dev/null || echo 1)"',`read TS_TOTAL_1 TS_IDLE_1 <<EOF
|
|
352
|
+
$(awk '/^cpu /{idle=$5+$6; total=0; for(i=2;i<=NF;i++) total+=$i; print total, idle; exit}' /proc/stat 2>/dev/null)
|
|
353
|
+
EOF`,"sleep 0.2",`read TS_TOTAL_2 TS_IDLE_2 <<EOF
|
|
354
|
+
$(awk '/^cpu /{idle=$5+$6; total=0; for(i=2;i<=NF;i++) total+=$i; print total, idle; exit}' /proc/stat 2>/dev/null)
|
|
355
|
+
EOF`,`echo "CPUUSED=$(awk -v t1="$TS_TOTAL_1" -v i1="$TS_IDLE_1" -v t2="$TS_TOTAL_2" -v i2="$TS_IDLE_2" 'BEGIN { dt=t2-t1; di=i2-i1; if (dt>0) printf "%.1f", 100*(dt-di)/dt; else print 0 }')"`,`echo "LOAD=$(cut -d' ' -f1 /proc/loadavg 2>/dev/null || echo 0)"`,`echo "LOAD5=$(cut -d' ' -f2 /proc/loadavg 2>/dev/null || echo 0)"`,`echo "LOAD15=$(cut -d' ' -f3 /proc/loadavg 2>/dev/null || echo 0)"`,`read MEMTOTAL MEMUSED MEMAVAILABLE MEMCACHE SWAPTOTAL SWAPUSED <<EOF
|
|
356
|
+
$(free -m 2>/dev/null | awk '/^Mem:/{total=$2; used=$3; available=$7; cache=$6} /^Swap:/{print total, used, available, cache, $2, $3}')
|
|
357
|
+
EOF`,'echo "MEMTOTAL=${MEMTOTAL:-0}"','echo "MEMUSED=${MEMUSED:-0}"','echo "MEMAVAILABLE=${MEMAVAILABLE:-0}"','echo "MEMCACHE=${MEMCACHE:-0}"','echo "SWAPTOTAL=${SWAPTOTAL:-0}"','echo "SWAPUSED=${SWAPUSED:-0}"',`read DISKTOTG DISKUSEDG DISKPCT <<EOF
|
|
358
|
+
$(df -BG -P / 2>/dev/null | awk 'NR==2{gsub("G","",$2); gsub("G","",$3); gsub("%","",$5); print $2, $3, $5}')
|
|
359
|
+
EOF`,'echo "DISKPCT=${DISKPCT:-0}"','echo "DISKUSEDG=${DISKUSEDG:-0}"','echo "DISKTOTG=${DISKTOTG:-0}"',`echo "INODEPCT=$(df -Pi / 2>/dev/null | awk 'NR==2{gsub("%","",$5);print $5}')"`,`echo "NETRX=$(awk -F'[: ]+' 'NR>2 && $2!="lo"{sum+=$3} END{print sum+0}' /proc/net/dev 2>/dev/null)"`,`echo "NETTX=$(awk -F'[: ]+' 'NR>2 && $2!="lo"{sum+=$11} END{print sum+0}' /proc/net/dev 2>/dev/null)"`,". /var/lib/ts-cloud/bandwidth-state 2>/dev/null || true",'echo "NETRXRATE=${RX_RATE:-0}"','echo "NETTXRATE=${TX_RATE:-0}"','echo "NETRXTODAY=${DAY_RX:-0}"','echo "NETTXTODAY=${DAY_TX:-0}"','echo "NETRXMONTH=${MONTH_RX:-0}"','echo "NETTXMONTH=${MONTH_TX:-0}"','echo "NETMONTHKEY=${MONTH_KEY:-}"',`echo "PROCESSES=$(ps -e --no-headers 2>/dev/null | awk 'END{print NR+0}')"`,`echo "UPTIME=$(uptime -p 2>/dev/null | sed 's/^up //' || echo unknown)"`,`echo "UPTIME_SECONDS=$(cut -d' ' -f1 /proc/uptime 2>/dev/null || echo 0)"`,'echo "OS=$(. /etc/os-release 2>/dev/null; echo "$PRETTY_NAME")"'];if(e.includeServices!==!1)t.push(`for s in ${BC.join(" ")}; do st=$(systemctl is-active "$s" 2>/dev/null); if [ -n "$st" ] && [ "$st" != "inactive" ] && [ "$st" != "unknown" ]; then mem=$(systemctl show "$s" -p MemoryCurrent --value 2>/dev/null); en=$(systemctl is-enabled "$s" 2>/dev/null); since=$(systemctl show "$s" -p ActiveEnterTimestamp --value 2>/dev/null); echo "SVC=$s=$st=$mem=$en=$since"; fi; done`);if(e.includeSites!==!1)t.push(LC(e.projectFilter));return t.push("true"),t}function zC(e){let t={services:[],discoveredSites:[]};for(let r of e.split(`
|
|
360
|
+
`)){let o=r.trim();if(o.startsWith("DISCOVERED_SITE="))try{let m=JSON.parse(Buffer.from(o.slice(16),"base64").toString("utf8"));if(m?.domain&&m?.route)t.discoveredSites.push(m)}catch{}else if(o.startsWith("SVC=")){let[,m,h,I,R,A]=/^SVC=([^=]+)=([^=]+)(?:=([^=]*)(?:=([^=]*)(?:=(.*))?)?)?$/.exec(o)??[];if(m)t.services.push({name:m,status:h,memBytes:Number(I||0),enabled:R||wn,since:A||wn})}else{let m=o.indexOf("=");if(m>0)t[o.slice(0,m)]=o.slice(m+1)}}return t}function tb(e){let t=e.infrastructure?.compute,r=[];if(t?.webServer==="rpx"||t?.proxy?.engine==="rpx")r.push({name:"rpx-gateway",status:"configured"});else if(t?.webServer==="nginx"||t?.webServer==null)r.push({name:"ts-cloud-nginx",status:"configured"});let o=t?.php?.versions??(t?.runtime==="php"?["8.3"]:[]);for(let I of o)r.push({name:`php${I}-fpm`,status:"configured"});let m=t?.managedServices??{},h=[["mysql","mysql"],["mariadb","mariadb"],["postgres","postgresql"],["redis","redis"],["memcached","memcached"],["meilisearch","meilisearch"]];for(let[I,R]of h)if(m[I])r.push({name:R,status:"configured"});return r}function UC(e){let t=e.infrastructure?.compute?.backups,r=!!t?.enabled,o=t?.schedule??"0 2 * * *";return{enabled:r,schedule:o,destination:r?t?.bucket??"local":"off",retention:r?t?.retentionCount??5:0,last:r?"pending first run":"not configured",size:r?wn:"0 MB"}}function rb(e){return e.infrastructure?.compute?.provider??e.cloud?.provider??e.provider??"aws"}function qC(e){if(rb(e)==="hetzner")return Fa(e);return e.project.region??"us-east-1"}function VC(e){let t=e.infrastructure?.compute?.monitoring,r=Number(typeof t==="object"?t?.alerts?.bandwidthTb??0:0);return Number.isFinite(r)&&r>0?Math.round(r*1000000000000):0}function WC(e,t){let r=Be(g(e,t));if(!jC(r))return null;try{return JSON.parse(MC(r,"utf8"))}catch{return null}}function HC(e){let t=e.sites??{},r=[];for(let[o,m]of Object.entries(t)){let h=m.queues??m.workers;if(Array.isArray(h)){for(let I of h)if(typeof I==="string")r.push({name:`${o}:${I}`,site:o,processes:1,status:"configured"});else if(I?.name)r.push({name:`${o}:${I.name}`,site:o,processes:I.processes??1,status:"configured"})}}return r}function nb(e){if(!e||e==="")return"/";return e.startsWith("/")?e:`/${e}`}function KC(e){let t=nb(e.path);if(!e.domain)return{route:"internal"};return{route:t==="/"?e.domain:`${e.domain}${t}`,href:`https://${e.domain}${t==="/"?"":t}`}}function eb(e,t){let r=u(t);if(r==="redirect")return"redirect";if(r==="proxy")return"proxy";let o=String(t.build??"").toLowerCase(),m=String(t.start??"").toLowerCase();if(e==="main"||m.includes("buddy/src/cli.ts serve"))return"stacks";if(e==="api"||m.includes("/serve/api"))return"api";if(o.includes("buildblog"))return"bunpress blog";if(o.includes("bunpress"))return"bunpress";if(o.includes("site:build")||o.includes("stx"))return"stx static";if(r==="server-static")return t.spa?"spa":"static";if(r==="server-app")return"app";if(r==="server-php")return t.type??"php";return"bucket"}function JC(e){let t=u(e);if(t==="redirect"||t==="proxy")return"—";let r=`${e.start??""} ${e.build??""}`.toLowerCase();if(t==="server-static")return r.includes("bunpress")||r.includes("bun ")||r.includes("bunx ")?"static/bun":"static";if(t==="server-app")return r.includes("bun")?"bun":"node";if(t==="server-php")return`php ${e.php??e.phpVersion??"8.3"}`;return"static"}function GC(e){let t=u(e);if(t==="redirect")return"redirect";if(t==="proxy")return"proxy";if(t==="server-app")return"service";if(t==="server-static")return"server static";if(t==="server-php")return"php release";return"bucket"}function QC(e){let t=new Map;return Object.entries(e.sites??{}).map(([r,o])=>{let m=nb(o.path),{route:h,href:I}=KC(o),R=o.domain?`${o.domain}${m}`:"",A=R?t.get(R):void 0;if(R&&!A)t.set(R,r);return{name:r,route:h,href:I,domain:o.domain??"internal",path:m,kind:eb(r,o),type:eb(r,o),runtime:JC(o),deploy:GC(o),tls:o.domain?o.ssl===!1?"http":"https":"loopback",ssl:o.domain?o.ssl!==!1:!1,status:A?"shadowed":"live",shadowedBy:A,lastDeploy:wn}})}function YC(e,t,r){let o=new Map(e.map((m)=>[String(m.route),{...m,project:r,managedBy:"owner"}]));for(let m of t){let h=o.get(m.route),I=h?.name??`${m.project}:${m.name}`;o.set(m.route,{...h,...m,name:I,kind:h?.kind??m.kind,type:h?.type??m.kind,runtime:h?.runtime??m.runtime,deploy:h?.deploy??m.deploy,root:h?.root??m.root,branch:h?.branch??m.branch,managedBy:m.project===r?"owner":"attached"})}return[...o.values()].sort((m,h)=>{if(m.managedBy!==h.managedBy)return m.managedBy==="owner"?-1:1;return String(m.route).localeCompare(String(h.route))})}function yg(e){return`'${e.replaceAll("'","'\\''")}'`}function ib(e){return e.replace(/[\\&/]/g,"\\$&")}function ZC(e,t){if(e.length===0)return["true"];return["set +e",...e.map((r)=>{let o=yg(`${B(t,r)}/.ts-cloud/deploy-history.log`),m=ib(r);return`TS_CLOUD_HISTORY=${o}; { [ -f "$TS_CLOUD_HISTORY" ] && tail -n 24 "$TS_CLOUD_HISTORY" | sed "s/^/DEPLOY=${m}\\t/"; } || true`}),"true"]}function ob(e){return[...new Set(Object.values(e.sites??{}).map((t)=>t.domain).filter((t)=>typeof t==="string"&&t.length>0))]}function XC(e){return["set +e",'if command -v ss >/dev/null 2>&1; then ss -H -lntup 2>/dev/null | while read -r proto _state _recv _send local _peer rest; do [ -n "$proto" ] && [ -n "$local" ] && printf "PORT=%s\\t%s\\t%s\\n" "$proto" "$local" "$rest"; done; fi','if command -v ufw >/dev/null 2>&1; then ufw status numbered 2>/dev/null | sed "s/^/FIREWALL=/"; else echo "FIREWALL=ufw unavailable"; fi','journalctl _COMM=sshd --no-pager -n 30 -o short-iso 2>/dev/null | sed "s/^/AUTH=/" || true',...ob(e).map((r)=>`TS_CLOUD_DOMAIN=${yg(r)}; TS_CLOUD_EXPIRY=$(echo | timeout 8 openssl s_client -servername "$TS_CLOUD_DOMAIN" -connect "$TS_CLOUD_DOMAIN:443" 2>/dev/null | openssl x509 -noout -enddate 2>/dev/null | cut -d= -f2-); [ -n "$TS_CLOUD_EXPIRY" ] && printf "CERT=%s\\t%s\\n" "$TS_CLOUD_DOMAIN" "$TS_CLOUD_EXPIRY" || printf "CERT=%s\\tunavailable\\n" "$TS_CLOUD_DOMAIN"`),"true"]}function eP(e){let t=new Map,r=(h,I)=>{if(h&&I&&!t.has(I))t.set(I,{pattern:h,label:I})},o=e.infrastructure?.compute,m=e.project.slug;if(o?.webServer==="rpx"||o?.proxy?.engine==="rpx")r("rpx-gateway","rpx-gateway");else r("nginx","nginx");for(let h of tb(e))r(h.name,h.name);for(let[h,I]of Object.entries(e.sites??{})){if(u(I)!=="server-app")continue;if(r(`${m}-${h}`,`${m}-${h}`),Array.isArray(I.queues??I.workers)&&(I.queues??I.workers).length)r(`${m}-${h}-queue-*`,`${m}-${h}-queues`);if(Array.isArray(I.daemons)&&I.daemons.length)r(`${m}-${h}-daemon-*`,`${m}-${h}-daemons`)}return[...t.values()]}function tP(e){let t=eP(e);if(t.length===0)return["true"];return["set +e",...t.map((r)=>{let o=yg(r.pattern),m=ib(r.label);return`journalctl -u ${o} --no-pager -n 150 -o short-iso 2>/dev/null | sed "s/^/LOG=${m}\\t/" || true`}),'[ -f /var/log/ts-cloud-backup.log ] && tail -n 60 /var/log/ts-cloud-backup.log 2>/dev/null | sed "s/^/LOG=backups\\t/" || true',"true"]}function vg(e){let t=new Date(e);if(Number.isNaN(t.getTime()))return e||wn;let r=Math.max(0,Math.round((Date.now()-t.getTime())/1000));if(r<60)return`${r}s ago`;let o=Math.round(r/60);if(o<60)return`${o}m ago`;let m=Math.round(o/60);if(m<48)return`${m}h ago`;return`${Math.round(m/24)}d ago`}function sb(e){if(/(?:error|failed|panic|fatal|exception|denied|unhealthy)/i.test(e))return"error";if(/(?:warn|warning|retry|restart|deprecated|timeout)/i.test(e))return"warn";return"info"}function rP(e){let t=[];for(let r of e.split(`
|
|
361
|
+
`)){let o=r.trim();if(!o.startsWith("LOG="))continue;let m=o.indexOf("\t");if(m<0)continue;let h=o.slice(4,m),I=o.slice(m+1),R=/^(\d{4}-\d{2}-\d{2}T[^\s]+)\s+(\S+)\s+(.*)$/.exec(I),A=R?.[1]??"",E=R?.[2]??"",x=(R?.[3]??I).trim();if(!x)continue;t.push({source:h,timestamp:A,when:A?vg(A):wn,host:E,message:x,level:sb(x)})}return t.sort((r,o)=>String(o.timestamp).localeCompare(String(r.timestamp))).slice(0,600)}function ab(e){if(/^(?:127\.|localhost:|\[?::1\]?)/.test(e))return"loopback";if(/^(?:10\.|192\.168\.|172\.(?:1[6-9]|2\d|3[01])\.|169\.254\.|\[?f[cd][0-9a-f:])/i.test(e))return"private";return"public"}function nP(e){if(ab(e)!=="public")return"ok";if(/:(?:22|80|443)$/.test(e)||/^\*:(?:22|80|443)$/.test(e))return"ok";return"warn"}function iP(e){if(e==null)return"warn";if(e<8)return"bad";if(e<30)return"warn";return"ok"}function oP(e){let t=[],r=[],o=[],m=[];for(let R of e.split(`
|
|
362
|
+
`)){let A=R.trim();if(A.startsWith("PORT=")){let[E="",x="",N=""]=A.slice(5).split("\t");if(E&&x)t.push({proto:E,listen:x,processName:N||wn,exposure:ab(x),tone:nP(x)})}else if(A.startsWith("FIREWALL=")){let E=A.slice(9).trim();if(E)r.push(E)}else if(A.startsWith("AUTH=")){let E=A.slice(5),x=/^(\d{4}-\d{2}-\d{2}T[^\s]+)\s+(\S+)\s+(.*)$/.exec(E),N=(x?.[3]??E).trim();if(N)o.push({timestamp:x?.[1]??"",when:x?.[1]?vg(x[1]):wn,host:x?.[2]??"",message:N,level:sb(N)})}else if(A.startsWith("CERT=")){let[E="",x=""]=A.slice(5).split("\t");if(!E)continue;let N=new Date(x),F=Number.isNaN(N.getTime())?null:Math.ceil((N.getTime()-Date.now())/86400000);m.push({domain:E,expires:Number.isNaN(N.getTime())?x||wn:N.toISOString().slice(0,10),daysRemaining:F,status:iP(F)})}}let h=r.some((R)=>/Status:\s*active/i.test(R)),I=r.some((R)=>/unavailable/i.test(R));return{ports:t.slice(0,80),firewall:{status:h?"active":I?"unavailable":"inactive",summary:h?"ufw active":I?"ufw unavailable":"ufw inactive or not configured",rules:r.filter((R)=>R&&!/^Status:/i.test(R)).slice(0,60)},tlsCertificates:m,authEvents:o.sort((R,A)=>String(A.timestamp).localeCompare(String(R.timestamp))).slice(0,30)}}function bg(e){let r=e.infrastructure?.compute?.firewall??{},o=[...new Set([22,80,443,...r.allowedPorts??[]])],m=ob(e);return{ports:o.map((h)=>({proto:"tcp",listen:`0.0.0.0:${h}`,processName:"configured firewall",exposure:"public",tone:[22,80,443].includes(Number(h))?"ok":"warn"})),firewall:{status:r.enabled===!1?"disabled":"configured",summary:r.enabled===!1?"host firewall disabled in config":"host firewall configured declaratively",rules:o.map((h)=>`ALLOW ${h}/tcp`)},tlsCertificates:m.map((h)=>({domain:h,expires:wn,daysRemaining:null,status:"warn"})),authEvents:[]}}function cb(e,t){let r=t.security??bg(e),o=!!t._serverReachable&&!t.metricsUnavailable,m=t.sites??[],h=m.filter((E)=>E.status==="shadowed"),I=(t.services??[]).filter((E)=>["failed","stopped"].includes(E.status)),R=(r.tlsCertificates??[]).filter((E)=>E.status!=="ok"),A=t.sshKeys??[];return[{name:"Live server probe",status:o?"pass":"warn",detail:o?"Metrics and remote checks are coming from the compute box.":"The dashboard is rendering config/state data until the box probe succeeds."},{name:"Managed services",status:I.length?"fail":"pass",detail:I.length?`${I.length} service(s) need attention.`:`${(t.services??[]).length} service(s) reported healthy or configured.`},{name:"Route conflicts",status:h.length?"warn":"pass",detail:h.length?`${h.map((E)=>E.name).join(", ")} is shadowed by an earlier route.`:`${m.length} site route(s) are unshadowed.`},{name:"SSH access",status:A.length?"pass":"warn",detail:A.length?`${A.length} declarative authorized key(s) configured.`:"No declarative SSH keys are configured."},{name:"Firewall",status:["active","configured"].includes(r.firewall?.status)?"pass":"warn",detail:r.firewall?.summary??"Firewall status unavailable."},{name:"TLS certificates",status:R.length?"warn":"pass",detail:R.length?`${R.length} certificate(s) need renewal visibility.`:`${(r.tlsCertificates??[]).length} certificate(s) look healthy.`}]}function db(e){let t=[];for(let r of e.serverDeploymentsDetail??e.serverDeployments??[])t.push({type:"deploy",tone:r.status==="failed"?"bad":"ok",title:`${r.site} deployed ${r.sha}`,detail:`${r.branch} · ${r.status}`,when:r.when,timestamp:r.timestamp});for(let r of e.serverLogs??[])if(r.level==="error"||r.level==="warn")t.push({type:"log",tone:r.level==="error"?"bad":"warn",title:`${r.source} ${r.level}`,detail:r.message,when:r.when,timestamp:r.timestamp});for(let r of e.sshKeys??[])t.push({type:"ssh",tone:"ok",title:`${r.name} authorized`,detail:r.fingerprint,when:r.added??wn,timestamp:""});return t.sort((r,o)=>String(o.timestamp).localeCompare(String(r.timestamp))).slice(0,80)}function sP(e,t={}){let r=[];for(let o of e.split(`
|
|
363
|
+
`)){let m=o.trim();if(!m.startsWith("DEPLOY="))continue;let h=m.split("\t"),I=h[0]?.replace(/^DEPLOY=/,"")??"",[R,A,E,x,N]=h.slice(1);if(!I||!R||!A)continue;let F=t[I]??{},z=u(F),K=(E||A).slice(0,7);r.push({sha:K,release:A,commit:E||A,site:I,branch:F.branch??(z==="server-static"?"build artifact":"main"),status:x||"unknown",when:vg(R),timestamp:R,took:"-",by:"ts-cloud",rc:N?.replace(/^rc=/,"")??"",steps:z==="server-static"?["upload artifact","publish static files"]:["upload artifact","restart service"]})}return r.sort((o,m)=>String(m.timestamp).localeCompare(String(o.timestamp)))}function aP(e,t){let r=WC(e,t),o=tb(e),m=QC(e),h=Math.max(1,Number(e.infrastructure?.compute?.disk?.size??1)),I={server:{name:r?.serverName??`${e.project.slug}-${t}-app`,provider:r?.provider??rb(e),region:qC(e),ip:r?.publicIp??wn,os:"Linux",uptime:wn,probeStatus:"unavailable"},systemMetrics:{load:0,load5:0,load15:0,cpuUsedPct:0,cpus:Math.max(1,Number(e.infrastructure?.compute?.instances??1)),memUsedMb:0,memTotalMb:0,memAvailableMb:0,memCacheMb:0,swapUsedMb:0,swapTotalMb:0,diskUsedPct:0,diskUsedGb:0,diskTotalGb:h,inodeUsedPct:0,networkRxBytes:0,networkTxBytes:0,networkRxBytesPerSec:0,networkTxBytesPerSec:0,networkRxBytesToday:0,networkTxBytesToday:0,networkRxBytesMonth:0,networkTxBytesMonth:0,networkMonthKey:"",processes:0,uptimeSeconds:0},metricsUnavailable:!0,bandwidthBudgetBytes:VC(e),services:o,servicesDetail:o.map((R)=>({...R,since:wn,memMb:0,auto:!0})),backup:UC(e),backupHistory:[],workers:HC(e),serverScheduler:{enabled:!1,lastRun:"not configured"},serverDeployments:[],serverDeploymentsDetail:[],deploymentsEmptyReason:"No deployment history has been recorded yet. Future server deploys will write /var/www/<site>/.ts-cloud/deploy-history.log.",serverLogs:[],serverLogsEmptyReason:"Live server logs are unavailable until the dashboard can reach the compute box.",security:bg(e),securityEmptyReason:"Live security checks are unavailable until the dashboard can reach the compute box.",sites:m,sitesDetail:m.map((R)=>{let A=e.sites?.[R.name]??{},E=u(A);return{...R,root:E==="server-static"?B(e.project.slug,R.name):`${B(e.project.slug,R.name)}/current`,branch:E==="server-static"?"build artifact":"main",build:A.build,php:A.php??A.phpVersion,aliases:Array.isArray(A.aliases)?A.aliases:[],redirects:A.redirects&&typeof A.redirects==="object"?A.redirects:{},envKeys:Object.keys(A.env??A.environment??{})}}),siteHealth:[],sshKeys:fs(e.infrastructure?.compute?.sshKeys??[]),_serverReachable:!1,_metricsStatus:"unavailable"};return I.diagnostics=cb(e,I),I.activity=db(I),I.topology=ht(I,{project:e.project.name,environment:t}),I}async function Su(e,t,r={}){if(!e.infrastructure?.compute)return null;let o=null;try{o=k({config:e})}catch{o=null}let m=null,h=0,I=[];if(o)try{if(I=await o.findComputeTargets({slug:e.project.slug,environment:t,role:"app"}),h=I.length,I.length){let N=(await o.runRemoteDeploy({targets:[I[0]],commands:FC({includeServices:!r.telemetryOnly,includeSites:r.includeSiteHealth!==!1,projectFilter:e.cloud?.attachTo?e.project.slug:void 0}),comment:`ts-cloud dashboard:build ${e.project.slug}`,tags:{Project:e.project.slug,Environment:t,Role:"app"}})).perInstance?.[0]?.output;if(N)m=zC(N)}}catch{}let R=aP(e,t),A=(x,N=0)=>{let F=Number(x);return Number.isFinite(F)?F:N};if(R.server.os=m?.OS||R.server.os,R.server.uptime=m?.UPTIME||R.server.uptime,m){if(R.server.probeStatus="live",R.metricsUnavailable=!1,R._metricsStatus="live",R.systemMetrics={load:A(m.LOAD),load5:A(m.LOAD5),load15:A(m.LOAD15),cpuUsedPct:A(m.CPUUSED),cpus:A(m.CPUS,1),memUsedMb:A(m.MEMUSED),memTotalMb:A(m.MEMTOTAL,1),memAvailableMb:A(m.MEMAVAILABLE),memCacheMb:A(m.MEMCACHE),swapUsedMb:A(m.SWAPUSED),swapTotalMb:A(m.SWAPTOTAL),diskUsedPct:A(m.DISKPCT),diskUsedGb:A(m.DISKUSEDG),diskTotalGb:A(m.DISKTOTG,1),inodeUsedPct:A(m.INODEPCT),networkRxBytes:A(m.NETRX),networkTxBytes:A(m.NETTX),networkRxBytesPerSec:A(m.NETRXRATE),networkTxBytesPerSec:A(m.NETTXRATE),networkRxBytesToday:A(m.NETRXTODAY),networkTxBytesToday:A(m.NETTXTODAY),networkRxBytesMonth:A(m.NETRXMONTH),networkTxBytesMonth:A(m.NETTXMONTH),networkMonthKey:m.NETMONTHKEY||"",processes:A(m.PROCESSES),uptimeSeconds:A(m.UPTIME_SECONDS)},m.services.length){let x=[...new Map(m.services.map((N)=>[N.name,N])).values()];R.services=x.map((N)=>({name:N.name,status:N.status==="active"?"running":N.status})),R.servicesDetail=x.map((N)=>({name:N.name,status:N.status==="active"?"running":N.status,since:N.since&&N.since!==wn?N.since:R.server.uptime,memMb:Math.max(0,Math.round(Number(N.memBytes??0)/1024/1024)),auto:N.enabled?!["disabled","static","masked"].includes(N.enabled):!0}))}if(m.discoveredSites.length)R.sitesDetail=YC(R.sitesDetail,m.discoveredSites,e.project.slug),R.sites=R.sitesDetail.map((x)=>({name:x.name,project:x.project,managedBy:x.managedBy,route:x.route,href:x.href,domain:x.domain,path:x.path,kind:x.kind,type:x.type,runtime:x.runtime,deploy:x.deploy,tls:x.tls,status:x.status,httpStatus:x.httpStatus,responseMs:x.responseMs,tlsDaysRemaining:x.tlsDaysRemaining,checkedAt:x.checkedAt})),R.siteHealth=R.sitesDetail.map((x)=>({name:x.name,project:x.project,route:x.route,status:x.status,httpStatus:x.httpStatus,responseMs:x.responseMs,tlsDaysRemaining:x.tlsDaysRemaining,checkedAt:x.checkedAt}))}if(o&&I.length&&!r.telemetryOnly)await Promise.all([(async()=>{try{let x=Object.keys(e.sites??{}),F=(await o.runRemoteDeploy({targets:[I[0]],commands:ZC(x,e.project.slug),comment:`ts-cloud dashboard:deploy-history ${e.project.slug}`,tags:{Project:e.project.slug,Environment:t,Role:"app"}})).perInstance?.[0]?.output??"",z=sP(F,e.sites);R.serverDeployments=z.slice(0,5),R.serverDeploymentsDetail=z.slice(0,50),R.deploymentsEmptyReason=z.length?void 0:"No deployment history was found on the server yet. Deploy again to populate this timeline."}catch{R.deploymentsEmptyReason="Deployment history could not be read from the server."}})(),(async()=>{try{let N=(await o.runRemoteDeploy({targets:[I[0]],commands:tP(e),comment:`ts-cloud dashboard:server-logs ${e.project.slug}`,tags:{Project:e.project.slug,Environment:t,Role:"app"}})).perInstance?.[0]?.output??"",F=rP(N);R.serverLogs=F,R.serverLogsEmptyReason=F.length?void 0:"No recent journal entries were found for the managed server units."}catch{R.serverLogsEmptyReason="Server logs could not be read from the box."}})(),(async()=>{try{let N=(await o.runRemoteDeploy({targets:[I[0]],commands:XC(e),comment:`ts-cloud dashboard:security ${e.project.slug}`,tags:{Project:e.project.slug,Environment:t,Role:"app"}})).perInstance?.[0]?.output??"",F=oP(N),z=bg(e);if(!F.ports.length)F.ports=z.ports;if(!F.tlsCertificates.length)F.tlsCertificates=z.tlsCertificates;R.security=F,R.securityEmptyReason=void 0}catch{R.securityEmptyReason="Server security checks could not be read from the box."}})()]);else if(o&&h===0)R.deploymentsEmptyReason="No app server target was found for this environment.";let E=e.infrastructure?.compute?.sshKeys??e.infrastructure?.ssh?.keys??e.infrastructure?.sshKeys;if(Array.isArray(E)&&E.length)R.sshKeys=fs(E);return R._serverReachable=h>0&&!!m,R.diagnostics=cb(e,R),R.activity=db(R),R.topology=ht(R,{project:e.project.name,environment:t}),R}var cP=new Set(["singlestore","vitess"]);function Eu(e){return cP.has(e)}var Iu={mysql:"/var/lib/pantry/mysql/mysqld.sock",mariadb:"/var/lib/pantry/mariadb/mariadbd.sock"};function Yi(e){return/^[A-Za-z_][A-Za-z0-9_]{0,62}$/.test(e)}function dP(e){if(e==="postgres"||e==="pgsql")return"postgres";if(e==="mariadb")return"mariadb";if(e==="singlestore")return"singlestore";if(e==="vitess")return"vitess";return"mysql"}function Vs(e){let t=e.infrastructure?.compute,r=p(e)?.engine;if(r)return dP(r);let o=t?.managedServices??{};if(o.postgres)return"postgres";if(o.mariadb)return"mariadb";return"mysql"}var Sg=(e)=>e.replace(/`/g,"``"),ui=(e)=>e.replace(/\\/g,"\\\\").replace(/'/g,"\\'"),qs=(e)=>`"${e.replace(/"/g,'""')}"`,ku=(e)=>`'${e.replace(/'/g,"''")}'`;function wg(e,t){return[`mysql --socket=${e==="mariadb"?Iu.mariadb:Iu.mysql} -u root <<'TS_CLOUD_SQL_EOF'`,...t,"TS_CLOUD_SQL_EOF"]}function kg(e,t){return[`${wt(t)} -tA <<'TS_CLOUD_PG_EOF'`,...e,"TS_CLOUD_PG_EOF"]}function lP(e,t){if(e==="vitess")return uP(t);if(e==="postgres")return kg(["SELECT 'DB=' || datname FROM pg_database WHERE datistemplate = false;","SELECT 'USER=' || usename FROM pg_user;"],t);return wg(e,["SELECT CONCAT('DB=', schema_name) FROM information_schema.schemata WHERE schema_name NOT IN ('information_schema', 'mysql', 'performance_schema', 'sys');","SELECT DISTINCT CONCAT('USER=', User) FROM mysql.user WHERE User NOT IN ('root', 'mysql.sys', 'mysql.session', 'mysql.infoschema', 'debian-sys-maint');"])}function za(e,t,r){let o=t?.host??"127.0.0.1",m=t?.username??"root",h=t?.password??"",I=t?.ssl===!1?"":" --ssl-mode=PREFERRED",R=(E)=>`'${String(E).replace(/'/g,"'\\''")}'`;return[`${h?`MYSQL_PWD=${R(h)} `:""}mysql -h ${R(o)} -P ${t?.port??r} -u ${R(m)}${I} --batch --skip-column-names <<'TS_CLOUD_SQL_EOF'`,...e,"TS_CLOUD_SQL_EOF"]}var Ua=15306;function uP(e){let t=za(["SHOW KEYSPACES;"],e,Ua);return t[0]=`${t[0]} | sed 's/^/DB=/'`,t}function pP(e){return za(["SHOW KEYSPACES;","SHOW VITESS_SHARDS;","SHOW VITESS_TABLETS;"],e,Ua)}function mP(e){let t=[],r=[],o=[];for(let m of e.split(`
|
|
364
|
+
`)){let h=m.trim();if(!h)continue;let I=h.split("\t").map((R)=>R.trim()).filter(Boolean);if(I.length>=6){let[R,A,E,x,N,F,z]=I;o.push({cell:R??"",keyspace:A??"",shard:E??"",type:x??"",state:N??"",alias:F??"",hostname:z??""});continue}if(I.length===1){let R=I[0];if(R.includes("/")){let[A,E]=R.split("/");if(A&&E)r.push({keyspace:A,shard:E})}else t.push(R)}}return{keyspaces:[...new Set(t)].filter(Boolean).sort(),shards:r,tablets:o}}function gP(e,t,r){if(e==="postgres")return kg([`SELECT 'CREATE DATABASE ${qs(t)}' WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = ${ku(t)})\\gexec`],r);return wg(e,[`CREATE DATABASE IF NOT EXISTS \`${Sg(t)}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;`])}function fP(e,t,r){let{username:o,password:m,database:h,access:I}=t;if(e==="postgres"){let E=["DO $$ BEGIN",` IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = ${ku(o)}) THEN CREATE ROLE ${qs(o)} LOGIN PASSWORD ${ku(m)};`,` ELSE ALTER ROLE ${qs(o)} LOGIN PASSWORD ${ku(m)}; END IF;`,"END $$;"];if(h)E.push(I==="readonly"?`GRANT CONNECT ON DATABASE ${qs(h)} TO ${qs(o)};`:`GRANT ALL PRIVILEGES ON DATABASE ${qs(h)} TO ${qs(o)};`);return kg(E,r)}let R=I==="readonly"?"SELECT":"ALL PRIVILEGES",A=[`CREATE USER IF NOT EXISTS '${ui(o)}'@'%' IDENTIFIED BY '${ui(m)}';`,`CREATE USER IF NOT EXISTS '${ui(o)}'@'localhost' IDENTIFIED BY '${ui(m)}';`,`ALTER USER '${ui(o)}'@'%' IDENTIFIED BY '${ui(m)}';`,`ALTER USER '${ui(o)}'@'localhost' IDENTIFIED BY '${ui(m)}';`];if(h)A.push(`GRANT ${R} ON \`${Sg(h)}\`.* TO '${ui(o)}'@'%';`,`GRANT ${R} ON \`${Sg(h)}\`.* TO '${ui(o)}'@'localhost';`);return A.push("FLUSH PRIVILEGES;"),wg(e,A)}function hP(e){let t=[],r=[];for(let o of e.split(`
|
|
365
|
+
`)){let m=o.trim();if(m.startsWith("DB="))t.push(m.slice(3));else if(m.startsWith("USER="))r.push(m.slice(5))}return{databases:[...new Set(t)].filter(Boolean).sort(),users:[...new Set(r)].filter(Boolean).sort()}}async function kn(e,t,r,o){let m;try{m=k({config:e})}catch(A){return{ok:!1,error:`Could not initialize the cloud driver: ${A?.message??A}`}}let h=e.project.slug,I=await m.findComputeTargets({slug:h,environment:t,role:"app"});if(!I.length)return{ok:!1,error:"No app server target was found for this environment."};let R=await m.runRemoteDeploy({targets:[I[0]],commands:["set -uo pipefail",...r],comment:o,tags:{Project:h,Environment:t,Role:"app"}});return{ok:R.success,stdout:R.perInstance?.[0]?.output??"",stderr:R.perInstance?.[0]?.error??R.error??""}}function Ru(e,t){return{ok:!1,error:`${t} is not available for ${e}: it is an external cluster, so this box has no engine to administer. ${e==="vitess"?"Create a keyspace with `vtctldclient CreateKeyspace`, and manage vtgate credentials on the cluster.":"Use the provider console or the cluster's own admin connection."}`}}async function lb(e,t){let r=Vs(e);if(Eu(r)&&r!=="vitess")return{...Ru(r,"Listing databases"),engine:r,databases:[],users:[]};let o=await kn(e,t,lP(r,p(e)),"ts-cloud db:list"),m=o.ok&&o.stdout?hP(o.stdout):{databases:[],users:[]};return{...o,engine:r,...m}}async function ub(e,t,r){let o=Vs(e);if(Eu(o))return Ru(o,"Creating a database");return kn(e,t,gP(o,r,p(e)),`ts-cloud db:create ${r}`)}async function pb(e,t,r){let o=Vs(e);if(Eu(o))return Ru(o,"Creating a database user");return kn(e,t,fP(o,r,p(e)),`ts-cloud db:user ${r.username}`)}var Ig="/var/backups/ts-cloud/databases";function yP(e,t,r=Ig,o){let h=`${r}/${t}-$TS_CLOUD_BACKUP_STAMP.sql.gz`,I=`mkdir -p ${r}`;if(e==="postgres")return[I,'TS_CLOUD_BACKUP_STAMP="$(date +%Y%m%d-%H%M%S)"',`${wt(o,"pg_dump")} ${t} | gzip > "${h}"`,`echo "BACKUP=${h}"`,`ls -l "${h}"`];let R=e==="mariadb"?Iu.mariadb:Iu.mysql;return[I,'TS_CLOUD_BACKUP_STAMP="$(date +%Y%m%d-%H%M%S)"',`mysqldump --socket=${R} -u root ${t} | gzip > "${h}"`,`echo "BACKUP=${h}"`,`ls -l "${h}"`]}function vP(e=Ig){return[`ls -1t ${e}/*.sql.gz 2>/dev/null | head -50 | sed 's|^|BACKUP=|' || true`]}function bP(e){let t=[];for(let r of e.split(`
|
|
366
|
+
`)){let o=r.trim();if(!o.startsWith("BACKUP="))continue;let m=o.slice(7),I=(m.split("/").pop()??m).replace(/-\d{8}-\d{6}\.sql\.gz$/,"");if(m)t.push({file:m,database:I})}return t}async function mb(e,t,r){if(!Yi(r))return{ok:!1,error:"Database name must be a valid identifier.",database:r};let o=Vs(e);if(Eu(o))return{...Ru(o,"Backing up"),database:r};return{...await kn(e,t,yP(o,r,Ig,p(e)),`ts-cloud db:backup ${r}`),database:r}}async function gb(e,t){let r=await kn(e,t,vP(),"ts-cloud db:backups"),o=r.ok&&r.stdout?bP(r.stdout):[];return{...r,backups:o}}async function fb(e,t){let r=Vs(e),o={keyspaces:[],shards:[],tablets:[]};if(r!=="vitess")return{ok:!1,error:"The configured database engine is not Vitess.",engine:r,...o};let m=await kn(e,t,pP(p(e)),"ts-cloud vitess:topology"),h=m.ok&&m.stdout?mP(m.stdout):o;return{...m,engine:r,...h}}function hb(e){return e.filter((t)=>t.state.toUpperCase()!=="SERVING")}function yb(e){let t=new Set(e.tablets.filter((r)=>r.type.toUpperCase()==="PRIMARY"&&r.state.toUpperCase()==="SERVING").map((r)=>`${r.keyspace}/${r.shard}`));return e.shards.filter((r)=>!t.has(`${r.keyspace}/${r.shard}`))}var Eg="/usr/local/bin/vtctldclient",SP="21.0.0";function Cu(e){return`'${String(e).split("'").join("'\\''")}'`}function Pu(e){return p(e)?.vitess??{}}function vb(e){return/^[A-Za-z_][A-Za-z0-9_]{0,63}$/.test(e)}function wP(e){return/^[0-9a-f]{8}(?:_[0-9a-f]{4}){3}_[0-9a-f]{12}$/i.test(e)}function kP(e=SP){let t=Cu(e);return[`if ${Eg} --version 2>/dev/null | grep -q ${t}; then echo "vtctldclient ${e} already installed"; else`," command -v curl >/dev/null 2>&1 || (apt-get update -y && apt-get install -y curl ca-certificates)",' if [ "$(uname -m)" != "x86_64" ]; then',' echo "vtctldclient: Vitess publishes only an x86_64 release tarball; this box is $(uname -m). Build from source or install via pantry." >&2'," exit 1"," fi",' tmp="$(mktemp -d)"',` api="https://api.github.com/repos/vitessio/vitess/releases/tags/v${e}"`,` url="$(curl -fsSL "$api" | grep -oE '"browser_download_url"[[:space:]]*:[[:space:]]*"[^"]+vitess-[0-9][^"]*\\.tar\\.gz"' | head -1 | sed -E 's/.*"(https[^"]+)"/\\1/')"`,' if [ -z "$url" ]; then echo "vtctldclient: no release tarball found for v'+e+'" >&2; rm -rf "$tmp"; exit 1; fi',` curl -fsSL "$url" | tar -xz -C "$tmp" --strip-components=2 --wildcards --occurrence=1 '*/bin/vtctldclient' || true`,' if [ ! -s "$tmp/vtctldclient" ]; then',' echo "vtctldclient: could not extract the binary from $url" >&2',' rm -rf "$tmp"; exit 1'," fi",` install -m 0755 "$tmp/vtctldclient" ${Eg}`,' rm -rf "$tmp"',` echo "installed vtctldclient ${e}"`,"fi"]}function bb(e){if(!e.vtctldAddr)return null;return`${Eg} --server ${Cu(e.vtctldAddr)}`}function IP(e,t,r={}){let o=bb(e);if(!o)return null;let m=r.sharded?" --sharded":"";return[`${o} CreateKeyspace${m} ${Cu(t)}`]}function EP(e,t,r){let o=bb(e);if(!o)return null;return['vschema_tmp="$(mktemp)"',`cat > "$vschema_tmp" <<'TS_CLOUD_VSCHEMA_EOF'`,r,"TS_CLOUD_VSCHEMA_EOF",`${o} ApplyVSchema --vschema-file "$vschema_tmp" ${Cu(t)}`,'rm -f "$vschema_tmp"']}function RP(e,t,r="vitess"){return za([`SET @@ddl_strategy='${r}';`,t.endsWith(";")?t:`${t};`],e,Ua)}function AP(e,t){let r=t?[`USE ${t.replace(/[^A-Z0-9_]/gi,"")};`,"SHOW VITESS_MIGRATIONS;"]:["SHOW VITESS_MIGRATIONS;"],o=za(r,e,Ua);return o[0]=o[0].replace(" --skip-column-names",""),o}function CP(e){let t=e.split(`
|
|
367
|
+
`).map((I)=>I.trimEnd()).filter((I)=>I.trim().length>0);if(t.length===0)return[];let r=t[0].split("\t").map((I)=>I.trim()),o=(I)=>r.indexOf(I),m={uuid:o("migration_uuid"),keyspace:o("keyspace"),shard:o("shard"),table:o("mysql_table"),status:o("migration_status"),strategy:o("strategy"),added:o("added_timestamp"),completed:o("completed_timestamp"),progress:o("progress")};if(m.uuid===-1)return[];let h=[];for(let I of t.slice(1)){let R=I.split("\t"),A=(x)=>x>=0?(R[x]??"").trim():"",E=A(m.uuid);if(!E)continue;h.push({uuid:E,keyspace:A(m.keyspace),shard:A(m.shard),table:A(m.table),status:A(m.status),strategy:A(m.strategy),added:A(m.added),completed:A(m.completed),progress:A(m.progress)})}return h}function PP(e){return e.filter((t)=>t.status.toLowerCase()==="failed")}function _P(e){return e.filter((t)=>["running","queued","ready"].includes(t.status.toLowerCase()))}function TP(e,t,r){let o=r.toUpperCase();return za([`ALTER VITESS_MIGRATION '${t}' ${o};`],e,Ua)}function qa(e){if(Vs(e)!=="vitess")return{ok:!1,error:"The configured database engine is not Vitess."};return null}function Au(e){if(!e.vtctldAddr)return{ok:!1,error:"No vtctld address is configured. Creating keyspaces and applying a VSchema are topology operations that vtgate cannot perform. Set `infrastructure.appDatabase.vitess.vtctldAddr` (for example `vtctld.internal:15999`) and redeploy."};return null}async function Sb(e,t){let r=qa(e);if(r)return r;let o=Pu(e);return kn(e,t,kP(o.clientVersion),"ts-cloud vitess:install-client")}async function wb(e,t,r,o={}){let m=qa(e);if(m)return{...m,keyspace:r};if(!vb(r))return{ok:!1,error:"Keyspace name must be a valid identifier.",keyspace:r};let h=Pu(e),I=Au(h);if(I)return{...I,keyspace:r};let R=IP(h,r,o);if(!R)return{...Au({}),keyspace:r};return{...await kn(e,t,R,`ts-cloud vitess:create-keyspace ${r}`),keyspace:r}}async function kb(e,t,r,o){let m=qa(e);if(m)return{...m,keyspace:r};if(!vb(r))return{ok:!1,error:"Keyspace name must be a valid identifier.",keyspace:r};try{let E=JSON.parse(o);if(!E||typeof E!=="object")return{ok:!1,error:"The VSchema must be a JSON object.",keyspace:r}}catch(E){return{ok:!1,error:`The VSchema is not valid JSON: ${E?.message??E}`,keyspace:r}}let h=Pu(e),I=Au(h);if(I)return{...I,keyspace:r};let R=EP(h,r,o);if(!R)return{...Au({}),keyspace:r};return{...await kn(e,t,R,`ts-cloud vitess:apply-vschema ${r}`),keyspace:r}}async function Ib(e,t,r){let o=qa(e);if(o)return o;if(!r.trim())return{ok:!1,error:"No SQL was provided."};let m=Pu(e);return kn(e,t,RP(p(e),r,m.ddlStrategy??"vitess"),"ts-cloud vitess:apply-schema")}async function Eb(e,t,r){let o=qa(e);if(o)return{...o,migrations:[],failed:[],running:[]};let m=await kn(e,t,AP(p(e),r),"ts-cloud vitess:migrations"),h=m.ok&&m.stdout?CP(m.stdout):[];return{...m,migrations:h,failed:PP(h),running:_P(h)}}async function Rb(e,t,r,o){let m=qa(e);if(m)return{...m,uuid:r};if(!wP(r))return{ok:!1,error:"Migration UUID is not in the expected format.",uuid:r};return{...await kn(e,t,TP(p(e),r,o),`ts-cloud vitess:migration ${o} ${r}`),uuid:r}}function xP(e){let t=e.infrastructure?.appDatabase;if(t?.engine&&t.engine!=="vitess")return`This project already declares ${t.engine} as its application database.`;let r=e.infrastructure?.compute?.managedServices,o=r?["mysql","mariadb","postgres"].find((m)=>Boolean(r[m])):void 0;if(o)return`This server already declares the ${o} database service.`;return}function DP(e,t){let r=t.keyspace.trim(),o=t.username.trim(),m=t.password,h=xP(e);if(h)throw Error(h);if(!Yi(r))throw Error("Keyspace must contain letters, numbers, or underscores and cannot start with a number.");if(!Yi(o))throw Error("Username must contain letters, numbers, or underscores and cannot start with a number.");if(m.length<16)throw Error("Password must contain at least 16 characters.");if(t.confirm!==r)throw Error(`Type "${r}" to confirm Vitess provisioning.`);let I={mode:"cluster",cell:"zone1",keyspaces:[{name:r,sharded:t.sharded===!0}],vtgatePort:15306,username:o,password:m,bindAddress:"127.0.0.1"};return{service:I,appDatabase:{engine:"vitess",name:r,username:o,host:"127.0.0.1",port:15306,ssl:!1},commands:[...ir(),...os(I)]}}async function Ab(e,t,r){let o=DP(e,r);return{...await kn(e,t,o.commands,`ts-cloud vitess:provision ${o.appDatabase.name}`),database:o.appDatabase,service:Object.fromEntries(Object.entries(o.service).filter(([h])=>h!=="password"))}}var OP={capability:"runtime:terminal",scope:"organization"};var $P={"GET /api/health":{capability:"project:read",anyUser:!0},"GET /api/dashboard-data":{capability:"project:read",anyUser:!0},"GET /api/config":{capability:"config:read",anyUser:!0},"GET /api/me":{capability:"project:read",anyUser:!0},"GET /api/search":{capability:"project:read",anyUser:!0},"GET /api/search/preferences":{capability:"project:read",anyUser:!0},"POST /api/search/preferences":{capability:"project:read",anyUser:!0},"DELETE /api/search/preferences":{capability:"project:read",anyUser:!0},"GET /api/auth/security":{capability:"project:read",anyUser:!0},"GET /api/auth/sessions":{capability:"project:read",anyUser:!0},"DELETE /api/auth/sessions":{capability:"project:read",anyUser:!0},"POST /api/auth/sessions/revoke-others":{capability:"project:read",anyUser:!0},"POST /api/auth/password/change":{capability:"project:read",anyUser:!0},"POST /api/auth/mfa/enroll":{capability:"project:read",anyUser:!0},"POST /api/auth/mfa/verify":{capability:"project:read",anyUser:!0},"DELETE /api/auth/mfa":{capability:"project:read",anyUser:!0},"POST /api/auth/step-up":{capability:"project:read",anyUser:!0},"GET /api/auth/oidc/providers":{capability:"users:read",scope:"organization"},"POST /api/auth/oidc/providers":{capability:"users:manage",scope:"organization"},"PATCH /api/auth/oidc/providers":{capability:"users:manage",scope:"organization"},"GET /api/automation":{capability:"users:read",scope:"organization"},"POST /api/automation/service-accounts":{capability:"users:manage",scope:"organization"},"PATCH /api/automation/service-accounts":{capability:"users:manage",scope:"organization"},"POST /api/automation/tokens":{capability:"users:manage",scope:"organization"},"POST /api/automation/tokens/rotate":{capability:"users:manage",scope:"organization"},"DELETE /api/automation/tokens":{capability:"users:manage",scope:"organization"},"GET /api/security/posture":{capability:"security:read"},"GET /api/security/export":{capability:"security:read"},"POST /api/security/scan":{capability:"security:manage"},"POST /api/security/review":{capability:"deployments:create"},"POST /api/security/policies":{capability:"security:manage"},"PATCH /api/security/policies":{capability:"security:manage"},"POST /api/security/waivers":{capability:"security:waive"},"DELETE /api/security/waivers":{capability:"security:waive"},"PATCH /api/security/findings":{capability:"security:manage"},"POST /api/security/comments":{capability:"security:manage"},"GET /api/sources":{capability:"sources:read"},"POST /api/sources/connections":{capability:"sources:manage"},"PATCH /api/sources/connections":{capability:"sources:manage"},"DELETE /api/sources/connections":{capability:"sources:manage"},"POST /api/sources/deploy-keys":{capability:"sources:manage"},"DELETE /api/sources/deploy-keys":{capability:"sources:manage"},"POST /api/sources/repositories/sync":{capability:"sources:manage"},"GET /api/sources/references":{capability:"sources:read"},"POST /api/sources/bindings":{capability:"sources:manage"},"PATCH /api/sources/bindings":{capability:"sources:manage"},"POST /api/sources/webhooks":{capability:"sources:manage"},"PATCH /api/sources/webhooks":{capability:"sources:manage"},"DELETE /api/sources/webhooks":{capability:"sources:manage"},"GET /api/onboarding":{capability:"applications:read"},"POST /api/onboarding/detect":{capability:"applications:manage"},"POST /api/onboarding/plan":{capability:"applications:manage"},"POST /api/onboarding/drafts":{capability:"applications:manage"},"PATCH /api/onboarding/drafts":{capability:"applications:manage"},"POST /api/onboarding/artifacts":{capability:"applications:manage"},"POST /api/onboarding/registries":{capability:"applications:manage"},"PATCH /api/onboarding/registries":{capability:"applications:manage"},"DELETE /api/onboarding/registries":{capability:"applications:manage"},"POST /api/onboarding/apply":{capability:"applications:manage"},"GET /api/compose":{capability:"applications:read",anyUser:!0},"POST /api/compose/preview":{capability:"applications:manage",anyUser:!0},"POST /api/compose/import":{capability:"applications:manage",anyUser:!0},"POST /api/compose/template":{capability:"applications:manage",anyUser:!0},"POST /api/compose/action":{capability:"project:read",anyUser:!0},"POST /api/compose/logs":{capability:"runtime:logs",anyUser:!0},"POST /api/compose/shell":{capability:"runtime:terminal",scope:"organization"},"GET /api/compose/import":{capability:"applications:manage"},"GET /api/compose/template":{capability:"applications:manage"},"GET /api/compose/logs":{capability:"runtime:logs"},"GET /api/compose/shell":{capability:"runtime:terminal",scope:"organization"},"GET /api/organization":{capability:"users:read",scope:"organization"},"GET /api/organization/invitations":{capability:"users:read",scope:"organization"},"POST /api/organization/invitations":{capability:"users:manage",scope:"organization"},"DELETE /api/organization/invitations":{capability:"users:manage",scope:"organization"},"POST /api/organization/invitations/resend":{capability:"users:manage",scope:"organization"},"PATCH /api/organization/memberships":{capability:"users:manage",scope:"organization"},"DELETE /api/organization/memberships":{capability:"users:manage",scope:"organization"},"POST /api/organization/grants":{capability:"users:manage",scope:"organization"},"DELETE /api/organization/grants":{capability:"users:manage",scope:"organization"},"GET /api/control-plane/operations":{capability:"deployments:read"},"GET /api/control-plane/events":{capability:"audit:read"},"GET /api/queue":{capability:"deployments:read",anyUser:!0},"GET /api/queue/logs":{capability:"deployments:read",anyUser:!0},"GET /api/queue/stream":{capability:"deployments:read",anyUser:!0},"POST /api/queue/cancel":{capability:"deployments:cancel",anyUser:!0},"POST /api/queue/retry":{capability:"deployments:create",anyUser:!0},"GET /api/queue/settings":{capability:"deployments:read"},"PATCH /api/queue/settings":{capability:"automation:manage",scope:"organization"},"DELETE /api/queue/history":{capability:"automation:manage",scope:"organization"},"GET /api/previews":{capability:"deployments:read",anyUser:!0},"GET /api/releases":{capability:"deployments:read",anyUser:!0},"POST /api/releases/action":{capability:"deployments:create",anyUser:!0},"POST /api/previews/deploy":{capability:"deployments:create",anyUser:!0},"POST /api/previews/destroy":{capability:"deployments:cancel",anyUser:!0},"POST /api/previews/extend":{capability:"deployments:create",anyUser:!0},"POST /api/previews/rebuild":{capability:"deployments:create",anyUser:!0},"POST /api/previews/definitions":{capability:"config:write",anyUser:!0},"POST /api/previews/cleanup":{capability:"automation:manage",scope:"organization"},"GET /api/tags":{capability:"project:read"},"POST /api/tags":{capability:"tags:manage"},"DELETE /api/tags":{capability:"tags:manage"},"POST /api/sites/deploy":{capability:"deployments:create",scope:"site",siteFrom:"body"},"PATCH /api/sites":{capability:"config:write",scope:"site",siteFrom:"body"},"POST /api/sites":{capability:"config:write"},"DELETE /api/sites":{capability:"config:write"},"POST /api/env":{capability:"config:write"},"GET /api/ssh-keys":{capability:"fleet:read"},"POST /api/ssh-keys":{capability:"fleet:manage"},"DELETE /api/ssh-keys":{capability:"fleet:manage"},"GET /api/firewall":{capability:"fleet:read"},"POST /api/firewall":{capability:"fleet:manage"},"DELETE /api/firewall":{capability:"fleet:manage"},"GET /api/databases":{capability:"data:read"},"POST /api/databases":{capability:"data:admin"},"GET /api/databases/vitess":{capability:"data:read"},"GET /api/databases/vitess/migrations":{capability:"data:read"},"POST /api/databases/vitess/migrations/action":{capability:"data:admin"},"POST /api/databases/vitess/provision":{capability:"runtime:terminal",scope:"organization"},"POST /api/databases/vitess/keyspaces":{capability:"data:admin"},"POST /api/databases/vitess/vschema":{capability:"data:admin"},"POST /api/databases/vitess/schema":{capability:"data:admin"},"POST /api/databases/vitess/install-client":{capability:"data:admin"},"GET /api/databases/backups":{capability:"backups:read"},"POST /api/databases/backup":{capability:"backups:create"},"POST /api/databases/users":{capability:"data:admin"},"GET /api/data-services":{capability:"data:read"},"GET /api/data-services/capabilities":{capability:"data:read"},"GET /api/data-services/connections":{capability:"data:read"},"POST /api/data-services/preview":{capability:"data:read"},"POST /api/data-services":{capability:"data:admin"},"POST /api/data-services/action":{capability:"data:admin"},"POST /api/data-services/dependencies":{capability:"data:admin"},"POST /api/data-services/management":{capability:"data:admin"},"POST /api/data-services/reveal":{capability:"data:admin"},"GET /api/backups":{capability:"backups:read"},"POST /api/backups/destinations":{capability:"backups:create"},"POST /api/backups/destinations/test":{capability:"backups:create"},"POST /api/backups/policies":{capability:"backups:create"},"POST /api/backups/run":{capability:"backups:create"},"POST /api/backups/verify":{capability:"backups:create"},"POST /api/backups/restore":{capability:"backups:restore"},"POST /api/backups/recovery-points":{capability:"backups:create"},"POST /api/backups/retention":{capability:"backups:create"},"GET /api/configuration":{capability:"config:read"},"GET /api/configuration/export":{capability:"config:read"},"POST /api/configuration/plan":{capability:"config:read"},"POST /api/configuration":{capability:"config:write"},"POST /api/configuration/import":{capability:"config:write"},"POST /api/configuration/dependencies":{capability:"config:write"},"POST /api/configuration/transfer":{capability:"config:write"},"POST /api/configuration/bulk":{capability:"config:write"},"POST /api/configuration/reveal":{capability:"secrets:read"},"DELETE /api/configuration":{capability:"config:write"},"GET /api/volumes":{capability:"data:read"},"POST /api/volumes":{capability:"data:admin"},"POST /api/volumes/attach":{capability:"data:admin"},"POST /api/volumes/detach":{capability:"data:admin"},"POST /api/volumes/resize":{capability:"data:admin"},"POST /api/volumes/snapshot":{capability:"backups:create"},"POST /api/volumes/restore":{capability:"backups:restore"},"POST /api/volumes/adopt":{capability:"data:admin"},"POST /api/volumes/discover":{capability:"data:read"},"DELETE /api/volumes":{capability:"data:admin"},"GET /api/fleet":{capability:"fleet:read"},"POST /api/fleet":{capability:"fleet:manage"},"POST /api/fleet/action":{capability:"fleet:manage"},"GET /api/capacity":{capability:"fleet:read"},"POST /api/capacity":{capability:"fleet:manage"},"POST /api/capacity/member":{capability:"fleet:manage"},"POST /api/capacity/explain":{capability:"fleet:read"},"POST /api/capacity/action":{capability:"fleet:manage"},"GET /api/regions":{capability:"config:read"},"POST /api/regions":{capability:"config:write"},"POST /api/regions/action":{capability:"config:write"},"GET /api/maintenance":{capability:"automation:read"},"POST /api/maintenance/cleanup-preview":{capability:"automation:manage"},"POST /api/maintenance/cleanup-apply":{capability:"automation:manage"},"POST /api/maintenance/drill":{capability:"automation:manage"},"POST /api/maintenance/drill-run":{capability:"automation:manage"},"GET /api/actions":{capability:"runtime:read"},"GET /api/runtime/workloads":{capability:"runtime:read"},"GET /api/runtime/logs":{capability:"runtime:logs"},"POST /api/runtime/log-sessions":{capability:"runtime:logs"},"GET /api/runtime/log-sessions":{capability:"runtime:logs"},"GET /api/runtime/log-stream":{capability:"runtime:logs"},"DELETE /api/runtime/log-sessions":{capability:"runtime:logs"},"POST /api/runtime/exec-sessions":{capability:"runtime:terminal",scope:"organization"},"GET /api/runtime/exec-sessions":{capability:"runtime:terminal",scope:"organization"},"GET /api/runtime/exec-stream":{capability:"runtime:terminal",scope:"organization"},"DELETE /api/runtime/exec-sessions":{capability:"runtime:terminal",scope:"organization"},"POST /api/runtime/files/read":{capability:"runtime:terminal",scope:"organization"},"POST /api/runtime/files/write":{capability:"runtime:terminal",scope:"organization"},"POST /api/runtime/operations":{capability:"runtime:restart"},"GET /api/telemetry/status":{capability:"runtime:read"},"GET /api/telemetry/settings":{capability:"runtime:read"},"PATCH /api/telemetry/settings":{capability:"config:write",scope:"organization"},"GET /api/telemetry/query":{capability:"runtime:logs"},"GET /api/telemetry/tail":{capability:"runtime:logs"},"GET /api/telemetry/series":{capability:"runtime:read"},"POST /api/telemetry/refresh":{capability:"runtime:logs"},"POST /api/telemetry/export":{capability:"runtime:logs"},"GET /api/telemetry/saved-queries":{capability:"runtime:logs"},"POST /api/telemetry/saved-queries":{capability:"runtime:logs"},"DELETE /api/telemetry/saved-queries":{capability:"runtime:logs"},"GET /api/health/checks":{capability:"runtime:read"},"POST /api/health/checks":{capability:"config:write"},"PATCH /api/health/checks":{capability:"config:write"},"POST /api/health/checks/test":{capability:"runtime:read"},"POST /api/health/checks/run":{capability:"runtime:restart"},"GET /api/protection":{capability:"security:read"},"POST /api/protection/attack-mode":{capability:"security:manage"},"POST /api/protection/pause":{capability:"security:manage"},"GET /api/protection/ip-rules":{capability:"security:read"},"POST /api/protection/ip-rules":{capability:"security:manage"},"DELETE /api/protection/ip-rules":{capability:"security:manage"},"GET /api/usage":{capability:"billing:read"},"GET /api/spend/budgets":{capability:"billing:read"},"POST /api/spend/budgets":{capability:"billing:manage"},"PATCH /api/spend/budgets":{capability:"billing:manage"},"DELETE /api/spend/budgets":{capability:"billing:manage"},"POST /api/spend/check":{capability:"billing:manage"},"GET /api/spend/enforcement":{capability:"billing:read"},"POST /api/spend/release":{capability:"billing:manage"},"GET /api/spend/anomalies":{capability:"billing:read"},"POST /api/spend/anomalies/acknowledge":{capability:"billing:manage"},"GET /api/alerts":{capability:"runtime:read"},"GET /api/alerts/rules":{capability:"runtime:read"},"POST /api/alerts/rules":{capability:"config:write"},"PATCH /api/alerts/rules":{capability:"config:write"},"POST /api/alerts/evaluate":{capability:"runtime:restart"},"POST /api/alerts/action":{capability:"runtime:restart"},"POST /api/alerts/silences":{capability:"config:write"},"GET /api/notifications/channels":{capability:"automation:read",scope:"organization"},"POST /api/notifications/channels":{capability:"automation:manage",scope:"organization"},"PATCH /api/notifications/channels":{capability:"automation:manage",scope:"organization"},"POST /api/notifications/channels/test":{capability:"automation:manage",scope:"organization"},"GET /api/notifications/routes":{capability:"automation:read",scope:"organization"},"POST /api/notifications/routes":{capability:"automation:manage",scope:"organization"},"PATCH /api/notifications/routes":{capability:"automation:manage",scope:"organization"},"POST /api/notifications/routes/preview":{capability:"automation:read",scope:"organization"},"GET /api/notifications/deliveries":{capability:"automation:read",scope:"organization"},"POST /api/notifications/deliveries/retry":{capability:"automation:manage",scope:"organization"},"GET /api/jobs":{capability:"automation:read"},"POST /api/jobs":{capability:"automation:manage"},"PATCH /api/jobs":{capability:"automation:manage"},"POST /api/jobs/preview":{capability:"automation:read"},"POST /api/jobs/action":{capability:"automation:manage"},"POST /api/jobs/reconcile":{capability:"automation:manage"},"GET /api/jobs/history":{capability:"automation:read"},"GET /api/workers":{capability:"automation:read"},"POST /api/workers/action":{capability:"automation:manage"},"POST /api/actions/run":{capability:"fleet:manage"},"GET /api/server/operations":{capability:"fleet:read"},"POST /api/server/operations/run":{capability:"fleet:manage"},"POST /api/server/command":{capability:"runtime:terminal",scope:"organization"},"GET /api/terminal":{capability:"runtime:terminal",scope:"organization"},"GET /api/users":{capability:"users:read",scope:"organization"},"POST /api/users":{capability:"users:manage",scope:"organization"},"DELETE /api/users":{capability:"users:manage",scope:"organization"},"GET /api/serverless/operations":{capability:"deployments:read"},"POST /api/serverless/operations/run":{capability:"deployments:create"},"POST /api/serverless/command":{capability:"runtime:terminal",scope:"organization"},"GET /api/serverless/dlq":{capability:"data:read"},"POST /api/serverless/dlq/redrive":{capability:"data:write"},"POST /api/serverless/dlq/purge":{capability:"data:admin"},"GET /api/serverless/secrets":{capability:"secrets:read"},"POST /api/serverless/secrets":{capability:"secrets:write"},"DELETE /api/serverless/secrets":{capability:"secrets:write"},"POST /api/serverless/functions/config":{capability:"config:write"},"GET /api/serverless/alarms":{capability:"runtime:read"},"POST /api/serverless/alarms":{capability:"config:write"},"DELETE /api/serverless/alarms":{capability:"config:write"},"GET /api/serverless/traces":{capability:"runtime:read"},"POST /api/serverless/scheduler":{capability:"automation:manage"}};function Rg(e,t){return $P[`${e.toUpperCase()} ${t}`]??OP}import{createHmac as NP,randomBytes as jP,timingSafeEqual as MP}from"node:crypto";import{chmodSync as BP,existsSync as LP,mkdirSync as FP,readFileSync as zP,writeFileSync as UP}from"node:fs";import{dirname as qP}from"node:path";var bc="ts_cloud_session";var VP=28800000;function Cb(e){let t=process.env.TS_CLOUD_DASHBOARD_SECRET?.trim();if(t)return t;let r=d(e,"dashboard-secret");try{if(LP(r)){let m=zP(r,"utf8").trim();if(m)return m}}catch{}let o=jP(32).toString("base64url");try{FP(qP(r),{recursive:!0}),UP(r,`${o}
|
|
368
|
+
`),BP(r,384)}catch{}return o}function WP(e,t){return NP("sha256",t).update(e).digest("base64url")}function Pb(e,t){if(!e)return null;let r=e.lastIndexOf(".");if(r<=0)return null;let o=e.slice(0,r),m=e.slice(r+1),h=WP(o,t),I=Buffer.from(m),R=Buffer.from(h);if(I.length!==R.length||!MP(I,R))return null;try{let A=JSON.parse(Buffer.from(o,"base64url").toString("utf8"));if(typeof A?.u!=="string"||typeof A?.exp!=="number")return null;if(A.mv!==void 0&&(!A.mv||typeof A.mv!=="object"||Array.isArray(A.mv)||Object.values(A.mv).some((E)=>!Number.isInteger(E)||E<1)))return null;if(Date.now()>=A.exp)return null;return A}catch{return null}}function Ag(e,t){if(!e)return;for(let r of e.split(";")){let o=r.indexOf("=");if(o<0)continue;if(r.slice(0,o).trim()===t)return decodeURIComponent(r.slice(o+1).trim())}return}function Va(e,t={secure:!0}){let r=Math.floor((t.maxAgeMs??VP)/1000),o=[`${bc}=${encodeURIComponent(e)}`,"Path=/","HttpOnly","SameSite=Lax",`Max-Age=${r}`];if(t.secure)o.push("Secure");return o.join("; ")}function _u(e={secure:!0}){let t=[`${bc}=`,"Path=/","HttpOnly","SameSite=Lax","Max-Age=0"];if(e.secure)t.push("Secure");return t.join("; ")}import{chmodSync as HP,existsSync as KP,mkdirSync as JP,readFileSync as GP,writeFileSync as QP}from"node:fs";import{dirname as YP}from"node:path";function _b(){return _("dashboard-users.json")}function Tu(e){return/^[a-z0-9][a-z0-9._-]{1,31}$/i.test(e)}function ZP(e){if(!e||typeof e.username!=="string"||!Tu(e.username)||typeof e.passwordHash!=="string")return null;let t=e.role==="admin"?"admin":"member",r={};if(e.sites&&typeof e.sites==="object"){for(let[o,m]of Object.entries(e.sites))if(m==="owner"||m==="collaborator")r[o]=m}return{username:e.username,passwordHash:e.passwordHash,role:t,sites:r,name:typeof e.name==="string"?e.name:void 0,email:typeof e.email==="string"?e.email.trim().toLowerCase():void 0,createdAt:typeof e.createdAt==="string"?e.createdAt:void 0}}function XP(e){try{let t=JSON.parse(e);if(!Array.isArray(t?.users))return[];return t.users.map(ZP).filter((r)=>r!==null)}catch{return[]}}function Tb(e){return d(e,"dashboard-users.json")}function In(e){let t=Tb(e);if(!KP(t))return[];try{return XP(GP(t,"utf8"))}catch{return[]}}function xu(e,t){let r=Tb(e);JP(YP(r),{recursive:!0}),QP(r,`${JSON.stringify({users:t},null,2)}
|
|
369
|
+
`),HP(r,384)}function Un(e,t){let r=t.trim().toLowerCase();return e.find((o)=>o.username.toLowerCase()===r)}function xb(e,t="admin"){let r=In(e);if(r.some((I)=>I.role==="admin"))return{users:r};let o=process.env.TS_CLOUD_UI_PASSWORD?.trim()||gg(),m={username:t,passwordHash:gs(o),role:"admin",sites:{},name:"Administrator",createdAt:new Date().toISOString()},h=[...r,m];return xu(e,h),{users:h,generated:{username:t,password:o}}}function wc(e,t){let r=In(e),o=Un(r,t.username),m=t.password?.trim()||(o?void 0:gg()),h={username:o?.username??t.username,passwordHash:m?gs(m):o.passwordHash,role:o?.role??"member",sites:t.sites,name:t.name??o?.name,email:t.email?.trim().toLowerCase()??o?.email,createdAt:o?.createdAt??new Date().toISOString()},I=o?r.map((R)=>R.username.toLowerCase()===h.username.toLowerCase()?h:R):[...r,h];return xu(e,I),{user:h,password:m}}function Du(e,t,r){let o=In(e),m=Un(o,t);if(!m)throw Error("Authentication user was not found");let h={...m,passwordHash:r};return xu(e,o.map((I)=>I.username.toLowerCase()===m.username.toLowerCase()?h:I)),h}function Cg(e,t){let r=In(e),o=Un(r,t);if(!o)return{ok:!1,error:`No such user: ${t}`};if(o.role==="admin"&&r.filter((m)=>m.role==="admin").length===1)return{ok:!1,error:"Cannot remove the last admin."};return xu(e,r.filter((m)=>m.username.toLowerCase()!==o.username.toLowerCase())),{ok:!0}}function Wa(e){return{username:e.username,name:e.name??e.username,role:e.role,email:e.email,sites:e.sites,siteCount:Object.keys(e.sites).length,createdAt:e.createdAt}}var e_={username:"local",passwordHash:"",role:"admin",sites:{},name:"Local (auth disabled)"};function Db(e){let{cwd:t,enabled:r,secret:o,authorization:m,authentication:h}=e,I=(A)=>{if(!h)return;let E=Ag(A.headers.get("cookie"),bc);return E?.startsWith("v2.")?h.verifySessionToken(E):void 0},R=(A)=>{let E=m.store.getActorByExternalId("user",`dashboard:${A.username.toLowerCase()}`),x=E?m.store.getMembershipForActor(m.organizationId,E.id):void 0;return{actor:E,membership:x}};return{enabled:r,resolveUser(A){if(!r)return e_;let E=Ag(A.headers.get("cookie"),bc),x=I(A),N=x?void 0:Pb(E,o),F=x?.identity.username??N?.u;if(!F)return null;let z=Un(In(t),F);if(!z)return null;let{membership:K}=R(z);if(!K||K.status!=="active"||!x&&N?.mv?.[m.organizationId]!==K.sessionVersion)return null;if(!K.lastActiveAt||Date.now()-new Date(K.lastActiveAt).getTime()>300000)m.store.touchMembership(K.id);return z},resolveSession(A){return I(A)?.session??null},check(A,E,x,N){if(!r)return{ok:!0};if(!x)return{ok:!1,status:401,error:"Sign in to continue.",unauthenticated:!0};let F=Rg(A.method,E);if(F.anyUser)return{ok:!0};if(F.siteFrom==="body"&&!N)return{ok:!1,status:400,error:"This request must name a site."};let{membership:z}=R(x);if(!z)return{ok:!1,status:401,error:"Sign in to continue.",unauthenticated:!0};let K=F.scope==="organization"?{organizationId:m.organizationId}:m.store.resolveAuthorizationTarget(m.organizationId,{type:"project",id:m.projectId});if(F.scope==="site"){let re=new URL(A.url).searchParams.get("env")??m.defaultEnvironment,se=m.store.getEnvironmentBySlug(m.projectId,re),ye=m.store.listResources(m.projectId,se?.id).find((ne)=>ne.kind==="application"&&ne.slug===N);K=ye?m.store.resolveAuthorizationTarget(m.organizationId,{type:"resource",id:ye.id}):void 0}if(!(K?w({membership:z,grants:m.store.listGrants(z.id),capability:F.capability,target:K}):{allowed:!1}).allowed)return{ok:!1,status:403,error:"You do not have access to this."};return{ok:!0}}}}async function Ob(e,t){if(Rg(e.method,t).siteFrom!=="body")return;try{let o=await e.clone().json();return String(o?.name??"").trim()||void 0}catch{return}}import{createHash as t_}from"node:crypto";function $b(e,t,r){for(let o of r){let m=S(t.store,o);if(e.getIdentityByActor(m.id))continue;e.createIdentity({actorId:m.id,username:o.username,email:o.email,emailVerified:!!o.email,passwordHash:o.passwordHash,requiresPasswordUpgrade:!0})}}function r_(e,t,r){let o=r.split("@")[0].toLowerCase().replace(/[^a-z0-9._-]/g,"-").replace(/^[^a-z0-9]+/,"").slice(0,24),m=/^[a-z0-9][a-z0-9._-]{1,31}$/.test(o)?o:"sso-user";if(!e.getIdentityByUsername(m)&&!Un(t,m))return m;let h=t_("sha256").update(r).digest("hex").slice(0,7);return`${m.slice(0,24)}-${h}`}function Nb(e,t,r,o){if(o.provider.organizationId!==t.organization.id)throw Error("OIDC provider is not configured for this organization");let m=e.getOidcSubject(o.provider.id,o.subject),h=m?e.getIdentity(m.identityId):e.getIdentityByEmail(o.email);if(h?.disabledAt)throw Error("OIDC identity is disabled");if(!m&&h&&!h.emailVerifiedAt)throw Error("OIDC cannot automatically link an unverified local email address");let I=In(r),R=!1,A=h?Un(I,h.username):void 0;if(h&&!A)throw Error("OIDC identity has no dashboard user");if(!h){let N=r_(e,I,o.email);A=wc(r,{username:N,name:o.name,email:o.email,sites:{}}).user;let z=S(t.store,A);h=e.createIdentity({actorId:z.id,username:A.username,email:o.email,emailVerified:!0,passwordHash:A.passwordHash}),R=!0}else if(h.email!==o.email)h=e.setVerifiedEmail(h.id,o.email);let E=t.store.getActor(h.actorId);if(!E)throw Error("OIDC identity actor was not found");let x=t.store.getMembershipForActor(t.organization.id,E.id);if(x?.status==="revoked")throw Error("OIDC organization membership is revoked");if(!x)t.store.createMembership({organizationId:t.organization.id,actorId:E.id,roleTemplate:o.provider.defaultRole,scope:{type:"organization"},source:"manual"});return e.linkOidcSubject(o.provider.id,h.id,o.subject,o.email),{identity:h,user:A,provisioned:R}}function jb(e,t,r){let o=t.store.getMembershipForActor(t.organization.id,r.actorId);if(!o||o.status!=="active"||o.roleTemplate==="owner"||!r.emailVerifiedAt||!r.email)return!1;let m=r.email.split("@")[1]?.toLowerCase();return e.listOidcProviders(t.organization.id).some((h)=>h.enforceSso&&!!m&&h.allowedDomains.includes(m))}function n_(e){return e.replace(/[&<>"']/g,(t)=>({"&":"&","<":"<",">":">",'"':""","'":"'"})[t])}function Mb(e=!1,t=[]){let r=e?"/serverless":"/",o=t.map((h)=>`<a class="sso-button" href="/auth/oidc/${encodeURIComponent(h.slug)}/start?return=${encodeURIComponent(r)}">Continue with ${n_(h.name)}</a>`).join("");return`<!doctype html>
|
|
370
|
+
<html lang="en">
|
|
371
|
+
<head>
|
|
372
|
+
<meta charset="UTF-8">
|
|
373
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
374
|
+
<title>Sign in · ts-cloud</title>
|
|
375
|
+
<link rel="icon" type="image/svg+xml" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Crect x='3' y='3' width='26' height='26' rx='8' fill='%235a8be0'/%3E%3C/svg%3E">
|
|
376
|
+
<link rel="preconnect" href="https://fonts.googleapis.com">
|
|
377
|
+
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
|
|
378
|
+
<style>${`
|
|
379
|
+
:root {
|
|
380
|
+
--bg: #0a0e15; --bg2: #0b0f18; --panel: rgba(255,255,255,0.03); --panel-br: rgba(255,255,255,0.072);
|
|
381
|
+
--txt: #e9edf5; --txt2: #94a1b8; --txt3: #5a6577;
|
|
382
|
+
--accent: #5a8be0; --accent-ink: #061019;
|
|
383
|
+
--bad: #f0736e; --badbg: rgba(240,115,110,0.11);
|
|
384
|
+
}
|
|
385
|
+
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
386
|
+
html { background: var(--bg); min-height: 100%; }
|
|
387
|
+
body {
|
|
388
|
+
font-family: 'Inter', system-ui, sans-serif; color: var(--txt); min-height: 100vh;
|
|
389
|
+
background:
|
|
390
|
+
radial-gradient(900px 460px at 88% -8%, rgba(90,139,224,0.06), transparent 62%),
|
|
391
|
+
linear-gradient(175deg, var(--bg), var(--bg2));
|
|
392
|
+
-webkit-font-smoothing: antialiased;
|
|
393
|
+
display: grid; place-items: center; padding: 24px;
|
|
394
|
+
}
|
|
395
|
+
.card {
|
|
396
|
+
width: 100%; max-width: 380px;
|
|
397
|
+
background: var(--panel); border: 1px solid var(--panel-br); border-radius: 16px;
|
|
398
|
+
padding: 32px; backdrop-filter: blur(6px);
|
|
399
|
+
animation: rise .5s cubic-bezier(0.16, 1, 0.3, 1) both;
|
|
400
|
+
}
|
|
401
|
+
@keyframes rise { from { opacity: 0; transform: translateY(8px) } to { opacity: 1; transform: none } }
|
|
402
|
+
@media (prefers-reduced-motion: reduce) { .card { animation: none } }
|
|
403
|
+
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; letter-spacing: -0.02em; }
|
|
404
|
+
.brand .dot { width: 20px; height: 20px; border-radius: 6px; background: var(--accent); box-shadow: inset 0 1px 0 rgba(255,255,255,0.35); }
|
|
405
|
+
h1 { font-size: 21px; font-weight: 800; letter-spacing: -0.02em; margin-top: 22px; }
|
|
406
|
+
.sub { color: var(--txt2); font-size: 13.5px; margin-top: 5px; line-height: 1.5; }
|
|
407
|
+
form { margin-top: 24px; display: grid; gap: 14px; }
|
|
408
|
+
.field { display: flex; flex-direction: column; gap: 7px; }
|
|
409
|
+
label { color: var(--txt3); font-size: 11px; font-weight: 800; text-transform: uppercase; letter-spacing: 0.07em; }
|
|
410
|
+
input {
|
|
411
|
+
width: 100%; background: rgba(255,255,255,0.05); border: 1px solid var(--panel-br); border-radius: 10px;
|
|
412
|
+
padding: 10px 12px; color: var(--txt); font-family: inherit; font-size: 14px;
|
|
413
|
+
}
|
|
414
|
+
input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(90,139,224,0.12); }
|
|
415
|
+
button {
|
|
416
|
+
margin-top: 4px; background: var(--accent); color: var(--accent-ink); border: 0; border-radius: 10px;
|
|
417
|
+
padding: 11px 16px; font-weight: 700; font-size: 13.5px; cursor: pointer; font-family: inherit;
|
|
418
|
+
transition: filter .12s, transform .12s;
|
|
419
|
+
}
|
|
420
|
+
button:hover { filter: brightness(1.06); }
|
|
421
|
+
button:active { transform: translateY(1px); }
|
|
422
|
+
button[disabled] { opacity: 0.6; cursor: default; }
|
|
423
|
+
.msg {
|
|
424
|
+
display: none; margin-top: 14px; padding: 10px 12px; border-radius: 10px;
|
|
425
|
+
background: var(--badbg); border: 1px solid rgba(240,115,110,0.22); color: var(--bad);
|
|
426
|
+
font-size: 13px; line-height: 1.45;
|
|
427
|
+
}
|
|
428
|
+
.msg.shown { display: block; }
|
|
429
|
+
.hidden { display: none !important; }
|
|
430
|
+
.note { color: var(--txt3); font-size: 12px; margin-top: 20px; line-height: 1.5; }
|
|
431
|
+
.note code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; color: var(--txt2); }
|
|
432
|
+
a { color: var(--accent); text-decoration: none; }
|
|
433
|
+
a:hover { text-decoration: underline; }
|
|
434
|
+
.sso { display: grid; gap: 10px; margin-top: 22px; }
|
|
435
|
+
.sso-button { display: block; border: 1px solid var(--panel-br); border-radius: 10px; padding: 11px 14px; color: var(--txt); text-align: center; font-size: 13.5px; font-weight: 700; background: rgba(255,255,255,0.04); }
|
|
436
|
+
.sso-button:hover { border-color: var(--accent); text-decoration: none; }
|
|
437
|
+
.separator { display: flex; align-items: center; gap: 10px; margin: 18px 0 -6px; color: var(--txt3); font-size: 10px; font-weight: 800; text-transform: uppercase; letter-spacing: .06em; }
|
|
438
|
+
.separator::before, .separator::after { content: ''; height: 1px; flex: 1; background: var(--panel-br); }
|
|
439
|
+
`}</style>
|
|
440
|
+
</head>
|
|
441
|
+
<body>
|
|
442
|
+
<main class="card">
|
|
443
|
+
<div class="brand"><span class="dot"></span> ts-cloud</div>
|
|
444
|
+
<h1>Sign in</h1>
|
|
445
|
+
<p class="sub">Manage the sites you have been given access to.</p>
|
|
446
|
+
|
|
447
|
+
${t.length>0?`<div class="sso" aria-label="Single sign-on">${o}</div><div class="separator"><span>or use local recovery</span></div>`:""}
|
|
448
|
+
|
|
449
|
+
<form id="login" autocomplete="on">
|
|
450
|
+
<div class="field">
|
|
451
|
+
<label for="username">Username</label>
|
|
452
|
+
<input id="username" name="username" autocomplete="username" required autofocus>
|
|
453
|
+
</div>
|
|
454
|
+
<div class="hidden field" id="mfa-field">
|
|
455
|
+
<label for="mfa-code">Authenticator or recovery code</label>
|
|
456
|
+
<input id="mfa-code" name="mfa-code" autocomplete="one-time-code">
|
|
457
|
+
</div>
|
|
458
|
+
<div class="field">
|
|
459
|
+
<label for="password">Password</label>
|
|
460
|
+
<input id="password" name="password" type="password" autocomplete="current-password" required>
|
|
461
|
+
</div>
|
|
462
|
+
<button type="submit" id="submit">Sign in</button>
|
|
463
|
+
</form>
|
|
464
|
+
|
|
465
|
+
<p class="msg" id="msg" role="alert" aria-live="polite"></p>
|
|
466
|
+
<p class="note"><a href="/forgot-password">Forgot your password?</a></p>
|
|
467
|
+
</main>
|
|
468
|
+
|
|
469
|
+
<script>
|
|
470
|
+
const form = document.getElementById('login')
|
|
471
|
+
const msg = document.getElementById('msg')
|
|
472
|
+
const submit = document.getElementById('submit')
|
|
473
|
+
const mfaField = document.getElementById('mfa-field')
|
|
474
|
+
const mfaCode = document.getElementById('mfa-code')
|
|
475
|
+
let challengeToken = ''
|
|
476
|
+
|
|
477
|
+
if (new URLSearchParams(location.search).has('sso_error')) {
|
|
478
|
+
msg.textContent = 'Single sign-on could not be completed. Try again or use the local recovery path.'
|
|
479
|
+
msg.classList.add('shown')
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
form.addEventListener('submit', async (event) => {
|
|
483
|
+
event.preventDefault()
|
|
484
|
+
msg.classList.remove('shown')
|
|
485
|
+
submit.disabled = true
|
|
486
|
+
submit.textContent = 'Signing in...'
|
|
487
|
+
try {
|
|
488
|
+
const res = await fetch(challengeToken ? '/api/auth/mfa/complete' : '/api/login', {
|
|
489
|
+
method: 'POST',
|
|
490
|
+
headers: { 'content-type': 'application/json' },
|
|
491
|
+
body: JSON.stringify(challengeToken
|
|
492
|
+
? { challengeToken, code: mfaCode.value }
|
|
493
|
+
: { username: document.getElementById('username').value, password: document.getElementById('password').value }),
|
|
494
|
+
})
|
|
495
|
+
const body = await res.json().catch(() => ({}))
|
|
496
|
+
if (!res.ok || !body.ok) throw new Error(body.error || 'Could not sign in.')
|
|
497
|
+
if (body.mfaRequired) {
|
|
498
|
+
challengeToken = body.challengeToken
|
|
499
|
+
mfaField.classList.remove('hidden')
|
|
500
|
+
mfaCode.required = true
|
|
501
|
+
document.getElementById('username').readOnly = true
|
|
502
|
+
document.getElementById('password').readOnly = true
|
|
503
|
+
submit.disabled = false
|
|
504
|
+
submit.textContent = 'Verify and sign in'
|
|
505
|
+
mfaCode.focus()
|
|
506
|
+
return
|
|
507
|
+
}
|
|
508
|
+
location.href = ${JSON.stringify(r)}
|
|
509
|
+
} catch (error) {
|
|
510
|
+
msg.textContent = (error && error.message) || String(error)
|
|
511
|
+
msg.classList.add('shown')
|
|
512
|
+
submit.disabled = false
|
|
513
|
+
submit.textContent = 'Sign in'
|
|
514
|
+
if (challengeToken) {
|
|
515
|
+
mfaCode.value = ''
|
|
516
|
+
mfaCode.focus()
|
|
517
|
+
}
|
|
518
|
+
else {
|
|
519
|
+
document.getElementById('password').value = ''
|
|
520
|
+
document.getElementById('password').focus()
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
})
|
|
524
|
+
</script>
|
|
525
|
+
</body>
|
|
526
|
+
</html>`}function Bb(e){let t=e==="reset";return`<!doctype html>
|
|
527
|
+
<html lang="en">
|
|
528
|
+
<head>
|
|
529
|
+
<meta charset="UTF-8">
|
|
530
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
531
|
+
<title>${t?"Choose a new password":"Recover your account"} · ts-cloud</title>
|
|
532
|
+
<style>${`
|
|
533
|
+
:root {
|
|
534
|
+
--bg: #0a0e15; --bg2: #0b0f18; --panel: rgba(255,255,255,0.03); --panel-br: rgba(255,255,255,0.072);
|
|
535
|
+
--txt: #e9edf5; --txt2: #94a1b8; --txt3: #5a6577;
|
|
536
|
+
--accent: #5a8be0; --accent-ink: #061019;
|
|
537
|
+
--bad: #f0736e; --badbg: rgba(240,115,110,0.11);
|
|
538
|
+
}
|
|
539
|
+
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
540
|
+
html { background: var(--bg); min-height: 100%; }
|
|
541
|
+
body {
|
|
542
|
+
font-family: 'Inter', system-ui, sans-serif; color: var(--txt); min-height: 100vh;
|
|
543
|
+
background:
|
|
544
|
+
radial-gradient(900px 460px at 88% -8%, rgba(90,139,224,0.06), transparent 62%),
|
|
545
|
+
linear-gradient(175deg, var(--bg), var(--bg2));
|
|
546
|
+
-webkit-font-smoothing: antialiased;
|
|
547
|
+
display: grid; place-items: center; padding: 24px;
|
|
548
|
+
}
|
|
549
|
+
.card {
|
|
550
|
+
width: 100%; max-width: 380px;
|
|
551
|
+
background: var(--panel); border: 1px solid var(--panel-br); border-radius: 16px;
|
|
552
|
+
padding: 32px; backdrop-filter: blur(6px);
|
|
553
|
+
animation: rise .5s cubic-bezier(0.16, 1, 0.3, 1) both;
|
|
554
|
+
}
|
|
555
|
+
@keyframes rise { from { opacity: 0; transform: translateY(8px) } to { opacity: 1; transform: none } }
|
|
556
|
+
@media (prefers-reduced-motion: reduce) { .card { animation: none } }
|
|
557
|
+
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; letter-spacing: -0.02em; }
|
|
558
|
+
.brand .dot { width: 20px; height: 20px; border-radius: 6px; background: var(--accent); box-shadow: inset 0 1px 0 rgba(255,255,255,0.35); }
|
|
559
|
+
h1 { font-size: 21px; font-weight: 800; letter-spacing: -0.02em; margin-top: 22px; }
|
|
560
|
+
.sub { color: var(--txt2); font-size: 13.5px; margin-top: 5px; line-height: 1.5; }
|
|
561
|
+
form { margin-top: 24px; display: grid; gap: 14px; }
|
|
562
|
+
.field { display: flex; flex-direction: column; gap: 7px; }
|
|
563
|
+
label { color: var(--txt3); font-size: 11px; font-weight: 800; text-transform: uppercase; letter-spacing: 0.07em; }
|
|
564
|
+
input {
|
|
565
|
+
width: 100%; background: rgba(255,255,255,0.05); border: 1px solid var(--panel-br); border-radius: 10px;
|
|
566
|
+
padding: 10px 12px; color: var(--txt); font-family: inherit; font-size: 14px;
|
|
567
|
+
}
|
|
568
|
+
input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(90,139,224,0.12); }
|
|
569
|
+
button {
|
|
570
|
+
margin-top: 4px; background: var(--accent); color: var(--accent-ink); border: 0; border-radius: 10px;
|
|
571
|
+
padding: 11px 16px; font-weight: 700; font-size: 13.5px; cursor: pointer; font-family: inherit;
|
|
572
|
+
transition: filter .12s, transform .12s;
|
|
573
|
+
}
|
|
574
|
+
button:hover { filter: brightness(1.06); }
|
|
575
|
+
button:active { transform: translateY(1px); }
|
|
576
|
+
button[disabled] { opacity: 0.6; cursor: default; }
|
|
577
|
+
.msg {
|
|
578
|
+
display: none; margin-top: 14px; padding: 10px 12px; border-radius: 10px;
|
|
579
|
+
background: var(--badbg); border: 1px solid rgba(240,115,110,0.22); color: var(--bad);
|
|
580
|
+
font-size: 13px; line-height: 1.45;
|
|
581
|
+
}
|
|
582
|
+
.msg.shown { display: block; }
|
|
583
|
+
.hidden { display: none !important; }
|
|
584
|
+
.note { color: var(--txt3); font-size: 12px; margin-top: 20px; line-height: 1.5; }
|
|
585
|
+
.note code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; color: var(--txt2); }
|
|
586
|
+
a { color: var(--accent); text-decoration: none; }
|
|
587
|
+
a:hover { text-decoration: underline; }
|
|
588
|
+
.sso { display: grid; gap: 10px; margin-top: 22px; }
|
|
589
|
+
.sso-button { display: block; border: 1px solid var(--panel-br); border-radius: 10px; padding: 11px 14px; color: var(--txt); text-align: center; font-size: 13.5px; font-weight: 700; background: rgba(255,255,255,0.04); }
|
|
590
|
+
.sso-button:hover { border-color: var(--accent); text-decoration: none; }
|
|
591
|
+
.separator { display: flex; align-items: center; gap: 10px; margin: 18px 0 -6px; color: var(--txt3); font-size: 10px; font-weight: 800; text-transform: uppercase; letter-spacing: .06em; }
|
|
592
|
+
.separator::before, .separator::after { content: ''; height: 1px; flex: 1; background: var(--panel-br); }
|
|
593
|
+
`}</style>
|
|
594
|
+
</head>
|
|
595
|
+
<body>
|
|
596
|
+
<main class="card">
|
|
597
|
+
<div class="brand"><span class="dot"></span> ts-cloud</div>
|
|
598
|
+
<h1>${t?"Choose a new password":"Recover your account"}</h1>
|
|
599
|
+
<p class="sub">${t?"This one-time link expires after one hour.":"Enter your username or verified email. The response is the same whether or not an account exists."}</p>
|
|
600
|
+
<form id="recovery" autocomplete="on">
|
|
601
|
+
${t?'<div class="field"><label for="password">New password</label><input id="password" name="password" type="password" autocomplete="new-password" minlength="12" required></div><div class="field"><label for="confirmation">Confirm new password</label><input id="confirmation" name="confirmation" type="password" autocomplete="new-password" minlength="12" required></div>':'<div class="field"><label for="identifier">Username or email</label><input id="identifier" name="identifier" autocomplete="username" required autofocus></div>'}
|
|
602
|
+
<button type="submit" id="submit">${t?"Change password":"Send reset link"}</button>
|
|
603
|
+
</form>
|
|
604
|
+
<p class="msg" id="msg" role="status" aria-live="polite"></p>
|
|
605
|
+
<p class="note"><a href="/login">Return to sign in</a></p>
|
|
606
|
+
</main>
|
|
607
|
+
<script>
|
|
608
|
+
const form = document.getElementById('recovery')
|
|
609
|
+
const msg = document.getElementById('msg')
|
|
610
|
+
const submit = document.getElementById('submit')
|
|
611
|
+
const reset = ${JSON.stringify(t)}
|
|
612
|
+
const token = new URLSearchParams(location.search).get('token') || ''
|
|
613
|
+
if (reset && !token) {
|
|
614
|
+
msg.textContent = 'This reset link is missing its token. Request a new link.'
|
|
615
|
+
msg.classList.add('shown')
|
|
616
|
+
submit.disabled = true
|
|
617
|
+
}
|
|
618
|
+
form.addEventListener('submit', async (event) => {
|
|
619
|
+
event.preventDefault()
|
|
620
|
+
if (reset && document.getElementById('password').value !== document.getElementById('confirmation').value) {
|
|
621
|
+
msg.textContent = 'The passwords do not match.'
|
|
622
|
+
msg.classList.add('shown')
|
|
623
|
+
return
|
|
624
|
+
}
|
|
625
|
+
submit.disabled = true
|
|
626
|
+
msg.textContent = reset ? 'Changing password…' : 'Requesting reset link…'
|
|
627
|
+
msg.classList.add('shown')
|
|
628
|
+
const response = await fetch(reset ? '/api/auth/password-reset/complete' : '/api/auth/password-reset/request', {
|
|
629
|
+
method: 'POST',
|
|
630
|
+
headers: { 'content-type': 'application/json' },
|
|
631
|
+
body: JSON.stringify(reset
|
|
632
|
+
? { token, password: document.getElementById('password').value }
|
|
633
|
+
: { identifier: document.getElementById('identifier').value }),
|
|
634
|
+
})
|
|
635
|
+
const result = await response.json().catch(() => ({}))
|
|
636
|
+
msg.textContent = result.message || result.error || 'The request could not be completed.'
|
|
637
|
+
if (!response.ok) {
|
|
638
|
+
submit.disabled = false
|
|
639
|
+
return
|
|
640
|
+
}
|
|
641
|
+
if (reset)
|
|
642
|
+
setTimeout(() => location.assign('/login'), 1200)
|
|
643
|
+
})
|
|
644
|
+
</script>
|
|
645
|
+
</body>
|
|
646
|
+
</html>`}var Lb={info:()=>{},warn:()=>{},error:()=>{},step:()=>{},success:()=>{}};async function Fb(e){return await e.driver.findComputeTargets({slug:e.slug,environment:e.environment,role:e.role||"app"})}async function zb(e,t){let r=e.logger||Lb,o=await Fb(e);if(o.length===0)return{success:!1,error:`No '${e.role||"app"}' servers found for ${e.slug}/${e.environment}.`};let m=B(e.slug,t.siteName),h=Dt(m,t.to||"unused"),I=["set -uo pipefail",...wu(h,{...t.to?{to:t.to}:{},unitBase:`${e.slug}-${t.siteName}`}),`(cd ${b} && pantry restart php-fpm) 2>/dev/null || true`,`(cd ${h.current} && eval "$(cd ${b} && pantry env 2>/dev/null)" && php artisan queue:restart) 2>/dev/null || true`];r.step(`Rolling back ${t.siteName}${t.to?` to ${t.to}`:" to the previous release"} on ${o.length} server(s)...`);let R=await e.driver.runRemoteDeploy({targets:o,commands:I,comment:`ts-cloud rollback ${e.slug}/${t.siteName}`,tags:{Project:e.slug,Environment:e.environment,Role:e.role||"app"}});if(!R.success)r.error(`Rollback failed: ${R.error||"unknown error"}`);else r.success(`Rolled back ${t.siteName}.`);return{success:R.success,error:R.error,perInstance:R.perInstance}}async function Ub(e,t){let r=e.logger||Lb;if(!t.database?.name)return{success:!1,error:"No appDatabase configured to restore."};let o=await Fb(e);if(o.length===0)return{success:!1,error:`No '${e.role||"app"}' servers found for ${e.slug}/${e.environment}.`};r.step(`Restoring database '${t.database.name}'${t.from?` from ${t.from}`:" from the latest backup"} on ${o.length} server(s)...`);let m=await e.driver.runRemoteDeploy({targets:o,commands:Yl(t.database,{from:t.from}),comment:`ts-cloud db:restore ${e.slug}/${t.database.name}`,tags:{Project:e.slug,Environment:e.environment,Role:e.role||"app"}});if(!m.success)r.error(`Restore failed: ${m.error||"unknown error"}`);else r.success(`Restored ${t.database.name}.`);return{success:m.success,error:m.error,perInstance:m.perInstance}}var i_=["restart","reload","start","stop","enable","disable"],qb=65536;function Ha(e){return e.length<=qb?e:`${e.slice(0,qb)}
|
|
647
|
+
|
|
648
|
+
[output truncated]`}function o_(e){return/^[A-Za-z0-9_.@:-]+$/.test(e)}function Pg(e){return/^[A-Za-z0-9_-]+$/.test(e)}function s_(e){return e.charAt(0).toUpperCase()+e.slice(1)}function Ou(e,t){let r=[],o=t.servicesDetail??t.services??[];for(let I of o){let R=String(I?.name??"");if(!R||!o_(R))continue;for(let A of i_)r.push({id:`${A}:${R}`,label:`${s_(A)} ${R}`,group:"service",target:R,mutates:!0,confirm:R,danger:A==="stop"||A==="disable"})}for(let[I,R]of Object.entries(e.sites??{})){if(!R||!Pg(I))continue;let A=u(R);if(A==="server-app"||A==="server-php")r.push({id:`rollback:${I}`,label:`Rollback ${I}`,group:"deploy",target:I,mutates:!0,confirm:I,danger:!0})}let m=new Set;for(let I of t.workers??[]){let R=String(I?.name??"").split(":")[0];if(R&&Pg(R))m.add(R)}for(let I of m)r.push({id:`worker:restart:${I}`,label:`Restart workers (${I})`,group:"worker",target:I,mutates:!0,confirm:I});for(let[I,R]of Object.entries(e.sites??{})){if(!R||!Pg(I))continue;if(R.scheduler||R.schedule){let A=pr(R)==="laravel";r.push({id:`scheduler:run:${I}`,label:`${A?"Run":"Restart"} scheduler (${I})`,group:"scheduler",target:I,mutates:!0,confirm:I})}}if(e.infrastructure?.compute?.backups?.enabled){if(r.push({id:"backup:run",label:"Run backup now",group:"backup",target:"backup",mutates:!0,confirm:"backup"}),p(e))r.push({id:"backup:restore",label:"Restore latest DB backup",group:"backup",target:"database",mutates:!0,confirm:"restore",danger:!0})}return r}function _g(e,t,r){return Ou(t,r).find((o)=>o.id===e)}function a_(e,t){let r=e==="enable"||e==="disable"?`systemctl is-enabled ${t} 2>/dev/null || true`:`systemctl is-active ${t} 2>/dev/null || true`;return["set -o pipefail",`systemctl ${e} ${t}`,r]}function c_(){return`eval "$(cd ${b} && pantry env 2>/dev/null)"`}function Wb(e,t,r){let o=`${B(e,t)}/current`;return["set -uo pipefail",`[ -d ${o} ] || { echo "site ${t} has no current release" >&2; exit 1; }`,`cd ${o} && ${c_()} && php artisan ${r}`]}function d_(e,t,r){if(e==="laravel")return Wb(t,r,"queue:restart");let o=`${t}-${r}-queue-*.service`;return["set -uo pipefail",`units=$(systemctl list-units --type=service --all --no-legend '${o}' 2>/dev/null | awk '{print $1}')`,`[ -n "$units" ] || { echo "no queue-worker units found for ${r} (${o})" >&2; exit 1; }`,"systemctl restart $units",`systemctl --no-legend --type=service list-units '${o}' 2>/dev/null || true`]}function l_(e,t,r){if(e==="laravel")return Wb(t,r,"schedule:run");let o=`${t}-${r}-scheduler.service`;return["set -uo pipefail",`systemctl restart ${o}`,`systemctl is-active ${o} 2>/dev/null || true`]}function u_(){return["set -uo pipefail",`[ -x ${sr} ] || { echo "scheduled backups are not provisioned on this server" >&2; exit 1; }`,sr]}function Vb(e,t,r){return{operation:e,command:t,ok:r.success,stdout:Ha(r.perInstance?.[0]?.output??""),stderr:Ha(r.perInstance?.[0]?.error??r.error??"")}}async function $u(e,t,r,o={}){let m=e.project.slug,h;try{h=k({config:e})}catch(N){return{operation:r.id,ok:!1,error:`Could not initialize the cloud driver: ${N?.message??N}`}}let I={driver:h,slug:m,environment:t,role:"app"};if(r.group==="deploy"){let N=o.to&&/^[A-Za-z0-9._-]+$/.test(o.to)?o.to:void 0,F=await zb(I,{siteName:r.target,to:N});return Vb(r.id,`rollback ${r.target}${N?` → ${N}`:""}`,F)}if(r.id==="backup:restore"){let N=await Ub(I,{database:p(e)});return Vb(r.id,"db restore",N)}let R=await h.findComputeTargets({slug:m,environment:t,role:"app"});if(!R.length)return{operation:r.id,ok:!1,error:"No app server target was found for this environment."};let A,E;if(r.group==="service"){let[N,...F]=r.id.split(":");A=a_(N,F.join(":")),E=`systemctl ${N} ${r.target}`}else if(r.group==="worker"){let N=pr(e.sites?.[r.target]??{});A=d_(N,m,r.target),E=N==="laravel"?`queue:restart (${r.target})`:`restart queue units (${r.target})`}else if(r.group==="scheduler"){let N=pr(e.sites?.[r.target]??{});A=l_(N,m,r.target),E=N==="laravel"?`schedule:run (${r.target})`:`restart scheduler (${r.target})`}else if(r.id==="backup:run")A=u_(),E="ts-cloud backup";else return{operation:r.id,ok:!1,error:"Unknown operation."};let x=await h.runRemoteDeploy({targets:[R[0]],commands:A,comment:`ts-cloud dashboard:${r.id}`,tags:{Project:m,Environment:t,Role:"app"}});return{operation:r.id,command:E,ok:x.success,stdout:Ha(x.perInstance?.[0]?.output??""),stderr:Ha(x.perInstance?.[0]?.error??x.error??"")}}async function kc(e,t,r){let o=r.trim();if(!o)return{operation:"command",ok:!1,error:"A command is required."};let m;try{m=k({config:e})}catch(A){return{operation:"command",ok:!1,error:`Could not initialize the cloud driver: ${A?.message??A}`}}let h=e.project.slug,I=await m.findComputeTargets({slug:h,environment:t,role:"app"});if(!I.length)return{operation:"command",ok:!1,error:"No app server target was found for this environment."};let R=await m.runRemoteDeploy({targets:[I[0]],commands:["set -o pipefail",o],comment:"ts-cloud dashboard:command",tags:{Project:h,Environment:t,Role:"app"}});return{operation:"command",command:o,ok:R.success,stdout:Ha(R.perInstance?.[0]?.output??""),stderr:Ha(R.perInstance?.[0]?.error??R.error??"")}}function p_(e,t){return[`${e}-${t}`,`${e}-${t}-queues`,`${e}-${t}-daemons`]}function Hb(e,t,r){return r.some((o)=>p_(t,o).includes(e))}function Tg(e,t){let{user:r,slug:o}=t;if(r.role==="admin")return e;let m=(e.sites??[]).map((K)=>K.name).filter(Boolean),h=mg(r,m),I=new Set(h),R=(e.sites??[]).filter((K)=>I.has(K.name)),A=(e.sitesDetail??[]).filter((K)=>I.has(K.name)),E=new Set(A.map((K)=>K.domain).filter(Boolean)),x=(e.serverDeploymentsDetail??e.serverDeployments??[]).filter((K)=>I.has(K.site)),N=(e.serverLogs??[]).filter((K)=>Hb(K.source,o,h)),F=(e.security?.tlsCertificates??[]).filter((K)=>E.has(K.domain)),z=(e.activity??[]).filter((K)=>{if(K.type==="deploy")return h.some((J)=>String(K.title??"").startsWith(`${J} `));if(K.type==="log")return Hb(String(K.title??"").split(" ")[0],o,h);return!1});return{mode:e.mode,environment:e.environment,environments:e.environments,scoped:!0,server:{name:""},sites:R,sitesDetail:A,workers:(e.workers??[]).filter((K)=>I.has(K.site)),serverDeployments:x.slice(0,5),serverDeploymentsDetail:x.slice(0,50),deploymentsEmptyReason:x.length?void 0:"No deployments have been recorded for your sites yet.",serverLogs:N,serverLogsEmptyReason:N.length?void 0:"No recent log entries for your sites.",security:{tlsCertificates:F,ports:[],firewall:null,authEvents:[]},diagnostics:(e.diagnostics??[]).filter((K)=>["Route conflicts","TLS certificates"].includes(K.name)),activity:z}}function Kb(e,t){if(t.role==="admin")return e;let r=new Set(mg(t,Object.keys(e.sites??{})));return{project:{name:e.project?.name},environment:e.environment,environments:e.environments,sites:Object.fromEntries(Object.entries(e.sites??{}).filter(([o])=>r.has(o)))}}function Jb(e){let t=new RegExp("[\\u007F-\\uFFFF]","g");return JSON.stringify(e).replace(t,(r)=>`\\u${r.charCodeAt(0).toString(16).padStart(4,"0")}`)}var m_=new Set(["name","ssl","env","redirects","aliases","domain","path"]),Gb={build:"build runs as a shell command on the server",start:"start runs as a shell command on the server",root:"root is a filesystem path on the shared server",port:"ports are allocated by the server owner",type:"the deploy type is set by the server owner",php:"the PHP runtime is set by the server owner"};function Yb(e){let t=Object.keys(e).filter((m)=>e[m]!==void 0),r=t.filter((m)=>(m in Gb));if(r.length)return{ok:!1,error:`Only the server owner can change ${r.map((h)=>`${h} (${Gb[h]})`).join(", ")}.`};let o=t.filter((m)=>!m_.has(m));if(o.length)return{ok:!1,error:`Only the server owner can change ${o.join(", ")}.`};return{ok:!0}}function Nu(e){return String(e??"").trim().toLowerCase().replace(/\.$/,"")}function Qb(e){let t=String(e??"/").trim();if(!t||t==="/")return"/";return(t.startsWith("/")?t:`/${t}`).replace(/\/+$/,"")||"/"}function g_(e){let t=[Nu(e?.domain)];if(Array.isArray(e?.aliases))t.push(...e.aliases.map(Nu));return t.filter(Boolean)}function Zb({siteName:e,body:t,sites:r,ownSites:o}){let m=r[e]??{},h=new Set(o),I=new Set;if(t.domain!==void 0)I.add(Nu(t.domain));if(Array.isArray(t.aliases))for(let A of t.aliases)I.add(Nu(A));if(I.delete(""),I.size===0)return{ok:!0};let R=Qb(t.path!==void 0?t.path:m.path);for(let[A,E]of Object.entries(r)){if(A===e||h.has(A))continue;let x=Qb(E?.path);for(let N of g_(E)){if(!I.has(N))continue;if(x===R)return{ok:!1,error:`${N}${R==="/"?"":R} is already served by another site on this server.`}}}return{ok:!0}}class Ec{maxAttempts;lockoutMs;windowMs;attempts=new Map;constructor(e=8,t=900000,r=900000){this.maxAttempts=e;this.lockoutMs=t;this.windowMs=r}key(e,t){return`${e.trim().toLowerCase()}\x00${t}`}check(e,t,r=Date.now()){let o=this.attempts.get(this.key(e,t));if(!o?.until)return{allowed:!0};if(r>=o.until)return this.attempts.delete(this.key(e,t)),{allowed:!0};return{allowed:!1,retryAfterSeconds:Math.ceil((o.until-r)/1000)}}recordFailure(e,t,r=Date.now()){let o=this.key(e,t),m=this.attempts.get(o),I=!m||r-m.last>this.windowMs?1:m.count+1,R={count:I,last:r};if(I>=this.maxAttempts)R.until=r+this.lockoutMs;this.attempts.set(o,R)}recordSuccess(e,t){this.attempts.delete(this.key(e,t))}prune(e=Date.now()){for(let[t,r]of this.attempts)if(r.until?e>=r.until:e-r.last>this.windowMs)this.attempts.delete(t)}get size(){return this.attempts.size}}var eS=new Set([22,80,443]);function Rc(e){return Number.isInteger(e)&&e>=1&&e<=65535}function Hs(e){return[...new Set(e.filter((t)=>Rc(t)&&!eS.has(t)))].sort((t,r)=>t-r)}function tS(e,t,r=[]){if(!Rc(t))throw Error("Port must be an integer between 1 and 65535.");if(eS.has(t))throw Error(`Port ${t} (SSH/HTTP/HTTPS) is always open and is not managed here.`);return nS({configText:e,ports:Hs([...r,t])})}function rS(e,t,r=[]){return nS({configText:e,ports:Hs(r.filter((o)=>o!==t))})}function Xb(e,t,r,o,m){let h=e.slice(t,r),I=new RegExp(`\\b${o}\\s*:\\s*\\${m}`).exec(h);if(!I)return null;let R=t+I.index,A=e.indexOf(m,R);return{propStart:R,open:A,close:vc(e,A,m,m==="{"?"}":"]")}}function xg(e){return`allowedPorts: [${e.join(", ")}]`}function nS(e){let{configText:t}=e,r=Hs(e.ports),o=/\bcompute\s*:\s*{/.exec(t);if(!o)throw Error("Could not find infrastructure.compute in the cloud config.");let m=t.indexOf("{",o.index),h=vc(t,m,"{","}"),I=Xb(t,m+1,h,"firewall","{");if(I){let x=Xb(t,I.open+1,I.close,"allowedPorts","[");if(x){let K=x.propStart;while(K>0&&t[K-1]!==`
|
|
649
|
+
`&&/\s/.test(t[K-1]))K--;let J=t.slice(K,x.propStart);return`${t.slice(0,K)}${J}${xg(r)}${t.slice(x.close+1)}`}let N=t.slice(0,I.close).trimEnd(),F=t.slice(I.close),z=N.endsWith(",")||N.endsWith("{")?"":",";return`${N}${z}
|
|
650
|
+
${xg(r)},
|
|
651
|
+
${F}`}let R=t.slice(0,h).trimEnd(),A=t.slice(h),E=R.endsWith(",")||R.endsWith("{")?"":",";return`${R}${E}
|
|
652
|
+
firewall: {
|
|
653
|
+
enabled: true,
|
|
654
|
+
${xg(r)},
|
|
655
|
+
},
|
|
656
|
+
${A}`}class Dg{client;region;constructor(e="us-east-1"){this.region=e,this.client=new n}async getTraceSummaries(e){let t={StartTime:Math.floor(e.startTime.getTime()/1000),EndTime:Math.floor(e.endTime.getTime()/1000),TimeRangeType:"Event",Sampling:!1};if(e.filterExpression)t.FilterExpression=e.filterExpression;if(e.nextToken)t.NextToken=e.nextToken;let r=await this.client.request({service:"xray",region:this.region,method:"POST",path:"/TraceSummaries",headers:{"Content-Type":"application/json"},body:JSON.stringify(t)});return{summaries:r?.TraceSummaries??[],nextToken:r?.NextToken}}async batchGetTraces(e){if(!e.length)return[];return(await this.client.request({service:"xray",region:this.region,method:"POST",path:"/Traces",headers:{"Content-Type":"application/json"},body:JSON.stringify({TraceIds:e.slice(0,5)})}))?.Traces??[]}}var iS=65536;function gn(e){return e.length<=iS?e:`${e.slice(0,iS)}
|
|
657
|
+
|
|
658
|
+
[output truncated]`}function $g(e,t){return e.environments?.[t]?.app}function Ng(e,t,r){let o=[],m=$g(e,t);o.push({id:"redeploy",label:"Redeploy current build",group:"deploy",target:"app",mutates:!0,confirm:"redeploy"}),o.push({id:"rollback",label:"Roll back to previous build",group:"deploy",target:"app",mutates:!0,confirm:"rollback",danger:!0});let h=r.maintenance?.enabled;if(h!==!0)o.push({id:"maintenance:on",label:"Enable maintenance mode",group:"maintenance",target:"app",mutates:!0,confirm:"maintenance",danger:!0});if(h!==!1)o.push({id:"maintenance:off",label:"Disable maintenance mode",group:"maintenance",target:"app",mutates:!0,confirm:"live"});if(m?.assets||r.assetsInfo)o.push({id:"assets:invalidate",label:"Invalidate CDN cache",group:"assets",target:"assets",mutates:!0,confirm:"invalidate"});if(m?.database?.connection==="aurora-serverless")o.push({id:"db:scale",label:"Scale Aurora capacity",group:"database",target:"database",mutates:!0,confirm:"scale",inputs:[{name:"min",label:"Min ACUs",placeholder:String(m.database.minCapacity??0.5)},{name:"max",label:"Max ACUs",placeholder:String(m.database.maxCapacity??4)}]});for(let I of r.queues??[]){let R=String(I?.name??"").trim();if(R&&/^[A-Za-z0-9_-]+$/.test(R))o.push({id:`queue:purge:${R}`,label:`Purge queue (${R})`,group:"queue",target:R,mutates:!0,confirm:R,danger:!0})}return o}function oS(e,t,r,o){return Ng(t,r,o).find((m)=>m.id===e)}async function f_(e,t,r){try{let o=new f(r),{Stacks:m}=await o.describeStacks({stackName:Mt(e,t)}),h={};for(let A of m?.[0]?.Outputs??[])if(A.OutputKey)h[A.OutputKey]=A.OutputValue??"";let I=h.AssetsCdnDomain;if(!I)return null;return(await new y(r).findDistributionByDomain(I))?.Id??null}catch{return null}}async function sS(e,t,r,o={}){try{if(r.id==="redeploy")return await Kr(e,t),{operation:r.id,command:"serverless redeploy",ok:!0,stdout:"Redeploy complete."};if(r.id==="rollback")return await Wr(e,t),{operation:r.id,command:"serverless rollback",ok:!0,stdout:"Rollback complete."};if(r.id==="maintenance:on")return await Wt(e,t,!0),{operation:r.id,command:"maintenance on",ok:!0,stdout:"Application is now in maintenance mode."};if(r.id==="maintenance:off")return await Wt(e,t,!1),{operation:r.id,command:"maintenance off",ok:!0,stdout:"Application is live."};if(r.id==="db:scale"){let m=Number(o.min),h=Number(o.max);if(!Number.isFinite(m)||!Number.isFinite(h)||m<=0||h<m)return{operation:r.id,ok:!1,error:"Provide valid Aurora capacities (0 < min ≤ max)."};return await Nf(e,t,m,h),{operation:r.id,command:`db:scale ${m}-${h} ACU`,ok:!0,stdout:`Scaling applied (${m}-${h} ACUs); takes effect shortly.`}}if(r.id==="assets:invalidate"){let m=await Nr(e,t),h=await f_(e,t,m.region);if(!h)return{operation:r.id,ok:!1,error:"Could not resolve the asset CloudFront distribution for this environment."};let R=await new y(m.region).invalidateAll(h);return{operation:r.id,command:`cloudfront invalidate ${h} /*`,ok:!0,stdout:`Invalidation ${R?.Id??"created"} for ${h} (/*).`}}if(r.group==="queue"&&r.id.startsWith("queue:purge:")){let m=r.target,h=await Nr(e,t),I=new te(h.region),R=`${h.slug}-${t}-${m}`,{QueueUrl:A}=await I.getQueueUrl(R);return await I.purgeQueue(A),{operation:r.id,command:`sqs purge ${R}`,ok:!0,stdout:`Purged queue ${R}.`}}return{operation:r.id,ok:!1,error:"Unknown or unavailable serverless operation."}}catch(m){return{operation:r.id,ok:!1,error:gn(m?.message??String(m))}}}async function aS(e,t,r){let o=r.trim();if(!o)return{operation:"command",ok:!1,error:"A command is required."};try{let m=await je(e,t,o);return{operation:"command",command:o,ok:!0,stdout:gn(m||"(no output)")}}catch(m){return{operation:"command",command:o,ok:!1,error:gn(m?.message??String(m))}}}var Mg="No dead-letter queue is provisioned for this environment.";async function Bg(e,t,r){try{let{QueueUrl:o}=await e.getQueueUrl(`${t}-${r}-dlq`);return o}catch{return null}}async function cS(e,t,r=10){try{let o=await Nr(e,t),m=new te(o.region),h=await Bg(m,o.slug,t);if(!h)return{ok:!1,messages:[],error:Mg};return{ok:!0,messages:((await m.receiveMessages({queueUrl:h,maxMessages:Math.min(10,Math.max(1,r)),waitTimeSeconds:1,visibilityTimeout:2})).Messages??[]).map((A)=>({id:A.MessageId,receiptHandle:A.ReceiptHandle,body:gn(String(A.Body??""))}))}}catch(o){return{ok:!1,messages:[],error:gn(o?.message??String(o))}}}async function dS(e,t,r={}){try{let o=await Nr(e,t),m=$g(e,t),I=(m?Gt(m,o.slug,t):[]).map((z)=>z.name.replace(`${o.slug}-${t}-`,"")),R=r.targetQueue&&I.includes(r.targetQueue)?r.targetQueue:I[0];if(!R)return{operation:"dlq:redrive",ok:!1,error:"No source queue is configured to redrive into."};let A=new te(o.region),E=await Bg(A,o.slug,t);if(!E)return{operation:"dlq:redrive",ok:!1,error:Mg};let{QueueUrl:x}=await A.getQueueUrl(`${o.slug}-${t}-${R}`),N=Math.min(50,Math.max(1,r.max??10)),F=0;while(F<N){let K=(await A.receiveMessages({queueUrl:E,maxMessages:Math.min(10,N-F),waitTimeSeconds:1,visibilityTimeout:30})).Messages??[];if(!K.length)break;for(let J of K)await A.sendMessage({queueUrl:x,messageBody:String(J.Body??"")}),await A.deleteMessage(E,J.ReceiptHandle),F++}return{operation:"dlq:redrive",command:`redrive → ${R}`,ok:!0,stdout:`Moved ${F} message(s) from the DLQ to ${R}.`}}catch(o){return{operation:"dlq:redrive",ok:!1,error:gn(o?.message??String(o))}}}async function lS(e,t){try{let r=await Nr(e,t),o=new te(r.region),m=await Bg(o,r.slug,t);if(!m)return{operation:"dlq:purge",ok:!1,error:Mg};return await o.purgeQueue(m),{operation:"dlq:purge",command:"sqs purge dlq",ok:!0,stdout:"Dead-letter queue purged."}}catch(r){return{operation:"dlq:purge",ok:!1,error:gn(r?.message??String(r))}}}function uS(e,t){let r=$g(e,t);if(!r?.secrets)return[];return Array.isArray(r.secrets)?r.secrets.map((o)=>({key:o.split("/").pop().toUpperCase().replace(/[^A-Z0-9_]/g,"_"),secretId:o})):Object.entries(r.secrets).map(([o,m])=>({key:o,secretId:String(m)}))}async function pS(e,t,r,o){if(!r.trim())return{operation:"secret:set",ok:!1,error:"A secret id is required."};try{let m=await Nr(e,t),h=new V(m.region);try{await h.putSecretValue({SecretId:r,SecretString:o})}catch{await h.createSecret({Name:r,SecretString:o})}return{operation:"secret:set",command:`secret set ${r}`,ok:!0,stdout:`Secret ${r} updated. Redeploy to apply it to the functions.`}}catch(m){return{operation:"secret:set",ok:!1,error:gn(m?.message??String(m))}}}async function mS(e,t,r){if(!r.trim())return{operation:"secret:delete",ok:!1,error:"A secret id is required."};try{let o=await Nr(e,t);return await new V(o.region).deleteSecret({SecretId:r,RecoveryWindowInDays:7}),{operation:"secret:delete",command:`secret delete ${r}`,ok:!0,stdout:`Secret ${r} scheduled for deletion (recoverable for 7 days).`}}catch(o){return{operation:"secret:delete",ok:!1,error:gn(o?.message??String(o))}}}var h_=new Set(["http","queue","cli"]);async function gS(e,t,r,o){if(!h_.has(r))return{operation:"function:config",ok:!1,error:`Unknown function mode '${r}'.`};let m=o.memory==null?void 0:Number(o.memory),h=o.timeout==null?void 0:Number(o.timeout);if(m!=null&&(!Number.isInteger(m)||m<128||m>10240))return{operation:"function:config",ok:!1,error:"Memory must be an integer between 128 and 10240 MB."};if(h!=null&&(!Number.isInteger(h)||h<1||h>900))return{operation:"function:config",ok:!1,error:"Timeout must be an integer between 1 and 900 seconds."};if(m==null&&h==null)return{operation:"function:config",ok:!1,error:"Provide a memory and/or timeout value."};try{let I=await Nr(e,t),R=new C(I.region),A=`${I.slug}-${t}-${r}`;await R.updateFunctionConfiguration({FunctionName:A,...m!=null?{MemorySize:m}:{},...h!=null?{Timeout:h}:{}});let E=[m!=null?`${m} MB`:"",h!=null?`${h}s`:""].filter(Boolean).join(", ");return{operation:"function:config",command:`update ${A} (${E})`,ok:!0,stdout:`Updated ${A}: ${E}.`}}catch(I){return{operation:"function:config",ok:!1,error:gn(I?.message??String(I))}}}var Og=[{key:"http-errors",label:"HTTP function errors",namespace:"AWS/Lambda",metricName:"Errors",statistic:"Sum",comparison:"GreaterThanThreshold",fnMode:"http",unit:"errors / 5 min"},{key:"http-throttles",label:"HTTP function throttles",namespace:"AWS/Lambda",metricName:"Throttles",statistic:"Sum",comparison:"GreaterThanThreshold",fnMode:"http",unit:"throttles / 5 min"},{key:"http-duration",label:"HTTP function duration (avg)",namespace:"AWS/Lambda",metricName:"Duration",statistic:"Average",comparison:"GreaterThanThreshold",fnMode:"http",unit:"ms"},{key:"http-concurrency",label:"HTTP concurrent executions",namespace:"AWS/Lambda",metricName:"ConcurrentExecutions",statistic:"Maximum",comparison:"GreaterThanThreshold",fnMode:"http",unit:"concurrent"},{key:"queue-errors",label:"Queue function errors",namespace:"AWS/Lambda",metricName:"Errors",statistic:"Sum",comparison:"GreaterThanThreshold",fnMode:"queue",unit:"errors / 5 min"}];function y_(e){return Og.find((t)=>t.key===e)}async function fS(e,t){try{let r=await Nr(e,t);return{ok:!0,alarms:await new hi(r.region).describeAlarms({AlarmNamePrefix:`${r.slug}-${t}-`,MaxRecords:100}),presets:Og}}catch(r){return{ok:!1,alarms:[],presets:Og,error:gn(r?.message??String(r))}}}async function hS(e,t,r,o){let m=y_(r);if(!m)return{operation:"alarm:create",ok:!1,error:`Unknown alarm metric '${r}'.`};if(!Number.isFinite(o)||o<0)return{operation:"alarm:create",ok:!1,error:"Provide a non-negative threshold."};try{let h=await Nr(e,t),I=new hi(h.region),R=`${h.slug}-${t}-${m.key}`,A=m.fnMode?[{Name:"FunctionName",Value:`${h.slug}-${t}-${m.fnMode}`}]:[];return await I.putMetricAlarm({AlarmName:R,Namespace:m.namespace,MetricName:m.metricName,ComparisonOperator:m.comparison,Threshold:o,EvaluationPeriods:1,Period:300,Statistic:m.statistic,Dimensions:A,AlarmDescription:`ts-cloud: ${m.label} > ${o} ${m.unit}`}),{operation:"alarm:create",command:`alarm ${R}`,ok:!0,stdout:`Alarm ${R} armed at ${o} ${m.unit}.`}}catch(h){return{operation:"alarm:create",ok:!1,error:gn(h?.message??String(h))}}}async function yS(e,t,r){try{let o=await Nr(e,t);if(!r.startsWith(`${o.slug}-${t}-`))return{operation:"alarm:delete",ok:!1,error:"Refusing to delete an alarm outside this environment."};return await new hi(o.region).deleteAlarms([r]),{operation:"alarm:delete",command:`delete alarm ${r}`,ok:!0,stdout:`Alarm ${r} deleted.`}}catch(o){return{operation:"alarm:delete",ok:!1,error:gn(o?.message??String(o))}}}function v_(e){let t=e.HasFault?"fault":e.HasError?"error":e.HasThrottle?"throttle":"ok";return{id:String(e.Id??""),timestamp:Number.isFinite(Number(e.StartTime))?new Date(Number(e.StartTime)*1000).toISOString():void 0,durationMs:Math.round((e.Duration??0)*1000),responseMs:Math.round((e.ResponseTime??0)*1000),status:t,method:e.Http?.HttpMethod??"-",url:e.Http?.HttpURL??"-",httpStatus:e.Http?.HttpStatus??0}}async function ju(e,t,r=30){try{let o=await Nr(e,t),m=new Dg(o.region),h=new Date,I=new Date(h.getTime()-Math.min(360,Math.max(1,r))*60000),R=`${o.slug}-${t}-http`,A=await m.getTraceSummaries({startTime:I,endTime:h,filterExpression:`service("${R}")`});if(!A.summaries.length)A=await m.getTraceSummaries({startTime:I,endTime:h});return{ok:!0,traces:A.summaries.slice(0,100).map(v_)}}catch(o){return{ok:!1,traces:[],error:gn(o?.message??String(o))}}}async function Lg(e,t,r){try{let o=await Nr(e,t),m=`${o.slug}-${t}-scheduler`;if(r==="run"){let I=await je(e,t,"schedule:run");return{operation:"scheduler:run",command:"schedule:run",ok:!0,stdout:gn(I||"Scheduler run triggered.")}}let h=new Zt(o.region);if(r==="enable")await h.enableRule({Name:m});else await h.disableRule({Name:m});return{operation:`scheduler:${r}`,command:`${r} ${m}`,ok:!0,stdout:`Scheduler rule ${m} ${r}d.`}}catch(o){return{operation:`scheduler:${r}`,ok:!1,error:gn(o?.message??String(o))}}}function vS(e,t,r){let o=(Array.isArray(r)?r:String(r??"").split(",")).map(String).filter(Boolean);if(!o.length){if(t)return;let h=e.map((I)=>I.id);return h.length?h:["__no_authorized_telemetry_resources__"]}let m=o.map((h)=>e.find((I)=>I.id===h||I.slug===h)?.id).filter((h)=>!!h);if(m.length!==o.length)throw Error("One or more telemetry resources are outside your authorized scope.");return[...new Set(m)]}import{createHash as SS}from"node:crypto";class wS{now;entries=new Map;constructor(e=Date.now){this.now=e}async getOrCreate(e,t,r,o){let m=this.entries.get(e);if(!r&&m&&m.expiresAt>this.now())return{value:await m.promise,cached:!0};let h=o().catch((I)=>{throw this.entries.delete(e),I});return this.entries.set(e,{expiresAt:this.now()+t,promise:h}),{value:await h,cached:!1}}invalidate(e){for(let t of this.entries.keys())if(t.startsWith(e))this.entries.delete(t)}}class kS{now;lastRun=new Map;constructor(e=Date.now){this.now=e}shouldRun(e,t,r=!1){let o=this.now(),m=this.lastRun.get(e);if(!r&&m!=null&&o-m<t)return!1;return this.lastRun.set(e,o),!0}}var IS=new wS,b_=new kS,S_=2000,w_=3600000;function k_(e,t){if(e.kind==="log"&&!t.collectLogs)return!1;if(e.kind==="trace"&&!t.collectTraces)return!1;if((e.kind==="request"||e.name.startsWith("request."))&&!t.collectRequestAnalytics)return!1;if(!["log","trace","request"].includes(e.kind)&&!e.name.startsWith("request."))return!0;let r=e.requestId??e.traceId??`${e.source}\x00${e.workloadId??""}\x00${e.timestamp}`;return Number.parseInt(SS("sha256").update(r).digest("hex").slice(0,8),16)/4294967295<=t.samplingRate}function fn(...e){return`telemetry:${SS("sha256").update(e.map((t)=>String(t??"")).join("\x00")).digest("hex").slice(0,40)}`}function Ac(e,t,r,o){if(!o)return;return e.listResources(t,r).find((m)=>m.slug===o)?.id}function I_(e,t){let r=String(t??"").toLowerCase();if(["debug","info","warn","warning","error","fatal"].includes(r))return r==="fatal"?"error":r;return/fatal|panic|exception|error/i.test(e)?"error":/warn|timeout|throttl|retr(?:y|ies)/i.test(e)?"warning":"info"}function bS(e){let t=e.timestamp&&Number.isFinite(new Date(e.timestamp).getTime())?new Date(e.timestamp).toISOString():new Date().toISOString(),r={};try{let J=JSON.parse(e.message);if(J&&typeof J==="object"&&!Array.isArray(J))r=J}catch{}let o=Object.fromEntries(Object.entries(r).filter(([J])=>!/^(?:body|requestBody|responseBody|headers|authorization|cookie|cookies|query|queryString)$/i.test(J))),m=String(r.requestId??r.request_id??r["x-request-id"]??"")||void 0,h=String(r.traceId??r.trace_id??r["x-amzn-trace-id"]??"")||void 0,I=Number(r.durationMs??r.duration_ms??r.latencyMs??r.latency_ms),R=Number(r.statusCode??r.status_code??r.status),A=String(r.method??r.httpMethod??"")||void 0,E=oi(String(r.pathTemplate??r.path??r.url??"")||void 0),x=String(r.releaseId??r.release_id??"")||void 0,N=String(r.deploymentId??r.deployment_id??"")||void 0,F={projectId:e.projectId,environmentId:e.environmentId,resourceId:e.resourceId,source:e.source,timestamp:t,requestId:m,traceId:h,releaseId:x,deploymentId:N,workloadId:e.workloadId},z=Object.keys(r).length?JSON.stringify(o):e.message,K=[{...F,id:fn(e.source,e.name,t,z),kind:"log",name:e.name,level:I_(z,r.level),message:z,attributes:o}];if(A&&E&&Number.isFinite(R)){if(K.push({...F,id:fn("request",m,t,A,E,R),kind:"request",name:"http.request",method:A,pathTemplate:E,host:r.host,statusCode:R,durationMs:Number.isFinite(I)?I:void 0,bytesIn:Number(r.bytesIn)||void 0,bytesOut:Number(r.bytesOut)||void 0,region:r.region,cacheResult:r.cacheResult??r.cache,upstream:r.upstream,attributes:{userAgentFamily:r.userAgentFamily??"",protocol:r.protocol??""}}),Number.isFinite(I))K.push({...F,id:fn("request-duration",m,t),kind:"metric",name:"request.duration",value:I,unit:"ms",attributes:{method:A,pathTemplate:E,statusCode:R}});if(K.push({...F,id:fn("request-count",m,t),kind:"metric",name:"request.count",value:1,unit:"count",attributes:{method:A,pathTemplate:E,statusCode:R}}),R>=500)K.push({...F,id:fn("request-error",m,t),kind:"metric",name:"request.error",value:1,unit:"count",attributes:{method:A,pathTemplate:E,statusCode:R}})}return K}async function E_(e){let t=new ot(e.controlPlane),r=$t(e.controlPlane,e.projectId),o=new Date,m=[],h=[],I={projectId:e.projectId,environmentId:e.environmentId},R=O(e.config);try{let F=(R==="serverless"?await Jr(e.config,e.environment):await Su(e.config,e.environment,{includeSiteHealth:e.includeSiteHealth,telemetryOnly:e.lightweight}))??{};if(R==="serverless"){for(let K of F.functionsDetail??[]){let J=Ac(e.controlPlane,e.projectId,e.environmentId,K.key),re={...I,resourceId:J,source:"cloudwatch",timestamp:o.toISOString(),workloadId:K.name,attributes:{provider:"aws",functionName:K.name,aggregationWindow:"24h"}};for(let[se,ye,ne]of[["snapshot.traffic.requests_24h",K.invocations,"count"],["snapshot.errors.count_24h",K.errors,"count"],["snapshot.latency.p50_24h",K.p50,"ms"],["snapshot.latency.p95_24h",K.p95,"ms"],["snapshot.latency.p99_24h",K.p99,"ms"],["snapshot.saturation.throttles_24h",K.throttles,"count"],["snapshot.memory.max_24h",K.maxMem,"MB"]])if(Number.isFinite(Number(ye)))m.push({...re,id:fn("cloudwatch",K.name,se,o.toISOString()),kind:"metric",name:se,value:Number(ye),unit:ne})}try{let K=await Nr(e.config,e.environment),J=new hi(K.region),re=new Date(o.getTime()-3600000),se=300;for(let ye of K.functions){let ne=[{Name:"FunctionName",Value:ye.name}],[fe,Ce,_e,he,Ue]=await Promise.all([J.getMetricStatistics({Namespace:"AWS/Lambda",MetricName:"Invocations",Dimensions:ne,StartTime:re,EndTime:o,Period:300,Statistics:["Sum"]}),J.getMetricStatistics({Namespace:"AWS/Lambda",MetricName:"Errors",Dimensions:ne,StartTime:re,EndTime:o,Period:300,Statistics:["Sum"]}),J.getMetricStatistics({Namespace:"AWS/Lambda",MetricName:"Duration",Dimensions:ne,StartTime:re,EndTime:o,Period:300,ExtendedStatistics:["p50","p95","p99"]}),J.getMetricStatistics({Namespace:"AWS/Lambda",MetricName:"Throttles",Dimensions:ne,StartTime:re,EndTime:o,Period:300,Statistics:["Sum"]}),J.getMetricStatistics({Namespace:"AWS/Lambda",MetricName:"ConcurrentExecutions",Dimensions:ne,StartTime:re,EndTime:o,Period:300,Statistics:["Maximum"]})]),ge=Ac(e.controlPlane,e.projectId,e.environmentId,ye.mode),Ke=(nt,Et,cr,at)=>{for(let Ft of nt){let dr=Ft.Timestamp&&Number.isFinite(new Date(Ft.Timestamp).getTime())?new Date(Ft.Timestamp).toISOString():void 0,bt=cr(Ft);if(dr&&bt!=null&&Number.isFinite(bt))m.push({...I,resourceId:ge,id:fn("cloudwatch-series",ye.name,Et,dr),kind:"metric",source:"cloudwatch",name:Et,timestamp:dr,value:bt,unit:at,workloadId:ye.name,region:K.region,attributes:{provider:"aws",functionName:ye.name,aggregationWindow:"300s"}})}};Ke(fe,"traffic.requests",(nt)=>nt.Sum,"count"),Ke(Ce,"errors.count",(nt)=>nt.Sum,"count"),Ke(_e,"latency.p50",(nt)=>nt.Percentiles?.p50,"ms"),Ke(_e,"latency.p95",(nt)=>nt.Percentiles?.p95,"ms"),Ke(_e,"latency.p99",(nt)=>nt.Percentiles?.p99,"ms"),Ke(he,"saturation.throttles",(nt)=>nt.Sum,"count"),Ke(Ue,"saturation.concurrency",(nt)=>nt.Maximum,"count")}}catch(K){h.push({source:"cloudwatch:series",message:K instanceof Error?K.message:String(K)})}for(let K of(F.serverlessLogs??[]).slice(0,S_))m.push(...bS({...I,source:`cloudwatch:${K.source}`,name:`${K.source}.log`,timestamp:K.timestamp,message:String(K.message),resourceId:Ac(e.controlPlane,e.projectId,e.environmentId,K.source)}));for(let K of F.queuesDetail??[]){let J={...I,source:"cloudwatch",timestamp:o.toISOString(),attributes:{queue:K.name}};m.push({...J,id:fn("queue-depth",K.name,o.toISOString()),kind:"metric",name:"queue.depth",value:Number(K.visible??K.depth??0),unit:"count"}),m.push({...J,id:fn("queue-inflight",K.name,o.toISOString()),kind:"metric",name:"queue.inflight",value:Number(K.inFlight??0),unit:"count"})}let z=await ju(e.config,e.environment,30);if(!z.ok)h.push({source:"xray",message:z.error??"X-Ray traces are unavailable."});for(let K of z.traces)m.push({...I,id:fn("xray",K.id),kind:"trace",source:"xray",name:"http.trace",timestamp:K.timestamp??o.toISOString(),traceId:K.id,durationMs:K.durationMs,statusCode:K.httpStatus,method:K.method,pathTemplate:K.url,attributes:{status:K.status,responseMs:K.responseMs}})}else{let z=F.systemMetrics;if(F.metricsUnavailable||!z)h.push({source:"host",message:"The host metrics probe is unavailable."});else{let K=Number(z.memTotalMb)>0?Number(z.memUsedMb)/Number(z.memTotalMb)*100:0,J=Number(z.swapTotalMb)>0?Number(z.swapUsedMb)/Number(z.swapTotalMb)*100:0,re=[["host.load",Number(z.load),"load"],["host.load.5m",Number(z.load5),"load"],["host.load.15m",Number(z.load15),"load"],["host.cpu.capacity",Number(z.cpus),"count"],["host.cpu.used_percent",Number(z.cpuUsedPct),"percent"],["host.memory.used",Number(z.memUsedMb)*1024*1024,"bytes"],["host.memory.total",Number(z.memTotalMb)*1024*1024,"bytes"],["host.memory.available",Number(z.memAvailableMb)*1024*1024,"bytes"],["host.memory.cache",Number(z.memCacheMb)*1024*1024,"bytes"],["host.memory.used_percent",K,"percent"],["host.swap.used",Number(z.swapUsedMb)*1024*1024,"bytes"],["host.swap.total",Number(z.swapTotalMb)*1024*1024,"bytes"],["host.swap.used_percent",J,"percent"],["host.disk.used_percent",Number(z.diskUsedPct),"percent"],["host.disk.used",Number(z.diskUsedGb)*1073741824,"bytes"],["host.disk.total",Number(z.diskTotalGb)*1073741824,"bytes"],["host.disk.inode_used_percent",Number(z.inodeUsedPct),"percent"],["host.network.rx",Number(z.networkRxBytes),"bytes"],["host.network.tx",Number(z.networkTxBytes),"bytes"],["host.network.rx_rate",Number(z.networkRxBytesPerSec),"bytes_per_second"],["host.network.tx_rate",Number(z.networkTxBytesPerSec),"bytes_per_second"],["host.network.rx_today",Number(z.networkRxBytesToday),"bytes"],["host.network.tx_today",Number(z.networkTxBytesToday),"bytes"],["host.network.rx_month",Number(z.networkRxBytesMonth),"bytes"],["host.network.tx_month",Number(z.networkTxBytesMonth),"bytes"],["host.processes",Number(z.processes),"count"],["host.uptime",Number(z.uptimeSeconds),"seconds"]];for(let[se,ye,ne]of re)if(Number.isFinite(ye))m.push({...I,id:fn("host",se,o.toISOString()),kind:"metric",source:"host",name:se,timestamp:o.toISOString(),value:ye,unit:ne});for(let se of F.siteHealth??[]){let ye=String(se.project??e.config.project.slug),ne=String(se.name),fe=ne.startsWith(`${ye}:`)?ne.slice(ye.length+1):ne,Ce=`site:${ye}:${fe}`,_e=Ac(e.controlPlane,e.projectId,e.environmentId,ne),he={...I,resourceId:_e,source:Ce,timestamp:se.checkedAt||o.toISOString(),host:se.route,attributes:{project:ye,site:fe,route:String(se.route),status:String(se.status)}},Ue=[["site.health.up",se.status==="live"?1:0,"boolean"],["site.http.status",Number(se.httpStatus),"status"],["site.response.time",Number(se.responseMs),"ms"]];if(se.tlsDaysRemaining!=null)Ue.push(["site.tls.days_remaining",Number(se.tlsDaysRemaining),"days"]);for(let[ge,Ke,nt]of Ue)if(Number.isFinite(Ke))m.push({...he,id:fn(Ce,ge,he.timestamp),kind:"metric",name:ge,value:Ke,unit:nt})}}}}catch(F){h.push({source:R==="serverless"?"aws":"host",message:F instanceof Error?F.message:String(F)})}{let F=e.config.infrastructure?.compute?.monitoring,z=typeof F==="object"?F?.egressEndpoints:void 0,K=await Yo(z,{now:o.toISOString()});h.push(...K.errors);for(let J of K.metrics)m.push({...I,id:fn(J.attributes.source,J.name,J.timestamp),kind:"metric",source:`egress:${J.attributes.source}`,name:J.name,timestamp:J.timestamp,value:J.value,unit:J.unit,attributes:J.attributes})}if(!e.lightweight)try{let F=await ee(e.config,e.environment),z=new de(e.config,e.environment,{inventory:async()=>F});for(let K of F.workloads){let J=Ac(e.controlPlane,e.projectId,e.environmentId,K.links.service),re={...I,resourceId:J,source:`runtime:${K.provider}`,timestamp:o.toISOString(),workloadId:K.id,releaseId:K.links.release,attributes:{status:K.status,provider:K.provider,service:K.links.service??""}};if(m.push({...re,id:fn(K.id,"health",o.toISOString()),kind:"metric",name:"health.up",value:K.status==="running"?1:0,unit:"boolean"}),m.push({...re,id:fn(K.id,"restarts",o.toISOString()),kind:"metric",name:"saturation.restarts",value:K.restartCount??0,unit:"count"}),K.resources?.memoryBytes!=null)m.push({...re,id:fn(K.id,"memory",o.toISOString()),kind:"metric",name:"runtime.memory.used",value:K.resources.memoryBytes,unit:"bytes"});if(!K.capabilities.logs.supported)continue;try{let se=await z.logs(K.id,{limit:100,since:new Date(o.getTime()-300000)});for(let ye of se.lines)m.push(...bS({...I,resourceId:J,source:`runtime:${K.provider}`,name:`${K.links.service??K.name}.log`,timestamp:ye.timestamp,message:ye.message,workloadId:K.id}))}catch(se){h.push({source:`runtime:${K.provider}`,message:se instanceof Error?se.message:String(se)})}}for(let K of F.sources.filter((J)=>J.status!=="fresh"))h.push({source:K.id,message:K.message??`Runtime source is ${K.status}.`})}catch(F){h.push({source:"runtime",message:F instanceof Error?F.message:String(F)})}if(!e.lightweight)for(let F of e.controlPlane.listEvents({projectId:e.projectId,limit:1000})){let z=F.payload;m.push({...I,id:`telemetry:event:${F.id}`,resourceId:F.resourceId,kind:"event",source:"control-plane",name:F.type,timestamp:F.createdAt,level:F.level,message:F.type,deploymentId:F.operationId,releaseId:typeof z.releaseId==="string"?z.releaseId:void 0,traceId:F.correlationId,attributes:z})}let A=m.filter((F)=>k_(F,r)).map((F)=>({...F,sampled:r.samplingRate<1&&(F.kind==="log"||F.kind==="trace"||F.kind==="request"||F.name.startsWith("request."))})),E=t.appendMany(A).length;if(b_.shouldRun(e.projectId,w_,!e.lightweight))t.enforceRetention(r,e.projectId);let x=t.status(e.projectId,e.environmentId,r.rawDays,r.samplingRate);for(let F of h)if(!x.some((z)=>z.source===F.source))x.push({source:F.source,freshness:"unavailable",samplingRate:r.samplingRate,retentionDays:r.rawDays,estimatedDailyBytes:0,message:F.message});let N=o.toISOString();return e.controlPlane.setSetting(`telemetry.collection:${e.projectId}:${e.environmentId??"all"}`,{generatedAt:N,errors:h,statuses:x,policy:r}),{collected:E,cached:!1,generatedAt:N,statuses:x,errors:h,policy:r}}async function Fg(e){let r=`${`${e.projectId}:${e.environmentId??"all"}:${e.environment}`}:${e.lightweight?"lightweight":"full"}`,o=O(e.config)==="serverless"?300000:60000,m=await IS.getOrCreate(r,o,!!e.force,()=>E_(e));return{...m.value,cached:m.cached}}function ES(e){IS.invalidate(`${e}:`)}function RS(e,t={}){let r=t.shell||(process.platform==="win32"?"powershell.exe":"bash"),o=Bun.spawn([r,"-i"],{stdin:"pipe",stdout:"pipe",stderr:"pipe",cwd:t.cwd,env:{...process.env,TERM:"xterm-256color",PS1:"\\w $ "}}),m=new TextDecoder,h=async(I)=>{if(!I)return;let R=I.getReader();try{for(;;){let{done:A,value:E}=await R.read();if(A)break;if(E)e(m.decode(E))}}catch{}};if(h(o.stdout),h(o.stderr),t.onExit)o.exited.then((I)=>t.onExit(I)).catch(()=>{});return{write(I){try{o.stdin.write(I),o.stdin.flush()}catch{}},close(){try{o.stdin.end()}catch{}try{o.kill()}catch{}}}}var D_="127.0.0.1",O_=7676,CS=65536,BS="ts-cloud-dashboard-",Ja=A_(x_(import.meta.url));function $_(e){try{return process.kill(e,0),!0}catch(t){return t?.code!=="ESRCH"}}function N_(e=Lu(),t=$_){let r=0;for(let o of AS(e,{withFileTypes:!0})){if(!o.isDirectory()||!o.name.startsWith(BS))continue;let m=rr(e,o.name),h=o.name.match(/^ts-cloud-dashboard-(\d+)-/);if(h){let I=Number(h[1]);if(I===process.pid||t(I))continue}else if(AS(m).length>0)continue;Gs(m,{recursive:!0,force:!0}),r+=1}return r}var j_={".html":"text/html; charset=utf-8",".css":"text/css; charset=utf-8",".js":"text/javascript; charset=utf-8",".json":"application/json; charset=utf-8",".svg":"image/svg+xml",".png":"image/png",".jpg":"image/jpeg",".jpeg":"image/jpeg",".webp":"image/webp",".ico":"image/x-icon"};function M(e,t=200,r={}){return new Response(JSON.stringify(e,null,2),{status:t,headers:{"content-type":"application/json; charset=utf-8",...r}})}function M_(e,t,r,o){let m=new TextEncoder,h=Math.max(0,Math.floor(r)),I,R,A=!1,E=new ReadableStream({start(x){let N=()=>{if(A)return;if(A=!0,I?.(),R)clearInterval(R);o.removeEventListener("abort",N);try{x.close()}catch{}},F=(K)=>{if(A)return;if(K.reset)x.enqueue(m.encode(`event: reset
|
|
659
|
+
data: ${JSON.stringify({cursor:K.cursor,droppedBytes:K.droppedBytes})}
|
|
660
|
+
|
|
661
|
+
`));for(let J of K.chunks.filter((re)=>re.cursor>h))h=J.cursor,x.enqueue(m.encode(`id: ${J.cursor}
|
|
662
|
+
event: ${J.stream}
|
|
663
|
+
data: ${JSON.stringify(J)}
|
|
664
|
+
|
|
665
|
+
`));if(K.state!=="open")x.enqueue(m.encode(`event: state
|
|
666
|
+
data: ${JSON.stringify({state:K.state,error:K.error,cursor:K.cursor})}
|
|
667
|
+
|
|
668
|
+
`)),N()};I=e.subscribe(t.id,t.workloadId,(K)=>F(e.read(K.id,K.workloadId,h)??K));let z=e.read(t.id,t.workloadId,h);if(!z)return N();if(F(z),A)return;R=setInterval(()=>{if(!A)x.enqueue(m.encode(`event: heartbeat
|
|
669
|
+
data: ${JSON.stringify({cursor:h,at:new Date().toISOString()})}
|
|
670
|
+
|
|
671
|
+
`))},15000),R.unref?.(),o.addEventListener("abort",N,{once:!0})},cancel(){if(A=!0,I?.(),R)clearInterval(R)}});return new Response(E,{headers:{"cache-control":"no-cache, no-transform",connection:"keep-alive","content-type":"text/event-stream; charset=utf-8","x-accel-buffering":"no"}})}var B_=new Set(["action","operation","site","name","username","database","secretId","port","key","resourceId","environmentId"]);function L_(e,t,r,o,m){let h={};if(r&&typeof r==="object"&&!Array.isArray(r))for(let[R,A]of Object.entries(r)){if(!B_.has(R)||!["string","number","boolean"].includes(typeof A))continue;h[R]=typeof A==="string"?A.slice(0,256):A}let I=String(h.resourceId||h.site||h.secretId||h.database||h.name||t);return{method:e.toUpperCase(),path:t,target:I,status:o,outcome:o<400?"succeeded":"failed",durationMs:Math.max(0,Math.round(m)),input:h}}var F_=gs("ts-cloud-dummy-password");function Fu(e,t=200){return new Response(e,{status:t,headers:{"content-type":"text/plain; charset=utf-8"}})}function z_(e,t,r=process.env){let o=r.TS_CLOUD_DASHBOARD_ORIGIN?.trim()||(r.TS_CLOUD_UI_DOMAIN?.trim()?`https://${r.TS_CLOUD_UI_DOMAIN.trim().replace(/^https?:\/\//,"")}`:""),m=["0.0.0.0","::"].includes(e)?void 0:e,h=o||(m&&["127.0.0.1","localhost","::1"].includes(m)?`http://${m.includes(":")?`[${m}]`:m}:${t}`:"");if(!h)return;try{let I=new URL(h),R=["127.0.0.1","localhost","::1"].includes(I.hostname);if(I.protocol!=="https:"&&!(I.protocol==="http:"&&R)||I.username||I.password||I.search||I.hash||I.pathname!=="/"&&I.pathname!=="")return;return I.origin}catch{return}}function U_(e,t){if(t&&Object.hasOwn(e.environments??{},t))return t;return Object.keys(e.environments??{})[0]??"production"}function q_(e,t,r){return r&&e.includes(r)?r:t}function PS(e,t,r){return e==null||t-e>=r}async function V_(e){let t=[rr(Ja,"..","..","..","..",".env"),rr(e,".env"),rr(e,".env.local"),rr(e,".env.production")];for(let r of t){if(!Zi(r))continue;let o=await pi(r,"utf8").catch(()=>"");for(let m of o.split(`
|
|
672
|
+
`)){let h=m.trim();if(!h||h.startsWith("#")||!h.includes("="))continue;let I=h.indexOf("="),R=h.slice(0,I).trim(),A=h.slice(I+1).trim();if(!/^[A-Z_][A-Z0-9_]*$/i.test(R)||process.env[R]!=null)continue;if(A.startsWith('"')&&A.endsWith('"')||A.startsWith("'")&&A.endsWith("'"))A=A.slice(1,-1);process.env[R]=A}}}function W_(e){return[rr(e,"config","cloud.ts"),rr(e,"config","cloud.js"),rr(e,"cloud.config.ts"),rr(e,"cloud.config.js")].find((r)=>Zi(r))??null}function Ga(e){return e.infrastructure?.compute?.sshKeys??[]}function _S(e,t){let r=e.infrastructure??(e.infrastructure={}),o=r.compute??(r.compute={});o.sshKeys=t}function H_(e,t,r){let o=e.sites??(e.sites={});o[t]=r}function Ug(e){return Hs(e.infrastructure?.compute?.firewall?.allowedPorts??[])}function TS(e,t){let r=e.infrastructure??(e.infrastructure={}),o=r.compute??(r.compute={}),m=o.firewall??(o.firewall={});m.allowedPorts=t}async function me(e){return await e.json().catch(()=>({}))}function Ka(e,t){let r=String(e.scopeType??"environment");if(!["project","environment","service","function","preview"].includes(r))throw Error("Configuration scopeType must be project, environment, service, function, or preview.");let o=String(e.scopeId??(r==="project"?t.projectId:r==="environment"?t.environmentId??"":""));if(!o)throw Error("Configuration scopeId is required.");if(r==="project")return{type:r,id:o};if(r==="environment")return{type:r,id:o,environmentId:o};if(r==="service"||r==="function")return{type:r,id:o,environmentId:String(e.environmentId??t.environmentId??"")||void 0,resourceId:o};return{type:r,id:o,environmentId:String(e.environmentId??t.environmentId??"")||void 0,resourceId:String(e.resourceId??"")||void 0,previewId:o}}function K_(e){if(["GET","HEAD","OPTIONS"].includes(e.method.toUpperCase()))return!0;let t=e.headers.get("sec-fetch-site");if(t==="cross-site")return!1;let r=e.headers.get("origin");if(!r)return!0;let o=new URL(e.url);if(r===o.origin)return!0;if(t!=="same-origin")return!1;try{let m=new URL(r),h=(e.headers.get("x-forwarded-host")??e.headers.get("host")??o.host).split(",")[0].trim(),I=e.headers.get("x-forwarded-proto")?.split(",")[0]?.trim();if(!/^[A-Za-z0-9.-]+(?::\d{1,5})?$/.test(h)||m.host!==h)return!1;return!I||m.protocol===`${I}:`}catch{return!1}}var J_=new Set(["owner","admin","deployer","operator","viewer","auditor"]),G_=new Set(["organization","project","environment","resource"]),Q_=new Set(["POST /api/organization/invitations","DELETE /api/organization/invitations","POST /api/organization/invitations/resend","PATCH /api/organization/memberships","DELETE /api/organization/memberships","POST /api/organization/grants","DELETE /api/organization/grants","POST /api/auth/oidc/providers","PATCH /api/auth/oidc/providers","POST /api/automation/service-accounts","PATCH /api/automation/service-accounts","POST /api/automation/tokens","POST /api/automation/tokens/rotate","DELETE /api/automation/tokens","POST /api/security/policies","PATCH /api/security/policies","POST /api/security/waivers","DELETE /api/security/waivers","POST /api/sources/connections","PATCH /api/sources/connections","DELETE /api/sources/connections","POST /api/sources/deploy-keys","DELETE /api/sources/deploy-keys","PATCH /api/telemetry/settings","POST /api/health/checks","PATCH /api/health/checks","POST /api/alerts/rules","PATCH /api/alerts/rules","POST /api/alerts/silences","POST /api/spend/budgets","PATCH /api/spend/budgets","DELETE /api/spend/budgets","POST /api/spend/check","POST /api/spend/release","POST /api/spend/anomalies/acknowledge","POST /api/protection/attack-mode","POST /api/protection/pause","POST /api/protection/ip-rules","DELETE /api/protection/ip-rules","POST /api/notifications/channels","PATCH /api/notifications/channels","POST /api/notifications/routes","PATCH /api/notifications/routes","POST /api/jobs","PATCH /api/jobs","POST /api/jobs/reconcile","POST /api/workers/action","POST /api/data-services","POST /api/data-services/action","POST /api/data-services/dependencies","POST /api/data-services/management","POST /api/data-services/reveal","POST /api/backups/destinations","POST /api/backups/destinations/test","POST /api/backups/policies","POST /api/backups/run","POST /api/backups/verify","POST /api/backups/restore","POST /api/backups/recovery-points","POST /api/backups/retention","POST /api/configuration/reveal","POST /api/configuration/transfer","POST /api/configuration/bulk","DELETE /api/configuration","POST /api/volumes","POST /api/volumes/attach","POST /api/volumes/detach","POST /api/volumes/resize","POST /api/volumes/snapshot","POST /api/volumes/restore","POST /api/volumes/adopt","POST /api/volumes/discover","DELETE /api/volumes","POST /api/fleet","POST /api/fleet/action","POST /api/capacity","POST /api/capacity/member","POST /api/capacity/action","POST /api/capacity/explain","POST /api/regions","POST /api/regions/action","POST /api/maintenance/cleanup-preview","POST /api/maintenance/cleanup-apply","POST /api/maintenance/drill","POST /api/maintenance/drill-run","POST /api/sources/webhooks","PATCH /api/sources/webhooks","DELETE /api/sources/webhooks","POST /api/onboarding/registries","PATCH /api/onboarding/registries","DELETE /api/onboarding/registries","POST /api/runtime/exec-sessions","POST /api/runtime/files/read","POST /api/runtime/files/write","POST /api/onboarding/apply","PATCH /api/queue/settings","DELETE /api/queue/history","POST /api/serverless/secrets","DELETE /api/serverless/secrets"]);function Mu(e){let t=String(e??"");return J_.has(t)?t:void 0}function Pc(e){let t=String(e.scopeType??"organization");if(!G_.has(t))return;if(t==="organization")return{type:"organization"};let r=String(e.scopeId??"").trim();return r?{type:t,id:r}:void 0}async function Js(e,t,r={}){let o=O(e),m={mode:o,environment:t,environments:Object.keys(e.environments??{}),project:{name:e.project.name,slug:e.project.slug,region:e.project.region}};try{return{...(o==="serverless"?await Jr(e,t):await Su(e,t,{includeSiteHealth:r.includeSiteHealth}))??{},...m}}catch{return{...m}}}function Y_(e,t){if(!e)return t;let r={...t,...Array.isArray(e.sites)?{sites:e.sites}:{},...Array.isArray(e.sitesDetail)?{sitesDetail:e.sitesDetail}:{},...Array.isArray(e.siteHealth)?{siteHealth:e.siteHealth}:{}};if(r.mode!=="serverless"&&Array.isArray(r.sites))r.topology=ht(r,{project:r.project?.name,environment:r.environment});return r}function Z_(e){let t=[rr(e,"packages","ui"),rr(Ja,"..","..","..","ui"),rr(Ja,"..","..","ui"),rr(Ja,"..","ui-src"),rr(Ja,"..","..","ui-src"),rr(Ja,"..","..","dist","ui-src")];for(let r of t)if(Zi(rr(r,"pages"))&&Zi(rr(r,"package.json")))return r;return null}async function X_(e,t){let r=Z_(e);if(!r)return null;let o;try{o=Gg(rr(Lu(),`${BS}${process.pid}-`));let m=rr(r,"node_modules",".bin","stx"),h=Zi(m)?[m,"build","--pages","pages","--out",o,"--no-sitemap","--no-cache"]:["bunx","--bun","@stacksjs/stx","build","--pages","pages","--out",o,"--no-sitemap","--no-cache"],I=Bun.spawn(h,{cwd:r,stdout:"pipe",stderr:"pipe",env:{...process.env,TSCLOUD_DASHBOARD_DATA:Jb(t)}}),[R,A,E]=await Promise.all([new Response(I.stdout).text(),new Response(I.stderr).text(),I.exited]);if(E!==0){if(Gs(o,{recursive:!0,force:!0}),process.env.TSCLOUD_DASHBOARD_VERBOSE)console.warn(`ts-cloud dashboard: live UI build failed; serving the pre-built UI.
|
|
673
|
+
${R}
|
|
674
|
+
${A}`);return null}return o}catch(m){if(o)Gs(o,{recursive:!0,force:!0});if(process.env.TSCLOUD_DASHBOARD_VERBOSE)console.warn(`ts-cloud dashboard: live UI build errored; serving the pre-built UI. ${m.message}`);return null}}function Mi(e){let t=e.infrastructure?.compute?.managedServices?JSON.parse(JSON.stringify(e.infrastructure.compute.managedServices)):void 0;if(t?.vitess?.password)t.vitess.password="";return{project:{name:e.project?.name,slug:e.project?.slug,region:e.project?.region},provider:e.provider,environments:Object.keys(e.environments??{}),compute:e.infrastructure?.compute?{provider:e.infrastructure.compute.provider,runtime:e.infrastructure.compute.runtime,webServer:e.infrastructure.compute.webServer,proxy:e.infrastructure.compute.proxy?{engine:e.infrastructure.compute.proxy.engine,onDemandTls:e.infrastructure.compute.proxy.onDemandTls,cdn:!!e.infrastructure.compute.proxy.cdn}:void 0,managedServices:t,sshKeys:fs(Ga(e)).map((r)=>({name:r.name,type:r.type,fingerprint:r.fingerprint,added:r.added}))}:void 0,sites:Object.fromEntries(Object.entries(e.sites??{}).map(([r,o])=>[r,{domain:o.domain,path:o.path,type:o.type,deploy:o.deploy,root:o.root,port:o.port,ssl:o.ssl}]))}}function Yr(e){return[{id:"status",label:"Refresh Cloud Status",description:"Runs the local ts-cloud status checks against configured providers.",command:["status","--env",e],mutates:!1},{id:"doctor",label:"Run Doctor",description:"Checks local tooling, credentials, and provider access.",command:["doctor"],mutates:!1},{id:"security-scan",label:"Security Scan",description:"Runs the pre-deploy scanner from this checkout.",command:["deploy:security-scan","--source",".","--fail-on","critical"],mutates:!1},{id:"deploy",label:"Deploy Environment",description:"Deploys the selected environment from the local checkout.",command:["deploy","--env",e,"--yes"],mutates:!0,confirm:"deploy"}]}function Fi(e,t){return Yr(t).find((r)=>r.id===e)}function xS(e){if(e.length<=CS)return e;return`${e.slice(0,CS)}
|
|
675
|
+
|
|
676
|
+
[output truncated]`}async function DS(e,t,r){let o=e.getReader(),m=new TextDecoder,h="";while(!0){let{done:R,value:A}=await o.read();if(R)break;let E=m.decode(A,{stream:!0});if(h+=E,E)r?.(t,E)}let I=m.decode();if(I)h+=I,r?.(t,I);return h}async function Hg(e,t){let r=Bun.spawn([process.execPath,t.cliEntry,...e.command],{cwd:t.cwd,stdout:"pipe",stderr:"pipe",env:{...process.env,...t.env}}),o=()=>{try{r.kill()}catch{}};if(t.signal?.aborted)o();else t.signal?.addEventListener("abort",o,{once:!0});try{let[m,h,I]=await Promise.all([DS(r.stdout,"stdout",t.onOutput),DS(r.stderr,"stderr",t.onOutput),r.exited]);return{action:e.id,command:["cloud",...e.command].join(" "),exitCode:I,ok:I===0,stdout:xS(m),stderr:xS(h)}}finally{t.signal?.removeEventListener("abort",o)}}function eT(){return Object.entries(process.env).filter(([e,t])=>!!t&&t.length>=8&&/(?:secret|token|password|passwd|private|credential|api.?key|access.?key)/i.test(e)).map(([,e])=>e)}var tT=new Set(["/server/sites","/server/deployments","/server/logs","/integrations","/applications/new","/applications/compose","/operations/queue","/operations/previews","/operations/releases","/operations/workloads","/operations/observability","/operations/alerts","/operations/jobs","/data/backups","/account/security","/security","/access-denied"]);function rT(e){let t=_c(e);if(t&&t!==".html")return!1;let r=e.replace(/\.html$/,"").replace(/\/+$/,"")||"/";if(r==="/")return!1;return!tT.has(r)}var nT={"/":"runtime:read","/server/activity":"audit:read","/server/sites":"project:read","/server/deployments":"deployments:read","/server/logs":"runtime:logs","/server/metrics":"runtime:read","/server/services":"runtime:read","/data/backups":"backups:read","/data/volumes":"data:read","/server/fleet":"fleet:read","/server/capacity":"fleet:read","/server/actions":"runtime:restart","/server/database":"data:read","/server/firewall":"fleet:read","/infrastructure/topology":"fleet:read","/server/security":"security:read","/security":"security:read","/integrations":"sources:read","/applications/new":"applications:read","/applications/compose":"applications:read","/operations/queue":"deployments:read","/operations/previews":"deployments:read","/operations/releases":"deployments:read","/operations/regions":"deployments:read","/operations/maintenance":"deployments:read","/operations/workloads":"runtime:read","/operations/observability":"runtime:logs","/operations/alerts":"runtime:read","/operations/jobs":"automation:read","/operations/configuration":"config:read","/server/ssh-keys":"fleet:read","/server/terminal":"runtime:terminal","/server/team":"users:read","/account/security":"project:read","/account/automation":"users:read","/serverless":"project:read","/serverless/deployments":"deployments:read","/serverless/logs":"runtime:logs","/serverless/metrics":"runtime:read","/serverless/traces":"runtime:read","/serverless/functions":"config:read","/serverless/queues":"data:read","/serverless/data":"data:read","/serverless/assets":"project:read","/serverless/secrets":"secrets:read","/serverless/firewall":"fleet:read","/serverless/alarms":"runtime:read","/serverless/cost":"project:read"};function OS(e,t){if(t.role==="admin")return!0;let r=_c(e);if(r&&r!==".html")return!0;if(t.organizationSource==="legacy")return!rT(e);let o=e.replace(/\.html$/,"").replace(/\/+$/,"")||"/";if(o==="/access-denied")return!0;let m=nT[o];return!!m&&!!t.capabilities?.includes(m)}function iT(e,t){let r=decodeURIComponent(t).replace(/^\/+/,""),m=C_(r===""?"index.html":r);if(m.startsWith("..")||m.includes("/../"))return null;let h=rr(e,m);if(Zi(h)&&!MS(h).isDirectory())return h;if(!_c(h)&&Zi(`${h}.html`))return`${h}.html`;if(!_c(h)&&Zi(rr(h,"index.html")))return rr(h,"index.html");return null}async function $S(e,t){let r=iT(e,t);if(!r){let h=rr(e,"404.html");if(Zi(h))return new Response(await pi(h),{status:404,headers:{"content-type":"text/html; charset=utf-8"}});return Fu("Not found",404)}if(MS(r).isDirectory())return Fu("Not found",404);let o=await pi(r),m=j_[_c(r)]??"application/octet-stream";return new Response(o,{headers:{"content-type":m}})}var Bu=["observe","backup","restore","restart","resize","version","rotate","expose","delete","logs","slow_queries","users","databases"],NS=new WeakMap;async function jS(e){let t=NS.get(e);if(t)return t;let r=await import("./spend/index.js"),o=new r.SpendStore(e),m={store:o,service:new r.SpendService(o),gate:new r.SpendGate(e)};return NS.set(e,m),m}async function pa(e={}){N_();let t=e.cwd??process.cwd(),r=e.host??D_,o=e.port??O_,m=e.cliEntry??process.argv[1];if(e.box)process.env.TS_CLOUD_DASHBOARD_BOX="1";await V_(t);let h=e.config??await wd(),I=process.env.TS_CLOUD_DASHBOARD_DB_PASSWORD,R=p(h);if(I&&R&&!R.password)R.password=I;let A=h.infrastructure?.compute?.managedServices?.vitess;if(I&&A&&typeof A==="object"&&!A.password)A.password=I;let E=Rl(t,h);if(E.reconciliation.failed>0)console.warn(` ts-cloud dashboard: marked ${E.reconciliation.failed} orphaned operation(s) as failed after restart.`);let x=Object.keys(h.environments??{}),N=U_(h,e.environment),F=W_(t),z=await Js(h,N),K=new Map([[N,z]]),J=new Map([[N,Date.now()]]),re=new Map([[N,Date.now()]]),se=new Map,ye=Math.min(300000,Math.max(5000,Number(process.env.TS_CLOUD_DASHBOARD_REFRESH_MS)||30000)),ne=Math.min(1800000,Math.max(60000,Number(process.env.TS_CLOUD_DASHBOARD_SITE_REFRESH_MS)||300000)),fe=O(h)!=="serverless",Ce=(L)=>{let Qe=K.get(L);if(Qe&&!PS(J.get(L),Date.now(),ye))return Promise.resolve(Qe);let Ze=se.get(L);if(Ze)return Ze;let Je=!fe||PS(re.get(L),Date.now(),ne),it=Js(h,L,{includeSiteHealth:Je}).then((Tt)=>{let H=K.get(L),un=Je?Tt:Y_(H,Tt);if(K.set(L,un),J.set(L,Date.now()),Je)re.set(L,Date.now());if(H?._serverReachable!==un._serverReachable||H?._metricsStatus!==un._metricsStatus||(H?.sitesDetail?.length??0)!==(un.sitesDetail?.length??0))at();return un}).finally(()=>se.delete(L));return se.set(L,it),it},_e=Pt(t),he=process.env.TS_CLOUD_DASHBOARD_TELEMETRY?.trim();if(e.box&&he!=="0"&&(he==="1"||!h.cloud?.attachTo)){let L=!1,Qe=Date.now()+300000,Ze=async()=>{if(L)return;L=!0;try{let it=Date.now(),Tt=it>=Qe;if(Tt)Qe=it+300000;let H=E.environments.get(N);await Fg({controlPlane:E.store,projectId:E.project.id,environmentId:H?.id,config:h,environment:N,force:!0,lightweight:!0,includeSiteHealth:Tt})}catch(it){if(e.verbose)console.warn(` ts-cloud dashboard: background telemetry collection failed: ${it instanceof Error?it.message:String(it)}`)}finally{L=!1}};Ze(),setInterval(()=>void Ze(),60000).unref?.()}let ge=new Map,Ke=new Map,nt=new Set,Et=0;function cr(L,Qe){if(L.role==="admin"&&!E.store.getActorByExternalId("user",`dashboard:${L.username.toLowerCase()}`))return L;let Ze=E.store.getActorByExternalId("user",`dashboard:${L.username.toLowerCase()}`),Je=Ze?E.store.getMembershipForActor(E.organization.id,Ze.id):void 0;if(!Je||Je.status!=="active")return{...L,role:"member",sites:{},capabilities:[]};let it=E.store.listGrants(Je.id),Tt=E.store.resolveAuthorizationTarget(E.organization.id,Je.scope)??E.store.resolveAuthorizationTarget(E.organization.id,{type:"project",id:E.project.id}),H=Bt({membership:Je,grants:it,target:Tt});if(Je.roleTemplate==="owner"||Je.roleTemplate==="admin")return{...L,role:"admin",sites:{},organizationRole:Je.roleTemplate,organizationSource:Je.source,capabilities:H};let un=E.environments.get(Qe),kr={};for(let yt of E.store.listResources(E.project.id,un?.id).filter((It)=>It.kind==="application")){let It=E.store.resolveAuthorizationTarget(E.organization.id,{type:"resource",id:yt.id});if(!It||!w({membership:Je,grants:it,capability:"project:read",target:It}).allowed)continue;kr[yt.slug]=w({membership:Je,grants:it,capability:"config:write",target:It}).allowed?"owner":"collaborator"}return{...L,role:"member",sites:kr,organizationRole:Je.roleTemplate,organizationSource:Je.source,capabilities:H}}function at(){Et+=1,ge.clear();for(let L of nt)Gs(L,{recursive:!0,force:!0});nt.clear()}let Ft=(L,Qe)=>`${Qe}:${L.role==="admin"?"admin":`member:${Object.entries(L.sites).sort(([Ze],[Je])=>Ze.localeCompare(Je)).map(([Ze,Je])=>`${Ze}=${Je}`).join(",")}`}:${[...L.capabilities??[]].sort().join(",")}`;async function dr(L,Qe){let Ze=cr(L,Qe),Je=Ft(Ze,Qe),it=ge.get(Je);if(it)return it;let Tt=Ke.get(Je);if(Tt)return Tt;let H=Et,un=(async()=>{let kr=K.get(Qe)??await Js(h,Qe);K.set(Qe,kr);let yt={...Tg(kr,{user:Ze,slug:h.project.slug}),viewer:{role:Ze.role,organizationRole:Ze.organizationRole,organizationSource:Ze.organizationSource,capabilities:Ze.capabilities??[]}},It=await X_(t,yt);if(It&&H!==Et)return Gs(It,{recursive:!0,force:!0}),_e?.uiRoot;if(It)nt.add(It);let nn=It??_e?.uiRoot;if(nn)ge.set(Je,nn);return nn})();Ke.set(Je,un);try{return await un}finally{Ke.delete(Je)}}let bt=await dr({username:"",passwordHash:"",role:"admin",sites:{}},N);if(!bt)throw Error("ts-cloud dashboard UI not found. Run `bun run build` in ts-cloud or reinstall the package.");let Y=new WeakMap,be=process.env.TS_CLOUD_DASHBOARD_TERMINAL!=="0",Ee=process.env.TS_CLOUD_DASHBOARD_AUTH==="0";if(Ee&&e.box)throw Error("TS_CLOUD_DASHBOARD_AUTH=0 is refused in box mode: the dashboard is internet-facing there, and disabling auth would expose a root shell.");let Ne=!Ee,Rt=Cb(t),Ut=Ne?xb(t,process.env.TS_CLOUD_UI_USERNAME?.trim()||"admin"):void 0,Fe=new Ci(E.store,{encryptionKey:Ge(t)}),Ct=new bi(E.store),St=new Fr(E.store),rt=new fo(E.store,{encryptionKey:Ge(t)}),ln=new ip(E.store),Ys=new cp(E.store,{cwd:t}),ei=new sp(E.store,{encryptionKey:Ge(t)}),nr=new c(E.store,{workerId:`dashboard:${process.pid}`}),lr=new No(E.store),mi=new Fo(E.store,Ge(t)),Xi=new aa(E.store),Tc=new Ni(lr,nr,mi),eo=h.project?.region??"us-east-1",Qg=Ge(t),Rn=new ua(Xi,{encryptionKey:Qg,backends:[new Xr(E.store,Qg),new ca(new V(eo),eo),new da(new Ie(eo),eo),new la]});await Fl(Rn,E,h);let zu=new j(eo),LS={aws_rds:new ko(new Lo(zu)),aws_aurora:new Bo(new $o(zu)),aws_elasticache:new xo(new Mo(new We(eo))),server:new _o(new jt),container:new Oo(new jt)},ef=(L)=>LS[L.provider],Er=new Zo(E.store),hs=new $i(Er,nr),tf=new ea(mi),FS={managed_database:new ta(lr,zu),logical_database:new na(lr,mi),volume:new ia,files:new ra(t),control_plane:new sa(E.store,t),infrastructure:new oa(new Jt(eo))},zS=(L)=>FS[L.resourceKind],US=(L)=>L.provider==="aws_backup"?void 0:tf,Uu=new xt(E.store),qu=[new Sr,new Es(d(t,"volumes"))],gi=new bs(Uu,qu,nr),Qa=new Uo(E.store),ti=new Qo(Qa,[new qr("aws"),new qr("hetzner"),new qr("ssh")],nr),to=new Ho(E.store),rf=new Go(to,Qa),ys=new qo(E.store),nf=new Vo(ys,nr),fi=new zo(E.store),Vu=new Ko(fi,nr),of=["releases","artifacts","operations","logs","previews","snapshots","orphaned_resources"].map((L)=>new Wo(L,E.store,E.project.id,E.organization.id)),Zr=new Ro(E.store),sf=new Po(Zr,{queue:nr}),wr=new Ir(E.store),ro=new Cr(E.store),$n=new dt(E.store),ct=new ni(E.store,{encryptionKey:Ge(t)}),qS=()=>jS(E.store);for(let L of E.environments.values())Do(Zr,h,{organization:E.organization,project:E.project,environment:L,resources:E.store.listResources(E.project.id,L.id)});let Nn=new ii(ct,{emailImpl:async(L)=>{let{email:Qe}=await import("./chunk-wrnhbb13.js");return Qe.send(L)}}),af=async(L)=>{let Qe=Ls(ct,E.project.id,L),Ze=[];for(let Je of Qe.filter((it)=>it.notify&&it.alert))Ze.push(...await Nn.deliverAll(Nn.enqueue(E.organization.id,Je.alert,Je.transition)));return Ze.push(...await Nn.deliverAll(Nn.enqueueRemindersAndEscalations(E.organization.id,ct.listAlerts(E.project.id,{environmentId:L,states:["firing"]})))),Ze.push(...await Nn.retryDue()),{evaluations:Qe,deliveries:Ze}},Wu=!1,VS=async()=>{if(Wu)return;Wu=!0;try{for(let L of E.environments.values())for(let Qe of ct.listHealthChecks(E.project.id,L.id).filter((Ze)=>Ze.enabled)){let Ze=ct.listHealthResults(Qe.id,1)[0];if(Ze&&Date.now()-new Date(Ze.checkedAt).getTime()<Qe.intervalSeconds*1000)continue;let Je=await new $e(ct).runAndEvaluate(Qe);for(let it of Je.evaluations.filter((Tt)=>Tt.notify&&Tt.alert))await Nn.deliverAll(Nn.enqueue(E.organization.id,it.alert,it.transition))}}finally{Wu=!1}},cf=setInterval(()=>{for(let L of E.environments.values())af(L.id).catch((Qe)=>console.error("ts-cloud alert evaluation failed:",Qe))},60000);cf.unref?.();let df=setInterval(()=>{VS().catch((L)=>console.error("ts-cloud health check evaluation failed:",L))},30000);df.unref?.();let lf=setInterval(()=>{try{sf.tick()}catch(L){console.error("ts-cloud scheduled job evaluation failed:",L)}},30000);lf.unref?.();let uf=setInterval(()=>{try{hs.enqueueDue(),hs.enqueueRetention()}catch(L){console.error("ts-cloud backup policy evaluation failed:",L)}},30000);uf.unref?.(),wr.cleanup();let pf=setInterval(()=>{try{wr.cleanup()}catch(L){console.error("ts-cloud preview cleanup failed:",L)}},3600000);pf.unref?.();let WS=e.queueWorker??!0,HS=eT(),KS=async(L,Qe)=>{let Je=(L.environmentId?E.store.listEnvironments(L.projectId).find((it)=>it.id===L.environmentId):void 0)?.slug??String(N);if(Qe.kind==="dashboard_operation"&&Qe.operationId){let it=K.get(Je)??z,Tt=_g(Qe.operationId,h,it);if(!Tt||Tt.danger||!["scheduler","backup","worker"].includes(Tt.group))return{ok:!1,stderr:"The allowlisted dashboard operation is no longer available."};let H=await $u(h,Je,Tt);return{ok:H.ok,stdout:H.stdout,stderr:H.stderr??H.error,retryable:/temporar|unavailable|timeout|network/i.test(H.stderr??H.error??""),output:{command:H.command??null,operation:H.operation}}}if(Qe.kind==="serverless_scheduler"){let it=await Lg(h,Je,"run");return{ok:it.ok,stdout:it.stdout,stderr:it.error,retryable:/temporar|unavailable|timeout|network/i.test(it.error??""),output:{command:it.command??null,operation:"serverless_scheduler"}}}return{ok:!1,stderr:`No safe executor is configured for ${Qe.kind}.`}},xc=WS?new Ss(nr,{...Sc({store:E.store,execute:async(L,Qe)=>{let Ze=Qe.operation.input&&typeof Qe.operation.input==="object"&&!Array.isArray(Qe.operation.input)?Qe.operation.input:{},Je=Ze.source&&typeof Ze.source==="object"?Ze.source:void 0,it=t,Tt,H,un;try{if(Je?.bindingId&&Je?.connectionId&&Je?.commitSha){let yt=rt.getBinding(String(Je.bindingId)),It=rt.getConnection(String(Je.connectionId));if(!yt||!It||yt.connectionId!==It.id)throw Error("Queued source binding is no longer available");let Dr=(yt.repositoryId?rt.getRepository(yt.repositoryId):rt.listRepositories(It.id).find((ma)=>ma.fullName.toLowerCase()===yt.repositoryFullName.toLowerCase()))?.cloneUrl;if(!Dr&&It.provider==="generic_ssh")Dr=`git@${new URL(It.host).hostname}:${yt.repositoryFullName}.git`;if(!Dr)Dr=new URL(`/${yt.repositoryFullName}.git`,It.host.endsWith("/")?It.host:`${It.host}/`).href;let xe=yt.deployKeyId?rt.getDeployKey(yt.deployKeyId):void 0,ur=xe&&yt.deployKeyId?{...xe,privateKey:rt.getDeployPrivateKey(yt.deployKeyId)}:void 0;if(["github","gitlab","bitbucket","gitea"].includes(It.provider)&&L.target.previewId){let ma=qe(It,rt.getCredential(It.id)),Se=wr.previews.getInstance(L.target.previewId);un=async(jn,jc)=>{try{await ma.setCommitStatus(yt.repositoryFullName,String(Je.commitSha),{state:jn,url:Se?.url,description:jc})}catch(ga){Qe.log(`Could not publish source status: ${ga instanceof Error?ga.message:String(ga)}`,{stream:"stderr"})}},await un("pending",`Preview deployment started for ${String(Je.commitSha).slice(0,12)}`)}Tt=Gg(rr(Lu(),"ts-cloud-queued-source-")),Qe.log(`Checking out immutable source commit ${Je.commitSha}.`,{stream:"system"}),it=(await yo({remote:Dr,binding:yt,destination:rr(Tt,"checkout"),ref:String(Je.branch??yt.defaultBranch),commitSha:String(Je.commitSha)},{credential:rt.getCredential(It.id),deployKey:ur})).directory}if(Qe.operation.kind!=="preview.destroy"){let yt=L.target.previewId?wr.previews.getInstance(L.target.previewId):void 0,It=yt?wr.previews.getDefinition(yt.definitionId):void 0,nn=Qe.operation.resourceId?E.store.getResource(Qe.operation.resourceId):void 0;if(H=await Rn.resolve({projectId:E.project.id,environmentId:Qe.operation.environmentId,resourceId:Qe.operation.resourceId,functionId:nn?.kind==="function"?nn.id:void 0,previewId:yt?.id,trustedPreview:!!yt&&!yt.fork,allowedPreviewSecrets:It?.inheritedSecrets}),H.warnings.some((Dr)=>Dr.code==="missing_required"))throw Error(`Required deployment configuration is unavailable: ${H.warnings.filter((Dr)=>Dr.code==="missing_required").map((Dr)=>Dr.key).join(", ")}`);if(Qe.operation.resourceId)for(let Dr of Object.values(H.entries))Xi.setDependency({entryId:Dr.id,resourceId:Qe.operation.resourceId,injectionTarget:"environment",required:Dr.required,requiresRedeploy:!0});Qe.log(`Resolved configuration ${H.configurationHash.slice(0,18)} for ${Object.keys(H.values).length} keys.`,{stream:"system"})}let kr=await Hg(L,{cwd:it,cliEntry:m,signal:Qe.signal,env:H?.values,onOutput:(yt,It)=>Qe.log(It,{stream:yt,step:"execute",secrets:[...HS,...Object.entries(H?.entries??{}).filter(([,nn])=>nn.kind==="secret").map(([nn])=>H.values[nn]).filter(Boolean)]})});if(kr.ok&&Qe.operation.resourceId&&H)for(let yt of Object.values(H.entries))Xi.setDependency({entryId:yt.id,resourceId:Qe.operation.resourceId,injectionTarget:"environment",required:yt.required,requiresRedeploy:!1,lastDeployedVersion:yt.version});return await un?.(kr.ok?"success":"failure",kr.ok?`Preview is ready at ${String(Je?.commitSha).slice(0,12)}`:`Preview deployment failed at ${String(Je?.commitSha).slice(0,12)}`),kr}catch(kr){throw await un?.("error",`Preview deployment could not complete at ${String(Je?.commitSha).slice(0,12)}`),kr}finally{if(Tt)Gs(Tt,{recursive:!0,force:!0})}}}),...al({store:Zr,executor:KS}),...dl({store:lr,secrets:mi,resolveAdapter:ef}),...pl(Qa,ti.drivers),...ml(ys,[new jo("aws","AWS regional execution requires an injected AwsRegionalTransport; topology and plans were preserved without provider mutation.")]),...Il({store:fi,cleanup:of}),...Cc(Uu,qu),..._l({store:Er,queue:nr,resolveSource:zS,resolveDestination:US,validateHealth:async(L,Qe)=>{if(!L.healthCheckId)return{healthy:!0,mode:"adapter"};let Ze=ct.getHealthCheck(L.healthCheckId);if(!Ze||Ze.projectId!==L.projectId)return{healthy:!1,error:"Configured restore health check was not found."};let Je=await new $e(ct).runAndEvaluate(Ze);return{healthy:Je.result.status==="healthy",healthCheckId:Ze.id,healthResultId:Je.result.id,target:Qe}}}),...Fd({store:E.store,resolveDriver:e.releaseDriver??((L)=>({name:`${E.store.getResource(L.resourceId)?.provider??"provider"} release driver`,capability:()=>({strategy:L.strategy,supported:!1,explanation:"This dashboard process has no immutable activation driver configured for the target provider.",capacityMultiplier:1,costImpact:"none",rollback:"The previous release remains preserved; configure a release driver before retrying."}),activate:async()=>{throw Error("Immutable activation driver is not configured")},rollback:async()=>{throw Error("Immutable rollback driver is not configured")}})),resolveHealthGate:async(L,Qe)=>{let Ze=ct.listHealthChecks(Qe.projectId,Qe.environmentId).find((it)=>it.enabled&&it.name===L&&(!it.resourceId||it.resourceId===Qe.resourceId));if(!Ze)return{healthy:!1,message:`Named health gate ${L} was not found for this release.`};let Je=await new $e(ct).runAndEvaluate(Ze);return{healthy:Je.result.status==="healthy",evidence:{healthCheckId:Ze.id,healthResultId:Je.result.id,status:Je.result.status,timings:Je.result.timings},message:Je.result.message}}})},{parallelism:e.queueParallelism??(Number(process.env.TS_CLOUD_QUEUE_PARALLELISM)||8),onError:(L)=>console.error("ts-cloud deployment queue worker failed:",L)}):void 0;if(Yd(E),Ri(St,Z(E,String(N)),z),Ut)_i(E,Ut.users),$b(Fe,E,Ut.users);let gr=Db({cwd:t,enabled:Ne,secret:Rt,authentication:Fe,authorization:{store:E.store,organizationId:E.organization.id,projectId:E.project.id,defaultEnvironment:String(N)}}),Rr=new wo,mf=setInterval(()=>Rr.sweep(),60000);mf.unref?.();let Dc=new Ec,Hu=new Ec(3,900000,900000),Oc=new Ec(8,900000,900000),gf=setInterval(()=>Dc.prune(),300000),ff=setInterval(()=>Hu.prune(),300000),hf=setInterval(()=>Oc.prune(),300000);if(gf.unref?.(),ff.unref?.(),hf.unref?.(),Ne){if(Ut?.generated)console.warn(`
|
|
677
|
+
ts-cloud dashboard: created the first admin.
|
|
678
|
+
username: ${Ut.generated.username}
|
|
679
|
+
password: ${Ut.generated.password}
|
|
680
|
+
Saved (hashed) to ${_b()}. This password is shown once.
|
|
681
|
+
`)}else console.warn(" ts-cloud dashboard: authentication is DISABLED (TS_CLOUD_DASHBOARD_AUTH=0). Every request runs as an admin.");let no=r!=="127.0.0.1"&&r!=="localhost",Ku=(L)=>R_("sha256").update(`${Rt}:${L}`).digest("hex").slice(0,16),JS=Co({controlPlane:E.store,identities:Ct,sources:rt,applications:{drafts:ln,artifacts:Ys,registries:ei}}),yf=(L)=>L.headers.get("user-agent")?.trim().slice(0,256)||void 0,$c=(L,Qe,Ze,Je="local")=>Fe.createSession({identityId:L,authMethod:Je,userAgent:yf(Qe),networkHint:Ku(Ze)}),Xe=(L)=>{let Qe=E.store.getActorByExternalId("user",`dashboard:${L.username.toLowerCase()}`),Ze=Qe?E.store.getMembershipForActor(E.organization.id,Qe.id):void 0;return{actor:Qe,membership:Ze,grants:Ze?E.store.listGrants(Ze.id):[]}},Zs=(L,Qe,Ze)=>{if(L.role==="admin")return!0;let Je=E.store.getOperation(Qe),it=Xe(L);if(!Je||!it.membership)return!1;let Tt=Je.resourceId?{type:"resource",id:Je.resourceId}:Je.environmentId?{type:"environment",id:Je.environmentId}:Je.projectId?{type:"project",id:Je.projectId}:{type:"organization"},H=E.store.resolveAuthorizationTarget(E.organization.id,Tt);return!!H&&w({membership:it.membership,grants:it.grants,capability:Ze,target:H}).allowed},rn=(L,Qe,Ze)=>{if(L.role==="admin")return!0;let Je=Xe(L);if(!Je.membership)return!1;let it=E.store.resolveAuthorizationTarget(E.organization.id,{type:"resource",id:Qe});return!!it&&w({membership:Je.membership,grants:Je.grants,capability:Ze,target:it}).allowed},Ya=(L,Qe,Ze)=>{if(L.role==="admin")return!0;let Je=Xe(L);if(!Je.membership)return!1;let it=E.store.resolveAuthorizationTarget(E.organization.id,{type:"environment",id:Qe});return!!it&&w({membership:Je.membership,grants:Je.grants,capability:Ze,target:it}).allowed},Nc=(L,Qe)=>{if(L.role==="admin")return!0;let Ze=Xe(L);if(!Ze.membership)return!1;let Je=E.store.resolveAuthorizationTarget(E.organization.id,{type:"organization"});return!!Je&&w({membership:Ze.membership,grants:Ze.grants,capability:Qe,target:Je}).allowed},Xs=Bun.serve({hostname:r,port:o,idleTimeout:30,websocket:{open(L){if(!be){L.send(`The web terminal is disabled (TS_CLOUD_DASHBOARD_TERMINAL=0).\r
|
|
682
|
+
`),L.close();return}let Qe=RS((Ze)=>L.send(Ze),{cwd:t,onExit:()=>{try{L.close()}catch{}}});Y.set(L,Qe),L.send(`Connected to ${r==="127.0.0.1"?"localhost":r} shell. This is a line-oriented shell (no full-screen apps).\r
|
|
683
|
+
`)},message(L,Qe){Y.get(L)?.write(typeof Qe==="string"?Qe:new TextDecoder().decode(Qe))},close(L){Y.get(L)?.close(),Y.delete(L)}},async fetch(L,Qe){let Ze=Date.now(),Je=L.clone(),it=L.headers.get("x-request-id")?.slice(0,128)||crypto.randomUUID(),Tt=!["GET","HEAD","OPTIONS"].includes(L.method.toUpperCase()),H=new URL(L.url),kr=await(async()=>{let yt=Qe??Xs,It=z_(r,yt.port??o),nn=await JS(L,Ku(yt.requestIP(L)?.address??"unknown"));if(nn)return nn;let Dr=H.searchParams.get("env"),xe=q_(x,N,Dr),ur=K.get(xe);if(!ur)ur=await Ce(xe);try{if(!K_(L))return M({ok:!1,error:"Cross-site requests are not allowed."},403);let vs=/^\/api\/source\/webhooks\/([A-Za-z0-9_-]{16,200})$/.exec(H.pathname);if(vs&&L.method==="POST"){let T=await qd({sources:rt,controlPlane:E.store,endpointToken:vs[1],headers:L.headers,rawBody:new Uint8Array(await L.arrayBuffer())});return M({accepted:T.accepted,duplicate:T.duplicate,status:T.status,operationIds:T.operations.map((D)=>D.id),message:T.message},T.accepted?202:404)}let ma=/^\/auth\/oidc\/([a-z0-9-]+)\/(start|callback)$/.exec(H.pathname);if(ma&&L.method==="GET"){let[,T,D]=ma;if(!It)return new Response(null,{status:302,headers:{location:"/login?sso_error=configuration"}});if(D==="start")try{let U=await po(Fe,T,It,H.searchParams.get("return")??void 0,e.oidcFetch);return new Response(null,{status:302,headers:{location:U.authorizationUrl,"cache-control":"no-store"}})}catch(U){return E.store.appendEvent({organizationId:E.organization.id,type:"auth.oidc.start_failed",level:"warning",payload:{provider:T,reason:U instanceof Error?U.message.slice(0,200):"unknown"}}),new Response(null,{status:302,headers:{location:"/login?sso_error=start","cache-control":"no-store"}})}try{if(H.searchParams.has("error")){let X=Fe.getOidcProviderBySlug(T),Q=H.searchParams.get("state");if(X&&Q)Fe.consumeOidcTransaction(X.id,Q);throw Error("OIDC provider returned an authorization error")}let U=await go(Fe,{providerSlug:T,state:H.searchParams.get("state")??"",code:H.searchParams.get("code")??"",origin:It},e.oidcFetch),q=Nb(Fe,E,t,U.identity);Fe.recordLogin(q.identity.id);let G=$c(q.identity.id,L,yt.requestIP(L)?.address??"unknown","oidc");return at(),E.store.appendEvent({organizationId:E.organization.id,actorId:q.identity.actorId,type:"auth.login.succeeded",payload:{method:"oidc",provider:T,provisioned:q.provisioned}}),new Response(null,{status:302,headers:{location:U.returnPath,"cache-control":"no-store","set-cookie":Va(G.token,{secure:no,maxAgeMs:He})}})}catch(U){return E.store.appendEvent({organizationId:E.organization.id,type:"auth.oidc.callback_failed",level:"warning",payload:{provider:T,reason:U instanceof Error?U.message.slice(0,200):"unknown"}}),new Response(null,{status:302,headers:{location:"/login?sso_error=callback","cache-control":"no-store"}})}}if(H.pathname==="/api/invitations/accept"&&L.method==="POST"){let T=await me(L),D=String(T.token??"").trim(),U=String(T.username??"").trim(),q=String(T.password??"");if(!D||!Tu(U)||q.length<12)return M({ok:!1,error:"Invitation token, valid username, and a password of at least 12 characters are required."},422);let G=E.store.inspectInvitationToken(D);if(!G||G.state!=="pending")return M({ok:!1,error:G?`Invitation is ${G.state}`:"Invitation is invalid"},G?409:404);let X=Un(In(t),U),Q=Fe.getIdentityByUsername(U);if(X&&!Xn(q,Q?.passwordHash??X.passwordHash))return M({ok:!1,error:"That username already exists; enter its current password to continue."},401);let ie=Fe.getIdentityByEmail(G.email);if(ie&&ie.id!==Q?.id)return M({ok:!1,error:"This invitation belongs to an existing account. Sign in with that account or ask an owner to resend it."},409);let Ae=wc(t,{username:U,password:X?void 0:q,name:typeof T.name==="string"?T.name:void 0,email:G.email,sites:X?.sites??{}}),ke=S(E.store,Ae.user);try{let st=E.store.acceptInvitation(D,ke.id),De=Q?Fe.setVerifiedEmail(Q.id,st.invitation.email):Fe.createIdentity({actorId:ke.id,username:Ae.user.username,email:st.invitation.email,emailVerified:!0,passwordHash:Ae.user.passwordHash}),tt=$c(De.id,L,yt.requestIP(L)?.address??"unknown");return at(),M({ok:!0,user:Wa(Ae.user),organization:E.organization,membership:st.membership},200,{"set-cookie":Va(tt.token,{secure:no,maxAgeMs:He})})}catch(st){if(!X)Cg(t,Ae.user.username);let De=st instanceof Error?st.message:"Invitation could not be accepted.";return M({ok:!1,error:De},De.includes("expired")||De.includes("revoked")||De.includes("accepted")?409:404)}}if(H.pathname==="/api/auth/password-reset/request"&&L.method==="POST"){let T=await me(L),D=String(T.identifier??"").trim().toLowerCase(),U=yt.requestIP(L)?.address??"unknown",q=Hu.check(D,U),G=D.includes("@")?Fe.getIdentityByEmail(D):Fe.getIdentityByUsername(D);if(q.allowed)Hu.recordFailure(D,U);if(q.allowed&&G?.email&&G.emailVerifiedAt&&!G.disabledAt){Fe.revokeActionTokens(G.id,"password_reset");let X=Fe.createActionToken(G.id,"password_reset"),Q=`${H.origin}/reset-password?token=${encodeURIComponent(X.token)}`,ie=await Lr(h,{to:G.email,subject:"Reset your ts-cloud password",text:`Reset your ts-cloud password: ${Q}
|
|
684
|
+
|
|
685
|
+
This link expires in one hour and can be used once.`});E.store.appendEvent({organizationId:E.organization.id,actorId:G.actorId,type:"auth.password_reset.requested",payload:{delivered:ie}})}return M({ok:!0,message:"If that account has a verified email, a reset link has been sent."},202)}if(H.pathname==="/api/auth/password-reset/complete"&&L.method==="POST"){let T=await me(L),D=String(T.token??"").trim(),U=String(T.password??"");if(!D||U.length<12)return M({ok:!1,error:"A valid reset token and password of at least 12 characters are required."},422);try{let q=Fe.consumeActionToken(D,"password_reset"),G=gs(U),X=Fe.updatePassword(q.identityId,G);return Du(t,X.username,G),E.store.appendEvent({organizationId:E.organization.id,actorId:X.actorId,type:"auth.password_reset.completed"}),M({ok:!0,message:"Password changed. Sign in again."},200,{"set-cookie":_u({secure:no})})}catch(q){let G=q instanceof Error?q.message:"Password reset failed.";return M({ok:!1,error:G.replace("Action token","Reset link")},G.includes("consumed")||G.includes("expired")?409:404)}}if(H.pathname==="/api/login"&&L.method==="POST"){let T=await me(L),D=String(T.username??"").trim(),U=String(T.password??""),q=yt.requestIP(L)?.address??"unknown",G=Dc.check(D,q);if(!G.allowed)return M({ok:!1,error:`Too many failed attempts. Try again in ${Math.ceil((G.retryAfterSeconds??0)/60)} minute(s).`},429,{"retry-after":String(G.retryAfterSeconds??60)});let X=Un(In(t),D),Q=Fe.getIdentityByUsername(D),ie=Q?.passwordHash??X?.passwordHash??F_;if(!(Xn(U,ie)&&!!X&&!!Q&&!Q.disabledAt))return Dc.recordFailure(D,q),M({ok:!1,error:"Incorrect username or password."},401);if(jb(Fe,E,Q))return M({ok:!1,error:"Single sign-on is required for this account."},403);Dc.recordSuccess(D,q);let ke=Q,st=ke.requiresPasswordUpgrade||Gv(ke.passwordHash);if(st){let tt=gs(U);ke=Fe.rehashPassword(ke.id,tt),Du(t,ke.username,tt)}if(Fe.recordLogin(ke.id),Fe.getMfaFactor(ke.id)?.state==="active"){let tt=Fe.createMfaChallenge(ke.id,"login");return E.store.appendEvent({organizationId:E.organization.id,actorId:ke.actorId,type:"auth.mfa.challenge.created",payload:{purpose:"login"}}),M({ok:!0,mfaRequired:!0,challengeToken:tt.token,passwordUpgraded:st})}let De=$c(ke.id,L,q);return E.store.appendEvent({organizationId:E.organization.id,actorId:ke.actorId,type:"auth.login.succeeded",payload:{method:"local"}}),M({ok:!0,user:Wa(X),passwordUpgraded:st},200,{"set-cookie":Va(De.token,{secure:no,maxAgeMs:He})})}if(H.pathname==="/api/auth/mfa/complete"&&L.method==="POST"){let T=await me(L),D=String(T.challengeToken??"").trim(),U=String(T.code??"").trim(),q=yt.requestIP(L)?.address??"unknown",X=Fe.inspectMfaChallengeToken(D,"login")?.identityId??"unknown-mfa",Q=Oc.check(X,q);if(!Q.allowed)return M({ok:!1,error:"Too many MFA attempts. Try again later."},429,{"retry-after":String(Q.retryAfterSeconds??60)});try{let ie=Fe.completeMfaChallenge(D,U,"login");Oc.recordSuccess(X,q);let Ae=new Date().toISOString(),ke=Fe.createSession({identityId:ie.identity.id,userAgent:yf(L),networkHint:Ku(q),mfaAt:Ae,recentAuthAt:Ae});return E.store.appendEvent({organizationId:E.organization.id,actorId:ie.identity.actorId,type:"auth.mfa.succeeded",payload:{purpose:"login",method:ie.method}}),M({ok:!0},200,{"set-cookie":Va(ke.token,{secure:no,maxAgeMs:He})})}catch{return Oc.recordFailure(X,q),M({ok:!1,error:"Authenticator or recovery code is incorrect."},401)}}if(H.pathname==="/api/logout"&&L.method==="POST"){let T=gr.resolveSession(L);if(T)Fe.revokeSession(T.identityId,T.id);let D=_u({secure:no});if(L.headers.get("accept")?.includes("text/html"))return new Response(null,{status:302,headers:{location:"/login","set-cookie":D}});return M({ok:!0},200,{"set-cookie":D})}if(H.pathname==="/login"){let T=Fe.listOidcProviders(E.organization.id).map((D)=>({slug:D.slug,name:D.name}));return new Response(Mb(z?.mode==="serverless",T),{headers:{"content-type":"text/html; charset=utf-8"}})}if(H.pathname==="/forgot-password"||H.pathname==="/reset-password")return new Response(Bb(H.pathname==="/reset-password"?"reset":"request"),{headers:{"content-type":"text/html; charset=utf-8"}});if(H.pathname==="/accept-invitation")return $S(bt,H.pathname);let Se=gr.resolveUser(L);if(!Se){if(!H.pathname.startsWith("/api/")&&L.headers.get("accept")?.includes("text/html"))return new Response(null,{status:302,headers:{location:"/login"}});return M({ok:!1,error:"Sign in to continue."},401)}let jn=cr(Se,xe);if(H.pathname==="/api/me"){let T=E.store.getActorByExternalId("user",`dashboard:${Se.username.toLowerCase()}`),D=T?E.store.getMembershipForActor(E.organization.id,T.id):void 0,U=E.store.resolveAuthorizationTarget(E.organization.id,D?.scope??{type:"project",id:E.project.id})??E.store.resolveAuthorizationTarget(E.organization.id,{type:"project",id:E.project.id});return M({ok:!0,user:Wa(Se),authEnabled:Ne,organization:E.organization,membership:D,capabilities:Bt({membership:D,grants:D?E.store.listGrants(D.id):[],target:U})})}if(H.pathname.startsWith("/api/")){let T=await Ob(L,H.pathname),D=gr.check(L,H.pathname,Se,T);if(!D.ok)return M({ok:!1,error:D.error},D.status??403);if(Ne&&Q_.has(`${L.method.toUpperCase()} ${H.pathname}`)){let U=gr.resolveSession(L);if(!U||!Fe.isRecentlyAuthenticated(U))return M({ok:!1,error:"Recent authentication is required for this change.",stepUpRequired:!0},401)}}if(H.pathname==="/api/auth/security"&&L.method==="GET"){let T=Xe(Se),D=T.actor?Fe.getIdentityByActor(T.actor.id):void 0,U=gr.resolveSession(L);return M({identity:D?{username:D.username,email:D.email,emailVerifiedAt:D.emailVerifiedAt,requiresPasswordUpgrade:D.requiresPasswordUpgrade,lastLoginAt:D.lastLoginAt}:void 0,currentSessionId:U?.id,sessions:D?Fe.listSessions(D.id,{includeInactive:!0}):[],mfa:D?{factor:Fe.getMfaFactor(D.id),recoveryCodesRemaining:Fe.remainingRecoveryCodes(D.id)}:void 0})}if(H.pathname==="/api/auth/oidc/providers"&&L.method==="GET")return M({providers:Fe.listOidcProviders(E.organization.id,{includeDisabled:!0}),callbackOrigin:It,callbackPattern:It?`${It}/auth/oidc/{slug}/callback`:void 0,localOwnerRecovery:!0});if(H.pathname==="/api/auth/oidc/providers"&&L.method==="POST"){let T=await me(L),D=Xe(Se),U=D.actor?Fe.getIdentityByActor(D.actor.id):void 0,q=T.enforceSso===!0;if(q&&(!U?.emailVerifiedAt||Fe.remainingRecoveryCodes(U.id)<1))return M({ok:!1,error:"Verify the owner email and generate MFA recovery codes before enforcing SSO."},409);try{let G=Mu(T.defaultRole),X=Fe.upsertOidcProvider({id:typeof T.id==="string"?T.id:void 0,organizationId:E.organization.id,slug:String(T.slug??""),name:String(T.name??""),issuer:String(T.issuer??""),clientId:String(T.clientId??""),clientSecret:typeof T.clientSecret==="string"&&T.clientSecret?T.clientSecret:void 0,scopes:Array.isArray(T.scopes)?T.scopes.map(String):void 0,allowedDomains:Array.isArray(T.allowedDomains)?T.allowedDomains.map(String):[],defaultRole:G&&G!=="owner"?G:void 0,enabled:T.enabled!==!1,enforceSso:q});return E.store.appendEvent({organizationId:E.organization.id,actorId:D.actor?.id,type:"auth.oidc.provider.configured",payload:{providerId:X.id,issuer:X.issuer,enabled:X.enabled,enforceSso:X.enforceSso}}),M({ok:!0,provider:X})}catch(G){return M({ok:!1,error:G instanceof Error?G.message:"OIDC provider could not be saved."},422)}}if(H.pathname==="/api/auth/oidc/providers"&&L.method==="PATCH"){let T=await me(L),D=Fe.getOidcProvider(String(T.id??""));if(!D||D.organizationId!==E.organization.id)return M({ok:!1,error:"OIDC provider was not found."},404);let U=Xe(Se),q=Fe.setOidcProviderEnabled(D.id,T.enabled===!0);return E.store.appendEvent({organizationId:E.organization.id,actorId:U.actor?.id,type:q.enabled?"auth.oidc.provider.enabled":"auth.oidc.provider.disabled",payload:{providerId:D.id}}),M({ok:!0,provider:q})}if(H.pathname==="/api/automation"&&L.method==="GET"){let T=Ct.listServiceAccounts(E.organization.id,{includeDisabled:!0}).map((D)=>{let U=E.store.getMembershipForActor(E.organization.id,D.actorId),q=D.createdByActorId?E.store.getActor(D.createdByActorId):void 0,G=Ct.listTokens(D.id,{includeInactive:!0}).map((X)=>({...X,createdBy:X.createdByActorId?E.store.getActor(X.createdByActorId):void 0}));return{account:D,createdBy:q,membership:U,tokens:G}});return M({accounts:T,apiBaseUrl:`${H.origin}/api/v1`,openApiUrl:`${H.origin}/api/v1/openapi.json`,tokenEnvironmentVariable:"TS_CLOUD_API_TOKEN"})}if(H.pathname==="/api/automation/service-accounts"&&L.method==="POST"){let T=await me(L),D=Xe(Se),U=Mu(T.roleTemplate),q=Pc(T);if(!D.actor||!U||U==="owner"||!q)return M({ok:!1,error:"A non-owner role and valid organization scope are required."},422);try{let G=Ct.createServiceAccount({organizationId:E.organization.id,slug:String(T.slug??""),name:String(T.name??""),description:typeof T.description==="string"?T.description:void 0,roleTemplate:U,scope:q,createdByActorId:D.actor.id});return M({ok:!0,...G},201)}catch(G){return M({ok:!1,error:G instanceof Error?G.message:"Service account could not be created."},422)}}if(H.pathname==="/api/automation/service-accounts"&&L.method==="PATCH"){let T=await me(L),D=Xe(Se),U=Ct.getServiceAccount(String(T.id??""));if(!U||U.organizationId!==E.organization.id)return M({ok:!1,error:"Service account was not found."},404);return M({ok:!0,account:Ct.disableServiceAccount(U.id,D.actor?.id)})}if(H.pathname==="/api/automation/tokens"&&L.method==="POST"){let T=await me(L),D=Xe(Se),U=Ct.getServiceAccount(String(T.serviceAccountId??"")),q=Pc(T);if(!D.actor||!U||U.organizationId!==E.organization.id||!q||!Array.isArray(T.capabilities))return M({ok:!1,error:"Service account, explicit capabilities, and valid scope are required."},422);try{let G=Ct.createToken({serviceAccountId:U.id,name:String(T.name??"API token"),capabilities:T.capabilities.map(String),scope:q,expiresAt:typeof T.expiresAt==="string"?T.expiresAt:void 0,createdByActorId:D.actor.id});return M({ok:!0,token:G.token,secret:G.secret,revealOnce:!0},201)}catch(G){return M({ok:!1,error:G instanceof Error?G.message:"API token could not be created."},422)}}if(H.pathname==="/api/automation/tokens/rotate"&&L.method==="POST"){let T=await me(L),D=Xe(Se),U=Ct.getToken(String(T.id??"")),q=U?Ct.getServiceAccount(U.serviceAccountId):void 0;if(!D.actor||!U||q?.organizationId!==E.organization.id)return M({ok:!1,error:"API token was not found."},404);try{let G=Ct.rotateToken(U.id,D.actor.id);return M({ok:!0,token:G.token,secret:G.secret,revealOnce:!0,overlap:!0},201)}catch(G){return M({ok:!1,error:G instanceof Error?G.message:"API token could not be rotated."},409)}}if(H.pathname==="/api/automation/tokens"&&L.method==="DELETE"){let T=await me(L),D=Xe(Se),U=Ct.getToken(String(T.id??"")),q=U?Ct.getServiceAccount(U.serviceAccountId):void 0;if(!U||q?.organizationId!==E.organization.id)return M({ok:!1,error:"API token was not found."},404);return M({ok:!0,token:Ct.revokeToken(U.id,D.actor?.id)})}if(H.pathname==="/api/onboarding"&&L.method==="GET"){let T=rt.listConnections(E.organization.id);return M({drafts:ln.list(E.project.id),artifacts:Ys.list(E.project.id),registries:ei.list(E.organization.id),sourceConnections:T,repositories:T.flatMap((D)=>rt.listRepositories(D.id)),project:{id:E.project.id,slug:E.project.slug,name:E.project.name},environments:[...E.environments.values()].map((D)=>({id:D.id,slug:D.slug,name:D.name,kind:D.kind,region:D.region})),resources:E.store.listResources(E.project.id).filter((D)=>D.kind==="application").map((D)=>({id:D.id,slug:D.slug,name:D.name,environmentId:D.environmentId}))})}if(H.pathname==="/api/onboarding/detect"&&L.method==="POST"){let T=await me(L);try{if(T.source?.kind==="git"){let q=rt.getConnection(String(T.source.connectionId??"")),G=rt.getRepository(String(T.source.repositoryId??""));if(!q||q.organizationId!==E.organization.id||!G||G.connectionId!==q.id)throw Error("Authorized Git repository was not found");let X=typeof T.source.deployKeyId==="string"?rt.getDeployKey(T.source.deployKeyId):void 0;if(X&&X.connectionId!==q.id)throw Error("Deploy key does not belong to this connection");let Q=Gg(rr(Lu(),"ts-cloud-onboarding-git-"));try{let ie=await yo({remote:G.cloneUrl,destination:rr(Q,"checkout"),ref:String(T.source.ref??G.defaultBranch),sparsePaths:Array.isArray(T.source.sparsePaths)?T.source.sparsePaths.map(String):void 0,binding:{id:"detection",projectId:E.project.id,connectionId:q.id,repositoryId:G.id,repositoryFullName:G.fullName,defaultBranch:G.defaultBranch,monorepoRoot:String(T.source.monorepoRoot??"."),includePaths:[],excludePaths:[],submodules:T.source.submodules===!0,cloneDepth:1,deployKeyId:X?.id,autoDeploy:!1,pullRequestPreviews:!1,status:"active",version:1,createdAt:new Date().toISOString(),updatedAt:new Date().toISOString()}},{credential:rt.getCredential(q.id),deployKey:X?{...X,privateKey:rt.getDeployPrivateKey(X.id)}:void 0,timeoutMs:120000});return M({ok:!0,commitSha:ie.commitSha,candidates:va(tp(ie.directory))})}finally{Gs(Q,{recursive:!0,force:!0})}}if(Array.isArray(T.files)){if(T.files.length>1e4)throw Error("Detection file-count limit exceeded");let q=T.files.map((G)=>({path:String(G.path??""),size:Number(G.size??0),content:typeof G.content==="string"?G.content.slice(0,262144):void 0}));return M({ok:!0,candidates:va(q)})}let D=__(t,String(T.root??".")),U=P_(t,D);if(U===".."||U.startsWith(`..${T_}`))throw Error("Detection root must stay inside the project");return M({ok:!0,candidates:va(tp(D))})}catch(D){return M({ok:!1,error:D instanceof Error?D.message:"Application detection failed."},422)}}if(H.pathname==="/api/onboarding/plan"&&L.method==="POST"){let T=await me(L);try{return M({ok:!0,plan:Vn(T.draft,Array.isArray(T.suppliedSecretNames)?T.suppliedSecretNames.map(String):[])})}catch(D){return M({ok:!1,error:D instanceof Error?D.message:"Application plan could not be generated."},422)}}if(H.pathname==="/api/onboarding/drafts"&&L.method==="POST"){let T=await me(L),D=Xe(Se);try{return M({ok:!0,draft:ln.create({organizationId:E.organization.id,projectId:E.project.id,name:String(T.name??T.draft?.name??"Application draft"),draft:{...T.draft,projectId:E.project.id},step:T.step,suppliedSecretNames:Array.isArray(T.suppliedSecretNames)?T.suppliedSecretNames.map(String):[],actorId:D.actor?.id})},201)}catch(U){return M({ok:!1,error:U instanceof Error?U.message:"Application draft could not be saved."},422)}}if(H.pathname==="/api/onboarding/drafts"&&L.method==="PATCH"){let T=await me(L),D=Xe(Se),U=ln.get(String(T.id??""));if(!U||U.projectId!==E.project.id)return M({ok:!1,error:"Application draft was not found."},404);try{return M({ok:!0,draft:ln.update(U.id,Number(T.version),{draft:{...T.draft,projectId:E.project.id},step:T.step,suppliedSecretNames:Array.isArray(T.suppliedSecretNames)?T.suppliedSecretNames.map(String):void 0,actorId:D.actor?.id})})}catch(q){return M({ok:!1,error:q instanceof Error?q.message:"Application draft could not be updated."},409)}}if(H.pathname==="/api/onboarding/artifacts"&&L.method==="POST"){let T=Xe(Se);if(Number(L.headers.get("content-length")??0)>104857600)return M({ok:!1,error:"Artifact exceeds the 100 MB upload limit."},413);try{let U=new Uint8Array(await L.arrayBuffer());return M({ok:!0,artifact:Ys.create({organizationId:E.organization.id,projectId:E.project.id,filename:L.headers.get("x-artifact-filename")??"application.zip",bytes:U,actorId:T.actor?.id})},201)}catch(U){return M({ok:!1,error:U instanceof Error?U.message:"Artifact could not be inspected."},422)}}if(H.pathname==="/api/onboarding/registries"&&L.method==="POST"){let T=await me(L),D=Xe(Se);try{return M({ok:!0,registry:ei.create({organizationId:E.organization.id,provider:T.provider,name:String(T.name??"Container registry"),host:String(T.host??""),credential:T.token||T.password?{username:typeof T.username==="string"?T.username:void 0,password:typeof T.password==="string"?T.password:void 0,token:typeof T.token==="string"?T.token:void 0}:void 0,credentialExpiresAt:typeof T.credentialExpiresAt==="string"?T.credentialExpiresAt:void 0,actorId:D.actor?.id})},201)}catch(U){return M({ok:!1,error:U instanceof Error?U.message:"Registry connection could not be created."},422)}}if(H.pathname==="/api/onboarding/registries"&&L.method==="PATCH"){let T=await me(L),D=Xe(Se),U=ei.get(String(T.id??""));if(!U||U.organizationId!==E.organization.id)return M({ok:!1,error:"Registry connection was not found."},404);try{if(T.action==="test")return M({ok:!0,registry:await ei.test(U.id,{image:typeof T.image==="string"?T.image:void 0})});if(T.action==="rotate")return M({ok:!0,registry:ei.rotate(U.id,{username:typeof T.username==="string"?T.username:void 0,password:typeof T.password==="string"?T.password:void 0,token:typeof T.token==="string"?T.token:void 0},{expiresAt:typeof T.expiresAt==="string"?T.expiresAt:void 0,actorId:D.actor?.id})});return M({ok:!1,error:"Registry action must be test or rotate."},422)}catch(q){return M({ok:!1,error:q instanceof Error?q.message:"Registry connection could not be updated."},422)}}if(H.pathname==="/api/onboarding/registries"&&L.method==="DELETE"){let T=await me(L),D=Xe(Se),U=ei.get(String(T.id??""));if(!U||U.organizationId!==E.organization.id)return M({ok:!1,error:"Registry connection was not found."},404);let q=ln.list(E.project.id).filter((G)=>G.input.source.kind==="image"&&G.input.source.registryConnectionId===U.id||G.input.build.kind==="prebuilt_image"&&G.input.build.registryConnectionId===U.id);if(T.preview===!0)return M({ok:!0,preview:!0,affectedDrafts:q});return M({ok:!0,registry:ei.disconnect(U.id,D.actor?.id),affectedDrafts:q})}if(H.pathname==="/api/onboarding/apply"&&L.method==="POST"){let T=await me(L),D=Xe(Se);try{let U=Fc({controlPlane:E.store,drafts:ln,draftId:String(T.id??""),expectedVersion:Number(T.version),confirmEnvironment:String(T.confirmEnvironment??""),actorId:D.actor?.id});return M({ok:!0,resource:U.resource,operation:U.operation,plan:U.plan},202)}catch(U){return M({ok:!1,error:U instanceof Error?U.message:"Application plan could not be applied."},422)}}if(H.pathname==="/api/sources"&&L.method==="GET"){let T=rt.listConnections(E.organization.id);return M({connections:T,bindings:rt.listBindings({projectId:E.project.id}),repositories:T.flatMap((D)=>rt.listRepositories(D.id)),deployKeys:T.flatMap((D)=>rt.listDeployKeys(D.id)),webhooks:T.flatMap((D)=>rt.listWebhooks(D.id)).map((D)=>({...D,deliveries:rt.listDeliveries(D.id,20)})),project:{id:E.project.id,slug:E.project.slug},environment:Z(E,String(xe)),resources:E.store.listResources(E.project.id,Z(E,String(xe)).environmentId).filter((D)=>D.kind==="application").map((D)=>({id:D.id,slug:D.slug,name:D.name}))})}if(H.pathname==="/api/sources/connections"&&L.method==="POST"){let T=await me(L),D=Xe(Se),U=String(T.provider??"");if(!["github","gitlab","bitbucket","gitea","generic_https","generic_ssh"].includes(U))return M({ok:!1,error:"A supported Git provider is required."},422);try{let q=U==="github"&&(T.privateKey||T.appId)?{appId:typeof T.appId==="string"?T.appId:void 0,installationId:typeof T.installationId==="string"?T.installationId:void 0,privateKey:typeof T.privateKey==="string"?T.privateKey:void 0}:U!=="generic_ssh"&&T.token?{token:typeof T.token==="string"?T.token:void 0,username:typeof T.username==="string"?T.username:void 0}:void 0,G={github:"https://github.com",gitlab:"https://gitlab.com",bitbucket:"https://bitbucket.org",gitea:"https://gitea.com"},X,Q,ie;if(E.store.database.transaction(()=>{if(X=rt.createConnection({organizationId:E.organization.id,provider:U,name:String(T.name??U),host:String(T.host??G[U]??""),owner:typeof T.owner==="string"?T.owner:void 0,authKind:T.authKind,credential:q,grantedScopes:Array.isArray(T.grantedScopes)?T.grantedScopes.map(String):[],credentialExpiresAt:typeof T.credentialExpiresAt==="string"?T.credentialExpiresAt:void 0,createdByActorId:D.actor?.id}),typeof T.repositoryUrl==="string"&&typeof T.repositoryFullName==="string")Q=rt.upsertRepository({connectionId:X.id,providerRepositoryId:`manual:${T.repositoryFullName}`,fullName:T.repositoryFullName,cloneUrl:T.repositoryUrl,defaultBranch:String(T.defaultBranch??"main"),visibility:"unknown",archived:!1,metadata:{source:"manual"}});if(U==="generic_ssh")ie=rt.createDeployKey({connectionId:X.id,name:String(T.deployKeyName??"Repository deploy key"),publicKey:String(T.publicKey??""),privateKey:String(T.deployPrivateKey??""),host:String(T.sshHost??""),hostKey:String(T.hostKey??""),actorId:D.actor?.id})})(),T.test===!0)X=await Io(rt,X.id,Q?.id);return M({ok:!0,connection:X,repository:Q,deployKey:ie},201)}catch(q){return M({ok:!1,error:q instanceof Error?q.message:"Source connection could not be created."},422)}}if(H.pathname==="/api/sources/connections"&&L.method==="PATCH"){let T=await me(L),D=Xe(Se),U=rt.getConnection(String(T.id??""));if(!U||U.organizationId!==E.organization.id)return M({ok:!1,error:"Source connection was not found."},404);try{if(T.action==="test")return M({ok:!0,connection:await Io(rt,U.id,typeof T.repositoryId==="string"?T.repositoryId:void 0)});if(T.action==="rotate")return M({ok:!0,connection:rt.rotateCredential(U.id,{token:typeof T.token==="string"?T.token:void 0,username:typeof T.username==="string"?T.username:void 0,appId:typeof T.appId==="string"?T.appId:void 0,installationId:typeof T.installationId==="string"?T.installationId:void 0,privateKey:typeof T.privateKey==="string"?T.privateKey:void 0},{expiresAt:typeof T.expiresAt==="string"?T.expiresAt:void 0,actorId:D.actor?.id})});return M({ok:!1,error:"Connection action must be test or rotate."},422)}catch(q){return M({ok:!1,error:q instanceof Error?q.message:"Source connection could not be updated."},422)}}if(H.pathname==="/api/sources/connections"&&L.method==="DELETE"){let T=await me(L),D=Xe(Se),U=rt.getConnection(String(T.id??""));if(!U||U.organizationId!==E.organization.id)return M({ok:!1,error:"Source connection was not found."},404);let q=rt.listBindings({connectionId:U.id,status:"active"});if(T.preview===!0)return M({ok:!0,preview:!0,affectedBindings:q});return M({ok:!0,...rt.disconnectConnection(U.id,D.actor?.id)})}if(H.pathname==="/api/sources/deploy-keys"&&L.method==="POST"){let T=await me(L),D=Xe(Se),U=rt.getConnection(String(T.connectionId??""));if(!U||U.organizationId!==E.organization.id)return M({ok:!1,error:"Source connection was not found."},404);try{return M({ok:!0,deployKey:rt.createDeployKey({connectionId:U.id,name:String(T.name??"Repository deploy key"),publicKey:String(T.publicKey??""),privateKey:String(T.privateKey??""),host:String(T.host??""),hostKey:String(T.hostKey??""),actorId:D.actor?.id})},201)}catch(q){return M({ok:!1,error:q instanceof Error?q.message:"Deploy key could not be stored."},422)}}if(H.pathname==="/api/sources/deploy-keys"&&L.method==="DELETE"){let T=await me(L),D=Xe(Se),U=rt.getDeployKey(String(T.id??"")),q=U?rt.getConnection(U.connectionId):void 0;if(!U||!q||q.organizationId!==E.organization.id)return M({ok:!1,error:"Deploy key was not found."},404);let G=rt.listBindings({connectionId:q.id}).filter((X)=>X.deployKeyId===U.id);if(T.preview===!0)return M({ok:!0,preview:!0,affectedBindings:G});return M({ok:!0,...rt.revokeDeployKey(U.id,D.actor?.id)})}if(H.pathname==="/api/sources/repositories/sync"&&L.method==="POST"){let T=await me(L),D=rt.getConnection(String(T.connectionId??""));if(!D||D.organizationId!==E.organization.id)return M({ok:!1,error:"Source connection was not found."},404);try{return M({ok:!0,repositories:await vi(rt,String(T.connectionId??""),{search:typeof T.search==="string"?T.search:void 0})})}catch(U){return M({ok:!1,error:U instanceof Error?U.message:"Repositories could not be synchronized."},422)}}if(H.pathname==="/api/sources/references"&&L.method==="GET"){let T=rt.getConnection(String(H.searchParams.get("connectionId")??""));if(!T||T.organizationId!==E.organization.id)return M({ok:!1,error:"Source connection was not found."},404);try{return M(await Ti(rt,{connectionId:String(H.searchParams.get("connectionId")??""),repository:String(H.searchParams.get("repository")??""),repositoryId:H.searchParams.get("repositoryId")??void 0,deployKeyId:H.searchParams.get("deployKeyId")??void 0,type:H.searchParams.get("type")==="tags"?"tags":"branches",cursor:H.searchParams.get("cursor")??void 0}))}catch(D){return M({ok:!1,error:D instanceof Error?D.message:"References could not be listed."},422)}}if(H.pathname==="/api/sources/bindings"&&L.method==="POST"){let T=await me(L),D=Xe(Se),U=Z(E,String(xe)),q=rt.getConnection(String(T.connectionId??""));if(!q||q.organizationId!==E.organization.id)return M({ok:!1,error:"Source connection was not found."},404);try{return M({ok:!0,binding:rt.createBinding({projectId:E.project.id,environmentId:U.environmentId,resourceId:typeof T.resourceId==="string"?T.resourceId:void 0,connectionId:String(T.connectionId??""),repositoryId:typeof T.repositoryId==="string"?T.repositoryId:void 0,repositoryFullName:String(T.repositoryFullName??""),defaultBranch:String(T.defaultBranch??"main"),branchRule:typeof T.branchRule==="string"?T.branchRule:void 0,tagRule:typeof T.tagRule==="string"?T.tagRule:void 0,monorepoRoot:String(T.monorepoRoot??"."),includePaths:Array.isArray(T.includePaths)?T.includePaths.map(String):[],excludePaths:Array.isArray(T.excludePaths)?T.excludePaths.map(String):[],submodules:T.submodules===!0,cloneDepth:T.cloneDepth?Number(T.cloneDepth):void 0,deployKeyId:typeof T.deployKeyId==="string"&&T.deployKeyId?T.deployKeyId:void 0,autoDeploy:T.autoDeploy!==!1,pullRequestPreviews:T.pullRequestPreviews!==!1,actorId:D.actor?.id})},201)}catch(G){return M({ok:!1,error:G instanceof Error?G.message:"Source binding could not be created."},422)}}if(H.pathname==="/api/sources/bindings"&&L.method==="PATCH"){let T=await me(L),D=Xe(Se),U=rt.getBinding(String(T.id??""));if(!U||U.projectId!==E.project.id)return M({ok:!1,error:"Source binding was not found."},404);try{return M({ok:!0,binding:rt.updateBinding(U.id,Number(T.version),{defaultBranch:T.defaultBranch,branchRule:T.branchRule,tagRule:T.tagRule,monorepoRoot:T.monorepoRoot,includePaths:T.includePaths,excludePaths:T.excludePaths,submodules:T.submodules,cloneDepth:T.cloneDepth,deployKeyId:T.deployKeyId,autoDeploy:T.autoDeploy,pullRequestPreviews:T.pullRequestPreviews,status:T.status,disabledReason:T.disabledReason,actorId:D.actor?.id})})}catch(q){return M({ok:!1,error:q instanceof Error?q.message:"Source binding could not be updated."},409)}}if(H.pathname==="/api/sources/webhooks"&&L.method==="POST"){let T=await me(L),D=rt.getConnection(String(T.connectionId??""));if(!D||D.organizationId!==E.organization.id)return M({ok:!1,error:"Source connection was not found."},404);try{let U=rt.createWebhook({connectionId:String(T.connectionId??""),repositoryId:typeof T.repositoryId==="string"?T.repositoryId:void 0,repositoryFullName:String(T.repositoryFullName??""),events:Array.isArray(T.events)?T.events.map(String):void 0,secret:typeof T.secret==="string"?T.secret:void 0}),q=process.env.TS_CLOUD_WEBHOOK_BASE_URL?.trim()||It,G=q?Vt(q,U.webhook):void 0,X=T.reconcile!==!1&&q?await Mr(rt,U.webhook.id,q):U.webhook;return M({ok:!0,webhook:X,endpoint:G,endpointRevealOnce:!0},201)}catch(U){return M({ok:!1,error:U instanceof Error?U.message:"Source webhook could not be created."},422)}}if(H.pathname==="/api/sources/webhooks"&&L.method==="PATCH"){let T=await me(L),D=process.env.TS_CLOUD_WEBHOOK_BASE_URL?.trim()||It,U=rt.getWebhook(String(T.id??"")),q=U?rt.getConnection(U.connectionId):void 0;if(!U||!q||q.organizationId!==E.organization.id)return M({ok:!1,error:"Source webhook was not found."},404);if(!D)return M({ok:!1,error:"Set TS_CLOUD_WEBHOOK_BASE_URL to reconcile provider webhooks."},422);try{return M({ok:!0,webhook:await Mr(rt,String(T.id??""),D)})}catch(G){return M({ok:!1,error:G instanceof Error?G.message:"Source webhook could not be reconciled."},422)}}if(H.pathname==="/api/sources/webhooks"&&L.method==="DELETE"){let T=await me(L),D=rt.getWebhook(String(T.id??"")),U=D?rt.getConnection(D.connectionId):void 0;if(!D||!U||U.organizationId!==E.organization.id)return M({ok:!1,error:"Source webhook was not found."},404);try{return M({ok:!0,webhook:await Vd(rt,String(T.id??""))})}catch(q){return M({ok:!1,error:q instanceof Error?q.message:"Source webhook could not be removed."},422)}}if(H.pathname==="/api/security/posture"&&L.method==="GET"){let T=Z(E,String(xe)),D=St.listFindings({...T,limit:500});return M({scope:T,summary:St.summary(T.organizationId,T.projectId,T.environmentId),findings:D,scans:St.listScanRuns({...T,limit:100}),policies:St.listPolicies(T.organizationId).filter((U)=>!U.environmentId||U.environmentId===T.environmentId),waivers:D.flatMap((U)=>St.listWaivers(U.id)),decisions:St.listDecisions(T.environmentId,50),actors:E.store.listMemberships(E.organization.id).map((U)=>E.store.getActor(U.actorId)).filter(Boolean)})}if(H.pathname==="/api/security/export"&&L.method==="GET")return M(St.exportPosture(E.organization.id));if(H.pathname==="/api/security/scan"&&L.method==="POST"){let T=Z(E,String(xe)),D=await new lt(St).run(new Qr,{...T,artifactRoot:t}),U=Ri(St,T,ur??{});return M({ok:!0,source:D,host:U,summary:St.summary(T.organizationId,T.projectId,T.environmentId)})}if(H.pathname==="/api/security/review"&&L.method==="POST")try{let T=Z(E,String(xe));return M({ok:!0,...Di(St,{scope:T,desiredConfigHash:E.project.desiredConfigHash})})}catch(T){return M({ok:!1,error:T instanceof Error?T.message:"Security change review failed."},409)}if(H.pathname==="/api/security/policies"&&L.method==="POST"){let T=await me(L),D=Xe(Se);if(!D.actor)return M({ok:!1,error:"An organization actor is required."},403);try{let U=St.createPolicy({organizationId:E.organization.id,environmentId:T.environmentId===null?void 0:String(T.environmentId??Z(E,String(xe)).environmentId),name:String(T.name??""),scannerFailMode:T.scannerFailMode==="open"?"open":"closed",requiredScanners:Array.isArray(T.requiredScanners)?T.requiredScanners.map(String):[],rules:Array.isArray(T.rules)?T.rules.map((q)=>({minimumSeverity:String(q.minimumSeverity),action:String(q.action),scannerId:q.scannerId?String(q.scannerId):void 0})):[],actorId:D.actor.id});return M({ok:!0,policy:U},201)}catch(U){return M({ok:!1,error:U instanceof Error?U.message:"Security policy could not be created."},422)}}if(H.pathname==="/api/security/policies"&&L.method==="PATCH"){let T=await me(L),D=Xe(Se),U=St.getPolicy(String(T.id??""));if(!D.actor||!U||U.organizationId!==E.organization.id)return M({ok:!1,error:"Security policy was not found."},404);try{return M({ok:!0,policy:St.updatePolicy(U.id,Number(T.version),{name:typeof T.name==="string"?T.name:void 0,scannerFailMode:T.scannerFailMode==="open"||T.scannerFailMode==="closed"?T.scannerFailMode:void 0,requiredScanners:Array.isArray(T.requiredScanners)?T.requiredScanners.map(String):void 0,rules:Array.isArray(T.rules)?T.rules.map((q)=>({minimumSeverity:String(q.minimumSeverity),action:String(q.action),scannerId:q.scannerId?String(q.scannerId):void 0})):void 0,enabled:typeof T.enabled==="boolean"?T.enabled:void 0,actorId:D.actor.id})})}catch(q){return M({ok:!1,error:q instanceof Error?q.message:"Security policy could not be updated."},409)}}if(H.pathname==="/api/security/waivers"&&L.method==="POST"){let T=await me(L),D=Xe(Se);if(!D.actor)return M({ok:!1,error:"An organization actor is required."},403);try{return M({ok:!0,waiver:St.createWaiver({findingId:String(T.findingId??""),policyId:typeof T.policyId==="string"?T.policyId:void 0,reason:String(T.reason??""),referenceUrl:typeof T.referenceUrl==="string"?T.referenceUrl:void 0,expiresAt:String(T.expiresAt??""),actorId:D.actor.id})},201)}catch(U){return M({ok:!1,error:U instanceof Error?U.message:"Security waiver could not be created."},422)}}if(H.pathname==="/api/security/waivers"&&L.method==="DELETE"){let T=await me(L),D=Xe(Se);if(!D.actor)return M({ok:!1,error:"An organization actor is required."},403);try{return M({ok:!0,waiver:St.revokeWaiver(String(T.id??""),D.actor.id)})}catch(U){return M({ok:!1,error:U instanceof Error?U.message:"Security waiver could not be revoked."},404)}}if(H.pathname==="/api/security/findings"&&L.method==="PATCH"){let T=await me(L),D=Xe(Se),U=St.getFinding(String(T.id??""));if(!D.actor||!U||U.organizationId!==E.organization.id)return M({ok:!1,error:"Security finding was not found."},404);try{let q=T.action==="acknowledge"?St.acknowledgeFinding(U.id,D.actor.id):St.assignFinding(U.id,typeof T.ownerActorId==="string"&&T.ownerActorId?T.ownerActorId:void 0,D.actor.id);return M({ok:!0,finding:q})}catch(q){return M({ok:!1,error:q instanceof Error?q.message:"Security finding could not be updated."},422)}}if(H.pathname==="/api/security/comments"&&L.method==="POST"){let T=await me(L),D=Xe(Se);if(!D.actor)return M({ok:!1,error:"An organization actor is required."},403);try{return M({ok:!0,comment:St.addComment({findingId:String(T.findingId??""),actorId:D.actor.id,body:String(T.body??""),referenceUrl:typeof T.referenceUrl==="string"?T.referenceUrl:void 0})},201)}catch(U){return M({ok:!1,error:U instanceof Error?U.message:"Comment could not be saved."},422)}}if(H.pathname==="/api/auth/sessions"&&L.method==="GET"){let T=gr.resolveSession(L);return M({currentSessionId:T?.id,sessions:T?Fe.listSessions(T.identityId,{includeInactive:!0}):[]})}if(H.pathname==="/api/auth/sessions"&&L.method==="DELETE"){let T=await me(L),D=gr.resolveSession(L);if(!D)return M({ok:!1,error:"Sign in again to manage sessions."},401);let U=String(T.id??""),q=Fe.revokeSession(D.identityId,U),G=U===D.id&&q?{"set-cookie":_u({secure:no})}:void 0;return M({ok:q,signedOut:U===D.id&&q},q?200:404,G)}if(H.pathname==="/api/auth/sessions/revoke-others"&&L.method==="POST"){let T=gr.resolveSession(L);if(!T)return M({ok:!1,error:"Sign in again to manage sessions."},401);return M({ok:!0,revoked:Fe.revokeOtherSessions(T.identityId,T.id)})}if(H.pathname==="/api/auth/password/change"&&L.method==="POST"){let T=await me(L),D=String(T.currentPassword??""),U=String(T.password??""),q=Xe(Se),G=q.actor?Fe.getIdentityByActor(q.actor.id):void 0;if(!G||!Xn(D,G.passwordHash))return M({ok:!1,error:"Current password is incorrect."},401);if(U.length<12)return M({ok:!1,error:"New password must be at least 12 characters."},422);if(Xn(U,G.passwordHash))return M({ok:!1,error:"Choose a different password."},409);let X=gs(U),Q=Fe.updatePassword(G.id,X);Du(t,Q.username,X);let ie=$c(Q.id,L,yt.requestIP(L)?.address??"unknown");return E.store.appendEvent({organizationId:E.organization.id,actorId:Q.actorId,type:"auth.password.changed"}),M({ok:!0,message:"Password changed and other sessions were signed out."},200,{"set-cookie":Va(ie.token,{secure:no,maxAgeMs:He})})}if(H.pathname==="/api/auth/mfa/enroll"&&L.method==="POST"){let T=await me(L),D=gr.resolveSession(L),U=Xe(Se),q=U.actor?Fe.getIdentityByActor(U.actor.id):void 0;if(!D||!q)return M({ok:!1,error:"Sign in again before enabling MFA."},401);if(!Xn(String(T.password??""),q.passwordHash))return M({ok:!1,error:"Current password is incorrect."},401);try{let G=Fe.beginTotpEnrollment(q.id,{issuer:E.organization.name});return E.store.appendEvent({organizationId:E.organization.id,actorId:q.actorId,type:"auth.mfa.enrollment.started"}),M({factor:G.factor,secret:G.secret,uri:G.uri})}catch(G){return M({ok:!1,error:G instanceof Error?G.message:"MFA enrollment failed."},409)}}if(H.pathname==="/api/auth/mfa/verify"&&L.method==="POST"){let T=await me(L),D=gr.resolveSession(L),U=Xe(Se),q=U.actor?Fe.getIdentityByActor(U.actor.id):void 0;if(!D||!q)return M({ok:!1,error:"Sign in again before enabling MFA."},401);try{let G=Fe.verifyTotpEnrollment(q.id,String(T.code??""));return Fe.markSessionStepUp(D.id,!0),E.store.appendEvent({organizationId:E.organization.id,actorId:q.actorId,type:"auth.mfa.enabled",payload:{recoveryCodeCount:G.recoveryCodes.length}}),M(G)}catch(G){return M({ok:!1,error:G instanceof Error?G.message:"MFA enrollment failed."},422)}}if(H.pathname==="/api/auth/mfa"&&L.method==="DELETE"){let T=await me(L),D=gr.resolveSession(L),U=Xe(Se),q=U.actor?Fe.getIdentityByActor(U.actor.id):void 0;if(!D||!q||!Xn(String(T.password??""),q.passwordHash))return M({ok:!1,error:"Current password is incorrect."},401);let G=Fe.verifyMfaCode(q.id,String(T.code??""));if(!G.valid||!G.method)return M({ok:!1,error:"Authenticator or recovery code is incorrect."},401);return Fe.disableMfa(q.id),Fe.revokeOtherSessions(q.id,D.id),Fe.markSessionStepUp(D.id),E.store.appendEvent({organizationId:E.organization.id,actorId:q.actorId,type:"auth.mfa.disabled",payload:{method:G.method}}),M({ok:!0})}if(H.pathname==="/api/auth/step-up"&&L.method==="POST"){let T=await me(L),D=gr.resolveSession(L),U=Xe(Se),q=U.actor?Fe.getIdentityByActor(U.actor.id):void 0;if(!D||!q||!Xn(String(T.password??""),q.passwordHash))return M({ok:!1,error:"Current password is incorrect."},401);let G=Fe.getMfaFactor(q.id);if(G?.state==="active"){if(!Fe.verifyMfaCode(q.id,String(T.code??"")).valid)return M({ok:!1,error:"Authenticator or recovery code is incorrect."},401)}let X=Fe.markSessionStepUp(D.id,G?.state==="active");return E.store.appendEvent({organizationId:E.organization.id,actorId:q.actorId,type:"auth.step_up.succeeded",payload:{mfa:G?.state==="active"}}),M({ok:!0,recentAuthAt:X.recentAuthAt})}if(H.pathname==="/api/terminal"){if(!be)return M({ok:!1,error:"The web terminal is disabled."},403);if(yt.upgrade(L))return;return Fu("WebSocket upgrade failed",400)}if(H.pathname==="/api/health"){let T=E.store.health();return M({ok:!0,cwd:t,environment:xe,environments:x,uiRoot:await dr(Se,xe),liveData:ge.size>0,terminal:be,localPackage:import.meta.url.includes("/Code/Libraries/ts-cloud/"),controlPlane:{schemaVersion:T.schemaVersion,supportedSchemaVersion:T.supportedSchemaVersion,integrity:T.integrity,pendingRetryableOperations:T.pendingRetryableOperations}})}if(H.pathname==="/api/control-plane/operations"){let T=H.searchParams.get("state"),D=T&&["queued","running","succeeded","failed","cancelled","timed_out"].includes(T)?T:void 0;return M({operations:E.store.listOperations({projectId:E.project.id,state:D,kind:H.searchParams.get("kind")??void 0,limit:Number(H.searchParams.get("limit"))||100})})}if(H.pathname==="/api/queue"&&L.method==="GET"){let T=H.searchParams.get("state"),D=T&&["queued","running","succeeded","failed","cancelled","timed_out"].includes(T)?T:void 0,U=nr.list({projectId:E.project.id,state:D,limit:Number(H.searchParams.get("limit"))||250}).filter((q)=>Zs(Se,q.operation.id,"deployments:read")).map((q)=>{let G=q.operation.resourceId?E.store.getResource(q.operation.resourceId):void 0,X=q.operation.environmentId?E.store.listEnvironments(E.project.id).find((Ae)=>Ae.id===q.operation.environmentId):void 0,Q=q.operation.actorId?E.store.getActor(q.operation.actorId):void 0,ie=q.operation.input;return{...q,target:G?{id:G.id,name:G.name,slug:G.slug,kind:G.kind}:X?{id:X.id,name:X.name,slug:X.slug,kind:"environment"}:{id:E.project.id,name:E.project.name,slug:E.project.slug,kind:"project"},actor:Q?{id:Q.id,name:Q.displayName,kind:Q.kind}:void 0,commit:ie.revision??ie.source?.commitSha??null}});return M({operations:U,concurrency:nr.limits})}if(H.pathname==="/api/queue/logs"&&L.method==="GET"){let T=String(H.searchParams.get("id")??"");if(!Zs(Se,T,"deployments:read"))return M({ok:!1,error:"Operation was not found in this scope."},404);let D=nr.logs(T,{after:Number(H.searchParams.get("after"))||0,limit:Number(H.searchParams.get("limit"))||500});return M({entries:D,cursor:D.at(-1)?.sequence??(Number(H.searchParams.get("after"))||0),operation:E.store.getOperation(T)})}if(H.pathname==="/api/queue/stream"&&L.method==="GET"){let T=String(H.searchParams.get("id")??"");if(!Zs(Se,T,"deployments:read"))return M({ok:!1,error:"Operation was not found in this scope."},404);let D=L.headers.get("last-event-id")?.trim(),U=Number(D||H.searchParams.get("after")||0),q=new TextEncoder,G,X=!1,Q=new ReadableStream({start(ie){let Ae=()=>{if(X)return;let ke=gr.resolveUser(L);if(!ke||!Zs(ke,T,"deployments:read")){if(X=!0,G)clearInterval(G);ie.close();return}let st=nr.logs(T,{after:U,limit:500});for(let tt of st)U=tt.sequence,ie.enqueue(q.encode(`id: ${tt.sequence}
|
|
686
|
+
event: log
|
|
687
|
+
data: ${JSON.stringify(tt)}
|
|
688
|
+
|
|
689
|
+
`));let De=E.store.getOperation(T);if(De&&["succeeded","failed","cancelled","timed_out"].includes(De.state)&&nr.logs(T,{after:U,limit:1}).length===0){if(X=!0,G)clearInterval(G);ie.enqueue(q.encode(`event: complete
|
|
690
|
+
data: ${JSON.stringify({state:De.state,cursor:U})}
|
|
691
|
+
|
|
692
|
+
`)),ie.close()}};if(Ae(),!X)G=setInterval(Ae,500);L.signal.addEventListener("abort",()=>{if(X=!0,G)clearInterval(G);try{ie.close()}catch{}},{once:!0})},cancel(){if(X=!0,G)clearInterval(G)}});return new Response(Q,{headers:{"content-type":"text/event-stream","cache-control":"no-store","x-accel-buffering":"no"}})}if(H.pathname==="/api/queue/cancel"&&L.method==="POST"){let T=await me(L),D=String(T.id??"");if(!Zs(Se,D,"deployments:cancel"))return M({ok:!1,error:"Operation was not found or cannot be cancelled in this scope."},404);let U=S(E.store,Se);try{return M({ok:!0,operation:nr.requestCancellation(D,U.id)})}catch(q){return M({ok:!1,error:q instanceof Error?q.message:"Cancellation failed."},409)}}if(H.pathname==="/api/queue/retry"&&L.method==="POST"){let T=await me(L),D=String(T.id??"");if(!Zs(Se,D,"deployments:create"))return M({ok:!1,error:"Operation was not found or cannot be retried in this scope."},404);let U=S(E.store,Se);try{return M({ok:!0,operation:nr.retry(D,String(T.errorClass??"manual"),{delayMs:Number(T.delayMs)||0,actorId:U.id})})}catch(q){return M({ok:!1,error:q instanceof Error?q.message:"Retry failed."},409)}}if(H.pathname==="/api/queue/settings"&&L.method==="GET")return M({concurrency:nr.limits});if(H.pathname==="/api/queue/settings"&&L.method==="PATCH"){let T=await me(L);if(T.confirm!=="update queue limits")return M({ok:!1,error:'Type "update queue limits" to confirm production concurrency changes.'},409);let D=S(E.store,Se);return M({ok:!0,concurrency:nr.configureConcurrency(T.concurrency??{},{organizationId:E.organization.id,actorId:D.id})})}if(H.pathname==="/api/queue/history"&&L.method==="DELETE"){let T=await me(L);if(T.confirm!=="clear completed")return M({ok:!1,error:'Type "clear completed" to confirm retained history cleanup.'},409);let D=S(E.store,Se);return M({ok:!0,deleted:nr.clearCompleted({projectId:E.project.id,before:typeof T.before==="string"?T.before:void 0,actorId:D.id})})}if(H.pathname==="/api/previews"&&L.method==="GET"){let T=wr.previews.listInstances({projectId:E.project.id}).filter((q)=>rn(Se,q.resourceId,"deployments:read")),D=wr.previews.listDefinitions(E.project.id).filter((q)=>rn(Se,q.resourceId,"project:read")),U=E.store.listResources(E.project.id).filter((q)=>q.kind==="application"&&rn(Se,q.id,"project:read")).map((q)=>({id:q.id,slug:q.slug,name:q.name,environmentId:q.environmentId}));return M({previews:T,definitions:D,resources:U,environments:[...E.environments.values()]})}if(H.pathname==="/api/releases"&&L.method==="GET"){let T=[...E.environments.values()],D=E.store.listResources(E.project.id).filter((G)=>rn(Se,G.id,"deployments:read")),U=new Set(D.map((G)=>G.id)),q=$n.releases.list({projectId:E.project.id}).filter((G)=>U.has(G.resourceId)).map((G)=>{let X=D.find((Ae)=>Ae.id===G.resourceId),Q=T.find((Ae)=>Ae.id===G.environmentId),ie=G.previousReleaseId?$n.releases.get(G.previousReleaseId):void 0;return{...G,resource:X?{id:X.id,name:X.name,slug:X.slug,provider:X.provider}:void 0,environment:Q?{id:Q.id,name:Q.name,slug:Q.slug,kind:Q.kind}:void 0,artifact:$n.releases.getArtifact(G.artifactId),transitions:$n.releases.transitions(G.id),approvals:$n.releases.approvals(G.id),previous:ie,comparison:ie?$n.releases.compare(ie.id,G.id):void 0,capabilities:$r({kind:G.kind,provider:X?.provider,hasHealthGate:!!G.healthGate,replicas:Number(G.manifest?.replicas)||1})}});return M({releases:q,resources:D.map((G)=>({id:G.id,name:G.name,slug:G.slug,environmentId:G.environmentId,provider:G.provider})),environments:T})}if(H.pathname==="/api/releases/action"&&L.method==="POST"){let T=await me(L),D=$n.releases.get(String(T.id??"")),U=String(T.action??"");if(!D||!rn(Se,D.resourceId,"deployments:create"))return M({ok:!1,error:"Release was not found in this scope."},404);let q=S(E.store,Se);try{if(U==="activate")return M({ok:!0,operation:$n.enqueueActivation(D,{actorId:q.id})});if(U==="rollback"){let G=E.store.getResource(D.resourceId);if(T.confirm!==G?.slug)return M({ok:!1,error:`Type "${G?.slug??""}" to confirm rollback.`},409);return M({ok:!0,operation:$n.enqueueRollback(D,{actorId:q.id,targetReleaseId:typeof T.targetReleaseId==="string"?T.targetReleaseId:void 0})})}if(U==="approve"){let G=E.store.listEnvironments(D.projectId).find((X)=>X.id===D.environmentId);if(T.confirm!==G?.slug)return M({ok:!1,error:`Type "${G?.slug??""}" to confirm the environment gate.`},409);return M({ok:!0,release:$n.releases.approve(D.id,{actorId:q.id,decision:T.decision==="rejected"?"rejected":"approved",comment:typeof T.comment==="string"?T.comment:void 0})})}if(U==="pin")return M({ok:!0,release:$n.releases.pin(D.id,T.pinned!==!1,typeof T.reason==="string"?T.reason:void 0)});if(U==="promote"){let G=E.store.getResource(String(T.targetResourceId??"")),X=G?E.store.listEnvironments(D.projectId).find((Q)=>Q.id===G.environmentId):void 0;if(!G||!X||!rn(Se,G.id,"deployments:create"))return M({ok:!1,error:"Promotion target was not found in this scope."},404);if(T.confirm!==X.slug)return M({ok:!1,error:`Type "${X.slug}" to confirm promotion.`},409);return M({ok:!0,release:$n.releases.promote(D.id,{targetEnvironmentId:X.id,targetResourceId:G.id,config:T.config&&typeof T.config==="object"?T.config:{},strategy:typeof T.strategy==="string"?T.strategy:void 0,actorId:q.id,approvalRequired:T.approvalRequired===!0})})}return M({ok:!1,error:"Unsupported release action."},422)}catch(G){return M({ok:!1,error:G instanceof Error?G.message:"Release action failed."},409)}}if(H.pathname==="/api/compose"&&L.method==="GET"){let T=ro.applications.list({projectId:E.project.id}).filter((D)=>rn(Se,D.resourceId,"applications:read")).map((D)=>({...D,services:ro.applications.services(D.id)}));return M({applications:T,templates:yr(),environments:[...E.environments.values()]})}if(H.pathname==="/api/compose/preview"&&L.method==="POST"){let T=await me(L),D=String(T.environmentId??"");if(!Ya(Se,D,"applications:manage"))return M({ok:!1,error:"Environment was not found in this scope."},404);try{return M({ok:!0,result:ro.applications.preview(String(T.source??""),{name:String(T.name??""),slug:typeof T.slug==="string"?T.slug:void 0,projectId:E.project.id,environmentId:D})})}catch(U){return M({ok:!1,error:U instanceof Error?U.message:"Compose could not be parsed."},422)}}if((H.pathname==="/api/compose/import"||H.pathname==="/api/compose/template")&&L.method==="POST"){let T=await me(L),D=String(T.environmentId??"");if(!Ya(Se,D,"applications:manage"))return M({ok:!1,error:"Environment was not found in this scope."},404);let U=S(E.store,Se);try{let q=H.pathname.endsWith("/template")?ro.applications.fromTemplate(String(T.templateId??""),T.inputs&&typeof T.inputs==="object"?T.inputs:{},{name:String(T.name??""),projectId:E.project.id,environmentId:D,version:typeof T.templateVersion==="string"?T.templateVersion:void 0,createdByActorId:U.id}):ro.applications.import(String(T.source??""),{name:String(T.name??""),slug:typeof T.slug==="string"?T.slug:void 0,projectId:E.project.id,environmentId:D,createdByActorId:U.id});return M({ok:!0,application:q.application,diagnostics:q.parsed.diagnostics})}catch(q){return M({ok:!1,error:q instanceof Error?q.message:"Compose application could not be saved."},422)}}if(H.pathname==="/api/compose/action"&&L.method==="POST"){let T=await me(L),D=ro.applications.get(String(T.id??"")),U=String(T.action??""),q=U==="delete"||U==="stop"?"deployments:cancel":"deployments:create";if(!D||!rn(Se,D.resourceId,q))return M({ok:!1,error:"Compose application was not found in this scope."},404);try{let G=S(E.store,Se);return M({ok:!0,operation:ro.enqueue(D,U,{actorId:G.id,service:typeof T.service==="string"?T.service:void 0,replicas:Number.isFinite(Number(T.replicas))?Number(T.replicas):void 0,removeVolumes:T.removeVolumes===!0,confirmation:typeof T.confirm==="string"?T.confirm:void 0})})}catch(G){return M({ok:!1,error:G instanceof Error?G.message:"Compose operation could not be queued."},409)}}if((H.pathname==="/api/compose/logs"||H.pathname==="/api/compose/shell")&&L.method==="POST"){let T=await me(L),D=ro.applications.get(String(T.id??"")),U=H.pathname.endsWith("/shell"),q=U?"runtime:terminal":"runtime:logs";if(!D||!rn(Se,D.resourceId,q))return M({ok:!1,error:"Compose service was not found in this scope."},404);let G=String(T.service??"");if(!D.manifest.spec.services[G])return M({ok:!1,error:"Compose service was not found."},404);if(U&&T.confirm!==G)return M({ok:!1,error:`Type "${G}" to run a service command.`},409);let X=E.store.listEnvironments(D.projectId).find((ie)=>ie.id===D.environmentId)?.slug;if(!X)return M({ok:!1,error:"Compose environment was not found."},404);let Q=U?Ic(D.manifest,G,Array.isArray(T.command)?T.command.map(String):["sh"]):yc(D.manifest,G,Number(T.lines)||200);return M(await kc(h,X,Q))}if(H.pathname==="/api/previews/definitions"&&L.method==="POST"){let T=await me(L),D=String(T.resourceId??"");if(!rn(Se,D,"config:write"))return M({ok:!1,error:"Application was not found in this scope."},404);let U=S(E.store,Se);try{return M({ok:!0,definition:wr.previews.createDefinition({...T,projectId:E.project.id,resourceId:D,baseEnvironmentId:String(T.baseEnvironmentId??""),domainPattern:String(T.domainPattern??""),createdByActorId:U.id})})}catch(q){return M({ok:!1,error:q instanceof Error?q.message:"Preview policy could not be created."},409)}}if(H.pathname==="/api/previews/deploy"&&L.method==="POST"){let T=await me(L),D=wr.previews.getDefinition(String(T.definitionId??""));if(!D||!rn(Se,D.resourceId,"deployments:create"))return M({ok:!1,error:"Preview policy was not found in this scope."},404);let U=S(E.store,Se);try{let q=wr.previews.upsert({definitionId:D.id,sourceProvider:"dashboard",repository:"dashboard",branch:String(T.branch??""),pullRequestNumber:Number(T.pullRequestNumber)||void 0,commitSha:String(T.commitSha??""),createdByActorId:U.id});return M({ok:!0,preview:q.preview,operation:wr.enqueueDeploy(q.preview,{created:q.created,actorId:U.id})})}catch(q){return M({ok:!1,error:q instanceof Error?q.message:"Preview could not be queued."},409)}}if(H.pathname==="/api/previews/destroy"&&L.method==="POST"){let T=await me(L),D=wr.previews.getInstance(String(T.id??""));if(!D||!rn(Se,D.resourceId,"deployments:cancel"))return M({ok:!1,error:"Preview was not found in this scope."},404);if(T.confirm!==D.name)return M({ok:!1,error:`Type "${D.name}" to confirm tagged teardown.`},409);let U=S(E.store,Se);return M({ok:!0,operation:wr.enqueueDestroy(D,"dashboard",U.id)})}if(H.pathname==="/api/previews/extend"&&L.method==="POST"){let T=await me(L),D=wr.previews.getInstance(String(T.id??""));if(!D||!rn(Se,D.resourceId,"deployments:create"))return M({ok:!1,error:"Preview was not found in this scope."},404);return M({ok:!0,preview:wr.previews.extend(D.id,Number(T.hours))})}if(H.pathname==="/api/previews/rebuild"&&L.method==="POST"){let T=await me(L),D=wr.previews.getInstance(String(T.id??""));if(!D||!rn(Se,D.resourceId,"deployments:create"))return M({ok:!1,error:"Preview was not found in this scope."},404);let U=S(E.store,Se);return M({ok:!0,operation:wr.enqueueDeploy(D,{actorId:U.id,reason:"manual_rebuild"})})}if(H.pathname==="/api/previews/cleanup"&&L.method==="POST"){let T=await me(L);if(!T.dryRun&&T.confirm!=="cleanup previews")return M({ok:!1,error:'Type "cleanup previews" to confirm teardown.'},409);let D=S(E.store,Se);return M({ok:!0,...wr.cleanup({dryRun:T.dryRun===!0,maxAgeHours:Number(T.maxAgeHours)||void 0,keepCount:Number(T.keepCount)||void 0,actorId:D.id})})}if(H.pathname==="/api/control-plane/events")return M({events:E.store.listEvents({organizationId:E.organization.id,projectId:E.project.id,operationId:H.searchParams.get("operationId")??void 0,correlationId:H.searchParams.get("correlationId")??void 0,afterSequence:Number(H.searchParams.get("afterSequence"))||void 0,limit:Number(H.searchParams.get("limit"))||200})});if(H.pathname==="/api/search"){let T=String(H.searchParams.get("q")??"").trim().slice(0,128),D=jn.role==="member"?new Set(Object.keys(jn.sites)):void 0,U=T?As(E.store,{projectId:E.project.id,query:T,allowedResourceSlugs:D,limit:Number(H.searchParams.get("limit"))||20}):[];return M({query:T,results:U,stale:!1})}if(H.pathname==="/api/search/preferences"&&L.method==="GET"){let T=`dashboard:${Se.username.toLowerCase()}`;return M({savedFilters:E.store.listSavedFilters(T),navigation:E.store.listNavigation(T)})}if(H.pathname==="/api/search/preferences"&&L.method==="POST"){let T=`dashboard:${Se.username.toLowerCase()}`,D=await me(L),U=String(D.action??"");if(U==="save-filter")return M({savedFilter:E.store.saveFilter(T,String(D.name??""),String(D.routeId??""),D.query??{})});if(U==="favorite")return M({navigation:E.store.setFavorite(T,String(D.entityType??""),String(D.entityId??""),D.favorite!==!1)});if(U==="visit")return M({navigation:E.store.recordNavigation(T,String(D.entityType??""),String(D.entityId??""))});return M({ok:!1,error:"Unknown search preference action."},422)}if(H.pathname==="/api/search/preferences"&&L.method==="DELETE"){let T=`dashboard:${Se.username.toLowerCase()}`,D=await me(L);return M({ok:E.store.deleteSavedFilter(T,String(D.id??""))})}if(H.pathname==="/api/organization"&&L.method==="GET"){let T=E.store.listMemberships(E.organization.id,{includeRevoked:!0}).map((D)=>{let U=E.store.getActor(D.actorId);return{...D,actor:U?{id:U.id,displayName:U.displayName,externalId:U.externalId,disabledAt:U.disabledAt}:void 0,grants:E.store.listGrants(D.id)}});return M({organization:E.organization,project:E.project,environments:E.store.listEnvironments(E.project.id),resources:E.store.listResources(E.project.id).map((D)=>({id:D.id,environmentId:D.environmentId,kind:D.kind,slug:D.slug,name:D.name})),memberships:T,invitations:E.store.listInvitations(E.organization.id)})}if(H.pathname==="/api/organization/invitations"&&L.method==="GET")return M({invitations:E.store.listInvitations(E.organization.id)});if(H.pathname==="/api/organization/invitations"&&L.method==="POST"){let T=await me(L),D=Mu(T.roleTemplate),U=Pc(T);if(!D||!U)return M({ok:!1,error:"Choose a valid role template and resource scope."},422);let q=Xe(Se);if(!q.actor||!q.membership)return M({ok:!1,error:"Your organization membership is unavailable."},403);if(D==="owner"&&(!Xn(String(T.password??""),Se.passwordHash)||q.membership.roleTemplate!=="owner"))return M({ok:!1,error:"Owner invitations require an owner to re-enter their password."},401);try{let G=E.store.createInvitation({organizationId:E.organization.id,email:String(T.email??""),roleTemplate:D,scope:U,invitedByActorId:q.actor.id,expiresInMs:Number(T.expiresInMs)||void 0}),X=`${H.origin}/accept-invitation?token=${encodeURIComponent(G.token)}`,Q=await Lr(h,{to:G.invitation.email,subject:"Your ts-cloud organization invitation",text:`Accept your ts-cloud invitation: ${X}
|
|
693
|
+
|
|
694
|
+
This link expires ${G.invitation.expiresAt} and can be used once.`});return M({invitation:G.invitation,token:G.token,acceptUrl:X,delivered:Q},201)}catch(G){return M({ok:!1,error:G instanceof Error?G.message:"Invitation could not be created."},422)}}if(H.pathname==="/api/organization/invitations"&&L.method==="DELETE"){let T=await me(L),D=Xe(Se);try{return M({invitation:E.store.revokeInvitation(String(T.id??""),D.actor?.id)})}catch(U){return M({ok:!1,error:U instanceof Error?U.message:"Invitation could not be revoked."},409)}}if(H.pathname==="/api/organization/invitations/resend"&&L.method==="POST"){let T=await me(L),D=Xe(Se);try{let U=E.store.reissueInvitation(String(T.id??""),D.actor?.id),q=`${H.origin}/accept-invitation?token=${encodeURIComponent(U.token)}`,G=await Lr(h,{to:U.invitation.email,subject:"Your ts-cloud organization invitation",text:`Accept your ts-cloud invitation: ${q}
|
|
695
|
+
|
|
696
|
+
This replacement link expires ${U.invitation.expiresAt} and can be used once.`});return M({invitation:U.invitation,token:U.token,acceptUrl:q,delivered:G})}catch(U){return M({ok:!1,error:U instanceof Error?U.message:"Invitation could not be resent."},409)}}if(H.pathname==="/api/organization/memberships"&&L.method==="PATCH"){let T=await me(L),D=E.store.getMembership(String(T.id??"")),U=Mu(T.roleTemplate),q=Pc(T);if(!D||D.organizationId!==E.organization.id)return M({ok:!1,error:"Membership was not found."},404);if(!U||!q)return M({ok:!1,error:"Choose a valid role template and resource scope."},422);let G=Xe(Se);if((D.roleTemplate==="owner"||U==="owner")&&(!Xn(String(T.password??""),Se.passwordHash)||G.membership?.roleTemplate!=="owner"))return M({ok:!1,error:"Ownership changes require an owner to re-enter their password."},401);let Q=E.store.resolveAuthorizationTarget(E.organization.id,q)??E.store.resolveAuthorizationTarget(E.organization.id,{type:"project",id:E.project.id}),ie=E.store.listGrants(D.id),Ae=Bt({membership:D,grants:ie,target:Q}),ke={...D,roleTemplate:U,scope:q},st=Bt({membership:ke,grants:ie,target:Q}),De={gained:st.filter((tt)=>!Ae.includes(tt)),lost:Ae.filter((tt)=>!st.includes(tt))};if(T.preview===!0)return M({membership:D,proposed:{roleTemplate:U,scope:q},accessDiff:De});if(String(T.confirm??"")!==D.id)return M({ok:!1,error:`Confirm this change with membership ID ${D.id}.`,accessDiff:De},409);try{return M({membership:E.store.updateMembership({id:D.id,roleTemplate:U,scope:q,actorId:G.actor?.id}),accessDiff:De})}catch(tt){return M({ok:!1,error:tt instanceof Error?tt.message:"Membership could not be updated."},409)}}if(H.pathname==="/api/organization/memberships"&&L.method==="DELETE"){let T=await me(L),D=E.store.getMembership(String(T.id??""));if(!D||D.organizationId!==E.organization.id)return M({ok:!1,error:"Membership was not found."},404);if(String(T.confirm??"")!==D.id)return M({ok:!1,error:`Confirm removal with membership ID ${D.id}.`},409);let U=Xe(Se);if(D.roleTemplate==="owner"&&(U.membership?.roleTemplate!=="owner"||!Xn(String(T.password??""),Se.passwordHash)))return M({ok:!1,error:"Removing an owner requires an owner to re-enter their current password."},401);try{return M({membership:E.store.revokeMembership(D.id,U.actor?.id)})}catch(q){return M({ok:!1,error:q instanceof Error?q.message:"Membership could not be removed."},409)}}if(H.pathname==="/api/organization/grants"&&L.method==="POST"){let T=await me(L),D=String(T.capability??""),U=T.effect==="deny"?"deny":T.effect==="allow"?"allow":void 0,q=Pc(T),G=Xe(Se);if(!U||!q||!oe.includes(D))return M({ok:!1,error:"Choose a valid capability, effect, and resource scope."},422);try{return M({grant:E.store.upsertGrant({organizationId:E.organization.id,membershipId:String(T.membershipId??""),effect:U,capability:D,scope:q,actorId:G.actor?.id})},201)}catch(X){return M({ok:!1,error:X instanceof Error?X.message:"Grant could not be created."},422)}}if(H.pathname==="/api/organization/grants"&&L.method==="DELETE"){let T=await me(L),D=Xe(Se),U=E.store.getGrant(String(T.id??""));if(!U||U.organizationId!==E.organization.id)return M({ok:!1,error:"Grant was not found."},404);return M({ok:E.store.removeGrant(U.id,D.actor?.id)})}if(H.pathname==="/api/tags"&&L.method==="GET")return M({tags:E.store.listTags(E.project.id),assignments:E.store.listResourceTags(E.project.id)});if(H.pathname==="/api/tags"&&L.method==="POST"){let T=await me(L),D=E.store.upsertTag(E.project.id,String(T.name??""),String(T.color??"#5a8be0"));if(T.resourceId)E.store.assignTag(String(T.resourceId),D.id);return M({tag:D},201)}if(H.pathname==="/api/tags"&&L.method==="DELETE"){let T=await me(L);return E.store.removeTag(String(T.resourceId??""),String(T.tagId??"")),M({ok:!0})}if(H.pathname==="/"&&!OS(H.pathname,jn))return new Response(null,{status:302,headers:{location:`/server/sites?env=${encodeURIComponent(xe)}`}});if(H.pathname==="/"&&ur?.mode==="serverless")return new Response(null,{status:302,headers:{location:`/serverless?env=${encodeURIComponent(xe)}`}});let jc=Pl(H.pathname);if(jc)return new Response(null,{status:308,headers:{location:`${jc}?env=${encodeURIComponent(xe)}`}});if(H.pathname==="/api/env"&&L.method==="POST"){let T=await me(L),D=String(T.env??"");if(!x.includes(D))return M({ok:!1,error:`Unknown environment '${D}'.`,environments:x},404);return M({ok:!0,environment:D,environments:x,href:`?env=${encodeURIComponent(D)}`})}if(H.pathname==="/api/config"){let T={...Mi(h),environment:xe,environments:x};return M(Kb(T,jn))}if(H.pathname==="/api/ssh-keys"&&L.method==="GET")return M({configPath:F,keys:fs(Ga(h))});if(H.pathname==="/api/ssh-keys"&&L.method==="POST"){if(!F)return M({ok:!1,error:"No cloud config file was found in this checkout."},404);let T=await me(L),D=await pi(F,"utf8"),U=Ga(h),q=Yv({configText:D,name:String(T.name??""),publicKey:String(T.publicKey??""),existingKeys:U});return await Ks(F,q),_S(h,[...U,{name:String(T.name??"").trim(),publicKey:String(T.publicKey??"").trim().replace(/\s+/g," ")}]),M({ok:!0,configPath:F,keys:fs(Ga(h))})}if(H.pathname==="/api/ssh-keys"&&L.method==="DELETE"){if(!F)return M({ok:!1,error:"No cloud config file was found in this checkout."},404);let T=await me(L),D=String(T.name??H.searchParams.get("name")??""),U=await pi(F,"utf8"),q=Ga(h),G=Zv({configText:U,name:D,existingKeys:q});return await Ks(F,G),_S(h,q.filter((X)=>X.name!==D.trim())),M({ok:!0,configPath:F,keys:fs(Ga(h))})}if(H.pathname==="/api/firewall"&&L.method==="GET")return M({configPath:F,alwaysOpen:[22,80,443],ports:Ug(h)});if(H.pathname==="/api/firewall"&&L.method==="POST"){if(!F)return M({ok:!1,error:"No cloud config file was found in this checkout."},404);let T=await me(L),D=Number(T.port);if(!Rc(D))return M({ok:!1,error:"Port must be an integer between 1 and 65535."},422);let U=Ug(h),q=await pi(F,"utf8");try{await Ks(F,tS(q,D,U))}catch(Q){return M({ok:!1,error:Q?.message??String(Q)},422)}let G=Hs([...U,D]);TS(h,G);let X=T.apply===!1?null:await kc(h,xe,`ufw allow ${D}/tcp`).catch((Q)=>({ok:!1,error:String(Q?.message??Q)}));return M({ok:!0,configPath:F,ports:G,apply:X})}if(H.pathname==="/api/firewall"&&L.method==="DELETE"){if(!F)return M({ok:!1,error:"No cloud config file was found in this checkout."},404);let T=await me(L),D=Number(T.port??H.searchParams.get("port"));if(!Rc(D))return M({ok:!1,error:"Port must be an integer between 1 and 65535."},422);let U=Ug(h),q=await pi(F,"utf8");await Ks(F,rS(q,D,U));let G=Hs(U.filter((Q)=>Q!==D));TS(h,G);let X=T.apply===!1?null:await kc(h,xe,`ufw delete allow ${D}/tcp`).catch((Q)=>({ok:!1,error:String(Q?.message??Q)}));return M({ok:!0,configPath:F,ports:G,apply:X})}if(H.pathname==="/api/sites"&&L.method==="POST"){if(!F)return M({ok:!1,error:"No cloud config file was found in this checkout."},404);let T=await me(L),D=String(T.name??"").trim(),U=String(T.root??"").trim();if(!D||!U)return M({ok:!1,error:"Site name and root are required."},422);let q={deploy:T.deploy==="bucket"?"bucket":"server",root:U,path:String(T.path??"").trim()||void 0,domain:String(T.domain??"").trim()||void 0,build:String(T.build??"").trim()||void 0,start:String(T.start??"").trim()||void 0,port:T.port?Number(T.port):void 0,type:String(T.type??"").trim()||void 0};if(q.port!==void 0&&(!Number.isInteger(q.port)||q.port<1||q.port>65535))return M({ok:!1,error:"Port must be a number between 1 and 65535."},422);let G=await pi(F,"utf8"),X=jr({configText:G,name:D,root:q.root,domain:q.domain,path:q.path,deploy:q.deploy,build:q.build,start:q.start,port:q.port,type:q.type});return await Ks(F,X),H_(h,D,Object.fromEntries(Object.entries(q).filter(([,Q])=>Q!==void 0))),ur=await Js(h,xe),K.set(xe,ur),M({ok:!0,configPath:F,site:D,data:ur})}if(H.pathname==="/api/sites"&&L.method==="DELETE"){if(!F)return M({ok:!1,error:"No cloud config file was found in this checkout."},404);let T=await me(L),D=String(T.name??H.searchParams.get("name")??"").trim();if(!D||!h.sites?.[D])return M({ok:!1,error:`Site '${D}' was not found.`},404);let U=await pi(F,"utf8");return await Ks(F,zr({configText:U,name:D})),delete h.sites[D],ur=await Js(h,xe),K.set(xe,ur),M({ok:!0,configPath:F,site:D,data:ur})}if(H.pathname==="/api/sites"&&L.method==="PATCH"){if(!F)return M({ok:!1,error:"No cloud config file was found in this checkout."},404);let T=await me(L),D=String(T.name??"").trim(),U=h.sites?.[D];if(!D||!U)return M({ok:!1,error:`Site '${D}' was not found.`},404);if(jn.role==="member"){let X=Yb(T);if(!X.ok)return M({ok:!1,error:X.error},403);let Q=Zb({siteName:D,body:T,sites:h.sites??{},ownSites:Object.keys(jn.sites)});if(!Q.ok)return M({ok:!1,error:Q.error},409)}if(T.port!==void 0&&T.port!==null&&T.port!==""&&(!Number.isInteger(Number(T.port))||Number(T.port)<1||Number(T.port)>65535))return M({ok:!1,error:"Port must be a number between 1 and 65535."},422);if(typeof T.domain==="string"&&T.domain.trim()&&!Jo(T.domain.trim()))return M({ok:!1,error:`Domain '${T.domain.trim()}' is not a valid hostname.`},422);let q=await pi(F,"utf8"),G=(X,Q)=>{q=xi({configText:q,siteName:D,key:X,valueText:Q})};if(T.ssl!==void 0)G("ssl",Cl(T.ssl)),U.ssl=T.ssl;if(T.env!==void 0&&T.env&&typeof T.env==="object")G("env",El(T.env)),U.env=T.env;if(T.aliases!==void 0&&Array.isArray(T.aliases)){let X=T.aliases.map((Q)=>String(Q));G("aliases",vl(X)),U.aliases=X.map((Q)=>Q.trim().toLowerCase()).filter(Boolean)}if(T.redirects!==void 0&&T.redirects&&typeof T.redirects==="object")G("redirects",Tl(T.redirects)),U.redirects=T.redirects;for(let X of["domain","path","build","start","type","root","php"])if(typeof T[X]==="string"&&T[X].trim())G(X,bl(String(T[X]).trim())),U[X]=String(T[X]).trim();if(T.port!==void 0&&T.port!==null&&T.port!=="")G("port",String(Number(T.port))),U.port=Number(T.port);return await Ks(F,q),ur=await Js(h,xe),K.set(xe,ur),M({ok:!0,configPath:F,site:D,data:ur})}if(H.pathname==="/api/sites/deploy"&&L.method==="POST"){let T=await me(L),D=String(T.name??"").trim();if(!D||!h.sites?.[D])return M({ok:!1,error:`Site '${D}' was not found.`},404);if(T.confirm!==D)return M({ok:!1,error:`Type "${D}" to deploy this site.`},409);let U=Z(E,String(xe));await new lt(St).run(new Qr,{...U,artifactRoot:t}),Ri(St,U,ur??{});let q=Di(St,{scope:U,desiredConfigHash:E.project.desiredConfigHash});if(q.decision.outcome==="block")return M({ok:!1,error:`Deployment blocked by security policy: ${q.decision.explanation}`,securityReview:q},409);let G={id:`deploy:${D}`,label:`Deploy ${D}`,description:`Deploy the ${D} site for ${xe}.`,command:["deploy","--env",xe,"--site",D,"--yes"],mutates:!0},X=await we({controlPlane:E,environment:xe,actor:Se,kind:"dashboard.site.deploy",resourceSlug:D,input:{site:D},execute:()=>Hg(G,{cwd:t,cliEntry:m})});return M({...X.result,controlPlaneOperation:X.operation,securityReview:q})}if(H.pathname==="/api/actions")return M(Yr(xe));if(H.pathname==="/api/server/operations")return M(Ou(h,ur));if(H.pathname==="/api/runtime/workloads"&&L.method==="GET"){let T=await ee(h,xe);return M(T)}if(H.pathname==="/api/runtime/logs"&&L.method==="GET"){let T=String(H.searchParams.get("workload")??"");if(!T)return M({ok:!1,error:"A workload query parameter is required."},422);let D=Math.min(1000,Math.max(1,Number(H.searchParams.get("limit"))||200));try{return M(await new de(h,xe).logs(T,{limit:D}))}catch(U){return M({ok:!1,error:String(U?.message??U)},404)}}if(H.pathname==="/api/runtime/log-sessions"&&L.method==="POST"){let T=await me(L),D=String(T.workloadId??"");if(!D)return M({ok:!1,error:"A workloadId is required."},422);let U=await ee(h,xe),q=U.workloads.find((Q)=>Q.id===D);if(!q)return M({ok:!1,error:"Workload was not found in the current project and environment."},404);if(!q.capabilities.logs.supported)return M({ok:!1,error:q.capabilities.logs.reason??"Logs are unsupported for this workload."},409);let G=new de(h,xe,{inventory:async()=>U}),X=Rr.create("logs",D);return(async()=>{let Q=new Date(Date.now()-Math.min(3600000,Math.max(1000,Number(T.sinceMs)||60000)));try{for(let ie=0;ie<200&&!Rr.signal(X.id)?.aborted;ie++){let Ae=await G.logs(D,{limit:Math.min(500,Math.max(1,Number(T.limit)||200)),since:Q});for(let ke of Ae.lines)if(Rr.append(X.id,`${ke.timestamp?`${ke.timestamp} `:""}${ke.message}
|
|
697
|
+
`),ke.timestamp)Q=new Date(Math.max(Q.getTime(),new Date(ke.timestamp).getTime()+1));await Bun.sleep(3000)}if(!Rr.signal(X.id)?.aborted)Rr.close(X.id,"complete")}catch(ie){Rr.append(X.id,ie instanceof Error?ie.message:String(ie),"stderr"),Rr.close(X.id,"failed",ie instanceof Error?ie.message:String(ie))}})(),M({ok:!0,session:X,streamUrl:`/api/runtime/log-stream?id=${encodeURIComponent(X.id)}&workload=${encodeURIComponent(D)}`},201)}if((H.pathname==="/api/runtime/log-stream"||H.pathname==="/api/runtime/exec-stream")&&L.method==="GET"){let T=String(H.searchParams.get("id")??""),D=String(H.searchParams.get("workload")??""),U=Number(L.headers.get("last-event-id")??H.searchParams.get("after")??0)||0,q=Rr.read(T,D,U),G=H.pathname.includes("exec")?"exec":"logs";if(!q||q.kind!==G)return M({ok:!1,error:"Runtime stream was not found in this workload scope."},404);return M_(Rr,q,U,L.signal)}if((H.pathname==="/api/runtime/log-sessions"||H.pathname==="/api/runtime/exec-sessions")&&L.method==="DELETE"){let T=await me(L),D=Rr.cancel(String(T.id??""),String(T.workloadId??""));if(!D)return M({ok:!1,error:"Runtime stream was not found in this workload scope."},404);return M({ok:!0,session:D})}if(H.pathname==="/api/runtime/exec-sessions"&&L.method==="POST"){let T=await me(L),D=String(T.workloadId??""),U=await ee(h,xe),q=U.workloads.find((ie)=>ie.id===D);if(!q)return M({ok:!1,error:"Workload was not found in the current project and environment."},404);let G=gr.resolveSession(L),X=new de(h,xe,{inventory:async()=>U}),Q=Rr.create("exec",D);return we({controlPlane:E,environment:xe,actor:Se,kind:"runtime.exec",resourceSlug:q.links.service,input:{workloadId:D,provider:q.provider,preset:T.preset?String(T.preset):null,freeForm:!!String(T.command??"").trim()},execute:()=>X.exec({workloadId:D,preset:["process","sockets","filesystem"].includes(String(T.preset))?String(T.preset):void 0,command:typeof T.command==="string"?T.command:void 0,confirm:String(T.confirm??""),recentAuth:!Ne||!!G&&Fe.isRecentlyAuthenticated(G)})}).then(({result:ie,operation:Ae})=>{if(Rr.append(Q.id,`operation ${Ae.id}
|
|
698
|
+
`,"system"),ie.stdout)Rr.append(Q.id,ie.stdout,"stdout");if(ie.stderr)Rr.append(Q.id,ie.stderr,"stderr");Rr.close(Q.id,ie.ok?"complete":"failed",ie.error)}).catch((ie)=>{let Ae=ie instanceof Error?ie.message:String(ie);Rr.append(Q.id,Ae,"stderr"),Rr.close(Q.id,"failed",Ae)}),M({ok:!0,session:Q,streamUrl:`/api/runtime/exec-stream?id=${encodeURIComponent(Q.id)}&workload=${encodeURIComponent(D)}`},202)}if(H.pathname==="/api/runtime/files/read"&&L.method==="POST"){let T=await me(L),D=await ee(h,xe),U=gr.resolveSession(L),q=await new de(h,xe,{inventory:async()=>D}).readFile({workloadId:String(T.workloadId??""),path:String(T.path??""),confirm:String(T.confirm??""),recentAuth:!Ne||!!U&&Fe.isRecentlyAuthenticated(U)});return M(q,q.ok?200:409)}if(H.pathname==="/api/runtime/files/write"&&L.method==="POST"){let T=await me(L),D=await ee(h,xe),U=String(T.workloadId??""),q=D.workloads.find((ie)=>ie.id===U);if(!q)return M({ok:!1,error:"Workload was not found in the current project and environment."},404);let G=gr.resolveSession(L),X=new de(h,xe,{inventory:async()=>D}),Q=await we({controlPlane:E,environment:xe,actor:Se,kind:"runtime.file.write",resourceSlug:q.links.service,input:{workloadId:U,provider:q.provider,path:String(T.path??""),bytes:Buffer.from(String(T.contentBase64??""),"base64").byteLength},execute:()=>X.writeFile({workloadId:U,path:String(T.path??""),contentBase64:String(T.contentBase64??""),confirm:String(T.confirm??""),recentAuth:!Ne||!!G&&Fe.isRecentlyAuthenticated(G)})});return M({...Q.result,controlPlaneOperation:Q.operation},Q.result.ok?200:409)}if(H.pathname==="/api/runtime/operations"&&L.method==="POST"){let T=await me(L),D=String(T.workloadId??""),U=String(T.action??"");if(!D||!["start","stop","restart","redeploy","scale","inspect"].includes(U))return M({ok:!1,error:"A valid workloadId and lifecycle action are required."},422);let q=await ee(h,xe),G=q.workloads.find((Ae)=>Ae.id===D);if(!G)return M({ok:!1,error:"Workload was not found in the current project and environment."},404);let X=gr.resolveSession(L),Q=new de(h,xe,{inventory:async()=>q}),ie=await we({controlPlane:E,environment:xe,actor:Se,kind:`runtime.${U}`,resourceSlug:G.links.service,input:{workloadId:D,provider:G.provider,action:U,replicas:T.replicas==null?null:Number(T.replicas)},execute:()=>Q.run({workloadId:D,action:U,replicas:T.replicas==null?void 0:Number(T.replicas),confirm:String(T.confirm??""),recentAuth:!Ne||!!X&&Fe.isRecentlyAuthenticated(X)})});return M({...ie.result,controlPlaneOperation:ie.operation},ie.result.ok?200:409)}if(H.pathname.startsWith("/api/protection")){let T=Se.role==="admin"||Nc(Se,"security:read"),D=Se.role==="admin"||Nc(Se,"security:manage");if(!T)return M({ok:!1,error:"Protection controls require security:read."},403);let{ProtectionControlStore:U,describePosture:q}=await import("./protection/index.js"),G=new U(E.store),X=()=>{if(!D)throw Error("Changing protection controls requires security:manage.")};if(H.pathname==="/api/protection"&&L.method==="GET"){let Q=G.current();return M({ok:!0,posture:q(Q),controls:Q,raw:G.raw()})}if(H.pathname==="/api/protection/attack-mode"&&L.method==="POST"){X();let Q=await me(L);if(Q.enabled===!1)return M({ok:!0,disabled:G.disableAttackMode()});return M({ok:!0,control:G.enableAttackMode({hours:Q.hours==null?void 0:Number(Q.hours),reason:String(Q.reason??"Enabled from the dashboard."),actorId:Xe(Se).actor?.id})})}if(H.pathname==="/api/protection/pause"&&L.method==="POST"){X();let Q=await me(L);if(Q.enabled===!1)return M({ok:!0,resumed:G.resumeMitigations()});return M({ok:!0,control:G.pauseMitigations({hours:Q.hours==null?void 0:Number(Q.hours),reason:String(Q.reason??""),actorId:Xe(Se).actor?.id})})}if(H.pathname==="/api/protection/ip-rules"&&L.method==="GET")return M({ok:!0,ipRules:G.current().ipRules});if(H.pathname==="/api/protection/ip-rules"&&(L.method==="POST"||L.method==="DELETE")){X();let Q=await me(L),ie=Q.list==="allow"?"allow":"block",Ae=L.method==="POST"?G.addIpRule(ie,String(Q.cidr??"")):G.removeIpRule(ie,String(Q.cidr??""));return M({ok:!0,ipRules:Ae})}return M({ok:!1,error:"Protection endpoint was not found."},404)}if(H.pathname.startsWith("/api/spend/")||H.pathname==="/api/usage"){let T=await qS(),{store:D,service:U,gate:q}=T,G=Se.role==="admin"||Nc(Se,"billing:read"),X=Se.role==="admin"||Nc(Se,"billing:manage");if(!G)return M({ok:!1,error:"Spend data requires the billing:read capability."},403);let Q=E.environments.get(xe),ie={organizationId:E.organization.id,projectId:E.project.id,environmentId:Q?.id},Ae=()=>{if(!X)throw Error("Changing a budget requires the billing:manage capability.")};if(H.pathname==="/api/usage"&&L.method==="GET")return M({ok:!0,...U.usageReport({organizationId:ie.organizationId,projectId:ie.projectId,period:H.searchParams.get("period")??"monthly",timezone:H.searchParams.get("timezone")??void 0})});if(H.pathname==="/api/spend/budgets"&&L.method==="GET")return M({ok:!0,budgets:D.listBudgets({organizationId:ie.organizationId,projectId:ie.projectId}).map((ke)=>({...ke,status:U.status(ke)}))});if(H.pathname==="/api/spend/budgets"&&L.method==="POST"){Ae();let ke=await me(L),st=String(ke.scope??"project"),De=D.createBudget({organizationId:ie.organizationId,projectId:st==="organization"?void 0:ie.projectId,environmentId:st==="environment"?ie.environmentId:void 0,name:String(ke.name??"").trim(),period:ke.period??"monthly",timezone:ke.timezone?String(ke.timezone):void 0,softLimitCents:ke.softLimitCents==null?void 0:Math.round(Number(ke.softLimitCents)),hardLimitCents:ke.hardLimitCents==null?void 0:Math.round(Number(ke.hardLimitCents)),graceSeconds:ke.graceSeconds==null?void 0:Number(ke.graceSeconds),dryRun:ke.dryRun!==!1});return M({ok:!0,budget:De})}if(H.pathname==="/api/spend/budgets"&&L.method==="PATCH"){Ae();let ke=await me(L),st=String(ke.id??""),De=D.getBudget(st);if(!De||De.organizationId!==ie.organizationId)return M({ok:!1,error:"Budget was not found."},404);return M({ok:!0,budget:D.updateBudget(st,{softLimitCents:ke.softLimitCents==null?De.softLimitCents:Math.round(Number(ke.softLimitCents)),hardLimitCents:ke.hardLimitCents==null?De.hardLimitCents:Math.round(Number(ke.hardLimitCents)),enabled:ke.enabled==null?De.enabled:ke.enabled===!0,dryRun:ke.dryRun==null?De.dryRun:ke.dryRun===!0},ke.expectedVersion==null?void 0:Number(ke.expectedVersion))})}if(H.pathname==="/api/spend/budgets"&&L.method==="DELETE"){Ae();let ke=await me(L),st=String(ke.id??""),De=D.getBudget(st);if(!De||De.organizationId!==ie.organizationId)return M({ok:!1,error:"Budget was not found."},404);let tt=q.closeBudget(st);return D.deleteBudget(st),M({ok:!0,deleted:!0,lifted:tt})}if(H.pathname==="/api/spend/check"&&L.method==="POST"){Ae();let ke=await me(L),{SpendRunner:st}=await import("./spend/index.js"),De=new st({controlPlane:E.store,store:D,alerts:ct});if(ke.apply!==!0){let Oe=D.budgetsForScope(ie.organizationId,ie.projectId,ie.environmentId);return M({ok:!0,applied:!1,preview:Oe.map((Ye)=>U.status(Ye))})}let tt=await De.run({...ie,environmentKind:Q?.kind});return M({ok:!0,applied:!0,result:tt})}if(H.pathname==="/api/spend/enforcement"&&L.method==="GET")return M({ok:!0,enforcement:q.listUnder({organizationId:ie.organizationId})});if(H.pathname==="/api/spend/release"&&L.method==="POST"){Ae();let ke=await me(L),st=q.close(String(ke.budgetId??""),ke.action);return M({ok:st,released:st,note:st?"The next spend cycle re-applies this if spend is still over the threshold.":"That action was not in force."})}if(H.pathname==="/api/spend/anomalies"&&L.method==="GET")return M({ok:!0,anomalies:D.listAnomalies({organizationId:ie.organizationId,projectId:ie.projectId,unacknowledgedOnly:H.searchParams.get("unacknowledged")==="true"})});if(H.pathname==="/api/spend/anomalies/acknowledge"&&L.method==="POST"){Ae();let ke=await me(L);return M({ok:!0,acknowledged:D.acknowledgeAnomaly(String(ke.id??""))})}return M({ok:!1,error:"Spend endpoint was not found."},404)}if(H.pathname.startsWith("/api/health/")||H.pathname.startsWith("/api/alerts")||H.pathname.startsWith("/api/notifications/")){let T=E.environments.get(xe),D=E.store.listResources(E.project.id,T?.id),U=Se.role==="admin"||!!T&&Ya(Se,T.id,"runtime:read")?D:D.filter((Q)=>rn(Se,Q.id,"runtime:read")),q=(Q)=>!Q||U.some((ie)=>ie.id===Q),G=(Q)=>{if(!Q)return;let ie=D.find((Ae)=>Ae.id===Q||Ae.slug===Q);if(!ie)throw Error("Alert resource was not found.");if(!q(ie.id))throw Error("Alert resource is outside your authorized scope.");return ie.id},X=Xe(Se).actor;if(H.pathname==="/api/health/checks"&&L.method==="GET")return M({ok:!0,checks:ct.listHealthChecks(E.project.id,T?.id).filter((Q)=>q(Q.resourceId)).map((Q)=>({...Q,results:ct.listHealthResults(Q.id,20)}))});if(H.pathname==="/api/health/checks"&&L.method==="POST"){let Q=await me(L),ie=String(Q.kind??"http");if(ie==="command"&&Se.role!=="admin")return M({ok:!1,error:"Command health checks require an administrator."},403);let Ae={method:String(Q.config?.method??"GET").toUpperCase().slice(0,10),path:Q.config?.path?String(Q.config.path).slice(0,500):void 0,port:Q.config?.port==null?void 0:Number(Q.config.port),command:Array.isArray(Q.config?.command)?Q.config.command.map(String).slice(0,20):void 0,expectedStatuses:Array.isArray(Q.config?.expectedStatuses)?Q.config.expectedStatuses.map(Number).filter(Number.isInteger).slice(0,20):void 0,expectedBody:Q.config?.expectedBody?String(Q.config.expectedBody).slice(0,500):void 0};return M({ok:!0,check:ct.createHealthCheck({projectId:E.project.id,environmentId:T?.id,resourceId:G(Q.resourceId??Q.resource),name:String(Q.name??""),kind:ie,target:String(Q.target??""),config:Ae,intervalSeconds:Number(Q.intervalSeconds)||60,timeoutSeconds:Number(Q.timeoutSeconds)||10,failureThreshold:Number(Q.failureThreshold)||3,recoveryThreshold:Number(Q.recoveryThreshold)||2,regions:Array.isArray(Q.regions)?Q.regions.map(String):["local"],enabled:Q.enabled!==!1})},201)}if(H.pathname==="/api/health/checks"&&L.method==="PATCH"){let Q=await me(L),ie=ct.getHealthCheck(String(Q.id??""));if(!ie||!q(ie.resourceId))return M({ok:!1,error:"Health check was not found."},404);return M({ok:!0,check:ct.setHealthCheckEnabled(ie.id,Q.enabled!==!1)})}if(H.pathname==="/api/health/checks/test"&&L.method==="POST"){let Q=await me(L),ie=ct.getHealthCheck(String(Q.id??""));if(!ie||!q(ie.resourceId))return M({ok:!1,error:"Health check was not found."},404);return M({ok:!0,productionIncidentCreated:!1,result:await new $e(ct).probe(ie)})}if(H.pathname==="/api/health/checks/run"&&L.method==="POST"){let Q=await me(L),ie=ct.getHealthCheck(String(Q.id??""));if(!ie||!q(ie.resourceId))return M({ok:!1,error:"Health check was not found."},404);let Ae=await new $e(ct).runAndEvaluate(ie),ke=[];for(let st of Ae.evaluations.filter((De)=>De.notify&&De.alert)){let De=Nn.enqueue(E.organization.id,st.alert,st.transition);ke.push(...await Nn.deliverAll(De))}return M({ok:!0,...Ae,deliveries:ke})}if(H.pathname==="/api/alerts/rules"&&L.method==="GET")return M({ok:!0,rules:ct.listRules(E.project.id,T?.id).filter((Q)=>q(Q.resourceId))});if(H.pathname==="/api/alerts/rules"&&L.method==="POST"){let Q=await me(L),ie=Q.healthCheckId?ct.getHealthCheck(String(Q.healthCheckId)):void 0;if(Q.healthCheckId&&(!ie||!q(ie.resourceId)))return M({ok:!1,error:"Health check was not found."},404);return M({ok:!0,rule:ct.createRule({projectId:E.project.id,environmentId:T?.id,resourceId:G(Q.resourceId??Q.resource)??ie?.resourceId,healthCheckId:ie?.id,name:String(Q.name??""),signal:String(Q.signal??(ie?"health":"")),operator:String(Q.operator??(ie?"unhealthy":"gt")),threshold:Q.threshold==null?void 0:Number(Q.threshold),recoveryThreshold:Q.recoveryThreshold==null?void 0:Number(Q.recoveryThreshold),windowMs:Number(Q.windowMs)||300000,consecutive:Number(Q.consecutive)||ie?.failureThreshold||3,recoveryConsecutive:Number(Q.recoveryConsecutive)||ie?.recoveryThreshold||2,noDataPolicy:["ignore","pending","firing"].includes(String(Q.noDataPolicy))?Q.noDataPolicy:"pending",severity:["info","warning","critical"].includes(String(Q.severity))?Q.severity:"warning",groupBy:Array.isArray(Q.groupBy)?Q.groupBy.map(String):[],labels:Q.labels&&typeof Q.labels==="object"?Q.labels:{},enabled:Q.enabled!==!1})},201)}if(H.pathname==="/api/alerts/rules"&&L.method==="PATCH"){let Q=await me(L),ie=ct.getRule(String(Q.id??""));if(!ie||!q(ie.resourceId))return M({ok:!1,error:"Alert rule was not found."},404);return M({ok:!0,rule:ct.setRuleEnabled(ie.id,Q.enabled!==!1)})}if(H.pathname==="/api/alerts/evaluate"&&L.method==="POST")return M({ok:!0,...await af(T?.id)});if(H.pathname==="/api/alerts"&&L.method==="GET")return M({ok:!0,alerts:ct.listAlerts(E.project.id,{environmentId:T?.id,states:H.searchParams.getAll("state").length?H.searchParams.getAll("state"):void 0,limit:Number(H.searchParams.get("limit"))||200}).filter((Q)=>q(Q.resourceId)).map((Q)=>({...Q,deliveries:ct.listDeliveries({alertId:Q.id,limit:50})}))});if(H.pathname==="/api/alerts/action"&&L.method==="POST"){let Q=await me(L),ie=ct.getAlert(String(Q.id??""));if(!ie||!q(ie.resourceId))return M({ok:!1,error:"Alert was not found."},404);let Ae=String(Q.action??"");if(Ae==="acknowledge")return M({ok:!0,alert:ct.acknowledge(ie.id,X?.id??"")});if(Ae==="assign")return M({ok:!0,alert:ct.assign(ie.id,Q.ownerActorId==="me"?X?.id:Q.ownerActorId?String(Q.ownerActorId):void 0,X?.id)});if(Ae==="silence"){if(Se.role!=="admin")return M({ok:!1,error:"Silencing production alerts requires an administrator."},403);let ke=gr.resolveSession(L);if(Ne&&(!ke||!Fe.isRecentlyAuthenticated(ke)))return M({ok:!1,error:"Recent authentication is required to silence alerts.",stepUpRequired:!0},401);return M({ok:!0,alert:ct.silenceAlert(ie.id,String(Q.until??""),X?.id)})}return M({ok:!1,error:"Action must be acknowledge, assign, or silence."},422)}if(H.pathname==="/api/alerts/silences"&&L.method==="POST"){let Q=await me(L);return M({ok:!0,id:ct.createSilence({projectId:E.project.id,environmentId:T?.id,resourceId:G(Q.resourceId??Q.resource),matcher:Q.matcher,reason:String(Q.reason??""),startsAt:String(Q.startsAt??new Date().toISOString()),endsAt:String(Q.endsAt??""),timezone:String(Q.timezone??"UTC"),actorId:X?.id})},201)}if(H.pathname==="/api/notifications/channels"&&L.method==="GET")return M({ok:!0,channels:ct.listChannels(E.organization.id)});if(H.pathname==="/api/notifications/channels"&&L.method==="POST"){let Q=await me(L),ie=String(Q.kind??""),Ae=ie==="email"?{to:Q.config?.to,from:Q.config?.from}:ie==="telegram"?{chatId:String(Q.config?.chatId??"")}:{};return M({ok:!0,channel:ct.createChannel({organizationId:E.organization.id,name:String(Q.name??""),kind:ie,config:Ae,credential:Q.credential?String(Q.credential):void 0,actorId:X?.id})},201)}if(H.pathname==="/api/notifications/channels"&&L.method==="PATCH"){let Q=await me(L),ie=String(Q.id??"");if(Q.credential)return M({ok:!0,channel:ct.rotateChannel(ie,String(Q.credential))});let Ae=String(Q.status??"paused");if(!["active","paused","disabled"].includes(Ae))return M({ok:!1,error:"Channel status must be active, paused, or disabled."},422);return M({ok:!0,channel:ct.setChannelStatus(ie,Ae)})}if(H.pathname==="/api/notifications/channels/test"&&L.method==="POST"){let Q=await me(L);return M({ok:!0,productionIncidentCreated:!1,result:await Nn.testChannel(String(Q.id??""))})}if(H.pathname==="/api/notifications/routes"&&L.method==="GET")return M({ok:!0,routes:ct.listRoutes(E.organization.id)});if(H.pathname==="/api/notifications/routes"&&L.method==="POST"){let Q=await me(L),ie=Array.isArray(Q.channelIds)?Q.channelIds.map(String):[];if(ie.some((Ae)=>ct.getChannel(Ae)?.organizationId!==E.organization.id))return M({ok:!1,error:"Route contains an unavailable channel."},422);return M({ok:!0,route:ct.createRoute({organizationId:E.organization.id,name:String(Q.name??""),priority:Number(Q.priority)||0,matcher:Q.matcher&&typeof Q.matcher==="object"?Q.matcher:{},channelIds:ie,quietHours:Q.quietHours,groupWaitSeconds:Number(Q.groupWaitSeconds)||30,reminderSeconds:Q.reminderSeconds==null?void 0:Number(Q.reminderSeconds),escalation:Array.isArray(Q.escalation)?Q.escalation:[],template:Q.template?String(Q.template):void 0,rateLimitPerMinute:Number(Q.rateLimitPerMinute)||60,enabled:Q.enabled!==!1})},201)}if(H.pathname==="/api/notifications/routes"&&L.method==="PATCH"){let Q=await me(L),ie=ct.getRoute(String(Q.id??""));if(!ie||ie.organizationId!==E.organization.id)return M({ok:!1,error:"Notification route was not found."},404);return M({ok:!0,route:ct.setRouteEnabled(ie.id,Q.enabled!==!1)})}if(H.pathname==="/api/notifications/routes/preview"&&L.method==="POST"){let Q=await me(L),Ae=(Q.alertId?ct.getAlert(String(Q.alertId)):void 0)??{id:"preview",ruleId:"preview",projectId:E.project.id,environmentId:T?.id,resourceId:G(Q.resourceId??Q.resource),dedupKey:"preview",groupKey:String(Q.groupKey??"default"),state:"firing",severity:["info","warning","critical"].includes(String(Q.severity))?Q.severity:"warning",title:String(Q.title??"Rule preview"),evidence:{},failureCount:1,recoveryCount:0,occurrenceCount:1,firstSeenAt:new Date().toISOString(),lastSeenAt:new Date().toISOString(),firingAt:new Date().toISOString(),updatedAt:new Date().toISOString()};return M({ok:!0,productionIncidentCreated:!1,matches:Nn.preview(E.organization.id,Ae,String(Q.eventType??"firing")).map((ke)=>({route:ke.route,channels:ke.channels,quiet:ke.quiet}))})}if(H.pathname==="/api/notifications/deliveries"&&L.method==="GET")return M({ok:!0,deliveries:ct.listDeliveries({alertId:H.searchParams.get("alertId")??void 0,states:H.searchParams.getAll("state"),limit:Number(H.searchParams.get("limit"))||200})});if(H.pathname==="/api/notifications/deliveries/retry"&&L.method==="POST"){let Q=await me(L),ie=ct.getDelivery(String(Q.id??""));if(!ie)return M({ok:!1,error:"Delivery was not found."},404);return ct.updateDelivery(ie.id,{state:"retrying",attempt:Math.max(0,ie.attempt-1),nextAttemptAt:new Date().toISOString()}),M({ok:!0,delivery:await Nn.deliver(ie.id)})}}if(H.pathname.startsWith("/api/jobs")||H.pathname.startsWith("/api/workers")){let T=E.environments.get(xe),D=E.store.listResources(E.project.id,T?.id),q=Se.role==="admin"||!!T&&Ya(Se,T.id,"automation:read")?D:D.filter((Oe)=>rn(Se,Oe.id,"automation:read")),G=(Oe)=>!Oe||q.some((Ye)=>Ye.id===Oe),X=Xe(Se).actor,Q=K.get(xe)??z,ie=Ou(h,Q),Ae=ie.filter((Oe)=>!Oe.danger&&["scheduler","backup","worker"].includes(Oe.group)),ke=O(h),st=()=>{let Oe=gr.resolveSession(L);return!Ne||!!Oe&&Fe.isRecentlyAuthenticated(Oe)},De=(Oe)=>{if(!Oe||typeof Oe!=="object"||Array.isArray(Oe))return{};let Ye=Object.entries(Oe).slice(0,50);if(Ye.some(([,vt])=>typeof vt!=="string"||!/^(?:secret|ssm|arn|env):\/\//.test(vt)))throw Error("Job payload values must be secret or environment references, never inline values.");return Object.fromEntries(Ye)},tt=(Oe,Ye,vt)=>E.store.appendEvent({organizationId:E.organization.id,projectId:E.project.id,resourceId:vt,actorId:X?.id,type:`job.${Oe}`,payload:Ye});if(H.pathname==="/api/jobs/preview"&&L.method==="POST"){let Oe=await me(L);try{return M({ok:!0,productionExecutionCreated:!1,preview:mt(String(Oe.expression??""),String(Oe.timezone??"UTC"),new Date(Oe.from?String(Oe.from):Date.now()),Number(Oe.count)||5)})}catch(Ye){return M({ok:!1,error:Ye instanceof Error?Ye.message:"Schedule preview could not be generated.",productionExecutionCreated:!1},422)}}if(H.pathname==="/api/jobs"&&L.method==="GET")return M({ok:!0,jobs:Zr.list(E.project.id,{environmentId:T?.id}).filter((Oe)=>G(Oe.resourceId)).map((Oe)=>({...Oe,preview:mt(Oe.normalizedExpression,Oe.timezone,new Date,5),capability:Hr(Oe),lastExecution:Zr.listExecutions(Oe.id,1)[0]}))});if(H.pathname==="/api/jobs"&&L.method==="POST"){let Oe=await me(L),Ye=String(Oe.provider??(ke==="serverless"?"eventbridge":"server")),vt=ke==="serverless"?"eventbridge":"server";if(Ye!==vt)return M({ok:!1,error:`${ke} environments accept ${vt} schedules through this adapter.`},422);let At=Oe.operationId?String(Oe.operationId):void 0;if(Ye==="server"&&(!At||!Ae.some((Ar)=>Ar.id===At)))return M({ok:!1,error:"Server jobs require an allowlisted operation target."},422);let Qt=Hr({provider:Ye,expression:String(Oe.expression??""),flexibleMinutes:Number(Oe.flexibleMinutes)||0,missedRunPolicy:Oe.missedRunPolicy==="catch_up"?"catch_up":"skip",overlapPolicy:["allow","forbid","replace"].includes(String(Oe.overlapPolicy))?Oe.overlapPolicy:"forbid"});if(!Qt.supported)return M({ok:!1,error:Qt.notes.join(" ")},422);let Tn=Oe.resourceId?D.find((Ar)=>Ar.id===Oe.resourceId||Ar.slug===Oe.resourceId):void 0;if(Oe.resourceId&&(!Tn||!G(Tn.id)))return M({ok:!1,error:"Job resource was not found."},404);try{let Ar=Ye==="server"?{kind:"dashboard_operation",operationId:At}:{kind:"serverless_scheduler",action:"run"},io=Zr.create({organizationId:E.organization.id,projectId:E.project.id,environmentId:T?.id,resourceId:Tn?.id,name:String(Oe.name??""),provider:Ye,expression:String(Oe.expression??""),timezone:String(Oe.timezone??"UTC"),startsAt:Oe.startsAt?String(Oe.startsAt):void 0,endsAt:Oe.endsAt?String(Oe.endsAt):void 0,flexibleMinutes:Number(Oe.flexibleMinutes)||0,target:Ar,payloadRefs:De(Oe.payloadRefs),missedRunPolicy:Oe.missedRunPolicy==="catch_up"?"catch_up":"skip",overlapPolicy:["allow","forbid","replace"].includes(String(Oe.overlapPolicy))?Oe.overlapPolicy:"forbid",retryPolicy:{maxAttempts:Number(Oe.maxAttempts)||3,backoffSeconds:Number(Oe.backoffSeconds)||30,deadLetterRef:Oe.deadLetterRef?String(Oe.deadLetterRef):void 0},timeoutSeconds:Number(Oe.timeoutSeconds)||900,enabled:Oe.enabled!==!1,origin:"managed",ownerActorId:X?.id,observedState:{},reconciliationStatus:"pending"});return tt("created",{jobId:io.id,provider:io.provider,expression:io.normalizedExpression},io.resourceId),M({ok:!0,job:io,capability:Qt},201)}catch(Ar){return M({ok:!1,error:Ar instanceof Error?Ar.message:"Scheduled job could not be created."},422)}}if(H.pathname==="/api/jobs"&&L.method==="PATCH"){let Oe=await me(L),Ye=Zr.get(String(Oe.id??""));if(!Ye||!G(Ye.resourceId))return M({ok:!1,error:"Scheduled job was not found."},404);if(Ye.origin==="config")return M({ok:!1,error:"Config-defined schedules must be edited in cloud.config.ts and reconciled."},409);let vt=Oe.missedRunPolicy==null?void 0:String(Oe.missedRunPolicy),At=Oe.overlapPolicy==null?void 0:String(Oe.overlapPolicy);if(vt&&!["skip","catch_up"].includes(vt))return M({ok:!1,error:"Missed-run policy must be skip or catch_up."},422);if(At&&!["allow","forbid","replace"].includes(At))return M({ok:!1,error:"Overlap policy must be allow, forbid, or replace."},422);try{let Qt=Zr.updateSchedule(Ye.id,{expression:Oe.expression?String(Oe.expression):void 0,timezone:Oe.timezone?String(Oe.timezone):void 0,missedRunPolicy:vt,overlapPolicy:At,timeoutSeconds:Oe.timeoutSeconds==null?void 0:Number(Oe.timeoutSeconds)});return tt("updated",{jobId:Qt.id,expression:Qt.normalizedExpression,version:Qt.version},Qt.resourceId),M({ok:!0,job:Qt})}catch(Qt){return M({ok:!1,error:Qt instanceof Error?Qt.message:"Scheduled job could not be updated."},422)}}if(H.pathname==="/api/jobs/action"&&L.method==="POST"){let Oe=await me(L),Ye=Zr.get(String(Oe.id??""));if(!Ye||!G(Ye.resourceId))return M({ok:!1,error:"Scheduled job was not found."},404);let vt=String(Oe.action??"");if(vt==="run"){let At=sf.enqueue(Ye,{trigger:"manual",actorId:X?.id});return tt("run_queued",{jobId:Ye.id,executionId:At.id},Ye.resourceId),M({ok:!0,execution:At},202)}if(["disable","delete"].includes(vt)&&T?.kind==="production"){if(String(Oe.confirm??"")!==vt)return M({ok:!1,error:`Type "${vt}" to confirm this production change.`},409);if(!st())return M({ok:!1,error:"Recent authentication is required for this production change.",stepUpRequired:!0},401)}if(vt==="enable"||vt==="disable"){let At=Zr.setEnabled(Ye.id,vt==="enable");return tt(vt==="enable"?"enabled":"disabled",{jobId:Ye.id},Ye.resourceId),M({ok:!0,job:At})}if(vt==="delete")try{return Zr.remove(Ye.id),tt("deleted",{jobId:Ye.id,name:Ye.name},Ye.resourceId),M({ok:!0,deleted:Ye.id})}catch(At){return M({ok:!1,error:At instanceof Error?At.message:"Scheduled job could not be deleted."},409)}return M({ok:!1,error:"Action must be run, enable, disable, or delete."},422)}if(H.pathname==="/api/jobs/history"&&L.method==="GET"){let Oe=Zr.get(String(H.searchParams.get("jobId")??""));if(!Oe||!G(Oe.resourceId))return M({ok:!1,error:"Scheduled job was not found."},404);return M({ok:!0,executions:Zr.listExecutions(Oe.id,Number(H.searchParams.get("limit"))||100).map((Ye)=>({...Ye,logs:Ye.operationId?nr.logs(Ye.operationId,{limit:500}):[]}))})}if(H.pathname==="/api/jobs/reconcile"&&L.method==="POST"){if(!T)return M({ok:!1,error:"Environment was not found."},404);let Oe=Do(Zr,h,{organization:E.organization,project:E.project,environment:T,resources:D});return tt("reconciled",Oe),M({ok:!0,reconciliation:Oe})}if(H.pathname==="/api/workers"&&L.method==="GET"){let Oe=Array.isArray(Q.workersDetail??Q.workers)?Q.workersDetail??Q.workers:[];return M({ok:!0,workers:Zr.listWorkers(E.project.id,T?.id).filter((Ye)=>G(Ye.resourceId)).map((Ye)=>{let vt=Oe.find((At)=>At.name===Ye.name||At.site===Ye.target.site);return vt?{...Ye,observedState:{...Ye.observedState,status:vt.status??"configured",currentJob:vt.currentJob??null,failures:Number(vt.failed??vt.failures)||0,processed:Number(vt.processed)||0}}:Ye})})}if(H.pathname==="/api/workers/action"&&L.method==="POST"){let Oe=await me(L),Ye=Zr.getWorker(String(Oe.id??""));if(!Ye||!G(Ye.resourceId))return M({ok:!1,error:"Worker was not found."},404);let vt=String(Oe.action??"");if(vt==="restart"){if(String(Oe.confirm??"")!==Ye.name)return M({ok:!1,error:`Type "${Ye.name}" to restart this worker.`},409);let At=ie.find((Ar)=>Ar.id===Ye.target.operationId);if(!At)return M({ok:!1,error:"The allowlisted worker restart operation is unavailable."},409);let Qt=await $u(h,xe,At),Tn=Zr.reconcileWorker(Ye.id,Qt.ok?"in_sync":"unavailable",{...Ye.observedState,lastRestartAt:new Date().toISOString(),command:Qt.command??null,error:Qt.error??Qt.stderr??null});return tt("worker_restarted",{workerId:Ye.id,ok:Qt.ok},Ye.resourceId),M({...Qt,worker:Tn},Qt.ok?200:409)}if(vt==="scale")return M({ok:!1,error:Ye.provider==="systemd"?"Systemd worker scale is config-managed; edit the worker definition and reconcile.":"This provider does not expose safe worker scaling through the current adapter."},422);return M({ok:!1,error:"Action must be restart or scale."},422)}}if(H.pathname.startsWith("/api/telemetry/")){let T=E.environments.get(xe),D=new ot(E.store),U=E.store.listResources(E.project.id,T?.id),q=["/api/telemetry/query","/api/telemetry/tail","/api/telemetry/export","/api/telemetry/saved-queries","/api/telemetry/refresh"].includes(H.pathname)?"runtime:logs":"runtime:read",G=Se.role==="admin"||!!T&&Ya(Se,T.id,q),X=G?U:U.filter((De)=>rn(Se,De.id,q)),Q=(De)=>vS(X,G,De),ie=(De)=>{let tt=new Date,Oe={"1h":3600000,"6h":21600000,"24h":86400000,"7d":604800000,"30d":2592000000},Ye=String(De.to??tt.toISOString()),vt=String(De.from??new Date(new Date(Ye).getTime()-(Oe[String(De.range??"1h")]??Oe["1h"])).toISOString()),At=(Tn)=>{let Ar=(Array.isArray(Tn)?Tn:String(Tn??"").split(",")).map(String).filter(Boolean);return Ar.length?Ar:void 0},Qt=At(De.kinds)?.filter((Tn)=>["metric","log","trace","request","event"].includes(Tn));return{projectId:E.project.id,environmentId:T?.id,resourceIds:Q(De.resources),kinds:Qt,names:At(De.names),sources:At(De.sources),levels:At(De.levels),from:vt,to:Ye,text:String(De.text??"")||void 0,traceId:String(De.traceId??"")||void 0,requestId:String(De.requestId??"")||void 0,deploymentId:String(De.deploymentId??"")||void 0,releaseId:String(De.releaseId??"")||void 0,workloadId:String(De.workloadId??"")||void 0,cursor:String(De.cursor??"")||void 0,limit:Number(De.limit)||void 0}},Ae=Object.fromEntries(H.searchParams.entries()),ke=(De=!1)=>Fg({controlPlane:E.store,projectId:E.project.id,environmentId:T?.id,config:h,environment:xe,force:De,lightweight:!De}),st=()=>{let De=E.store.getSetting(`telemetry.collection:${E.project.id}:${T?.id??"all"}`)??{};return{generatedAt:String(De.generatedAt??new Date().toISOString()),cached:!0,statuses:Array.isArray(De.statuses)?De.statuses:[],errors:Array.isArray(De.errors)?De.errors:[],policy:De.policy??$t(E.store,E.project.id)}};if(H.pathname==="/api/telemetry/status"&&L.method==="GET"){ke(!1);let De=st(),tt=G?De.statuses:D.status(E.project.id,T?.id,De.policy.rawDays,De.policy.samplingRate,X.map((Ye)=>Ye.id)),Oe=tt.reduce((Ye,vt)=>Ye+vt.estimatedDailyBytes*30,0);return M({ok:!0,generatedAt:De.generatedAt,cached:De.cached,sources:tt,errors:G?De.errors:[],policy:De.policy,retention:{rawDays:De.policy.rawDays,downsampleAfterDays:De.policy.downsampleAfterDays,downsampleBucketMs:De.policy.downsampleBucketMs,maxRecords:De.policy.maxRecords},estimatedMonthlyBytes:Oe,estimatedMonthlyCostUsd:Gc(Oe,De.policy),costEstimateConfigured:De.policy.estimatedStorageUsdPerGbMonth>0})}if(H.pathname==="/api/telemetry/settings"&&L.method==="GET")return M({ok:!0,policy:$t(E.store,E.project.id)});if(H.pathname==="/api/telemetry/settings"&&L.method==="PATCH"){let De=Hc(E.store,E.project.id,await me(L));return ES(E.project.id),M({ok:!0,policy:De})}if(H.pathname==="/api/telemetry/query"&&L.method==="GET"){ke(!1);let De=st();return M({ok:!0,collection:{generatedAt:De.generatedAt,cached:De.cached},...D.query(ie(Ae))})}if(H.pathname==="/api/telemetry/tail"&&L.method==="GET"){ke(!1);let De=new TextEncoder,tt=new Date,Oe=ie({...Ae,kinds:Ae.kinds||"log,request,event,trace",from:Ae.from||new Date(tt.getTime()-120000).toISOString(),to:tt.toISOString(),limit:Math.min(1000,Number(Ae.limit)||200),cursor:void 0}),Ye=L.headers.get("last-event-id")||String(Ae.cursor??"")||void 0,vt,At=!1,Qt=!1,Tn=new ReadableStream({start(Ar){let io=()=>{if(At)return;if(At=!0,vt)clearInterval(vt);L.signal.removeEventListener("abort",io);try{Ar.close()}catch{}},bf=async()=>{if(At||Qt)return;Qt=!0;try{let ws=D.tail({...Oe,to:new Date(Date.now()+1000).toISOString()},Ye);for(let Sf of ws.records)Ye=ai(Sf)??Ye,Ar.enqueue(De.encode(`id: ${Ye??""}
|
|
699
|
+
event: record
|
|
700
|
+
data: ${JSON.stringify(Sf)}
|
|
701
|
+
|
|
702
|
+
`));if(ws.cursor)Ye=ws.cursor}catch(ws){Ar.enqueue(De.encode(`event: error
|
|
703
|
+
data: ${JSON.stringify({message:ws instanceof Error?ws.message:String(ws)})}
|
|
704
|
+
|
|
705
|
+
`))}finally{Qt=!1}};bf(),vt=setInterval(()=>{if(bf(),!At)Ar.enqueue(De.encode(`event: heartbeat
|
|
706
|
+
data: ${JSON.stringify({cursor:Ye,at:new Date().toISOString()})}
|
|
707
|
+
|
|
708
|
+
`))},5000),vt.unref?.(),L.signal.addEventListener("abort",io,{once:!0})},cancel(){if(At=!0,vt)clearInterval(vt)}});return new Response(Tn,{headers:{"cache-control":"no-cache, no-transform",connection:"keep-alive","content-type":"text/event-stream; charset=utf-8","x-accel-buffering":"no"}})}if(H.pathname==="/api/telemetry/series"&&L.method==="GET"){ke(!1);let De=st(),tt=["sum","avg","min","max","count","p50","p90","p95","p99"].includes(String(Ae.aggregation))?Ae.aggregation:"avg",Oe=ie({...Ae,kinds:"metric",limit:5000}),Ye=D.series({...Oe,bucketMs:Number(Ae.bucketMs)||60000,aggregation:tt,timezone:String(Ae.timezone??"UTC"),compareFrom:Ae.compareFrom,compareTo:Ae.compareTo});return M({ok:!0,collection:{generatedAt:De.generatedAt,cached:De.cached},query:{aggregation:tt,bucketMs:Number(Ae.bucketMs)||60000,timezone:String(Ae.timezone??"UTC")},series:Ye})}if(H.pathname==="/api/telemetry/refresh"&&L.method==="POST")return M({ok:!0,...await ke(!0)});if(H.pathname==="/api/telemetry/export"&&L.method==="POST"){let De=await me(L);await ke(!1);let tt=D.query({...ie(De),limit:Math.min(5000,Number(De.limit)||5000),cursor:void 0});if((De.format==="csv"?"csv":"json")==="json")return new Response(JSON.stringify({exportedAt:new Date().toISOString(),query:ie(De),...tt},null,2),{headers:{"content-disposition":'attachment; filename="ts-cloud-telemetry.json"',"content-type":"application/json; charset=utf-8"}});let Ye=["timestamp","kind","source","name","level","value","unit","message","traceId","requestId","deploymentId","releaseId","workloadId"],vt=[Ye.join(","),...tt.records.map((At)=>Ye.map((Qt)=>`"${String(At[Qt]??"").replaceAll('"','""')}"`).join(","))].join(`
|
|
709
|
+
`);return new Response(vt,{headers:{"content-disposition":'attachment; filename="ts-cloud-telemetry.csv"',"content-type":"text/csv; charset=utf-8"}})}if(H.pathname==="/api/telemetry/saved-queries"&&L.method==="GET"){let De=Xe(Se);return M({ok:!0,queries:D.listSavedQueries(E.project.id,De.actor?.id)})}if(H.pathname==="/api/telemetry/saved-queries"&&L.method==="POST"){let De=await me(L),tt=Xe(Se);return M({ok:!0,query:D.saveQuery(E.project.id,tt.actor?.id,String(De.name??""),ie(De.query??De))},201)}if(H.pathname==="/api/telemetry/saved-queries"&&L.method==="DELETE"){let De=await me(L),tt=Xe(Se),Oe=D.deleteSavedQuery(E.project.id,tt.actor?.id,String(De.id??""));return M({ok:Oe},Oe?200:404)}}if(H.pathname.startsWith("/api/configuration")){let T=E.environments.get(xe),D=Se.role==="admin"||!!jn.capabilities?.includes("secrets:read"),U=Se.role==="admin"||!!jn.capabilities?.includes("secrets:write"),q={projectId:E.project.id,environmentId:T?.id},G={"cache-control":"no-store"};if(H.pathname==="/api/configuration"&&L.method==="GET"){let X=Object.fromEntries(H.searchParams),Q=X.scopeType||X.scopeId?Ka(X,q):void 0,ie=["variable","secret"].includes(String(X.kind))?X.kind:void 0,Ae=Q?.type==="preview"?wr.previews.getInstance(Q.id):void 0,ke=Ae?wr.previews.getDefinition(Ae.definitionId):void 0,st=Q?await Rn.resolve({projectId:E.project.id,environmentId:Q.environmentId,resourceId:Q.resourceId,functionId:Q.type==="function"?Q.id:void 0,previewId:Q.previewId,trustedPreview:!!Ae&&!Ae.fork,allowedPreviewSecrets:ke?.inheritedSecrets,nativeReferences:!0,canReadSecretMetadata:D}):void 0,De=E.store.listResources(E.project.id);return M({ok:!0,entries:Rn.list({projectId:E.project.id,scope:Q,kind:ie,search:X.search,canReadSecretMetadata:D}),effectiveEntries:st?Object.values(st.entries).filter((tt)=>!ie||tt.kind===ie).filter((tt)=>!X.search||tt.key.toLowerCase().includes(String(X.search).toLowerCase())):[],configurationHash:st?.configurationHash,warnings:st?.warnings??[],scopes:{project:E.project,environments:E.store.listEnvironments(E.project.id),resources:De.filter((tt)=>tt.kind!=="function"),functions:De.filter((tt)=>tt.kind==="function"),previews:wr.previews.listInstances({projectId:E.project.id}).filter((tt)=>tt.status!=="destroyed").map((tt)=>({id:tt.id,name:tt.name,environmentId:tt.baseEnvironmentId,resourceId:tt.resourceId,fork:tt.fork,status:tt.status}))}},200,G)}if(H.pathname==="/api/configuration/export"&&L.method==="GET"){let X=Ka(Object.fromEntries(H.searchParams),q);return new Response(Rn.exportVariables({projectId:E.project.id,scope:X}),{headers:{...G,"content-disposition":'attachment; filename="ts-cloud.env"',"content-type":"text/plain; charset=utf-8"}})}if(H.pathname==="/api/configuration/plan"&&L.method==="POST"){let X=await me(L),Q=Ka(X,q),ie=X.values&&typeof X.values==="object"&&!Array.isArray(X.values)?Object.fromEntries(Object.entries(X.values).map(([Ae,ke])=>[Ae,String(ke)])):{};return M({ok:!0,plan:Rn.plan({projectId:E.project.id,scope:Q,values:ie,removeMissing:X.removeMissing===!0})},200,G)}if(H.pathname==="/api/configuration"&&L.method==="POST"){let X=await me(L),Q=Ka(X,q),ie=X.kind==="secret"?"secret":"variable",Ae=String(X.key??"");if(ie==="secret"&&!U)return M({ok:!1,error:"Secret write permission is required."},403,G);try{let ke=await Rn.set({organizationId:E.organization.id,projectId:E.project.id,scope:Q,key:Ae,kind:ie,value:X.value==null?void 0:String(X.value),reference:X.reference==null?void 0:String(X.reference),backend:X.backend,required:X.required===!0,expectedVersion:X.expectedVersion==null?void 0:Number(X.expectedVersion),confirmed:String(X.confirm??"")===Ae,idempotencyKey:L.headers.get("idempotency-key")??X.idempotencyKey,actorId:Xe(Se).actor?.id});return M({ok:!0,entry:Rn.list({projectId:E.project.id,scope:Q,canReadSecretMetadata:D}).find((st)=>st.id===ke.entry.id),mutation:ke.mutation,warnings:ke.warnings},ke.mutation.added.length?201:200,G)}catch(ke){return M({ok:!1,error:ke instanceof Error?ke.message:String(ke)},409,G)}}if(H.pathname==="/api/configuration/import"&&L.method==="POST"){let X=await me(L),Q=Ka(X,q),ie=Array.isArray(X.secretKeys)?X.secretKeys.map(String):[];if(ie.length&&!U)return M({ok:!1,error:"Secret write permission is required."},403,G);try{let Ae=await Rn.importDotenv({organizationId:E.organization.id,projectId:E.project.id,scope:Q,source:String(X.source??""),secretKeys:ie,backend:X.backend,confirmed:X.confirm==="import",idempotencyKey:L.headers.get("idempotency-key")??X.idempotencyKey,actorId:Xe(Se).actor?.id});return M({ok:Ae.document.valid,diagnostics:Ae.document.diagnostics,mutation:Ae.mutation,warnings:Ae.warnings},Ae.document.valid?200:422,G)}catch(Ae){return M({ok:!1,error:Ae instanceof Error?Ae.message:String(Ae)},409,G)}}if(H.pathname==="/api/configuration/dependencies"&&L.method==="POST"){let X=await me(L),Q=Xi.get(String(X.entryId??"")),ie=E.store.getResource(String(X.resourceId??""));if(!Q||Q.projectId!==E.project.id||!ie||ie.projectId!==E.project.id)return M({ok:!1,error:"Configuration entry or dependent resource was not found."},404,G);return M({ok:!0,dependency:Xi.setDependency({entryId:Q.id,resourceId:ie.id,injectionTarget:["native_reference","file"].includes(String(X.injectionTarget))?X.injectionTarget:"environment",required:X.required!==!1,requiresRedeploy:X.requiresRedeploy!==!1,lastDeployedVersion:X.lastDeployedVersion==null?void 0:Number(X.lastDeployedVersion)})},200,G)}if(H.pathname==="/api/configuration/transfer"&&L.method==="POST"){let X=await me(L),Q=Xi.get(String(X.id??"")),ie=Ka({...X,scopeType:X.targetScopeType,scopeId:X.targetScopeId},q);if(!Q||Q.projectId!==E.project.id)return M({ok:!1,error:"Configuration entry was not found."},404,G);if(Q.kind==="secret"&&!U)return M({ok:!1,error:"Secret write permission is required."},403,G);try{let Ae=await Rn.transfer({entryId:Q.id,targetScope:ie,mode:X.mode==="move"?"move":"copy",confirmed:X.confirm===Q.key,actorId:Xe(Se).actor?.id,idempotencyKey:L.headers.get("idempotency-key")??X.idempotencyKey});return M({ok:!0,entry:Rn.list({projectId:E.project.id,scope:ie,canReadSecretMetadata:D}).find((ke)=>ke.id===Ae.entry.id),mutation:Ae.mutation},200,G)}catch(Ae){return M({ok:!1,error:Ae instanceof Error?Ae.message:String(Ae)},409,G)}}if(H.pathname==="/api/configuration/bulk"&&L.method==="POST"){let X=await me(L),Q=Array.isArray(X.ids)?X.ids.map(String).slice(0,500):[],ie=Q.map((ke)=>Xi.get(ke)).filter((ke)=>!!ke&&ke.projectId===E.project.id);if(ie.length!==Q.length)return M({ok:!1,error:"One or more configuration entries were not found."},404,G);if(ie.some((ke)=>ke.kind==="secret")&&!U)return M({ok:!1,error:"Secret write permission is required."},403,G);if(X.action!=="delete"||X.confirm!=="bulk")return M({ok:!1,error:"Bulk deletion requires action delete and confirm bulk."},409,G);let Ae=[];try{for(let ke of ie)Ae.push(await Rn.remove({entryId:ke.id,expectedVersion:ke.version,confirmed:!0,actorId:Xe(Se).actor?.id}));return M({ok:!0,removed:Ae.flatMap((ke)=>ke.removed),affectedResourceIds:[...new Set(Ae.flatMap((ke)=>ke.affectedResourceIds))].sort()},200,G)}catch(ke){return M({ok:!1,error:ke instanceof Error?ke.message:String(ke),partial:Ae.flatMap((st)=>st.removed)},409,G)}}if(H.pathname==="/api/configuration/reveal"&&L.method==="POST"){let X=await me(L),Q=gr.resolveSession(L);try{return M({ok:!0,value:await Rn.reveal({entryId:String(X.id??""),canRevealSecrets:D,recentlyAuthenticated:!Ne||!!Q&&Fe.isRecentlyAuthenticated(Q),actorId:Xe(Se).actor?.id})},200,G)}catch(ie){return M({ok:!1,error:ie instanceof Error?ie.message:String(ie)},403,G)}}if(H.pathname==="/api/configuration"&&L.method==="DELETE"){let X=await me(L),Q=Xi.get(String(X.id??""));if(Q?.kind==="secret"&&!U)return M({ok:!1,error:"Secret write permission is required."},403,G);if(!Q||Q.projectId!==E.project.id)return M({ok:!1,error:"Configuration entry was not found."},404,G);try{return M({ok:!0,mutation:await Rn.remove({entryId:Q.id,expectedVersion:Number(X.expectedVersion),confirmed:String(X.confirm??"")===Q.key,actorId:Xe(Se).actor?.id})},200,G)}catch(ie){return M({ok:!1,error:ie instanceof Error?ie.message:String(ie)},409,G)}}}if(H.pathname.startsWith("/api/fleet")){let T=Xe(Se).actor?.id,D=(U,q=409)=>M({ok:!1,error:U instanceof Error?U.message:String(U)},q,{"cache-control":"no-store"});if(H.pathname==="/api/fleet"&&L.method==="GET")return M({ok:!0,servers:Qa.list(E.project.id),resources:E.store.listResources(E.project.id),operations:nr.list({projectId:E.project.id,limit:200}).filter((U)=>U.operation.kind.startsWith("fleet."))},200,{"cache-control":"no-store"});if(H.pathname==="/api/fleet"&&L.method==="POST"){let U=await me(L);try{return M({ok:!0,server:ti.enroll({organizationId:E.organization.id,projectId:E.project.id,name:String(U.name??""),provider:["aws","hetzner"].includes(String(U.provider))?U.provider:"ssh",providerId:U.providerId?String(U.providerId):void 0,region:U.region?String(U.region):void 0,zone:U.zone?String(U.zone):void 0,endpoint:String(U.endpoint??""),sshUser:String(U.sshUser??"deploy"),sshPort:U.sshPort?Number(U.sshPort):22,credentialRef:String(U.credentialRef??""),roles:Array.isArray(U.roles)?U.roles:[],labels:U.labels&&typeof U.labels==="object"?U.labels:{}})},201)}catch(q){return D(q,422)}}if(H.pathname==="/api/fleet/action"&&L.method==="POST"){let U=await me(L),q=String(U.id??""),G=String(U.action??"");try{if(G==="test")return M({ok:!0,server:await ti.test(q)});if(G==="validate")return M({ok:!0,server:await ti.validate(q)});if(G==="review_host_key")return M({ok:!0,server:ti.reviewHostKey(q,String(U.fingerprint??""))});if(G==="bootstrap")return M({ok:!0,...ti.bootstrap(q,U.confirmed===!0)},U.confirmed===!0?202:200);if(G==="heartbeat")return M({ok:!0,server:ti.heartbeat(q,{usage:U.usage&&typeof U.usage==="object"?U.usage:{}})});if(G==="drain")return M({ok:!0,server:ti.drain(q,U.completed===!0)});if(G==="uncordon")return M({ok:!0,server:ti.uncordon(q)});if(G==="archive")return M({ok:!0,server:ti.archive(q,String(U.confirmation??""))});return D("Unsupported fleet action.",422)}catch(X){return D(X)}}}if(H.pathname.startsWith("/api/capacity")){let T=(D,U=409)=>M({ok:!1,error:D instanceof Error?D.message:String(D)},U,{"cache-control":"no-store"});if(H.pathname==="/api/capacity"&&L.method==="GET")return M({ok:!0,pools:to.listPools(E.project.id),placements:to.listPlacements(E.project.id),servers:Qa.list(E.project.id)},200,{"cache-control":"no-store"});if(H.pathname==="/api/capacity"&&L.method==="POST"){let D=await me(L);try{return M({ok:!0,pool:to.createPool({organizationId:E.organization.id,projectId:E.project.id,name:String(D.name??""),purpose:String(D.purpose??"application"),backend:String(D.backend??"server"),region:D.region?String(D.region):void 0,architecture:D.architecture?String(D.architecture):void 0,labels:D.labels&&typeof D.labels==="object"?D.labels:{},requiredServerLabels:D.requiredServerLabels&&typeof D.requiredServerLabels==="object"?D.requiredServerLabels:{},toleratedTaints:Array.isArray(D.toleratedTaints)?D.toleratedTaints.map(String):[],capacity:D.capacity&&typeof D.capacity==="object"?D.capacity:{},maxWorkloads:Number(D.maxWorkloads??10),costWeight:Number(D.costWeight??1),spreadKey:D.spreadKey?String(D.spreadKey):void 0,concurrency:Number(D.concurrency??1),ephemeralWorkspaces:!0,allowProductionSecrets:!1,status:"active"})},201)}catch(U){return T(U,422)}}if(H.pathname==="/api/capacity/member"&&L.method==="POST"){let D=await me(L);try{return to.addMember(String(D.poolId??""),String(D.serverId??"")),M({ok:!0,pool:to.getPool(String(D.poolId??""))})}catch(U){return T(U,422)}}if(H.pathname==="/api/capacity/explain"&&L.method==="POST"){let D=await me(L);try{return M({ok:!0,decisions:rf.explain(E.project.id,{purpose:String(D.purpose??"application"),resources:D.resources&&typeof D.resources==="object"?D.resources:{},region:D.region?String(D.region):void 0,architecture:D.architecture?String(D.architecture):void 0,labels:D.labels&&typeof D.labels==="object"?D.labels:{}})})}catch(U){return T(U,422)}}if(H.pathname==="/api/capacity/action"&&L.method==="POST"){let D=await me(L);try{if(D.action==="drain")return M({ok:!0,...rf.drainPool(String(D.poolId??""))});if(D.action==="enable")return M({ok:!0,pool:to.updatePool(String(D.poolId??""),{status:"active"})});if(D.action==="disable")return M({ok:!0,pool:to.updatePool(String(D.poolId??""),{status:"disabled"})});return T("Unsupported capacity action.",422)}catch(U){return T(U,422)}}}if(H.pathname.startsWith("/api/regions")){let T=(D,U=409)=>M({ok:!1,error:D instanceof Error?D.message:String(D)},U,{"cache-control":"no-store"});if(H.pathname==="/api/regions"&&L.method==="GET")return M({ok:!0,topologies:ys.list(E.project.id).map((D)=>({topology:D,targets:ys.targets(D.id),channels:ys.channels(D.id),route:ys.route(D.id),executions:ys.executions(D.id)}))},200,{"cache-control":"no-store"});if(H.pathname==="/api/regions"&&L.method==="POST"){let D=await me(L);try{return M({ok:!0,topology:nf.create({organizationId:E.organization.id,projectId:E.project.id,environmentId:E.environments.get(xe)?.id,name:String(D.name??""),hostname:String(D.hostname??""),regions:Array.isArray(D.regions)?D.regions:[],trafficPolicy:D.trafficPolicy==="weighted"||D.trafficPolicy==="latency"?D.trafficPolicy:"active_passive",dataPolicy:{replicate:Array.isArray(D.replicate)?D.replicate:["s3","dynamodb","secrets"],maxLagSeconds:Number(D.maxLagSeconds??30),retainOnDestroy:D.retainOnDestroy!==!1},dnsProvider:"route53",cdnEnabled:D.cdnEnabled===!0,wafEnabled:D.wafEnabled===!0})},201)}catch(U){return T(U,422)}}if(H.pathname==="/api/regions/action"&&L.method==="POST"){let D=await me(L);try{return M({ok:!0,...nf.enqueue({topologyId:String(D.topologyId??""),kind:String(D.action??""),targetRegion:D.targetRegion?String(D.targetRegion):void 0,revision:D.revision?String(D.revision):void 0,manifest:D.manifest&&typeof D.manifest==="object"?D.manifest:{},confirmation:D.confirmation?String(D.confirmation):void 0,deleteData:D.deleteData===!0})},202)}catch(U){return T(U,422)}}}if(H.pathname.startsWith("/api/maintenance")){let T=(D,U=409)=>M({ok:!1,error:D instanceof Error?D.message:String(D)},U,{"cache-control":"no-store"});if(H.pathname==="/api/maintenance"&&L.method==="GET")return M({ok:!0,manifests:fi.manifests(E.organization.id),windows:fi.windows(E.project.id),campaigns:fi.campaigns(E.project.id).map((D)=>({campaign:D,targets:fi.targets(D.id)})),cleanups:fi.cleanups(E.project.id),drills:fi.drills(E.project.id)},200,{"cache-control":"no-store"});if(H.pathname==="/api/maintenance/cleanup-preview"&&L.method==="POST"){let D=await me(L);try{return M({ok:!0,plan:await Vu.previewCleanup({projectId:E.project.id,kind:String(D.kind??"operations"),criteria:{olderThanDays:Number(D.olderThanDays??30)}},of)},201)}catch(U){return T(U,422)}}if(H.pathname==="/api/maintenance/cleanup-apply"&&L.method==="POST"){let D=await me(L);try{return M({ok:!0,...Vu.enqueueCleanup(String(D.planId??""),String(D.confirmation??""))},202)}catch(U){return T(U,422)}}if(H.pathname==="/api/maintenance/drill"&&L.method==="POST"){let D=await me(L);try{return M({ok:!0,drill:fi.createDrill({projectId:E.project.id,scenario:String(D.scenario??"control_plane"),isolatedTarget:String(D.isolatedTarget??""),expectedRpoMinutes:Number(D.expectedRpoMinutes??60),expectedRtoMinutes:Number(D.expectedRtoMinutes??30),recoveryPointId:D.recoveryPointId?String(D.recoveryPointId):void 0,topologyId:D.topologyId?String(D.topologyId):void 0})},201)}catch(U){return T(U,422)}}if(H.pathname==="/api/maintenance/drill-run"&&L.method==="POST"){let D=await me(L);try{return M({ok:!0,...Vu.enqueueDrill(String(D.drillId??""))},202)}catch(U){return T(U,422)}}}if(H.pathname.startsWith("/api/volumes")){let T=E.environments.get(xe),D=Xe(Se).actor?.id,U=(q,G=409)=>M({ok:!1,error:q instanceof Error?q.message:String(q)},G,{"cache-control":"no-store"});if(H.pathname==="/api/volumes"&&L.method==="GET"){let q=Uu.inventory(E.project.id,T?.id),G=E.store.listResources(E.project.id,T?.id).map((X)=>({id:X.id,name:X.name,slug:X.slug,kind:X.kind,provider:X.provider,providerId:X.providerId}));return M({ok:!0,volumes:q,resources:G,operations:nr.list({projectId:E.project.id,limit:100}).filter((X)=>X.operation.kind.startsWith("volume.")),recoveryPoints:Er.listRecoveryPoints(E.project.id).filter((X)=>X.kind==="volume"),providers:qu.map((X)=>({provider:X.provider,type:X.type,capabilities:X.capabilities()}))},200,{"cache-control":"no-store"})}if(H.pathname==="/api/volumes"&&L.method==="POST"){let q=await me(L);try{return M({ok:!0,...gi.create({organizationId:E.organization.id,projectId:E.project.id,environmentId:T?.id,resourceId:q.resourceId?String(q.resourceId):void 0,name:String(q.name??""),provider:String(q.provider??"docker"),type:q.type==="docker"?"docker":q.type,capacityBytes:q.capacityBytes==null?void 0:Number(q.capacityBytes),filesystem:q.filesystem?String(q.filesystem):void 0,encrypted:q.encrypted===!0,desiredState:q.desiredState&&typeof q.desiredState==="object"?q.desiredState:{},actorId:D})},202,{"cache-control":"no-store"})}catch(G){return U(G,422)}}if(H.pathname==="/api/volumes/attach"&&L.method==="POST"){let q=await me(L);try{return M({ok:!0,...gi.attach(String(q.volumeId??""),{resourceId:String(q.resourceId??""),targetPath:String(q.targetPath??""),readOnly:q.readOnly===!0,uid:q.uid==null?void 0:Number(q.uid),gid:q.gid==null?void 0:Number(q.gid),mode:q.mode?String(q.mode):void 0,propagation:q.propagation,driverOptions:q.driverOptions&&typeof q.driverOptions==="object"?q.driverOptions:{},actorId:D})},202)}catch(G){return U(G,422)}}if(H.pathname==="/api/volumes/detach"&&L.method==="POST"){let q=await me(L);try{return M({ok:!0,...gi.detach(String(q.volumeId??""),String(q.attachmentId??""),{force:q.force===!0,drained:q.drained===!0,unmounted:q.unmounted===!0,confirmation:q.confirmation?String(q.confirmation):void 0,actorId:D})},202)}catch(G){return U(G)}}if(H.pathname==="/api/volumes/resize"&&L.method==="POST"){let q=await me(L);try{return M({ok:!0,...gi.resize(String(q.volumeId??""),Number(q.capacityBytes),{drained:q.drained===!0,actorId:D})},202)}catch(G){return U(G,422)}}if(H.pathname==="/api/volumes/snapshot"&&L.method==="POST"){let q=await me(L);try{return M({ok:!0,...gi.snapshot(String(q.volumeId??""),{name:q.name?String(q.name):void 0,actorId:D})},202)}catch(G){return U(G,422)}}if(H.pathname==="/api/volumes/restore"&&L.method==="POST"){let q=await me(L);try{return M({ok:!0,...gi.restore(String(q.snapshotId??""),{name:String(q.name??""),actorId:D})},202)}catch(G){return U(G,422)}}if(H.pathname==="/api/volumes/discover"&&L.method==="POST")try{return M({ok:!0,volumes:await gi.discover({organizationId:E.organization.id,projectId:E.project.id,environmentId:T?.id,actorId:D})})}catch(q){return U(q,502)}if(H.pathname==="/api/volumes/adopt"&&L.method==="POST"){let q=await me(L);try{return M({ok:!0,volume:gi.adopt(String(q.volumeId??""),{name:String(q.name??""),actorId:D})})}catch(G){return U(G,422)}}if(H.pathname==="/api/volumes"&&L.method==="DELETE"){let q=await me(L),G=gr.resolveSession(L);try{return M({ok:!0,...gi.delete(String(q.volumeId??""),{recentAuthAt:!Ne||G&&Fe.isRecentlyAuthenticated(G)?new Date().toISOString():void 0,confirmation:q.confirmation?String(q.confirmation):void 0,backupOverride:q.backupOverride===!0,backupOverrideConfirmation:q.backupOverrideConfirmation?String(q.backupOverrideConfirmation):void 0,actorId:D})},202)}catch(X){return U(X)}}}if(H.pathname==="/api/backups"&&L.method==="GET"){let T=E.environments.get(xe),D=Er.listPolicies(E.project.id,T?.id),U=new Set(D.map((ie)=>ie.id)),q=Er.listRecoveryPoints(E.project.id).filter((ie)=>!ie.policyId||U.has(ie.policyId)),G=Er.listJobs(E.project.id).filter((ie)=>!ie.policyId||U.has(ie.policyId)),X=(ie)=>({...ie,credentialRef:void 0,encryptionKeyRef:void 0,credentialsConfigured:!!ie.credentialRef,clientEncryptionConfigured:!!ie.encryptionKeyRef}),Q=await Promise.all(Er.listDestinations(E.project.id).map(async(ie)=>({...X(ie),credentialStatus:await xl(ie,mi)})));return M({ok:!0,destinations:Q,policies:D,recoveryPoints:q,jobs:G,coverage:Er.coverage(E.project.id).filter((ie)=>U.has(ie.policy.id)),resources:T?E.store.listResources(E.project.id,T.id).map((ie)=>({id:ie.id,name:ie.name,slug:ie.slug,kind:ie.kind})):[]})}if(H.pathname==="/api/backups/destinations"&&L.method==="POST"){let T=await me(L),D=String(T.name??"").trim(),U=String(T.provider??""),q=String(T.encryption??"provider"),G=`secret://data-services/backups/${E.project.id}/${D}`,X=[];if(!["aws_s3","s3_compatible","aws_backup"].includes(U))return M({ok:!1,error:"A supported backup destination provider is required."},422);if(!["provider","client_side","both"].includes(q))return M({ok:!1,error:"Encryption must be provider, client_side, or both."},422);try{let Q,ie;if(T.credentials&&typeof T.credentials==="object"&&!Array.isArray(T.credentials))Q=`${G}/credentials`,await mi.put(Q,JSON.stringify(T.credentials)),X.push(Q);if(q!=="provider"){let ke=String(T.encryptionKey??"");if(ke.length<32)throw Error("Client-side encryption keys must contain at least 32 characters.");ie=`${G}/encryption-key`,await mi.put(ie,ke),X.push(ie)}let Ae=Er.createDestination({organizationId:E.organization.id,projectId:E.project.id,name:D,provider:U,endpoint:T.endpoint?String(T.endpoint):void 0,endpointPolicy:T.endpointPolicy==="allow_private"?"allow_private":"public_https",bucket:T.bucket?String(T.bucket):void 0,prefix:String(T.prefix??""),region:T.region?String(T.region):eo,forcePathStyle:T.forcePathStyle===!0,credentialRef:Q,encryption:q,encryptionKeyRef:ie,immutability:T.immutability&&typeof T.immutability==="object"&&!Array.isArray(T.immutability)?T.immutability:{},status:"untested"});return M({ok:!0,destination:{...Ae,credentialRef:void 0,encryptionKeyRef:void 0,credentialsConfigured:!!Q,clientEncryptionConfigured:!!ie}},201)}catch(Q){for(let ie of X)await mi.remove(ie).catch(()=>{});return M({ok:!1,error:Q instanceof Error?Q.message:String(Q)},422)}}if(H.pathname==="/api/backups/destinations/test"&&L.method==="POST"){let T=await me(L),D=Er.getDestination(String(T.id??""));if(!D||D.projectId!==E.project.id)return M({ok:!1,error:"Backup destination was not found."},404);if(D.provider==="aws_backup")return M({ok:!1,error:"AWS Backup destinations are verified by creating a provider recovery point."},422);try{return await tf.test(D),M({ok:!0,destination:Er.recordDestinationTest(D.id,{ok:!0})})}catch(U){let q=U instanceof Error?U.message:String(U);return Er.recordDestinationTest(D.id,{ok:!1,error:q}),M({ok:!1,error:q},502)}}if(H.pathname==="/api/backups/policies"&&L.method==="POST"){let T=await me(L),D=E.environments.get(xe),U=Er.getDestination(String(T.destinationId??"")),q=String(T.resourceKind??""),G=T.dataServiceId?String(T.dataServiceId):void 0,X=G?lr.get(G):void 0,Q=T.resourceId?String(T.resourceId):void 0,ie=Q?E.store.getResource(Q):void 0;if(!D)return M({ok:!1,error:"Environment was not found."},404);if(!U||U.projectId!==E.project.id)return M({ok:!1,error:"Backup destination was not found."},404);if(!["managed_database","logical_database","volume","files","control_plane","infrastructure"].includes(q))return M({ok:!1,error:"A supported backup resource kind is required."},422);if(G&&(!X||X.projectId!==E.project.id||X.environmentId!==D.id))return M({ok:!1,error:"Data service was not found in this environment."},404);if(Q&&(!ie||ie.projectId!==E.project.id||ie.environmentId!==D.id))return M({ok:!1,error:"Resource was not found in this environment."},404);if(q==="managed_database"&&!X)return M({ok:!1,error:"Managed database policies require a data service."},422);if(q==="logical_database"&&(!X||!["container","server"].includes(X.provider)))return M({ok:!1,error:"Logical database policies require an on-box data service."},422);if(q==="volume"&&(!ie||!/^[A-Za-z0-9][A-Za-z0-9_.-]{1,127}$/.test(String(T.volumeName??""))))return M({ok:!1,error:"Volume policies require a scoped resource and valid volumeName."},422);let Ae=Array.isArray(T.includePatterns)?T.includePatterns.map(String).filter(Boolean):[];if(q==="files"&&(!ie||!Ae.length))return M({ok:!1,error:"File policies require a scoped resource and at least one project-relative include path."},422);if(q==="control_plane"&&U.encryption==="provider")return M({ok:!1,error:"Control-plane policies require a destination with client-side encryption."},422);if(q==="infrastructure"&&(U.provider!=="aws_backup"||!Ae.some((ke)=>ke.startsWith("resource:arn:"))||!Ae.some((ke)=>ke.startsWith("role:arn:"))))return M({ok:!1,error:"Infrastructure policies require an AWS Backup destination plus resource:<arn> and role:<arn> include values."},422);try{let ke=Er.createPolicy({organizationId:E.organization.id,projectId:E.project.id,environmentId:D.id,resourceId:Q,dataServiceId:G,destinationId:U.id,name:String(T.name??"").trim(),resourceKind:q,schedule:String(T.schedule??"daily"),timezone:String(T.timezone??"UTC"),retention:T.retention&&typeof T.retention==="object"&&!Array.isArray(T.retention)?T.retention:{keepLast:7,expireAfterDays:30},compression:["none","gzip","zstd"].includes(String(T.compression))?T.compression:"gzip",encryption:["destination","client_side","both"].includes(String(T.encryption))?T.encryption:"destination",includePatterns:q==="volume"?[String(T.volumeName),...Ae]:Ae,excludePatterns:Array.isArray(T.excludePatterns)?T.excludePatterns.map(String):[],expectedRpoMinutes:Math.max(1,Number(T.expectedRpoMinutes)||1440),expectedRtoMinutes:Math.max(1,Number(T.expectedRtoMinutes)||120),healthCheckId:T.healthCheckId?String(T.healthCheckId):void 0,enabled:T.enabled!==!1});return M({ok:!0,policy:ke},201)}catch(ke){return M({ok:!1,error:ke instanceof Error?ke.message:String(ke)},422)}}if(H.pathname==="/api/backups/run"&&L.method==="POST"){let T=await me(L),D=Er.getPolicy(String(T.policyId??"")),U=E.environments.get(xe);if(!D||D.projectId!==E.project.id||D.environmentId!==U?.id)return M({ok:!1,error:"Backup policy was not found in this environment."},404);try{let q=hs.enqueueBackup(D,new Date().toISOString(),Xe(Se).actor?.id);return M({ok:!0,job:q},202)}catch(q){return M({ok:!1,error:q instanceof Error?q.message:String(q)},409)}}if(H.pathname==="/api/backups/verify"&&L.method==="POST"){let T=await me(L),D=Er.getRecoveryPoint(String(T.recoveryPointId??""));if(!D||D.projectId!==E.project.id)return M({ok:!1,error:"Recovery point was not found."},404);return M({ok:!0,job:hs.enqueueVerification(D)},202)}if(H.pathname==="/api/backups/restore"&&L.method==="POST"){let T=await me(L),D=Er.getRecoveryPoint(String(T.recoveryPointId??"")),U=T.mode==="in_place"?"in_place":"isolated",q=T.target&&typeof T.target==="object"&&!Array.isArray(T.target)?{...T.target,inPlace:U==="in_place"}:{inPlace:U==="in_place"},G=gr.resolveSession(L),X={mode:U,target:q,targetName:String(T.targetName??""),confirm:T.confirm?String(T.confirm):void 0,recentAuth:!Ne||!!G&&Fe.isRecentlyAuthenticated(G),downtimeAcknowledged:T.downtimeAcknowledged===!0,safetyBackupId:T.safetyBackupId?String(T.safetyBackupId):void 0};if(!D||D.projectId!==E.project.id)return M({ok:!1,error:"Recovery point was not found."},404);if(T.drill===!0&&U!=="isolated")return M({ok:!1,error:"Recovery drills must use an isolated target."},422);try{let Q=hs.planRestore(D,X);if(T.execute!==!0)return M({ok:!0,plan:Q,executionCreated:!1});let ie=hs.enqueueRestore(D,{...X,drill:T.drill===!0,actorId:Xe(Se).actor?.id});return M({ok:!0,plan:Q,job:ie},202)}catch(Q){return M({ok:!1,error:Q instanceof Error?Q.message:String(Q)},409)}}if(H.pathname==="/api/backups/recovery-points"&&L.method==="POST"){let T=await me(L),D=Er.getRecoveryPoint(String(T.id??""));if(!D||D.projectId!==E.project.id)return M({ok:!1,error:"Recovery point was not found."},404);if(T.held==null&&T.pinned==null)return M({ok:!1,error:"held or pinned must be provided."},422);return M({ok:!0,recoveryPoint:Er.updateRecoveryPoint(D.id,{held:T.held==null?D.held:T.held===!0,pinned:T.pinned==null?D.pinned:T.pinned===!0})})}if(H.pathname==="/api/backups/retention"&&L.method==="POST")return M({ok:!0,jobs:hs.enqueueRetention()},202);if(H.pathname==="/api/data-services/capabilities"&&L.method==="GET"){let T=String(H.searchParams.get("engine")??""),D=String(H.searchParams.get("provider")??"");if(!["postgres","mysql","mariadb","redis","mongodb","libsql"].includes(T)||!["aws_rds","aws_aurora","aws_elasticache","server","container","external"].includes(D))return M({ok:!1,error:"A supported engine and provider are required."},422);return M({ok:!0,engine:T,provider:D,capabilities:Gr(T,D)})}if(H.pathname==="/api/data-services"&&L.method==="GET"){let T=E.environments.get(xe),D=lr.list(E.project.id,T?.id).map((U)=>{let q=lr.credential(U.id);return{...U,credentialRef:void 0,credential:q?{configured:!0,username:q.username,version:q.version,rotatedAt:q.rotatedAt}:void 0,dependencies:lr.dependencies(U.id).map((G)=>({resourceId:G.resourceId,requiresRedeploy:G.requiresRedeploy}))}});return M({ok:!0,services:D})}if(H.pathname==="/api/data-services/preview"&&L.method==="POST"){let T=await me(L);if(T.id){let q=lr.get(String(T.id)),G=E.environments.get(xe),X=String(T.action??"");if(!q||q.projectId!==E.project.id||q.environmentId!==G?.id)return M({ok:!1,error:"Data service was not found in this environment."},404);if(!Bu.includes(X))return M({ok:!1,error:`Action must be one of: ${Bu.join(", ")}.`},422);try{return M({ok:!0,plan:Tc.plan(q,X,T.changes??{})})}catch(Q){return M({ok:!1,error:Q instanceof Error?Q.message:String(Q)},422)}}let D=String(T.engine??""),U=String(T.provider??"");if(!["postgres","mysql","mariadb","redis","mongodb","libsql"].includes(D)||!["aws_rds","aws_aurora","aws_elasticache","server","container","external"].includes(U))return M({ok:!1,error:"A supported engine and provider are required."},422);return M({ok:!0,capabilities:Gr(D,U),defaults:{publicExposure:!1,backupRetentionDays:7,deletionProtection:!0},productionExecutionCreated:!1})}if(H.pathname==="/api/data-services"&&L.method==="POST"){let T=await me(L),D=String(T.engine??""),U=String(T.provider??""),q=E.environments.get(xe),G=Xe(Se);if(!q)return M({ok:!1,error:"Environment was not found."},404);if(!["postgres","mysql","mariadb","redis","mongodb","libsql"].includes(D)||!["aws_rds","aws_aurora","aws_elasticache","server","container","external"].includes(U))return M({ok:!1,error:"A supported engine and provider are required."},422);let X={organizationId:E.organization.id,projectId:E.project.id,environmentId:q.id,resourceId:T.resourceId?String(T.resourceId):void 0,name:String(T.name??"").trim(),engine:D,provider:U,placement:String(T.placement??T.name??"").trim(),engineVersion:T.engineVersion?String(T.engineVersion):void 0,plan:String(T.plan??(U==="aws_elasticache"?"cache.t4g.micro":U==="aws_aurora"?"db.serverless":"db.t4g.micro")),storageGb:T.storageGb==null?void 0:Number(T.storageGb),highAvailability:T.highAvailability===!0,publicExposure:T.publicExposure===!0,allowedCidrs:Array.isArray(T.allowedCidrs)?T.allowedCidrs.map(String):[],desiredState:T.desiredState&&typeof T.desiredState==="object"&&!Array.isArray(T.desiredState)?T.desiredState:{},observedState:{},origin:T.adopt===!0?"adopted":"managed",managementEnabled:T.adopt!==!0,ownerActorId:G.actor?.id,username:T.username?String(T.username):void 0};try{if(T.adopt===!0){let ie=lr.create({...X,status:"adopted",credentialRef:void 0});return M({ok:!0,service:ie,readOnly:!0},201)}if(!ef({provider:U,engine:D}))return M({ok:!1,error:`No ${U}/${D} provisioning adapter is configured; adopt it read-only or choose a supported provider.`},422);let Q=await Tc.create(X);return M({ok:!0,service:Q.service,operationId:Q.operationId,oneTimeCredential:Q.credential},201)}catch(Q){return M({ok:!1,error:Q instanceof Error?Q.message:String(Q)},422)}}if(H.pathname==="/api/data-services/connections"&&L.method==="GET"){let T=lr.get(String(H.searchParams.get("id")??"")),D=E.environments.get(xe);if(!T||T.projectId!==E.project.id||T.environmentId!==D?.id)return M({ok:!1,error:"Data service was not found in this environment."},404);let U=typeof T.observedState.endpoint==="string"?T.observedState.endpoint:void 0,q=typeof T.observedState.readerEndpoint==="string"?T.observedState.readerEndpoint:void 0,G=Number(T.observedState.port)||(T.engine==="postgres"?5432:T.engine==="redis"?6379:T.engine==="mongodb"?27017:T.engine==="libsql"?8080:3306),X=typeof T.observedState.database==="string"?T.observedState.database:void 0,Q=typeof T.observedState.tls==="boolean"?T.observedState.tls:T.provider.startsWith("aws_"),ie=[...U?[{type:T.publicExposure?"external":"internal",host:U,port:G,database:X,tls:Q}]:[],...q?[{type:"internal",host:q,port:G,database:X,tls:Q}]:[],{type:"tunnel",host:U??T.placement,port:G,database:X,tls:Q}];return M({ok:!0,serviceId:T.id,guidance:cl(T,ie)})}if(H.pathname==="/api/data-services/action"&&L.method==="POST"){let T=await me(L),D=lr.get(String(T.id??"")),U=E.environments.get(xe),q=String(T.action??"");if(!D||D.projectId!==E.project.id||D.environmentId!==U?.id)return M({ok:!1,error:"Data service was not found in this environment."},404);if(!Bu.includes(q))return M({ok:!1,error:`Action must be one of: ${Bu.join(", ")}.`},422);let G=T.changes&&typeof T.changes==="object"&&!Array.isArray(T.changes)?T.changes:{};try{let X=Tc.plan(D,q,G);if(T.execute!==!0)return M({ok:!0,plan:X,productionExecutionCreated:!1});let Q=Tc.enqueue(D,q,G,Xe(Se).actor?.id);return M({ok:!0,plan:X,operationId:Q},202)}catch(X){return M({ok:!1,error:X instanceof Error?X.message:String(X)},409)}}if(H.pathname==="/api/data-services/dependencies"&&L.method==="POST"){let T=await me(L),D=lr.get(String(T.id??"")),U=E.environments.get(xe),q=E.store.getResource(String(T.resourceId??""));if(!D||D.projectId!==E.project.id||D.environmentId!==U?.id)return M({ok:!1,error:"Data service was not found in this environment."},404);if(!q||q.projectId!==E.project.id||q.environmentId!==U?.id)return M({ok:!1,error:"Dependent resource was not found in this environment."},404);if(T.remove===!0)return M({ok:lr.removeDependency(D.id,q.id)});return lr.addDependency({serviceId:D.id,resourceId:q.id,secretRef:D.credentialRef??`secret://data-services/${D.projectId}/${D.name}`,requiresRedeploy:T.requiresRedeploy!==!1}),M({ok:!0,dependencies:lr.dependencies(D.id).map((G)=>({resourceId:G.resourceId,requiresRedeploy:G.requiresRedeploy}))})}if(H.pathname==="/api/data-services/management"&&L.method==="POST"){let T=await me(L),D=lr.get(String(T.id??"")),U=E.environments.get(xe);if(!D||D.projectId!==E.project.id||D.environmentId!==U?.id)return M({ok:!1,error:"Data service was not found in this environment."},404);if(D.origin!=="adopted"||T.reviewed!==!0||T.confirm!==D.name)return M({ok:!1,error:`Enabling management requires reviewed=true and typing ${D.name}.`},409);return M({ok:!0,service:lr.update(D.id,D.version,{managementEnabled:!0})})}if(H.pathname==="/api/data-services/reveal"&&L.method==="POST"){let T=await me(L),D=lr.get(String(T.id??"")),U=E.environments.get(xe),q=D?lr.credential(D.id):void 0;if(!D||D.projectId!==E.project.id||D.environmentId!==U?.id)return M({ok:!1,error:"Data service was not found in this environment."},404);if(!q)return M({ok:!1,error:"No managed credential is configured."},404);return M({ok:!0,credential:{username:q.username,password:await mi.resolve(q.secretRef),version:q.version}},200,{"cache-control":"no-store"})}if(H.pathname==="/api/databases"&&L.method==="GET")return M(await lb(h,xe));if(H.pathname==="/api/databases"&&L.method==="POST"){let T=await me(L),D=String(T.name??"").trim();if(!Yi(D))return M({ok:!1,error:"Database name must be a valid identifier (letters, numbers, underscore; not starting with a digit)."},422);return M({...await ub(h,xe,D),name:D})}if(H.pathname==="/api/databases/vitess"&&L.method==="GET"){let T=await fb(h,xe);return M({...T,unhealthy:hb(T.tablets),missingPrimary:yb(T)})}if(H.pathname==="/api/databases/vitess/provision"&&L.method==="POST"){let T=await me(L);try{return M(await Ab(h,xe,{keyspace:String(T.keyspace??""),username:String(T.username??""),password:String(T.password??""),sharded:T.sharded===!0,confirm:String(T.confirm??"")}))}catch(D){return M({ok:!1,error:D instanceof Error?D.message:String(D)},422)}}if(H.pathname==="/api/databases/vitess/migrations"&&L.method==="GET"){let T=H.searchParams.get("keyspace")??void 0;return M(await Eb(h,xe,T))}if(H.pathname==="/api/databases/vitess/migrations/action"&&L.method==="POST"){let T=await me(L),D=String(T.uuid??"").trim(),U=String(T.action??"");if(!["retry","cancel","cleanup","complete"].includes(U))return M({ok:!1,error:"Unknown migration action."},422);return M(await Rb(h,xe,D,U))}if(H.pathname==="/api/databases/vitess/keyspaces"&&L.method==="POST"){let T=await me(L),D=String(T.name??"").trim(),U=T.sharded===!0;return M(await wb(h,xe,D,{sharded:U}))}if(H.pathname==="/api/databases/vitess/vschema"&&L.method==="POST"){let T=await me(L),D=String(T.keyspace??"").trim(),U=typeof T.vschema==="string"?T.vschema:JSON.stringify(T.vschema??null);return M(await kb(h,xe,D,U))}if(H.pathname==="/api/databases/vitess/schema"&&L.method==="POST"){let T=await me(L),D=String(T.sql??"");return M(await Ib(h,xe,D))}if(H.pathname==="/api/databases/vitess/install-client"&&L.method==="POST")return M(await Sb(h,xe));if(H.pathname==="/api/databases/backups"&&L.method==="GET")return M(await gb(h,xe));if(H.pathname==="/api/databases/backup"&&L.method==="POST"){let T=await me(L),D=String(T.database??"").trim();if(!Yi(D))return M({ok:!1,error:"Database name must be a valid identifier."},422);if(T.confirm!==D)return M({ok:!1,error:`Type "${D}" to back up this database.`},409);return M(await mb(h,xe,D))}if(H.pathname==="/api/databases/users"&&L.method==="POST"){let T=await me(L),D=String(T.username??"").trim(),U=String(T.password??""),q=String(T.database??"").trim()||void 0,G=T.access==="readonly"?"readonly":"all";if(!Yi(D))return M({ok:!1,error:"Username must be a valid identifier."},422);if(!U)return M({ok:!1,error:"Password is required."},422);if(q&&!Yi(q))return M({ok:!1,error:"Database must be a valid identifier."},422);return M({...await pb(h,xe,{username:D,password:U,database:q,access:G}),username:D})}if(H.pathname==="/api/dashboard-data")return ur=K.get(xe)??await Ce(xe),Ce(xe),M(Tg(ur,{user:jn,slug:h.project.slug}));if(H.pathname==="/api/users"&&L.method==="GET")return M({ok:!0,users:In(t).map(Wa)});if(H.pathname==="/api/users"&&L.method==="POST"){let T=await me(L),D=String(T.username??"").trim();if(!Tu(D))return M({ok:!1,error:"Username must be 2-32 characters: letters, numbers, dot, dash or underscore."},422);let U=T.sites&&typeof T.sites==="object"?T.sites:{},q=new Set(Object.keys(h.sites??{})),G={};for(let[ie,Ae]of Object.entries(U)){if(!q.has(ie))return M({ok:!1,error:`Site '${ie}' was not found.`},404);if(Ae!=="owner"&&Ae!=="collaborator")return M({ok:!1,error:`Unknown site role '${Ae}'. Use 'owner' or 'collaborator'.`},422);G[ie]=Ae}let X=typeof T.password==="string"&&T.password.trim()?T.password.trim():void 0;if(X&&X.length<12)return M({ok:!1,error:"Password must be at least 12 characters."},422);let Q=wc(t,{username:D,password:X,name:typeof T.name==="string"?T.name:void 0,sites:G});return _i(E,In(t)),at(),M({ok:!0,user:Wa(Q.user),password:Q.password})}if(H.pathname==="/api/users"&&L.method==="DELETE"){let T=await me(L),D=String(T.username??"").trim();if(D.toLowerCase()===Se.username.toLowerCase())return M({ok:!1,error:"You cannot remove your own account."},409);let U=Un(In(t),D),q=U?E.store.getActorByExternalId("user",`dashboard:${U.username.toLowerCase()}`):void 0,G=q?E.store.getMembershipForActor(E.organization.id,q.id):void 0;if(G&&G.source!=="legacy")return M({ok:!1,error:"Organization members must be removed from Organization access."},409);let X=Cg(t,D);if(!X.ok)return M(X,409);return _i(E,In(t)),ge.clear(),M({ok:!0,username:D})}if(H.pathname==="/api/actions/run"&&L.method==="POST"){let T=await L.json().catch(()=>({})),D=T.action?Fi(T.action,xe):void 0;if(!D)return M({ok:!1,error:"Unknown dashboard action."},404);if(D.mutates&&T.confirm!==D.confirm)return M({ok:!1,error:`Type "${D.confirm}" to run this action.`},409);let U=await we({controlPlane:E,environment:xe,actor:Se,kind:`dashboard.action.${D.id}`,input:{action:D.id},execute:()=>Hg(D,{cwd:t,cliEntry:m})});return M({...U.result,controlPlaneOperation:U.operation})}if(H.pathname==="/api/server/operations/run"&&L.method==="POST"){let T=await L.json().catch(()=>({})),D=T.operation?_g(T.operation,h,ur):void 0;if(!D)return M({ok:!1,error:"Unknown or unavailable server operation."},404);if(D.mutates&&T.confirm!==D.confirm)return M({ok:!1,error:`Type "${D.confirm}" to run this operation.`},409);let U=Z(E,String(xe));await new lt(St).run(new Qr,{...U,artifactRoot:t});let q=Di(St,{scope:U,desiredConfigHash:E.project.desiredConfigHash});if(q.decision.outcome==="block")return M({ok:!1,error:`Operation blocked by security policy: ${q.decision.explanation}`,securityReview:q},409);let G=await we({controlPlane:E,environment:xe,actor:Se,kind:`dashboard.server.${D.id}`,resourceSlug:D.target,input:{operation:D.id,target:D.target,to:T.to??null},execute:()=>$u(h,xe,D,{to:T.to})});return M({...G.result,controlPlaneOperation:G.operation,securityReview:q})}if(H.pathname==="/api/server/command"&&L.method==="POST"){let T=await L.json().catch(()=>({}));if(T.confirm!=="run")return M({ok:!1,error:'Type "run" to execute this command on the server.'},409);let D=await we({controlPlane:E,environment:xe,actor:Se,kind:"dashboard.server.command",input:{source:"interactive-command"},execute:()=>kc(h,xe,String(T.command??""))});return M({...D.result,controlPlaneOperation:D.operation})}if(H.pathname==="/api/serverless/operations")return M(Ng(h,xe,ur));if(H.pathname==="/api/serverless/operations/run"&&L.method==="POST"){let T=await L.json().catch(()=>({})),D=T.operation?oS(T.operation,h,xe,ur):void 0;if(!D)return M({ok:!1,error:"Unknown or unavailable serverless operation."},404);if(D.mutates&&T.confirm!==D.confirm)return M({ok:!1,error:`Type "${D.confirm}" to run this operation.`},409);let U=await we({controlPlane:E,environment:xe,actor:Se,kind:`dashboard.serverless.${D.id}`,input:{operation:D.id,min:T.min??null,max:T.max??null},execute:()=>sS(h,xe,D,{min:T.min,max:T.max})});return ur=await Js(h,xe),K.set(xe,ur),M({...U.result,controlPlaneOperation:U.operation})}if(H.pathname==="/api/serverless/command"&&L.method==="POST"){let T=await L.json().catch(()=>({}));if(T.confirm!=="run")return M({ok:!1,error:'Type "run" to execute this command.'},409);return M(await aS(h,xe,String(T.command??"")))}if(H.pathname==="/api/serverless/dlq"&&L.method==="GET"){let T=Number(H.searchParams.get("max"))||10;return M(await cS(h,xe,T))}if(H.pathname==="/api/serverless/dlq/redrive"&&L.method==="POST"){let T=await L.json().catch(()=>({}));if(T.confirm!=="redrive")return M({ok:!1,error:'Type "redrive" to move messages back onto a source queue.'},409);return M(await dS(h,xe,{max:T.max,targetQueue:T.targetQueue}))}if(H.pathname==="/api/serverless/dlq/purge"&&L.method==="POST"){if((await L.json().catch(()=>({}))).confirm!=="purge")return M({ok:!1,error:'Type "purge" to permanently discard every DLQ message.'},409);return M(await lS(h,xe))}if(H.pathname==="/api/serverless/secrets"&&L.method==="GET")return M({secrets:uS(h,xe)});if(H.pathname==="/api/serverless/secrets"&&L.method==="POST"){let T=await L.json().catch(()=>({})),D=String(T.secretId??"").trim();if(!D)return M({ok:!1,error:"A secret id is required."},422);if(typeof T.value!=="string"||T.value==="")return M({ok:!1,error:"A non-empty value is required."},422);return M(await pS(h,xe,D,T.value))}if(H.pathname==="/api/serverless/secrets"&&L.method==="DELETE"){let T=await L.json().catch(()=>({})),D=String(T.secretId??"").trim();if(!D)return M({ok:!1,error:"A secret id is required."},422);if(T.confirm!==D)return M({ok:!1,error:`Type "${D}" to delete this secret.`},409);return M(await mS(h,xe,D))}if(H.pathname==="/api/serverless/functions/config"&&L.method==="POST"){let T=await L.json().catch(()=>({})),D=String(T.mode??"").trim();if(T.confirm!==D)return M({ok:!1,error:`Type "${D}" to update this function.`},409);return M(await gS(h,xe,D,{memory:T.memory,timeout:T.timeout}))}if(H.pathname==="/api/serverless/alarms"&&L.method==="GET")return M(await fS(h,xe));if(H.pathname==="/api/serverless/alarms"&&L.method==="POST"){let T=await L.json().catch(()=>({})),D=String(T.preset??"").trim(),U=Number(T.threshold);if(!D)return M({ok:!1,error:"An alarm metric is required."},422);if(!Number.isFinite(U))return M({ok:!1,error:"A numeric threshold is required."},422);return M(await hS(h,xe,D,U))}if(H.pathname==="/api/serverless/alarms"&&L.method==="DELETE"){let T=await L.json().catch(()=>({})),D=String(T.name??"").trim();if(!D)return M({ok:!1,error:"An alarm name is required."},422);if(T.confirm!==D)return M({ok:!1,error:`Type "${D}" to delete this alarm.`},409);return M(await yS(h,xe,D))}if(H.pathname==="/api/serverless/traces"&&L.method==="GET"){let T=Number(H.searchParams.get("minutes"))||30;return M(await ju(h,xe,T))}if(H.pathname==="/api/serverless/scheduler"&&L.method==="POST"){let T=await L.json().catch(()=>({})),D=String(T.action??"").trim();if(D!=="enable"&&D!=="disable"&&D!=="run")return M({ok:!1,error:"Unknown scheduler action."},422);if(T.confirm!==D)return M({ok:!1,error:`Type "${D}" to ${D} the scheduler.`},409);return M(await Lg(h,xe,D))}if(!OS(H.pathname,jn))return new Response(null,{status:302,headers:{location:`/access-denied?env=${encodeURIComponent(xe)}`}});let ga=await dr(Se,xe);if(!ga)return Fu("ts-cloud dashboard UI is unavailable.",503);return $S(ga,H.pathname)}catch(vs){if(e.verbose)console.error(vs);return M({ok:!1,error:vs?.message??String(vs)},500)}})();if(!kr)return kr;if(Tt&&(H.pathname.startsWith("/api/")||H.pathname.startsWith("/auth/"))){let yt;if((Je.headers.get("content-type")||"").includes("application/json"))yt=await Je.json().catch(()=>{return});let nn=gr.resolveUser(L),Dr=nn?Xe(nn).actor:void 0;E.store.appendEvent({organizationId:E.organization.id,projectId:E.project.id,actorId:Dr?.id,correlationId:it,type:kr.status<400?"dashboard.mutation.succeeded":"dashboard.mutation.failed",level:kr.status<400?"info":"warning",payload:L_(L.method,H.pathname,yt,kr.status,Date.now()-Ze)})}return kr.headers.set("x-request-id",it),kr}});xc?.start();let GS=e.spendLoop??!0,vf;if(GS)(async()=>{let{SpendRunner:L,SpendLoopLease:Qe,startSpendLoop:Ze}=await import("./spend/index.js"),{store:Je}=await jS(E.store),it=new L({controlPlane:E.store,store:Je,alerts:ct});vf=Ze(it,{lease:new Qe(E.store,{owner:`dashboard:${process.pid}`}),onError:(Tt)=>console.error(`[ts-cloud] spend cycle failed: ${Tt instanceof Error?Tt.message:String(Tt)}`)})})();let QS=Xs.stop.bind(Xs);return Xs.stop=(L)=>{clearInterval(gf),clearInterval(ff),clearInterval(hf),clearInterval(mf),clearInterval(pf),clearInterval(cf),clearInterval(df),clearInterval(lf),clearInterval(uf),at(),Rr.clear(),xc?.stop(),vf?.();let Qe=QS(L);if(xc)xc.settled().finally(()=>E.store.close());else E.store.close();return Qe},{server:Xs,url:`http://${r}:${Xs.port}/`}}export{ao,qg,wd,pA,Si,Rd,co,Vg,jg,Or,lo,Dd,He,Pd,Nd,kd,Ci,Lr,zg,Ge,Bd,uo,po,go,xd,_d,Od,bi,mo,Ld,$r,Ei,ho,dt,Kg,$d,Md,Wg,Fd,fo,qt,Ud,Qd,Hd,Gd,qe,Jg,qd,Vt,Ao,yo,Io,vi,Ti,Mr,Vd,wi,So,Co,Fr,jd,Ur,Xg,Yg,lt,Qr,bo,Zg,zd,Kd,Eo,Wd,Jd,Xd,gA,Z,Yd,mA,hA,Ri,Di,fA,ce,em,ut,ve,Te,pt,Zd,el,tl,rl,tm,rm,vo,nm,im,To,nl,il,sm,ee,om,sl,de,wo,Ve,gt,mt,Hr,Pi,ol,AA,Ro,am,cm,dm,lm,Do,Po,al,Gr,No,cl,Ni,dl,ko,Bo,xo,_o,Oo,Lo,$o,Mo,ll,jt,Fo,ul,qr,pl,Uo,Qo,gl,zt,Ho,Go,um,yA,ki,IA,SA,CA,qo,Vo,ml,pm,jo,Vr,hl,Bi,fl,Al,yl,zo,gm,Ko,Il,mm,Wo,hm,jr,zr,Sl,xi,Cl,bl,El,Jo,vl,Tl,fm,wl,S,Rl,_i,we,Dl,Pl,bA,Nl,Am,EA,vA,TA,ym,Im,wA,RA,Sm,Cm,DA,Oi,Kt,Xo,Kr,Wr,Wt,je,ht,Li,bm,Jr,kl,Bl,Yo,Em,Zo,vm,Tm,xl,ea,$i,_l,ta,ft,Re,Ol,ra,na,Ll,ia,sa,oa,aa,$l,Ml,Xr,ca,da,la,ua,Fl,Mi,Yr,Fi,pa};
|