summaryrefslogtreecommitdiffstats
path: root/comm/chat/protocols/matrix/matrix-sdk.sys.mjs
blob: fd691dbc1be64f573794407cf195f7d21b808d99 (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import { console } from "resource://gre/modules/Console.sys.mjs";
import {
  clearInterval,
  clearTimeout,
  setInterval,
  setTimeout,
} from "resource://gre/modules/Timer.sys.mjs";
import { scriptError } from "resource:///modules/imXPCOMUtils.sys.mjs";
import {
  Loader,
  Require,
} from "resource://devtools/shared/loader/base-loader.sys.mjs";

/**
 * Set of packages that have a top level index.js. This makes it so we don't
 * even try to require them as a js file directly and just fall through to the
 * index.js logic. These are paths without matrixPath in front.
 *
 * @type {Set<string>}
 */
const KNOWN_INDEX_JS = new Set([
  "matrix_events_sdk",
  "p_retry",
  "retry",
  "sdp_transform",
  "unhomoglyph",
  "matrix_sdk/crypto",
  "matrix_sdk/crypto/algorithms",
  "matrix_sdk/http_api",
  "matrix_sdk/rendezvous",
  "matrix_sdk/rendezvous/channels",
  "matrix_sdk/rendezvous/transports",
  "matrix_widget_api",
]);

// Set-up loading so require works properly in CommonJS modules.

let matrixPath = "resource:///modules/matrix/";

let globals = {
  atob,
  btoa,
  crypto,
  console,
  fetch,
  setTimeout,
  clearTimeout,
  setInterval,
  clearInterval,
  TextEncoder,
  TextDecoder,
  URL,
  URLSearchParams,
  IDBKeyRange,
  get window() {
    return globals;
  },

  // Necessary for interacting with the logging framework.
  scriptError,
  imIDebugMessage: Ci.imIDebugMessage,
};
let loaderGlobal = {
  get window() {
    return globals;
  },
  get global() {
    return globals;
  },
  ...globals,
};
let loader = Loader({
  paths: {
    // Matrix SDK files.
    "matrix-sdk": matrixPath + "matrix_sdk",
    "matrix-sdk/@types": matrixPath + "matrix_sdk/types",
    "matrix-sdk/@types/requests": matrixPath + "empty.js",
    // The entire directory can't be mapped from crypto-api to crypto_api since
    // there's also a matrix-sdk/crypto-api.js.
    "matrix-sdk/crypto-api/verification":
      matrixPath + "matrix_sdk/crypto_api/verification.js",
    "matrix-sdk/http-api": matrixPath + "matrix_sdk/http_api",
    "matrix-sdk/rust-crypto": matrixPath + "matrix_sdk/rust_crypto",

    // Simple (one-file) dependencies.
    "another-json": matrixPath + "another-json.js",
    "base-x": matrixPath + "base_x/index.js",
    bs58: matrixPath + "bs58/index.js",
    "content-type": matrixPath + "content_type/index.js",

    // unhomoglyph
    unhomoglyph: matrixPath + "unhomoglyph",

    // p-retry
    "p-retry": matrixPath + "p_retry",
    retry: matrixPath + "retry",

    // matrix-events-sdk
    "matrix-events-sdk": matrixPath + "matrix_events_sdk",
    "matrix-events-sdk/IPartialEvent": matrixPath + "empty.js",

    // matrix-widget-api
    "matrix-widget-api": matrixPath + "matrix_widget_api",
    "matrix-widget-api/interfaces/CapabilitiesAction": matrixPath + "empty.js",
    "matrix-widget-api/interfaces/ContentLoadedAction": matrixPath + "empty.js",
    "matrix-widget-api/interfaces/ICustomWidgetData": matrixPath + "empty.js",
    "matrix-widget-api/interfaces/IJitsiWidgetData": matrixPath + "empty.js",
    "matrix-widget-api/interfaces/IRoomEvent": matrixPath + "empty.js",
    "matrix-widget-api/interfaces/IStickerpickerWidgetData":
      matrixPath + "empty.js",
    "matrix-widget-api/interfaces/IWidget": matrixPath + "empty.js",
    "matrix-widget-api/interfaces/IWidgetApiRequest": matrixPath + "empty.js",
    "matrix-widget-api/interfaces/IWidgetApiResponse": matrixPath + "empty.js",
    "matrix-widget-api/interfaces/NavigateAction": matrixPath + "empty.js",
    "matrix-widget-api/interfaces/OpenIDCredentialsAction":
      matrixPath + "empty.js",
    "matrix-widget-api/interfaces/ReadEventAction": matrixPath + "empty.js",
    "matrix-widget-api/interfaces/ReadRelationsAction": matrixPath + "empty.js",
    "matrix-widget-api/interfaces/ScreenshotAction": matrixPath + "empty.js",
    "matrix-widget-api/interfaces/SetModalButtonEnabledAction":
      matrixPath + "empty.js",
    "matrix-widget-api/interfaces/SendAction": matrixPath + "empty.js",
    "matrix-widget-api/interfaces/SendEventAction": matrixPath + "empty.js",
    "matrix-widget-api/interfaces/SendToDeviceAction": matrixPath + "empty.js",
    "matrix-widget-api/interfaces/StickerAction": matrixPath + "empty.js",
    "matrix-widget-api/interfaces/StickyAction": matrixPath + "empty.js",
    "matrix-widget-api/interfaces/SupportedVersionsAction":
      matrixPath + "empty.js",
    "matrix-widget-api/interfaces/TurnServerActions": matrixPath + "empty.js",
    "matrix-widget-api/interfaces/VisibilityAction": matrixPath + "empty.js",
    "matrix-widget-api/interfaces/WidgetAction": matrixPath + "empty.js",
    "matrix-widget-api/interfaces/WidgetConfigAction": matrixPath + "empty.js",
    "matrix-widget-api/transport/ITransport": matrixPath + "empty.js",

    // sdp-transform
    "sdp-transform": matrixPath + "sdp_transform",

    // Packages that are not included, but an alternate implementation is given.
    events: matrixPath + "events.js",
    loglevel: matrixPath + "loglevel.js",
    "safe-buffer": matrixPath + "safe-buffer.js",
    uuid: matrixPath + "uuid.js",
  },
  globals: loaderGlobal,
  sandboxName: "Matrix SDK",
  // Custom require hook to support loading */index.js without explicitly
  // including it in the require path.
  requireHook: (id, require) => {
    try {
      // Get resolved path without matrixPath prefix and .js extension.
      const resolved = require.resolve(id).slice(matrixPath.length, -3);
      if (KNOWN_INDEX_JS.has(resolved)) {
        throw new Error("Must require index.js for module " + id);
      }
      return require(id);
    } catch (error) {
      // Make sure we only try to look for index.js on the initial failure and
      // not in requires earlier in the tree.
      if (!error.rethrown && !id.endsWith("/index.js")) {
        try {
          return require(id + "/index.js");
        } catch (indexError) {
          indexError.rethrown = true;
          throw indexError;
        }
      }
      error.rethrown = true;
      throw error;
    }
  },
});

// Load olm library in a browser-like environment. This allows it to load its
// wasm module, do crypto operations and log errors.
// Create the global in the commonJS loader context, so they share the same
// Uint8Array constructor.
let olmScope = Cu.createObjectIn(loader.sharedGlobal);
Object.assign(olmScope, {
  crypto,
  fetch,
  XMLHttpRequest,
  console,
  location: {
    href: matrixPath + "olm",
  },
  document: {
    currentScript: {
      src: matrixPath + "olm/olm.js",
    },
  },
});
Object.defineProperty(olmScope, "window", {
  get() {
    return olmScope;
  },
});
Services.scriptloader.loadSubScript(matrixPath + "olm/olm.js", olmScope);
olmScope.Olm.init().catch(console.error);
loader.globals.Olm = olmScope.Olm;
globals.Olm = olmScope.Olm;

let require = Require(loader, { id: "matrix-module" });

// Load the buffer shim into the global commonJS scope
loader.globals.Buffer = require("safe-buffer").Buffer;

globals.Buffer = loader.globals.Buffer;

// The main entry point into the Matrix client.
export let MatrixSDK = require("matrix-sdk/browser-index.js");

// Helper enums not exposed on MatrixSDK.
export let MatrixCrypto = require("matrix-sdk/crypto");
export let { SyncState } = require("matrix-sdk/sync");
export let OlmLib = require("matrix-sdk/crypto/olmlib");
export let { ReceiptType } = require("matrix-sdk/@types/read_receipts");