@hocuspocus/provider 3.4.3 → 3.4.5-rc.0

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 (146) hide show
  1. package/dist/hocuspocus-provider.cjs +1836 -2083
  2. package/dist/hocuspocus-provider.cjs.map +1 -1
  3. package/dist/hocuspocus-provider.esm.js +1803 -2060
  4. package/dist/hocuspocus-provider.esm.js.map +1 -1
  5. package/dist/index.d.ts +558 -0
  6. package/dist/index.js +1940 -0
  7. package/package.json +4 -4
  8. package/src/HocuspocusProvider.ts +16 -9
  9. package/src/HocuspocusProviderWebsocket.ts +16 -4
  10. package/src/MessageReceiver.ts +2 -2
  11. package/src/types.ts +3 -1
  12. package/dist/node_modules/@tiptap/pm/model/index.d.ts +0 -1
  13. package/dist/node_modules/@tiptap/pm/state/index.d.ts +0 -1
  14. package/dist/node_modules/@tiptap/pm/transform/index.d.ts +0 -1
  15. package/dist/node_modules/@tiptap/pm/view/index.d.ts +0 -1
  16. package/dist/packages/common/src/CloseEvents.d.ts +0 -29
  17. package/dist/packages/common/src/auth.d.ts +0 -12
  18. package/dist/packages/common/src/awarenessStatesToArray.d.ts +0 -3
  19. package/dist/packages/common/src/index.d.ts +0 -4
  20. package/dist/packages/common/src/types.d.ts +0 -10
  21. package/dist/packages/extension-database/src/Database.d.ts +0 -30
  22. package/dist/packages/extension-database/src/index.d.ts +0 -1
  23. package/dist/packages/extension-logger/src/Logger.d.ts +0 -67
  24. package/dist/packages/extension-logger/src/index.d.ts +0 -1
  25. package/dist/packages/extension-redis/src/Redis.d.ts +0 -124
  26. package/dist/packages/extension-redis/src/index.d.ts +0 -1
  27. package/dist/packages/extension-s3/src/S3.d.ts +0 -44
  28. package/dist/packages/extension-s3/src/index.d.ts +0 -1
  29. package/dist/packages/extension-sqlite/src/SQLite.d.ts +0 -27
  30. package/dist/packages/extension-sqlite/src/index.d.ts +0 -1
  31. package/dist/packages/extension-throttle/src/index.d.ts +0 -30
  32. package/dist/packages/extension-webhook/src/index.d.ts +0 -56
  33. package/dist/packages/provider/src/EventEmitter.d.ts +0 -9
  34. package/dist/packages/provider/src/HocuspocusProvider.d.ts +0 -116
  35. package/dist/packages/provider/src/HocuspocusProviderWebsocket.d.ts +0 -114
  36. package/dist/packages/provider/src/IncomingMessage.d.ts +0 -17
  37. package/dist/packages/provider/src/MessageReceiver.d.ts +0 -12
  38. package/dist/packages/provider/src/MessageSender.d.ts +0 -9
  39. package/dist/packages/provider/src/OutgoingMessage.d.ts +0 -9
  40. package/dist/packages/provider/src/OutgoingMessages/AuthenticationMessage.d.ts +0 -8
  41. package/dist/packages/provider/src/OutgoingMessages/AwarenessMessage.d.ts +0 -9
  42. package/dist/packages/provider/src/OutgoingMessages/CloseMessage.d.ts +0 -9
  43. package/dist/packages/provider/src/OutgoingMessages/QueryAwarenessMessage.d.ts +0 -9
  44. package/dist/packages/provider/src/OutgoingMessages/StatelessMessage.d.ts +0 -8
  45. package/dist/packages/provider/src/OutgoingMessages/SyncStepOneMessage.d.ts +0 -9
  46. package/dist/packages/provider/src/OutgoingMessages/SyncStepTwoMessage.d.ts +0 -9
  47. package/dist/packages/provider/src/OutgoingMessages/UpdateMessage.d.ts +0 -8
  48. package/dist/packages/provider/src/index.d.ts +0 -3
  49. package/dist/packages/provider/src/types.d.ts +0 -92
  50. package/dist/packages/server/src/ClientConnection.d.ts +0 -63
  51. package/dist/packages/server/src/Connection.d.ts +0 -71
  52. package/dist/packages/server/src/DirectConnection.d.ts +0 -14
  53. package/dist/packages/server/src/Document.d.ts +0 -92
  54. package/dist/packages/server/src/Hocuspocus.d.ts +0 -80
  55. package/dist/packages/server/src/IncomingMessage.d.ts +0 -25
  56. package/dist/packages/server/src/MessageReceiver.d.ts +0 -11
  57. package/dist/packages/server/src/OutgoingMessage.d.ts +0 -23
  58. package/dist/packages/server/src/Server.d.ts +0 -32
  59. package/dist/packages/server/src/index.d.ts +0 -9
  60. package/dist/packages/server/src/types.d.ts +0 -342
  61. package/dist/packages/server/src/util/debounce.d.ts +0 -6
  62. package/dist/packages/server/src/util/getParameters.d.ts +0 -6
  63. package/dist/packages/transformer/src/Prosemirror.d.ts +0 -11
  64. package/dist/packages/transformer/src/Tiptap.d.ts +0 -10
  65. package/dist/packages/transformer/src/index.d.ts +0 -3
  66. package/dist/packages/transformer/src/types.d.ts +0 -5
  67. package/dist/playground/backend/src/default.d.ts +0 -1
  68. package/dist/playground/backend/src/deno.d.ts +0 -1
  69. package/dist/playground/backend/src/express.d.ts +0 -1
  70. package/dist/playground/backend/src/hono.d.ts +0 -1
  71. package/dist/playground/backend/src/koa.d.ts +0 -1
  72. package/dist/playground/backend/src/load-document.d.ts +0 -1
  73. package/dist/playground/backend/src/redis.d.ts +0 -1
  74. package/dist/playground/backend/src/s3-redis.d.ts +0 -1
  75. package/dist/playground/backend/src/s3.d.ts +0 -1
  76. package/dist/playground/backend/src/slow.d.ts +0 -1
  77. package/dist/playground/backend/src/tiptapcollab.d.ts +0 -1
  78. package/dist/playground/backend/src/webhook.d.ts +0 -1
  79. package/dist/playground/frontend/app/SocketContext1.d.ts +0 -2
  80. package/dist/playground/frontend/app/SocketContext2.d.ts +0 -2
  81. package/dist/playground/frontend/next.config.d.ts +0 -3
  82. package/dist/tests/extension-database/fetch.d.ts +0 -1
  83. package/dist/tests/extension-logger/onListen.d.ts +0 -1
  84. package/dist/tests/extension-redis/onAwarenessChange.d.ts +0 -1
  85. package/dist/tests/extension-redis/onChange.d.ts +0 -1
  86. package/dist/tests/extension-redis/onStateless.d.ts +0 -1
  87. package/dist/tests/extension-redis/onStoreDocument.d.ts +0 -1
  88. package/dist/tests/extension-s3/fetch.d.ts +0 -1
  89. package/dist/tests/extension-throttle/banning.d.ts +0 -1
  90. package/dist/tests/extension-throttle/configuration.d.ts +0 -1
  91. package/dist/tests/provider/hasUnsyncedChanges.d.ts +0 -1
  92. package/dist/tests/provider/observe.d.ts +0 -1
  93. package/dist/tests/provider/observeDeep.d.ts +0 -1
  94. package/dist/tests/provider/onAuthenticated.d.ts +0 -1
  95. package/dist/tests/provider/onAuthenticationFailed.d.ts +0 -1
  96. package/dist/tests/provider/onAwarenessChange.d.ts +0 -1
  97. package/dist/tests/provider/onAwarenessUpdate.d.ts +0 -1
  98. package/dist/tests/provider/onClose.d.ts +0 -1
  99. package/dist/tests/provider/onConnect.d.ts +0 -1
  100. package/dist/tests/provider/onDisconnect.d.ts +0 -1
  101. package/dist/tests/provider/onMessage.d.ts +0 -1
  102. package/dist/tests/provider/onOpen.d.ts +0 -1
  103. package/dist/tests/provider/onStateless.d.ts +0 -1
  104. package/dist/tests/provider/onSynced.d.ts +0 -1
  105. package/dist/tests/providerwebsocket/configuration.d.ts +0 -1
  106. package/dist/tests/server/address.d.ts +0 -1
  107. package/dist/tests/server/afterLoadDocument.d.ts +0 -1
  108. package/dist/tests/server/afterStoreDocument.d.ts +0 -1
  109. package/dist/tests/server/afterUnloadDocument.d.ts +0 -1
  110. package/dist/tests/server/beforeBroadcastStateless.d.ts +0 -1
  111. package/dist/tests/server/beforeHandleMessage.d.ts +0 -1
  112. package/dist/tests/server/beforeSync.d.ts +0 -1
  113. package/dist/tests/server/beforeUnloadDocument.d.ts +0 -1
  114. package/dist/tests/server/closeConnections.d.ts +0 -1
  115. package/dist/tests/server/getConnectionsCount.d.ts +0 -1
  116. package/dist/tests/server/getDocumentsCount.d.ts +0 -1
  117. package/dist/tests/server/listen.d.ts +0 -1
  118. package/dist/tests/server/onAuthenticate.d.ts +0 -1
  119. package/dist/tests/server/onAwarenessUpdate.d.ts +0 -1
  120. package/dist/tests/server/onChange.d.ts +0 -1
  121. package/dist/tests/server/onClose.d.ts +0 -1
  122. package/dist/tests/server/onConfigure.d.ts +0 -1
  123. package/dist/tests/server/onConnect.d.ts +0 -1
  124. package/dist/tests/server/onDestroy.d.ts +0 -1
  125. package/dist/tests/server/onDisconnect.d.ts +0 -1
  126. package/dist/tests/server/onListen.d.ts +0 -1
  127. package/dist/tests/server/onLoadDocument.d.ts +0 -1
  128. package/dist/tests/server/onRequest.d.ts +0 -1
  129. package/dist/tests/server/onStateless.d.ts +0 -1
  130. package/dist/tests/server/onStoreDocument.d.ts +0 -1
  131. package/dist/tests/server/onTokenSync.d.ts +0 -1
  132. package/dist/tests/server/onUpgrade.d.ts +0 -1
  133. package/dist/tests/server/openDirectConnection.d.ts +0 -1
  134. package/dist/tests/server/websocketError.d.ts +0 -1
  135. package/dist/tests/transformer/TiptapTransformer.d.ts +0 -1
  136. package/dist/tests/utils/createDirectory.d.ts +0 -1
  137. package/dist/tests/utils/flushRedis.d.ts +0 -1
  138. package/dist/tests/utils/index.d.ts +0 -9
  139. package/dist/tests/utils/newHocuspocus.d.ts +0 -2
  140. package/dist/tests/utils/newHocuspocusProvider.d.ts +0 -3
  141. package/dist/tests/utils/newHocuspocusProviderWebsocket.d.ts +0 -4
  142. package/dist/tests/utils/randomInteger.d.ts +0 -1
  143. package/dist/tests/utils/redisConnectionSettings.d.ts +0 -4
  144. package/dist/tests/utils/removeDirectory.d.ts +0 -1
  145. package/dist/tests/utils/retryableAssertion.d.ts +0 -2
  146. package/dist/tests/utils/sleep.d.ts +0 -1
@@ -1,2221 +1,1974 @@
1
- 'use strict';
2
-
3
- var common = require('@hocuspocus/common');
4
- var Y = require('yjs');
5
- var attempt = require('@lifeomic/attempt');
6
-
7
- function _interopNamespaceDefault(e) {
8
- var n = Object.create(null);
9
- if (e) {
10
- Object.keys(e).forEach(function (k) {
11
- if (k !== 'default') {
12
- var d = Object.getOwnPropertyDescriptor(e, k);
13
- Object.defineProperty(n, k, d.get ? d : {
14
- enumerable: true,
15
- get: function () { return e[k]; }
16
- });
17
- }
18
- });
19
- }
20
- n.default = e;
21
- return Object.freeze(n);
22
- }
23
-
24
- var Y__namespace = /*#__PURE__*/_interopNamespaceDefault(Y);
25
-
26
- /**
27
- * Common Math expressions.
28
- *
29
- * @module math
30
- */
31
-
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ //#region \0rolldown/runtime.js
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
12
+ key = keys[i];
13
+ if (!__hasOwnProp.call(to, key) && key !== except) {
14
+ __defProp(to, key, {
15
+ get: ((k) => from[k]).bind(null, key),
16
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
17
+ });
18
+ }
19
+ }
20
+ }
21
+ return to;
22
+ };
23
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
24
+ value: mod,
25
+ enumerable: true
26
+ }) : target, mod));
27
+
28
+ //#endregion
29
+ let _hocuspocus_common = require("@hocuspocus/common");
30
+ let yjs = require("yjs");
31
+ yjs = __toESM(yjs);
32
+ let _lifeomic_attempt = require("@lifeomic/attempt");
33
+
34
+ //#region node_modules/lib0/math.js
35
+ /**
36
+ * Common Math expressions.
37
+ *
38
+ * @module math
39
+ */
32
40
  const floor = Math.floor;
33
-
34
41
  /**
35
- * @function
36
- * @param {number} a
37
- * @param {number} b
38
- * @return {number} The smaller element of a and b
39
- */
42
+ * @function
43
+ * @param {number} a
44
+ * @param {number} b
45
+ * @return {number} The smaller element of a and b
46
+ */
40
47
  const min = (a, b) => a < b ? a : b;
41
-
42
48
  /**
43
- * @function
44
- * @param {number} a
45
- * @param {number} b
46
- * @return {number} The bigger element of a and b
47
- */
49
+ * @function
50
+ * @param {number} a
51
+ * @param {number} b
52
+ * @return {number} The bigger element of a and b
53
+ */
48
54
  const max = (a, b) => a > b ? a : b;
55
+ const isNaN$1 = Number.isNaN;
49
56
 
50
- /* eslint-env browser */
51
-
57
+ //#endregion
58
+ //#region node_modules/lib0/binary.js
52
59
  const BIT7 = 64;
53
60
  const BIT8 = 128;
61
+ const BIT18 = 1 << 17;
62
+ const BIT19 = 1 << 18;
63
+ const BIT20 = 1 << 19;
64
+ const BIT21 = 1 << 20;
65
+ const BIT22 = 1 << 21;
66
+ const BIT23 = 1 << 22;
67
+ const BIT24 = 1 << 23;
68
+ const BIT25 = 1 << 24;
69
+ const BIT26 = 1 << 25;
70
+ const BIT27 = 1 << 26;
71
+ const BIT28 = 1 << 27;
72
+ const BIT29 = 1 << 28;
73
+ const BIT30 = 1 << 29;
74
+ const BIT31 = 1 << 30;
75
+ const BIT32 = 1 << 31;
54
76
  const BITS6 = 63;
55
77
  const BITS7 = 127;
56
-
57
- /**
58
- * Utility helpers for working with numbers.
59
- *
60
- * @module number
61
- */
62
-
63
-
78
+ const BITS17 = BIT18 - 1;
79
+ const BITS18 = BIT19 - 1;
80
+ const BITS19 = BIT20 - 1;
81
+ const BITS20 = BIT21 - 1;
82
+ const BITS21 = BIT22 - 1;
83
+ const BITS22 = BIT23 - 1;
84
+ const BITS23 = BIT24 - 1;
85
+ const BITS24 = BIT25 - 1;
86
+ const BITS25 = BIT26 - 1;
87
+ const BITS26 = BIT27 - 1;
88
+ const BITS27 = BIT28 - 1;
89
+ const BITS28 = BIT29 - 1;
90
+ const BITS29 = BIT30 - 1;
91
+ const BITS30 = BIT31 - 1;
92
+ /**
93
+ * @type {number}
94
+ */
95
+ const BITS31 = 2147483647;
96
+ /**
97
+ * @type {number}
98
+ */
99
+ const BITS32 = 4294967295;
100
+
101
+ //#endregion
102
+ //#region node_modules/lib0/number.js
103
+ /**
104
+ * Utility helpers for working with numbers.
105
+ *
106
+ * @module number
107
+ */
64
108
  const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER;
65
-
66
- /**
67
- * Utility module to work with sets.
68
- *
69
- * @module set
70
- */
71
-
72
- const create$2 = () => new Set();
73
-
74
- /**
75
- * Utility module to work with Arrays.
76
- *
77
- * @module array
78
- */
79
-
80
-
81
- /**
82
- * Transforms something array-like to an actual Array.
83
- *
84
- * @function
85
- * @template T
86
- * @param {ArrayLike<T>|Iterable<T>} arraylike
87
- * @return {T}
88
- */
109
+ const MIN_SAFE_INTEGER = Number.MIN_SAFE_INTEGER;
110
+ const LOWEST_INT32 = 1 << 31;
111
+ const HIGHEST_INT32 = BITS31;
112
+ const HIGHEST_UINT32 = BITS32;
113
+ /* c8 ignore next */
114
+ const isInteger = Number.isInteger || ((num) => typeof num === "number" && isFinite(num) && floor(num) === num);
115
+ const isNaN = Number.isNaN;
116
+ const parseInt = Number.parseInt;
117
+
118
+ //#endregion
119
+ //#region node_modules/lib0/set.js
120
+ /**
121
+ * Utility module to work with sets.
122
+ *
123
+ * @module set
124
+ */
125
+ const create$2 = () => /* @__PURE__ */ new Set();
126
+
127
+ //#endregion
128
+ //#region node_modules/lib0/array.js
129
+ /**
130
+ * Transforms something array-like to an actual Array.
131
+ *
132
+ * @function
133
+ * @template T
134
+ * @param {ArrayLike<T>|Iterable<T>} arraylike
135
+ * @return {T}
136
+ */
89
137
  const from = Array.from;
