mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
WIP: separate runtime and api components
This commit is contained in:
191
red/runtime/storage/index.js
Normal file
191
red/runtime/storage/index.js
Normal file
@@ -0,0 +1,191 @@
|
||||
/**
|
||||
* Copyright 2013, 2015 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 when = require('when');
|
||||
var Path = require('path');
|
||||
var log = require("../log");
|
||||
|
||||
var storageModule;
|
||||
var settingsAvailable;
|
||||
var sessionsAvailable;
|
||||
|
||||
function moduleSelector(aSettings) {
|
||||
var toReturn;
|
||||
if (aSettings.storageModule) {
|
||||
if (typeof aSettings.storageModule === "string") {
|
||||
// TODO: allow storage modules to be specified by absolute path
|
||||
toReturn = require("./"+aSettings.storageModule);
|
||||
} else {
|
||||
toReturn = aSettings.storageModule;
|
||||
}
|
||||
} else {
|
||||
toReturn = require("./localfilesystem");
|
||||
}
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
function is_malicious(path) {
|
||||
return path.indexOf('../') != -1 || path.indexOf('..\\') != -1;
|
||||
}
|
||||
|
||||
var storageModuleInterface = {
|
||||
init: function(settings) {
|
||||
try {
|
||||
storageModule = moduleSelector(settings);
|
||||
settingsAvailable = storageModule.hasOwnProperty("getSettings") && storageModule.hasOwnProperty("saveSettings");
|
||||
sessionsAvailable = storageModule.hasOwnProperty("getSessions") && storageModule.hasOwnProperty("saveSessions");
|
||||
} catch (e) {
|
||||
return when.reject(e);
|
||||
}
|
||||
return storageModule.init(settings);
|
||||
},
|
||||
getFlows: function() {
|
||||
return storageModule.getFlows();
|
||||
},
|
||||
saveFlows: function(flows) {
|
||||
return storageModule.saveFlows(flows);
|
||||
},
|
||||
getCredentials: function() {
|
||||
return storageModule.getCredentials();
|
||||
},
|
||||
saveCredentials: function(credentials) {
|
||||
return storageModule.saveCredentials(credentials);
|
||||
},
|
||||
getSettings: function() {
|
||||
if (settingsAvailable) {
|
||||
return storageModule.getSettings();
|
||||
} else {
|
||||
return when.resolve(null);
|
||||
}
|
||||
},
|
||||
saveSettings: function(settings) {
|
||||
if (settingsAvailable) {
|
||||
return storageModule.saveSettings(settings);
|
||||
} else {
|
||||
return when.resolve();
|
||||
}
|
||||
},
|
||||
getSessions: function() {
|
||||
if (sessionsAvailable) {
|
||||
return storageModule.getSessions();
|
||||
} else {
|
||||
return when.resolve(null);
|
||||
}
|
||||
},
|
||||
saveSessions: function(sessions) {
|
||||
if (sessionsAvailable) {
|
||||
return storageModule.saveSessions(sessions);
|
||||
} else {
|
||||
return when.resolve();
|
||||
}
|
||||
},
|
||||
|
||||
/* Library Functions */
|
||||
|
||||
getLibraryEntry: function(type, path) {
|
||||
if (is_malicious(path)) {
|
||||
var err = new Error();
|
||||
err.code = "forbidden";
|
||||
return when.reject(err);
|
||||
}
|
||||
return storageModule.getLibraryEntry(type, path);
|
||||
},
|
||||
saveLibraryEntry: function(type, path, meta, body) {
|
||||
if (is_malicious(path)) {
|
||||
var err = new Error();
|
||||
err.code = "forbidden";
|
||||
return when.reject(err);
|
||||
}
|
||||
return storageModule.saveLibraryEntry(type, path, meta, body);
|
||||
},
|
||||
|
||||
/* Deprecated functions */
|
||||
getAllFlows: function() {
|
||||
if (storageModule.hasOwnProperty("getAllFlows")) {
|
||||
return storageModule.getAllFlows();
|
||||
} else {
|
||||
return listFlows("/");
|
||||
}
|
||||
},
|
||||
getFlow: function(fn) {
|
||||
if (is_malicious(fn)) {
|
||||
var err = new Error();
|
||||
err.code = "forbidden";
|
||||
return when.reject(err);
|
||||
}
|
||||
if (storageModule.hasOwnProperty("getFlow")) {
|
||||
return storageModule.getFlow(fn);
|
||||
} else {
|
||||
return storageModule.getLibraryEntry("flows",fn);
|
||||
}
|
||||
|
||||
},
|
||||
saveFlow: function(fn, data) {
|
||||
if (is_malicious(fn)) {
|
||||
var err = new Error();
|
||||
err.code = "forbidden";
|
||||
return when.reject(err);
|
||||
}
|
||||
if (storageModule.hasOwnProperty("saveFlow")) {
|
||||
return storageModule.saveFlow(fn, data);
|
||||
} else {
|
||||
return storageModule.saveLibraryEntry("flows",fn,{},data);
|
||||
}
|
||||
}
|
||||
/* End deprecated functions */
|
||||
|
||||
}
|
||||
|
||||
|
||||
function listFlows(path) {
|
||||
return storageModule.getLibraryEntry("flows",path).then(function(res) {
|
||||
return when.promise(function(resolve) {
|
||||
var promises = [];
|
||||
res.forEach(function(r) {
|
||||
if (typeof r === "string") {
|
||||
promises.push(listFlows(Path.join(path,r)));
|
||||
} else {
|
||||
promises.push(when.resolve(r));
|
||||
}
|
||||
});
|
||||
var i=0;
|
||||
when.settle(promises).then(function(res2) {
|
||||
var result = {};
|
||||
res2.forEach(function(r) {
|
||||
// TODO: name||fn
|
||||
if (r.value.fn) {
|
||||
var name = r.value.name;
|
||||
if (!name) {
|
||||
name = r.value.fn.split(".")[0];
|
||||
}
|
||||
result.f = result.f || [];
|
||||
result.f.push(name);
|
||||
} else {
|
||||
result.d = result.d || {};
|
||||
result.d[res[i]] = r.value;
|
||||
//console.log(">",r.value);
|
||||
}
|
||||
i++;
|
||||
});
|
||||
resolve(result);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
module.exports = storageModuleInterface;
|
372
red/runtime/storage/localfilesystem.js
Normal file
372
red/runtime/storage/localfilesystem.js
Normal file
@@ -0,0 +1,372 @@
|
||||
/**
|
||||
* Copyright 2013, 2014 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-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 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);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
var localfilesystem = {
|
||||
init: function(_settings) {
|
||||
settings = _settings;
|
||||
|
||||
var promises = [];
|
||||
|
||||
if (!settings.userDir) {
|
||||
if (fs.existsSync(fspath.join(process.env.NODE_RED_HOME,".config.json"))) {
|
||||
settings.userDir = process.env.NODE_RED_HOME;
|
||||
} else {
|
||||
settings.userDir = fspath.join(process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE || process.env.NODE_RED_HOME,".node-red");
|
||||
if (!settings.readOnly) {
|
||||
promises.push(promiseDir(settings.userDir));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
if (fs.existsSync(fspath.join(process.cwd(),flowsFile))) {
|
||||
// Found in cwd
|
||||
flowsFullPath = fspath.join(process.cwd(),flowsFile);
|
||||
} else {
|
||||
// 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");
|
||||
|
||||
if (!settings.readOnly) {
|
||||
promises.push(promiseDir(libFlowsDir));
|
||||
}
|
||||
|
||||
return when.all(promises);
|
||||
},
|
||||
|
||||
getFlows: function() {
|
||||
return when.promise(function(resolve) {
|
||||
log.info(log._("storage.localfilesystem.user-dir",{path:settings.userDir}));
|
||||
log.info(log._("storage.localfilesystem.flows-file",{path:flowsFullPath}));
|
||||
fs.exists(flowsFullPath, function(exists) {
|
||||
if (exists) {
|
||||
resolve(nodeFn.call(fs.readFile,flowsFullPath,'utf8').then(function(data) {
|
||||
return JSON.parse(data);
|
||||
}));
|
||||
} else {
|
||||
log.info(log._("storage.localfilesystem.create"));
|
||||
resolve([]);
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
saveFlows: function(flows) {
|
||||
if (settings.readOnly) {
|
||||
return when.resolve();
|
||||
}
|
||||
|
||||
if (fs.existsSync(flowsFullPath)) {
|
||||
fs.renameSync(flowsFullPath,flowsFileBackup);
|
||||
}
|
||||
|
||||
var flowData;
|
||||
|
||||
if (settings.flowFilePretty) {
|
||||
flowData = JSON.stringify(flows,null,4);
|
||||
} else {
|
||||
flowData = JSON.stringify(flows);
|
||||
}
|
||||
return writeFile(flowsFullPath, flowData);
|
||||
},
|
||||
|
||||
getCredentials: function() {
|
||||
return when.promise(function(resolve) {
|
||||
fs.exists(credentialsFile, function(exists) {
|
||||
if (exists) {
|
||||
resolve(nodeFn.call(fs.readFile, credentialsFile, 'utf8').then(function(data) {
|
||||
return JSON.parse(data)
|
||||
}));
|
||||
} else {
|
||||
fs.exists(oldCredentialsFile, function(exists) {
|
||||
if (exists) {
|
||||
resolve(nodeFn.call(fs.readFile, oldCredentialsFile, 'utf8').then(function(data) {
|
||||
return JSON.parse(data)
|
||||
}));
|
||||
} else {
|
||||
resolve({});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
saveCredentials: function(credentials) {
|
||||
if (settings.readOnly) {
|
||||
return when.resolve();
|
||||
}
|
||||
|
||||
if (fs.existsSync(credentialsFile)) {
|
||||
fs.renameSync(credentialsFile,credentialsFileBackup);
|
||||
}
|
||||
var credentialData;
|
||||
if (settings.flowFilePretty) {
|
||||
credentialData = JSON.stringify(credentials,null,4);
|
||||
} else {
|
||||
credentialData = JSON.stringify(credentials);
|
||||
}
|
||||
return writeFile(credentialsFile, credentialData);
|
||||
},
|
||||
|
||||
getSettings: function() {
|
||||
if (fs.existsSync(globalSettingsFile)) {
|
||||
return nodeFn.call(fs.readFile,globalSettingsFile,'utf8').then(function(data) {
|
||||
if (data) {
|
||||
try {
|
||||
return JSON.parse(data);
|
||||
} catch(err) {
|
||||
log.trace("Corrupted config detected - resetting");
|
||||
return {};
|
||||
}
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
});
|
||||
}
|
||||
return when.resolve({});
|
||||
},
|
||||
saveSettings: function(settings) {
|
||||
if (settings.readOnly) {
|
||||
return when.resolve();
|
||||
}
|
||||
return writeFile(globalSettingsFile,JSON.stringify(settings,null,1));
|
||||
},
|
||||
getSessions: function() {
|
||||
if (fs.existsSync(sessionsFile)) {
|
||||
return nodeFn.call(fs.readFile,sessionsFile,'utf8').then(function(data) {
|
||||
if (data) {
|
||||
try {
|
||||
return JSON.parse(data);
|
||||
} catch(err) {
|
||||
log.trace("Corrupted sessions file - resetting");
|
||||
return {};
|
||||
}
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
});
|
||||
}
|
||||
return when.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);
|
||||
return promiseDir(root).then(function () {
|
||||
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 (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();
|
||||
}
|
||||
var fn = fspath.join(libDir, type, path);
|
||||
var headers = "";
|
||||
for (var i in meta) {
|
||||
if (meta.hasOwnProperty(i)) {
|
||||
headers += "// "+i+": "+meta[i]+"\n";
|
||||
}
|
||||
}
|
||||
return promiseDir(fspath.dirname(fn)).then(function () {
|
||||
writeFile(fn,headers+body);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = localfilesystem;
|
Reference in New Issue
Block a user