MCPcopy Create free account
hub / github.com/ProtonDriveApps/sdk / DriveCryptoCacheAdapter

Class DriveCryptoCacheAdapter

cli/src/cache/driveCryptoCacheAdapter.ts:37–86  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

35 * of the whole value.
36 */
37export class DriveCryptoCacheAdapter implements ProtonDriveCache<CachedCryptoMaterial> {
38 constructor(private readonly cache: ProtonDriveCache<string>) {}
39
40 async setEntity(key: string, value: CachedCryptoMaterial, tags?: string[]): Promise<void> {
41 await this.cache.setEntity(key, await serializeCachedCryptoMaterial(value), tags);
42 }
43
44 async getEntity(key: string): Promise<CachedCryptoMaterial> {
45 return deserializeCachedCryptoMaterial(await this.cache.getEntity(key));
46 }
47
48 async *iterateEntities(keys: string[]): AsyncGenerator<EntityResult<CachedCryptoMaterial>> {
49 for await (const result of this.cache.iterateEntities(keys)) {
50 yield await this.convertResult(result);
51 }
52 }
53
54 async *iterateEntitiesByTag(tag: string): AsyncGenerator<EntityResult<CachedCryptoMaterial>> {
55 for await (const result of this.cache.iterateEntitiesByTag(tag)) {
56 yield await this.convertResult(result);
57 }
58 }
59
60 private async convertResult(result: EntityResult<string>): Promise<EntityResult<CachedCryptoMaterial>> {
61 if (!result.ok) {
62 return result;
63 }
64 try {
65 return {
66 key: result.key,
67 ok: true,
68 value: await deserializeCachedCryptoMaterial(result.value),
69 };
70 } catch (error: unknown) {
71 return {
72 key: result.key,
73 ok: false,
74 error: error instanceof Error ? error.message : `${error}`,
75 };
76 }
77 }
78
79 async clear(): Promise<void> {
80 return this.cache.clear();
81 }
82
83 async removeEntities(keys: string[]): Promise<void> {
84 return this.cache.removeEntities(keys);
85 }
86}
87
88async function serializeCachedCryptoMaterial(value: CachedCryptoMaterial): Promise<string> {
89 const output: SerializedCachedCryptoMaterial = { v: VERSION };

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected