Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up server-main.js #160094

Merged
merged 2 commits into from Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 13 additions & 31 deletions src/server-main.js
Expand Up @@ -21,7 +21,7 @@ async function start() {
// Do a quick parse to determine if a server or the cli needs to be started
const parsedArgs = 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'],
string: ['install-extension', 'install-builtin-extension', 'uninstall-extension', 'locate-extension', 'socket-path', 'host', 'port', 'compatibility'],
alias: { help: 'h', version: 'v' }
});
['host', 'port', 'accept-server-license-terms'].forEach(e => {
Expand Down Expand Up @@ -55,13 +55,17 @@ async function start() {
* @typedef { import('./vs/server/node/remoteExtensionHostAgentServer').IServerAPI } IServerAPI
*/
/** @type {IServerAPI | null} */
const _remoteExtensionHostAgentServer = null;
let _remoteExtensionHostAgentServer = null;
/** @type {Promise<IServerAPI> | null} */
let _remoteExtensionHostAgentServerPromise = null;
/** @returns {Promise<IServerAPI>} */
const getRemoteExtensionHostAgentServer = () => {
if (!_remoteExtensionHostAgentServerPromise) {
_remoteExtensionHostAgentServerPromise = loadCode().then((mod) => mod.createServer(address));
_remoteExtensionHostAgentServerPromise = loadCode().then(async (mod) => {
const server = await mod.createServer(address);
_remoteExtensionHostAgentServer = server;
return server;
});
}
return _remoteExtensionHostAgentServerPromise;
};
Expand Down Expand Up @@ -119,15 +123,15 @@ async function start() {
const nodeListenOptions = (
parsedArgs['socket-path']
? { path: sanitizeStringArg(parsedArgs['socket-path']) }
: { host, port: await parsePort(host, sanitizeStringArg(parsedArgs['port']), sanitizeStringArg(parsedArgs['pick-port'])) }
: { host, port: await parsePort(host, sanitizeStringArg(parsedArgs['port'])) }
);
server.listen(nodeListenOptions, async () => {
let output = Array.isArray(product.serverGreeting) && product.serverGreeting.length ? `\n\n${product.serverGreeting.join('\n')}\n\n` : ``;

if (typeof nodeListenOptions.port === 'number' && parsedArgs['print-ip-address']) {
const ifaces = os.networkInterfaces();
Object.keys(ifaces).forEach(function (ifname) {
ifaces[ifname].forEach(function (iface) {
ifaces[ifname]?.forEach(function (iface) {
if (!iface.internal && iface.family === 'IPv4') {
output += `IP Address: ${iface.address}\n`;
}
Expand Down Expand Up @@ -171,28 +175,25 @@ function sanitizeStringArg(val) {
}

/**
* If `--pick-port` and `--port` is specified, connect to that port.
* If `--port` is specified and describes a single port, connect to that port.
*
* If not and a port range is specified through `--pick-port`
* If `--port`describes a port range
* then find a free port in that range. Throw error if no
* free port available in range.
*
* If only `--port` is provided then connect to that port.
*
* In absence of specified ports, connect to port 8000.
* @param {string | undefined} host
* @param {string | undefined} strPort
* @param {string | undefined} strPickPort
* @returns {Promise<number>}
* @throws
*/
async function parsePort(host, strPort, strPickPort) {
async function parsePort(host, strPort) {
let specificPort;
if (strPort) {
let range;
if (strPort.match(/^\d+$/)) {
specificPort = parseInt(strPort, 10);
if (specificPort === 0 || !strPickPort) {
if (specificPort === 0) {
return specificPort;
}
} else if (range = parseRange(strPort)) {
Expand All @@ -208,25 +209,6 @@ async function parsePort(host, strPort, strPickPort) {
process.exit(1);
}
}
// pick-port is deprecated and will be removed soon
if (strPickPort) {
const range = parseRange(strPickPort);
if (range) {
if (range.start <= specificPort && specificPort <= range.end) {
return specificPort;
} else {
const port = await findFreePort(host, range.start, range.end);
if (port !== undefined) {
return port;
}
console.log(`--pick-port: Could not find free port in range: ${range.start} - ${range.end}.`);
process.exit(1);
}
} else {
console.log(`--pick-port "${strPickPort}" 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 8000;
}

Expand Down
5 changes: 3 additions & 2 deletions src/vs/server/node/serverEnvironmentService.ts
Expand Up @@ -16,7 +16,6 @@ export const serverOptions: OptionDescriptions<ServerParsedArgs> = {

'host': { type: 'string', cat: 'o', args: 'ip-address', description: nls.localize('host', "The host name or IP address the server should listen to. If not set, defaults to 'localhost'.") },
'port': { type: 'string', cat: 'o', args: 'port | port range', description: nls.localize('port', "The port the server should listen to. If 0 is passed a random free port is picked. If a range in the format num-num is passed, a free port from the range (end inclusive) is selected.") },
'pick-port': { type: 'string', deprecationMessage: 'Use the range notation in \'port\' instead.' },
'socket-path': { type: 'string', cat: 'o', args: 'path', description: nls.localize('socket-path', "The path to a socket file for the server to listen to.") },
'connection-token': { type: 'string', cat: 'o', args: 'token', deprecates: ['connectionToken'], description: nls.localize('connection-token', "A secret that must be included with all requests.") },
'connection-token-file': { type: 'string', cat: 'o', args: 'path', deprecates: ['connection-secret', 'connectionTokenFile'], description: nls.localize('connection-token-file', "Path to a file that contains the connection token.") },
Expand Down Expand Up @@ -93,8 +92,10 @@ export interface ServerParsedArgs {
/* ----- server setup ----- */

host?: string;
/**
* A port or a port range
*/
port?: string;
'pick-port'?: string;
'socket-path'?: string;

/**
Expand Down