"use strict";var l=Object.defineProperty;var v=Object.getOwnPropertyDescriptor;var S=Object.getOwnPropertyNames;var h=Object.prototype.hasOwnProperty;var m=(e,r)=>l(e,"name",{value:r,configurable:true});var L=(e,r)=>{for(var o in r)l(e,o,{get:r[o],enumerable:true})};var b=(e,r,o,n)=>{if(r&&typeof r==="object"||typeof r==="function"){for(let i of S(r))if(!h.call(e,i)&&i!==o)l(e,i,{get:()=>r[i],enumerable:!(n=v(r,i))||n.enumerable})}return e};var g=e=>b(l({},"__esModule",{value:true}),e);var y={};L(y,{default:()=>W});module.exports=g(y);var p=require("node:crypto");var w=require("node:net");var d=require("electron");var a=require("../../config");var f=require("../../environment-remote");var u=require("../../internal-server/start");const P=require("../../preload-safe-debug")("Ferdium:LocalServer");const O=m(e=>new Promise(r=>{const o=(0,w.createServer)(n=>{n.write("Echo server\r\n");n.pipe(n)});o.listen(e,a.LOCAL_HOSTNAME);o.on("error",()=>{r(true)});o.on("listening",()=>{o.close();r(false)})}),"portInUse");let c=false;let t=a.LOCAL_PORT;let s="";var W=m(e=>{d.ipcMain.on("startLocalServer",()=>{(async()=>{if(!c){t=a.LOCAL_PORT;while(await O(t)&&t<a.LOCAL_PORT+10){t+=1}s=(0,p.randomBytes)(256/8).toString("base64url");P("Starting local server at",`http://localhost:${t}/token/${s}`);await(0,u.server)((0,f.userDataPath)(),t,s);c=true}e.mainWindow.webContents.send("localServerPort",{port:t,token:s})})().catch(r=>{console.error("Error while starting local server",r)})})},"default");
