Compare commits
19 Commits
3eeef3fc9a
...
6fa04317b6
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6fa04317b6 | ||
|
|
66a27cb03c | ||
|
|
9c8c5fc24c | ||
|
|
c1802c9c59 | ||
|
|
93a4fd17c7 | ||
|
|
8a356fe2fd | ||
|
|
59fff148ac | ||
|
|
54dcd2b29d | ||
|
|
245341e3f5 | ||
|
|
f675dc01ae | ||
|
|
414f8e5dca | ||
|
|
f74fcabec7 | ||
|
|
71d0d14095 | ||
|
|
d161402e1e | ||
|
|
92a9c6e455 | ||
|
|
29d2688278 | ||
|
|
94fa55c2a1 | ||
|
|
739f749619 | ||
|
|
19dcb2000e |
@ -1,281 +1,152 @@
|
||||
const EMPTY32BYTES = String('').padStart(64, '0');
|
||||
|
||||
self.addEventListener('install', (event) => {
|
||||
event.waitUntil(self.skipWaiting()); // Activate worker immediately
|
||||
});
|
||||
|
||||
self.addEventListener('activate', (event) => {
|
||||
event.waitUntil(self.clients.claim()); // Become available to all pages
|
||||
});
|
||||
|
||||
// Event listener for messages from clients
|
||||
self.addEventListener('message', async (event) => {
|
||||
const data = event.data;
|
||||
console.log(data);
|
||||
|
||||
if (data.type === 'SCAN') {
|
||||
try {
|
||||
const myProcessesId = data.payload;
|
||||
if (myProcessesId && myProcessesId.length != 0) {
|
||||
const toDownload = await scanMissingData(myProcessesId);
|
||||
if (toDownload.length != 0) {
|
||||
console.log('Sending TO_DOWNLOAD message');
|
||||
event.source.postMessage({ type: 'TO_DOWNLOAD', data: toDownload});
|
||||
}
|
||||
} else {
|
||||
event.source.postMessage({ status: 'error', message: 'Empty lists' });
|
||||
}
|
||||
} catch (error) {
|
||||
event.source.postMessage({ status: 'error', message: error.message });
|
||||
}
|
||||
} else if (data.type === 'ADD_OBJECT') {
|
||||
try {
|
||||
const { storeName, object, key } = data.payload;
|
||||
const db = await openDatabase();
|
||||
const tx = db.transaction(storeName, 'readwrite');
|
||||
const store = tx.objectStore(storeName);
|
||||
|
||||
if (key) {
|
||||
await store.put(object, key);
|
||||
} else {
|
||||
await store.put(object);
|
||||
}
|
||||
|
||||
event.ports[0].postMessage({ status: 'success', message: '' });
|
||||
} catch (error) {
|
||||
event.ports[0].postMessage({ status: 'error', message: error.message });
|
||||
}
|
||||
} else if (data.type === 'BATCH_WRITING') {
|
||||
const { storeName, objects } = data.payload;
|
||||
const db = await openDatabase();
|
||||
const tx = db.transaction(storeName, 'readwrite');
|
||||
const store = tx.objectStore(storeName);
|
||||
|
||||
for (const { key, object } of objects) {
|
||||
if (key) {
|
||||
await store.put(object, key);
|
||||
} else {
|
||||
await store.put(object);
|
||||
}
|
||||
}
|
||||
|
||||
await tx.done;
|
||||
}
|
||||
});
|
||||
|
||||
async function scanMissingData(processesToScan) {
|
||||
console.log('Scanning for missing data...');
|
||||
const myProcesses = await getProcesses(processesToScan);
|
||||
|
||||
let toDownload = new Set();
|
||||
// Iterate on each process
|
||||
if (myProcesses && myProcesses.length != 0) {
|
||||
for (const process of myProcesses) {
|
||||
// Iterate on states
|
||||
const firstState = process.states[0];
|
||||
const processId = firstState.commited_in;
|
||||
for (const state of process.states) {
|
||||
if (state.state_id === EMPTY32BYTES) continue;
|
||||
// iterate on pcd_commitment
|
||||
for (const [field, hash] of Object.entries(state.pcd_commitment)) {
|
||||
// Skip public fields
|
||||
if (state.public_data[field] !== undefined || field === 'roles') continue;
|
||||
// Check if we have the data in db
|
||||
const existingData = await getBlob(hash);
|
||||
if (!existingData) {
|
||||
toDownload.add(hash);
|
||||
// We also add an entry in diff, in case it doesn't already exist
|
||||
await addDiff(processId, state.state_id, hash, state.roles, field);
|
||||
} else {
|
||||
// We remove it if we have it in the set
|
||||
if (toDownload.delete(hash)) {
|
||||
console.log(`Removing ${hash} from the set`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log(toDownload);
|
||||
return Array.from(toDownload);
|
||||
}
|
||||
|
||||
async function openDatabase() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const request = indexedDB.open('4nk', 1);
|
||||
request.onerror = (event) => {
|
||||
reject(request.error);
|
||||
};
|
||||
request.onsuccess = (event) => {
|
||||
resolve(request.result);
|
||||
};
|
||||
request.onupgradeneeded = (event) => {
|
||||
const db = event.target.result;
|
||||
if (!db.objectStoreNames.contains('wallet')) {
|
||||
db.createObjectStore('wallet', { keyPath: 'pre_id' });
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
// Function to get all processes because it is asynchronous
|
||||
async function getAllProcesses() {
|
||||
const db = await openDatabase();
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!db) {
|
||||
reject(new Error('Database is not available'));
|
||||
return;
|
||||
}
|
||||
const tx = db.transaction('processes', 'readonly');
|
||||
const store = tx.objectStore('processes');
|
||||
const request = store.getAll();
|
||||
|
||||
request.onsuccess = () => {
|
||||
resolve(request.result);
|
||||
};
|
||||
|
||||
request.onerror = () => {
|
||||
reject(request.error);
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
async function getProcesses(processIds) {
|
||||
if (!processIds || processIds.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const db = await openDatabase();
|
||||
if (!db) {
|
||||
throw new Error('Database is not available');
|
||||
}
|
||||
|
||||
const tx = db.transaction('processes', 'readonly');
|
||||
const store = tx.objectStore('processes');
|
||||
|
||||
const requests = Array.from(processIds).map((processId) => {
|
||||
return new Promise((resolve) => {
|
||||
const request = store.get(processId);
|
||||
request.onsuccess = () => resolve(request.result);
|
||||
request.onerror = () => {
|
||||
console.error(`Error fetching process ${processId}:`, request.error);
|
||||
resolve(undefined);
|
||||
};
|
||||
});
|
||||
});
|
||||
|
||||
const results = await Promise.all(requests);
|
||||
return results.filter(result => result !== undefined);
|
||||
}
|
||||
|
||||
async function getAllDiffsNeedValidation() {
|
||||
const db = await openDatabase();
|
||||
|
||||
const allProcesses = await getAllProcesses();
|
||||
const tx = db.transaction('diffs', 'readonly');
|
||||
const store = tx.objectStore('diffs');
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const request = store.getAll();
|
||||
request.onsuccess = (event) => {
|
||||
const allItems = event.target.result;
|
||||
const itemsWithFlag = allItems.filter((item) => item.need_validation);
|
||||
|
||||
const processMap = {};
|
||||
|
||||
for (const diff of itemsWithFlag) {
|
||||
const currentProcess = allProcesses.find((item) => {
|
||||
return item.states.some((state) => state.merkle_root === diff.new_state_merkle_root);
|
||||
});
|
||||
|
||||
if (currentProcess) {
|
||||
const processKey = currentProcess.merkle_root;
|
||||
|
||||
if (!processMap[processKey]) {
|
||||
processMap[processKey] = {
|
||||
process: currentProcess.states,
|
||||
processId: currentProcess.key,
|
||||
diffs: [],
|
||||
};
|
||||
}
|
||||
processMap[processKey].diffs.push(diff);
|
||||
}
|
||||
}
|
||||
|
||||
const results = Object.values(processMap).map((entry) => {
|
||||
const diffs = []
|
||||
for(const state of entry.process) {
|
||||
const filteredDiff = entry.diffs.filter(diff => diff.new_state_merkle_root === state.merkle_root);
|
||||
if(filteredDiff && filteredDiff.length) {
|
||||
diffs.push(filteredDiff)
|
||||
}
|
||||
}
|
||||
return {
|
||||
process: entry.process,
|
||||
processId: entry.processId,
|
||||
diffs: diffs,
|
||||
};
|
||||
});
|
||||
|
||||
resolve(results);
|
||||
};
|
||||
|
||||
request.onerror = (event) => {
|
||||
reject(event.target.error);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
async function getBlob(hash) {
|
||||
const db = await openDatabase();
|
||||
const storeName = 'data';
|
||||
const tx = db.transaction(storeName, 'readonly');
|
||||
const store = tx.objectStore(storeName);
|
||||
const result = await new Promise((resolve, reject) => {
|
||||
const getRequest = store.get(hash);
|
||||
getRequest.onsuccess = () => resolve(getRequest.result);
|
||||
getRequest.onerror = () => reject(getRequest.error);
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
async function addDiff(processId, stateId, hash, roles, field) {
|
||||
const db = await openDatabase();
|
||||
const storeName = 'diffs';
|
||||
const tx = db.transaction(storeName, 'readwrite');
|
||||
const store = tx.objectStore(storeName);
|
||||
|
||||
// Check if the diff already exists
|
||||
const existingDiff = await new Promise((resolve, reject) => {
|
||||
const getRequest = store.get(hash);
|
||||
getRequest.onsuccess = () => resolve(getRequest.result);
|
||||
getRequest.onerror = () => reject(getRequest.error);
|
||||
});
|
||||
|
||||
if (!existingDiff) {
|
||||
const newDiff = {
|
||||
process_id: processId,
|
||||
state_id: stateId,
|
||||
value_commitment: hash,
|
||||
roles: roles,
|
||||
field: field,
|
||||
description: null,
|
||||
previous_value: null,
|
||||
new_value: null,
|
||||
notify_user: false,
|
||||
need_validation: false,
|
||||
validation_status: 'None'
|
||||
};
|
||||
|
||||
const insertResult = await new Promise((resolve, reject) => {
|
||||
const putRequest = store.put(newDiff);
|
||||
putRequest.onsuccess = () => resolve(putRequest.result);
|
||||
putRequest.onerror = () => reject(putRequest.error);
|
||||
});
|
||||
|
||||
return insertResult;
|
||||
}
|
||||
|
||||
return existingDiff;
|
||||
}
|
||||
const EMPTY32BYTES = String('').padStart(64, '0');
|
||||
|
||||
// ============================================
|
||||
// SERVICE WORKER LIFECYCLE
|
||||
// ============================================
|
||||
|
||||
self.addEventListener('install', (event) => {
|
||||
event.waitUntil(self.skipWaiting());
|
||||
});
|
||||
|
||||
self.addEventListener('activate', (event) => {
|
||||
event.waitUntil(self.clients.claim());
|
||||
});
|
||||
|
||||
// ============================================
|
||||
// MESSAGE HANDLER
|
||||
// ============================================
|
||||
|
||||
self.addEventListener('message', async (event) => {
|
||||
const data = event.data;
|
||||
console.log('[Service Worker] Message received:', data.type);
|
||||
|
||||
if (data.type === 'SCAN') {
|
||||
try {
|
||||
const myProcessesId = data.payload;
|
||||
if (myProcessesId && myProcessesId.length != 0) {
|
||||
const scanResult = await scanMissingData(myProcessesId, event.source);
|
||||
|
||||
if (scanResult.toDownload.length != 0) {
|
||||
console.log('[Service Worker] Sending TO_DOWNLOAD message');
|
||||
event.source.postMessage({ type: 'TO_DOWNLOAD', data: scanResult.toDownload });
|
||||
}
|
||||
|
||||
if (scanResult.diffsToCreate.length > 0) {
|
||||
console.log('[Service Worker] Sending DIFFS_TO_CREATE message');
|
||||
event.source.postMessage({ type: 'DIFFS_TO_CREATE', data: scanResult.diffsToCreate });
|
||||
}
|
||||
} else {
|
||||
event.source.postMessage({ status: 'error', message: 'Empty lists' });
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[Service Worker] Scan error:', error);
|
||||
event.source.postMessage({ status: 'error', message: error.message });
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// ============================================
|
||||
// DATABASE COMMUNICATION
|
||||
// ============================================
|
||||
|
||||
async function requestFromMainThread(client, action, payload) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const messageId = `sw_${Date.now()}_${Math.random()}`;
|
||||
|
||||
const messageHandler = (event) => {
|
||||
if (event.data.id === messageId) {
|
||||
self.removeEventListener('message', messageHandler);
|
||||
if (event.data.type === 'DB_RESPONSE') {
|
||||
resolve(event.data.result);
|
||||
} else if (event.data.type === 'DB_ERROR') {
|
||||
reject(new Error(event.data.error));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
self.addEventListener('message', messageHandler);
|
||||
|
||||
client.postMessage({
|
||||
type: 'DB_REQUEST',
|
||||
id: messageId,
|
||||
action,
|
||||
payload
|
||||
});
|
||||
|
||||
setTimeout(() => {
|
||||
self.removeEventListener('message', messageHandler);
|
||||
reject(new Error('Database request timeout'));
|
||||
}, 10000);
|
||||
});
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// SCAN LOGIC
|
||||
// ============================================
|
||||
|
||||
async function scanMissingData(processesToScan, client) {
|
||||
console.log('[Service Worker] Scanning for missing data...');
|
||||
|
||||
const myProcesses = await requestFromMainThread(client, 'GET_MULTIPLE_OBJECTS', {
|
||||
storeName: 'processes',
|
||||
keys: processesToScan
|
||||
});
|
||||
|
||||
let toDownload = new Set();
|
||||
let diffsToCreate = [];
|
||||
|
||||
if (myProcesses && myProcesses.length != 0) {
|
||||
for (const process of myProcesses) {
|
||||
const firstState = process.states[0];
|
||||
const processId = firstState.commited_in;
|
||||
for (const state of process.states) {
|
||||
if (state.state_id === EMPTY32BYTES) continue;
|
||||
|
||||
for (const [field, hash] of Object.entries(state.pcd_commitment)) {
|
||||
if (state.public_data[field] !== undefined || field === 'roles') continue;
|
||||
|
||||
const existingData = await requestFromMainThread(client, 'GET_OBJECT', {
|
||||
storeName: 'data',
|
||||
key: hash
|
||||
});
|
||||
|
||||
if (!existingData) {
|
||||
toDownload.add(hash);
|
||||
|
||||
const existingDiff = await requestFromMainThread(client, 'GET_OBJECT', {
|
||||
storeName: 'diffs',
|
||||
key: hash
|
||||
});
|
||||
|
||||
if (!existingDiff) {
|
||||
diffsToCreate.push({
|
||||
process_id: processId,
|
||||
state_id: state.state_id,
|
||||
value_commitment: hash,
|
||||
roles: state.roles,
|
||||
field: field,
|
||||
description: null,
|
||||
previous_value: null,
|
||||
new_value: null,
|
||||
notify_user: false,
|
||||
need_validation: false,
|
||||
validation_status: 'None'
|
||||
});
|
||||
}
|
||||
} else {
|
||||
if (toDownload.delete(hash)) {
|
||||
console.log(`[Service Worker] Removing ${hash} from the set`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log('[Service Worker] Scan complete:', { toDownload: toDownload.size, diffsToCreate: diffsToCreate.length });
|
||||
return {
|
||||
toDownload: Array.from(toDownload),
|
||||
diffsToCreate: diffsToCreate
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@ -9,13 +9,12 @@ async function bootstrap() {
|
||||
console.log("🚀 Démarrage de l'application 4NK...");
|
||||
|
||||
try {
|
||||
// 1. Initialisation de la Base de données
|
||||
const db = await Database.getInstance();
|
||||
db.registerServiceWorker('/database.worker.js');
|
||||
|
||||
// 2. Initialisation des Services (WASM, Sockets...)
|
||||
// 1. Initialisation des Services (WASM, Sockets, Database...)
|
||||
const services = await Services.getInstance();
|
||||
|
||||
// 2. Initialisation de la base de données (Web Worker + Service Worker)
|
||||
await Database.getInstance();
|
||||
|
||||
// Injection du Header dans le slot prévu dans index.html
|
||||
const headerSlot = document.getElementById('header-slot');
|
||||
if (headerSlot) {
|
||||
|
||||
@ -1,467 +1,463 @@
|
||||
import Services from './service';
|
||||
|
||||
export class Database {
|
||||
private static instance: Database;
|
||||
private db: IDBDatabase | null = null;
|
||||
private dbName: string = '4nk';
|
||||
private dbVersion: number = 1;
|
||||
private serviceWorkerRegistration: ServiceWorkerRegistration | null = null;
|
||||
private messageChannel: MessageChannel | null = null;
|
||||
private messageChannelForGet: MessageChannel | null = null;
|
||||
private serviceWorkerCheckIntervalId: number | null = null;
|
||||
private storeDefinitions = {
|
||||
AnkLabels: {
|
||||
name: 'labels',
|
||||
options: { keyPath: 'emoji' },
|
||||
indices: [],
|
||||
},
|
||||
AnkWallet: {
|
||||
name: 'wallet',
|
||||
options: { keyPath: 'pre_id' },
|
||||
indices: [],
|
||||
},
|
||||
AnkProcess: {
|
||||
name: 'processes',
|
||||
options: {},
|
||||
indices: [],
|
||||
},
|
||||
AnkSharedSecrets: {
|
||||
name: 'shared_secrets',
|
||||
options: {},
|
||||
indices: [],
|
||||
},
|
||||
AnkUnconfirmedSecrets: {
|
||||
name: 'unconfirmed_secrets',
|
||||
options: { autoIncrement: true },
|
||||
indices: [],
|
||||
},
|
||||
AnkPendingDiffs: {
|
||||
name: 'diffs',
|
||||
options: { keyPath: 'value_commitment' },
|
||||
indices: [
|
||||
{ name: 'byStateId', keyPath: 'state_id', options: { unique: false } },
|
||||
{ name: 'byNeedValidation', keyPath: 'need_validation', options: { unique: false } },
|
||||
{ name: 'byStatus', keyPath: 'validation_status', options: { unique: false } },
|
||||
],
|
||||
},
|
||||
AnkData: {
|
||||
name: 'data',
|
||||
options: {},
|
||||
indices: [],
|
||||
},
|
||||
};
|
||||
|
||||
// Private constructor to prevent direct instantiation from outside
|
||||
private constructor() {}
|
||||
|
||||
// Method to access the singleton instance of Database
|
||||
public static async getInstance(): Promise<Database> {
|
||||
if (!Database.instance) {
|
||||
Database.instance = new Database();
|
||||
await Database.instance.init();
|
||||
}
|
||||
return Database.instance;
|
||||
}
|
||||
|
||||
// Initialize the database
|
||||
private async init(): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const request = indexedDB.open(this.dbName, this.dbVersion);
|
||||
|
||||
request.onupgradeneeded = () => {
|
||||
const db = request.result;
|
||||
|
||||
Object.values(this.storeDefinitions).forEach(({ name, options, indices }) => {
|
||||
if (!db.objectStoreNames.contains(name)) {
|
||||
let store = db.createObjectStore(name, options as IDBObjectStoreParameters);
|
||||
|
||||
indices.forEach(({ name, keyPath, options }) => {
|
||||
store.createIndex(name, keyPath, options);
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
request.onsuccess = async () => {
|
||||
this.db = request.result;
|
||||
resolve();
|
||||
};
|
||||
|
||||
request.onerror = () => {
|
||||
console.error('Database error:', request.error);
|
||||
reject(request.error);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
public async getDb(): Promise<IDBDatabase> {
|
||||
if (!this.db) {
|
||||
await this.init();
|
||||
}
|
||||
return this.db!;
|
||||
}
|
||||
|
||||
public getStoreList(): { [key: string]: string } {
|
||||
const objectList: { [key: string]: string } = {};
|
||||
Object.keys(this.storeDefinitions).forEach((key) => {
|
||||
objectList[key] = this.storeDefinitions[key as keyof typeof this.storeDefinitions].name;
|
||||
});
|
||||
return objectList;
|
||||
}
|
||||
|
||||
public async registerServiceWorker(path: string) {
|
||||
if (!('serviceWorker' in navigator)) return;
|
||||
console.log('[Database] Initialisation du Service Worker sur :', path);
|
||||
|
||||
try {
|
||||
// 1. NETTOYAGE DES ANCIENS WORKERS (ZOMBIES)
|
||||
const registrations = await navigator.serviceWorker.getRegistrations();
|
||||
|
||||
for (const registration of registrations) {
|
||||
const scriptURL = registration.active?.scriptURL || registration.installing?.scriptURL || registration.waiting?.scriptURL;
|
||||
const scope = registration.scope;
|
||||
|
||||
// On détecte spécifiquement l'ancien dossier qui pose problème
|
||||
// L'erreur mentionne : scope ('.../src/service-workers/')
|
||||
if (scope.includes('/src/service-workers/') || (scriptURL && scriptURL.includes('/src/service-workers/'))) {
|
||||
console.warn(`[Database] 🚨 ANCIEN Service Worker détecté (${scope}). Suppression immédiate...`);
|
||||
await registration.unregister();
|
||||
// On continue la boucle, ne pas retourner ici, il faut installer le nouveau après
|
||||
}
|
||||
}
|
||||
|
||||
// 2. INSTALLATION DU NOUVEAU WORKER (PROPRE)
|
||||
// On vérifie s'il est déjà installé à la BONNE adresse
|
||||
const existingValidWorker = registrations.find((r) => {
|
||||
const url = r.active?.scriptURL || r.installing?.scriptURL || r.waiting?.scriptURL;
|
||||
// On compare la fin de l'URL pour éviter les soucis http/https/localhost
|
||||
return url && url.endsWith(path.replace(/^\//, ''));
|
||||
});
|
||||
|
||||
if (!existingValidWorker) {
|
||||
console.log('[Database] Enregistrement du nouveau Service Worker...');
|
||||
this.serviceWorkerRegistration = await navigator.serviceWorker.register(path, { type: 'module', scope: '/' });
|
||||
} else {
|
||||
console.log('[Database] Service Worker déjà actif et valide.');
|
||||
this.serviceWorkerRegistration = existingValidWorker;
|
||||
await this.serviceWorkerRegistration.update();
|
||||
}
|
||||
// Set up listeners
|
||||
navigator.serviceWorker.addEventListener('message', async (event) => {
|
||||
// console.log('Received message from service worker:', event.data);
|
||||
await this.handleServiceWorkerMessage(event.data);
|
||||
});
|
||||
|
||||
// Periodic check
|
||||
if (this.serviceWorkerCheckIntervalId) clearInterval(this.serviceWorkerCheckIntervalId);
|
||||
this.serviceWorkerCheckIntervalId = window.setInterval(async () => {
|
||||
const activeWorker = this.serviceWorkerRegistration?.active || (await this.waitForServiceWorkerActivation(this.serviceWorkerRegistration!));
|
||||
const service = await Services.getInstance();
|
||||
const payload = await service.getMyProcesses();
|
||||
if (payload && payload.length != 0) {
|
||||
activeWorker?.postMessage({ type: 'SCAN', payload });
|
||||
}
|
||||
}, 5000);
|
||||
} catch (error) {
|
||||
console.error('[Database] 💥 Erreur critique Service Worker:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to wait for service worker activation
|
||||
private async waitForServiceWorkerActivation(registration: ServiceWorkerRegistration): Promise<ServiceWorker | null> {
|
||||
return new Promise((resolve) => {
|
||||
if (registration.active) {
|
||||
resolve(registration.active);
|
||||
} else {
|
||||
const listener = () => {
|
||||
if (registration.active) {
|
||||
navigator.serviceWorker.removeEventListener('controllerchange', listener);
|
||||
resolve(registration.active);
|
||||
}
|
||||
};
|
||||
navigator.serviceWorker.addEventListener('controllerchange', listener);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private async checkForUpdates() {
|
||||
if (this.serviceWorkerRegistration) {
|
||||
// Check for updates to the service worker
|
||||
try {
|
||||
await this.serviceWorkerRegistration.update();
|
||||
|
||||
// If there's a new worker waiting, activate it immediately
|
||||
if (this.serviceWorkerRegistration.waiting) {
|
||||
this.serviceWorkerRegistration.waiting.postMessage({ type: 'SKIP_WAITING' });
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error checking for service worker updates:', error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async handleServiceWorkerMessage(message: any) {
|
||||
switch (message.type) {
|
||||
case 'TO_DOWNLOAD':
|
||||
await this.handleDownloadList(message.data);
|
||||
break;
|
||||
default:
|
||||
console.warn('Unknown message type received from service worker:', message);
|
||||
}
|
||||
}
|
||||
|
||||
private async handleDownloadList(downloadList: string[]): Promise<void> {
|
||||
// Download the missing data
|
||||
let requestedStateId: string[] = [];
|
||||
const service = await Services.getInstance();
|
||||
for (const hash of downloadList) {
|
||||
const diff = await service.getDiffByValue(hash);
|
||||
if (!diff) {
|
||||
// This should never happen
|
||||
console.warn(`Missing a diff for hash ${hash}`);
|
||||
continue;
|
||||
}
|
||||
const processId = diff.process_id;
|
||||
const stateId = diff.state_id;
|
||||
const roles = diff.roles;
|
||||
try {
|
||||
const valueBytes = await service.fetchValueFromStorage(hash);
|
||||
if (valueBytes) {
|
||||
// Save data to db
|
||||
const blob = new Blob([valueBytes], { type: 'application/octet-stream' });
|
||||
await service.saveBlobToDb(hash, blob);
|
||||
document.dispatchEvent(
|
||||
new CustomEvent('newDataReceived', {
|
||||
detail: {
|
||||
processId,
|
||||
stateId,
|
||||
hash,
|
||||
},
|
||||
}),
|
||||
);
|
||||
} else {
|
||||
// We first request the data from managers
|
||||
console.log('Request data from managers of the process');
|
||||
// get the diff from db
|
||||
if (!requestedStateId.includes(stateId)) {
|
||||
await service.requestDataFromPeers(processId, [stateId], [roles]);
|
||||
requestedStateId.push(stateId);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private handleAddObjectResponse = async (event: MessageEvent) => {
|
||||
const data = event.data;
|
||||
console.log('Received response from service worker (ADD_OBJECT):', data);
|
||||
const service = await Services.getInstance();
|
||||
if (data.type === 'NOTIFICATIONS') {
|
||||
service.setNotifications(data.data);
|
||||
} else if (data.type === 'TO_DOWNLOAD') {
|
||||
console.log(`Received missing data ${data}`);
|
||||
// Download the missing data
|
||||
let requestedStateId: string[] = [];
|
||||
for (const hash of data.data) {
|
||||
try {
|
||||
const valueBytes = await service.fetchValueFromStorage(hash);
|
||||
if (valueBytes) {
|
||||
// Save data to db
|
||||
const blob = new Blob([valueBytes], { type: 'application/octet-stream' });
|
||||
await service.saveBlobToDb(hash, blob);
|
||||
} else {
|
||||
// We first request the data from managers
|
||||
console.log('Request data from managers of the process');
|
||||
// get the diff from db
|
||||
const diff = await service.getDiffByValue(hash);
|
||||
if (diff === null) {
|
||||
continue;
|
||||
}
|
||||
const processId = diff!.process_id;
|
||||
const stateId = diff!.state_id;
|
||||
const roles = diff!.roles;
|
||||
if (!requestedStateId.includes(stateId)) {
|
||||
await service.requestDataFromPeers(processId, [stateId], [roles]);
|
||||
requestedStateId.push(stateId);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private handleGetObjectResponse = (event: MessageEvent) => {
|
||||
console.log('Received response from service worker (GET_OBJECT):', event.data);
|
||||
};
|
||||
|
||||
public addObject(payload: { storeName: string; object: any; key: any }): Promise<void> {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
// Check if the service worker is active
|
||||
if (!this.serviceWorkerRegistration) {
|
||||
// console.warn('Service worker registration is not ready. Waiting...');
|
||||
this.serviceWorkerRegistration = await navigator.serviceWorker.ready;
|
||||
}
|
||||
|
||||
const activeWorker = await this.waitForServiceWorkerActivation(this.serviceWorkerRegistration);
|
||||
|
||||
// Create a message channel for communication
|
||||
const messageChannel = new MessageChannel();
|
||||
|
||||
// Handle the response from the service worker
|
||||
messageChannel.port1.onmessage = (event) => {
|
||||
if (event.data.status === 'success') {
|
||||
resolve();
|
||||
} else {
|
||||
const error = event.data.message;
|
||||
reject(new Error(error || 'Unknown error occurred while adding object'));
|
||||
}
|
||||
};
|
||||
|
||||
// Send the add object request to the service worker
|
||||
try {
|
||||
activeWorker?.postMessage(
|
||||
{
|
||||
type: 'ADD_OBJECT',
|
||||
payload,
|
||||
},
|
||||
[messageChannel.port2],
|
||||
);
|
||||
} catch (error) {
|
||||
reject(new Error(`Failed to send message to service worker: ${error}`));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public batchWriting(payload: { storeName: string; objects: { key: any; object: any }[] }): Promise<void> {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
if (!this.serviceWorkerRegistration) {
|
||||
this.serviceWorkerRegistration = await navigator.serviceWorker.ready;
|
||||
}
|
||||
|
||||
const activeWorker = await this.waitForServiceWorkerActivation(this.serviceWorkerRegistration);
|
||||
const messageChannel = new MessageChannel();
|
||||
|
||||
messageChannel.port1.onmessage = (event) => {
|
||||
if (event.data.status === 'success') {
|
||||
resolve();
|
||||
} else {
|
||||
const error = event.data.message;
|
||||
reject(new Error(error || 'Unknown error occurred while adding objects'));
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
activeWorker?.postMessage(
|
||||
{
|
||||
type: 'BATCH_WRITING',
|
||||
payload,
|
||||
},
|
||||
[messageChannel.port2],
|
||||
);
|
||||
} catch (error) {
|
||||
reject(new Error(`Failed to send message to service worker: ${error}`));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public async getObject(storeName: string, key: string): Promise<any | null> {
|
||||
const db = await this.getDb();
|
||||
const tx = db.transaction(storeName, 'readonly');
|
||||
const store = tx.objectStore(storeName);
|
||||
const result = await new Promise((resolve, reject) => {
|
||||
const getRequest = store.get(key);
|
||||
getRequest.onsuccess = () => resolve(getRequest.result);
|
||||
getRequest.onerror = () => reject(getRequest.error);
|
||||
});
|
||||
return result ?? null; // Convert undefined to null
|
||||
}
|
||||
|
||||
public async dumpStore(storeName: string): Promise<Record<string, any>> {
|
||||
const db = await this.getDb();
|
||||
const tx = db.transaction(storeName, 'readonly');
|
||||
const store = tx.objectStore(storeName);
|
||||
|
||||
try {
|
||||
return new Promise((resolve, reject) => {
|
||||
const result: Record<string, any> = {};
|
||||
const cursor = store.openCursor();
|
||||
|
||||
cursor.onsuccess = (event) => {
|
||||
const request = event.target as IDBRequest<IDBCursorWithValue | null>;
|
||||
const cursor = request.result;
|
||||
if (cursor) {
|
||||
result[cursor.key as string] = cursor.value;
|
||||
cursor.continue();
|
||||
} else {
|
||||
resolve(result);
|
||||
}
|
||||
};
|
||||
|
||||
cursor.onerror = () => {
|
||||
reject(cursor.error);
|
||||
};
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error fetching data from IndexedDB:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async deleteObject(storeName: string, key: string): Promise<void> {
|
||||
const db = await this.getDb();
|
||||
const tx = db.transaction(storeName, 'readwrite');
|
||||
const store = tx.objectStore(storeName);
|
||||
try {
|
||||
await new Promise((resolve, reject) => {
|
||||
const getRequest = store.delete(key);
|
||||
getRequest.onsuccess = () => resolve(getRequest.result);
|
||||
getRequest.onerror = () => reject(getRequest.error);
|
||||
});
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public async clearStore(storeName: string): Promise<void> {
|
||||
const db = await this.getDb();
|
||||
const tx = db.transaction(storeName, 'readwrite');
|
||||
const store = tx.objectStore(storeName);
|
||||
try {
|
||||
await new Promise((resolve, reject) => {
|
||||
const clearRequest = store.clear();
|
||||
clearRequest.onsuccess = () => resolve(clearRequest.result);
|
||||
clearRequest.onerror = () => reject(clearRequest.error);
|
||||
});
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
// Request a store by index
|
||||
public async requestStoreByIndex(storeName: string, indexName: string, request: string): Promise<any[]> {
|
||||
const db = await this.getDb();
|
||||
const tx = db.transaction(storeName, 'readonly');
|
||||
const store = tx.objectStore(storeName);
|
||||
const index = store.index(indexName);
|
||||
|
||||
try {
|
||||
return new Promise((resolve, reject) => {
|
||||
const getAllRequest = index.getAll(request);
|
||||
getAllRequest.onsuccess = () => {
|
||||
const allItems = getAllRequest.result;
|
||||
const filtered = allItems.filter((item) => item.state_id === request);
|
||||
resolve(filtered);
|
||||
};
|
||||
getAllRequest.onerror = () => reject(getAllRequest.error);
|
||||
});
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default Database;
|
||||
import Services from './service';
|
||||
|
||||
/**
|
||||
* Database service managing IndexedDB operations via Web Worker and Service Worker
|
||||
*/
|
||||
export class Database {
|
||||
// ============================================
|
||||
// PRIVATE PROPERTIES
|
||||
// ============================================
|
||||
|
||||
private static instance: Database;
|
||||
private serviceWorkerRegistration: ServiceWorkerRegistration | null = null;
|
||||
private serviceWorkerCheckIntervalId: number | null = null;
|
||||
private indexedDBWorker: Worker | null = null;
|
||||
private messageIdCounter: number = 0;
|
||||
private pendingMessages: Map<number, { resolve: (value: any) => void; reject: (error: any) => void }> = new Map();
|
||||
|
||||
// ============================================
|
||||
// INITIALIZATION & SINGLETON
|
||||
// ============================================
|
||||
|
||||
private constructor() {
|
||||
this.initIndexedDBWorker();
|
||||
this.initServiceWorker();
|
||||
}
|
||||
|
||||
public static async getInstance(): Promise<Database> {
|
||||
if (!Database.instance) {
|
||||
Database.instance = new Database();
|
||||
await Database.instance.waitForWorkerReady();
|
||||
}
|
||||
return Database.instance;
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// INDEXEDDB WEB WORKER
|
||||
// ============================================
|
||||
|
||||
private initIndexedDBWorker(): void {
|
||||
this.indexedDBWorker = new Worker(new URL('../workers/indexeddb.worker.js', import.meta.url), { type: 'module' });
|
||||
|
||||
this.indexedDBWorker.onmessage = (event) => {
|
||||
const { id, type, result, error } = event.data;
|
||||
const pending = this.pendingMessages.get(id);
|
||||
|
||||
if (pending) {
|
||||
this.pendingMessages.delete(id);
|
||||
|
||||
if (type === 'SUCCESS') {
|
||||
pending.resolve(result);
|
||||
} else if (type === 'ERROR') {
|
||||
pending.reject(new Error(error));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.indexedDBWorker.onerror = (error) => {
|
||||
console.error('[Database] IndexedDB Worker error:', error);
|
||||
};
|
||||
}
|
||||
|
||||
private async waitForWorkerReady(): Promise<void> {
|
||||
return this.sendMessageToWorker('INIT', {});
|
||||
}
|
||||
|
||||
private sendMessageToWorker<T = any>(type: string, payload: any): Promise<T> {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!this.indexedDBWorker) {
|
||||
reject(new Error('IndexedDB Worker not initialized'));
|
||||
return;
|
||||
}
|
||||
|
||||
const id = this.messageIdCounter++;
|
||||
this.pendingMessages.set(id, { resolve, reject });
|
||||
|
||||
this.indexedDBWorker.postMessage({ type, payload, id });
|
||||
|
||||
// Timeout de sécurité (30 secondes)
|
||||
setTimeout(() => {
|
||||
if (this.pendingMessages.has(id)) {
|
||||
this.pendingMessages.delete(id);
|
||||
reject(new Error(`Worker message timeout for type: ${type}`));
|
||||
}
|
||||
}, 30000);
|
||||
});
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// SERVICE WORKER
|
||||
// ============================================
|
||||
|
||||
private initServiceWorker(): void {
|
||||
this.registerServiceWorker('/database.worker.js');
|
||||
}
|
||||
|
||||
private async registerServiceWorker(path: string): Promise<void> {
|
||||
if (!('serviceWorker' in navigator)) return;
|
||||
console.log('[Database] Initializing Service Worker:', path);
|
||||
|
||||
try {
|
||||
const registrations = await navigator.serviceWorker.getRegistrations();
|
||||
|
||||
for (const registration of registrations) {
|
||||
const scriptURL = registration.active?.scriptURL || registration.installing?.scriptURL || registration.waiting?.scriptURL;
|
||||
const scope = registration.scope;
|
||||
|
||||
if (scope.includes('/src/service-workers/') || (scriptURL && scriptURL.includes('/src/service-workers/'))) {
|
||||
console.warn(`[Database] Removing old Service Worker (${scope})`);
|
||||
await registration.unregister();
|
||||
}
|
||||
}
|
||||
|
||||
const existingValidWorker = registrations.find((r) => {
|
||||
const url = r.active?.scriptURL || r.installing?.scriptURL || r.waiting?.scriptURL;
|
||||
return url && url.endsWith(path.replace(/^\//,''));
|
||||
});
|
||||
|
||||
if (!existingValidWorker) {
|
||||
console.log('[Database] Registering new Service Worker');
|
||||
this.serviceWorkerRegistration = await navigator.serviceWorker.register(path, { type: 'module', scope: '/' });
|
||||
} else {
|
||||
console.log('[Database] Service Worker already active');
|
||||
this.serviceWorkerRegistration = existingValidWorker;
|
||||
await this.serviceWorkerRegistration.update();
|
||||
}
|
||||
|
||||
navigator.serviceWorker.addEventListener('message', async (event) => {
|
||||
if (event.data.type === 'DB_REQUEST') {
|
||||
await this.handleDatabaseRequest(event.data);
|
||||
return;
|
||||
}
|
||||
await this.handleServiceWorkerMessage(event.data);
|
||||
});
|
||||
|
||||
if (this.serviceWorkerCheckIntervalId) clearInterval(this.serviceWorkerCheckIntervalId);
|
||||
this.serviceWorkerCheckIntervalId = window.setInterval(async () => {
|
||||
const activeWorker = this.serviceWorkerRegistration?.active || (await this.waitForServiceWorkerActivation(this.serviceWorkerRegistration!));
|
||||
const service = await Services.getInstance();
|
||||
const payload = await service.getMyProcesses();
|
||||
if (payload && payload.length != 0) {
|
||||
activeWorker?.postMessage({ type: 'SCAN', payload });
|
||||
}
|
||||
}, 5000);
|
||||
} catch (error) {
|
||||
console.error('[Database] Service Worker error:', error);
|
||||
}
|
||||
}
|
||||
|
||||
private async waitForServiceWorkerActivation(registration: ServiceWorkerRegistration): Promise<ServiceWorker | null> {
|
||||
return new Promise((resolve) => {
|
||||
if (registration.active) {
|
||||
resolve(registration.active);
|
||||
} else {
|
||||
const listener = () => {
|
||||
if (registration.active) {
|
||||
navigator.serviceWorker.removeEventListener('controllerchange', listener);
|
||||
resolve(registration.active);
|
||||
}
|
||||
};
|
||||
navigator.serviceWorker.addEventListener('controllerchange', listener);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private async checkForUpdates(): Promise<void> {
|
||||
if (this.serviceWorkerRegistration) {
|
||||
try {
|
||||
await this.serviceWorkerRegistration.update();
|
||||
|
||||
if (this.serviceWorkerRegistration.waiting) {
|
||||
this.serviceWorkerRegistration.waiting.postMessage({ type: 'SKIP_WAITING' });
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error checking for service worker updates:', error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// SERVICE WORKER MESSAGE HANDLERS
|
||||
// ============================================
|
||||
private async handleDatabaseRequest(request: any): Promise<void> {
|
||||
const { id, action, payload } = request;
|
||||
|
||||
try {
|
||||
let result;
|
||||
|
||||
switch (action) {
|
||||
case 'GET_OBJECT':
|
||||
result = await this.getObject(payload.storeName, payload.key);
|
||||
break;
|
||||
|
||||
case 'GET_MULTIPLE_OBJECTS':
|
||||
result = await this.sendMessageToWorker('GET_MULTIPLE_OBJECTS', payload);
|
||||
break;
|
||||
|
||||
case 'GET_ALL_OBJECTS':
|
||||
result = await this.sendMessageToWorker('GET_ALL_OBJECTS', payload);
|
||||
break;
|
||||
|
||||
case 'GET_ALL_OBJECTS_WITH_FILTER':
|
||||
result = await this.sendMessageToWorker('GET_ALL_OBJECTS_WITH_FILTER', payload);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new Error(`Unknown database action: ${action}`);
|
||||
}
|
||||
|
||||
if (this.serviceWorkerRegistration?.active) {
|
||||
this.serviceWorkerRegistration.active.postMessage({
|
||||
type: 'DB_RESPONSE',
|
||||
id,
|
||||
result
|
||||
});
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.error('[Database] Error handling database request:', error);
|
||||
|
||||
if (this.serviceWorkerRegistration?.active) {
|
||||
this.serviceWorkerRegistration.active.postMessage({
|
||||
type: 'DB_ERROR',
|
||||
id,
|
||||
error: error.message || String(error)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async handleServiceWorkerMessage(message: any) {
|
||||
switch (message.type) {
|
||||
case 'TO_DOWNLOAD':
|
||||
await this.handleDownloadList(message.data);
|
||||
break;
|
||||
case 'DIFFS_TO_CREATE':
|
||||
await this.handleDiffsToCreate(message.data);
|
||||
break;
|
||||
default:
|
||||
console.warn('Unknown message type received from service worker:', message);
|
||||
}
|
||||
}
|
||||
|
||||
private async handleDiffsToCreate(diffs: any[]): Promise<void> {
|
||||
console.log(`[Database] Creating ${diffs.length} diffs from Service Worker scan`);
|
||||
try {
|
||||
await this.saveDiffs(diffs);
|
||||
console.log('[Database] Diffs created successfully');
|
||||
} catch (error) {
|
||||
console.error('[Database] Error creating diffs:', error);
|
||||
}
|
||||
}
|
||||
|
||||
private async handleDownloadList(downloadList: string[]): Promise<void> {
|
||||
let requestedStateId: string[] = [];
|
||||
const service = await Services.getInstance();
|
||||
for (const hash of downloadList) {
|
||||
const diff = await service.getDiffByValue(hash);
|
||||
if (!diff) {
|
||||
console.warn(`Missing a diff for hash ${hash}`);
|
||||
continue;
|
||||
}
|
||||
const processId = diff.process_id;
|
||||
const stateId = diff.state_id;
|
||||
const roles = diff.roles;
|
||||
try {
|
||||
const valueBytes = await service.fetchValueFromStorage(hash);
|
||||
if (valueBytes) {
|
||||
const blob = new Blob([valueBytes], { type: 'application/octet-stream' });
|
||||
await service.saveBlobToDb(hash, blob);
|
||||
document.dispatchEvent(
|
||||
new CustomEvent('newDataReceived', {
|
||||
detail: { processId, stateId, hash },
|
||||
}),
|
||||
);
|
||||
} else {
|
||||
console.log('Request data from managers of the process');
|
||||
if (!requestedStateId.includes(stateId)) {
|
||||
await service.requestDataFromPeers(processId, [stateId], [roles]);
|
||||
requestedStateId.push(stateId);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// GENERIC INDEXEDDB OPERATIONS
|
||||
// ============================================
|
||||
|
||||
public async getStoreList(): Promise<{ [key: string]: string }> {
|
||||
return this.sendMessageToWorker('GET_STORE_LIST', {});
|
||||
}
|
||||
|
||||
public async addObject(payload: { storeName: string; object: any; key: any }): Promise<void> {
|
||||
await this.sendMessageToWorker('ADD_OBJECT', payload);
|
||||
}
|
||||
|
||||
public async batchWriting(payload: { storeName: string; objects: { key: any; object: any }[] }): Promise<void> {
|
||||
await this.sendMessageToWorker('BATCH_WRITING', payload);
|
||||
}
|
||||
|
||||
public async getObject(storeName: string, key: string): Promise<any | null> {
|
||||
return this.sendMessageToWorker('GET_OBJECT', { storeName, key });
|
||||
}
|
||||
|
||||
public async dumpStore(storeName: string): Promise<Record<string, any>> {
|
||||
return this.sendMessageToWorker('DUMP_STORE', { storeName });
|
||||
}
|
||||
|
||||
public async deleteObject(storeName: string, key: string): Promise<void> {
|
||||
await this.sendMessageToWorker('DELETE_OBJECT', { storeName, key });
|
||||
}
|
||||
|
||||
public async clearStore(storeName: string): Promise<void> {
|
||||
await this.sendMessageToWorker('CLEAR_STORE', { storeName });
|
||||
}
|
||||
|
||||
public async requestStoreByIndex(storeName: string, indexName: string, request: string): Promise<any[]> {
|
||||
return this.sendMessageToWorker('REQUEST_STORE_BY_INDEX', { storeName, indexName, request });
|
||||
}
|
||||
|
||||
public async clearMultipleStores(storeNames: string[]): Promise<void> {
|
||||
for (const storeName of storeNames) {
|
||||
await this.clearStore(storeName);
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// BUSINESS METHODS - DEVICE
|
||||
// ============================================
|
||||
|
||||
public async saveDevice(device: any): Promise<void> {
|
||||
try {
|
||||
const existing = await this.getObject('wallet', '1');
|
||||
if (existing) {
|
||||
await this.deleteObject('wallet', '1');
|
||||
}
|
||||
} catch (e) {}
|
||||
|
||||
await this.addObject({
|
||||
storeName: 'wallet',
|
||||
object: { pre_id: '1', device },
|
||||
key: null,
|
||||
});
|
||||
}
|
||||
|
||||
public async getDevice(): Promise<any | null> {
|
||||
const result = await this.getObject('wallet', '1');
|
||||
return result ? result['device'] : null;
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// BUSINESS METHODS - PROCESS
|
||||
// ============================================
|
||||
|
||||
public async saveProcess(processId: string, process: any): Promise<void> {
|
||||
await this.addObject({
|
||||
storeName: 'processes',
|
||||
object: process,
|
||||
key: processId,
|
||||
});
|
||||
}
|
||||
|
||||
public async saveProcessesBatch(processes: Record<string, any>): Promise<void> {
|
||||
if (Object.keys(processes).length === 0) return;
|
||||
|
||||
await this.batchWriting({
|
||||
storeName: 'processes',
|
||||
objects: Object.entries(processes).map(([key, value]) => ({ key, object: value })),
|
||||
});
|
||||
}
|
||||
|
||||
public async getProcess(processId: string): Promise<any | null> {
|
||||
return this.getObject('processes', processId);
|
||||
}
|
||||
|
||||
public async getAllProcesses(): Promise<Record<string, any>> {
|
||||
return this.dumpStore('processes');
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// BUSINESS METHODS - BLOBS
|
||||
// ============================================
|
||||
|
||||
public async saveBlob(hash: string, data: Blob): Promise<void> {
|
||||
await this.addObject({
|
||||
storeName: 'data',
|
||||
object: data,
|
||||
key: hash,
|
||||
});
|
||||
}
|
||||
|
||||
public async getBlob(hash: string): Promise<Blob | null> {
|
||||
return this.getObject('data', hash);
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// BUSINESS METHODS - DIFFS
|
||||
// ============================================
|
||||
|
||||
public async saveDiffs(diffs: any[]): Promise<void> {
|
||||
if (diffs.length === 0) return;
|
||||
|
||||
for (const diff of diffs) {
|
||||
await this.addObject({
|
||||
storeName: 'diffs',
|
||||
object: diff,
|
||||
key: null,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public async getDiff(hash: string): Promise<any | null> {
|
||||
return this.getObject('diffs', hash);
|
||||
}
|
||||
|
||||
public async getAllDiffs(): Promise<Record<string, any>> {
|
||||
return this.dumpStore('diffs');
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// BUSINESS METHODS - SECRETS
|
||||
// ============================================
|
||||
|
||||
public async getSharedSecret(address: string): Promise<string | null> {
|
||||
return this.getObject('shared_secrets', address);
|
||||
}
|
||||
|
||||
public async saveSecretsBatch(unconfirmedSecrets: any[], sharedSecrets: { key: string; value: any }[]): Promise<void> {
|
||||
if (unconfirmedSecrets && unconfirmedSecrets.length > 0) {
|
||||
for (const secret of unconfirmedSecrets) {
|
||||
await this.addObject({
|
||||
storeName: 'unconfirmed_secrets',
|
||||
object: secret,
|
||||
key: null,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (sharedSecrets && sharedSecrets.length > 0) {
|
||||
for (const { key, value } of sharedSecrets) {
|
||||
await this.addObject({
|
||||
storeName: 'shared_secrets',
|
||||
object: value,
|
||||
key: key,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async getAllSecrets(): Promise<{ shared_secrets: Record<string, any>; unconfirmed_secrets: any[] }> {
|
||||
const sharedSecrets = await this.dumpStore('shared_secrets');
|
||||
const unconfirmedSecrets = await this.dumpStore('unconfirmed_secrets');
|
||||
|
||||
return {
|
||||
shared_secrets: sharedSecrets,
|
||||
unconfirmed_secrets: Object.values(unconfirmedSecrets),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export default Database;
|
||||
|
||||
@ -24,6 +24,7 @@ export default class Services {
|
||||
private notifications: any[] | null = null;
|
||||
private subscriptions: { element: Element; event: string; eventHandler: string }[] = [];
|
||||
private database: any;
|
||||
private db!: Database; // Database singleton
|
||||
private relayAddresses: { [wsurl: string]: string } = {};
|
||||
private membersList: Record<string, Member> = {};
|
||||
private currentBlockHeight: number = -1;
|
||||
@ -58,6 +59,7 @@ export default class Services {
|
||||
this.notifications = this.getNotifications();
|
||||
this.sdkClient = await import('../../pkg/sdk_client');
|
||||
this.sdkClient.setup();
|
||||
this.db = await Database.getInstance(); // Initialiser l'instance DB
|
||||
for (const wsurl of Object.values(BOOTSTRAPURL)) {
|
||||
this.updateRelay(wsurl, '');
|
||||
}
|
||||
@ -192,26 +194,15 @@ export default class Services {
|
||||
}
|
||||
|
||||
public async getSecretForAddress(address: string): Promise<string | null> {
|
||||
const db = await Database.getInstance();
|
||||
return await db.getObject('shared_secrets', address);
|
||||
return await this.db.getSharedSecret(address);
|
||||
}
|
||||
|
||||
public async getAllSecrets(): Promise<SecretsStore> {
|
||||
const db = await Database.getInstance();
|
||||
const sharedSecrets = await db.dumpStore('shared_secrets');
|
||||
const unconfirmedSecrets = await db.dumpStore('unconfirmed_secrets'); // keys are numeric values
|
||||
|
||||
const secretsStore = {
|
||||
shared_secrets: sharedSecrets,
|
||||
unconfirmed_secrets: Object.values(unconfirmedSecrets),
|
||||
};
|
||||
|
||||
return secretsStore;
|
||||
return await this.db.getAllSecrets();
|
||||
}
|
||||
|
||||
public async getAllDiffs(): Promise<Record<string, UserDiff>> {
|
||||
const db = await Database.getInstance();
|
||||
return await db.dumpStore('diffs');
|
||||
return await this.db.getAllDiffs();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -234,10 +225,7 @@ export default class Services {
|
||||
}
|
||||
|
||||
public async getDiffByValue(value: string): Promise<UserDiff | null> {
|
||||
const db = await Database.getInstance();
|
||||
const store = 'diffs';
|
||||
const res = await db.getObject(store, value);
|
||||
return res;
|
||||
return await this.db.getDiff(value);
|
||||
}
|
||||
|
||||
private async getTokensFromFaucet(): Promise<void> {
|
||||
@ -841,12 +829,7 @@ export default class Services {
|
||||
this.sdkClient.reset_device();
|
||||
|
||||
// Clear all stores
|
||||
const db = await Database.getInstance();
|
||||
await db.clearStore('wallet');
|
||||
await db.clearStore('shared_secrets');
|
||||
await db.clearStore('unconfirmed_secrets');
|
||||
await db.clearStore('processes');
|
||||
await db.clearStore('diffs');
|
||||
await this.db.clearMultipleStores(['wallet', 'shared_secrets', 'unconfirmed_secrets', 'processes', 'diffs']);
|
||||
console.warn('[Services:resetDevice] ✅ Réinitialisation terminée.');
|
||||
}
|
||||
|
||||
@ -1050,39 +1033,19 @@ export default class Services {
|
||||
|
||||
private async handleSecrets(secrets: any) {
|
||||
const { unconfirmed_secrets, shared_secrets } = secrets;
|
||||
const db = await Database.getInstance();
|
||||
|
||||
// Sauvegarder les secrets non confirmés
|
||||
if (unconfirmed_secrets && unconfirmed_secrets.length > 0) {
|
||||
console.log(`[Services:handleSecrets] 💾 Sauvegarde de ${unconfirmed_secrets.length} secret(s) non confirmé(s)`);
|
||||
for (const secret of unconfirmed_secrets) {
|
||||
try {
|
||||
await db.addObject({
|
||||
storeName: 'unconfirmed_secrets',
|
||||
object: secret,
|
||||
key: null,
|
||||
});
|
||||
} catch (e) {
|
||||
console.error("[Services:handleSecrets] 💥 Échec de sauvegarde d'un secret non confirmé:", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
const unconfirmedList = unconfirmed_secrets && unconfirmed_secrets.length > 0 ? unconfirmed_secrets : [];
|
||||
const sharedList = shared_secrets && Object.keys(shared_secrets).length > 0
|
||||
? Object.entries(shared_secrets).map(([key, value]) => ({ key, value }))
|
||||
: [];
|
||||
|
||||
// Sauvegarder les secrets partagés (confirmés)
|
||||
if (shared_secrets && Object.keys(shared_secrets).length > 0) {
|
||||
const entries = Object.entries(shared_secrets).map(([key, value]) => ({ key, value }));
|
||||
console.log(`[Services:handleSecrets] 💾 Sauvegarde de ${entries.length} secret(s) partagé(s)`);
|
||||
for (const entry of entries) {
|
||||
try {
|
||||
await db.addObject({
|
||||
storeName: 'shared_secrets',
|
||||
object: entry.value,
|
||||
key: entry.key,
|
||||
});
|
||||
console.log(`[Services:handleSecrets] ✅ Secret partagé pour ${entry.key} sauvegardé.`);
|
||||
} catch (e) {
|
||||
console.error(`[Services:handleSecrets] 💥 Échec de l'ajout du secret partagé pour ${entry.key}:`, e);
|
||||
}
|
||||
if (unconfirmedList.length > 0 || sharedList.length > 0) {
|
||||
console.log(`[Services:handleSecrets] 💾 Sauvegarde batch: ${unconfirmedList.length} secret(s) non confirmé(s) + ${sharedList.length} secret(s) partagé(s)`);
|
||||
try {
|
||||
await this.db.saveSecretsBatch(unconfirmedList, sharedList);
|
||||
console.log('[Services:handleSecrets] ✅ Secrets sauvegardés en batch.');
|
||||
} catch (e) {
|
||||
console.error('[Services:handleSecrets] 💥 Échec de sauvegarde batch des secrets:', e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1388,49 +1351,22 @@ export default class Services {
|
||||
}
|
||||
|
||||
async saveDeviceInDatabase(device: Device): Promise<void> {
|
||||
const db = await Database.getInstance();
|
||||
const walletStore = 'wallet';
|
||||
try {
|
||||
console.log("[Services:saveDeviceInDatabase] 💾 Sauvegarde de l'appareil en BDD...", {
|
||||
pairing_process_commitment: device.pairing_process_commitment,
|
||||
paired_member: device.paired_member,
|
||||
});
|
||||
|
||||
const prevDevice = await this.getDeviceFromDatabase();
|
||||
if (prevDevice) {
|
||||
// console.debug('[Services:saveDeviceInDatabase] ℹ️ Appareil précédent trouvé, suppression...');
|
||||
await db.deleteObject(walletStore, '1');
|
||||
}
|
||||
|
||||
await db.addObject({
|
||||
storeName: walletStore,
|
||||
object: { pre_id: '1', device },
|
||||
key: null,
|
||||
});
|
||||
|
||||
await this.db.saveDevice(device);
|
||||
console.log('[Services:saveDeviceInDatabase] ✅ Appareil sauvegardé avec succès');
|
||||
|
||||
// // Verify save
|
||||
// const savedDevice = await this.getDeviceFromDatabase();
|
||||
// console.log('[Services:saveDeviceInDatabase] 🔎 Vérification:', {
|
||||
// pairing_process_commitment: savedDevice?.pairing_process_commitment,
|
||||
// paired_member: savedDevice?.paired_member,
|
||||
// });
|
||||
} catch (e) {
|
||||
console.error('[Services:saveDeviceInDatabase] 💥 Erreur lors de la sauvegarde:', e);
|
||||
}
|
||||
}
|
||||
|
||||
async getDeviceFromDatabase(): Promise<Device | null> {
|
||||
const db = await Database.getInstance();
|
||||
const walletStore = 'wallet';
|
||||
try {
|
||||
const dbRes = await db.getObject(walletStore, '1');
|
||||
if (dbRes) {
|
||||
return dbRes['device'];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
return await this.db.getDevice();
|
||||
} catch (e) {
|
||||
throw new Error(`[Services:getDeviceFromDatabase] 💥 Échec: ${e}`);
|
||||
}
|
||||
@ -1581,44 +1517,22 @@ export default class Services {
|
||||
}
|
||||
}
|
||||
|
||||
private async removeProcess(processId: string): Promise<void> {
|
||||
const db = await Database.getInstance();
|
||||
const storeName = 'processes';
|
||||
|
||||
try {
|
||||
console.log(`[Services:removeProcess] 🗑️ Suppression du processus ${processId}`);
|
||||
await db.deleteObject(storeName, processId);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
public async batchSaveProcessesToDb(processes: Record<string, Process>) {
|
||||
if (Object.keys(processes).length === 0) {
|
||||
return;
|
||||
}
|
||||
console.log(`[Services:batchSaveProcessesToDb] 💾 Sauvegarde de ${Object.keys(processes).length} processus en BDD...`);
|
||||
const db = await Database.getInstance();
|
||||
const storeName = 'processes';
|
||||
try {
|
||||
await db.batchWriting({ storeName, objects: Object.entries(processes).map(([key, value]) => ({ key, object: value })) });
|
||||
await this.db.saveProcessesBatch(processes);
|
||||
this.processesCache = { ...this.processesCache, ...processes };
|
||||
} catch (e) {
|
||||
console.error('[Services:batchSaveProcessesToDb] 💥 Échec:', e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public async saveProcessToDb(processId: string, process: Process) {
|
||||
const db = await Database.getInstance();
|
||||
const storeName = 'processes';
|
||||
try {
|
||||
await db.addObject({
|
||||
storeName,
|
||||
object: process,
|
||||
key: processId,
|
||||
});
|
||||
|
||||
await this.db.saveProcess(processId, process);
|
||||
// Update the process in the cache
|
||||
this.processesCache[processId] = process;
|
||||
} catch (e) {
|
||||
@ -1627,27 +1541,81 @@ export default class Services {
|
||||
}
|
||||
|
||||
public async saveBlobToDb(hash: string, data: Blob) {
|
||||
const db = await Database.getInstance();
|
||||
try {
|
||||
await db.addObject({
|
||||
storeName: 'data',
|
||||
object: data,
|
||||
key: hash,
|
||||
});
|
||||
await this.db.saveBlob(hash, data);
|
||||
} catch (e) {
|
||||
console.error(`[Services:saveBlobToDb] 💥 Échec de la sauvegarde du blob ${hash}: ${e}`);
|
||||
}
|
||||
}
|
||||
|
||||
public async getBlobFromDb(hash: string): Promise<Blob | null> {
|
||||
const db = await Database.getInstance();
|
||||
try {
|
||||
return await db.getObject('data', hash);
|
||||
return await this.db.getBlob(hash);
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async getProcess(processId: string): Promise<Process | null> {
|
||||
// 1. Essayer le cache en mémoire
|
||||
if (this.processesCache[processId]) {
|
||||
return this.processesCache[processId];
|
||||
}
|
||||
|
||||
// 2. Si non trouvé, essayer la BDD
|
||||
try {
|
||||
const process = await this.db.getProcess(processId);
|
||||
if (process) {
|
||||
this.processesCache[processId] = process; // Mettre en cache
|
||||
}
|
||||
return process;
|
||||
} catch (e) {
|
||||
console.error(`[Services:getProcess] 💥 Échec de la récupération du processus ${processId}: ${e}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async getProcesses(): Promise<Record<string, Process>> {
|
||||
// 1. Essayer le cache en mémoire
|
||||
if (Object.keys(this.processesCache).length > 0) {
|
||||
return this.processesCache;
|
||||
}
|
||||
|
||||
// 2. Si non trouvé, charger depuis la BDD
|
||||
try {
|
||||
console.log('[Services:getProcesses] ℹ️ Cache de processus vide. Chargement depuis la BDD...');
|
||||
this.processesCache = await this.db.getAllProcesses();
|
||||
console.log(`[Services:getProcesses] ✅ ${Object.keys(this.processesCache).length} processus chargés en cache.`);
|
||||
return this.processesCache;
|
||||
} catch (e) {
|
||||
console.error('[Services:getProcesses] 💥 Échec:', e);
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
public async restoreProcessesFromBackUp(processes: Record<string, Process>) {
|
||||
console.log(`[Services:restoreProcessesFromBackUp] 💾 Restauration de ${Object.keys(processes).length} processus depuis un backup...`);
|
||||
try {
|
||||
await this.db.saveProcessesBatch(processes);
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
// Restore processes cache from persistent storage
|
||||
public async restoreProcessesFromDB() {
|
||||
try {
|
||||
const processes: Record<string, Process> = await this.db.getAllProcesses();
|
||||
if (processes && Object.keys(processes).length != 0) {
|
||||
console.log(`[Services:restoreProcessesFromDB] 🔄 Restauration de ${Object.keys(processes).length} processus depuis la BDD vers le cache...`);
|
||||
this.processesCache = processes;
|
||||
console.log('[Services:restoreProcessesFromDB] ✅ Processus restaurés.');
|
||||
}
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public async saveDataToStorage(storages: string[], hash: string, data: Blob, ttl: number | null) {
|
||||
try {
|
||||
await storeData(storages, hash, data, ttl);
|
||||
@ -1663,113 +1631,23 @@ export default class Services {
|
||||
}
|
||||
|
||||
public async getDiffByValueFromDb(hash: string): Promise<UserDiff | null> {
|
||||
const db = await Database.getInstance();
|
||||
const diff = await db.getObject('diffs', hash);
|
||||
return diff;
|
||||
return await this.db.getDiff(hash);
|
||||
}
|
||||
|
||||
public async saveDiffsToDb(diffs: UserDiff[]) {
|
||||
const db = await Database.getInstance();
|
||||
try {
|
||||
for (const diff of diffs) {
|
||||
await db.addObject({
|
||||
storeName: 'diffs',
|
||||
object: diff,
|
||||
key: null,
|
||||
});
|
||||
}
|
||||
await this.db.saveDiffs(diffs);
|
||||
} catch (e) {
|
||||
throw new Error(`[Services:saveDiffsToDb] 💥 Échec: ${e}`);
|
||||
}
|
||||
}
|
||||
|
||||
public async getProcess(processId: string): Promise<Process | null> {
|
||||
// 1. Essayer le cache en mémoire
|
||||
if (this.processesCache[processId]) {
|
||||
return this.processesCache[processId];
|
||||
}
|
||||
|
||||
// 2. Si non trouvé, essayer la BDD
|
||||
try {
|
||||
const db = await Database.getInstance();
|
||||
const process = await db.getObject('processes', processId);
|
||||
if (process) {
|
||||
this.processesCache[processId] = process; // Mettre en cache
|
||||
}
|
||||
return process;
|
||||
} catch (e) {
|
||||
console.error(`[Services:getProcess] 💥 Échec de récupération du processus ${processId}:`, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async getProcesses(): Promise<Record<string, Process>> {
|
||||
// 1. Essayer le cache en mémoire
|
||||
if (Object.keys(this.processesCache).length > 0) {
|
||||
return this.processesCache;
|
||||
}
|
||||
|
||||
// 2. Si non trouvé, charger depuis la BDD
|
||||
try {
|
||||
console.log('[Services:getProcesses] ℹ️ Cache de processus vide. Chargement depuis la BDD...');
|
||||
const db = await Database.getInstance();
|
||||
this.processesCache = await db.dumpStore('processes');
|
||||
console.log(`[Services:getProcesses] ✅ ${Object.keys(this.processesCache).length} processus chargés en cache.`);
|
||||
return this.processesCache;
|
||||
} catch (e) {
|
||||
console.error('[Services:getProcesses] 💥 Échec du chargement des processus:', e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public async restoreProcessesFromBackUp(processes: Record<string, Process>) {
|
||||
console.log(`[Services:restoreProcessesFromBackUp] 💾 Restauration de ${Object.keys(processes).length} processus depuis un backup...`);
|
||||
const db = await Database.getInstance();
|
||||
const storeName = 'processes';
|
||||
try {
|
||||
await db.batchWriting({ storeName, objects: Object.entries(processes).map(([key, value]) => ({ key, object: value })) });
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
|
||||
await this.restoreProcessesFromDB();
|
||||
}
|
||||
|
||||
// Restore processes cache from persistent storage
|
||||
public async restoreProcessesFromDB() {
|
||||
const db = await Database.getInstance();
|
||||
try {
|
||||
const processes: Record<string, Process> = await db.dumpStore('processes');
|
||||
if (processes && Object.keys(processes).length != 0) {
|
||||
console.log(`[Services:restoreProcessesFromDB] 🔄 Restauration de ${Object.keys(processes).length} processus depuis la BDD vers le cache...`);
|
||||
this.processesCache = processes;
|
||||
} else {
|
||||
console.log('[Services:restoreProcessesFromDB] ℹ️ Aucun processus à restaurer.');
|
||||
}
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public async restoreSecretsFromBackUp(secretsStore: SecretsStore) {
|
||||
console.log('[Services:restoreSecretsFromBackUp] 💾 Restauration des secrets depuis un backup...');
|
||||
const db = await Database.getInstance();
|
||||
|
||||
for (const secret of secretsStore.unconfirmed_secrets) {
|
||||
await db.addObject({
|
||||
storeName: 'unconfirmed_secrets',
|
||||
object: secret,
|
||||
key: null,
|
||||
});
|
||||
}
|
||||
const entries = Object.entries(secretsStore.shared_secrets).map(([key, value]) => ({ key, value }));
|
||||
for (const entry of entries) {
|
||||
await db.addObject({
|
||||
storeName: 'shared_secrets',
|
||||
object: entry.value,
|
||||
key: entry.key,
|
||||
});
|
||||
}
|
||||
const sharedList = Object.entries(secretsStore.shared_secrets).map(([key, value]) => ({ key, value }));
|
||||
await this.db.saveSecretsBatch(secretsStore.unconfirmed_secrets, sharedList);
|
||||
console.log('[Services:restoreSecretsFromBackUp] ✅ Secrets restaurés en batch.');
|
||||
|
||||
// Now we can transfer them to memory
|
||||
await this.restoreSecretsFromDB();
|
||||
@ -1777,16 +1655,10 @@ export default class Services {
|
||||
|
||||
public async restoreSecretsFromDB() {
|
||||
console.log('[Services:restoreSecretsFromDB] 🔄 Restauration des secrets depuis la BDD vers la mémoire SDK...');
|
||||
const db = await Database.getInstance();
|
||||
try {
|
||||
const sharedSecrets: Record<string, string> = await db.dumpStore('shared_secrets');
|
||||
const unconfirmedSecrets = await db.dumpStore('unconfirmed_secrets');
|
||||
const secretsStore = {
|
||||
shared_secrets: sharedSecrets,
|
||||
unconfirmed_secrets: Object.values(unconfirmedSecrets),
|
||||
};
|
||||
const secretsStore = await this.db.getAllSecrets();
|
||||
this.sdkClient.set_shared_secrets(JSON.stringify(secretsStore));
|
||||
console.log(`[Services:restoreSecretsFromDB] ✅ ${Object.keys(sharedSecrets).length} secrets partagés restaurés.`);
|
||||
console.log(`[Services:restoreSecretsFromDB] ✅ ${Object.keys(secretsStore.shared_secrets).length} secrets partagés restaurés.`);
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
|
||||
381
src/workers/indexeddb.worker.ts
Normal file
381
src/workers/indexeddb.worker.ts
Normal file
@ -0,0 +1,381 @@
|
||||
/**
|
||||
* Database Web Worker - Handles all IndexedDB operations in background
|
||||
*/
|
||||
|
||||
import type {
|
||||
StoreDefinition,
|
||||
WorkerMessagePayload,
|
||||
WorkerMessageResponse,
|
||||
BatchWriteItem
|
||||
} from './worker.types';
|
||||
|
||||
const DB_NAME = '4nk';
|
||||
const DB_VERSION = 1;
|
||||
|
||||
// ============================================
|
||||
// STORE DEFINITIONS
|
||||
// ============================================
|
||||
|
||||
const STORE_DEFINITIONS: Record<string, StoreDefinition> = {
|
||||
AnkLabels: {
|
||||
name: 'labels',
|
||||
options: { keyPath: 'emoji' },
|
||||
indices: [],
|
||||
},
|
||||
AnkWallet: {
|
||||
name: 'wallet',
|
||||
options: { keyPath: 'pre_id' },
|
||||
indices: [],
|
||||
},
|
||||
AnkProcess: {
|
||||
name: 'processes',
|
||||
options: {},
|
||||
indices: [],
|
||||
},
|
||||
AnkSharedSecrets: {
|
||||
name: 'shared_secrets',
|
||||
options: {},
|
||||
indices: [],
|
||||
},
|
||||
AnkUnconfirmedSecrets: {
|
||||
name: 'unconfirmed_secrets',
|
||||
options: { autoIncrement: true },
|
||||
indices: [],
|
||||
},
|
||||
AnkPendingDiffs: {
|
||||
name: 'diffs',
|
||||
options: { keyPath: 'value_commitment' },
|
||||
indices: [
|
||||
{ name: 'byStateId', keyPath: 'state_id', options: { unique: false } },
|
||||
{ name: 'byNeedValidation', keyPath: 'need_validation', options: { unique: false } },
|
||||
{ name: 'byStatus', keyPath: 'validation_status', options: { unique: false } },
|
||||
],
|
||||
},
|
||||
AnkData: {
|
||||
name: 'data',
|
||||
options: {},
|
||||
indices: [],
|
||||
},
|
||||
};
|
||||
|
||||
let db: IDBDatabase | null = null;
|
||||
|
||||
// ============================================
|
||||
// DATABASE INITIALIZATION
|
||||
// ============================================
|
||||
|
||||
async function openDatabase(): Promise<IDBDatabase> {
|
||||
if (db) {
|
||||
return db;
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const request = indexedDB.open(DB_NAME, DB_VERSION);
|
||||
|
||||
request.onupgradeneeded = (event: IDBVersionChangeEvent) => {
|
||||
const database = (event.target as IDBOpenDBRequest).result;
|
||||
|
||||
Object.values(STORE_DEFINITIONS).forEach(({ name, options, indices }) => {
|
||||
if (!database.objectStoreNames.contains(name)) {
|
||||
const store = database.createObjectStore(name, options);
|
||||
|
||||
indices.forEach(({ name: indexName, keyPath, options: indexOptions }) => {
|
||||
store.createIndex(indexName, keyPath, indexOptions);
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
request.onsuccess = () => {
|
||||
db = request.result;
|
||||
resolve(db);
|
||||
};
|
||||
|
||||
request.onerror = () => {
|
||||
reject(request.error);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// WRITE OPERATIONS
|
||||
// ============================================
|
||||
|
||||
async function addObject(storeName: string, object: any, key?: IDBValidKey): Promise<{ success: boolean }> {
|
||||
const database = await openDatabase();
|
||||
const tx = database.transaction(storeName, 'readwrite');
|
||||
const store = tx.objectStore(storeName);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
let request: IDBRequest;
|
||||
if (key !== null && key !== undefined) {
|
||||
request = store.put(object, key);
|
||||
} else {
|
||||
request = store.put(object);
|
||||
}
|
||||
|
||||
request.onsuccess = () => resolve({ success: true });
|
||||
request.onerror = () => reject(request.error);
|
||||
});
|
||||
}
|
||||
|
||||
async function batchWriting(storeName: string, objects: BatchWriteItem[]): Promise<{ success: boolean }> {
|
||||
const database = await openDatabase();
|
||||
const tx = database.transaction(storeName, 'readwrite');
|
||||
const store = tx.objectStore(storeName);
|
||||
|
||||
for (const { key, object } of objects) {
|
||||
if (key !== null && key !== undefined) {
|
||||
store.put(object, key);
|
||||
} else {
|
||||
store.put(object);
|
||||
}
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
tx.oncomplete = () => resolve({ success: true });
|
||||
tx.onerror = () => reject(tx.error);
|
||||
});
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// READ OPERATIONS
|
||||
// ============================================
|
||||
|
||||
async function getObject(storeName: string, key: IDBValidKey): Promise<any> {
|
||||
const database = await openDatabase();
|
||||
const tx = database.transaction(storeName, 'readonly');
|
||||
const store = tx.objectStore(storeName);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const request = store.get(key);
|
||||
request.onsuccess = () => resolve(request.result ?? null);
|
||||
request.onerror = () => reject(request.error);
|
||||
});
|
||||
}
|
||||
|
||||
async function dumpStore(storeName: string): Promise<Record<string, any>> {
|
||||
const database = await openDatabase();
|
||||
const tx = database.transaction(storeName, 'readonly');
|
||||
const store = tx.objectStore(storeName);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const result: Record<string, any> = {};
|
||||
const request = store.openCursor();
|
||||
|
||||
request.onsuccess = (event) => {
|
||||
const cursor = (event.target as IDBRequest<IDBCursorWithValue | null>).result;
|
||||
if (cursor) {
|
||||
result[cursor.key as string] = cursor.value;
|
||||
cursor.continue();
|
||||
} else {
|
||||
resolve(result);
|
||||
}
|
||||
};
|
||||
|
||||
request.onerror = () => reject(request.error);
|
||||
});
|
||||
}
|
||||
|
||||
async function getAllObjects(storeName: string): Promise<any[]> {
|
||||
const database = await openDatabase();
|
||||
const tx = database.transaction(storeName, 'readonly');
|
||||
const store = tx.objectStore(storeName);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const request = store.getAll();
|
||||
request.onsuccess = () => resolve(request.result || []);
|
||||
request.onerror = () => reject(request.error);
|
||||
});
|
||||
}
|
||||
|
||||
async function getMultipleObjects(storeName: string, keys: IDBValidKey[]): Promise<any[]> {
|
||||
const database = await openDatabase();
|
||||
const tx = database.transaction(storeName, 'readonly');
|
||||
const store = tx.objectStore(storeName);
|
||||
|
||||
const requests = keys.map((key) => {
|
||||
return new Promise<any>((resolve) => {
|
||||
const request = store.get(key);
|
||||
request.onsuccess = () => resolve(request.result || null);
|
||||
request.onerror = () => {
|
||||
console.error(`Error fetching key ${key}:`, request.error);
|
||||
resolve(null);
|
||||
};
|
||||
});
|
||||
});
|
||||
|
||||
const results = await Promise.all(requests);
|
||||
return results.filter(result => result !== null);
|
||||
}
|
||||
|
||||
async function getAllObjectsWithFilter(storeName: string, filterFn?: string): Promise<any[]> {
|
||||
const database = await openDatabase();
|
||||
const tx = database.transaction(storeName, 'readonly');
|
||||
const store = tx.objectStore(storeName);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const request = store.getAll();
|
||||
request.onsuccess = () => {
|
||||
const allItems = request.result || [];
|
||||
if (filterFn) {
|
||||
const filter = new Function('item', `return ${filterFn}`) as (item: any) => boolean;
|
||||
resolve(allItems.filter(filter));
|
||||
} else {
|
||||
resolve(allItems);
|
||||
}
|
||||
};
|
||||
request.onerror = () => reject(request.error);
|
||||
});
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// DELETE OPERATIONS
|
||||
// ============================================
|
||||
|
||||
async function deleteObject(storeName: string, key: IDBValidKey): Promise<{ success: boolean }> {
|
||||
const database = await openDatabase();
|
||||
const tx = database.transaction(storeName, 'readwrite');
|
||||
const store = tx.objectStore(storeName);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const request = store.delete(key);
|
||||
request.onsuccess = () => resolve({ success: true });
|
||||
request.onerror = () => reject(request.error);
|
||||
});
|
||||
}
|
||||
|
||||
async function clearStore(storeName: string): Promise<{ success: boolean }> {
|
||||
const database = await openDatabase();
|
||||
const tx = database.transaction(storeName, 'readwrite');
|
||||
const store = tx.objectStore(storeName);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const request = store.clear();
|
||||
request.onsuccess = () => resolve({ success: true });
|
||||
request.onerror = () => reject(request.error);
|
||||
});
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// INDEX OPERATIONS
|
||||
// ============================================
|
||||
|
||||
async function requestStoreByIndex(storeName: string, indexName: string, requestValue: IDBValidKey): Promise<any[]> {
|
||||
const database = await openDatabase();
|
||||
const tx = database.transaction(storeName, 'readonly');
|
||||
const store = tx.objectStore(storeName);
|
||||
const index = store.index(indexName);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const request = index.getAll(requestValue);
|
||||
request.onsuccess = () => {
|
||||
const allItems = request.result;
|
||||
const filtered = allItems.filter((item: any) => item.state_id === requestValue);
|
||||
resolve(filtered);
|
||||
};
|
||||
request.onerror = () => reject(request.error);
|
||||
});
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// UTILITY FUNCTIONS
|
||||
// ============================================
|
||||
|
||||
function getStoreList(): Record<string, string> {
|
||||
const storeList: Record<string, string> = {};
|
||||
Object.keys(STORE_DEFINITIONS).forEach((key) => {
|
||||
storeList[key] = STORE_DEFINITIONS[key].name;
|
||||
});
|
||||
return storeList;
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// MESSAGE HANDLER
|
||||
// ============================================
|
||||
|
||||
self.addEventListener('message', async (event: MessageEvent<WorkerMessagePayload>) => {
|
||||
const { type, payload, id } = event.data;
|
||||
|
||||
try {
|
||||
let result: any;
|
||||
|
||||
switch (type) {
|
||||
case 'INIT':
|
||||
await openDatabase();
|
||||
result = { success: true };
|
||||
break;
|
||||
|
||||
case 'ADD_OBJECT':
|
||||
result = await addObject(payload.storeName, payload.object, payload.key);
|
||||
break;
|
||||
|
||||
case 'BATCH_WRITING':
|
||||
result = await batchWriting(payload.storeName, payload.objects);
|
||||
break;
|
||||
|
||||
case 'GET_OBJECT':
|
||||
result = await getObject(payload.storeName, payload.key);
|
||||
break;
|
||||
|
||||
case 'DUMP_STORE':
|
||||
result = await dumpStore(payload.storeName);
|
||||
break;
|
||||
|
||||
case 'DELETE_OBJECT':
|
||||
result = await deleteObject(payload.storeName, payload.key);
|
||||
break;
|
||||
|
||||
case 'CLEAR_STORE':
|
||||
result = await clearStore(payload.storeName);
|
||||
break;
|
||||
|
||||
case 'REQUEST_STORE_BY_INDEX':
|
||||
result = await requestStoreByIndex(
|
||||
payload.storeName,
|
||||
payload.indexName,
|
||||
payload.request
|
||||
);
|
||||
break;
|
||||
|
||||
case 'GET_ALL_OBJECTS':
|
||||
result = await getAllObjects(payload.storeName);
|
||||
break;
|
||||
|
||||
case 'GET_MULTIPLE_OBJECTS':
|
||||
result = await getMultipleObjects(payload.storeName, payload.keys);
|
||||
break;
|
||||
|
||||
case 'GET_ALL_OBJECTS_WITH_FILTER':
|
||||
result = await getAllObjectsWithFilter(payload.storeName, payload.filterFn);
|
||||
break;
|
||||
|
||||
case 'GET_STORE_LIST':
|
||||
result = getStoreList();
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new Error(`Unknown message type: ${type}`);
|
||||
}
|
||||
|
||||
self.postMessage({
|
||||
id,
|
||||
type: 'SUCCESS',
|
||||
result,
|
||||
} as WorkerMessageResponse);
|
||||
} catch (error) {
|
||||
self.postMessage({
|
||||
id,
|
||||
type: 'ERROR',
|
||||
error: (error as Error).message || String(error),
|
||||
} as WorkerMessageResponse);
|
||||
}
|
||||
});
|
||||
|
||||
// ============================================
|
||||
// INITIALIZATION
|
||||
// ============================================
|
||||
|
||||
openDatabase().catch((error) => {
|
||||
console.error('[Database Worker] Failed to initialize database:', error);
|
||||
});
|
||||
33
src/workers/worker.types.ts
Normal file
33
src/workers/worker.types.ts
Normal file
@ -0,0 +1,33 @@
|
||||
/**
|
||||
* Shared types for Web Workers
|
||||
*/
|
||||
|
||||
export interface StoreDefinition {
|
||||
name: string;
|
||||
options: IDBObjectStoreParameters;
|
||||
indices: IndexDefinition[];
|
||||
}
|
||||
|
||||
export interface IndexDefinition {
|
||||
name: string;
|
||||
keyPath: string | string[];
|
||||
options: IDBIndexParameters;
|
||||
}
|
||||
|
||||
export interface WorkerMessagePayload {
|
||||
type: string;
|
||||
payload?: any;
|
||||
id: number;
|
||||
}
|
||||
|
||||
export interface WorkerMessageResponse {
|
||||
id: number;
|
||||
type: 'SUCCESS' | 'ERROR';
|
||||
result?: any;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export interface BatchWriteItem {
|
||||
key?: IDBValidKey;
|
||||
object: any;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user