mirror of https://github.com/node-red/node-red.git
Merge branch 'master' of https://github.com/node-red/node-red
commit
c1420b2898
|
@ -202,7 +202,7 @@
|
|||
},
|
||||
oneditprepare: function() {
|
||||
var repeattype = "none";
|
||||
if (this.repeat != "") {
|
||||
if (this.repeat != "" && this.repeat != 0) {
|
||||
repeattype = "interval";
|
||||
$("#inject-time-interval-units option").filter(function() {return $(this).val() == "s";}).attr('selected',true);
|
||||
$("#inject-time-interval-count").val(this.repeat);
|
||||
|
@ -307,7 +307,7 @@
|
|||
var timerange = "";
|
||||
if (startTime == endTime) {
|
||||
//TODO: invalid
|
||||
repeat = 0;
|
||||
repeat = "";
|
||||
crontab = "";
|
||||
} else if (endTime == 0) {
|
||||
timerange = startTime+"-23";
|
||||
|
@ -330,13 +330,13 @@
|
|||
}
|
||||
timerange = startpart+","+endpart;
|
||||
}
|
||||
repeat = 0;
|
||||
repeat = "";
|
||||
crontab = "*/"+count+" "+timerange+" * * "+days;
|
||||
} else if (type == "time") {
|
||||
var time = $("#inject-time-time").val();
|
||||
var days = $("#inject-time-time-days option:selected").val();
|
||||
var parts = time.split(":");
|
||||
repeat = 0;
|
||||
repeat = "";
|
||||
crontab = parts[1]+" "+parts[0]+" * * "+days;
|
||||
}
|
||||
|
||||
|
|
|
@ -104,7 +104,7 @@ function HTTPRequest(n) {
|
|||
msg.statusCode = err.code;
|
||||
node.send(msg);
|
||||
});
|
||||
if (msg.payload && (method == "PUSH" || method == "PUT") ) {
|
||||
if (msg.payload && (method == "POST" || method == "PUT") ) {
|
||||
if (typeof msg.payload === "string" || Buffer.isBuffer(msg.payload)) {
|
||||
req.write(msg.payload);
|
||||
} else if (typeof msg.payload == "number") {
|
||||
|
|
|
@ -21,8 +21,10 @@
|
|||
],
|
||||
"dependencies": {
|
||||
"express": "3.x",
|
||||
"when": "~2.6.0",
|
||||
"mqtt": "~0.3.3",
|
||||
"ws": "~0.4.31",
|
||||
"fs-extra": "~0.8.1",
|
||||
"mustache": "~0.7.2",
|
||||
"cron":"1.x",
|
||||
"ntwitter":"0.5.0",
|
||||
|
|
237
red/library.js
237
red/library.js
|
@ -14,78 +14,49 @@
|
|||
* limitations under the License.
|
||||
**/
|
||||
|
||||
var fs = require("fs");
|
||||
var fspath = require("path");
|
||||
var redApp = null;
|
||||
var storage = null;
|
||||
|
||||
function init() {
|
||||
redApp = require("./server").app;
|
||||
storage = require("./storage").storage;
|
||||
|
||||
// -------- Flow Library --------
|
||||
redApp.post(new RegExp("/library/flows\/(.*)"), function(req,res) {
|
||||
var fullBody = '';
|
||||
req.on('data', function(chunk) {
|
||||
fullBody += chunk.toString();
|
||||
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();
|
||||
});
|
||||
|
||||
storage.saveFlow(req.params[0],fullBody).then(function() {
|
||||
res.writeHead(204, {'Content-Type': 'text/plain'});
|
||||
res.end();
|
||||
}).otherwise(function(err) {
|
||||
util.log("[red] Error loading flow '"+req.params[0]+"' : "+err);
|
||||
res.send(500);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
redApp.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();
|
||||
|
||||
storage.getAllFlows().then(function(flows) {
|
||||
res.writeHead(200, {'Content-Type': 'text/plain'});
|
||||
res.write(JSON.stringify(flows));
|
||||
res.end();
|
||||
});
|
||||
});
|
||||
|
||||
redApp.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 {
|
||||
storage.getFlow(req.params[0]).then(function(data) {
|
||||
res.writeHead(200, {'Content-Type': 'text/plain'});
|
||||
res.write(data);
|
||||
res.end();
|
||||
}).otherwise(function(err) {
|
||||
if (err) {
|
||||
util.log("[red] Error loading flow '"+req.params[0]+"' : "+err);
|
||||
}
|
||||
res.send(404);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// ------------------------------
|
||||
|
@ -93,53 +64,21 @@ function init() {
|
|||
|
||||
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);
|
||||
}
|
||||
});
|
||||
redApp.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();
|
||||
});
|
||||
}
|
||||
|
||||
});
|
||||
storage.getLibraryEntry(type,path).then(function(result) {
|
||||
res.writeHead(200, {'Content-Type': 'text/plain'});
|
||||
if (typeof result === "string") {
|
||||
res.write(result);
|
||||
} else {
|
||||
res.write(JSON.stringify(result));
|
||||
}
|
||||
res.end();
|
||||
}).otherwise(function(err) {
|
||||
if (err) {
|
||||
util.log("[red] Error loading library entry '"+path+"' : "+err);
|
||||
}
|
||||
res.send(404);
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -150,103 +89,15 @@ function createLibrary(type) {
|
|||
fullBody += chunk.toString();
|
||||
});
|
||||
req.on('end', function() {
|
||||
writeFile(root,path,req.query,fullBody,res);
|
||||
storage.saveLibraryEntry(type,path,req.query,fullBody).then(function() {
|
||||
res.send(204);
|
||||
}).otherwise(function(err) {
|
||||
util.log("[red] Error saving library entry '"+path+"' : "+err);
|
||||
res.send(500);
|
||||
});;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
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.init = init;
|
||||
module.exports.register = createLibrary;
|
||||
|
|
37
red/nodes.js
37
red/nodes.js
|
@ -17,6 +17,7 @@ var util = require("util");
|
|||
var EventEmitter = require("events").EventEmitter;
|
||||
var fs = require("fs");
|
||||
var events = require("./events");
|
||||
var storage = null;
|
||||
|
||||
function getCallerFilename(type) {
|
||||
//if (type == "summary") {
|
||||
|
@ -194,29 +195,17 @@ Node.prototype.error = function(msg) {
|
|||
}
|
||||
|
||||
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();
|
||||
storage.saveCredentials(credentials);
|
||||
}
|
||||
module.exports.getCredentials = function(id) {
|
||||
return credentials[id];
|
||||
}
|
||||
module.exports.deleteCredentials = function(id) {
|
||||
delete credentials[id];
|
||||
saveCredentialsFile();
|
||||
storage.saveCredentials(credentials);
|
||||
}
|
||||
module.exports.createNode = function(node,def) {
|
||||
Node.call(node,def);
|
||||
|
@ -253,7 +242,7 @@ module.exports.load = function() {
|
|||
//events.emit("nodes-loaded");
|
||||
}
|
||||
|
||||
var activeConfig = null;
|
||||
var activeConfig = [];
|
||||
var missingTypes = [];
|
||||
|
||||
events.on('type-registered',function(type) {
|
||||
|
@ -285,7 +274,21 @@ module.exports.stopFlows = stopFlows;
|
|||
module.exports.setConfig = function(conf) {
|
||||
stopFlows();
|
||||
activeConfig = conf;
|
||||
parseConfig();
|
||||
|
||||
if (!storage) {
|
||||
// Do this lazily to ensure the storage provider as been initialised
|
||||
storage = require("./storage").storage;
|
||||
}
|
||||
storage.getCredentials().then(function(creds) {
|
||||
credentials = creds;
|
||||
parseConfig();
|
||||
}).otherwise(function(err) {
|
||||
util.log("[red] Error loading credentials : "+err);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports.getConfig = function() {
|
||||
return activeConfig;
|
||||
}
|
||||
|
||||
var parseConfig = function() {
|
||||
|
@ -339,7 +342,7 @@ var parseConfig = function() {
|
|||
}
|
||||
}
|
||||
if (deletedCredentials) {
|
||||
saveCredentialsFile();
|
||||
storage.saveCredentials(credentials);
|
||||
}
|
||||
events.emit("nodes-started");
|
||||
}
|
||||
|
|
|
@ -14,31 +14,22 @@
|
|||
* limitations under the License.
|
||||
**/
|
||||
|
||||
var fs = require('fs');
|
||||
var util = require('util');
|
||||
var createUI = require("./ui");
|
||||
var redNodes = require("./nodes");
|
||||
|
||||
//TODO: relocated user dir
|
||||
|
||||
var flowfile = '';
|
||||
|
||||
var app = null;
|
||||
var server = null;
|
||||
|
||||
function createServer(_server,settings) {
|
||||
server = _server;
|
||||
|
||||
storage = require("./storage").init(settings);
|
||||
|
||||
app = createUI(settings);
|
||||
|
||||
flowfile = settings.flowFile || 'flows_'+require('os').hostname()+'.json';
|
||||
|
||||
//TODO: relocated user dir
|
||||
fs.exists("lib/",function(exists) {
|
||||
if (!exists) {
|
||||
fs.mkdir("lib");
|
||||
}
|
||||
});
|
||||
|
||||
app.get("/nodes",function(req,res) {
|
||||
res.writeHead(200, {'Content-Type': 'text/plain'});
|
||||
res.write(redNodes.getNodeConfigs());
|
||||
|
@ -46,15 +37,9 @@ function createServer(_server,settings) {
|
|||
});
|
||||
|
||||
app.get("/flows",function(req,res) {
|
||||
fs.exists(flowfile, function (exists) {
|
||||
if (exists) {
|
||||
res.sendfile(flowfile);
|
||||
} else {
|
||||
res.writeHead(200, {'Content-Type': 'text/plain'});
|
||||
res.write("[]");
|
||||
res.end();
|
||||
}
|
||||
});
|
||||
res.writeHead(200, {'Content-Type': 'text/plain'});
|
||||
res.write(JSON.stringify(redNodes.getConfig()));
|
||||
res.end();
|
||||
});
|
||||
|
||||
app.post("/flows",function(req,res) {
|
||||
|
@ -65,12 +50,11 @@ function createServer(_server,settings) {
|
|||
req.on('end', function() {
|
||||
res.writeHead(204, {'Content-Type': 'text/plain'});
|
||||
res.end();
|
||||
fs.writeFile(flowfile, fullBody, function(err) {
|
||||
if(err) {
|
||||
util.log(err);
|
||||
} else {
|
||||
redNodes.setConfig(JSON.parse(fullBody));
|
||||
}
|
||||
var flows = JSON.parse(fullBody);
|
||||
storage.saveFlows(flows).then(function() {
|
||||
redNodes.setConfig(flows);
|
||||
}).otherwise(function(err) {
|
||||
util.log("[red] Error saving flows : "+err);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -90,16 +74,12 @@ function start() {
|
|||
util.log('or any other errors are resolved');
|
||||
util.log("------------------------------------------");
|
||||
|
||||
|
||||
fs.exists(flowfile, function (exists) {
|
||||
if (exists) {
|
||||
util.log("[red] Loading flows : "+flowfile);
|
||||
fs.readFile(flowfile,'utf8',function(err,data) {
|
||||
redNodes.setConfig(JSON.parse(data));
|
||||
});
|
||||
} else {
|
||||
util.log("[red] Flows file not found : "+flowfile);
|
||||
storage.getFlows().then(function(flows) {
|
||||
if (flows.length > 0) {
|
||||
redNodes.setConfig(flows);
|
||||
}
|
||||
}).otherwise(function(err) {
|
||||
util.log("[red] Error loading flows : "+err);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
/**
|
||||
* 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 settings;
|
||||
var storage;
|
||||
|
||||
module.exports = {
|
||||
init: function(_settings) {
|
||||
settings = _settings;
|
||||
|
||||
var storageType = settings.storageModule || "localfilesystem";
|
||||
|
||||
storage = require("./"+storageType).init(settings);
|
||||
return storage;
|
||||
},
|
||||
};
|
||||
|
||||
module.exports.__defineGetter__("storage", function() { return storage; });
|
||||
|
|
@ -0,0 +1,356 @@
|
|||
/**
|
||||
* 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 when = require('when');
|
||||
var util = require('util');
|
||||
var fspath = require("path");
|
||||
|
||||
var settings;
|
||||
var flowsFile;
|
||||
var credentialsFile;
|
||||
|
||||
var userDir;
|
||||
var libDir;
|
||||
var libFlowsDir;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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) {
|
||||
var body = "";
|
||||
var fn = fspath.join(root,path);
|
||||
var fd = fs.openSync(fn,"r");
|
||||
var size = fs.fstatSync(fd).size;
|
||||
var scanning = true;
|
||||
var read = 0;
|
||||
var length = 50;
|
||||
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.slice(0,thisRead).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;
|
||||
body += parts[i]+"\n";
|
||||
}
|
||||
}
|
||||
if (! /^\/\/ \w+: /.test(remaining)) {
|
||||
scanning = false;
|
||||
}
|
||||
if (!scanning) {
|
||||
body += remaining;
|
||||
}
|
||||
} else {
|
||||
body += buffer.slice(0,thisRead).toString();
|
||||
}
|
||||
}
|
||||
fs.closeSync(fd);
|
||||
return body;
|
||||
}
|
||||
|
||||
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();
|
||||
});
|
||||
}
|
||||
|
||||
var localfilesystem = {
|
||||
init: function(_settings) {
|
||||
settings = _settings;
|
||||
userDir = settings.userDir || process.env.NODE_RED_HOME;
|
||||
|
||||
if (settings.flowFile) {
|
||||
flowsFile = settings.flowFile;
|
||||
flowsFullPath = flowsFile;
|
||||
} else {
|
||||
flowsFile = 'flows_'+require('os').hostname()+'.json';
|
||||
flowsFullPath = fspath.join(userDir,flowsFile);
|
||||
}
|
||||
credentialsFile = fspath.join(userDir,"credentials.json");
|
||||
|
||||
libDir = fspath.join(userDir,"lib");
|
||||
libFlowsDir = fspath.join(libDir,"flows");
|
||||
|
||||
if (!fs.existsSync(libDir)) {
|
||||
fs.mkdirSync(libDir);
|
||||
}
|
||||
if (!fs.existsSync(libFlowsDir)) {
|
||||
fs.mkdirSync(libFlowsDir);
|
||||
}
|
||||
return this;
|
||||
},
|
||||
getFlows: function() {
|
||||
var defer = when.defer();
|
||||
fs.exists(flowsFullPath, function(exists) {
|
||||
if (exists) {
|
||||
util.log("[red] Loading flows : "+flowsFile);
|
||||
fs.readFile(flowsFullPath,'utf8',function(err,data) {
|
||||
if (err) {
|
||||
defer.reject(err);
|
||||
} else {
|
||||
try {
|
||||
defer.resolve(JSON.parse(data));
|
||||
} catch(err) {
|
||||
defer.reject(err);
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
util.log("[red] Flows file not found : "+flowsFile );
|
||||
defer.resolve([]);
|
||||
}
|
||||
});
|
||||
return defer.promise;
|
||||
},
|
||||
saveFlows: function(flows) {
|
||||
var defer = when.defer();
|
||||
fs.writeFile(flowsFullPath, JSON.stringify(flows), function(err) {
|
||||
if(err) {
|
||||
defer.reject(err);
|
||||
} else {
|
||||
defer.resolve();
|
||||
}
|
||||
});
|
||||
return defer.promise;
|
||||
},
|
||||
|
||||
getCredentials: function() {
|
||||
var defer = when.defer();
|
||||
fs.exists(credentialsFile, function(exists) {
|
||||
if (exists) {
|
||||
fs.readFile(credentialsFile,'utf8',function(err,data) {
|
||||
if (err) {
|
||||
defer.reject(err);
|
||||
} else {
|
||||
try {
|
||||
defer.resolve(JSON.parse(data));
|
||||
} catch(err) {
|
||||
defer.reject(err);
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
defer.resolve({});
|
||||
}
|
||||
});
|
||||
return defer.promise;
|
||||
},
|
||||
|
||||
saveCredentials: function(credentials) {
|
||||
var defer = when.defer();
|
||||
fs.writeFile(credentialsFile, JSON.stringify(credentials), function(err) {
|
||||
if(err) {
|
||||
defer.reject(err);
|
||||
} else {
|
||||
defer.resolve();
|
||||
}
|
||||
});
|
||||
return defer.promise;
|
||||
},
|
||||
|
||||
getAllFlows: function() {
|
||||
var defer = when.defer();
|
||||
defer.resolve(listFiles(libFlowsDir));
|
||||
return defer.promise;
|
||||
},
|
||||
|
||||
getFlow: function(fn) {
|
||||
var defer = when.defer();
|
||||
|
||||
var file = fspath.join(libFlowsDir,fn+".json");
|
||||
fs.exists(file, function(exists) {
|
||||
if (exists) {
|
||||
fs.readFile(file,'utf8',function(err,data) {
|
||||
if (err) {
|
||||
defer.reject(err);
|
||||
} else {
|
||||
defer.resolve(data);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
defer.reject();
|
||||
}
|
||||
});
|
||||
|
||||
return defer.promise;
|
||||
},
|
||||
|
||||
saveFlow: function(fn,data) {
|
||||
var defer = when.defer();
|
||||
var file = fspath.join(libFlowsDir,fn+".json");
|
||||
var parts = file.split("/");
|
||||
for (var i = 0;i<parts.length;i+=1) {
|
||||
var dirname = parts.slice(0,i).join("/");
|
||||
if (dirname != "" ){
|
||||
if (!fs.existsSync(dirname)) {
|
||||
fs.mkdirSync(dirname);
|
||||
}
|
||||
}
|
||||
}
|
||||
fs.writeFile(file,data,function(err) {
|
||||
if (err) {
|
||||
defer.reject(err);
|
||||
} else {
|
||||
defer.resolve();
|
||||
}
|
||||
});
|
||||
return defer.promise;
|
||||
},
|
||||
|
||||
getLibraryEntry: function(type,path) {
|
||||
var defer = when.defer();
|
||||
var root = fspath.join(libDir,type);
|
||||
if (!fs.existsSync(root)) {
|
||||
fs.mkdirSync(root);
|
||||
}
|
||||
var rootPath = fspath.join(libDir,type,path);
|
||||
|
||||
fs.lstat(rootPath,function(err,stats) {
|
||||
if (err) {
|
||||
defer.reject(err);
|
||||
} else if (stats.isFile()) {
|
||||
defer.resolve(getFileBody(root,path));
|
||||
} 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);
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
defer.resolve(dirs.concat(files));
|
||||
});
|
||||
}
|
||||
});
|
||||
return defer.promise;
|
||||
},
|
||||
saveLibraryEntry: function(type,path,meta,body) {
|
||||
var defer = when.defer();
|
||||
|
||||
var root = fspath.join(libDir,type);
|
||||
if (!fs.existsSync(root)) {
|
||||
fs.mkdirSync(root);
|
||||
}
|
||||
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) {
|
||||
if (err) {
|
||||
defer.reject(err);
|
||||
} else {
|
||||
defer.resolve();
|
||||
}
|
||||
});
|
||||
return defer.promise;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = localfilesystem;
|
|
@ -28,6 +28,10 @@ module.exports = {
|
|||
// The file containing the flows. If not set, it defaults to flows_<hostname>.json
|
||||
//flowFile: 'flows.json',
|
||||
|
||||
// By default, all user data is stored in the Node-RED install directory. To
|
||||
// use a different location, the following property can be used
|
||||
//userDir: '/home/nol/.node-red/',
|
||||
|
||||
// You can protect the user interface with a userid and password by using the following property
|
||||
// the password must be an md5 hash eg.. 5f4dcc3b5aa765d61d8327deb882cf99 ('password')
|
||||
//httpAuth: {user:"user",pass:"5f4dcc3b5aa765d61d8327deb882cf99"},
|
||||
|
|
|
@ -26,7 +26,7 @@ describe('Node', function() {
|
|||
n.should.have.property('id','123');
|
||||
n.should.have.property('type','abc');
|
||||
n.should.not.have.property('name');
|
||||
n.wires.should.have.a.length(2);
|
||||
n.wires.should.have.length(2);
|
||||
});
|
||||
|
||||
});
|
||||
|
|
|
@ -0,0 +1,258 @@
|
|||
var should = require("should");
|
||||
var fs = require('fs-extra');
|
||||
var path = require('path');
|
||||
|
||||
var localfilesystem = require("../red/storage/localfilesystem");
|
||||
|
||||
describe('LocalFileSystem', function() {
|
||||
var userDir = path.join(__dirname,".testUserHome");
|
||||
var testFlow = [{"type":"tab","id":"d8be2a6d.2741d8","label":"Sheet 1"}];
|
||||
beforeEach(function() {
|
||||
if (fs.existsSync(userDir)) {
|
||||
fs.removeSync(userDir)
|
||||
}
|
||||
fs.mkdirSync(userDir);
|
||||
});
|
||||
afterEach(function() {
|
||||
fs.removeSync(userDir);
|
||||
});
|
||||
|
||||
it('should initialise the user directory',function() {
|
||||
localfilesystem.init({userDir:userDir});
|
||||
fs.existsSync(path.join(userDir,"lib")).should.be.true;
|
||||
fs.existsSync(path.join(userDir,"lib",'flows')).should.be.true;
|
||||
});
|
||||
|
||||
it('should handle missing flow file',function(done) {
|
||||
localfilesystem.init({userDir:userDir});
|
||||
var flowFile = 'flows_'+require('os').hostname()+'.json';
|
||||
var flowFilePath = path.join(userDir,flowFile);
|
||||
fs.existsSync(flowFilePath).should.be.false;
|
||||
localfilesystem.getFlows().then(function(flows) {
|
||||
flows.should.eql([]);
|
||||
done();
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
it('should save flows to the default file',function(done) {
|
||||
localfilesystem.init({userDir:userDir});
|
||||
var flowFile = 'flows_'+require('os').hostname()+'.json';
|
||||
var flowFilePath = path.join(userDir,flowFile);
|
||||
fs.existsSync(flowFilePath).should.be.false;
|
||||
localfilesystem.saveFlows(testFlow).then(function() {
|
||||
fs.existsSync(flowFilePath).should.be.true;
|
||||
localfilesystem.getFlows().then(function(flows) {
|
||||
flows.should.eql(testFlow);
|
||||
done();
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should save flows to the specified file',function(done) {
|
||||
var defaultFlowFile = 'flows_'+require('os').hostname()+'.json';
|
||||
var defaultFlowFilePath = path.join(userDir,defaultFlowFile);
|
||||
var flowFile = 'test.json';
|
||||
var flowFilePath = path.join(userDir,flowFile);
|
||||
|
||||
localfilesystem.init({userDir:userDir, flowFile:flowFilePath});
|
||||
|
||||
fs.existsSync(defaultFlowFilePath).should.be.false;
|
||||
fs.existsSync(flowFilePath).should.be.false;
|
||||
|
||||
localfilesystem.saveFlows(testFlow).then(function() {
|
||||
fs.existsSync(defaultFlowFilePath).should.be.false;
|
||||
fs.existsSync(flowFilePath).should.be.true;
|
||||
localfilesystem.getFlows().then(function(flows) {
|
||||
flows.should.eql(testFlow);
|
||||
done();
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle missing credentials', function(done) {
|
||||
var credFile = path.join(userDir,"credentials.json");
|
||||
localfilesystem.init({userDir:userDir});
|
||||
fs.existsSync(credFile).should.be.false;
|
||||
|
||||
localfilesystem.getCredentials().then(function(creds) {
|
||||
creds.should.eql({});
|
||||
done();
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle credentials', function(done) {
|
||||
var credFile = path.join(userDir,"credentials.json");
|
||||
|
||||
localfilesystem.init({userDir:userDir});
|
||||
|
||||
fs.existsSync(credFile).should.be.false;
|
||||
|
||||
var credentials = {"abc":{"type":"creds"}};
|
||||
|
||||
localfilesystem.saveCredentials(credentials).then(function() {
|
||||
fs.existsSync(credFile).should.be.true;
|
||||
localfilesystem.getCredentials().then(function(creds) {
|
||||
creds.should.eql(credentials);
|
||||
done();
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should return an empty list of library flows',function(done) {
|
||||
localfilesystem.init({userDir:userDir});
|
||||
localfilesystem.getAllFlows().then(function(flows) {
|
||||
flows.should.eql({});
|
||||
done();
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should return a valid list of library flows',function(done) {
|
||||
localfilesystem.init({userDir:userDir});
|
||||
var flowLib = path.join(userDir,"lib","flows");
|
||||
fs.closeSync(fs.openSync(path.join(flowLib,"A.json"),"w"));
|
||||
fs.closeSync(fs.openSync(path.join(flowLib,"B.json"),"w"));
|
||||
fs.mkdirSync(path.join(flowLib,"C"));
|
||||
fs.closeSync(fs.openSync(path.join(flowLib,"C","D.json"),"w"));
|
||||
var testFlowsList = {"d":{"C":{"f":["D"]}},"f":["A","B"]};
|
||||
|
||||
localfilesystem.getAllFlows().then(function(flows) {
|
||||
flows.should.eql(testFlowsList);
|
||||
done();
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should fail a non-existent flow', function(done) {
|
||||
localfilesystem.init({userDir:userDir});
|
||||
localfilesystem.getFlow("a/b/c.json").then(function(flow) {
|
||||
should.fail(flow,"No flow","Flow found");
|
||||
}).otherwise(function(err) {
|
||||
// err should be null, so this will pass
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should return a flow',function(done) {
|
||||
localfilesystem.init({userDir:userDir});
|
||||
var testflowString = JSON.stringify(testFlow);
|
||||
localfilesystem.saveFlow("a/b/c/d.json",testflowString).then(function() {
|
||||
localfilesystem.getFlow("a/b/c/d.json").then(function(flow) {
|
||||
flow.should.eql(testflowString);
|
||||
done();
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should return an empty list of library objects',function(done) {
|
||||
localfilesystem.init({userDir:userDir});
|
||||
localfilesystem.getLibraryEntry('object','').then(function(flows) {
|
||||
flows.should.eql({});
|
||||
done();
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should return an error for a non-existent library object',function(done) {
|
||||
localfilesystem.init({userDir:userDir});
|
||||
localfilesystem.getLibraryEntry('object','A/B').then(function(flows) {
|
||||
should.fail(null,null,"non-existent flow");
|
||||
}).otherwise(function(err) {
|
||||
should.exist(err);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
function createObjectLibrary() {
|
||||
var objLib = path.join(userDir,"lib","object");
|
||||
fs.mkdirSync(objLib);
|
||||
fs.mkdirSync(path.join(objLib,"A"));
|
||||
fs.mkdirSync(path.join(objLib,"B"));
|
||||
fs.mkdirSync(path.join(objLib,"B","C"));
|
||||
fs.writeFileSync(path.join(objLib,"file1.js"),"// abc: def\n// not a metaline \n\n Hi",'utf8');
|
||||
fs.writeFileSync(path.join(objLib,"B","file2.js"),"// ghi: jkl\n// not a metaline \n\n Hi",'utf8');
|
||||
}
|
||||
|
||||
it('should return a directory listing of library objects',function(done) {
|
||||
localfilesystem.init({userDir:userDir});
|
||||
createObjectLibrary();
|
||||
|
||||
localfilesystem.getLibraryEntry('object','').then(function(flows) {
|
||||
flows.should.eql([ 'A', 'B', { abc: 'def', fn: 'file1.js' } ]);
|
||||
localfilesystem.getLibraryEntry('object','B').then(function(flows) {
|
||||
flows.should.eql([ 'C', { ghi: 'jkl', fn: 'file2.js' } ]);
|
||||
localfilesystem.getLibraryEntry('object','B/C').then(function(flows) {
|
||||
flows.should.eql([]);
|
||||
done();
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should return a library object',function(done) {
|
||||
localfilesystem.init({userDir:userDir});
|
||||
createObjectLibrary();
|
||||
localfilesystem.getLibraryEntry('object','B/file2.js').then(function(body) {
|
||||
body.should.eql("// not a metaline \n\n Hi");
|
||||
done();
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should return a newly saved library object',function(done) {
|
||||
localfilesystem.init({userDir:userDir});
|
||||
createObjectLibrary();
|
||||
localfilesystem.getLibraryEntry('object','B').then(function(flows) {
|
||||
flows.should.eql([ 'C', { ghi: 'jkl', fn: 'file2.js' } ]);
|
||||
localfilesystem.saveLibraryEntry('object','B/D/file3.js',{mno:'pqr'},"// another non meta line\n\n Hi There").then(function() {
|
||||
localfilesystem.getLibraryEntry('object','B/D').then(function(flows) {
|
||||
flows.should.eql([ { mno: 'pqr', fn: 'file3.js' } ]);
|
||||
localfilesystem.getLibraryEntry('object','B/D/file3.js').then(function(body) {
|
||||
body.should.eql("// another non meta line\n\n Hi There");
|
||||
done();
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
Loading…
Reference in New Issue