From f68a04b05be25c27e877708d78662966249364ef Mon Sep 17 00:00:00 2001 From: Rafael Oleza Date: Mon, 4 Jun 2018 02:22:44 -0700 Subject: [PATCH] Remove duplicated attachWebsocketServer module Reviewed By: jeanlauliac Differential Revision: D8248752 fbshipit-source-id: b829de4b0d75d154dd80196b589fe70b0050be4d --- local-cli/server/runServer.js | 2 +- .../server/util/attachWebsocketServer.js | 76 ------------------- 2 files changed, 1 insertion(+), 77 deletions(-) delete mode 100644 local-cli/server/util/attachWebsocketServer.js diff --git a/local-cli/server/runServer.js b/local-cli/server/runServer.js index e9b5f3e35..ed2af28da 100644 --- a/local-cli/server/runServer.js +++ b/local-cli/server/runServer.js @@ -18,7 +18,7 @@ const HmrServer = require('metro/src/HmrServer'); const {Terminal} = require('metro-core'); -const attachWebsocketServer = require('./util/attachWebsocketServer'); +const attachWebsocketServer = require('metro/src/lib/attachWebsocketServer'); const compression = require('compression'); const connect = require('connect'); const copyToClipBoardMiddleware = require('./middleware/copyToClipBoardMiddleware'); diff --git a/local-cli/server/util/attachWebsocketServer.js b/local-cli/server/util/attachWebsocketServer.js deleted file mode 100644 index 66f8fe953..000000000 --- a/local-cli/server/util/attachWebsocketServer.js +++ /dev/null @@ -1,76 +0,0 @@ -/** - * Copyright (c) 2015-present, Facebook, Inc. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @format - * @flow - */ - -'use strict'; - -import type {Server as HTTPServer} from 'http'; -import type {Server as HTTPSServer} from 'https'; - -type WebsocketServiceInterface = { - +onClientConnect: ( - url: string, - sendFn: (data: string) => mixed, - ) => Promise, - +onClientDisconnect?: (client: T) => mixed, - +onClientError?: (client: T, e: Error) => mixed, - +onClientMessage?: (client: T, message: string) => mixed, -}; - -type HMROptions = { - httpServer: HTTPServer | HTTPSServer, - websocketServer: WebsocketServiceInterface, - path: string, -}; - -/** - * Attaches a WebSocket based connection to the Packager to expose - * Hot Module Replacement updates to the simulator. - */ -function attachWebsocketServer({ - httpServer, - websocketServer, - path, -}: HMROptions) { - const WebSocketServer = require('ws').Server; - const wss = new WebSocketServer({ - server: httpServer, - path: path, - }); - - wss.on('connection', async ws => { - let connected = true; - const url = ws.upgradeReq.url; - - const sendFn = (...args) => { - if (connected) { - ws.send(...args); - } - }; - - const client = await websocketServer.onClientConnect(url, sendFn); - - ws.on('error', e => { - websocketServer.onClientError && websocketServer.onClientError(client, e); - }); - - ws.on('close', () => { - websocketServer.onClientDisconnect && - websocketServer.onClientDisconnect(client); - connected = false; - }); - - ws.on('message', message => { - websocketServer.onClientMessage && - websocketServer.onClientMessage(client, message); - }); - }); -} - -module.exports = attachWebsocketServer;