/** * Copyright JS Foundation and other contributors, http://js.foundation * * 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. **/ const fs = require("fs"); const path = require("path"); const log = require("@node-red/util").log; const i18n = require("@node-red/util").i18n; const registryUtil = require("./util"); // Default allow/deny lists let loadAllowList = ['*']; let loadDenyList = []; var settings; var disableNodePathScan = false; var iconFileExtensions = [".png", ".gif", ".svg"]; var packageList = {}; function init(_settings) { settings = _settings; // TODO: This is duplicated in installer.js // Should it *all* be managed by util? if (settings.externalModules && settings.externalModules.palette) { if (settings.externalModules.palette.allowList || settings.externalModules.palette.denyList) { loadAllowList = settings.externalModules.palette.allowList; loadDenyList = settings.externalModules.palette.denyList; } } loadAllowList = registryUtil.parseModuleList(loadAllowList); loadDenyList = registryUtil.parseModuleList(loadDenyList); } function isIncluded(name) { if (settings.nodesIncludes) { for (var i=0;i/node_modules then it is considered // a local module. // Also check to see if it is listed in the package.json file as a user-installed // module. This distinguishes modules installed as a dependency r.local = true; r.user = !!packageList[r.package.name]; }); } if (dir) { var up = path.resolve(path.join(dir,"..")); while (up !== dir) { var pm = path.join(dir,"node_modules"); if (pm != userDir) { results = results.concat(scanDirForNodesModules(pm,moduleName)); } dir = up; up = path.resolve(path.join(dir,"..")); } } return results; } function getModuleNodeFiles(module) { var moduleDir = module.dir; var pkg = module.package; var iconDirs = []; var iconList = []; function scanTypes(types) { const files = []; for (var n in types) { /* istanbul ignore else */ if (types.hasOwnProperty(n)) { var file = path.join(moduleDir,types[n]); files.push({ file: file, module: pkg.name, name: n, version: pkg.version }); var iconDir = path.join(moduleDir,path.dirname(types[n]),"icons"); if (iconDirs.indexOf(iconDir) == -1) { try { fs.statSync(iconDir); var icons = scanIconDir(iconDir); iconList.push({path:iconDir,icons:icons}); iconDirs.push(iconDir); } catch(err) { } } } } return files; } var result = { nodeFiles:scanTypes(pkg['node-red'].nodes||{}), pluginFiles:scanTypes(pkg['node-red'].plugins||{}), icons:iconList }; var examplesDir = path.join(moduleDir,"examples"); try { fs.statSync(examplesDir) result.examples = {path:examplesDir}; } catch(err) { } return result; } function getNodeFiles(disableNodePathScan) { var dir; // Find all of the nodes to load var nodeFiles = []; var results; var dir; var iconList = []; if (settings.coreNodesDir) { results = getLocalNodeFiles(path.resolve(settings.coreNodesDir)); nodeFiles = nodeFiles.concat(results.files); iconList = iconList.concat(results.icons); var defaultLocalesPath = path.join(settings.coreNodesDir,"locales"); i18n.registerMessageCatalog("node-red",defaultLocalesPath,"messages.json"); } if (settings.userDir) { dir = path.join(settings.userDir,"lib","icons"); var icons = scanIconDir(dir); if (icons.length > 0) { iconList.push({path:dir,icons:icons}); } dir = path.join(settings.userDir,"nodes"); results = getLocalNodeFiles(path.resolve(dir)); nodeFiles = nodeFiles.concat(results.files); iconList = iconList.concat(results.icons); } if (settings.nodesDir) { dir = settings.nodesDir; if (typeof settings.nodesDir == "string") { dir = [dir]; } for (var i=0;i { if (knownModules[dep]) { knownModules[dep].usedBy = knownModules[dep].usedBy || []; knownModules[dep].usedBy.push(mod.package.name) } else { return true; } }) if (missingDeps.length > 0) { log.error(`Module: ${mod.package.name} missing dependencies:`); missingDeps.forEach(m => { log.error(` - ${m}`)}); return false; } } return true; }); nodeList = convertModuleFileListToObject(moduleFiles, nodeList); } else { // console.log("node path scan disabled"); } return nodeList; } function getModuleFiles(module) { // Update the package list var moduleFiles = scanTreeForNodesModules(module); if (moduleFiles.length === 0) { var err = new Error(log._("nodes.registry.localfilesystem.module-not-found", {module:module})); err.code = 'MODULE_NOT_FOUND'; throw err; } // Unlike when doing the initial palette load, this call cannot verify the // dependencies of the new module as it doesn't have visiblity of what // is in the registry. That will have to be done be the caller in loader.js return convertModuleFileListToObject(moduleFiles); } function convertModuleFileListToObject(moduleFiles,seedObject) { const nodeList = seedObject || {}; moduleFiles.forEach(function(moduleFile) { var nodeModuleFiles = getModuleNodeFiles(moduleFile); nodeList[moduleFile.package.name] = { name: moduleFile.package.name, version: moduleFile.package.version, path: moduleFile.dir, local: moduleFile.local||false, user: moduleFile.user||false, nodes: {}, plugins: {}, icons: nodeModuleFiles.icons, examples: nodeModuleFiles.examples }; if (moduleFile.package['node-red'].version) { nodeList[moduleFile.package.name].redVersion = moduleFile.package['node-red'].version; } if (moduleFile.package['node-red'].dependencies) { nodeList[moduleFile.package.name].dependencies = moduleFile.package['node-red'].dependencies; } if (moduleFile.usedBy) { nodeList[moduleFile.package.name].usedBy = moduleFile.usedBy; } nodeModuleFiles.nodeFiles.forEach(function(node) { nodeList[moduleFile.package.name].nodes[node.name] = node; nodeList[moduleFile.package.name].nodes[node.name].local = moduleFile.local || false; }); nodeModuleFiles.pluginFiles.forEach(function(plugin) { nodeList[moduleFile.package.name].plugins[plugin.name] = plugin; nodeList[moduleFile.package.name].plugins[plugin.name].local = moduleFile.local || false; }); }); return nodeList; } // If this finds an svg and a png with the same name, it will only list the svg function scanIconDir(dir) { var iconList = []; var svgs = {}; try { var files = fs.readdirSync(dir); files.forEach(function(file) { var stats = fs.statSync(path.join(dir, file)); var ext = path.extname(file).toLowerCase(); if (stats.isFile() && iconFileExtensions.indexOf(ext) !== -1) { iconList.push(file); if (ext === ".svg") { svgs[file.substring(0,file.length-4)] = true; } } }); } catch(err) { } iconList = iconList.filter(f => { return /.svg$/i.test(f) || !svgs[f.substring(0,f.length-4)] }) return iconList; } /** * Gets the list of modules installed in this runtime as reported by package.json * Note: these may include non-Node-RED modules */ function getPackageList() { var list = {}; if (settings.userDir) { try { var userPackage = path.join(settings.userDir,"package.json"); var pkg = JSON.parse(fs.readFileSync(userPackage,"utf-8")); return pkg.dependencies; } catch(err) { log.error(err); } } return list; } module.exports = { init: init, getNodeFiles: getNodeFiles, getLocalFile: getLocalFile, getModuleFiles: getModuleFiles }