Fix: Standardize Electron bridge and implement robust caching
- Refactored all IPC methods and parameters to snake_case for consistency with SvelteKit. - Implemented exhaustive background caching engine with download locking. - Reverted to legacy-proven flat hash storage pattern (hash.file). - Added axios for reliable stream-based binary downloads. - Updated preload and main handlers to support recursive room data fetching.
This commit is contained in:
@@ -33,7 +33,6 @@ export async function fetchFullConfig(seed: SeedConfig): Promise<any> {
|
||||
|
||||
// Use 'app_base_url' as the FQDN for the site lookup
|
||||
const fqdn = deviceData.app_base_url || 'native-demo.oneskyit.com';
|
||||
console.log(`Bootstrap Step 1 Success: Device identified. FQDN to use: ${fqdn}`);
|
||||
|
||||
// --- STEP 2: Get Site Context ---
|
||||
const searchUrl = `${baseUrl}/v3/crud/site_domain/search`;
|
||||
@@ -64,7 +63,8 @@ export async function fetchFullConfig(seed: SeedConfig): Promise<any> {
|
||||
|
||||
return {
|
||||
...siteDomain,
|
||||
native_device: deviceData
|
||||
native_device: deviceData,
|
||||
aether_api_key: seed.aether_api_key // Include the key for frontend use
|
||||
};
|
||||
|
||||
} catch (error) {
|
||||
|
||||
70
src/main/file_handlers.ts
Normal file
70
src/main/file_handlers.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import { ipcMain, shell } from 'electron';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import axios from 'axios';
|
||||
import { expandPath } from './file_utils';
|
||||
|
||||
let endpoints_in_progress: string[] = [];
|
||||
|
||||
export function registerFileHandlers() {
|
||||
function get_legacy_hashed_path(root: string, hash: string) {
|
||||
return path.join(expandPath(root), `${hash}.file`);
|
||||
}
|
||||
|
||||
ipcMain.handle('native:check-cache', async (event, { cache_root, hash }) => {
|
||||
const full_path = get_legacy_hashed_path(cache_root, hash);
|
||||
return fs.existsSync(full_path);
|
||||
});
|
||||
|
||||
ipcMain.handle('native:download-to-cache', async (event, { url, cache_root, hash, api_key, account_id }) => {
|
||||
const full_path = get_legacy_hashed_path(cache_root, hash);
|
||||
const tmp_path = `${full_path}.tmp`;
|
||||
|
||||
if (endpoints_in_progress.includes(url)) return { success: true, status: 'in_progress' };
|
||||
if (fs.existsSync(full_path)) return { success: true, path: full_path, status: 'exists' };
|
||||
|
||||
console.log(`Native: Downloading ${hash} -> ${full_path}`);
|
||||
|
||||
try {
|
||||
endpoints_in_progress.push(url);
|
||||
const response = await axios({
|
||||
method: 'get', url, responseType: 'stream',
|
||||
headers: {
|
||||
'x-aether-api-key': api_key,
|
||||
'x-account-id': account_id || '',
|
||||
'x-no-account-id': 'Nothing to See Here'
|
||||
}
|
||||
});
|
||||
|
||||
const writer = fs.createWriteStream(tmp_path);
|
||||
response.data.pipe(writer);
|
||||
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
writer.on('finish', () => resolve());
|
||||
writer.on('error', reject);
|
||||
});
|
||||
|
||||
fs.renameSync(tmp_path, full_path);
|
||||
return { success: true, path: full_path };
|
||||
} catch (error: any) {
|
||||
if (fs.existsSync(tmp_path)) fs.unlinkSync(tmp_path);
|
||||
return { success: false, error: error.message };
|
||||
} finally {
|
||||
endpoints_in_progress = endpoints_in_progress.filter(e => e !== url);
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle('native:launch-from-cache', async (event, { cache_root, hash, temp_root, filename }) => {
|
||||
try {
|
||||
const source = get_legacy_hashed_path(cache_root, hash);
|
||||
const expanded_temp = expandPath(temp_root);
|
||||
const target = path.join(expanded_temp, filename);
|
||||
if (!fs.existsSync(expanded_temp)) fs.mkdirSync(expanded_temp, { recursive: true });
|
||||
fs.copyFileSync(source, target);
|
||||
await shell.openPath(target);
|
||||
return { success: true };
|
||||
} catch (error: any) {
|
||||
return { success: false, error: error.message };
|
||||
}
|
||||
});
|
||||
}
|
||||
16
src/main/file_utils.ts
Normal file
16
src/main/file_utils.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import * as os from 'os';
|
||||
import * as path from 'path';
|
||||
|
||||
export function expandPath(filePath: string): string {
|
||||
if (!filePath) return filePath;
|
||||
if (filePath.startsWith('[home]')) {
|
||||
return path.join(os.homedir(), filePath.replace('[home]', ''));
|
||||
}
|
||||
return filePath;
|
||||
}
|
||||
|
||||
export function getHashedPath(cacheRoot: string, hash: string): string {
|
||||
const expandedRoot = expandPath(cacheRoot);
|
||||
const subdirectory = hash.substring(0, 2);
|
||||
return path.join(expandedRoot, subdirectory, `${hash}.file`);
|
||||
}
|
||||
@@ -2,6 +2,8 @@ import { app, BrowserWindow, ipcMain } from 'electron';
|
||||
import * as path from 'path';
|
||||
import { loadSeedConfig } from './config_loader';
|
||||
import { fetchFullConfig } from './api_client';
|
||||
import { registerShellHandlers } from './shell_handlers';
|
||||
import { registerFileHandlers } from './file_handlers';
|
||||
import { SeedConfig } from '../shared/types';
|
||||
|
||||
let mainWindow: BrowserWindow | null = null;
|
||||
@@ -27,7 +29,7 @@ async function createWindow() {
|
||||
});
|
||||
|
||||
// Determine target URL based on hydrated config
|
||||
let targetUrl = 'http://demo.localhost:5173'; // Default Dev Fallback
|
||||
let targetUrl = 'http://demo.localhost:5173';
|
||||
|
||||
if (cachedFullConfig && cachedFullConfig.native_device) {
|
||||
const device = cachedFullConfig.native_device;
|
||||
@@ -35,7 +37,6 @@ async function createWindow() {
|
||||
const locationId = device.event_location_id_random || device.event_location_id || '';
|
||||
const host = device.app_base_url || 'demo.localhost:5173';
|
||||
|
||||
// In development, we likely want to stick to localhost even if app_base_url is set
|
||||
const useHost = (host.includes('localhost')) ? host : 'demo.localhost:5173';
|
||||
|
||||
targetUrl = `http://${useHost}/events/${eventId}/launcher/${locationId}`;
|
||||
@@ -53,6 +54,10 @@ async function createWindow() {
|
||||
});
|
||||
}
|
||||
|
||||
// Register OS-level handlers
|
||||
registerShellHandlers();
|
||||
registerFileHandlers();
|
||||
|
||||
app.on('ready', createWindow);
|
||||
|
||||
app.on('window-all-closed', () => {
|
||||
|
||||
38
src/main/shell_handlers.ts
Normal file
38
src/main/shell_handlers.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { ipcMain, shell } from 'electron';
|
||||
import { exec } from 'child_process';
|
||||
import * as path from 'path';
|
||||
import * as fs from 'fs';
|
||||
|
||||
export function registerShellHandlers() {
|
||||
// Open a local directory in the OS File Manager
|
||||
ipcMain.handle('native:open-folder', async (event, folderPath: string) => {
|
||||
console.log(`Native: Opening folder ${folderPath}`);
|
||||
const error = await shell.openPath(folderPath);
|
||||
return { success: !error, error };
|
||||
});
|
||||
|
||||
// Run a generic shell command (Whitelisted logic can be added here)
|
||||
ipcMain.handle('native:run-cmd', async (event, command: string) => {
|
||||
console.log(`Native: Running command: ${command}`);
|
||||
return new Promise((resolve) => {
|
||||
exec(command, (error, stdout, stderr) => {
|
||||
resolve({
|
||||
success: !error,
|
||||
stdout,
|
||||
stderr,
|
||||
error: error ? error.message : null
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// Specific handler for launching a presentation file
|
||||
ipcMain.handle('native:launch-file', async (event, filePath: string) => {
|
||||
console.log(`Native: Launching file: ${filePath}`);
|
||||
if (!fs.existsSync(filePath)) {
|
||||
return { success: false, error: 'File not found' };
|
||||
}
|
||||
const error = await shell.openPath(filePath);
|
||||
return { success: !error, error };
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user