Read config from URL

This commit is contained in:
RMidhunSuresh 2022-04-20 12:25:23 +05:30
parent 2cfcd4653f
commit 6cd3c8ee2b
4 changed files with 22 additions and 6 deletions

View file

@ -126,10 +126,11 @@ function adaptUIOnVisualViewportResize(container) {
}
export class Platform {
constructor({ container, assetPaths, config, options = null, cryptoExtras = null }) {
constructor({ container, assetPaths, config, configURL, options = null, cryptoExtras = null }) {
this._container = container;
this._assetPaths = assetPaths;
this._config = config;
this._configURL = configURL;
this.settingsStorage = new SettingsStorage("hydrogen_setting_v1_");
this.clock = new Clock();
this.encoding = new Encoding();
@ -142,7 +143,7 @@ export class Platform {
this._serviceWorkerHandler = new ServiceWorkerHandler();
this._serviceWorkerHandler.registerAndStart(assetPaths.serviceWorker);
}
this.notificationService = new NotificationService(this._serviceWorkerHandler, config.push);
this.notificationService = null;
// Only try to use crypto when olm is provided
if(this._assetPaths.olm) {
this.crypto = new Crypto(cryptoExtras);
@ -165,6 +166,20 @@ export class Platform {
this._workerPromise = undefined;
}
async init() {
if (!this._config) {
if (!this._configURL) {
throw new Error("Neither config nor configURL was provided!");
}
const {body}= await this.request(this._configURL, {method: "GET", format: "json"}).response();
this._config = body;
}
this._notificationService = new NotificationService(
this._serviceWorkerHandler,
this._config.push
);
}
_createLogger(isDevelopment) {
// Make sure that loginToken does not end up in the logs
const transformer = (item) => {

View file

@ -17,7 +17,7 @@
<script id="main" type="module">
import {main} from "./main";
import {Platform} from "./Platform";
import configJSON from "./assets/config.json?raw";
import configURL from "./assets/config.json?url";
import assetPaths from "./sdk/paths/vite";
if (import.meta.env.PROD) {
assetPaths.serviceWorker = "sw.js";
@ -25,10 +25,9 @@
const platform = new Platform({
container: document.body,
assetPaths,
config: JSON.parse(configJSON),
configURL,
options: {development: import.meta.env.DEV}
}
);
});
main(platform);
</script>
</body>

View file

@ -32,6 +32,7 @@ export async function main(platform) {
// const recorder = new RecordRequester(createFetchRequest(clock.createTimeout));
// const request = recorder.request;
// window.getBrawlFetchLog = () => recorder.log();
await platform.init();
const navigation = createNavigation();
platform.setNavigation(navigation);
const urlRouter = createRouter({navigation, history: platform.history});

View file

@ -31,6 +31,7 @@ const commonOptions = {
assetsInlineLimit: 0,
polyfillModulePreload: false,
},
assetsInclude: ['**/config.json'],
define: {
DEFINE_VERSION: JSON.stringify(version),
DEFINE_GLOBAL_HASH: JSON.stringify(null),