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 { export class Platform {
constructor({ container, assetPaths, config, options = null, cryptoExtras = null }) { constructor({ container, assetPaths, config, configURL, options = null, cryptoExtras = null }) {
this._container = container; this._container = container;
this._assetPaths = assetPaths; this._assetPaths = assetPaths;
this._config = config; this._config = config;
this._configURL = configURL;
this.settingsStorage = new SettingsStorage("hydrogen_setting_v1_"); this.settingsStorage = new SettingsStorage("hydrogen_setting_v1_");
this.clock = new Clock(); this.clock = new Clock();
this.encoding = new Encoding(); this.encoding = new Encoding();
@ -142,7 +143,7 @@ export class Platform {
this._serviceWorkerHandler = new ServiceWorkerHandler(); this._serviceWorkerHandler = new ServiceWorkerHandler();
this._serviceWorkerHandler.registerAndStart(assetPaths.serviceWorker); 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 // Only try to use crypto when olm is provided
if(this._assetPaths.olm) { if(this._assetPaths.olm) {
this.crypto = new Crypto(cryptoExtras); this.crypto = new Crypto(cryptoExtras);
@ -165,6 +166,20 @@ export class Platform {
this._workerPromise = undefined; 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) { _createLogger(isDevelopment) {
// Make sure that loginToken does not end up in the logs // Make sure that loginToken does not end up in the logs
const transformer = (item) => { const transformer = (item) => {

View File

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

View File

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

View File

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