@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.
Files changed (99) hide show
  1. package/dist/hocuspocus-provider.cjs +126 -351
  2. package/dist/hocuspocus-provider.cjs.map +1 -1
  3. package/dist/hocuspocus-provider.esm.js +123 -347
  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/awarenessStatesToArray.d.ts +3 -0
  7. package/dist/packages/common/src/index.d.ts +2 -0
  8. package/dist/packages/extension-database/src/Database.d.ts +32 -0
  9. package/dist/packages/extension-database/src/index.d.ts +1 -0
  10. package/dist/packages/extension-logger/src/Logger.d.ts +5 -0
  11. package/dist/packages/extension-sqlite/src/SQLite.d.ts +26 -0
  12. package/dist/packages/extension-sqlite/src/index.d.ts +1 -0
  13. package/dist/packages/extension-throttle/src/index.d.ts +6 -2
  14. package/dist/packages/extension-webhook/src/index.d.ts +2 -3
  15. package/dist/packages/provider/src/EventEmitter.d.ts +1 -1
  16. package/dist/packages/provider/src/HocuspocusCloudProvider.d.ts +4 -3
  17. package/dist/packages/provider/src/HocuspocusProvider.d.ts +17 -9
  18. package/dist/packages/provider/src/index.d.ts +0 -1
  19. package/dist/packages/provider/src/types.d.ts +10 -0
  20. package/dist/packages/server/src/Connection.d.ts +5 -5
  21. package/dist/packages/server/src/Debugger.d.ts +1 -2
  22. package/dist/packages/server/src/Document.d.ts +8 -5
  23. package/dist/packages/server/src/Hocuspocus.d.ts +32 -14
  24. package/dist/packages/server/src/MessageReceiver.d.ts +8 -5
  25. package/dist/packages/server/src/OutgoingMessage.d.ts +1 -0
  26. package/dist/packages/server/src/index.d.ts +7 -0
  27. package/dist/packages/server/src/types.d.ts +92 -23
  28. package/dist/{demos/backend/src/express.d.ts → playground/backend/src/default.d.ts} +0 -0
  29. package/dist/{demos/backend/src/koa.d.ts → playground/backend/src/express.d.ts} +0 -0
  30. package/dist/{demos/backend/src/load-document.d.ts → playground/backend/src/koa.d.ts} +0 -0
  31. package/dist/{demos/backend/src/minimal.d.ts → playground/backend/src/load-document.d.ts} +0 -0
  32. package/dist/{demos → playground}/backend/src/monitor.d.ts +0 -0
  33. package/dist/{demos → playground}/backend/src/redis.d.ts +0 -0
  34. package/dist/{demos → playground}/backend/src/slow.d.ts +0 -0
  35. package/dist/{demos → playground}/backend/src/webhook.d.ts +0 -0
  36. package/dist/tests/extension-logger/onListen.d.ts +1 -0
  37. package/dist/tests/extension-redis/onLoadDocument.d.ts +1 -0
  38. package/dist/tests/extension-redis/onSynced.d.ts +1 -0
  39. package/dist/tests/extension-redis-rewrite/closeConnections.d.ts +1 -0
  40. package/dist/tests/extension-redis-rewrite/getConnectionCount.d.ts +1 -0
  41. package/dist/tests/extension-redis-rewrite/getDocumentsCount.d.ts +1 -0
  42. package/dist/tests/extension-redis-rewrite/onAwarenessChange.d.ts +1 -0
  43. package/dist/tests/extension-redis-rewrite/onChange.d.ts +1 -0
  44. package/dist/tests/extension-redis-rewrite/onStoreDocument.d.ts +1 -0
  45. package/dist/tests/extension-rocksdb/onLoadDocument.d.ts +1 -0
  46. package/dist/tests/extension-throttle/configuration.d.ts +1 -0
  47. package/dist/tests/provider/configuration.d.ts +1 -0
  48. package/dist/tests/provider/observeDeep.d.ts +1 -0
  49. package/dist/tests/provider/onAuthenticated.d.ts +1 -0
  50. package/dist/tests/provider/onAuthenticationFailed.d.ts +1 -0
  51. package/dist/tests/provider/onAwarenessChange.d.ts +1 -0
  52. package/dist/tests/provider/onAwarenessUpdate.d.ts +1 -0
  53. package/dist/tests/provider/onClose.d.ts +1 -0
  54. package/dist/tests/provider/onConnect.d.ts +1 -0
  55. package/dist/tests/provider/onDisconnect.d.ts +1 -0
  56. package/dist/tests/provider/onMessage.d.ts +1 -0
  57. package/dist/tests/provider/onOpen.d.ts +1 -0
  58. package/dist/tests/provider/onSynced.d.ts +1 -0
  59. package/dist/tests/server/address.d.ts +1 -0
  60. package/dist/tests/server/afterStoreDocument.d.ts +1 -0
  61. package/dist/tests/server/closeConnections.d.ts +1 -0
  62. package/dist/tests/server/getConnectionsCount.d.ts +1 -0
  63. package/dist/tests/server/getDocumentName.d.ts +1 -0
  64. package/dist/tests/server/getDocumentsCount.d.ts +1 -0
  65. package/dist/tests/server/getMessageLogs.d.ts +1 -0
  66. package/dist/tests/server/listen.d.ts +1 -0
  67. package/dist/tests/server/onAuthenticate.d.ts +1 -0
  68. package/dist/tests/server/onAwarenessUpdate.d.ts +1 -0
  69. package/dist/tests/server/onChange.d.ts +1 -0
  70. package/dist/tests/server/onConfigure.d.ts +1 -0
  71. package/dist/tests/server/onConnect.d.ts +1 -0
  72. package/dist/tests/server/onDestroy.d.ts +1 -0
  73. package/dist/tests/server/onDisconnect.d.ts +1 -0
  74. package/dist/tests/server/onListen.d.ts +1 -0
  75. package/dist/tests/server/onLoadDocument.d.ts +1 -0
  76. package/dist/tests/server/onRequest.d.ts +1 -0
  77. package/dist/tests/server/onStoreDocument.d.ts +1 -0
  78. package/dist/tests/server/onUpgrade.d.ts +1 -0
  79. package/dist/tests/server/requiresAuthentication.d.ts +1 -0
  80. package/dist/tests/transformer/TiptapTransformer.d.ts +1 -0
  81. package/dist/tests/utils/createDirectory.d.ts +1 -0
  82. package/dist/tests/utils/flushRedis.d.ts +1 -0
  83. package/dist/tests/utils/index.d.ts +7 -0
  84. package/dist/tests/utils/newHocuspocus.d.ts +2 -0
  85. package/dist/tests/utils/newHocuspocusProvider.d.ts +3 -0
  86. package/dist/tests/utils/redisConnectionSettings.d.ts +4 -0
  87. package/dist/tests/utils/removeDirectory.d.ts +1 -0
  88. package/dist/tests/utils/sleep.d.ts +1 -0
  89. package/package.json +6 -5
  90. package/src/EventEmitter.ts +1 -1
  91. package/src/HocuspocusCloudProvider.ts +18 -10
  92. package/src/HocuspocusProvider.ts +100 -69
  93. package/src/index.ts +0 -1
  94. package/src/types.ts +10 -0
  95. package/dist/packages/provider/src/utils/awarenessStatesToArray.d.ts +0 -4
  96. package/dist/packages/provider/src/utils/index.d.ts +0 -1
  97. package/dist/packages/server/src/CloseEvents.d.ts +0 -4
  98. package/src/utils/awarenessStatesToArray.ts +0 -8
  99. package/src/utils/index.ts +0 -1
