Allow module to provide resources and automatically expose them

This commit is contained in:
Nick O'Leary
2021-03-15 21:06:10 +00:00
parent 827f8d4d51
commit 8543613563
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
}
}
}