Compare commits

...

7 commits

Author SHA1 Message Date
RMidhunSuresh e40d74ebce WIP 2021-11-18 15:21:38 +05:30
RMidhunSuresh eaa31eb8fb Pull more props into interface 2021-11-18 13:46:01 +05:30
RMidhunSuresh 866bef7223 begin creating type for CryptoExtras 2021-11-18 12:43:48 +05:30
RMidhunSuresh 998ad06cd7 Add type to container 2021-11-18 12:43:31 +05:30
RMidhunSuresh e6e20044c8 Create interface for Platform config 2021-11-17 22:16:57 +05:30
RMidhunSuresh fe6090183f Rename Platform.js to Platform.ts 2021-11-17 22:16:57 +05:30
RMidhunSuresh cf76e9c605 Create file for types 2021-11-17 22:16:57 +05:30
4 changed files with 75 additions and 11 deletions

58
src/platform/types/Platform.d.ts vendored Normal file
View file

@ -0,0 +1,58 @@
/*
Copyright 2021 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import type {BaseLogger} from "../../logging/BaseLogger";
import type {SettingsStorage} from "../web/dom/SettingsStorage.js";
import type {Clock} from "../web/dom/Clock.js";
import type {History} from "../web/dom/History.js";
import type {OnlineStatus} from "../web/dom/OnlineStatus.js";
import type {ServiceWorkerHandler} from "../web/dom/ServiceWorkerHandler.js";
import type {Encoding} from "../web/utils/Encoding.js";
export interface IPlatformConfig {
worker: string;
downloadSandbox: string;
defaultHomeServer: string;
serviceWorker?: string;
olm: {
wasm: string;
legacyBundle: string;
wasmBundle: string;
};
push: {
}
}
export interface IPlatformOptions {
development?: boolean;
}
export interface CryptoExtras {
aesjs?: any;
hkdf?: any;
}
export interface IPlatform {
readonly logger: BaseLogger;
readonly settingsStorage: SettingsStorage;
readonly clock: Clock;
readonly encoding: Encoding;
readonly random: () => number;
readonly history: History;
readonly onlineStatus: OnlineStatus;
readonly updateService?: ServiceWorkerHandler;
}

View file

@ -16,7 +16,7 @@ limitations under the License.
import aesjs from "../../../lib/aes-js/index.js";
import {hkdf} from "../../utils/crypto/hkdf.js";
import {Platform as ModernPlatform} from "./Platform.js";
import {Platform as ModernPlatform} from "./Platform";
export function Platform(container, paths) {
return new ModernPlatform(container, paths, {aesjs, hkdf});

View file

@ -38,6 +38,8 @@ import {downloadInIframe} from "./dom/download.js";
import {Disposables} from "../../utils/Disposables.js";
import {parseHTML} from "./parsehtml.js";
import {handleAvatarError} from "./ui/avatar.js";
import {IPlatform, IPlatformConfig, IPlatformOptions} from "../types/Platform";
import type {BaseLogger} from "../../logging/BaseLogger.js";
function addScript(src) {
return new Promise(function (resolve, reject) {
@ -124,22 +126,26 @@ function adaptUIOnVisualViewportResize(container) {
};
}
export class Platform {
constructor(container, config, cryptoExtras = null, options = null) {
export class Platform implements IPlatform {
public readonly logger: BaseLogger;
public readonly settingsStorage: SettingsStorage = new SettingsStorage("hydrogen_setting_v1_");
public readonly clock: Clock = new Clock();
public readonly encoding: Encoding = new Encoding();
public readonly random: () => number = Math.random;
public readonly history: History = new History();
public readonly onlineStatus: OnlineStatus = new OnlineStatus();
private readonly _config: IPlatformConfig;
private readonly _container: HTMLElement;
private readonly _serviceWorkerHandler?: ServiceWorkerHandler;
constructor(container: HTMLElement, config: IPlatformConfig, cryptoExtras = null, options?: IPlatformOptions) {
this._config = config;
this._container = container;
this.settingsStorage = new SettingsStorage("hydrogen_setting_v1_");
this.clock = new Clock();
this.encoding = new Encoding();
this.random = Math.random;
if (options?.development) {
this.logger = new ConsoleLogger({platform: this});
} else {
this.logger = new IDBLogger({name: "hydrogen_logs", platform: this});
}
this.history = new History();
this.onlineStatus = new OnlineStatus();
this._serviceWorkerHandler = null;
if (config.serviceWorker && "serviceWorker" in navigator) {
this._serviceWorkerHandler = new ServiceWorkerHandler();
this._serviceWorkerHandler.registerAndStart(config.serviceWorker);

View file

@ -22,7 +22,7 @@
</script>
<script id="main" type="module">
import {main} from "./src/main.js";
import {Platform} from "./src/platform/web/Platform.js";
import {Platform} from "./src/platform/web/Platform";
main(new Platform(document.body, {
worker: "src/worker.js",
downloadSandbox: "assets/download-sandbox.html",