hydrogen-web/src/matrix/net/HomeServerApi.js

227 lines
7.1 KiB
JavaScript
Raw Normal View History

import {
2019-06-27 02:01:36 +05:30
HomeServerError,
2020-04-19 22:35:12 +05:30
ConnectionError,
AbortError
2020-04-20 23:17:45 +05:30
} from "../error.js";
2019-02-11 01:55:29 +05:30
2019-02-05 03:56:24 +05:30
class RequestWrapper {
constructor(method, url, requestResult, responsePromise) {
2019-12-23 18:58:27 +05:30
this._requestResult = requestResult;
this._promise = responsePromise.then(response => {
2019-12-23 18:58:27 +05:30
// ok?
if (response.status >= 200 && response.status < 300) {
return response.body;
} else {
switch (response.status) {
default:
2020-03-17 04:37:54 +05:30
throw new HomeServerError(method, url, response.body, response.status);
2019-12-23 18:58:27 +05:30
}
}
});
2019-06-27 02:01:36 +05:30
}
2018-12-21 19:05:24 +05:30
2019-06-27 02:01:36 +05:30
abort() {
2019-12-23 18:58:27 +05:30
return this._requestResult.abort();
2019-06-27 02:01:36 +05:30
}
2018-12-21 19:05:24 +05:30
2019-06-27 02:01:36 +05:30
response() {
return this._promise;
}
2018-12-21 19:05:24 +05:30
}
export class HomeServerApi {
2020-04-05 18:41:15 +05:30
constructor({homeServer, accessToken, request, createTimeout, reconnector}) {
2019-03-09 00:33:47 +05:30
// 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
2019-12-23 18:58:27 +05:30
this._homeserver = homeServer;
2019-06-27 02:01:36 +05:30
this._accessToken = accessToken;
2019-12-23 18:58:27 +05:30
this._requestFn = request;
2020-04-05 18:41:15 +05:30
this._createTimeout = createTimeout;
this._reconnector = reconnector;
2019-06-27 02:01:36 +05:30
}
2018-12-21 19:05:24 +05:30
2019-06-27 02:01:36 +05:30
_url(csPath) {
return `${this._homeserver}/_matrix/client/r0${csPath}`;
}
2018-12-21 19:05:24 +05:30
_abortOnTimeout(timeoutAmount, requestResult, responsePromise) {
const timeout = this._createTimeout(timeoutAmount);
// abort request if timeout finishes first
let timedOut = false;
timeout.elapsed().then(
() => {
timedOut = true;
requestResult.abort();
},
() => {} // ignore AbortError
);
// abort timeout if request finishes first
return responsePromise.then(
response => {
timeout.abort();
return response;
},
err => {
timeout.abort();
// map error to TimeoutError
if (err instanceof AbortError && timedOut) {
throw new ConnectionError(`Request timed out after ${timeoutAmount}ms`, true);
} else {
throw err;
}
}
);
}
2020-05-09 23:32:08 +05:30
_encodeQueryParams(queryParams) {
return Object.entries(queryParams || {})
2019-06-27 02:01:36 +05:30
.filter(([, value]) => value !== undefined)
2019-10-12 23:54:09 +05:30
.map(([name, value]) => {
if (typeof value === "object") {
value = JSON.stringify(value);
}
return `${encodeURIComponent(name)}=${encodeURIComponent(value)}`;
})
2019-06-27 02:01:36 +05:30
.join("&");
2020-05-09 23:32:08 +05:30
}
_request(method, url, queryParams, body, options) {
const queryString = this._encodeQueryParams(queryParams);
2020-03-31 03:26:03 +05:30
url = `${url}?${queryString}`;
2019-06-27 02:01:36 +05:30
let bodyString;
const headers = new Map();
2019-06-27 02:01:36 +05:30
if (this._accessToken) {
headers.set("Authorization", `Bearer ${this._accessToken}`);
2019-06-27 02:01:36 +05:30
}
headers.set("Accept", "application/json");
2019-06-27 02:01:36 +05:30
if (body) {
headers.set("Content-Type", "application/json");
2019-06-27 02:01:36 +05:30
bodyString = JSON.stringify(body);
}
2019-12-23 18:58:27 +05:30
const requestResult = this._requestFn(url, {
2019-06-27 02:01:36 +05:30
method,
headers,
body: bodyString,
});
2020-04-05 18:41:15 +05:30
let responsePromise = requestResult.response();
2020-04-23 00:17:46 +05:30
if (options && options.timeout) {
responsePromise = this._abortOnTimeout(
options.timeout,
requestResult,
responsePromise
2020-04-05 18:41:15 +05:30
);
}
const wrapper = new RequestWrapper(method, url, requestResult, responsePromise);
2020-04-05 18:41:15 +05:30
if (this._reconnector) {
wrapper.response().catch(err => {
if (err.name === "ConnectionError") {
2020-04-05 18:41:15 +05:30
this._reconnector.onRequestFailed(this);
}
});
}
return wrapper;
2019-06-27 02:01:36 +05:30
}
2019-02-05 03:56:24 +05:30
2020-04-05 18:41:15 +05:30
_post(csPath, queryParams, body, options) {
return this._request("POST", this._url(csPath), queryParams, body, options);
2019-06-27 02:01:36 +05:30
}
2019-02-05 03:56:24 +05:30
2020-04-05 18:41:15 +05:30
_put(csPath, queryParams, body, options) {
return this._request("PUT", this._url(csPath), queryParams, body, options);
2019-07-27 01:33:57 +05:30
}
2020-04-05 18:41:15 +05:30
_get(csPath, queryParams, body, options) {
return this._request("GET", this._url(csPath), queryParams, body, options);
2019-06-27 02:01:36 +05:30
}
2018-12-21 19:05:24 +05:30
2020-04-05 18:41:15 +05:30
sync(since, filter, timeout, options = null) {
return this._get("/sync", {since, timeout, filter}, null, options);
2019-06-27 02:01:36 +05:30
}
2019-02-05 03:56:24 +05:30
2019-03-09 05:11:06 +05:30
// params is from, dir and optionally to, limit, filter.
2020-04-05 18:41:15 +05:30
messages(roomId, params, options = null) {
return this._get(`/rooms/${encodeURIComponent(roomId)}/messages`, params, null, options);
2019-03-09 05:11:06 +05:30
}
2020-04-05 18:41:15 +05:30
send(roomId, eventType, txnId, content, options = null) {
return this._put(`/rooms/${encodeURIComponent(roomId)}/send/${encodeURIComponent(eventType)}/${encodeURIComponent(txnId)}`, {}, content, options);
2019-07-27 01:33:57 +05:30
}
2020-04-05 18:41:15 +05:30
passwordLogin(username, password, options = null) {
return this._post("/login", null, {
2019-02-05 03:56:24 +05:30
"type": "m.login.password",
"identifier": {
"type": "m.id.user",
"user": username
},
"password": password
2020-04-05 18:41:15 +05:30
}, options);
2019-06-27 02:01:36 +05:30
}
2019-10-12 23:54:09 +05:30
2020-04-05 18:41:15 +05:30
createFilter(userId, filter, options = null) {
return this._post(`/user/${encodeURIComponent(userId)}/filter`, null, filter, options);
2019-10-12 23:54:09 +05:30
}
2020-03-31 03:26:03 +05:30
2020-04-05 18:41:15 +05:30
versions(options = null) {
2020-05-06 02:43:05 +05:30
return this._request("GET", `${this._homeserver}/_matrix/client/versions`, null, null, options);
2020-03-31 03:26:03 +05:30
}
2020-05-09 23:32:08 +05:30
_parseMxcUrl(url) {
const prefix = "mxc://";
if (url.startsWith(prefix)) {
return url.substr(prefix.length).split("/", 2);
} else {
return null;
}
}
mxcUrlThumbnail(url, width, height, method) {
const parts = this._parseMxcUrl(url);
if (parts) {
const [serverName, mediaId] = parts;
const httpUrl = `${this._homeserver}/_matrix/media/r0/thumbnail/${encodeURIComponent(serverName)}/${encodeURIComponent(mediaId)}`;
return httpUrl + "?" + this._encodeQueryParams({width, height, method});
}
return null;
}
mxcUrl(url) {
const parts = this._parseMxcUrl(url);
if (parts) {
const [serverName, mediaId] = parts;
return `${this._homeserver}/_matrix/media/r0/download/${encodeURIComponent(serverName)}/${encodeURIComponent(mediaId)}`;
} else {
return null;
}
}
2019-03-08 16:56:59 +05:30
}
2020-04-23 00:17:31 +05:30
export function tests() {
function createRequestMock(result) {
return function() {
return {
abort() {},
response() {
return Promise.resolve(result);
}
}
}
}
return {
"superficial happy path for GET": async assert => {
const hsApi = new HomeServerApi({
request: createRequestMock({body: 42, status: 200}),
homeServer: "https://hs.tld"
});
const result = await hsApi._get("foo", null, null, null).response();
assert.strictEqual(result, 42);
}
}
}