2022-11-03 19:43:41 +01:00
|
|
|
/*
|
2024-09-06 10:22:13 +02:00
|
|
|
Copyright 2021-2024 New Vector Ltd.
|
2022-11-03 19:43:41 +01:00
|
|
|
|
2024-09-06 10:22:13 +02:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
Please see LICENSE in the repository root for full details.
|
2022-11-03 19:43:41 +01:00
|
|
|
*/
|
|
|
|
|
|
2024-11-14 12:18:43 +01:00
|
|
|
import { merge } from "lodash-es";
|
2024-10-24 17:43:04 -04:00
|
|
|
|
2024-02-21 21:52:31 +01:00
|
|
|
import { getUrlParams } from "../UrlParams";
|
2022-12-21 15:25:08 +00:00
|
|
|
import {
|
|
|
|
|
DEFAULT_CONFIG,
|
|
|
|
|
ConfigOptions,
|
|
|
|
|
ResolvedConfigOptions,
|
|
|
|
|
} from "./ConfigOptions";
|
2022-11-03 19:43:41 +01:00
|
|
|
|
|
|
|
|
export class Config {
|
2024-09-10 09:49:35 +02:00
|
|
|
private static internalInstance: Config | undefined;
|
2022-12-21 10:17:53 +00:00
|
|
|
|
2024-10-24 17:43:04 -04:00
|
|
|
public static get(): ResolvedConfigOptions {
|
2022-12-21 10:17:53 +00:00
|
|
|
if (!this.internalInstance?.config)
|
2022-11-03 19:43:41 +01:00
|
|
|
throw new Error("Config instance read before config got initialized");
|
2022-12-21 10:17:53 +00:00
|
|
|
return this.internalInstance.config;
|
2022-11-03 19:43:41 +01:00
|
|
|
}
|
2022-12-21 10:17:53 +00:00
|
|
|
|
2024-09-10 09:49:35 +02:00
|
|
|
public static async init(): Promise<void> {
|
|
|
|
|
if (!Config.internalInstance?.initPromise) {
|
|
|
|
|
const internalInstance = new Config();
|
|
|
|
|
Config.internalInstance = internalInstance;
|
|
|
|
|
|
2024-11-15 13:25:33 +01:00
|
|
|
Config.internalInstance.initPromise = downloadConfig("/config.json").then(
|
|
|
|
|
(config) => {
|
|
|
|
|
internalInstance.config = merge({}, DEFAULT_CONFIG, config);
|
|
|
|
|
},
|
|
|
|
|
);
|
2024-09-10 09:49:35 +02:00
|
|
|
}
|
2022-11-03 19:43:41 +01:00
|
|
|
return Config.internalInstance.initPromise;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-27 09:45:39 -04:00
|
|
|
/**
|
|
|
|
|
* This is a alternative initializer that does not load anything
|
2024-09-10 09:49:35 +02:00
|
|
|
* from a hosted config file but instead just initializes the config using the
|
2024-08-27 09:45:39 -04:00
|
|
|
* default config.
|
|
|
|
|
*
|
|
|
|
|
* It is supposed to only be used in tests. (It is executed in `vite.setup.js`)
|
|
|
|
|
*/
|
|
|
|
|
public static initDefault(): void {
|
|
|
|
|
Config.internalInstance = new Config();
|
|
|
|
|
Config.internalInstance.config = { ...DEFAULT_CONFIG };
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-20 17:26:45 +00:00
|
|
|
// Convenience accessors
|
|
|
|
|
public static defaultHomeserverUrl(): string | undefined {
|
2024-02-21 21:52:31 +01:00
|
|
|
return (
|
|
|
|
|
getUrlParams().homeserver ??
|
|
|
|
|
Config.get().default_server_config?.["m.homeserver"].base_url
|
|
|
|
|
);
|
2022-12-20 17:26:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static defaultServerName(): string | undefined {
|
2024-02-21 21:52:31 +01:00
|
|
|
const homeserver = getUrlParams().homeserver;
|
|
|
|
|
if (homeserver) {
|
|
|
|
|
const url = new URL(homeserver);
|
|
|
|
|
return url.hostname;
|
|
|
|
|
}
|
2023-06-30 16:43:28 +01:00
|
|
|
return Config.get().default_server_config?.["m.homeserver"].server_name;
|
2022-12-20 17:26:45 +00:00
|
|
|
}
|
|
|
|
|
|
2022-12-21 15:25:08 +00:00
|
|
|
public config?: ResolvedConfigOptions;
|
2022-12-21 10:17:53 +00:00
|
|
|
private initPromise?: Promise<void>;
|
2022-11-03 19:43:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function downloadConfig(
|
2023-10-11 10:42:04 -04:00
|
|
|
configJsonFilename: string,
|
2022-11-09 10:53:33 -05:00
|
|
|
): Promise<ConfigOptions> {
|
2022-11-03 19:43:41 +01:00
|
|
|
const url = new URL(configJsonFilename, window.location.href);
|
2024-11-15 13:25:33 +01:00
|
|
|
const res = await fetch(url);
|
2022-11-03 19:43:41 +01:00
|
|
|
|
2022-11-09 10:53:33 -05:00
|
|
|
if (!res.ok || res.status === 404 || res.status === 0) {
|
2022-11-03 19:43:41 +01:00
|
|
|
// Lack of a config isn't an error, we should just use the defaults.
|
|
|
|
|
// Also treat a blank config as no config, assuming the status code is 0, because we don't get 404s from file:
|
|
|
|
|
// URIs so this is the only way we can not fail if the file doesn't exist when loading from a file:// URI.
|
2022-12-20 17:26:45 +00:00
|
|
|
return DEFAULT_CONFIG;
|
2022-11-03 19:43:41 +01:00
|
|
|
}
|
|
|
|
|
|
2022-11-09 10:53:33 -05:00
|
|
|
return res.json();
|
2022-11-03 19:43:41 +01:00
|
|
|
}
|