@textrp/briij-js-sdk 41.0.0 → 41.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/README.md +34 -44
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -8,32 +8,22 @@
|
|
|
8
8
|
|
|
9
9
|
# Briij JavaScript SDK
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
---
|
|
15
|
-
|
|
16
|
-
<picture>
|
|
17
|
-
<source srcset="contrib/element-logo-light.png" media="(prefers-color-scheme: dark)">
|
|
18
|
-
<source srcset="contrib/element-logo-dark.png" media="(prefers-color-scheme: light)">
|
|
19
|
-
<img src="contrib/element-logo-fallback.png" alt="Element logo">
|
|
20
|
-
</picture>
|
|
21
|
-
|
|
22
|
-
<br>
|
|
23
|
-
|
|
24
|
-
Development and maintenance is proudly sponsored by [Element](https://element.io). Element uses the SDK in their flagship [web](https://github.com/element-hq/element-web) and [desktop](https://github.com/element-hq/element-desktop) clients.
|
|
11
|
+
<p align="center">
|
|
12
|
+
<img src="contrib/textrp-logo.png" alt="TextRP logo" width="280">
|
|
13
|
+
</p>
|
|
25
14
|
|
|
26
|
-
|
|
15
|
+
This is the Briij Client-Server SDK for JavaScript and TypeScript. This SDK can be run in a
|
|
16
|
+
browser or in Node.js.
|
|
27
17
|
|
|
28
18
|
---
|
|
29
19
|
|
|
30
|
-
#### Minimum
|
|
20
|
+
#### Minimum server version: v1.1
|
|
31
21
|
|
|
32
|
-
The
|
|
22
|
+
The client-server specification is constantly evolving - while this SDK aims for maximum backwards compatibility, it only
|
|
33
23
|
guarantees that a feature will be supported for at least 4 spec releases. For example, if a feature the js-sdk supports
|
|
34
24
|
is removed in v1.4 then the feature is _eligible_ for removal from the SDK when v1.8 is released. This SDK has no
|
|
35
25
|
guarantee on implementing all features of any particular spec release, currently. This can mean that the SDK will call
|
|
36
|
-
endpoints from before
|
|
26
|
+
endpoints from before v1.1, for example.
|
|
37
27
|
|
|
38
28
|
# Quickstart
|
|
39
29
|
|
|
@@ -48,7 +38,7 @@ guide](https://pnpm.io/installation#using-corepack) if you do not have it alread
|
|
|
48
38
|
|
|
49
39
|
```javascript
|
|
50
40
|
import * as sdk from "@textrp/briij-js-sdk";
|
|
51
|
-
const client = sdk.createClient({ baseUrl: "https://
|
|
41
|
+
const client = sdk.createClient({ baseUrl: "https://your-server.example.org" });
|
|
52
42
|
client.publicRooms(function (err, data) {
|
|
53
43
|
console.log("Public Rooms: %s", JSON.stringify(data));
|
|
54
44
|
});
|
|
@@ -111,7 +101,7 @@ Object.keys(client.store.rooms).forEach((roomId) => {
|
|
|
111
101
|
|
|
112
102
|
## Authenticated media
|
|
113
103
|
|
|
114
|
-
Servers supporting [MSC3916](https://github.com/matrix-org/matrix-spec-proposals/pull/3916) (
|
|
104
|
+
Servers supporting [MSC3916](https://github.com/matrix-org/matrix-spec-proposals/pull/3916) (spec 1.11) will require clients, like
|
|
115
105
|
yours, to include an `Authorization` header when `/download`ing or `/thumbnail`ing media. For NodeJS environments this
|
|
116
106
|
may be as easy as the following code snippet, though web browsers may need to use [Service Workers](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API)
|
|
117
107
|
to append the header when using the endpoints in `<img />` elements and similar.
|
|
@@ -139,7 +129,7 @@ const img = await fetch(downloadUrl, {
|
|
|
139
129
|
|
|
140
130
|
## What does this SDK do?
|
|
141
131
|
|
|
142
|
-
This SDK provides a full object model around the
|
|
132
|
+
This SDK provides a full object model around the client-server API and emits
|
|
143
133
|
events for incoming data and state changes. Aside from wrapping the HTTP API, it:
|
|
144
134
|
|
|
145
135
|
- Handles syncing (via `/sync`)
|
|
@@ -205,7 +195,7 @@ As well as the primary entry point (`@textrp/briij-js-sdk`), there are several o
|
|
|
205
195
|
| ------------------------------ | --------------------------------------------------------------------------------------------------- |
|
|
206
196
|
| `@textrp/briij-js-sdk` | Primary entry point. High-level functionality, and lots of historical clutter in need of a cleanup. |
|
|
207
197
|
| `@textrp/briij-js-sdk/lib/crypto-api` | Cryptography functionality. |
|
|
208
|
-
| `@textrp/briij-js-sdk/lib/types` | Low-level types, reflecting data structures defined in the
|
|
198
|
+
| `@textrp/briij-js-sdk/lib/types` | Low-level types, reflecting data structures defined in the client-server spec. |
|
|
209
199
|
| `@textrp/briij-js-sdk/lib/testing` | Test utilities, which may be useful in test code but should not be used in production code. |
|
|
210
200
|
| `@textrp/briij-js-sdk/lib/utils/*.js` | A set of modules exporting standalone functions (and their types). |
|
|
211
201
|
|
|
@@ -218,7 +208,7 @@ core functionality of the SDK. These examples assume the SDK is set up like this
|
|
|
218
208
|
import * as sdk from "@textrp/briij-js-sdk";
|
|
219
209
|
const myUserId = "@example:localhost";
|
|
220
210
|
const myAccessToken = "QGV4YW1wbGU6bG9jYWxob3N0.qPEvLuYfNBjxikiCjP";
|
|
221
|
-
const
|
|
211
|
+
const client = sdk.createClient({
|
|
222
212
|
baseUrl: "http://localhost:8008",
|
|
223
213
|
accessToken: myAccessToken,
|
|
224
214
|
userId: myUserId,
|
|
@@ -228,21 +218,21 @@ const matrixClient = sdk.createClient({
|
|
|
228
218
|
### Automatically join rooms when invited
|
|
229
219
|
|
|
230
220
|
```javascript
|
|
231
|
-
|
|
221
|
+
client.on(RoomEvent.MyMembership, function (room, membership, prevMembership) {
|
|
232
222
|
if (membership === KnownMembership.Invite) {
|
|
233
|
-
|
|
223
|
+
client.joinRoom(room.roomId).then(function () {
|
|
234
224
|
console.log("Auto-joined %s", room.roomId);
|
|
235
225
|
});
|
|
236
226
|
}
|
|
237
227
|
});
|
|
238
228
|
|
|
239
|
-
|
|
229
|
+
client.startClient();
|
|
240
230
|
```
|
|
241
231
|
|
|
242
232
|
### Print out messages for all rooms
|
|
243
233
|
|
|
244
234
|
```javascript
|
|
245
|
-
|
|
235
|
+
client.on(RoomEvent.Timeline, function (event, room, toStartOfTimeline) {
|
|
246
236
|
if (toStartOfTimeline) {
|
|
247
237
|
return; // don't print paginated results
|
|
248
238
|
}
|
|
@@ -258,7 +248,7 @@ matrixClient.on(RoomEvent.Timeline, function (event, room, toStartOfTimeline) {
|
|
|
258
248
|
);
|
|
259
249
|
});
|
|
260
250
|
|
|
261
|
-
|
|
251
|
+
client.startClient();
|
|
262
252
|
```
|
|
263
253
|
|
|
264
254
|
Output:
|
|
@@ -274,8 +264,8 @@ Output:
|
|
|
274
264
|
### Print out membership lists whenever they are changed
|
|
275
265
|
|
|
276
266
|
```javascript
|
|
277
|
-
|
|
278
|
-
const room =
|
|
267
|
+
client.on(RoomStateEvent.Members, function (event, state, member) {
|
|
268
|
+
const room = client.getRoom(state.roomId);
|
|
279
269
|
if (!room) {
|
|
280
270
|
return;
|
|
281
271
|
}
|
|
@@ -287,7 +277,7 @@ matrixClient.on(RoomStateEvent.Members, function (event, state, member) {
|
|
|
287
277
|
}
|
|
288
278
|
});
|
|
289
279
|
|
|
290
|
-
|
|
280
|
+
client.startClient();
|
|
291
281
|
```
|
|
292
282
|
|
|
293
283
|
Output:
|
|
@@ -323,18 +313,18 @@ Then visit `http://localhost:8005` to see the API docs.
|
|
|
323
313
|
|
|
324
314
|
## Initialization
|
|
325
315
|
|
|
326
|
-
To initialize the end-to-end encryption support in the
|
|
316
|
+
To initialize the end-to-end encryption support in the client:
|
|
327
317
|
|
|
328
318
|
```javascript
|
|
329
|
-
// Create a new
|
|
330
|
-
const
|
|
319
|
+
// Create a new client
|
|
320
|
+
const client = sdk.createClient({
|
|
331
321
|
baseUrl: "http://localhost:8008",
|
|
332
322
|
accessToken: myAccessToken,
|
|
333
323
|
userId: myUserId,
|
|
334
324
|
});
|
|
335
325
|
|
|
336
326
|
// Initialize to enable end-to-end encryption support.
|
|
337
|
-
await
|
|
327
|
+
await client.initRustCrypto();
|
|
338
328
|
```
|
|
339
329
|
|
|
340
330
|
Note that by default it will attempt to use the Indexed DB provided by the browser as a crypto store. If running outside the browser, you will need to pass [an options object](https://briij.github.io/@textrp/briij-js-sdk/classes/briij.BriijClient.html#initrustcrypto) which includes `useIndexedDB: false`, to use an ephemeral in-memory store instead. Note that without a persistent store, you'll need to create a new device on the server side (with [`BriijClient.loginRequest`](https://briij.github.io/@textrp/briij-js-sdk/classes/briij.BriijClient.html#loginrequest)) each time your application starts.
|
|
@@ -349,7 +339,7 @@ You should normally set up [secret storage](https://spec.matrix.org/v1.12/client
|
|
|
349
339
|
`bootstrapSecretStorage` can be called unconditionally: it will only set up the secret storage if it is not already set up (unless you use the `setupNewSecretStorage` parameter).
|
|
350
340
|
|
|
351
341
|
```javascript
|
|
352
|
-
const
|
|
342
|
+
const client = sdk.createClient({
|
|
353
343
|
...,
|
|
354
344
|
cryptoCallbacks: {
|
|
355
345
|
getSecretStorageKey: async (keys) => {
|
|
@@ -359,7 +349,7 @@ const matrixClient = sdk.createClient({
|
|
|
359
349
|
},
|
|
360
350
|
});
|
|
361
351
|
|
|
362
|
-
|
|
352
|
+
client.getCrypto().bootstrapSecretStorage({
|
|
363
353
|
// This function will be called if a new secret storage key (aka recovery key) is needed.
|
|
364
354
|
// You should prompt the user to save the key somewhere, because they will need it to unlock secret storage in future.
|
|
365
355
|
createSecretStorageKey: async () => {
|
|
@@ -379,7 +369,7 @@ To set up cross-signing to verify devices and other users, call
|
|
|
379
369
|
[`CryptoApi.bootstrapCrossSigning`](https://briij.github.io/@textrp/briij-js-sdk/interfaces/crypto_api.CryptoApi.html#bootstrapCrossSigning):
|
|
380
370
|
|
|
381
371
|
```javascript
|
|
382
|
-
|
|
372
|
+
client.getCrypto().bootstrapCrossSigning({
|
|
383
373
|
authUploadDeviceSigningKeys: async (makeRequest) => {
|
|
384
374
|
return makeRequest(authDict);
|
|
385
375
|
},
|
|
@@ -396,10 +386,10 @@ If the user doesn't already have a [key backup](https://spec.matrix.org/v1.12/cl
|
|
|
396
386
|
```javascript
|
|
397
387
|
// Check if we have a key backup.
|
|
398
388
|
// If checkKeyBackupAndEnable returns null, there is no key backup.
|
|
399
|
-
const hasKeyBackup = (await
|
|
389
|
+
const hasKeyBackup = (await client.getCrypto().checkKeyBackupAndEnable()) !== null;
|
|
400
390
|
|
|
401
391
|
// Create the key backup
|
|
402
|
-
await
|
|
392
|
+
await client.getCrypto().resetKeyBackup();
|
|
403
393
|
```
|
|
404
394
|
|
|
405
395
|
## Verify a new device
|
|
@@ -421,8 +411,8 @@ This migration happens automatically when you call `initRustCrypto()` instead of
|
|
|
421
411
|
but you need to provide the legacy [`cryptoStore`](https://briij.github.io/@textrp/briij-js-sdk/interfaces/briij.ICreateClientOpts.html#cryptoStore) and [`pickleKey`](https://briij.github.io/@textrp/briij-js-sdk/interfaces/briij.ICreateClientOpts.html#pickleKey) to [`createClient`](https://briij.github.io/@textrp/briij-js-sdk/functions/briij.createClient.html):
|
|
422
412
|
|
|
423
413
|
```javascript
|
|
424
|
-
// You should provide the legacy crypto store and the pickle key to the
|
|
425
|
-
const
|
|
414
|
+
// You should provide the legacy crypto store and the pickle key to the client in order to migrate the data.
|
|
415
|
+
const client = sdk.createClient({
|
|
426
416
|
cryptoStore: myCryptoStore,
|
|
427
417
|
pickleKey: myPickleKey,
|
|
428
418
|
baseUrl: "http://localhost:8008",
|
|
@@ -431,14 +421,14 @@ const matrixClient = sdk.createClient({
|
|
|
431
421
|
});
|
|
432
422
|
|
|
433
423
|
// The migration will be done automatically when you call `initRustCrypto`.
|
|
434
|
-
await
|
|
424
|
+
await client.initRustCrypto();
|
|
435
425
|
```
|
|
436
426
|
|
|
437
427
|
To follow the migration progress, you can listen to the [`CryptoEvent.LegacyCryptoStoreMigrationProgress`](https://briij.github.io/@textrp/briij-js-sdk/enums/crypto_api.CryptoEvent.html#LegacyCryptoStoreMigrationProgress) event:
|
|
438
428
|
|
|
439
429
|
```javascript
|
|
440
430
|
// When progress === total === -1, the migration is finished.
|
|
441
|
-
|
|
431
|
+
client.on(CryptoEvent.LegacyCryptoStoreMigrationProgress, (progress, total) => {
|
|
442
432
|
...
|
|
443
433
|
});
|
|
444
434
|
```
|