mirror of
https://github.com/embarklabs/embark.git
synced 2025-02-24 03:18:31 +00:00
Refactoring Storage logic to allow multiple storages and keep backwards compatibility
This commit is contained in:
parent
3bb81c6a79
commit
c1f72ae10e
@ -78,8 +78,7 @@ var EmbarkJS =
|
||||
// Embark Smart Contracts
|
||||
//=========================================================
|
||||
|
||||
var EmbarkJS = {
|
||||
};
|
||||
var EmbarkJS = {};
|
||||
|
||||
EmbarkJS.Contract = function(options) {
|
||||
var self = this;
|
||||
@ -200,7 +199,11 @@ EmbarkJS.Contract.prototype.deploy = function(args, _options) {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else if (transaction.address !== undefined) {
|
||||
resolve(new EmbarkJS.Contract({abi: self.abi, code: self.code, address: transaction.address}));
|
||||
resolve(new EmbarkJS.Contract({
|
||||
abi: self.abi,
|
||||
code: self.code,
|
||||
address: transaction.address
|
||||
}));
|
||||
}
|
||||
});
|
||||
|
||||
@ -218,31 +221,14 @@ EmbarkJS.Contract.prototype.deploy = function(args, _options) {
|
||||
// Embark Storage
|
||||
//=========================================================
|
||||
|
||||
EmbarkJS.Storage = {
|
||||
};
|
||||
EmbarkJS.Storage = {};
|
||||
|
||||
EmbarkJS.Storage.Providers = {
|
||||
IPFS: 'ipfs',
|
||||
SWARM: 'swarm'
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.IPFS = {
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.connect = function(provider){
|
||||
var self = this;
|
||||
var promise = new Promise(function(resolve, reject) {
|
||||
if (provider.toLowerCase() === EmbarkJS.Storage.Providers.IPFS) {
|
||||
resolve(self.currentStorage.ipfsConnection);
|
||||
}
|
||||
else {
|
||||
var error = new Error(provider + ' storage provider not supported');
|
||||
reject(error);
|
||||
};
|
||||
});
|
||||
|
||||
return promise;
|
||||
};
|
||||
EmbarkJS.Storage.IPFS = {};
|
||||
|
||||
EmbarkJS.Storage.saveText = function(text) {
|
||||
return this.currentStorage.saveText(text);
|
||||
@ -261,48 +247,48 @@ EmbarkJS.Storage.getUrl = function(hash){
|
||||
}
|
||||
|
||||
EmbarkJS.Storage.setProvider = function(provider, options) {
|
||||
var self = this;
|
||||
var promise = new Promise(function(resolve, reject) {
|
||||
if (provider.toLowerCase() === EmbarkJS.Storage.Providers.IPFS) {
|
||||
//I don't think currentStorage is used anywhere, this might not be needed
|
||||
//for now until additional storage providers are supported. But keeping it
|
||||
//anyways
|
||||
this.currentStorage = EmbarkJS.Storage.IPFS;
|
||||
self.currentStorage = EmbarkJS.Storage.IPFS;
|
||||
|
||||
try {
|
||||
if (options === undefined) {
|
||||
this.currentStorage.ipfsConnection = IpfsApi('localhost', '5001');
|
||||
self.ipfsConnection = IpfsApi('localhost', '5001');
|
||||
} else {
|
||||
this.currentStorage.ipfsConnection = IpfsApi(options.server, options.port);
|
||||
self.ipfsConnection = IpfsApi(options.server, options.port);
|
||||
}
|
||||
resolve(self);
|
||||
} catch (err) {
|
||||
self.ipfsConnection = null;
|
||||
reject(new Error('Failed to connect to IPFS'));
|
||||
}
|
||||
catch(err){
|
||||
this.currentStorage.ipfsConnection = null;
|
||||
}
|
||||
|
||||
}
|
||||
else if (provider.toLowerCase() === EmbarkJS.Storage.SWARM){
|
||||
throw Error('Swarm not implemented');
|
||||
this.currentStorage = EmbarkJS.Storage.SWARM;
|
||||
if (options === undefined) {
|
||||
//Connect to default Swarm node
|
||||
} else if (provider.toLowerCase() === EmbarkJS.Storage.SWARM) {
|
||||
reject('Swarm not implemented');
|
||||
// TODO Implement Swarm
|
||||
// this.currentStorage = EmbarkJS.Storage.SWARM;
|
||||
// if (options === undefined) {
|
||||
// //Connect to default Swarm node
|
||||
// } else {
|
||||
// //Connect using options
|
||||
// }
|
||||
} else {
|
||||
//Connect using options
|
||||
}
|
||||
}
|
||||
else {
|
||||
throw Error('Unknown storage provider');
|
||||
reject('Unknown storage provider');
|
||||
}
|
||||
});
|
||||
return promise;
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.IPFS.saveText = function(text) {
|
||||
var self = this;
|
||||
if (!self.ipfsConnection) {
|
||||
EmbarkJS.Storage.setProvider(EmbarkJS.Storage.Providers.IPFS);
|
||||
}
|
||||
var promise = new Promise(function(resolve, reject) {
|
||||
if (!self.ipfsConnection){
|
||||
var connectionError = new Error('No IPFS connection');
|
||||
if (!EmbarkJS.Storage.ipfsConnection) {
|
||||
var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()');
|
||||
reject(connectionError);
|
||||
}
|
||||
self.ipfsConnection.add((new self.ipfsConnection.Buffer(text)), function(err, result) {
|
||||
EmbarkJS.Storage.ipfsConnection.add((new EmbarkJS.Storage.ipfsConnection.Buffer(text)), function(err, result) {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
@ -315,18 +301,14 @@ EmbarkJS.Storage.IPFS.saveText = function(text) {
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.IPFS.get = function(hash) {
|
||||
var self = this;
|
||||
// TODO: detect type, then convert if needed
|
||||
//var ipfsHash = web3.toAscii(hash);
|
||||
if (!self.ipfsConnection) {
|
||||
EmbarkJS.Storage.setProvider(EmbarkJS.Storage.Providers.IPFS);
|
||||
}
|
||||
var promise = new Promise(function(resolve, reject) {
|
||||
if (!self.ipfsConnection){
|
||||
var connectionError = new Error('No IPFS connection');
|
||||
if (!EmbarkJS.Storage.ipfsConnection) {
|
||||
var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()');
|
||||
reject(connectionError);
|
||||
}
|
||||
self.ipfsConnection.object.get([hash]).then(function(node) {
|
||||
EmbarkJS.Storage.ipfsConnection.object.get([hash]).then(function(node) {
|
||||
resolve(node.data);
|
||||
}).catch(function(err) {
|
||||
reject(err);
|
||||
@ -337,27 +319,22 @@ EmbarkJS.Storage.IPFS.get = function(hash) {
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.IPFS.uploadFile = function(inputSelector) {
|
||||
var self = this;
|
||||
var file = inputSelector[0].files[0];
|
||||
|
||||
if (file === undefined) {
|
||||
throw new Error('no file found');
|
||||
}
|
||||
|
||||
if (!self.ipfsConnection) {
|
||||
EmbarkJS.Storage.setProvider(EmbarkJS.Storage.Providers.IPFS);
|
||||
}
|
||||
|
||||
var promise = new Promise(function(resolve, reject) {
|
||||
if (!self.ipfsConnection){
|
||||
var connectionError = new Error('No IPFS connection');
|
||||
if (!EmbarkJS.Storage.ipfsConnection) {
|
||||
var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()');
|
||||
reject(connectionError);
|
||||
}
|
||||
var reader = new FileReader();
|
||||
reader.onloadend = function() {
|
||||
var fileContent = reader.result;
|
||||
var buffer = self.ipfsConnection.Buffer.from(fileContent);
|
||||
self.ipfsConnection.add(buffer, function(err, result) {
|
||||
var buffer = EmbarkJS.Storage.ipfsConnection.Buffer.from(fileContent);
|
||||
EmbarkJS.Storage.ipfsConnection.add(buffer, function(err, result) {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
@ -381,8 +358,7 @@ EmbarkJS.Storage.IPFS.getUrl = function(hash) {
|
||||
// Embark Messaging
|
||||
//=========================================================
|
||||
|
||||
EmbarkJS.Messages = {
|
||||
};
|
||||
EmbarkJS.Messages = {};
|
||||
|
||||
EmbarkJS.Messages.setProvider = function(provider, options) {
|
||||
var self = this;
|
||||
@ -425,8 +401,7 @@ EmbarkJS.Messages.listenTo = function(options) {
|
||||
return this.currentMessages.listenTo(options);
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.Whisper = {
|
||||
};
|
||||
EmbarkJS.Messages.Whisper = {};
|
||||
|
||||
EmbarkJS.Messages.Whisper.sendMessage = function(options) {
|
||||
var topics = options.topic || options.topics;
|
||||
@ -525,8 +500,7 @@ EmbarkJS.Messages.Whisper.listenTo = function(options) {
|
||||
return promise;
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.Orbit = {
|
||||
};
|
||||
EmbarkJS.Messages.Orbit = {};
|
||||
|
||||
EmbarkJS.Messages.Orbit.sendMessage = function(options) {
|
||||
var topics = options.topic || options.topics;
|
||||
|
112
js/embark.js
112
js/embark.js
@ -5,8 +5,7 @@
|
||||
// Embark Smart Contracts
|
||||
//=========================================================
|
||||
|
||||
var EmbarkJS = {
|
||||
};
|
||||
var EmbarkJS = {};
|
||||
|
||||
EmbarkJS.Contract = function(options) {
|
||||
var self = this;
|
||||
@ -127,7 +126,11 @@ EmbarkJS.Contract.prototype.deploy = function(args, _options) {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else if (transaction.address !== undefined) {
|
||||
resolve(new EmbarkJS.Contract({abi: self.abi, code: self.code, address: transaction.address}));
|
||||
resolve(new EmbarkJS.Contract({
|
||||
abi: self.abi,
|
||||
code: self.code,
|
||||
address: transaction.address
|
||||
}));
|
||||
}
|
||||
});
|
||||
|
||||
@ -145,31 +148,14 @@ EmbarkJS.Contract.prototype.deploy = function(args, _options) {
|
||||
// Embark Storage
|
||||
//=========================================================
|
||||
|
||||
EmbarkJS.Storage = {
|
||||
};
|
||||
EmbarkJS.Storage = {};
|
||||
|
||||
EmbarkJS.Storage.Providers = {
|
||||
IPFS: 'ipfs',
|
||||
SWARM: 'swarm'
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.IPFS = {
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.connect = function(provider){
|
||||
var self = this;
|
||||
var promise = new Promise(function(resolve, reject) {
|
||||
if (provider.toLowerCase() === EmbarkJS.Storage.Providers.IPFS) {
|
||||
resolve(self.currentStorage.ipfsConnection);
|
||||
}
|
||||
else {
|
||||
var error = new Error(provider + ' storage provider not supported');
|
||||
reject(error);
|
||||
};
|
||||
});
|
||||
|
||||
return promise;
|
||||
};
|
||||
EmbarkJS.Storage.IPFS = {};
|
||||
|
||||
EmbarkJS.Storage.saveText = function(text) {
|
||||
return this.currentStorage.saveText(text);
|
||||
@ -188,48 +174,48 @@ EmbarkJS.Storage.getUrl = function(hash){
|
||||
}
|
||||
|
||||
EmbarkJS.Storage.setProvider = function(provider, options) {
|
||||
var self = this;
|
||||
var promise = new Promise(function(resolve, reject) {
|
||||
if (provider.toLowerCase() === EmbarkJS.Storage.Providers.IPFS) {
|
||||
//I don't think currentStorage is used anywhere, this might not be needed
|
||||
//for now until additional storage providers are supported. But keeping it
|
||||
//anyways
|
||||
this.currentStorage = EmbarkJS.Storage.IPFS;
|
||||
self.currentStorage = EmbarkJS.Storage.IPFS;
|
||||
|
||||
try {
|
||||
if (options === undefined) {
|
||||
this.currentStorage.ipfsConnection = IpfsApi('localhost', '5001');
|
||||
self.ipfsConnection = IpfsApi('localhost', '5001');
|
||||
} else {
|
||||
this.currentStorage.ipfsConnection = IpfsApi(options.server, options.port);
|
||||
self.ipfsConnection = IpfsApi(options.server, options.port);
|
||||
}
|
||||
resolve(self);
|
||||
} catch (err) {
|
||||
self.ipfsConnection = null;
|
||||
reject(new Error('Failed to connect to IPFS'));
|
||||
}
|
||||
catch(err){
|
||||
this.currentStorage.ipfsConnection = null;
|
||||
}
|
||||
|
||||
}
|
||||
else if (provider.toLowerCase() === EmbarkJS.Storage.SWARM){
|
||||
throw Error('Swarm not implemented');
|
||||
this.currentStorage = EmbarkJS.Storage.SWARM;
|
||||
if (options === undefined) {
|
||||
//Connect to default Swarm node
|
||||
} else if (provider.toLowerCase() === EmbarkJS.Storage.SWARM) {
|
||||
reject('Swarm not implemented');
|
||||
// TODO Implement Swarm
|
||||
// this.currentStorage = EmbarkJS.Storage.SWARM;
|
||||
// if (options === undefined) {
|
||||
// //Connect to default Swarm node
|
||||
// } else {
|
||||
// //Connect using options
|
||||
// }
|
||||
} else {
|
||||
//Connect using options
|
||||
}
|
||||
}
|
||||
else {
|
||||
throw Error('Unknown storage provider');
|
||||
reject('Unknown storage provider');
|
||||
}
|
||||
});
|
||||
return promise;
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.IPFS.saveText = function(text) {
|
||||
var self = this;
|
||||
if (!self.ipfsConnection) {
|
||||
EmbarkJS.Storage.setProvider(EmbarkJS.Storage.Providers.IPFS);
|
||||
}
|
||||
var promise = new Promise(function(resolve, reject) {
|
||||
if (!self.ipfsConnection){
|
||||
var connectionError = new Error('No IPFS connection');
|
||||
if (!EmbarkJS.Storage.ipfsConnection) {
|
||||
var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()');
|
||||
reject(connectionError);
|
||||
}
|
||||
self.ipfsConnection.add((new self.ipfsConnection.Buffer(text)), function(err, result) {
|
||||
EmbarkJS.Storage.ipfsConnection.add((new EmbarkJS.Storage.ipfsConnection.Buffer(text)), function(err, result) {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
@ -242,18 +228,14 @@ EmbarkJS.Storage.IPFS.saveText = function(text) {
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.IPFS.get = function(hash) {
|
||||
var self = this;
|
||||
// TODO: detect type, then convert if needed
|
||||
//var ipfsHash = web3.toAscii(hash);
|
||||
if (!self.ipfsConnection) {
|
||||
EmbarkJS.Storage.setProvider(EmbarkJS.Storage.Providers.IPFS);
|
||||
}
|
||||
var promise = new Promise(function(resolve, reject) {
|
||||
if (!self.ipfsConnection){
|
||||
var connectionError = new Error('No IPFS connection');
|
||||
if (!EmbarkJS.Storage.ipfsConnection) {
|
||||
var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()');
|
||||
reject(connectionError);
|
||||
}
|
||||
self.ipfsConnection.object.get([hash]).then(function(node) {
|
||||
EmbarkJS.Storage.ipfsConnection.object.get([hash]).then(function(node) {
|
||||
resolve(node.data);
|
||||
}).catch(function(err) {
|
||||
reject(err);
|
||||
@ -264,27 +246,22 @@ EmbarkJS.Storage.IPFS.get = function(hash) {
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.IPFS.uploadFile = function(inputSelector) {
|
||||
var self = this;
|
||||
var file = inputSelector[0].files[0];
|
||||
|
||||
if (file === undefined) {
|
||||
throw new Error('no file found');
|
||||
}
|
||||
|
||||
if (!self.ipfsConnection) {
|
||||
EmbarkJS.Storage.setProvider(EmbarkJS.Storage.Providers.IPFS);
|
||||
}
|
||||
|
||||
var promise = new Promise(function(resolve, reject) {
|
||||
if (!self.ipfsConnection){
|
||||
var connectionError = new Error('No IPFS connection');
|
||||
if (!EmbarkJS.Storage.ipfsConnection) {
|
||||
var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()');
|
||||
reject(connectionError);
|
||||
}
|
||||
var reader = new FileReader();
|
||||
reader.onloadend = function() {
|
||||
var fileContent = reader.result;
|
||||
var buffer = self.ipfsConnection.Buffer.from(fileContent);
|
||||
self.ipfsConnection.add(buffer, function(err, result) {
|
||||
var buffer = EmbarkJS.Storage.ipfsConnection.Buffer.from(fileContent);
|
||||
EmbarkJS.Storage.ipfsConnection.add(buffer, function(err, result) {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
@ -308,8 +285,7 @@ EmbarkJS.Storage.IPFS.getUrl = function(hash) {
|
||||
// Embark Messaging
|
||||
//=========================================================
|
||||
|
||||
EmbarkJS.Messages = {
|
||||
};
|
||||
EmbarkJS.Messages = {};
|
||||
|
||||
EmbarkJS.Messages.setProvider = function(provider, options) {
|
||||
var self = this;
|
||||
@ -352,8 +328,7 @@ EmbarkJS.Messages.listenTo = function(options) {
|
||||
return this.currentMessages.listenTo(options);
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.Whisper = {
|
||||
};
|
||||
EmbarkJS.Messages.Whisper = {};
|
||||
|
||||
EmbarkJS.Messages.Whisper.sendMessage = function(options) {
|
||||
var topics = options.topic || options.topics;
|
||||
@ -452,8 +427,7 @@ EmbarkJS.Messages.Whisper.listenTo = function(options) {
|
||||
return promise;
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.Orbit = {
|
||||
};
|
||||
EmbarkJS.Messages.Orbit = {};
|
||||
|
||||
EmbarkJS.Messages.Orbit.sendMessage = function(options) {
|
||||
var topics = options.topic || options.topics;
|
||||
|
Loading…
x
Reference in New Issue
Block a user