Merge pull request #2903 from node-red/plugin-resources

Allow module to provide resources and automatically expose them
This commit is contained in:
Nick O'Leary
2021-03-30 22:50:36 +01:00
committed by GitHub
8 changed files with 94 additions and 5 deletions

View File

@@ -463,5 +463,30 @@ var api = module.exports = {
} else {
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
}
}
}

View File

@@ -230,6 +230,7 @@ module.exports = {
getNodeIcons: registry.getNodeIcons,
getNodeExampleFlows: registry.getNodeExampleFlows,
getNodeExampleFlowPath: registry.getNodeExampleFlowPath,
getModuleResource: registry.getModuleResource,
clearRegistry: registry.clear,
cleanModuleList: registry.cleanModuleList,