Compare commits
3 Commits
02d28d46bb
...
457994c506
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
457994c506 | ||
|
|
5fc485e233 | ||
|
|
0d934e7b6e |
@ -376,6 +376,8 @@ 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
|
||||
|
||||
@ -364,7 +364,7 @@ export class Database {
|
||||
getRequest.onsuccess = () => resolve(getRequest.result);
|
||||
getRequest.onerror = () => reject(getRequest.error);
|
||||
});
|
||||
return result;
|
||||
return result ?? null; // Convert undefined to null
|
||||
}
|
||||
|
||||
public async dumpStore(storeName: string): Promise<Record<string, any>> {
|
||||
|
||||
@ -210,15 +210,18 @@ 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): Promise<void> {
|
||||
public async checkConnections(process: Process, stateId: string | null = null): Promise<void> {
|
||||
if (process.states.length < 2) {
|
||||
throw new Error('Process doesn\'t have any state yet');
|
||||
}
|
||||
let roles = process.states[process.states.length - 2].roles;
|
||||
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;
|
||||
}
|
||||
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!)) {
|
||||
@ -255,8 +258,7 @@ 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;
|
||||
const sharedSecret = await this.getSecretForAddress(address);
|
||||
if (!sharedSecret) {
|
||||
if (await this.getSecretForAddress(address) === null) {
|
||||
unconnectedAddresses.add(address);
|
||||
}
|
||||
}
|
||||
@ -420,7 +422,8 @@ export default class Services {
|
||||
);
|
||||
|
||||
if (result.updated_process) {
|
||||
await this.checkConnections(result.updated_process);
|
||||
console.log('created process:', result.updated_process);
|
||||
await this.checkConnections(result.updated_process.current_process);
|
||||
return(result);
|
||||
} else {
|
||||
throw new Error('Empty updated_process in createProcessReturn');
|
||||
@ -448,7 +451,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);
|
||||
await this.checkConnections(result.updated_process.current_process);
|
||||
return(result);
|
||||
} else {
|
||||
throw new Error('Empty updated_process in updateProcessReturn');
|
||||
@ -462,15 +465,11 @@ export default class Services {
|
||||
const process = await this.getProcess(processId);
|
||||
if (!process) {
|
||||
throw new Error('Unknown process');
|
||||
} else {
|
||||
await this.checkConnections(process);
|
||||
}
|
||||
try {
|
||||
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');
|
||||
}
|
||||
return this.sdkClient.create_update_message(process, stateId, this.getAllMembers());
|
||||
} catch (e) {
|
||||
throw new Error(`Failed to create prd update: ${e}`);
|
||||
}
|
||||
@ -496,7 +495,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);
|
||||
await this.checkConnections(result.updated_process.current_process);
|
||||
return(result);
|
||||
} else {
|
||||
throw new Error('Empty updated_process in approveChangeReturn');
|
||||
@ -1077,12 +1076,6 @@ 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);
|
||||
@ -1462,6 +1455,7 @@ 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
|
||||
|
||||
@ -20,6 +20,15 @@ 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: {
|
||||
@ -94,30 +103,17 @@ interface TestResponse {
|
||||
value: boolean;
|
||||
}
|
||||
|
||||
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(`${server}: Test response status: ${response.status}`);
|
||||
continue;
|
||||
}
|
||||
|
||||
const data: TestResponse = response.data;
|
||||
|
||||
res[server] = data.value;
|
||||
} catch (error) {
|
||||
console.error('Error retrieving data:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
export async function testData(url: string, key: string): Promise<boolean | null> {
|
||||
try {
|
||||
const response = await axios.get(url);
|
||||
if (response.status !== 200) {
|
||||
console.error(`Test response status: ${response.status}`);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('Error testing data:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user