signalk-server
Version:
An implementation of a [Signal K](http://signalk.org) server for boats.
89 lines (88 loc) • 3.81 kB
JavaScript
;
/*
* Copyright 2017 Teppo Kurki <teppo.kurki@iki.fi>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = __importDefault(require("express"));
const fs_1 = __importDefault(require("fs"));
const lodash_1 = require("lodash");
const path_1 = __importDefault(require("path"));
const constants_1 = require("../constants");
const debug_1 = require("../debug");
const modules_1 = require("../modules");
const debug = (0, debug_1.createDebug)('signalk-server:interfaces:webapps');
const PLUGIN_KEYWORDS = [
'signalk-node-server-plugin',
'signalk-wasm-plugin'
];
function isPluginWebapp(metadata) {
return (metadata.keywords?.some((k) => PLUGIN_KEYWORDS.includes(k)) ?? false);
}
function isPluginEnabled(app, metadata) {
if (!app.plugins) {
return true;
}
const plugin = app.plugins.find((p) => p.packageName === metadata.name);
if (!plugin) {
return true;
}
const options = app.getPluginOptions?.(plugin.id);
return options?.enabled ?? false;
}
function filterEnabledWebapps(app, webapps) {
return webapps.filter((webapp) => !isPluginWebapp(webapp) || isPluginEnabled(app, webapp));
}
function mountWebModules(app, keyword) {
debug(`mountWebModules:${keyword}`);
const modules = (0, modules_1.modulesWithKeyword)(app.config, keyword);
modules.forEach((moduleData) => {
let webappPath = path_1.default.join(moduleData.location, moduleData.module);
if (fs_1.default.existsSync(webappPath + '/public/')) {
webappPath += '/public/';
}
debug('Mounting web module /' + moduleData.module + ':' + webappPath);
app.use('/' + moduleData.module, express_1.default.static(webappPath));
});
return modules.map((moduleData) => moduleData.metadata);
}
function mountApis(app) {
app.get(`${constants_1.SERVERROUTESPREFIX}/webapps`, (_req, res) => {
const allWebapps = [...app.webapps, ...app.embeddablewebapps];
res.json((0, lodash_1.uniqBy)(filterEnabledWebapps(app, allWebapps), 'name'));
});
app.get(`${constants_1.SERVERROUTESPREFIX}/addons`, (_req, res) => {
res.json(app.addons);
});
}
module.exports = (app) => {
return {
start() {
// Preserve any existing webapps (e.g., from WASM plugins loaded earlier)
const existingWebapps = app.webapps || [];
const nodeWebapps = mountWebModules(app, 'signalk-webapp');
app.webapps = (0, lodash_1.uniqBy)([...nodeWebapps, ...existingWebapps], 'name');
app.addons = mountWebModules(app, 'signalk-node-server-addon');
const existingEmbeddableWebapps = app.embeddablewebapps || [];
const nodeEmbeddableWebapps = mountWebModules(app, 'signalk-embeddable-webapp');
app.embeddablewebapps = (0, lodash_1.uniqBy)([...nodeEmbeddableWebapps, ...existingEmbeddableWebapps], 'name');
app.pluginconfigurators = mountWebModules(app, 'signalk-plugin-configurator');
mountApis(app);
},
stop() { }
};
};
//# sourceMappingURL=webapps.js.map