1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Make storage.init return a promise to async initialisation

part of #62
This commit is contained in:
Nicholas O'Leary 2013-11-12 17:13:06 +00:00
parent 1536dcdf1e
commit 848a69dc26
6 changed files with 205 additions and 197 deletions

View File

@ -19,7 +19,7 @@ var storage = null;
function init() { function init() {
redApp = require("./server").app; redApp = require("./server").app;
storage = require("./storage").storage; storage = require("./storage");
// -------- Flow Library -------- // -------- Flow Library --------
redApp.post(new RegExp("/library/flows\/(.*)"), function(req,res) { redApp.post(new RegExp("/library/flows\/(.*)"), function(req,res) {

View File

@ -277,7 +277,7 @@ module.exports.setConfig = function(conf) {
if (!storage) { if (!storage) {
// Do this lazily to ensure the storage provider as been initialised // Do this lazily to ensure the storage provider as been initialised
storage = require("./storage").storage; storage = require("./storage");
} }
storage.getCredentials().then(function(creds) { storage.getCredentials().then(function(creds) {
credentials = creds; credentials = creds;

View File

@ -20,12 +20,13 @@ var redNodes = require("./nodes");
var app = null; var app = null;
var server = null; var server = null;
var settings = null;
var storage = null;
function createServer(_server,settings) { function createServer(_server,_settings) {
server = _server; server = _server;
settings = _settings;
storage = require("./storage").init(settings); storage = require("./storage");
app = createUI(settings); app = createUI(settings);
flowfile = settings.flowFile || 'flows_'+require('os').hostname()+'.json'; flowfile = settings.flowFile || 'flows_'+require('os').hostname()+'.json';
@ -61,6 +62,7 @@ function createServer(_server,settings) {
} }
function start() { function start() {
storage.init(settings).then(function() {
console.log("\nWelcome to Node-RED\n===================\n"); console.log("\nWelcome to Node-RED\n===================\n");
util.log("[red] Loading palette nodes"); util.log("[red] Loading palette nodes");
util.log("------------------------------------------"); util.log("------------------------------------------");
@ -81,6 +83,7 @@ function start() {
}).otherwise(function(err) { }).otherwise(function(err) {
util.log("[red] Error loading flows : "+err); util.log("[red] Error loading flows : "+err);
}); });
});
} }
function stop() { function stop() {

View File

@ -15,19 +15,9 @@
**/ **/
var settings; var settings = require('../red').settings;
var storage;
module.exports = {
init: function(_settings) {
settings = _settings;
var storageType = settings.storageModule || "localfilesystem"; var storageType = settings.storageModule || "localfilesystem";
storage = require("./"+storageType).init(settings); module.exports = require("./"+storageType);
return storage;
},
};
module.exports.__defineGetter__("storage", function() { return storage; });

View File

@ -160,7 +160,9 @@ var localfilesystem = {
if (!fs.existsSync(libFlowsDir)) { if (!fs.existsSync(libFlowsDir)) {
fs.mkdirSync(libFlowsDir); fs.mkdirSync(libFlowsDir);
} }
return this; var defer = when.defer();
defer.resolve();
return defer.promise;
}, },
getFlows: function() { getFlows: function() {
var defer = when.defer(); var defer = when.defer();

View File

@ -18,13 +18,14 @@ describe('LocalFileSystem', function() {
}); });
it('should initialise the user directory',function() { it('should initialise the user directory',function() {
localfilesystem.init({userDir:userDir}); localfilesystem.init({userDir:userDir}).then(function() {
fs.existsSync(path.join(userDir,"lib")).should.be.true; fs.existsSync(path.join(userDir,"lib")).should.be.true;
fs.existsSync(path.join(userDir,"lib",'flows')).should.be.true; fs.existsSync(path.join(userDir,"lib",'flows')).should.be.true;
}); });
});
it('should handle missing flow file',function(done) { it('should handle missing flow file',function(done) {
localfilesystem.init({userDir:userDir}); localfilesystem.init({userDir:userDir}).then(function() {
var flowFile = 'flows_'+require('os').hostname()+'.json'; var flowFile = 'flows_'+require('os').hostname()+'.json';
var flowFilePath = path.join(userDir,flowFile); var flowFilePath = path.join(userDir,flowFile);
fs.existsSync(flowFilePath).should.be.false; fs.existsSync(flowFilePath).should.be.false;
@ -34,11 +35,11 @@ describe('LocalFileSystem', function() {
}).otherwise(function(err) { }).otherwise(function(err) {
done(err); done(err);
}); });
});
}); });
it('should save flows to the default file',function(done) { it('should save flows to the default file',function(done) {
localfilesystem.init({userDir:userDir}); localfilesystem.init({userDir:userDir}).then(function() {
var flowFile = 'flows_'+require('os').hostname()+'.json'; var flowFile = 'flows_'+require('os').hostname()+'.json';
var flowFilePath = path.join(userDir,flowFile); var flowFilePath = path.join(userDir,flowFile);
fs.existsSync(flowFilePath).should.be.false; fs.existsSync(flowFilePath).should.be.false;
@ -54,6 +55,7 @@ describe('LocalFileSystem', function() {
done(err); done(err);
}); });
}); });
});
it('should save flows to the specified file',function(done) { it('should save flows to the specified file',function(done) {
var defaultFlowFile = 'flows_'+require('os').hostname()+'.json'; var defaultFlowFile = 'flows_'+require('os').hostname()+'.json';
@ -61,8 +63,7 @@ describe('LocalFileSystem', function() {
var flowFile = 'test.json'; var flowFile = 'test.json';
var flowFilePath = path.join(userDir,flowFile); var flowFilePath = path.join(userDir,flowFile);
localfilesystem.init({userDir:userDir, flowFile:flowFilePath}); localfilesystem.init({userDir:userDir, flowFile:flowFilePath}).then(function() {
fs.existsSync(defaultFlowFilePath).should.be.false; fs.existsSync(defaultFlowFilePath).should.be.false;
fs.existsSync(flowFilePath).should.be.false; fs.existsSync(flowFilePath).should.be.false;
@ -79,10 +80,11 @@ describe('LocalFileSystem', function() {
done(err); done(err);
}); });
}); });
});
it('should handle missing credentials', function(done) { it('should handle missing credentials', function(done) {
var credFile = path.join(userDir,"credentials.json"); var credFile = path.join(userDir,"credentials.json");
localfilesystem.init({userDir:userDir}); localfilesystem.init({userDir:userDir}).then(function() {
fs.existsSync(credFile).should.be.false; fs.existsSync(credFile).should.be.false;
localfilesystem.getCredentials().then(function(creds) { localfilesystem.getCredentials().then(function(creds) {
@ -92,11 +94,12 @@ describe('LocalFileSystem', function() {
done(err); done(err);
}); });
}); });
});
it('should handle credentials', function(done) { it('should handle credentials', function(done) {
var credFile = path.join(userDir,"credentials.json"); var credFile = path.join(userDir,"credentials.json");
localfilesystem.init({userDir:userDir}); localfilesystem.init({userDir:userDir}).then(function() {
fs.existsSync(credFile).should.be.false; fs.existsSync(credFile).should.be.false;
@ -114,9 +117,10 @@ describe('LocalFileSystem', function() {
done(err); done(err);
}); });
}); });
});
it('should return an empty list of library flows',function(done) { it('should return an empty list of library flows',function(done) {
localfilesystem.init({userDir:userDir}); localfilesystem.init({userDir:userDir}).then(function() {
localfilesystem.getAllFlows().then(function(flows) { localfilesystem.getAllFlows().then(function(flows) {
flows.should.eql({}); flows.should.eql({});
done(); done();
@ -124,9 +128,10 @@ describe('LocalFileSystem', function() {
done(err); done(err);
}); });
}); });
});
it('should return a valid list of library flows',function(done) { it('should return a valid list of library flows',function(done) {
localfilesystem.init({userDir:userDir}); localfilesystem.init({userDir:userDir}).then(function() {
var flowLib = path.join(userDir,"lib","flows"); var flowLib = path.join(userDir,"lib","flows");
fs.closeSync(fs.openSync(path.join(flowLib,"A.json"),"w")); fs.closeSync(fs.openSync(path.join(flowLib,"A.json"),"w"));
fs.closeSync(fs.openSync(path.join(flowLib,"B.json"),"w")); fs.closeSync(fs.openSync(path.join(flowLib,"B.json"),"w"));
@ -141,9 +146,10 @@ describe('LocalFileSystem', function() {
done(err); done(err);
}); });
}); });
});
it('should fail a non-existent flow', function(done) { it('should fail a non-existent flow', function(done) {
localfilesystem.init({userDir:userDir}); localfilesystem.init({userDir:userDir}).then(function() {
localfilesystem.getFlow("a/b/c.json").then(function(flow) { localfilesystem.getFlow("a/b/c.json").then(function(flow) {
should.fail(flow,"No flow","Flow found"); should.fail(flow,"No flow","Flow found");
}).otherwise(function(err) { }).otherwise(function(err) {
@ -151,9 +157,10 @@ describe('LocalFileSystem', function() {
done(err); done(err);
}); });
}); });
});
it('should return a flow',function(done) { it('should return a flow',function(done) {
localfilesystem.init({userDir:userDir}); localfilesystem.init({userDir:userDir}).then(function() {
var testflowString = JSON.stringify(testFlow); var testflowString = JSON.stringify(testFlow);
localfilesystem.saveFlow("a/b/c/d.json",testflowString).then(function() { localfilesystem.saveFlow("a/b/c/d.json",testflowString).then(function() {
localfilesystem.getFlow("a/b/c/d.json").then(function(flow) { localfilesystem.getFlow("a/b/c/d.json").then(function(flow) {
@ -166,9 +173,10 @@ describe('LocalFileSystem', function() {
done(err); done(err);
}); });
}); });
});
it('should return an empty list of library objects',function(done) { it('should return an empty list of library objects',function(done) {
localfilesystem.init({userDir:userDir}); localfilesystem.init({userDir:userDir}).then(function() {
localfilesystem.getLibraryEntry('object','').then(function(flows) { localfilesystem.getLibraryEntry('object','').then(function(flows) {
flows.should.eql({}); flows.should.eql({});
done(); done();
@ -176,9 +184,10 @@ describe('LocalFileSystem', function() {
done(err); done(err);
}); });
}); });
});
it('should return an error for a non-existent library object',function(done) { it('should return an error for a non-existent library object',function(done) {
localfilesystem.init({userDir:userDir}); localfilesystem.init({userDir:userDir}).then(function() {
localfilesystem.getLibraryEntry('object','A/B').then(function(flows) { localfilesystem.getLibraryEntry('object','A/B').then(function(flows) {
should.fail(null,null,"non-existent flow"); should.fail(null,null,"non-existent flow");
}).otherwise(function(err) { }).otherwise(function(err) {
@ -186,6 +195,7 @@ describe('LocalFileSystem', function() {
done(); done();
}); });
}); });
});
function createObjectLibrary() { function createObjectLibrary() {
var objLib = path.join(userDir,"lib","object"); var objLib = path.join(userDir,"lib","object");
@ -198,7 +208,7 @@ describe('LocalFileSystem', function() {
} }
it('should return a directory listing of library objects',function(done) { it('should return a directory listing of library objects',function(done) {
localfilesystem.init({userDir:userDir}); localfilesystem.init({userDir:userDir}).then(function() {
createObjectLibrary(); createObjectLibrary();
localfilesystem.getLibraryEntry('object','').then(function(flows) { localfilesystem.getLibraryEntry('object','').then(function(flows) {
@ -218,9 +228,10 @@ describe('LocalFileSystem', function() {
done(err); done(err);
}); });
}); });
});
it('should return a library object',function(done) { it('should return a library object',function(done) {
localfilesystem.init({userDir:userDir}); localfilesystem.init({userDir:userDir}).then(function() {
createObjectLibrary(); createObjectLibrary();
localfilesystem.getLibraryEntry('object','B/file2.js').then(function(body) { localfilesystem.getLibraryEntry('object','B/file2.js').then(function(body) {
body.should.eql("// not a metaline \n\n Hi"); body.should.eql("// not a metaline \n\n Hi");
@ -229,9 +240,10 @@ describe('LocalFileSystem', function() {
done(err); done(err);
}); });
}); });
});
it('should return a newly saved library object',function(done) { it('should return a newly saved library object',function(done) {
localfilesystem.init({userDir:userDir}); localfilesystem.init({userDir:userDir}).then(function() {
createObjectLibrary(); createObjectLibrary();
localfilesystem.getLibraryEntry('object','B').then(function(flows) { localfilesystem.getLibraryEntry('object','B').then(function(flows) {
flows.should.eql([ 'C', { ghi: 'jkl', fn: 'file2.js' } ]); flows.should.eql([ 'C', { ghi: 'jkl', fn: 'file2.js' } ]);
@ -254,5 +266,6 @@ describe('LocalFileSystem', function() {
done(err); done(err);
}); });
}); });
});
}); });