UDS/TCP options
Summary: Provide an option to enable/disable TCP connections on flipper-server. The only change at this stage is that Flipper Desktop will use UDS to connect to flipper-server. Reviewed By: passy Differential Revision: D37519656 fbshipit-source-id: 3d02084666fde532ec76134edf8cf6a231060a48
This commit is contained in:
committed by
Facebook GitHub Bot
parent
f46cf2b0ce
commit
646b9d5a5d
@@ -13,7 +13,7 @@ import express, {Express} from 'express';
|
||||
import http, {ServerResponse} from 'http';
|
||||
import path from 'path';
|
||||
import fs from 'fs-extra';
|
||||
import {VerifyClientCallbackSync, WebSocketServer} from 'ws';
|
||||
import {ServerOptions, VerifyClientCallbackSync, WebSocketServer} from 'ws';
|
||||
import {WEBSOCKET_MAX_MESSAGE_SIZE} from '../comms/ServerWebSocket';
|
||||
import {parse} from 'url';
|
||||
import {makeSocketPath, checkSocketInUse} from './utilities';
|
||||
@@ -28,6 +28,7 @@ type Config = {
|
||||
port: number;
|
||||
staticDir: string;
|
||||
entry: string;
|
||||
tcp: boolean;
|
||||
};
|
||||
|
||||
type ReadyForConnections = (
|
||||
@@ -108,6 +109,12 @@ async function startProxyServer(
|
||||
// On Windows, a proxy is not created and the server starts
|
||||
// listening at the specified port.
|
||||
if (os.platform() === 'win32') {
|
||||
if (!config.tcp) {
|
||||
console.error(
|
||||
'No port was supplied and domain socket access is not available for non-POSIX systems, unable to start server',
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
return new Promise((resolve) => {
|
||||
console.log(`Starting server on http://localhost:${config.port}`);
|
||||
const readyForIncomingConnections = (): Promise<void> => {
|
||||
@@ -129,17 +136,22 @@ async function startProxyServer(
|
||||
await fs.rm(socketPath, {force: true});
|
||||
}
|
||||
|
||||
const proxyServer = proxy.createProxyServer({
|
||||
target: {host: 'localhost', port: 0, socketPath},
|
||||
autoRewrite: true,
|
||||
ws: true,
|
||||
});
|
||||
const proxyServer: proxy | undefined = config.tcp
|
||||
? proxy.createProxyServer({
|
||||
target: {host: 'localhost', port: 0, socketPath},
|
||||
autoRewrite: true,
|
||||
ws: true,
|
||||
})
|
||||
: undefined;
|
||||
|
||||
console.log('Starting socket server on ', socketPath);
|
||||
console.log(`Starting proxy server on http://localhost:${config.port}`);
|
||||
if (proxyServer) {
|
||||
console.log(`Starting proxy server on http://localhost:${config.port}`);
|
||||
}
|
||||
|
||||
exitHook(() => {
|
||||
console.log('Shutdown server');
|
||||
proxyServer.close();
|
||||
proxyServer?.close();
|
||||
server.close();
|
||||
|
||||
console.log('Cleaning up socket on exit:', socketPath);
|
||||
@@ -148,7 +160,7 @@ async function startProxyServer(
|
||||
fs.rmSync(socketPath, {force: true});
|
||||
});
|
||||
|
||||
proxyServer.on('error', (err, _req, res) => {
|
||||
proxyServer?.on('error', (err, _req, res) => {
|
||||
console.warn('Error in proxy server:', err);
|
||||
if (res instanceof ServerResponse) {
|
||||
res.writeHead(502, 'Failed to proxy request');
|
||||
@@ -163,7 +175,7 @@ async function startProxyServer(
|
||||
): Promise<void> => {
|
||||
attachSocketServer(socket, serverImpl, companionEnv);
|
||||
return new Promise((resolve) => {
|
||||
proxyServer.listen(config.port);
|
||||
proxyServer?.listen(config.port);
|
||||
server.listen(socketPath, undefined, () => resolve());
|
||||
});
|
||||
};
|
||||
@@ -222,12 +234,15 @@ function addWebsocket(server: http.Server, config: Config) {
|
||||
}
|
||||
};
|
||||
|
||||
const wss = new WebSocketServer({
|
||||
const options: ServerOptions = {
|
||||
noServer: true,
|
||||
maxPayload: WEBSOCKET_MAX_MESSAGE_SIZE,
|
||||
verifyClient,
|
||||
});
|
||||
};
|
||||
if (config.tcp) {
|
||||
options.verifyClient = verifyClient;
|
||||
}
|
||||
|
||||
const wss = new WebSocketServer(options);
|
||||
server.on('upgrade', function upgrade(request, socket, head) {
|
||||
const {pathname} = parse(request.url!);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user