mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Move comms from runtime to api component
This commit is contained in:
parent
9f5e6a4b37
commit
8fb955e182
@ -124,7 +124,6 @@ module.exports = function(RED) {
|
|||||||
if (msg.msg.length > debuglength) {
|
if (msg.msg.length > debuglength) {
|
||||||
msg.msg = msg.msg.substr(0,debuglength) +" ....";
|
msg.msg = msg.msg.substr(0,debuglength) +" ....";
|
||||||
}
|
}
|
||||||
|
|
||||||
RED.comms.publish("debug",msg);
|
RED.comms.publish("debug",msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
**/
|
**/
|
||||||
|
|
||||||
var ws = require("ws");
|
var ws = require("ws");
|
||||||
var log = require("./log");
|
var log;
|
||||||
|
|
||||||
var server;
|
var server;
|
||||||
var settings;
|
var settings;
|
||||||
@ -29,17 +29,24 @@ var retained = {};
|
|||||||
var heartbeatTimer;
|
var heartbeatTimer;
|
||||||
var lastSentTime;
|
var lastSentTime;
|
||||||
|
|
||||||
|
function handleStatus(event) {
|
||||||
|
publish("status/"+event.id,event.status,true);
|
||||||
|
}
|
||||||
|
|
||||||
function init(_server,_settings) {
|
function init(_server,runtime) {
|
||||||
server = _server;
|
server = _server;
|
||||||
settings = _settings;
|
settings = runtime.settings;
|
||||||
|
log = runtime.log;
|
||||||
|
|
||||||
|
runtime.events.removeListener("node-status",handleStatus);
|
||||||
|
runtime.events.on("node-status",handleStatus);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function start() {
|
function start() {
|
||||||
var Tokens = require("../api/auth/tokens");
|
var Tokens = require("./auth/tokens");
|
||||||
var Users = require("../api/auth/users");
|
var Users = require("./auth/users");
|
||||||
var Permissions = require("../api/auth/permissions");
|
var Permissions = require("./auth/permissions");
|
||||||
if (!settings.disableEditor) {
|
if (!settings.disableEditor) {
|
||||||
Users.default().then(function(anonymousUser) {
|
Users.default().then(function(anonymousUser) {
|
||||||
var webSocketKeepAliveTime = settings.webSocketKeepAliveTime || 15000;
|
var webSocketKeepAliveTime = settings.webSocketKeepAliveTime || 15000;
|
||||||
@ -151,6 +158,7 @@ function stop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function publish(topic,data,retain) {
|
function publish(topic,data,retain) {
|
||||||
|
if (server) {
|
||||||
if (retain) {
|
if (retain) {
|
||||||
retained[topic] = data;
|
retained[topic] = data;
|
||||||
} else {
|
} else {
|
||||||
@ -161,6 +169,7 @@ function publish(topic,data,retain) {
|
|||||||
publishTo(conn,topic,data);
|
publishTo(conn,topic,data);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function publishTo(ws,topic,data) {
|
function publishTo(ws,topic,data) {
|
||||||
var msg = JSON.stringify({topic:topic,data:data});
|
var msg = JSON.stringify({topic:topic,data:data});
|
@ -19,6 +19,7 @@ var bodyParser = require("body-parser");
|
|||||||
var util = require('util');
|
var util = require('util');
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
var passport = require('passport');
|
var passport = require('passport');
|
||||||
|
var when = require('when');
|
||||||
|
|
||||||
var ui = require("./ui");
|
var ui = require("./ui");
|
||||||
var nodes = require("./nodes");
|
var nodes = require("./nodes");
|
||||||
@ -28,6 +29,7 @@ var info = require("./info");
|
|||||||
var theme = require("./theme");
|
var theme = require("./theme");
|
||||||
var locales = require("./locales");
|
var locales = require("./locales");
|
||||||
var credentials = require("./credentials");
|
var credentials = require("./credentials");
|
||||||
|
var comms = require("./comms");
|
||||||
|
|
||||||
var auth = require("./auth");
|
var auth = require("./auth");
|
||||||
var needsPermission = auth.needsPermission;
|
var needsPermission = auth.needsPermission;
|
||||||
@ -46,13 +48,14 @@ var errorHandler = function(err,req,res,next) {
|
|||||||
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
||||||
};
|
};
|
||||||
|
|
||||||
function init(runtime) {
|
function init(server,runtime) {
|
||||||
var settings = runtime.settings;
|
var settings = runtime.settings;
|
||||||
log = runtime.log;
|
log = runtime.log;
|
||||||
if (settings.httpNodeRoot !== false) {
|
if (settings.httpNodeRoot !== false) {
|
||||||
nodeApp = express();
|
nodeApp = express();
|
||||||
}
|
}
|
||||||
if (settings.httpAdminRoot !== false) {
|
if (settings.httpAdminRoot !== false) {
|
||||||
|
comms.init(server,runtime);
|
||||||
adminApp = express();
|
adminApp = express();
|
||||||
auth.init(runtime);
|
auth.init(runtime);
|
||||||
credentials.init(runtime);
|
credentials.init(runtime);
|
||||||
@ -123,15 +126,27 @@ function init(runtime) {
|
|||||||
adminApp.use(errorHandler);
|
adminApp.use(errorHandler);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
function start() {
|
||||||
|
comms.start();
|
||||||
|
return when.resolve();
|
||||||
|
}
|
||||||
|
function stop() {
|
||||||
|
comms.stop();
|
||||||
|
return when.resolve();
|
||||||
|
}
|
||||||
module.exports = {
|
module.exports = {
|
||||||
init: init,
|
init: init,
|
||||||
|
start: start,
|
||||||
|
stop: stop,
|
||||||
library: {
|
library: {
|
||||||
register: library.register
|
register: library.register
|
||||||
},
|
},
|
||||||
auth: {
|
auth: {
|
||||||
needsPermission: auth.needsPermission
|
needsPermission: auth.needsPermission
|
||||||
},
|
},
|
||||||
|
comms: {
|
||||||
|
publish: comms.publish
|
||||||
|
},
|
||||||
adminApp: function() { return adminApp; },
|
adminApp: function() { return adminApp; },
|
||||||
nodeApp: function() { return nodeApp; }
|
nodeApp: function() { return nodeApp; }
|
||||||
};
|
};
|
||||||
|
25
red/red.js
25
red/red.js
@ -25,6 +25,7 @@ process.env.NODE_RED_HOME = process.env.NODE_RED_HOME || path.resolve(__dirname+
|
|||||||
var nodeApp = null;
|
var nodeApp = null;
|
||||||
var adminApp = null;
|
var adminApp = null;
|
||||||
var server = null;
|
var server = null;
|
||||||
|
var apiEnabled = false;
|
||||||
|
|
||||||
function checkBuild() {
|
function checkBuild() {
|
||||||
var editorFile = path.resolve(path.join(__dirname,"..","public","red","red.min.js"));
|
var editorFile = path.resolve(path.join(__dirname,"..","public","red","red.min.js"));
|
||||||
@ -44,9 +45,9 @@ var RED = {
|
|||||||
if (!userSettings.SKIP_BUILD_CHECK) {
|
if (!userSettings.SKIP_BUILD_CHECK) {
|
||||||
checkBuild();
|
checkBuild();
|
||||||
}
|
}
|
||||||
runtime.init(httpServer,userSettings);
|
runtime.init(userSettings);
|
||||||
if (userSettings.httpAdminRoot !== false || userSettings.httpNodeRoot !== false) {
|
if (userSettings.httpAdminRoot !== false || userSettings.httpNodeRoot !== false) {
|
||||||
api.init(runtime);
|
api.init(server,runtime);
|
||||||
adminApp = api.adminApp();
|
adminApp = api.adminApp();
|
||||||
nodeApp = api.nodeApp();
|
nodeApp = api.nodeApp();
|
||||||
}
|
}
|
||||||
@ -58,15 +59,29 @@ var RED = {
|
|||||||
put: function(){},
|
put: function(){},
|
||||||
delete: function(){}
|
delete: function(){}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
apiEnabled = true;
|
||||||
}
|
}
|
||||||
return runtime.app;
|
return runtime.app;
|
||||||
},
|
},
|
||||||
start: runtime.start,
|
start: function() {
|
||||||
stop: runtime.stop,
|
return runtime.start().then(function() {
|
||||||
|
if (apiEnabled) {
|
||||||
|
return api.start();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
stop: function() {
|
||||||
|
return runtime.stop().then(function() {
|
||||||
|
if (apiEnabled) {
|
||||||
|
return api.stop();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},
|
||||||
nodes: runtime.api,
|
nodes: runtime.api,
|
||||||
events: runtime.events,
|
events: runtime.events,
|
||||||
log: runtime.log,
|
log: runtime.log,
|
||||||
comms: runtime.comms,
|
comms: api.comms,
|
||||||
settings:runtime.settings,
|
settings:runtime.settings,
|
||||||
util: runtime.util,
|
util: runtime.util,
|
||||||
version: runtime.version,
|
version: runtime.version,
|
||||||
|
@ -17,7 +17,6 @@
|
|||||||
var when = require('when');
|
var when = require('when');
|
||||||
|
|
||||||
var redNodes = require("./nodes");
|
var redNodes = require("./nodes");
|
||||||
var comms = require("./comms");
|
|
||||||
var storage = require("./storage");
|
var storage = require("./storage");
|
||||||
var log = require("./log");
|
var log = require("./log");
|
||||||
var i18n = require("./i18n");
|
var i18n = require("./i18n");
|
||||||
@ -28,11 +27,10 @@ var fs = require("fs");
|
|||||||
|
|
||||||
var runtimeMetricInterval = null;
|
var runtimeMetricInterval = null;
|
||||||
|
|
||||||
function init(server,userSettings) {
|
function init(userSettings) {
|
||||||
userSettings.version = version();
|
userSettings.version = version();
|
||||||
log.init(userSettings);
|
log.init(userSettings);
|
||||||
settings.init(userSettings);
|
settings.init(userSettings);
|
||||||
comms.init(server,settings);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function version() {
|
function version() {
|
||||||
@ -105,7 +103,6 @@ function start() {
|
|||||||
}
|
}
|
||||||
log.info(log._("runtime.paths.settings",{path:settings.settingsFile}));
|
log.info(log._("runtime.paths.settings",{path:settings.settingsFile}));
|
||||||
redNodes.loadFlows().then(redNodes.startFlows);
|
redNodes.loadFlows().then(redNodes.startFlows);
|
||||||
comms.start();
|
|
||||||
}).otherwise(function(err) {
|
}).otherwise(function(err) {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
});
|
});
|
||||||
@ -137,8 +134,7 @@ function stop() {
|
|||||||
clearInterval(runtimeMetricInterval);
|
clearInterval(runtimeMetricInterval);
|
||||||
runtimeMetricInterval = null;
|
runtimeMetricInterval = null;
|
||||||
}
|
}
|
||||||
redNodes.stopFlows();
|
return redNodes.stopFlows();
|
||||||
comms.stop();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var runtime = module.exports = {
|
var runtime = module.exports = {
|
||||||
@ -152,7 +148,6 @@ var runtime = module.exports = {
|
|||||||
i18n: i18n,
|
i18n: i18n,
|
||||||
settings: settings,
|
settings: settings,
|
||||||
storage: storage,
|
storage: storage,
|
||||||
comms: comms,
|
|
||||||
events: events,
|
events: events,
|
||||||
api: redNodes,
|
api: redNodes,
|
||||||
util: require("./util")
|
util: require("./util")
|
||||||
|
@ -22,7 +22,6 @@ var redUtil = require("../util");
|
|||||||
var Log = require("../log");
|
var Log = require("../log");
|
||||||
|
|
||||||
var flows = require("./flows");
|
var flows = require("./flows");
|
||||||
var comms = require("../comms");
|
|
||||||
|
|
||||||
function Node(n) {
|
function Node(n) {
|
||||||
this.id = n.id;
|
this.id = n.id;
|
||||||
@ -250,7 +249,6 @@ Node.prototype.metric = function(eventname, msg, metricValue) {
|
|||||||
* status: { fill:"red|green", shape:"dot|ring", text:"blah" }
|
* status: { fill:"red|green", shape:"dot|ring", text:"blah" }
|
||||||
*/
|
*/
|
||||||
Node.prototype.status = function(status) {
|
Node.prototype.status = function(status) {
|
||||||
comms.publish("status/" + this.id, status, true);
|
|
||||||
flows.handleStatus(this,status);
|
flows.handleStatus(this,status);
|
||||||
};
|
};
|
||||||
module.exports = Node;
|
module.exports = Node;
|
||||||
|
@ -182,6 +182,10 @@ function delegateStatus(node,statusMessage) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
function handleStatus(node,statusMessage) {
|
function handleStatus(node,statusMessage) {
|
||||||
|
events.emit("node-status",{
|
||||||
|
id: node.id,
|
||||||
|
status:statusMessage
|
||||||
|
});
|
||||||
if (node.z) {
|
if (node.z) {
|
||||||
delegateStatus(node,statusMessage);
|
delegateStatus(node,statusMessage);
|
||||||
} else {
|
} else {
|
||||||
|
@ -23,21 +23,26 @@ var express = require('express');
|
|||||||
var app = express();
|
var app = express();
|
||||||
var WebSocket = require('ws');
|
var WebSocket = require('ws');
|
||||||
|
|
||||||
var comms = require("../../../red/runtime/comms");
|
var comms = require("../../../red/api/comms");
|
||||||
var Users = require("../../../red/api/auth/users");
|
var Users = require("../../../red/api/auth/users");
|
||||||
var Tokens = require("../../../red/api/auth/tokens");
|
var Tokens = require("../../../red/api/auth/tokens");
|
||||||
|
|
||||||
var address = '127.0.0.1';
|
var address = '127.0.0.1';
|
||||||
var listenPort = 0; // use ephemeral port
|
var listenPort = 0; // use ephemeral port
|
||||||
|
|
||||||
describe("runtime/comms", function() {
|
describe("api/comms", function() {
|
||||||
describe("with default keepalive", function() {
|
describe("with default keepalive", function() {
|
||||||
var server;
|
var server;
|
||||||
var url;
|
var url;
|
||||||
var port;
|
var port;
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
|
sinon.stub(Users,"default",function() { return when.resolve(null);});
|
||||||
server = http.createServer(function(req,res){app(req,res)});
|
server = http.createServer(function(req,res){app(req,res)});
|
||||||
comms.init(server, {});
|
comms.init(server, {
|
||||||
|
settings:{},
|
||||||
|
log:{warn:function(){},_:function(){},trace:function(){},audit:function(){}},
|
||||||
|
events:{on:function(){},removeListener:function(){}}
|
||||||
|
});
|
||||||
server.listen(listenPort, address);
|
server.listen(listenPort, address);
|
||||||
server.on('listening', function() {
|
server.on('listening', function() {
|
||||||
port = server.address().port;
|
port = server.address().port;
|
||||||
@ -48,6 +53,7 @@ describe("runtime/comms", function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
after(function() {
|
after(function() {
|
||||||
|
Users.default.restore();
|
||||||
comms.stop();
|
comms.stop();
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -119,8 +125,7 @@ describe("runtime/comms", function() {
|
|||||||
// implementation. More test should be written to test topic
|
// implementation. More test should be written to test topic
|
||||||
// matching once this one is passing
|
// matching once this one is passing
|
||||||
|
|
||||||
if (0) {
|
it.skip('receives message on correct topic', function(done) {
|
||||||
it('receives message on correct topic', function(done) {
|
|
||||||
var ws = new WebSocket(url);
|
var ws = new WebSocket(url);
|
||||||
ws.on('open', function() {
|
ws.on('open', function() {
|
||||||
ws.send('{"subscribe":"topic4"}');
|
ws.send('{"subscribe":"topic4"}');
|
||||||
@ -133,15 +138,21 @@ describe("runtime/comms", function() {
|
|||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
it.skip('listens for node/status events');
|
||||||
});
|
});
|
||||||
describe("disabled editor", function() {
|
describe("disabled editor", function() {
|
||||||
var server;
|
var server;
|
||||||
var url;
|
var url;
|
||||||
var port;
|
var port;
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
|
sinon.stub(Users,"default",function() { return when.resolve(null);});
|
||||||
server = http.createServer(function(req,res){app(req,res)});
|
server = http.createServer(function(req,res){app(req,res)});
|
||||||
comms.init(server, {disableEditor:true});
|
comms.init(server, {
|
||||||
|
settings:{disableEditor:true},
|
||||||
|
log:{warn:function(){},_:function(){},trace:function(){},audit:function(){}},
|
||||||
|
events:{on:function(){},removeListener:function(){}}
|
||||||
|
});
|
||||||
server.listen(listenPort, address);
|
server.listen(listenPort, address);
|
||||||
server.on('listening', function() {
|
server.on('listening', function() {
|
||||||
port = server.address().port;
|
port = server.address().port;
|
||||||
@ -152,6 +163,7 @@ describe("runtime/comms", function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
after(function() {
|
after(function() {
|
||||||
|
Users.default.restore();
|
||||||
comms.stop();
|
comms.stop();
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -173,8 +185,13 @@ describe("runtime/comms", function() {
|
|||||||
var url;
|
var url;
|
||||||
var port;
|
var port;
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
|
sinon.stub(Users,"default",function() { return when.resolve(null);});
|
||||||
server = http.createServer(function(req,res){app(req,res)});
|
server = http.createServer(function(req,res){app(req,res)});
|
||||||
comms.init(server, {httpAdminRoot:"/adminPath"});
|
comms.init(server, {
|
||||||
|
settings:{httpAdminRoot:"/adminPath"},
|
||||||
|
log:{warn:function(){},_:function(){},trace:function(){},audit:function(){}},
|
||||||
|
events:{on:function(){},removeListener:function(){}}
|
||||||
|
});
|
||||||
server.listen(listenPort, address);
|
server.listen(listenPort, address);
|
||||||
server.on('listening', function() {
|
server.on('listening', function() {
|
||||||
port = server.address().port;
|
port = server.address().port;
|
||||||
@ -185,6 +202,7 @@ describe("runtime/comms", function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
after(function() {
|
after(function() {
|
||||||
|
Users.default.restore();
|
||||||
comms.stop();
|
comms.stop();
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -206,8 +224,13 @@ describe("runtime/comms", function() {
|
|||||||
var url;
|
var url;
|
||||||
var port;
|
var port;
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
|
sinon.stub(Users,"default",function() { return when.resolve(null);});
|
||||||
server = http.createServer(function(req,res){app(req,res)});
|
server = http.createServer(function(req,res){app(req,res)});
|
||||||
comms.init(server, {httpAdminRoot:"/adminPath/"});
|
comms.init(server,{
|
||||||
|
settings:{httpAdminRoot:"/adminPath"},
|
||||||
|
log:{warn:function(){},_:function(){},trace:function(){},audit:function(){}},
|
||||||
|
events:{on:function(){},removeListener:function(){}}
|
||||||
|
});
|
||||||
server.listen(listenPort, address);
|
server.listen(listenPort, address);
|
||||||
server.on('listening', function() {
|
server.on('listening', function() {
|
||||||
port = server.address().port;
|
port = server.address().port;
|
||||||
@ -218,6 +241,7 @@ describe("runtime/comms", function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
after(function() {
|
after(function() {
|
||||||
|
Users.default.restore();
|
||||||
comms.stop();
|
comms.stop();
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -239,8 +263,13 @@ describe("runtime/comms", function() {
|
|||||||
var url;
|
var url;
|
||||||
var port;
|
var port;
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
|
sinon.stub(Users,"default",function() { return when.resolve(null);});
|
||||||
server = http.createServer(function(req,res){app(req,res)});
|
server = http.createServer(function(req,res){app(req,res)});
|
||||||
comms.init(server, {httpAdminRoot:"adminPath"});
|
comms.init(server, {
|
||||||
|
settings:{httpAdminRoot:"adminPath"},
|
||||||
|
log:{warn:function(){},_:function(){},trace:function(){},audit:function(){}},
|
||||||
|
events:{on:function(){},removeListener:function(){}}
|
||||||
|
});
|
||||||
server.listen(listenPort, address);
|
server.listen(listenPort, address);
|
||||||
server.on('listening', function() {
|
server.on('listening', function() {
|
||||||
port = server.address().port;
|
port = server.address().port;
|
||||||
@ -251,6 +280,7 @@ describe("runtime/comms", function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
after(function() {
|
after(function() {
|
||||||
|
Users.default.restore();
|
||||||
comms.stop();
|
comms.stop();
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -272,8 +302,13 @@ describe("runtime/comms", function() {
|
|||||||
var url;
|
var url;
|
||||||
var port;
|
var port;
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
|
sinon.stub(Users,"default",function() { return when.resolve(null);});
|
||||||
server = http.createServer(function(req,res){app(req,res)});
|
server = http.createServer(function(req,res){app(req,res)});
|
||||||
comms.init(server, {webSocketKeepAliveTime: 100});
|
comms.init(server, {
|
||||||
|
settings:{webSocketKeepAliveTime: 100},
|
||||||
|
log:{warn:function(){},_:function(){},trace:function(){},audit:function(){}},
|
||||||
|
events:{on:function(){},removeListener:function(){}}
|
||||||
|
});
|
||||||
server.listen(listenPort, address);
|
server.listen(listenPort, address);
|
||||||
server.on('listening', function() {
|
server.on('listening', function() {
|
||||||
port = server.address().port;
|
port = server.address().port;
|
||||||
@ -283,6 +318,7 @@ describe("runtime/comms", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
after(function() {
|
after(function() {
|
||||||
|
Users.default.restore();
|
||||||
comms.stop();
|
comms.stop();
|
||||||
});
|
});
|
||||||
it('are sent', function(done) {
|
it('are sent', function(done) {
|
||||||
@ -354,7 +390,11 @@ describe("runtime/comms", function() {
|
|||||||
|
|
||||||
|
|
||||||
server = http.createServer(function(req,res){app(req,res)});
|
server = http.createServer(function(req,res){app(req,res)});
|
||||||
comms.init(server, {adminAuth:{}});
|
comms.init(server,{
|
||||||
|
settings:{adminAuth:{}},
|
||||||
|
log:{warn:function(){},_:function(){},trace:function(){},audit:function(){}},
|
||||||
|
events:{on:function(){},removeListener:function(){}}
|
||||||
|
});
|
||||||
server.listen(listenPort, address);
|
server.listen(listenPort, address);
|
||||||
server.on('listening', function() {
|
server.on('listening', function() {
|
||||||
port = server.address().port;
|
port = server.address().port;
|
||||||
@ -440,7 +480,11 @@ describe("runtime/comms", function() {
|
|||||||
before(function(done) {
|
before(function(done) {
|
||||||
getDefaultUser = sinon.stub(Users,"default",function() { return when.resolve({permissions:"read"});});
|
getDefaultUser = sinon.stub(Users,"default",function() { return when.resolve({permissions:"read"});});
|
||||||
server = http.createServer(function(req,res){app(req,res)});
|
server = http.createServer(function(req,res){app(req,res)});
|
||||||
comms.init(server, {adminAuth:{}});
|
comms.init(server, {
|
||||||
|
settings:{adminAuth:{}},
|
||||||
|
log:{warn:function(){},_:function(){},trace:function(){},audit:function(){}},
|
||||||
|
events:{on:function(){},removeListener:function(){}}
|
||||||
|
});
|
||||||
server.listen(listenPort, address);
|
server.listen(listenPort, address);
|
||||||
server.on('listening', function() {
|
server.on('listening', function() {
|
||||||
port = server.address().port;
|
port = server.address().port;
|
@ -28,7 +28,7 @@ describe("api index", function() {
|
|||||||
|
|
||||||
describe("disables editor", function() {
|
describe("disables editor", function() {
|
||||||
before(function() {
|
before(function() {
|
||||||
api.init({
|
api.init({},{
|
||||||
settings:{httpNodeRoot:true, httpAdminRoot: true,disableEditor:true},
|
settings:{httpNodeRoot:true, httpAdminRoot: true,disableEditor:true},
|
||||||
events: {on:function(){},removeListener: function(){}}
|
events: {on:function(){},removeListener: function(){}}
|
||||||
});
|
});
|
||||||
@ -67,9 +67,10 @@ describe("api index", function() {
|
|||||||
})
|
})
|
||||||
});
|
});
|
||||||
before(function() {
|
before(function() {
|
||||||
api.init({
|
api.init({},{
|
||||||
settings:{httpNodeRoot:true, httpAdminRoot: true, adminAuth:{type: "credentials",users:[],default:{permissions:"read"}}},
|
settings:{httpNodeRoot:true, httpAdminRoot: true, adminAuth:{type: "credentials",users:[],default:{permissions:"read"}}},
|
||||||
storage:{getSessions:function(){return when.resolve({})}}
|
storage:{getSessions:function(){return when.resolve({})}},
|
||||||
|
events:{on:function(){},removeListener:function(){}}
|
||||||
});
|
});
|
||||||
app = api.adminApp();
|
app = api.adminApp();
|
||||||
});
|
});
|
||||||
@ -103,7 +104,7 @@ describe("api index", function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
before(function() {
|
before(function() {
|
||||||
api.init({
|
api.init({},{
|
||||||
log:{audit:function(){}},
|
log:{audit:function(){}},
|
||||||
settings:{httpNodeRoot:true, httpAdminRoot: true,disableEditor:false},
|
settings:{httpNodeRoot:true, httpAdminRoot: true,disableEditor:false},
|
||||||
events:{on:function(){},removeListener:function(){}}
|
events:{on:function(){},removeListener:function(){}}
|
||||||
|
@ -21,19 +21,12 @@ var path = require("path");
|
|||||||
var api = require("../../../red/api");
|
var api = require("../../../red/api");
|
||||||
var runtime = require("../../../red/runtime");
|
var runtime = require("../../../red/runtime");
|
||||||
|
|
||||||
var comms = require("../../../red/runtime/comms");
|
|
||||||
var redNodes = require("../../../red/runtime/nodes");
|
var redNodes = require("../../../red/runtime/nodes");
|
||||||
var storage = require("../../../red/runtime/storage");
|
var storage = require("../../../red/runtime/storage");
|
||||||
var settings = require("../../../red/runtime/settings");
|
var settings = require("../../../red/runtime/settings");
|
||||||
var log = require("../../../red/runtime/log");
|
var log = require("../../../red/runtime/log");
|
||||||
|
|
||||||
describe("runtime", function() {
|
describe("runtime", function() {
|
||||||
var commsMessages = [];
|
|
||||||
var commsPublish;
|
|
||||||
|
|
||||||
beforeEach(function() {
|
|
||||||
commsMessages = [];
|
|
||||||
});
|
|
||||||
afterEach(function() {
|
afterEach(function() {
|
||||||
if (console.log.restore) {
|
if (console.log.restore) {
|
||||||
console.log.restore();
|
console.log.restore();
|
||||||
@ -41,13 +34,9 @@ describe("runtime", function() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
before(function() {
|
before(function() {
|
||||||
commsPublish = sinon.stub(comms,"publish", function(topic,msg,retained) {
|
|
||||||
commsMessages.push({topic:topic,msg:msg,retained:retained});
|
|
||||||
});
|
|
||||||
process.env.NODE_RED_HOME = path.resolve(path.join(__dirname,"..","..",".."))
|
process.env.NODE_RED_HOME = path.resolve(path.join(__dirname,"..","..",".."))
|
||||||
});
|
});
|
||||||
after(function() {
|
after(function() {
|
||||||
commsPublish.restore();
|
|
||||||
delete process.env.NODE_RED_HOME;
|
delete process.env.NODE_RED_HOME;
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -55,32 +44,26 @@ describe("runtime", function() {
|
|||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
sinon.stub(log,"init",function() {});
|
sinon.stub(log,"init",function() {});
|
||||||
sinon.stub(settings,"init",function() {});
|
sinon.stub(settings,"init",function() {});
|
||||||
sinon.stub(comms,"init",function() {});
|
|
||||||
});
|
});
|
||||||
afterEach(function() {
|
afterEach(function() {
|
||||||
log.init.restore();
|
log.init.restore();
|
||||||
settings.init.restore();
|
settings.init.restore();
|
||||||
comms.init.restore();
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it("initialises components", function() {
|
it("initialises components", function() {
|
||||||
var dummyServer = {};
|
runtime.init({testSettings: true, httpAdminRoot:"/"});
|
||||||
runtime.init(dummyServer,{testSettings: true, httpAdminRoot:"/"});
|
|
||||||
log.init.called.should.be.true;
|
log.init.called.should.be.true;
|
||||||
settings.init.called.should.be.true;
|
settings.init.called.should.be.true;
|
||||||
comms.init.called.should.be.true;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("returns version", function() {
|
it("returns version", function() {
|
||||||
var dummyServer = {};
|
runtime.init({testSettings: true, httpAdminRoot:"/"});
|
||||||
runtime.init(dummyServer,{testSettings: true, httpAdminRoot:"/"});
|
|
||||||
/^\d+\.\d+\.\d+(-git)?$/.test(runtime.version()).should.be.true;
|
/^\d+\.\d+\.\d+(-git)?$/.test(runtime.version()).should.be.true;
|
||||||
|
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("start",function() {
|
describe("start",function() {
|
||||||
var commsInit;
|
|
||||||
var storageInit;
|
var storageInit;
|
||||||
var settingsLoad;
|
var settingsLoad;
|
||||||
var logMetric;
|
var logMetric;
|
||||||
@ -93,10 +76,8 @@ describe("runtime", function() {
|
|||||||
var redNodesGetNodeList;
|
var redNodesGetNodeList;
|
||||||
var redNodesLoadFlows;
|
var redNodesLoadFlows;
|
||||||
var redNodesStartFlows;
|
var redNodesStartFlows;
|
||||||
var commsStart;
|
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
commsInit = sinon.stub(comms,"init",function() {});
|
|
||||||
storageInit = sinon.stub(storage,"init",function(settings) {return when.resolve();});
|
storageInit = sinon.stub(storage,"init",function(settings) {return when.resolve();});
|
||||||
logMetric = sinon.stub(log,"metric",function() { return false; });
|
logMetric = sinon.stub(log,"metric",function() { return false; });
|
||||||
logWarn = sinon.stub(log,"warn",function() { });
|
logWarn = sinon.stub(log,"warn",function() { });
|
||||||
@ -107,10 +88,8 @@ describe("runtime", function() {
|
|||||||
redNodesCleanModuleList = sinon.stub(redNodes,"cleanModuleList",function(){});
|
redNodesCleanModuleList = sinon.stub(redNodes,"cleanModuleList",function(){});
|
||||||
redNodesLoadFlows = sinon.stub(redNodes,"loadFlows",function() {return when.resolve()});
|
redNodesLoadFlows = sinon.stub(redNodes,"loadFlows",function() {return when.resolve()});
|
||||||
redNodesStartFlows = sinon.stub(redNodes,"startFlows",function() {});
|
redNodesStartFlows = sinon.stub(redNodes,"startFlows",function() {});
|
||||||
commsStart = sinon.stub(comms,"start",function(){});
|
|
||||||
});
|
});
|
||||||
afterEach(function() {
|
afterEach(function() {
|
||||||
commsInit.restore();
|
|
||||||
storageInit.restore();
|
storageInit.restore();
|
||||||
logMetric.restore();
|
logMetric.restore();
|
||||||
logWarn.restore();
|
logWarn.restore();
|
||||||
@ -122,7 +101,6 @@ describe("runtime", function() {
|
|||||||
redNodesCleanModuleList.restore();
|
redNodesCleanModuleList.restore();
|
||||||
redNodesLoadFlows.restore();
|
redNodesLoadFlows.restore();
|
||||||
redNodesStartFlows.restore();
|
redNodesStartFlows.restore();
|
||||||
commsStart.restore();
|
|
||||||
});
|
});
|
||||||
it("reports errored/missing modules",function(done) {
|
it("reports errored/missing modules",function(done) {
|
||||||
redNodesGetNodeList = sinon.stub(redNodes,"getNodeList", function(cb) {
|
redNodesGetNodeList = sinon.stub(redNodes,"getNodeList", function(cb) {
|
||||||
@ -131,7 +109,7 @@ describe("runtime", function() {
|
|||||||
{ module:"module",enabled:true,loaded:false,types:["typeA","typeB"]} // missing
|
{ module:"module",enabled:true,loaded:false,types:["typeA","typeB"]} // missing
|
||||||
].filter(cb);
|
].filter(cb);
|
||||||
});
|
});
|
||||||
runtime.init({},{testSettings: true, httpAdminRoot:"/", load:function() { return when.resolve();}});
|
runtime.init({testSettings: true, httpAdminRoot:"/", load:function() { return when.resolve();}});
|
||||||
sinon.stub(console,"log");
|
sinon.stub(console,"log");
|
||||||
runtime.start().then(function() {
|
runtime.start().then(function() {
|
||||||
console.log.restore();
|
console.log.restore();
|
||||||
@ -139,7 +117,6 @@ describe("runtime", function() {
|
|||||||
storageInit.calledOnce.should.be.true;
|
storageInit.calledOnce.should.be.true;
|
||||||
redNodesInit.calledOnce.should.be.true;
|
redNodesInit.calledOnce.should.be.true;
|
||||||
redNodesLoad.calledOnce.should.be.true;
|
redNodesLoad.calledOnce.should.be.true;
|
||||||
commsStart.calledOnce.should.be.true;
|
|
||||||
redNodesLoadFlows.calledOnce.should.be.true;
|
redNodesLoadFlows.calledOnce.should.be.true;
|
||||||
|
|
||||||
logWarn.calledWithMatch("Failed to register 1 node type");
|
logWarn.calledWithMatch("Failed to register 1 node type");
|
||||||
@ -162,7 +139,7 @@ describe("runtime", function() {
|
|||||||
].filter(cb);
|
].filter(cb);
|
||||||
});
|
});
|
||||||
var serverInstallModule = sinon.stub(redNodes,"installModule",function(name) { return when.resolve();});
|
var serverInstallModule = sinon.stub(redNodes,"installModule",function(name) { return when.resolve();});
|
||||||
runtime.init({},{testSettings: true, autoInstallModules:true, httpAdminRoot:"/", load:function() { return when.resolve();}});
|
runtime.init({testSettings: true, autoInstallModules:true, httpAdminRoot:"/", load:function() { return when.resolve();}});
|
||||||
sinon.stub(console,"log");
|
sinon.stub(console,"log");
|
||||||
runtime.start().then(function() {
|
runtime.start().then(function() {
|
||||||
console.log.restore();
|
console.log.restore();
|
||||||
@ -188,7 +165,7 @@ describe("runtime", function() {
|
|||||||
{ err:"errored",name:"errName" } // error
|
{ err:"errored",name:"errName" } // error
|
||||||
].filter(cb);
|
].filter(cb);
|
||||||
});
|
});
|
||||||
runtime.init({},{testSettings: true, verbose:true, httpAdminRoot:"/", load:function() { return when.resolve();}});
|
runtime.init({testSettings: true, verbose:true, httpAdminRoot:"/", load:function() { return when.resolve();}});
|
||||||
sinon.stub(console,"log");
|
sinon.stub(console,"log");
|
||||||
runtime.start().then(function() {
|
runtime.start().then(function() {
|
||||||
console.log.restore();
|
console.log.restore();
|
||||||
@ -203,12 +180,11 @@ describe("runtime", function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("reports runtime metrics",function(done) {
|
it("reports runtime metrics",function(done) {
|
||||||
var commsStop = sinon.stub(comms,"stop",function() {} );
|
|
||||||
var stopFlows = sinon.stub(redNodes,"stopFlows",function() {} );
|
var stopFlows = sinon.stub(redNodes,"stopFlows",function() {} );
|
||||||
redNodesGetNodeList = sinon.stub(redNodes,"getNodeList", function() {return []});
|
redNodesGetNodeList = sinon.stub(redNodes,"getNodeList", function() {return []});
|
||||||
logMetric.restore();
|
logMetric.restore();
|
||||||
logMetric = sinon.stub(log,"metric",function() { return true; });
|
logMetric = sinon.stub(log,"metric",function() { return true; });
|
||||||
runtime.init({},{testSettings: true, runtimeMetricInterval:200, httpAdminRoot:"/", load:function() { return when.resolve();}});
|
runtime.init({testSettings: true, runtimeMetricInterval:200, httpAdminRoot:"/", load:function() { return when.resolve();}});
|
||||||
sinon.stub(console,"log");
|
sinon.stub(console,"log");
|
||||||
runtime.start().then(function() {
|
runtime.start().then(function() {
|
||||||
console.log.restore();
|
console.log.restore();
|
||||||
@ -226,7 +202,6 @@ describe("runtime", function() {
|
|||||||
done(err);
|
done(err);
|
||||||
} finally {
|
} finally {
|
||||||
runtime.stop();
|
runtime.stop();
|
||||||
commsStop.restore();
|
|
||||||
stopFlows.restore();
|
stopFlows.restore();
|
||||||
}
|
}
|
||||||
},300);
|
},300);
|
||||||
@ -237,15 +212,9 @@ describe("runtime", function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("stops components", function() {
|
it("stops components", function() {
|
||||||
var commsStop = sinon.stub(comms,"stop",function() {} );
|
|
||||||
var stopFlows = sinon.stub(redNodes,"stopFlows",function() {} );
|
var stopFlows = sinon.stub(redNodes,"stopFlows",function() {} );
|
||||||
|
|
||||||
runtime.stop();
|
runtime.stop();
|
||||||
|
|
||||||
commsStop.called.should.be.true;
|
|
||||||
stopFlows.called.should.be.true;
|
stopFlows.called.should.be.true;
|
||||||
|
|
||||||
commsStop.restore();
|
|
||||||
stopFlows.restore();
|
stopFlows.restore();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -19,7 +19,6 @@ var sinon = require('sinon');
|
|||||||
var RedNode = require("../../../../red/runtime/nodes/Node");
|
var RedNode = require("../../../../red/runtime/nodes/Node");
|
||||||
var Log = require("../../../../red/runtime/log");
|
var Log = require("../../../../red/runtime/log");
|
||||||
var flows = require("../../../../red/runtime/nodes/flows");
|
var flows = require("../../../../red/runtime/nodes/flows");
|
||||||
var comms = require("../../../../red/runtime/comms");
|
|
||||||
|
|
||||||
describe('Node', function() {
|
describe('Node', function() {
|
||||||
describe('#constructor',function() {
|
describe('#constructor',function() {
|
||||||
@ -512,9 +511,6 @@ describe('Node', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('#status', function() {
|
describe('#status', function() {
|
||||||
after(function() {
|
|
||||||
comms.publish.restore();
|
|
||||||
});
|
|
||||||
it('publishes status', function(done) {
|
it('publishes status', function(done) {
|
||||||
sinon.stub(flows,"handleStatus", function(node,message,msg) {});
|
sinon.stub(flows,"handleStatus", function(node,message,msg) {});
|
||||||
var n = new RedNode({id:'123',type:'abc'});
|
var n = new RedNode({id:'123',type:'abc'});
|
||||||
@ -522,18 +518,9 @@ describe('Node', function() {
|
|||||||
var topic;
|
var topic;
|
||||||
var message;
|
var message;
|
||||||
var retain;
|
var retain;
|
||||||
sinon.stub(comms, 'publish', function(_topic, _message, _retain) {
|
|
||||||
topic = _topic;
|
|
||||||
message = _message;
|
|
||||||
retain = _retain;
|
|
||||||
});
|
|
||||||
|
|
||||||
n.status(status);
|
n.status(status);
|
||||||
|
|
||||||
topic.should.equal('status/123');
|
|
||||||
message.should.equal(status);
|
|
||||||
retain.should.be.true;
|
|
||||||
|
|
||||||
flows.handleStatus.called.should.be.true;
|
flows.handleStatus.called.should.be.true;
|
||||||
flows.handleStatus.args[0][0].should.eql(n);
|
flows.handleStatus.args[0][0].should.eql(n);
|
||||||
flows.handleStatus.args[0][1].should.eql(status);
|
flows.handleStatus.args[0][1].should.eql(status);
|
||||||
|
Loading…
Reference in New Issue
Block a user