This repository has been archived on 2024-04-14. You can view files and clone it, but cannot push or open issues or pull requests.
icydns/src/keys.ts

36 lines
808 B
TypeScript

import * as fs from 'fs/promises';
import { v4 } from 'uuid';
import path from 'path';
export class Keys {
private keys: Record<string, string> = {};
async load(): Promise<void> {
const file = path.join(__dirname, '..', 'keys.json');
let content = '{}';
try {
content = await fs.readFile(file, { encoding: 'utf-8' });
} catch (e: unknown) {
if ((e as Error).message.includes('ENOENT')) {
fs.writeFile(file, '{}');
} else {
throw e;
}
}
this.keys = JSON.parse(content);
}
getDomain(key: string): string | undefined {
return this.keys[key];
}
getKey(domain: string): string | undefined {
return Object.keys(this.keys).find((key) => this.keys[key] === domain);
}
static generateKey(): string {
return v4();
}
}