Workflow updating files of cc

This commit is contained in:
RanchiMall Dev 2025-08-25 02:31:43 +00:00
parent adc64149af
commit 0d9ed0b081
6 changed files with 273 additions and 132 deletions

View File

@ -1,4 +1,4 @@
(function (EXPORTS) { //floBlockchainAPI v3.1.2 (function (EXPORTS) { //floBlockchainAPI v3.1.3
/* FLO Blockchain Operator to send/receive data from blockchain using API calls via FLO Blockbook*/ /* FLO Blockchain Operator to send/receive data from blockchain using API calls via FLO Blockbook*/
'use strict'; 'use strict';
const floBlockchainAPI = EXPORTS; const floBlockchainAPI = EXPORTS;
@ -32,8 +32,8 @@
checkIfTor().then(result => { checkIfTor().then(result => {
isTor = result isTor = result
if (isTor) { if (isTor) {
DEFAULT.apiURL.FLO.push('http://vl7ni6byqx7rbub5hypxtod5dbfeuhoj5r5exuyl44pspqh2gasjj4qd.onion:9166/') DEFAULT.apiURL.FLO.push('http://kvrddx6heo47rbbt77etxg6litckacbgos3nv5z7vc23ol2kjjeq72id.onion/')
DEFAULT.apiURL.FLO_TEST.push('http://omwkzk6bd6zuragdqsrhdyzgxzre7yx4vzrou4vzftintzc2dmagp6qd.onion:15017/') // DEFAULT.apiURL.FLO_TEST.push('http://omwkzk6bd6zuragdqsrhdyzgxzre7yx4vzrou4vzftintzc2dmagp6qd.onion:15017/')
} }
}); });

File diff suppressed because one or more lines are too long

View File

@ -1,4 +1,4 @@
(function (EXPORTS) { //floCloudAPI v2.4.5 (function (EXPORTS) { //floCloudAPI v2.4.5a
/* FLO Cloud operations to send/request application data*/ /* FLO Cloud operations to send/request application data*/
'use strict'; 'use strict';
const floCloudAPI = EXPORTS; const floCloudAPI = EXPORTS;
@ -195,14 +195,24 @@
floCloudAPI.init = function startCloudProcess(nodes) { floCloudAPI.init = function startCloudProcess(nodes) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
try { try {
// accept only plain-ish objects
nodes = (nodes && typeof nodes === 'object' && !Array.isArray(nodes)) ? nodes : {};
supernodes = nodes; supernodes = nodes;
// reset liveness bookkeeping for the new set
if (_inactive && typeof _inactive.clear === 'function') _inactive.clear();
// (re)build the bucket with current IDs
kBucket = new K_Bucket(DEFAULT.SNStorageID, Object.keys(supernodes)); kBucket = new K_Bucket(DEFAULT.SNStorageID, Object.keys(supernodes));
resolve('Cloud init successful'); resolve('Cloud init successful');
} catch (error) { } catch (error) {
reject(error); reject(error);
} }
}) });
} };
Object.defineProperty(floCloudAPI, 'kBucket', { Object.defineProperty(floCloudAPI, 'kBucket', {
get: () => kBucket get: () => kBucket
@ -227,24 +237,27 @@
function ws_activeConnect(snID, reverse = false) { function ws_activeConnect(snID, reverse = false) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (_inactive.size === kBucket.list.length) // Safe guard: uninitialized kBucket, empty list, or all inactive
if (!kBucket || !kBucket.list || !kBucket.list.length || _inactive.size === kBucket.list.length)
return reject('Cloud offline'); return reject('Cloud offline');
if (!(snID in supernodes))
snID = kBucket.closestNode(proxyID(snID)); if (!(snID in supernodes)) {
var closest = kBucket.closestNode(proxyID(snID));
if (!closest) return reject('Cloud offline'); // no candidate to try
snID = closest;
}
ws_connect(snID) ws_connect(snID)
.then(node => resolve(node)) .then(node => resolve(node))
.catch(error => { .catch(error => {
if (reverse) var nxtNode = reverse ? kBucket.prevNode(snID) : kBucket.nextNode(snID);
var nxtNode = kBucket.prevNode(snID); if (!nxtNode || nxtNode === snID) return reject('Cloud offline'); // nothing else to try
else ws_activeConnect(nxtNode, reverse).then(resolve).catch(reject);
var nxtNode = kBucket.nextNode(snID); });
ws_activeConnect(nxtNode, reverse) });
.then(node => resolve(node))
.catch(error => reject(error))
})
})
} }
function fetch_API(snID, data) { function fetch_API(snID, data) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (_inactive.has(snID)) if (_inactive.has(snID))
@ -265,25 +278,28 @@
function fetch_ActiveAPI(snID, data, reverse = false) { function fetch_ActiveAPI(snID, data, reverse = false) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (_inactive.size === kBucket.list.length) // Safe guard: uninitialized kBucket, empty list, or all inactive
if (!kBucket || !kBucket.list || !kBucket.list.length || _inactive.size === kBucket.list.length)
return reject('Cloud offline'); return reject('Cloud offline');
if (!(snID in supernodes))
snID = kBucket.closestNode(proxyID(snID)); if (!(snID in supernodes)) {
var closest = kBucket.closestNode(proxyID(snID));
if (!closest) return reject('Cloud offline'); // no candidate available
snID = closest;
}
fetch_API(snID, data) fetch_API(snID, data)
.then(result => resolve(result)) .then(resolve)
.catch(error => { .catch(error => {
_inactive.add(snID) _inactive.add(snID);
if (reverse) var nxtNode = reverse ? kBucket.prevNode(snID) : kBucket.nextNode(snID);
var nxtNode = kBucket.prevNode(snID); if (!nxtNode || nxtNode === snID) return reject('Cloud offline'); // nothing else to try
else fetch_ActiveAPI(nxtNode, data, reverse).then(resolve).catch(reject);
var nxtNode = kBucket.nextNode(snID); });
fetch_ActiveAPI(nxtNode, data, reverse) });
.then(result => resolve(result))
.catch(error => reject(error));
})
})
} }
function singleRequest(floID, data_obj, method = "POST") { function singleRequest(floID, data_obj, method = "POST") {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let data; let data;
@ -468,21 +484,59 @@
function storeGeneral(fk, dataSet) { function storeGeneral(fk, dataSet) {
try { try {
console.log(dataSet) if (!dataSet || typeof dataSet !== "object") return;
if (typeof generalData[fk] !== "object")
generalData[fk] = {} // Ensure containers exist
for (let vc in dataSet) { if (typeof generalData[fk] !== "object" || generalData[fk] === null)
generalData[fk][vc] = dataSet[vc]; generalData[fk] = {};
if (dataSet[vc].log_time > lastVC[fk]) if (typeof lastVC[fk] !== "number")
lastVC[fk] = dataSet[vc].log_time; lastVC[fk] = 0;
// Merge data and track latest log_time
let updated = false;
let newLast = lastVC[fk];
for (const vc in dataSet) {
const rec = dataSet[vc];
// Skip bad records
if (!rec || typeof rec !== "object") continue;
// Assign only if changed reference (cheap check)
if (generalData[fk][vc] !== rec) {
generalData[fk][vc] = rec;
updated = true;
}
if (typeof rec.log_time === "number" && rec.log_time > newLast) {
newLast = rec.log_time;
updated = true;
}
} }
compactIDB.writeData("lastVC", lastVC[fk], fk)
compactIDB.writeData("generalData", generalData[fk], fk) if (!updated) return; // nothing new, avoid IDB writes
lastVC[fk] = newLast;
// --- Debounce writes per fk to avoid IDB thrash ---
storeGeneral._pending = storeGeneral._pending || Object.create(null);
const pend = storeGeneral._pending;
clearTimeout(pend[fk]);
pend[fk] = setTimeout(() => {
try {
// Fire-and-forget; callers dont wait on these
compactIDB.writeData("lastVC", lastVC[fk], fk);
compactIDB.writeData("generalData", generalData[fk], fk);
} catch (e) {
console.error(e);
}
}, 50);
} catch (error) { } catch (error) {
console.error(error) console.error(error);
} }
} }
function objectifier(data) { function objectifier(data) {
if (!Array.isArray(data)) if (!Array.isArray(data))
data = [data]; data = [data];
@ -551,7 +605,7 @@
} }
//request any data from supernode cloud //request any data from supernode cloud
const requestApplicationData = floCloudAPI.requestApplicationData = function (type, options = {}) { const _requestApplicationData = function (type, options = {}) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
var request = { var request = {
receiverID: options.receiverID || DEFAULT.adminID, receiverID: options.receiverID || DEFAULT.adminID,
@ -582,6 +636,17 @@
}) })
} }
floCloudAPI.requestApplicationData = function (type, options = {}) {
return new Promise((resolve, reject) => {
let single_request_mode = !(options.callback instanceof Function);
_requestApplicationData(type, options).then(data => {
if (single_request_mode)
resolve(objectifier(data))
else resolve(data);
}).catch(error => reject(error))
})
}
/*(NEEDS UPDATE) /*(NEEDS UPDATE)
//delete data from supernode cloud (received only) //delete data from supernode cloud (received only)
floCloudAPI.deleteApplicationData = function(vectorClocks, options = {}) { floCloudAPI.deleteApplicationData = function(vectorClocks, options = {}) {
@ -615,7 +680,7 @@
//request the data from cloud for resigning //request the data from cloud for resigning
let req_options = Object.assign({}, options); let req_options = Object.assign({}, options);
req_options.atVectorClock = vectorClock; req_options.atVectorClock = vectorClock;
requestApplicationData(undefined, req_options).then(result => { _requestApplicationData(undefined, req_options).then(result => {
if (!result.length) if (!result.length)
return reject("Data not found"); return reject("Data not found");
let data = result[0]; let data = result[0];
@ -709,11 +774,11 @@
storeGeneral(fk, d); storeGeneral(fk, d);
options.callback(d, e) options.callback(d, e)
} }
requestApplicationData(type, new_options) _requestApplicationData(type, new_options)
.then(result => resolve(result)) .then(result => resolve(result))
.catch(error => reject(error)) .catch(error => reject(error))
} else { } else {
requestApplicationData(type, options).then(dataSet => { _requestApplicationData(type, options).then(dataSet => {
storeGeneral(fk, objectifier(dataSet)) storeGeneral(fk, objectifier(dataSet))
resolve(dataSet) resolve(dataSet)
}).catch(error => reject(error)) }).catch(error => reject(error))
@ -738,7 +803,7 @@
} }
delete options.callback; delete options.callback;
} }
requestApplicationData(objectName, options).then(dataSet => { _requestApplicationData(objectName, options).then(dataSet => {
updateObject(objectName, objectifier(dataSet)); updateObject(objectName, objectifier(dataSet));
delete options.comment; delete options.comment;
options.lowerVectorClock = lastVC[objectName] + 1; options.lowerVectorClock = lastVC[objectName] + 1;
@ -746,11 +811,11 @@
if (callback) { if (callback) {
let new_options = Object.create(options); let new_options = Object.create(options);
new_options.callback = callback; new_options.callback = callback;
requestApplicationData(objectName, new_options) _requestApplicationData(objectName, new_options)
.then(result => resolve(result)) .then(result => resolve(result))
.catch(error => reject(error)) .catch(error => reject(error))
} else { } else {
requestApplicationData(objectName, options).then(dataSet => { _requestApplicationData(objectName, options).then(dataSet => {
updateObject(objectName, objectifier(dataSet)) updateObject(objectName, objectifier(dataSet))
resolve(appObjects[objectName]) resolve(appObjects[objectName])
}).catch(error => reject(error)) }).catch(error => reject(error))
@ -825,7 +890,7 @@
floCloudAPI.downloadFile = function (vectorClock, options = {}) { floCloudAPI.downloadFile = function (vectorClock, options = {}) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
options.atVectorClock = vectorClock; options.atVectorClock = vectorClock;
requestApplicationData(options.type, options).then(result => { _requestApplicationData(options.type, options).then(result => {
if (!result.length) if (!result.length)
return reject("File not found"); return reject("File not found");
result = result[0]; result = result[0];

View File

@ -144,7 +144,7 @@
} }
}); });
var subAdmins, trustedIDs, settings; var subAdmins = [], trustedIDs = [], settings = {};
Object.defineProperties(floGlobals, { Object.defineProperties(floGlobals, {
subAdmins: { subAdmins: {
get: () => subAdmins get: () => subAdmins
@ -249,106 +249,154 @@
const startUpFunctions = []; const startUpFunctions = [];
startUpFunctions.push(function readSupernodeListFromAPI() { startUpFunctions.push(function readSupernodeListFromAPI() {
return new Promise((resolve, reject) => { return new Promise((resolve) => {
if (!startUpOptions.cloud) if (!startUpOptions.cloud)
return resolve("No cloud for this app"); return resolve("No cloud for this app");
const CLOUD_KEY = "floCloudAPI#" + floCloudAPI.SNStorageID; const CLOUD_KEY = "floCloudAPI#" + floCloudAPI.SNStorageID;
// Fallback: init from cached nodes (never reject)
const initFromCache = (tag) =>
compactIDB.readData("supernodes", CLOUD_KEY, DEFAULT.root)
.then(nodes => {
nodes = nodes || {};
return floCloudAPI.init(nodes)
.then(r => resolve(`${tag} (from cache)\n${r}`))
.catch(() => resolve(`${tag} (cache present, init skipped)`));
})
.catch(() => resolve(`${tag} (no cache)`));
compactIDB.readData("lastTx", CLOUD_KEY, DEFAULT.root).then(lastTx => { compactIDB.readData("lastTx", CLOUD_KEY, DEFAULT.root).then(lastTx => {
var query_options = { sentOnly: true, pattern: floCloudAPI.SNStorageName }; const query_options = { sentOnly: true, pattern: floCloudAPI.SNStorageName };
if (typeof lastTx == 'number') //lastTx is tx count (*backward support) if (typeof lastTx === 'number') // backward support (tx count)
query_options.ignoreOld = lastTx; query_options.ignoreOld = lastTx;
else if (typeof lastTx == 'string') //lastTx is txid of last tx else if (typeof lastTx === 'string') // last txid
query_options.after = lastTx; query_options.after = lastTx;
//fetch data from flosight
// Try online; if it fails, fall back to cache
floBlockchainAPI.readData(floCloudAPI.SNStorageID, query_options).then(result => { floBlockchainAPI.readData(floCloudAPI.SNStorageID, query_options).then(result => {
compactIDB.readData("supernodes", CLOUD_KEY, DEFAULT.root).then(nodes => { compactIDB.readData("supernodes", CLOUD_KEY, DEFAULT.root).then(nodes => {
nodes = nodes || {}; nodes = nodes || {};
for (var i = result.data.length - 1; i >= 0; i--) { for (let i = result.data.length - 1; i >= 0; i--) {
var content = JSON.parse(result.data[i])[floCloudAPI.SNStorageName]; const content = JSON.parse(result.data[i])[floCloudAPI.SNStorageName];
for (let sn in content.removeNodes) if (!content || typeof content !== 'object') continue;
delete nodes[sn]; if (content.removeNodes)
for (let sn in content.newNodes) for (let sn in content.removeNodes) delete nodes[sn];
nodes[sn] = content.newNodes[sn]; if (content.newNodes)
for (let sn in content.updateNodes) for (let sn in content.newNodes) nodes[sn] = content.newNodes[sn];
if (sn in nodes) //check if node is listed if (content.updateNodes)
nodes[sn].uri = content.updateNodes[sn]; for (let sn in content.updateNodes)
if (sn in nodes) nodes[sn].uri = content.updateNodes[sn];
} }
Promise.all([ Promise.all([
compactIDB.writeData("lastTx", result.lastItem, CLOUD_KEY, DEFAULT.root), compactIDB.writeData("lastTx", result.lastItem, CLOUD_KEY, DEFAULT.root),
compactIDB.writeData("supernodes", nodes, CLOUD_KEY, DEFAULT.root) compactIDB.writeData("supernodes", nodes, CLOUD_KEY, DEFAULT.root)
]).then(_ => { ]).then(() => {
floCloudAPI.init(nodes) floCloudAPI.init(nodes)
.then(result => resolve("Loaded Supernode list\n" + result)) .then(r => resolve("Loaded Supernode list\n" + r))
.catch(error => reject(error)) .catch(() => resolve("Loaded Supernode list (init deferred)"));
}).catch(error => reject(error)) }).catch(() => resolve("Supernode list updated (persist partial)"));
}).catch(error => reject(error)) }).catch(() => initFromCache("Supernode list read failed"));
}) }).catch(() => initFromCache("Supernode network fetch failed"));
}).catch(error => reject(error)) }).catch(() => initFromCache("Supernode lastTx read failed"));
}) });
}); });
startUpFunctions.push(function readAppConfigFromAPI() { startUpFunctions.push(function readAppConfigFromAPI() {
return new Promise((resolve, reject) => { return new Promise((resolve) => {
if (!startUpOptions.app_config) if (!startUpOptions.app_config)
return resolve("No configs for this app"); return resolve("No configs for this app");
compactIDB.readData("lastTx", `${DEFAULT.application}|${DEFAULT.adminID}`, DEFAULT.root).then(lastTx => {
var query_options = { sentOnly: true, pattern: DEFAULT.application }; // small helper: load cached directives into memory and resolve
if (typeof lastTx == 'number') //lastTx is tx count (*backward support) const loadFromIDB = (msg) => Promise.all([
query_options.ignoreOld = lastTx; compactIDB.readAllData("subAdmins"),
else if (typeof lastTx == 'string') //lastTx is txid of last tx compactIDB.readAllData("trustedIDs"),
query_options.after = lastTx; compactIDB.readAllData("settings")
//fetch data from flosight ]).then(([sub, trust, set]) => {
subAdmins = Object.keys(sub || {}); // arrays of IDs
trustedIDs = Object.keys(trust || {});
settings = set || {};
resolve(msg);
}).catch(() => {
// safe defaults if cache missing
subAdmins = []; trustedIDs = []; settings = {};
resolve(msg + " (no local cache)");
});
// If cloud is disabled, use cached config and move on
if (!startUpOptions.cloud)
return loadFromIDB("Read app configuration from local cache (offline)");
const lastKey = `${DEFAULT.application}|${DEFAULT.adminID}`;
// Try to read lastTx; on failure, just use cache (dont block startup)
compactIDB.readData("lastTx", lastKey, DEFAULT.root).then(lastTx => {
const query_options = { sentOnly: true, pattern: DEFAULT.application };
if (typeof lastTx === 'number') query_options.ignoreOld = lastTx;
else if (typeof lastTx === 'string') query_options.after = lastTx;
// Fetch deltas from chain; on failure, fall back to cache
floBlockchainAPI.readData(DEFAULT.adminID, query_options).then(result => { floBlockchainAPI.readData(DEFAULT.adminID, query_options).then(result => {
for (var i = result.data.length - 1; i >= 0; i--) { for (let i = result.data.length - 1; i >= 0; i--) {
var content = JSON.parse(result.data[i])[DEFAULT.application]; const content = JSON.parse(result.data[i])[DEFAULT.application];
if (!content || typeof content !== "object") if (!content || typeof content !== "object") continue;
continue;
if (Array.isArray(content.removeSubAdmin)) if (Array.isArray(content.removeSubAdmin))
for (var j = 0; j < content.removeSubAdmin.length; j++) for (let j = 0; j < content.removeSubAdmin.length; j++)
compactIDB.removeData("subAdmins", content.removeSubAdmin[j]); compactIDB.removeData("subAdmins", content.removeSubAdmin[j]);
if (Array.isArray(content.addSubAdmin)) if (Array.isArray(content.addSubAdmin))
for (var k = 0; k < content.addSubAdmin.length; k++) for (let k = 0; k < content.addSubAdmin.length; k++)
compactIDB.writeData("subAdmins", true, content.addSubAdmin[k]); compactIDB.writeData("subAdmins", true, content.addSubAdmin[k]);
if (Array.isArray(content.removeTrustedID)) if (Array.isArray(content.removeTrustedID))
for (var j = 0; j < content.removeTrustedID.length; j++) for (let j = 0; j < content.removeTrustedID.length; j++)
compactIDB.removeData("trustedIDs", content.removeTrustedID[j]); compactIDB.removeData("trustedIDs", content.removeTrustedID[j]);
if (Array.isArray(content.addTrustedID)) if (Array.isArray(content.addTrustedID))
for (var k = 0; k < content.addTrustedID.length; k++) for (let k = 0; k < content.addTrustedID.length; k++)
compactIDB.writeData("trustedIDs", true, content.addTrustedID[k]); compactIDB.writeData("trustedIDs", true, content.addTrustedID[k]);
if (content.settings) if (content.settings)
for (let l in content.settings) for (let l in content.settings)
compactIDB.writeData("settings", content.settings[l], l) compactIDB.writeData("settings", content.settings[l], l);
} }
compactIDB.writeData("lastTx", result.lastItem, `${DEFAULT.application}|${DEFAULT.adminID}`, DEFAULT.root);
compactIDB.readAllData("subAdmins").then(result => { // persist last item marker (best effort)
subAdmins = Object.keys(result); compactIDB.writeData("lastTx", result.lastItem, lastKey, DEFAULT.root).catch(() => {});
compactIDB.readAllData("trustedIDs").then(result => {
trustedIDs = Object.keys(result); // load fresh values from IDB into memory and finish
compactIDB.readAllData("settings").then(result => { loadFromIDB("Read app configuration from blockchain");
settings = result; }).catch(() => {
resolve("Read app configuration from blockchain"); // network failed → boot from cache
}) loadFromIDB("Read app configuration from local cache (network fail)");
}) });
}) }).catch(() => {
}) // couldn't read lastTx → still boot from cache
}).catch(error => reject(error)) loadFromIDB("Read app configuration from local cache (no lastTx)");
}) });
});
}); });
startUpFunctions.push(function loadDataFromAppIDB() { startUpFunctions.push(function loadDataFromAppIDB() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!startUpOptions.cloud) const loadData = ["appObjects", "generalData", "lastVC"];
return resolve("No cloud for this app");
var loadData = ["appObjects", "generalData", "lastVC"] // If cloud is disabled AND no IDB stores are expected, skip early
var promises = [] if (!startUpOptions.cloud && (!initIndexedDB.appObs || Object.keys(initIndexedDB.appObs).length === 0))
for (var i = 0; i < loadData.length; i++) return resolve("No cloud and no local data to load");
promises[i] = compactIDB.readAllData(loadData[i])
Promise.all(promises).then(results => { // Otherwise, read from IDB
for (var i = 0; i < loadData.length; i++) Promise.all(loadData.map(item => compactIDB.readAllData(item)))
floGlobals[loadData[i]] = results[i] .then(results => {
resolve("Loaded Data from app IDB") for (let i = 0; i < loadData.length; i++)
}).catch(error => reject(error)) floGlobals[loadData[i]] = results[i];
}) resolve("Loaded Data from app IDB");
})
.catch(error => reject(error));
});
}); });
var keyInput = type => new Promise((resolve, reject) => { var keyInput = type => new Promise((resolve, reject) => {

File diff suppressed because one or more lines are too long

1
cc/scripts/floTokenAPI.min.js vendored Normal file

File diff suppressed because one or more lines are too long