90
-
91
- /**
92
- * @param {string} str
93
- * @return {Uint8Array}
94
- */
95
- const _encodeUtf8Polyfill = str => {
96
- const encodedString = unescape(encodeURIComponent(str));
97
- const len = encodedString.length;
98
- const buf = new Uint8Array(len);
99
- for (let i = 0; i < len; i++) {
100
- buf[i] = /** @type {number} */ (encodedString.codePointAt(i));
101
- }
102
- return buf
138
+ const isArray$1 = Array.isArray;
139
+
140
+ //#endregion
141
+ //#region node_modules/lib0/string.js
142
+ /**
143
+ * Utility module to work with strings.
144
+ *
145
+ * @module string
146
+ */
147
+ const fromCharCode = String.fromCharCode;
148
+ const fromCodePoint = String.fromCodePoint;
149
+ /**
150
+ * The largest utf16 character.
151
+ * Corresponds to Uint8Array([255, 255]) or charcodeof(2x2^8)
152
+ */
153
+ const MAX_UTF16_CHARACTER = fromCharCode(65535);
154
+ /**
155
+ * @param {string} str
156
+ * @return {Uint8Array}
157
+ */
158
+ const _encodeUtf8Polyfill = (str) => {
159
+ const encodedString = unescape(encodeURIComponent(str));
160
+ const len = encodedString.length;
161
+ const buf = new Uint8Array(len);
162
+ for (let i = 0; i < len; i++) buf[i] = encodedString.codePointAt(i);
163
+ return buf;
103
164
  };
104
-
105
165
  /* c8 ignore next */
106
- const utf8TextEncoder = /** @type {TextEncoder} */ (typeof TextEncoder !== 'undefined' ? new TextEncoder() : null);
107
-
166
+ const utf8TextEncoder = typeof TextEncoder !== "undefined" ? new TextEncoder() : null;
108
167
  /**
109
- * @param {string} str
110
- * @return {Uint8Array}
111
- */
112
- const _encodeUtf8Native = str => utf8TextEncoder.encode(str);
113
-
168
+ * @param {string} str
169
+ * @return {Uint8Array}
170
+ */
171
+ const _encodeUtf8Native = (str) => utf8TextEncoder.encode(str);
114
172
  /**
115
- * @param {string} str
116
- * @return {Uint8Array}
117
- */
173
+ * @param {string} str
174
+ * @return {Uint8Array}
175
+ */
118
176
  /* c8 ignore next */
119
177
  const encodeUtf8 = utf8TextEncoder ? _encodeUtf8Native : _encodeUtf8Polyfill;
120
-
121
178
  /* c8 ignore next */
122
- let utf8TextDecoder = typeof TextDecoder === 'undefined' ? null : new TextDecoder('utf-8', { fatal: true, ignoreBOM: true });
123
-
179
+ let utf8TextDecoder = typeof TextDecoder === "undefined" ? null : new TextDecoder("utf-8", {
180
+ fatal: true,
181
+ ignoreBOM: true
182
+ });
124
183
  /* c8 ignore start */
125
- if (utf8TextDecoder && utf8TextDecoder.decode(new Uint8Array()).length === 1) {
126
- // Safari doesn't handle BOM correctly.
127
- // This fixes a bug in Safari 13.0.5 where it produces a BOM the first time it is called.
128
- // utf8TextDecoder.decode(new Uint8Array()).length === 1 on the first call and
129
- // utf8TextDecoder.decode(new Uint8Array()).length === 1 on the second call
130
- // Another issue is that from then on no BOM chars are recognized anymore
131
- /* c8 ignore next */
132
- utf8TextDecoder = null;
133
- }
134
-
135
- /**
136
- * Efficient schema-less binary encoding with support for variable length encoding.
137
- *
138
- * Use [lib0/encoding] with [lib0/decoding]. Every encoding function has a corresponding decoding function.
139
- *
140
- * Encodes numbers in little-endian order (least to most significant byte order)
141
- * and is compatible with Golang's binary encoding (https://golang.org/pkg/encoding/binary/)
142
- * which is also used in Protocol Buffers.
143
- *
144
- * ```js
145
- * // encoding step
146
- * const encoder = encoding.createEncoder()
147
- * encoding.writeVarUint(encoder, 256)
148
- * encoding.writeVarString(encoder, 'Hello world!')
149
- * const buf = encoding.toUint8Array(encoder)
150
- * ```
151
- *
152
- * ```js
153
- * // decoding step
154
- * const decoder = decoding.createDecoder(buf)
155
- * decoding.readVarUint(decoder) // => 256
156
- * decoding.readVarString(decoder) // => 'Hello world!'
157
- * decoding.hasContent(decoder) // => false - all data is read
158
- * ```
159
- *
160
- * @module encoding
161
- */
162
-
163
-
164
- /**
165
- * A BinaryEncoder handles the encoding to an Uint8Array.
166
- */
167
- class Encoder {
168
- constructor () {
169
- this.cpos = 0;
170
- this.cbuf = new Uint8Array(100);
171
- /**
172
- * @type {Array<Uint8Array>}
173
- */
174
- this.bufs = [];
175
- }
176
- }
177
-
184
+ if (utf8TextDecoder && utf8TextDecoder.decode(new Uint8Array()).length === 1)
185
+ /* c8 ignore next */
186
+ utf8TextDecoder = null;
187
+
188
+ //#endregion
189
+ //#region node_modules/lib0/encoding.js
190
+ /**
191
+ * Efficient schema-less binary encoding with support for variable length encoding.
192
+ *
193
+ * Use [lib0/encoding] with [lib0/decoding]. Every encoding function has a corresponding decoding function.
194
+ *
195
+ * Encodes numbers in little-endian order (least to most significant byte order)
196
+ * and is compatible with Golang's binary encoding (https://golang.org/pkg/encoding/binary/)
197
+ * which is also used in Protocol Buffers.
198
+ *
199
+ * ```js
200
+ * // encoding step
201
+ * const encoder = encoding.createEncoder()
202
+ * encoding.writeVarUint(encoder, 256)
203
+ * encoding.writeVarString(encoder, 'Hello world!')
204
+ * const buf = encoding.toUint8Array(encoder)
205
+ * ```
206
+ *
207
+ * ```js
208
+ * // decoding step
209
+ * const decoder = decoding.createDecoder(buf)
210
+ * decoding.readVarUint(decoder) // => 256
211
+ * decoding.readVarString(decoder) // => 'Hello world!'
212
+ * decoding.hasContent(decoder) // => false - all data is read
213
+ * ```
214
+ *
215
+ * @module encoding
216
+ */
217
+ /**
218
+ * A BinaryEncoder handles the encoding to an Uint8Array.
219
+ */
220
+ var Encoder = class {
221
+ constructor() {
222
+ this.cpos = 0;
223
+ this.cbuf = new Uint8Array(100);
224
+ /**
225
+ * @type {Array<Uint8Array>}
226
+ */
227
+ this.bufs = [];
228
+ }
229
+ };
178
230
  /**
179
- * @function
180
- * @return {Encoder}
181
- */
231
+ * @function
232
+ * @return {Encoder}
233
+ */
182
234
  const createEncoder = () => new Encoder();
183
-
184
235
  /**
185
- * The current length of the encoded data.
186
- *
187
- * @function
188
- * @param {Encoder} encoder
189
- * @return {number}
190
- */
191
- const length$1 = encoder => {
192
- let len = encoder.cpos;
193
- for (let i = 0; i < encoder.bufs.length; i++) {
194
- len += encoder.bufs[i].length;
195
- }
196
- return len
236
+ * The current length of the encoded data.
237
+ *
238
+ * @function
239
+ * @param {Encoder} encoder
240
+ * @return {number}
241
+ */
242
+ const length$1 = (encoder) => {
243
+ let len = encoder.cpos;
244
+ for (let i = 0; i < encoder.bufs.length; i++) len += encoder.bufs[i].length;
245
+ return len;
197
246
  };
198
-
199
247
  /**
200
- * Transform to Uint8Array.
201
- *
202
- * @function
203
- * @param {Encoder} encoder
204
- * @return {Uint8Array} The created ArrayBuffer.
205
- */
206
- const toUint8Array = encoder => {
207
- const uint8arr = new Uint8Array(length$1(encoder));
208
- let curPos = 0;
209
- for (let i = 0; i < encoder.bufs.length; i++) {
210
- const d = encoder.bufs[i];
211
- uint8arr.set(d, curPos);
212
- curPos += d.length;
213
- }
214
- uint8arr.set(new Uint8Array(encoder.cbuf.buffer, 0, encoder.cpos), curPos);
215
- return uint8arr
248
+ * Transform to Uint8Array.
249
+ *
250
+ * @function
251
+ * @param {Encoder} encoder
252
+ * @return {Uint8Array} The created ArrayBuffer.
253
+ */
254
+ const toUint8Array = (encoder) => {
255
+ const uint8arr = new Uint8Array(length$1(encoder));
256
+ let curPos = 0;
257
+ for (let i = 0; i < encoder.bufs.length; i++) {
258
+ const d = encoder.bufs[i];
259
+ uint8arr.set(d, curPos);
260
+ curPos += d.length;
261
+ }
262
+ uint8arr.set(new Uint8Array(encoder.cbuf.buffer, 0, encoder.cpos), curPos);
263
+ return uint8arr;
216
264
  };
217
-
218
265
  /**
219
- * Write one byte to the encoder.
220
- *
221
- * @function
222
- * @param {Encoder} encoder
223
- * @param {number} num The byte that is to be encoded.
224
- */
266
+ * Write one byte to the encoder.
267
+ *
268
+ * @function
269
+ * @param {Encoder} encoder
270
+ * @param {number} num The byte that is to be encoded.
271
+ */
225
272
  const write = (encoder, num) => {
226
- const bufferLen = encoder.cbuf.length;
227
- if (encoder.cpos === bufferLen) {
228
- encoder.bufs.push(encoder.cbuf);
229
- encoder.cbuf = new Uint8Array(bufferLen * 2);
230
- encoder.cpos = 0;
231
- }
232
- encoder.cbuf[encoder.cpos++] = num;
273
+ const bufferLen = encoder.cbuf.length;
274
+ if (encoder.cpos === bufferLen) {
275
+ encoder.bufs.push(encoder.cbuf);
276
+ encoder.cbuf = new Uint8Array(bufferLen * 2);
277
+ encoder.cpos = 0;
278
+ }
279
+ encoder.cbuf[encoder.cpos++] = num;
233
280
  };
234
-
235
281
  /**
236
- * Write a variable length unsigned integer. Max encodable integer is 2^53.
237
- *
238
- * @function
239
- * @param {Encoder} encoder
240
- * @param {number} num The number that is to be encoded.
241
- */
282
+ * Write a variable length unsigned integer. Max encodable integer is 2^53.
283
+ *
284
+ * @function
285
+ * @param {Encoder} encoder
286
+ * @param {number} num The number that is to be encoded.
287
+ */
242
288
  const writeVarUint = (encoder, num) => {
243
- while (num > BITS7) {
244
- write(encoder, BIT8 | (BITS7 & num));
245
- num = floor(num / 128); // shift >>> 7
246
- }
247
- write(encoder, BITS7 & num);
289
+ while (num > BITS7) {
290
+ write(encoder, BIT8 | BITS7 & num);
291
+ num = floor(num / 128);
292
+ }
293
+ write(encoder, BITS7 & num);
248
294
  };
249
-
250
295
  /**
251
- * A cache to store strings temporarily
252
- */
253
- const _strBuffer = new Uint8Array(30000);
296
+ * A cache to store strings temporarily
297
+ */
298
+ const _strBuffer = new Uint8Array(3e4);
254
299
  const _maxStrBSize = _strBuffer.length / 3;
255
-
256
300
  /**
257
- * Write a variable length string.
258
- *
259
- * @function
260
- * @param {Encoder} encoder
261
- * @param {String} str The string that is to be encoded.
262
- */
301
+ * Write a variable length string.
302
+ *
303
+ * @function
304
+ * @param {Encoder} encoder
305
+ * @param {String} str The string that is to be encoded.
306
+ */
263
307
  const _writeVarStringNative = (encoder, str) => {
264
- if (str.length < _maxStrBSize) {
265
- // We can encode the string into the existing buffer
266
- /* c8 ignore next */
267
- const written = utf8TextEncoder.encodeInto(str, _strBuffer).written || 0;
268
- writeVarUint(encoder, written);
269
- for (let i = 0; i < written; i++) {
270
- write(encoder, _strBuffer[i]);
271
- }
272
- } else {
273
- writeVarUint8Array(encoder, encodeUtf8(str));
274
- }
308
+ if (str.length < _maxStrBSize) {
309
+ /* c8 ignore next */
310
+ const written = utf8TextEncoder.encodeInto(str, _strBuffer).written || 0;
311
+ writeVarUint(encoder, written);
312
+ for (let i = 0; i < written; i++) write(encoder, _strBuffer[i]);
313
+ } else writeVarUint8Array(encoder, encodeUtf8(str));
275
314
  };
276
-
277
315
  /**
278
- * Write a variable length string.
279
- *
280
- * @function
281
- * @param {Encoder} encoder
282
- * @param {String} str The string that is to be encoded.
283
- */
316
+ * Write a variable length string.
317
+ *
318
+ * @function
319
+ * @param {Encoder} encoder
320
+ * @param {String} str The string that is to be encoded.
321
+ */
284
322
  const _writeVarStringPolyfill = (encoder, str) => {
285
- const encodedString = unescape(encodeURIComponent(str));
286
- const len = encodedString.length;
287
- writeVarUint(encoder, len);
288
- for (let i = 0; i < len; i++) {
289
- write(encoder, /** @type {number} */ (encodedString.codePointAt(i)));
290
- }
323
+ const encodedString = unescape(encodeURIComponent(str));
324
+ const len = encodedString.length;
325
+ writeVarUint(encoder, len);
326
+ for (let i = 0; i < len; i++) write(encoder, encodedString.codePointAt(i));
291
327
  };
292
-
293
328
  /**
294
- * Write a variable length string.
295
- *
296
- * @function
297
- * @param {Encoder} encoder
298
- * @param {String} str The string that is to be encoded.
299
- */
329
+ * Write a variable length string.
330
+ *
331
+ * @function
332
+ * @param {Encoder} encoder
333
+ * @param {String} str The string that is to be encoded.
334
+ */
300
335
  /* c8 ignore next */
301
- const writeVarString = (utf8TextEncoder && /** @type {any} */ (utf8TextEncoder).encodeInto) ? _writeVarStringNative : _writeVarStringPolyfill;
302
-
303
- /**
304
- * Append fixed-length Uint8Array to the encoder.
305
- *
306
- * @function
307
- * @param {Encoder} encoder
308
- * @param {Uint8Array} uint8Array
309
- */
336
+ const writeVarString = utf8TextEncoder && utf8TextEncoder.encodeInto ? _writeVarStringNative : _writeVarStringPolyfill;
337
+ /**
338
+ * Append fixed-length Uint8Array to the encoder.
339
+ *
340
+ * @function
341
+ * @param {Encoder} encoder
342
+ * @param {Uint8Array} uint8Array
343
+ */
310
344
  const writeUint8Array = (encoder, uint8Array) => {
311
- const bufferLen = encoder.cbuf.length;
312
- const cpos = encoder.cpos;
313
- const leftCopyLen = min(bufferLen - cpos, uint8Array.length);
314
- const rightCopyLen = uint8Array.length - leftCopyLen;
315
- encoder.cbuf.set(uint8Array.subarray(0, leftCopyLen), cpos);
316
- encoder.cpos += leftCopyLen;
317
- if (rightCopyLen > 0) {
318
- // Still something to write, write right half..
319
- // Append new buffer
320
- encoder.bufs.push(encoder.cbuf);
321
- // must have at least size of remaining buffer
322
- encoder.cbuf = new Uint8Array(max(bufferLen * 2, rightCopyLen));
323
- // copy array
324
- encoder.cbuf.set(uint8Array.subarray(leftCopyLen));
325
- encoder.cpos = rightCopyLen;
326
- }
345
+ const bufferLen = encoder.cbuf.length;
346
+ const cpos = encoder.cpos;
347
+ const leftCopyLen = min(bufferLen - cpos, uint8Array.length);
348
+ const rightCopyLen = uint8Array.length - leftCopyLen;
349
+ encoder.cbuf.set(uint8Array.subarray(0, leftCopyLen), cpos);
350
+ encoder.cpos += leftCopyLen;
351
+ if (rightCopyLen > 0) {
352
+ encoder.bufs.push(encoder.cbuf);
353
+ encoder.cbuf = new Uint8Array(max(bufferLen * 2, rightCopyLen));
354
+ encoder.cbuf.set(uint8Array.subarray(leftCopyLen));
355
+ encoder.cpos = rightCopyLen;
356
+ }
327
357
  };
328
-
329
358
  /**
330
- * Append an Uint8Array to Encoder.
331
- *
332
- * @function
333
- * @param {Encoder} encoder
334
- * @param {Uint8Array} uint8Array
335
- */
359
+ * Append an Uint8Array to Encoder.
360
+ *
361
+ * @function
362
+ * @param {Encoder} encoder
363
+ * @param {Uint8Array} uint8Array
364
+ */
336
365
  const writeVarUint8Array = (encoder, uint8Array) => {
337
- writeVarUint(encoder, uint8Array.byteLength);
338
- writeUint8Array(encoder, uint8Array);
366
+ writeVarUint(encoder, uint8Array.byteLength);
367
+ writeUint8Array(encoder, uint8Array);
339
368
  };
340
369
 
370
+ //#endregion
371
+ //#region node_modules/lib0/error.js
341
372
  /**
342
- * Error helpers.
343
- *
344
- * @module error
345
- */
346
-
373
+ * Error helpers.
374
+ *
375
+ * @module error
376
+ */
347
377
  /**
348
- * @param {string} s
349
- * @return {Error}
350
- */
378
+ * @param {string} s
379
+ * @return {Error}
380
+ */
351
381
  /* c8 ignore next */
352
- const create$1 = s => new Error(s);
353
-
354
- /**
355
- * Efficient schema-less binary decoding with support for variable length encoding.
356
- *
357
- * Use [lib0/decoding] with [lib0/encoding]. Every encoding function has a corresponding decoding function.
358
- *
359
- * Encodes numbers in little-endian order (least to most significant byte order)
360
- * and is compatible with Golang's binary encoding (https://golang.org/pkg/encoding/binary/)
361
- * which is also used in Protocol Buffers.
362
- *
363
- * ```js
364
- * // encoding step
365
- * const encoder = encoding.createEncoder()
366
- * encoding.writeVarUint(encoder, 256)
367
- * encoding.writeVarString(encoder, 'Hello world!')
368
- * const buf = encoding.toUint8Array(encoder)
369
- * ```
370
- *
371
- * ```js
372
- * // decoding step
373
- * const decoder = decoding.createDecoder(buf)
374
- * decoding.readVarUint(decoder) // => 256
375
- * decoding.readVarString(decoder) // => 'Hello world!'
376
- * decoding.hasContent(decoder) // => false - all data is read
377
- * ```
378
- *
379
- * @module decoding
380
- */
381
-
382
-
383
- const errorUnexpectedEndOfArray = create$1('Unexpected end of array');
384
- const errorIntegerOutOfRange = create$1('Integer out of Range');
385
-
386
- /**
387
- * A Decoder handles the decoding of an Uint8Array.
388
- */
389
- class Decoder {
390
- /**
391
- * @param {Uint8Array} uint8Array Binary data to decode
392
- */
393
- constructor (uint8Array) {
394
- /**
395
- * Decoding target.
396
- *
397
- * @type {Uint8Array}
398
- */
399
- this.arr = uint8Array;
400
- /**
401
- * Current decoding position.
402
- *
403
- * @type {number}
404
- */
405
- this.pos = 0;
406
- }
407
- }
408
-
409
- /**
410
- * @function
411
- * @param {Uint8Array} uint8Array
412
- * @return {Decoder}
413
- */
414
- const createDecoder = uint8Array => new Decoder(uint8Array);
415
-
382
+ const create$1 = (s) => new Error(s);
383
+
384
+ //#endregion
385
+ //#region node_modules/lib0/decoding.js
386
+ /**
387
+ * Efficient schema-less binary decoding with support for variable length encoding.
388
+ *
389
+ * Use [lib0/decoding] with [lib0/encoding]. Every encoding function has a corresponding decoding function.
390
+ *
391
+ * Encodes numbers in little-endian order (least to most significant byte order)
392
+ * and is compatible with Golang's binary encoding (https://golang.org/pkg/encoding/binary/)
393
+ * which is also used in Protocol Buffers.
394
+ *
395
+ * ```js
396
+ * // encoding step
397
+ * const encoder = encoding.createEncoder()
398
+ * encoding.writeVarUint(encoder, 256)
399
+ * encoding.writeVarString(encoder, 'Hello world!')
400
+ * const buf = encoding.toUint8Array(encoder)
401
+ * ```
402
+ *
403
+ * ```js
404
+ * // decoding step
405
+ * const decoder = decoding.createDecoder(buf)
406
+ * decoding.readVarUint(decoder) // => 256
407
+ * decoding.readVarString(decoder) // => 'Hello world!'
408
+ * decoding.hasContent(decoder) // => false - all data is read
409
+ * ```
410
+ *
411
+ * @module decoding
412
+ */
413
+ const errorUnexpectedEndOfArray = create$1("Unexpected end of array");
414
+ const errorIntegerOutOfRange = create$1("Integer out of Range");
415
+ /**
416
+ * A Decoder handles the decoding of an Uint8Array.
417
+ */
418
+ var Decoder = class {
419
+ /**
420
+ * @param {Uint8Array} uint8Array Binary data to decode
421
+ */
422
+ constructor(uint8Array) {
423
+ /**
424
+ * Decoding target.
425
+ *
426
+ * @type {Uint8Array}
427
+ */
428
+ this.arr = uint8Array;
429
+ /**
430
+ * Current decoding position.
431
+ *
432
+ * @type {number}
433
+ */
434
+ this.pos = 0;
435
+ }
436
+ };
416
437
  /**
417
- * Create an Uint8Array view of the next `len` bytes and advance the position by `len`.
418
- *
419
- * Important: The Uint8Array still points to the underlying ArrayBuffer. Make sure to discard the result as soon as possible to prevent any memory leaks.
420
- * Use `buffer.copyUint8Array` to copy the result into a new Uint8Array.
421
- *
422
- * @function
423
- * @param {Decoder} decoder The decoder instance
424
- * @param {number} len The length of bytes to read
425
- * @return {Uint8Array}
426
- */
438
+ * @function
439
+ * @param {Uint8Array} uint8Array
440
+ * @return {Decoder}
441
+ */
442
+ const createDecoder = (uint8Array) => new Decoder(uint8Array);
443
+ /**
444
+ * Create an Uint8Array view of the next `len` bytes and advance the position by `len`.
445
+ *
446
+ * Important: The Uint8Array still points to the underlying ArrayBuffer. Make sure to discard the result as soon as possible to prevent any memory leaks.
447
+ * Use `buffer.copyUint8Array` to copy the result into a new Uint8Array.
448
+ *
449
+ * @function
450
+ * @param {Decoder} decoder The decoder instance
451
+ * @param {number} len The length of bytes to read
452
+ * @return {Uint8Array}
453
+ */
427
454
  const readUint8Array = (decoder, len) => {
428
- const view = new Uint8Array(decoder.arr.buffer, decoder.pos + decoder.arr.byteOffset, len);
429
- decoder.pos += len;
430
- return view
455
+ const view = new Uint8Array(decoder.arr.buffer, decoder.pos + decoder.arr.byteOffset, len);
456
+ decoder.pos += len;
457
+ return view;
431
458
  };
432
-
433
- /**
434
- * Read variable length Uint8Array.
435
- *
436
- * Important: The Uint8Array still points to the underlying ArrayBuffer. Make sure to discard the result as soon as possible to prevent any memory leaks.
437
- * Use `buffer.copyUint8Array` to copy the result into a new Uint8Array.
438
- *
439
- * @function
440
- * @param {Decoder} decoder
441
- * @return {Uint8Array}
442
- */
443
- const readVarUint8Array = decoder => readUint8Array(decoder, readVarUint(decoder));
444
-
445
- /**
446
- * Read one byte as unsigned integer.
447
- * @function
448
- * @param {Decoder} decoder The decoder instance
449
- * @return {number} Unsigned 8-bit integer
450
- */
451
- const readUint8 = decoder => decoder.arr[decoder.pos++];
452
-
453
459
  /**
454
- * Read unsigned integer (32bit) with variable length.
455
- * 1/8th of the storage is used as encoding overhead.
456
- * * numbers < 2^7 is stored in one bytlength
457
- * * numbers < 2^14 is stored in two bylength
458
- *
459
- * @function
460
- * @param {Decoder} decoder
461
- * @return {number} An unsigned integer.length
462
- */
463
- const readVarUint = decoder => {
464
- let num = 0;
465
- let mult = 1;
466
- const len = decoder.arr.length;
467
- while (decoder.pos < len) {
468
- const r = decoder.arr[decoder.pos++];
469
- // num = num | ((r & binary.BITS7) << len)
470
- num = num + (r & BITS7) * mult; // shift $r << (7*#iterations) and add it to num
471
- mult *= 128; // next iteration, shift 7 "more" to the left
472
- if (r < BIT8) {
473
- return num
474
- }
475
- /* c8 ignore start */
476
- if (num > MAX_SAFE_INTEGER) {
477
- throw errorIntegerOutOfRange
478
- }
479
- /* c8 ignore stop */
480
- }
481
- throw errorUnexpectedEndOfArray
460
+ * Read variable length Uint8Array.
461
+ *
462
+ * Important: The Uint8Array still points to the underlying ArrayBuffer. Make sure to discard the result as soon as possible to prevent any memory leaks.
463
+ * Use `buffer.copyUint8Array` to copy the result into a new Uint8Array.
464
+ *
465
+ * @function
466
+ * @param {Decoder} decoder
467
+ * @return {Uint8Array}
468
+ */
469
+ const readVarUint8Array = (decoder) => readUint8Array(decoder, readVarUint(decoder));
470
+ /**
471
+ * Read one byte as unsigned integer.
472
+ * @function
473
+ * @param {Decoder} decoder The decoder instance
474
+ * @return {number} Unsigned 8-bit integer
475
+ */
476
+ const readUint8 = (decoder) => decoder.arr[decoder.pos++];
477
+ /**
478
+ * Read unsigned integer (32bit) with variable length.
479
+ * 1/8th of the storage is used as encoding overhead.
480
+ * * numbers < 2^7 is stored in one bytlength
481
+ * * numbers < 2^14 is stored in two bylength
482
+ *
483
+ * @function
484
+ * @param {Decoder} decoder
485
+ * @return {number} An unsigned integer.length
486
+ */
487
+ const readVarUint = (decoder) => {
488
+ let num = 0;
489
+ let mult = 1;
490
+ const len = decoder.arr.length;
491
+ while (decoder.pos < len) {
492
+ const r = decoder.arr[decoder.pos++];
493
+ num = num + (r & BITS7) * mult;
494
+ mult *= 128;
495
+ if (r < BIT8) return num;
496
+ /* c8 ignore start */
497
+ if (num > MAX_SAFE_INTEGER) throw errorIntegerOutOfRange;
498
+ }
499
+ throw errorUnexpectedEndOfArray;
482
500
  };
483
-
484
501
  /**
485
- * Read signed integer (32bit) with variable length.
486
- * 1/8th of the storage is used as encoding overhead.
487
- * * numbers < 2^7 is stored in one bytlength
488
- * * numbers < 2^14 is stored in two bylength
489
- * @todo This should probably create the inverse ~num if number is negative - but this would be a breaking change.
490
- *
491
- * @function
492
- * @param {Decoder} decoder
493
- * @return {number} An unsigned integer.length
494
- */
495
- const readVarInt = decoder => {
496
- let r = decoder.arr[decoder.pos++];
497
- let num = r & BITS6;
498
- let mult = 64;
499
- const sign = (r & BIT7) > 0 ? -1 : 1;
500
- if ((r & BIT8) === 0) {
501
- // don't continue reading
502
- return sign * num
503
- }
504
- const len = decoder.arr.length;
505
- while (decoder.pos < len) {
506
- r = decoder.arr[decoder.pos++];
507
- // num = num | ((r & binary.BITS7) << len)
508
- num = num + (r & BITS7) * mult;
509
- mult *= 128;
510
- if (r < BIT8) {
511
- return sign * num
512
- }
513
- /* c8 ignore start */
514
- if (num > MAX_SAFE_INTEGER) {
515
- throw errorIntegerOutOfRange
516
- }
517
- /* c8 ignore stop */
518
- }
519
- throw errorUnexpectedEndOfArray
502
+ * Read signed integer (32bit) with variable length.
503
+ * 1/8th of the storage is used as encoding overhead.
504
+ * * numbers < 2^7 is stored in one bytlength
505
+ * * numbers < 2^14 is stored in two bylength
506
+ * @todo This should probably create the inverse ~num if number is negative - but this would be a breaking change.
507
+ *
508
+ * @function
509
+ * @param {Decoder} decoder
510
+ * @return {number} An unsigned integer.length
511
+ */
512
+ const readVarInt = (decoder) => {
513
+ let r = decoder.arr[decoder.pos++];
514
+ let num = r & BITS6;
515
+ let mult = 64;
516
+ const sign = (r & BIT7) > 0 ? -1 : 1;
517
+ if ((r & BIT8) === 0) return sign * num;
518
+ const len = decoder.arr.length;
519
+ while (decoder.pos < len) {
520
+ r = decoder.arr[decoder.pos++];
521
+ num = num + (r & BITS7) * mult;
522
+ mult *= 128;
523
+ if (r < BIT8) return sign * num;
524
+ /* c8 ignore start */
525
+ if (num > MAX_SAFE_INTEGER) throw errorIntegerOutOfRange;
526
+ }
527
+ throw errorUnexpectedEndOfArray;
520
528
  };
521
-
522
529
  /**
523
- * We don't test this function anymore as we use native decoding/encoding by default now.
524
- * Better not modify this anymore..
525
- *
526
- * Transforming utf8 to a string is pretty expensive. The code performs 10x better
527
- * when String.fromCodePoint is fed with all characters as arguments.
528
- * But most environments have a maximum number of arguments per functions.
529
- * For effiency reasons we apply a maximum of 10000 characters at once.
530
- *
531
- * @function
532
- * @param {Decoder} decoder
533
- * @return {String} The read String.
534
- */
530
+ * We don't test this function anymore as we use native decoding/encoding by default now.
531
+ * Better not modify this anymore..
532
+ *
533
+ * Transforming utf8 to a string is pretty expensive. The code performs 10x better
534
+ * when String.fromCodePoint is fed with all characters as arguments.
535
+ * But most environments have a maximum number of arguments per functions.
536
+ * For effiency reasons we apply a maximum of 10000 characters at once.
537
+ *
538
+ * @function
539
+ * @param {Decoder} decoder
540
+ * @return {String} The read String.
541
+ */
535
542
  /* c8 ignore start */
536
- const _readVarStringPolyfill = decoder => {
537
- let remainingLen = readVarUint(decoder);
538
- if (remainingLen === 0) {
539
- return ''
540
- } else {
541
- let encodedString = String.fromCodePoint(readUint8(decoder)); // remember to decrease remainingLen
542
- if (--remainingLen < 100) { // do not create a Uint8Array for small strings
543
- while (remainingLen--) {
544
- encodedString += String.fromCodePoint(readUint8(decoder));
545
- }
546
- } else {
547
- while (remainingLen > 0) {
548
- const nextLen = remainingLen < 10000 ? remainingLen : 10000;
549
- // this is dangerous, we create a fresh array view from the existing buffer
550
- const bytes = decoder.arr.subarray(decoder.pos, decoder.pos + nextLen);
551
- decoder.pos += nextLen;
552
- // Starting with ES5.1 we can supply a generic array-like object as arguments
553
- encodedString += String.fromCodePoint.apply(null, /** @type {any} */ (bytes));
554
- remainingLen -= nextLen;
555
- }
556
- }
557
- return decodeURIComponent(escape(encodedString))
558
- }
543
+ const _readVarStringPolyfill = (decoder) => {
544
+ let remainingLen = readVarUint(decoder);
545
+ if (remainingLen === 0) return "";
546
+ else {
547
+ let encodedString = String.fromCodePoint(readUint8(decoder));
548
+ if (--remainingLen < 100) while (remainingLen--) encodedString += String.fromCodePoint(readUint8(decoder));
549
+ else while (remainingLen > 0) {
550
+ const nextLen = remainingLen < 1e4 ? remainingLen : 1e4;
551
+ const bytes = decoder.arr.subarray(decoder.pos, decoder.pos + nextLen);
552
+ decoder.pos += nextLen;
553
+ encodedString += String.fromCodePoint.apply(null, bytes);
554
+ remainingLen -= nextLen;
555
+ }
556
+ return decodeURIComponent(escape(encodedString));
557
+ }
559
558
  };
560
559
  /* c8 ignore stop */
561
-
562
560
  /**
563
- * @function
564
- * @param {Decoder} decoder
565
- * @return {String} The read String
566
- */
567
- const _readVarStringNative = decoder =>
568
- /** @type any */ (utf8TextDecoder).decode(readVarUint8Array(decoder));
569
-
570
- /**
571
- * Read string of variable length
572
- * * varUint is used to store the length of the string
573
- *
574
- * @function
575
- * @param {Decoder} decoder
576
- * @return {String} The read String
577
- *
578
- */
561
+ * @function
562
+ * @param {Decoder} decoder
563
+ * @return {String} The read String
564
+ */
565
+ const _readVarStringNative = (decoder) => utf8TextDecoder.decode(readVarUint8Array(decoder));
566
+ /**
567
+ * Read string of variable length
568
+ * * varUint is used to store the length of the string
569
+ *
570
+ * @function
571
+ * @param {Decoder} decoder
572
+ * @return {String} The read String
573
+ *
574
+ */
579
575
  /* c8 ignore next */
580
576
  const readVarString = utf8TextDecoder ? _readVarStringNative : _readVarStringPolyfill;
581
-
582
577
  /**
583
- * Look ahead and read varString without incrementing position
584
- *
585
- * @function
586
- * @param {Decoder} decoder
587
- * @return {string}
588
- */
589
- const peekVarString = decoder => {
590
- const pos = decoder.pos;
591
- const s = readVarString(decoder);
592
- decoder.pos = pos;
593
- return s
578
+ * Look ahead and read varString without incrementing position
579
+ *
580
+ * @function
581
+ * @param {Decoder} decoder
582
+ * @return {string}
583
+ */
584
+ const peekVarString = (decoder) => {
585
+ const pos = decoder.pos;
586
+ const s = readVarString(decoder);
587
+ decoder.pos = pos;
588
+ return s;
594
589
  };
595
590
 
591
+ //#endregion
592
+ //#region node_modules/lib0/time.js
596
593
  /**
597
- * Utility module to work with time.
598
- *
599
- * @module time
600
- */
601
-
602
-
603
- /**
604
- * Return current unix time.
605
- *
606
- * @return {number}
607
- */
594
+ * Return current unix time.
595
+ *
596
+ * @return {number}
597
+ */
608
598
  const getUnixTime = Date.now;
609
599
 
610
- /**
611
- * Utility module to work with key-value stores.
612
- *
613
- * @module map
614
- */
615
-
616
- /**
617
- * Creates a new Map instance.
618
- *
619
- * @function
620
- * @return {Map<any, any>}
621
- *
622
- * @function
623
- */
624
- const create = () => new Map();
625
-
626
- /**
627
- * Get map property. Create T if property is undefined and set T on map.
628
- *
629
- * ```js
630
- * const listeners = map.setIfUndefined(events, 'eventName', set.create)
631
- * listeners.add(listener)
632
- * ```
633
- *
634
- * @function
635
- * @template {Map<any, any>} MAP
636
- * @template {MAP extends Map<any,infer V> ? function():V : unknown} CF
637
- * @param {MAP} map
638
- * @param {MAP extends Map<infer K,any> ? K : unknown} key
639
- * @param {CF} createT
640
- * @return {ReturnType<CF>}
641
- */
600
+ //#endregion
601
+ //#region node_modules/lib0/map.js
602
+ /**
603
+ * Utility module to work with key-value stores.
604
+ *
605
+ * @module map
606
+ */
607
+ /**
608
+ * Creates a new Map instance.
609
+ *
610
+ * @function
611
+ * @return {Map<any, any>}
612
+ *
613
+ * @function
614
+ */
615
+ const create = () => /* @__PURE__ */ new Map();
616
+ /**
617
+ * Get map property. Create T if property is undefined and set T on map.
618
+ *
619
+ * ```js
620
+ * const listeners = map.setIfUndefined(events, 'eventName', set.create)
621
+ * listeners.add(listener)
622
+ * ```
623
+ *
624
+ * @function
625
+ * @template {Map<any, any>} MAP
626
+ * @template {MAP extends Map<any,infer V> ? function():V : unknown} CF
627
+ * @param {MAP} map
628
+ * @param {MAP extends Map<infer K,any> ? K : unknown} key
629
+ * @param {CF} createT
630
+ * @return {ReturnType<CF>}
631
+ */
642
632
  const setIfUndefined = (map, key, createT) => {
643
- let set = map.get(key);
644
- if (set === undefined) {
645
- map.set(key, set = createT());
646
- }
647
- return set
633
+ let set = map.get(key);
634
+ if (set === void 0) map.set(key, set = createT());
635
+ return set;
648
636
  };
649
637
 
638
+ //#endregion
639
+ //#region node_modules/lib0/observable.js
650
640
  /**
651
- * Observable class prototype.
652
- *
653
- * @module observable
654
- */
655
-
656
-
641
+ * Observable class prototype.
642
+ *
643
+ * @module observable
644
+ */
657
645
  /* c8 ignore start */
658
646
  /**
659
- * Handles named events.
660
- *
661
- * @deprecated
662
- * @template N
663
- */
664
- class Observable {
665
- constructor () {
666
- /**
667
- * Some desc.
668
- * @type {Map<N, any>}
669
- */
670
- this._observers = create();
671
- }
672
-
673
- /**
674
- * @param {N} name
675
- * @param {function} f
676
- */
677
- on (name, f) {
678
- setIfUndefined(this._observers, name, create$2).add(f);
679
- }
680
-
681
- /**
682
- * @param {N} name
683
- * @param {function} f
684
- */
685
- once (name, f) {
686
- /**
687
- * @param {...any} args
688
- */
689
- const _f = (...args) => {
690
- this.off(name, _f);
691
- f(...args);
692
- };
693
- this.on(name, _f);
694
- }
695
-
696
- /**
697
- * @param {N} name
698
- * @param {function} f
699
- */
700
- off (name, f) {
701
- const observers = this._observers.get(name);
702
- if (observers !== undefined) {
703
- observers.delete(f);
704
- if (observers.size === 0) {
705
- this._observers.delete(name);
706
- }
707
- }
708
- }
709
-
710
- /**
711
- * Emit a named event. All registered event listeners that listen to the
712
- * specified name will receive the event.
713
- *
714
- * @todo This should catch exceptions
715
- *
716
- * @param {N} name The event name.
717
- * @param {Array<any>} args The arguments that are applied to the event listener.
718
- */
719
- emit (name, args) {
720
- // copy all listeners to an array first to make sure that no event is emitted to listeners that are subscribed while the event handler is called.
721
- return from((this._observers.get(name) || create()).values()).forEach(f => f(...args))
722
- }
723
-
724
- destroy () {
725
- this._observers = create();
726
- }
727
- }
647
+ * Handles named events.
648
+ *
649
+ * @deprecated
650
+ * @template N
651
+ */
652
+ var Observable = class {
653
+ constructor() {
654
+ /**
655
+ * Some desc.
656
+ * @type {Map<N, any>}
657
+ */
658
+ this._observers = create();
659
+ }
660
+ /**
661
+ * @param {N} name
662
+ * @param {function} f
663
+ */
664
+ on(name, f) {
665
+ setIfUndefined(this._observers, name, create$2).add(f);
666
+ }
667
+ /**
668
+ * @param {N} name
669
+ * @param {function} f
670
+ */
671
+ once(name, f) {
672
+ /**
673
+ * @param {...any} args
674
+ */
675
+ const _f = (...args) => {
676
+ this.off(name, _f);
677
+ f(...args);
678
+ };
679
+ this.on(name, _f);
680
+ }
681
+ /**
682
+ * @param {N} name
683
+ * @param {function} f
684
+ */
685
+ off(name, f) {
686
+ const observers = this._observers.get(name);
687
+ if (observers !== void 0) {
688
+ observers.delete(f);
689
+ if (observers.size === 0) this._observers.delete(name);
690
+ }
691
+ }
692
+ /**
693
+ * Emit a named event. All registered event listeners that listen to the
694
+ * specified name will receive the event.
695
+ *
696
+ * @todo This should catch exceptions
697
+ *
698
+ * @param {N} name The event name.
699
+ * @param {Array<any>} args The arguments that are applied to the event listener.
700
+ */
701
+ emit(name, args) {
702
+ return from((this._observers.get(name) || create()).values()).forEach((f) => f(...args));
703
+ }
704
+ destroy() {
705
+ this._observers = create();
706
+ }
707
+ };
728
708
  /* c8 ignore end */
729
709
 
710
+ //#endregion
711
+ //#region node_modules/lib0/object.js
730
712
  /**
731
- * Utility functions for working with EcmaScript objects.
732
- *
733
- * @module object
734
- */
735
-
736
-
737
- /**
738
- * @param {Object<string,any>} obj
739
- */
713
+ * @param {Object<string,any>} obj
714
+ */
740
715
  const keys = Object.keys;
741
-
742
716
  /**
743
- * @deprecated use object.size instead
744
- * @param {Object<string,any>} obj
745
- * @return {number}
746
- */
747
- const length = obj => keys(obj).length;
748
-
749
- /**
750
- * Calls `Object.prototype.hasOwnProperty`.
751
- *
752
- * @param {any} obj
753
- * @param {string|symbol} key
754
- * @return {boolean}
755
- */
717
+ * @deprecated use object.size instead
718
+ * @param {Object<string,any>} obj
719
+ * @return {number}
720
+ */
721
+ const length = (obj) => keys(obj).length;
722
+ /**
723
+ * Calls `Object.prototype.hasOwnProperty`.
724
+ *
725
+ * @param {any} obj
726
+ * @param {string|symbol} key
727
+ * @return {boolean}
728
+ */
756
729
  const hasProperty = (obj, key) => Object.prototype.hasOwnProperty.call(obj, key);
757
730
 
731
+ //#endregion
732
+ //#region node_modules/lib0/function.js
758
733
  /**
759
- * Common functions and function call helpers.
760
- *
761
- * @module function
762
- */
763
-
764
-
734
+ * Common functions and function call helpers.
735
+ *
736
+ * @module function
737
+ */
765
738
  /**
766
- * @template T
767
- *
768
- * @param {T} a
769
- * @param {T} b
770
- * @return {boolean}
771
- */
739
+ * @template T
740
+ *
741
+ * @param {T} a
742
+ * @param {T} b
743
+ * @return {boolean}
744
+ */
772
745
  const equalityStrict = (a, b) => a === b;
773
-
774
746
  /* c8 ignore start */
775
-
776
747
  /**
777
- * @param {any} a
778
- * @param {any} b
779
- * @return {boolean}
780
- */
748
+ * @param {any} a
749
+ * @param {any} b
750
+ * @return {boolean}
751
+ */
781
752
  const equalityDeep = (a, b) => {
782
- if (a == null || b == null) {
783
- return equalityStrict(a, b)
784
- }
785
- if (a.constructor !== b.constructor) {
786
- return false
787
- }
788
- if (a === b) {
789
- return true
790
- }
791
- switch (a.constructor) {
792
- case ArrayBuffer:
793
- a = new Uint8Array(a);
794
- b = new Uint8Array(b);
795
- // eslint-disable-next-line no-fallthrough
796
- case Uint8Array: {
797
- if (a.byteLength !== b.byteLength) {
798
- return false
799
- }
800
- for (let i = 0; i < a.length; i++) {
801
- if (a[i] !== b[i]) {
802
- return false
803
- }
804
- }
805
- break
806
- }
807
- case Set: {
808
- if (a.size !== b.size) {
809
- return false
810
- }
811
- for (const value of a) {
812
- if (!b.has(value)) {
813
- return false
814
- }
815
- }
816
- break
817
- }
818
- case Map: {
819
- if (a.size !== b.size) {
820
- return false
821
- }
822
- for (const key of a.keys()) {
823
- if (!b.has(key) || !equalityDeep(a.get(key), b.get(key))) {
824
- return false
825
- }
826
- }
827
- break
828
- }
829
- case Object:
830
- if (length(a) !== length(b)) {
831
- return false
832
- }
833
- for (const key in a) {
834
- if (!hasProperty(a, key) || !equalityDeep(a[key], b[key])) {
835
- return false
836
- }
837
- }
838
- break
839
- case Array:
840
- if (a.length !== b.length) {
841
- return false
842
- }
843
- for (let i = 0; i < a.length; i++) {
844
- if (!equalityDeep(a[i], b[i])) {
845
- return false
846
- }
847
- }
848
- break
849
- default:
850
- return false
851
- }
852
- return true
753
+ if (a == null || b == null) return equalityStrict(a, b);
754
+ if (a.constructor !== b.constructor) return false;
755
+ if (a === b) return true;
756
+ switch (a.constructor) {
757
+ case ArrayBuffer:
758
+ a = new Uint8Array(a);
759
+ b = new Uint8Array(b);
760
+ case Uint8Array:
761
+ if (a.byteLength !== b.byteLength) return false;
762
+ for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
763
+ break;
764
+ case Set:
765
+ if (a.size !== b.size) return false;
766
+ for (const value of a) if (!b.has(value)) return false;
767
+ break;
768
+ case Map:
769
+ if (a.size !== b.size) return false;
770
+ for (const key of a.keys()) if (!b.has(key) || !equalityDeep(a.get(key), b.get(key))) return false;
771
+ break;
772
+ case Object:
773
+ if (length(a) !== length(b)) return false;
774
+ for (const key in a) if (!hasProperty(a, key) || !equalityDeep(a[key], b[key])) return false;
775
+ break;
776
+ case Array:
777
+ if (a.length !== b.length) return false;
778
+ for (let i = 0; i < a.length; i++) if (!equalityDeep(a[i], b[i])) return false;
779
+ break;
780
+ default: return false;
781
+ }
782
+ return true;
783
+ };
784
+ /* c8 ignore stop */
785
+ const isArray = isArray$1;
786
+
787
+ //#endregion
788
+ //#region node_modules/y-protocols/awareness.js
789
+ /**
790
+ * @module awareness-protocol
791
+ */
792
+ const outdatedTimeout = 3e4;
793
+ /**
794
+ * @typedef {Object} MetaClientState
795
+ * @property {number} MetaClientState.clock
796
+ * @property {number} MetaClientState.lastUpdated unix timestamp
797
+ */
798
+ /**
799
+ * The Awareness class implements a simple shared state protocol that can be used for non-persistent data like awareness information
800
+ * (cursor, username, status, ..). Each client can update its own local state and listen to state changes of
801
+ * remote clients. Every client may set a state of a remote peer to `null` to mark the client as offline.
802
+ *
803
+ * Each client is identified by a unique client id (something we borrow from `doc.clientID`). A client can override
804
+ * its own state by propagating a message with an increasing timestamp (`clock`). If such a message is received, it is
805
+ * applied if the known state of that client is older than the new state (`clock < newClock`). If a client thinks that
806
+ * a remote client is offline, it may propagate a message with
807
+ * `{ clock: currentClientClock, state: null, client: remoteClient }`. If such a
808
+ * message is received, and the known clock of that client equals the received clock, it will override the state with `null`.
809
+ *
810
+ * Before a client disconnects, it should propagate a `null` state with an updated clock.
811
+ *
812
+ * Awareness states must be updated every 30 seconds. Otherwise the Awareness instance will delete the client state.
813
+ *
814
+ * @extends {Observable<string>}
815
+ */
816
+ var Awareness = class extends Observable {
817
+ /**
818
+ * @param {Y.Doc} doc
819
+ */
820
+ constructor(doc) {
821
+ super();
822
+ this.doc = doc;
823
+ /**
824
+ * @type {number}
825
+ */
826
+ this.clientID = doc.clientID;
827
+ /**
828
+ * Maps from client id to client state
829
+ * @type {Map<number, Object<string, any>>}
830
+ */
831
+ this.states = /* @__PURE__ */ new Map();
832
+ /**
833
+ * @type {Map<number, MetaClientState>}
834
+ */
835
+ this.meta = /* @__PURE__ */ new Map();
836
+ this._checkInterval = setInterval(() => {
837
+ const now = getUnixTime();
838
+ if (this.getLocalState() !== null && outdatedTimeout / 2 <= now - this.meta.get(this.clientID).lastUpdated) this.setLocalState(this.getLocalState());
839
+ /**
840
+ * @type {Array<number>}
841
+ */
842
+ const remove = [];
843
+ this.meta.forEach((meta, clientid) => {
844
+ if (clientid !== this.clientID && outdatedTimeout <= now - meta.lastUpdated && this.states.has(clientid)) remove.push(clientid);
845
+ });
846
+ if (remove.length > 0) removeAwarenessStates(this, remove, "timeout");
847
+ }, floor(outdatedTimeout / 10));
848
+ doc.on("destroy", () => {
849
+ this.destroy();
850
+ });
851
+ this.setLocalState({});
852
+ }
853
+ destroy() {
854
+ this.emit("destroy", [this]);
855
+ this.setLocalState(null);
856
+ super.destroy();
857
+ clearInterval(this._checkInterval);
858
+ }
859
+ /**
860
+ * @return {Object<string,any>|null}
861
+ */
862
+ getLocalState() {
863
+ return this.states.get(this.clientID) || null;
864
+ }
865
+ /**
866
+ * @param {Object<string,any>|null} state
867
+ */
868
+ setLocalState(state) {
869
+ const clientID = this.clientID;
870
+ const currLocalMeta = this.meta.get(clientID);
871
+ const clock = currLocalMeta === void 0 ? 0 : currLocalMeta.clock + 1;
872
+ const prevState = this.states.get(clientID);
873
+ if (state === null) this.states.delete(clientID);
874
+ else this.states.set(clientID, state);
875
+ this.meta.set(clientID, {
876
+ clock,
877
+ lastUpdated: getUnixTime()
878
+ });
879
+ const added = [];
880
+ const updated = [];
881
+ const filteredUpdated = [];
882
+ const removed = [];
883
+ if (state === null) removed.push(clientID);
884
+ else if (prevState == null) {
885
+ if (state != null) added.push(clientID);
886
+ } else {
887
+ updated.push(clientID);
888
+ if (!equalityDeep(prevState, state)) filteredUpdated.push(clientID);
889
+ }
890
+ if (added.length > 0 || filteredUpdated.length > 0 || removed.length > 0) this.emit("change", [{
891
+ added,
892
+ updated: filteredUpdated,
893
+ removed
894
+ }, "local"]);
895
+ this.emit("update", [{
896
+ added,
897
+ updated,
898
+ removed
899
+ }, "local"]);
900
+ }
901
+ /**
902
+ * @param {string} field
903
+ * @param {any} value
904
+ */
905
+ setLocalStateField(field, value) {
906
+ const state = this.getLocalState();
907
+ if (state !== null) this.setLocalState({
908
+ ...state,
909
+ [field]: value
910
+ });
911
+ }
912
+ /**
913
+ * @return {Map<number,Object<string,any>>}
914
+ */
915
+ getStates() {
916
+ return this.states;
917
+ }
853
918
  };
854
-
855
- /**
856
- * @module awareness-protocol
857
- */
858
-
859
-
860
- const outdatedTimeout = 30000;
861
-
862
- /**
863
- * @typedef {Object} MetaClientState
864
- * @property {number} MetaClientState.clock
865
- * @property {number} MetaClientState.lastUpdated unix timestamp
866
- */
867
-
868
- /**
869
- * The Awareness class implements a simple shared state protocol that can be used for non-persistent data like awareness information
870
- * (cursor, username, status, ..). Each client can update its own local state and listen to state changes of
871
- * remote clients. Every client may set a state of a remote peer to `null` to mark the client as offline.
872
- *
873
- * Each client is identified by a unique client id (something we borrow from `doc.clientID`). A client can override
874
- * its own state by propagating a message with an increasing timestamp (`clock`). If such a message is received, it is
875
- * applied if the known state of that client is older than the new state (`clock < newClock`). If a client thinks that
876
- * a remote client is offline, it may propagate a message with
877
- * `{ clock: currentClientClock, state: null, client: remoteClient }`. If such a
878
- * message is received, and the known clock of that client equals the received clock, it will override the state with `null`.
879
- *
880
- * Before a client disconnects, it should propagate a `null` state with an updated clock.
881
- *
882
- * Awareness states must be updated every 30 seconds. Otherwise the Awareness instance will delete the client state.
883
- *
884
- * @extends {Observable<string>}
885
- */
886
- class Awareness extends Observable {
887
- /**
888
- * @param {Y.Doc} doc
889
- */
890
- constructor (doc) {
891
- super();
892
- this.doc = doc;
893
- /**
894
- * @type {number}
895
- */
896
- this.clientID = doc.clientID;
897
- /**
898
- * Maps from client id to client state
899
- * @type {Map<number, Object<string, any>>}
900
- */
901
- this.states = new Map();
902
- /**
903
- * @type {Map<number, MetaClientState>}
904
- */
905
- this.meta = new Map();
906
- this._checkInterval = /** @type {any} */ (setInterval(() => {
907
- const now = getUnixTime();
908
- if (this.getLocalState() !== null && (outdatedTimeout / 2 <= now - /** @type {{lastUpdated:number}} */ (this.meta.get(this.clientID)).lastUpdated)) {
909
- // renew local clock
910
- this.setLocalState(this.getLocalState());
911
- }
912
- /**
913
- * @type {Array<number>}
914
- */
915
- const remove = [];
916
- this.meta.forEach((meta, clientid) => {
917
- if (clientid !== this.clientID && outdatedTimeout <= now - meta.lastUpdated && this.states.has(clientid)) {
918
- remove.push(clientid);
919
- }
920
- });
921
- if (remove.length > 0) {
922
- removeAwarenessStates(this, remove, 'timeout');
923
- }
924
- }, floor(outdatedTimeout / 10)));
925
- doc.on('destroy', () => {
926
- this.destroy();
927
- });
928
- this.setLocalState({});
929
- }
930
-
931
- destroy () {
932
- this.emit('destroy', [this]);
933
- this.setLocalState(null);
934
- super.destroy();
935
- clearInterval(this._checkInterval);
936
- }
937
-
938
- /**
939
- * @return {Object<string,any>|null}
940
- */
941
- getLocalState () {
942
- return this.states.get(this.clientID) || null
943
- }
944
-
945
- /**
946
- * @param {Object<string,any>|null} state
947
- */
948
- setLocalState (state) {
949
- const clientID = this.clientID;
950
- const currLocalMeta = this.meta.get(clientID);
951
- const clock = currLocalMeta === undefined ? 0 : currLocalMeta.clock + 1;
952
- const prevState = this.states.get(clientID);
953
- if (state === null) {
954
- this.states.delete(clientID);
955
- } else {
956
- this.states.set(clientID, state);
957
- }
958
- this.meta.set(clientID, {
959
- clock,
960
- lastUpdated: getUnixTime()
961
- });
962
- const added = [];
963
- const updated = [];
964
- const filteredUpdated = [];
965
- const removed = [];
966
- if (state === null) {
967
- removed.push(clientID);
968
- } else if (prevState == null) {
969
- if (state != null) {
970
- added.push(clientID);
971
- }
972
- } else {
973
- updated.push(clientID);
974
- if (!equalityDeep(prevState, state)) {
975
- filteredUpdated.push(clientID);
976
- }
977
- }
978
- if (added.length > 0 || filteredUpdated.length > 0 || removed.length > 0) {
979
- this.emit('change', [{ added, updated: filteredUpdated, removed }, 'local']);
980
- }
981
- this.emit('update', [{ added, updated, removed }, 'local']);
982
- }
983
-
984
- /**
985
- * @param {string} field
986
- * @param {any} value
987
- */
988
- setLocalStateField (field, value) {
989
- const state = this.getLocalState();
990
- if (state !== null) {
991
- this.setLocalState({
992
- ...state,
993
- [field]: value
994
- });
995
- }
996
- }
997
-
998
- /**
999
- * @return {Map<number,Object<string,any>>}
1000
- */
1001
- getStates () {
1002
- return this.states
1003
- }
1004
- }
1005
-
1006
919
  /**
1007
- * Mark (remote) clients as inactive and remove them from the list of active peers.
1008
- * This change will be propagated to remote clients.
1009
- *
1010
- * @param {Awareness} awareness
1011
- * @param {Array<number>} clients
1012
- * @param {any} origin
1013
- */
920
+ * Mark (remote) clients as inactive and remove them from the list of active peers.
921
+ * This change will be propagated to remote clients.
922
+ *
923
+ * @param {Awareness} awareness
924
+ * @param {Array<number>} clients
925
+ * @param {any} origin
926
+ */
1014
927
  const removeAwarenessStates = (awareness, clients, origin) => {
1015
- const removed = [];
1016
- for (let i = 0; i < clients.length; i++) {
1017
- const clientID = clients[i];
1018
- if (awareness.states.has(clientID)) {
1019
- awareness.states.delete(clientID);
1020
- if (clientID === awareness.clientID) {
1021
- const curMeta = /** @type {MetaClientState} */ (awareness.meta.get(clientID));
1022
- awareness.meta.set(clientID, {
1023
- clock: curMeta.clock + 1,
1024
- lastUpdated: getUnixTime()
1025
- });
1026
- }
1027
- removed.push(clientID);
1028
- }
1029
- }
1030
- if (removed.length > 0) {
1031
- awareness.emit('change', [{ added: [], updated: [], removed }, origin]);
1032
- awareness.emit('update', [{ added: [], updated: [], removed }, origin]);
1033
- }
928
+ const removed = [];
929
+ for (let i = 0; i < clients.length; i++) {
930
+ const clientID = clients[i];
931
+ if (awareness.states.has(clientID)) {
932
+ awareness.states.delete(clientID);
933
+ if (clientID === awareness.clientID) {
934
+ const curMeta = awareness.meta.get(clientID);
935
+ awareness.meta.set(clientID, {
936
+ clock: curMeta.clock + 1,
937
+ lastUpdated: getUnixTime()
938
+ });
939
+ }
940
+ removed.push(clientID);
941
+ }
942
+ }
943
+ if (removed.length > 0) {
944
+ awareness.emit("change", [{
945
+ added: [],
946
+ updated: [],
947
+ removed
948
+ }, origin]);
949
+ awareness.emit("update", [{
950
+ added: [],
951
+ updated: [],
952
+ removed
953
+ }, origin]);
954
+ }
1034
955
  };
1035
-
1036
956
  /**
1037
- * @param {Awareness} awareness
1038
- * @param {Array<number>} clients
1039
- * @return {Uint8Array}
1040
- */
957
+ * @param {Awareness} awareness
958
+ * @param {Array<number>} clients
959
+ * @return {Uint8Array}
960
+ */
1041
961
  const encodeAwarenessUpdate = (awareness, clients, states = awareness.states) => {
1042
- const len = clients.length;
1043
- const encoder = createEncoder();
1044
- writeVarUint(encoder, len);
1045
- for (let i = 0; i < len; i++) {
1046
- const clientID = clients[i];
1047
- const state = states.get(clientID) || null;
1048
- const clock = /** @type {MetaClientState} */ (awareness.meta.get(clientID)).clock;
1049
- writeVarUint(encoder, clientID);
1050
- writeVarUint(encoder, clock);
1051
- writeVarString(encoder, JSON.stringify(state));
1052
- }
1053
- return toUint8Array(encoder)
962
+ const len = clients.length;
963
+ const encoder = createEncoder();
964
+ writeVarUint(encoder, len);
965
+ for (let i = 0; i < len; i++) {
966
+ const clientID = clients[i];
967
+ const state = states.get(clientID) || null;
968
+ const clock = awareness.meta.get(clientID).clock;
969
+ writeVarUint(encoder, clientID);
970
+ writeVarUint(encoder, clock);
971
+ writeVarString(encoder, JSON.stringify(state));
972
+ }
973
+ return toUint8Array(encoder);
1054
974
  };
1055
-
1056
975
  /**
1057
- * @param {Awareness} awareness
1058
- * @param {Uint8Array} update
1059
- * @param {any} origin This will be added to the emitted change event
1060
- */
976
+ * @param {Awareness} awareness
977
+ * @param {Uint8Array} update
978
+ * @param {any} origin This will be added to the emitted change event
979
+ */
1061
980
  const applyAwarenessUpdate = (awareness, update, origin) => {
1062
- const decoder = createDecoder(update);
1063
- const timestamp = getUnixTime();
1064
- const added = [];
1065
- const updated = [];
1066
- const filteredUpdated = [];
1067
- const removed = [];
1068
- const len = readVarUint(decoder);
1069
- for (let i = 0; i < len; i++) {
1070
- const clientID = readVarUint(decoder);
1071
- let clock = readVarUint(decoder);
1072
- const state = JSON.parse(readVarString(decoder));
1073
- const clientMeta = awareness.meta.get(clientID);
1074
- const prevState = awareness.states.get(clientID);
1075
- const currClock = clientMeta === undefined ? 0 : clientMeta.clock;
1076
- if (currClock < clock || (currClock === clock && state === null && awareness.states.has(clientID))) {
1077
- if (state === null) {
1078
- // never let a remote client remove this local state
1079
- if (clientID === awareness.clientID && awareness.getLocalState() != null) {
1080
- // remote client removed the local state. Do not remote state. Broadcast a message indicating
1081
- // that this client still exists by increasing the clock
1082
- clock++;
1083
- } else {
1084
- awareness.states.delete(clientID);
1085
- }
1086
- } else {
1087
- awareness.states.set(clientID, state);
1088
- }
1089
- awareness.meta.set(clientID, {
1090
- clock,
1091
- lastUpdated: timestamp
1092
- });
1093
- if (clientMeta === undefined && state !== null) {
1094
- added.push(clientID);
1095
- } else if (clientMeta !== undefined && state === null) {
1096
- removed.push(clientID);
1097
- } else if (state !== null) {
1098
- if (!equalityDeep(state, prevState)) {
1099
- filteredUpdated.push(clientID);
1100
- }
1101
- updated.push(clientID);
1102
- }
1103
- }
1104
- }
1105
- if (added.length > 0 || filteredUpdated.length > 0 || removed.length > 0) {
1106
- awareness.emit('change', [{
1107
- added, updated: filteredUpdated, removed
1108
- }, origin]);
1109
- }
1110
- if (added.length > 0 || updated.length > 0 || removed.length > 0) {
1111
- awareness.emit('update', [{
1112
- added, updated, removed
1113
- }, origin]);
1114
- }
981
+ const decoder = createDecoder(update);
982
+ const timestamp = getUnixTime();
983
+ const added = [];
984
+ const updated = [];
985
+ const filteredUpdated = [];
986
+ const removed = [];
987
+ const len = readVarUint(decoder);
988
+ for (let i = 0; i < len; i++) {
989
+ const clientID = readVarUint(decoder);
990
+ let clock = readVarUint(decoder);
991
+ const state = JSON.parse(readVarString(decoder));
992
+ const clientMeta = awareness.meta.get(clientID);
993
+ const prevState = awareness.states.get(clientID);
994
+ const currClock = clientMeta === void 0 ? 0 : clientMeta.clock;
995
+ if (currClock < clock || currClock === clock && state === null && awareness.states.has(clientID)) {
996
+ if (state === null) if (clientID === awareness.clientID && awareness.getLocalState() != null) clock++;
997
+ else awareness.states.delete(clientID);
998
+ else awareness.states.set(clientID, state);
999
+ awareness.meta.set(clientID, {
1000
+ clock,
1001
+ lastUpdated: timestamp
1002
+ });
1003
+ if (clientMeta === void 0 && state !== null) added.push(clientID);
1004
+ else if (clientMeta !== void 0 && state === null) removed.push(clientID);
1005
+ else if (state !== null) {
1006
+ if (!equalityDeep(state, prevState)) filteredUpdated.push(clientID);
1007
+ updated.push(clientID);
1008
+ }
1009
+ }
1010
+ }
1011
+ if (added.length > 0 || filteredUpdated.length > 0 || removed.length > 0) awareness.emit("change", [{
1012
+ added,
1013
+ updated: filteredUpdated,
1014
+ removed
1015
+ }, origin]);
1016
+ if (added.length > 0 || updated.length > 0 || removed.length > 0) awareness.emit("update", [{
1017
+ added,
1018
+ updated,
1019
+ removed
1020
+ }, origin]);
1115
1021
  };
1116
1022
 
1117
- class EventEmitter {
1118
- constructor() {
1119
- // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
1120
- this.callbacks = {};
1121
- }
1122
- // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
1123
- on(event, fn) {
1124
- if (!this.callbacks[event]) {
1125
- this.callbacks[event] = [];
1126
- }
1127
- this.callbacks[event].push(fn);
1128
- return this;
1129
- }
1130
- emit(event, ...args) {
1131
- const callbacks = this.callbacks[event];
1132
- if (callbacks) {
1133
- callbacks.forEach((callback) => callback.apply(this, args));
1134
- }
1135
- return this;
1136
- }
1137
- // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
1138
- off(event, fn) {
1139
- const callbacks = this.callbacks[event];
1140
- if (callbacks) {
1141
- if (fn) {
1142
- this.callbacks[event] = callbacks.filter((callback) => callback !== fn);
1143
- }
1144
- else {
1145
- delete this.callbacks[event];
1146
- }
1147
- }
1148
- return this;
1149
- }
1150
- removeAllListeners() {
1151
- this.callbacks = {};
1152
- }
1153
- }
1154
-
1155
- class IncomingMessage {
1156
- constructor(data) {
1157
- this.data = data;
1158
- this.encoder = createEncoder();
1159
- this.decoder = createDecoder(new Uint8Array(this.data));
1160
- }
1161
- peekVarString() {
1162
- return peekVarString(this.decoder);
1163
- }
1164
- readVarUint() {
1165
- return readVarUint(this.decoder);
1166
- }
1167
- readVarString() {
1168
- return readVarString(this.decoder);
1169
- }
1170
- readVarUint8Array() {
1171
- return readVarUint8Array(this.decoder);
1172
- }
1173
- writeVarUint(type) {
1174
- return writeVarUint(this.encoder, type);
1175
- }
1176
- writeVarString(string) {
1177
- return writeVarString(this.encoder, string);
1178
- }
1179
- writeVarUint8Array(data) {
1180
- return writeVarUint8Array(this.encoder, data);
1181
- }
1182
- length() {
1183
- return length$1(this.encoder);
1184
- }
1185
- }
1186
-
1187
- exports.MessageType = void 0;
1188
- (function (MessageType) {
1189
- MessageType[MessageType["Sync"] = 0] = "Sync";
1190
- MessageType[MessageType["Awareness"] = 1] = "Awareness";
1191
- MessageType[MessageType["Auth"] = 2] = "Auth";
1192
- MessageType[MessageType["QueryAwareness"] = 3] = "QueryAwareness";
1193
- MessageType[MessageType["Stateless"] = 5] = "Stateless";
1194
- MessageType[MessageType["CLOSE"] = 7] = "CLOSE";
1195
- MessageType[MessageType["SyncStatus"] = 8] = "SyncStatus";
1196
- })(exports.MessageType || (exports.MessageType = {}));
1197
- exports.WebSocketStatus = void 0;
1198
- (function (WebSocketStatus) {
1199
- WebSocketStatus["Connecting"] = "connecting";
1200
- WebSocketStatus["Connected"] = "connected";
1201
- WebSocketStatus["Disconnected"] = "disconnected";
1202
- })(exports.WebSocketStatus || (exports.WebSocketStatus = {}));
1203
-
1204
- class OutgoingMessage {
1205
- constructor() {
1206
- this.encoder = createEncoder();
1207
- }
1208
- get(args) {
1209
- return args.encoder;
1210
- }
1211
- toUint8Array() {
1212
- return toUint8Array(this.encoder);
1213
- }
1214
- }
1215
-
1216
- class CloseMessage extends OutgoingMessage {
1217
- constructor() {
1218
- super(...arguments);
1219
- this.type = exports.MessageType.CLOSE;
1220
- this.description = "Ask the server to close the connection";
1221
- }
1222
- get(args) {
1223
- writeVarString(this.encoder, args.documentName);
1224
- writeVarUint(this.encoder, this.type);
1225
- return this.encoder;
1226
- }
1227
- }
1228
-
1229
- class HocuspocusProviderWebsocket extends EventEmitter {
1230
- constructor(configuration) {
1231
- super();
1232
- this.messageQueue = [];
1233
- this.configuration = {
1234
- url: "",
1235
- autoConnect: true,
1236
- // @ts-ignore
1237
- document: undefined,
1238
- WebSocketPolyfill: undefined,
1239
- // TODO: this should depend on awareness.outdatedTime
1240
- messageReconnectTimeout: 30000,
1241
- // 1 second
1242
- delay: 1000,
1243
- // instant
1244
- initialDelay: 0,
1245
- // double the delay each time
1246
- factor: 2,
1247
- // unlimited retries
1248
- maxAttempts: 0,
1249
- // wait at least 1 second
1250
- minDelay: 1000,
1251
- // at least every 30 seconds
1252
- maxDelay: 30000,
1253
- // randomize
1254
- jitter: true,
1255
- // retry forever
1256
- timeout: 0,
1257
- onOpen: () => null,
1258
- onConnect: () => null,
1259
- onMessage: () => null,
1260
- onOutgoingMessage: () => null,
1261
- onStatus: () => null,
1262
- onDisconnect: () => null,
1263
- onClose: () => null,
1264
- onDestroy: () => null,
1265
- onAwarenessUpdate: () => null,
1266
- onAwarenessChange: () => null,
1267
- handleTimeout: null,
1268
- providerMap: new Map(),
1269
- };
1270
- this.webSocket = null;
1271
- this.webSocketHandlers = {};
1272
- this.shouldConnect = true;
1273
- this.status = exports.WebSocketStatus.Disconnected;
1274
- this.lastMessageReceived = 0;
1275
- this.identifier = 0;
1276
- this.intervals = {
1277
- connectionChecker: null,
1278
- };
1279
- this.connectionAttempt = null;
1280
- this.receivedOnOpenPayload = undefined;
1281
- this.closeTries = 0;
1282
- this.setConfiguration(configuration);
1283
- this.configuration.WebSocketPolyfill = configuration.WebSocketPolyfill
1284
- ? configuration.WebSocketPolyfill
1285
- : WebSocket;
1286
- this.on("open", this.configuration.onOpen);
1287
- this.on("open", this.onOpen.bind(this));
1288
- this.on("connect", this.configuration.onConnect);
1289
- this.on("message", this.configuration.onMessage);
1290
- this.on("outgoingMessage", this.configuration.onOutgoingMessage);
1291
- this.on("status", this.configuration.onStatus);
1292
- this.on("disconnect", this.configuration.onDisconnect);
1293
- this.on("close", this.configuration.onClose);
1294
- this.on("destroy", this.configuration.onDestroy);
1295
- this.on("awarenessUpdate", this.configuration.onAwarenessUpdate);
1296
- this.on("awarenessChange", this.configuration.onAwarenessChange);
1297
- this.on("close", this.onClose.bind(this));
1298
- this.on("message", this.onMessage.bind(this));
1299
- this.intervals.connectionChecker = setInterval(this.checkConnection.bind(this), this.configuration.messageReconnectTimeout / 10);
1300
- if (this.shouldConnect) {
1301
- this.connect();
1302
- }
1303
- }
1304
- async onOpen(event) {
1305
- this.status = exports.WebSocketStatus.Connected;
1306
- this.emit("status", { status: exports.WebSocketStatus.Connected });
1307
- this.cancelWebsocketRetry = undefined;
1308
- this.receivedOnOpenPayload = event;
1309
- }
1310
- attach(provider) {
1311
- this.configuration.providerMap.set(provider.configuration.name, provider);
1312
- if (this.status === exports.WebSocketStatus.Disconnected && this.shouldConnect) {
1313
- this.connect();
1314
- }
1315
- if (this.receivedOnOpenPayload &&
1316
- this.status === exports.WebSocketStatus.Connected) {
1317
- provider.onOpen(this.receivedOnOpenPayload);
1318
- }
1319
- }
1320
- detach(provider) {
1321
- if (this.configuration.providerMap.has(provider.configuration.name)) {
1322
- provider.send(CloseMessage, {
1323
- documentName: provider.configuration.name,
1324
- });
1325
- this.configuration.providerMap.delete(provider.configuration.name);
1326
- }
1327
- }
1328
- setConfiguration(configuration = {}) {
1329
- this.configuration = { ...this.configuration, ...configuration };
1330
- if (!this.configuration.autoConnect) {
1331
- this.shouldConnect = false;
1332
- }
1333
- }
1334
- async connect() {
1335
- if (this.status === exports.WebSocketStatus.Connected) {
1336
- return;
1337
- }
1338
- // Always cancel any previously initiated connection retryer instances
1339
- if (this.cancelWebsocketRetry) {
1340
- this.cancelWebsocketRetry();
1341
- this.cancelWebsocketRetry = undefined;
1342
- }
1343
- this.receivedOnOpenPayload = undefined;
1344
- this.shouldConnect = true;
1345
- const abortableRetry = () => {
1346
- let cancelAttempt = false;
1347
- const retryPromise = attempt.retry(this.createWebSocketConnection.bind(this), {
1348
- delay: this.configuration.delay,
1349
- initialDelay: this.configuration.initialDelay,
1350
- factor: this.configuration.factor,
1351
- maxAttempts: this.configuration.maxAttempts,
1352
- minDelay: this.configuration.minDelay,
1353
- maxDelay: this.configuration.maxDelay,
1354
- jitter: this.configuration.jitter,
1355
- timeout: this.configuration.timeout,
1356
- handleTimeout: this.configuration.handleTimeout,
1357
- beforeAttempt: (context) => {
1358
- if (!this.shouldConnect || cancelAttempt) {
1359
- context.abort();
1360
- }
1361
- },
1362
- }).catch((error) => {
1363
- // If we aborted the connection attempt then don’t throw an error
1364
- // ref: https://github.com/lifeomic/attempt/blob/master/src/index.ts#L136
1365
- if (error && error.code !== "ATTEMPT_ABORTED") {
1366
- throw error;
1367
- }
1368
- });
1369
- return {
1370
- retryPromise,
1371
- cancelFunc: () => {
1372
- cancelAttempt = true;
1373
- },
1374
- };
1375
- };
1376
- const { retryPromise, cancelFunc } = abortableRetry();
1377
- this.cancelWebsocketRetry = cancelFunc;
1378
- return retryPromise;
1379
- }
1380
- // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
1381
- attachWebSocketListeners(ws, reject) {
1382
- const { identifier } = ws;
1383
- const onMessageHandler = (payload) => this.emit("message", payload);
1384
- const onCloseHandler = (payload) => this.emit("close", { event: payload });
1385
- const onOpenHandler = (payload) => this.emit("open", payload);
1386
- const onErrorHandler = (err) => {
1387
- reject(err);
1388
- };
1389
- this.webSocketHandlers[identifier] = {
1390
- message: onMessageHandler,
1391
- close: onCloseHandler,
1392
- open: onOpenHandler,
1393
- error: onErrorHandler,
1394
- };
1395
- const handlers = this.webSocketHandlers[ws.identifier];
1396
- Object.keys(handlers).forEach((name) => {
1397
- ws.addEventListener(name, handlers[name]);
1398
- });
1399
- }
1400
- cleanupWebSocket() {
1401
- if (!this.webSocket) {
1402
- return;
1403
- }
1404
- const { identifier } = this.webSocket;
1405
- const handlers = this.webSocketHandlers[identifier];
1406
- Object.keys(handlers).forEach((name) => {
1407
- var _a;
1408
- (_a = this.webSocket) === null || _a === void 0 ? void 0 : _a.removeEventListener(name, handlers[name]);
1409
- delete this.webSocketHandlers[identifier];
1410
- });
1411
- this.webSocket.close();
1412
- this.webSocket = null;
1413
- }
1414
- createWebSocketConnection() {
1415
- return new Promise((resolve, reject) => {
1416
- if (this.webSocket) {
1417
- this.messageQueue = [];
1418
- this.cleanupWebSocket();
1419
- }
1420
- this.lastMessageReceived = 0;
1421
- this.identifier += 1;
1422
- // Init the WebSocket connection
1423
- const ws = new this.configuration.WebSocketPolyfill(this.url);
1424
- ws.binaryType = "arraybuffer";
1425
- ws.identifier = this.identifier;
1426
- this.attachWebSocketListeners(ws, reject);
1427
- this.webSocket = ws;
1428
- // Reset the status
1429
- this.status = exports.WebSocketStatus.Connecting;
1430
- this.emit("status", { status: exports.WebSocketStatus.Connecting });
1431
- // Store resolve/reject for later use
1432
- this.connectionAttempt = {
1433
- resolve,
1434
- reject,
1435
- };
1436
- });
1437
- }
1438
- onMessage(event) {
1439
- var _a;
1440
- this.resolveConnectionAttempt();
1441
- this.lastMessageReceived = getUnixTime();
1442
- const message = new IncomingMessage(event.data);
1443
- const documentName = message.peekVarString();
1444
- (_a = this.configuration.providerMap.get(documentName)) === null || _a === void 0 ? void 0 : _a.onMessage(event);
1445
- }
1446
- resolveConnectionAttempt() {
1447
- if (this.connectionAttempt) {
1448
- this.connectionAttempt.resolve();
1449
- this.connectionAttempt = null;
1450
- this.status = exports.WebSocketStatus.Connected;
1451
- this.emit("status", { status: exports.WebSocketStatus.Connected });
1452
- this.emit("connect");
1453
- this.messageQueue.forEach((message) => this.send(message));
1454
- this.messageQueue = [];
1455
- }
1456
- }
1457
- stopConnectionAttempt() {
1458
- this.connectionAttempt = null;
1459
- }
1460
- rejectConnectionAttempt() {
1461
- var _a;
1462
- (_a = this.connectionAttempt) === null || _a === void 0 ? void 0 : _a.reject();
1463
- this.connectionAttempt = null;
1464
- }
1465
- checkConnection() {
1466
- var _a;
1467
- // Don’t check the connection when it’s not even established
1468
- if (this.status !== exports.WebSocketStatus.Connected) {
1469
- return;
1470
- }
1471
- // Don’t close the connection while waiting for the first message
1472
- if (!this.lastMessageReceived) {
1473
- return;
1474
- }
1475
- // Don’t close the connection when a message was received recently
1476
- if (this.configuration.messageReconnectTimeout >=
1477
- getUnixTime() - this.lastMessageReceived) {
1478
- return;
1479
- }
1480
- // No message received in a long time, not even your own
1481
- // Awareness updates, which are updated every 15 seconds
1482
- // if awareness is enabled.
1483
- this.closeTries += 1;
1484
- // https://bugs.webkit.org/show_bug.cgi?id=247943
1485
- if (this.closeTries > 2) {
1486
- this.onClose({
1487
- event: {
1488
- code: 4408,
1489
- reason: "forced",
1490
- },
1491
- });
1492
- this.closeTries = 0;
1493
- }
1494
- else {
1495
- (_a = this.webSocket) === null || _a === void 0 ? void 0 : _a.close();
1496
- this.messageQueue = [];
1497
- }
1498
- }
1499
- // Ensure that the URL never ends with /
1500
- get serverUrl() {
1501
- while (this.configuration.url[this.configuration.url.length - 1] === "/") {
1502
- return this.configuration.url.slice(0, this.configuration.url.length - 1);
1503
- }
1504
- return this.configuration.url;
1505
- }
1506
- get url() {
1507
- return this.serverUrl;
1508
- }
1509
- disconnect() {
1510
- this.shouldConnect = false;
1511
- if (this.webSocket === null) {
1512
- return;
1513
- }
1514
- try {
1515
- this.webSocket.close();
1516
- this.messageQueue = [];
1517
- }
1518
- catch (e) {
1519
- console.error(e);
1520
- }
1521
- }
1522
- send(message) {
1523
- var _a;
1524
- if (((_a = this.webSocket) === null || _a === void 0 ? void 0 : _a.readyState) === common.WsReadyStates.Open) {
1525
- this.webSocket.send(message);
1526
- }
1527
- else {
1528
- this.messageQueue.push(message);
1529
- }
1530
- }
1531
- onClose({ event }) {
1532
- this.closeTries = 0;
1533
- this.cleanupWebSocket();
1534
- if (this.connectionAttempt) {
1535
- // That connection attempt failed.
1536
- this.rejectConnectionAttempt();
1537
- }
1538
- // Let’s update the connection status.
1539
- this.status = exports.WebSocketStatus.Disconnected;
1540
- this.emit("status", { status: exports.WebSocketStatus.Disconnected });
1541
- this.emit("disconnect", { event });
1542
- // trigger connect if no retry is running and we want to have a connection
1543
- if (!this.cancelWebsocketRetry && this.shouldConnect) {
1544
- setTimeout(() => {
1545
- this.connect();
1546
- }, this.configuration.delay);
1547
- }
1548
- }
1549
- destroy() {
1550
- this.emit("destroy");
1551
- clearInterval(this.intervals.connectionChecker);
1552
- // If there is still a connection attempt outstanding then we should stop
1553
- // it before calling disconnect, otherwise it will be rejected in the onClose
1554
- // handler and trigger a retry
1555
- this.stopConnectionAttempt();
1556
- this.disconnect();
1557
- this.removeAllListeners();
1558
- this.cleanupWebSocket();
1559
- }
1560
- }
1023
+ //#endregion
1024
+ //#region packages/provider/src/EventEmitter.ts
1025
+ var EventEmitter = class {
1026
+ constructor() {
1027
+ this.callbacks = {};
1028
+ }
1029
+ on(event, fn) {
1030
+ if (!this.callbacks[event]) this.callbacks[event] = [];
1031
+ this.callbacks[event].push(fn);
1032
+ return this;
1033
+ }
1034
+ emit(event, ...args) {
1035
+ const callbacks = this.callbacks[event];
1036
+ if (callbacks) callbacks.forEach((callback) => callback.apply(this, args));
1037
+ return this;
1038
+ }
1039
+ off(event, fn) {
1040
+ const callbacks = this.callbacks[event];
1041
+ if (callbacks) if (fn) this.callbacks[event] = callbacks.filter((callback) => callback !== fn);
1042
+ else delete this.callbacks[event];
1043
+ return this;
1044
+ }
1045
+ removeAllListeners() {
1046
+ this.callbacks = {};
1047
+ }
1048
+ };
1561
1049
 
1562
- /**
1563
- * @module sync-protocol
1564
- */
1050
+ //#endregion
1051
+ //#region packages/provider/src/IncomingMessage.ts
1052
+ var IncomingMessage = class {
1053
+ constructor(data) {
1054
+ this.data = data;
1055
+ this.encoder = createEncoder();
1056
+ this.decoder = createDecoder(new Uint8Array(this.data));
1057
+ }
1058
+ peekVarString() {
1059
+ return peekVarString(this.decoder);
1060
+ }
1061
+ readVarUint() {
1062
+ return readVarUint(this.decoder);
1063
+ }
1064
+ readVarString() {
1065
+ return readVarString(this.decoder);
1066
+ }
1067
+ readVarUint8Array() {
1068
+ return readVarUint8Array(this.decoder);
1069
+ }
1070
+ writeVarUint(type) {
1071
+ return writeVarUint(this.encoder, type);
1072
+ }
1073
+ writeVarString(string) {
1074
+ return writeVarString(this.encoder, string);
1075
+ }
1076
+ writeVarUint8Array(data) {
1077
+ return writeVarUint8Array(this.encoder, data);
1078
+ }
1079
+ length() {
1080
+ return length$1(this.encoder);
1081
+ }
1082
+ };
1565
1083
 
1084
+ //#endregion
1085
+ //#region packages/provider/src/types.ts
1086
+ let MessageType = /* @__PURE__ */ function(MessageType) {
1087
+ MessageType[MessageType["Sync"] = 0] = "Sync";
1088
+ MessageType[MessageType["Awareness"] = 1] = "Awareness";
1089
+ MessageType[MessageType["Auth"] = 2] = "Auth";
1090
+ MessageType[MessageType["QueryAwareness"] = 3] = "QueryAwareness";
1091
+ MessageType[MessageType["Stateless"] = 5] = "Stateless";
1092
+ MessageType[MessageType["CLOSE"] = 7] = "CLOSE";
1093
+ MessageType[MessageType["SyncStatus"] = 8] = "SyncStatus";
1094
+ return MessageType;
1095
+ }({});
1096
+ let WebSocketStatus = /* @__PURE__ */ function(WebSocketStatus) {
1097
+ WebSocketStatus["Connecting"] = "connecting";
1098
+ WebSocketStatus["Connected"] = "connected";
1099
+ WebSocketStatus["Disconnected"] = "disconnected";
1100
+ return WebSocketStatus;
1101
+ }({});
1102
+
1103
+ //#endregion
1104
+ //#region packages/provider/src/OutgoingMessage.ts
1105
+ var OutgoingMessage = class {
1106
+ constructor() {
1107
+ this.encoder = createEncoder();
1108
+ }
1109
+ get(args) {
1110
+ return args.encoder;
1111
+ }
1112
+ toUint8Array() {
1113
+ return toUint8Array(this.encoder);
1114
+ }
1115
+ };
1566
1116
 
1567
- /**
1568
- * @typedef {Map<number, number>} StateMap
1569
- */
1117
+ //#endregion
1118
+ //#region packages/provider/src/OutgoingMessages/CloseMessage.ts
1119
+ var CloseMessage = class extends OutgoingMessage {
1120
+ constructor(..._args) {
1121
+ super(..._args);
1122
+ this.type = MessageType.CLOSE;
1123
+ this.description = "Ask the server to close the connection";
1124
+ }
1125
+ get(args) {
1126
+ writeVarString(this.encoder, args.documentName);
1127
+ writeVarUint(this.encoder, this.type);
1128
+ return this.encoder;
1129
+ }
1130
+ };
1570
1131
 
1571
- /**
1572
- * Core Yjs defines two message types:
1573
- * YjsSyncStep1: Includes the State Set of the sending client. When received, the client should reply with YjsSyncStep2.
1574
- * • YjsSyncStep2: Includes all missing structs and the complete delete set. When received, the client is assured that it
1575
- * received all information from the remote client.
1576
- *
1577
- * In a peer-to-peer network, you may want to introduce a SyncDone message type. Both parties should initiate the connection
1578
- * with SyncStep1. When a client received SyncStep2, it should reply with SyncDone. When the local client received both
1579
- * SyncStep2 and SyncDone, it is assured that it is synced to the remote client.
1580
- *
1581
- * In a client-server model, you want to handle this differently: The client should initiate the connection with SyncStep1.
1582
- * When the server receives SyncStep1, it should reply with SyncStep2 immediately followed by SyncStep1. The client replies
1583
- * with SyncStep2 when it receives SyncStep1. Optionally the server may send a SyncDone after it received SyncStep2, so the
1584
- * client knows that the sync is finished. There are two reasons for this more elaborated sync model: 1. This protocol can
1585
- * easily be implemented on top of http and websockets. 2. The server should only reply to requests, and not initiate them.
1586
- * Therefore it is necessary that the client initiates the sync.
1587
- *
1588
- * Construction of a message:
1589
- * [messageType : varUint, message definition..]
1590
- *
1591
- * Note: A message does not include information about the room name. This must to be handled by the upper layer protocol!
1592
- *
1593
- * stringify[messageType] stringifies a message definition (messageType is already read from the bufffer)
1594
- */
1132
+ //#endregion
1133
+ //#region packages/provider/src/HocuspocusProviderWebsocket.ts
1134
+ var HocuspocusProviderWebsocket = class extends EventEmitter {
1135
+ constructor(configuration) {
1136
+ super();
1137
+ this.messageQueue = [];
1138
+ this.configuration = {
1139
+ url: "",
1140
+ autoConnect: true,
1141
+ preserveTrailingSlash: false,
1142
+ document: void 0,
1143
+ WebSocketPolyfill: void 0,
1144
+ messageReconnectTimeout: 3e4,
1145
+ delay: 1e3,
1146
+ initialDelay: 0,
1147
+ factor: 2,
1148
+ maxAttempts: 0,
1149
+ minDelay: 1e3,
1150
+ maxDelay: 3e4,
1151
+ jitter: true,
1152
+ timeout: 0,
1153
+ onOpen: () => null,
1154
+ onConnect: () => null,
1155
+ onMessage: () => null,
1156
+ onOutgoingMessage: () => null,
1157
+ onStatus: () => null,
1158
+ onDisconnect: () => null,
1159
+ onClose: () => null,
1160
+ onDestroy: () => null,
1161
+ onAwarenessUpdate: () => null,
1162
+ onAwarenessChange: () => null,
1163
+ handleTimeout: null,
1164
+ providerMap: /* @__PURE__ */ new Map()
1165
+ };
1166
+ this.webSocket = null;
1167
+ this.webSocketHandlers = {};
1168
+ this.shouldConnect = true;
1169
+ this.status = WebSocketStatus.Disconnected;
1170
+ this.lastMessageReceived = 0;
1171
+ this.identifier = 0;
1172
+ this.intervals = { connectionChecker: null };
1173
+ this.connectionAttempt = null;
1174
+ this.receivedOnOpenPayload = void 0;
1175
+ this.closeTries = 0;
1176
+ this.setConfiguration(configuration);
1177
+ this.configuration.WebSocketPolyfill = configuration.WebSocketPolyfill ? configuration.WebSocketPolyfill : WebSocket;
1178
+ this.on("open", this.configuration.onOpen);
1179
+ this.on("open", this.onOpen.bind(this));
1180
+ this.on("connect", this.configuration.onConnect);
1181
+ this.on("message", this.configuration.onMessage);
1182
+ this.on("outgoingMessage", this.configuration.onOutgoingMessage);
1183
+ this.on("status", this.configuration.onStatus);
1184
+ this.on("disconnect", this.configuration.onDisconnect);
1185
+ this.on("close", this.configuration.onClose);
1186
+ this.on("destroy", this.configuration.onDestroy);
1187
+ this.on("awarenessUpdate", this.configuration.onAwarenessUpdate);
1188
+ this.on("awarenessChange", this.configuration.onAwarenessChange);
1189
+ this.on("close", this.onClose.bind(this));
1190
+ this.on("message", this.onMessage.bind(this));
1191
+ this.intervals.connectionChecker = setInterval(this.checkConnection.bind(this), this.configuration.messageReconnectTimeout / 10);
1192
+ if (this.shouldConnect) this.connect();
1193
+ }
1194
+ async onOpen(event) {
1195
+ this.status = WebSocketStatus.Connected;
1196
+ this.emit("status", { status: WebSocketStatus.Connected });
1197
+ this.cancelWebsocketRetry = void 0;
1198
+ this.receivedOnOpenPayload = event;
1199
+ }
1200
+ attach(provider) {
1201
+ this.configuration.providerMap.set(provider.configuration.name, provider);
1202
+ if (this.status === WebSocketStatus.Disconnected && this.shouldConnect) this.connect();
1203
+ if (this.receivedOnOpenPayload && this.status === WebSocketStatus.Connected) provider.onOpen(this.receivedOnOpenPayload);
1204
+ }
1205
+ detach(provider) {
1206
+ if (this.configuration.providerMap.has(provider.configuration.name)) {
1207
+ provider.send(CloseMessage, { documentName: provider.configuration.name });
1208
+ this.configuration.providerMap.delete(provider.configuration.name);
1209
+ }
1210
+ }
1211
+ setConfiguration(configuration = {}) {
1212
+ this.configuration = {
1213
+ ...this.configuration,
1214
+ ...configuration
1215
+ };
1216
+ if (!this.configuration.autoConnect) this.shouldConnect = false;
1217
+ }
1218
+ async connect() {
1219
+ if (this.status === WebSocketStatus.Connected) return;
1220
+ if (this.cancelWebsocketRetry) {
1221
+ this.cancelWebsocketRetry();
1222
+ this.cancelWebsocketRetry = void 0;
1223
+ }
1224
+ this.receivedOnOpenPayload = void 0;
1225
+ this.shouldConnect = true;
1226
+ const abortableRetry = () => {
1227
+ let cancelAttempt = false;
1228
+ return {
1229
+ retryPromise: (0, _lifeomic_attempt.retry)(this.createWebSocketConnection.bind(this), {
1230
+ delay: this.configuration.delay,
1231
+ initialDelay: this.configuration.initialDelay,
1232
+ factor: this.configuration.factor,
1233
+ maxAttempts: this.configuration.maxAttempts,
1234
+ minDelay: this.configuration.minDelay,
1235
+ maxDelay: this.configuration.maxDelay,
1236
+ jitter: this.configuration.jitter,
1237
+ timeout: this.configuration.timeout,
1238
+ handleTimeout: this.configuration.handleTimeout,
1239
+ beforeAttempt: (context) => {
1240
+ if (!this.shouldConnect || cancelAttempt) context.abort();
1241
+ }
1242
+ }).catch((error) => {
1243
+ if (error && error.code !== "ATTEMPT_ABORTED") throw error;
1244
+ }),
1245
+ cancelFunc: () => {
1246
+ cancelAttempt = true;
1247
+ }
1248
+ };
1249
+ };
1250
+ const { retryPromise, cancelFunc } = abortableRetry();
1251
+ this.cancelWebsocketRetry = cancelFunc;
1252
+ return retryPromise;
1253
+ }
1254
+ attachWebSocketListeners(ws, reject) {
1255
+ const { identifier } = ws;
1256
+ const onMessageHandler = (payload) => this.emit("message", payload);
1257
+ const onCloseHandler = (payload) => this.emit("close", { event: payload });
1258
+ const onOpenHandler = (payload) => this.emit("open", payload);
1259
+ const onErrorHandler = (err) => {
1260
+ reject(err);
1261
+ };
1262
+ this.webSocketHandlers[identifier] = {
1263
+ message: onMessageHandler,
1264
+ close: onCloseHandler,
1265
+ open: onOpenHandler,
1266
+ error: onErrorHandler
1267
+ };
1268
+ const handlers = this.webSocketHandlers[ws.identifier];
1269
+ Object.keys(handlers).forEach((name) => {
1270
+ ws.addEventListener(name, handlers[name]);
1271
+ });
1272
+ }
1273
+ cleanupWebSocket() {
1274
+ if (!this.webSocket) return;
1275
+ const { identifier } = this.webSocket;
1276
+ const handlers = this.webSocketHandlers[identifier];
1277
+ Object.keys(handlers).forEach((name) => {
1278
+ this.webSocket?.removeEventListener(name, handlers[name]);
1279
+ delete this.webSocketHandlers[identifier];
1280
+ });
1281
+ this.webSocket.close();
1282
+ this.webSocket = null;
1283
+ }
1284
+ createWebSocketConnection() {
1285
+ return new Promise((resolve, reject) => {
1286
+ if (this.webSocket) {
1287
+ this.messageQueue = [];
1288
+ this.cleanupWebSocket();
1289
+ }
1290
+ this.lastMessageReceived = 0;
1291
+ this.identifier += 1;
1292
+ const ws = new this.configuration.WebSocketPolyfill(this.url);
1293
+ ws.binaryType = "arraybuffer";
1294
+ ws.identifier = this.identifier;
1295
+ this.attachWebSocketListeners(ws, reject);
1296
+ this.webSocket = ws;
1297
+ this.status = WebSocketStatus.Connecting;
1298
+ this.emit("status", { status: WebSocketStatus.Connecting });
1299
+ this.connectionAttempt = {
1300
+ resolve,
1301
+ reject
1302
+ };
1303
+ });
1304
+ }
1305
+ onMessage(event) {
1306
+ this.resolveConnectionAttempt();
1307
+ this.lastMessageReceived = getUnixTime();
1308
+ const documentName = new IncomingMessage(event.data).peekVarString();
1309
+ this.configuration.providerMap.get(documentName)?.onMessage(event);
1310
+ }
1311
+ resolveConnectionAttempt() {
1312
+ if (this.connectionAttempt) {
1313
+ this.connectionAttempt.resolve();
1314
+ this.connectionAttempt = null;
1315
+ this.status = WebSocketStatus.Connected;
1316
+ this.emit("status", { status: WebSocketStatus.Connected });
1317
+ this.emit("connect");
1318
+ this.messageQueue.forEach((message) => this.send(message));
1319
+ this.messageQueue = [];
1320
+ }
1321
+ }
1322
+ stopConnectionAttempt() {
1323
+ this.connectionAttempt = null;
1324
+ }
1325
+ rejectConnectionAttempt() {
1326
+ this.connectionAttempt?.reject();
1327
+ this.connectionAttempt = null;
1328
+ }
1329
+ checkConnection() {
1330
+ if (this.status !== WebSocketStatus.Connected) return;
1331
+ if (!this.lastMessageReceived) return;
1332
+ if (this.configuration.messageReconnectTimeout >= getUnixTime() - this.lastMessageReceived) return;
1333
+ this.closeTries += 1;
1334
+ if (this.closeTries > 2) {
1335
+ this.onClose({ event: {
1336
+ code: 4408,
1337
+ reason: "forced"
1338
+ } });
1339
+ this.closeTries = 0;
1340
+ } else {
1341
+ this.webSocket?.close();
1342
+ this.messageQueue = [];
1343
+ }
1344
+ }
1345
+ get serverUrl() {
1346
+ if (this.configuration.preserveTrailingSlash) return this.configuration.url;
1347
+ let url = this.configuration.url;
1348
+ while (url[url.length - 1] === "/") url = url.slice(0, url.length - 1);
1349
+ return url;
1350
+ }
1351
+ get url() {
1352
+ return this.serverUrl;
1353
+ }
1354
+ disconnect() {
1355
+ this.shouldConnect = false;
1356
+ if (this.webSocket === null) return;
1357
+ try {
1358
+ this.webSocket.close();
1359
+ this.messageQueue = [];
1360
+ } catch (e) {
1361
+ console.error(e);
1362
+ }
1363
+ }
1364
+ send(message) {
1365
+ if (this.webSocket?.readyState === _hocuspocus_common.WsReadyStates.Open) this.webSocket.send(message);
1366
+ else this.messageQueue.push(message);
1367
+ }
1368
+ onClose({ event }) {
1369
+ this.closeTries = 0;
1370
+ this.cleanupWebSocket();
1371
+ if (this.connectionAttempt) this.rejectConnectionAttempt();
1372
+ this.status = WebSocketStatus.Disconnected;
1373
+ this.emit("status", { status: WebSocketStatus.Disconnected });
1374
+ this.emit("disconnect", { event });
1375
+ if (!this.cancelWebsocketRetry && this.shouldConnect) setTimeout(() => {
1376
+ this.connect();
1377
+ }, this.configuration.delay);
1378
+ }
1379
+ destroy() {
1380
+ this.emit("destroy");
1381
+ clearInterval(this.intervals.connectionChecker);
1382
+ this.stopConnectionAttempt();
1383
+ this.disconnect();
1384
+ this.removeAllListeners();
1385
+ this.cleanupWebSocket();
1386
+ }
1387
+ };
1595
1388
 
1389
+ //#endregion
1390
+ //#region node_modules/y-protocols/sync.js
1391
+ /**
1392
+ * @module sync-protocol
1393
+ */
1394
+ /**
1395
+ * @typedef {Map<number, number>} StateMap
1396
+ */
1397
+ /**
1398
+ * Core Yjs defines two message types:
1399
+ * • YjsSyncStep1: Includes the State Set of the sending client. When received, the client should reply with YjsSyncStep2.
1400
+ * • YjsSyncStep2: Includes all missing structs and the complete delete set. When received, the client is assured that it
1401
+ * received all information from the remote client.
1402
+ *
1403
+ * In a peer-to-peer network, you may want to introduce a SyncDone message type. Both parties should initiate the connection
1404
+ * with SyncStep1. When a client received SyncStep2, it should reply with SyncDone. When the local client received both
1405
+ * SyncStep2 and SyncDone, it is assured that it is synced to the remote client.
1406
+ *
1407
+ * In a client-server model, you want to handle this differently: The client should initiate the connection with SyncStep1.
1408
+ * When the server receives SyncStep1, it should reply with SyncStep2 immediately followed by SyncStep1. The client replies
1409
+ * with SyncStep2 when it receives SyncStep1. Optionally the server may send a SyncDone after it received SyncStep2, so the
1410
+ * client knows that the sync is finished. There are two reasons for this more elaborated sync model: 1. This protocol can
1411
+ * easily be implemented on top of http and websockets. 2. The server should only reply to requests, and not initiate them.
1412
+ * Therefore it is necessary that the client initiates the sync.
1413
+ *
1414
+ * Construction of a message:
1415
+ * [messageType : varUint, message definition..]
1416
+ *
1417
+ * Note: A message does not include information about the room name. This must to be handled by the upper layer protocol!
1418
+ *
1419
+ * stringify[messageType] stringifies a message definition (messageType is already read from the bufffer)
1420
+ */
1596
1421
  const messageYjsSyncStep1 = 0;
1597
1422
  const messageYjsSyncStep2 = 1;
1598
1423
  const messageYjsUpdate = 2;
1599
-
1600
1424
  /**
1601
- * Create a sync step 1 message based on the state of the current shared document.
1602
- *
1603
- * @param {encoding.Encoder} encoder
1604
- * @param {Y.Doc} doc
1605
- */
1425
+ * Create a sync step 1 message based on the state of the current shared document.
1426
+ *
1427
+ * @param {encoding.Encoder} encoder
1428
+ * @param {Y.Doc} doc
1429
+ */
1606
1430
  const writeSyncStep1 = (encoder, doc) => {
1607
- writeVarUint(encoder, messageYjsSyncStep1);
1608
- const sv = Y__namespace.encodeStateVector(doc);
1609
- writeVarUint8Array(encoder, sv);
1431
+ writeVarUint(encoder, messageYjsSyncStep1);
1432
+ const sv = yjs.encodeStateVector(doc);
1433
+ writeVarUint8Array(encoder, sv);
1610
1434
  };
1611
-
1612
1435
  /**
1613
- * @param {encoding.Encoder} encoder
1614
- * @param {Y.Doc} doc
1615
- * @param {Uint8Array} [encodedStateVector]
1616
- */
1436
+ * @param {encoding.Encoder} encoder
1437
+ * @param {Y.Doc} doc
1438
+ * @param {Uint8Array} [encodedStateVector]
1439
+ */
1617
1440
  const writeSyncStep2 = (encoder, doc, encodedStateVector) => {
1618
- writeVarUint(encoder, messageYjsSyncStep2);
1619
- writeVarUint8Array(encoder, Y__namespace.encodeStateAsUpdate(doc, encodedStateVector));
1441
+ writeVarUint(encoder, messageYjsSyncStep2);
1442
+ writeVarUint8Array(encoder, yjs.encodeStateAsUpdate(doc, encodedStateVector));
1620
1443
  };
1621
-
1622
1444
  /**
1623
- * Read SyncStep1 message and reply with SyncStep2.
1624
- *
1625
- * @param {decoding.Decoder} decoder The reply to the received message
1626
- * @param {encoding.Encoder} encoder The received message
1627
- * @param {Y.Doc} doc
1628
- */
1629
- const readSyncStep1 = (decoder, encoder, doc) =>
1630
- writeSyncStep2(encoder, doc, readVarUint8Array(decoder));
1631
-
1632
- /**
1633
- * Read and apply Structs and then DeleteStore to a y instance.
1634
- *
1635
- * @param {decoding.Decoder} decoder
1636
- * @param {Y.Doc} doc
1637
- * @param {any} transactionOrigin
1638
- */
1445
+ * Read SyncStep1 message and reply with SyncStep2.
1446
+ *
1447
+ * @param {decoding.Decoder} decoder The reply to the received message
1448
+ * @param {encoding.Encoder} encoder The received message
1449
+ * @param {Y.Doc} doc
1450
+ */
1451
+ const readSyncStep1 = (decoder, encoder, doc) => writeSyncStep2(encoder, doc, readVarUint8Array(decoder));
1452
+ /**
1453
+ * Read and apply Structs and then DeleteStore to a y instance.
1454
+ *
1455
+ * @param {decoding.Decoder} decoder
1456
+ * @param {Y.Doc} doc
1457
+ * @param {any} transactionOrigin
1458
+ */
1639
1459
  const readSyncStep2 = (decoder, doc, transactionOrigin) => {
1640
- try {
1641
- Y__namespace.applyUpdate(doc, readVarUint8Array(decoder), transactionOrigin);
1642
- } catch (error) {
1643
- // This catches errors that are thrown by event handlers
1644
- console.error('Caught error while handling a Yjs update', error);
1645
- }
1460
+ try {
1461
+ yjs.applyUpdate(doc, readVarUint8Array(decoder), transactionOrigin);
1462
+ } catch (error) {
1463
+ console.error("Caught error while handling a Yjs update", error);
1464
+ }
1646
1465
  };
1647
-
1648
1466
  /**
1649
- * @param {encoding.Encoder} encoder
1650
- * @param {Uint8Array} update
1651
- */
1467
+ * @param {encoding.Encoder} encoder
1468
+ * @param {Uint8Array} update
1469
+ */
1652
1470
  const writeUpdate = (encoder, update) => {
1653
- writeVarUint(encoder, messageYjsUpdate);
1654
- writeVarUint8Array(encoder, update);
1471
+ writeVarUint(encoder, messageYjsUpdate);
1472
+ writeVarUint8Array(encoder, update);
1655
1473
  };
1656
-
1657
1474
  /**
1658
- * Read and apply Structs and then DeleteStore to a y instance.
1659
- *
1660
- * @param {decoding.Decoder} decoder
1661
- * @param {Y.Doc} doc
1662
- * @param {any} transactionOrigin
1663
- */
1475
+ * Read and apply Structs and then DeleteStore to a y instance.
1476
+ *
1477
+ * @param {decoding.Decoder} decoder
1478
+ * @param {Y.Doc} doc
1479
+ * @param {any} transactionOrigin
1480
+ */
1664
1481
  const readUpdate = readSyncStep2;
1665
-
1666
1482
  /**
1667
- * @param {decoding.Decoder} decoder A message received from another client
1668
- * @param {encoding.Encoder} encoder The reply message. Does not need to be sent if empty.
1669
- * @param {Y.Doc} doc
1670
- * @param {any} transactionOrigin
1671
- */
1483
+ * @param {decoding.Decoder} decoder A message received from another client
1484
+ * @param {encoding.Encoder} encoder The reply message. Does not need to be sent if empty.
1485
+ * @param {Y.Doc} doc
1486
+ * @param {any} transactionOrigin
1487
+ */
1672
1488
  const readSyncMessage = (decoder, encoder, doc, transactionOrigin) => {
1673
- const messageType = readVarUint(decoder);
1674
- switch (messageType) {
1675
- case messageYjsSyncStep1:
1676
- readSyncStep1(decoder, encoder, doc);
1677
- break
1678
- case messageYjsSyncStep2:
1679
- readSyncStep2(decoder, doc, transactionOrigin);
1680
- break
1681
- case messageYjsUpdate:
1682
- readUpdate(decoder, doc, transactionOrigin);
1683
- break
1684
- default:
1685
- throw new Error('Unknown message type')
1686
- }
1687
- return messageType
1489
+ const messageType = readVarUint(decoder);
1490
+ switch (messageType) {
1491
+ case messageYjsSyncStep1:
1492
+ readSyncStep1(decoder, encoder, doc);
1493
+ break;
1494
+ case messageYjsSyncStep2:
1495
+ readSyncStep2(decoder, doc, transactionOrigin);
1496
+ break;
1497
+ case messageYjsUpdate:
1498
+ readUpdate(decoder, doc, transactionOrigin);
1499
+ break;
1500
+ default: throw new Error("Unknown message type");
1501
+ }
1502
+ return messageType;
1688
1503
  };
1689
1504
 
1690
- class MessageReceiver {
1691
- constructor(message) {
1692
- this.message = message;
1693
- }
1694
- apply(provider, emitSynced) {
1695
- const { message } = this;
1696
- const type = message.readVarUint();
1697
- const emptyMessageLength = message.length();
1698
- switch (type) {
1699
- case exports.MessageType.Sync:
1700
- this.applySyncMessage(provider, emitSynced);
1701
- break;
1702
- case exports.MessageType.Awareness:
1703
- this.applyAwarenessMessage(provider);
1704
- break;
1705
- case exports.MessageType.Auth:
1706
- this.applyAuthMessage(provider);
1707
- break;
1708
- case exports.MessageType.QueryAwareness:
1709
- this.applyQueryAwarenessMessage(provider);
1710
- break;
1711
- case exports.MessageType.Stateless:
1712
- provider.receiveStateless(readVarString(message.decoder));
1713
- break;
1714
- case exports.MessageType.SyncStatus:
1715
- this.applySyncStatusMessage(provider, readVarInt(message.decoder) === 1);
1716
- break;
1717
- case exports.MessageType.CLOSE:
1718
- // eslint-disable-next-line no-case-declarations
1719
- const event = {
1720
- code: 1000,
1721
- reason: readVarString(message.decoder),
1722
- // @ts-ignore
1723
- target: provider.configuration.websocketProvider.webSocket,
1724
- type: "close",
1725
- };
1726
- provider.onClose();
1727
- provider.configuration.onClose({ event });
1728
- provider.forwardClose(event);
1729
- break;
1730
- default:
1731
- throw new Error(`Can’t apply message of unknown type: ${type}`);
1732
- }
1733
- // Reply
1734
- if (message.length() > emptyMessageLength + 1) {
1735
- // length of documentName (considered in emptyMessageLength plus length of yjs sync type, set in applySyncMessage)
1736
- // @ts-ignore
1737
- provider.send(OutgoingMessage, { encoder: message.encoder });
1738
- }
1739
- }
1740
- applySyncMessage(provider, emitSynced) {
1741
- const { message } = this;
1742
- message.writeVarUint(exports.MessageType.Sync);
1743
- // Apply update
1744
- const syncMessageType = readSyncMessage(message.decoder, message.encoder, provider.document, provider);
1745
- // Synced once we receive Step2
1746
- if (emitSynced && syncMessageType === messageYjsSyncStep2) {
1747
- provider.synced = true;
1748
- }
1749
- }
1750
- applySyncStatusMessage(provider, applied) {
1751
- if (applied) {
1752
- provider.decrementUnsyncedChanges();
1753
- }
1754
- }
1755
- applyAwarenessMessage(provider) {
1756
- if (!provider.awareness)
1757
- return;
1758
- const { message } = this;
1759
- applyAwarenessUpdate(provider.awareness, message.readVarUint8Array(), provider);
1760
- }
1761
- applyAuthMessage(provider) {
1762
- const { message } = this;
1763
- common.readAuthMessage(message.decoder, provider.sendToken.bind(provider), provider.permissionDeniedHandler.bind(provider), provider.authenticatedHandler.bind(provider));
1764
- }
1765
- applyQueryAwarenessMessage(provider) {
1766
- if (!provider.awareness)
1767
- return;
1768
- const { message } = this;
1769
- message.writeVarUint(exports.MessageType.Awareness);
1770
- message.writeVarUint8Array(encodeAwarenessUpdate(provider.awareness, Array.from(provider.awareness.getStates().keys())));
1771
- }
1772
- }
1505
+ //#endregion
1506
+ //#region packages/provider/src/MessageReceiver.ts
1507
+ var MessageReceiver = class {
1508
+ constructor(message) {
1509
+ this.message = message;
1510
+ }
1511
+ apply(provider, emitSynced) {
1512
+ const { message } = this;
1513
+ const type = message.readVarUint();
1514
+ const emptyMessageLength = message.length();
1515
+ switch (type) {
1516
+ case MessageType.Sync:
1517
+ this.applySyncMessage(provider, emitSynced);
1518
+ break;
1519
+ case MessageType.Awareness:
1520
+ this.applyAwarenessMessage(provider);
1521
+ break;
1522
+ case MessageType.Auth:
1523
+ this.applyAuthMessage(provider);
1524
+ break;
1525
+ case MessageType.QueryAwareness:
1526
+ this.applyQueryAwarenessMessage(provider);
1527
+ break;
1528
+ case MessageType.Stateless:
1529
+ provider.receiveStateless(readVarString(message.decoder));
1530
+ break;
1531
+ case MessageType.SyncStatus:
1532
+ this.applySyncStatusMessage(provider, readVarInt(message.decoder) === 1);
1533
+ break;
1534
+ case MessageType.CLOSE:
1535
+ const event = {
1536
+ code: 1e3,
1537
+ reason: readVarString(message.decoder),
1538
+ target: provider.configuration.websocketProvider.webSocket,
1539
+ type: "close"
1540
+ };
1541
+ provider.onClose();
1542
+ provider.configuration.onClose({ event });
1543
+ provider.forwardClose({ event });
1544
+ break;
1545
+ default: throw new Error(`Can’t apply message of unknown type: ${type}`);
1546
+ }
1547
+ if (message.length() > emptyMessageLength + 1) provider.send(OutgoingMessage, { encoder: message.encoder });
1548
+ }
1549
+ applySyncMessage(provider, emitSynced) {
1550
+ const { message } = this;
1551
+ message.writeVarUint(MessageType.Sync);
1552
+ const syncMessageType = readSyncMessage(message.decoder, message.encoder, provider.document, provider);
1553
+ if (emitSynced && syncMessageType === messageYjsSyncStep2) provider.synced = true;
1554
+ }
1555
+ applySyncStatusMessage(provider, applied) {
1556
+ if (applied) provider.decrementUnsyncedChanges();
1557
+ }
1558
+ applyAwarenessMessage(provider) {
1559
+ if (!provider.awareness) return;
1560
+ const { message } = this;
1561
+ applyAwarenessUpdate(provider.awareness, message.readVarUint8Array(), provider);
1562
+ }
1563
+ applyAuthMessage(provider) {
1564
+ const { message } = this;
1565
+ (0, _hocuspocus_common.readAuthMessage)(message.decoder, provider.sendToken.bind(provider), provider.permissionDeniedHandler.bind(provider), provider.authenticatedHandler.bind(provider));
1566
+ }
1567
+ applyQueryAwarenessMessage(provider) {
1568
+ if (!provider.awareness) return;
1569
+ const { message } = this;
1570
+ message.writeVarUint(MessageType.Awareness);
1571
+ message.writeVarUint8Array(encodeAwarenessUpdate(provider.awareness, Array.from(provider.awareness.getStates().keys())));
1572
+ }
1573
+ };
1773
1574
 
1774
- class MessageSender {
1775
- constructor(Message, args = {}) {
1776
- this.message = new Message();
1777
- this.encoder = this.message.get(args);
1778
- }
1779
- create() {
1780
- return toUint8Array(this.encoder);
1781
- }
1782
- send(webSocket) {
1783
- webSocket === null || webSocket === void 0 ? void 0 : webSocket.send(this.create());
1784
- }
1785
- }
1575
+ //#endregion
1576
+ //#region packages/provider/src/MessageSender.ts
1577
+ var MessageSender = class {
1578
+ constructor(Message, args = {}) {
1579
+ this.message = new Message();
1580
+ this.encoder = this.message.get(args);
1581
+ }
1582
+ create() {
1583
+ return toUint8Array(this.encoder);
1584
+ }
1585
+ send(webSocket) {
1586
+ webSocket?.send(this.create());
1587
+ }
1588
+ };
1786
1589
 
1787
- class AuthenticationMessage extends OutgoingMessage {
1788
- constructor() {
1789
- super(...arguments);
1790
- this.type = exports.MessageType.Auth;
1791
- this.description = "Authentication";
1792
- }
1793
- get(args) {
1794
- if (typeof args.token === "undefined") {
1795
- throw new Error("The authentication message requires `token` as an argument.");
1796
- }
1797
- writeVarString(this.encoder, args.documentName);
1798
- writeVarUint(this.encoder, this.type);
1799
- common.writeAuthentication(this.encoder, args.token);
1800
- return this.encoder;
1801
- }
1802
- }
1590
+ //#endregion
1591
+ //#region packages/provider/src/OutgoingMessages/AuthenticationMessage.ts
1592
+ var AuthenticationMessage = class extends OutgoingMessage {
1593
+ constructor(..._args) {
1594
+ super(..._args);
1595
+ this.type = MessageType.Auth;
1596
+ this.description = "Authentication";
1597
+ }
1598
+ get(args) {
1599
+ if (typeof args.token === "undefined") throw new Error("The authentication message requires `token` as an argument.");
1600
+ writeVarString(this.encoder, args.documentName);
1601
+ writeVarUint(this.encoder, this.type);
1602
+ (0, _hocuspocus_common.writeAuthentication)(this.encoder, args.token);
1603
+ return this.encoder;
1604
+ }
1605
+ };
1803
1606
 
1804
- class AwarenessMessage extends OutgoingMessage {
1805
- constructor() {
1806
- super(...arguments);
1807
- this.type = exports.MessageType.Awareness;
1808
- this.description = "Awareness states update";
1809
- }
1810
- get(args) {
1811
- if (typeof args.awareness === "undefined") {
1812
- throw new Error("The awareness message requires awareness as an argument");
1813
- }
1814
- if (typeof args.clients === "undefined") {
1815
- throw new Error("The awareness message requires clients as an argument");
1816
- }
1817
- writeVarString(this.encoder, args.documentName);
1818
- writeVarUint(this.encoder, this.type);
1819
- let awarenessUpdate;
1820
- if (args.states === undefined) {
1821
- awarenessUpdate = encodeAwarenessUpdate(args.awareness, args.clients);
1822
- }
1823
- else {
1824
- awarenessUpdate = encodeAwarenessUpdate(args.awareness, args.clients, args.states);
1825
- }
1826
- writeVarUint8Array(this.encoder, awarenessUpdate);
1827
- return this.encoder;
1828
- }
1829
- }
1607
+ //#endregion
1608
+ //#region packages/provider/src/OutgoingMessages/AwarenessMessage.ts
1609
+ var AwarenessMessage = class extends OutgoingMessage {
1610
+ constructor(..._args) {
1611
+ super(..._args);
1612
+ this.type = MessageType.Awareness;
1613
+ this.description = "Awareness states update";
1614
+ }
1615
+ get(args) {
1616
+ if (typeof args.awareness === "undefined") throw new Error("The awareness message requires awareness as an argument");
1617
+ if (typeof args.clients === "undefined") throw new Error("The awareness message requires clients as an argument");
1618
+ writeVarString(this.encoder, args.documentName);
1619
+ writeVarUint(this.encoder, this.type);
1620
+ let awarenessUpdate;
1621
+ if (args.states === void 0) awarenessUpdate = encodeAwarenessUpdate(args.awareness, args.clients);
1622
+ else awarenessUpdate = encodeAwarenessUpdate(args.awareness, args.clients, args.states);
1623
+ writeVarUint8Array(this.encoder, awarenessUpdate);
1624
+ return this.encoder;
1625
+ }
1626
+ };
1830
1627
 
1831
- class StatelessMessage extends OutgoingMessage {
1832
- constructor() {
1833
- super(...arguments);
1834
- this.type = exports.MessageType.Stateless;
1835
- this.description = "A stateless message";
1836
- }
1837
- get(args) {
1838
- var _a;
1839
- writeVarString(this.encoder, args.documentName);
1840
- writeVarUint(this.encoder, this.type);
1841
- writeVarString(this.encoder, (_a = args.payload) !== null && _a !== void 0 ? _a : "");
1842
- return this.encoder;
1843
- }
1844
- }
1628
+ //#endregion
1629
+ //#region packages/provider/src/OutgoingMessages/StatelessMessage.ts
1630
+ var StatelessMessage = class extends OutgoingMessage {
1631
+ constructor(..._args) {
1632
+ super(..._args);
1633
+ this.type = MessageType.Stateless;
1634
+ this.description = "A stateless message";
1635
+ }
1636
+ get(args) {
1637
+ writeVarString(this.encoder, args.documentName);
1638
+ writeVarUint(this.encoder, this.type);
1639
+ writeVarString(this.encoder, args.payload ?? "");
1640
+ return this.encoder;
1641
+ }
1642
+ };
1845
1643
 
1846
- class SyncStepOneMessage extends OutgoingMessage {
1847
- constructor() {
1848
- super(...arguments);
1849
- this.type = exports.MessageType.Sync;
1850
- this.description = "First sync step";
1851
- }
1852
- get(args) {
1853
- if (typeof args.document === "undefined") {
1854
- throw new Error("The sync step one message requires document as an argument");
1855
- }
1856
- writeVarString(this.encoder, args.documentName);
1857
- writeVarUint(this.encoder, this.type);
1858
- writeSyncStep1(this.encoder, args.document);
1859
- return this.encoder;
1860
- }
1861
- }
1644
+ //#endregion
1645
+ //#region packages/provider/src/OutgoingMessages/SyncStepOneMessage.ts
1646
+ var SyncStepOneMessage = class extends OutgoingMessage {
1647
+ constructor(..._args) {
1648
+ super(..._args);
1649
+ this.type = MessageType.Sync;
1650
+ this.description = "First sync step";
1651
+ }
1652
+ get(args) {
1653
+ if (typeof args.document === "undefined") throw new Error("The sync step one message requires document as an argument");
1654
+ writeVarString(this.encoder, args.documentName);
1655
+ writeVarUint(this.encoder, this.type);
1656
+ writeSyncStep1(this.encoder, args.document);
1657
+ return this.encoder;
1658
+ }
1659
+ };
1862
1660
 
1863
- class UpdateMessage extends OutgoingMessage {
1864
- constructor() {
1865
- super(...arguments);
1866
- this.type = exports.MessageType.Sync;
1867
- this.description = "A document update";
1868
- }
1869
- get(args) {
1870
- writeVarString(this.encoder, args.documentName);
1871
- writeVarUint(this.encoder, this.type);
1872
- writeUpdate(this.encoder, args.update);
1873
- return this.encoder;
1874
- }
1875
- }
1661
+ //#endregion
1662
+ //#region packages/provider/src/OutgoingMessages/UpdateMessage.ts
1663
+ var UpdateMessage = class extends OutgoingMessage {
1664
+ constructor(..._args) {
1665
+ super(..._args);
1666
+ this.type = MessageType.Sync;
1667
+ this.description = "A document update";
1668
+ }
1669
+ get(args) {
1670
+ writeVarString(this.encoder, args.documentName);
1671
+ writeVarUint(this.encoder, this.type);
1672
+ writeUpdate(this.encoder, args.update);
1673
+ return this.encoder;
1674
+ }
1675
+ };
1876
1676
 
1877
- class AwarenessError extends Error {
1878
- constructor() {
1879
- super(...arguments);
1880
- this.code = 1001;
1881
- }
1882
- }
1883
- class HocuspocusProvider extends EventEmitter {
1884
- constructor(configuration) {
1885
- var _a, _b, _c;
1886
- super();
1887
- this.configuration = {
1888
- name: "",
1889
- // @ts-ignore
1890
- document: undefined,
1891
- // @ts-ignore
1892
- awareness: undefined,
1893
- token: null,
1894
- forceSyncInterval: false,
1895
- onAuthenticated: () => null,
1896
- onAuthenticationFailed: () => null,
1897
- onOpen: () => null,
1898
- onConnect: () => null,
1899
- onMessage: () => null,
1900
- onOutgoingMessage: () => null,
1901
- onSynced: () => null,
1902
- onStatus: () => null,
1903
- onDisconnect: () => null,
1904
- onClose: () => null,
1905
- onDestroy: () => null,
1906
- onAwarenessUpdate: () => null,
1907
- onAwarenessChange: () => null,
1908
- onStateless: () => null,
1909
- onUnsyncedChanges: () => null,
1910
- };
1911
- this.isSynced = false;
1912
- this.unsyncedChanges = 0;
1913
- this.isAuthenticated = false;
1914
- this.authorizedScope = undefined;
1915
- // @internal
1916
- this.manageSocket = false;
1917
- this._isAttached = false;
1918
- this.intervals = {
1919
- forceSync: null,
1920
- };
1921
- this.boundDocumentUpdateHandler = this.documentUpdateHandler.bind(this);
1922
- this.boundAwarenessUpdateHandler = this.awarenessUpdateHandler.bind(this);
1923
- this.boundPageHide = this.pageHide.bind(this);
1924
- this.boundOnOpen = this.onOpen.bind(this);
1925
- this.boundOnClose = this.onClose.bind(this);
1926
- this.forwardConnect = () => this.emit("connect");
1927
- this.forwardStatus = (e) => this.emit("status", e);
1928
- this.forwardClose = (e) => this.emit("close", e);
1929
- this.forwardDisconnect = (e) => this.emit("disconnect", e);
1930
- this.forwardDestroy = () => this.emit("destroy");
1931
- this.setConfiguration(configuration);
1932
- this.configuration.document = configuration.document
1933
- ? configuration.document
1934
- : new Y__namespace.Doc();
1935
- this.configuration.awareness =
1936
- configuration.awareness !== undefined
1937
- ? configuration.awareness
1938
- : new Awareness(this.document);
1939
- this.on("open", this.configuration.onOpen);
1940
- this.on("message", this.configuration.onMessage);
1941
- this.on("outgoingMessage", this.configuration.onOutgoingMessage);
1942
- this.on("synced", this.configuration.onSynced);
1943
- this.on("destroy", this.configuration.onDestroy);
1944
- this.on("awarenessUpdate", this.configuration.onAwarenessUpdate);
1945
- this.on("awarenessChange", this.configuration.onAwarenessChange);
1946
- this.on("stateless", this.configuration.onStateless);
1947
- this.on("unsyncedChanges", this.configuration.onUnsyncedChanges);
1948
- this.on("authenticated", this.configuration.onAuthenticated);
1949
- this.on("authenticationFailed", this.configuration.onAuthenticationFailed);
1950
- (_a = this.awareness) === null || _a === void 0 ? void 0 : _a.on("update", () => {
1951
- this.emit("awarenessUpdate", {
1952
- states: common.awarenessStatesToArray(this.awareness.getStates()),
1953
- });
1954
- });
1955
- (_b = this.awareness) === null || _b === void 0 ? void 0 : _b.on("change", () => {
1956
- this.emit("awarenessChange", {
1957
- states: common.awarenessStatesToArray(this.awareness.getStates()),
1958
- });
1959
- });
1960
- this.document.on("update", this.boundDocumentUpdateHandler);
1961
- (_c = this.awareness) === null || _c === void 0 ? void 0 : _c.on("update", this.boundAwarenessUpdateHandler);
1962
- this.registerEventListeners();
1963
- if (this.configuration.forceSyncInterval &&
1964
- typeof this.configuration.forceSyncInterval === "number") {
1965
- this.intervals.forceSync = setInterval(this.forceSync.bind(this), this.configuration.forceSyncInterval);
1966
- }
1967
- if (this.manageSocket) {
1968
- this.attach();
1969
- }
1970
- }
1971
- setConfiguration(configuration = {}) {
1972
- if (!configuration.websocketProvider) {
1973
- const websocketProviderConfig = configuration;
1974
- this.manageSocket = true;
1975
- this.configuration.websocketProvider = new HocuspocusProviderWebsocket({
1976
- url: websocketProviderConfig.url,
1977
- });
1978
- }
1979
- this.configuration = { ...this.configuration, ...configuration };
1980
- }
1981
- get document() {
1982
- return this.configuration.document;
1983
- }
1984
- get isAttached() {
1985
- return this._isAttached;
1986
- }
1987
- get awareness() {
1988
- return this.configuration.awareness;
1989
- }
1990
- get hasUnsyncedChanges() {
1991
- return this.unsyncedChanges > 0;
1992
- }
1993
- resetUnsyncedChanges() {
1994
- this.unsyncedChanges = 1;
1995
- this.emit("unsyncedChanges", { number: this.unsyncedChanges });
1996
- }
1997
- incrementUnsyncedChanges() {
1998
- this.unsyncedChanges += 1;
1999
- this.emit("unsyncedChanges", { number: this.unsyncedChanges });
2000
- }
2001
- decrementUnsyncedChanges() {
2002
- if (this.unsyncedChanges > 0) {
2003
- this.unsyncedChanges -= 1;
2004
- }
2005
- if (this.unsyncedChanges === 0) {
2006
- this.synced = true;
2007
- }
2008
- this.emit("unsyncedChanges", { number: this.unsyncedChanges });
2009
- }
2010
- forceSync() {
2011
- this.resetUnsyncedChanges();
2012
- this.send(SyncStepOneMessage, {
2013
- document: this.document,
2014
- documentName: this.configuration.name,
2015
- });
2016
- }
2017
- pageHide() {
2018
- if (this.awareness) {
2019
- removeAwarenessStates(this.awareness, [this.document.clientID], "page hide");
2020
- }
2021
- }
2022
- registerEventListeners() {
2023
- if (typeof window === "undefined" || !("addEventListener" in window)) {
2024
- return;
2025
- }
2026
- window.addEventListener("pagehide", this.boundPageHide);
2027
- }
2028
- sendStateless(payload) {
2029
- this.send(StatelessMessage, {
2030
- documentName: this.configuration.name,
2031
- payload,
2032
- });
2033
- }
2034
- async sendToken() {
2035
- let token;
2036
- try {
2037
- token = await this.getToken();
2038
- }
2039
- catch (error) {
2040
- this.permissionDeniedHandler(`Failed to get token during sendToken(): ${error}`);
2041
- return;
2042
- }
2043
- this.send(AuthenticationMessage, {
2044
- token: token !== null && token !== void 0 ? token : "",
2045
- documentName: this.configuration.name,
2046
- });
2047
- }
2048
- documentUpdateHandler(update, origin) {
2049
- if (origin === this) {
2050
- return;
2051
- }
2052
- this.incrementUnsyncedChanges();
2053
- this.send(UpdateMessage, { update, documentName: this.configuration.name });
2054
- }
2055
- awarenessUpdateHandler({ added, updated, removed }, origin) {
2056
- const changedClients = added.concat(updated).concat(removed);
2057
- this.send(AwarenessMessage, {
2058
- awareness: this.awareness,
2059
- clients: changedClients,
2060
- documentName: this.configuration.name,
2061
- });
2062
- }
2063
- /**
2064
- * Indicates whether a first handshake with the server has been established
2065
- *
2066
- * Note: this does not mean all updates from the client have been persisted to the backend. For this,
2067
- * use `hasUnsyncedChanges`.
2068
- */
2069
- get synced() {
2070
- return this.isSynced;
2071
- }
2072
- set synced(state) {
2073
- if (this.isSynced === state) {
2074
- return;
2075
- }
2076
- this.isSynced = state;
2077
- if (state) {
2078
- this.emit("synced", { state });
2079
- }
2080
- }
2081
- receiveStateless(payload) {
2082
- this.emit("stateless", { payload });
2083
- }
2084
- // not needed, but provides backward compatibility with e.g. lexical/yjs
2085
- async connect() {
2086
- if (this.manageSocket) {
2087
- return this.configuration.websocketProvider.connect();
2088
- }
2089
- console.warn("HocuspocusProvider::connect() is deprecated and does not do anything. Please connect/disconnect on the websocketProvider, or attach/deattach providers.");
2090
- }
2091
- disconnect() {
2092
- if (this.manageSocket) {
2093
- return this.configuration.websocketProvider.disconnect();
2094
- }
2095
- console.warn("HocuspocusProvider::disconnect() is deprecated and does not do anything. Please connect/disconnect on the websocketProvider, or attach/deattach providers.");
2096
- }
2097
- async onOpen(event) {
2098
- this.isAuthenticated = false;
2099
- this.emit("open", { event });
2100
- await this.sendToken();
2101
- this.startSync();
2102
- }
2103
- async getToken() {
2104
- if (typeof this.configuration.token === "function") {
2105
- const token = await this.configuration.token();
2106
- return token;
2107
- }
2108
- return this.configuration.token;
2109
- }
2110
- startSync() {
2111
- this.resetUnsyncedChanges();
2112
- this.send(SyncStepOneMessage, {
2113
- document: this.document,
2114
- documentName: this.configuration.name,
2115
- });
2116
- if (this.awareness && this.awareness.getLocalState() !== null) {
2117
- this.send(AwarenessMessage, {
2118
- awareness: this.awareness,
2119
- clients: [this.document.clientID],
2120
- documentName: this.configuration.name,
2121
- });
2122
- }
2123
- }
2124
- send(message, args) {
2125
- if (!this._isAttached)
2126
- return;
2127
- const messageSender = new MessageSender(message, args);
2128
- this.emit("outgoingMessage", { message: messageSender.message });
2129
- messageSender.send(this.configuration.websocketProvider);
2130
- }
2131
- onMessage(event) {
2132
- const message = new IncomingMessage(event.data);
2133
- const documentName = message.readVarString();
2134
- message.writeVarString(documentName);
2135
- this.emit("message", { event, message: new IncomingMessage(event.data) });
2136
- new MessageReceiver(message).apply(this, true);
2137
- }
2138
- onClose() {
2139
- this.isAuthenticated = false;
2140
- this.synced = false;
2141
- // update awareness (all users except local left)
2142
- if (this.awareness) {
2143
- removeAwarenessStates(this.awareness, Array.from(this.awareness.getStates().keys()).filter((client) => client !== this.document.clientID), this);
2144
- }
2145
- }
2146
- destroy() {
2147
- this.emit("destroy");
2148
- if (this.intervals.forceSync) {
2149
- clearInterval(this.intervals.forceSync);
2150
- }
2151
- if (this.awareness) {
2152
- removeAwarenessStates(this.awareness, [this.document.clientID], "provider destroy");
2153
- this.awareness.off("update", this.boundAwarenessUpdateHandler);
2154
- this.awareness.destroy();
2155
- }
2156
- this.document.off("update", this.boundDocumentUpdateHandler);
2157
- this.removeAllListeners();
2158
- this.detach();
2159
- if (this.manageSocket) {
2160
- this.configuration.websocketProvider.destroy();
2161
- }
2162
- if (typeof window === "undefined" || !("removeEventListener" in window)) {
2163
- return;
2164
- }
2165
- window.removeEventListener("pagehide", this.boundPageHide);
2166
- }
2167
- detach() {
2168
- this.configuration.websocketProvider.off("connect", this.configuration.onConnect);
2169
- this.configuration.websocketProvider.off("connect", this.forwardConnect);
2170
- this.configuration.websocketProvider.off("status", this.forwardStatus);
2171
- this.configuration.websocketProvider.off("status", this.configuration.onStatus);
2172
- this.configuration.websocketProvider.off("open", this.boundOnOpen);
2173
- this.configuration.websocketProvider.off("close", this.boundOnClose);
2174
- this.configuration.websocketProvider.off("close", this.configuration.onClose);
2175
- this.configuration.websocketProvider.off("close", this.forwardClose);
2176
- this.configuration.websocketProvider.off("disconnect", this.configuration.onDisconnect);
2177
- this.configuration.websocketProvider.off("disconnect", this.forwardDisconnect);
2178
- this.configuration.websocketProvider.off("destroy", this.configuration.onDestroy);
2179
- this.configuration.websocketProvider.off("destroy", this.forwardDestroy);
2180
- this.configuration.websocketProvider.detach(this);
2181
- this._isAttached = false;
2182
- }
2183
- attach() {
2184
- if (this._isAttached)
2185
- return;
2186
- this.configuration.websocketProvider.on("connect", this.configuration.onConnect);
2187
- this.configuration.websocketProvider.on("connect", this.forwardConnect);
2188
- this.configuration.websocketProvider.on("status", this.configuration.onStatus);
2189
- this.configuration.websocketProvider.on("status", this.forwardStatus);
2190
- this.configuration.websocketProvider.on("open", this.boundOnOpen);
2191
- this.configuration.websocketProvider.on("close", this.boundOnClose);
2192
- this.configuration.websocketProvider.on("close", this.configuration.onClose);
2193
- this.configuration.websocketProvider.on("close", this.forwardClose);
2194
- this.configuration.websocketProvider.on("disconnect", this.configuration.onDisconnect);
2195
- this.configuration.websocketProvider.on("disconnect", this.forwardDisconnect);
2196
- this.configuration.websocketProvider.on("destroy", this.configuration.onDestroy);
2197
- this.configuration.websocketProvider.on("destroy", this.forwardDestroy);
2198
- this.configuration.websocketProvider.attach(this);
2199
- this._isAttached = true;
2200
- }
2201
- permissionDeniedHandler(reason) {
2202
- this.emit("authenticationFailed", { reason });
2203
- this.isAuthenticated = false;
2204
- }
2205
- authenticatedHandler(scope) {
2206
- this.isAuthenticated = true;
2207
- this.authorizedScope = scope;
2208
- this.emit("authenticated", { scope });
2209
- }
2210
- setAwarenessField(key, value) {
2211
- if (!this.awareness) {
2212
- throw new AwarenessError(`Cannot set awareness field "${key}" to ${JSON.stringify(value)}. You have disabled Awareness for this provider by explicitly passing awareness: null in the provider configuration.`);
2213
- }
2214
- this.awareness.setLocalStateField(key, value);
2215
- }
2216
- }
1677
+ //#endregion
1678
+ //#region packages/provider/src/HocuspocusProvider.ts
1679
+ var AwarenessError = class extends Error {
1680
+ constructor(..._args) {
1681
+ super(..._args);
1682
+ this.code = 1001;
1683
+ }
1684
+ };
1685
+ var HocuspocusProvider = class extends EventEmitter {
1686
+ constructor(configuration) {
1687
+ super();
1688
+ this.configuration = {
1689
+ name: "",
1690
+ document: void 0,
1691
+ awareness: void 0,
1692
+ token: null,
1693
+ forceSyncInterval: false,
1694
+ onAuthenticated: () => null,
1695
+ onAuthenticationFailed: () => null,
1696
+ onOpen: () => null,
1697
+ onConnect: () => null,
1698
+ onMessage: () => null,
1699
+ onOutgoingMessage: () => null,
1700
+ onSynced: () => null,
1701
+ onStatus: () => null,
1702
+ onDisconnect: () => null,
1703
+ onClose: () => null,
1704
+ onDestroy: () => null,
1705
+ onAwarenessUpdate: () => null,
1706
+ onAwarenessChange: () => null,
1707
+ onStateless: () => null,
1708
+ onUnsyncedChanges: () => null
1709
+ };
1710
+ this.isSynced = false;
1711
+ this.unsyncedChanges = 0;
1712
+ this.isAuthenticated = false;
1713
+ this.authorizedScope = void 0;
1714
+ this.manageSocket = false;
1715
+ this._isAttached = false;
1716
+ this.intervals = { forceSync: null };
1717
+ this.boundDocumentUpdateHandler = this.documentUpdateHandler.bind(this);
1718
+ this.boundAwarenessUpdateHandler = this.awarenessUpdateHandler.bind(this);
1719
+ this.boundPageHide = this.pageHide.bind(this);
1720
+ this.boundOnOpen = this.onOpen.bind(this);
1721
+ this.boundOnClose = this.onClose.bind(this);
1722
+ this.forwardConnect = () => this.emit("connect");
1723
+ this.forwardStatus = (e) => this.emit("status", e);
1724
+ this.forwardClose = (e) => this.emit("close", e);
1725
+ this.forwardDisconnect = (e) => this.emit("disconnect", e);
1726
+ this.forwardDestroy = () => this.emit("destroy");
1727
+ this.setConfiguration(configuration);
1728
+ this.configuration.document = configuration.document ? configuration.document : new yjs.Doc();
1729
+ this.configuration.awareness = configuration.awareness !== void 0 ? configuration.awareness : new Awareness(this.document);
1730
+ this.on("open", this.configuration.onOpen);
1731
+ this.on("message", this.configuration.onMessage);
1732
+ this.on("outgoingMessage", this.configuration.onOutgoingMessage);
1733
+ this.on("synced", this.configuration.onSynced);
1734
+ this.on("destroy", this.configuration.onDestroy);
1735
+ this.on("awarenessUpdate", this.configuration.onAwarenessUpdate);
1736
+ this.on("awarenessChange", this.configuration.onAwarenessChange);
1737
+ this.on("stateless", this.configuration.onStateless);
1738
+ this.on("unsyncedChanges", this.configuration.onUnsyncedChanges);
1739
+ this.on("authenticated", this.configuration.onAuthenticated);
1740
+ this.on("authenticationFailed", this.configuration.onAuthenticationFailed);
1741
+ this.awareness?.on("update", () => {
1742
+ this.emit("awarenessUpdate", { states: (0, _hocuspocus_common.awarenessStatesToArray)(this.awareness.getStates()) });
1743
+ });
1744
+ this.awareness?.on("change", () => {
1745
+ this.emit("awarenessChange", { states: (0, _hocuspocus_common.awarenessStatesToArray)(this.awareness.getStates()) });
1746
+ });
1747
+ this.document.on("update", this.boundDocumentUpdateHandler);
1748
+ this.awareness?.on("update", this.boundAwarenessUpdateHandler);
1749
+ this.registerEventListeners();
1750
+ if (this.configuration.forceSyncInterval && typeof this.configuration.forceSyncInterval === "number") this.intervals.forceSync = setInterval(this.forceSync.bind(this), this.configuration.forceSyncInterval);
1751
+ if (this.manageSocket) this.attach();
1752
+ }
1753
+ setConfiguration(configuration = {}) {
1754
+ if (!configuration.websocketProvider) {
1755
+ this.manageSocket = true;
1756
+ this.configuration.websocketProvider = new HocuspocusProviderWebsocket(configuration);
1757
+ }
1758
+ this.configuration = {
1759
+ ...this.configuration,
1760
+ ...configuration
1761
+ };
1762
+ }
1763
+ get document() {
1764
+ return this.configuration.document;
1765
+ }
1766
+ get isAttached() {
1767
+ return this._isAttached;
1768
+ }
1769
+ get awareness() {
1770
+ return this.configuration.awareness;
1771
+ }
1772
+ get hasUnsyncedChanges() {
1773
+ return this.unsyncedChanges > 0;
1774
+ }
1775
+ resetUnsyncedChanges() {
1776
+ this.unsyncedChanges = 1;
1777
+ this.emit("unsyncedChanges", { number: this.unsyncedChanges });
1778
+ }
1779
+ incrementUnsyncedChanges() {
1780
+ this.unsyncedChanges += 1;
1781
+ this.emit("unsyncedChanges", { number: this.unsyncedChanges });
1782
+ }
1783
+ decrementUnsyncedChanges() {
1784
+ if (this.unsyncedChanges > 0) this.unsyncedChanges -= 1;
1785
+ if (this.unsyncedChanges === 0) this.synced = true;
1786
+ this.emit("unsyncedChanges", { number: this.unsyncedChanges });
1787
+ }
1788
+ forceSync() {
1789
+ this.resetUnsyncedChanges();
1790
+ this.send(SyncStepOneMessage, {
1791
+ document: this.document,
1792
+ documentName: this.configuration.name
1793
+ });
1794
+ }
1795
+ pageHide() {
1796
+ if (this.awareness) removeAwarenessStates(this.awareness, [this.document.clientID], "page hide");
1797
+ }
1798
+ registerEventListeners() {
1799
+ if (typeof window === "undefined" || !("addEventListener" in window)) return;
1800
+ window.addEventListener("pagehide", this.boundPageHide);
1801
+ }
1802
+ sendStateless(payload) {
1803
+ this.send(StatelessMessage, {
1804
+ documentName: this.configuration.name,
1805
+ payload
1806
+ });
1807
+ }
1808
+ async sendToken() {
1809
+ let token;
1810
+ try {
1811
+ token = await this.getToken();
1812
+ } catch (error) {
1813
+ this.permissionDeniedHandler(`Failed to get token during sendToken(): ${error}`);
1814
+ return;
1815
+ }
1816
+ this.send(AuthenticationMessage, {
1817
+ token: token ?? "",
1818
+ documentName: this.configuration.name
1819
+ });
1820
+ }
1821
+ documentUpdateHandler(update, origin) {
1822
+ if (origin === this) return;
1823
+ this.incrementUnsyncedChanges();
1824
+ this.send(UpdateMessage, {
1825
+ update,
1826
+ documentName: this.configuration.name
1827
+ });
1828
+ }
1829
+ awarenessUpdateHandler({ added, updated, removed }, origin) {
1830
+ const changedClients = added.concat(updated).concat(removed);
1831
+ this.send(AwarenessMessage, {
1832
+ awareness: this.awareness,
1833
+ clients: changedClients,
1834
+ documentName: this.configuration.name
1835
+ });
1836
+ }
1837
+ /**
1838
+ * Indicates whether a first handshake with the server has been established
1839
+ *
1840
+ * Note: this does not mean all updates from the client have been persisted to the backend. For this,
1841
+ * use `hasUnsyncedChanges`.
1842
+ */
1843
+ get synced() {
1844
+ return this.isSynced;
1845
+ }
1846
+ set synced(state) {
1847
+ if (this.isSynced === state) return;
1848
+ this.isSynced = state;
1849
+ if (state) this.emit("synced", { state });
1850
+ }
1851
+ receiveStateless(payload) {
1852
+ this.emit("stateless", { payload });
1853
+ }
1854
+ async connect() {
1855
+ if (this.manageSocket) return this.configuration.websocketProvider.connect();
1856
+ console.warn("HocuspocusProvider::connect() is deprecated and does not do anything. Please connect/disconnect on the websocketProvider, or attach/deattach providers.");
1857
+ }
1858
+ disconnect() {
1859
+ if (this.manageSocket) return this.configuration.websocketProvider.disconnect();
1860
+ console.warn("HocuspocusProvider::disconnect() is deprecated and does not do anything. Please connect/disconnect on the websocketProvider, or attach/deattach providers.");
1861
+ }
1862
+ async onOpen(event) {
1863
+ this.isAuthenticated = false;
1864
+ this.emit("open", { event });
1865
+ await this.sendToken();
1866
+ this.startSync();
1867
+ }
1868
+ async getToken() {
1869
+ if (typeof this.configuration.token === "function") return await this.configuration.token();
1870
+ return this.configuration.token;
1871
+ }
1872
+ startSync() {
1873
+ this.resetUnsyncedChanges();
1874
+ this.send(SyncStepOneMessage, {
1875
+ document: this.document,
1876
+ documentName: this.configuration.name
1877
+ });
1878
+ if (this.awareness && this.awareness.getLocalState() !== null) this.send(AwarenessMessage, {
1879
+ awareness: this.awareness,
1880
+ clients: [this.document.clientID],
1881
+ documentName: this.configuration.name
1882
+ });
1883
+ }
1884
+ send(message, args) {
1885
+ if (!this._isAttached) return;
1886
+ const messageSender = new MessageSender(message, args);
1887
+ this.emit("outgoingMessage", { message: messageSender.message });
1888
+ messageSender.send(this.configuration.websocketProvider);
1889
+ }
1890
+ onMessage(event) {
1891
+ const message = new IncomingMessage(event.data);
1892
+ const documentName = message.readVarString();
1893
+ message.writeVarString(documentName);
1894
+ this.emit("message", {
1895
+ event,
1896
+ message: new IncomingMessage(event.data)
1897
+ });
1898
+ new MessageReceiver(message).apply(this, true);
1899
+ }
1900
+ onClose() {
1901
+ this.isAuthenticated = false;
1902
+ this.synced = false;
1903
+ if (this.awareness) removeAwarenessStates(this.awareness, Array.from(this.awareness.getStates().keys()).filter((client) => client !== this.document.clientID), this);
1904
+ }
1905
+ destroy() {
1906
+ this.emit("destroy");
1907
+ if (this.intervals.forceSync) clearInterval(this.intervals.forceSync);
1908
+ if (this.awareness) {
1909
+ removeAwarenessStates(this.awareness, [this.document.clientID], "provider destroy");
1910
+ this.awareness.off("update", this.boundAwarenessUpdateHandler);
1911
+ this.awareness.destroy();
1912
+ }
1913
+ this.document.off("update", this.boundDocumentUpdateHandler);
1914
+ this.removeAllListeners();
1915
+ this.detach();
1916
+ if (this.manageSocket) this.configuration.websocketProvider.destroy();
1917
+ if (typeof window === "undefined" || !("removeEventListener" in window)) return;
1918
+ window.removeEventListener("pagehide", this.boundPageHide);
1919
+ }
1920
+ detach() {
1921
+ this.configuration.websocketProvider.off("connect", this.configuration.onConnect);
1922
+ this.configuration.websocketProvider.off("connect", this.forwardConnect);
1923
+ this.configuration.websocketProvider.off("status", this.forwardStatus);
1924
+ this.configuration.websocketProvider.off("status", this.configuration.onStatus);
1925
+ this.configuration.websocketProvider.off("open", this.boundOnOpen);
1926
+ this.configuration.websocketProvider.off("close", this.boundOnClose);
1927
+ this.configuration.websocketProvider.off("close", this.configuration.onClose);
1928
+ this.configuration.websocketProvider.off("close", this.forwardClose);
1929
+ this.configuration.websocketProvider.off("disconnect", this.configuration.onDisconnect);
1930
+ this.configuration.websocketProvider.off("disconnect", this.forwardDisconnect);
1931
+ this.configuration.websocketProvider.off("destroy", this.configuration.onDestroy);
1932
+ this.configuration.websocketProvider.off("destroy", this.forwardDestroy);
1933
+ this.configuration.websocketProvider.detach(this);
1934
+ this._isAttached = false;
1935
+ }
1936
+ attach() {
1937
+ if (this._isAttached) return;
1938
+ this.configuration.websocketProvider.on("connect", this.configuration.onConnect);
1939
+ this.configuration.websocketProvider.on("connect", this.forwardConnect);
1940
+ this.configuration.websocketProvider.on("status", this.configuration.onStatus);
1941
+ this.configuration.websocketProvider.on("status", this.forwardStatus);
1942
+ this.configuration.websocketProvider.on("open", this.boundOnOpen);
1943
+ this.configuration.websocketProvider.on("close", this.boundOnClose);
1944
+ this.configuration.websocketProvider.on("close", this.configuration.onClose);
1945
+ this.configuration.websocketProvider.on("close", this.forwardClose);
1946
+ this.configuration.websocketProvider.on("disconnect", this.configuration.onDisconnect);
1947
+ this.configuration.websocketProvider.on("disconnect", this.forwardDisconnect);
1948
+ this.configuration.websocketProvider.on("destroy", this.configuration.onDestroy);
1949
+ this.configuration.websocketProvider.on("destroy", this.forwardDestroy);
1950
+ this.configuration.websocketProvider.attach(this);
1951
+ this._isAttached = true;
1952
+ }
1953
+ permissionDeniedHandler(reason) {
1954
+ this.emit("authenticationFailed", { reason });
1955
+ this.isAuthenticated = false;
1956
+ }
1957
+ authenticatedHandler(scope) {
1958
+ this.isAuthenticated = true;
1959
+ this.authorizedScope = scope;
1960
+ this.emit("authenticated", { scope });
1961
+ }
1962
+ setAwarenessField(key, value) {
1963
+ if (!this.awareness) throw new AwarenessError(`Cannot set awareness field "${key}" to ${JSON.stringify(value)}. You have disabled Awareness for this provider by explicitly passing awareness: null in the provider configuration.`);
1964
+ this.awareness.setLocalStateField(key, value);
1965
+ }
1966
+ };
2217
1967
 
1968
+ //#endregion
2218
1969
  exports.AwarenessError = AwarenessError;
2219
1970
  exports.HocuspocusProvider = HocuspocusProvider;
2220
1971
  exports.HocuspocusProviderWebsocket = HocuspocusProviderWebsocket;
2221
- //# sourceMappingURL=hocuspocus-provider.cjs.map
1972
+ exports.MessageType = MessageType;
1973
+ exports.WebSocketStatus = WebSocketStatus;
1974
+ //# sourceMappingURL=hocuspocus-provider.cjs.map