@@ -1,5 +1,6 @@
1
1
  import * as Y from 'yjs';
2
2
  import { retry } from '@lifeomic/attempt';
3
+ import { readAuthMessage, writeAuthentication, awarenessStatesToArray, Unauthorized, Forbidden } from '@hocuspocus/common';
3
4
 
4
5
  /**
5
6
  * Utility module to work with key-value stores.
@@ -277,8 +278,8 @@ const min = (a, b) => a < b ? a : b;
277
278
  const max = (a, b) => a > b ? a : b;
278
279
 
279
280
  /* eslint-env browser */
280
- const BIT8$1 = 128;
281
- const BITS7$1 = 127;
281
+ const BIT8 = 128;
282
+ const BITS7 = 127;
282
283
 
283
284
  /**
284
285
  * Efficient schema-less binary encoding with support for variable length encoding.
@@ -369,7 +370,7 @@ const toUint8Array = encoder => {
369
370
  * @param {Encoder} encoder
370
371
  * @param {number} num The byte that is to be encoded.
371
372
  */
372
- const write$1 = (encoder, num) => {
373
+ const write = (encoder, num) => {
373
374
  const bufferLen = encoder.cbuf.length;
374
375
  if (encoder.cpos === bufferLen) {
375
376
  encoder.bufs.push(encoder.cbuf);
@@ -388,12 +389,12 @@ const write$1 = (encoder, num) => {
388
389
  * @param {Encoder} encoder
389
390
  * @param {number} num The number that is to be encoded.
390
391
  */
391
- const writeVarUint$1 = (encoder, num) => {
392
- while (num > BITS7$1) {
393
- write$1(encoder, BIT8$1 | (BITS7$1 & num));
392
+ const writeVarUint = (encoder, num) => {
393
+ while (num > BITS7) {
394
+ write(encoder, BIT8 | (BITS7 & num));
394
395
  num >>>= 7;
395
396
  }
396
- write$1(encoder, BITS7$1 & num);
397
+ write(encoder, BITS7 & num);
397
398
  };
398
399
 
399
400
  /**
@@ -403,12 +404,12 @@ const writeVarUint$1 = (encoder, num) => {
403
404
  * @param {Encoder} encoder
404
405
  * @param {String} str The string that is to be encoded.
405
406
  */
406
- const writeVarString$1 = (encoder, str) => {
407
+ const writeVarString = (encoder, str) => {
407
408
  const encodedString = unescape(encodeURIComponent(str));
408
409
  const len = encodedString.length;
409
- writeVarUint$1(encoder, len);
410
+ writeVarUint(encoder, len);
410
411
  for (let i = 0; i < len; i++) {
411
- write$1(encoder, /** @type {number} */ (encodedString.codePointAt(i)));
412
+ write(encoder, /** @type {number} */ (encodedString.codePointAt(i)));
412
413
  }
413
414
  };
414
415
 
@@ -446,7 +447,7 @@ const writeUint8Array = (encoder, uint8Array) => {
446
447
  * @param {Uint8Array} uint8Array
447
448
  */
448
449
  const writeVarUint8Array = (encoder, uint8Array) => {
449
- writeVarUint$1(encoder, uint8Array.byteLength);
450
+ writeVarUint(encoder, uint8Array.byteLength);
450
451
  writeUint8Array(encoder, uint8Array);
451
452
  };
452
453
 
@@ -535,7 +536,7 @@ const readUint8Array = (decoder, len) => {
535
536
  * @param {Decoder} decoder
536
537
  * @return {Uint8Array}
537
538
  */
538
- const readVarUint8Array = decoder => readUint8Array(decoder, readVarUint$1(decoder));
539
+ const readVarUint8Array = decoder => readUint8Array(decoder, readVarUint(decoder));
539
540
 
540
541
  /**
541
542
  * Read one byte as unsigned integer.
@@ -543,7 +544,7 @@ const readVarUint8Array = decoder => readUint8Array(decoder, readVarUint$1(decod
543
544
  * @param {Decoder} decoder The decoder instance
544
545
  * @return {number} Unsigned 8-bit integer
545
546
  */
546
- const readUint8$1 = decoder => decoder.arr[decoder.pos++];
547
+ const readUint8 = decoder => decoder.arr[decoder.pos++];
547
548
 
548
549
  /**
549
550
  * Read unsigned integer (32bit) with variable length.
@@ -555,14 +556,14 @@ const readUint8$1 = decoder => decoder.arr[decoder.pos++];
555
556
  * @param {Decoder} decoder
556
557
  * @return {number} An unsigned integer.length
557
558
  */
558
- const readVarUint$1 = decoder => {
559
+ const readVarUint = decoder => {
559
560
  let num = 0;
560
561
  let len = 0;
561
562
  while (true) {
562
563
  const r = decoder.arr[decoder.pos++];
563
- num = num | ((r & BITS7$1) << len);
564
+ num = num | ((r & BITS7) << len);
564
565
  len += 7;
565
- if (r < BIT8$1) {
566
+ if (r < BIT8) {
566
567
  return num >>> 0 // return unsigned number!
567
568
  }
568
569
  /* istanbul ignore if */
@@ -585,15 +586,15 @@ const readVarUint$1 = decoder => {
585
586
  * @param {Decoder} decoder
586
587
  * @return {String} The read String.
587
588
  */
588
- const readVarString$1 = decoder => {
589
- let remainingLen = readVarUint$1(decoder);
589
+ const readVarString = decoder => {
590
+ let remainingLen = readVarUint(decoder);
590
591
  if (remainingLen === 0) {
591
592
  return ''
592
593
  } else {
593
- let encodedString = String.fromCodePoint(readUint8$1(decoder)); // remember to decrease remainingLen
594
+ let encodedString = String.fromCodePoint(readUint8(decoder)); // remember to decrease remainingLen
594
595
  if (--remainingLen < 100) { // do not create a Uint8Array for small strings
595
596
  while (remainingLen--) {
596
- encodedString += String.fromCodePoint(readUint8$1(decoder));
597
+ encodedString += String.fromCodePoint(readUint8(decoder));
597
598
  }
598
599
  } else {
599
600
  while (remainingLen > 0) {
@@ -1206,14 +1207,14 @@ const removeAwarenessStates = (awareness, clients, origin) => {
1206
1207
  const encodeAwarenessUpdate = (awareness, clients, states = awareness.states) => {
1207
1208
  const len = clients.length;
1208
1209
  const encoder = createEncoder();
1209
- writeVarUint$1(encoder, len);
1210
+ writeVarUint(encoder, len);
1210
1211
  for (let i = 0; i < len; i++) {
1211
1212
  const clientID = clients[i];
1212
1213
  const state = states.get(clientID) || null;
1213
1214
  const clock = /** @type {MetaClientState} */ (awareness.meta.get(clientID)).clock;
1214
- writeVarUint$1(encoder, clientID);
1215
- writeVarUint$1(encoder, clock);
1216
- writeVarString$1(encoder, JSON.stringify(state));
1215
+ writeVarUint(encoder, clientID);
1216
+ writeVarUint(encoder, clock);
1217
+ writeVarString(encoder, JSON.stringify(state));
1217
1218
  }
1218
1219
  return toUint8Array(encoder)
1219
1220
  };
@@ -1230,11 +1231,11 @@ const applyAwarenessUpdate = (awareness, update, origin) => {
1230
1231
  const updated = [];
1231
1232
  const filteredUpdated = [];
1232
1233
  const removed = [];
1233
- const len = readVarUint$1(decoder);
1234
+ const len = readVarUint(decoder);
1234
1235
  for (let i = 0; i < len; i++) {
1235
- const clientID = readVarUint$1(decoder);
1236
- let clock = readVarUint$1(decoder);
1237
- const state = JSON.parse(readVarString$1(decoder));
1236
+ const clientID = readVarUint(decoder);
1237
+ let clock = readVarUint(decoder);
1238
+ const state = JSON.parse(readVarString(decoder));
1238
1239
  const clientMeta = awareness.meta.get(clientID);
1239
1240
  const prevState = awareness.states.get(clientID);
1240
1241
  const currClock = clientMeta === undefined ? 0 : clientMeta.clock;
@@ -1378,13 +1379,13 @@ class IncomingMessage {
1378
1379
  this.decoder = createDecoder(new Uint8Array(this.data));
1379
1380
  }
1380
1381
  readVarUint() {
1381
- return readVarUint$1(this.decoder);
1382
+ return readVarUint(this.decoder);
1382
1383
  }
1383
1384
  readVarUint8Array() {
1384
1385
  return readVarUint8Array(this.decoder);
1385
1386
  }
1386
1387
  writeVarUint(type) {
1387
- return writeVarUint$1(this.encoder, type);
1388
+ return writeVarUint(this.encoder, type);
1388
1389
  }
1389
1390
  writeVarUint8Array(data) {
1390
1391
  return writeVarUint8Array(this.encoder, data);
@@ -1438,7 +1439,7 @@ const messageYjsUpdate = 2;
1438
1439
  * @param {Y.Doc} doc
1439
1440
  */
1440
1441
  const writeSyncStep1 = (encoder, doc) => {
1441
- writeVarUint$1(encoder, messageYjsSyncStep1);
1442
+ writeVarUint(encoder, messageYjsSyncStep1);
1442
1443
  const sv = Y.encodeStateVector(doc);
1443
1444
  writeVarUint8Array(encoder, sv);
1444
1445
  };
@@ -1449,7 +1450,7 @@ const writeSyncStep1 = (encoder, doc) => {
1449
1450
  * @param {Uint8Array} [encodedStateVector]
1450
1451
  */
1451
1452
  const writeSyncStep2 = (encoder, doc, encodedStateVector) => {
1452
- writeVarUint$1(encoder, messageYjsSyncStep2);
1453
+ writeVarUint(encoder, messageYjsSyncStep2);
1453
1454
  writeVarUint8Array(encoder, Y.encodeStateAsUpdate(doc, encodedStateVector));
1454
1455
  };
1455
1456
 
@@ -1484,7 +1485,7 @@ const readSyncStep2 = (decoder, doc, transactionOrigin) => {
1484
1485
  * @param {Uint8Array} update
1485
1486
  */
1486
1487
  const writeUpdate = (encoder, update) => {
1487
- writeVarUint$1(encoder, messageYjsUpdate);
1488
+ writeVarUint(encoder, messageYjsUpdate);
1488
1489
  writeVarUint8Array(encoder, update);
1489
1490
  };
1490
1491
 
@@ -1504,7 +1505,7 @@ const readUpdate = readSyncStep2;
1504
1505
  * @param {any} transactionOrigin
1505
1506
  */
1506
1507
  const readSyncMessage = (decoder, encoder, doc, transactionOrigin) => {
1507
- const messageType = readVarUint$1(decoder);
1508
+ const messageType = readVarUint(decoder);
1508
1509
  switch (messageType) {
1509
1510
  case messageYjsSyncStep1:
1510
1511
  readSyncStep1(decoder, encoder, doc);
@@ -1521,239 +1522,6 @@ const readSyncMessage = (decoder, encoder, doc, transactionOrigin) => {
1521
1522
  return messageType
1522
1523
  };
1523
1524
 
1524
- /* eslint-env browser */
1525
- const BIT8 = 128;
1526
- const BITS7 = 127;
1527
- /**
1528
- * Efficient schema-less binary decoding with support for variable length encoding.
1529
- *
1530
- * Use [lib0/decoding] with [lib0/encoding]. Every encoding function has a corresponding decoding function.
1531
- *
1532
- * Encodes numbers in little-endian order (least to most significant byte order)
1533
- * and is compatible with Golang's binary encoding (https://golang.org/pkg/encoding/binary/)
1534
- * which is also used in Protocol Buffers.
1535
- *
1536
- * ```js
1537
- * // encoding step
1538
- * const encoder = new encoding.createEncoder()
1539
- * encoding.writeVarUint(encoder, 256)
1540
- * encoding.writeVarString(encoder, 'Hello world!')
1541
- * const buf = encoding.toUint8Array(encoder)
1542
- * ```
1543
- *
1544
- * ```js
1545
- * // decoding step
1546
- * const decoder = new decoding.createDecoder(buf)
1547
- * decoding.readVarUint(decoder) // => 256
1548
- * decoding.readVarString(decoder) // => 'Hello world!'
1549
- * decoding.hasContent(decoder) // => false - all data is read
1550
- * ```
1551
- *
1552
- * @module decoding
1553
- */
1554
-
1555
- /**
1556
- * Read one byte as unsigned integer.
1557
- * @function
1558
- * @param {Decoder} decoder The decoder instance
1559
- * @return {number} Unsigned 8-bit integer
1560
- */
1561
-
1562
- const readUint8 = decoder => decoder.arr[decoder.pos++];
1563
- /**
1564
- * Read unsigned integer (32bit) with variable length.
1565
- * 1/8th of the storage is used as encoding overhead.
1566
- * * numbers < 2^7 is stored in one bytlength
1567
- * * numbers < 2^14 is stored in two bylength
1568
- *
1569
- * @function
1570
- * @param {Decoder} decoder
1571
- * @return {number} An unsigned integer.length
1572
- */
1573
-
1574
-
1575
- const readVarUint = decoder => {
1576
- let num = 0;
1577
- let len = 0;
1578
-
1579
- while (true) {
1580
- const r = decoder.arr[decoder.pos++];
1581
- num = num | (r & BITS7) << len;
1582
- len += 7;
1583
-
1584
- if (r < BIT8) {
1585
- return num >>> 0; // return unsigned number!
1586
- }
1587
- /* istanbul ignore if */
1588
-
1589
-
1590
- if (len > 35) {
1591
- throw new Error('Integer out of range!');
1592
- }
1593
- }
1594
- };
1595
- /**
1596
- * Read string of variable length
1597
- * * varUint is used to store the length of the string
1598
- *
1599
- * Transforming utf8 to a string is pretty expensive. The code performs 10x better
1600
- * when String.fromCodePoint is fed with all characters as arguments.
1601
- * But most environments have a maximum number of arguments per functions.
1602
- * For effiency reasons we apply a maximum of 10000 characters at once.
1603
- *
1604
- * @function
1605
- * @param {Decoder} decoder
1606
- * @return {String} The read String.
1607
- */
1608
-
1609
-
1610
- const readVarString = decoder => {
1611
- let remainingLen = readVarUint(decoder);
1612
-
1613
- if (remainingLen === 0) {
1614
- return '';
1615
- } else {
1616
- let encodedString = String.fromCodePoint(readUint8(decoder)); // remember to decrease remainingLen
1617
-
1618
- if (--remainingLen < 100) {
1619
- // do not create a Uint8Array for small strings
1620
- while (remainingLen--) {
1621
- encodedString += String.fromCodePoint(readUint8(decoder));
1622
- }
1623
- } else {
1624
- while (remainingLen > 0) {
1625
- const nextLen = remainingLen < 10000 ? remainingLen : 10000; // this is dangerous, we create a fresh array view from the existing buffer
1626
-
1627
- const bytes = decoder.arr.subarray(decoder.pos, decoder.pos + nextLen);
1628
- decoder.pos += nextLen; // Starting with ES5.1 we can supply a generic array-like object as arguments
1629
-
1630
- encodedString += String.fromCodePoint.apply(null,
1631
- /** @type {any} */
1632
- bytes);
1633
- remainingLen -= nextLen;
1634
- }
1635
- }
1636
-
1637
- return decodeURIComponent(escape(encodedString));
1638
- }
1639
- };
1640
- /**
1641
- * Efficient schema-less binary encoding with support for variable length encoding.
1642
- *
1643
- * Use [lib0/encoding] with [lib0/decoding]. Every encoding function has a corresponding decoding function.
1644
- *
1645
- * Encodes numbers in little-endian order (least to most significant byte order)
1646
- * and is compatible with Golang's binary encoding (https://golang.org/pkg/encoding/binary/)
1647
- * which is also used in Protocol Buffers.
1648
- *
1649
- * ```js
1650
- * // encoding step
1651
- * const encoder = new encoding.createEncoder()
1652
- * encoding.writeVarUint(encoder, 256)
1653
- * encoding.writeVarString(encoder, 'Hello world!')
1654
- * const buf = encoding.toUint8Array(encoder)
1655
- * ```
1656
- *
1657
- * ```js
1658
- * // decoding step
1659
- * const decoder = new decoding.createDecoder(buf)
1660
- * decoding.readVarUint(decoder) // => 256
1661
- * decoding.readVarString(decoder) // => 'Hello world!'
1662
- * decoding.hasContent(decoder) // => false - all data is read
1663
- * ```
1664
- *
1665
- * @module encoding
1666
- */
1667
-
1668
- /**
1669
- * Write one byte to the encoder.
1670
- *
1671
- * @function
1672
- * @param {Encoder} encoder
1673
- * @param {number} num The byte that is to be encoded.
1674
- */
1675
-
1676
-
1677
- const write = (encoder, num) => {
1678
- const bufferLen = encoder.cbuf.length;
1679
-
1680
- if (encoder.cpos === bufferLen) {
1681
- encoder.bufs.push(encoder.cbuf);
1682
- encoder.cbuf = new Uint8Array(bufferLen * 2);
1683
- encoder.cpos = 0;
1684
- }
1685
-
1686
- encoder.cbuf[encoder.cpos++] = num;
1687
- };
1688
- /**
1689
- * Write a variable length unsigned integer.
1690
- *
1691
- * Encodes integers in the range from [0, 4294967295] / [0, 0xffffffff]. (max 32 bit unsigned integer)
1692
- *
1693
- * @function
1694
- * @param {Encoder} encoder
1695
- * @param {number} num The number that is to be encoded.
1696
- */
1697
-
1698
-
1699
- const writeVarUint = (encoder, num) => {
1700
- while (num > BITS7) {
1701
- write(encoder, BIT8 | BITS7 & num);
1702
- num >>>= 7;
1703
- }
1704
-
1705
- write(encoder, BITS7 & num);
1706
- };
1707
- /**
1708
- * Write a variable length string.
1709
- *
1710
- * @function
1711
- * @param {Encoder} encoder
1712
- * @param {String} str The string that is to be encoded.
1713
- */
1714
-
1715
-
1716
- const writeVarString = (encoder, str) => {
1717
- const encodedString = unescape(encodeURIComponent(str));
1718
- const len = encodedString.length;
1719
- writeVarUint(encoder, len);
1720
-
1721
- for (let i = 0; i < len; i++) {
1722
- write(encoder,
1723
- /** @type {number} */
1724
- encodedString.codePointAt(i));
1725
- }
1726
- };
1727
-
1728
- var AuthMessageType;
1729
-
1730
- (function (AuthMessageType) {
1731
- AuthMessageType[AuthMessageType["Token"] = 0] = "Token";
1732
- AuthMessageType[AuthMessageType["PermissionDenied"] = 1] = "PermissionDenied";
1733
- AuthMessageType[AuthMessageType["Authenticated"] = 2] = "Authenticated";
1734
- })(AuthMessageType || (AuthMessageType = {}));
1735
-
1736
- const writeAuthentication = (encoder, auth) => {
1737
- writeVarUint(encoder, AuthMessageType.Token);
1738
- writeVarString(encoder, auth);
1739
- };
1740
-
1741
- const readAuthMessage = (decoder, permissionDeniedHandler, authenticatedHandler) => {
1742
- switch (readVarUint(decoder)) {
1743
- case AuthMessageType.PermissionDenied:
1744
- {
1745
- permissionDeniedHandler(readVarString(decoder));
1746
- break;
1747
- }
1748
-
1749
- case AuthMessageType.Authenticated:
1750
- {
1751
- authenticatedHandler();
1752
- break;
1753
- }
1754
- }
1755
- };
1756
-
1757
1525
  var MessageType;
1758
1526
  (function (MessageType) {
1759
1527
  MessageType[MessageType["Sync"] = 0] = "Sync";
@@ -1867,7 +1635,7 @@ class SyncStepOneMessage extends OutgoingMessage {
1867
1635
  if (typeof args.document === 'undefined') {
1868
1636
  throw new Error('The sync step one message requires document as an argument');
1869
1637
  }
1870
- writeVarUint$1(this.encoder, this.type);
1638
+ writeVarUint(this.encoder, this.type);
1871
1639
  writeSyncStep1(this.encoder, args.document);
1872
1640
  return this.encoder;
1873
1641
  }
@@ -1883,7 +1651,7 @@ class SyncStepTwoMessage extends OutgoingMessage {
1883
1651
  if (typeof args.document === 'undefined') {
1884
1652
  throw new Error('The sync step two message requires document as an argument');
1885
1653
  }
1886
- writeVarUint$1(this.encoder, this.type);
1654
+ writeVarUint(this.encoder, this.type);
1887
1655
  writeSyncStep2(this.encoder, args.document);
1888
1656
  return this.encoder;
1889
1657
  }
@@ -1896,7 +1664,7 @@ class QueryAwarenessMessage extends OutgoingMessage {
1896
1664
  this.description = 'Queries awareness states';
1897
1665
  }
1898
1666
  get(args) {
1899
- writeVarUint$1(this.encoder, this.type);
1667
+ writeVarUint(this.encoder, this.type);
1900
1668
  return this.encoder;
1901
1669
  }
1902
1670
  }
@@ -1911,7 +1679,7 @@ class AuthenticationMessage extends OutgoingMessage {
1911
1679
  if (typeof args.token === 'undefined') {
1912
1680
  throw new Error('The authentication message requires `token` as an argument.');
1913
1681
  }
1914
- writeVarUint$1(this.encoder, this.type);
1682
+ writeVarUint(this.encoder, this.type);
1915
1683
  writeAuthentication(this.encoder, args.token);
1916
1684
  return this.encoder;
1917
1685
  }
@@ -1930,7 +1698,7 @@ class AwarenessMessage extends OutgoingMessage {
1930
1698
  if (typeof args.clients === 'undefined') {
1931
1699
  throw new Error('The awareness message requires clients as an argument');
1932
1700
  }
1933
- writeVarUint$1(this.encoder, this.type);
1701
+ writeVarUint(this.encoder, this.type);
1934
1702
  let awarenessUpdate;
1935
1703
  if (args.states === undefined) {
1936
1704
  awarenessUpdate = encodeAwarenessUpdate(args.awareness, args.clients);
@@ -1950,21 +1718,12 @@ class UpdateMessage extends OutgoingMessage {
1950
1718
  this.description = 'A document update';
1951
1719
  }
1952
1720
  get(args) {
1953
- writeVarUint$1(this.encoder, this.type);
1721
+ writeVarUint(this.encoder, this.type);
1954
1722
  writeUpdate(this.encoder, args.update);
1955
1723
  return this.encoder;
1956
1724
  }
1957
1725
  }
1958
1726
 
1959
- var awarenessStatesToArray = (states) => {
1960
- return Array.from(states.entries()).map(([key, value]) => {
1961
- return {
1962
- clientId: key,
1963
- ...value,
1964
- };
1965
- });
1966
- };
1967
-
1968
1727
  var WebSocketStatus;
1969
1728
  (function (WebSocketStatus) {
1970
1729
  WebSocketStatus["Connecting"] = "connecting";
@@ -1972,16 +1731,16 @@ var WebSocketStatus;
1972
1731
  WebSocketStatus["Disconnected"] = "disconnected";
1973
1732
  })(WebSocketStatus || (WebSocketStatus = {}));
1974
1733
  class HocuspocusProvider extends EventEmitter {
1975
- constructor(options = {}) {
1734
+ constructor(configuration) {
1976
1735
  super();
1977
- this.options = {
1736
+ this.configuration = {
1737
+ name: '',
1738
+ url: '',
1978
1739
  // @ts-ignore
1979
1740
  document: undefined,
1980
1741
  // @ts-ignore
1981
1742
  awareness: undefined,
1982
1743
  WebSocketPolyfill: undefined,
1983
- url: '',
1984
- name: '',
1985
1744
  token: null,
1986
1745
  parameters: {},
1987
1746
  connect: true,
@@ -2018,6 +1777,7 @@ class HocuspocusProvider extends EventEmitter {
2018
1777
  onDestroy: () => null,
2019
1778
  onAwarenessUpdate: () => null,
2020
1779
  onAwarenessChange: () => null,
1780
+ quiet: false,
2021
1781
  };
2022
1782
  this.subscribedToBroadcastChannel = false;
2023
1783
  this.webSocket = null;
@@ -2032,23 +1792,23 @@ class HocuspocusProvider extends EventEmitter {
2032
1792
  connectionChecker: null,
2033
1793
  };
2034
1794
  this.connectionAttempt = null;
2035
- this.setOptions(options);
2036
- this.options.document = options.document ? options.document : new Y.Doc();
2037
- this.options.awareness = options.awareness ? options.awareness : new Awareness(this.document);
2038
- this.options.WebSocketPolyfill = options.WebSocketPolyfill ? options.WebSocketPolyfill : WebSocket;
2039
- this.on('open', this.options.onOpen);
2040
- this.on('authenticated', this.options.onAuthenticated);
2041
- this.on('authenticationFailed', this.options.onAuthenticationFailed);
2042
- this.on('connect', this.options.onConnect);
2043
- this.on('message', this.options.onMessage);
2044
- this.on('outgoingMessage', this.options.onOutgoingMessage);
2045
- this.on('synced', this.options.onSynced);
2046
- this.on('status', this.options.onStatus);
2047
- this.on('disconnect', this.options.onDisconnect);
2048
- this.on('close', this.options.onClose);
2049
- this.on('destroy', this.options.onDestroy);
2050
- this.on('awarenessUpdate', this.options.onAwarenessUpdate);
2051
- this.on('awarenessChange', this.options.onAwarenessChange);
1795
+ this.setConfiguration(configuration);
1796
+ this.configuration.document = configuration.document ? configuration.document : new Y.Doc();
1797
+ this.configuration.awareness = configuration.awareness ? configuration.awareness : new Awareness(this.document);
1798
+ this.configuration.WebSocketPolyfill = configuration.WebSocketPolyfill ? configuration.WebSocketPolyfill : WebSocket;
1799
+ this.on('open', this.configuration.onOpen);
1800
+ this.on('authenticated', this.configuration.onAuthenticated);
1801
+ this.on('authenticationFailed', this.configuration.onAuthenticationFailed);
1802
+ this.on('connect', this.configuration.onConnect);
1803
+ this.on('message', this.configuration.onMessage);
1804
+ this.on('outgoingMessage', this.configuration.onOutgoingMessage);
1805
+ this.on('synced', this.configuration.onSynced);
1806
+ this.on('status', this.configuration.onStatus);
1807
+ this.on('disconnect', this.configuration.onDisconnect);
1808
+ this.on('close', this.configuration.onClose);
1809
+ this.on('destroy', this.configuration.onDestroy);
1810
+ this.on('awarenessUpdate', this.configuration.onAwarenessUpdate);
1811
+ this.on('awarenessChange', this.configuration.onAwarenessChange);
2052
1812
  this.awareness.on('update', () => {
2053
1813
  this.emit('awarenessUpdate', { states: awarenessStatesToArray(this.awareness.getStates()) });
2054
1814
  });
@@ -2057,21 +1817,21 @@ class HocuspocusProvider extends EventEmitter {
2057
1817
  });
2058
1818
  this.document.on('update', this.documentUpdateHandler.bind(this));
2059
1819
  this.awareness.on('update', this.awarenessUpdateHandler.bind(this));
2060
- this.registerBeforeUnloadEventListener();
2061
- this.intervals.connectionChecker = setInterval(this.checkConnection.bind(this), this.options.messageReconnectTimeout / 10);
2062
- if (this.options.forceSyncInterval) {
2063
- this.intervals.forceSync = setInterval(this.forceSync.bind(this), this.options.forceSyncInterval);
1820
+ this.registerEventListeners();
1821
+ this.intervals.connectionChecker = setInterval(this.checkConnection.bind(this), this.configuration.messageReconnectTimeout / 10);
1822
+ if (this.configuration.forceSyncInterval) {
1823
+ this.intervals.forceSync = setInterval(this.forceSync.bind(this), this.configuration.forceSyncInterval);
2064
1824
  }
2065
- if (typeof options.connect !== 'undefined') {
2066
- this.shouldConnect = options.connect;
1825
+ if (typeof configuration.connect !== 'undefined') {
1826
+ this.shouldConnect = configuration.connect;
2067
1827
  }
2068
1828
  if (!this.shouldConnect) {
2069
1829
  return;
2070
1830
  }
2071
1831
  this.connect();
2072
1832
  }
2073
- setOptions(options = {}) {
2074
- this.options = { ...this.options, ...options };
1833
+ setConfiguration(configuration = {}) {
1834
+ this.configuration = { ...this.configuration, ...configuration };
2075
1835
  }
2076
1836
  async connect() {
2077
1837
  if (this.status === WebSocketStatus.Connected) {
@@ -2081,14 +1841,14 @@ class HocuspocusProvider extends EventEmitter {
2081
1841
  this.subscribeToBroadcastChannel();
2082
1842
  try {
2083
1843
  await retry(this.createWebSocketConnection.bind(this), {
2084
- delay: this.options.delay,
2085
- initialDelay: this.options.initialDelay,
2086
- factor: this.options.factor,
2087
- maxAttempts: this.options.maxAttempts,
2088
- minDelay: this.options.minDelay,
2089
- maxDelay: this.options.maxDelay,
2090
- jitter: this.options.jitter,
2091
- timeout: this.options.timeout,
1844
+ delay: this.configuration.delay,
1845
+ initialDelay: this.configuration.initialDelay,
1846
+ factor: this.configuration.factor,
1847
+ maxAttempts: this.configuration.maxAttempts,
1848
+ minDelay: this.configuration.minDelay,
1849
+ maxDelay: this.configuration.maxDelay,
1850
+ jitter: this.configuration.jitter,
1851
+ timeout: this.configuration.timeout,
2092
1852
  beforeAttempt: context => {
2093
1853
  if (!this.shouldConnect) {
2094
1854
  context.abort();
@@ -2107,7 +1867,7 @@ class HocuspocusProvider extends EventEmitter {
2107
1867
  createWebSocketConnection() {
2108
1868
  return new Promise((resolve, reject) => {
2109
1869
  // Init the WebSocket connection
2110
- const ws = new this.options.WebSocketPolyfill(this.url);
1870
+ const ws = new this.configuration.WebSocketPolyfill(this.url);
2111
1871
  ws.binaryType = 'arraybuffer';
2112
1872
  ws.onmessage = this.onMessage.bind(this);
2113
1873
  ws.onclose = this.onClose.bind(this);
@@ -2138,10 +1898,10 @@ class HocuspocusProvider extends EventEmitter {
2138
1898
  this.connectionAttempt = null;
2139
1899
  }
2140
1900
  get document() {
2141
- return this.options.document;
1901
+ return this.configuration.document;
2142
1902
  }
2143
1903
  get awareness() {
2144
- return this.options.awareness;
1904
+ return this.configuration.awareness;
2145
1905
  }
2146
1906
  checkConnection() {
2147
1907
  var _a;
@@ -2154,7 +1914,7 @@ class HocuspocusProvider extends EventEmitter {
2154
1914
  return;
2155
1915
  }
2156
1916
  // Don’t close the connection when a message was received recently
2157
- if (this.options.messageReconnectTimeout >= getUnixTime() - this.lastMessageReceived) {
1917
+ if (this.configuration.messageReconnectTimeout >= getUnixTime() - this.lastMessageReceived) {
2158
1918
  return;
2159
1919
  }
2160
1920
  // No message received in a long time, not even your own
@@ -2167,10 +1927,11 @@ class HocuspocusProvider extends EventEmitter {
2167
1927
  }
2168
1928
  this.send(SyncStepOneMessage, { document: this.document });
2169
1929
  }
2170
- registerBeforeUnloadEventListener() {
1930
+ registerEventListeners() {
2171
1931
  if (typeof window === 'undefined') {
2172
1932
  return;
2173
1933
  }
1934
+ window.addEventListener('online', this.connect.bind(this));
2174
1935
  window.addEventListener('beforeunload', () => {
2175
1936
  removeAwarenessStates(this.awareness, [this.document.clientID], 'window unload');
2176
1937
  });
@@ -2200,14 +1961,14 @@ class HocuspocusProvider extends EventEmitter {
2200
1961
  }
2201
1962
  // Ensure that the URL always ends with /
2202
1963
  get serverUrl() {
2203
- while (this.options.url[this.options.url.length - 1] === '/') {
2204
- return this.options.url.slice(0, this.options.url.length - 1);
1964
+ while (this.configuration.url[this.configuration.url.length - 1] === '/') {
1965
+ return this.configuration.url.slice(0, this.configuration.url.length - 1);
2205
1966
  }
2206
- return this.options.url;
1967
+ return this.configuration.url;
2207
1968
  }
2208
1969
  get url() {
2209
- const encodedParams = encodeQueryParams(this.options.parameters);
2210
- return `${this.serverUrl}/${this.options.name}${encodedParams.length === 0 ? '' : `?${encodedParams}`}`;
1970
+ const encodedParams = encodeQueryParams(this.configuration.parameters);
1971
+ return `${this.serverUrl}/${this.configuration.name}${encodedParams.length === 0 ? '' : `?${encodedParams}`}`;
2211
1972
  }
2212
1973
  get synced() {
2213
1974
  return this.isSynced;
@@ -2221,7 +1982,7 @@ class HocuspocusProvider extends EventEmitter {
2221
1982
  this.emit('sync', { state });
2222
1983
  }
2223
1984
  get isAuthenticationRequired() {
2224
- return !!this.options.token && !this.isAuthenticated;
1985
+ return !!this.configuration.token && !this.isAuthenticated;
2225
1986
  }
2226
1987
  disconnect() {
2227
1988
  this.shouldConnect = false;
@@ -2243,11 +2004,11 @@ class HocuspocusProvider extends EventEmitter {
2243
2004
  }
2244
2005
  }
2245
2006
  async getToken() {
2246
- if (typeof this.options.token === 'function') {
2247
- const token = await this.options.token();
2007
+ if (typeof this.configuration.token === 'function') {
2008
+ const token = await this.configuration.token();
2248
2009
  return token;
2249
2010
  }
2250
- return this.options.token;
2011
+ return this.configuration.token;
2251
2012
  }
2252
2013
  async webSocketConnectionEstablished() {
2253
2014
  this.status = WebSocketStatus.Connected;
@@ -2299,15 +2060,26 @@ class HocuspocusProvider extends EventEmitter {
2299
2060
  this.emit('status', { status: 'disconnected' });
2300
2061
  this.emit('disconnect', { event });
2301
2062
  }
2063
+ if (event.code === Unauthorized.code) {
2064
+ if (!this.configuration.quiet) {
2065
+ 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.');
2066
+ }
2067
+ this.shouldConnect = false;
2068
+ }
2069
+ if (event.code === Forbidden.code) {
2070
+ if (!this.configuration.quiet) {
2071
+ console.warn('[HocuspocusProvider] The provided authentication token isn’t allowed to connect to this server. Will try again.');
2072
+ }
2073
+ }
2302
2074
  if (this.connectionAttempt) {
2303
- // Okay, that connection attempt failed
2075
+ // That connection attempt failed.
2304
2076
  this.rejectConnectionAttempt();
2305
2077
  }
2306
2078
  else if (this.shouldConnect) {
2307
- // The connection was closed by the server, so let’s just try again.
2079
+ // The connection was closed by the server. Let’s just try again.
2308
2080
  this.connect();
2309
2081
  }
2310
- // If we’ll reconnect anyway, we’re done for now.
2082
+ // If we’ll reconnect, we’re done for now.
2311
2083
  if (this.shouldConnect) {
2312
2084
  return;
2313
2085
  }
@@ -2335,9 +2107,13 @@ class HocuspocusProvider extends EventEmitter {
2335
2107
  this.awareness.off('update', this.awarenessUpdateHandler);
2336
2108
  this.document.off('update', this.documentUpdateHandler);
2337
2109
  this.removeAllListeners();
2110
+ if (typeof window === 'undefined') {
2111
+ return;
2112
+ }
2113
+ window.removeEventListener('online', this.connect.bind(this));
2338
2114
  }
2339
2115
  get broadcastChannel() {
2340
- return `${this.serverUrl}/${this.options.name}`;
2116
+ return `${this.serverUrl}/${this.configuration.name}`;
2341
2117
  }
2342
2118
  broadcastChannelSubscriber(data) {
2343
2119
  this.mux(() => {
@@ -2372,7 +2148,7 @@ class HocuspocusProvider extends EventEmitter {
2372
2148
  }
2373
2149
  }
2374
2150
  broadcast(Message, args) {
2375
- if (!this.options.broadcast) {
2151
+ if (!this.configuration.broadcast) {
2376
2152
  return;
2377
2153
  }
2378
2154
  if (!this.subscribedToBroadcastChannel) {
@@ -2386,19 +2162,19 @@ class HocuspocusProvider extends EventEmitter {
2386
2162
  }
2387
2163
 
2388
2164
  class HocuspocusCloudProvider extends HocuspocusProvider {
2389
- constructor(options) {
2390
- if (!options.url) {
2391
- options.url = 'wss://connect.hocuspocus.cloud';
2165
+ constructor(configuration) {
2166
+ if (!configuration.url) {
2167
+ configuration.url = 'wss://connect.hocuspocus.cloud';
2392
2168
  }
2393
- if (options.key) {
2394
- if (!options.parameters) {
2395
- options.parameters = {};
2169
+ if (configuration.key) {
2170
+ if (!configuration.parameters) {
2171
+ configuration.parameters = {};
2396
2172
  }
2397
- options.parameters.key = options.key;
2173
+ configuration.parameters.key = configuration.key;
2398
2174
  }
2399
- super(options);
2175
+ super(configuration);
2400
2176
  }
2401
2177
  }
2402
2178
 
2403
- export { HocuspocusCloudProvider, HocuspocusProvider, MessageType, WebSocketStatus, awarenessStatesToArray };
2179
+ export { HocuspocusCloudProvider, HocuspocusProvider, MessageType, WebSocketStatus };
2404
2180
  //# sourceMappingURL=hocuspocus-provider.esm.js.map