2023-12-06 17:44:48 +00:00
|
|
|
import { KvPrimitives } from "./lib/kv_primitives.ts";
|
|
|
|
import { Plug } from "./plug.ts";
|
|
|
|
import { Manifest } from "./types.ts";
|
|
|
|
|
|
|
|
export interface ManifestCache<T> {
|
|
|
|
getManifest(plug: Plug<T>, hash: number): Promise<Manifest<T>>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class KVPrimitivesManifestCache<T> implements ManifestCache<T> {
|
|
|
|
constructor(private kv: KvPrimitives, private manifestPrefix: string) {
|
|
|
|
}
|
|
|
|
|
|
|
|
async getManifest(plug: Plug<T>, hash: number): Promise<Manifest<T>> {
|
|
|
|
const [cached] = await this.kv.batchGet([[
|
|
|
|
this.manifestPrefix,
|
|
|
|
plug.name,
|
|
|
|
]]);
|
|
|
|
if (cached && cached.hash === hash) {
|
|
|
|
// 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, plug.name],
|
2023-12-10 14:18:34 +00:00
|
|
|
// Deliverately removing the assets from the manifest to preserve space, will be re-added upon load of actual worker
|
2023-12-09 17:08:46 +00:00
|
|
|
value: { manifest: { ...manifest, assets: undefined }, hash },
|
2023-12-06 17:44:48 +00:00
|
|
|
}]);
|
|
|
|
return manifest;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class InMemoryManifestCache<T> implements ManifestCache<T> {
|
|
|
|
private cache = new Map<string, {
|
|
|
|
manifest: Manifest<T>;
|
|
|
|
hash: number;
|
|
|
|
}>();
|
|
|
|
|
|
|
|
async getManifest(plug: Plug<T>, hash: number): Promise<Manifest<T>> {
|
|
|
|
const cached = this.cache.get(plug.workerUrl.href);
|
|
|
|
if (cached && cached.hash === hash) {
|
|
|
|
// console.log("Using memory cached manifest for", plug.name);
|
|
|
|
return cached.manifest;
|
|
|
|
}
|
|
|
|
await plug.sandbox.init();
|
|
|
|
const manifest = plug.sandbox.manifest!;
|
2023-12-10 14:18:34 +00:00
|
|
|
// Deliverately removing the assets from the manifest to preserve space, will be re-added upon load of actual worker
|
|
|
|
this.cache.set(plug.name!, {
|
|
|
|
manifest: { ...manifest, assets: undefined },
|
|
|
|
hash,
|
|
|
|
});
|
2023-12-06 17:44:48 +00:00
|
|
|
return manifest;
|
|
|
|
}
|
|
|
|
}
|