From 4b19e3c498fa850d54817ffdbbfabf22fda5eb94 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Fri, 9 Apr 2021 16:27:25 +0200 Subject: [PATCH 1/4] remove unused member --- src/matrix/SessionContainer.js | 3 +-- src/matrix/net/HomeServerApi.js | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/matrix/SessionContainer.js b/src/matrix/SessionContainer.js index 57752309..4e17e010 100644 --- a/src/matrix/SessionContainer.js +++ b/src/matrix/SessionContainer.js @@ -109,7 +109,7 @@ export class SessionContainer { let sessionInfo; try { const request = this._platform.request; - const hsApi = new HomeServerApi({homeServer, request, createTimeout: clock.createTimeout}); + const hsApi = new HomeServerApi({homeServer, request}); const loginData = await hsApi.passwordLogin(username, password, "Hydrogen", {log}).response(); const sessionId = this.createNewSessionId(); sessionInfo = { @@ -169,7 +169,6 @@ export class SessionContainer { accessToken: sessionInfo.accessToken, request: this._platform.request, reconnector: this._reconnector, - createTimeout: clock.createTimeout }); this._sessionId = sessionInfo.id; this._storage = await this._platform.storageFactory.create(sessionInfo.id); diff --git a/src/matrix/net/HomeServerApi.js b/src/matrix/net/HomeServerApi.js index cbcb55ab..051bb44a 100644 --- a/src/matrix/net/HomeServerApi.js +++ b/src/matrix/net/HomeServerApi.js @@ -19,13 +19,12 @@ import {encodeQueryParams, encodeBody} from "./common.js"; import {HomeServerRequest} from "./HomeServerRequest.js"; export class HomeServerApi { - constructor({homeServer, accessToken, request, createTimeout, reconnector}) { + constructor({homeServer, accessToken, request, reconnector}) { // store these both in a closure somehow so it's harder to get at in case of XSS? // one could change the homeserver as well so the token gets sent there, so both must be protected from read/write this._homeserver = homeServer; this._accessToken = accessToken; this._requestFn = request; - this._createTimeout = createTimeout; this._reconnector = reconnector; } From b852feeb9cbb65b2072084bd38f628f5ac34d3df Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Fri, 9 Apr 2021 16:28:14 +0200 Subject: [PATCH 2/4] ConnectionError isn't throw from start, but sets sync.error --- src/matrix/SessionContainer.js | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/matrix/SessionContainer.js b/src/matrix/SessionContainer.js index 4e17e010..5191e745 100644 --- a/src/matrix/SessionContainer.js +++ b/src/matrix/SessionContainer.js @@ -234,19 +234,18 @@ export class SessionContainer { } async _waitForFirstSync() { - try { - this._sync.start(); - this._status.set(LoadStatus.FirstSync); - } catch (err) { - // swallow ConnectionError here and continue, - // as the reconnector above will call - // sync.start again to retry in this case - if (!(err instanceof ConnectionError)) { - throw err; - } - } + this._sync.start(); + this._status.set(LoadStatus.FirstSync); // only transition into Ready once the first sync has succeeded - this._waitForFirstSyncHandle = this._sync.status.waitFor(s => s === SyncStatus.Syncing || s === SyncStatus.Stopped); + this._waitForFirstSyncHandle = this._sync.status.waitFor(s => { + if (s === SyncStatus.Stopped) { + // keep waiting if there is a ConnectionError + // as the reconnector above will call + // sync.start again to retry in this case + return this._sync.error.name !== "ConnectionError"; + } + return s === SyncStatus.Syncing; + }); try { await this._waitForFirstSyncHandle.promise; if (this._sync.status.get() === SyncStatus.Stopped) { From ba38ce7976c8bac43a06a7e8a776c7b358bee1b8 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Fri, 9 Apr 2021 16:30:53 +0200 Subject: [PATCH 3/4] use canonical way of check error type --- src/matrix/SessionContainer.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/matrix/SessionContainer.js b/src/matrix/SessionContainer.js index 5191e745..55acd4e7 100644 --- a/src/matrix/SessionContainer.js +++ b/src/matrix/SessionContainer.js @@ -21,7 +21,6 @@ import {Reconnector, ConnectionStatus} from "./net/Reconnector.js"; import {ExponentialRetryDelay} from "./net/ExponentialRetryDelay.js"; import {MediaRepository} from "./net/MediaRepository.js"; import {RequestScheduler} from "./net/RequestScheduler.js"; -import {HomeServerError, ConnectionError, AbortError} from "./error.js"; import {Sync, SyncStatus} from "./Sync.js"; import {Session} from "./Session.js"; @@ -124,7 +123,7 @@ export class SessionContainer { await this._platform.sessionInfoStorage.add(sessionInfo); } catch (err) { this._error = err; - if (err instanceof HomeServerError) { + if (err.name === "HomeServerError") { if (err.errcode === "M_FORBIDDEN") { this._loginFailure = LoginFailure.Credentials; } else { @@ -132,7 +131,7 @@ export class SessionContainer { } log.set("loginFailure", this._loginFailure); this._status.set(LoadStatus.LoginFailed); - } else if (err instanceof ConnectionError) { + } else if (err.name === "ConnectionError") { this._loginFailure = LoginFailure.Connection; this._status.set(LoadStatus.LoginFailed); } else { @@ -253,7 +252,7 @@ export class SessionContainer { } } catch (err) { // if dispose is called from stop, bail out - if (err instanceof AbortError) { + if (err.name === "AbortError") { return; } throw err; From d414fb6b94ef5f08d10f2a6876731f6c3dede2d4 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Fri, 9 Apr 2021 19:50:22 +0200 Subject: [PATCH 4/4] sync can also stop without an error --- src/matrix/SessionContainer.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/matrix/SessionContainer.js b/src/matrix/SessionContainer.js index 55acd4e7..07c4a870 100644 --- a/src/matrix/SessionContainer.js +++ b/src/matrix/SessionContainer.js @@ -241,13 +241,13 @@ export class SessionContainer { // keep waiting if there is a ConnectionError // as the reconnector above will call // sync.start again to retry in this case - return this._sync.error.name !== "ConnectionError"; + return this._sync.error?.name !== "ConnectionError"; } return s === SyncStatus.Syncing; }); try { await this._waitForFirstSyncHandle.promise; - if (this._sync.status.get() === SyncStatus.Stopped) { + if (this._sync.status.get() === SyncStatus.Stopped && this._sync.error) { throw this._sync.error; } } catch (err) {