2021-02-12 19:14:13 +01:00
|
|
|
// This module handles the management of modules required by the runtime and flows.
|
|
|
|
// Essentially this means keeping track of what extra modules a flow requires,
|
|
|
|
// ensuring those modules are installed and providing a standard way for nodes
|
|
|
|
// to require those modules safely.
|
|
|
|
|
|
|
|
const fs = require("fs-extra");
|
|
|
|
const registryUtil = require("./util");
|
|
|
|
const path = require("path");
|
2021-02-14 01:02:08 +01:00
|
|
|
const clone = require("clone");
|
2021-02-12 19:14:13 +01:00
|
|
|
const exec = require("@node-red/util").exec;
|
|
|
|
const log = require("@node-red/util").log;
|
2021-04-15 16:11:45 +02:00
|
|
|
const hooks = require("@node-red/util").hooks;
|
2021-02-12 19:14:13 +01:00
|
|
|
|
|
|
|
const BUILTIN_MODULES = require('module').builtinModules;
|
|
|
|
|
|
|
|
// TODO: outsource running npm to a plugin
|
|
|
|
const NPM_COMMAND = (process.platform === "win32") ? "npm.cmd" : "npm";
|
|
|
|
|
|
|
|
let registeredTypes = {};
|
2021-02-14 01:02:08 +01:00
|
|
|
let subflowTypes = {};
|
2021-02-12 19:14:13 +01:00
|
|
|
let settings;
|
|
|
|
|
|
|
|
let knownExternalModules = {};
|
|
|
|
|
|
|
|
let installEnabled = true;
|
|
|
|
let installAllowList = ['*'];
|
|
|
|
let installDenyList = [];
|
|
|
|
|
|
|
|
function getInstallDir() {
|
2021-07-13 12:24:10 +02:00
|
|
|
return path.resolve(settings.userDir || process.env.NODE_RED_HOME || ".");
|
2021-02-12 19:14:13 +01:00
|
|
|
}
|
|
|
|
|
2021-07-13 12:24:10 +02:00
|
|
|
let loggedLegacyWarning = false;
|
2021-02-12 19:14:13 +01:00
|
|
|
async function refreshExternalModules() {
|
2021-07-13 12:24:10 +02:00
|
|
|
|
|
|
|
if (!loggedLegacyWarning) {
|
|
|
|
loggedLegacyWarning = true;
|
|
|
|
const oldExternalModulesDir = path.join(path.resolve(settings.userDir || process.env.NODE_RED_HOME || "."),"externalModules");
|
|
|
|
if (fs.existsSync(oldExternalModulesDir)) {
|
|
|
|
try {
|
|
|
|
log.warn(log._("server.install.old-ext-mod-dir-warning",{oldDir:oldExternalModulesDir, newDir:getInstallDir()}))
|
|
|
|
} catch(err) {console.log(err)}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const externalModuleDir = getInstallDir();
|
2021-02-12 19:14:13 +01:00
|
|
|
try {
|
|
|
|
const pkgFile = JSON.parse(await fs.readFile(path.join(externalModuleDir,"package.json"),"utf-8"));
|
2021-07-14 20:18:39 +02:00
|
|
|
knownExternalModules = pkgFile.dependencies || {};
|
2021-02-12 19:14:13 +01:00
|
|
|
} catch(err) {
|
2021-07-13 12:24:10 +02:00
|
|
|
knownExternalModules = {};
|
2021-02-12 19:14:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function init(_settings) {
|
|
|
|
settings = _settings;
|
2021-02-15 18:28:14 +01:00
|
|
|
knownExternalModules = {};
|
|
|
|
installEnabled = true;
|
2021-07-13 12:24:10 +02:00
|
|
|
|
2021-02-12 19:14:13 +01:00
|
|
|
if (settings.externalModules && settings.externalModules.modules) {
|
|
|
|
if (settings.externalModules.modules.allowList || settings.externalModules.modules.denyList) {
|
|
|
|
installAllowList = settings.externalModules.modules.allowList;
|
|
|
|
installDenyList = settings.externalModules.modules.denyList;
|
|
|
|
}
|
|
|
|
if (settings.externalModules.modules.hasOwnProperty("allowInstall")) {
|
|
|
|
installEnabled = settings.externalModules.modules.allowInstall
|
|
|
|
}
|
|
|
|
}
|
|
|
|
installAllowList = registryUtil.parseModuleList(installAllowList);
|
|
|
|
installDenyList = registryUtil.parseModuleList(installDenyList);
|
|
|
|
}
|
|
|
|
|
|
|
|
function register(type, dynamicModuleListProperty) {
|
|
|
|
registeredTypes[type] = dynamicModuleListProperty;
|
|
|
|
}
|
|
|
|
|
2021-02-14 01:02:08 +01:00
|
|
|
function registerSubflow(type, subflowConfig) {
|
|
|
|
subflowTypes[type] = subflowConfig;
|
|
|
|
}
|
|
|
|
|
2021-02-12 19:14:13 +01:00
|
|
|
function requireModule(module) {
|
|
|
|
if (!registryUtil.checkModuleAllowed( module, null,installAllowList,installDenyList)) {
|
|
|
|
const e = new Error("Module not allowed");
|
|
|
|
e.code = "module_not_allowed";
|
|
|
|
throw e;
|
|
|
|
}
|
2021-06-16 18:02:24 +02:00
|
|
|
|
|
|
|
const parsedModule = parseModuleName(module);
|
|
|
|
|
|
|
|
if (BUILTIN_MODULES.indexOf(parsedModule.module) !== -1) {
|
|
|
|
return require(parsedModule.module);
|
2021-02-12 19:14:13 +01:00
|
|
|
}
|
2021-06-16 18:02:24 +02:00
|
|
|
if (!knownExternalModules[parsedModule.module]) {
|
2021-02-12 19:14:13 +01:00
|
|
|
const e = new Error("Module not allowed");
|
|
|
|
e.code = "module_not_allowed";
|
|
|
|
throw e;
|
|
|
|
}
|
2021-07-13 12:24:10 +02:00
|
|
|
const externalModuleDir = getInstallDir();
|
2021-02-12 19:14:13 +01:00
|
|
|
const moduleDir = path.join(externalModuleDir,"node_modules",module);
|
|
|
|
return require(moduleDir);
|
|
|
|
}
|
2021-07-14 20:18:39 +02:00
|
|
|
function importModule(module) {
|
|
|
|
if (!registryUtil.checkModuleAllowed( module, null,installAllowList,installDenyList)) {
|
|
|
|
const e = new Error("Module not allowed");
|
|
|
|
e.code = "module_not_allowed";
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
|
|
|
const parsedModule = parseModuleName(module);
|
|
|
|
|
|
|
|
if (BUILTIN_MODULES.indexOf(parsedModule.module) !== -1) {
|
|
|
|
return import(parsedModule.module);
|
|
|
|
}
|
|
|
|
if (!knownExternalModules[parsedModule.module]) {
|
|
|
|
const e = new Error("Module not allowed");
|
|
|
|
e.code = "module_not_allowed";
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
const externalModuleDir = getInstallDir();
|
|
|
|
const moduleDir = path.join(externalModuleDir,"node_modules",module);
|
|
|
|
// Import needs the full path to the module's main .js file
|
|
|
|
const moduleFile = require.resolve(moduleDir);
|
|
|
|
return import(moduleFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-12 19:14:13 +01:00
|
|
|
|
|
|
|
function parseModuleName(module) {
|
|
|
|
var match = /((?:@[^/]+\/)?[^/@]+)(?:@([\s\S]+))?/.exec(module);
|
|
|
|
if (match) {
|
|
|
|
return {
|
|
|
|
spec: module,
|
|
|
|
module: match[1],
|
|
|
|
version: match[2],
|
|
|
|
builtin: BUILTIN_MODULES.indexOf(match[1]) !== -1,
|
|
|
|
known: !!knownExternalModules[match[1]]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isInstalled(moduleDetails) {
|
|
|
|
return moduleDetails.builtin || moduleDetails.known;
|
|
|
|
}
|
|
|
|
|
2021-02-14 01:02:08 +01:00
|
|
|
|
2021-02-12 19:14:13 +01:00
|
|
|
async function checkFlowDependencies(flowConfig) {
|
2021-02-14 01:02:08 +01:00
|
|
|
let nodes = clone(flowConfig);
|
2021-02-12 19:14:13 +01:00
|
|
|
await refreshExternalModules();
|
|
|
|
|
|
|
|
const checkedModules = {};
|
|
|
|
const promises = [];
|
|
|
|
const errors = [];
|
2021-02-14 01:02:08 +01:00
|
|
|
const checkedSubflows = {};
|
|
|
|
while (nodes.length > 0) {
|
|
|
|
let n = nodes.shift();
|
|
|
|
if (subflowTypes[n.type] && !checkedSubflows[n.type]) {
|
|
|
|
checkedSubflows[n.type] = true;
|
|
|
|
nodes = nodes.concat(subflowTypes[n.type].flow)
|
|
|
|
} else if (registeredTypes[n.type]) {
|
2021-02-12 19:14:13 +01:00
|
|
|
let nodeModules = n[registeredTypes[n.type]] || [];
|
|
|
|
if (!Array.isArray(nodeModules)) {
|
|
|
|
nodeModules = [nodeModules]
|
|
|
|
}
|
|
|
|
nodeModules.forEach(module => {
|
|
|
|
if (typeof module !== 'string') {
|
|
|
|
module = module.module || "";
|
|
|
|
}
|
|
|
|
if (module) {
|
|
|
|
let moduleDetails = parseModuleName(module)
|
|
|
|
if (moduleDetails && checkedModules[moduleDetails.module] === undefined) {
|
|
|
|
checkedModules[moduleDetails.module] = isInstalled(moduleDetails)
|
|
|
|
if (!checkedModules[moduleDetails.module]) {
|
|
|
|
if (installEnabled) {
|
|
|
|
promises.push(installModule(moduleDetails).catch(err => {
|
|
|
|
errors.push({module: moduleDetails,error:err});
|
|
|
|
}))
|
|
|
|
} else if (!installEnabled) {
|
|
|
|
const e = new Error("Module install disabled - externalModules.modules.allowInstall=false");
|
|
|
|
e.code = "install_not_allowed";
|
|
|
|
errors.push({module: moduleDetails,error:e});
|
|
|
|
}
|
|
|
|
} else if (!registryUtil.checkModuleAllowed( moduleDetails.module, moduleDetails.version,installAllowList,installDenyList)) {
|
|
|
|
const e = new Error("Module not allowed");
|
|
|
|
e.code = "module_not_allowed";
|
|
|
|
errors.push({module: moduleDetails,error:e});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-02-14 01:02:08 +01:00
|
|
|
}
|
2021-02-12 23:40:30 +01:00
|
|
|
return Promise.all(promises).then(refreshExternalModules).then(() => {
|
2021-02-12 19:14:13 +01:00
|
|
|
if (errors.length > 0) {
|
|
|
|
throw errors;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function ensureModuleDir() {
|
|
|
|
const installDir = getInstallDir();
|
|
|
|
|
|
|
|
if (!fs.existsSync(installDir)) {
|
|
|
|
await fs.ensureDir(installDir);
|
|
|
|
}
|
|
|
|
const pkgFile = path.join(installDir,"package.json");
|
|
|
|
if (!fs.existsSync(pkgFile)) {
|
|
|
|
await fs.writeFile(path.join(installDir,"package.json"),`{
|
2021-02-12 23:40:30 +01:00
|
|
|
"name": "Node-RED-External-Modules",
|
|
|
|
"description": "These modules are automatically installed by Node-RED to use in Function nodes.",
|
2021-02-12 19:14:13 +01:00
|
|
|
"version": "1.0.0",
|
|
|
|
"private": true,
|
|
|
|
"dependencies": {}
|
|
|
|
}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function installModule(moduleDetails) {
|
|
|
|
let installSpec = moduleDetails.module;
|
|
|
|
if (!registryUtil.checkModuleAllowed( moduleDetails.module, moduleDetails.version,installAllowList,installDenyList)) {
|
|
|
|
const e = new Error("Install not allowed");
|
|
|
|
e.code = "install_not_allowed";
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
if (moduleDetails.version) {
|
|
|
|
installSpec = installSpec+"@"+moduleDetails.version;
|
|
|
|
}
|
|
|
|
log.info(log._("server.install.installing",{name: moduleDetails.module,version: moduleDetails.version||"latest"}));
|
|
|
|
const installDir = getInstallDir();
|
|
|
|
|
|
|
|
await ensureModuleDir();
|
|
|
|
|
2021-04-15 16:11:45 +02:00
|
|
|
let triggerPayload = {
|
|
|
|
"module": moduleDetails.module,
|
|
|
|
"version": moduleDetails.version,
|
|
|
|
"dir": installDir,
|
2021-04-28 14:40:17 +02:00
|
|
|
"args": ["--production","--engine-strict"]
|
2021-04-15 16:11:45 +02:00
|
|
|
}
|
2021-04-19 21:29:30 +02:00
|
|
|
return hooks.trigger("preInstall", triggerPayload).then((result) => {
|
2021-04-15 16:11:45 +02:00
|
|
|
// preInstall passed
|
|
|
|
// - run install
|
2021-04-19 21:29:30 +02:00
|
|
|
if (result !== false) {
|
2021-04-20 23:55:06 +02:00
|
|
|
let extraArgs = triggerPayload.args || [];
|
|
|
|
let args = ['install', ...extraArgs, installSpec]
|
2021-04-19 21:29:30 +02:00
|
|
|
log.trace(NPM_COMMAND + JSON.stringify(args));
|
|
|
|
return exec.run(NPM_COMMAND, args, { cwd: installDir },true)
|
|
|
|
} else {
|
|
|
|
log.trace("skipping npm install");
|
|
|
|
}
|
2021-04-15 16:11:45 +02:00
|
|
|
}).then(() => {
|
|
|
|
return hooks.trigger("postInstall", triggerPayload)
|
|
|
|
}).then(() => {
|
2021-03-15 09:23:30 +01:00
|
|
|
log.info(log._("server.install.installed", { name: installSpec }));
|
2021-02-12 19:14:13 +01:00
|
|
|
}).catch(result => {
|
2021-04-15 16:11:45 +02:00
|
|
|
var output = result.stderr || result.toString();
|
2021-02-12 19:14:13 +01:00
|
|
|
var e;
|
|
|
|
if (/E404/.test(output) || /ETARGET/.test(output)) {
|
|
|
|
log.error(log._("server.install.install-failed-not-found",{name:installSpec}));
|
|
|
|
e = new Error("Module not found");
|
|
|
|
e.code = 404;
|
|
|
|
throw e;
|
|
|
|
} else {
|
|
|
|
log.error(log._("server.install.install-failed-long",{name:installSpec}));
|
|
|
|
log.error("------------------------------------------");
|
|
|
|
log.error(output);
|
|
|
|
log.error("------------------------------------------");
|
2021-02-15 18:28:14 +01:00
|
|
|
e = new Error(log._("server.install.install-failed"));
|
|
|
|
e.code = "unexpected_error";
|
|
|
|
throw e;
|
2021-02-12 19:14:13 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
init: init,
|
|
|
|
register: register,
|
2021-02-14 01:02:08 +01:00
|
|
|
registerSubflow: registerSubflow,
|
2021-02-12 19:14:13 +01:00
|
|
|
checkFlowDependencies: checkFlowDependencies,
|
2021-07-14 20:18:39 +02:00
|
|
|
require: requireModule,
|
|
|
|
import: importModule
|
2021-03-15 09:23:30 +01:00
|
|
|
}
|