Skip to content

Commit

Permalink
WIP: runtime api for node modules
Browse files Browse the repository at this point in the history
  • Loading branch information
knolleary committed Jan 6, 2016
1 parent f62b7af commit d673846
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 75 deletions.
8 changes: 3 additions & 5 deletions red/red.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function checkBuild() {
}
}

var RED = {
module.exports = {
init: function(httpServer,userSettings) {
server = httpServer;

Expand Down Expand Up @@ -79,12 +79,12 @@ var RED = {
})
},
nodes: runtime.api,
events: runtime.events,
log: runtime.log,
comms: api.comms,
settings:runtime.settings,
util: runtime.util,
version: runtime.version,

comms: api.comms,
library: api.library,
auth: api.auth,

Expand All @@ -93,5 +93,3 @@ var RED = {
get httpNode() { return nodeApp },
get server() { return server }
};

module.exports = RED;
2 changes: 1 addition & 1 deletion red/runtime/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function start() {
}
log.info(log._("runtime.version",{component:"Node.js ",version:process.version}));
log.info(log._("server.loading"));
redNodes.init(settings,storage);
redNodes.init(runtime);
return redNodes.load().then(function() {

var i;
Expand Down
10 changes: 5 additions & 5 deletions red/runtime/nodes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ function createNode(node,def) {
}
}

function init(_settings,storage) {
settings = _settings;
credentials.init(storage);
flows.init(_settings,storage);
registry.init(_settings);
function init(runtime) {
settings = runtime.settings;
credentials.init(runtime.storage);
flows.init(runtime.settings,runtime.storage);
registry.init(runtime);
}

function disableNode(id) {
Expand Down
10 changes: 5 additions & 5 deletions red/runtime/nodes/registry/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ var installer = require("./installer");

var settings;

function init(_settings) {
settings = _settings;
installer.init(settings);
loader.init(settings);
registry.init(settings,loader);
function init(runtime) {
settings = runtime.settings;
installer.init(runtime.settings);
loader.init(runtime);
registry.init(runtime.settings,loader);
}

function addModule(module) {
Expand Down
40 changes: 14 additions & 26 deletions red/runtime/nodes/registry/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,25 @@ var fs = require("fs");
var path = require("path");
var semver = require("semver");

var events = require("../../events");

var localfilesystem = require("./localfilesystem");
var registry = require("./registry");

var RED;
var settings;
var runtime;

var i18n = require("../../i18n");

events.on("node-locales-dir", function(info) {
i18n.registerMessageCatalog(info.namespace,info.dir,info.file);
});
function registerMessageCatalog(info) {
runtime.i18n.registerMessageCatalog(info.namespace,info.dir,info.file);
}

function init(_settings) {
settings = _settings;
localfilesystem.init(settings);
function init(_runtime) {
runtime = _runtime;
settings = runtime.settings;
localfilesystem.init(runtime);
if (runtime.events) {
runtime.events.removeListener("node-locales-dir", registerMessageCatalog);
runtime.events.on("node-locales-dir", registerMessageCatalog);
}
RED = require('../../../red');
}

Expand All @@ -55,7 +57,7 @@ function loadNodeFiles(nodeFiles) {
/* istanbul ignore else */
if (nodeFiles.hasOwnProperty(module)) {
if (nodeFiles[module].redVersion &&
!semver.satisfies(RED.version().replace("-git",""), nodeFiles[module].redVersion)) {
!semver.satisfies(runtime.version().replace("-git",""), nodeFiles[module].redVersion)) {
//TODO: log it
continue;
}
Expand Down Expand Up @@ -195,7 +197,7 @@ function loadNodeConfig(fileInfo) {
fs.stat(path.join(path.dirname(file),"locales"),function(err,stat) {
if (!err) {
node.namespace = node.id;
i18n.registerMessageCatalog(node.id,
runtime.i18n.registerMessageCatalog(node.id,
path.join(path.dirname(file),"locales"),
path.basename(file,".js")+".json")
.then(function() {
Expand All @@ -211,20 +213,6 @@ function loadNodeConfig(fileInfo) {
});
}


//function getAPIForNode(node) {
// var red = {
// nodes: RED.nodes,
// library: RED.library,
// credentials: RED.credentials,
// events: RED.events,
// log: RED.log,
//
// }
//
//}


/**
* Loads the specified node into the runtime
* @param node a node info object - see loadNodeConfig
Expand Down
11 changes: 7 additions & 4 deletions red/runtime/nodes/registry/localfilesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@ var when = require("when");
var fs = require("fs");
var path = require("path");

var events = require("../../events");
var log = require("../../log");
var events;
var log;

var settings;
var defaultNodesDir = path.resolve(path.join(__dirname,"..","..","..","..","nodes"));
var disableNodePathScan = false;

function init(_settings,_defaultNodesDir,_disableNodePathScan) {
settings = _settings;
function init(runtime,_defaultNodesDir,_disableNodePathScan) {
settings = runtime.settings;
events = runtime.events;
log = runtime.log;

if (_disableNodePathScan) {
disableNodePathScan = _disableNodePathScan;
}
Expand Down
2 changes: 1 addition & 1 deletion test/red/runtime/nodes/credentials_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ describe('Credentials', function() {
var settings = {
available: function() { return false;}
}
index.init(settings, storage);
index.init({settings:settings, storage:storage});
index.registerType('test', TestNode);
index.loadFlows().then(function() {
var testnode = new TestNode({id:'tab1',type:'test',name:'barney'});
Expand Down
19 changes: 12 additions & 7 deletions test/red/runtime/nodes/index_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ describe("red/nodes/index", function() {
available: function() { return false }
};

var runtime = {
settings: settings,
storage: storage
};

function TestNode(n) {
index.createNode(this, n);
var node = this;
Expand All @@ -68,7 +73,7 @@ describe("red/nodes/index", function() {
}

it('nodes are initialised with credentials',function(done) {
index.init(settings, storage);
index.init(runtime);
index.registerType('test', TestNode);
index.loadFlows().then(function() {
var testnode = new TestNode({id:'tab1',type:'test',name:'barney'});
Expand All @@ -81,7 +86,7 @@ describe("red/nodes/index", function() {
});

it('flows should be initialised',function(done) {
index.init(settings, storage);
index.init(runtime);
index.loadFlows().then(function() {
should.deepEqual(testFlows, index.getFlows());
done();
Expand Down Expand Up @@ -168,7 +173,7 @@ describe("red/nodes/index", function() {
});

it(': allows an unused node type to be disabled',function(done) {
index.init(settings, storage);
index.init(runtime);
index.registerType('test', TestNode);
index.loadFlows().then(function() {
var info = index.disableNode("5678");
Expand All @@ -182,7 +187,7 @@ describe("red/nodes/index", function() {
});

it(': prevents disabling a node type that is in use',function(done) {
index.init(settings, storage);
index.init(runtime);
index.registerType('test', TestNode);
index.loadFlows().then(function() {
/*jshint immed: false */
Expand All @@ -197,7 +202,7 @@ describe("red/nodes/index", function() {
});

it(': prevents disabling a node type that is unknown',function(done) {
index.init(settings, storage);
index.init(runtime);
index.registerType('test', TestNode);
index.loadFlows().then(function() {
/*jshint immed: false */
Expand Down Expand Up @@ -250,7 +255,7 @@ describe("red/nodes/index", function() {
});

it(': prevents removing a module that is in use',function(done) {
index.init(settings, storage);
index.init(runtime);
index.registerType('test', TestNode);
index.loadFlows().then(function() {
/*jshint immed: false */
Expand All @@ -265,7 +270,7 @@ describe("red/nodes/index", function() {
});

it(': prevents removing a module that is unknown',function(done) {
index.init(settings, storage);
index.init(runtime);
index.registerType('test', TestNode);
index.loadFlows().then(function() {
/*jshint immed: false */
Expand Down
26 changes: 13 additions & 13 deletions test/red/runtime/nodes/registry/loader_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe("red/nodes/registry/loader",function() {
})
describe("#init",function() {
it("init",function() {
loader.init({});
loader.init({events:{on:function(){},removeListener:function(){}}});
localfilesystem.init.called.should.be.true;
});
});
Expand All @@ -53,7 +53,7 @@ describe("red/nodes/registry/loader",function() {
it("load empty set without settings available", function(done) {
stubs.push(sinon.stub(localfilesystem,"getNodeFiles", function(){ return {};}));
stubs.push(sinon.stub(registry,"saveNodeList", function(){ return {};}));
loader.init({available:function(){return false;}});
loader.init({events:{on:function(){},removeListener:function(){}},settings:{available:function(){return false;}}});
loader.load("foo",true).then(function() {
localfilesystem.getNodeFiles.called.should.be.true;
localfilesystem.getNodeFiles.lastCall.args[0].should.eql('foo');
Expand All @@ -65,7 +65,7 @@ describe("red/nodes/registry/loader",function() {
it("load empty set with settings available triggers registery save", function(done) {
stubs.push(sinon.stub(localfilesystem,"getNodeFiles", function(){ return {};}));
stubs.push(sinon.stub(registry,"saveNodeList", function(){ return {};}));
loader.init({available:function(){return true;}});
loader.init({events:{on:function(){},removeListener:function(){}},settings:{available:function(){return true;}}});
loader.load("foo",true).then(function() {
registry.saveNodeList.called.should.be.true;
done();
Expand Down Expand Up @@ -96,7 +96,7 @@ describe("red/nodes/registry/loader",function() {
stubs.push(sinon.stub(registry,"getNodeInfo", function(){ return null; }));

stubs.push(sinon.stub(nodes,"registerType"));
loader.init({available:function(){return true;}});
loader.init({events:{on:function(){},removeListener:function(){}},settings:{available:function(){return true;}}});
loader.load().then(function(result) {
registry.addNodeSet.called.should.be.true;
registry.addNodeSet.lastCall.args[0].should.eql("node-red/TestNode1");
Expand Down Expand Up @@ -143,7 +143,7 @@ describe("red/nodes/registry/loader",function() {
// This module isn't already loaded
stubs.push(sinon.stub(registry,"getNodeInfo", function(){ return null; }));
stubs.push(sinon.stub(nodes,"registerType"));
loader.init({available:function(){return true;}});
loader.init({events:{on:function(){},removeListener:function(){}},settings:{available:function(){return true;}}});
loader.load().then(function(result) {
registry.addNodeSet.called.should.be.true;
registry.addNodeSet.lastCall.args[0].should.eql("node-red/MultipleNodes1");
Expand Down Expand Up @@ -194,7 +194,7 @@ describe("red/nodes/registry/loader",function() {
stubs.push(sinon.stub(registry,"getNodeInfo", function(){ return null; }));

stubs.push(sinon.stub(nodes,"registerType"));
loader.init({available:function(){return true;}});
loader.init({events:{on:function(){},removeListener:function(){}},settings:{available:function(){return true;}}});
loader.load().then(function(result) {
registry.addNodeSet.called.should.be.true;
registry.addNodeSet.lastCall.args[0].should.eql("node-red/TestNode2");
Expand Down Expand Up @@ -243,7 +243,7 @@ describe("red/nodes/registry/loader",function() {
stubs.push(sinon.stub(registry,"getNodeInfo", function(){ return null; }));

stubs.push(sinon.stub(nodes,"registerType"));
loader.init({available:function(){return true;}});
loader.init({events:{on:function(){},removeListener:function(){}},settings:{available:function(){return true;}}});
loader.load().then(function(result) {
registry.addNodeSet.called.should.be.true;
registry.addNodeSet.lastCall.args[0].should.eql("node-red/TestNode3");
Expand Down Expand Up @@ -290,7 +290,7 @@ describe("red/nodes/registry/loader",function() {
stubs.push(sinon.stub(registry,"getNodeInfo", function(){ return null; }));

stubs.push(sinon.stub(nodes,"registerType"));
loader.init({available:function(){return true;}});
loader.init({events:{on:function(){},removeListener:function(){}},settings:{available:function(){return true;}}});
loader.load().then(function(result) {
registry.addNodeSet.called.should.be.true;
registry.addNodeSet.lastCall.args[0].should.eql("node-red/DoesNotExist");
Expand All @@ -317,7 +317,7 @@ describe("red/nodes/registry/loader",function() {

describe("#addModule",function() {
it("throws error if settings unavailable", function() {
loader.init({available:function(){return false;}});
loader.init({events:{on:function(){},removeListener:function(){}},settings:{available:function(){return false;}}});
/*jshint immed: false */
(function(){
loader.addModule("test-module");
Expand All @@ -326,7 +326,7 @@ describe("red/nodes/registry/loader",function() {

it("returns rejected error if module already loaded", function(done) {
stubs.push(sinon.stub(registry,"getModuleInfo",function(){return{}}));
loader.init({available:function(){return true;}});
loader.init({events:{on:function(){},removeListener:function(){}},settings:{available:function(){return true;}}});

loader.addModule("test-module").otherwise(function(err) {
err.code.should.eql("module_already_loaded");
Expand All @@ -338,7 +338,7 @@ describe("red/nodes/registry/loader",function() {
stubs.push(sinon.stub(localfilesystem,"getModuleFiles",function() {
throw new Error("failure");
}));
loader.init({available:function(){return true;}});
loader.init({events:{on:function(){},removeListener:function(){}},settings:{available:function(){return true;}}});
loader.addModule("test-module").otherwise(function(err) {
err.message.should.eql("failure");
done();
Expand Down Expand Up @@ -370,7 +370,7 @@ describe("red/nodes/registry/loader",function() {
stubs.push(sinon.stub(registry,"saveNodeList", function(){ return "a node list" }));
stubs.push(sinon.stub(registry,"addNodeSet", function(){ return }));
stubs.push(sinon.stub(nodes,"registerType"));
loader.init({available:function(){return true;}});
loader.init({events:{on:function(){},removeListener:function(){}},settings:{available:function(){return true;}}});
loader.addModule("TestNodeModule").then(function(result) {
result.should.eql("a node list");
registry.addNodeSet.calledOnce.should.be.true;
Expand Down Expand Up @@ -420,7 +420,7 @@ describe("red/nodes/registry/loader",function() {
stubs.push(sinon.stub(registry,"saveNodeList", function(){ return "a node list" }));
stubs.push(sinon.stub(registry,"addNodeSet", function(){ return }));
stubs.push(sinon.stub(nodes,"registerType"));
loader.init({available:function(){return true;}});
loader.init({events:{on:function(){},removeListener:function(){}},version: function() { return "0.12.0"}, settings:{available:function(){return true;}}});
loader.addModule("TestNodeModule").then(function(result) {
result.should.eql("a node list");
registry.addNodeSet.called.should.be.false;
Expand Down
Loading

0 comments on commit d673846

Please sign in to comment.