@hocuspocus/provider 1.0.0-alpha.25 → 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.
Files changed (31) hide show
  1. package/dist/hocuspocus-provider.cjs +132 -330
  2. package/dist/hocuspocus-provider.cjs.map +1 -1
  3. package/dist/hocuspocus-provider.esm.js +130 -329
  4. package/dist/hocuspocus-provider.esm.js.map +1 -1
  5. package/dist/packages/common/src/CloseEvents.d.ts +23 -0
  6. package/dist/packages/common/src/index.d.ts +1 -0
  7. package/dist/packages/extension-database/src/Database.d.ts +36 -0
  8. package/dist/packages/extension-database/src/index.d.ts +1 -0
  9. package/dist/packages/extension-logger/src/Logger.d.ts +2 -8
  10. package/dist/packages/extension-sqlite/src/SQLite.d.ts +26 -0
  11. package/dist/packages/extension-sqlite/src/index.d.ts +1 -0
  12. package/dist/packages/extension-webhook/src/index.d.ts +1 -1
  13. package/dist/packages/provider/src/HocuspocusCloudProvider.d.ts +11 -0
  14. package/dist/packages/provider/src/HocuspocusProvider.d.ts +10 -5
  15. package/dist/packages/provider/src/index.d.ts +1 -0
  16. package/dist/packages/server/src/Connection.d.ts +1 -1
  17. package/dist/packages/server/src/Hocuspocus.d.ts +15 -5
  18. package/dist/packages/server/src/types.d.ts +17 -8
  19. package/dist/{demos/backend/src/express.d.ts → playground/backend/src/default.d.ts} +0 -0
  20. package/dist/{demos/backend/src/koa.d.ts → playground/backend/src/express.d.ts} +0 -0
  21. package/dist/{demos/backend/src/load-document.d.ts → playground/backend/src/koa.d.ts} +0 -0
  22. package/dist/{demos/backend/src/minimal.d.ts → playground/backend/src/load-document.d.ts} +0 -0
  23. package/dist/{demos → playground}/backend/src/monitor.d.ts +0 -0
  24. package/dist/{demos → playground}/backend/src/redis.d.ts +0 -0
  25. package/dist/{demos → playground}/backend/src/slow.d.ts +0 -0
  26. package/dist/{demos → playground}/backend/src/webhook.d.ts +0 -0
  27. package/package.json +6 -8
  28. package/src/HocuspocusCloudProvider.ts +34 -0
  29. package/src/HocuspocusProvider.ts +95 -63
  30. package/src/index.ts +1 -0
  31. 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$1 = 128;
