import type { KvPrimitives } from "../data/kv_primitives.ts"; import type { Plug } from "./plug.ts"; import type { Manifest } from "./types.ts"; export interface ManifestCache { getManifest( plug: Plug, cacheKey: string, cacheHash: number, ): Promise>; } export class KVPrimitivesManifestCache implements ManifestCache { constructor( private kv: KvPrimitives, private manifestPrefix: string, ) {} async getManifest( plug: Plug, cacheKey: string, cacheHash: number, ): Promise> { const [cached] = await this.kv.batchGet([[this.manifestPrefix, cacheKey]]); if (cached && cached.hash === cacheHash) { // console.log("Using KV cached manifest for", plug.name); return cached.manifest; } await plug.sandbox.init(); const manifest = plug.sandbox.manifest!; await this.kv.batchSet([ { key: [this.manifestPrefix, cacheKey], // Deliverately removing the assets from the manifest to preserve space, will be re-added upon load of actual worker value: { manifest: { ...manifest, assets: undefined }, hash: cacheHash, }, }, ]); return manifest; } } export class InMemoryManifestCache implements ManifestCache { private cache = new Map< string, { manifest: Manifest; hash: number; } >(); async getManifest( plug: Plug, cacheKey: string, cacheHash: number, ): Promise> { const cached = this.cache.get(cacheKey); if (cached && cached.hash === cacheHash) { // console.log("Using memory cached manifest for", plug.name); return cached.manifest; } await plug.sandbox.init(); const manifest = plug.sandbox.manifest!; // Deliverately removing the assets from the manifest to preserve space, will be re-added upon load of actual worker this.cache.set(cacheKey, { manifest: { ...manifest, assets: undefined }, hash: cacheHash, }); return manifest; } }