2019-02-10 21:40:11 +01:00
|
|
|
import {
|
2019-06-26 22:31:36 +02:00
|
|
|
HomeServerError,
|
2020-04-19 19:05:12 +02:00
|
|
|
ConnectionError,
|
2020-04-20 19:47:45 +02:00
|
|
|
} from "../error.js";
|
2019-02-10 21:25:29 +01:00
|
|
|
|
2019-02-04 23:26:24 +01:00
|
|
|
class RequestWrapper {
|
2019-12-23 14:28:27 +01:00
|
|
|
constructor(method, url, requestResult) {
|
|
|
|
this._requestResult = requestResult;
|
|
|
|
this._promise = this._requestResult.response().then(response => {
|
|
|
|
// ok?
|
|
|
|
if (response.status >= 200 && response.status < 300) {
|
|
|
|
return response.body;
|
|
|
|
} else {
|
|
|
|
switch (response.status) {
|
|
|
|
default:
|
2020-03-17 00:07:54 +01:00
|
|
|
throw new HomeServerError(method, url, response.body, response.status);
|
2019-12-23 14:28:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2019-06-26 22:31:36 +02:00
|
|
|
}
|
2018-12-21 14:35:24 +01:00
|
|
|
|
2019-06-26 22:31:36 +02:00
|
|
|
abort() {
|
2019-12-23 14:28:27 +01:00
|
|
|
return this._requestResult.abort();
|
2019-06-26 22:31:36 +02:00
|
|
|
}
|
2018-12-21 14:35:24 +01:00
|
|
|
|
2019-06-26 22:31:36 +02:00
|
|
|
response() {
|
|
|
|
return this._promise;
|
|
|
|
}
|
2018-12-21 14:35:24 +01:00
|
|
|
}
|
|
|
|
|
2020-04-20 21:26:39 +02:00
|
|
|
export class HomeServerApi {
|
2020-04-05 15:11:15 +02:00
|
|
|
constructor({homeServer, accessToken, request, createTimeout, reconnector}) {
|
2019-03-08 20:03:47 +01:00
|
|
|
// 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 14:28:27 +01:00
|
|
|
this._homeserver = homeServer;
|
2019-06-26 22:31:36 +02:00
|
|
|
this._accessToken = accessToken;
|
2019-12-23 14:28:27 +01:00
|
|
|
this._requestFn = request;
|
2020-04-05 15:11:15 +02:00
|
|
|
this._createTimeout = createTimeout;
|
|
|
|
this._reconnector = reconnector;
|
2019-06-26 22:31:36 +02:00
|
|
|
}
|
2018-12-21 14:35:24 +01:00
|
|
|
|
2019-06-26 22:31:36 +02:00
|
|
|
_url(csPath) {
|
|
|
|
return `${this._homeserver}/_matrix/client/r0${csPath}`;
|
|
|
|
}
|
2018-12-21 14:35:24 +01:00
|
|
|
|
2020-04-05 15:11:15 +02:00
|
|
|
_request(method, url, queryParams = {}, body, options) {
|
2019-06-26 22:31:36 +02:00
|
|
|
const queryString = Object.entries(queryParams)
|
|
|
|
.filter(([, value]) => value !== undefined)
|
2019-10-12 20:24:09 +02:00
|
|
|
.map(([name, value]) => {
|
|
|
|
if (typeof value === "object") {
|
|
|
|
value = JSON.stringify(value);
|
|
|
|
}
|
|
|
|
return `${encodeURIComponent(name)}=${encodeURIComponent(value)}`;
|
|
|
|
})
|
2019-06-26 22:31:36 +02:00
|
|
|
.join("&");
|
2020-03-30 23:56:03 +02:00
|
|
|
url = `${url}?${queryString}`;
|
2019-06-26 22:31:36 +02:00
|
|
|
let bodyString;
|
2020-04-22 20:46:47 +02:00
|
|
|
const headers = new Map();
|
2019-06-26 22:31:36 +02:00
|
|
|
if (this._accessToken) {
|
2020-04-22 20:46:47 +02:00
|
|
|
headers.set("Authorization", `Bearer ${this._accessToken}`);
|
2019-06-26 22:31:36 +02:00
|
|
|
}
|
2020-04-22 20:46:47 +02:00
|
|
|
headers.set("Accept", "application/json");
|
2019-06-26 22:31:36 +02:00
|
|
|
if (body) {
|
2020-04-22 20:46:47 +02:00
|
|
|
headers.set("Content-Type", "application/json");
|
2019-06-26 22:31:36 +02:00
|
|
|
bodyString = JSON.stringify(body);
|
|
|
|
}
|
2019-12-23 14:28:27 +01:00
|
|
|
const requestResult = this._requestFn(url, {
|
2019-06-26 22:31:36 +02:00
|
|
|
method,
|
|
|
|
headers,
|
|
|
|
body: bodyString,
|
|
|
|
});
|
2020-04-05 15:11:15 +02:00
|
|
|
|
2020-04-22 20:47:46 +02:00
|
|
|
if (options && options.timeout) {
|
2020-04-05 15:11:15 +02:00
|
|
|
const timeout = this._createTimeout(options.timeout);
|
|
|
|
// abort request if timeout finishes first
|
|
|
|
timeout.elapsed().then(
|
|
|
|
() => requestResult.abort(),
|
|
|
|
() => {} // ignore AbortError
|
|
|
|
);
|
|
|
|
// abort timeout if request finishes first
|
|
|
|
requestResult.response().then(() => timeout.abort());
|
|
|
|
}
|
|
|
|
|
|
|
|
const wrapper = new RequestWrapper(method, url, requestResult);
|
|
|
|
|
|
|
|
if (this._reconnector) {
|
|
|
|
wrapper.response().catch(err => {
|
2020-04-19 19:05:12 +02:00
|
|
|
if (err instanceof ConnectionError) {
|
2020-04-05 15:11:15 +02:00
|
|
|
this._reconnector.onRequestFailed(this);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return wrapper;
|
2019-06-26 22:31:36 +02:00
|
|
|
}
|
2019-02-04 23:26:24 +01:00
|
|
|
|
2020-04-05 15:11:15 +02:00
|
|
|
_post(csPath, queryParams, body, options) {
|
|
|
|
return this._request("POST", this._url(csPath), queryParams, body, options);
|
2019-06-26 22:31:36 +02:00
|
|
|
}
|
2019-02-04 23:26:24 +01:00
|
|
|
|
2020-04-05 15:11:15 +02:00
|
|
|
_put(csPath, queryParams, body, options) {
|
|
|
|
return this._request("PUT", this._url(csPath), queryParams, body, options);
|
2019-07-26 22:03:57 +02:00
|
|
|
}
|
|
|
|
|
2020-04-05 15:11:15 +02:00
|
|
|
_get(csPath, queryParams, body, options) {
|
|
|
|
return this._request("GET", this._url(csPath), queryParams, body, options);
|
2019-06-26 22:31:36 +02:00
|
|
|
}
|
2018-12-21 14:35:24 +01:00
|
|
|
|
2020-04-05 15:11:15 +02:00
|
|
|
sync(since, filter, timeout, options = null) {
|
|
|
|
return this._get("/sync", {since, timeout, filter}, null, options);
|
2019-06-26 22:31:36 +02:00
|
|
|
}
|
2019-02-04 23:26:24 +01:00
|
|
|
|
2019-03-09 00:41:06 +01:00
|
|
|
// params is from, dir and optionally to, limit, filter.
|
2020-04-05 15:11:15 +02:00
|
|
|
messages(roomId, params, options = null) {
|
|
|
|
return this._get(`/rooms/${encodeURIComponent(roomId)}/messages`, params, null, options);
|
2019-03-09 00:41:06 +01:00
|
|
|
}
|
|
|
|
|
2020-04-05 15:11:15 +02:00
|
|
|
send(roomId, eventType, txnId, content, options = null) {
|
|
|
|
return this._put(`/rooms/${encodeURIComponent(roomId)}/send/${encodeURIComponent(eventType)}/${encodeURIComponent(txnId)}`, {}, content, options);
|
2019-07-26 22:03:57 +02:00
|
|
|
}
|
|
|
|
|
2020-04-05 15:11:15 +02:00
|
|
|
passwordLogin(username, password, options = null) {
|
|
|
|
return this._post("/login", null, {
|
2019-02-04 23:26:24 +01:00
|
|
|
"type": "m.login.password",
|
|
|
|
"identifier": {
|
|
|
|
"type": "m.id.user",
|
|
|
|
"user": username
|
|
|
|
},
|
|
|
|
"password": password
|
2020-04-05 15:11:15 +02:00
|
|
|
}, options);
|
2019-06-26 22:31:36 +02:00
|
|
|
}
|
2019-10-12 20:24:09 +02:00
|
|
|
|
2020-04-05 15:11:15 +02:00
|
|
|
createFilter(userId, filter, options = null) {
|
|
|
|
return this._post(`/user/${encodeURIComponent(userId)}/filter`, null, filter, options);
|
2019-10-12 20:24:09 +02:00
|
|
|
}
|
2020-03-30 23:56:03 +02:00
|
|
|
|
2020-04-05 15:11:15 +02:00
|
|
|
versions(options = null) {
|
|
|
|
return this._request("GET", `${this._homeserver}/_matrix/client/versions`, null, options);
|
2020-03-30 23:56:03 +02:00
|
|
|
}
|
2019-03-08 12:26:59 +01:00
|
|
|
}
|
2020-04-22 20:47:31 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|