From ff1897ee94b2b15bc5cff357e22d36e99f9f1164 Mon Sep 17 00:00:00 2001 From: Sam Frances Date: Fri, 3 Nov 2017 16:30:21 +0000 Subject: [PATCH] Add ability to handle auth modules which export a single function --- other/js/utils.js | 8 ++++++++ other/js/websockify.js | 22 ++++++++++++++++------ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/other/js/utils.js b/other/js/utils.js index 4b16094..5e27449 100644 --- a/other/js/utils.js +++ b/other/js/utils.js @@ -10,6 +10,14 @@ * @param {function} function_or_class */ exports.factorify = function factorify(function_or_class) { + // Must be callable + if (typeof function_or_class !== "function") { + console.log(function_or_class) + throw new TypeError( + "must be called with a function or class constructor" + ); + } + return (...args) => { try { return function_or_class(...args); diff --git a/other/js/websockify.js b/other/js/websockify.js index 6770c3c..50098d4 100755 --- a/other/js/websockify.js +++ b/other/js/websockify.js @@ -166,14 +166,24 @@ if (argv.cert) { } if (argv["auth-plugin"]) { - const auth_plugin_arg = argv["auth-plugin"].split(".") - const plugin_name = auth_plugin_arg.pop(); - const module_path = auth_plugin_arg.join("."); + + // Extract the plugin module, and any exported item from that module + const [ module_path, plugin_name ] = + argv["auth-plugin"] + .replace(/\.([A-z])/, "@$1") + .split("@"); + + // If an exported item is specified as the plugin, get it, otherwise + // use the default export + const plugin_factory = utils.factorify( + plugin_name + ? require(module_path)[plugin_name] + : require(module_path) + ); const auth_source = argv["auth-source"] || undefined; - const auth_plugin = utils.factorify( - require(module_path)[plugin_name] - )(auth_source); + + const auth_plugin = plugin_factory(auth_source); websocket_server_opts = { server: webServer,