2024-02-28 00:53:58 +00:00
|
|
|
// Copyright 2024 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { join } from 'path';
|
|
|
|
import { readFile } from 'fs/promises';
|
|
|
|
import { DNSFallbackSchema } from '../ts/types/DNSFallback';
|
|
|
|
import type { DNSFallbackType } from '../ts/types/DNSFallback';
|
2024-10-02 19:03:10 +00:00
|
|
|
import { parseUnknown } from '../ts/util/schemas';
|
2024-02-28 00:53:58 +00:00
|
|
|
|
|
|
|
let cached: DNSFallbackType | undefined;
|
|
|
|
|
|
|
|
export async function getDNSFallback(): Promise<DNSFallbackType> {
|
|
|
|
if (cached != null) {
|
|
|
|
return cached;
|
|
|
|
}
|
|
|
|
|
|
|
|
const configPath = join(__dirname, '..', 'build', 'dns-fallback.json');
|
|
|
|
let str: string;
|
|
|
|
try {
|
|
|
|
str = await readFile(configPath, 'utf8');
|
|
|
|
} catch (error) {
|
|
|
|
console.error(
|
2024-08-05 18:41:37 +00:00
|
|
|
'Warning: build/dns-fallback.json not build, run `npm run build:dns-fallback`'
|
2024-02-28 00:53:58 +00:00
|
|
|
);
|
|
|
|
cached = [];
|
|
|
|
return cached;
|
|
|
|
}
|
|
|
|
|
2024-10-02 19:03:10 +00:00
|
|
|
const json: unknown = JSON.parse(str);
|
2024-02-28 00:53:58 +00:00
|
|
|
|
2024-10-02 19:03:10 +00:00
|
|
|
const result = parseUnknown(DNSFallbackSchema, json);
|
2024-02-28 00:53:58 +00:00
|
|
|
cached = result;
|
|
|
|
return result;
|
|
|
|
}
|