UNPKG

@coder/code-server-pr

Version:

Run VS Code on a remote server.

14 lines (10 loc) 4.8 kB
const perf=require("./vs/base/common/performance"),performance=require("perf_hooks").performance,product=require("../product.json"),readline=require("readline"),http=require("http");perf.mark("code/server/start"),global.vscodeServerStartTime=performance.now();async function start(){const e=require("minimist")(process.argv.slice(2),{boolean:["start-server","list-extensions","print-ip-address","help","version","accept-server-license-terms"],string:["install-extension","install-builtin-extension","uninstall-extension","locate-extension","socket-path","host","port","pick-port","compatibility"]}),r=["list-extensions","locate-extension"],o=["install-extension","install-builtin-extension","uninstall-extension"];if(e.help||e.version||r.some(n=>!!e[n])||o.some(n=>!!e[n])&&!e["start-server"]){loadCode().then(n=>{n.spawnCli()});return}e.compatibility==="1.63"&&(console.warn(`server.sh is being replaced by 'bin/${product.serverApplicationName}'. Please migrate to the new command and adopt the following new default behaviors:`),console.warn("* connection token is mandatory unless --without-connection-token is used"),console.warn("* host defaults to `localhost`"));let s=null,c=null;const d=()=>(c||(c=loadCode().then(n=>n.createServer(l))),c),w=require("http"),E=require("os");if(Array.isArray(product.serverLicense)&&product.serverLicense.length&&(console.log(product.serverLicense.join(` `)),product.serverLicensePrompt&&e["accept-server-license-terms"]!==!0))try{await prompt(product.serverLicensePrompt)||process.exit()}catch(n){console.log(n),process.exit()}let m=!0,v=!0,l=null;const p=w.createServer(async(n,a)=>(m&&(m=!1,perf.mark("code/server/firstRequest")),(await d()).handleRequest(n,a)));p.on("upgrade",async(n,a)=>(v&&(v=!1,perf.mark("code/server/firstWebSocket")),(await d()).handleUpgrade(n,a))),p.on("error",async n=>(await d()).handleServerError(n));const g=e.host||(e.compatibility!=="1.63"?"localhost":void 0),h=e["socket-path"]?{path:e["socket-path"]}:{host:g,port:await parsePort(g,e.port,e["pick-port"])};p.listen(h,async()=>{const n=product.serverGreeting.join(` `);let a=n?` ${n} `:"";if(typeof h.port=="number"&&e["print-ip-address"]){const u=E.networkInterfaces();Object.keys(u).forEach(function(x){u[x].forEach(function(f){!f.internal&&f.family==="IPv4"&&(a+=`IP Address: ${f.address} `)})})}if(l=p.address(),l===null)throw new Error("Unexpected server address");a+=`Extension host agent listening on ${typeof l=="string"?l:l.port} `,console.log(a),perf.mark("code/server/started"),global.vscodeServerListenTime=performance.now(),await d()}),process.on("exit",()=>{p.close(),s&&s.dispose()})}async function parsePort(i,e,r){let o;if(e){let t;if(e.match(/^\d+$/)){if(o=parseInt(e,10),o===0||!r)return o}else if(t=parseRange(e)){const s=await findFreePort(i,t.start,t.end);if(s!==void 0)return s;console.warn(`--port: Could not find free port in range: ${t.start} - ${t.end} (inclusive).`),process.exit(1)}else console.warn(`--port "${e}" is not a valid number or range. Ranges must be in the form 'from-to' with 'from' an integer larger than 0 and not larger than 'end'.`),process.exit(1)}if(r){const t=parseRange(r);if(t){if(t.start<=o&&o<=t.end)return o;{const s=await findFreePort(i,t.start,t.end);if(s!==void 0)return s;console.log(`--pick-port: Could not find free port in range: ${t.start} - ${t.end}.`),process.exit(1)}}else console.log(`--pick-port "${r}" is not a valid range. Ranges must be in the form 'from-to' with 'from' an integer larger than 0 and not larger than 'end'.`),process.exit(1)}return 8e3}function parseRange(i){const e=i.match(/^(\d+)-(\d+)$/);if(e){const r=parseInt(e[1],10),o=parseInt(e[2],10);if(r>0&&r<=o&&o<=65535)return{start:r,end:o}}}async function findFreePort(i,e,r){const o=t=>new Promise(s=>{const c=http.createServer();c.listen(t,i,()=>{c.close(),s(!0)}).on("error",()=>{s(!1)})});for(let t=e;t<=r;t++)if(await o(t))return t}function loadCode(){return new Promise((i,e)=>{const r=require("path");process.env.VSCODE_DEV?(process.env.VSCODE_INJECT_NODE_MODULE_LOOKUP_PATH=process.env.VSCODE_INJECT_NODE_MODULE_LOOKUP_PATH||r.join(__dirname,"..","remote","node_modules"),require("./bootstrap-node").injectNodeModuleLookupPath(process.env.VSCODE_INJECT_NODE_MODULE_LOOKUP_PATH)):delete process.env.VSCODE_INJECT_NODE_MODULE_LOOKUP_PATH,require("./bootstrap-amd").load("vs/server/node/server.main",i,e)})}function prompt(i){const e=readline.createInterface({input:process.stdin,output:process.stdout});return new Promise((r,o)=>{e.question(i+" ",async function(t){e.close();const s=t.toString().trim().toLowerCase();s===""||s==="y"||s==="yes"?r(!0):s==="n"||s==="no"?r(!1):(process.stdout.write(` Invalid Response. Answer either yes (y, yes) or no (n, no) `),r(await prompt(i)))})})}start(); //# sourceMappingURL=server-main.js.map