2018-03-15 21:52:17 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
**/
|
|
|
|
|
2018-07-02 23:32:20 +02:00
|
|
|
/**
|
|
|
|
* Local file-system based context storage
|
|
|
|
*
|
|
|
|
* Configuration options:
|
|
|
|
* {
|
2018-08-09 15:39:20 +02:00
|
|
|
* base: "context", // the base directory to use
|
|
|
|
* // default: "context"
|
2018-07-02 23:32:20 +02:00
|
|
|
* dir: "/path/to/storage", // the directory to create the base directory in
|
|
|
|
* // default: settings.userDir
|
2018-08-09 15:39:20 +02:00
|
|
|
* cache: true, // whether to cache contents in memory
|
2018-07-02 23:32:20 +02:00
|
|
|
* // default: true
|
2018-08-09 15:39:20 +02:00
|
|
|
* flushInterval: 30 // if cache is enabled, the minimum interval
|
|
|
|
* // between writes to storage, in seconds. This
|
|
|
|
* can be used to reduce wear on underlying storage.
|
|
|
|
* default: 30 seconds
|
2018-07-02 23:32:20 +02:00
|
|
|
* }
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* $HOME/.node-red/contexts
|
|
|
|
* ├── global
|
|
|
|
* │ └── global_context.json
|
|
|
|
* ├── <id of Flow 1>
|
|
|
|
* │ ├── flow_context.json
|
|
|
|
* │ ├── <id of Node a>.json
|
|
|
|
* │ └── <id of Node b>.json
|
|
|
|
* └── <id of Flow 2>
|
|
|
|
* ├── flow_context.json
|
|
|
|
* ├── <id of Node x>.json
|
|
|
|
* └── <id of Node y>.json
|
|
|
|
*/
|
|
|
|
|
2018-03-15 21:52:17 +01:00
|
|
|
var fs = require('fs-extra');
|
2018-03-23 09:18:56 +01:00
|
|
|
var path = require("path");
|
2018-08-16 00:12:51 +02:00
|
|
|
var util = require("@node-red/util").util;
|
|
|
|
var log = require("@node-red/util").log;
|
2018-03-15 21:52:17 +01:00
|
|
|
|
2018-08-15 11:19:37 +02:00
|
|
|
var safeJSONStringify = require("json-stringify-safe");
|
2018-07-02 23:32:20 +02:00
|
|
|
var MemoryStore = require("./memory");
|
|
|
|
|
2018-08-15 11:19:37 +02:00
|
|
|
|
2018-06-07 13:17:51 +02:00
|
|
|
function getStoragePath(storageBaseDir, scope) {
|
2018-05-30 03:24:27 +02:00
|
|
|
if(scope.indexOf(":") === -1){
|
2018-03-23 09:18:56 +01:00
|
|
|
if(scope === "global"){
|
2018-06-08 12:26:07 +02:00
|
|
|
return path.join(storageBaseDir,"global",scope);
|
2018-03-23 09:18:56 +01:00
|
|
|
}else{ // scope:flow
|
2018-06-07 13:17:51 +02:00
|
|
|
return path.join(storageBaseDir,scope,"flow");
|
2018-03-23 09:18:56 +01:00
|
|
|
}
|
|
|
|
}else{ // scope:local
|
|
|
|
var ids = scope.split(":")
|
2018-06-07 13:17:51 +02:00
|
|
|
return path.join(storageBaseDir,ids[1],ids[0]);
|
2018-03-23 09:18:56 +01:00
|
|
|
}
|
2018-03-15 21:52:17 +01:00
|
|
|
}
|
|
|
|
|
2018-06-07 13:17:51 +02:00
|
|
|
function getBasePath(config) {
|
2018-08-09 15:39:20 +02:00
|
|
|
var base = config.base || "context";
|
2018-05-24 14:42:40 +02:00
|
|
|
var storageBaseDir;
|
|
|
|
if (!config.dir) {
|
|
|
|
if(config.settings && config.settings.userDir){
|
|
|
|
storageBaseDir = path.join(config.settings.userDir, base);
|
|
|
|
}else{
|
|
|
|
try {
|
|
|
|
fs.statSync(path.join(process.env.NODE_RED_HOME,".config.json"));
|
|
|
|
storageBaseDir = path.join(process.env.NODE_RED_HOME, base);
|
|
|
|
} catch(err) {
|
2018-03-15 21:52:17 +01:00
|
|
|
try {
|
2018-05-24 14:42:40 +02:00
|
|
|
// Consider compatibility for older versions
|
|
|
|
if (process.env.HOMEPATH) {
|
|
|
|
fs.statSync(path.join(process.env.HOMEPATH,".node-red",".config.json"));
|
|
|
|
storageBaseDir = path.join(process.env.HOMEPATH, ".node-red", base);
|
2018-03-23 09:18:56 +01:00
|
|
|
}
|
2018-05-24 14:42:40 +02:00
|
|
|
} catch(err) {
|
|
|
|
}
|
|
|
|
if (!storageBaseDir) {
|
|
|
|
storageBaseDir = path.join(process.env.HOME || process.env.USERPROFILE || process.env.HOMEPATH || process.env.NODE_RED_HOME,".node-red", base);
|
2018-03-15 21:52:17 +01:00
|
|
|
}
|
2018-03-23 09:18:56 +01:00
|
|
|
}
|
2018-03-15 21:52:17 +01:00
|
|
|
}
|
2018-05-24 14:42:40 +02:00
|
|
|
}else{
|
|
|
|
storageBaseDir = path.join(config.dir, base);
|
|
|
|
}
|
|
|
|
return storageBaseDir;
|
|
|
|
}
|
2018-03-15 21:52:17 +01:00
|
|
|
|
2018-06-07 13:17:51 +02:00
|
|
|
function loadFile(storagePath){
|
|
|
|
return fs.pathExists(storagePath).then(function(exists){
|
|
|
|
if(exists === true){
|
|
|
|
return fs.readFile(storagePath, "utf8");
|
|
|
|
}else{
|
2018-06-20 12:50:55 +02:00
|
|
|
return Promise.resolve(undefined);
|
2018-06-07 13:17:51 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-09 15:39:20 +02:00
|
|
|
function listFiles(storagePath) {
|
|
|
|
var promises = [];
|
|
|
|
return fs.readdir(storagePath).then(function(files) {
|
|
|
|
files.forEach(function(file) {
|
2018-08-16 15:36:11 +02:00
|
|
|
if (!/^\./.test(file)) {
|
|
|
|
var fullPath = path.join(storagePath,file);
|
|
|
|
var stats = fs.statSync(fullPath);
|
|
|
|
if (stats.isDirectory()) {
|
|
|
|
promises.push(fs.readdir(fullPath).then(function(subdirFiles) {
|
|
|
|
var result = [];
|
|
|
|
subdirFiles.forEach(subfile => {
|
|
|
|
if (/\.json$/.test(subfile)) {
|
|
|
|
result.push(path.join(file,subfile))
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
2018-08-09 15:39:20 +02:00
|
|
|
});
|
|
|
|
return Promise.all(promises);
|
|
|
|
}).then(dirs => dirs.reduce((acc, val) => acc.concat(val), []));
|
|
|
|
}
|
|
|
|
|
2018-08-15 11:19:37 +02:00
|
|
|
function stringify(value) {
|
|
|
|
var hasCircular;
|
|
|
|
var result = safeJSONStringify(value,null,4,function(k,v){hasCircular = true})
|
|
|
|
return { json: result, circular: hasCircular };
|
|
|
|
}
|
|
|
|
|
2019-08-21 17:54:26 +02:00
|
|
|
|
|
|
|
function writeFileAtomic(storagePath, content) {
|
|
|
|
// To protect against file corruption, write to a tmp file first and then
|
|
|
|
// rename to the destination file
|
|
|
|
let finalFile = storagePath + ".json";
|
|
|
|
let tmpFile = finalFile + "."+Date.now()+".tmp";
|
|
|
|
return fs.outputFile(tmpFile, content, "utf8").then(function() {
|
|
|
|
return fs.rename(tmpFile,finalFile);
|
|
|
|
})
|
|
|
|
}
|
2018-05-24 14:42:40 +02:00
|
|
|
function LocalFileSystem(config){
|
|
|
|
this.config = config;
|
2018-06-07 13:17:51 +02:00
|
|
|
this.storageBaseDir = getBasePath(this.config);
|
2018-09-17 11:31:00 +02:00
|
|
|
this.writePromise = Promise.resolve();
|
2018-07-02 23:32:20 +02:00
|
|
|
if (config.hasOwnProperty('cache')?config.cache:true) {
|
|
|
|
this.cache = MemoryStore({});
|
|
|
|
}
|
2018-08-09 15:39:20 +02:00
|
|
|
this.pendingWrites = {};
|
2018-08-15 11:19:37 +02:00
|
|
|
this.knownCircularRefs = {};
|
|
|
|
|
2018-08-09 15:39:20 +02:00
|
|
|
if (config.hasOwnProperty('flushInterval')) {
|
|
|
|
this.flushInterval = Math.max(0,config.flushInterval) * 1000;
|
|
|
|
} else {
|
|
|
|
this.flushInterval = 30000;
|
|
|
|
}
|
2018-05-24 14:42:40 +02:00
|
|
|
}
|
|
|
|
|
2018-05-30 03:24:27 +02:00
|
|
|
LocalFileSystem.prototype.open = function(){
|
2018-07-02 23:32:20 +02:00
|
|
|
var self = this;
|
|
|
|
if (this.cache) {
|
|
|
|
var scopes = [];
|
|
|
|
var promises = [];
|
2018-08-09 15:39:20 +02:00
|
|
|
return listFiles(self.storageBaseDir).then(function(files) {
|
|
|
|
files.forEach(function(file) {
|
2018-08-15 16:31:42 +02:00
|
|
|
var parts = file.split(path.sep);
|
2018-08-09 15:39:20 +02:00
|
|
|
if (parts[0] === 'global') {
|
|
|
|
scopes.push("global");
|
|
|
|
} else if (parts[1] === 'flow.json') {
|
|
|
|
scopes.push(parts[0])
|
|
|
|
} else {
|
|
|
|
scopes.push(parts[1].substring(0,parts[1].length-5)+":"+parts[0]);
|
|
|
|
}
|
|
|
|
promises.push(loadFile(path.join(self.storageBaseDir,file)));
|
2018-07-02 23:32:20 +02:00
|
|
|
})
|
|
|
|
return Promise.all(promises);
|
|
|
|
}).then(function(res) {
|
|
|
|
scopes.forEach(function(scope,i) {
|
|
|
|
var data = res[i]?JSON.parse(res[i]):{};
|
|
|
|
Object.keys(data).forEach(function(key) {
|
|
|
|
self.cache.set(scope,key,data[key]);
|
|
|
|
})
|
|
|
|
});
|
2018-07-10 13:41:16 +02:00
|
|
|
}).catch(function(err){
|
|
|
|
if(err.code == 'ENOENT') {
|
2018-07-12 11:20:47 +02:00
|
|
|
return fs.ensureDir(self.storageBaseDir);
|
2018-07-10 13:41:16 +02:00
|
|
|
}else{
|
2018-08-16 15:36:11 +02:00
|
|
|
throw err;
|
2018-07-10 13:41:16 +02:00
|
|
|
}
|
2018-08-09 15:39:20 +02:00
|
|
|
}).then(function() {
|
|
|
|
self._flushPendingWrites = function() {
|
|
|
|
var scopes = Object.keys(self.pendingWrites);
|
|
|
|
self.pendingWrites = {};
|
|
|
|
var promises = [];
|
|
|
|
var newContext = self.cache._export();
|
|
|
|
scopes.forEach(function(scope) {
|
|
|
|
var storagePath = getStoragePath(self.storageBaseDir,scope);
|
2020-04-08 12:32:39 +02:00
|
|
|
var context = newContext[scope] || {};
|
2018-08-15 11:19:37 +02:00
|
|
|
var stringifiedContext = stringify(context);
|
|
|
|
if (stringifiedContext.circular && !self.knownCircularRefs[scope]) {
|
2020-04-08 12:32:39 +02:00
|
|
|
log.warn(log._("context.localfilesystem.error-circular",{scope:scope}));
|
2018-08-15 11:19:37 +02:00
|
|
|
self.knownCircularRefs[scope] = true;
|
|
|
|
} else {
|
|
|
|
delete self.knownCircularRefs[scope];
|
|
|
|
}
|
|
|
|
log.debug("Flushing localfilesystem context scope "+scope);
|
2019-08-21 17:54:26 +02:00
|
|
|
promises.push(writeFileAtomic(storagePath, stringifiedContext.json))
|
2018-08-09 15:39:20 +02:00
|
|
|
});
|
|
|
|
delete self._pendingWriteTimeout;
|
|
|
|
return Promise.all(promises);
|
|
|
|
}
|
2018-07-10 13:41:16 +02:00
|
|
|
});
|
2018-07-02 23:32:20 +02:00
|
|
|
} else {
|
2018-12-13 13:46:19 +01:00
|
|
|
self._flushPendingWrites = function() { }
|
2018-08-09 15:39:20 +02:00
|
|
|
return fs.ensureDir(self.storageBaseDir);
|
2018-07-02 23:32:20 +02:00
|
|
|
}
|
2018-05-30 03:24:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
LocalFileSystem.prototype.close = function(){
|
2018-09-16 22:15:23 +02:00
|
|
|
var self = this;
|
|
|
|
if (this.cache && this._pendingWriteTimeout) {
|
2018-08-09 15:39:20 +02:00
|
|
|
clearTimeout(this._pendingWriteTimeout);
|
|
|
|
delete this._pendingWriteTimeout;
|
2018-09-16 22:15:23 +02:00
|
|
|
this.flushInterval = 0;
|
2018-12-13 13:46:19 +01:00
|
|
|
self.writePromise = self.writePromise.then(function(){
|
|
|
|
return self._flushPendingWrites.call(self).catch(function(err) {
|
|
|
|
log.error(log._("context.localfilesystem.error-write",{message:err.toString()}));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-08-09 15:39:20 +02:00
|
|
|
}
|
2018-09-17 11:31:00 +02:00
|
|
|
return this.writePromise;
|
2018-05-30 03:24:27 +02:00
|
|
|
}
|
|
|
|
|
2018-06-22 10:11:54 +02:00
|
|
|
LocalFileSystem.prototype.get = function(scope, key, callback) {
|
2018-07-02 23:32:20 +02:00
|
|
|
if (this.cache) {
|
|
|
|
return this.cache.get(scope,key,callback);
|
|
|
|
}
|
2018-06-22 10:11:54 +02:00
|
|
|
if(typeof callback !== "function"){
|
2020-02-07 15:26:30 +01:00
|
|
|
throw new Error("File Store cache disabled - only asynchronous access supported");
|
2018-06-22 10:11:54 +02:00
|
|
|
}
|
2018-06-07 13:17:51 +02:00
|
|
|
var storagePath = getStoragePath(this.storageBaseDir ,scope);
|
2018-06-22 10:11:54 +02:00
|
|
|
loadFile(storagePath + ".json").then(function(data){
|
2018-09-10 00:47:31 +02:00
|
|
|
var value;
|
2018-06-07 13:17:51 +02:00
|
|
|
if(data){
|
2018-07-23 14:27:43 +02:00
|
|
|
data = JSON.parse(data);
|
|
|
|
if (!Array.isArray(key)) {
|
2018-09-10 00:47:31 +02:00
|
|
|
try {
|
|
|
|
value = util.getObjectProperty(data,key);
|
|
|
|
} catch(err) {
|
|
|
|
if (err.code === "INVALID_EXPR") {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
value = undefined;
|
|
|
|
}
|
|
|
|
callback(null, value);
|
2018-07-23 14:27:43 +02:00
|
|
|
} else {
|
|
|
|
var results = [undefined];
|
|
|
|
for (var i=0;i<key.length;i++) {
|
2018-09-10 00:47:31 +02:00
|
|
|
try {
|
|
|
|
value = util.getObjectProperty(data,key[i]);
|
|
|
|
} catch(err) {
|
|
|
|
if (err.code === "INVALID_EXPR") {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
value = undefined;
|
|
|
|
}
|
|
|
|
results.push(value)
|
2018-07-23 14:27:43 +02:00
|
|
|
}
|
|
|
|
callback.apply(null,results);
|
|
|
|
}
|
2018-03-23 09:18:56 +01:00
|
|
|
}else{
|
2018-06-22 10:11:54 +02:00
|
|
|
callback(null, undefined);
|
2018-03-15 21:52:17 +01:00
|
|
|
}
|
2018-06-07 13:17:51 +02:00
|
|
|
}).catch(function(err){
|
2018-06-22 10:11:54 +02:00
|
|
|
callback(err);
|
2018-06-07 13:17:51 +02:00
|
|
|
});
|
2018-06-01 04:44:45 +02:00
|
|
|
};
|
|
|
|
|
2018-07-02 23:32:20 +02:00
|
|
|
LocalFileSystem.prototype.set = function(scope, key, value, callback) {
|
2018-08-15 11:19:37 +02:00
|
|
|
var self = this;
|
2018-07-23 14:27:43 +02:00
|
|
|
var storagePath = getStoragePath(this.storageBaseDir ,scope);
|
2018-07-02 23:32:20 +02:00
|
|
|
if (this.cache) {
|
|
|
|
this.cache.set(scope,key,value,callback);
|
2018-08-09 15:39:20 +02:00
|
|
|
this.pendingWrites[scope] = true;
|
|
|
|
if (this._pendingWriteTimeout) {
|
|
|
|
// there's a pending write which will handle this
|
|
|
|
return;
|
|
|
|
} else {
|
2018-08-16 15:36:11 +02:00
|
|
|
this._pendingWriteTimeout = setTimeout(function() {
|
2018-09-16 22:15:23 +02:00
|
|
|
self.writePromise = self.writePromise.then(function(){
|
|
|
|
return self._flushPendingWrites.call(self).catch(function(err) {
|
|
|
|
log.error(log._("context.localfilesystem.error-write",{message:err.toString()}));
|
|
|
|
});
|
2018-08-16 15:36:11 +02:00
|
|
|
});
|
|
|
|
}, this.flushInterval);
|
2018-08-09 15:39:20 +02:00
|
|
|
}
|
2018-07-20 04:23:37 +02:00
|
|
|
} else if (callback && typeof callback !== 'function') {
|
2020-02-07 15:26:30 +01:00
|
|
|
throw new Error("File Store cache disabled - only asynchronous access supported");
|
2018-07-02 23:32:20 +02:00
|
|
|
} else {
|
2018-09-17 11:31:00 +02:00
|
|
|
self.writePromise = self.writePromise.then(function() { return loadFile(storagePath + ".json") }).then(function(data){
|
2018-07-23 14:27:43 +02:00
|
|
|
var obj = data ? JSON.parse(data) : {}
|
|
|
|
if (!Array.isArray(key)) {
|
|
|
|
key = [key];
|
|
|
|
value = [value];
|
|
|
|
} else if (!Array.isArray(value)) {
|
|
|
|
// key is an array, but value is not - wrap it as an array
|
|
|
|
value = [value];
|
|
|
|
}
|
|
|
|
for (var i=0;i<key.length;i++) {
|
|
|
|
var v = null;
|
|
|
|
if (i<value.length) {
|
|
|
|
v = value[i];
|
|
|
|
}
|
2018-07-25 10:59:26 +02:00
|
|
|
util.setObjectProperty(obj,key[i],v);
|
2018-07-23 14:27:43 +02:00
|
|
|
}
|
2018-08-15 11:19:37 +02:00
|
|
|
var stringifiedContext = stringify(obj);
|
|
|
|
if (stringifiedContext.circular && !self.knownCircularRefs[scope]) {
|
2020-04-08 12:32:39 +02:00
|
|
|
log.warn(log._("context.localfilesystem.error-circular",{scope:scope}));
|
2018-08-15 11:19:37 +02:00
|
|
|
self.knownCircularRefs[scope] = true;
|
|
|
|
} else {
|
|
|
|
delete self.knownCircularRefs[scope];
|
|
|
|
}
|
2019-08-21 17:54:26 +02:00
|
|
|
return writeFileAtomic(storagePath, stringifiedContext.json);
|
2018-07-23 14:27:43 +02:00
|
|
|
}).then(function(){
|
|
|
|
if(typeof callback === "function"){
|
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
}).catch(function(err){
|
|
|
|
if(typeof callback === "function"){
|
|
|
|
callback(err);
|
|
|
|
}
|
|
|
|
});
|
2018-07-02 23:32:20 +02:00
|
|
|
}
|
2018-06-01 04:44:45 +02:00
|
|
|
};
|
|
|
|
|
2018-06-22 10:11:54 +02:00
|
|
|
LocalFileSystem.prototype.keys = function(scope, callback){
|
2018-07-02 23:32:20 +02:00
|
|
|
if (this.cache) {
|
|
|
|
return this.cache.keys(scope,callback);
|
|
|
|
}
|
2018-06-22 10:11:54 +02:00
|
|
|
if(typeof callback !== "function"){
|
|
|
|
throw new Error("Callback must be a function");
|
|
|
|
}
|
2018-06-07 13:17:51 +02:00
|
|
|
var storagePath = getStoragePath(this.storageBaseDir ,scope);
|
2018-06-22 10:11:54 +02:00
|
|
|
loadFile(storagePath + ".json").then(function(data){
|
2018-06-07 13:17:51 +02:00
|
|
|
if(data){
|
2018-06-22 10:11:54 +02:00
|
|
|
callback(null, Object.keys(JSON.parse(data)));
|
2018-06-07 13:17:51 +02:00
|
|
|
}else{
|
2018-06-22 10:11:54 +02:00
|
|
|
callback(null, []);
|
2018-06-07 13:17:51 +02:00
|
|
|
}
|
|
|
|
}).catch(function(err){
|
2018-06-22 10:11:54 +02:00
|
|
|
callback(err);
|
2018-06-07 13:17:51 +02:00
|
|
|
});
|
2018-06-01 04:44:45 +02:00
|
|
|
};
|
|
|
|
|
2018-05-24 14:42:40 +02:00
|
|
|
LocalFileSystem.prototype.delete = function(scope){
|
2018-07-02 23:32:20 +02:00
|
|
|
var cachePromise;
|
|
|
|
if (this.cache) {
|
|
|
|
cachePromise = this.cache.delete(scope);
|
|
|
|
} else {
|
|
|
|
cachePromise = Promise.resolve();
|
|
|
|
}
|
|
|
|
var that = this;
|
2018-08-09 15:39:20 +02:00
|
|
|
delete this.pendingWrites[scope];
|
2018-07-02 23:32:20 +02:00
|
|
|
return cachePromise.then(function() {
|
|
|
|
var storagePath = getStoragePath(that.storageBaseDir,scope);
|
|
|
|
return fs.remove(storagePath + ".json");
|
|
|
|
});
|
2018-05-24 14:42:40 +02:00
|
|
|
}
|
|
|
|
|
2018-08-09 15:39:20 +02:00
|
|
|
LocalFileSystem.prototype.clean = function(_activeNodes) {
|
|
|
|
var activeNodes = {};
|
|
|
|
_activeNodes.forEach(function(node) { activeNodes[node] = true });
|
2018-05-30 08:23:34 +02:00
|
|
|
var self = this;
|
2018-07-02 23:32:20 +02:00
|
|
|
var cachePromise;
|
|
|
|
if (this.cache) {
|
2018-08-09 15:39:20 +02:00
|
|
|
cachePromise = this.cache.clean(_activeNodes);
|
2018-07-02 23:32:20 +02:00
|
|
|
} else {
|
|
|
|
cachePromise = Promise.resolve();
|
|
|
|
}
|
2018-08-15 11:19:37 +02:00
|
|
|
this.knownCircularRefs = {};
|
2018-08-09 15:39:20 +02:00
|
|
|
return cachePromise.then(() => listFiles(self.storageBaseDir)).then(function(files) {
|
|
|
|
var promises = [];
|
|
|
|
files.forEach(function(file) {
|
2018-08-15 16:31:42 +02:00
|
|
|
var parts = file.split(path.sep);
|
2018-08-09 15:39:20 +02:00
|
|
|
var removePromise;
|
|
|
|
if (parts[0] === 'global') {
|
|
|
|
// never clean global
|
|
|
|
return;
|
|
|
|
} else if (!activeNodes[parts[0]]) {
|
|
|
|
// Flow removed - remove the whole dir
|
|
|
|
removePromise = fs.remove(path.join(self.storageBaseDir,parts[0]));
|
|
|
|
} else if (parts[1] !== 'flow.json' && !activeNodes[parts[1].substring(0,parts[1].length-5)]) {
|
|
|
|
// Node removed - remove the context file
|
|
|
|
removePromise = fs.remove(path.join(self.storageBaseDir,file));
|
|
|
|
}
|
|
|
|
if (removePromise) {
|
|
|
|
promises.push(removePromise);
|
2018-05-30 08:23:34 +02:00
|
|
|
}
|
2018-07-02 23:32:20 +02:00
|
|
|
});
|
2018-08-09 15:39:20 +02:00
|
|
|
return Promise.all(promises)
|
|
|
|
})
|
2018-06-08 12:26:07 +02:00
|
|
|
}
|
2018-05-30 08:23:34 +02:00
|
|
|
|
2018-05-24 14:42:40 +02:00
|
|
|
module.exports = function(config){
|
|
|
|
return new LocalFileSystem(config);
|
2018-03-15 21:52:17 +01:00
|
|
|
};
|