1 | /*
|
2 | Copyright 2015, 2016 OpenMarket Ltd
|
3 | Copyright 2017 Vector Creations Ltd
|
4 | Copyright 2019 The Matrix.org Foundation C.I.C.
|
5 |
|
6 | Licensed under the Apache License, Version 2.0 (the "License");
|
7 | you may not use this file except in compliance with the License.
|
8 | You may obtain a copy of the License at
|
9 |
|
10 | http://www.apache.org/licenses/LICENSE-2.0
|
11 |
|
12 | Unless required by applicable law or agreed to in writing, software
|
13 | distributed under the License is distributed on an "AS IS" BASIS,
|
14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15 | See the License for the specific language governing permissions and
|
16 | limitations under the License.
|
17 | */
|
18 |
|
19 | import {MemoryCryptoStore} from "./crypto/store/memory-crypto-store";
|
20 | import {MemoryStore} from "./store/memory";
|
21 | import {MatrixScheduler} from "./scheduler";
|
22 | import {MatrixClient} from "./client";
|
23 |
|
24 | export * from "./client";
|
25 | export * from "./http-api";
|
26 | export * from "./autodiscovery";
|
27 | export * from "./sync-accumulator";
|
28 | export * from "./errors";
|
29 | export * from "./models/event";
|
30 | export * from "./models/room";
|
31 | export * from "./models/group";
|
32 | export * from "./models/event-timeline";
|
33 | export * from "./models/event-timeline-set";
|
34 | export * from "./models/room-member";
|
35 | export * from "./models/room-state";
|
36 | export * from "./models/user";
|
37 | export * from "./scheduler";
|
38 | export * from "./filter";
|
39 | export * from "./timeline-window";
|
40 | export * from "./interactive-auth";
|
41 | export * from "./service-types";
|
42 | export * from "./store/memory";
|
43 | export * from "./store/indexeddb";
|
44 | export * from "./store/session/webstorage";
|
45 | export * from "./crypto/store/memory-crypto-store";
|
46 | export * from "./crypto/store/indexeddb-crypto-store";
|
47 | export * from "./content-repo";
|
48 | export const ContentHelpers = import("./content-helpers");
|
49 | export {
|
50 | createNewMatrixCall,
|
51 | setAudioOutput as setMatrixCallAudioOutput,
|
52 | setAudioInput as setMatrixCallAudioInput,
|
53 | setVideoInput as setMatrixCallVideoInput,
|
54 | } from "./webrtc/call";
|
55 |
|
56 |
|
57 | // expose the underlying request object so different environments can use
|
58 | // different request libs (e.g. request or browser-request)
|
59 | let requestInstance;
|
60 |
|
61 | /**
|
62 | * The function used to perform HTTP requests. Only use this if you want to
|
63 | * use a different HTTP library, e.g. Angular's <code>$http</code>. This should
|
64 | * be set prior to calling {@link createClient}.
|
65 | * @param {requestFunction} r The request function to use.
|
66 | */
|
67 | export function request(r) {
|
68 | requestInstance = r;
|
69 | }
|
70 |
|
71 | /**
|
72 | * Return the currently-set request function.
|
73 | * @return {requestFunction} The current request function.
|
74 | */
|
75 | export function getRequest() {
|
76 | return requestInstance;
|
77 | }
|
78 |
|
79 | /**
|
80 | * Apply wrapping code around the request function. The wrapper function is
|
81 | * installed as the new request handler, and when invoked it is passed the
|
82 | * previous value, along with the options and callback arguments.
|
83 | * @param {requestWrapperFunction} wrapper The wrapping function.
|
84 | */
|
85 | export function wrapRequest(wrapper) {
|
86 | const origRequest = requestInstance;
|
87 | requestInstance = function(options, callback) {
|
88 | return wrapper(origRequest, options, callback);
|
89 | };
|
90 | }
|
91 |
|
92 |
|
93 | let cryptoStoreFactory = () => new MemoryCryptoStore;
|
94 |
|
95 | /**
|
96 | * Configure a different factory to be used for creating crypto stores
|
97 | *
|
98 | * @param {Function} fac a function which will return a new
|
99 | * {@link module:crypto.store.base~CryptoStore}.
|
100 | */
|
101 | export function setCryptoStoreFactory(fac) {
|
102 | cryptoStoreFactory = fac;
|
103 | }
|
104 |
|
105 | /**
|
106 | * Construct a Matrix Client. Similar to {@link module:client.MatrixClient}
|
107 | * except that the 'request', 'store' and 'scheduler' dependencies are satisfied.
|
108 | * @param {(Object|string)} opts The configuration options for this client. If
|
109 | * this is a string, it is assumed to be the base URL. These configuration
|
110 | * options will be passed directly to {@link module:client.MatrixClient}.
|
111 | * @param {Object} opts.store If not set, defaults to
|
112 | * {@link module:store/memory.MemoryStore}.
|
113 | * @param {Object} opts.scheduler If not set, defaults to
|
114 | * {@link module:scheduler~MatrixScheduler}.
|
115 | * @param {requestFunction} opts.request If not set, defaults to the function
|
116 | * supplied to {@link request} which defaults to the request module from NPM.
|
117 | *
|
118 | * @param {module:crypto.store.base~CryptoStore=} opts.cryptoStore
|
119 | * crypto store implementation. Calls the factory supplied to
|
120 | * {@link setCryptoStoreFactory} if unspecified; or if no factory has been
|
121 | * specified, uses a default implementation (indexeddb in the browser,
|
122 | * in-memory otherwise).
|
123 | *
|
124 | * @return {MatrixClient} A new matrix client.
|
125 | * @see {@link module:client.MatrixClient} for the full list of options for
|
126 | * <code>opts</code>.
|
127 | */
|
128 | export function createClient(opts) {
|
129 | if (typeof opts === "string") {
|
130 | opts = {
|
131 | "baseUrl": opts,
|
132 | };
|
133 | }
|
134 | opts.request = opts.request || requestInstance;
|
135 | opts.store = opts.store || new MemoryStore({
|
136 | localStorage: global.localStorage,
|
137 | });
|
138 | opts.scheduler = opts.scheduler || new MatrixScheduler();
|
139 | opts.cryptoStore = opts.cryptoStore || cryptoStoreFactory();
|
140 | return new MatrixClient(opts);
|
141 | }
|
142 |
|
143 | /**
|
144 | * The request function interface for performing HTTP requests. This matches the
|
145 | * API for the {@link https://github.com/request/request#requestoptions-callback|
|
146 | * request NPM module}. The SDK will attempt to call this function in order to
|
147 | * perform an HTTP request.
|
148 | * @callback requestFunction
|
149 | * @param {Object} opts The options for this HTTP request.
|
150 | * @param {string} opts.uri The complete URI.
|
151 | * @param {string} opts.method The HTTP method.
|
152 | * @param {Object} opts.qs The query parameters to append to the URI.
|
153 | * @param {Object} opts.body The JSON-serializable object.
|
154 | * @param {boolean} opts.json True if this is a JSON request.
|
155 | * @param {Object} opts._matrix_opts The underlying options set for
|
156 | * {@link MatrixHttpApi}.
|
157 | * @param {requestCallback} callback The request callback.
|
158 | */
|
159 |
|
160 | /**
|
161 | * A wrapper for the request function interface.
|
162 | * @callback requestWrapperFunction
|
163 | * @param {requestFunction} origRequest The underlying request function being
|
164 | * wrapped
|
165 | * @param {Object} opts The options for this HTTP request, given in the same
|
166 | * form as {@link requestFunction}.
|
167 | * @param {requestCallback} callback The request callback.
|
168 | */
|
169 |
|
170 | /**
|
171 | * The request callback interface for performing HTTP requests. This matches the
|
172 | * API for the {@link https://github.com/request/request#requestoptions-callback|
|
173 | * request NPM module}. The SDK will implement a callback which meets this
|
174 | * interface in order to handle the HTTP response.
|
175 | * @callback requestCallback
|
176 | * @param {Error} err The error if one occurred, else falsey.
|
177 | * @param {Object} response The HTTP response which consists of
|
178 | * <code>{statusCode: {Number}, headers: {Object}}</code>
|
179 | * @param {Object} body The parsed HTTP response body.
|
180 | */
|