mirror of
https://github.com/siteboon/claudecodeui.git
synced 2026-06-03 11:05:35 +08:00
43 lines
1005 B
TypeScript
43 lines
1005 B
TypeScript
import webPush from 'web-push';
|
|
|
|
import { vapidKeysDb } from '@/shared/database/repositories/vapid-keys.js';
|
|
|
|
type VapidKeyPair = {
|
|
publicKey: string;
|
|
privateKey: string;
|
|
};
|
|
|
|
let cachedKeys: VapidKeyPair | null = null;
|
|
|
|
function ensureVapidKeys(): VapidKeyPair {
|
|
if (cachedKeys) return cachedKeys;
|
|
|
|
const existingKeys = vapidKeysDb.getVapidKeys();
|
|
if (existingKeys) {
|
|
cachedKeys = existingKeys;
|
|
return existingKeys;
|
|
}
|
|
|
|
const generatedKeys = webPush.generateVAPIDKeys();
|
|
vapidKeysDb.createVapidKeys(generatedKeys.publicKey, generatedKeys.privateKey);
|
|
cachedKeys = generatedKeys;
|
|
return generatedKeys;
|
|
}
|
|
|
|
function getPublicKey(): string {
|
|
return ensureVapidKeys().publicKey;
|
|
}
|
|
|
|
function configureWebPush(): void {
|
|
const keys = ensureVapidKeys();
|
|
webPush.setVapidDetails(
|
|
'mailto:noreply@claudecodeui.local',
|
|
keys.publicKey,
|
|
keys.privateKey
|
|
);
|
|
console.log('Web Push notifications configured');
|
|
}
|
|
|
|
export { ensureVapidKeys, getPublicKey, configureWebPush };
|
|
|