@hocuspocus/provider 1.0.0-alpha.27 → 1.0.0-alpha.31
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/dist/hocuspocus-provider.cjs +126 -351
- package/dist/hocuspocus-provider.cjs.map +1 -1
- package/dist/hocuspocus-provider.esm.js +123 -347
- package/dist/hocuspocus-provider.esm.js.map +1 -1
- package/dist/packages/common/src/CloseEvents.d.ts +23 -0
- package/dist/packages/common/src/awarenessStatesToArray.d.ts +3 -0
- package/dist/packages/common/src/index.d.ts +2 -0
- package/dist/packages/extension-database/src/Database.d.ts +32 -0
- package/dist/packages/extension-database/src/index.d.ts +1 -0
- package/dist/packages/extension-logger/src/Logger.d.ts +5 -0
- package/dist/packages/extension-sqlite/src/SQLite.d.ts +26 -0
- package/dist/packages/extension-sqlite/src/index.d.ts +1 -0
- package/dist/packages/extension-throttle/src/index.d.ts +6 -2
- package/dist/packages/extension-webhook/src/index.d.ts +2 -3
- package/dist/packages/provider/src/EventEmitter.d.ts +1 -1
- package/dist/packages/provider/src/HocuspocusCloudProvider.d.ts +4 -3
- package/dist/packages/provider/src/HocuspocusProvider.d.ts +17 -9
- package/dist/packages/provider/src/index.d.ts +0 -1
- package/dist/packages/provider/src/types.d.ts +10 -0
- package/dist/packages/server/src/Connection.d.ts +5 -5
- package/dist/packages/server/src/Debugger.d.ts +1 -2
- package/dist/packages/server/src/Document.d.ts +8 -5
- package/dist/packages/server/src/Hocuspocus.d.ts +32 -14
- package/dist/packages/server/src/MessageReceiver.d.ts +8 -5
- package/dist/packages/server/src/OutgoingMessage.d.ts +1 -0
- package/dist/packages/server/src/index.d.ts +7 -0
- package/dist/packages/server/src/types.d.ts +92 -23
- package/dist/{demos/backend/src/express.d.ts → playground/backend/src/default.d.ts} +0 -0
- package/dist/{demos/backend/src/koa.d.ts → playground/backend/src/express.d.ts} +0 -0
- package/dist/{demos/backend/src/load-document.d.ts → playground/backend/src/koa.d.ts} +0 -0
- package/dist/{demos/backend/src/minimal.d.ts → playground/backend/src/load-document.d.ts} +0 -0
- package/dist/{demos → playground}/backend/src/monitor.d.ts +0 -0
- package/dist/{demos → playground}/backend/src/redis.d.ts +0 -0
- package/dist/{demos → playground}/backend/src/slow.d.ts +0 -0
- package/dist/{demos → playground}/backend/src/webhook.d.ts +0 -0
- package/dist/tests/extension-logger/onListen.d.ts +1 -0
- package/dist/tests/extension-redis/onLoadDocument.d.ts +1 -0
- package/dist/tests/extension-redis/onSynced.d.ts +1 -0
- package/dist/tests/extension-redis-rewrite/closeConnections.d.ts +1 -0
- package/dist/tests/extension-redis-rewrite/getConnectionCount.d.ts +1 -0
- package/dist/tests/extension-redis-rewrite/getDocumentsCount.d.ts +1 -0
- package/dist/tests/extension-redis-rewrite/onAwarenessChange.d.ts +1 -0
- package/dist/tests/extension-redis-rewrite/onChange.d.ts +1 -0
- package/dist/tests/extension-redis-rewrite/onStoreDocument.d.ts +1 -0
- package/dist/tests/extension-rocksdb/onLoadDocument.d.ts +1 -0
- package/dist/tests/extension-throttle/configuration.d.ts +1 -0
- package/dist/tests/provider/configuration.d.ts +1 -0
- package/dist/tests/provider/observeDeep.d.ts +1 -0
- package/dist/tests/provider/onAuthenticated.d.ts +1 -0
- package/dist/tests/provider/onAuthenticationFailed.d.ts +1 -0
- package/dist/tests/provider/onAwarenessChange.d.ts +1 -0
- package/dist/tests/provider/onAwarenessUpdate.d.ts +1 -0
- package/dist/tests/provider/onClose.d.ts +1 -0
- package/dist/tests/provider/onConnect.d.ts +1 -0
- package/dist/tests/provider/onDisconnect.d.ts +1 -0
- package/dist/tests/provider/onMessage.d.ts +1 -0
- package/dist/tests/provider/onOpen.d.ts +1 -0
- package/dist/tests/provider/onSynced.d.ts +1 -0
- package/dist/tests/server/address.d.ts +1 -0
- package/dist/tests/server/afterStoreDocument.d.ts +1 -0
- package/dist/tests/server/closeConnections.d.ts +1 -0
- package/dist/tests/server/getConnectionsCount.d.ts +1 -0
- package/dist/tests/server/getDocumentName.d.ts +1 -0
- package/dist/tests/server/getDocumentsCount.d.ts +1 -0
- package/dist/tests/server/getMessageLogs.d.ts +1 -0
- package/dist/tests/server/listen.d.ts +1 -0
- package/dist/tests/server/onAuthenticate.d.ts +1 -0
- package/dist/tests/server/onAwarenessUpdate.d.ts +1 -0
- package/dist/tests/server/onChange.d.ts +1 -0
- package/dist/tests/server/onConfigure.d.ts +1 -0
- package/dist/tests/server/onConnect.d.ts +1 -0
- package/dist/tests/server/onDestroy.d.ts +1 -0
- package/dist/tests/server/onDisconnect.d.ts +1 -0
- package/dist/tests/server/onListen.d.ts +1 -0
- package/dist/tests/server/onLoadDocument.d.ts +1 -0
- package/dist/tests/server/onRequest.d.ts +1 -0
- package/dist/tests/server/onStoreDocument.d.ts +1 -0
- package/dist/tests/server/onUpgrade.d.ts +1 -0
- package/dist/tests/server/requiresAuthentication.d.ts +1 -0
- package/dist/tests/transformer/TiptapTransformer.d.ts +1 -0
- package/dist/tests/utils/createDirectory.d.ts +1 -0
- package/dist/tests/utils/flushRedis.d.ts +1 -0
- package/dist/tests/utils/index.d.ts +7 -0
- package/dist/tests/utils/newHocuspocus.d.ts +2 -0
- package/dist/tests/utils/newHocuspocusProvider.d.ts +3 -0
- package/dist/tests/utils/redisConnectionSettings.d.ts +4 -0
- package/dist/tests/utils/removeDirectory.d.ts +1 -0
- package/dist/tests/utils/sleep.d.ts +1 -0
- package/package.json +6 -5
- package/src/EventEmitter.ts +1 -1
- package/src/HocuspocusCloudProvider.ts +18 -10
- package/src/HocuspocusProvider.ts +100 -69
- package/src/index.ts +0 -1
- package/src/types.ts +10 -0
- package/dist/packages/provider/src/utils/awarenessStatesToArray.d.ts +0 -4
- package/dist/packages/provider/src/utils/index.d.ts +0 -1
- package/dist/packages/server/src/CloseEvents.d.ts +0 -4
- package/src/utils/awarenessStatesToArray.ts +0 -8
- package/src/utils/index.ts +0 -1
|
@@ -4,6 +4,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
var Y = require('yjs');
|
|
6
6
|
var attempt = require('@lifeomic/attempt');
|
|
7
|
+
var common = require('@hocuspocus/common');
|
|
7
8
|
|
|
8
9
|
function _interopNamespace(e) {
|
|
9
10
|
if (e && e.__esModule) return e;
|
|
@@ -301,8 +302,8 @@ const min = (a, b) => a < b ? a : b;
|
|
|
301
302
|
const max = (a, b) => a > b ? a : b;
|
|
302
303
|
|
|
303
304
|
/* eslint-env browser */
|
|
304
|
-
const BIT8
|
|
305
|
-
const BITS7
|
|
305
|
+
const BIT8 = 128;
|
|
306
|
+
const BITS7 = 127;
|
|
306
307
|
|
|
307
308
|
/**
|
|
308
309
|
* Efficient schema-less binary encoding with support for variable length encoding.
|
|
@@ -393,7 +394,7 @@ const toUint8Array = encoder => {
|
|
|
393
394
|
* @param {Encoder} encoder
|
|
394
395
|
* @param {number} num The byte that is to be encoded.
|
|
395
396
|
*/
|
|
396
|
-
const write
|
|
397
|
+
const write = (encoder, num) => {
|
|
397
398
|
const bufferLen = encoder.cbuf.length;
|
|
398
399
|
if (encoder.cpos === bufferLen) {
|
|
399
400
|
encoder.bufs.push(encoder.cbuf);
|
|
@@ -412,12 +413,12 @@ const write$1 = (encoder, num) => {
|
|
|
412
413
|
* @param {Encoder} encoder
|
|
413
414
|
* @param {number} num The number that is to be encoded.
|
|
414
415
|
*/
|
|
415
|
-
const writeVarUint
|
|
416
|
-
while (num > BITS7
|
|
417
|
-
write
|
|
416
|
+
const writeVarUint = (encoder, num) => {
|
|
417
|
+
while (num > BITS7) {
|
|
418
|
+
write(encoder, BIT8 | (BITS7 & num));
|
|
418
419
|
num >>>= 7;
|
|
419
420
|
}
|
|
420
|
-
write
|
|
421
|
+
write(encoder, BITS7 & num);
|
|
421
422
|
};
|
|
422
423
|
|
|
423
424
|
/**
|
|
@@ -427,12 +428,12 @@ const writeVarUint$1 = (encoder, num) => {
|
|
|
427
428
|
* @param {Encoder} encoder
|
|
428
429
|
* @param {String} str The string that is to be encoded.
|
|
429
430
|
*/
|
|
430
|
-
const writeVarString
|
|
431
|
+
const writeVarString = (encoder, str) => {
|
|
431
432
|
const encodedString = unescape(encodeURIComponent(str));
|
|
432
433
|
const len = encodedString.length;
|
|
433
|
-
writeVarUint
|
|
434
|
+
writeVarUint(encoder, len);
|
|
434
435
|
for (let i = 0; i < len; i++) {
|
|
435
|
-
write
|
|
436
|
+
write(encoder, /** @type {number} */ (encodedString.codePointAt(i)));
|
|
436
437
|
}
|
|
437
438
|
};
|
|
438
439
|
|
|
@@ -470,7 +471,7 @@ const writeUint8Array = (encoder, uint8Array) => {
|
|
|
470
471
|
* @param {Uint8Array} uint8Array
|
|
471
472
|
*/
|
|
472
473
|
const writeVarUint8Array = (encoder, uint8Array) => {
|
|
473
|
-
writeVarUint
|
|
474
|
+
writeVarUint(encoder, uint8Array.byteLength);
|
|
474
475
|
writeUint8Array(encoder, uint8Array);
|
|
475
476
|
};
|
|
476
477
|
|
|
@@ -559,7 +560,7 @@ const readUint8Array = (decoder, len) => {
|
|
|
559
560
|
* @param {Decoder} decoder
|
|
560
561
|
* @return {Uint8Array}
|
|
561
562
|
*/
|
|
562
|
-
const readVarUint8Array = decoder => readUint8Array(decoder, readVarUint
|
|
563
|
+
const readVarUint8Array = decoder => readUint8Array(decoder, readVarUint(decoder));
|
|
563
564
|
|
|
564
565
|
/**
|
|
565
566
|
* Read one byte as unsigned integer.
|
|
@@ -567,7 +568,7 @@ const readVarUint8Array = decoder => readUint8Array(decoder, readVarUint$1(decod
|
|
|
567
568
|
* @param {Decoder} decoder The decoder instance
|
|
568
569
|
* @return {number} Unsigned 8-bit integer
|
|
569
570
|
*/
|
|
570
|
-
const readUint8
|
|
571
|
+
const readUint8 = decoder => decoder.arr[decoder.pos++];
|
|
571
572
|
|
|
572
573
|
/**
|
|
573
574
|
* Read unsigned integer (32bit) with variable length.
|
|
@@ -579,14 +580,14 @@ const readUint8$1 = decoder => decoder.arr[decoder.pos++];
|
|
|
579
580
|
* @param {Decoder} decoder
|
|
580
581
|
* @return {number} An unsigned integer.length
|
|
581
582
|
*/
|
|
582
|
-
const readVarUint
|
|
583
|
+
const readVarUint = decoder => {
|
|
583
584
|
let num = 0;
|
|
584
585
|
let len = 0;
|
|
585
586
|
while (true) {
|
|
586
587
|
const r = decoder.arr[decoder.pos++];
|
|
587
|
-
num = num | ((r & BITS7
|
|
588
|
+
num = num | ((r & BITS7) << len);
|
|
588
589
|
len += 7;
|
|
589
|
-
if (r < BIT8
|
|
590
|
+
if (r < BIT8) {
|
|
590
591
|
return num >>> 0 // return unsigned number!
|
|
591
592
|
}
|
|
592
593
|
/* istanbul ignore if */
|
|
@@ -609,15 +610,15 @@ const readVarUint$1 = decoder => {
|
|
|
609
610
|
* @param {Decoder} decoder
|
|
610
611
|
* @return {String} The read String.
|
|
611
612
|
*/
|
|
612
|
-
const readVarString
|
|
613
|
-
let remainingLen = readVarUint
|
|
613
|
+
const readVarString = decoder => {
|
|
614
|
+
let remainingLen = readVarUint(decoder);
|
|
614
615
|
if (remainingLen === 0) {
|
|
615
616
|
return ''
|
|
616
617
|
} else {
|
|
617
|
-
let encodedString = String.fromCodePoint(readUint8
|
|
618
|
+
let encodedString = String.fromCodePoint(readUint8(decoder)); // remember to decrease remainingLen
|
|
618
619
|
if (--remainingLen < 100) { // do not create a Uint8Array for small strings
|
|
619
620
|
while (remainingLen--) {
|
|
620
|
-
encodedString += String.fromCodePoint(readUint8
|
|
621
|
+
encodedString += String.fromCodePoint(readUint8(decoder));
|
|
621
622
|
}
|
|
622
623
|
} else {
|
|
623
624
|
while (remainingLen > 0) {
|
|
@@ -1230,14 +1231,14 @@ const removeAwarenessStates = (awareness, clients, origin) => {
|
|
|
1230
1231
|
const encodeAwarenessUpdate = (awareness, clients, states = awareness.states) => {
|
|
1231
1232
|
const len = clients.length;
|
|
1232
1233
|
const encoder = createEncoder();
|
|
1233
|
-
writeVarUint
|
|
1234
|
+
writeVarUint(encoder, len);
|
|
1234
1235
|
for (let i = 0; i < len; i++) {
|
|
1235
1236
|
const clientID = clients[i];
|
|
1236
1237
|
const state = states.get(clientID) || null;
|
|
1237
1238
|
const clock = /** @type {MetaClientState} */ (awareness.meta.get(clientID)).clock;
|
|
1238
|
-
writeVarUint
|
|
1239
|
-
writeVarUint
|
|
1240
|
-
writeVarString
|
|
1239
|
+
writeVarUint(encoder, clientID);
|
|
1240
|
+
writeVarUint(encoder, clock);
|
|
1241
|
+
writeVarString(encoder, JSON.stringify(state));
|
|
1241
1242
|
}
|
|
1242
1243
|
return toUint8Array(encoder)
|
|
1243
1244
|
};
|
|
@@ -1254,11 +1255,11 @@ const applyAwarenessUpdate = (awareness, update, origin) => {
|
|
|
1254
1255
|
const updated = [];
|
|
1255
1256
|
const filteredUpdated = [];
|
|
1256
1257
|
const removed = [];
|
|
1257
|
-
const len = readVarUint
|
|
1258
|
+
const len = readVarUint(decoder);
|
|
1258
1259
|
for (let i = 0; i < len; i++) {
|
|
1259
|
-
const clientID = readVarUint
|
|
1260
|
-
let clock = readVarUint
|
|
1261
|
-
const state = JSON.parse(readVarString
|
|
1260
|
+
const clientID = readVarUint(decoder);
|
|
1261
|
+
let clock = readVarUint(decoder);
|
|
1262
|
+
const state = JSON.parse(readVarString(decoder));
|
|
1262
1263
|
const clientMeta = awareness.meta.get(clientID);
|
|
1263
1264
|
const prevState = awareness.states.get(clientID);
|
|
1264
1265
|
const currClock = clientMeta === undefined ? 0 : clientMeta.clock;
|
|
@@ -1402,13 +1403,13 @@ class IncomingMessage {
|
|
|
1402
1403
|
this.decoder = createDecoder(new Uint8Array(this.data));
|
|
1403
1404
|
}
|
|
1404
1405
|
readVarUint() {
|
|
1405
|
-
return readVarUint
|
|
1406
|
+
return readVarUint(this.decoder);
|
|
1406
1407
|
}
|
|
1407
1408
|
readVarUint8Array() {
|
|
1408
1409
|
return readVarUint8Array(this.decoder);
|
|
1409
1410
|
}
|
|
1410
1411
|
writeVarUint(type) {
|
|
1411
|
-
return writeVarUint
|
|
1412
|
+
return writeVarUint(this.encoder, type);
|
|
1412
1413
|
}
|
|
1413
1414
|
writeVarUint8Array(data) {
|
|
1414
1415
|
return writeVarUint8Array(this.encoder, data);
|
|
@@ -1462,7 +1463,7 @@ const messageYjsUpdate = 2;
|
|
|
1462
1463
|
* @param {Y.Doc} doc
|
|
1463
1464
|
*/
|
|
1464
1465
|
const writeSyncStep1 = (encoder, doc) => {
|
|
1465
|
-
writeVarUint
|
|
1466
|
+
writeVarUint(encoder, messageYjsSyncStep1);
|
|
1466
1467
|
const sv = Y__namespace.encodeStateVector(doc);
|
|
1467
1468
|
writeVarUint8Array(encoder, sv);
|
|
1468
1469
|
};
|
|
@@ -1473,7 +1474,7 @@ const writeSyncStep1 = (encoder, doc) => {
|
|
|
1473
1474
|
* @param {Uint8Array} [encodedStateVector]
|
|
1474
1475
|
*/
|
|
1475
1476
|
const writeSyncStep2 = (encoder, doc, encodedStateVector) => {
|
|
1476
|
-
writeVarUint
|
|
1477
|
+
writeVarUint(encoder, messageYjsSyncStep2);
|
|
1477
1478
|
writeVarUint8Array(encoder, Y__namespace.encodeStateAsUpdate(doc, encodedStateVector));
|
|
1478
1479
|
};
|
|
1479
1480
|
|
|
@@ -1508,7 +1509,7 @@ const readSyncStep2 = (decoder, doc, transactionOrigin) => {
|
|
|
1508
1509
|
* @param {Uint8Array} update
|
|
1509
1510
|
*/
|
|
1510
1511
|
const writeUpdate = (encoder, update) => {
|
|
1511
|
-
writeVarUint
|
|
1512
|
+
writeVarUint(encoder, messageYjsUpdate);
|
|
1512
1513
|
writeVarUint8Array(encoder, update);
|
|
1513
1514
|
};
|
|
1514
1515
|
|
|
@@ -1528,7 +1529,7 @@ const readUpdate = readSyncStep2;
|
|
|
1528
1529
|
* @param {any} transactionOrigin
|
|
1529
1530
|
*/
|
|
1530
1531
|
const readSyncMessage = (decoder, encoder, doc, transactionOrigin) => {
|
|
1531
|
-
const messageType = readVarUint
|
|
1532
|
+
const messageType = readVarUint(decoder);
|
|
1532
1533
|
switch (messageType) {
|
|
1533
1534
|
case messageYjsSyncStep1:
|
|
1534
1535
|
readSyncStep1(decoder, encoder, doc);
|
|
@@ -1545,239 +1546,6 @@ const readSyncMessage = (decoder, encoder, doc, transactionOrigin) => {
|
|
|
1545
1546
|
return messageType
|
|
1546
1547
|
};
|
|
1547
1548
|
|
|
1548
|
-
/* eslint-env browser */
|
|
1549
|
-
const BIT8 = 128;
|
|
1550
|
-
const BITS7 = 127;
|
|
1551
|
-
/**
|
|
1552
|
-
* Efficient schema-less binary decoding with support for variable length encoding.
|
|
1553
|
-
*
|
|
1554
|
-
* Use [lib0/decoding] with [lib0/encoding]. Every encoding function has a corresponding decoding function.
|
|
1555
|
-
*
|
|
1556
|
-
* Encodes numbers in little-endian order (least to most significant byte order)
|
|
1557
|
-
* and is compatible with Golang's binary encoding (https://golang.org/pkg/encoding/binary/)
|
|
1558
|
-
* which is also used in Protocol Buffers.
|
|
1559
|
-
*
|
|
1560
|
-
* ```js
|
|
1561
|
-
* // encoding step
|
|
1562
|
-
* const encoder = new encoding.createEncoder()
|
|
1563
|
-
* encoding.writeVarUint(encoder, 256)
|
|
1564
|
-
* encoding.writeVarString(encoder, 'Hello world!')
|
|
1565
|
-
* const buf = encoding.toUint8Array(encoder)
|
|
1566
|
-
* ```
|
|
1567
|
-
*
|
|
1568
|
-
* ```js
|
|
1569
|
-
* // decoding step
|
|
1570
|
-
* const decoder = new decoding.createDecoder(buf)
|
|
1571
|
-
* decoding.readVarUint(decoder) // => 256
|
|
1572
|
-
* decoding.readVarString(decoder) // => 'Hello world!'
|
|
1573
|
-
* decoding.hasContent(decoder) // => false - all data is read
|
|
1574
|
-
* ```
|
|
1575
|
-
*
|
|
1576
|
-
* @module decoding
|
|
1577
|
-
*/
|
|
1578
|
-
|
|
1579
|
-
/**
|
|
1580
|
-
* Read one byte as unsigned integer.
|
|
1581
|
-
* @function
|
|
1582
|
-
* @param {Decoder} decoder The decoder instance
|
|
1583
|
-
* @return {number} Unsigned 8-bit integer
|
|
1584
|
-
*/
|
|
1585
|
-
|
|
1586
|
-
const readUint8 = decoder => decoder.arr[decoder.pos++];
|
|
1587
|
-
/**
|
|
1588
|
-
* Read unsigned integer (32bit) with variable length.
|
|
1589
|
-
* 1/8th of the storage is used as encoding overhead.
|
|
1590
|
-
* * numbers < 2^7 is stored in one bytlength
|
|
1591
|
-
* * numbers < 2^14 is stored in two bylength
|
|
1592
|
-
*
|
|
1593
|
-
* @function
|
|
1594
|
-
* @param {Decoder} decoder
|
|
1595
|
-
* @return {number} An unsigned integer.length
|
|
1596
|
-
*/
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
const readVarUint = decoder => {
|
|
1600
|
-
let num = 0;
|
|
1601
|
-
let len = 0;
|
|
1602
|
-
|
|
1603
|
-
while (true) {
|
|
1604
|
-
const r = decoder.arr[decoder.pos++];
|
|
1605
|
-
num = num | (r & BITS7) << len;
|
|
1606
|
-
len += 7;
|
|
1607
|
-
|
|
1608
|
-
if (r < BIT8) {
|
|
1609
|
-
return num >>> 0; // return unsigned number!
|
|
1610
|
-
}
|
|
1611
|
-
/* istanbul ignore if */
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
if (len > 35) {
|
|
1615
|
-
throw new Error('Integer out of range!');
|
|
1616
|
-
}
|
|
1617
|
-
}
|
|
1618
|
-
};
|
|
1619
|
-
/**
|
|
1620
|
-
* Read string of variable length
|
|
1621
|
-
* * varUint is used to store the length of the string
|
|
1622
|
-
*
|
|
1623
|
-
* Transforming utf8 to a string is pretty expensive. The code performs 10x better
|
|
1624
|
-
* when String.fromCodePoint is fed with all characters as arguments.
|
|
1625
|
-
* But most environments have a maximum number of arguments per functions.
|
|
1626
|
-
* For effiency reasons we apply a maximum of 10000 characters at once.
|
|
1627
|
-
*
|
|
1628
|
-
* @function
|
|
1629
|
-
* @param {Decoder} decoder
|
|
1630
|
-
* @return {String} The read String.
|
|
1631
|
-
*/
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
const readVarString = decoder => {
|
|
1635
|
-
let remainingLen = readVarUint(decoder);
|
|
1636
|
-
|
|
1637
|
-
if (remainingLen === 0) {
|
|
1638
|
-
return '';
|
|
1639
|
-
} else {
|
|
1640
|
-
let encodedString = String.fromCodePoint(readUint8(decoder)); // remember to decrease remainingLen
|
|
1641
|
-
|
|
1642
|
-
if (--remainingLen < 100) {
|
|
1643
|
-
// do not create a Uint8Array for small strings
|
|
1644
|
-
while (remainingLen--) {
|
|
1645
|
-
encodedString += String.fromCodePoint(readUint8(decoder));
|
|
1646
|
-
}
|
|
1647
|
-
} else {
|
|
1648
|
-
while (remainingLen > 0) {
|
|
1649
|
-
const nextLen = remainingLen < 10000 ? remainingLen : 10000; // this is dangerous, we create a fresh array view from the existing buffer
|
|
1650
|
-
|
|
1651
|
-
const bytes = decoder.arr.subarray(decoder.pos, decoder.pos + nextLen);
|
|
1652
|
-
decoder.pos += nextLen; // Starting with ES5.1 we can supply a generic array-like object as arguments
|
|
1653
|
-
|
|
1654
|
-
encodedString += String.fromCodePoint.apply(null,
|
|
1655
|
-
/** @type {any} */
|
|
1656
|
-
bytes);
|
|
1657
|
-
remainingLen -= nextLen;
|
|
1658
|
-
}
|
|
1659
|
-
}
|
|
1660
|
-
|
|
1661
|
-
return decodeURIComponent(escape(encodedString));
|
|
1662
|
-
}
|
|
1663
|
-
};
|
|
1664
|
-
/**
|
|
1665
|
-
* Efficient schema-less binary encoding with support for variable length encoding.
|
|
1666
|
-
*
|
|
1667
|
-
* Use [lib0/encoding] with [lib0/decoding]. Every encoding function has a corresponding decoding function.
|
|
1668
|
-
*
|
|
1669
|
-
* Encodes numbers in little-endian order (least to most significant byte order)
|
|
1670
|
-
* and is compatible with Golang's binary encoding (https://golang.org/pkg/encoding/binary/)
|
|
1671
|
-
* which is also used in Protocol Buffers.
|
|
1672
|
-
*
|
|
1673
|
-
* ```js
|
|
1674
|
-
* // encoding step
|
|
1675
|
-
* const encoder = new encoding.createEncoder()
|
|
1676
|
-
* encoding.writeVarUint(encoder, 256)
|
|
1677
|
-
* encoding.writeVarString(encoder, 'Hello world!')
|
|
1678
|
-
* const buf = encoding.toUint8Array(encoder)
|
|
1679
|
-
* ```
|
|
1680
|
-
*
|
|
1681
|
-
* ```js
|
|
1682
|
-
* // decoding step
|
|
1683
|
-
* const decoder = new decoding.createDecoder(buf)
|
|
1684
|
-
* decoding.readVarUint(decoder) // => 256
|
|
1685
|
-
* decoding.readVarString(decoder) // => 'Hello world!'
|
|
1686
|
-
* decoding.hasContent(decoder) // => false - all data is read
|
|
1687
|
-
* ```
|
|
1688
|
-
*
|
|
1689
|
-
* @module encoding
|
|
1690
|
-
*/
|
|
1691
|
-
|
|
1692
|
-
/**
|
|
1693
|
-
* Write one byte to the encoder.
|
|
1694
|
-
*
|
|
1695
|
-
* @function
|
|
1696
|
-
* @param {Encoder} encoder
|
|
1697
|
-
* @param {number} num The byte that is to be encoded.
|
|
1698
|
-
*/
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
const write = (encoder, num) => {
|
|
1702
|
-
const bufferLen = encoder.cbuf.length;
|
|
1703
|
-
|
|
1704
|
-
if (encoder.cpos === bufferLen) {
|
|
1705
|
-
encoder.bufs.push(encoder.cbuf);
|
|
1706
|
-
encoder.cbuf = new Uint8Array(bufferLen * 2);
|
|
1707
|
-
encoder.cpos = 0;
|
|
1708
|
-
}
|
|
1709
|
-
|
|
1710
|
-
encoder.cbuf[encoder.cpos++] = num;
|
|
1711
|
-
};
|
|
1712
|
-
/**
|
|
1713
|
-
* Write a variable length unsigned integer.
|
|
1714
|
-
*
|
|
1715
|
-
* Encodes integers in the range from [0, 4294967295] / [0, 0xffffffff]. (max 32 bit unsigned integer)
|
|
1716
|
-
*
|
|
1717
|
-
* @function
|
|
1718
|
-
* @param {Encoder} encoder
|
|
1719
|
-
* @param {number} num The number that is to be encoded.
|
|
1720
|
-
*/
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
const writeVarUint = (encoder, num) => {
|
|
1724
|
-
while (num > BITS7) {
|
|
1725
|
-
write(encoder, BIT8 | BITS7 & num);
|
|
1726
|
-
num >>>= 7;
|
|
1727
|
-
}
|
|
1728
|
-
|
|
1729
|
-
write(encoder, BITS7 & num);
|
|
1730
|
-
};
|
|
1731
|
-
/**
|
|
1732
|
-
* Write a variable length string.
|
|
1733
|
-
*
|
|
1734
|
-
* @function
|
|
1735
|
-
* @param {Encoder} encoder
|
|
1736
|
-
* @param {String} str The string that is to be encoded.
|
|
1737
|
-
*/
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
const writeVarString = (encoder, str) => {
|
|
1741
|
-
const encodedString = unescape(encodeURIComponent(str));
|
|
1742
|
-
const len = encodedString.length;
|
|
1743
|
-
writeVarUint(encoder, len);
|
|
1744
|
-
|
|
1745
|
-
for (let i = 0; i < len; i++) {
|
|
1746
|
-
write(encoder,
|
|
1747
|
-
/** @type {number} */
|
|
1748
|
-
encodedString.codePointAt(i));
|
|
1749
|
-
}
|
|
1750
|
-
};
|
|
1751
|
-
|
|
1752
|
-
var AuthMessageType;
|
|
1753
|
-
|
|
1754
|
-
(function (AuthMessageType) {
|
|
1755
|
-
AuthMessageType[AuthMessageType["Token"] = 0] = "Token";
|
|
1756
|
-
AuthMessageType[AuthMessageType["PermissionDenied"] = 1] = "PermissionDenied";
|
|
1757
|
-
AuthMessageType[AuthMessageType["Authenticated"] = 2] = "Authenticated";
|
|
1758
|
-
})(AuthMessageType || (AuthMessageType = {}));
|
|
1759
|
-
|
|
1760
|
-
const writeAuthentication = (encoder, auth) => {
|
|
1761
|
-
writeVarUint(encoder, AuthMessageType.Token);
|
|
1762
|
-
writeVarString(encoder, auth);
|
|
1763
|
-
};
|
|
1764
|
-
|
|
1765
|
-
const readAuthMessage = (decoder, permissionDeniedHandler, authenticatedHandler) => {
|
|
1766
|
-
switch (readVarUint(decoder)) {
|
|
1767
|
-
case AuthMessageType.PermissionDenied:
|
|
1768
|
-
{
|
|
1769
|
-
permissionDeniedHandler(readVarString(decoder));
|
|
1770
|
-
break;
|
|
1771
|
-
}
|
|
1772
|
-
|
|
1773
|
-
case AuthMessageType.Authenticated:
|
|
1774
|
-
{
|
|
1775
|
-
authenticatedHandler();
|
|
1776
|
-
break;
|
|
1777
|
-
}
|
|
1778
|
-
}
|
|
1779
|
-
};
|
|
1780
|
-
|
|
1781
1549
|
exports.MessageType = void 0;
|
|
1782
1550
|
(function (MessageType) {
|
|
1783
1551
|
MessageType[MessageType["Sync"] = 0] = "Sync";
|
|
@@ -1856,7 +1624,7 @@ class MessageReceiver {
|
|
|
1856
1624
|
}
|
|
1857
1625
|
applyAuthMessage(provider) {
|
|
1858
1626
|
const { message } = this;
|
|
1859
|
-
readAuthMessage(message.decoder, provider.permissionDeniedHandler.bind(provider), provider.authenticatedHandler.bind(provider));
|
|
1627
|
+
common.readAuthMessage(message.decoder, provider.permissionDeniedHandler.bind(provider), provider.authenticatedHandler.bind(provider));
|
|
1860
1628
|
}
|
|
1861
1629
|
applyQueryAwarenessMessage(provider) {
|
|
1862
1630
|
const { message } = this;
|
|
@@ -1891,7 +1659,7 @@ class SyncStepOneMessage extends OutgoingMessage {
|
|
|
1891
1659
|
if (typeof args.document === 'undefined') {
|
|
1892
1660
|
throw new Error('The sync step one message requires document as an argument');
|
|
1893
1661
|
}
|
|
1894
|
-
writeVarUint
|
|
1662
|
+
writeVarUint(this.encoder, this.type);
|
|
1895
1663
|
writeSyncStep1(this.encoder, args.document);
|
|
1896
1664
|
return this.encoder;
|
|
1897
1665
|
}
|
|
@@ -1907,7 +1675,7 @@ class SyncStepTwoMessage extends OutgoingMessage {
|
|
|
1907
1675
|
if (typeof args.document === 'undefined') {
|
|
1908
1676
|
throw new Error('The sync step two message requires document as an argument');
|
|
1909
1677
|
}
|
|
1910
|
-
writeVarUint
|
|
1678
|
+
writeVarUint(this.encoder, this.type);
|
|
1911
1679
|
writeSyncStep2(this.encoder, args.document);
|
|
1912
1680
|
return this.encoder;
|
|
1913
1681
|
}
|
|
@@ -1920,7 +1688,7 @@ class QueryAwarenessMessage extends OutgoingMessage {
|
|
|
1920
1688
|
this.description = 'Queries awareness states';
|
|
1921
1689
|
}
|
|
1922
1690
|
get(args) {
|
|
1923
|
-
writeVarUint
|
|
1691
|
+
writeVarUint(this.encoder, this.type);
|
|
1924
1692
|
return this.encoder;
|
|
1925
1693
|
}
|
|
1926
1694
|
}
|
|
@@ -1935,8 +1703,8 @@ class AuthenticationMessage extends OutgoingMessage {
|
|
|
1935
1703
|
if (typeof args.token === 'undefined') {
|
|
1936
1704
|
throw new Error('The authentication message requires `token` as an argument.');
|
|
1937
1705
|
}
|
|
1938
|
-
writeVarUint
|
|
1939
|
-
writeAuthentication(this.encoder, args.token);
|
|
1706
|
+
writeVarUint(this.encoder, this.type);
|
|
1707
|
+
common.writeAuthentication(this.encoder, args.token);
|
|
1940
1708
|
return this.encoder;
|
|
1941
1709
|
}
|
|
1942
1710
|
}
|
|
@@ -1954,7 +1722,7 @@ class AwarenessMessage extends OutgoingMessage {
|
|
|
1954
1722
|
if (typeof args.clients === 'undefined') {
|
|
1955
1723
|
throw new Error('The awareness message requires clients as an argument');
|
|
1956
1724
|
}
|
|
1957
|
-
writeVarUint
|
|
1725
|
+
writeVarUint(this.encoder, this.type);
|
|
1958
1726
|
let awarenessUpdate;
|
|
1959
1727
|
if (args.states === undefined) {
|
|
1960
1728
|
awarenessUpdate = encodeAwarenessUpdate(args.awareness, args.clients);
|
|
@@ -1974,21 +1742,12 @@ class UpdateMessage extends OutgoingMessage {
|
|
|
1974
1742
|
this.description = 'A document update';
|
|
1975
1743
|
}
|
|
1976
1744
|
get(args) {
|
|
1977
|
-
writeVarUint
|
|
1745
|
+
writeVarUint(this.encoder, this.type);
|
|
1978
1746
|
writeUpdate(this.encoder, args.update);
|
|
1979
1747
|
return this.encoder;
|
|
1980
1748
|
}
|
|
1981
1749
|
}
|
|
1982
1750
|
|
|
1983
|
-
var awarenessStatesToArray = (states) => {
|
|
1984
|
-
return Array.from(states.entries()).map(([key, value]) => {
|
|
1985
|
-
return {
|
|
1986
|
-
clientId: key,
|
|
1987
|
-
...value,
|
|
1988
|
-
};
|
|
1989
|
-
});
|
|
1990
|
-
};
|
|
1991
|
-
|
|
1992
1751
|
exports.WebSocketStatus = void 0;
|
|
1993
1752
|
(function (WebSocketStatus) {
|
|
1994
1753
|
WebSocketStatus["Connecting"] = "connecting";
|
|
@@ -1996,16 +1755,16 @@ exports.WebSocketStatus = void 0;
|
|
|
1996
1755
|
WebSocketStatus["Disconnected"] = "disconnected";
|
|
1997
1756
|
})(exports.WebSocketStatus || (exports.WebSocketStatus = {}));
|
|
1998
1757
|
class HocuspocusProvider extends EventEmitter {
|
|
1999
|
-
constructor(
|
|
1758
|
+
constructor(configuration) {
|
|
2000
1759
|
super();
|
|
2001
|
-
this.
|
|
1760
|
+
this.configuration = {
|
|
1761
|
+
name: '',
|
|
1762
|
+
url: '',
|
|
2002
1763
|
// @ts-ignore
|
|
2003
1764
|
document: undefined,
|
|
2004
1765
|
// @ts-ignore
|
|
2005
1766
|
awareness: undefined,
|
|
2006
1767
|
WebSocketPolyfill: undefined,
|
|
2007
|
-
url: '',
|
|
2008
|
-
name: '',
|
|
2009
1768
|
token: null,
|
|
2010
1769
|
parameters: {},
|
|
2011
1770
|
connect: true,
|
|
@@ -2042,6 +1801,7 @@ class HocuspocusProvider extends EventEmitter {
|
|
|
2042
1801
|
onDestroy: () => null,
|
|
2043
1802
|
onAwarenessUpdate: () => null,
|
|
2044
1803
|
onAwarenessChange: () => null,
|
|
1804
|
+
quiet: false,
|
|
2045
1805
|
};
|
|
2046
1806
|
this.subscribedToBroadcastChannel = false;
|
|
2047
1807
|
this.webSocket = null;
|
|
@@ -2056,46 +1816,46 @@ class HocuspocusProvider extends EventEmitter {
|
|
|
2056
1816
|
connectionChecker: null,
|
|
2057
1817
|
};
|
|
2058
1818
|
this.connectionAttempt = null;
|
|
2059
|
-
this.
|
|
2060
|
-
this.
|
|
2061
|
-
this.
|
|
2062
|
-
this.
|
|
2063
|
-
this.on('open', this.
|
|
2064
|
-
this.on('authenticated', this.
|
|
2065
|
-
this.on('authenticationFailed', this.
|
|
2066
|
-
this.on('connect', this.
|
|
2067
|
-
this.on('message', this.
|
|
2068
|
-
this.on('outgoingMessage', this.
|
|
2069
|
-
this.on('synced', this.
|
|
2070
|
-
this.on('status', this.
|
|
2071
|
-
this.on('disconnect', this.
|
|
2072
|
-
this.on('close', this.
|
|
2073
|
-
this.on('destroy', this.
|
|
2074
|
-
this.on('awarenessUpdate', this.
|
|
2075
|
-
this.on('awarenessChange', this.
|
|
1819
|
+
this.setConfiguration(configuration);
|
|
1820
|
+
this.configuration.document = configuration.document ? configuration.document : new Y__namespace.Doc();
|
|
1821
|
+
this.configuration.awareness = configuration.awareness ? configuration.awareness : new Awareness(this.document);
|
|
1822
|
+
this.configuration.WebSocketPolyfill = configuration.WebSocketPolyfill ? configuration.WebSocketPolyfill : WebSocket;
|
|
1823
|
+
this.on('open', this.configuration.onOpen);
|
|
1824
|
+
this.on('authenticated', this.configuration.onAuthenticated);
|
|
1825
|
+
this.on('authenticationFailed', this.configuration.onAuthenticationFailed);
|
|
1826
|
+
this.on('connect', this.configuration.onConnect);
|
|
1827
|
+
this.on('message', this.configuration.onMessage);
|
|
1828
|
+
this.on('outgoingMessage', this.configuration.onOutgoingMessage);
|
|
1829
|
+
this.on('synced', this.configuration.onSynced);
|
|
1830
|
+
this.on('status', this.configuration.onStatus);
|
|
1831
|
+
this.on('disconnect', this.configuration.onDisconnect);
|
|
1832
|
+
this.on('close', this.configuration.onClose);
|
|
1833
|
+
this.on('destroy', this.configuration.onDestroy);
|
|
1834
|
+
this.on('awarenessUpdate', this.configuration.onAwarenessUpdate);
|
|
1835
|
+
this.on('awarenessChange', this.configuration.onAwarenessChange);
|
|
2076
1836
|
this.awareness.on('update', () => {
|
|
2077
|
-
this.emit('awarenessUpdate', { states: awarenessStatesToArray(this.awareness.getStates()) });
|
|
1837
|
+
this.emit('awarenessUpdate', { states: common.awarenessStatesToArray(this.awareness.getStates()) });
|
|
2078
1838
|
});
|
|
2079
1839
|
this.awareness.on('change', () => {
|
|
2080
|
-
this.emit('awarenessChange', { states: awarenessStatesToArray(this.awareness.getStates()) });
|
|
1840
|
+
this.emit('awarenessChange', { states: common.awarenessStatesToArray(this.awareness.getStates()) });
|
|
2081
1841
|
});
|
|
2082
1842
|
this.document.on('update', this.documentUpdateHandler.bind(this));
|
|
2083
1843
|
this.awareness.on('update', this.awarenessUpdateHandler.bind(this));
|
|
2084
|
-
this.
|
|
2085
|
-
this.intervals.connectionChecker = setInterval(this.checkConnection.bind(this), this.
|
|
2086
|
-
if (this.
|
|
2087
|
-
this.intervals.forceSync = setInterval(this.forceSync.bind(this), this.
|
|
1844
|
+
this.registerEventListeners();
|
|
1845
|
+
this.intervals.connectionChecker = setInterval(this.checkConnection.bind(this), this.configuration.messageReconnectTimeout / 10);
|
|
1846
|
+
if (this.configuration.forceSyncInterval) {
|
|
1847
|
+
this.intervals.forceSync = setInterval(this.forceSync.bind(this), this.configuration.forceSyncInterval);
|
|
2088
1848
|
}
|
|
2089
|
-
if (typeof
|
|
2090
|
-
this.shouldConnect =
|
|
1849
|
+
if (typeof configuration.connect !== 'undefined') {
|
|
1850
|
+
this.shouldConnect = configuration.connect;
|
|
2091
1851
|
}
|
|
2092
1852
|
if (!this.shouldConnect) {
|
|
2093
1853
|
return;
|
|
2094
1854
|
}
|
|
2095
1855
|
this.connect();
|
|
2096
1856
|
}
|
|
2097
|
-
|
|
2098
|
-
this.
|
|
1857
|
+
setConfiguration(configuration = {}) {
|
|
1858
|
+
this.configuration = { ...this.configuration, ...configuration };
|
|
2099
1859
|
}
|
|
2100
1860
|
async connect() {
|
|
2101
1861
|
if (this.status === exports.WebSocketStatus.Connected) {
|
|
@@ -2105,14 +1865,14 @@ class HocuspocusProvider extends EventEmitter {
|
|
|
2105
1865
|
this.subscribeToBroadcastChannel();
|
|
2106
1866
|
try {
|
|
2107
1867
|
await attempt.retry(this.createWebSocketConnection.bind(this), {
|
|
2108
|
-
delay: this.
|
|
2109
|
-
initialDelay: this.
|
|
2110
|
-
factor: this.
|
|
2111
|
-
maxAttempts: this.
|
|
2112
|
-
minDelay: this.
|
|
2113
|
-
maxDelay: this.
|
|
2114
|
-
jitter: this.
|
|
2115
|
-
timeout: this.
|
|
1868
|
+
delay: this.configuration.delay,
|
|
1869
|
+
initialDelay: this.configuration.initialDelay,
|
|
1870
|
+
factor: this.configuration.factor,
|
|
1871
|
+
maxAttempts: this.configuration.maxAttempts,
|
|
1872
|
+
minDelay: this.configuration.minDelay,
|
|
1873
|
+
maxDelay: this.configuration.maxDelay,
|
|
1874
|
+
jitter: this.configuration.jitter,
|
|
1875
|
+
timeout: this.configuration.timeout,
|
|
2116
1876
|
beforeAttempt: context => {
|
|
2117
1877
|
if (!this.shouldConnect) {
|
|
2118
1878
|
context.abort();
|
|
@@ -2131,7 +1891,7 @@ class HocuspocusProvider extends EventEmitter {
|
|
|
2131
1891
|
createWebSocketConnection() {
|
|
2132
1892
|
return new Promise((resolve, reject) => {
|
|
2133
1893
|
// Init the WebSocket connection
|
|
2134
|
-
const ws = new this.
|
|
1894
|
+
const ws = new this.configuration.WebSocketPolyfill(this.url);
|
|
2135
1895
|
ws.binaryType = 'arraybuffer';
|
|
2136
1896
|
ws.onmessage = this.onMessage.bind(this);
|
|
2137
1897
|
ws.onclose = this.onClose.bind(this);
|
|
@@ -2162,10 +1922,10 @@ class HocuspocusProvider extends EventEmitter {
|
|
|
2162
1922
|
this.connectionAttempt = null;
|
|
2163
1923
|
}
|
|
2164
1924
|
get document() {
|
|
2165
|
-
return this.
|
|
1925
|
+
return this.configuration.document;
|
|
2166
1926
|
}
|
|
2167
1927
|
get awareness() {
|
|
2168
|
-
return this.
|
|
1928
|
+
return this.configuration.awareness;
|
|
2169
1929
|
}
|
|
2170
1930
|
checkConnection() {
|
|
2171
1931
|
var _a;
|
|
@@ -2178,7 +1938,7 @@ class HocuspocusProvider extends EventEmitter {
|
|
|
2178
1938
|
return;
|
|
2179
1939
|
}
|
|
2180
1940
|
// Don’t close the connection when a message was received recently
|
|
2181
|
-
if (this.
|
|
1941
|
+
if (this.configuration.messageReconnectTimeout >= getUnixTime() - this.lastMessageReceived) {
|
|
2182
1942
|
return;
|
|
2183
1943
|
}
|
|
2184
1944
|
// No message received in a long time, not even your own
|
|
@@ -2191,10 +1951,11 @@ class HocuspocusProvider extends EventEmitter {
|
|
|
2191
1951
|
}
|
|
2192
1952
|
this.send(SyncStepOneMessage, { document: this.document });
|
|
2193
1953
|
}
|
|
2194
|
-
|
|
1954
|
+
registerEventListeners() {
|
|
2195
1955
|
if (typeof window === 'undefined') {
|
|
2196
1956
|
return;
|
|
2197
1957
|
}
|
|
1958
|
+
window.addEventListener('online', this.connect.bind(this));
|
|
2198
1959
|
window.addEventListener('beforeunload', () => {
|
|
2199
1960
|
removeAwarenessStates(this.awareness, [this.document.clientID], 'window unload');
|
|
2200
1961
|
});
|
|
@@ -2224,14 +1985,14 @@ class HocuspocusProvider extends EventEmitter {
|
|
|
2224
1985
|
}
|
|
2225
1986
|
// Ensure that the URL always ends with /
|
|
2226
1987
|
get serverUrl() {
|
|
2227
|
-
while (this.
|
|
2228
|
-
return this.
|
|
1988
|
+
while (this.configuration.url[this.configuration.url.length - 1] === '/') {
|
|
1989
|
+
return this.configuration.url.slice(0, this.configuration.url.length - 1);
|
|
2229
1990
|
}
|
|
2230
|
-
return this.
|
|
1991
|
+
return this.configuration.url;
|
|
2231
1992
|
}
|
|
2232
1993
|
get url() {
|
|
2233
|
-
const encodedParams = encodeQueryParams(this.
|
|
2234
|
-
return `${this.serverUrl}/${this.
|
|
1994
|
+
const encodedParams = encodeQueryParams(this.configuration.parameters);
|
|
1995
|
+
return `${this.serverUrl}/${this.configuration.name}${encodedParams.length === 0 ? '' : `?${encodedParams}`}`;
|
|
2235
1996
|
}
|
|
2236
1997
|
get synced() {
|
|
2237
1998
|
return this.isSynced;
|
|
@@ -2245,7 +2006,7 @@ class HocuspocusProvider extends EventEmitter {
|
|
|
2245
2006
|
this.emit('sync', { state });
|
|
2246
2007
|
}
|
|
2247
2008
|
get isAuthenticationRequired() {
|
|
2248
|
-
return !!this.
|
|
2009
|
+
return !!this.configuration.token && !this.isAuthenticated;
|
|
2249
2010
|
}
|
|
2250
2011
|
disconnect() {
|
|
2251
2012
|
this.shouldConnect = false;
|
|
@@ -2267,11 +2028,11 @@ class HocuspocusProvider extends EventEmitter {
|
|
|
2267
2028
|
}
|
|
2268
2029
|
}
|
|
2269
2030
|
async getToken() {
|
|
2270
|
-
if (typeof this.
|
|
2271
|
-
const token = await this.
|
|
2031
|
+
if (typeof this.configuration.token === 'function') {
|
|
2032
|
+
const token = await this.configuration.token();
|
|
2272
2033
|
return token;
|
|
2273
2034
|
}
|
|
2274
|
-
return this.
|
|
2035
|
+
return this.configuration.token;
|
|
2275
2036
|
}
|
|
2276
2037
|
async webSocketConnectionEstablished() {
|
|
2277
2038
|
this.status = exports.WebSocketStatus.Connected;
|
|
@@ -2323,15 +2084,26 @@ class HocuspocusProvider extends EventEmitter {
|
|
|
2323
2084
|
this.emit('status', { status: 'disconnected' });
|
|
2324
2085
|
this.emit('disconnect', { event });
|
|
2325
2086
|
}
|
|
2087
|
+
if (event.code === common.Unauthorized.code) {
|
|
2088
|
+
if (!this.configuration.quiet) {
|
|
2089
|
+
console.warn('[HocuspocusProvider] An authentication token is required, but you didn’t send one. Try adding a `token` to your HocuspocusProvider configuration. Won’t try again.');
|
|
2090
|
+
}
|
|
2091
|
+
this.shouldConnect = false;
|
|
2092
|
+
}
|
|
2093
|
+
if (event.code === common.Forbidden.code) {
|
|
2094
|
+
if (!this.configuration.quiet) {
|
|
2095
|
+
console.warn('[HocuspocusProvider] The provided authentication token isn’t allowed to connect to this server. Will try again.');
|
|
2096
|
+
}
|
|
2097
|
+
}
|
|
2326
2098
|
if (this.connectionAttempt) {
|
|
2327
|
-
//
|
|
2099
|
+
// That connection attempt failed.
|
|
2328
2100
|
this.rejectConnectionAttempt();
|
|
2329
2101
|
}
|
|
2330
2102
|
else if (this.shouldConnect) {
|
|
2331
|
-
// The connection was closed by the server
|
|
2103
|
+
// The connection was closed by the server. Let’s just try again.
|
|
2332
2104
|
this.connect();
|
|
2333
2105
|
}
|
|
2334
|
-
// If we’ll reconnect
|
|
2106
|
+
// If we’ll reconnect, we’re done for now.
|
|
2335
2107
|
if (this.shouldConnect) {
|
|
2336
2108
|
return;
|
|
2337
2109
|
}
|
|
@@ -2359,9 +2131,13 @@ class HocuspocusProvider extends EventEmitter {
|
|
|
2359
2131
|
this.awareness.off('update', this.awarenessUpdateHandler);
|
|
2360
2132
|
this.document.off('update', this.documentUpdateHandler);
|
|
2361
2133
|
this.removeAllListeners();
|
|
2134
|
+
if (typeof window === 'undefined') {
|
|
2135
|
+
return;
|
|
2136
|
+
}
|
|
2137
|
+
window.removeEventListener('online', this.connect.bind(this));
|
|
2362
2138
|
}
|
|
2363
2139
|
get broadcastChannel() {
|
|
2364
|
-
return `${this.serverUrl}/${this.
|
|
2140
|
+
return `${this.serverUrl}/${this.configuration.name}`;
|
|
2365
2141
|
}
|
|
2366
2142
|
broadcastChannelSubscriber(data) {
|
|
2367
2143
|
this.mux(() => {
|
|
@@ -2396,7 +2172,7 @@ class HocuspocusProvider extends EventEmitter {
|
|
|
2396
2172
|
}
|
|
2397
2173
|
}
|
|
2398
2174
|
broadcast(Message, args) {
|
|
2399
|
-
if (!this.
|
|
2175
|
+
if (!this.configuration.broadcast) {
|
|
2400
2176
|
return;
|
|
2401
2177
|
}
|
|
2402
2178
|
if (!this.subscribedToBroadcastChannel) {
|
|
@@ -2410,21 +2186,20 @@ class HocuspocusProvider extends EventEmitter {
|
|
|
2410
2186
|
}
|
|
2411
2187
|
|
|
2412
2188
|
class HocuspocusCloudProvider extends HocuspocusProvider {
|
|
2413
|
-
constructor(
|
|
2414
|
-
if (!
|
|
2415
|
-
|
|
2189
|
+
constructor(configuration) {
|
|
2190
|
+
if (!configuration.url) {
|
|
2191
|
+
configuration.url = 'wss://connect.hocuspocus.cloud';
|
|
2416
2192
|
}
|
|
2417
|
-
if (
|
|
2418
|
-
if (!
|
|
2419
|
-
|
|
2193
|
+
if (configuration.key) {
|
|
2194
|
+
if (!configuration.parameters) {
|
|
2195
|
+
configuration.parameters = {};
|
|
2420
2196
|
}
|
|
2421
|
-
|
|
2197
|
+
configuration.parameters.key = configuration.key;
|
|
2422
2198
|
}
|
|
2423
|
-
super(
|
|
2199
|
+
super(configuration);
|
|
2424
2200
|
}
|
|
2425
2201
|
}
|
|
2426
2202
|
|
|
2427
2203
|
exports.HocuspocusCloudProvider = HocuspocusCloudProvider;
|
|
2428
2204
|
exports.HocuspocusProvider = HocuspocusProvider;
|
|
2429
|
-
exports.awarenessStatesToArray = awarenessStatesToArray;
|
|
2430
2205
|
//# sourceMappingURL=hocuspocus-provider.cjs.map
|