URL and Token provider

Summary:
Use an URLProvider paired with a token provider for attempts to establish a websocket connection.

This gives extra flexibility whenever a token is not available or changes as the ReconnectingWebSocket will call the URLProvider after each unsuccessful connection.

Reviewed By: antonk52

Differential Revision: D50220329

fbshipit-source-id: f53993a90c9c0f64bf213019b6b8af5fa818048d
This commit is contained in:
Lorenzo Blasa
2023-10-12 04:29:13 -07:00
committed by Facebook GitHub Bot
parent 8cc35d74ef
commit db4d15ed05
2 changed files with 29 additions and 20 deletions

View File

@@ -30,10 +30,15 @@ export type {FlipperServer, FlipperServerCommands, FlipperServerExecOptions};
export function createFlipperServer(
host: string,
port: number,
args: URLSearchParams,
tokenProvider: () => Promise<string | null | undefined>,
onStateChange: (state: FlipperServerState) => void,
): Promise<FlipperServer> {
const socket = new ReconnectingWebSocket(`ws://${host}:${port}?${args}`);
const URLProvider = async () => {
const token = await tokenProvider();
return `ws://${host}:${port}?token=${token}`;
};
const socket = new ReconnectingWebSocket(URLProvider);
return createFlipperServerWithSocket(socket as WebSocket, onStateChange);
}
@@ -49,7 +54,7 @@ export function createFlipperServerWithSocket(
reject(
new Error(
`Failed to connect to the server in a timely manner.
It may be unresponsive. Run the following from the terminal
It may be unresponsive. Run the following from the terminal
'sudo kill -9 $(lsof -t -i :52342)' as to kill any existing running instance, if any.`,
),
);