305
- const BITS7$1 = 127;
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$1 = (encoder, num) => {
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$1 = (encoder, num) => {
416
- while (num > BITS7$1) {
417
- write$1(encoder, BIT8$1 | (BITS7$1 & num));
416
+ const writeVarUint = (encoder, num) => {
417
+ while (num > BITS7) {
418
+ write(encoder, BIT8 | (BITS7 & num));
418
419
  num >>>= 7;
419
420
  }
420
- write$1(encoder, BITS7$1 & num);
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$1 = (encoder, str) => {
431
+ const writeVarString = (encoder, str) => {
431
432
  const encodedString = unescape(encodeURIComponent(str));
432
433
  const len = encodedString.length;
433
- writeVarUint$1(encoder, len);
434
+ writeVarUint(encoder, len);
434
435
  for (let i = 0; i < len; i++) {
435
- write$1(encoder, /** @type {number} */ (encodedString.codePointAt(i)));
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$1(encoder, uint8Array.byteLength);
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$1(decoder));
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$1 = decoder => decoder.arr[decoder.pos++];
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$1 = decoder => {
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$1) << len);
588
+ num = num | ((r & BITS7) << len);
588
589
  len += 7;
589
- if (r < BIT8$1) {
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$1 = decoder => {
613
- let remainingLen = readVarUint$1(decoder);
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$1(decoder)); // remember to decrease remainingLen
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$1(decoder));
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$1(encoder, len);
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$1(encoder, clientID);
1239
- writeVarUint$1(encoder, clock);
1240
- writeVarString$1(encoder, JSON.stringify(state));
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$1(decoder);
1258
+ const len = readVarUint(decoder);
1258
1259
  for (let i = 0; i < len; i++) {
1259
- const clientID = readVarUint$1(decoder);
1260
- let clock = readVarUint$1(decoder);
1261
- const state = JSON.parse(readVarString$1(decoder));
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$1(this.decoder);
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$1(this.encoder, type);
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$1(encoder, messageYjsSyncStep1);
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$1(encoder, messageYjsSyncStep2);
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$1(encoder, messageYjsUpdate);
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$1(decoder);
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$1(this.encoder, this.type);
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$1(this.encoder, this.type);
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$1(this.encoder, this.type);
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$1(this.encoder, this.type);
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$1(this.encoder, this.type);
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$1(this.encoder, this.type);
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(options = {}) {
1767
+ constructor(configuration) {
2000
1768
  super();
2001
- this.options = {
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,22 +1825,23 @@ class HocuspocusProvider extends EventEmitter {
2056
1825
  connectionChecker: null,
2057
1826
  };
2058
1827
  this.connectionAttempt = null;
2059
- this.setOptions(options);
2060
- this.options.awareness = options.awareness ? options.awareness : new Awareness(this.document);
2061
- this.options.WebSocketPolyfill = options.WebSocketPolyfill ? options.WebSocketPolyfill : WebSocket;
2062
- this.on('open', this.options.onOpen);
2063
- this.on('authenticated', this.options.onAuthenticated);
2064
- this.on('authenticationFailed', this.options.onAuthenticationFailed);
2065
- this.on('connect', this.options.onConnect);
2066
- this.on('message', this.options.onMessage);
2067
- this.on('outgoingMessage', this.options.onOutgoingMessage);
2068
- this.on('synced', this.options.onSynced);
2069
- this.on('status', this.options.onStatus);
2070
- this.on('disconnect', this.options.onDisconnect);
2071
- this.on('close', this.options.onClose);
2072
- this.on('destroy', this.options.onDestroy);
2073
- this.on('awarenessUpdate', this.options.onAwarenessUpdate);
2074
- this.on('awarenessChange', this.options.onAwarenessChange);
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);
2075
1845
  this.awareness.on('update', () => {
2076
1846
  this.emit('awarenessUpdate', { states: awarenessStatesToArray(this.awareness.getStates()) });
2077
1847
  });
@@ -2080,21 +1850,21 @@ class HocuspocusProvider extends EventEmitter {
2080
1850
  });
2081
1851
  this.document.on('update', this.documentUpdateHandler.bind(this));
2082
1852
  this.awareness.on('update', this.awarenessUpdateHandler.bind(this));
2083
- this.registerBeforeUnloadEventListener();
2084
- this.intervals.connectionChecker = setInterval(this.checkConnection.bind(this), this.options.messageReconnectTimeout / 10);
2085
- if (this.options.forceSyncInterval) {
2086
- this.intervals.forceSync = setInterval(this.forceSync.bind(this), this.options.forceSyncInterval);
1853
+ this.registerEventListeners();
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);
2087
1857
  }
2088
- if (typeof options.connect !== 'undefined') {
2089
- this.shouldConnect = options.connect;
1858
+ if (typeof configuration.connect !== 'undefined') {
1859
+ this.shouldConnect = configuration.connect;
2090
1860
  }
2091
1861
  if (!this.shouldConnect) {
2092
1862
  return;
2093
1863
  }
2094
1864
  this.connect();
2095
1865
  }
2096
- setOptions(options = {}) {
2097
- this.options = { ...this.options, ...options };
1866
+ setConfiguration(configuration = {}) {
1867
+ this.configuration = { ...this.configuration, ...configuration };
2098
1868
  }
2099
1869
  async connect() {
2100
1870
  if (this.status === exports.WebSocketStatus.Connected) {
@@ -2104,14 +1874,14 @@ class HocuspocusProvider extends EventEmitter {
2104
1874
  this.subscribeToBroadcastChannel();
2105
1875
  try {
2106
1876
  await attempt.retry(this.createWebSocketConnection.bind(this), {
2107
- delay: this.options.delay,
2108
- initialDelay: this.options.initialDelay,
2109
- factor: this.options.factor,
2110
- maxAttempts: this.options.maxAttempts,
2111
- minDelay: this.options.minDelay,
2112
- maxDelay: this.options.maxDelay,
2113
- jitter: this.options.jitter,
2114
- timeout: this.options.timeout,
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,
2115
1885
  beforeAttempt: context => {
2116
1886
  if (!this.shouldConnect) {
2117
1887
  context.abort();
@@ -2130,7 +1900,7 @@ class HocuspocusProvider extends EventEmitter {
2130
1900
  createWebSocketConnection() {
2131
1901
  return new Promise((resolve, reject) => {
2132
1902
  // Init the WebSocket connection
2133
- const ws = new this.options.WebSocketPolyfill(this.url);
1903
+ const ws = new this.configuration.WebSocketPolyfill(this.url);
2134
1904
  ws.binaryType = 'arraybuffer';
2135
1905
  ws.onmessage = this.onMessage.bind(this);
2136
1906
  ws.onclose = this.onClose.bind(this);
@@ -2161,10 +1931,10 @@ class HocuspocusProvider extends EventEmitter {
2161
1931
  this.connectionAttempt = null;
2162
1932
  }
2163
1933
  get document() {
2164
- return this.options.document;
1934
+ return this.configuration.document;
2165
1935
  }
2166
1936
  get awareness() {
2167
- return this.options.awareness;
1937
+ return this.configuration.awareness;
2168
1938
  }
2169
1939
  checkConnection() {
2170
1940
  var _a;
@@ -2177,7 +1947,7 @@ class HocuspocusProvider extends EventEmitter {
2177
1947
  return;
2178
1948
  }
2179
1949
  // Don’t close the connection when a message was received recently
2180
- if (this.options.messageReconnectTimeout >= getUnixTime() - this.lastMessageReceived) {
1950
+ if (this.configuration.messageReconnectTimeout >= getUnixTime() - this.lastMessageReceived) {
2181
1951
  return;
2182
1952
  }
2183
1953
  // No message received in a long time, not even your own
@@ -2190,10 +1960,11 @@ class HocuspocusProvider extends EventEmitter {
2190
1960
  }
2191
1961
  this.send(SyncStepOneMessage, { document: this.document });
2192
1962
  }
2193
- registerBeforeUnloadEventListener() {
1963
+ registerEventListeners() {
2194
1964
  if (typeof window === 'undefined') {
2195
1965
  return;
2196
1966
  }
1967
+ window.addEventListener('online', this.connect.bind(this));
2197
1968
  window.addEventListener('beforeunload', () => {
2198
1969
  removeAwarenessStates(this.awareness, [this.document.clientID], 'window unload');
2199
1970
  });
@@ -2223,14 +1994,14 @@ class HocuspocusProvider extends EventEmitter {
2223
1994
  }
2224
1995
  // Ensure that the URL always ends with /
2225
1996
  get serverUrl() {
2226
- while (this.options.url[this.options.url.length - 1] === '/') {
2227
- return this.options.url.slice(0, this.options.url.length - 1);
1997
+ while (this.configuration.url[this.configuration.url.length - 1] === '/') {
1998
+ return this.configuration.url.slice(0, this.configuration.url.length - 1);
2228
1999
  }
2229
- return this.options.url;
2000
+ return this.configuration.url;
2230
2001
  }
2231
2002
  get url() {
2232
- const encodedParams = encodeQueryParams(this.options.parameters);
2233
- return `${this.serverUrl}/${this.options.name}${encodedParams.length === 0 ? '' : `?${encodedParams}`}`;
2003
+ const encodedParams = encodeQueryParams(this.configuration.parameters);
2004
+ return `${this.serverUrl}/${this.configuration.name}${encodedParams.length === 0 ? '' : `?${encodedParams}`}`;
2234
2005
  }
2235
2006
  get synced() {
2236
2007
  return this.isSynced;
@@ -2244,7 +2015,7 @@ class HocuspocusProvider extends EventEmitter {
2244
2015
  this.emit('sync', { state });
2245
2016
  }
2246
2017
  get isAuthenticationRequired() {
2247
- return !!this.options.token && !this.isAuthenticated;
2018
+ return !!this.configuration.token && !this.isAuthenticated;
2248
2019
  }
2249
2020
  disconnect() {
2250
2021
  this.shouldConnect = false;
@@ -2266,11 +2037,11 @@ class HocuspocusProvider extends EventEmitter {
2266
2037
  }
2267
2038
  }
2268
2039
  async getToken() {
2269
- if (typeof this.options.token === 'function') {
2270
- const token = await this.options.token();
2040
+ if (typeof this.configuration.token === 'function') {
2041
+ const token = await this.configuration.token();
2271
2042
  return token;
2272
2043
  }
2273
- return this.options.token;
2044
+ return this.configuration.token;
2274
2045
  }
2275
2046
  async webSocketConnectionEstablished() {
2276
2047
  this.status = exports.WebSocketStatus.Connected;
@@ -2322,15 +2093,26 @@ class HocuspocusProvider extends EventEmitter {
2322
2093
  this.emit('status', { status: 'disconnected' });
2323
2094
  this.emit('disconnect', { event });
2324
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
+ }
2325
2107
  if (this.connectionAttempt) {
2326
- // Okay, that connection attempt failed
2108
+ // That connection attempt failed.
2327
2109
  this.rejectConnectionAttempt();
2328
2110
  }
2329
2111
  else if (this.shouldConnect) {
2330
- // The connection was closed by the server, so let’s just try again.
2112
+ // The connection was closed by the server. Let’s just try again.
2331
2113
  this.connect();
2332
2114
  }
2333
- // If we’ll reconnect anyway, we’re done for now.
2115
+ // If we’ll reconnect, we’re done for now.
2334
2116
  if (this.shouldConnect) {
2335
2117
  return;
2336
2118
  }
@@ -2358,9 +2140,13 @@ class HocuspocusProvider extends EventEmitter {
2358
2140
  this.awareness.off('update', this.awarenessUpdateHandler);
2359
2141
  this.document.off('update', this.documentUpdateHandler);
2360
2142
  this.removeAllListeners();
2143
+ if (typeof window === 'undefined') {
2144
+ return;
2145
+ }
2146
+ window.removeEventListener('online', this.connect.bind(this));
2361
2147
  }
2362
2148
  get broadcastChannel() {
2363
- return `${this.serverUrl}/${this.options.name}`;
2149
+ return `${this.serverUrl}/${this.configuration.name}`;
2364
2150
  }
2365
2151
  broadcastChannelSubscriber(data) {
2366
2152
  this.mux(() => {
@@ -2395,7 +2181,7 @@ class HocuspocusProvider extends EventEmitter {
2395
2181
  }
2396
2182
  }
2397
2183
  broadcast(Message, args) {
2398
- if (!this.options.broadcast) {
2184
+ if (!this.configuration.broadcast) {
2399
2185
  return;
2400
2186
  }
2401
2187
  if (!this.subscribedToBroadcastChannel) {
@@ -2408,6 +2194,22 @@ class HocuspocusProvider extends EventEmitter {
2408
2194
  }
2409
2195
  }
2410
2196
 
2197
+ class HocuspocusCloudProvider extends HocuspocusProvider {
2198
+ constructor(configuration) {
2199
+ if (!configuration.url) {
2200
+ configuration.url = 'wss://connect.hocuspocus.cloud';
2201
+ }
2202
+ if (configuration.key) {
2203
+ if (!configuration.parameters) {
2204
+ configuration.parameters = {};
2205
+ }
2206
+ configuration.parameters.key = configuration.key;
2207
+ }
2208
+ super(configuration);
2209
+ }
2210
+ }
2211
+
2212
+ exports.HocuspocusCloudProvider = HocuspocusCloudProvider;
2411
2213
  exports.HocuspocusProvider = HocuspocusProvider;
2412
2214
  exports.awarenessStatesToArray = awarenessStatesToArray;
2413
2215
  //# sourceMappingURL=hocuspocus-provider.cjs.map