import ElectronAPIs from "@ente/shared/electron"; import { LimitedCache } from "@ente/shared/storage/cacheStorage/types"; import * as Comlink from "comlink"; import { deserializeToResponse, serializeResponse } from "./utils/proxy"; export interface ProxiedLimitedElectronAPIs { openDiskCache: ( cacheName: string, cacheLimitInBytes?: number, ) => Promise; deleteDiskCache: (cacheName: string) => Promise; getSentryUserID: () => Promise; convertToJPEG: ( inputFileData: Uint8Array, filename: string, ) => Promise; logToDisk: (message: string) => void; } export interface ProxiedWorkerLimitedCache { match: ( key: string, options?: { sizeInBytes?: number }, ) => Promise; put: (key: string, data: ArrayBuffer) => Promise; delete: (key: string) => Promise; } export class WorkerSafeElectronClient implements ProxiedLimitedElectronAPIs { async openDiskCache(cacheName: string, cacheLimitInBytes?: number) { const cache = await ElectronAPIs.openDiskCache( cacheName, cacheLimitInBytes, ); return Comlink.proxy({ match: Comlink.proxy(transformMatch(cache.match.bind(cache))), put: Comlink.proxy(transformPut(cache.put.bind(cache))), delete: Comlink.proxy(cache.delete.bind(cache)), }); } async deleteDiskCache(cacheName: string) { return await ElectronAPIs.deleteDiskCache(cacheName); } async getSentryUserID() { return await ElectronAPIs.getSentryUserID(); } async convertToJPEG( inputFileData: Uint8Array, filename: string, ): Promise { return await ElectronAPIs.convertToJPEG(inputFileData, filename); } logToDisk(message: string) { return ElectronAPIs.logToDisk(message); } } function transformMatch( fn: LimitedCache["match"], ): ProxiedWorkerLimitedCache["match"] { return async (key: string, options: { sizeInBytes?: number }) => { return serializeResponse(await fn(key, options)); }; } function transformPut( fn: LimitedCache["put"], ): ProxiedWorkerLimitedCache["put"] { return async (key: string, data: ArrayBuffer) => { fn(key, deserializeToResponse(data)); }; }