import { getOrGenerateKey } from "https://deno.land/x/deco@1.35.3/commons/secrets/keys.ts";
The overall behavior here is to generate the key in the first use and then use it for the entire environment life (across deployments). Essentially we try to retrieve the key from the memory and then fallback to KV using the following order.
- Use the in-memory key variable if available (which means it was retrieved at least once)
- If not, try fetch from KV (which means it was generated at least once)
- If not available, on KV so it needs to be generated. So we generate a new key and then try to atomically save it on KV. in that way we avoid concurrency of two keys being saved/stored in memory at the same time.
- If any transaction error occur so we try to retrieve from KV again and use it for the entire isolate life.