UNPKG

10.4 kBJavaScriptView Raw
1/*
2Copyright 2015, 2016 OpenMarket Ltd
3Copyright 2017 Vector Creations Ltd
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16*/
17"use strict";
18
19/** The {@link module:ContentHelpers} object */
20module.exports.ContentHelpers = require("./content-helpers");
21/** The {@link module:models/event.MatrixEvent|MatrixEvent} class. */
22module.exports.MatrixEvent = require("./models/event").MatrixEvent;
23/** The {@link module:models/event.EventStatus|EventStatus} enum. */
24module.exports.EventStatus = require("./models/event").EventStatus;
25/** The {@link module:store/memory.MatrixInMemoryStore|MatrixInMemoryStore} class. */
26module.exports.MatrixInMemoryStore = require("./store/memory").MatrixInMemoryStore;
27/** The {@link module:store/indexeddb.IndexedDBStore|IndexedDBStore} class. */
28module.exports.IndexedDBStore = require("./store/indexeddb").IndexedDBStore;
29/** The {@link module:store/indexeddb.IndexedDBStoreBackend|IndexedDBStoreBackend} class. */
30module.exports.IndexedDBStoreBackend = require("./store/indexeddb").IndexedDBStoreBackend;
31/** The {@link module:sync-accumulator.SyncAccumulator|SyncAccumulator} class. */
32module.exports.SyncAccumulator = require("./sync-accumulator");
33/** The {@link module:http-api.MatrixHttpApi|MatrixHttpApi} class. */
34module.exports.MatrixHttpApi = require("./http-api").MatrixHttpApi;
35/** The {@link module:http-api.MatrixError|MatrixError} class. */
36module.exports.MatrixError = require("./http-api").MatrixError;
37/** The {@link module:errors.InvalidStoreError|InvalidStoreError} class. */
38module.exports.InvalidStoreError = require("./errors").InvalidStoreError;
39/** The {@link module:client.MatrixClient|MatrixClient} class. */
40module.exports.MatrixClient = require("./client").MatrixClient;
41/** The {@link module:models/room|Room} class. */
42module.exports.Room = require("./models/room");
43/** The {@link module:models/group|Group} class. */
44module.exports.Group = require("./models/group");
45/** The {@link module:models/event-timeline~EventTimeline} class. */
46module.exports.EventTimeline = require("./models/event-timeline");
47/** The {@link module:models/event-timeline-set~EventTimelineSet} class. */
48module.exports.EventTimelineSet = require("./models/event-timeline-set");
49/** The {@link module:models/room-member|RoomMember} class. */
50module.exports.RoomMember = require("./models/room-member");
51/** The {@link module:models/room-state~RoomState|RoomState} class. */
52module.exports.RoomState = require("./models/room-state");
53/** The {@link module:models/user~User|User} class. */
54module.exports.User = require("./models/user");
55/** The {@link module:scheduler~MatrixScheduler|MatrixScheduler} class. */
56module.exports.MatrixScheduler = require("./scheduler");
57/** The {@link module:store/session/webstorage~WebStorageSessionStore|
58 * WebStorageSessionStore} class. <strong>Work in progress; unstable.</strong> */
59module.exports.WebStorageSessionStore = require("./store/session/webstorage");
60/** True if crypto libraries are being used on this client. */
61module.exports.CRYPTO_ENABLED = require("./client").CRYPTO_ENABLED;
62/** {@link module:content-repo|ContentRepo} utility functions. */
63module.exports.ContentRepo = require("./content-repo");
64/** The {@link module:filter~Filter|Filter} class. */
65module.exports.Filter = require("./filter");
66/** The {@link module:timeline-window~TimelineWindow} class. */
67module.exports.TimelineWindow = require("./timeline-window").TimelineWindow;
68/** The {@link module:interactive-auth} class. */
69module.exports.InteractiveAuth = require("./interactive-auth");
70/** The {@link module:auto-discovery|AutoDiscovery} class. */
71module.exports.AutoDiscovery = require("./autodiscovery").AutoDiscovery;
72
73
74module.exports.MemoryCryptoStore =
75 require("./crypto/store/memory-crypto-store").default;
76module.exports.IndexedDBCryptoStore =
77 require("./crypto/store/indexeddb-crypto-store").default;
78
79/**
80 * Create a new Matrix Call.
81 * @function
82 * @param {module:client.MatrixClient} client The MatrixClient instance to use.
83 * @param {string} roomId The room the call is in.
84 * @return {module:webrtc/call~MatrixCall} The Matrix call or null if the browser
85 * does not support WebRTC.
86 */
87module.exports.createNewMatrixCall = require("./webrtc/call").createNewMatrixCall;
88
89
90/**
91 * Set an audio output device to use for MatrixCalls
92 * @function
93 * @param {string=} deviceId the identifier for the device
94 * undefined treated as unset
95 */
96module.exports.setMatrixCallAudioOutput = require('./webrtc/call').setAudioOutput;
97/**
98 * Set an audio input device to use for MatrixCalls
99 * @function
100 * @param {string=} deviceId the identifier for the device
101 * undefined treated as unset
102 */
103module.exports.setMatrixCallAudioInput = require('./webrtc/call').setAudioInput;
104/**
105 * Set a video input device to use for MatrixCalls
106 * @function
107 * @param {string=} deviceId the identifier for the device
108 * undefined treated as unset
109 */
110module.exports.setMatrixCallVideoInput = require('./webrtc/call').setVideoInput;
111
112
113// expose the underlying request object so different environments can use
114// different request libs (e.g. request or browser-request)
115let request;
116/**
117 * The function used to perform HTTP requests. Only use this if you want to
118 * use a different HTTP library, e.g. Angular's <code>$http</code>. This should
119 * be set prior to calling {@link createClient}.
120 * @param {requestFunction} r The request function to use.
121 */
122module.exports.request = function(r) {
123 request = r;
124};
125
126/**
127 * Return the currently-set request function.
128 * @return {requestFunction} The current request function.
129 */
130module.exports.getRequest = function() {
131 return request;
132};
133
134/**
135 * Apply wrapping code around the request function. The wrapper function is
136 * installed as the new request handler, and when invoked it is passed the
137 * previous value, along with the options and callback arguments.
138 * @param {requestWrapperFunction} wrapper The wrapping function.
139 */
140module.exports.wrapRequest = function(wrapper) {
141 const origRequest = request;
142 request = function(options, callback) {
143 return wrapper(origRequest, options, callback);
144 };
145};
146
147
148let cryptoStoreFactory = () => new module.exports.MemoryCryptoStore;
149
150/**
151 * Configure a different factory to be used for creating crypto stores
152 *
153 * @param {Function} fac a function which will return a new
154 * {@link module:crypto.store.base~CryptoStore}.
155 */
156module.exports.setCryptoStoreFactory = function(fac) {
157 cryptoStoreFactory = fac;
158};
159
160/**
161 * Construct a Matrix Client. Similar to {@link module:client~MatrixClient}
162 * except that the 'request', 'store' and 'scheduler' dependencies are satisfied.
163 * @param {(Object|string)} opts The configuration options for this client. If
164 * this is a string, it is assumed to be the base URL. These configuration
165 * options will be passed directly to {@link module:client~MatrixClient}.
166 * @param {Object} opts.store If not set, defaults to
167 * {@link module:store/memory.MatrixInMemoryStore}.
168 * @param {Object} opts.scheduler If not set, defaults to
169 * {@link module:scheduler~MatrixScheduler}.
170 * @param {requestFunction} opts.request If not set, defaults to the function
171 * supplied to {@link request} which defaults to the request module from NPM.
172 *
173 * @param {module:crypto.store.base~CryptoStore=} opts.cryptoStore
174 * crypto store implementation. Calls the factory supplied to
175 * {@link setCryptoStoreFactory} if unspecified; or if no factory has been
176 * specified, uses a default implementation (indexeddb in the browser,
177 * in-memory otherwise).
178 *
179 * @return {MatrixClient} A new matrix client.
180 * @see {@link module:client~MatrixClient} for the full list of options for
181 * <code>opts</code>.
182 */
183module.exports.createClient = function(opts) {
184 if (typeof opts === "string") {
185 opts = {
186 "baseUrl": opts,
187 };
188 }
189 opts.request = opts.request || request;
190 opts.store = opts.store || new module.exports.MatrixInMemoryStore({
191 localStorage: global.localStorage,
192 });
193 opts.scheduler = opts.scheduler || new module.exports.MatrixScheduler();
194 opts.cryptoStore = opts.cryptoStore || cryptoStoreFactory();
195 return new module.exports.MatrixClient(opts);
196};
197
198/**
199 * The request function interface for performing HTTP requests. This matches the
200 * API for the {@link https://github.com/request/request#requestoptions-callback|
201 * request NPM module}. The SDK will attempt to call this function in order to
202 * perform an HTTP request.
203 * @callback requestFunction
204 * @param {Object} opts The options for this HTTP request.
205 * @param {string} opts.uri The complete URI.
206 * @param {string} opts.method The HTTP method.
207 * @param {Object} opts.qs The query parameters to append to the URI.
208 * @param {Object} opts.body The JSON-serializable object.
209 * @param {boolean} opts.json True if this is a JSON request.
210 * @param {Object} opts._matrix_opts The underlying options set for
211 * {@link MatrixHttpApi}.
212 * @param {requestCallback} callback The request callback.
213 */
214
215/**
216 * A wrapper for the request function interface.
217 * @callback requestWrapperFunction
218 * @param {requestFunction} origRequest The underlying request function being
219 * wrapped
220 * @param {Object} opts The options for this HTTP request, given in the same
221 * form as {@link requestFunction}.
222 * @param {requestCallback} callback The request callback.
223 */
224
225 /**
226 * The request callback interface for performing HTTP requests. This matches the
227 * API for the {@link https://github.com/request/request#requestoptions-callback|
228 * request NPM module}. The SDK will implement a callback which meets this
229 * interface in order to handle the HTTP response.
230 * @callback requestCallback
231 * @param {Error} err The error if one occurred, else falsey.
232 * @param {Object} response The HTTP response which consists of
233 * <code>{statusCode: {Number}, headers: {Object}}</code>
234 * @param {Object} body The parsed HTTP response body.
235 */