Rename base server to just server

Summary:
^

BaseServer doesn't really have any meaning. So, change it to just Server as it really creates the Express app and HTTP server attached to it.

Reviewed By: passy

Differential Revision: D36310822

fbshipit-source-id: eee5a2a395ca726679c1c4756ed58d4bc6be3cb8
This commit is contained in:
Lorenzo Blasa
2022-05-11 06:56:20 -07:00
committed by Facebook GitHub Bot
parent 2493de75ab
commit 453ec4bd83
2 changed files with 46 additions and 4 deletions

View File

@@ -11,7 +11,7 @@ import process from 'process';
import chalk from 'chalk'; import chalk from 'chalk';
import path from 'path'; import path from 'path';
import {startFlipperServer} from './startFlipperServer'; import {startFlipperServer} from './startFlipperServer';
import {startBaseServer} from './startBaseServer'; import {startServer} from './startServer';
import {startSocketServer} from './startSocketServer'; import {startSocketServer} from './startSocketServer';
import {startDevServer} from './startDevServer'; import {startDevServer} from './startDevServer';
@@ -77,7 +77,7 @@ async function start() {
// Noop // Noop
}; };
const {app, server, socket} = await startBaseServer({ const {app, server, socket} = await startServer({
port: argv.port, port: argv.port,
staticDir, staticDir,
entry: 'index.web.dev.html', entry: 'index.web.dev.html',

View File

@@ -26,7 +26,12 @@ type Config = {
entry: string; entry: string;
}; };
export async function startBaseServer(config: Config): Promise<{ /**
* Orchestrates the creation of the HTTP server, proxy, and web socket.
* @param config Server configuration.
* @returns Returns a promise to the created server, proxy and web socket.
*/
export async function startServer(config: Config): Promise<{
app: Express; app: Express;
server: http.Server; server: http.Server;
socket: WebSocketServer; socket: WebSocketServer;
@@ -40,6 +45,12 @@ export async function startBaseServer(config: Config): Promise<{
}; };
} }
/**
* Creates an express app with configured routing and creates
* a proxy server.
* @param config Server configuration.
* @returns A promise to both app and HTTP server.
*/
async function startHTTPServer( async function startHTTPServer(
config: Config, config: Config,
): Promise<{app: Express; server: http.Server}> { ): Promise<{app: Express; server: http.Server}> {
@@ -67,6 +78,17 @@ async function startHTTPServer(
return startProxyServer(config, app); return startProxyServer(config, app);
} }
/**
* Checks if a socket is in used for given path.
* If the path doesn't exist then is not in use. If it does,
* create a socket client and attempt to connect to it.
* If the connection is established, then there's a process
* already listening. Otherwise, it kind of indicates the
* contrary.
* @param path Path used instead of port number.
* @returns True or false dependning on whether the socket is in
* use or not.
*/
async function checkSocketInUse(path: string): Promise<boolean> { async function checkSocketInUse(path: string): Promise<boolean> {
if (!(await fs.pathExists(path))) { if (!(await fs.pathExists(path))) {
return false; return false;
@@ -92,6 +114,11 @@ async function checkSocketInUse(path: string): Promise<boolean> {
}); });
} }
/**
* Creates a socket path. Used to open the socket at location.
* Format: flipper-server-${userInfo}.sock
* @returns The created socket path.
*/
async function makeSocketPath(): Promise<string> { async function makeSocketPath(): Promise<string> {
const runtimeDir = xdgBasedir.runtime || '/tmp'; const runtimeDir = xdgBasedir.runtime || '/tmp';
await fs.mkdirp(runtimeDir); await fs.mkdirp(runtimeDir);
@@ -111,6 +138,13 @@ async function makeSocketPath(): Promise<string> {
return path; return path;
} }
/**
* Creates and starts the HTTP and proxy servers.
* @param config Server configuration.
* @param app Express app.
* @returns Returns both the app and server configured and
* listening.
*/
async function startProxyServer( async function startProxyServer(
config: Config, config: Config,
app: Express, app: Express,
@@ -118,6 +152,8 @@ async function startProxyServer(
const server = http.createServer(app); const server = http.createServer(app);
// For now, we only support domain socket access on POSIX-like systems. // For now, we only support domain socket access on POSIX-like systems.
// On Windows, a proxy is not created and the server starts
// listening at the specified port.
if (os.platform() === 'win32') { if (os.platform() === 'win32') {
return new Promise((resolve) => { return new Promise((resolve) => {
console.log(`Starting server on http://localhost:${config.port}`); console.log(`Starting server on http://localhost:${config.port}`);
@@ -126,7 +162,6 @@ async function startProxyServer(
} }
const socketPath = await makeSocketPath(); const socketPath = await makeSocketPath();
if (await checkSocketInUse(socketPath)) { if (await checkSocketInUse(socketPath)) {
console.warn( console.warn(
`Cannot start flipper-server because socket ${socketPath} is in use.`, `Cannot start flipper-server because socket ${socketPath} is in use.`,
@@ -165,6 +200,13 @@ async function startProxyServer(
}); });
} }
/**
* Adds a WS to the existing HTTP server.
* @param server HTTP server.
* @param config Server configuration. Port is used to verify
* incoming connections origin.
* @returns Returns the created WS.
*/
function addWebsocket(server: http.Server, config: Config) { function addWebsocket(server: http.Server, config: Config) {
const localhostIPV4 = `localhost:${config.port}`; const localhostIPV4 = `localhost:${config.port}`;
const localhostIPV6 = `[::1]:${config.port}`; const localhostIPV6 = `[::1]:${config.port}`;