Got to start somewhere

This commit is contained in:
Nicholas O'Leary
2013-09-05 15:02:48 +01:00
commit 32796dd74c
155 changed files with 21836 additions and 0 deletions

249
red/library.js Normal file
View File

@@ -0,0 +1,249 @@
/**
* Copyright 2013 IBM Corp.
*
* 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 redUI = require("./server");
// -------- Flow Library --------
redUI.app.post(new RegExp("/library/flows\/(.*)"), function(req,res) {
var fullBody = '';
req.on('data', function(chunk) {
fullBody += chunk.toString();
});
req.on('end', function() {
var fn = "lib/flows/"+req.params[0]+".json";
var parts = fn.split("/");
for (var i = 3;i<parts.length;i+=1) {
var dirname = parts.slice(0,i).join("/");
if (!fs.existsSync(dirname)) {
fs.mkdirSync(dirname);
}
}
fs.writeFile(fn,fullBody,function(err) {
res.writeHead(204, {'Content-Type': 'text/plain'});
res.end();
});
});
});
function listFiles(dir) {
var dirs = {};
var files = [];
var dirCount = 0;
fs.readdirSync(dir).sort().filter(function(fn) {
var stats = fs.lstatSync(dir+"/"+fn);
if (stats.isDirectory()) {
dirCount += 1;
dirs[fn] = listFiles(dir+"/"+fn);
} else {
files.push(fn.split(".")[0]);
}
});
var result = {};
if (dirCount > 0) { result.d = dirs; }
if (files.length > 0) { result.f = files; }
return result;
}
redUI.app.get("/library/flows",function(req,res) {
var flows = {};
if (fs.existsSync("lib/flows")) {
flows = listFiles("lib/flows");
} else {
fs.mkdirSync("lib/flows");
}
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write(JSON.stringify(flows));
res.end();
});
redUI.app.get(new RegExp("/library/flows\/(.*)"), function(req,res) {
var fn = "lib/flows/"+req.params[0]+".json";
if (fs.existsSync(fn)) {
fs.readFile(fn,function(err,data) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write(data);
res.end();
});
} else {
res.send(404);
}
});
// ------------------------------
function createLibrary(type) {
//TODO: use settings.library as the base root
var root = fspath.join("lib",type)+"/";
fs.exists(root,function(exists) {
if (!exists) {
fs.mkdir(root);
}
});
redUI.app.get(new RegExp("/library/"+type+"($|\/(.*))"),function(req,res) {
var path = req.params[1]||"";
var rootPath = fspath.join(root,path);
fs.exists(rootPath,function(exists) {
fs.lstat(rootPath,function(err,stats) {
if (stats.isFile()) {
getFileBody(root,path,res);
} else {
if (path.substr(-1) == '/') {
path = path.substr(0,path.length-1);
}
fs.readdir(rootPath,function(err,fns) {
var dirs = [];
var files = [];
fns.sort().filter(function(fn) {
var fullPath = fspath.join(path,fn);
var absoluteFullPath = fspath.join(root,fullPath);
if (fn[0] != ".") {
var stats = fs.lstatSync(absoluteFullPath);
if (stats.isDirectory()) {
dirs.push(fn);
} else {
var meta = getFileMeta(root,fullPath);
meta.fn = fn;
files.push(meta);
}
}
});
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write(JSON.stringify(dirs.concat(files)));
res.end();
});
}
});
});
});
redUI.app.post(new RegExp("/library/"+type+"\/(.*)"),function(req,res) {
var path = req.params[0];
var fullBody = '';
req.on('data', function(chunk) {
fullBody += chunk.toString();
});
req.on('end', function() {
writeFile(root,path,req.query,fullBody,res);
});
});
}
function writeFile(root,path,meta,body,res) {
var fn = fspath.join(root,path);
var parts = path.split("/");
var dirname = root;
for (var i = 0;i<parts.length-1;i+=1) {
dirname = fspath.join(dirname,parts[i]);
if (!fs.existsSync(dirname)) {
fs.mkdirSync(dirname);
}
}
var headers = "";
for (var i in meta) {
headers += "// "+i+": "+meta[i]+"\n";
}
fs.writeFile(fn,headers+body,function(err) {
//TODO: handle error
res.writeHead(204, {'Content-Type': 'text/plain'});
res.end();
});
}
function getFileMeta(root,path) {
var fn = fspath.join(root,path);
var fd = fs.openSync(fn,"r");
var size = fs.fstatSync(fd).size;
var meta = {};
var scanning = true;
var read = 0;
var length = 10;
var offset = 0;
var remaining = "";
var buffer = Buffer(length);
while(read < size) {
read+=fs.readSync(fd,buffer,0,length);
var data = remaining+buffer.toString();
var parts = data.split("\n");
remaining = parts.splice(-1);
for (var i=0;i<parts.length;i+=1) {
var match = /^\/\/ (\w+): (.*)/.exec(parts[i]);
if (match) {
meta[match[1]] = match[2];
} else {
read = size;
break;
}
}
}
fs.closeSync(fd);
return meta;
}
function getFileBody(root,path,res) {
var fn = fspath.join(root,path);
res.writeHead(200, {'Content-Type': 'text/plain'});
var fd = fs.openSync(fn,"r");
var size = fs.fstatSync(fd).size;
var scanning = true;
var read = 0;
var length = 10;
var offset = 0;
var remaining = "";
var buffer = Buffer(length);
while(read < size) {
var thisRead = fs.readSync(fd,buffer,0,length);
read += thisRead;
if (scanning) {
var data = remaining+buffer.toString();
var parts = data.split("\n");
remaining = parts.splice(-1)[0];
for (var i=0;i<parts.length;i+=1) {
if (! /^\/\/ \w+: /.test(parts[i])) {
scanning = false;
res.write(parts[i]+"\n");
}
}
if (! /^\/\/ \w+: /.test(remaining)) {
scanning = false;
}
if (!scanning) {
res.write(remaining);
}
} else {
res.write(buffer.slice(0,thisRead));
}
}
fs.closeSync(fd);
res.end();
}
module.exports.register = createLibrary;

290
red/nodes.js Normal file
View File

@@ -0,0 +1,290 @@
/**
* Copyright 2013 IBM Corp.
*
* 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 util = require("util");
var events = require("events");
var fs = require("fs");
function getCallerFilename(type) {
//if (type == "summary") {
// var err = new Error();
// console.log(err.stack);
// return null;
//}
// Save original Error.prepareStackTrace
var origPrepareStackTrace = Error.prepareStackTrace;
// Override with function that just returns `stack`
Error.prepareStackTrace = function (_, stack) {
return stack;
}
// Create a new `Error`, which automatically gets `stack`
var err = new Error();
// Evaluate `err.stack`, which calls our new `Error.prepareStackTrace`
var stack = err.stack;
// Restore original `Error.prepareStackTrace`
Error.prepareStackTrace = origPrepareStackTrace;
// Remove superfluous function call on stack
stack.shift();
stack.shift();
return stack[0].getFileName();
}
var registry = (function() {
var nodes = {};
var logHandlers = [];
var obj = {
add: function(n) {
nodes[n.id] = n;
n.on("log",function(msg) {
for (var i in logHandlers) {
logHandlers[i].emit("log",msg);
}
});
},
get: function(i) {
return nodes[i];
},
clear: function() {
for (var n in nodes) {
nodes[n].close();
}
nodes = {};
},
addLogHandler: function(handler) {
logHandlers.push(handler);
}
}
return obj;
})();
var ConsoleLogHandler = new events.EventEmitter();
ConsoleLogHandler.on("log",function(msg) {
util.log("["+msg.level+"] ["+msg.type+":"+(msg.name||msg.id)+"] "+msg.msg);
});
registry.addLogHandler(ConsoleLogHandler);
var node_type_registry = (function() {
var node_types = {};
var node_configs = {};
var obj = {
register: function(type,node) {
util.inherits(node, Node);
var callerFilename = getCallerFilename(type);
if (callerFilename == null) {
util.log("["+type+"] unable to determine filename");
} else {
var configFilename = callerFilename.replace(/\.js$/,".html");
if (fs.existsSync(configFilename)) {
node_types[type] = node;
if (! node_configs[configFilename]) {
node_configs[configFilename] = fs.readFileSync(configFilename,'utf8');
}
} else {
util.log("["+type+"] missing template file: "+configFilename);
}
}
},
get: function(type) {
return node_types[type];
},
registerNodeConfig: function(type,config) {
node_configs[type] = config;
},
getNodeConfigs: function() {
var result = "";
for (var nt in node_configs) {
result += node_configs[nt];
}
return result;
}
}
return obj;
})();
function Node(n) {
this.id = n.id;
registry.add(this);
this.type = n.type;
if (n.name) {
this.name = n.name;
}
this.wires = n.wires||[];
}
util.inherits(Node,events.EventEmitter);
Node.prototype.close = function() {
// called when a node is removed
}
Node.prototype.send = function(msg) {
// instanceof doesn't work for some reason here
if (msg == null) {
msg = [];
} else if (!util.isArray(msg)) {
msg = [msg];
}
for (var i in this.wires) {
var wires = this.wires[i];
if (i < msg.length) {
for (var j in wires) {
if (msg[i] != null) {
var msgs = msg[i];
if (!util.isArray(msg[i])) {
msgs = [msg[i]];
}
for (var k in msgs) {
var mm = msgs[k];
var m = {};
for (var p in mm) {
if (mm.hasOwnProperty(p)) {
m[p] = mm[p];
}
}
var node = registry.get(wires[j]);
if (node) {
node.receive(m);
}
}
}
}
}
}
}
module.exports.Node = Node;
Node.prototype.receive = function(msg) {
this.emit("input",msg);
}
Node.prototype.log = function(msg) {
var o = {level:'log',id:this.id, type:this.type, msg:msg};
if (this.name) o.name = this.name;
this.emit("log",o);
}
Node.prototype.warn = function(msg) {
var o = {level:'warn',id:this.id, type:this.type, msg:msg};
if (this.name) o.name = this.name;
this.emit("log",o);
}
Node.prototype.error = function(msg) {
var o = {level:'error',id:this.id, type:this.type, msg:msg};
if (this.name) o.name = this.name;
this.emit("log",o);
}
var credentials = {};
var credentialsFile = "credentials.json";
if (fs.existsSync(credentialsFile)) {
credentials = JSON.parse(fs.readFileSync(credentialsFile,'utf8'));
}
function saveCredentialsFile() {
fs.writeFile(credentialsFile, JSON.stringify(credentials), function(err) {
if(err) {
util.log(err);
}
});
}
module.exports.addCredentials = function(id,creds) {
credentials[id] = creds;
saveCredentialsFile();
}
module.exports.getCredentials = function(id) {
return credentials[id];
}
module.exports.deleteCredentials = function(id) {
delete credentials[id];
saveCredentialsFile();
}
module.exports.createNode = function(node,def) {
Node.call(node,def);
}
module.exports.registerType = node_type_registry.register;
module.exports.getNodeConfigs = node_type_registry.getNodeConfigs;
module.exports.addLogHandler = registry.addLogHandler;
module.exports.load = function() {
function loadNodes(dir) {
fs.readdirSync(dir).sort().filter(function(fn){
var stats = fs.statSync(dir+"/"+fn);
if (stats.isFile()) {
if (/\.js$/.test(fn)) {
try {
require("../"+dir+"/"+fn);
} catch(err) {
util.log("["+fn+"] "+err);
//console.log(err.stack);
}
}
} else if (stats.isDirectory()) {
// Ignore /.dirs/ and /lib/
if (!/^(\..*|lib)$/.test(fn)) {
loadNodes(dir+"/"+fn);
}
}
});
}
loadNodes("nodes");
}
module.exports.getNode = function(nid) {
return registry.get(nid);
}
module.exports.parseConfig = function(conf) {
registry.clear();
for (var i in conf) {
var nn = null;
var nt = node_type_registry.get(conf[i].type);
if (nt) {
nn = new nt(conf[i]);
}
// console.log(nn);
if (nn == null) {
util.log("[red] unknown type: "+conf[i].type);
}
}
// Clean up any orphaned credentials
var deletedCredentials = false;
for (var c in credentials) {
var n = registry.get(c);
if (!n) {
deletedCredentials = true;
delete credentials[c];
}
}
if (deletedCredentials) {
saveCredentialsFile();
}
}

28
red/red.js Normal file
View File

@@ -0,0 +1,28 @@
/**
* Copyright 2013 IBM Corp.
*
* 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 server = require("./server");
var nodes = require("./nodes");
var library = require("./library");
var settings = require("../settings");
module.exports = {
nodes: nodes,
app: server.app,
server: server.server,
settings: settings,
library: library
}

99
red/server.js Normal file
View File

@@ -0,0 +1,99 @@
/**
* Copyright 2013 IBM Corp.
*
* 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 util = require('util');
var createUI = require("./ui");
var redNodes = require("./nodes");
var host = require('os').hostname();
var app = null;
var server = null;
function createServer(_server,settings) {
server = _server;
app = createUI(settings);
//TODO: relocated user dir
var rulesfile = process.argv[2] || 'flows_'+host+'.json';
app.get("/nodes",function(req,res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write(redNodes.getNodeConfigs());
res.end();
});
app.get("/flows",function(req,res) {
fs.exists(rulesfile, function (exists) {
if (exists) {
res.sendfile(rulesfile);
} else {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write("[]");
res.end();
}
});
});
app.post("/flows",function(req,res) {
var fullBody = '';
req.on('data', function(chunk) {
fullBody += chunk.toString();
});
req.on('end', function() {
res.writeHead(204, {'Content-Type': 'text/plain'});
res.end();
fs.writeFile(rulesfile, fullBody, function(err) {
if(err) {
util.log(err);
} else {
redNodes.parseConfig(JSON.parse(fullBody));
}
});
});
});
console.log("\nWelcome to Node-RED\n===================\n");
util.log("[red] Loading palette nodes");
util.log("------------------------------------------");
redNodes.load();
util.log("");
util.log('You may ignore any errors above here if they are for');
util.log('nodes you are not using. The nodes indicated will not');
util.log('be available in the main palette until any missing');
util.log('modules are installed, typically by running:');
util.log(' npm install {the module name}');
util.log('or any other errors are resolved');
util.log("------------------------------------------");
util.log("[red] Loading workspace flow : "+rulesfile);
fs.exists(rulesfile, function (exists) {
if (exists) {
fs.readFile(rulesfile,'utf8',function(err,data) {
redNodes.parseConfig(JSON.parse(data));
});
}
});
return app;
}
module.exports = {
init: createServer
}
module.exports.__defineGetter__("app", function() { return app });
module.exports.__defineGetter__("server", function() { return server });

34
red/ui.js Normal file
View File

@@ -0,0 +1,34 @@
/**
* Copyright 2013 IBM Corp.
*
* 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 express = require('express');
var util = require('util');
var crypto = require('crypto');
var fs = require("fs");
var app = express();
function setupUI(settings) {
app.get(/^$/,function(req,res) {
res.redirect("/");
});
app.use("/",express.static(__dirname + '/../public'));
return app;
}
module.exports = setupUI;