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