mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Merge pull request #2903 from node-red/plugin-resources
Allow module to provide resources and automatically expose them
This commit is contained in:
commit
d555fcf7bd
@ -75,6 +75,8 @@ module.exports = {
|
|||||||
editorApp.get("/icons/:module/:icon",ui.icon);
|
editorApp.get("/icons/:module/:icon",ui.icon);
|
||||||
editorApp.get("/icons/:scope/:module/:icon",ui.icon);
|
editorApp.get("/icons/:scope/:module/:icon",ui.icon);
|
||||||
|
|
||||||
|
editorApp.get(/^\/resources\/((?:@[^\/]+\/)?[^\/]+)\/(.+)$/,ui.moduleResource);
|
||||||
|
|
||||||
var theme = require("./theme");
|
var theme = require("./theme");
|
||||||
theme.init(settings, runtimeAPI);
|
theme.init(settings, runtimeAPI);
|
||||||
editorApp.use("/theme",theme.app());
|
editorApp.use("/theme",theme.app());
|
||||||
|
@ -68,6 +68,28 @@ module.exports = {
|
|||||||
apiUtils.rejectHandler(req,res,err);
|
apiUtils.rejectHandler(req,res,err);
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
|
moduleResource: function(req, res) {
|
||||||
|
let resourcePath = req.params[1];
|
||||||
|
let opts = {
|
||||||
|
user: req.user,
|
||||||
|
module: req.params[0],
|
||||||
|
path: resourcePath
|
||||||
|
}
|
||||||
|
runtimeAPI.nodes.getModuleResource(opts).then(function(data) {
|
||||||
|
if (data) {
|
||||||
|
var contentType = mime.getType(resourcePath);
|
||||||
|
res.set("Content-Type", contentType);
|
||||||
|
res.send(data);
|
||||||
|
} else {
|
||||||
|
res.status(404).end()
|
||||||
|
}
|
||||||
|
}).catch(function(err) {
|
||||||
|
console.log(err.stack);
|
||||||
|
apiUtils.rejectHandler(req,res,err);
|
||||||
|
})
|
||||||
|
},
|
||||||
|
|
||||||
editor: async function(req,res) {
|
editor: async function(req,res) {
|
||||||
res.send(Mustache.render(editorTemplate,await theme.context()));
|
res.send(Mustache.render(editorTemplate,await theme.context()));
|
||||||
},
|
},
|
||||||
|
@ -301,6 +301,17 @@ module.exports = {
|
|||||||
*/
|
*/
|
||||||
getNodeExampleFlowPath: library.getExampleFlowPath,
|
getNodeExampleFlowPath: library.getExampleFlowPath,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the full path to a module's resource file
|
||||||
|
* @param {String} module - the name of the module providing the resource file
|
||||||
|
* @param {String} path - the relative path of the resource file
|
||||||
|
* @return {String} the full path to the resource file
|
||||||
|
*
|
||||||
|
* @function
|
||||||
|
* @memberof @node-red/registry
|
||||||
|
*/
|
||||||
|
getModuleResource: registry.getModuleResource,
|
||||||
|
|
||||||
checkFlowDependencies: externalModules.checkFlowDependencies,
|
checkFlowDependencies: externalModules.checkFlowDependencies,
|
||||||
|
|
||||||
registerPlugin: plugins.registerPlugin,
|
registerPlugin: plugins.registerPlugin,
|
||||||
@ -310,6 +321,7 @@ module.exports = {
|
|||||||
getPluginConfigs: plugins.getPluginConfigs,
|
getPluginConfigs: plugins.getPluginConfigs,
|
||||||
exportPluginSettings: plugins.exportPluginSettings,
|
exportPluginSettings: plugins.exportPluginSettings,
|
||||||
|
|
||||||
|
|
||||||
deprecated: require("./deprecated")
|
deprecated: require("./deprecated")
|
||||||
|
|
||||||
};
|
};
|
||||||
|
@ -262,6 +262,14 @@ function getModuleNodeFiles(module) {
|
|||||||
result.examples = {path:examplesDir};
|
result.examples = {path:examplesDir};
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var resourcesDir = path.join(moduleDir,"resources");
|
||||||
|
try {
|
||||||
|
fs.statSync(resourcesDir)
|
||||||
|
result.resources = {path:resourcesDir};
|
||||||
|
} catch(err) {
|
||||||
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -406,6 +414,7 @@ function convertModuleFileListToObject(moduleFiles,seedObject) {
|
|||||||
user: moduleFile.user||false,
|
user: moduleFile.user||false,
|
||||||
nodes: {},
|
nodes: {},
|
||||||
plugins: {},
|
plugins: {},
|
||||||
|
resources: nodeModuleFiles.resources,
|
||||||
icons: nodeModuleFiles.icons,
|
icons: nodeModuleFiles.icons,
|
||||||
examples: nodeModuleFiles.examples
|
examples: nodeModuleFiles.examples
|
||||||
};
|
};
|
||||||
|
@ -15,8 +15,8 @@
|
|||||||
**/
|
**/
|
||||||
|
|
||||||
//var UglifyJS = require("uglify-js");
|
//var UglifyJS = require("uglify-js");
|
||||||
var path = require("path");
|
const path = require("path");
|
||||||
var fs = require("fs");
|
const fs = require("fs");
|
||||||
|
|
||||||
var library = require("./library");
|
var library = require("./library");
|
||||||
const {events} = require("@node-red/util")
|
const {events} = require("@node-red/util")
|
||||||
@ -680,7 +680,6 @@ function getNodeIconPath(module,icon) {
|
|||||||
|
|
||||||
function getNodeIcons() {
|
function getNodeIcons() {
|
||||||
var iconList = {};
|
var iconList = {};
|
||||||
|
|
||||||
for (var module in moduleConfigs) {
|
for (var module in moduleConfigs) {
|
||||||
if (moduleConfigs.hasOwnProperty(module)) {
|
if (moduleConfigs.hasOwnProperty(module)) {
|
||||||
if (moduleConfigs[module].icons) {
|
if (moduleConfigs[module].icons) {
|
||||||
@ -692,6 +691,21 @@ function getNodeIcons() {
|
|||||||
return iconList;
|
return iconList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getModuleResource(module, resourcePath) {
|
||||||
|
let mod = moduleConfigs[module];
|
||||||
|
if (mod && mod.resources) {
|
||||||
|
let basePath = mod.resources.path;
|
||||||
|
let fullPath = path.join(basePath,resourcePath);
|
||||||
|
if (/^\.\./.test(path.relative(basePath,fullPath))) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (fs.existsSync(fullPath)) {
|
||||||
|
return fullPath;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
var registry = module.exports = {
|
var registry = module.exports = {
|
||||||
init: init,
|
init: init,
|
||||||
load: load,
|
load: load,
|
||||||
@ -722,6 +736,8 @@ var registry = module.exports = {
|
|||||||
|
|
||||||
getNodeIconPath: getNodeIconPath,
|
getNodeIconPath: getNodeIconPath,
|
||||||
getNodeIcons: getNodeIcons,
|
getNodeIcons: getNodeIcons,
|
||||||
|
getModuleResource: getModuleResource,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets all of the node template configs
|
* Gets all of the node template configs
|
||||||
* @return all of the node templates in a single string
|
* @return all of the node templates in a single string
|
||||||
|
@ -463,5 +463,30 @@ var api = module.exports = {
|
|||||||
} else {
|
} else {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a resource from a module
|
||||||
|
* @param {Object} opts
|
||||||
|
* @param {User} opts.user - the user calling the api
|
||||||
|
* @param {String} opts.module - the id of the module requesting the resource
|
||||||
|
* @param {String} opts.path - the path of the resource
|
||||||
|
* @param {Object} opts.req - the request to log (optional)
|
||||||
|
* @return {Promise<Buffer>} - the resource file as a Buffer or null if not found
|
||||||
|
* @memberof @node-red/runtime_nodes
|
||||||
|
*/
|
||||||
|
getModuleResource: async function(opts) {
|
||||||
|
var resourcePath = runtime.nodes.getModuleResource(opts.module, opts.path);
|
||||||
|
if (resourcePath) {
|
||||||
|
return fs.readFile(resourcePath).catch(err => {
|
||||||
|
if (err.code === 'EISDIR') {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
err.status = 400;
|
||||||
|
throw err;
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
return null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -230,6 +230,7 @@ module.exports = {
|
|||||||
getNodeIcons: registry.getNodeIcons,
|
getNodeIcons: registry.getNodeIcons,
|
||||||
getNodeExampleFlows: registry.getNodeExampleFlows,
|
getNodeExampleFlows: registry.getNodeExampleFlows,
|
||||||
getNodeExampleFlowPath: registry.getNodeExampleFlowPath,
|
getNodeExampleFlowPath: registry.getNodeExampleFlowPath,
|
||||||
|
getModuleResource: registry.getModuleResource,
|
||||||
|
|
||||||
clearRegistry: registry.clear,
|
clearRegistry: registry.clear,
|
||||||
cleanModuleList: registry.cleanModuleList,
|
cleanModuleList: registry.cleanModuleList,
|
||||||
|
@ -2,3 +2,5 @@
|
|||||||
console.log("Loaded test-plugin/test")
|
console.log("Loaded test-plugin/test")
|
||||||
// RED.plugins.registerPlugin("")
|
// RED.plugins.registerPlugin("")
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
<script src="resources/test-plugin/script.js">
|
Loading…
Reference in New Issue
Block a user