[backend] Generate VAPID keys during meta bootstrap

This commit is contained in:
Laura Hausmann 2023-10-17 18:44:59 +02:00
parent 4006e74eca
commit eadf9acdc3
Signed by: zotan
GPG key ID: D044E84C5BE01605

View file

@ -1,5 +1,6 @@
import { db } from "@/db/postgre.js"; import { db } from "@/db/postgre.js";
import { Meta } from "@/models/entities/meta.js"; import { Meta } from "@/models/entities/meta.js";
import push from 'web-push';
let cache: Meta; let cache: Meta;
@ -46,12 +47,16 @@ export async function fetchMeta(noCache = false): Promise<Meta> {
cache = meta; cache = meta;
return meta; return meta;
} else { } else {
const { publicKey, privateKey } = push.generateVAPIDKeys();
// If fetchMeta is called at the same time when meta is empty, this part may be called at the same time, so use fail-safe upsert. // If fetchMeta is called at the same time when meta is empty, this part may be called at the same time, so use fail-safe upsert.
const saved = await transactionalEntityManager const saved = await transactionalEntityManager
.upsert( .upsert(
Meta, Meta,
{ {
id: "x", id: "x",
swPublicKey: publicKey,
swPrivateKey: privateKey,
}, },
["id"], ["id"],
) )