1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Remove event passing for icons/examples from the api layer

This commit is contained in:
Nick O'Leary 2017-02-15 22:54:32 +00:00
parent 702e6d3b51
commit 869fdbcc6a
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
12 changed files with 291 additions and 149 deletions

View File

@ -20,6 +20,7 @@ var when = require('when');
var redApp = null; var redApp = null;
var storage; var storage;
var log; var log;
var redNodes;
var needsPermission = require("./auth").needsPermission; var needsPermission = require("./auth").needsPermission;
function createLibrary(type) { function createLibrary(type) {
@ -72,80 +73,22 @@ function createLibrary(type) {
} }
} }
var exampleRoots = {};
var exampleFlows = {d:{}};
var exampleCount = 0;
function getFlowsFromPath(path) {
return when.promise(function(resolve,reject) {
var result = {};
fs.readdir(path,function(err,files) {
var promises = [];
var validFiles = [];
files.forEach(function(file) {
var fullPath = fspath.join(path,file);
var stats = fs.lstatSync(fullPath);
if (stats.isDirectory()) {
validFiles.push(file);
promises.push(getFlowsFromPath(fullPath));
} else if (/\.json$/.test(file)){
validFiles.push(file);
exampleCount++;
promises.push(when.resolve(file.split(".")[0]))
}
})
var i=0;
when.all(promises).then(function(results) {
results.forEach(function(r) {
if (typeof r === 'string') {
result.f = result.f||[];
result.f.push(r);
} else {
result.d = result.d||{};
result.d[validFiles[i]] = r;
}
i++;
})
resolve(result);
})
});
})
}
function addNodeExamplesDir(module) {
exampleRoots[module.name] = module.path;
getFlowsFromPath(module.path).then(function(result) {
exampleFlows.d[module.name] = result;
});
}
function removeNodeExamplesDir(module) {
delete exampleRoots[module];
delete exampleFlows.d[module];
}
module.exports = { module.exports = {
init: function(app,runtime) { init: function(app,runtime) {
redApp = app; redApp = app;
log = runtime.log; log = runtime.log;
storage = runtime.storage; storage = runtime.storage;
// TODO: this allows init to be called multiple times without redNodes = runtime.nodes;
// registering multiple instances of the listener.
// It isn't.... ideal.
runtime.events.removeListener("node-examples-dir",addNodeExamplesDir);
runtime.events.on("node-examples-dir",addNodeExamplesDir);
runtime.events.removeListener("node-module-uninstalled",removeNodeExamplesDir);
runtime.events.on("node-module-uninstalled",removeNodeExamplesDir);
}, },
register: createLibrary, register: createLibrary,
getAll: function(req,res) { getAll: function(req,res) {
storage.getAllFlows().then(function(flows) { storage.getAllFlows().then(function(flows) {
log.audit({event: "library.get.all",type:"flow"},req); log.audit({event: "library.get.all",type:"flow"},req);
if (exampleCount > 0) { var examples = redNodes.getNodeExampleFlows();
if (examples) {
flows.d = flows.d||{}; flows.d = flows.d||{};
flows.d._examples_ = exampleFlows; flows.d._examples_ = redNodes.getNodeExampleFlows();
} }
res.json(flows); res.json(flows);
}); });
@ -155,9 +98,9 @@ module.exports = {
var m = /^_examples_\/([^\/]+)\/(.*)$/.exec(req.params[0]); var m = /^_examples_\/([^\/]+)\/(.*)$/.exec(req.params[0]);
if (m) { if (m) {
var module = m[1]; var module = m[1];
var path = m[2]+".json"; var path = m[2];
if (exampleRoots[module]) { var fullPath = redNodes.getNodeExampleFlowPath(module,path);
var fullPath = fspath.join(exampleRoots[module],path); if (fullPath) {
try { try {
fs.statSync(fullPath); fs.statSync(fullPath);
log.audit({event: "library.get",type:"flow",path:req.params[0]},req); log.audit({event: "library.get",type:"flow",path:req.params[0]},req);

View File

@ -16,34 +16,20 @@
var express = require('express'); var express = require('express');
var fs = require("fs"); var fs = require("fs");
var path = require("path"); var path = require("path");
var Mustache = require("mustache");
var theme = require("./theme"); var theme = require("./theme");
var Mustache = require("mustache"); var redNodes;
var icon_paths = {
"node-red":[path.resolve(__dirname + '/../../public/icons')]
};
var iconCache = {};
//TODO: create a default icon
var defaultIcon = path.resolve(__dirname + '/../../public/icons/arrow-in.png');
var templateDir = path.resolve(__dirname+"/../../editor/templates"); var templateDir = path.resolve(__dirname+"/../../editor/templates");
var editorTemplate; var editorTemplate;
function nodeIconDir(dir) {
icon_paths[dir.name] = icon_paths[dir.name] || [];
icon_paths[dir.name].push(path.resolve(dir.path));
}
module.exports = { module.exports = {
init: function(runtime) { init: function(runtime) {
redNodes = runtime.nodes;
editorTemplate = fs.readFileSync(path.join(templateDir,"index.mst"),"utf8"); editorTemplate = fs.readFileSync(path.join(templateDir,"index.mst"),"utf8");
Mustache.parse(editorTemplate); Mustache.parse(editorTemplate);
// TODO: this allows init to be called multiple times without
// registering multiple instances of the listener.
// It isn't.... ideal.
runtime.events.removeListener("node-icon-dir",nodeIconDir);
runtime.events.on("node-icon-dir",nodeIconDir);
}, },
ensureSlash: function(req,res,next) { ensureSlash: function(req,res,next) {
@ -59,26 +45,8 @@ module.exports = {
icon: function(req,res) { icon: function(req,res) {
var icon = req.params.icon; var icon = req.params.icon;
var module = req.params.module; var module = req.params.module;
var iconName = module+"/"+icon; var iconPath = redNodes.getNodeIconPath(module,icon);
if (iconCache[iconName]) {
res.sendFile(iconCache[iconName]); // if not found, express prints this to the console and serves 404
} else {
var paths = icon_paths[module];
if (paths) {
for (var p=0;p<paths.length;p++) {
var iconPath = path.join(paths[p],icon);
try {
fs.statSync(iconPath);
res.sendFile(iconPath); res.sendFile(iconPath);
iconCache[iconName] = iconPath;
return;
} catch(err) {
// iconPath doesn't exist
}
}
}
res.sendFile(defaultIcon);
}
}, },
editor: function(req,res) { editor: function(req,res) {
res.send(Mustache.render(editorTemplate,theme.context())); res.send(Mustache.render(editorTemplate,theme.context()));

View File

@ -25,6 +25,7 @@ var flowUtil = require("./flows/util")
var context = require("./context"); var context = require("./context");
var Node = require("./Node"); var Node = require("./Node");
var log = require("../log"); var log = require("../log");
var library = require("./library");
var events = require("../events"); var events = require("../events");
@ -88,6 +89,7 @@ function init(runtime) {
flows.init(runtime); flows.init(runtime);
registry.init(runtime); registry.init(runtime);
context.init(runtime.settings); context.init(runtime.settings);
library.init(runtime);
} }
function disableNode(id) { function disableNode(id) {
@ -135,6 +137,9 @@ module.exports = {
getNodeConfigs: registry.getNodeConfigs, getNodeConfigs: registry.getNodeConfigs,
getNodeConfig: registry.getNodeConfig, getNodeConfig: registry.getNodeConfig,
getNodeIconPath: registry.getNodeIconPath,
getNodeExampleFlows: library.getExampleFlows,
getNodeExampleFlowPath: library.getExampleFlowPath,
clearRegistry: registry.clear, clearRegistry: registry.clear,
cleanModuleList: registry.cleanModuleList, cleanModuleList: registry.cleanModuleList,

View File

@ -0,0 +1,108 @@
/**
* 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.
**/
var fs = require('fs');
var fspath = require('path');
var when = require('when');
var runtime;
var exampleRoots = {};
var exampleFlows = null;
function getFlowsFromPath(path) {
return when.promise(function(resolve,reject) {
var result = {};
fs.readdir(path,function(err,files) {
var promises = [];
var validFiles = [];
files.forEach(function(file) {
var fullPath = fspath.join(path,file);
var stats = fs.lstatSync(fullPath);
if (stats.isDirectory()) {
validFiles.push(file);
promises.push(getFlowsFromPath(fullPath));
} else if (/\.json$/.test(file)){
validFiles.push(file);
promises.push(when.resolve(file.split(".")[0]))
}
})
var i=0;
when.all(promises).then(function(results) {
results.forEach(function(r) {
if (typeof r === 'string') {
result.f = result.f||[];
result.f.push(r);
} else {
result.d = result.d||{};
result.d[validFiles[i]] = r;
}
i++;
})
resolve(result);
})
});
})
}
function addNodeExamplesDir(module) {
exampleRoots[module.name] = module.path;
getFlowsFromPath(module.path).then(function(result) {
exampleFlows = exampleFlows||{d:{}};
exampleFlows.d[module.name] = result;
});
}
function removeNodeExamplesDir(module) {
delete exampleRoots[module];
if (exampleFlows && exampleFlows.d) {
delete exampleFlows.d[module];
}
if (exampleFlows && Object.keys(exampleFlows.d).length === 0) {
exampleFlows = null;
}
}
function init(_runtime) {
runtime = _runtime;
exampleRoots = {};
exampleFlows = null;
runtime.events.removeListener("node-examples-dir",addNodeExamplesDir);
runtime.events.on("node-examples-dir",addNodeExamplesDir);
runtime.events.removeListener("node-module-uninstalled",removeNodeExamplesDir);
runtime.events.on("node-module-uninstalled",removeNodeExamplesDir);
}
function getExampleFlows() {
return exampleFlows;
}
function getExampleFlowPath(module,path) {
if (exampleRoots[module]) {
return fspath.join(exampleRoots[module],path)+".json";
}
return null;
}
module.exports = {
init: init,
getExampleFlows: getExampleFlows,
getExampleFlowPath: getExampleFlowPath
}

View File

@ -70,6 +70,7 @@ module.exports = {
getNodeConfigs: registry.getAllNodeConfigs, getNodeConfigs: registry.getAllNodeConfigs,
getNodeConfig: registry.getNodeConfig, getNodeConfig: registry.getNodeConfig,
getNodeIconPath: registry.getNodeIconPath,
enableNode: enableNodeSet, enableNode: enableNodeSet,
disableNode: registry.disableNodeSet, disableNode: registry.disableNodeSet,

View File

@ -17,6 +17,9 @@
//var UglifyJS = require("uglify-js"); //var UglifyJS = require("uglify-js");
var util = require("util"); var util = require("util");
var when = require("when"); var when = require("when");
var path = require("path");
var fs = require("fs");
var events = require("../../events"); var events = require("../../events");
var settings; var settings;
@ -41,6 +44,9 @@ function init(_settings,_loader) {
nodeList = []; nodeList = [];
nodeConfigCache = null; nodeConfigCache = null;
Node = require("../Node"); Node = require("../Node");
events.removeListener("node-icon-dir",nodeIconDir);
events.on("node-icon-dir",nodeIconDir);
} }
function load() { function load() {
@ -562,6 +568,40 @@ function setModulePendingUpdated(module,version) {
}); });
} }
var icon_paths = {
"node-red":[path.resolve(__dirname + '/../../../../public/icons')]
};
var iconCache = {};
var defaultIcon = path.resolve(__dirname + '/../../../../public/icons/arrow-in.png');
function nodeIconDir(dir) {
icon_paths[dir.name] = icon_paths[dir.name] || [];
icon_paths[dir.name].push(path.resolve(dir.path));
}
function getNodeIconPath(module,icon) {
var iconName = module+"/"+icon;
if (iconCache[iconName]) {
return iconCache[iconName];
} else {
var paths = icon_paths[module];
if (paths) {
for (var p=0;p<paths.length;p++) {
var iconPath = path.join(paths[p],icon);
try {
fs.statSync(iconPath);
iconCache[iconName] = iconPath;
return iconPath;
} catch(err) {
// iconPath doesn't exist
}
}
}
return defaultIcon;
}
}
var registry = module.exports = { var registry = module.exports = {
init: init, init: init,
load: load, load: load,
@ -583,6 +623,7 @@ var registry = module.exports = {
getModuleList: getModuleList, getModuleList: getModuleList,
getModuleInfo: getModuleInfo, getModuleInfo: getModuleInfo,
getNodeIconPath: getNodeIconPath,
/** /**
* 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

View File

@ -27,7 +27,7 @@ var auth = require("../../../red/api/auth");
describe("library api", function() { describe("library api", function() {
function initLibrary(_flows,_libraryEntries) { function initLibrary(_flows,_libraryEntries,_examples) {
var flows = _flows; var flows = _flows;
var libraryEntries = _libraryEntries; var libraryEntries = _libraryEntries;
library.init(app,{ library.init(app,{
@ -84,6 +84,11 @@ describe("library api", function() {
events: { events: {
on: function(){}, on: function(){},
removeListener: function(){} removeListener: function(){}
},
nodes: {
getNodeExampleFlows: function() {
return _examples;
}
} }
}); });
} }
@ -179,6 +184,22 @@ describe("library api", function() {
.expect(403) .expect(403)
.end(done); .end(done);
}); });
it('includes examples flows if set', function(done) {
var examples = {"d":{"node-module":{"f":["example-one"]}}};
initLibrary({},{},examples);
request(app)
.get('/library/flows')
.expect(200)
.end(function(err,res) {
if (err) {
throw err;
}
res.body.should.have.property('d');
res.body.d.should.have.property('_examples_');
should.deepEqual(res.body.d._examples_,examples);
done();
});
});
}); });
describe("type", function() { describe("type", function() {

View File

@ -29,7 +29,14 @@ describe("ui api", function() {
var app; var app;
before(function() { before(function() {
ui.init({events:events}); ui.init({
events:events,
nodes: {
getNodeIconPath: function(module,icon) {
return path.resolve(__dirname+'/../../../public/icons/arrow-in.png');
}
}
});
}); });
describe("slash handler", function() { describe("slash handler", function() {
before(function() { before(function() {
@ -83,11 +90,10 @@ describe("ui api", function() {
b1[i].should.equal(b2[i]); b1[i].should.equal(b2[i]);
} }
} }
it('returns the requested icon', function(done) {
it('returns the default icon when getting an unknown icon', function(done) {
var defaultIcon = fs.readFileSync(path.resolve(__dirname+'/../../../public/icons/arrow-in.png')); var defaultIcon = fs.readFileSync(path.resolve(__dirname+'/../../../public/icons/arrow-in.png'));
request(app) request(app)
.get("/icons/random-module/youwonthaveme.png") .get("/icons/module/icon.png")
.expect("Content-Type", /image\/png/) .expect("Content-Type", /image\/png/)
.expect(200) .expect(200)
.parse(binaryParser) .parse(binaryParser)
@ -101,40 +107,6 @@ describe("ui api", function() {
}); });
}); });
it('returns a known icon', function(done) {
var injectIcon = fs.readFileSync(path.resolve(__dirname+'/../../../public/icons/inject.png'));
request(app)
.get("/icons/node-red/inject.png")
.expect("Content-Type", /image\/png/)
.expect(200)
.parse(binaryParser)
.end(function(err, res){
if (err){
return done(err);
}
Buffer.isBuffer(res.body).should.be.true();
compareBuffers(res.body,injectIcon);
done();
});
});
it('returns a registered icon' , function(done) {
var testIcon = fs.readFileSync(path.resolve(__dirname+'/../../resources/icons/test_icon.png'));
events.emit("node-icon-dir",{name:"test-module", path: path.resolve(__dirname+'/../../resources/icons')});
request(app)
.get("/icons/test-module/test_icon.png")
.expect("Content-Type", /image\/png/)
.expect(200)
.parse(binaryParser)
.end(function(err, res){
if (err){
return done(err);
}
Buffer.isBuffer(res.body).should.be.true();
compareBuffers(res.body,testIcon);
done();
});
});
}); });
describe("editor ui handler", function() { describe("editor ui handler", function() {
@ -174,8 +146,4 @@ describe("ui api", function() {
}); });
}); });
}); });
}); });

View File

@ -56,10 +56,12 @@ describe("red/nodes/index", function() {
get: function() { return false } get: function() { return false }
}; };
var EventEmitter = require('events').EventEmitter;
var runtime = { var runtime = {
settings: settings, settings: settings,
storage: storage, storage: storage,
log: {debug:function() {}, warn:function() {}} log: {debug:function() {}, warn:function() {}},
events: new EventEmitter()
}; };
function TestNode(n) { function TestNode(n) {

View File

@ -0,0 +1,69 @@
/**
* 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.
**/
var EventEmitter = require('events').EventEmitter;
var events = new EventEmitter();
var should = require("should");
var fs = require("fs");
var path = require("path");
var library = require("../../../../red/runtime/nodes/library")
describe("library api", function() {
it('returns null list when no modules have been registered', function() {
library.init({events:events});
should.not.exist(library.getExampleFlows());
});
it('returns null path when module is not known', function() {
library.init({events:events});
should.not.exist(library.getExampleFlowPath('foo','bar'));
});
it('returns a valid example path', function(done) {
library.init({events:events});
events.emit('node-examples-dir',{
name: "test-module",
path: path.resolve(__dirname+'/../../../resources/examples')
});
setTimeout(function() {
try {
var flows = library.getExampleFlows();
flows.should.deepEqual({"d":{"test-module":{"f":["one"]}}});
var examplePath = library.getExampleFlowPath('test-module','one');
examplePath.should.eql(path.resolve(__dirname+'/../../../resources/examples/one.json'))
events.emit('node-module-uninstalled', 'test-module');
setTimeout(function() {
try {
should.not.exist(library.getExampleFlows());
should.not.exist(library.getExampleFlowPath('test-module','one'));
done();
} catch(err) {
done(err);
}
},20);
}catch(err) {
done(err);
}
},20);
})
});

View File

@ -17,6 +17,7 @@
var should = require("should"); var should = require("should");
var when = require("when"); var when = require("when");
var sinon = require("sinon"); var sinon = require("sinon");
var path = require("path");
var typeRegistry = require("../../../../../red/runtime/nodes/registry/registry"); var typeRegistry = require("../../../../../red/runtime/nodes/registry/registry");
@ -483,4 +484,19 @@ describe("red/nodes/registry/registry",function() {
}); });
}); });
describe('#getNodeIconPath', function() {
it('returns the default icon when getting an unknown icon', function() {
var defaultIcon = path.resolve(__dirname+'/../../../../../public/icons/arrow-in.png');
var iconPath = typeRegistry.getNodeIconPath('random-module','youwonthaveme.png');
iconPath.should.eql(defaultIcon);
});
it('returns a registered icon' , function() {
var testIcon = path.resolve(__dirname+'/../../../../resources/icons/test_icon.png');
events.emit("node-icon-dir",{name:"test-module", path: path.resolve(__dirname+'/../../../../resources/icons')});
var iconPath = typeRegistry.getNodeIconPath('test-module','test_icon.png');
iconPath.should.eql(testIcon);
});
});
}); });

View File