Fix WebSocket server not starting on Windows

Summary: When Flipper starts with Flipper Server enabled, on Windows we forgot to attach the WebSocket handler. It led to a white screen on Electron or to connection timeout messages on Flipper Server.

Reviewed By: passy, lblasa

Differential Revision: D40679781

fbshipit-source-id: 1c8df8012efc54077409eb8891b1d82ddaf16689
This commit is contained in:
Andrey Goncharov
2022-10-26 03:36:04 -07:00
committed by Facebook GitHub Bot
parent 13e06803c9
commit 3e88a53a3f
2 changed files with 21 additions and 10 deletions

View File

@@ -50,6 +50,7 @@ import {initCompanionEnv} from 'flipper-server-companion';
import ReconnectingWebSocket from 'reconnecting-websocket'; import ReconnectingWebSocket from 'reconnecting-websocket';
import WS from 'ws'; import WS from 'ws';
import {Module} from 'module'; import {Module} from 'module';
import os from 'os';
Module.prototype.require = wrapRequire(Module.prototype.require); Module.prototype.require = wrapRequire(Module.prototype.require);
enableMapSet(); enableMapSet();
@@ -76,7 +77,7 @@ async function getKeytarModule(staticPath: string): Promise<KeytarModule> {
return keytar; return keytar;
} }
async function getExternalServer(path: string) { async function getExternalServer(url: string) {
const options = { const options = {
WebSocket: class WSWithUnixDomainSocketSupport extends WS { WebSocket: class WSWithUnixDomainSocketSupport extends WS {
constructor(url: string, protocols: string | string[]) { constructor(url: string, protocols: string | string[]) {
@@ -86,7 +87,7 @@ async function getExternalServer(path: string) {
} }
}, },
}; };
const socket = new ReconnectingWebSocket(`ws+unix://${path}`, [], options); const socket = new ReconnectingWebSocket(url, [], options);
const server = await createFlipperServerWithSocket( const server = await createFlipperServerWithSocket(
socket, socket,
(_state: FlipperServerState) => {}, (_state: FlipperServerState) => {},
@@ -116,13 +117,15 @@ async function getFlipperServer(
const settings = await loadSettings(); const settings = await loadSettings();
const socketPath = await makeSocketPath(); const socketPath = await makeSocketPath();
let serverRunning = await checkSocketInUse(socketPath); let externalServerConnectionURL = `ws+unix://${socketPath}`;
// On Windows this is going to return false at all times as we do not use domain sockets there.
let serverRunning = await checkSocketInUse(socketPath);
if (serverRunning) { if (serverRunning) {
console.info( console.info(
'flipper-server: currently running/listening, attempt to shutdown', 'flipper-server: currently running/listening, attempt to shutdown',
); );
const server = await getExternalServer(socketPath); const server = await getExternalServer(externalServerConnectionURL);
await server.exec('shutdown').catch(() => { await server.exec('shutdown').catch(() => {
/** shutdown will ultimately make this request fail, ignore error. */ /** shutdown will ultimately make this request fail, ignore error. */
}); });
@@ -160,11 +163,16 @@ async function getFlipperServer(
if (!serverRunning) { if (!serverRunning) {
console.info('flipper-server: not running/listening, start'); console.info('flipper-server: not running/listening, start');
const port = 52342;
if (os.platform() === 'win32') {
externalServerConnectionURL = `ws://localhost:${port}`;
}
const {readyForIncomingConnections} = await startServer({ const {readyForIncomingConnections} = await startServer({
staticDir: staticPath, staticDir: staticPath,
entry: 'index.web.dev.html', entry: 'index.web.dev.html',
tcp: false, tcp: false,
port: 52342, port,
}); });
const server = await startFlipperServer( const server = await startFlipperServer(
@@ -182,7 +190,7 @@ async function getFlipperServer(
await readyForIncomingConnections(server, companionEnv); await readyForIncomingConnections(server, companionEnv);
} }
return getExternalServer(socketPath); return getExternalServer(externalServerConnectionURL);
} }
return getEmbeddedServer(); return getEmbeddedServer();
} }

View File

@@ -110,14 +110,17 @@ async function startProxyServer(
// listening at the specified port. // listening at the specified port.
if (os.platform() === 'win32') { if (os.platform() === 'win32') {
if (!config.tcp) { if (!config.tcp) {
console.error( console.warn(
'No port was supplied and domain socket access is not available for non-POSIX systems, unable to start server', 'No port was supplied and domain socket access is not available for non-POSIX systems, falling back to TCP',
); );
process.exit(1);
} }
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}`);
const readyForIncomingConnections = (): Promise<void> => { const readyForIncomingConnections = (
serverImpl: FlipperServerImpl,
companionEnv: FlipperServerCompanionEnv,
): Promise<void> => {
attachSocketServer(socket, serverImpl, companionEnv);
return new Promise((resolve) => { return new Promise((resolve) => {
server.listen(config.port, undefined, () => resolve()); server.listen(config.port, undefined, () => resolve());
}); });