Compare commits

..

No commits in common. "457994c5068ca6045833fdc0bc7ecc96c1397d92" and "02d28d46bb7f6d3bfaeff1be3666de733b512396" have entirely different histories.

4 changed files with 49 additions and 41 deletions

View File

@ -376,8 +376,6 @@ export async function registerAllListeners() {
}
const state = services.getStateFromId(process, stateId);
await services.checkConnections(process, stateId);
let res: Record<string, any> = {};
if (state) {
// Decrypt all the data we have the key for

View File

@ -364,7 +364,7 @@ export class Database {
getRequest.onsuccess = () => resolve(getRequest.result);
getRequest.onerror = () => reject(getRequest.error);
});
return result ?? null; // Convert undefined to null
return result;
}
public async dumpStore(storeName: string): Promise<Record<string, any>> {

View File

@ -210,18 +210,15 @@ export default class Services {
// If we're updating a process, we must call that after update especially if roles are part of it
// We will take the roles from the last state, wheter it's commited or not
public async checkConnections(process: Process, stateId: string | null = null): Promise<void> {
public async checkConnections(process: Process): Promise<void> {
if (process.states.length < 2) {
throw new Error('Process doesn\'t have any state yet');
}
let roles: Record<string, RoleDefinition> | null = null;
if (!stateId) {
roles = process.states[process.states.length - 2].roles;
} else {
roles = process.states.find(state => state.state_id === stateId)?.roles || null;
}
let roles = process.states[process.states.length - 2].roles;
if (!roles) {
throw new Error('No roles found');
} else {
console.log('roles found', roles);
}
let members: Set<Member> = new Set();
for (const role of Object.values(roles!)) {
@ -258,7 +255,8 @@ export default class Services {
for (const address of sp_addresses) {
// For now, we ignore our own device address, although there might be use cases for having a secret with ourselves
if (address === myAddress) continue;
if (await this.getSecretForAddress(address) === null) {
const sharedSecret = await this.getSecretForAddress(address);
if (!sharedSecret) {
unconnectedAddresses.add(address);
}
}
@ -422,8 +420,7 @@ export default class Services {
);
if (result.updated_process) {
console.log('created process:', result.updated_process);
await this.checkConnections(result.updated_process.current_process);
await this.checkConnections(result.updated_process);
return(result);
} else {
throw new Error('Empty updated_process in createProcessReturn');
@ -451,7 +448,7 @@ export default class Services {
try {
const result = this.sdkClient.update_process(process, encodedPrivateData, roles, encodedPublicData, this.getAllMembers());
if (result.updated_process) {
await this.checkConnections(result.updated_process.current_process);
await this.checkConnections(result.updated_process);
return(result);
} else {
throw new Error('Empty updated_process in updateProcessReturn');
@ -465,11 +462,15 @@ export default class Services {
const process = await this.getProcess(processId);
if (!process) {
throw new Error('Unknown process');
} else {
await this.checkConnections(process);
}
try {
return this.sdkClient.create_update_message(process, stateId, this.getAllMembers());
const result = this.sdkClient.create_update_message(process, stateId, this.getAllMembers());
if (result.updated_process) {
await this.checkConnections(result.updated_process);
return(result);
} else {
throw new Error('Empty updated_process in createPrdUpdateReturn');
}
} catch (e) {
throw new Error(`Failed to create prd update: ${e}`);
}
@ -495,7 +496,7 @@ export default class Services {
try {
const result = this.sdkClient.validate_state(process, stateId, this.getAllMembers());
if (result.updated_process) {
await this.checkConnections(result.updated_process.current_process);
await this.checkConnections(result.updated_process);
return(result);
} else {
throw new Error('Empty updated_process in approveChangeReturn');
@ -1076,6 +1077,12 @@ export default class Services {
return await retrieveData(storages, hash);
}
public async testDataInStorage(hash: string): Promise<Record<string, boolean | null> | null> {
const storages = [STORAGEURL];
return await testData(storages, hash);
}
public async getDiffByValueFromDb(hash: string): Promise<UserDiff | null> {
const db = await Database.getInstance();
const diff = await db.getObject('diffs', hash);
@ -1455,7 +1462,6 @@ export default class Services {
}
if (newStates.length != 0) {
await this.checkConnections(existing);
await this.requestDataFromPeers(processId, newStates, newRoles);
}
// Otherwise we're probably just in the initial loading at page initialization

View File

@ -20,15 +20,6 @@ export async function storeData(servers: string[], key: string, value: Blob, ttl
url = urlObj.toString();
}
// Test first that data is not already stored
const testResponse = await testData(url, key);
if (testResponse) {
console.log('Data already stored:', key);
continue;
} else {
console.log('Data not stored for server:', key, server);
}
// Send the encrypted ArrayBuffer as the raw request body.
const response = await axios.post(url, value, {
headers: {
@ -103,17 +94,30 @@ interface TestResponse {
value: boolean;
}
export async function testData(url: string, key: string): Promise<boolean | null> {
export async function testData(servers: string[], key: string): Promise<Record<string, boolean | null> | null> {
const res: Record<string, boolean | null> = {};
for (const server of servers) {
res[server] = null;
try {
// Handle relative paths (for development proxy) vs absolute URLs (for production)
const url = server.startsWith('/')
? `${server}/test/${key}` // Relative path - use as-is for proxy
: new URL(`${server}/test/${key}`).toString(); // Absolute URL - construct properly
const response = await axios.get(url);
if (response.status !== 200) {
console.error(`Test response status: ${response.status}`);
return false;
console.error(`${server}: Test response status: ${response.status}`);
continue;
}
return true;
const data: TestResponse = response.data;
res[server] = data.value;
} catch (error) {
console.error('Error testing data:', error);
console.error('Error retrieving data:', error);
return null;
}
}
return res;
}