"use strict";var ee=Object.defineProperty;var a=(e,t)=>ee(e,"name",{value:t,configurable:!0});var I=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports);var C=I((be,U)=>{var{join:te,dirname:L}=require("node:path"),{existsSync:ne,readFileSync:re}=require("node:fs");function oe(){let t=__dirname,n,r=0;for(;!ne(n=te(t,"package.json"));)if(t===(t=L(t))||r++>10)throw new Error("Could not find package root");return n}a(oe,"findPackageJson");var b=oe(),se=JSON.parse(re(b,"utf8")),ie=L(b);U.exports={packageJson:se,PACKAGE_ROOT:ie}});var j=I((Ve,V)=>{if(__filename.endsWith("dev.js")&&!process.env.HARPER_SKIP_COMPILE){let e=require("fast-glob"),{tmpdir:t}=require("node:os"),{relative:n,join:r,basename:y}=require("node:path"),{existsSync:S,statSync:f,readFileSync:x,writeFileSync:v,unlinkSync:w}=require("node:fs"),{isMainThread:m}=require("node:worker_threads"),{spawnSync:Z,spawn:K}=require("node:child_process");process.setSourceMapsEnabled(!0);let{PACKAGE_ROOT:i}=C(),D=["bin","components","dataLayer","resources","server","sqlTranslator","upgrade","utility","validation"],R="ts-build";if(m){let l=!1,g=!1;if(g=S(r(i,R))){let d=new Set;e.sync(D.map(o=>`${o}/**/*.ts`),{cwd:i}).forEach(o=>{let u=0,c=0;try{d.add(o),u=f(r(i,o)).mtimeMs-5e3,c=f(r(i,R,o.replace(/.ts$/,".js"))).mtimeMs}catch{}u>c&&(l=!0)}),e.sync(D.map(o=>`${o}/**/*.js`),{cwd:r(i,R)}).forEach(o=>{if(!d.has(o.replace(/.js$/,".ts")))try{w(r(i,R,o))}catch{}})}else l=!0;if(l){console.log("Compiling TypeScript...");let d=Z("npx",["tsc"],{cwd:i});if(d.stdout?.length&&console.log(d.stdout.toString()),d.stderr?.length&&console.log(d.stderr.toString()),g){let o=r(t(),"harperdb-tsc.pid"),u=!1;if(S(o))try{process.kill(+x(o,"utf8"),0),u=!0}catch{}if(!u){console.log("Starting background TypeScript compilation...");let c=K("npx",["tsc","--watch"],{detached:!0,cwd:i,stdio:"ignore"});c.on("error",P=>{console.error("Error trying to compile TypeScript",P)}),c.pid&&v(o,String(c.pid),"utf-8"),c.unref()}}}}let F=V.constructor,Q=F._findPath;F._findPath=function(l,g,d){if(l.startsWith(".")&&!d&&g.length===1&&g[0].startsWith(i)&&!g[0].includes("node_modules")){let o=n(i,g[0]),u;o.startsWith(R)?u=r(i,n(R,o)):u=r(i,R,o),(l.endsWith(".js")||l.endsWith(".ts"))&&(l=l.slice(0,-3));let c=r(u,l),P=c+".js";if(S(P))return P;if(y(c).includes(".")&&S(c))return c}return Q(l,g,d)}}});var k=I((je,G)=>{"use strict";var ce="nats-server.zip",M="nats-server",ae=process.platform==="win32"?`${M}.exe`:M,le=/^[^\s.,*>]+$/,q="__request__",de=a(e=>`${e}.${q}`,"REQUEST_SUBJECT"),Se={NATS_MSG_ID:"Nats-Msg-Id",ORIGIN:"origin",TRANSACTED_NODES:"transacted_nodes"},ue={HUB_SERVER:"hub.json",LEAF_SERVER:"leaf.json"},Ee={HUB:"hub.pid",LEAF:"leaf.pid"},ge={HUB:"-hub",LEAF:"-leaf",ADMIN:"-admin"},pe={SUCCESS:"success",ERROR:"error"},Re={OPEN:"open",CLOSED:"closed",NO_RESPONDERS:"NoResponders",TIMEOUT:"Timeout"},_e={TXN:"txn",MSGID:"msgid"},A={ERR:"error",WRN:"warn",INF:"info",DBG:"debug",TRC:"trace"},Te={[A.ERR]:1,[A.WRN]:2,[A.INF]:3,[A.DBG]:4,[A.TRC]:5},fe={debug:"-D",trace:"-DVV"};G.exports={NATS_SERVER_ZIP:ce,NATS_SERVER_NAME:M,NATS_BINARY_NAME:ae,PID_FILES:Ee,NATS_CONFIG_FILES:ue,SERVER_SUFFIX:ge,NATS_TERM_CONSTRAINTS_RX:le,REQUEST_SUFFIX:q,UPDATE_REMOTE_RESPONSE_STATUSES:pe,CLUSTER_STATUS_STATUSES:Re,REQUEST_SUBJECT:de,SUBJECT_PREFIXES:_e,MSG_HEADERS:Se,LOG_LEVELS:A,LOG_LEVEL_FLAGS:fe,LOG_LEVEL_HIERARCHY:Te}});var Y=I((Ge,X)=>{"use strict";var _=require("fs-extra"),p=require("path"),s=require("chalk"),Ae=require("needle"),B=require("node-stream-zip"),H=require("semver"),T=k(),we=require("util"),me=require("child_process"),{platform:Ne}=require("os"),he=we.promisify(me.exec),{packageJson:z,PACKAGE_ROOT:ye}=C(),E=p.join(ye,"dependencies"),ve=p.join(E,T.NATS_SERVER_ZIP),O=z.engines["go-lang"],h=z.engines[T.NATS_SERVER_NAME],Pe=`${process.platform}-${process.arch}`,$=p.join(E,Pe,`${T.NATS_BINARY_NAME}`),Ie=`https://github.com/nats-io/nats-server/releases/download/v${h}/nats-server-v${h}-`,W={"linux-x64":"linux-amd64.zip","linux-arm64":"linux-arm64.zip","darwin-x64":"darwin-amd64.zip","darwin-arm64":"darwin-arm64.zip","win32-x64":"windows-amd64.zip"},Oe=Object.keys(W).map(e=>e.split("-"));async function N(e,t=void 0){let{stdout:n,stderr:r}=await he(e,{cwd:t});if(r)throw new Error(r.replace(`
`,""));return n.replace(`
`,"")}a(N,"runCommand");async function $e(){try{await _.access($)}catch{return!1}let e=await N(`${$} --version`,void 0),t=e.substring(e.lastIndexOf("v")+1,e.length);return H.eq(t,h)}a($e,"checkNATSServerInstalled");async function xe(){console.log(s.green(`Verifying go v${O} is on system.`));let e;try{e=(await N("go version",void 0)).match(/[\d.]+/)[0]}catch{throw Error("go does not appear to be installed or is not in the PATH, cannot install clustering dependencies.")}if(!H.gte(e,O))throw Error(`go version ${O} or higher must be installed.`);console.log(s.green(`go v${O} is on the system.`))}a(xe,"checkGoVersion");async function Ce(){console.log(s.green("Extracting NATS Server source code."));let e=new B.async({file:ve}),t=p.join(E,`${T.NATS_SERVER_NAME}-src`),n=await e.extract(null,E);return console.log(s.green(`Extracted ${n} entries.`)),await e.close(),t}a(Ce,"extractNATSServer");async function Me(e){let t=p.join(e,T.NATS_BINARY_NAME),n=p.join(E,"pkg");await _.move(t,$,{overwrite:!0}),await _.remove(e),await _.remove(n)}a(Me,"cleanUp");async function J(e,t){let n=e&&t?`${e}-${t}`:`${process.platform}-${process.arch}`,r=W[n];if(r===void 0)throw Error(`unknown platform - architecture: ${n}`);let y=`${Ie}${r}`,S=p.join(E,n,r);await _.ensureFile(S),console.log(s.green(`****Downloading install of NATS Server: ${y}****`)),await Ae("get",y,{output:S,follow_max:5}),console.log(s.green("Successfully downloaded and saved nats-server zip.")),console.log(s.green("Extracting nats-server zip."));let f=new B.async({file:S}),x=await f.entries(),v=e==="win32"||process.platform==="win32"?`${T.NATS_SERVER_NAME}.exe`:T.NATS_SERVER_NAME,w=p.join(E,n,v);for(let m of Object.values(x))!m.isDirectory&&m.name.endsWith(v)&&(await f.extract(m.name,w),console.log(s.green(`Successfully extracted nats-server zip to ${w}.`)));await f.close(),await _.remove(S),await _.chmod(w,511)}a(J,"downloadNATSServer");async function De(){if(console.log(s.green("****Starting install of NATS Server.****")),await $e()){console.log(s.green(`****NATS Server v${h} installed.****`));return}try{await J();try{let n=await N(`${$} --version`,void 0);console.log(s.green(`****Successfully extracted ${n}.****`))}catch(n){if(n.toString().includes("file busy"))console.warn("Error checking NATS versions",n);else throw n}return}catch(n){console.error(s.red(`Error: ${n.message}. Failed to download NATS server.  Building from source`))}try{await xe()}catch(n){console.error(s.red(n.message)),process.exit(1)}let t=await Ce();console.log(s.green("Building NATS Server binary.")),Ne()=="win32"?await N(`set GOPATH=${E}&& go build`,t):await N(`export GOPATH=${E} && go build`,t),console.log(s.green("Building NATS Server binary complete.")),await Me(t),console.log(s.green(`****NATS Server v${h} is installed.****`))}a(De,"installer");X.exports={installer:De,downloadNATSServer:J,ALL_SUPPORTED_PLATFORM_ARCHITECTURES:Oe}});j();var Fe=Y();(async()=>{try{await Fe.installer()}catch(e){console.error(e)}})();
