Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

update workerElectronCacheStorageService to workerElectronService

Abhinav3364420b15c6
diff --git a/packages/shared/electron/service.ts b/packages/shared/electron/service.ts
new file mode 100644
index 0000000000..74d63c91b2
--- /dev/null
+++ b/packages/shared/electron/service.ts
@@ -0,0 +1,73 @@
+import * as Comlink from 'comlink';
+import { LimitedCache } from '@ente/shared/storage/cacheStorage/types';
+import {
+ ProxiedWorkerLimitedCache,
+ WorkerSafeElectronClient,
+} from './worker/client';
+import { wrap } from 'comlink';
+import { deserializeToResponse, serializeResponse } from './worker/utils/proxy';
+import { runningInWorker } from '@ente/shared/platform';
+
+export interface LimitedElectronAPIs {
+ openDiskCache: (cacheName: string) => Promise<LimitedCache>;
+ deleteDiskCache: (cacheName: string) => Promise<boolean>;
+ getSentryUserID: () => Promise<string>;
+}
+
+class WorkerSafeElectronServiceImpl implements LimitedElectronAPIs {
+ proxiedElectron:
+ | Comlink.Remote<WorkerSafeElectronClient>
+ | WorkerSafeElectronClient;
+ ready: Promise<any>;
+
+ constructor() {
+ this.ready = this.init();
+ }
+ private async init() {
+ if (runningInWorker()) {
+ const workerSafeElectronClient =
+ wrap<typeof WorkerSafeElectronClient>(self);
+
+ this.proxiedElectron = await new workerSafeElectronClient();
+ } else {
+ this.proxiedElectron = new WorkerSafeElectronClient();
+ }
+ }
+ async openDiskCache(cacheName: string) {
+ await this.ready;
+ const cache = await this.proxiedElectron.openDiskCache(cacheName);
+ return {
+ match: transformMatch(cache.match.bind(cache)),
+ put: transformPut(cache.put.bind(cache)),
+ delete: cache.delete.bind(cache),
+ };
+ }
+
+ async deleteDiskCache(cacheName: string) {
+ await this.ready;
+ return await this.proxiedElectron.deleteDiskCache(cacheName);
+ }
+
+ async getSentryUserID() {
+ await this.ready;
+ return this.proxiedElectron.getSentryUserID();
+ }
+}
+
+export const WorkerSafeElectronService = new WorkerSafeElectronServiceImpl();
+
+function transformMatch(
+ fn: ProxiedWorkerLimitedCache['match']
+): LimitedCache['match'] {
+ return async (key: string) => {
+ return deserializeToResponse(await fn(key));
+ };
+}
+
+function transformPut(
+ fn: ProxiedWorkerLimitedCache['put']
+): LimitedCache['put'] {
+ return async (key: string, data: Response) => {
+ fn(key, await serializeResponse(data));
+ };
+}
diff --git a/packages/shared/storage/cacheStorage/workerElectron/client.ts b/packages/shared/electron/worker/client.ts
similarity index 55%
rename from packages/shared/storage/cacheStorage/workerElectron/client.ts
rename to packages/shared/electron/worker/client.ts
index 62414b15e9..c7d1666614 100644
--- a/packages/shared/storage/cacheStorage/workerElectron/client.ts
+++ b/packages/shared/electron/worker/client.ts
@@ -1,16 +1,21 @@
import * as Comlink from 'comlink';
-import {
- LimitedCache,
- ProxiedLimitedCacheStorage,
- ProxiedWorkerLimitedCache,
-} from '@ente/shared/storage/cacheStorage/types';
+import { LimitedCache } from '@ente/shared/storage/cacheStorage/types';
import { serializeResponse, deserializeToResponse } from './utils/proxy';
import ElectronAPIs from '@ente/shared/electron';
-export class WorkerElectronCacheStorageClient
- implements ProxiedLimitedCacheStorage
-{
- async open(cacheName: string) {
+export interface ProxiedLimitedElectronAPIs {
+ openDiskCache: (cacheName: string) => Promise<ProxiedWorkerLimitedCache>;
+ deleteDiskCache: (cacheName: string) => Promise<boolean>;
+ getSentryUserID: () => Promise<string>;
+}
+export interface ProxiedWorkerLimitedCache {
+ match: (key: string) => Promise<ArrayBuffer>;
+ put: (key: string, data: ArrayBuffer) => Promise<void>;
+ delete: (key: string) => Promise<boolean>;
+}
+
+export class WorkerSafeElectronClient implements ProxiedLimitedElectronAPIs {
+ async openDiskCache(cacheName: string) {
const cache = await ElectronAPIs.openDiskCache(cacheName);
return Comlink.proxy({
match: Comlink.proxy(transformMatch(cache.match.bind(cache))),
@@ -19,9 +24,13 @@ export class WorkerElectronCacheStorageClient
});
}
- async delete(cacheName: string) {
+ async deleteDiskCache(cacheName: string) {
return await ElectronAPIs.deleteDiskCache(cacheName);
}
+
+ async getSentryUserID() {
+ return await ElectronAPIs.getSentryUserID();
+ }
}
function transformMatch(
diff --git a/packages/shared/storage/cacheStorage/workerElectron/utils/proxy.ts b/packages/shared/electron/worker/utils/proxy.ts
similarity index 100%
rename from packages/shared/storage/cacheStorage/workerElectron/utils/proxy.ts
rename to packages/shared/electron/worker/utils/proxy.ts
diff --git a/packages/shared/storage/cacheStorage/workerElectron/utils/transferHandler.ts b/packages/shared/electron/worker/utils/transferHandler.ts
similarity index 100%
rename from packages/shared/storage/cacheStorage/workerElectron/utils/transferHandler.ts
rename to packages/shared/electron/worker/utils/transferHandler.ts
diff --git a/packages/shared/sentry/utils.ts b/packages/shared/sentry/utils.ts
index 680d2029e4..20f342c77a 100644
--- a/packages/shared/sentry/utils.ts
+++ b/packages/shared/sentry/utils.ts
@@ -1,4 +1,4 @@
-import ElectronAPIs from '@ente/shared/electron';
+import { WorkerSafeElectronService } from '@ente/shared/electron/service';
import {
getLocalSentryUserID,
setLocalSentryUserID,
@@ -12,7 +12,7 @@ import { HttpStatusCode } from 'axios';
export async function getSentryUserID() {
if (isElectron()) {
- return await ElectronAPIs.getSentryUserID();
+ return await WorkerSafeElectronService.getSentryUserID();
} else {
let anonymizeUserID = getLocalSentryUserID();
if (!anonymizeUserID) {
diff --git a/packages/shared/storage/cacheStorage/factory.ts b/packages/shared/storage/cacheStorage/factory.ts
index 7d4b5a969c..fc88d604d6 100644
--- a/packages/shared/storage/cacheStorage/factory.ts
+++ b/packages/shared/storage/cacheStorage/factory.ts
@@ -1,28 +1,17 @@
import { LimitedCacheStorage } from './types';
-import { runningInElectron, runningInWorker } from '@ente/shared/platform';
-import { WorkerElectronCacheStorageService } from './workerElectron/service';
-import ElectronAPIs from '@ente/shared/electron';
-
+import { runningInElectron } from '@ente/shared/platform';
+import { WorkerSafeElectronService } from '@ente/shared/electron/service';
class cacheStorageFactory {
- workerElectronCacheStorageServiceInstance: WorkerElectronCacheStorageService;
getCacheStorage(): LimitedCacheStorage {
if (runningInElectron()) {
- if (runningInWorker()) {
- if (!this.workerElectronCacheStorageServiceInstance) {
- this.workerElectronCacheStorageServiceInstance =
- new WorkerElectronCacheStorageService();
- }
- return this.workerElectronCacheStorageServiceInstance;
- } else {
- return {
- open(cacheName) {
- return ElectronAPIs.openDiskCache(cacheName);
- },
- delete(cacheName) {
- return ElectronAPIs.deleteDiskCache(cacheName);
- },
- };
- }
+ return {
+ open(cacheName) {
+ return WorkerSafeElectronService.openDiskCache(cacheName);
+ },
+ delete(cacheName) {
+ return WorkerSafeElectronService.deleteDiskCache(cacheName);
+ },
+ };
} else {
return transformBrowserCacheStorageToLimitedCacheStorage(caches);
}
diff --git a/packages/shared/storage/cacheStorage/types.ts b/packages/shared/storage/cacheStorage/types.ts
index 2920ece105..8980ca08f4 100644
--- a/packages/shared/storage/cacheStorage/types.ts
+++ b/packages/shared/storage/cacheStorage/types.ts
@@ -8,13 +8,3 @@ export interface LimitedCache {
put: (key: string, data: Response) => Promise<void>;
delete: (key: string) => Promise<boolean>;
}
-
-export interface ProxiedLimitedCacheStorage {
- open: (cacheName: string) => Promise<ProxiedWorkerLimitedCache>;
- delete: (cacheName: string) => Promise<boolean>;
-}
-export interface ProxiedWorkerLimitedCache {
- match: (key: string) => Promise<ArrayBuffer>;
- put: (key: string, data: ArrayBuffer) => Promise<void>;
- delete: (key: string) => Promise<boolean>;
-}
diff --git a/packages/shared/storage/cacheStorage/workerElectron/service.ts b/packages/shared/storage/cacheStorage/workerElectron/service.ts
deleted file mode 100644
index c1fe502200..0000000000
--- a/packages/shared/storage/cacheStorage/workerElectron/service.ts
+++ /dev/null
@@ -1,55 +0,0 @@
-import * as Comlink from 'comlink';
-import {
- LimitedCache,
- LimitedCacheStorage,
- ProxiedWorkerLimitedCache,
-} from '../types';
-import { WorkerElectronCacheStorageClient } from './client';
-import { wrap } from 'comlink';
-import { deserializeToResponse, serializeResponse } from './utils/proxy';
-
-export class WorkerElectronCacheStorageService implements LimitedCacheStorage {
- proxiedElectronCacheService: Comlink.Remote<WorkerElectronCacheStorageClient>;
- ready: Promise<any>;
-
- constructor() {
- this.ready = this.init();
- }
- async init() {
- const electronCacheStorageProxy =
- wrap<typeof WorkerElectronCacheStorageClient>(self);
-
- this.proxiedElectronCacheService =
- await new electronCacheStorageProxy();
- }
- async open(cacheName: string) {
- await this.ready;
- const cache = await this.proxiedElectronCacheService.open(cacheName);
- return {
- match: transformMatch(cache.match.bind(cache)),
- put: transformPut(cache.put.bind(cache)),
- delete: cache.delete.bind(cache),
- };
- }
-
- async delete(cacheName: string) {
- await this.ready;
- return await this.proxiedElectronCacheService.delete(cacheName);
- }
-}
-
-function transformMatch(
- fn: ProxiedWorkerLimitedCache['match']
-): LimitedCache['match'] {
- return async (key: string) => {
- return deserializeToResponse(await fn(key));
- };
-}
-
-function transformPut(
- fn: ProxiedWorkerLimitedCache['put']
-): LimitedCache['put'] {
- return async (key: string, data: Response) => {
- fn(key, await serializeResponse(data));
- };
-}
diff --git a/packages/shared/worker/comlinkWorker.ts b/packages/shared/worker/comlinkWorker.ts
index 6ab1773736..19ca3f7d7c 100644
--- a/packages/shared/worker/comlinkWorker.ts
+++ b/packages/shared/worker/comlinkWorker.ts
@@ -1,5 +1,5 @@
import { expose, Remote, wrap } from 'comlink';
-import { WorkerElectronCacheStorageClient } from '@ente/shared/storage/cacheStorage/workerElectron/client';
+import { WorkerSafeElectronClient } from '@ente/shared/electron/worker/client';
import { addLocalLog } from '@ente/shared/logging';
export class ComlinkWorker<T extends new () => InstanceType<T>> {
@@ -17,7 +17,7 @@ export class ComlinkWorker<T extends new () => InstanceType<T>> {
addLocalLog(() => `Initiated ${this.name}`);
const comlink = wrap<T>(this.worker);
this.remote = new comlink() as Promise<Remote<InstanceType<T>>>;
- expose(WorkerElectronCacheStorageClient, this.worker);
+ expose(WorkerSafeElectronClient, this.worker);
}
public getName() {