2018-04-15 12:18:10 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
**/
|
2018-04-19 22:39:44 +02:00
|
|
|
|
2018-04-18 18:09:31 +02:00
|
|
|
/**
|
2018-12-01 00:01:09 +01:00
|
|
|
* @mixin @node-red/runtime_nodes
|
2018-04-18 18:09:31 +02:00
|
|
|
*/
|
2018-04-15 12:18:10 +02:00
|
|
|
|
2020-11-30 17:58:05 +01:00
|
|
|
var fs = require("fs-extra");
|
2018-04-20 21:50:20 +02:00
|
|
|
|
2018-04-15 12:18:10 +02:00
|
|
|
var runtime;
|
|
|
|
|
2018-04-19 22:39:44 +02:00
|
|
|
function putNode(node, enabled) {
|
|
|
|
var info;
|
|
|
|
var promise;
|
|
|
|
if (!node.err && node.enabled === enabled) {
|
|
|
|
promise = Promise.resolve(node);
|
|
|
|
} else {
|
|
|
|
if (enabled) {
|
|
|
|
promise = runtime.nodes.enableNode(node.id);
|
|
|
|
} else {
|
|
|
|
promise = runtime.nodes.disableNode(node.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
|
2018-04-15 12:18:10 +02:00
|
|
|
var api = module.exports = {
|
|
|
|
init: function(_runtime) {
|
|
|
|
runtime = _runtime;
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the info of an individual node set
|
|
|
|
* @param {Object} opts
|
|
|
|
* @param {User} opts.user - the user calling the api
|
|
|
|
* @param {String} opts.id - the id of the node set to return
|
2019-08-09 17:56:11 +02:00
|
|
|
* @param {Object} opts.req - the request to log (optional)
|
2018-04-15 12:18:10 +02:00
|
|
|
* @return {Promise<NodeInfo>} - the node information
|
2018-12-01 00:01:09 +01:00
|
|
|
* @memberof @node-red/runtime_nodes
|
2018-04-15 12:18:10 +02:00
|
|
|
*/
|
2020-11-30 17:58:05 +01:00
|
|
|
getNodeInfo: async function(opts) {
|
|
|
|
var id = opts.id;
|
|
|
|
var result = runtime.nodes.getNodeInfo(id);
|
|
|
|
if (result) {
|
|
|
|
runtime.log.audit({event: "nodes.info.get",id:id}, opts.req);
|
|
|
|
delete result.loaded;
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
runtime.log.audit({event: "nodes.info.get",id:id,error:"not_found"}, opts.req);
|
|
|
|
var err = new Error("Node not found");
|
|
|
|
err.code = "not_found";
|
|
|
|
err.status = 404;
|
|
|
|
throw err;
|
|
|
|
}
|
2018-04-15 12:18:10 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the list of node modules installed in the runtime
|
|
|
|
* @param {Object} opts
|
|
|
|
* @param {User} opts.user - the user calling the api
|
2019-08-09 17:56:11 +02:00
|
|
|
* @param {Object} opts.req - the request to log (optional)
|
2018-04-15 12:18:10 +02:00
|
|
|
* @return {Promise<NodeList>} - the list of node modules
|
2018-12-01 00:01:09 +01:00
|
|
|
* @memberof @node-red/runtime_nodes
|
2018-04-15 12:18:10 +02:00
|
|
|
*/
|
2020-11-30 17:58:05 +01:00
|
|
|
getNodeList: async function(opts) {
|
|
|
|
runtime.log.audit({event: "nodes.list.get"}, opts.req);
|
|
|
|
return runtime.nodes.getNodeList();
|
2018-04-15 12:18:10 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an individual node's html content
|
|
|
|
* @param {Object} opts
|
|
|
|
* @param {User} opts.user - the user calling the api
|
|
|
|
* @param {String} opts.id - the id of the node set to return
|
|
|
|
* @param {String} opts.lang - the locale language to return
|
2019-08-09 17:56:11 +02:00
|
|
|
* @param {Object} opts.req - the request to log (optional)
|
2018-04-15 12:18:10 +02:00
|
|
|
* @return {Promise<String>} - the node html content
|
2018-12-01 00:01:09 +01:00
|
|
|
* @memberof @node-red/runtime_nodes
|
2018-04-15 12:18:10 +02:00
|
|
|
*/
|
2020-11-30 17:58:05 +01:00
|
|
|
getNodeConfig: async function(opts) {
|
|
|
|
var id = opts.id;
|
|
|
|
var lang = opts.lang;
|
|
|
|
var result = runtime.nodes.getNodeConfig(id,lang);
|
|
|
|
if (result) {
|
|
|
|
runtime.log.audit({event: "nodes.config.get",id:id}, opts.req);
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
runtime.log.audit({event: "nodes.config.get",id:id,error:"not_found"}, opts.req);
|
|
|
|
var err = new Error("Node not found");
|
|
|
|
err.code = "not_found";
|
|
|
|
err.status = 404;
|
|
|
|
throw err;
|
|
|
|
}
|
2018-04-15 12:18:10 +02:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Gets all node html content
|
|
|
|
* @param {Object} opts
|
|
|
|
* @param {User} opts.user - the user calling the api
|
|
|
|
* @param {String} opts.lang - the locale language to return
|
2019-08-09 17:56:11 +02:00
|
|
|
* @param {Object} opts.req - the request to log (optional)
|
2018-04-15 12:18:10 +02:00
|
|
|
* @return {Promise<String>} - the node html content
|
2018-12-01 00:01:09 +01:00
|
|
|
* @memberof @node-red/runtime_nodes
|
2018-04-15 12:18:10 +02:00
|
|
|
*/
|
2020-11-30 17:58:05 +01:00
|
|
|
getNodeConfigs: async function(opts) {
|
|
|
|
runtime.log.audit({event: "nodes.configs.get"}, opts.req);
|
|
|
|
return runtime.nodes.getNodeConfigs(opts.lang);
|
2018-04-15 12:18:10 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the info of a node module
|
|
|
|
* @param {Object} opts
|
|
|
|
* @param {User} opts.user - the user calling the api
|
|
|
|
* @param {String} opts.module - the id of the module to return
|
2019-08-09 17:56:11 +02:00
|
|
|
* @param {Object} opts.req - the request to log (optional)
|
2018-04-15 12:18:10 +02:00
|
|
|
* @return {Promise<ModuleInfo>} - the node module info
|
2018-12-01 00:01:09 +01:00
|
|
|
* @memberof @node-red/runtime_nodes
|
2018-04-15 12:18:10 +02:00
|
|
|
*/
|
2020-11-30 17:58:05 +01:00
|
|
|
getModuleInfo: async function(opts) {
|
|
|
|
var result = runtime.nodes.getModuleInfo(opts.module);
|
|
|
|
if (result) {
|
|
|
|
runtime.log.audit({event: "nodes.module.get",id:opts.module}, opts.req);
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
runtime.log.audit({event: "nodes.module.get",id:opts.module,error:"not_found"}, opts.req);
|
|
|
|
var err = new Error("Module not found");
|
|
|
|
err.code = "not_found";
|
|
|
|
err.status = 404;
|
|
|
|
throw err;
|
|
|
|
}
|
2018-04-15 12:18:10 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Install a new module into the runtime
|
|
|
|
* @param {Object} opts
|
|
|
|
* @param {User} opts.user - the user calling the api
|
|
|
|
* @param {String} opts.module - the id of the module to install
|
|
|
|
* @param {String} opts.version - (optional) the version of the module to install
|
2020-08-13 16:54:54 +02:00
|
|
|
* @param {Object} opts.tarball - (optional) a tarball file to install. Object has properties `name`, `size` and `buffer`.
|
2019-11-11 10:25:36 +01:00
|
|
|
* @param {String} opts.url - (optional) url to install
|
2019-08-09 17:56:11 +02:00
|
|
|
* @param {Object} opts.req - the request to log (optional)
|
2018-04-15 12:18:10 +02:00
|
|
|
* @return {Promise<ModuleInfo>} - the node module info
|
2018-12-01 00:01:09 +01:00
|
|
|
* @memberof @node-red/runtime_nodes
|
2018-04-15 12:18:10 +02:00
|
|
|
*/
|
2020-11-30 17:58:05 +01:00
|
|
|
addModule: async function(opts) {
|
|
|
|
if (!runtime.settings.available()) {
|
|
|
|
runtime.log.audit({event: "nodes.install",error:"settings_unavailable"}, opts.req);
|
|
|
|
var err = new Error("Settings unavailable");
|
|
|
|
err.code = "settings_unavailable";
|
|
|
|
err.status = 400;
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
if (opts.tarball) {
|
2020-12-27 13:49:17 +01:00
|
|
|
if (runtime.settings.externalModules && runtime.settings.externalModules.palette && runtime.settings.externalModules.palette.upload === false) {
|
2020-11-30 17:58:05 +01:00
|
|
|
runtime.log.audit({event: "nodes.install",tarball:opts.tarball.file,error:"invalid_request"}, opts.req);
|
|
|
|
var err = new Error("Invalid request");
|
|
|
|
err.code = "invalid_request";
|
2018-04-15 12:18:10 +02:00
|
|
|
err.status = 400;
|
2020-11-30 17:58:05 +01:00
|
|
|
throw err;
|
2018-04-15 12:18:10 +02:00
|
|
|
}
|
2020-11-30 17:58:05 +01:00
|
|
|
if (opts.module || opts.version || opts.url) {
|
|
|
|
runtime.log.audit({event: "nodes.install",tarball:opts.tarball.file,module:opts.module,error:"invalid_request"}, opts.req);
|
|
|
|
var err = new Error("Invalid request");
|
|
|
|
err.code = "invalid_request";
|
|
|
|
err.status = 400;
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
return runtime.nodes.installModule(opts.tarball.buffer).then(function(info) {
|
|
|
|
runtime.log.audit({event: "nodes.install",tarball:opts.tarball.file,module:info.id}, opts.req);
|
|
|
|
return info;
|
|
|
|
}).catch(function(err) {
|
|
|
|
if (err.code) {
|
2020-08-13 16:54:54 +02:00
|
|
|
err.status = 400;
|
2020-11-30 17:58:05 +01:00
|
|
|
runtime.log.audit({event: "nodes.install",module:opts.module,version:opts.version,url:opts.url,error:err.code}, opts.req);
|
|
|
|
} else {
|
|
|
|
err.status = 400;
|
|
|
|
runtime.log.audit({event: "nodes.install",module:opts.module,version:opts.version,url:opts.url,error:err.code||"unexpected_error",message:err.toString()}, opts.req);
|
2020-08-13 16:54:54 +02:00
|
|
|
}
|
2020-11-30 17:58:05 +01:00
|
|
|
throw err;
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (opts.module) {
|
|
|
|
var existingModule = runtime.nodes.getModuleInfo(opts.module);
|
|
|
|
if (existingModule) {
|
|
|
|
if (!opts.version || existingModule.version === opts.version) {
|
|
|
|
runtime.log.audit({event: "nodes.install",module:opts.module, version:opts.version, error:"module_already_loaded"}, opts.req);
|
|
|
|
var err = new Error("Module already loaded");
|
|
|
|
err.code = "module_already_loaded";
|
2020-08-13 16:54:54 +02:00
|
|
|
err.status = 400;
|
2020-11-30 17:58:05 +01:00
|
|
|
throw err;
|
2020-08-13 16:54:54 +02:00
|
|
|
}
|
|
|
|
}
|
2020-11-30 17:58:05 +01:00
|
|
|
return runtime.nodes.installModule(opts.module,opts.version,opts.url).then(function(info) {
|
|
|
|
runtime.log.audit({event: "nodes.install",module:opts.module,version:opts.version,url:opts.url}, opts.req);
|
|
|
|
return info;
|
|
|
|
}).catch(function(err) {
|
|
|
|
if (err.code === 404) {
|
|
|
|
runtime.log.audit({event: "nodes.install",module:opts.module,version:opts.version,url:opts.url,error:"not_found"}, opts.req);
|
|
|
|
// TODO: code/status
|
|
|
|
err.status = 404;
|
|
|
|
} else if (err.code) {
|
|
|
|
err.status = 400;
|
|
|
|
runtime.log.audit({event: "nodes.install",module:opts.module,version:opts.version,url:opts.url,error:err.code}, opts.req);
|
|
|
|
} else {
|
|
|
|
err.status = 400;
|
|
|
|
runtime.log.audit({event: "nodes.install",module:opts.module,version:opts.version,url:opts.url,error:err.code||"unexpected_error",message:err.toString()}, opts.req);
|
2018-04-15 12:18:10 +02:00
|
|
|
}
|
2020-11-30 17:58:05 +01:00
|
|
|
throw err;
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
runtime.log.audit({event: "nodes.install",module:opts.module,error:"invalid_request"}, opts.req);
|
|
|
|
var err = new Error("Invalid request");
|
|
|
|
err.code = "invalid_request";
|
|
|
|
err.status = 400;
|
|
|
|
throw err;
|
|
|
|
}
|
2018-04-15 12:18:10 +02:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Removes a module from the runtime
|
|
|
|
* @param {Object} opts
|
|
|
|
* @param {User} opts.user - the user calling the api
|
|
|
|
* @param {String} opts.module - the id of the module to remove
|
2019-08-09 17:56:11 +02:00
|
|
|
* @param {Object} opts.req - the request to log (optional)
|
2018-04-15 12:18:10 +02:00
|
|
|
* @return {Promise} - resolves when complete
|
2018-12-01 00:01:09 +01:00
|
|
|
* @memberof @node-red/runtime_nodes
|
2018-04-15 12:18:10 +02:00
|
|
|
*/
|
2020-11-30 17:58:05 +01:00
|
|
|
removeModule: async function(opts) {
|
|
|
|
if (!runtime.settings.available()) {
|
|
|
|
runtime.log.audit({event: "nodes.install",error:"settings_unavailable"}, opts.req);
|
|
|
|
var err = new Error("Settings unavailable");
|
|
|
|
err.code = "settings_unavailable";
|
|
|
|
err.status = 400;
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
var module = runtime.nodes.getModuleInfo(opts.module);
|
|
|
|
if (!module) {
|
|
|
|
runtime.log.audit({event: "nodes.remove",module:opts.module,error:"not_found"}, opts.req);
|
|
|
|
var err = new Error("Module not found");
|
|
|
|
err.code = "not_found";
|
|
|
|
err.status = 404;
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
return runtime.nodes.uninstallModule(opts.module).then(function() {
|
|
|
|
runtime.log.audit({event: "nodes.remove",module:opts.module}, opts.req);
|
|
|
|
}).catch(function(err) {
|
2018-04-15 12:18:10 +02:00
|
|
|
err.status = 400;
|
2020-11-30 17:58:05 +01:00
|
|
|
runtime.log.audit({event: "nodes.remove",module:opts.module,error:err.code||"unexpected_error",message:err.toString()}, opts.req);
|
|
|
|
throw err;
|
|
|
|
})
|
|
|
|
} catch(error) {
|
|
|
|
runtime.log.audit({event: "nodes.remove",module:opts.module,error:error.code||"unexpected_error",message:error.toString()}, opts.req);
|
|
|
|
error.status = 400;
|
|
|
|
throw err;
|
|
|
|
}
|
2018-04-15 12:18:10 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enables or disables a module in the runtime
|
|
|
|
* @param {Object} opts
|
|
|
|
* @param {User} opts.user - the user calling the api
|
|
|
|
* @param {String} opts.module - the id of the module to enable or disable
|
|
|
|
* @param {String} opts.enabled - whether the module should be enabled or disabled
|
2019-08-09 17:56:11 +02:00
|
|
|
* @param {Object} opts.req - the request to log (optional)
|
2018-04-15 12:18:10 +02:00
|
|
|
* @return {Promise<ModuleInfo>} - the module info object
|
2018-12-01 00:01:09 +01:00
|
|
|
* @memberof @node-red/runtime_nodes
|
2018-04-15 12:18:10 +02:00
|
|
|
*/
|
2020-11-30 17:58:05 +01:00
|
|
|
setModuleState: async function(opts) {
|
2018-04-19 22:39:44 +02:00
|
|
|
var mod = opts.module;
|
2020-11-30 17:58:05 +01:00
|
|
|
if (!runtime.settings.available()) {
|
|
|
|
runtime.log.audit({event: "nodes.module.set",error:"settings_unavailable"}, opts.req);
|
|
|
|
var err = new Error("Settings unavailable");
|
|
|
|
err.code = "settings_unavailable";
|
|
|
|
err.status = 400;
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
var module = runtime.nodes.getModuleInfo(mod);
|
|
|
|
if (!module) {
|
|
|
|
runtime.log.audit({event: "nodes.module.set",module:mod,error:"not_found"}, opts.req);
|
|
|
|
var err = new Error("Module not found");
|
|
|
|
err.code = "not_found";
|
|
|
|
err.status = 404;
|
|
|
|
throw err;
|
2018-04-15 12:18:10 +02:00
|
|
|
}
|
|
|
|
|
2020-11-30 17:58:05 +01:00
|
|
|
var nodes = module.nodes;
|
|
|
|
var promises = [];
|
|
|
|
for (var i = 0; i < nodes.length; ++i) {
|
|
|
|
promises.push(putNode(nodes[i],opts.enabled));
|
2018-04-15 12:18:10 +02:00
|
|
|
}
|
2020-11-30 17:58:05 +01:00
|
|
|
return Promise.all(promises).then(function() {
|
|
|
|
return runtime.nodes.getModuleInfo(mod);
|
|
|
|
}).catch(function(err) {
|
|
|
|
err.status = 400;
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
} catch(error) {
|
|
|
|
runtime.log.audit({event: "nodes.module.set",module:mod,enabled:opts.enabled,error:error.code||"unexpected_error",message:error.toString()}, opts.req);
|
|
|
|
error.status = 400;
|
|
|
|
throw err;
|
|
|
|
}
|
2018-04-15 12:18:10 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enables or disables a n individual node-set in the runtime
|
|
|
|
* @param {Object} opts
|
|
|
|
* @param {User} opts.user - the user calling the api
|
|
|
|
* @param {String} opts.id - the id of the node-set to enable or disable
|
|
|
|
* @param {String} opts.enabled - whether the module should be enabled or disabled
|
2019-08-09 17:56:11 +02:00
|
|
|
* @param {Object} opts.req - the request to log (optional)
|
2018-04-15 12:18:10 +02:00
|
|
|
* @return {Promise<ModuleInfo>} - the module info object
|
2018-12-01 00:01:09 +01:00
|
|
|
* @memberof @node-red/runtime_nodes
|
2018-04-15 12:18:10 +02:00
|
|
|
*/
|
2020-11-30 17:58:05 +01:00
|
|
|
setNodeSetState: async function(opts) {
|
|
|
|
if (!runtime.settings.available()) {
|
|
|
|
runtime.log.audit({event: "nodes.info.set",error:"settings_unavailable"}, opts.req);
|
|
|
|
var err = new Error("Settings unavailable");
|
|
|
|
err.code = "settings_unavailable";
|
|
|
|
err.status = 400;
|
|
|
|
throw err;
|
|
|
|
}
|
2018-04-15 12:18:10 +02:00
|
|
|
|
2020-11-30 17:58:05 +01:00
|
|
|
var id = opts.id;
|
|
|
|
var enabled = opts.enabled;
|
|
|
|
try {
|
|
|
|
var node = runtime.nodes.getNodeInfo(id);
|
|
|
|
if (!node) {
|
|
|
|
runtime.log.audit({event: "nodes.info.set",id:id,error:"not_found"}, opts.req);
|
|
|
|
var err = new Error("Node not found");
|
|
|
|
err.code = "not_found";
|
|
|
|
err.status = 404;
|
|
|
|
throw err;
|
|
|
|
} else {
|
|
|
|
delete node.loaded;
|
|
|
|
return putNode(node,enabled).then(function(result) {
|
|
|
|
runtime.log.audit({event: "nodes.info.set",id:id,enabled:enabled}, opts.req);
|
|
|
|
return result;
|
|
|
|
}).catch(function(err) {
|
|
|
|
runtime.log.audit({event: "nodes.info.set",id:id,enabled:enabled,error:err.code||"unexpected_error",message:err.toString()}, opts.req);
|
|
|
|
err.status = 400;
|
|
|
|
throw err;
|
|
|
|
});
|
2018-04-15 12:18:10 +02:00
|
|
|
}
|
2020-11-30 17:58:05 +01:00
|
|
|
} catch(error) {
|
|
|
|
runtime.log.audit({event: "nodes.info.set",id:id,enabled:enabled,error:error.code||"unexpected_error",message:error.toString()}, opts.req);
|
|
|
|
error.status = 400;
|
|
|
|
throw err;
|
|
|
|
}
|
2018-04-15 12:18:10 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2018-04-30 11:57:14 +02:00
|
|
|
* Gets all registered module message catalogs
|
|
|
|
* @param {Object} opts
|
|
|
|
* @param {User} opts.user - the user calling the api
|
|
|
|
* @param {User} opts.lang - the i18n language to return. If not set, uses runtime default (en-US)
|
2019-08-09 17:56:11 +02:00
|
|
|
* @param {Object} opts.req - the request to log (optional)
|
2018-04-30 11:57:14 +02:00
|
|
|
* @return {Promise<Object>} - the message catalogs
|
2018-12-01 00:01:09 +01:00
|
|
|
* @memberof @node-red/runtime_nodes
|
2018-04-15 12:18:10 +02:00
|
|
|
*/
|
2020-11-30 17:58:05 +01:00
|
|
|
getModuleCatalogs: async function(opts) {
|
|
|
|
var namespace = opts.module;
|
|
|
|
var lang = opts.lang;
|
|
|
|
var prevLang = runtime.i18n.i.language;
|
|
|
|
// Trigger a load from disk of the language if it is not the default
|
2020-12-02 10:25:10 +01:00
|
|
|
return new Promise( (resolve,reject) => {
|
|
|
|
runtime.i18n.i.changeLanguage(lang, function(){
|
|
|
|
var nodeList = runtime.nodes.getNodeList();
|
|
|
|
var result = {};
|
|
|
|
nodeList.forEach(function(n) {
|
|
|
|
if (n.module !== "node-red") {
|
|
|
|
result[n.id] = runtime.i18n.i.getResourceBundle(lang, n.id)||{};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
runtime.i18n.i.changeLanguage(prevLang);
|
|
|
|
resolve(result);
|
2018-04-30 11:57:14 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-04-15 12:18:10 +02:00
|
|
|
/**
|
2018-04-30 11:57:14 +02:00
|
|
|
* Gets a modules message catalog
|
|
|
|
* @param {Object} opts
|
|
|
|
* @param {User} opts.user - the user calling the api
|
|
|
|
* @param {User} opts.module - the module
|
|
|
|
* @param {User} opts.lang - the i18n language to return. If not set, uses runtime default (en-US)
|
2019-08-09 17:56:11 +02:00
|
|
|
* @param {Object} opts.req - the request to log (optional)
|
2018-04-30 11:57:14 +02:00
|
|
|
* @return {Promise<Object>} - the message catalog
|
2018-12-01 00:01:09 +01:00
|
|
|
* @memberof @node-red/runtime_nodes
|
2018-04-15 12:18:10 +02:00
|
|
|
*/
|
2020-11-30 17:58:05 +01:00
|
|
|
getModuleCatalog: async function(opts) {
|
|
|
|
var namespace = opts.module;
|
|
|
|
var lang = opts.lang;
|
|
|
|
var prevLang = runtime.i18n.i.language;
|
|
|
|
// Trigger a load from disk of the language if it is not the default
|
2021-01-06 21:20:32 +01:00
|
|
|
return new Promise(resolve => {
|
|
|
|
runtime.i18n.i.changeLanguage(lang, function() {
|
|
|
|
var catalog = runtime.i18n.i.getResourceBundle(lang, namespace);
|
|
|
|
runtime.i18n.i.changeLanguage(prevLang);
|
|
|
|
resolve(catalog||{});
|
|
|
|
});
|
2018-04-30 11:57:14 +02:00
|
|
|
});
|
|
|
|
},
|
2018-04-15 12:18:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the list of all icons available in the modules installed within the runtime
|
|
|
|
* @param {Object} opts
|
|
|
|
* @param {User} opts.user - the user calling the api
|
2019-08-09 17:56:11 +02:00
|
|
|
* @param {Object} opts.req - the request to log (optional)
|
2018-04-15 12:18:10 +02:00
|
|
|
* @return {Promise<IconList>} - the list of all icons
|
2018-12-01 00:01:09 +01:00
|
|
|
* @memberof @node-red/runtime_nodes
|
2018-04-15 12:18:10 +02:00
|
|
|
*/
|
2020-11-30 17:58:05 +01:00
|
|
|
getIconList: async function(opts) {
|
|
|
|
runtime.log.audit({event: "nodes.icons.get"}, opts.req);
|
|
|
|
return runtime.nodes.getNodeIcons();
|
2018-04-15 12:18:10 +02:00
|
|
|
},
|
|
|
|
/**
|
2018-04-20 21:50:20 +02:00
|
|
|
* Gets a node icon
|
|
|
|
* @param {Object} opts
|
|
|
|
* @param {User} opts.user - the user calling the api
|
|
|
|
* @param {String} opts.module - the id of the module requesting the icon
|
|
|
|
* @param {String} opts.icon - the name of the icon
|
2019-08-09 17:56:11 +02:00
|
|
|
* @param {Object} opts.req - the request to log (optional)
|
2018-08-16 00:12:51 +02:00
|
|
|
* @return {Promise<Buffer>} - the icon file as a Buffer or null if no icon available
|
2018-12-01 00:01:09 +01:00
|
|
|
* @memberof @node-red/runtime_nodes
|
2018-04-15 12:18:10 +02:00
|
|
|
*/
|
2020-11-30 17:58:05 +01:00
|
|
|
getIcon: async function(opts) {
|
|
|
|
var iconPath = runtime.nodes.getNodeIconPath(opts.module,opts.icon);
|
|
|
|
if (iconPath) {
|
|
|
|
return fs.readFile(iconPath).catch(err => {
|
|
|
|
err.status = 400;
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
2018-04-20 21:50:20 +02:00
|
|
|
}
|
2018-04-15 12:18:10 +02:00
|
|
|
}
|