@hocuspocus/provider 1.0.0-alpha.28 → 1.0.0-alpha.29
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 +117 -337
- package/dist/hocuspocus-provider.cjs.map +1 -1
- package/dist/hocuspocus-provider.esm.js +115 -335
- package/dist/hocuspocus-provider.esm.js.map +1 -1
- package/dist/packages/common/src/CloseEvents.d.ts +23 -0
- package/dist/packages/common/src/index.d.ts +1 -0
- package/dist/packages/provider/src/HocuspocusCloudProvider.d.ts +4 -3
- package/dist/packages/provider/src/HocuspocusProvider.d.ts +9 -4
- package/dist/packages/server/src/Connection.d.ts +1 -1
- package/dist/packages/server/src/Hocuspocus.d.ts +9 -2
- package/dist/packages/server/src/types.d.ts +5 -8
- package/package.json +3 -2
- package/src/HocuspocusCloudProvider.ts +18 -10
- package/src/HocuspocusProvider.ts +86 -62
- package/dist/packages/server/src/CloseEvents.d.ts +0 -4
|
@@ -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,7 +1742,7 @@ 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
|
}
|
|
@@ -1996,16 +1764,16 @@ exports.WebSocketStatus = void 0;
|
|
|
1996
1764
|
WebSocketStatus["Disconnected"] = "disconnected";
|
|
1997
1765
|
})(exports.WebSocketStatus || (exports.WebSocketStatus = {}));
|
|
1998
1766
|
class HocuspocusProvider extends EventEmitter {
|
|
1999
|
-
constructor(
|
|
1767
|
+
constructor(configuration) {
|
|
2000
1768
|
super();
|
|
2001
|
-
this.
|
|
1769
|
+
this.configuration = {
|
|
1770
|
+
name: '',
|
|
1771
|
+
url: '',
|
|
2002
1772
|
// @ts-ignore
|
|
2003
1773
|
document: undefined,
|
|
2004
1774
|
// @ts-ignore
|
|
2005
1775
|
awareness: undefined,
|
|
2006
1776
|
WebSocketPolyfill: undefined,
|
|
2007
|
-
url: '',
|
|
2008
|
-
name: '',
|
|
2009
1777
|
token: null,
|
|
2010
1778
|
parameters: {},
|
|
2011
1779
|
connect: true,
|
|
@@ -2042,6 +1810,7 @@ class HocuspocusProvider extends EventEmitter {
|
|
|
2042
1810
|
onDestroy: () => null,
|
|
2043
1811
|
onAwarenessUpdate: () => null,
|
|
2044
1812
|
onAwarenessChange: () => null,
|
|
1813
|
+
quiet: false,
|
|
2045
1814
|
};
|
|
2046
1815
|
this.subscribedToBroadcastChannel = false;
|
|
2047
1816
|
this.webSocket = null;
|
|
@@ -2056,23 +1825,23 @@ class HocuspocusProvider extends EventEmitter {
|
|
|
2056
1825
|
connectionChecker: null,
|
|
2057
1826
|
};
|
|
2058
1827
|
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.
|
|
1828
|
+
this.setConfiguration(configuration);
|
|
1829
|
+
this.configuration.document = configuration.document ? configuration.document : new Y__namespace.Doc();
|
|
1830
|
+
this.configuration.awareness = configuration.awareness ? configuration.awareness : new Awareness(this.document);
|
|
1831
|
+
this.configuration.WebSocketPolyfill = configuration.WebSocketPolyfill ? configuration.WebSocketPolyfill : WebSocket;
|
|
1832
|
+
this.on('open', this.configuration.onOpen);
|
|
1833
|
+
this.on('authenticated', this.configuration.onAuthenticated);
|
|
1834
|
+
this.on('authenticationFailed', this.configuration.onAuthenticationFailed);
|
|
1835
|
+
this.on('connect', this.configuration.onConnect);
|
|
1836
|
+
this.on('message', this.configuration.onMessage);
|
|
1837
|
+
this.on('outgoingMessage', this.configuration.onOutgoingMessage);
|
|
1838
|
+
this.on('synced', this.configuration.onSynced);
|
|
1839
|
+
this.on('status', this.configuration.onStatus);
|
|
1840
|
+
this.on('disconnect', this.configuration.onDisconnect);
|
|
1841
|
+
this.on('close', this.configuration.onClose);
|
|
1842
|
+
this.on('destroy', this.configuration.onDestroy);
|
|
1843
|
+
this.on('awarenessUpdate', this.configuration.onAwarenessUpdate);
|
|
1844
|
+
this.on('awarenessChange', this.configuration.onAwarenessChange);
|
|
2076
1845
|
this.awareness.on('update', () => {
|
|
2077
1846
|
this.emit('awarenessUpdate', { states: awarenessStatesToArray(this.awareness.getStates()) });
|
|
2078
1847
|
});
|
|
@@ -2082,20 +1851,20 @@ class HocuspocusProvider extends EventEmitter {
|
|
|
2082
1851
|
this.document.on('update', this.documentUpdateHandler.bind(this));
|
|
2083
1852
|
this.awareness.on('update', this.awarenessUpdateHandler.bind(this));
|
|
2084
1853
|
this.registerEventListeners();
|
|
2085
|
-
this.intervals.connectionChecker = setInterval(this.checkConnection.bind(this), this.
|
|
2086
|
-
if (this.
|
|
2087
|
-
this.intervals.forceSync = setInterval(this.forceSync.bind(this), this.
|
|
1854
|
+
this.intervals.connectionChecker = setInterval(this.checkConnection.bind(this), this.configuration.messageReconnectTimeout / 10);
|
|
1855
|
+
if (this.configuration.forceSyncInterval) {
|
|
1856
|
+
this.intervals.forceSync = setInterval(this.forceSync.bind(this), this.configuration.forceSyncInterval);
|
|
2088
1857
|
}
|
|
2089
|
-
if (typeof
|
|
2090
|
-
this.shouldConnect =
|
|
1858
|
+
if (typeof configuration.connect !== 'undefined') {
|
|
1859
|
+
this.shouldConnect = configuration.connect;
|
|
2091
1860
|
}
|
|
2092
1861
|
if (!this.shouldConnect) {
|
|
2093
1862
|
return;
|
|
2094
1863
|
}
|
|
2095
1864
|
this.connect();
|
|
2096
1865
|
}
|
|
2097
|
-
|
|
2098
|
-
this.
|
|
1866
|
+
setConfiguration(configuration = {}) {
|
|
1867
|
+
this.configuration = { ...this.configuration, ...configuration };
|
|
2099
1868
|
}
|
|
2100
1869
|
async connect() {
|
|
2101
1870
|
if (this.status === exports.WebSocketStatus.Connected) {
|
|
@@ -2105,14 +1874,14 @@ class HocuspocusProvider extends EventEmitter {
|
|
|
2105
1874
|
this.subscribeToBroadcastChannel();
|
|
2106
1875
|
try {
|
|
2107
1876
|
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.
|
|
1877
|
+
delay: this.configuration.delay,
|
|
1878
|
+
initialDelay: this.configuration.initialDelay,
|
|
1879
|
+
factor: this.configuration.factor,
|
|
1880
|
+
maxAttempts: this.configuration.maxAttempts,
|
|
1881
|
+
minDelay: this.configuration.minDelay,
|
|
1882
|
+
maxDelay: this.configuration.maxDelay,
|
|
1883
|
+
jitter: this.configuration.jitter,
|
|
1884
|
+
timeout: this.configuration.timeout,
|
|
2116
1885
|
beforeAttempt: context => {
|
|
2117
1886
|
if (!this.shouldConnect) {
|
|
2118
1887
|
context.abort();
|
|
@@ -2131,7 +1900,7 @@ class HocuspocusProvider extends EventEmitter {
|
|
|
2131
1900
|
createWebSocketConnection() {
|
|
2132
1901
|
return new Promise((resolve, reject) => {
|
|
2133
1902
|
// Init the WebSocket connection
|
|
2134
|
-
const ws = new this.
|
|
1903
|
+
const ws = new this.configuration.WebSocketPolyfill(this.url);
|
|
2135
1904
|
ws.binaryType = 'arraybuffer';
|
|
2136
1905
|
ws.onmessage = this.onMessage.bind(this);
|
|
2137
1906
|
ws.onclose = this.onClose.bind(this);
|
|
@@ -2162,10 +1931,10 @@ class HocuspocusProvider extends EventEmitter {
|
|
|
2162
1931
|
this.connectionAttempt = null;
|
|
2163
1932
|
}
|
|
2164
1933
|
get document() {
|
|
2165
|
-
return this.
|
|
1934
|
+
return this.configuration.document;
|
|
2166
1935
|
}
|
|
2167
1936
|
get awareness() {
|
|
2168
|
-
return this.
|
|
1937
|
+
return this.configuration.awareness;
|
|
2169
1938
|
}
|
|
2170
1939
|
checkConnection() {
|
|
2171
1940
|
var _a;
|
|
@@ -2178,7 +1947,7 @@ class HocuspocusProvider extends EventEmitter {
|
|
|
2178
1947
|
return;
|
|
2179
1948
|
}
|
|
2180
1949
|
// Don’t close the connection when a message was received recently
|
|
2181
|
-
if (this.
|
|
1950
|
+
if (this.configuration.messageReconnectTimeout >= getUnixTime() - this.lastMessageReceived) {
|
|
2182
1951
|
return;
|
|
2183
1952
|
}
|
|
2184
1953
|
// No message received in a long time, not even your own
|
|
@@ -2225,14 +1994,14 @@ class HocuspocusProvider extends EventEmitter {
|
|
|
2225
1994
|
}
|
|
2226
1995
|
// Ensure that the URL always ends with /
|
|
2227
1996
|
get serverUrl() {
|
|
2228
|
-
while (this.
|
|
2229
|
-
return this.
|
|
1997
|
+
while (this.configuration.url[this.configuration.url.length - 1] === '/') {
|
|
1998
|
+
return this.configuration.url.slice(0, this.configuration.url.length - 1);
|
|
2230
1999
|
}
|
|
2231
|
-
return this.
|
|
2000
|
+
return this.configuration.url;
|
|
2232
2001
|
}
|
|
2233
2002
|
get url() {
|
|
2234
|
-
const encodedParams = encodeQueryParams(this.
|
|
2235
|
-
return `${this.serverUrl}/${this.
|
|
2003
|
+
const encodedParams = encodeQueryParams(this.configuration.parameters);
|
|
2004
|
+
return `${this.serverUrl}/${this.configuration.name}${encodedParams.length === 0 ? '' : `?${encodedParams}`}`;
|
|
2236
2005
|
}
|
|
2237
2006
|
get synced() {
|
|
2238
2007
|
return this.isSynced;
|
|
@@ -2246,7 +2015,7 @@ class HocuspocusProvider extends EventEmitter {
|
|
|
2246
2015
|
this.emit('sync', { state });
|
|
2247
2016
|
}
|
|
2248
2017
|
get isAuthenticationRequired() {
|
|
2249
|
-
return !!this.
|
|
2018
|
+
return !!this.configuration.token && !this.isAuthenticated;
|
|
2250
2019
|
}
|
|
2251
2020
|
disconnect() {
|
|
2252
2021
|
this.shouldConnect = false;
|
|
@@ -2268,11 +2037,11 @@ class HocuspocusProvider extends EventEmitter {
|
|
|
2268
2037
|
}
|
|
2269
2038
|
}
|
|
2270
2039
|
async getToken() {
|
|
2271
|
-
if (typeof this.
|
|
2272
|
-
const token = await this.
|
|
2040
|
+
if (typeof this.configuration.token === 'function') {
|
|
2041
|
+
const token = await this.configuration.token();
|
|
2273
2042
|
return token;
|
|
2274
2043
|
}
|
|
2275
|
-
return this.
|
|
2044
|
+
return this.configuration.token;
|
|
2276
2045
|
}
|
|
2277
2046
|
async webSocketConnectionEstablished() {
|
|
2278
2047
|
this.status = exports.WebSocketStatus.Connected;
|
|
@@ -2324,15 +2093,26 @@ class HocuspocusProvider extends EventEmitter {
|
|
|
2324
2093
|
this.emit('status', { status: 'disconnected' });
|
|
2325
2094
|
this.emit('disconnect', { event });
|
|
2326
2095
|
}
|
|
2096
|
+
if (event.code === common.Unauthorized.code) {
|
|
2097
|
+
if (!this.configuration.quiet) {
|
|
2098
|
+
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.');
|
|
2099
|
+
}
|
|
2100
|
+
this.shouldConnect = false;
|
|
2101
|
+
}
|
|
2102
|
+
if (event.code === common.Forbidden.code) {
|
|
2103
|
+
if (!this.configuration.quiet) {
|
|
2104
|
+
console.warn('[HocuspocusProvider] The provided authentication token isn’t allowed to connect to this server. Will try again.');
|
|
2105
|
+
}
|
|
2106
|
+
}
|
|
2327
2107
|
if (this.connectionAttempt) {
|
|
2328
|
-
//
|
|
2108
|
+
// That connection attempt failed.
|
|
2329
2109
|
this.rejectConnectionAttempt();
|
|
2330
2110
|
}
|
|
2331
2111
|
else if (this.shouldConnect) {
|
|
2332
|
-
// The connection was closed by the server
|
|
2112
|
+
// The connection was closed by the server. Let’s just try again.
|
|
2333
2113
|
this.connect();
|
|
2334
2114
|
}
|
|
2335
|
-
// If we’ll reconnect
|
|
2115
|
+
// If we’ll reconnect, we’re done for now.
|
|
2336
2116
|
if (this.shouldConnect) {
|
|
2337
2117
|
return;
|
|
2338
2118
|
}
|
|
@@ -2366,7 +2146,7 @@ class HocuspocusProvider extends EventEmitter {
|
|
|
2366
2146
|
window.removeEventListener('online', this.connect.bind(this));
|
|
2367
2147
|
}
|
|
2368
2148
|
get broadcastChannel() {
|
|
2369
|
-
return `${this.serverUrl}/${this.
|
|
2149
|
+
return `${this.serverUrl}/${this.configuration.name}`;
|
|
2370
2150
|
}
|
|
2371
2151
|
broadcastChannelSubscriber(data) {
|
|
2372
2152
|
this.mux(() => {
|
|
@@ -2401,7 +2181,7 @@ class HocuspocusProvider extends EventEmitter {
|
|
|
2401
2181
|
}
|
|
2402
2182
|
}
|
|
2403
2183
|
broadcast(Message, args) {
|
|
2404
|
-
if (!this.
|
|
2184
|
+
if (!this.configuration.broadcast) {
|
|
2405
2185
|
return;
|
|
2406
2186
|
}
|
|
2407
2187
|
if (!this.subscribedToBroadcastChannel) {
|
|
@@ -2415,17 +2195,17 @@ class HocuspocusProvider extends EventEmitter {
|
|
|
2415
2195
|
}
|
|
2416
2196
|
|
|
2417
2197
|
class HocuspocusCloudProvider extends HocuspocusProvider {
|
|
2418
|
-
constructor(
|
|
2419
|
-
if (!
|
|
2420
|
-
|
|
2198
|
+
constructor(configuration) {
|
|
2199
|
+
if (!configuration.url) {
|
|
2200
|
+
configuration.url = 'wss://connect.hocuspocus.cloud';
|
|
2421
2201
|
}
|
|
2422
|
-
if (
|
|
2423
|
-
if (!
|
|
2424
|
-
|
|
2202
|
+
if (configuration.key) {
|
|
2203
|
+
if (!configuration.parameters) {
|
|
2204
|
+
configuration.parameters = {};
|
|
2425
2205
|
}
|
|
2426
|
-
|
|
2206
|
+
configuration.parameters.key = configuration.key;
|
|
2427
2207
|
}
|
|
2428
|
-
super(
|
|
2208
|
+
super(configuration);
|
|
2429
2209
|
}
|
|
2430
2210
|
}
|
|
2431
2211
|
|