floCloudAPI_v2.1.0
- Updating to node version of SuperNodeStorage
This commit is contained in:
parent
df8df5edbc
commit
b575432ace
@ -12,9 +12,7 @@
|
|||||||
|
|
||||||
//Required for blockchain API operators
|
//Required for blockchain API operators
|
||||||
apiURL: {
|
apiURL: {
|
||||||
FLO: ['https://explorer.mediciland.com/', 'https://livenet.flocha.in/', 'https://flosight.duckdns.org/',
|
FLO: ['https://livenet.flocha.in/', 'https://flosight.duckdns.org/'],
|
||||||
'http://livenet-explorer.floexperiments.com/'
|
|
||||||
],
|
|
||||||
FLO_TEST: ['https://testnet-flosight.duckdns.org/', 'https://testnet.flocha.in/']
|
FLO_TEST: ['https://testnet-flosight.duckdns.org/', 'https://testnet.flocha.in/']
|
||||||
},
|
},
|
||||||
adminID: "FKAEdnPfjXLHSYwrXQu377ugN4tXU7VGdf",
|
adminID: "FKAEdnPfjXLHSYwrXQu377ugN4tXU7VGdf",
|
||||||
@ -8035,7 +8033,7 @@ Bitcoin.Util = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
<script id="floCloudAPI" version="2.0.2e">
|
<script id="floCloudAPI" version="2.1.0">
|
||||||
/* FLO Cloud operations to send/request application data*/
|
/* FLO Cloud operations to send/request application data*/
|
||||||
const floCloudAPI = {
|
const floCloudAPI = {
|
||||||
|
|
||||||
@ -8180,23 +8178,16 @@ Bitcoin.Util = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
inactive: new Set(),
|
inactive: new Set(),
|
||||||
connect(snID) {
|
|
||||||
|
ws_connect(snID) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!(snID in floGlobals.supernodes))
|
if (!(snID in floGlobals.supernodes))
|
||||||
return reject(`${snID} is not a supernode`)
|
return reject(`${snID} is not a supernode`)
|
||||||
let inactive = this.inactive
|
let inactive = this.inactive
|
||||||
if (inactive.has(snID))
|
if (inactive.has(snID))
|
||||||
return reject(`${snID} is not active`)
|
return reject(`${snID} is not active`)
|
||||||
var wsConn = new WebSocket("wss://" + floGlobals.supernodes[snID].uri + "/ws");
|
var wsConn = new WebSocket("wss://" + floGlobals.supernodes[snID].uri + "/");
|
||||||
wsConn.onmessage = (evt) => {
|
wsConn.onopen = evt => resolve(wsConn);
|
||||||
if (evt.data == '$+')
|
|
||||||
resolve(wsConn)
|
|
||||||
else if (evt.data == '$-') {
|
|
||||||
wsConn.close();
|
|
||||||
inactive.add(snID)
|
|
||||||
reject(`${snID} is not active`)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
wsConn.onerror = evt => {
|
wsConn.onerror = evt => {
|
||||||
inactive.add(snID)
|
inactive.add(snID)
|
||||||
reject(`${snID} is unavailable`)
|
reject(`${snID} is unavailable`)
|
||||||
@ -8204,51 +8195,88 @@ Bitcoin.Util = {
|
|||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
connectActive(snID, reverse = false) {
|
ws_activeConnect(snID, reverse = false) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (this.inactive.size === this.kBucket.SNCO.length)
|
if (this.inactive.size === this.kBucket.SNCO.length)
|
||||||
return reject('Cloud offline')
|
return reject('Cloud offline');
|
||||||
if (!(snID in floGlobals.supernodes))
|
if (!(snID in floGlobals.supernodes))
|
||||||
snID = this.kBucket.closestNode(snID);
|
snID = this.kBucket.closestNode(snID);
|
||||||
this.connect(snID)
|
this.ws_connect(snID)
|
||||||
.then(node => resolve(node))
|
.then(node => resolve(node))
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
if (reverse)
|
if (reverse)
|
||||||
var nxtNode = this.kBucket.prevNode(snID);
|
var nxtNode = this.kBucket.prevNode(snID);
|
||||||
else
|
else
|
||||||
var nxtNode = this.kBucket.nextNode(snID);
|
var nxtNode = this.kBucket.nextNode(snID);
|
||||||
this.connectActive(nxtNode, reverse)
|
this.ws_activeConnect(nxtNode, reverse)
|
||||||
.then(node => resolve(node))
|
.then(node => resolve(node))
|
||||||
.catch(error => reject(error))
|
.catch(error => reject(error))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
singleRequest: function(floID, data) {
|
fetch_API: function(snID, data) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this.connectActive(floID).then(node => {
|
if (this.inactive.has(snID))
|
||||||
let randID = floCrypto.randString(5);
|
return reject(`${snID} is not active`);
|
||||||
node.send(`${floID}|${randID}:${JSON.stringify(data)}`);
|
let fetcher, sn_url = "https://" + floGlobals.supernodes[snID].uri;
|
||||||
node.onmessage = (evt) => {
|
if (typeof data === "string")
|
||||||
if (evt.data.startsWith(randID, 1)) {
|
fetcher = fetch(sn_url + "?" + data);
|
||||||
try {
|
else if (typeof data === "object" && data.method === "POST")
|
||||||
let data = JSON.parse(evt.data.substring(42))
|
fetcher = fetch(sn_url, data);
|
||||||
resolve(data)
|
fetcher.then(response => {
|
||||||
} catch (error) {
|
if (response.ok)
|
||||||
reject(evt.data.substring(42))
|
resolve(response);
|
||||||
} finally {
|
else
|
||||||
node.close()
|
reject(response);
|
||||||
}
|
}).catch(error => reject(error))
|
||||||
}
|
})
|
||||||
}
|
|
||||||
}).catch(error => reject(error));
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
|
|
||||||
liveRequest: function(floID, datareq, callback) {
|
fetch_ActiveAPI: function(snID, data, reverse = false) {
|
||||||
let request = {
|
return new Promise((resolve, reject) => {
|
||||||
...datareq.request
|
if (this.inactive.size === this.kBucket.SNCO.length)
|
||||||
};
|
return reject('Cloud offline');
|
||||||
|
if (!(snID in floGlobals.supernodes))
|
||||||
|
snID = this.kBucket.closestNode(snID);
|
||||||
|
this.fetch_API(snID, data)
|
||||||
|
.then(result => resolve(result))
|
||||||
|
.catch(error => {
|
||||||
|
this.inactive.add(snID)
|
||||||
|
if (reverse)
|
||||||
|
var nxtNode = this.kBucket.prevNode(snID);
|
||||||
|
else
|
||||||
|
var nxtNode = this.kBucket.nextNode(snID);
|
||||||
|
this.fetch_ActiveAPI(nxtNode, data, reverse)
|
||||||
|
.then(result => resolve(result))
|
||||||
|
.catch(error => reject(error));
|
||||||
|
})
|
||||||
|
})
|
||||||
|
},
|
||||||
|
|
||||||
|
singleRequest: function(floID, data_obj, method = "POST") {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
let data;
|
||||||
|
if (method === "POST")
|
||||||
|
data = {
|
||||||
|
method: "POST",
|
||||||
|
body: JSON.stringify(data_obj)
|
||||||
|
};
|
||||||
|
else
|
||||||
|
data = new URLSearchParams(JSON.parse(JSON.stringify(data_obj))).toString();
|
||||||
|
this.fetch_ActiveAPI(floID, data).then(response => {
|
||||||
|
response.json()
|
||||||
|
.then(result => resolve(this.objectifier(result)))
|
||||||
|
.catch(error => {
|
||||||
|
response.text()
|
||||||
|
.then(result => reject(result)) //Error Message from Node
|
||||||
|
.catch(error => reject(error))
|
||||||
|
})
|
||||||
|
}).catch(error => reject(error))
|
||||||
|
})
|
||||||
|
},
|
||||||
|
|
||||||
|
liveRequest: function(floID, request, callback) {
|
||||||
const checkFilter = (v, d, r) =>
|
const checkFilter = (v, d, r) =>
|
||||||
(!r.atVectorClock || r.atVectorClock == v) &&
|
(!r.atVectorClock || r.atVectorClock == v) &&
|
||||||
(r.atVectorClock || !r.lowerVectorClock || r.lowerVectorClock <= v) &&
|
(r.atVectorClock || !r.lowerVectorClock || r.lowerVectorClock <= v) &&
|
||||||
@ -8259,25 +8287,20 @@ Bitcoin.Util = {
|
|||||||
(!r.type || r.type == d.type) &&
|
(!r.type || r.type == d.type) &&
|
||||||
(!r.senderIDs || r.senderIDs.includes(d.senderID));
|
(!r.senderIDs || r.senderIDs.includes(d.senderID));
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this.connectActive(floID).then(node => {
|
this.ws_activeConnect(floID).then(node => {
|
||||||
let randID = floCrypto.randString(5);
|
let randID = floCrypto.randString(5);
|
||||||
node.send(`${floID}|${randID}:${JSON.stringify(datareq)}`);
|
node.send(JSON.stringify(request));
|
||||||
node.onmessage = (evt) => {
|
node.onmessage = (evt) => {
|
||||||
if (evt.data.startsWith(randID, 1))
|
|
||||||
var i = 42
|
|
||||||
else if (evt.data.startsWith(floID, 1))
|
|
||||||
var i = 36
|
|
||||||
else return;
|
|
||||||
let d = e = null;
|
let d = e = null;
|
||||||
try {
|
try {
|
||||||
let data = JSON.parse(evt.data.substring(i))
|
let data = this.objectifier(JSON.parse(evt.data)),
|
||||||
let filter = {}
|
filter = {};
|
||||||
for (let v in data)
|
for (let v in data)
|
||||||
if (checkFilter(v, data[v], request))
|
if (checkFilter(v, data[v], request))
|
||||||
filter[v] = data[v]
|
filter[v] = data[v];
|
||||||
d = filter
|
d = filter;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
e = evt.data.substring(i)
|
e = evt.data
|
||||||
} finally {
|
} finally {
|
||||||
callback(d, e)
|
callback(d, e)
|
||||||
}
|
}
|
||||||
@ -8352,6 +8375,15 @@ Bitcoin.Util = {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error)
|
console.error(error)
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
objectifier: function(data) {
|
||||||
|
if (!Array.isArray(data))
|
||||||
|
data = [data];
|
||||||
|
return Object.fromEntries(data.map(d => {
|
||||||
|
d.message = this.decodeMessage(d.message);
|
||||||
|
return [d.vectorClock, d];
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -8372,12 +8404,8 @@ Bitcoin.Util = {
|
|||||||
.map(d => data[d]).join("|")
|
.map(d => data[d]).join("|")
|
||||||
data.sign = floCrypto.signData(hashcontent, myPrivKey)
|
data.sign = floCrypto.signData(hashcontent, myPrivKey)
|
||||||
this.util.singleRequest(data.receiverID, data)
|
this.util.singleRequest(data.receiverID, data)
|
||||||
.then(result => {
|
.then(result => resolve(result))
|
||||||
data.message = message
|
.catch(error => reject(error))
|
||||||
resolve({
|
|
||||||
[result.vectorClock]: data
|
|
||||||
})
|
|
||||||
}).catch(error => reject(error))
|
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -8395,30 +8423,24 @@ Bitcoin.Util = {
|
|||||||
atVectorClock: options.atVectorClock || undefined,
|
atVectorClock: options.atVectorClock || undefined,
|
||||||
mostRecent: options.mostRecent || undefined,
|
mostRecent: options.mostRecent || undefined,
|
||||||
}
|
}
|
||||||
var datareq = {
|
|
||||||
time: Date.now(),
|
|
||||||
request
|
|
||||||
}
|
|
||||||
if (options.callback instanceof Function) {
|
if (options.callback instanceof Function) {
|
||||||
let callback = (d, e) => {
|
this.util.liveRequest(request.receiverID, request, options.callback)
|
||||||
for (let v in d)
|
|
||||||
d[v].message = this.util.decodeMessage(d[v].message)
|
|
||||||
options.callback(d, e)
|
|
||||||
}
|
|
||||||
this.util.liveRequest(request.receiverID, datareq, callback)
|
|
||||||
.then(result => resolve(result))
|
.then(result => resolve(result))
|
||||||
.catch(error => reject(error))
|
.catch(error => reject(error))
|
||||||
} else {
|
} else {
|
||||||
this.util.singleRequest(request.receiverID, datareq).then(data => {
|
if (options.method === "POST")
|
||||||
for (let v in data)
|
request = {
|
||||||
data[v].message = this.util.decodeMessage(data[v].message)
|
time: Date.now(),
|
||||||
resolve(data)
|
request
|
||||||
}).catch(error => reject(error))
|
};
|
||||||
|
this.util.singleRequest(request.receiverID, request, options.method || "GET")
|
||||||
|
.then(data => resolve(data)).catch(error => reject(error))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
//delete data from supernode cloud (received only)
|
//(NEEDS UPDATE) delete data from supernode cloud (received only)
|
||||||
deleteApplicationData: function(vectorClocks, options = {}) {
|
deleteApplicationData: function(vectorClocks, options = {}) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
var delreq = {
|
var delreq = {
|
||||||
@ -8444,7 +8466,7 @@ Bitcoin.Util = {
|
|||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
//edit comment of data in supernode cloud (mutable comments only)
|
//(NEEDS UPDATE) edit comment of data in supernode cloud (mutable comments only)
|
||||||
editApplicationData: function(vectorClock, newComment, oldData, options = {}) {
|
editApplicationData: function(vectorClock, newComment, oldData, options = {}) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let p0
|
let p0
|
||||||
@ -8606,7 +8628,7 @@ Bitcoin.Util = {
|
|||||||
if (!conn)
|
if (!conn)
|
||||||
return reject('Request not found')
|
return reject('Request not found')
|
||||||
conn.onclose = evt => {
|
conn.onclose = evt => {
|
||||||
delete this.util.liveRequest[requestID]
|
delete this.util.liveRequest[requestID];
|
||||||
resolve('Request connection closed')
|
resolve('Request connection closed')
|
||||||
}
|
}
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user