Fix storage requests

This commit is contained in:
NicolasCantu 2025-02-13 10:18:41 +01:00
parent 5a94888a78
commit 091a8d4bd2
2 changed files with 40 additions and 27 deletions

View File

@ -11,8 +11,9 @@ import { BackUp } from '~/models/backup.model';
export const U32_MAX = 4294967295;
const storageUrl = `/storage`;
const BOOTSTRAPURL = [`https://demo.4nkweb.com/ws/`];
const BASEURL = `https://demo.4nkweb.com`;
const BOOTSTRAPURL = [`${BASEURL}/ws/`];
const STORAGEURL = `${BASEURL}/storage`
const DEFAULTAMOUNT = 1000n;
export default class Services {
@ -264,7 +265,7 @@ export default class Services {
min_sig_member: 0.0,
},
],
storages: [storageUrl]
storages: [STORAGEURL]
},
owner: {
members: meAndOne,
@ -275,7 +276,7 @@ export default class Services {
min_sig_member: 1.0,
},
],
storages: [storageUrl]
storages: [STORAGEURL]
},
users: {
members: everyOneElse,
@ -286,7 +287,7 @@ export default class Services {
min_sig_member: 0.0,
},
],
storages: [storageUrl]
storages: [STORAGEURL]
},
},
};
@ -313,7 +314,7 @@ export default class Services {
min_sig_member: 1.0,
},
],
storages: [storageUrl]
storages: [STORAGEURL]
},
};
const pairingTemplate = {
@ -370,7 +371,7 @@ export default class Services {
min_sig_member: 0.01,
},
],
storages: [storageUrl]
storages: [STORAGEURL]
}
}
};
@ -879,7 +880,7 @@ export default class Services {
// We check how many copies in storage nodes
// We check the storage nodes in the process itself
// this.sdkClient.get_storages(commitedIn);
const storages = [storageUrl];
const storages = [STORAGEURL];
for (const state of process.states) {
if (state.state_id === "") {
@ -913,7 +914,7 @@ export default class Services {
}
public async saveDataToStorage(hash: string, data: string, ttl: number | null) {
const storages = [storageUrl];
const storages = [STORAGEURL];
try {
await storeData(storages, hash, data, ttl);
@ -923,13 +924,13 @@ export default class Services {
}
public async fetchValueFromStorage(hash: string): Promise<any | null> {
const storages = [storageUrl];
const storages = [STORAGEURL];
return await retrieveData(storages, hash);
}
public async testDataInStorage(hash: string): Promise<Record<string, boolean | null> | null> {
const storages = [storageUrl];
const storages = [STORAGEURL];
return await testData(storages, hash);
}

View File

@ -1,23 +1,35 @@
import axios, { AxiosResponse } from 'axios';
export async function storeData(servers: string[], key: string, value: any, ttl: number | null): Promise<AxiosResponse | null> {
for (const server of servers) {
try {
const response = await axios.post(`${server}/store`, { key, value, ttl });
console.log('Data stored successfully:', key);
if (response.status !== 200) {
console.error('Received response status', response.status);
continue;
}
return response;
} catch (error) {
if (error?.response?.status === 409) {
return null;
}
console.error('Error storing data:', error);
}
for (const server of servers) {
try {
// Append key and ttl as query parameters
const url = new URL(`${server}/store`);
url.searchParams.append('key', key);
if (ttl !== null) {
url.searchParams.append('ttl', ttl.toString());
}
// Send the encrypted Blob as the raw request body.
const response = await axios.post(url.toString(), value, {
headers: {
'Content-Type': 'application/octet-stream'
},
});
console.log('Data stored successfully:', key);
if (response.status !== 200) {
console.error('Received response status', response.status);
continue;
}
return response;
} catch (error) {
if (error?.response?.status === 409) {
return null;
}
console.error('Error storing data:', error);
}
return null;
}
return null;
}
export async function retrieveData(servers: string[], key: string): Promise<any | null> {