WIP - HomeServerApi.js to ts conversion

This commit is contained in:
RMidhunSuresh 2021-11-23 18:09:33 +05:30
parent 145b40f28d
commit 7403cbc389
7 changed files with 99 additions and 79 deletions

View file

@ -19,7 +19,7 @@ import {createEnum} from "../utils/enum";
import {lookupHomeserver} from "./well-known.js"; import {lookupHomeserver} from "./well-known.js";
import {AbortableOperation} from "../utils/AbortableOperation"; import {AbortableOperation} from "../utils/AbortableOperation";
import {ObservableValue} from "../observable/ObservableValue"; import {ObservableValue} from "../observable/ObservableValue";
import {HomeServerApi} from "./net/HomeServerApi.js"; import {HomeServerApi} from "./net/HomeServerApi";
import {Reconnector, ConnectionStatus} from "./net/Reconnector"; import {Reconnector, ConnectionStatus} from "./net/Reconnector";
import {ExponentialRetryDelay} from "./net/ExponentialRetryDelay"; import {ExponentialRetryDelay} from "./net/ExponentialRetryDelay";
import {MediaRepository} from "./net/MediaRepository"; import {MediaRepository} from "./net/MediaRepository";

View file

@ -17,12 +17,23 @@ limitations under the License.
import {encodeQueryParams, encodeBody} from "./common"; import {encodeQueryParams, encodeBody} from "./common";
import {HomeServerRequest} from "./HomeServerRequest"; import {HomeServerRequest} from "./HomeServerRequest";
import type {Reconnector} from "./Reconnector";
import type {IEncodedBody} from "./common";
import type {IRequestOptions, RequestFunction} from "../../platform/types/Platform";
import type {LogItem} from "../../logging/LogItem";
type RequestMethod = "POST" | "GET" | "PUT";
const CS_R0_PREFIX = "/_matrix/client/r0"; const CS_R0_PREFIX = "/_matrix/client/r0";
const DEHYDRATION_PREFIX = "/_matrix/client/unstable/org.matrix.msc2697.v2"; const DEHYDRATION_PREFIX = "/_matrix/client/unstable/org.matrix.msc2697.v2";
export class HomeServerApi { export class HomeServerApi {
constructor({homeserver, accessToken, request, reconnector}) { private readonly _homeserver: string;
private readonly _accessToken: string;
private readonly _requestFn: RequestFunction;
private readonly _reconnector: Reconnector;
constructor({homeserver, accessToken, request, reconnector}: {homeserver: string, accessToken: string, request: RequestFunction, reconnector: Reconnector}) {
// store these both in a closure somehow so it's harder to get at in case of XSS? // 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 // 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._homeserver = homeserver;
@ -31,14 +42,14 @@ export class HomeServerApi {
this._reconnector = reconnector; this._reconnector = reconnector;
} }
_url(csPath, prefix = CS_R0_PREFIX) { _url(csPath: string, prefix: string = CS_R0_PREFIX): string {
return this._homeserver + prefix + csPath; return this._homeserver + prefix + csPath;
} }
_baseRequest(method, url, queryParams, body, options, accessToken) { _baseRequest(method: RequestMethod, url: string, queryParams?: object, body?: object, options?: IRequestOptions, accessToken?: string): HomeServerRequest {
const queryString = encodeQueryParams(queryParams); const queryString = encodeQueryParams(queryParams);
url = `${url}?${queryString}`; url = `${url}?${queryString}`;
let log; let log: LogItem | undefined;
if (options?.log) { if (options?.log) {
const parent = options?.log; const parent = options?.log;
log = parent.child({ log = parent.child({
@ -47,8 +58,8 @@ export class HomeServerApi {
method, method,
}, parent.level.Info); }, parent.level.Info);
} }
let encodedBody; let encodedBody: IEncodedBody["body"];
const headers = new Map(); const headers: Map<string, string | number> = new Map();
if (accessToken) { if (accessToken) {
headers.set("Authorization", `Bearer ${accessToken}`); headers.set("Authorization", `Bearer ${accessToken}`);
} }
@ -56,6 +67,7 @@ export class HomeServerApi {
if (body) { if (body) {
const encoded = encodeBody(body); const encoded = encodeBody(body);
headers.set("Content-Type", encoded.mimeType); headers.set("Content-Type", encoded.mimeType);
//todo: remove this?
headers.set("Content-Length", encoded.length); headers.set("Content-Length", encoded.length);
encodedBody = encoded.body; encodedBody = encoded.body;
} }
@ -86,63 +98,63 @@ export class HomeServerApi {
return hsRequest; return hsRequest;
} }
_unauthedRequest(method, url, queryParams, body, options) { _unauthedRequest(method: RequestMethod, url: string, queryParams?: object, body?: object, options?: IRequestOptions): HomeServerRequest {
return this._baseRequest(method, url, queryParams, body, options, null); return this._baseRequest(method, url, queryParams, body, options);
} }
_authedRequest(method, url, queryParams, body, options) { _authedRequest(method: RequestMethod, url: string, queryParams?: object, body?: object, options?: IRequestOptions): HomeServerRequest {
return this._baseRequest(method, url, queryParams, body, options, this._accessToken); return this._baseRequest(method, url, queryParams, body, options, this._accessToken);
} }
_post(csPath, queryParams, body, options) { _post(csPath: string, queryParams: object, body: object, options?: IRequestOptions): HomeServerRequest {
return this._authedRequest("POST", this._url(csPath, options?.prefix || CS_R0_PREFIX), queryParams, body, options); return this._authedRequest("POST", this._url(csPath, options?.prefix || CS_R0_PREFIX), queryParams, body, options);
} }
_put(csPath, queryParams, body, options) { _put(csPath: string, queryParams: object, body?: object, options?: IRequestOptions): HomeServerRequest {
return this._authedRequest("PUT", this._url(csPath, options?.prefix || CS_R0_PREFIX), queryParams, body, options); return this._authedRequest("PUT", this._url(csPath, options?.prefix || CS_R0_PREFIX), queryParams, body, options);
} }
_get(csPath, queryParams, body, options) { _get(csPath: string, queryParams?: object, body?: object, options?: IRequestOptions): HomeServerRequest {
return this._authedRequest("GET", this._url(csPath, options?.prefix || CS_R0_PREFIX), queryParams, body, options); return this._authedRequest("GET", this._url(csPath, options?.prefix || CS_R0_PREFIX), queryParams, body, options);
} }
sync(since, filter, timeout, options = null) { sync(since: string, filter: string, timeout: number, options?: IRequestOptions) {
return this._get("/sync", {since, timeout, filter}, null, options); return this._get("/sync", {since, timeout, filter}, undefined, options);
} }
// params is from, dir and optionally to, limit, filter. // params is from, dir and optionally to, limit, filter.
messages(roomId, params, options = null) { messages(roomId: string, params: object, options?: IRequestOptions) {
return this._get(`/rooms/${encodeURIComponent(roomId)}/messages`, params, null, options); return this._get(`/rooms/${encodeURIComponent(roomId)}/messages`, params, undefined, options);
} }
// params is at, membership and not_membership // params is at, membership and not_membership
members(roomId, params, options = null) { members(roomId: string, params: object, options?: IRequestOptions) {
return this._get(`/rooms/${encodeURIComponent(roomId)}/members`, params, null, options); return this._get(`/rooms/${encodeURIComponent(roomId)}/members`, params, undefined, options);
} }
send(roomId, eventType, txnId, content, options = null) { send(roomId: string, eventType: string, txnId: string, content: object, options?: IRequestOptions) {
return this._put(`/rooms/${encodeURIComponent(roomId)}/send/${encodeURIComponent(eventType)}/${encodeURIComponent(txnId)}`, {}, content, options); return this._put(`/rooms/${encodeURIComponent(roomId)}/send/${encodeURIComponent(eventType)}/${encodeURIComponent(txnId)}`, {}, content, options);
} }
redact(roomId, eventId, txnId, content, options = null) { redact(roomId: string, eventId: string, txnId: string, content: object, options?: IRequestOptions) {
return this._put(`/rooms/${encodeURIComponent(roomId)}/redact/${encodeURIComponent(eventId)}/${encodeURIComponent(txnId)}`, {}, content, options); return this._put(`/rooms/${encodeURIComponent(roomId)}/redact/${encodeURIComponent(eventId)}/${encodeURIComponent(txnId)}`, {}, content, options);
} }
receipt(roomId, receiptType, eventId, options = null) { receipt(roomId: string, receiptType: string, eventId: string, options?: IRequestOptions) {
return this._post(`/rooms/${encodeURIComponent(roomId)}/receipt/${encodeURIComponent(receiptType)}/${encodeURIComponent(eventId)}`, return this._post(`/rooms/${encodeURIComponent(roomId)}/receipt/${encodeURIComponent(receiptType)}/${encodeURIComponent(eventId)}`,
{}, {}, options); {}, {}, options);
} }
state(roomId, eventType, stateKey, options = null) { state(roomId: string, eventType: string, stateKey: string, options?: IRequestOptions) {
return this._get(`/rooms/${encodeURIComponent(roomId)}/state/${encodeURIComponent(eventType)}/${encodeURIComponent(stateKey)}`, {}, null, options); return this._get(`/rooms/${encodeURIComponent(roomId)}/state/${encodeURIComponent(eventType)}/${encodeURIComponent(stateKey)}`, {}, undefined, options);
} }
getLoginFlows() { getLoginFlows() {
return this._unauthedRequest("GET", this._url("/login"), null, null, null); return this._unauthedRequest("GET", this._url("/login"));
} }
passwordLogin(username, password, initialDeviceDisplayName, options = null) { passwordLogin(username: string, password: string, initialDeviceDisplayName: string, options?: IRequestOptions) {
return this._unauthedRequest("POST", this._url("/login"), null, { return this._unauthedRequest("POST", this._url("/login"), undefined, {
"type": "m.login.password", "type": "m.login.password",
"identifier": { "identifier": {
"type": "m.id.user", "type": "m.id.user",
@ -153,8 +165,8 @@ export class HomeServerApi {
}, options); }, options);
} }
tokenLogin(loginToken, txnId, initialDeviceDisplayName, options = null) { tokenLogin(loginToken: string, txnId: string, initialDeviceDisplayName: string, options?: IRequestOptions) {
return this._unauthedRequest("POST", this._url("/login"), null, { return this._unauthedRequest("POST", this._url("/login"), undefined, {
"type": "m.login.token", "type": "m.login.token",
"identifier": { "identifier": {
"type": "m.id.user", "type": "m.id.user",
@ -165,91 +177,91 @@ export class HomeServerApi {
}, options); }, options);
} }
createFilter(userId, filter, options = null) { createFilter(userId: string, filter: object, options?: IRequestOptions) {
return this._post(`/user/${encodeURIComponent(userId)}/filter`, null, filter, options); return this._post(`/user/${encodeURIComponent(userId)}/filter`, {}, filter, options);
} }
versions(options = null) { versions(options?: IRequestOptions) {
return this._unauthedRequest("GET", `${this._homeserver}/_matrix/client/versions`, null, null, options); return this._unauthedRequest("GET", `${this._homeserver}/_matrix/client/versions`, undefined, undefined, options);
} }
uploadKeys(dehydratedDeviceId, payload, options = null) { uploadKeys(dehydratedDeviceId: string, payload: object, options?: IRequestOptions) {
let path = "/keys/upload"; let path = "/keys/upload";
if (dehydratedDeviceId) { if (dehydratedDeviceId) {
path = path + `/${encodeURIComponent(dehydratedDeviceId)}`; path = path + `/${encodeURIComponent(dehydratedDeviceId)}`;
} }
return this._post(path, null, payload, options); return this._post(path, {}, payload, options);
} }
queryKeys(queryRequest, options = null) { queryKeys(queryRequest: object, options?: IRequestOptions) {
return this._post("/keys/query", null, queryRequest, options); return this._post("/keys/query", {}, queryRequest, options);
} }
claimKeys(payload, options = null) { claimKeys(payload: object, options?: IRequestOptions) {
return this._post("/keys/claim", null, payload, options); return this._post("/keys/claim", {}, payload, options);
} }
sendToDevice(type, payload, txnId, options = null) { sendToDevice(type: string, payload: object, txnId: string, options?: IRequestOptions) {
return this._put(`/sendToDevice/${encodeURIComponent(type)}/${encodeURIComponent(txnId)}`, null, payload, options); return this._put(`/sendToDevice/${encodeURIComponent(type)}/${encodeURIComponent(txnId)}`, {}, payload, options);
} }
roomKeysVersion(version = null, options = null) { roomKeysVersion(version?: string, options?: IRequestOptions) {
let versionPart = ""; let versionPart = "";
if (version) { if (version) {
versionPart = `/${encodeURIComponent(version)}`; versionPart = `/${encodeURIComponent(version)}`;
} }
return this._get(`/room_keys/version${versionPart}`, null, null, options); return this._get(`/room_keys/version${versionPart}`, undefined, undefined, options);
} }
roomKeyForRoomAndSession(version, roomId, sessionId, options = null) { roomKeyForRoomAndSession(version: string, roomId: string, sessionId: string, options?: IRequestOptions) {
return this._get(`/room_keys/keys/${encodeURIComponent(roomId)}/${encodeURIComponent(sessionId)}`, {version}, null, options); return this._get(`/room_keys/keys/${encodeURIComponent(roomId)}/${encodeURIComponent(sessionId)}`, {version}, undefined, options);
} }
uploadAttachment(blob, filename, options = null) { uploadAttachment(blob: Blob, filename: string, options?: IRequestOptions) {
return this._authedRequest("POST", `${this._homeserver}/_matrix/media/r0/upload`, {filename}, blob, options); return this._authedRequest("POST", `${this._homeserver}/_matrix/media/r0/upload`, {filename}, blob, options);
} }
setPusher(pusher, options = null) { setPusher(pusher: object, options?: IRequestOptions) {
return this._post("/pushers/set", null, pusher, options); return this._post("/pushers/set", {}, pusher, options);
} }
getPushers(options = null) { getPushers(options?: IRequestOptions) {
return this._get("/pushers", null, null, options); return this._get("/pushers", undefined, undefined, options);
} }
join(roomId, options = null) { join(roomId: string, options?: IRequestOptions) {
return this._post(`/rooms/${encodeURIComponent(roomId)}/join`, null, null, options); return this._post(`/rooms/${encodeURIComponent(roomId)}/join`, {}, {}, options);
} }
joinIdOrAlias(roomIdOrAlias, options = null) { joinIdOrAlias(roomIdOrAlias: string, options?: IRequestOptions) {
return this._post(`/join/${encodeURIComponent(roomIdOrAlias)}`, null, null, options); return this._post(`/join/${encodeURIComponent(roomIdOrAlias)}`, {}, {}, options);
} }
leave(roomId, options = null) { leave(roomId: string, options?: IRequestOptions) {
return this._post(`/rooms/${encodeURIComponent(roomId)}/leave`, null, null, options); return this._post(`/rooms/${encodeURIComponent(roomId)}/leave`, {}, {}, options);
} }
forget(roomId, options = null) { forget(roomId: string, options?: IRequestOptions) {
return this._post(`/rooms/${encodeURIComponent(roomId)}/forget`, null, null, options); return this._post(`/rooms/${encodeURIComponent(roomId)}/forget`, {}, {}, options);
} }
logout(options = null) { logout(options?: IRequestOptions) {
return this._post(`/logout`, null, null, options); return this._post(`/logout`, {}, {}, options);
} }
getDehydratedDevice(options = {}) { getDehydratedDevice(options: IRequestOptions) {
options.prefix = DEHYDRATION_PREFIX; options.prefix = DEHYDRATION_PREFIX;
return this._get(`/dehydrated_device`, null, null, options); return this._get(`/dehydrated_device`, undefined, undefined, options);
} }
createDehydratedDevice(payload, options = {}) { createDehydratedDevice(payload: object, options: IRequestOptions) {
options.prefix = DEHYDRATION_PREFIX; options.prefix = DEHYDRATION_PREFIX;
return this._put(`/dehydrated_device`, null, payload, options); return this._put(`/dehydrated_device`, {}, payload, options);
} }
claimDehydratedDevice(deviceId, options = {}) { claimDehydratedDevice(deviceId: string, options: IRequestOptions) {
options.prefix = DEHYDRATION_PREFIX; options.prefix = DEHYDRATION_PREFIX;
return this._post(`/dehydrated_device/claim`, null, {device_id: deviceId}, options); return this._post(`/dehydrated_device/claim`, {}, {device_id: deviceId}, options);
} }
} }
@ -258,11 +270,14 @@ import {Request as MockRequest} from "../../mocks/Request.js";
export function tests() { export function tests() {
return { return {
"superficial happy path for GET": async assert => { "superficial happy path for GET": async assert => {
// @ts-ignore
const hsApi = new HomeServerApi({ const hsApi = new HomeServerApi({
request: () => new MockRequest().respond(200, 42), request: () => new MockRequest().respond(200, 42),
homeserver: "https://hs.tld" homeserver: "https://hs.tld",
}); });
const result = await hsApi._get("foo", null, null, null).response(); const result = await hsApi
._get("foo", undefined, undefined, undefined)
.response();
assert.strictEqual(result, 42); assert.strictEqual(result, 42);
} }
} }

View file

@ -23,7 +23,7 @@ export class HomeServerRequest {
// todo: Shouldn't log be of type ILogItem; but ILogItem does not have finish method // todo: Shouldn't log be of type ILogItem; but ILogItem does not have finish method
private readonly _log?: LogItem; private readonly _log?: LogItem;
private _sourceRequest?: RequestResult; private _sourceRequest?: RequestResult;
private readonly _promise: Promise<Request>; private readonly _promise: Promise<any>;
constructor(method: string, url: string, sourceRequest: RequestResult, log?: LogItem) { constructor(method: string, url: string, sourceRequest: RequestResult, log?: LogItem) {
this._log = log; this._log = log;
@ -96,7 +96,7 @@ export class HomeServerRequest {
} }
} }
response(): Promise<Request> { response(): Promise<any> {
return this._promise; return this._promise;
} }
} }

View file

@ -19,7 +19,7 @@ import type {ExponentialRetryDelay} from "./ExponentialRetryDelay";
import type {TimeMeasure} from "../../platform/web/dom/Clock.js"; import type {TimeMeasure} from "../../platform/web/dom/Clock.js";
import type {OnlineStatus} from "../../platform/web/dom/OnlineStatus.js"; import type {OnlineStatus} from "../../platform/web/dom/OnlineStatus.js";
import type {IVersionResponse} from "./types/response"; import type {IVersionResponse} from "./types/response";
import type {HomeServerApi} from "./HomeServerApi.js"; import type {HomeServerApi} from "./HomeServerApi";
export enum ConnectionStatus { export enum ConnectionStatus {
"Waiting", "Waiting",
@ -33,7 +33,7 @@ export class Reconnector {
private readonly _onlineStatus: OnlineStatus; private readonly _onlineStatus: OnlineStatus;
private readonly _state: ObservableValue<ConnectionStatus>; private readonly _state: ObservableValue<ConnectionStatus>;
private _isReconnecting: boolean; private _isReconnecting: boolean;
private _versionsResponse?: IVersionResponse = undefined; private _versionsResponse?: IVersionResponse;
private _stateSince: TimeMeasure; private _stateSince: TimeMeasure;
constructor({retryDelay, createMeasure, onlineStatus}: {retryDelay: ExponentialRetryDelay, createMeasure: () => TimeMeasure, onlineStatus: OnlineStatus}) { constructor({retryDelay, createMeasure, onlineStatus}: {retryDelay: ExponentialRetryDelay, createMeasure: () => TimeMeasure, onlineStatus: OnlineStatus}) {
@ -164,6 +164,7 @@ export function tests() {
const subscription = reconnector.connectionStatus.subscribe(s => { const subscription = reconnector.connectionStatus.subscribe(s => {
statuses.push(s); statuses.push(s);
}); });
// @ts-ignore
reconnector.onRequestFailed(createHsApiMock(1)); reconnector.onRequestFailed(createHsApiMock(1));
await connectionStatus.waitFor(s => s === ConnectionStatus.Waiting).promise; await connectionStatus.waitFor(s => s === ConnectionStatus.Waiting).promise;
clock.elapse(2000); clock.elapse(2000);
@ -184,6 +185,7 @@ export function tests() {
const retryDelay = new _ExponentialRetryDelay(clock.createTimeout); const retryDelay = new _ExponentialRetryDelay(clock.createTimeout);
const reconnector = new Reconnector({retryDelay, onlineStatus, createMeasure}); const reconnector = new Reconnector({retryDelay, onlineStatus, createMeasure});
const {connectionStatus} = reconnector; const {connectionStatus} = reconnector;
// @ts-ignore
reconnector.onRequestFailed(createHsApiMock(1)); reconnector.onRequestFailed(createHsApiMock(1));
await connectionStatus.waitFor(s => s === ConnectionStatus.Waiting).promise; await connectionStatus.waitFor(s => s === ConnectionStatus.Waiting).promise;
onlineStatus.set(true); //skip waiting onlineStatus.set(true); //skip waiting

View file

@ -17,7 +17,7 @@ limitations under the License.
import {AbortError} from "../../utils/error"; import {AbortError} from "../../utils/error";
import {HomeServerError} from "../error.js"; import {HomeServerError} from "../error.js";
import {HomeServerApi} from "./HomeServerApi.js"; import {HomeServerApi} from "./HomeServerApi";
import {ExponentialRetryDelay} from "./ExponentialRetryDelay"; import {ExponentialRetryDelay} from "./ExponentialRetryDelay";
import {Clock} from "../../platform/web/dom/Clock.js"; import {Clock} from "../../platform/web/dom/Clock.js";
import type {HomeServerRequest} from "./HomeServerRequest.js"; import type {HomeServerRequest} from "./HomeServerRequest.js";

View file

@ -23,7 +23,7 @@ export interface IEncodedBody {
length: number; length: number;
} }
export function encodeQueryParams(queryParams: object): string { export function encodeQueryParams(queryParams?: object): string {
return Object.entries(queryParams || {}) return Object.entries(queryParams || {})
.filter(([, value]) => value !== undefined) .filter(([, value]) => value !== undefined)
.map(([name, value]) => { .map(([name, value]) => {

View file

@ -18,15 +18,18 @@ limitations under the License.
import type {RequestResult} from "../web/dom/request/fetch.js"; import type {RequestResult} from "../web/dom/request/fetch.js";
import type {IEncodedBody} from "../../matrix/net/common"; import type {IEncodedBody} from "../../matrix/net/common";
import {LogItem} from "../../logging/LogItem";
interface IRequestOptions { export interface IRequestOptions {
uploadProgress?: (loadedBytes: number) => void; uploadProgress?: (loadedBytes: number) => void;
timeout?: number; timeout?: number;
body?: IEncodedBody; body?: IEncodedBody;
headers?: { [key: string]: number | string }; headers?: Map<string, string|number>;
cache?: boolean; cache?: boolean;
method: string; log?: LogItem;
format: string; prefix?: string;
method?: string;
format?: string;
} }
export type Request = (url: string, options: IRequestOptions) => RequestResult; export type RequestFunction = (url: string, options: IRequestOptions) => RequestResult;