From 4a2ff7d02aedc3157413145040bc7fd115070660 Mon Sep 17 00:00:00 2001 From: Rolando Santamaria Maso Date: Sun, 5 Dec 2021 10:36:58 +0100 Subject: [PATCH] making faye-websocket an optional dependency --- index.js | 17 +++++++++++++---- lib/ws-proxy.js | 3 ++- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 9a48d6d..bcebd9e 100644 --- a/index.js +++ b/index.js @@ -35,10 +35,19 @@ const gateway = (opts) => { }) // processing websocket routes - registerWebSocketRoutes({ - routes: opts.routes.filter(route => route.proxyType === 'websocket'), - server: router.getServer() - }) + const wsRoutes = opts.routes.filter(route => route.proxyType === 'websocket') + if (wsRoutes.length) { + if (typeof router.getServer !== 'function') { + throw new Error( + 'Unable to retrieve the HTTP server instance. ' + + 'If you are not using restana, make sure to provide an "app.getServer()" alternative method!' + ) + } + registerWebSocketRoutes({ + routes: wsRoutes, + server: router.getServer() + }) + } // processing non-websocket routes opts.routes.filter(route => route.proxyType !== 'websocket').forEach(route => { diff --git a/lib/ws-proxy.js b/lib/ws-proxy.js index 8fc879b..144110b 100644 --- a/lib/ws-proxy.js +++ b/lib/ws-proxy.js @@ -1,9 +1,10 @@ 'use strict' -const WebSocket = require('faye-websocket') const { onOpenNoOp } = require('./default-hooks').websocket module.exports = (config) => { + const WebSocket = require('faye-websocket') + const { routes, server } = config const prefix2route = new Map()