code-server
Version:
Run VS Code on a remote server.
15 lines (11 loc) • 5.36 kB
JavaScript
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"],alias:{help:"h",version:"v"}}),s=["list-extensions","locate-extension"],i=["install-extension","install-builtin-extension","uninstall-extension"];if(e.help||e.version||s.some(n=>!!e[n])||i.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 o=null,l=null;const d=()=>(l||(l=loadCode().then(n=>n.createServer(a))),l),w=require("http"),y=require("os");if(Array.isArray(product.serverLicense)&&product.serverLicense.length&&(console.log(product.serverLicense.join(`
`)),product.serverLicensePrompt&&e["accept-server-license-terms"]!==!0)){hasStdinWithoutTty()&&(console.log("To accept the license terms, start the server with --accept-server-license-terms"),process.exit());try{await prompt(product.serverLicensePrompt)||process.exit()}catch(n){console.log(n),process.exit()}}let m=!0,h=!0,a=null;const p=w.createServer(async(n,c)=>(m&&(m=!1,perf.mark("code/server/firstRequest")),(await d()).handleRequest(n,c)));p.on("upgrade",async(n,c)=>(h&&(h=!1,perf.mark("code/server/firstWebSocket")),(await d()).handleUpgrade(n,c))),p.on("error",async n=>(await d()).handleServerError(n));const g=sanitizeStringArg(e.host)||(e.compatibility!=="1.63"?"localhost":void 0),v=e["socket-path"]?{path:sanitizeStringArg(e["socket-path"])}:{host:g,port:await parsePort(g,sanitizeStringArg(e.port),sanitizeStringArg(e["pick-port"]))};p.listen(v,async()=>{let n=Array.isArray(product.serverGreeting)&&product.serverGreeting.length?`
${product.serverGreeting.join(`
`)}
`:"";if(typeof v.port=="number"&&e["print-ip-address"]){const c=y.networkInterfaces();Object.keys(c).forEach(function(u){c[u].forEach(function(f){!f.internal&&f.family==="IPv4"&&(n+=`IP Address: ${f.address}
`)})})}if(a=p.address(),a===null)throw new Error("Unexpected server address");n+=`Server bound to ${typeof a=="string"?a:`${a.address}:${a.port} (${a.family})`}
`,n+=`Extension host agent listening on ${typeof a=="string"?a:a.port}
`,console.log(n),perf.mark("code/server/started"),global.vscodeServerListenTime=performance.now(),await d()}),process.on("exit",()=>{p.close(),o&&o.dispose()})}function sanitizeStringArg(r){return Array.isArray(r)&&(r=r.pop()),typeof r=="string"?r:void 0}async function parsePort(r,e,s){let i;if(e){let t;if(e.match(/^\d+$/)){if(i=parseInt(e,10),i===0||!s)return i}else if(t=parseRange(e)){const o=await findFreePort(r,t.start,t.end);if(o!==void 0)return o;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(s){const t=parseRange(s);if(t){if(t.start<=i&&i<=t.end)return i;{const o=await findFreePort(r,t.start,t.end);if(o!==void 0)return o;console.log(`--pick-port: Could not find free port in range: ${t.start} - ${t.end}.`),process.exit(1)}}else console.log(`--pick-port "${s}" 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(r){const e=r.match(/^(\d+)-(\d+)$/);if(e){const s=parseInt(e[1],10),i=parseInt(e[2],10);if(s>0&&s<=i&&i<=65535)return{start:s,end:i}}}async function findFreePort(r,e,s){const i=t=>new Promise(o=>{const l=http.createServer();l.listen(t,r,()=>{l.close(),o(!0)}).on("error",()=>{o(!1)})});for(let t=e;t<=s;t++)if(await i(t))return t}function loadCode(){return new Promise((r,e)=>{const s=require("path");process.env.VSCODE_DEV?(process.env.VSCODE_INJECT_NODE_MODULE_LOOKUP_PATH=process.env.VSCODE_INJECT_NODE_MODULE_LOOKUP_PATH||s.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",r,e)})}function hasStdinWithoutTty(){try{return!process.stdin.isTTY}catch{}return!1}function prompt(r){const e=readline.createInterface({input:process.stdin,output:process.stdout});return new Promise((s,i)=>{e.question(r+" ",async function(t){e.close();const o=t.toString().trim().toLowerCase();o===""||o==="y"||o==="yes"?s(!0):o==="n"||o==="no"?s(!1):(process.stdout.write(`
Invalid Response. Answer either yes (y, yes) or no (n, no)
`),s(await prompt(r)))})})}start();
//# sourceMappingURL=server-main.js.map