From 20f10c3ce59c1a3fe1871ad44468af271a9b7f20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Steunou?= Date: Mon, 2 Jul 2018 18:22:04 +0200 Subject: [PATCH] Add support for a split server / client configuration * add options `{server: {host, port}, client: {host, port, secure}}` * secondary effect options.server can be a server instance or a definition object * all options are set in importance order, compatiblity is preserved (at least it should be) * for clarity introduce websocketServer & websocketClient computed options --- lib/client/socket.js | 6 +++--- lib/options.js | 33 +++++++++++++++++++-------------- lib/socket-server.js | 5 ++--- 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/lib/client/socket.js b/lib/client/socket.js index 2ab36af..ec05877 100644 --- a/lib/client/socket.js +++ b/lib/client/socket.js @@ -6,11 +6,11 @@ const maxRetries = 10; let retry = maxRetries; module.exports = function connect(options, handler) { - const { host } = options.webSocket; + const { host, port, secure } = options.webSocketClient; const socketUrl = url.format({ - protocol: options.https ? 'wss' : 'ws', + protocol: secure ? 'wss' : 'ws', hostname: host === '*' ? window.location.hostname : host, - port: options.webSocket.port, + port: port, slashes: true, }); diff --git a/lib/options.js b/lib/options.js index 739e321..861a369 100644 --- a/lib/options.js +++ b/lib/options.js @@ -24,6 +24,7 @@ const defaults = { warnings: true, }, server: null, + client: {}, stats: { context: process.cwd(), }, @@ -59,24 +60,28 @@ module.exports = (opts = {}) => { ); } - const { server } = options; + const { server, client } = options; + const hasServer = server && server.listening; + options.webSocketServer = {}; - if (server && !server.listening) { - throw new HotClientError( - '`options.server` must be a running/listening net.Server instance' - ); - } else if (server) { - options.webSocket = { - host: server.address().address, - port: server.address().port, - }; + if (hasServer && server instanceof HttpsServer && typeof options.https === 'undefined') { + options.https = true; + } - if (server instanceof HttpsServer && typeof options.https === 'undefined') { - options.https = true; - } + if (hasServer) { + options.webSocketServer = { server }; } else { - options.webSocket = { host: options.host.client, port: options.port }; + options.webSocketServer = { + host: server && server.host || options.host.server, + port: server && server.port || options.port, + } } + options.webSocketClient = { + host: client.host || options.host.client || (hasServer && server.address().address) || '*', + port: client.port || (hasServer && server.address().port) || options.port, + secure: typeof client.secure !== 'undefined' ? client.secure : options.https, + }; + return options; }; diff --git a/lib/socket-server.js b/lib/socket-server.js index 9d6f1bd..2e4b602 100644 --- a/lib/socket-server.js +++ b/lib/socket-server.js @@ -3,9 +3,8 @@ const strip = require('strip-ansi'); const WebSocket = require('ws'); function getServer(options) { - const { host, log, port, server } = options; - const wssOptions = server ? { server } : { host: host.server, port }; - const wss = new WebSocket.Server(wssOptions); + const { log, webSocketServer } = options; + const wss = new WebSocket.Server(webSocketServer); onConnection(wss, options); onError(wss, options);