Notifications (#1)
This commit is contained in:
31
src/vapidKeys.ts
Normal file
31
src/vapidKeys.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { readFileSync, writeFileSync } from "node:fs";
|
||||
import { generateVAPIDKeys, VapidKeys } from "web-push";
|
||||
|
||||
class VapidKeysSettings {
|
||||
private _keys: VapidKeys;
|
||||
public get keys(): VapidKeys {
|
||||
return this._keys;
|
||||
}
|
||||
public set keys(value: VapidKeys) {
|
||||
this._keys = value;
|
||||
this.save()
|
||||
}
|
||||
|
||||
constructor() {
|
||||
this.reload()
|
||||
}
|
||||
|
||||
private save() {
|
||||
writeFileSync("./config/keys.json", JSON.stringify(this._keys, undefined, 2))
|
||||
}
|
||||
|
||||
reload() {
|
||||
this._keys = JSON.parse(readFileSync("./config/keys.json", {encoding: "utf-8"}))
|
||||
if (!(this._keys.privateKey && this._keys.publicKey)) {
|
||||
this.keys = generateVAPIDKeys()
|
||||
}
|
||||
console.log("Reloaded VAPID keys");
|
||||
}
|
||||
}
|
||||
|
||||
export default new VapidKeysSettings();
|
||||
Reference in New Issue
Block a user