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 storage;
var log;
var redNodes;
var needsPermission = require("./auth").needsPermission;
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 = {
init: function(app,runtime) {
redApp = app;
log = runtime.log;
storage = runtime.storage;
// TODO: this allows init to be called multiple times without
// 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);
redNodes = runtime.nodes;
},
register: createLibrary,
getAll: function(req,res) {
storage.getAllFlows().then(function(flows) {
log.audit({event: "library.get.all",type:"flow"},req);
if (exampleCount > 0) {
var examples = redNodes.getNodeExampleFlows();
if (examples) {
flows.d = flows.d||{};
flows.d._examples_ = exampleFlows;
flows.d._examples_ = redNodes.getNodeExampleFlows();
}
res.json(flows);
});
@ -155,9 +98,9 @@ module.exports = {
var m = /^_examples_\/([^\/]+)\/(.*)$/.exec(req.params[0]);
if (m) {
var module = m[1];
var path = m[2]+".json";
if (exampleRoots[module]) {
var fullPath = fspath.join(exampleRoots[module],path);
var path = m[2];
var fullPath = redNodes.getNodeExampleFlowPath(module,path);
if (fullPath) {
try {
fs.statSync(fullPath);
log.audit({event: "library.get",type:"flow",path:req.params[0]},req);

View File

@ -16,34 +16,20 @@
var express = require('express');
var fs = require("fs");
var path = require("path");
var Mustache = require("mustache");
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 editorTemplate;
function nodeIconDir(dir) {
icon_paths[dir.name] = icon_paths[dir.name] || [];
icon_paths[dir.name].push(path.resolve(dir.path));
}
module.exports = {
init: function(runtime) {
redNodes = runtime.nodes;
editorTemplate = fs.readFileSync(path.join(templateDir,"index.mst"),"utf8");
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) {
@ -59,26 +45,8 @@ module.exports = {
icon: function(req,res) {
var icon = req.params.icon;
var module = req.params.module;
var iconName = 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);
iconCache[iconName] = iconPath;
return;
} catch(err) {
// iconPath doesn't exist
}
}
}
res.sendFile(defaultIcon);
}
var iconPath = redNodes.getNodeIconPath(module,icon);
res.sendFile(iconPath);
},
editor: function(req,res) {
res.send(Mustache.render(editorTemplate,theme.context()));

View File

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

View File

@ -17,6 +17,9 @@
//var UglifyJS = require("uglify-js");
var util = require("util");
var when = require("when");
var path = require("path");
var fs = require("fs");
var events = require("../../events");
var settings;
@ -41,6 +44,9 @@ function init(_settings,_loader) {
nodeList = [];
nodeConfigCache = null;
Node = require("../Node");
events.removeListener("node-icon-dir",nodeIconDir);
events.on("node-icon-dir",nodeIconDir);
}
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 = {
init: init,
load: load,
@ -583,6 +623,7 @@ var registry = module.exports = {
getModuleList: getModuleList,
getModuleInfo: getModuleInfo,
getNodeIconPath: getNodeIconPath,
/**
* Gets all of the node template configs
* @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() {
function initLibrary(_flows,_libraryEntries) {
function initLibrary(_flows,_libraryEntries,_examples) {
var flows = _flows;
var libraryEntries = _libraryEntries;
library.init(app,{
@ -84,6 +84,11 @@ describe("library api", function() {
events: {
on: function(){},
removeListener: function(){}
},
nodes: {
getNodeExampleFlows: function() {
return _examples;
}
}
});
}
@ -179,6 +184,22 @@ describe("library api", function() {
.expect(403)
.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() {

View File

@ -29,7 +29,14 @@ describe("ui api", function() {
var app;
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() {
before(function() {
@ -71,10 +78,10 @@ describe("ui api", function() {
res.setEncoding('binary');
res.data = '';
res.on('data', function (chunk) {
res.data += chunk;
res.data += chunk;
});
res.on('end', function () {
callback(null, new Buffer(res.data, 'binary'));
callback(null, new Buffer(res.data, 'binary'));
});
}
function compareBuffers(b1,b2) {
@ -83,11 +90,10 @@ describe("ui api", function() {
b1[i].should.equal(b2[i]);
}
}
it('returns the default icon when getting an unknown icon', function(done) {
it('returns the requested icon', function(done) {
var defaultIcon = fs.readFileSync(path.resolve(__dirname+'/../../../public/icons/arrow-in.png'));
request(app)
.get("/icons/random-module/youwonthaveme.png")
.get("/icons/module/icon.png")
.expect("Content-Type", /image\/png/)
.expect(200)
.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() {
@ -174,8 +146,4 @@ describe("ui api", function() {
});
});
});
});

View File

@ -56,10 +56,12 @@ describe("red/nodes/index", function() {
get: function() { return false }
};
var EventEmitter = require('events').EventEmitter;
var runtime = {
settings: settings,
storage: storage,
log: {debug:function() {}, warn:function() {}}
log: {debug:function() {}, warn:function() {}},
events: new EventEmitter()
};
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 when = require("when");
var sinon = require("sinon");
var path = require("path");
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