Refactor server implementation for WebSockets
Summary: Standardize WS implementation for JS environments. Why do we need a separate server implementation for browsers? Browser targets cannot authenticate via the default certificate exchange flow. For browser targets we verify the origin instead. Moreover, for already forgotten reasons the initial implementation of the WS server for browsers used a different kind of message structure and added extra `connect`/`disconnect` messages. After examination, it seems the `connect`/`disconnect` flow is redundant. Major changes: 1. Updated class hierarchy for WS server implementations. 2. Updated browser WS server to support the modern and the legacy protocols. 3. Now a websocket connection with the device is closed on error. The idea is it is highly unlikely to handle any subsequent messages properly once we observe an error. It is better to bail and reconnect. What do you think? Reviewed By: mweststrate Differential Revision: D31532172 fbshipit-source-id: f86aa63a40efe4d5263353cc124fac8c63b80e45
This commit is contained in:
committed by
Facebook GitHub Bot
parent
6bd4a07286
commit
37498ad5a9
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its 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 {
|
||||
ClientResponseType,
|
||||
GetBackgroundPluginsMessage,
|
||||
GetPluginsMessage,
|
||||
} from 'flipper-common';
|
||||
import WebSocket from 'ws';
|
||||
import WebSocketClientConnection from './WebSocketClientConnection';
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
* Default `WebSocketClientConnection` should be used instead.
|
||||
* See BrowserServerWebSocket.handleMessage.
|
||||
*/
|
||||
export class BrowserClientConnection extends WebSocketClientConnection {
|
||||
public legacyFormat = false;
|
||||
|
||||
private static isGetPluginsCall(data: object): data is GetPluginsMessage {
|
||||
return (data as GetPluginsMessage).method === 'getPlugins';
|
||||
}
|
||||
private static isGetBackgroundPluginsCall(
|
||||
data: object,
|
||||
): data is GetBackgroundPluginsMessage {
|
||||
return (
|
||||
(data as GetBackgroundPluginsMessage).method === 'getBackgroundPlugins'
|
||||
);
|
||||
}
|
||||
|
||||
constructor(ws: WebSocket, public plugins?: string[]) {
|
||||
super(ws);
|
||||
}
|
||||
|
||||
async sendExpectResponse(data: object): Promise<ClientResponseType> {
|
||||
if (BrowserClientConnection.isGetPluginsCall(data) && this.plugins) {
|
||||
return {
|
||||
success: {plugins: this.plugins},
|
||||
length: 0,
|
||||
};
|
||||
}
|
||||
|
||||
if (
|
||||
BrowserClientConnection.isGetBackgroundPluginsCall(data) &&
|
||||
this.plugins
|
||||
) {
|
||||
return {
|
||||
success: {plugins: []},
|
||||
length: 0,
|
||||
};
|
||||
}
|
||||
|
||||
return super.sendExpectResponse(data);
|
||||
}
|
||||
|
||||
protected serializeData(data: object): string {
|
||||
return super.serializeData(this.legacyFormat ? {payload: data} : data);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user