howtocode 0.7.75 → 0.7.77

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.
@@ -0,0 +1,2 @@
1
+ import{a as v,c as T}from"./chunk-DBPJZUIT.js";import{H as S}from"./chunk-5FIFNXLT.js";import"./chunk-UTYFLLSM.js";import"./chunk-WOT6VMZA.js";import{readFileSync as L}from"node:fs";import{WebSocket as E}from"ws";var U=900*1024,k=64*1024,C=64,O=250,_=5e3;function D(f){let r=Buffer.from(JSON.stringify(f),"utf8"),e=new Uint8Array(4+r.byteLength);return new DataView(e.buffer).setUint32(0,r.byteLength,!0),e.set(r,4),e}var N=class{buffer=new Uint8Array(0);push(r){let e=new Uint8Array(this.buffer.byteLength+r.byteLength);e.set(this.buffer,0),e.set(r,this.buffer.byteLength);let u=[],s=0;for(;e.byteLength-s>=4;){let c=new DataView(e.buffer,s,4).getUint32(0,!0);if(e.byteLength-s-4<c)break;let i=e.subarray(s+4,s+4+c);u.push(JSON.parse(new TextDecoder().decode(i))),s+=4+c}return this.buffer=e.slice(s),u}};async function B(f={}){let r=f.log??(t=>process.stderr.write(`howtocode native host: ${t}
2
+ `)),e=process.env.HOWTOCODE_BROWSER_RELAY_PORT?.trim(),u=f.port??(e&&/^\d+$/.test(e)?Number(e):T),s=f.tokenPath??v(S).tokenPath,c="";try{c=L(s,"utf8").trim()}catch{r(`cannot read relay token at ${s}; run /browser-relay install`)}let i=null,m=!1,l=O,y,g=[],d=Promise.resolve(),h=t=>{let o=Buffer.from(JSON.stringify(t),"utf8");if(o.byteLength<=U){let a=D(t);d=d.then(()=>{let{promise:w,resolve:b}=Promise.withResolvers();return process.stdout.write(a,()=>b()),w});return}let n=o.toString("utf8"),R=`${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`,x=0;for(let a=0;a<n.length;a+=k){let w=n.slice(a,a+k),b=a+k>=n.length;h({t:"chunk",id:R,index:x++,last:b,data:w})}},A=t=>{if(i?.readyState===E.OPEN){i.send(t);return}g.length<C&&g.push(t)},p=()=>{if(m)return;if(!c){y=setTimeout(p,_);return}let t=new E(`ws://127.0.0.1:${u}/ext?token=${encodeURIComponent(c)}`);i=t,t.on("open",()=>{l=O,r(`relay connected on 127.0.0.1:${u}`),h({t:"linked"});for(let o of g.splice(0))t.send(o)}),t.on("message",o=>{let n=typeof o=="string"?o:o.toString("utf8");try{h(JSON.parse(n))}catch{}}),t.on("close",()=>{i===t&&(i=null,h({t:"unlinked",reason:"relay closed"}),!m&&(y=setTimeout(p,l),l=Math.min(l*2,_)))}),t.on("error",()=>{})},P=new N;process.stdin.on("data",t=>{let o;try{o=P.push(t)}catch(n){r(`bad native frame: ${n instanceof Error?n.message:String(n)}`);return}for(let n of o)typeof n=="object"&&n!==null&&"t"in n&&n.t==="chunk"||A(JSON.stringify(n))}),process.stdin.on("end",()=>{m=!0,clearTimeout(y),i?.close(),d.finally(()=>process.exit(0))}),p(),await new Promise(t=>process.stdin.once("end",t)),await d}export{U as NATIVE_FRAME_LIMIT,N as NativeFrameReader,D as encodeNativeFrame,B as runNativeHost};