mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Split localfilesystem storage plugin into component parts
This commit is contained in:
parent
41af5187aa
commit
9a8b404054
@ -34,14 +34,11 @@ module.exports = {
|
||||
},
|
||||
|
||||
determineLangFromHeaders: function(acceptedLanguages){
|
||||
console.log("GOT",acceptedLanguages)
|
||||
var lang = i18n.defaultLang;
|
||||
acceptedLanguages = acceptedLanguages || [];
|
||||
if (acceptedLanguages.length >= 1) {
|
||||
console.log("WE HAVE SOMETHING");
|
||||
lang = acceptedLanguages[0];
|
||||
}
|
||||
console.log("RETURNING",lang);
|
||||
return lang;
|
||||
}
|
||||
}
|
||||
|
@ -1,441 +0,0 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
**/
|
||||
|
||||
var fs = require('fs-extra');
|
||||
var when = require('when');
|
||||
var nodeFn = require('when/node/function');
|
||||
var keys = require('when/keys');
|
||||
var fspath = require("path");
|
||||
var mkdirp = fs.mkdirs;
|
||||
|
||||
var log = require("../log");
|
||||
|
||||
var promiseDir = nodeFn.lift(mkdirp);
|
||||
|
||||
var initialFlowLoadComplete = false;
|
||||
var settings;
|
||||
var flowsFile;
|
||||
var flowsFullPath;
|
||||
var flowsFileBackup;
|
||||
var credentialsFile;
|
||||
var credentialsFileBackup;
|
||||
var oldCredentialsFile;
|
||||
var sessionsFile;
|
||||
var libDir;
|
||||
var libFlowsDir;
|
||||
var globalSettingsFile;
|
||||
|
||||
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 read = 0;
|
||||
var length = 10;
|
||||
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 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write content to a file using UTF8 encoding.
|
||||
* This forces a fsync before completing to ensure
|
||||
* the write hits disk.
|
||||
*/
|
||||
function writeFile(path,content) {
|
||||
return when.promise(function(resolve,reject) {
|
||||
var stream = fs.createWriteStream(path);
|
||||
stream.on('open',function(fd) {
|
||||
stream.end(content,'utf8',function() {
|
||||
fs.fsync(fd,resolve);
|
||||
});
|
||||
});
|
||||
stream.on('error',function(err) {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function parseJSON(data) {
|
||||
if (data.charCodeAt(0) === 0xFEFF) {
|
||||
data = data.slice(1)
|
||||
}
|
||||
return JSON.parse(data);
|
||||
}
|
||||
|
||||
function readFile(path,backupPath,emptyResponse,type) {
|
||||
return when.promise(function(resolve) {
|
||||
fs.readFile(path,'utf8',function(err,data) {
|
||||
if (!err) {
|
||||
if (data.length === 0) {
|
||||
log.warn(log._("storage.localfilesystem.empty",{type:type}));
|
||||
try {
|
||||
var backupStat = fs.statSync(backupPath);
|
||||
if (backupStat.size === 0) {
|
||||
// Empty flows, empty backup - return empty flow
|
||||
return resolve(emptyResponse);
|
||||
}
|
||||
// Empty flows, restore backup
|
||||
log.warn(log._("storage.localfilesystem.restore",{path:backupPath,type:type}));
|
||||
fs.copy(backupPath,path,function(backupCopyErr) {
|
||||
if (backupCopyErr) {
|
||||
// Restore backup failed
|
||||
log.warn(log._("storage.localfilesystem.restore-fail",{message:backupCopyErr.toString(),type:type}));
|
||||
resolve([]);
|
||||
} else {
|
||||
// Loop back in to load the restored backup
|
||||
resolve(readFile(path,backupPath,emptyResponse,type));
|
||||
}
|
||||
});
|
||||
return;
|
||||
} catch(backupStatErr) {
|
||||
// Empty flow file, no back-up file
|
||||
return resolve(emptyResponse);
|
||||
}
|
||||
}
|
||||
try {
|
||||
return resolve(parseJSON(data));
|
||||
} catch(parseErr) {
|
||||
log.warn(log._("storage.localfilesystem.invalid",{type:type}));
|
||||
return resolve(emptyResponse);
|
||||
}
|
||||
} else {
|
||||
if (type === 'flow') {
|
||||
log.info(log._("storage.localfilesystem.create",{type:type}));
|
||||
}
|
||||
resolve(emptyResponse);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
var localfilesystem = {
|
||||
init: function(_settings) {
|
||||
settings = _settings;
|
||||
|
||||
var promises = [];
|
||||
|
||||
if (!settings.userDir) {
|
||||
try {
|
||||
fs.statSync(fspath.join(process.env.NODE_RED_HOME,".config.json"));
|
||||
settings.userDir = process.env.NODE_RED_HOME;
|
||||
} catch(err) {
|
||||
try {
|
||||
// Consider compatibility for older versions
|
||||
if (process.env.HOMEPATH) {
|
||||
fs.statSync(fspath.join(process.env.HOMEPATH,".node-red",".config.json"));
|
||||
settings.userDir = fspath.join(process.env.HOMEPATH,".node-red");
|
||||
}
|
||||
} catch(err) {
|
||||
}
|
||||
if (!settings.userDir) {
|
||||
settings.userDir = fspath.join(process.env.HOME || process.env.USERPROFILE || process.env.HOMEPATH || process.env.NODE_RED_HOME,".node-red");
|
||||
if (!settings.readOnly) {
|
||||
promises.push(promiseDir(fspath.join(settings.userDir,"node_modules")));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (settings.flowFile) {
|
||||
flowsFile = settings.flowFile;
|
||||
// handle Unix and Windows "C:\"
|
||||
if ((flowsFile[0] == "/") || (flowsFile[1] == ":")) {
|
||||
// Absolute path
|
||||
flowsFullPath = flowsFile;
|
||||
} else if (flowsFile.substring(0,2) === "./") {
|
||||
// Relative to cwd
|
||||
flowsFullPath = fspath.join(process.cwd(),flowsFile);
|
||||
} else {
|
||||
try {
|
||||
fs.statSync(fspath.join(process.cwd(),flowsFile));
|
||||
// Found in cwd
|
||||
flowsFullPath = fspath.join(process.cwd(),flowsFile);
|
||||
} catch(err) {
|
||||
// Use userDir
|
||||
flowsFullPath = fspath.join(settings.userDir,flowsFile);
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
flowsFile = 'flows_'+require('os').hostname()+'.json';
|
||||
flowsFullPath = fspath.join(settings.userDir,flowsFile);
|
||||
}
|
||||
var ffExt = fspath.extname(flowsFullPath);
|
||||
var ffName = fspath.basename(flowsFullPath);
|
||||
var ffBase = fspath.basename(flowsFullPath,ffExt);
|
||||
var ffDir = fspath.dirname(flowsFullPath);
|
||||
|
||||
credentialsFile = fspath.join(settings.userDir,ffBase+"_cred"+ffExt);
|
||||
credentialsFileBackup = fspath.join(settings.userDir,"."+ffBase+"_cred"+ffExt+".backup");
|
||||
|
||||
oldCredentialsFile = fspath.join(settings.userDir,"credentials.json");
|
||||
|
||||
flowsFileBackup = fspath.join(ffDir,"."+ffName+".backup");
|
||||
|
||||
sessionsFile = fspath.join(settings.userDir,".sessions.json");
|
||||
|
||||
libDir = fspath.join(settings.userDir,"lib");
|
||||
libFlowsDir = fspath.join(libDir,"flows");
|
||||
|
||||
globalSettingsFile = fspath.join(settings.userDir,".config.json");
|
||||
|
||||
var packageFile = fspath.join(settings.userDir,"package.json");
|
||||
var packagePromise = when.resolve();
|
||||
if (!settings.readOnly) {
|
||||
promises.push(promiseDir(libFlowsDir));
|
||||
packagePromise = function() {
|
||||
try {
|
||||
fs.statSync(packageFile);
|
||||
} catch(err) {
|
||||
var defaultPackage = {
|
||||
"name": "node-red-project",
|
||||
"description": "A Node-RED Project",
|
||||
"version": "0.0.1"
|
||||
};
|
||||
return writeFile(packageFile,JSON.stringify(defaultPackage,"",4));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return when.all(promises).then(packagePromise);
|
||||
},
|
||||
|
||||
getFlows: function() {
|
||||
if (!initialFlowLoadComplete) {
|
||||
initialFlowLoadComplete = true;
|
||||
log.info(log._("storage.localfilesystem.user-dir",{path:settings.userDir}));
|
||||
log.info(log._("storage.localfilesystem.flows-file",{path:flowsFullPath}));
|
||||
}
|
||||
return readFile(flowsFullPath,flowsFileBackup,[],'flow');
|
||||
},
|
||||
|
||||
saveFlows: function(flows) {
|
||||
if (settings.readOnly) {
|
||||
return when.resolve();
|
||||
}
|
||||
|
||||
try {
|
||||
fs.renameSync(flowsFullPath,flowsFileBackup);
|
||||
} catch(err) {
|
||||
}
|
||||
|
||||
var flowData;
|
||||
|
||||
if (settings.flowFilePretty) {
|
||||
flowData = JSON.stringify(flows,null,4);
|
||||
} else {
|
||||
flowData = JSON.stringify(flows);
|
||||
}
|
||||
return writeFile(flowsFullPath, flowData);
|
||||
},
|
||||
|
||||
getCredentials: function() {
|
||||
return readFile(credentialsFile,credentialsFileBackup,{},'credentials');
|
||||
},
|
||||
|
||||
saveCredentials: function(credentials) {
|
||||
if (settings.readOnly) {
|
||||
return when.resolve();
|
||||
}
|
||||
|
||||
try {
|
||||
fs.renameSync(credentialsFile,credentialsFileBackup);
|
||||
} catch(err) {
|
||||
}
|
||||
var credentialData;
|
||||
if (settings.flowFilePretty) {
|
||||
credentialData = JSON.stringify(credentials,null,4);
|
||||
} else {
|
||||
credentialData = JSON.stringify(credentials);
|
||||
}
|
||||
return writeFile(credentialsFile, credentialData);
|
||||
},
|
||||
|
||||
getSettings: function() {
|
||||
return when.promise(function(resolve,reject) {
|
||||
fs.readFile(globalSettingsFile,'utf8',function(err,data) {
|
||||
if (!err) {
|
||||
try {
|
||||
return resolve(parseJSON(data));
|
||||
} catch(err2) {
|
||||
log.trace("Corrupted config detected - resetting");
|
||||
}
|
||||
}
|
||||
return resolve({});
|
||||
})
|
||||
})
|
||||
},
|
||||
saveSettings: function(newSettings) {
|
||||
if (settings.readOnly) {
|
||||
return when.resolve();
|
||||
}
|
||||
return writeFile(globalSettingsFile,JSON.stringify(newSettings,null,1));
|
||||
},
|
||||
getSessions: function() {
|
||||
return when.promise(function(resolve,reject) {
|
||||
fs.readFile(sessionsFile,'utf8',function(err,data){
|
||||
if (!err) {
|
||||
try {
|
||||
return resolve(parseJSON(data));
|
||||
} catch(err2) {
|
||||
log.trace("Corrupted sessions file - resetting");
|
||||
}
|
||||
}
|
||||
resolve({});
|
||||
})
|
||||
});
|
||||
},
|
||||
saveSessions: function(sessions) {
|
||||
if (settings.readOnly) {
|
||||
return when.resolve();
|
||||
}
|
||||
return writeFile(sessionsFile,JSON.stringify(sessions));
|
||||
},
|
||||
|
||||
getLibraryEntry: function(type,path) {
|
||||
var root = fspath.join(libDir,type);
|
||||
var rootPath = fspath.join(libDir,type,path);
|
||||
|
||||
// don't create the folder if it does not exist - we are only reading....
|
||||
return nodeFn.call(fs.lstat, rootPath).then(function(stats) {
|
||||
if (stats.isFile()) {
|
||||
return getFileBody(root,path);
|
||||
}
|
||||
if (path.substr(-1) == '/') {
|
||||
path = path.substr(0,path.length-1);
|
||||
}
|
||||
return nodeFn.call(fs.readdir, rootPath).then(function(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);
|
||||
}
|
||||
}
|
||||
});
|
||||
return dirs.concat(files);
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
// if path is empty, then assume it was a folder, return empty
|
||||
if (path === ""){
|
||||
return [];
|
||||
}
|
||||
|
||||
// if path ends with slash, it was a folder
|
||||
// so return empty
|
||||
if (path.substr(-1) == '/') {
|
||||
return [];
|
||||
}
|
||||
|
||||
// else path was specified, but did not exist,
|
||||
// check for path.json as an alternative if flows
|
||||
if (type === "flows" && !/\.json$/.test(path)) {
|
||||
return localfilesystem.getLibraryEntry(type,path+".json")
|
||||
.otherwise(function(e) {
|
||||
throw err;
|
||||
});
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
saveLibraryEntry: function(type,path,meta,body) {
|
||||
if (settings.readOnly) {
|
||||
return when.resolve();
|
||||
}
|
||||
if (type === "flows" && !path.endsWith(".json")) {
|
||||
path += ".json";
|
||||
}
|
||||
var fn = fspath.join(libDir, type, path);
|
||||
var headers = "";
|
||||
for (var i in meta) {
|
||||
if (meta.hasOwnProperty(i)) {
|
||||
headers += "// "+i+": "+meta[i]+"\n";
|
||||
}
|
||||
}
|
||||
if (type === "flows" && settings.flowFilePretty) {
|
||||
body = JSON.stringify(JSON.parse(body),null,4);
|
||||
}
|
||||
return promiseDir(fspath.dirname(fn)).then(function () {
|
||||
writeFile(fn,headers+body);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = localfilesystem;
|
187
red/runtime/storage/localfilesystem/index.js
Normal file
187
red/runtime/storage/localfilesystem/index.js
Normal file
@ -0,0 +1,187 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
**/
|
||||
|
||||
var fs = require('fs-extra');
|
||||
var when = require('when');
|
||||
var nodeFn = require('when/node/function');
|
||||
var fspath = require("path");
|
||||
|
||||
var log = require("../../log");
|
||||
var util = require("./util");
|
||||
var library = require("./library");
|
||||
var sessions = require("./sessions");
|
||||
var runtimeSettings = require("./settings");
|
||||
|
||||
var initialFlowLoadComplete = false;
|
||||
var settings;
|
||||
var flowsFile;
|
||||
var flowsFullPath;
|
||||
var flowsFileBackup;
|
||||
var credentialsFile;
|
||||
var credentialsFileBackup;
|
||||
var oldCredentialsFile;
|
||||
|
||||
var localfilesystem = {
|
||||
init: function(_settings) {
|
||||
settings = _settings;
|
||||
|
||||
var promises = [];
|
||||
|
||||
if (!settings.userDir) {
|
||||
try {
|
||||
fs.statSync(fspath.join(process.env.NODE_RED_HOME,".config.json"));
|
||||
settings.userDir = process.env.NODE_RED_HOME;
|
||||
} catch(err) {
|
||||
try {
|
||||
// Consider compatibility for older versions
|
||||
if (process.env.HOMEPATH) {
|
||||
fs.statSync(fspath.join(process.env.HOMEPATH,".node-red",".config.json"));
|
||||
settings.userDir = fspath.join(process.env.HOMEPATH,".node-red");
|
||||
}
|
||||
} catch(err) {
|
||||
}
|
||||
if (!settings.userDir) {
|
||||
settings.userDir = fspath.join(process.env.HOME || process.env.USERPROFILE || process.env.HOMEPATH || process.env.NODE_RED_HOME,".node-red");
|
||||
if (!settings.readOnly) {
|
||||
promises.push(util.promiseDir(fspath.join(settings.userDir,"node_modules")));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (settings.flowFile) {
|
||||
flowsFile = settings.flowFile;
|
||||
// handle Unix and Windows "C:\"
|
||||
if ((flowsFile[0] == "/") || (flowsFile[1] == ":")) {
|
||||
// Absolute path
|
||||
flowsFullPath = flowsFile;
|
||||
} else if (flowsFile.substring(0,2) === "./") {
|
||||
// Relative to cwd
|
||||
flowsFullPath = fspath.join(process.cwd(),flowsFile);
|
||||
} else {
|
||||
try {
|
||||
fs.statSync(fspath.join(process.cwd(),flowsFile));
|
||||
// Found in cwd
|
||||
flowsFullPath = fspath.join(process.cwd(),flowsFile);
|
||||
} catch(err) {
|
||||
// Use userDir
|
||||
flowsFullPath = fspath.join(settings.userDir,flowsFile);
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
flowsFile = 'flows_'+require('os').hostname()+'.json';
|
||||
flowsFullPath = fspath.join(settings.userDir,flowsFile);
|
||||
}
|
||||
var ffExt = fspath.extname(flowsFullPath);
|
||||
var ffName = fspath.basename(flowsFullPath);
|
||||
var ffBase = fspath.basename(flowsFullPath,ffExt);
|
||||
var ffDir = fspath.dirname(flowsFullPath);
|
||||
|
||||
credentialsFile = fspath.join(settings.userDir,ffBase+"_cred"+ffExt);
|
||||
credentialsFileBackup = fspath.join(settings.userDir,"."+ffBase+"_cred"+ffExt+".backup");
|
||||
|
||||
oldCredentialsFile = fspath.join(settings.userDir,"credentials.json");
|
||||
|
||||
flowsFileBackup = fspath.join(ffDir,"."+ffName+".backup");
|
||||
|
||||
sessions.init(settings);
|
||||
runtimeSettings.init(settings);
|
||||
|
||||
var packageFile = fspath.join(settings.userDir,"package.json");
|
||||
var packagePromise = when.resolve();
|
||||
|
||||
promises.push(library.init(settings));
|
||||
|
||||
if (!settings.readOnly) {
|
||||
packagePromise = function() {
|
||||
try {
|
||||
fs.statSync(packageFile);
|
||||
} catch(err) {
|
||||
var defaultPackage = {
|
||||
"name": "node-red-project",
|
||||
"description": "A Node-RED Project",
|
||||
"version": "0.0.1"
|
||||
};
|
||||
return util.writeFile(packageFile,JSON.stringify(defaultPackage,"",4));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return when.all(promises).then(packagePromise);
|
||||
},
|
||||
|
||||
getFlows: function() {
|
||||
if (!initialFlowLoadComplete) {
|
||||
initialFlowLoadComplete = true;
|
||||
log.info(log._("storage.localfilesystem.user-dir",{path:settings.userDir}));
|
||||
log.info(log._("storage.localfilesystem.flows-file",{path:flowsFullPath}));
|
||||
}
|
||||
return util.readFile(flowsFullPath,flowsFileBackup,[],'flow');
|
||||
},
|
||||
|
||||
saveFlows: function(flows) {
|
||||
if (settings.readOnly) {
|
||||
return when.resolve();
|
||||
}
|
||||
|
||||
try {
|
||||
fs.renameSync(flowsFullPath,flowsFileBackup);
|
||||
} catch(err) {
|
||||
}
|
||||
|
||||
var flowData;
|
||||
|
||||
if (settings.flowFilePretty) {
|
||||
flowData = JSON.stringify(flows,null,4);
|
||||
} else {
|
||||
flowData = JSON.stringify(flows);
|
||||
}
|
||||
return util.writeFile(flowsFullPath, flowData);
|
||||
},
|
||||
|
||||
getCredentials: function() {
|
||||
return util.readFile(credentialsFile,credentialsFileBackup,{},'credentials');
|
||||
},
|
||||
|
||||
saveCredentials: function(credentials) {
|
||||
if (settings.readOnly) {
|
||||
return when.resolve();
|
||||
}
|
||||
|
||||
try {
|
||||
fs.renameSync(credentialsFile,credentialsFileBackup);
|
||||
} catch(err) {
|
||||
}
|
||||
var credentialData;
|
||||
if (settings.flowFilePretty) {
|
||||
credentialData = JSON.stringify(credentials,null,4);
|
||||
} else {
|
||||
credentialData = JSON.stringify(credentials);
|
||||
}
|
||||
return util.writeFile(credentialsFile, credentialData);
|
||||
},
|
||||
|
||||
getSettings: runtimeSettings.getSettings,
|
||||
saveSettings: runtimeSettings.saveSettings,
|
||||
getSessions: sessions.getSessions,
|
||||
saveSessions: sessions.saveSessions,
|
||||
getLibraryEntry: library.getLibraryEntry,
|
||||
saveLibraryEntry: library.saveLibraryEntry
|
||||
|
||||
};
|
||||
|
||||
module.exports = localfilesystem;
|
183
red/runtime/storage/localfilesystem/library.js
Normal file
183
red/runtime/storage/localfilesystem/library.js
Normal file
@ -0,0 +1,183 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
**/
|
||||
|
||||
var fs = require('fs-extra');
|
||||
var when = require('when');
|
||||
var fspath = require("path");
|
||||
var nodeFn = require('when/node/function');
|
||||
|
||||
var util = require("./util");
|
||||
|
||||
var settings;
|
||||
var libDir;
|
||||
var libFlowsDir;
|
||||
|
||||
|
||||
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 read = 0;
|
||||
var length = 10;
|
||||
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 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 getLibraryEntry(type,path) {
|
||||
var root = fspath.join(libDir,type);
|
||||
var rootPath = fspath.join(libDir,type,path);
|
||||
|
||||
// don't create the folder if it does not exist - we are only reading....
|
||||
return nodeFn.call(fs.lstat, rootPath).then(function(stats) {
|
||||
if (stats.isFile()) {
|
||||
return getFileBody(root,path);
|
||||
}
|
||||
if (path.substr(-1) == '/') {
|
||||
path = path.substr(0,path.length-1);
|
||||
}
|
||||
return nodeFn.call(fs.readdir, rootPath).then(function(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);
|
||||
}
|
||||
}
|
||||
});
|
||||
return dirs.concat(files);
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
// if path is empty, then assume it was a folder, return empty
|
||||
if (path === ""){
|
||||
return [];
|
||||
}
|
||||
|
||||
// if path ends with slash, it was a folder
|
||||
// so return empty
|
||||
if (path.substr(-1) == '/') {
|
||||
return [];
|
||||
}
|
||||
|
||||
// else path was specified, but did not exist,
|
||||
// check for path.json as an alternative if flows
|
||||
if (type === "flows" && !/\.json$/.test(path)) {
|
||||
return getLibraryEntry(type,path+".json")
|
||||
.otherwise(function(e) {
|
||||
throw err;
|
||||
});
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
init: function(_settings) {
|
||||
settings = _settings;
|
||||
libDir = fspath.join(settings.userDir,"lib");
|
||||
libFlowsDir = fspath.join(libDir,"flows");
|
||||
if (!settings.readOnly) {
|
||||
return util.promiseDir(libFlowsDir);
|
||||
} else {
|
||||
return when.resolve();
|
||||
}
|
||||
},
|
||||
getLibraryEntry: getLibraryEntry,
|
||||
|
||||
saveLibraryEntry: function(type,path,meta,body) {
|
||||
if (settings.readOnly) {
|
||||
return when.resolve();
|
||||
}
|
||||
if (type === "flows" && !path.endsWith(".json")) {
|
||||
path += ".json";
|
||||
}
|
||||
var fn = fspath.join(libDir, type, path);
|
||||
var headers = "";
|
||||
for (var i in meta) {
|
||||
if (meta.hasOwnProperty(i)) {
|
||||
headers += "// "+i+": "+meta[i]+"\n";
|
||||
}
|
||||
}
|
||||
if (type === "flows" && settings.flowFilePretty) {
|
||||
body = JSON.stringify(JSON.parse(body),null,4);
|
||||
}
|
||||
return util.promiseDir(fspath.dirname(fn)).then(function () {
|
||||
util.writeFile(fn,headers+body);
|
||||
});
|
||||
}
|
||||
}
|
52
red/runtime/storage/localfilesystem/sessions.js
Normal file
52
red/runtime/storage/localfilesystem/sessions.js
Normal file
@ -0,0 +1,52 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
**/
|
||||
|
||||
var when = require('when');
|
||||
var fs = require('fs-extra');
|
||||
var fspath = require("path");
|
||||
|
||||
var log = require("../../log");
|
||||
var util = require("./util");
|
||||
|
||||
var sessionsFile;
|
||||
var settings;
|
||||
|
||||
module.exports = {
|
||||
init: function(_settings) {
|
||||
settings = _settings;
|
||||
sessionsFile = fspath.join(settings.userDir,".sessions.json");
|
||||
},
|
||||
getSessions: function() {
|
||||
return when.promise(function(resolve,reject) {
|
||||
fs.readFile(sessionsFile,'utf8',function(err,data){
|
||||
if (!err) {
|
||||
try {
|
||||
return resolve(util.parseJSON(data));
|
||||
} catch(err2) {
|
||||
log.trace("Corrupted sessions file - resetting");
|
||||
}
|
||||
}
|
||||
resolve({});
|
||||
})
|
||||
});
|
||||
},
|
||||
saveSessions: function(sessions) {
|
||||
if (settings.readOnly) {
|
||||
return when.resolve();
|
||||
}
|
||||
return util.writeFile(sessionsFile,JSON.stringify(sessions));
|
||||
}
|
||||
}
|
52
red/runtime/storage/localfilesystem/settings.js
Normal file
52
red/runtime/storage/localfilesystem/settings.js
Normal file
@ -0,0 +1,52 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
**/
|
||||
|
||||
var when = require('when');
|
||||
var fs = require('fs-extra');
|
||||
var fspath = require("path");
|
||||
|
||||
var log = require("../../log");
|
||||
var util = require("./util");
|
||||
|
||||
var globalSettingsFile;
|
||||
var settings;
|
||||
|
||||
module.exports = {
|
||||
init: function(_settings) {
|
||||
settings = _settings;
|
||||
globalSettingsFile = fspath.join(settings.userDir,".config.json");
|
||||
},
|
||||
getSettings: function() {
|
||||
return when.promise(function(resolve,reject) {
|
||||
fs.readFile(globalSettingsFile,'utf8',function(err,data) {
|
||||
if (!err) {
|
||||
try {
|
||||
return resolve(util.parseJSON(data));
|
||||
} catch(err2) {
|
||||
log.trace("Corrupted config detected - resetting");
|
||||
}
|
||||
}
|
||||
return resolve({});
|
||||
})
|
||||
})
|
||||
},
|
||||
saveSettings: function(newSettings) {
|
||||
if (settings.readOnly) {
|
||||
return when.resolve();
|
||||
}
|
||||
return util.writeFile(globalSettingsFile,JSON.stringify(newSettings,null,1));
|
||||
}
|
||||
}
|
98
red/runtime/storage/localfilesystem/util.js
Normal file
98
red/runtime/storage/localfilesystem/util.js
Normal file
@ -0,0 +1,98 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
**/
|
||||
|
||||
var fs = require('fs-extra');
|
||||
var when = require('when');
|
||||
var nodeFn = require('when/node/function');
|
||||
|
||||
var log = require("../../log");
|
||||
|
||||
function parseJSON(data) {
|
||||
if (data.charCodeAt(0) === 0xFEFF) {
|
||||
data = data.slice(1)
|
||||
}
|
||||
return JSON.parse(data);
|
||||
}
|
||||
function readFile(path,backupPath,emptyResponse,type) {
|
||||
return when.promise(function(resolve) {
|
||||
fs.readFile(path,'utf8',function(err,data) {
|
||||
if (!err) {
|
||||
if (data.length === 0) {
|
||||
log.warn(log._("storage.localfilesystem.empty",{type:type}));
|
||||
try {
|
||||
var backupStat = fs.statSync(backupPath);
|
||||
if (backupStat.size === 0) {
|
||||
// Empty flows, empty backup - return empty flow
|
||||
return resolve(emptyResponse);
|
||||
}
|
||||
// Empty flows, restore backup
|
||||
log.warn(log._("storage.localfilesystem.restore",{path:backupPath,type:type}));
|
||||
fs.copy(backupPath,path,function(backupCopyErr) {
|
||||
if (backupCopyErr) {
|
||||
// Restore backup failed
|
||||
log.warn(log._("storage.localfilesystem.restore-fail",{message:backupCopyErr.toString(),type:type}));
|
||||
resolve([]);
|
||||
} else {
|
||||
// Loop back in to load the restored backup
|
||||
resolve(readFile(path,backupPath,emptyResponse,type));
|
||||
}
|
||||
});
|
||||
return;
|
||||
} catch(backupStatErr) {
|
||||
// Empty flow file, no back-up file
|
||||
return resolve(emptyResponse);
|
||||
}
|
||||
}
|
||||
try {
|
||||
return resolve(parseJSON(data));
|
||||
} catch(parseErr) {
|
||||
log.warn(log._("storage.localfilesystem.invalid",{type:type}));
|
||||
return resolve(emptyResponse);
|
||||
}
|
||||
} else {
|
||||
if (type === 'flow') {
|
||||
log.info(log._("storage.localfilesystem.create",{type:type}));
|
||||
}
|
||||
resolve(emptyResponse);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
promiseDir: nodeFn.lift(fs.mkdirs),
|
||||
/**
|
||||
* Write content to a file using UTF8 encoding.
|
||||
* This forces a fsync before completing to ensure
|
||||
* the write hits disk.
|
||||
*/
|
||||
writeFile: function(path,content) {
|
||||
return when.promise(function(resolve,reject) {
|
||||
var stream = fs.createWriteStream(path);
|
||||
stream.on('open',function(fd) {
|
||||
stream.end(content,'utf8',function() {
|
||||
fs.fsync(fd,resolve);
|
||||
});
|
||||
});
|
||||
stream.on('error',function(err) {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
},
|
||||
readFile: readFile,
|
||||
|
||||
parseJSON: parseJSON
|
||||
}
|
0
red/runtime/storage/projects/index.js
Normal file
0
red/runtime/storage/projects/index.js
Normal file
@ -18,9 +18,9 @@ var should = require("should");
|
||||
var fs = require('fs-extra');
|
||||
var path = require('path');
|
||||
|
||||
var localfilesystem = require("../../../../red/runtime/storage/localfilesystem");
|
||||
var localfilesystem = require("../../../../../red/runtime/storage/localfilesystem");
|
||||
|
||||
describe('LocalFileSystem', function() {
|
||||
describe('storage/localfilesystem', function() {
|
||||
var userDir = path.join(__dirname,".testUserHome");
|
||||
var testFlow = [{"type":"tab","id":"d8be2a6d.2741d8","label":"Sheet 1"}];
|
||||
beforeEach(function(done) {
|
||||
@ -395,7 +395,6 @@ describe('LocalFileSystem', function() {
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it('should format the creds file when flowFilePretty specified',function(done) {
|
||||
var flowFile = 'test.json';
|
||||
var flowFilePath = path.join(userDir,flowFile);
|
||||
@ -424,290 +423,4 @@ describe('LocalFileSystem', function() {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle non-existent settings', function(done) {
|
||||
var settingsFile = path.join(userDir,".settings.json");
|
||||
|
||||
localfilesystem.init({userDir:userDir}).then(function() {
|
||||
fs.existsSync(settingsFile).should.be.false();
|
||||
localfilesystem.getSettings().then(function(settings) {
|
||||
settings.should.eql({});
|
||||
done();
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle corrupt settings', function(done) {
|
||||
var settingsFile = path.join(userDir,".config.json");
|
||||
fs.writeFileSync(settingsFile,"[This is not json","utf8");
|
||||
localfilesystem.init({userDir:userDir}).then(function() {
|
||||
fs.existsSync(settingsFile).should.be.true();
|
||||
localfilesystem.getSettings().then(function(settings) {
|
||||
settings.should.eql({});
|
||||
done();
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle settings', function(done) {
|
||||
var settingsFile = path.join(userDir,".config.json");
|
||||
|
||||
localfilesystem.init({userDir:userDir}).then(function() {
|
||||
fs.existsSync(settingsFile).should.be.false();
|
||||
|
||||
var settings = {"abc":{"type":"creds"}};
|
||||
|
||||
localfilesystem.saveSettings(settings).then(function() {
|
||||
fs.existsSync(settingsFile).should.be.true();
|
||||
localfilesystem.getSettings().then(function(_settings) {
|
||||
_settings.should.eql(settings);
|
||||
done();
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle non-existent sessions', function(done) {
|
||||
var sessionsFile = path.join(userDir,".sessions.json");
|
||||
|
||||
localfilesystem.init({userDir:userDir}).then(function() {
|
||||
fs.existsSync(sessionsFile).should.be.false();
|
||||
localfilesystem.getSessions().then(function(sessions) {
|
||||
sessions.should.eql({});
|
||||
done();
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle corrupt sessions', function(done) {
|
||||
var sessionsFile = path.join(userDir,".sessions.json");
|
||||
fs.writeFileSync(sessionsFile,"[This is not json","utf8");
|
||||
localfilesystem.init({userDir:userDir}).then(function() {
|
||||
fs.existsSync(sessionsFile).should.be.true();
|
||||
localfilesystem.getSessions().then(function(sessions) {
|
||||
sessions.should.eql({});
|
||||
done();
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle sessions', function(done) {
|
||||
var sessionsFile = path.join(userDir,".sessions.json");
|
||||
|
||||
localfilesystem.init({userDir:userDir}).then(function() {
|
||||
fs.existsSync(sessionsFile).should.be.false();
|
||||
|
||||
var sessions = {"abc":{"type":"creds"}};
|
||||
|
||||
localfilesystem.saveSessions(sessions).then(function() {
|
||||
fs.existsSync(sessionsFile).should.be.true();
|
||||
localfilesystem.getSessions().then(function(_sessions) {
|
||||
_sessions.should.eql(sessions);
|
||||
done();
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).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}).then(function() {
|
||||
localfilesystem.getLibraryEntry('object','').then(function(flows) {
|
||||
flows.should.eql([]);
|
||||
done();
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should return an empty list of library objects (path=/)',function(done) {
|
||||
localfilesystem.init({userDir:userDir}).then(function() {
|
||||
localfilesystem.getLibraryEntry('object','/').then(function(flows) {
|
||||
flows.should.eql([]);
|
||||
done();
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should return an error for a non-existent library object',function(done) {
|
||||
localfilesystem.init({userDir:userDir}).then(function() {
|
||||
localfilesystem.getLibraryEntry('object','A/B').then(function(flows) {
|
||||
should.fail(null,null,"non-existent flow");
|
||||
}).otherwise(function(err) {
|
||||
should.exist(err);
|
||||
done();
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
function createObjectLibrary(type) {
|
||||
type = type ||"object";
|
||||
var objLib = path.join(userDir,"lib",type);
|
||||
try {
|
||||
fs.mkdirSync(objLib);
|
||||
} catch(err) {
|
||||
}
|
||||
fs.mkdirSync(path.join(objLib,"A"));
|
||||
fs.mkdirSync(path.join(objLib,"B"));
|
||||
fs.mkdirSync(path.join(objLib,"B","C"));
|
||||
if (type === "functions" || type === "object") {
|
||||
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');
|
||||
}
|
||||
if (type === "flows" || type === "object") {
|
||||
fs.writeFileSync(path.join(objLib,"B","flow.json"),"Hi",'utf8');
|
||||
}
|
||||
}
|
||||
|
||||
it('should return a directory listing of library objects',function(done) {
|
||||
localfilesystem.init({userDir:userDir}).then(function() {
|
||||
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' }, { fn: 'flow.json' } ]);
|
||||
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);
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should load a flow library object with .json unspecified', function(done) {
|
||||
localfilesystem.init({userDir:userDir}).then(function() {
|
||||
createObjectLibrary("flows");
|
||||
localfilesystem.getLibraryEntry('flows','B/flow').then(function(flows) {
|
||||
flows.should.eql("Hi");
|
||||
done();
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
it('should return a library object',function(done) {
|
||||
localfilesystem.init({userDir:userDir}).then(function() {
|
||||
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);
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should return a newly saved library function',function(done) {
|
||||
localfilesystem.init({userDir:userDir}).then(function() {
|
||||
createObjectLibrary("functions");
|
||||
localfilesystem.getLibraryEntry('functions','B').then(function(flows) {
|
||||
flows.should.eql([ 'C', { ghi: 'jkl', fn: 'file2.js' } ]);
|
||||
var ft = path.join("B","D","file3.js");
|
||||
localfilesystem.saveLibraryEntry('functions',ft,{mno:'pqr'},"// another non meta line\n\n Hi There").then(function() {
|
||||
setTimeout(function() {
|
||||
localfilesystem.getLibraryEntry('functions',path.join("B","D")).then(function(flows) {
|
||||
flows.should.eql([ { mno: 'pqr', fn: 'file3.js' } ]);
|
||||
localfilesystem.getLibraryEntry('functions',ft).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);
|
||||
})}
|
||||
, 50);
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should return a newly saved library flow',function(done) {
|
||||
localfilesystem.init({userDir:userDir}).then(function() {
|
||||
createObjectLibrary("flows");
|
||||
localfilesystem.getLibraryEntry('flows','B').then(function(flows) {
|
||||
flows.should.eql([ 'C', {fn:'flow.json'} ]);
|
||||
var ft = path.join("B","D","file3");
|
||||
localfilesystem.saveLibraryEntry('flows',ft,{mno:'pqr'},"Hi").then(function() {
|
||||
setTimeout(function() {
|
||||
localfilesystem.getLibraryEntry('flows',path.join("B","D")).then(function(flows) {
|
||||
flows.should.eql([ { mno: 'pqr', fn: 'file3.json' } ]);
|
||||
localfilesystem.getLibraryEntry('flows',ft+".json").then(function(body) {
|
||||
body.should.eql("Hi");
|
||||
done();
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
})}
|
||||
, 50);
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
205
test/red/runtime/storage/localfilesystem/library_spec.js
Normal file
205
test/red/runtime/storage/localfilesystem/library_spec.js
Normal file
@ -0,0 +1,205 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var fs = require('fs-extra');
|
||||
var path = require('path');
|
||||
|
||||
var localfilesystemLibrary = require("../../../../../red/runtime/storage/localfilesystem/library");
|
||||
|
||||
describe('storage/localfilesystem/library', function() {
|
||||
var userDir = path.join(__dirname,".testUserHome");
|
||||
beforeEach(function(done) {
|
||||
fs.remove(userDir,function(err) {
|
||||
fs.mkdir(userDir,done);
|
||||
});
|
||||
});
|
||||
afterEach(function(done) {
|
||||
fs.remove(userDir,done);
|
||||
});
|
||||
|
||||
it('should return an empty list of library objects',function(done) {
|
||||
localfilesystemLibrary.init({userDir:userDir}).then(function() {
|
||||
localfilesystemLibrary.getLibraryEntry('object','').then(function(flows) {
|
||||
flows.should.eql([]);
|
||||
done();
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should return an empty list of library objects (path=/)',function(done) {
|
||||
localfilesystemLibrary.init({userDir:userDir}).then(function() {
|
||||
localfilesystemLibrary.getLibraryEntry('object','/').then(function(flows) {
|
||||
flows.should.eql([]);
|
||||
done();
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should return an error for a non-existent library object',function(done) {
|
||||
localfilesystemLibrary.init({userDir:userDir}).then(function() {
|
||||
localfilesystemLibrary.getLibraryEntry('object','A/B').then(function(flows) {
|
||||
should.fail(null,null,"non-existent flow");
|
||||
}).otherwise(function(err) {
|
||||
should.exist(err);
|
||||
done();
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
function createObjectLibrary(type) {
|
||||
type = type ||"object";
|
||||
var objLib = path.join(userDir,"lib",type);
|
||||
try {
|
||||
fs.mkdirSync(objLib);
|
||||
} catch(err) {
|
||||
}
|
||||
fs.mkdirSync(path.join(objLib,"A"));
|
||||
fs.mkdirSync(path.join(objLib,"B"));
|
||||
fs.mkdirSync(path.join(objLib,"B","C"));
|
||||
if (type === "functions" || type === "object") {
|
||||
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');
|
||||
}
|
||||
if (type === "flows" || type === "object") {
|
||||
fs.writeFileSync(path.join(objLib,"B","flow.json"),"Hi",'utf8');
|
||||
}
|
||||
}
|
||||
|
||||
it('should return a directory listing of library objects',function(done) {
|
||||
localfilesystemLibrary.init({userDir:userDir}).then(function() {
|
||||
createObjectLibrary();
|
||||
|
||||
localfilesystemLibrary.getLibraryEntry('object','').then(function(flows) {
|
||||
flows.should.eql([ 'A', 'B', { abc: 'def', fn: 'file1.js' } ]);
|
||||
localfilesystemLibrary.getLibraryEntry('object','B').then(function(flows) {
|
||||
flows.should.eql([ 'C', { ghi: 'jkl', fn: 'file2.js' }, { fn: 'flow.json' } ]);
|
||||
localfilesystemLibrary.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);
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should load a flow library object with .json unspecified', function(done) {
|
||||
localfilesystemLibrary.init({userDir:userDir}).then(function() {
|
||||
createObjectLibrary("flows");
|
||||
localfilesystemLibrary.getLibraryEntry('flows','B/flow').then(function(flows) {
|
||||
flows.should.eql("Hi");
|
||||
done();
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
it('should return a library object',function(done) {
|
||||
localfilesystemLibrary.init({userDir:userDir}).then(function() {
|
||||
createObjectLibrary();
|
||||
localfilesystemLibrary.getLibraryEntry('object','B/file2.js').then(function(body) {
|
||||
body.should.eql("// not a metaline \n\n Hi");
|
||||
done();
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should return a newly saved library function',function(done) {
|
||||
localfilesystemLibrary.init({userDir:userDir}).then(function() {
|
||||
createObjectLibrary("functions");
|
||||
localfilesystemLibrary.getLibraryEntry('functions','B').then(function(flows) {
|
||||
flows.should.eql([ 'C', { ghi: 'jkl', fn: 'file2.js' } ]);
|
||||
var ft = path.join("B","D","file3.js");
|
||||
localfilesystemLibrary.saveLibraryEntry('functions',ft,{mno:'pqr'},"// another non meta line\n\n Hi There").then(function() {
|
||||
setTimeout(function() {
|
||||
localfilesystemLibrary.getLibraryEntry('functions',path.join("B","D")).then(function(flows) {
|
||||
flows.should.eql([ { mno: 'pqr', fn: 'file3.js' } ]);
|
||||
localfilesystemLibrary.getLibraryEntry('functions',ft).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);
|
||||
})
|
||||
}, 50);
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should return a newly saved library flow',function(done) {
|
||||
localfilesystemLibrary.init({userDir:userDir}).then(function() {
|
||||
createObjectLibrary("flows");
|
||||
localfilesystemLibrary.getLibraryEntry('flows','B').then(function(flows) {
|
||||
flows.should.eql([ 'C', {fn:'flow.json'} ]);
|
||||
var ft = path.join("B","D","file3");
|
||||
localfilesystemLibrary.saveLibraryEntry('flows',ft,{mno:'pqr'},"Hi").then(function() {
|
||||
setTimeout(function() {
|
||||
localfilesystemLibrary.getLibraryEntry('flows',path.join("B","D")).then(function(flows) {
|
||||
flows.should.eql([ { mno: 'pqr', fn: 'file3.json' } ]);
|
||||
localfilesystemLibrary.getLibraryEntry('flows',ft+".json").then(function(body) {
|
||||
body.should.eql("Hi");
|
||||
done();
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
})
|
||||
}, 50);
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
});
|
79
test/red/runtime/storage/localfilesystem/sessions_spec.js
Normal file
79
test/red/runtime/storage/localfilesystem/sessions_spec.js
Normal file
@ -0,0 +1,79 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var fs = require('fs-extra');
|
||||
var path = require('path');
|
||||
|
||||
var localfilesystemSessions = require("../../../../../red/runtime/storage/localfilesystem/sessions");
|
||||
|
||||
describe('storage/localfilesystem/sessions', function() {
|
||||
var userDir = path.join(__dirname,".testUserHome");
|
||||
beforeEach(function(done) {
|
||||
fs.remove(userDir,function(err) {
|
||||
fs.mkdir(userDir,done);
|
||||
});
|
||||
});
|
||||
afterEach(function(done) {
|
||||
fs.remove(userDir,done);
|
||||
});
|
||||
it('should handle non-existent sessions', function(done) {
|
||||
var sessionsFile = path.join(userDir,".sessions.json");
|
||||
|
||||
localfilesystemSessions.init({userDir:userDir});
|
||||
fs.existsSync(sessionsFile).should.be.false();
|
||||
localfilesystemSessions.getSessions().then(function(sessions) {
|
||||
sessions.should.eql({});
|
||||
done();
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle corrupt sessions', function(done) {
|
||||
var sessionsFile = path.join(userDir,".sessions.json");
|
||||
fs.writeFileSync(sessionsFile,"[This is not json","utf8");
|
||||
localfilesystemSessions.init({userDir:userDir});
|
||||
fs.existsSync(sessionsFile).should.be.true();
|
||||
localfilesystemSessions.getSessions().then(function(sessions) {
|
||||
sessions.should.eql({});
|
||||
done();
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle sessions', function(done) {
|
||||
var sessionsFile = path.join(userDir,".sessions.json");
|
||||
|
||||
localfilesystemSessions.init({userDir:userDir});
|
||||
fs.existsSync(sessionsFile).should.be.false();
|
||||
|
||||
var sessions = {"abc":{"type":"creds"}};
|
||||
|
||||
localfilesystemSessions.saveSessions(sessions).then(function() {
|
||||
fs.existsSync(sessionsFile).should.be.true();
|
||||
localfilesystemSessions.getSessions().then(function(_sessions) {
|
||||
_sessions.should.eql(sessions);
|
||||
done();
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
});
|
80
test/red/runtime/storage/localfilesystem/settings_spec.js
Normal file
80
test/red/runtime/storage/localfilesystem/settings_spec.js
Normal file
@ -0,0 +1,80 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var fs = require('fs-extra');
|
||||
var path = require('path');
|
||||
|
||||
var localfilesystemSettings = require("../../../../../red/runtime/storage/localfilesystem/settings");
|
||||
|
||||
describe('storage/localfilesystem/settings', function() {
|
||||
var userDir = path.join(__dirname,".testUserHome");
|
||||
beforeEach(function(done) {
|
||||
fs.remove(userDir,function(err) {
|
||||
fs.mkdir(userDir,done);
|
||||
});
|
||||
});
|
||||
afterEach(function(done) {
|
||||
fs.remove(userDir,done);
|
||||
});
|
||||
|
||||
it('should handle non-existent settings', function(done) {
|
||||
var settingsFile = path.join(userDir,".settings.json");
|
||||
|
||||
localfilesystemSettings.init({userDir:userDir});
|
||||
fs.existsSync(settingsFile).should.be.false();
|
||||
localfilesystemSettings.getSettings().then(function(settings) {
|
||||
settings.should.eql({});
|
||||
done();
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle corrupt settings', function(done) {
|
||||
var settingsFile = path.join(userDir,".config.json");
|
||||
fs.writeFileSync(settingsFile,"[This is not json","utf8");
|
||||
localfilesystemSettings.init({userDir:userDir});
|
||||
fs.existsSync(settingsFile).should.be.true();
|
||||
localfilesystemSettings.getSettings().then(function(settings) {
|
||||
settings.should.eql({});
|
||||
done();
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle settings', function(done) {
|
||||
var settingsFile = path.join(userDir,".config.json");
|
||||
|
||||
localfilesystemSettings.init({userDir:userDir});
|
||||
fs.existsSync(settingsFile).should.be.false();
|
||||
|
||||
var settings = {"abc":{"type":"creds"}};
|
||||
|
||||
localfilesystemSettings.saveSettings(settings).then(function() {
|
||||
fs.existsSync(settingsFile).should.be.true();
|
||||
localfilesystemSettings.getSettings().then(function(_settings) {
|
||||
_settings.should.eql(settings);
|
||||
done();
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).otherwise(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
});
|
31
test/red/runtime/storage/localfilesystem/util_spec.js
Normal file
31
test/red/runtime/storage/localfilesystem/util_spec.js
Normal file
@ -0,0 +1,31 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var util = require("../../../../../red/runtime/storage/localfilesystem/util");
|
||||
|
||||
describe('storage/localfilesystem/util', function() {
|
||||
describe('parseJSON', function() {
|
||||
it('returns parsed JSON', function() {
|
||||
var result = util.parseJSON('{"a":123}');
|
||||
result.should.eql({a:123});
|
||||
})
|
||||
it('ignores BOM character', function() {
|
||||
var result = util.parseJSON('\uFEFF{"a":123}');
|
||||
result.should.eql({a:123});
|
||||
})
|
||||
})
|
||||
});
|
Loading…
Reference in New Issue
Block a user