summaryrefslogtreecommitdiffstats
path: root/comm/chat/protocols/matrix/lib/matrix-sdk/rendezvous/transports/MSC3886SimpleHttpRendezvousTransport.js
blob: 6347229acae76e47b3969875bb93bcf98ad01191 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.MSC3886SimpleHttpRendezvousTransport = void 0;
var _matrixEventsSdk = require("matrix-events-sdk");
var _logger = require("../../logger");
var _utils = require("../../utils");
var _ = require("..");
var _httpApi = require("../../http-api");
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } /*
                                                                                                                                                                                                                                                                                                                                                                                          Copyright 2022 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.
                                                                                                                                                                                                                                                                                                                                                                                          */
const TYPE = new _matrixEventsSdk.UnstableValue("http.v1", "org.matrix.msc3886.http.v1");
/**
 * Implementation of the unstable [MSC3886](https://github.com/matrix-org/matrix-spec-proposals/pull/3886)
 * simple HTTP rendezvous protocol.
 * Note that this is UNSTABLE and may have breaking changes without notice.
 */
class MSC3886SimpleHttpRendezvousTransport {
  constructor({
    onFailure,
    client,
    fallbackRzServer,
    fetchFn
  }) {
    _defineProperty(this, "uri", void 0);
    _defineProperty(this, "etag", void 0);
    _defineProperty(this, "expiresAt", void 0);
    _defineProperty(this, "client", void 0);
    _defineProperty(this, "fallbackRzServer", void 0);
    _defineProperty(this, "fetchFn", void 0);
    _defineProperty(this, "cancelled", false);
    _defineProperty(this, "_ready", false);
    _defineProperty(this, "onFailure", void 0);
    this.fetchFn = fetchFn;
    this.onFailure = onFailure;
    this.client = client;
    this.fallbackRzServer = fallbackRzServer;
  }
  get ready() {
    return this._ready;
  }
  async details() {
    if (!this.uri) {
      throw new Error("Rendezvous not set up");
    }
    return {
      type: TYPE.name,
      uri: this.uri
    };
  }
  fetch(resource, options) {
    if (this.fetchFn) {
      return this.fetchFn(resource, options);
    }
    return global.fetch(resource, options);
  }
  async getPostEndpoint() {
    try {
      if (await this.client.doesServerSupportUnstableFeature("org.matrix.msc3886")) {
        return `${this.client.baseUrl}${_httpApi.ClientPrefix.Unstable}/org.matrix.msc3886/rendezvous`;
      }
    } catch (err) {
      _logger.logger.warn("Failed to get unstable features", err);
    }
    return this.fallbackRzServer;
  }
  async send(data) {
    if (this.cancelled) {
      return;
    }
    const method = this.uri ? "PUT" : "POST";
    const uri = this.uri ?? (await this.getPostEndpoint());
    if (!uri) {
      throw new Error("Invalid rendezvous URI");
    }
    const headers = {
      "content-type": "application/json"
    };
    if (this.etag) {
      headers["if-match"] = this.etag;
    }
    const res = await this.fetch(uri, {
      method,
      headers,
      body: JSON.stringify(data)
    });
    if (res.status === 404) {
      return this.cancel(_.RendezvousFailureReason.Unknown);
    }
    this.etag = res.headers.get("etag") ?? undefined;
    if (method === "POST") {
      const location = res.headers.get("location");
      if (!location) {
        throw new Error("No rendezvous URI given");
      }
      const expires = res.headers.get("expires");
      if (expires) {
        this.expiresAt = new Date(expires);
      }
      // we would usually expect the final `url` to be set by a proper fetch implementation.
      // however, if a polyfill based on XHR is used it won't be set, we we use existing URI as fallback
      const baseUrl = res.url ?? uri;
      // resolve location header which could be relative or absolute
      this.uri = new URL(location, `${baseUrl}${baseUrl.endsWith("/") ? "" : "/"}`).href;
      this._ready = true;
    }
  }
  async receive() {
    if (!this.uri) {
      throw new Error("Rendezvous not set up");
    }
    // eslint-disable-next-line no-constant-condition
    while (true) {
      if (this.cancelled) {
        return undefined;
      }
      const headers = {};
      if (this.etag) {
        headers["if-none-match"] = this.etag;
      }
      const poll = await this.fetch(this.uri, {
        method: "GET",
        headers
      });
      if (poll.status === 404) {
        this.cancel(_.RendezvousFailureReason.Unknown);
        return undefined;
      }

      // rely on server expiring the channel rather than checking ourselves

      if (poll.headers.get("content-type") !== "application/json") {
        this.etag = poll.headers.get("etag") ?? undefined;
      } else if (poll.status === 200) {
        this.etag = poll.headers.get("etag") ?? undefined;
        return poll.json();
      }
      await (0, _utils.sleep)(1000);
    }
  }
  async cancel(reason) {
    if (reason === _.RendezvousFailureReason.Unknown && this.expiresAt && this.expiresAt.getTime() < Date.now()) {
      reason = _.RendezvousFailureReason.Expired;
    }
    this.cancelled = true;
    this._ready = false;
    this.onFailure?.(reason);
    if (this.uri && reason === _.RendezvousFailureReason.UserDeclined) {
      try {
        await this.fetch(this.uri, {
          method: "DELETE"
        });
      } catch (e) {
        _logger.logger.warn(e);
      }
    }
  }
}
exports.MSC3886SimpleHttpRendezvousTransport = MSC3886SimpleHttpRendezvousTransport;