flipper-server refactor

Summary:
This changes moves most of the functionality found in flipper-server to flipper-server-core.

flipper-server will mostly be a package that wraps around flipper-server-core. Staying in flipper-server:
- Command line args
- Orchestration to start the necessary servers

Reviewed By: aigoncharov

Differential Revision: D36807087

fbshipit-source-id: f29002c7cc5d08b8c5184fdaaa02ba22562a9f45
This commit is contained in:
Lorenzo Blasa
2022-06-07 02:42:16 -07:00
committed by Facebook GitHub Bot
parent c88e769013
commit 9cc8e4076f
9 changed files with 78 additions and 53 deletions

View File

@@ -21,6 +21,7 @@
"flipper-doctor": "0.0.0",
"flipper-plugin": "0.0.0",
"flipper-plugin-lib": "0.0.0",
"flipper-server-companion": "0.0.0",
"form-data": "^4.0.0",
"fs-extra": "^10.1.0",
"invariant": "^2.2.4",
@@ -44,6 +45,8 @@
"xdg-basedir": "^4.0.0"
},
"devDependencies": {
"@types/express": "^4.17.13",
"@types/http-proxy": "^1.17.8",
"@types/archiver": "^5.3.1",
"@types/invariant": "^2.2.35",
"@types/memorystream": "^0.3.0",
@@ -56,7 +59,10 @@
"@types/tmp": "^0.2.3",
"@types/which": "^2.0.1",
"@types/ws": "^8.5.3",
"memorystream": "^0.3.1"
"memorystream": "^0.3.1",
"exit-hook": "^2.1.1",
"http-proxy": "^1.18.1",
"express": "^4.17.3"
},
"peerDependencies": {},
"scripts": {

View File

@@ -0,0 +1,21 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import GKImplementation from './fb-stubs/GK';
let loaded = false;
export function getGatekeepers(username: string): Record<string, boolean> {
if (!loaded) {
// this starts fetching gatekeepers, note that they will only be available on next restart!
GKImplementation.init(username);
loaded = true;
}
return GKImplementation.allGKs();
}

View File

@@ -13,19 +13,11 @@ export {loadLauncherSettings} from './utils/launcherSettings';
export {loadProcessConfig} from './utils/processConfig';
export {getEnvironmentInfo} from './utils/environmentInfo';
export * from './utils/tail';
import GKImplementation from './fb-stubs/GK';
export {getGatekeepers} from './gk';
export {setupPrefetcher} from './fb-stubs/Prefetcher';
let loaded = false;
export function getGatekeepers(username: string): Record<string, boolean> {
if (!loaded) {
// this starts fetching gatekeepers, note that they will only be available on next restart!
GKImplementation.init(username);
loaded = true;
}
return GKImplementation.allGKs();
}
export * from './server/attachSocketServer';
export * from './server/startFlipperServer';
export * from './server/startServer';
export * from './server/utilities';
export {WEBSOCKET_MAX_MESSAGE_SIZE} from './comms/ServerWebSocket';

View File

@@ -7,7 +7,6 @@
* @format
*/
import chalk from 'chalk';
import {
ClientWebSocketMessage,
ExecResponseWebSocketMessage,
@@ -19,7 +18,7 @@ import {
getLogger,
CompanionEventWebSocketMessage,
} from 'flipper-common';
import {FlipperServerImpl} from 'flipper-server-core';
import {FlipperServerImpl} from '../FlipperServerImpl';
import {WebSocketServer} from 'ws';
import {
FlipperServerCompanion,
@@ -43,7 +42,7 @@ export function attachSocketServer(
` ${req.socket.remoteAddress}:${req.socket.remotePort}`) ||
'';
console.log(chalk.green(`Client connected${clientAddress}`));
console.log(`Client connected${clientAddress}`);
let connected = true;
@@ -209,14 +208,14 @@ export function attachSocketServer(
});
client.on('close', () => {
console.log(chalk.red(`Client disconnected ${clientAddress}`));
console.log(`Client disconnected ${clientAddress}`);
connected = false;
flipperServer.offAny(onServerEvent);
flipperServerCompanion?.destroyAll();
});
client.on('error', (e) => {
console.error(chalk.red(`Socket error ${clientAddress}`), e);
console.error(`Socket error ${clientAddress}`, e);
connected = false;
flipperServer.offAny(onServerEvent);
flipperServerCompanion?.destroyAll();

View File

@@ -8,17 +8,16 @@
*/
import os from 'os';
import {
FlipperServerImpl,
getGatekeepers,
loadLauncherSettings,
loadProcessConfig,
loadSettings,
getEnvironmentInfo,
} from 'flipper-server-core';
import {parseEnvironmentVariables, isTest, getLogger} from 'flipper-common';
import {parseEnvironmentVariables, getLogger} from 'flipper-common';
import path from 'path';
import fs from 'fs-extra';
import {KeytarModule} from '../utils/keytar';
import {FlipperServerImpl} from '../FlipperServerImpl';
import {getEnvironmentInfo} from '../utils/environmentInfo';
import {getGatekeepers} from '../gk';
import {loadLauncherSettings} from '../utils/launcherSettings';
import {loadProcessConfig} from '../utils/processConfig';
import {loadSettings} from '../utils/settings';
/**
* Creates an instance of FlipperServer (FlipperServerImpl). This is the
@@ -34,6 +33,7 @@ export async function startFlipperServer(
staticPath: string,
settingsString: string,
enableLauncherSettings: boolean,
keytarModule: KeytarModule,
): Promise<FlipperServerImpl> {
const execPath = process.execPath;
const appPath = rootDir;
@@ -48,25 +48,6 @@ export async function startFlipperServer(
desktopPath = os.homedir();
}
let keytar: any = undefined;
try {
if (!isTest()) {
const keytarPath = path.join(
staticPath,
'native-modules',
`keytar-${process.platform}-${process.arch}.node`,
);
if (!(await fs.pathExists(keytarPath))) {
throw new Error(
`Keytar binary does not exist for platform ${process.platform}-${process.arch}`,
);
}
keytar = electronRequire(keytarPath);
}
} catch (e) {
console.error('Failed to load keytar:', e);
}
const environmentInfo = await getEnvironmentInfo(appPath, isProduction, true);
return new FlipperServerImpl(
@@ -89,6 +70,6 @@ export async function startFlipperServer(
validWebSocketOrigins: ['localhost:', 'http://localhost:'],
},
getLogger(),
keytar,
keytarModule,
);
}

View File

@@ -14,7 +14,7 @@ import http, {ServerResponse} from 'http';
import path from 'path';
import fs from 'fs-extra';
import {VerifyClientCallbackSync, WebSocketServer} from 'ws';
import {WEBSOCKET_MAX_MESSAGE_SIZE} from 'flipper-server-core';
import {WEBSOCKET_MAX_MESSAGE_SIZE} from '../comms/ServerWebSocket';
import {parse} from 'url';
import {makeSocketPath, checkSocketInUse} from './utilities';

View File

@@ -21,6 +21,9 @@
{
"path": "../flipper-plugin"
},
{
"path": "../flipper-server-companion"
},
{
"path": "../plugin-lib"
},

View File

@@ -10,15 +10,18 @@
import process from 'process';
import chalk from 'chalk';
import path from 'path';
import {startFlipperServer} from './startFlipperServer';
import {startServer} from './startServer';
import {attachSocketServer} from './attachSocketServer';
import {attachDevServer} from './attachDevServer';
import {initializeLogger} from './logger';
import fs from 'fs-extra';
import yargs from 'yargs';
import open from 'open';
import {initCompanionEnv} from 'flipper-server-companion';
import {
attachSocketServer,
startFlipperServer,
startServer,
} from 'flipper-server-core';
import {isTest} from 'flipper-common';
const argv = yargs
.usage('yarn flipper-server [args]')
@@ -75,6 +78,25 @@ const staticDir = path.join(rootDir, 'static');
async function start() {
initializeLogger(staticDir);
let keytar: any = undefined;
try {
if (!isTest()) {
const keytarPath = path.join(
staticDir,
'native-modules',
`keytar-${process.platform}-${process.arch}.node`,
);
if (!(await fs.pathExists(keytarPath))) {
throw new Error(
`Keytar binary does not exist for platform ${process.platform}-${process.arch}`,
);
}
keytar = electronRequire(keytarPath);
}
} catch (e) {
console.error('Failed to load keytar:', e);
}
const {app, server, socket} = await startServer({
port: argv.port,
staticDir,
@@ -86,6 +108,7 @@ async function start() {
staticDir,
argv.settingsString,
argv.launcherSettings,
keytar,
);
const companionEnv = await initCompanionEnv(flipperServer);
if (argv.failFast) {