@inditextech/weave-store-azure-web-pubsub 0.2.0 → 0.3.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/client.cjs +1075 -46
- package/dist/client.d.cts +60 -21
- package/dist/client.d.ts +61 -22
- package/dist/client.js +1010 -3
- package/dist/server.cjs +1059 -30
- package/dist/server.d.cts +36 -1
- package/dist/server.d.ts +36 -1
- package/dist/server.js +1009 -2
- package/package.json +6 -5
- package/dist/awareness-CJH5s1Gv.cjs +0 -1153
- package/dist/awareness-CZbSemWr.js +0 -1010
- package/dist/types.d-BjiC_a5v.d.cts +0 -38
- package/dist/types.d-Cw5mafrL.d.ts +0 -38
package/dist/client.cjs
CHANGED
|
@@ -1,13 +1,1042 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
//#region rolldown:runtime
|
|
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$1, except, desc) => {
|
|
10
|
+
if (from$1 && typeof from$1 === "object" || typeof from$1 === "function") for (var keys$1 = __getOwnPropNames(from$1), i = 0, n = keys$1.length, key; i < n; i++) {
|
|
11
|
+
key = keys$1[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
13
|
+
get: ((k) => from$1[k]).bind(null, key),
|
|
14
|
+
enumerable: !(desc = __getOwnPropDesc(from$1, key)) || desc.enumerable
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
20
|
+
value: mod,
|
|
21
|
+
enumerable: true
|
|
22
|
+
}) : target, mod));
|
|
23
|
+
|
|
24
|
+
//#endregion
|
|
25
|
+
const __inditextech_weave_sdk = __toESM(require("@inditextech/weave-sdk"));
|
|
5
26
|
require("@inditextech/weave-types");
|
|
6
|
-
const emittery =
|
|
7
|
-
const buffer =
|
|
8
|
-
const uuid =
|
|
9
|
-
const
|
|
27
|
+
const emittery = __toESM(require("emittery"));
|
|
28
|
+
const buffer = __toESM(require("buffer"));
|
|
29
|
+
const uuid = __toESM(require("uuid"));
|
|
30
|
+
const yjs = __toESM(require("yjs"));
|
|
31
|
+
const reconnecting_websocket = __toESM(require("reconnecting-websocket"));
|
|
32
|
+
|
|
33
|
+
//#region ../../node_modules/lib0/math.js
|
|
34
|
+
/**
|
|
35
|
+
* Common Math expressions.
|
|
36
|
+
*
|
|
37
|
+
* @module math
|
|
38
|
+
*/
|
|
39
|
+
const floor = Math.floor;
|
|
40
|
+
/**
|
|
41
|
+
* @function
|
|
42
|
+
* @param {number} a
|
|
43
|
+
* @param {number} b
|
|
44
|
+
* @return {number} The smaller element of a and b
|
|
45
|
+
*/
|
|
46
|
+
const min = (a, b) => a < b ? a : b;
|
|
47
|
+
/**
|
|
48
|
+
* @function
|
|
49
|
+
* @param {number} a
|
|
50
|
+
* @param {number} b
|
|
51
|
+
* @return {number} The bigger element of a and b
|
|
52
|
+
*/
|
|
53
|
+
const max = (a, b) => a > b ? a : b;
|
|
54
|
+
const isNaN$1 = Number.isNaN;
|
|
55
|
+
|
|
56
|
+
//#endregion
|
|
57
|
+
//#region ../../node_modules/lib0/binary.js
|
|
58
|
+
const BIT8 = 128;
|
|
59
|
+
const BIT18 = 1 << 17;
|
|
60
|
+
const BIT19 = 1 << 18;
|
|
61
|
+
const BIT20 = 1 << 19;
|
|
62
|
+
const BIT21 = 1 << 20;
|
|
63
|
+
const BIT22 = 1 << 21;
|
|
64
|
+
const BIT23 = 1 << 22;
|
|
65
|
+
const BIT24 = 1 << 23;
|
|
66
|
+
const BIT25 = 1 << 24;
|
|
67
|
+
const BIT26 = 1 << 25;
|
|
68
|
+
const BIT27 = 1 << 26;
|
|
69
|
+
const BIT28 = 1 << 27;
|
|
70
|
+
const BIT29 = 1 << 28;
|
|
71
|
+
const BIT30 = 1 << 29;
|
|
72
|
+
const BIT31 = 1 << 30;
|
|
73
|
+
const BIT32 = 1 << 31;
|
|
74
|
+
const BITS7 = 127;
|
|
75
|
+
const BITS17 = BIT18 - 1;
|
|
76
|
+
const BITS18 = BIT19 - 1;
|
|
77
|
+
const BITS19 = BIT20 - 1;
|
|
78
|
+
const BITS20 = BIT21 - 1;
|
|
79
|
+
const BITS21 = BIT22 - 1;
|
|
80
|
+
const BITS22 = BIT23 - 1;
|
|
81
|
+
const BITS23 = BIT24 - 1;
|
|
82
|
+
const BITS24 = BIT25 - 1;
|
|
83
|
+
const BITS25 = BIT26 - 1;
|
|
84
|
+
const BITS26 = BIT27 - 1;
|
|
85
|
+
const BITS27 = BIT28 - 1;
|
|
86
|
+
const BITS28 = BIT29 - 1;
|
|
87
|
+
const BITS29 = BIT30 - 1;
|
|
88
|
+
const BITS30 = BIT31 - 1;
|
|
89
|
+
/**
|
|
90
|
+
* @type {number}
|
|
91
|
+
*/
|
|
92
|
+
const BITS31 = 2147483647;
|
|
93
|
+
/**
|
|
94
|
+
* @type {number}
|
|
95
|
+
*/
|
|
96
|
+
const BITS32 = 4294967295;
|
|
97
|
+
|
|
98
|
+
//#endregion
|
|
99
|
+
//#region ../../node_modules/lib0/number.js
|
|
100
|
+
const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER;
|
|
101
|
+
const MIN_SAFE_INTEGER = Number.MIN_SAFE_INTEGER;
|
|
102
|
+
const LOWEST_INT32 = 1 << 31;
|
|
103
|
+
const HIGHEST_INT32 = BITS31;
|
|
104
|
+
const HIGHEST_UINT32 = BITS32;
|
|
105
|
+
/* c8 ignore next */
|
|
106
|
+
const isInteger = Number.isInteger || ((num) => typeof num === "number" && isFinite(num) && floor(num) === num);
|
|
107
|
+
const isNaN = Number.isNaN;
|
|
108
|
+
const parseInt = Number.parseInt;
|
|
109
|
+
|
|
110
|
+
//#endregion
|
|
111
|
+
//#region ../../node_modules/lib0/set.js
|
|
112
|
+
/**
|
|
113
|
+
* Utility module to work with sets.
|
|
114
|
+
*
|
|
115
|
+
* @module set
|
|
116
|
+
*/
|
|
117
|
+
const create$2 = () => new Set();
|
|
118
|
+
|
|
119
|
+
//#endregion
|
|
120
|
+
//#region ../../node_modules/lib0/array.js
|
|
121
|
+
/**
|
|
122
|
+
* Transforms something array-like to an actual Array.
|
|
123
|
+
*
|
|
124
|
+
* @function
|
|
125
|
+
* @template T
|
|
126
|
+
* @param {ArrayLike<T>|Iterable<T>} arraylike
|
|
127
|
+
* @return {T}
|
|
128
|
+
*/
|
|
129
|
+
const from = Array.from;
|
|
130
|
+
const isArray$1 = Array.isArray;
|
|
131
|
+
|
|
132
|
+
//#endregion
|
|
133
|
+
//#region ../../node_modules/lib0/string.js
|
|
134
|
+
/**
|
|
135
|
+
* Utility module to work with strings.
|
|
136
|
+
*
|
|
137
|
+
* @module string
|
|
138
|
+
*/
|
|
139
|
+
const fromCharCode = String.fromCharCode;
|
|
140
|
+
const fromCodePoint = String.fromCodePoint;
|
|
141
|
+
/**
|
|
142
|
+
* The largest utf16 character.
|
|
143
|
+
* Corresponds to Uint8Array([255, 255]) or charcodeof(2x2^8)
|
|
144
|
+
*/
|
|
145
|
+
const MAX_UTF16_CHARACTER = fromCharCode(65535);
|
|
146
|
+
/**
|
|
147
|
+
* @param {string} str
|
|
148
|
+
* @return {Uint8Array}
|
|
149
|
+
*/
|
|
150
|
+
const _encodeUtf8Polyfill = (str) => {
|
|
151
|
+
const encodedString = unescape(encodeURIComponent(str));
|
|
152
|
+
const len = encodedString.length;
|
|
153
|
+
const buf = new Uint8Array(len);
|
|
154
|
+
for (let i = 0; i < len; i++) buf[i] = encodedString.codePointAt(i);
|
|
155
|
+
return buf;
|
|
156
|
+
};
|
|
157
|
+
/* c8 ignore next */
|
|
158
|
+
const utf8TextEncoder = typeof TextEncoder !== "undefined" ? new TextEncoder() : null;
|
|
159
|
+
/**
|
|
160
|
+
* @param {string} str
|
|
161
|
+
* @return {Uint8Array}
|
|
162
|
+
*/
|
|
163
|
+
const _encodeUtf8Native = (str) => utf8TextEncoder.encode(str);
|
|
164
|
+
/**
|
|
165
|
+
* @param {string} str
|
|
166
|
+
* @return {Uint8Array}
|
|
167
|
+
*/
|
|
168
|
+
/* c8 ignore next */
|
|
169
|
+
const encodeUtf8 = utf8TextEncoder ? _encodeUtf8Native : _encodeUtf8Polyfill;
|
|
170
|
+
/* c8 ignore next */
|
|
171
|
+
let utf8TextDecoder = typeof TextDecoder === "undefined" ? null : new TextDecoder("utf-8", {
|
|
172
|
+
fatal: true,
|
|
173
|
+
ignoreBOM: true
|
|
174
|
+
});
|
|
175
|
+
/* c8 ignore start */
|
|
176
|
+
if (utf8TextDecoder && utf8TextDecoder.decode(new Uint8Array()).length === 1)
|
|
177
|
+
/* c8 ignore next */
|
|
178
|
+
utf8TextDecoder = null;
|
|
179
|
+
|
|
180
|
+
//#endregion
|
|
181
|
+
//#region ../../node_modules/lib0/encoding.js
|
|
182
|
+
/**
|
|
183
|
+
* A BinaryEncoder handles the encoding to an Uint8Array.
|
|
184
|
+
*/
|
|
185
|
+
var Encoder = class {
|
|
186
|
+
constructor() {
|
|
187
|
+
this.cpos = 0;
|
|
188
|
+
this.cbuf = new Uint8Array(100);
|
|
189
|
+
/**
|
|
190
|
+
* @type {Array<Uint8Array>}
|
|
191
|
+
*/
|
|
192
|
+
this.bufs = [];
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
/**
|
|
196
|
+
* @function
|
|
197
|
+
* @return {Encoder}
|
|
198
|
+
*/
|
|
199
|
+
const createEncoder = () => new Encoder();
|
|
200
|
+
/**
|
|
201
|
+
* The current length of the encoded data.
|
|
202
|
+
*
|
|
203
|
+
* @function
|
|
204
|
+
* @param {Encoder} encoder
|
|
205
|
+
* @return {number}
|
|
206
|
+
*/
|
|
207
|
+
const length$1 = (encoder) => {
|
|
208
|
+
let len = encoder.cpos;
|
|
209
|
+
for (let i = 0; i < encoder.bufs.length; i++) len += encoder.bufs[i].length;
|
|
210
|
+
return len;
|
|
211
|
+
};
|
|
212
|
+
/**
|
|
213
|
+
* Transform to Uint8Array.
|
|
214
|
+
*
|
|
215
|
+
* @function
|
|
216
|
+
* @param {Encoder} encoder
|
|
217
|
+
* @return {Uint8Array} The created ArrayBuffer.
|
|
218
|
+
*/
|
|
219
|
+
const toUint8Array = (encoder) => {
|
|
220
|
+
const uint8arr = new Uint8Array(length$1(encoder));
|
|
221
|
+
let curPos = 0;
|
|
222
|
+
for (let i = 0; i < encoder.bufs.length; i++) {
|
|
223
|
+
const d = encoder.bufs[i];
|
|
224
|
+
uint8arr.set(d, curPos);
|
|
225
|
+
curPos += d.length;
|
|
226
|
+
}
|
|
227
|
+
uint8arr.set(new Uint8Array(encoder.cbuf.buffer, 0, encoder.cpos), curPos);
|
|
228
|
+
return uint8arr;
|
|
229
|
+
};
|
|
230
|
+
/**
|
|
231
|
+
* Write one byte to the encoder.
|
|
232
|
+
*
|
|
233
|
+
* @function
|
|
234
|
+
* @param {Encoder} encoder
|
|
235
|
+
* @param {number} num The byte that is to be encoded.
|
|
236
|
+
*/
|
|
237
|
+
const write = (encoder, num) => {
|
|
238
|
+
const bufferLen = encoder.cbuf.length;
|
|
239
|
+
if (encoder.cpos === bufferLen) {
|
|
240
|
+
encoder.bufs.push(encoder.cbuf);
|
|
241
|
+
encoder.cbuf = new Uint8Array(bufferLen * 2);
|
|
242
|
+
encoder.cpos = 0;
|
|
243
|
+
}
|
|
244
|
+
encoder.cbuf[encoder.cpos++] = num;
|
|
245
|
+
};
|
|
246
|
+
/**
|
|
247
|
+
* Write a variable length unsigned integer. Max encodable integer is 2^53.
|
|
248
|
+
*
|
|
249
|
+
* @function
|
|
250
|
+
* @param {Encoder} encoder
|
|
251
|
+
* @param {number} num The number that is to be encoded.
|
|
252
|
+
*/
|
|
253
|
+
const writeVarUint = (encoder, num) => {
|
|
254
|
+
while (num > BITS7) {
|
|
255
|
+
write(encoder, BIT8 | BITS7 & num);
|
|
256
|
+
num = floor(num / 128);
|
|
257
|
+
}
|
|
258
|
+
write(encoder, BITS7 & num);
|
|
259
|
+
};
|
|
260
|
+
/**
|
|
261
|
+
* A cache to store strings temporarily
|
|
262
|
+
*/
|
|
263
|
+
const _strBuffer = new Uint8Array(3e4);
|
|
264
|
+
const _maxStrBSize = _strBuffer.length / 3;
|
|
265
|
+
/**
|
|
266
|
+
* Write a variable length string.
|
|
267
|
+
*
|
|
268
|
+
* @function
|
|
269
|
+
* @param {Encoder} encoder
|
|
270
|
+
* @param {String} str The string that is to be encoded.
|
|
271
|
+
*/
|
|
272
|
+
const _writeVarStringNative = (encoder, str) => {
|
|
273
|
+
if (str.length < _maxStrBSize) {
|
|
274
|
+
/* c8 ignore next */
|
|
275
|
+
const written = utf8TextEncoder.encodeInto(str, _strBuffer).written || 0;
|
|
276
|
+
writeVarUint(encoder, written);
|
|
277
|
+
for (let i = 0; i < written; i++) write(encoder, _strBuffer[i]);
|
|
278
|
+
} else writeVarUint8Array(encoder, encodeUtf8(str));
|
|
279
|
+
};
|
|
280
|
+
/**
|
|
281
|
+
* Write a variable length string.
|
|
282
|
+
*
|
|
283
|
+
* @function
|
|
284
|
+
* @param {Encoder} encoder
|
|
285
|
+
* @param {String} str The string that is to be encoded.
|
|
286
|
+
*/
|
|
287
|
+
const _writeVarStringPolyfill = (encoder, str) => {
|
|
288
|
+
const encodedString = unescape(encodeURIComponent(str));
|
|
289
|
+
const len = encodedString.length;
|
|
290
|
+
writeVarUint(encoder, len);
|
|
291
|
+
for (let i = 0; i < len; i++) write(
|
|
292
|
+
encoder,
|
|
293
|
+
/** @type {number} */
|
|
294
|
+
encodedString.codePointAt(i)
|
|
295
|
+
);
|
|
296
|
+
};
|
|
297
|
+
/**
|
|
298
|
+
* Write a variable length string.
|
|
299
|
+
*
|
|
300
|
+
* @function
|
|
301
|
+
* @param {Encoder} encoder
|
|
302
|
+
* @param {String} str The string that is to be encoded.
|
|
303
|
+
*/
|
|
304
|
+
/* c8 ignore next */
|
|
305
|
+
const writeVarString = utf8TextEncoder && utf8TextEncoder.encodeInto ? _writeVarStringNative : _writeVarStringPolyfill;
|
|
306
|
+
/**
|
|
307
|
+
* Append fixed-length Uint8Array to the encoder.
|
|
308
|
+
*
|
|
309
|
+
* @function
|
|
310
|
+
* @param {Encoder} encoder
|
|
311
|
+
* @param {Uint8Array} uint8Array
|
|
312
|
+
*/
|
|
313
|
+
const writeUint8Array = (encoder, uint8Array) => {
|
|
314
|
+
const bufferLen = encoder.cbuf.length;
|
|
315
|
+
const cpos = encoder.cpos;
|
|
316
|
+
const leftCopyLen = min(bufferLen - cpos, uint8Array.length);
|
|
317
|
+
const rightCopyLen = uint8Array.length - leftCopyLen;
|
|
318
|
+
encoder.cbuf.set(uint8Array.subarray(0, leftCopyLen), cpos);
|
|
319
|
+
encoder.cpos += leftCopyLen;
|
|
320
|
+
if (rightCopyLen > 0) {
|
|
321
|
+
encoder.bufs.push(encoder.cbuf);
|
|
322
|
+
encoder.cbuf = new Uint8Array(max(bufferLen * 2, rightCopyLen));
|
|
323
|
+
encoder.cbuf.set(uint8Array.subarray(leftCopyLen));
|
|
324
|
+
encoder.cpos = rightCopyLen;
|
|
325
|
+
}
|
|
326
|
+
};
|
|
327
|
+
/**
|
|
328
|
+
* Append an Uint8Array to Encoder.
|
|
329
|
+
*
|
|
330
|
+
* @function
|
|
331
|
+
* @param {Encoder} encoder
|
|
332
|
+
* @param {Uint8Array} uint8Array
|
|
333
|
+
*/
|
|
334
|
+
const writeVarUint8Array = (encoder, uint8Array) => {
|
|
335
|
+
writeVarUint(encoder, uint8Array.byteLength);
|
|
336
|
+
writeUint8Array(encoder, uint8Array);
|
|
337
|
+
};
|
|
338
|
+
const floatTestBed = new DataView(new ArrayBuffer(4));
|
|
339
|
+
|
|
340
|
+
//#endregion
|
|
341
|
+
//#region ../../node_modules/lib0/error.js
|
|
342
|
+
/**
|
|
343
|
+
* Error helpers.
|
|
344
|
+
*
|
|
345
|
+
* @module error
|
|
346
|
+
*/
|
|
347
|
+
/**
|
|
348
|
+
* @param {string} s
|
|
349
|
+
* @return {Error}
|
|
350
|
+
*/
|
|
351
|
+
/* c8 ignore next */
|
|
352
|
+
const create$1 = (s) => new Error(s);
|
|
353
|
+
|
|
354
|
+
//#endregion
|
|
355
|
+
//#region ../../node_modules/lib0/decoding.js
|
|
356
|
+
const errorUnexpectedEndOfArray = create$1("Unexpected end of array");
|
|
357
|
+
const errorIntegerOutOfRange = create$1("Integer out of Range");
|
|
358
|
+
/**
|
|
359
|
+
* A Decoder handles the decoding of an Uint8Array.
|
|
360
|
+
*/
|
|
361
|
+
var Decoder = class {
|
|
362
|
+
/**
|
|
363
|
+
* @param {Uint8Array} uint8Array Binary data to decode
|
|
364
|
+
*/
|
|
365
|
+
constructor(uint8Array) {
|
|
366
|
+
/**
|
|
367
|
+
* Decoding target.
|
|
368
|
+
*
|
|
369
|
+
* @type {Uint8Array}
|
|
370
|
+
*/
|
|
371
|
+
this.arr = uint8Array;
|
|
372
|
+
/**
|
|
373
|
+
* Current decoding position.
|
|
374
|
+
*
|
|
375
|
+
* @type {number}
|
|
376
|
+
*/
|
|
377
|
+
this.pos = 0;
|
|
378
|
+
}
|
|
379
|
+
};
|
|
380
|
+
/**
|
|
381
|
+
* @function
|
|
382
|
+
* @param {Uint8Array} uint8Array
|
|
383
|
+
* @return {Decoder}
|
|
384
|
+
*/
|
|
385
|
+
const createDecoder = (uint8Array) => new Decoder(uint8Array);
|
|
386
|
+
/**
|
|
387
|
+
* Create an Uint8Array view of the next `len` bytes and advance the position by `len`.
|
|
388
|
+
*
|
|
389
|
+
* Important: The Uint8Array still points to the underlying ArrayBuffer. Make sure to discard the result as soon as possible to prevent any memory leaks.
|
|
390
|
+
* Use `buffer.copyUint8Array` to copy the result into a new Uint8Array.
|
|
391
|
+
*
|
|
392
|
+
* @function
|
|
393
|
+
* @param {Decoder} decoder The decoder instance
|
|
394
|
+
* @param {number} len The length of bytes to read
|
|
395
|
+
* @return {Uint8Array}
|
|
396
|
+
*/
|
|
397
|
+
const readUint8Array = (decoder, len) => {
|
|
398
|
+
const view = new Uint8Array(decoder.arr.buffer, decoder.pos + decoder.arr.byteOffset, len);
|
|
399
|
+
decoder.pos += len;
|
|
400
|
+
return view;
|
|
401
|
+
};
|
|
402
|
+
/**
|
|
403
|
+
* Read variable length Uint8Array.
|
|
404
|
+
*
|
|
405
|
+
* Important: The Uint8Array still points to the underlying ArrayBuffer. Make sure to discard the result as soon as possible to prevent any memory leaks.
|
|
406
|
+
* Use `buffer.copyUint8Array` to copy the result into a new Uint8Array.
|
|
407
|
+
*
|
|
408
|
+
* @function
|
|
409
|
+
* @param {Decoder} decoder
|
|
410
|
+
* @return {Uint8Array}
|
|
411
|
+
*/
|
|
412
|
+
const readVarUint8Array = (decoder) => readUint8Array(decoder, readVarUint(decoder));
|
|
413
|
+
/**
|
|
414
|
+
* Read one byte as unsigned integer.
|
|
415
|
+
* @function
|
|
416
|
+
* @param {Decoder} decoder The decoder instance
|
|
417
|
+
* @return {number} Unsigned 8-bit integer
|
|
418
|
+
*/
|
|
419
|
+
const readUint8 = (decoder) => decoder.arr[decoder.pos++];
|
|
420
|
+
/**
|
|
421
|
+
* Read unsigned integer (32bit) with variable length.
|
|
422
|
+
* 1/8th of the storage is used as encoding overhead.
|
|
423
|
+
* * numbers < 2^7 is stored in one bytlength
|
|
424
|
+
* * numbers < 2^14 is stored in two bylength
|
|
425
|
+
*
|
|
426
|
+
* @function
|
|
427
|
+
* @param {Decoder} decoder
|
|
428
|
+
* @return {number} An unsigned integer.length
|
|
429
|
+
*/
|
|
430
|
+
const readVarUint = (decoder) => {
|
|
431
|
+
let num = 0;
|
|
432
|
+
let mult = 1;
|
|
433
|
+
const len = decoder.arr.length;
|
|
434
|
+
while (decoder.pos < len) {
|
|
435
|
+
const r = decoder.arr[decoder.pos++];
|
|
436
|
+
num = num + (r & BITS7) * mult;
|
|
437
|
+
mult *= 128;
|
|
438
|
+
if (r < BIT8) return num;
|
|
439
|
+
/* c8 ignore start */
|
|
440
|
+
if (num > MAX_SAFE_INTEGER) throw errorIntegerOutOfRange;
|
|
441
|
+
}
|
|
442
|
+
throw errorUnexpectedEndOfArray;
|
|
443
|
+
};
|
|
444
|
+
/**
|
|
445
|
+
* We don't test this function anymore as we use native decoding/encoding by default now.
|
|
446
|
+
* Better not modify this anymore..
|
|
447
|
+
*
|
|
448
|
+
* Transforming utf8 to a string is pretty expensive. The code performs 10x better
|
|
449
|
+
* when String.fromCodePoint is fed with all characters as arguments.
|
|
450
|
+
* But most environments have a maximum number of arguments per functions.
|
|
451
|
+
* For effiency reasons we apply a maximum of 10000 characters at once.
|
|
452
|
+
*
|
|
453
|
+
* @function
|
|
454
|
+
* @param {Decoder} decoder
|
|
455
|
+
* @return {String} The read String.
|
|
456
|
+
*/
|
|
457
|
+
/* c8 ignore start */
|
|
458
|
+
const _readVarStringPolyfill = (decoder) => {
|
|
459
|
+
let remainingLen = readVarUint(decoder);
|
|
460
|
+
if (remainingLen === 0) return "";
|
|
461
|
+
else {
|
|
462
|
+
let encodedString = String.fromCodePoint(readUint8(decoder));
|
|
463
|
+
if (--remainingLen < 100) while (remainingLen--) encodedString += String.fromCodePoint(readUint8(decoder));
|
|
464
|
+
else while (remainingLen > 0) {
|
|
465
|
+
const nextLen = remainingLen < 1e4 ? remainingLen : 1e4;
|
|
466
|
+
const bytes = decoder.arr.subarray(decoder.pos, decoder.pos + nextLen);
|
|
467
|
+
decoder.pos += nextLen;
|
|
468
|
+
encodedString += String.fromCodePoint.apply(
|
|
469
|
+
null,
|
|
470
|
+
/** @type {any} */
|
|
471
|
+
bytes
|
|
472
|
+
);
|
|
473
|
+
remainingLen -= nextLen;
|
|
474
|
+
}
|
|
475
|
+
return decodeURIComponent(escape(encodedString));
|
|
476
|
+
}
|
|
477
|
+
};
|
|
478
|
+
/* c8 ignore stop */
|
|
479
|
+
/**
|
|
480
|
+
* @function
|
|
481
|
+
* @param {Decoder} decoder
|
|
482
|
+
* @return {String} The read String
|
|
483
|
+
*/
|
|
484
|
+
const _readVarStringNative = (decoder) => utf8TextDecoder.decode(readVarUint8Array(decoder));
|
|
485
|
+
/**
|
|
486
|
+
* Read string of variable length
|
|
487
|
+
* * varUint is used to store the length of the string
|
|
488
|
+
*
|
|
489
|
+
* @function
|
|
490
|
+
* @param {Decoder} decoder
|
|
491
|
+
* @return {String} The read String
|
|
492
|
+
*
|
|
493
|
+
*/
|
|
494
|
+
/* c8 ignore next */
|
|
495
|
+
const readVarString = utf8TextDecoder ? _readVarStringNative : _readVarStringPolyfill;
|
|
496
|
+
|
|
497
|
+
//#endregion
|
|
498
|
+
//#region ../../node_modules/y-protocols/sync.js
|
|
499
|
+
/**
|
|
500
|
+
* @typedef {Map<number, number>} StateMap
|
|
501
|
+
*/
|
|
502
|
+
/**
|
|
503
|
+
* Core Yjs defines two message types:
|
|
504
|
+
* • YjsSyncStep1: Includes the State Set of the sending client. When received, the client should reply with YjsSyncStep2.
|
|
505
|
+
* • YjsSyncStep2: Includes all missing structs and the complete delete set. When received, the client is assured that it
|
|
506
|
+
* received all information from the remote client.
|
|
507
|
+
*
|
|
508
|
+
* In a peer-to-peer network, you may want to introduce a SyncDone message type. Both parties should initiate the connection
|
|
509
|
+
* with SyncStep1. When a client received SyncStep2, it should reply with SyncDone. When the local client received both
|
|
510
|
+
* SyncStep2 and SyncDone, it is assured that it is synced to the remote client.
|
|
511
|
+
*
|
|
512
|
+
* In a client-server model, you want to handle this differently: The client should initiate the connection with SyncStep1.
|
|
513
|
+
* When the server receives SyncStep1, it should reply with SyncStep2 immediately followed by SyncStep1. The client replies
|
|
514
|
+
* with SyncStep2 when it receives SyncStep1. Optionally the server may send a SyncDone after it received SyncStep2, so the
|
|
515
|
+
* client knows that the sync is finished. There are two reasons for this more elaborated sync model: 1. This protocol can
|
|
516
|
+
* easily be implemented on top of http and websockets. 2. The server should only reply to requests, and not initiate them.
|
|
517
|
+
* Therefore it is necessary that the client initiates the sync.
|
|
518
|
+
*
|
|
519
|
+
* Construction of a message:
|
|
520
|
+
* [messageType : varUint, message definition..]
|
|
521
|
+
*
|
|
522
|
+
* Note: A message does not include information about the room name. This must to be handled by the upper layer protocol!
|
|
523
|
+
*
|
|
524
|
+
* stringify[messageType] stringifies a message definition (messageType is already read from the bufffer)
|
|
525
|
+
*/
|
|
526
|
+
const messageYjsSyncStep1 = 0;
|
|
527
|
+
const messageYjsSyncStep2 = 1;
|
|
528
|
+
const messageYjsUpdate = 2;
|
|
529
|
+
/**
|
|
530
|
+
* Create a sync step 1 message based on the state of the current shared document.
|
|
531
|
+
*
|
|
532
|
+
* @param {encoding.Encoder} encoder
|
|
533
|
+
* @param {Y.Doc} doc
|
|
534
|
+
*/
|
|
535
|
+
const writeSyncStep1 = (encoder, doc) => {
|
|
536
|
+
writeVarUint(encoder, messageYjsSyncStep1);
|
|
537
|
+
const sv = yjs.encodeStateVector(doc);
|
|
538
|
+
writeVarUint8Array(encoder, sv);
|
|
539
|
+
};
|
|
540
|
+
/**
|
|
541
|
+
* @param {encoding.Encoder} encoder
|
|
542
|
+
* @param {Y.Doc} doc
|
|
543
|
+
* @param {Uint8Array} [encodedStateVector]
|
|
544
|
+
*/
|
|
545
|
+
const writeSyncStep2 = (encoder, doc, encodedStateVector) => {
|
|
546
|
+
writeVarUint(encoder, messageYjsSyncStep2);
|
|
547
|
+
writeVarUint8Array(encoder, yjs.encodeStateAsUpdate(doc, encodedStateVector));
|
|
548
|
+
};
|
|
549
|
+
/**
|
|
550
|
+
* Read SyncStep1 message and reply with SyncStep2.
|
|
551
|
+
*
|
|
552
|
+
* @param {decoding.Decoder} decoder The reply to the received message
|
|
553
|
+
* @param {encoding.Encoder} encoder The received message
|
|
554
|
+
* @param {Y.Doc} doc
|
|
555
|
+
*/
|
|
556
|
+
const readSyncStep1 = (decoder, encoder, doc) => writeSyncStep2(encoder, doc, readVarUint8Array(decoder));
|
|
557
|
+
/**
|
|
558
|
+
* Read and apply Structs and then DeleteStore to a y instance.
|
|
559
|
+
*
|
|
560
|
+
* @param {decoding.Decoder} decoder
|
|
561
|
+
* @param {Y.Doc} doc
|
|
562
|
+
* @param {any} transactionOrigin
|
|
563
|
+
*/
|
|
564
|
+
const readSyncStep2 = (decoder, doc, transactionOrigin) => {
|
|
565
|
+
try {
|
|
566
|
+
yjs.applyUpdate(doc, readVarUint8Array(decoder), transactionOrigin);
|
|
567
|
+
} catch (error) {
|
|
568
|
+
console.error("Caught error while handling a Yjs update", error);
|
|
569
|
+
}
|
|
570
|
+
};
|
|
571
|
+
/**
|
|
572
|
+
* @param {encoding.Encoder} encoder
|
|
573
|
+
* @param {Uint8Array} update
|
|
574
|
+
*/
|
|
575
|
+
const writeUpdate = (encoder, update) => {
|
|
576
|
+
writeVarUint(encoder, messageYjsUpdate);
|
|
577
|
+
writeVarUint8Array(encoder, update);
|
|
578
|
+
};
|
|
579
|
+
/**
|
|
580
|
+
* Read and apply Structs and then DeleteStore to a y instance.
|
|
581
|
+
*
|
|
582
|
+
* @param {decoding.Decoder} decoder
|
|
583
|
+
* @param {Y.Doc} doc
|
|
584
|
+
* @param {any} transactionOrigin
|
|
585
|
+
*/
|
|
586
|
+
const readUpdate = readSyncStep2;
|
|
587
|
+
/**
|
|
588
|
+
* @param {decoding.Decoder} decoder A message received from another client
|
|
589
|
+
* @param {encoding.Encoder} encoder The reply message. Does not need to be sent if empty.
|
|
590
|
+
* @param {Y.Doc} doc
|
|
591
|
+
* @param {any} transactionOrigin
|
|
592
|
+
*/
|
|
593
|
+
const readSyncMessage = (decoder, encoder, doc, transactionOrigin) => {
|
|
594
|
+
const messageType = readVarUint(decoder);
|
|
595
|
+
switch (messageType) {
|
|
596
|
+
case messageYjsSyncStep1:
|
|
597
|
+
readSyncStep1(decoder, encoder, doc);
|
|
598
|
+
break;
|
|
599
|
+
case messageYjsSyncStep2:
|
|
600
|
+
readSyncStep2(decoder, doc, transactionOrigin);
|
|
601
|
+
break;
|
|
602
|
+
case messageYjsUpdate:
|
|
603
|
+
readUpdate(decoder, doc, transactionOrigin);
|
|
604
|
+
break;
|
|
605
|
+
default: throw new Error("Unknown message type");
|
|
606
|
+
}
|
|
607
|
+
return messageType;
|
|
608
|
+
};
|
|
10
609
|
|
|
610
|
+
//#endregion
|
|
611
|
+
//#region ../../node_modules/lib0/time.js
|
|
612
|
+
/**
|
|
613
|
+
* Return current unix time.
|
|
614
|
+
*
|
|
615
|
+
* @return {number}
|
|
616
|
+
*/
|
|
617
|
+
const getUnixTime = Date.now;
|
|
618
|
+
|
|
619
|
+
//#endregion
|
|
620
|
+
//#region ../../node_modules/lib0/map.js
|
|
621
|
+
/**
|
|
622
|
+
* Utility module to work with key-value stores.
|
|
623
|
+
*
|
|
624
|
+
* @module map
|
|
625
|
+
*/
|
|
626
|
+
/**
|
|
627
|
+
* Creates a new Map instance.
|
|
628
|
+
*
|
|
629
|
+
* @function
|
|
630
|
+
* @return {Map<any, any>}
|
|
631
|
+
*
|
|
632
|
+
* @function
|
|
633
|
+
*/
|
|
634
|
+
const create = () => new Map();
|
|
635
|
+
/**
|
|
636
|
+
* Get map property. Create T if property is undefined and set T on map.
|
|
637
|
+
*
|
|
638
|
+
* ```js
|
|
639
|
+
* const listeners = map.setIfUndefined(events, 'eventName', set.create)
|
|
640
|
+
* listeners.add(listener)
|
|
641
|
+
* ```
|
|
642
|
+
*
|
|
643
|
+
* @function
|
|
644
|
+
* @template {Map<any, any>} MAP
|
|
645
|
+
* @template {MAP extends Map<any,infer V> ? function():V : unknown} CF
|
|
646
|
+
* @param {MAP} map
|
|
647
|
+
* @param {MAP extends Map<infer K,any> ? K : unknown} key
|
|
648
|
+
* @param {CF} createT
|
|
649
|
+
* @return {ReturnType<CF>}
|
|
650
|
+
*/
|
|
651
|
+
const setIfUndefined = (map, key, createT) => {
|
|
652
|
+
let set = map.get(key);
|
|
653
|
+
if (set === void 0) map.set(key, set = createT());
|
|
654
|
+
return set;
|
|
655
|
+
};
|
|
656
|
+
|
|
657
|
+
//#endregion
|
|
658
|
+
//#region ../../node_modules/lib0/observable.js
|
|
659
|
+
/* c8 ignore start */
|
|
660
|
+
/**
|
|
661
|
+
* Handles named events.
|
|
662
|
+
*
|
|
663
|
+
* @deprecated
|
|
664
|
+
* @template N
|
|
665
|
+
*/
|
|
666
|
+
var Observable = class {
|
|
667
|
+
constructor() {
|
|
668
|
+
/**
|
|
669
|
+
* Some desc.
|
|
670
|
+
* @type {Map<N, any>}
|
|
671
|
+
*/
|
|
672
|
+
this._observers = create();
|
|
673
|
+
}
|
|
674
|
+
/**
|
|
675
|
+
* @param {N} name
|
|
676
|
+
* @param {function} f
|
|
677
|
+
*/
|
|
678
|
+
on(name, f) {
|
|
679
|
+
setIfUndefined(this._observers, name, create$2).add(f);
|
|
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
|
+
* @param {N} name
|
|
697
|
+
* @param {function} f
|
|
698
|
+
*/
|
|
699
|
+
off(name, f) {
|
|
700
|
+
const observers = this._observers.get(name);
|
|
701
|
+
if (observers !== void 0) {
|
|
702
|
+
observers.delete(f);
|
|
703
|
+
if (observers.size === 0) this._observers.delete(name);
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
/**
|
|
707
|
+
* Emit a named event. All registered event listeners that listen to the
|
|
708
|
+
* specified name will receive the event.
|
|
709
|
+
*
|
|
710
|
+
* @todo This should catch exceptions
|
|
711
|
+
*
|
|
712
|
+
* @param {N} name The event name.
|
|
713
|
+
* @param {Array<any>} args The arguments that are applied to the event listener.
|
|
714
|
+
*/
|
|
715
|
+
emit(name, args) {
|
|
716
|
+
return from((this._observers.get(name) || create()).values()).forEach((f) => f(...args));
|
|
717
|
+
}
|
|
718
|
+
destroy() {
|
|
719
|
+
this._observers = create();
|
|
720
|
+
}
|
|
721
|
+
};
|
|
722
|
+
/* c8 ignore end */
|
|
723
|
+
|
|
724
|
+
//#endregion
|
|
725
|
+
//#region ../../node_modules/lib0/object.js
|
|
726
|
+
/**
|
|
727
|
+
* @param {Object<string,any>} obj
|
|
728
|
+
*/
|
|
729
|
+
const keys = Object.keys;
|
|
730
|
+
/**
|
|
731
|
+
* @deprecated use object.size instead
|
|
732
|
+
* @param {Object<string,any>} obj
|
|
733
|
+
* @return {number}
|
|
734
|
+
*/
|
|
735
|
+
const length = (obj) => keys(obj).length;
|
|
736
|
+
/**
|
|
737
|
+
* Calls `Object.prototype.hasOwnProperty`.
|
|
738
|
+
*
|
|
739
|
+
* @param {any} obj
|
|
740
|
+
* @param {string|symbol} key
|
|
741
|
+
* @return {boolean}
|
|
742
|
+
*/
|
|
743
|
+
const hasProperty = (obj, key) => Object.prototype.hasOwnProperty.call(obj, key);
|
|
744
|
+
|
|
745
|
+
//#endregion
|
|
746
|
+
//#region ../../node_modules/lib0/traits.js
|
|
747
|
+
const EqualityTraitSymbol = Symbol("Equality");
|
|
748
|
+
/**
|
|
749
|
+
* @typedef {{ [EqualityTraitSymbol]:(other:EqualityTrait)=>boolean }} EqualityTrait
|
|
750
|
+
*/
|
|
751
|
+
|
|
752
|
+
//#endregion
|
|
753
|
+
//#region ../../node_modules/lib0/function.js
|
|
754
|
+
/* c8 ignore start */
|
|
755
|
+
/**
|
|
756
|
+
* @param {any} a
|
|
757
|
+
* @param {any} b
|
|
758
|
+
* @return {boolean}
|
|
759
|
+
*/
|
|
760
|
+
const equalityDeep = (a, b) => {
|
|
761
|
+
if (a === b) return true;
|
|
762
|
+
if (a == null || b == null || a.constructor !== b.constructor) return false;
|
|
763
|
+
if (a[EqualityTraitSymbol] != null) return a[EqualityTraitSymbol](b);
|
|
764
|
+
switch (a.constructor) {
|
|
765
|
+
case ArrayBuffer:
|
|
766
|
+
a = new Uint8Array(a);
|
|
767
|
+
b = new Uint8Array(b);
|
|
768
|
+
case Uint8Array: {
|
|
769
|
+
if (a.byteLength !== b.byteLength) return false;
|
|
770
|
+
for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
|
|
771
|
+
break;
|
|
772
|
+
}
|
|
773
|
+
case Set: {
|
|
774
|
+
if (a.size !== b.size) return false;
|
|
775
|
+
for (const value of a) if (!b.has(value)) return false;
|
|
776
|
+
break;
|
|
777
|
+
}
|
|
778
|
+
case Map: {
|
|
779
|
+
if (a.size !== b.size) return false;
|
|
780
|
+
for (const key of a.keys()) if (!b.has(key) || !equalityDeep(a.get(key), b.get(key))) return false;
|
|
781
|
+
break;
|
|
782
|
+
}
|
|
783
|
+
case Object:
|
|
784
|
+
if (length(a) !== length(b)) return false;
|
|
785
|
+
for (const key in a) if (!hasProperty(a, key) || !equalityDeep(a[key], b[key])) return false;
|
|
786
|
+
break;
|
|
787
|
+
case Array:
|
|
788
|
+
if (a.length !== b.length) return false;
|
|
789
|
+
for (let i = 0; i < a.length; i++) if (!equalityDeep(a[i], b[i])) return false;
|
|
790
|
+
break;
|
|
791
|
+
default: return false;
|
|
792
|
+
}
|
|
793
|
+
return true;
|
|
794
|
+
};
|
|
795
|
+
/* c8 ignore stop */
|
|
796
|
+
const isArray = isArray$1;
|
|
797
|
+
|
|
798
|
+
//#endregion
|
|
799
|
+
//#region ../../node_modules/y-protocols/awareness.js
|
|
800
|
+
const outdatedTimeout = 3e4;
|
|
801
|
+
/**
|
|
802
|
+
* @typedef {Object} MetaClientState
|
|
803
|
+
* @property {number} MetaClientState.clock
|
|
804
|
+
* @property {number} MetaClientState.lastUpdated unix timestamp
|
|
805
|
+
*/
|
|
806
|
+
/**
|
|
807
|
+
* The Awareness class implements a simple shared state protocol that can be used for non-persistent data like awareness information
|
|
808
|
+
* (cursor, username, status, ..). Each client can update its own local state and listen to state changes of
|
|
809
|
+
* remote clients. Every client may set a state of a remote peer to `null` to mark the client as offline.
|
|
810
|
+
*
|
|
811
|
+
* Each client is identified by a unique client id (something we borrow from `doc.clientID`). A client can override
|
|
812
|
+
* its own state by propagating a message with an increasing timestamp (`clock`). If such a message is received, it is
|
|
813
|
+
* applied if the known state of that client is older than the new state (`clock < newClock`). If a client thinks that
|
|
814
|
+
* a remote client is offline, it may propagate a message with
|
|
815
|
+
* `{ clock: currentClientClock, state: null, client: remoteClient }`. If such a
|
|
816
|
+
* message is received, and the known clock of that client equals the received clock, it will override the state with `null`.
|
|
817
|
+
*
|
|
818
|
+
* Before a client disconnects, it should propagate a `null` state with an updated clock.
|
|
819
|
+
*
|
|
820
|
+
* Awareness states must be updated every 30 seconds. Otherwise the Awareness instance will delete the client state.
|
|
821
|
+
*
|
|
822
|
+
* @extends {Observable<string>}
|
|
823
|
+
*/
|
|
824
|
+
var Awareness = class extends Observable {
|
|
825
|
+
/**
|
|
826
|
+
* @param {Y.Doc} doc
|
|
827
|
+
*/
|
|
828
|
+
constructor(doc) {
|
|
829
|
+
super();
|
|
830
|
+
this.doc = doc;
|
|
831
|
+
/**
|
|
832
|
+
* @type {number}
|
|
833
|
+
*/
|
|
834
|
+
this.clientID = doc.clientID;
|
|
835
|
+
/**
|
|
836
|
+
* Maps from client id to client state
|
|
837
|
+
* @type {Map<number, Object<string, any>>}
|
|
838
|
+
*/
|
|
839
|
+
this.states = new Map();
|
|
840
|
+
/**
|
|
841
|
+
* @type {Map<number, MetaClientState>}
|
|
842
|
+
*/
|
|
843
|
+
this.meta = new Map();
|
|
844
|
+
this._checkInterval = setInterval(() => {
|
|
845
|
+
const now = getUnixTime();
|
|
846
|
+
if (this.getLocalState() !== null && outdatedTimeout / 2 <= now - this.meta.get(this.clientID).lastUpdated) this.setLocalState(this.getLocalState());
|
|
847
|
+
/**
|
|
848
|
+
* @type {Array<number>}
|
|
849
|
+
*/
|
|
850
|
+
const remove = [];
|
|
851
|
+
this.meta.forEach((meta, clientid) => {
|
|
852
|
+
if (clientid !== this.clientID && outdatedTimeout <= now - meta.lastUpdated && this.states.has(clientid)) remove.push(clientid);
|
|
853
|
+
});
|
|
854
|
+
if (remove.length > 0) removeAwarenessStates(this, remove, "timeout");
|
|
855
|
+
}, floor(outdatedTimeout / 10));
|
|
856
|
+
doc.on("destroy", () => {
|
|
857
|
+
this.destroy();
|
|
858
|
+
});
|
|
859
|
+
this.setLocalState({});
|
|
860
|
+
}
|
|
861
|
+
destroy() {
|
|
862
|
+
this.emit("destroy", [this]);
|
|
863
|
+
this.setLocalState(null);
|
|
864
|
+
super.destroy();
|
|
865
|
+
clearInterval(this._checkInterval);
|
|
866
|
+
}
|
|
867
|
+
/**
|
|
868
|
+
* @return {Object<string,any>|null}
|
|
869
|
+
*/
|
|
870
|
+
getLocalState() {
|
|
871
|
+
return this.states.get(this.clientID) || null;
|
|
872
|
+
}
|
|
873
|
+
/**
|
|
874
|
+
* @param {Object<string,any>|null} state
|
|
875
|
+
*/
|
|
876
|
+
setLocalState(state) {
|
|
877
|
+
const clientID = this.clientID;
|
|
878
|
+
const currLocalMeta = this.meta.get(clientID);
|
|
879
|
+
const clock = currLocalMeta === void 0 ? 0 : currLocalMeta.clock + 1;
|
|
880
|
+
const prevState = this.states.get(clientID);
|
|
881
|
+
if (state === null) this.states.delete(clientID);
|
|
882
|
+
else this.states.set(clientID, state);
|
|
883
|
+
this.meta.set(clientID, {
|
|
884
|
+
clock,
|
|
885
|
+
lastUpdated: getUnixTime()
|
|
886
|
+
});
|
|
887
|
+
const added = [];
|
|
888
|
+
const updated = [];
|
|
889
|
+
const filteredUpdated = [];
|
|
890
|
+
const removed = [];
|
|
891
|
+
if (state === null) removed.push(clientID);
|
|
892
|
+
else if (prevState == null) {
|
|
893
|
+
if (state != null) added.push(clientID);
|
|
894
|
+
} else {
|
|
895
|
+
updated.push(clientID);
|
|
896
|
+
if (!equalityDeep(prevState, state)) filteredUpdated.push(clientID);
|
|
897
|
+
}
|
|
898
|
+
if (added.length > 0 || filteredUpdated.length > 0 || removed.length > 0) this.emit("change", [{
|
|
899
|
+
added,
|
|
900
|
+
updated: filteredUpdated,
|
|
901
|
+
removed
|
|
902
|
+
}, "local"]);
|
|
903
|
+
this.emit("update", [{
|
|
904
|
+
added,
|
|
905
|
+
updated,
|
|
906
|
+
removed
|
|
907
|
+
}, "local"]);
|
|
908
|
+
}
|
|
909
|
+
/**
|
|
910
|
+
* @param {string} field
|
|
911
|
+
* @param {any} value
|
|
912
|
+
*/
|
|
913
|
+
setLocalStateField(field, value) {
|
|
914
|
+
const state = this.getLocalState();
|
|
915
|
+
if (state !== null) this.setLocalState({
|
|
916
|
+
...state,
|
|
917
|
+
[field]: value
|
|
918
|
+
});
|
|
919
|
+
}
|
|
920
|
+
/**
|
|
921
|
+
* @return {Map<number,Object<string,any>>}
|
|
922
|
+
*/
|
|
923
|
+
getStates() {
|
|
924
|
+
return this.states;
|
|
925
|
+
}
|
|
926
|
+
};
|
|
927
|
+
/**
|
|
928
|
+
* Mark (remote) clients as inactive and remove them from the list of active peers.
|
|
929
|
+
* This change will be propagated to remote clients.
|
|
930
|
+
*
|
|
931
|
+
* @param {Awareness} awareness
|
|
932
|
+
* @param {Array<number>} clients
|
|
933
|
+
* @param {any} origin
|
|
934
|
+
*/
|
|
935
|
+
const removeAwarenessStates = (awareness, clients, origin) => {
|
|
936
|
+
const removed = [];
|
|
937
|
+
for (let i = 0; i < clients.length; i++) {
|
|
938
|
+
const clientID = clients[i];
|
|
939
|
+
if (awareness.states.has(clientID)) {
|
|
940
|
+
awareness.states.delete(clientID);
|
|
941
|
+
if (clientID === awareness.clientID) {
|
|
942
|
+
const curMeta = awareness.meta.get(clientID);
|
|
943
|
+
awareness.meta.set(clientID, {
|
|
944
|
+
clock: curMeta.clock + 1,
|
|
945
|
+
lastUpdated: getUnixTime()
|
|
946
|
+
});
|
|
947
|
+
}
|
|
948
|
+
removed.push(clientID);
|
|
949
|
+
}
|
|
950
|
+
}
|
|
951
|
+
if (removed.length > 0) {
|
|
952
|
+
awareness.emit("change", [{
|
|
953
|
+
added: [],
|
|
954
|
+
updated: [],
|
|
955
|
+
removed
|
|
956
|
+
}, origin]);
|
|
957
|
+
awareness.emit("update", [{
|
|
958
|
+
added: [],
|
|
959
|
+
updated: [],
|
|
960
|
+
removed
|
|
961
|
+
}, origin]);
|
|
962
|
+
}
|
|
963
|
+
};
|
|
964
|
+
/**
|
|
965
|
+
* @param {Awareness} awareness
|
|
966
|
+
* @param {Array<number>} clients
|
|
967
|
+
* @return {Uint8Array}
|
|
968
|
+
*/
|
|
969
|
+
const encodeAwarenessUpdate = (awareness, clients, states = awareness.states) => {
|
|
970
|
+
const len = clients.length;
|
|
971
|
+
const encoder = createEncoder();
|
|
972
|
+
writeVarUint(encoder, len);
|
|
973
|
+
for (let i = 0; i < len; i++) {
|
|
974
|
+
const clientID = clients[i];
|
|
975
|
+
const state = states.get(clientID) || null;
|
|
976
|
+
const clock = awareness.meta.get(clientID).clock;
|
|
977
|
+
writeVarUint(encoder, clientID);
|
|
978
|
+
writeVarUint(encoder, clock);
|
|
979
|
+
writeVarString(encoder, JSON.stringify(state));
|
|
980
|
+
}
|
|
981
|
+
return toUint8Array(encoder);
|
|
982
|
+
};
|
|
983
|
+
/**
|
|
984
|
+
* @param {Awareness} awareness
|
|
985
|
+
* @param {Uint8Array} update
|
|
986
|
+
* @param {any} origin This will be added to the emitted change event
|
|
987
|
+
*/
|
|
988
|
+
const applyAwarenessUpdate = (awareness, update, origin) => {
|
|
989
|
+
const decoder = createDecoder(update);
|
|
990
|
+
const timestamp = getUnixTime();
|
|
991
|
+
const added = [];
|
|
992
|
+
const updated = [];
|
|
993
|
+
const filteredUpdated = [];
|
|
994
|
+
const removed = [];
|
|
995
|
+
const len = readVarUint(decoder);
|
|
996
|
+
for (let i = 0; i < len; i++) {
|
|
997
|
+
const clientID = readVarUint(decoder);
|
|
998
|
+
let clock = readVarUint(decoder);
|
|
999
|
+
const state = JSON.parse(readVarString(decoder));
|
|
1000
|
+
const clientMeta = awareness.meta.get(clientID);
|
|
1001
|
+
const prevState = awareness.states.get(clientID);
|
|
1002
|
+
const currClock = clientMeta === void 0 ? 0 : clientMeta.clock;
|
|
1003
|
+
if (currClock < clock || currClock === clock && state === null && awareness.states.has(clientID)) {
|
|
1004
|
+
if (state === null) if (clientID === awareness.clientID && awareness.getLocalState() != null) clock++;
|
|
1005
|
+
else awareness.states.delete(clientID);
|
|
1006
|
+
else awareness.states.set(clientID, state);
|
|
1007
|
+
awareness.meta.set(clientID, {
|
|
1008
|
+
clock,
|
|
1009
|
+
lastUpdated: timestamp
|
|
1010
|
+
});
|
|
1011
|
+
if (clientMeta === void 0 && state !== null) added.push(clientID);
|
|
1012
|
+
else if (clientMeta !== void 0 && state === null) removed.push(clientID);
|
|
1013
|
+
else if (state !== null) {
|
|
1014
|
+
if (!equalityDeep(state, prevState)) filteredUpdated.push(clientID);
|
|
1015
|
+
updated.push(clientID);
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
1019
|
+
if (added.length > 0 || filteredUpdated.length > 0 || removed.length > 0) awareness.emit("change", [{
|
|
1020
|
+
added,
|
|
1021
|
+
updated: filteredUpdated,
|
|
1022
|
+
removed
|
|
1023
|
+
}, origin]);
|
|
1024
|
+
if (added.length > 0 || updated.length > 0 || removed.length > 0) awareness.emit("update", [{
|
|
1025
|
+
added,
|
|
1026
|
+
updated,
|
|
1027
|
+
removed
|
|
1028
|
+
}, origin]);
|
|
1029
|
+
};
|
|
1030
|
+
|
|
1031
|
+
//#endregion
|
|
1032
|
+
//#region src/constants.ts
|
|
1033
|
+
const WEAVE_STORE_AZURE_WEB_PUBSUB = "store-azure-web-pubsub";
|
|
1034
|
+
const WEAVE_STORE_AZURE_WEB_PUBSUB_CONNECTION_STATUS = {
|
|
1035
|
+
["CONNECTED"]: "connected",
|
|
1036
|
+
["DISCONNECTED"]: "disconnected"
|
|
1037
|
+
};
|
|
1038
|
+
|
|
1039
|
+
//#endregion
|
|
11
1040
|
//#region src/client.ts
|
|
12
1041
|
const messageSyncStep1 = 0;
|
|
13
1042
|
const messageAwareness = 1;
|
|
@@ -27,21 +1056,21 @@ let MessageDataType = /* @__PURE__ */ function(MessageDataType$1) {
|
|
|
27
1056
|
}({});
|
|
28
1057
|
const messageHandlers = [];
|
|
29
1058
|
messageHandlers[messageSyncStep1] = (encoder, decoder, client, emitSynced, messageType) => {
|
|
30
|
-
|
|
31
|
-
const syncMessageType =
|
|
32
|
-
if (emitSynced && syncMessageType ===
|
|
1059
|
+
writeVarUint(encoder, messageType);
|
|
1060
|
+
const syncMessageType = readSyncMessage(decoder, encoder, client.doc, client);
|
|
1061
|
+
if (emitSynced && syncMessageType === messageYjsSyncStep2 && !client.synced) client.synced = true;
|
|
33
1062
|
};
|
|
34
1063
|
messageHandlers[messageQueryAwareness] = (encoder, _, client) => {
|
|
35
|
-
|
|
36
|
-
|
|
1064
|
+
writeVarUint(encoder, messageAwareness);
|
|
1065
|
+
writeVarUint8Array(encoder, encodeAwarenessUpdate(client.awareness, Array.from(client.awareness.getStates().keys())));
|
|
37
1066
|
};
|
|
38
1067
|
messageHandlers[messageAwareness] = (_, decoder, client) => {
|
|
39
|
-
|
|
1068
|
+
applyAwarenessUpdate(client.awareness, readVarUint8Array(decoder), client);
|
|
40
1069
|
};
|
|
41
1070
|
const readMessage = (client, buf, emitSynced) => {
|
|
42
|
-
const decoder =
|
|
43
|
-
const encoder =
|
|
44
|
-
const messageType =
|
|
1071
|
+
const decoder = createDecoder(buf);
|
|
1072
|
+
const encoder = createEncoder();
|
|
1073
|
+
const messageType = readVarUint(decoder);
|
|
45
1074
|
const messageHandler = messageHandlers[messageType];
|
|
46
1075
|
if (messageHandler) messageHandler(encoder, decoder, client, emitSynced, messageType);
|
|
47
1076
|
else throw new Error(`unable to handle message with type: ${messageType}`);
|
|
@@ -69,32 +1098,32 @@ var WeaveStoreAzureWebPubSubSyncClient = class extends emittery.default {
|
|
|
69
1098
|
this._wsConnected = false;
|
|
70
1099
|
this._synced = false;
|
|
71
1100
|
this._ws = null;
|
|
72
|
-
const awareness = new
|
|
1101
|
+
const awareness = new Awareness(doc);
|
|
73
1102
|
this._awareness = awareness;
|
|
74
1103
|
this._resyncInterval = null;
|
|
75
1104
|
if (options.resyncInterval > 0) this._resyncInterval = setInterval(() => {
|
|
76
1105
|
if (this._ws) {
|
|
77
|
-
const encoder =
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
sendToControlGroup(this, topic, MessageDataType.Sync,
|
|
1106
|
+
const encoder = createEncoder();
|
|
1107
|
+
writeVarUint(encoder, messageSyncStep1);
|
|
1108
|
+
writeSyncStep1(encoder, doc);
|
|
1109
|
+
sendToControlGroup(this, topic, MessageDataType.Sync, toUint8Array(encoder));
|
|
81
1110
|
}
|
|
82
1111
|
}, options.resyncInterval);
|
|
83
1112
|
this._updateHandler = (update, origin) => {
|
|
84
1113
|
if (origin !== this) {
|
|
85
|
-
const encoder =
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
sendToControlGroup(this, topic, MessageDataType.Sync,
|
|
1114
|
+
const encoder = createEncoder();
|
|
1115
|
+
writeVarUint(encoder, messageSyncStep1);
|
|
1116
|
+
writeUpdate(encoder, update);
|
|
1117
|
+
sendToControlGroup(this, topic, MessageDataType.Sync, toUint8Array(encoder));
|
|
89
1118
|
}
|
|
90
1119
|
};
|
|
91
1120
|
this.doc.on("update", this._updateHandler);
|
|
92
1121
|
this._awarenessUpdateHandler = ({ added, updated, removed }) => {
|
|
93
1122
|
const changedClients = added.concat(updated).concat(removed);
|
|
94
|
-
const encoder =
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
sendToControlGroup(this, topic, MessageDataType.Awareness,
|
|
1123
|
+
const encoder = createEncoder();
|
|
1124
|
+
writeVarUint(encoder, messageAwareness);
|
|
1125
|
+
writeVarUint8Array(encoder, encodeAwarenessUpdate(awareness, changedClients));
|
|
1126
|
+
sendToControlGroup(this, topic, MessageDataType.Awareness, toUint8Array(encoder));
|
|
98
1127
|
};
|
|
99
1128
|
awareness.on("update", this._awarenessUpdateHandler);
|
|
100
1129
|
}
|
|
@@ -120,10 +1149,10 @@ var WeaveStoreAzureWebPubSubSyncClient = class extends emittery.default {
|
|
|
120
1149
|
}
|
|
121
1150
|
stop() {
|
|
122
1151
|
if (this._ws !== null) {
|
|
123
|
-
const encoder =
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
const u8 =
|
|
1152
|
+
const encoder = createEncoder();
|
|
1153
|
+
writeVarUint(encoder, messageAwareness);
|
|
1154
|
+
writeVarUint8Array(encoder, encodeAwarenessUpdate(this.awareness, [this.doc.clientID], new Map()));
|
|
1155
|
+
const u8 = toUint8Array(encoder);
|
|
127
1156
|
sendToControlGroup(this, this.topic, MessageDataType.Awareness, u8);
|
|
128
1157
|
this._ws.close();
|
|
129
1158
|
}
|
|
@@ -157,7 +1186,7 @@ var WeaveStoreAzureWebPubSubSyncClient = class extends emittery.default {
|
|
|
157
1186
|
if (messageData.t !== void 0 && messageData.t !== this._uuid) return;
|
|
158
1187
|
const buf = buffer.Buffer.from(messageData.c, "base64");
|
|
159
1188
|
const encoder = readMessage(this, buf, true);
|
|
160
|
-
if (
|
|
1189
|
+
if (length$1(encoder) > 1) sendToControlGroup(this, this.topic, MessageDataType.Sync, toUint8Array(encoder));
|
|
161
1190
|
};
|
|
162
1191
|
websocket.onclose = () => {
|
|
163
1192
|
this._ws = null;
|
|
@@ -166,7 +1195,7 @@ var WeaveStoreAzureWebPubSubSyncClient = class extends emittery.default {
|
|
|
166
1195
|
this.emit("status", this._status);
|
|
167
1196
|
this._wsConnected = false;
|
|
168
1197
|
this.synced = false;
|
|
169
|
-
|
|
1198
|
+
removeAwarenessStates(this.awareness, Array.from(this.awareness.getStates().keys()).filter((x) => x !== this.doc.clientID), this);
|
|
170
1199
|
}
|
|
171
1200
|
};
|
|
172
1201
|
websocket.onerror = (err) => {
|
|
@@ -177,16 +1206,16 @@ var WeaveStoreAzureWebPubSubSyncClient = class extends emittery.default {
|
|
|
177
1206
|
this._status = "connected";
|
|
178
1207
|
this.emit("status", this._status);
|
|
179
1208
|
joinGroup(this, this.topic);
|
|
180
|
-
const encoder =
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
const u8 =
|
|
1209
|
+
const encoder = createEncoder();
|
|
1210
|
+
writeVarUint(encoder, messageSyncStep1);
|
|
1211
|
+
writeSyncStep1(encoder, this.doc);
|
|
1212
|
+
const u8 = toUint8Array(encoder);
|
|
184
1213
|
sendToControlGroup(this, this.topic, MessageDataType.Init, u8);
|
|
185
1214
|
if (this.awareness.getLocalState() !== null) {
|
|
186
|
-
const encoderAwarenessState =
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
const u8$1 =
|
|
1215
|
+
const encoderAwarenessState = createEncoder();
|
|
1216
|
+
writeVarUint(encoderAwarenessState, messageAwareness);
|
|
1217
|
+
writeVarUint8Array(encoderAwarenessState, encodeAwarenessUpdate(this.awareness, [this.doc.clientID]));
|
|
1218
|
+
const u8$1 = toUint8Array(encoder);
|
|
190
1219
|
sendToControlGroup(this, this.topic, MessageDataType.Awareness, u8$1);
|
|
191
1220
|
}
|
|
192
1221
|
};
|
|
@@ -213,7 +1242,7 @@ function sendToControlGroup(client, group, type, u8) {
|
|
|
213
1242
|
//#endregion
|
|
214
1243
|
//#region src/store-azure-web-pubsub.ts
|
|
215
1244
|
var WeaveStoreAzureWebPubsub = class extends __inditextech_weave_sdk.WeaveStore {
|
|
216
|
-
name =
|
|
1245
|
+
name = WEAVE_STORE_AZURE_WEB_PUBSUB;
|
|
217
1246
|
supportsUndoManager = true;
|
|
218
1247
|
constructor(storeOptions, azureWebPubsubOptions) {
|
|
219
1248
|
super(storeOptions);
|
|
@@ -278,6 +1307,6 @@ var WeaveStoreAzureWebPubsub = class extends __inditextech_weave_sdk.WeaveStore
|
|
|
278
1307
|
};
|
|
279
1308
|
|
|
280
1309
|
//#endregion
|
|
281
|
-
exports.WEAVE_STORE_AZURE_WEB_PUBSUB =
|
|
282
|
-
exports.WEAVE_STORE_AZURE_WEB_PUBSUB_CONNECTION_STATUS =
|
|
1310
|
+
exports.WEAVE_STORE_AZURE_WEB_PUBSUB = WEAVE_STORE_AZURE_WEB_PUBSUB
|
|
1311
|
+
exports.WEAVE_STORE_AZURE_WEB_PUBSUB_CONNECTION_STATUS = WEAVE_STORE_AZURE_WEB_PUBSUB_CONNECTION_STATUS
|
|
283
1312
|
exports.WeaveStoreAzureWebPubsub = WeaveStoreAzureWebPubsub
|