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:
committed by
Facebook GitHub Bot
parent
c88e769013
commit
9cc8e4076f
@@ -21,6 +21,7 @@
|
|||||||
"flipper-doctor": "0.0.0",
|
"flipper-doctor": "0.0.0",
|
||||||
"flipper-plugin": "0.0.0",
|
"flipper-plugin": "0.0.0",
|
||||||
"flipper-plugin-lib": "0.0.0",
|
"flipper-plugin-lib": "0.0.0",
|
||||||
|
"flipper-server-companion": "0.0.0",
|
||||||
"form-data": "^4.0.0",
|
"form-data": "^4.0.0",
|
||||||
"fs-extra": "^10.1.0",
|
"fs-extra": "^10.1.0",
|
||||||
"invariant": "^2.2.4",
|
"invariant": "^2.2.4",
|
||||||
@@ -44,6 +45,8 @@
|
|||||||
"xdg-basedir": "^4.0.0"
|
"xdg-basedir": "^4.0.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@types/express": "^4.17.13",
|
||||||
|
"@types/http-proxy": "^1.17.8",
|
||||||
"@types/archiver": "^5.3.1",
|
"@types/archiver": "^5.3.1",
|
||||||
"@types/invariant": "^2.2.35",
|
"@types/invariant": "^2.2.35",
|
||||||
"@types/memorystream": "^0.3.0",
|
"@types/memorystream": "^0.3.0",
|
||||||
@@ -56,7 +59,10 @@
|
|||||||
"@types/tmp": "^0.2.3",
|
"@types/tmp": "^0.2.3",
|
||||||
"@types/which": "^2.0.1",
|
"@types/which": "^2.0.1",
|
||||||
"@types/ws": "^8.5.3",
|
"@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": {},
|
"peerDependencies": {},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
21
desktop/flipper-server-core/src/gk.tsx
Normal file
21
desktop/flipper-server-core/src/gk.tsx
Normal 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();
|
||||||
|
}
|
||||||
@@ -13,19 +13,11 @@ export {loadLauncherSettings} from './utils/launcherSettings';
|
|||||||
export {loadProcessConfig} from './utils/processConfig';
|
export {loadProcessConfig} from './utils/processConfig';
|
||||||
export {getEnvironmentInfo} from './utils/environmentInfo';
|
export {getEnvironmentInfo} from './utils/environmentInfo';
|
||||||
export * from './utils/tail';
|
export * from './utils/tail';
|
||||||
|
export {getGatekeepers} from './gk';
|
||||||
import GKImplementation from './fb-stubs/GK';
|
|
||||||
export {setupPrefetcher} from './fb-stubs/Prefetcher';
|
export {setupPrefetcher} from './fb-stubs/Prefetcher';
|
||||||
|
export * from './server/attachSocketServer';
|
||||||
let loaded = false;
|
export * from './server/startFlipperServer';
|
||||||
|
export * from './server/startServer';
|
||||||
export function getGatekeepers(username: string): Record<string, boolean> {
|
export * from './server/utilities';
|
||||||
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 {WEBSOCKET_MAX_MESSAGE_SIZE} from './comms/ServerWebSocket';
|
export {WEBSOCKET_MAX_MESSAGE_SIZE} from './comms/ServerWebSocket';
|
||||||
|
|||||||
@@ -7,7 +7,6 @@
|
|||||||
* @format
|
* @format
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import chalk from 'chalk';
|
|
||||||
import {
|
import {
|
||||||
ClientWebSocketMessage,
|
ClientWebSocketMessage,
|
||||||
ExecResponseWebSocketMessage,
|
ExecResponseWebSocketMessage,
|
||||||
@@ -19,7 +18,7 @@ import {
|
|||||||
getLogger,
|
getLogger,
|
||||||
CompanionEventWebSocketMessage,
|
CompanionEventWebSocketMessage,
|
||||||
} from 'flipper-common';
|
} from 'flipper-common';
|
||||||
import {FlipperServerImpl} from 'flipper-server-core';
|
import {FlipperServerImpl} from '../FlipperServerImpl';
|
||||||
import {WebSocketServer} from 'ws';
|
import {WebSocketServer} from 'ws';
|
||||||
import {
|
import {
|
||||||
FlipperServerCompanion,
|
FlipperServerCompanion,
|
||||||
@@ -43,7 +42,7 @@ export function attachSocketServer(
|
|||||||
` ${req.socket.remoteAddress}:${req.socket.remotePort}`) ||
|
` ${req.socket.remoteAddress}:${req.socket.remotePort}`) ||
|
||||||
'';
|
'';
|
||||||
|
|
||||||
console.log(chalk.green(`Client connected${clientAddress}`));
|
console.log(`Client connected${clientAddress}`);
|
||||||
|
|
||||||
let connected = true;
|
let connected = true;
|
||||||
|
|
||||||
@@ -209,14 +208,14 @@ export function attachSocketServer(
|
|||||||
});
|
});
|
||||||
|
|
||||||
client.on('close', () => {
|
client.on('close', () => {
|
||||||
console.log(chalk.red(`Client disconnected ${clientAddress}`));
|
console.log(`Client disconnected ${clientAddress}`);
|
||||||
connected = false;
|
connected = false;
|
||||||
flipperServer.offAny(onServerEvent);
|
flipperServer.offAny(onServerEvent);
|
||||||
flipperServerCompanion?.destroyAll();
|
flipperServerCompanion?.destroyAll();
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on('error', (e) => {
|
client.on('error', (e) => {
|
||||||
console.error(chalk.red(`Socket error ${clientAddress}`), e);
|
console.error(`Socket error ${clientAddress}`, e);
|
||||||
connected = false;
|
connected = false;
|
||||||
flipperServer.offAny(onServerEvent);
|
flipperServer.offAny(onServerEvent);
|
||||||
flipperServerCompanion?.destroyAll();
|
flipperServerCompanion?.destroyAll();
|
||||||
@@ -8,17 +8,16 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import os from 'os';
|
import os from 'os';
|
||||||
import {
|
import {parseEnvironmentVariables, getLogger} from 'flipper-common';
|
||||||
FlipperServerImpl,
|
|
||||||
getGatekeepers,
|
|
||||||
loadLauncherSettings,
|
|
||||||
loadProcessConfig,
|
|
||||||
loadSettings,
|
|
||||||
getEnvironmentInfo,
|
|
||||||
} from 'flipper-server-core';
|
|
||||||
import {parseEnvironmentVariables, isTest, getLogger} from 'flipper-common';
|
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import fs from 'fs-extra';
|
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
|
* Creates an instance of FlipperServer (FlipperServerImpl). This is the
|
||||||
@@ -34,6 +33,7 @@ export async function startFlipperServer(
|
|||||||
staticPath: string,
|
staticPath: string,
|
||||||
settingsString: string,
|
settingsString: string,
|
||||||
enableLauncherSettings: boolean,
|
enableLauncherSettings: boolean,
|
||||||
|
keytarModule: KeytarModule,
|
||||||
): Promise<FlipperServerImpl> {
|
): Promise<FlipperServerImpl> {
|
||||||
const execPath = process.execPath;
|
const execPath = process.execPath;
|
||||||
const appPath = rootDir;
|
const appPath = rootDir;
|
||||||
@@ -48,25 +48,6 @@ export async function startFlipperServer(
|
|||||||
desktopPath = os.homedir();
|
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);
|
const environmentInfo = await getEnvironmentInfo(appPath, isProduction, true);
|
||||||
|
|
||||||
return new FlipperServerImpl(
|
return new FlipperServerImpl(
|
||||||
@@ -89,6 +70,6 @@ export async function startFlipperServer(
|
|||||||
validWebSocketOrigins: ['localhost:', 'http://localhost:'],
|
validWebSocketOrigins: ['localhost:', 'http://localhost:'],
|
||||||
},
|
},
|
||||||
getLogger(),
|
getLogger(),
|
||||||
keytar,
|
keytarModule,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -14,7 +14,7 @@ import http, {ServerResponse} from 'http';
|
|||||||
import path from 'path';
|
import path from 'path';
|
||||||
import fs from 'fs-extra';
|
import fs from 'fs-extra';
|
||||||
import {VerifyClientCallbackSync, WebSocketServer} from 'ws';
|
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 {parse} from 'url';
|
||||||
import {makeSocketPath, checkSocketInUse} from './utilities';
|
import {makeSocketPath, checkSocketInUse} from './utilities';
|
||||||
|
|
||||||
@@ -21,6 +21,9 @@
|
|||||||
{
|
{
|
||||||
"path": "../flipper-plugin"
|
"path": "../flipper-plugin"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"path": "../flipper-server-companion"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"path": "../plugin-lib"
|
"path": "../plugin-lib"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -10,15 +10,18 @@
|
|||||||
import process from 'process';
|
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 {startServer} from './startServer';
|
|
||||||
import {attachSocketServer} from './attachSocketServer';
|
|
||||||
import {attachDevServer} from './attachDevServer';
|
import {attachDevServer} from './attachDevServer';
|
||||||
import {initializeLogger} from './logger';
|
import {initializeLogger} from './logger';
|
||||||
|
import fs from 'fs-extra';
|
||||||
import yargs from 'yargs';
|
import yargs from 'yargs';
|
||||||
import open from 'open';
|
import open from 'open';
|
||||||
import {initCompanionEnv} from 'flipper-server-companion';
|
import {initCompanionEnv} from 'flipper-server-companion';
|
||||||
|
import {
|
||||||
|
attachSocketServer,
|
||||||
|
startFlipperServer,
|
||||||
|
startServer,
|
||||||
|
} from 'flipper-server-core';
|
||||||
|
import {isTest} from 'flipper-common';
|
||||||
|
|
||||||
const argv = yargs
|
const argv = yargs
|
||||||
.usage('yarn flipper-server [args]')
|
.usage('yarn flipper-server [args]')
|
||||||
@@ -75,6 +78,25 @@ const staticDir = path.join(rootDir, 'static');
|
|||||||
async function start() {
|
async function start() {
|
||||||
initializeLogger(staticDir);
|
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({
|
const {app, server, socket} = await startServer({
|
||||||
port: argv.port,
|
port: argv.port,
|
||||||
staticDir,
|
staticDir,
|
||||||
@@ -86,6 +108,7 @@ async function start() {
|
|||||||
staticDir,
|
staticDir,
|
||||||
argv.settingsString,
|
argv.settingsString,
|
||||||
argv.launcherSettings,
|
argv.launcherSettings,
|
||||||
|
keytar,
|
||||||
);
|
);
|
||||||
const companionEnv = await initCompanionEnv(flipperServer);
|
const companionEnv = await initCompanionEnv(flipperServer);
|
||||||
if (argv.failFast) {
|
if (argv.failFast) {
|
||||||
|
|||||||
Reference in New Issue
Block a user