@hocuspocus/provider 2.0.1 → 2.0.2

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.
@@ -40,7 +40,7 @@ var Y__namespace = /*#__PURE__*/_interopNamespace(Y);
40
40
  *
41
41
  * @function
42
42
  */
43
- const create$1 = () => new Map();
43
+ const create$2 = () => new Map();
44
44
 
45
45
  /**
46
46
  * Get map property. Create T if property is undefined and set T on map.
@@ -51,11 +51,12 @@ const create$1 = () => new Map();
51
51
  * ```
52
52
  *
53
53
  * @function
54
- * @template T,K
55
- * @param {Map<K, T>} map
54
+ * @template V,K
55
+ * @template {Map<K,V>} MAP
56
+ * @param {MAP} map
56
57
  * @param {K} key
57
- * @param {function():T} createT
58
- * @return {T}
58
+ * @param {function():V} createT
59
+ * @return {V}
59
60
  */
60
61
  const setIfUndefined = (map, key, createT) => {
61
62
  let set = map.get(key);
@@ -65,6 +66,14 @@ const setIfUndefined = (map, key, createT) => {
65
66
  return set
66
67
  };
67
68
 
69
+ /**
70
+ * Utility module to work with sets.
71
+ *
72
+ * @module set
73
+ */
74
+
75
+ const create$1 = () => new Set();
76
+
68
77
  /**
69
78
  * Utility module to work with strings.
70
79
  *
@@ -96,20 +105,47 @@ const fromCamelCaseRegex = /([A-Z])/g;
96
105
  */
97
106
  const fromCamelCase = (s, separator) => trimLeft(s.replace(fromCamelCaseRegex, match => `${separator}${toLowerCase(match)}`));
98
107
 
99
- /* istanbul ignore next */
100
- /** @type {TextEncoder} */ (typeof TextEncoder !== 'undefined' ? new TextEncoder() : null);
108
+ /**
109
+ * @param {string} str
110
+ * @return {Uint8Array}
111
+ */
112
+ const _encodeUtf8Polyfill = str => {
113
+ const encodedString = unescape(encodeURIComponent(str));
114
+ const len = encodedString.length;
115
+ const buf = new Uint8Array(len);
116
+ for (let i = 0; i < len; i++) {
117
+ buf[i] = /** @type {number} */ (encodedString.codePointAt(i));
118
+ }
119
+ return buf
120
+ };
121
+
122
+ /* c8 ignore next */
123
+ const utf8TextEncoder = /** @type {TextEncoder} */ (typeof TextEncoder !== 'undefined' ? new TextEncoder() : null);
101
124
 
102
- /* istanbul ignore next */
125
+ /**
126
+ * @param {string} str
127
+ * @return {Uint8Array}
128
+ */
129
+ const _encodeUtf8Native = str => utf8TextEncoder.encode(str);
130
+
131
+ /**
132
+ * @param {string} str
133
+ * @return {Uint8Array}
134
+ */
135
+ /* c8 ignore next */
136
+ const encodeUtf8 = utf8TextEncoder ? _encodeUtf8Native : _encodeUtf8Polyfill;
137
+
138
+ /* c8 ignore next */
103
139
  let utf8TextDecoder = typeof TextDecoder === 'undefined' ? null : new TextDecoder('utf-8', { fatal: true, ignoreBOM: true });
104
140
 
105
- /* istanbul ignore next */
141
+ /* c8 ignore start */
106
142
  if (utf8TextDecoder && utf8TextDecoder.decode(new Uint8Array()).length === 1) {
107
143
  // Safari doesn't handle BOM correctly.
108
144
  // This fixes a bug in Safari 13.0.5 where it produces a BOM the first time it is called.
109
145
  // utf8TextDecoder.decode(new Uint8Array()).length === 1 on the first call and
110
146
  // utf8TextDecoder.decode(new Uint8Array()).length === 1 on the second call
111
147
  // Another issue is that from then on no BOM chars are recognized anymore
112
- /* istanbul ignore next */
148
+ /* c8 ignore next */
113
149
  utf8TextDecoder = null;
114
150
  }
115
151
 
@@ -124,10 +160,10 @@ if (utf8TextDecoder && utf8TextDecoder.decode(new Uint8Array()).length === 1) {
124
160
  * @param {T|null|undefined} v
125
161
  * @return {T|null}
126
162
  */
127
- /* istanbul ignore next */
163
+ /* c8 ignore next */
128
164
  const undefinedToNull = v => v === undefined ? null : v;
129
165
 
130
- /* global localStorage, addEventListener */
166
+ /* eslint-env browser */
131
167
 
132
168
  /**
133
169
  * Isomorphic variable storage.
@@ -137,7 +173,7 @@ const undefinedToNull = v => v === undefined ? null : v;
137
173
  * @module storage
138
174
  */
139
175
 
140
- /* istanbul ignore next */
176
+ /* c8 ignore start */
141
177
  class VarStoragePolyfill {
142
178
  constructor () {
143
179
  this.map = new Map();
@@ -158,65 +194,242 @@ class VarStoragePolyfill {
158
194
  return this.map.get(key)
159
195
  }
160
196
  }
197
+ /* c8 ignore stop */
161
198
 
162
- /* istanbul ignore next */
163
199
  /**
164
200
  * @type {any}
165
201
  */
166
202
  let _localStorage = new VarStoragePolyfill();
167
203
  let usePolyfill = true;
168
204
 
205
+ /* c8 ignore start */
169
206
  try {
170
207
  // if the same-origin rule is violated, accessing localStorage might thrown an error
171
- /* istanbul ignore next */
172
208
  if (typeof localStorage !== 'undefined') {
173
209
  _localStorage = localStorage;
174
210
  usePolyfill = false;
175
211
  }
176
212
  } catch (e) { }
213
+ /* c8 ignore stop */
177
214
 
178
- /* istanbul ignore next */
179
215
  /**
180
216
  * This is basically localStorage in browser, or a polyfill in nodejs
181
217
  */
218
+ /* c8 ignore next */
182
219
  const varStorage = _localStorage;
183
220
 
184
- /* istanbul ignore next */
185
221
  /**
186
222
  * A polyfill for `addEventListener('storage', event => {..})` that does nothing if the polyfill is being used.
187
223
  *
188
224
  * @param {function({ key: string, newValue: string, oldValue: string }): void} eventHandler
189
225
  * @function
190
226
  */
227
+ /* c8 ignore next */
191
228
  const onChange = eventHandler => usePolyfill || addEventListener('storage', /** @type {any} */ (eventHandler));
192
229
 
230
+ /**
231
+ * A polyfill for `removeEventListener('storage', event => {..})` that does nothing if the polyfill is being used.
232
+ *
233
+ * @param {function({ key: string, newValue: string, oldValue: string }): void} eventHandler
234
+ * @function
235
+ */
236
+ /* c8 ignore next */
237
+ const offChange = eventHandler => usePolyfill || removeEventListener('storage', /** @type {any} */ (eventHandler));
238
+
239
+ /**
240
+ * Utility module to work with Arrays.
241
+ *
242
+ * @module array
243
+ */
244
+
245
+ /**
246
+ * Transforms something array-like to an actual Array.
247
+ *
248
+ * @function
249
+ * @template T
250
+ * @param {ArrayLike<T>|Iterable<T>} arraylike
251
+ * @return {T}
252
+ */
253
+ const from = Array.from;
254
+
255
+ /**
256
+ * Utility functions for working with EcmaScript objects.
257
+ *
258
+ * @module object
259
+ */
260
+
261
+ /**
262
+ * @param {Object<string,any>} obj
263
+ */
264
+ const keys = Object.keys;
265
+
266
+ /**
267
+ * @todo implement mapToArray & map
268
+ *
269
+ * @template R
270
+ * @param {Object<string,any>} obj
271
+ * @param {function(any,string):R} f
272
+ * @return {Array<R>}
273
+ */
274
+ const map = (obj, f) => {
275
+ const results = [];
276
+ for (const key in obj) {
277
+ results.push(f(obj[key], key));
278
+ }
279
+ return results
280
+ };
281
+
282
+ /**
283
+ * @param {Object<string,any>} obj
284
+ * @return {number}
285
+ */
286
+ const length$1 = obj => keys(obj).length;
287
+
288
+ /**
289
+ * Calls `Object.prototype.hasOwnProperty`.
290
+ *
291
+ * @param {any} obj
292
+ * @param {string|symbol} key
293
+ * @return {boolean}
294
+ */
295
+ const hasProperty = (obj, key) => Object.prototype.hasOwnProperty.call(obj, key);
296
+
297
+ /**
298
+ * Common functions and function call helpers.
299
+ *
300
+ * @module function
301
+ */
302
+
303
+ /**
304
+ * @template T
305
+ *
306
+ * @param {T} a
307
+ * @param {T} b
308
+ * @return {boolean}
309
+ */
310
+ const equalityStrict = (a, b) => a === b;
311
+
312
+ /* c8 ignore start */
313
+
314
+ /**
315
+ * @param {any} a
316
+ * @param {any} b
317
+ * @return {boolean}
318
+ */
319
+ const equalityDeep = (a, b) => {
320
+ if (a == null || b == null) {
321
+ return equalityStrict(a, b)
322
+ }
323
+ if (a.constructor !== b.constructor) {
324
+ return false
325
+ }
326
+ if (a === b) {
327
+ return true
328
+ }
329
+ switch (a.constructor) {
330
+ case ArrayBuffer:
331
+ a = new Uint8Array(a);
332
+ b = new Uint8Array(b);
333
+ // eslint-disable-next-line no-fallthrough
334
+ case Uint8Array: {
335
+ if (a.byteLength !== b.byteLength) {
336
+ return false
337
+ }
338
+ for (let i = 0; i < a.length; i++) {
339
+ if (a[i] !== b[i]) {
340
+ return false
341
+ }
342
+ }
343
+ break
344
+ }
345
+ case Set: {
346
+ if (a.size !== b.size) {
347
+ return false
348
+ }
349
+ for (const value of a) {
350
+ if (!b.has(value)) {
351
+ return false
352
+ }
353
+ }
354
+ break
355
+ }
356
+ case Map: {
357
+ if (a.size !== b.size) {
358
+ return false
359
+ }
360
+ for (const key of a.keys()) {
361
+ if (!b.has(key) || !equalityDeep(a.get(key), b.get(key))) {
362
+ return false
363
+ }
364
+ }
365
+ break
366
+ }
367
+ case Object:
368
+ if (length$1(a) !== length$1(b)) {
369
+ return false
370
+ }
371
+ for (const key in a) {
372
+ if (!hasProperty(a, key) || !equalityDeep(a[key], b[key])) {
373
+ return false
374
+ }
375
+ }
376
+ break
377
+ case Array:
378
+ if (a.length !== b.length) {
379
+ return false
380
+ }
381
+ for (let i = 0; i < a.length; i++) {
382
+ if (!equalityDeep(a[i], b[i])) {
383
+ return false
384
+ }
385
+ }
386
+ break
387
+ default:
388
+ return false
389
+ }
390
+ return true
391
+ };
392
+
393
+ /**
394
+ * @template V
395
+ * @template {V} OPTS
396
+ *
397
+ * @param {V} value
398
+ * @param {Array<OPTS>} options
399
+ */
400
+ // @ts-ignore
401
+ const isOneOf = (value, options) => options.includes(value);
402
+ /* c8 ignore stop */
403
+
193
404
  /**
194
405
  * Isomorphic module to work access the environment (query params, env variables).
195
406
  *
196
407
  * @module map
197
408
  */
198
409
 
199
- /* istanbul ignore next */
410
+ /* c8 ignore next */
200
411
  // @ts-ignore
201
- const isNode = typeof process !== 'undefined' && process.release && /node|io\.js/.test(process.release.name);
202
- /* istanbul ignore next */
203
- const isBrowser = typeof window !== 'undefined' && !isNode;
204
- /* istanbul ignore next */
205
- typeof navigator !== 'undefined' ? /Mac/.test(navigator.platform) : false;
412
+ const isNode = typeof process !== 'undefined' && process.release &&
413
+ /node|io\.js/.test(process.release.name);
414
+ /* c8 ignore next */
415
+ const isBrowser = typeof window !== 'undefined' && typeof document !== 'undefined' && !isNode;
416
+ /* c8 ignore next 3 */
417
+ typeof navigator !== 'undefined'
418
+ ? /Mac/.test(navigator.platform)
419
+ : false;
206
420
 
207
421
  /**
208
422
  * @type {Map<string,string>}
209
423
  */
210
424
  let params;
211
425
 
212
- /* istanbul ignore next */
426
+ /* c8 ignore start */
213
427
  const computeParams = () => {
214
428
  if (params === undefined) {
215
429
  if (isNode) {
216
- params = create$1();
430
+ params = create$2();
217
431
  const pargs = process.argv;
218
432
  let currParamName = null;
219
- /* istanbul ignore next */
220
433
  for (let i = 0; i < pargs.length; i++) {
221
434
  const parg = pargs[i];
222
435
  if (parg[0] === '-') {
@@ -234,11 +447,10 @@ const computeParams = () => {
234
447
  if (currParamName !== null) {
235
448
  params.set(currParamName, '');
236
449
  }
237
- // in ReactNative for example this would not be true (unless connected to the Remote Debugger)
450
+ // in ReactNative for example this would not be true (unless connected to the Remote Debugger)
238
451
  } else if (typeof location === 'object') {
239
- params = create$1()
240
- // eslint-disable-next-line no-undef
241
- ;(location.search || '?').slice(1).split('&').forEach(kv => {
452
+ params = create$2(); // eslint-disable-next-line no-undef
453
+ (location.search || '?').slice(1).split('&').forEach((kv) => {
242
454
  if (kv.length !== 0) {
243
455
  const [key, value] = kv.split('=');
244
456
  params.set(`--${fromCamelCase(key, '-')}`, value);
@@ -246,37 +458,54 @@ const computeParams = () => {
246
458
  }
247
459
  });
248
460
  } else {
249
- params = create$1();
461
+ params = create$2();
250
462
  }
251
463
  }
252
464
  return params
253
465
  };
466
+ /* c8 ignore stop */
254
467
 
255
468
  /**
256
469
  * @param {string} name
257
470
  * @return {boolean}
258
471
  */
259
- /* istanbul ignore next */
260
- const hasParam = name => computeParams().has(name);
261
- // export const getArgs = name => computeParams() && args
472
+ /* c8 ignore next */
473
+ const hasParam = (name) => computeParams().has(name);
262
474
 
263
475
  /**
264
476
  * @param {string} name
265
477
  * @return {string|null}
266
478
  */
267
- /* istanbul ignore next */
268
- const getVariable = name => isNode ? undefinedToNull(process.env[name.toUpperCase()]) : undefinedToNull(varStorage.getItem(name));
479
+ /* c8 ignore next 4 */
480
+ const getVariable = (name) =>
481
+ isNode
482
+ ? undefinedToNull(process.env[name.toUpperCase()])
483
+ : undefinedToNull(varStorage.getItem(name));
269
484
 
270
485
  /**
271
486
  * @param {string} name
272
487
  * @return {boolean}
273
488
  */
274
- /* istanbul ignore next */
275
- const hasConf = name => hasParam('--' + name) || getVariable(name) !== null;
489
+ /* c8 ignore next 2 */
490
+ const hasConf = (name) =>
491
+ hasParam('--' + name) || getVariable(name) !== null;
276
492
 
277
- /* istanbul ignore next */
493
+ /* c8 ignore next */
278
494
  hasConf('production');
279
495
 
496
+ /* c8 ignore next 2 */
497
+ const forceColor = isNode &&
498
+ isOneOf(process.env.FORCE_COLOR, ['true', '1', '2']);
499
+
500
+ /* c8 ignore start */
501
+ !hasParam('no-colors') &&
502
+ (!isNode || process.stdout.isTTY || forceColor) && (
503
+ !isNode || hasParam('color') || forceColor ||
504
+ getVariable('COLORTERM') !== null ||
505
+ (getVariable('TERM') || '').includes('color')
506
+ );
507
+ /* c8 ignore stop */
508
+
280
509
  /**
281
510
  * Common Math expressions.
282
511
  *
@@ -305,6 +534,14 @@ const max = (a, b) => a > b ? a : b;
305
534
  const BIT8 = 128;
306
535
  const BITS7 = 127;
307
536
 
537
+ /**
538
+ * Utility helpers for working with numbers.
539
+ *
540
+ * @module number
541
+ */
542
+
543
+ const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER;
544
+
308
545
  /**
309
546
  * Efficient schema-less binary encoding with support for variable length encoding.
310
547
  *
@@ -316,7 +553,7 @@ const BITS7 = 127;
316
553
  *
317
554
  * ```js
318
555
  * // encoding step
319
- * const encoder = new encoding.createEncoder()
556
+ * const encoder = encoding.createEncoder()
320
557
  * encoding.writeVarUint(encoder, 256)
321
558
  * encoding.writeVarString(encoder, 'Hello world!')
322
559
  * const buf = encoding.toUint8Array(encoder)
@@ -324,7 +561,7 @@ const BITS7 = 127;
324
561
  *
325
562
  * ```js
326
563
  * // decoding step
327
- * const decoder = new decoding.createDecoder(buf)
564
+ * const decoder = decoding.createDecoder(buf)
328
565
  * decoding.readVarUint(decoder) // => 256
329
566
  * decoding.readVarString(decoder) // => 'Hello world!'
330
567
  * decoding.hasContent(decoder) // => false - all data is read
@@ -360,7 +597,7 @@ const createEncoder = () => new Encoder();
360
597
  * @param {Encoder} encoder
361
598
  * @return {number}
362
599
  */
363
- const length$1 = encoder => {
600
+ const length = encoder => {
364
601
  let len = encoder.cpos;
365
602
  for (let i = 0; i < encoder.bufs.length; i++) {
366
603
  len += encoder.bufs[i].length;
@@ -376,7 +613,7 @@ const length$1 = encoder => {
376
613
  * @return {Uint8Array} The created ArrayBuffer.
377
614
  */
378
615
  const toUint8Array = encoder => {
379
- const uint8arr = new Uint8Array(length$1(encoder));
616
+ const uint8arr = new Uint8Array(length(encoder));
380
617
  let curPos = 0;
381
618
  for (let i = 0; i < encoder.bufs.length; i++) {
382
619
  const d = encoder.bufs[i];
@@ -405,9 +642,7 @@ const write = (encoder, num) => {
405
642
  };
406
643
 
407
644
  /**
408
- * Write a variable length unsigned integer.
409
- *
410
- * Encodes integers in the range from [0, 4294967295] / [0, 0xffffffff]. (max 32 bit unsigned integer)
645
+ * Write a variable length unsigned integer. Max encodable integer is 2^53.
411
646
  *
412
647
  * @function
413
648
  * @param {Encoder} encoder
@@ -416,11 +651,17 @@ const write = (encoder, num) => {
416
651
  const writeVarUint = (encoder, num) => {
417
652
  while (num > BITS7) {
418
653
  write(encoder, BIT8 | (BITS7 & num));
419
- num >>>= 7;
654
+ num = floor(num / 128); // shift >>> 7
420
655
  }
421
656
  write(encoder, BITS7 & num);
422
657
  };
423
658
 
659
+ /**
660
+ * A cache to store strings temporarily
661
+ */
662
+ const _strBuffer = new Uint8Array(30000);
663
+ const _maxStrBSize = _strBuffer.length / 3;
664
+
424
665
  /**
425
666
  * Write a variable length string.
426
667
  *
@@ -428,7 +669,28 @@ const writeVarUint = (encoder, num) => {
428
669
  * @param {Encoder} encoder
429
670
  * @param {String} str The string that is to be encoded.
430
671
  */
431
- const writeVarString = (encoder, str) => {
672
+ const _writeVarStringNative = (encoder, str) => {
673
+ if (str.length < _maxStrBSize) {
674
+ // We can encode the string into the existing buffer
675
+ /* c8 ignore next */
676
+ const written = utf8TextEncoder.encodeInto(str, _strBuffer).written || 0;
677
+ writeVarUint(encoder, written);
678
+ for (let i = 0; i < written; i++) {
679
+ write(encoder, _strBuffer[i]);
680
+ }
681
+ } else {
682
+ writeVarUint8Array(encoder, encodeUtf8(str));
683
+ }
684
+ };
685
+
686
+ /**
687
+ * Write a variable length string.
688
+ *
689
+ * @function
690
+ * @param {Encoder} encoder
691
+ * @param {String} str The string that is to be encoded.
692
+ */
693
+ const _writeVarStringPolyfill = (encoder, str) => {
432
694
  const encodedString = unescape(encodeURIComponent(str));
433
695
  const len = encodedString.length;
434
696
  writeVarUint(encoder, len);
@@ -437,6 +699,16 @@ const writeVarString = (encoder, str) => {
437
699
  }
438
700
  };
439
701
 
702
+ /**
703
+ * Write a variable length string.
704
+ *
705
+ * @function
706
+ * @param {Encoder} encoder
707
+ * @param {String} str The string that is to be encoded.
708
+ */
709
+ /* c8 ignore next */
710
+ const writeVarString = (utf8TextEncoder && /** @type {any} */ (utf8TextEncoder).encodeInto) ? _writeVarStringNative : _writeVarStringPolyfill;
711
+
440
712
  /**
441
713
  * Append fixed-length Uint8Array to the encoder.
442
714
  *
@@ -475,6 +747,19 @@ const writeVarUint8Array = (encoder, uint8Array) => {
475
747
  writeUint8Array(encoder, uint8Array);
476
748
  };
477
749
 
750
+ /**
751
+ * Error helpers.
752
+ *
753
+ * @module error
754
+ */
755
+
756
+ /**
757
+ * @param {string} s
758
+ * @return {Error}
759
+ */
760
+ /* c8 ignore next */
761
+ const create = s => new Error(s);
762
+
478
763
  /**
479
764
  * Efficient schema-less binary decoding with support for variable length encoding.
480
765
  *
@@ -486,7 +771,7 @@ const writeVarUint8Array = (encoder, uint8Array) => {
486
771
  *
487
772
  * ```js
488
773
  * // encoding step
489
- * const encoder = new encoding.createEncoder()
774
+ * const encoder = encoding.createEncoder()
490
775
  * encoding.writeVarUint(encoder, 256)
491
776
  * encoding.writeVarString(encoder, 'Hello world!')
492
777
  * const buf = encoding.toUint8Array(encoder)
@@ -494,7 +779,7 @@ const writeVarUint8Array = (encoder, uint8Array) => {
494
779
  *
495
780
  * ```js
496
781
  * // decoding step
497
- * const decoder = new decoding.createDecoder(buf)
782
+ * const decoder = decoding.createDecoder(buf)
498
783
  * decoding.readVarUint(decoder) // => 256
499
784
  * decoding.readVarString(decoder) // => 'Hello world!'
500
785
  * decoding.hasContent(decoder) // => false - all data is read
@@ -503,6 +788,9 @@ const writeVarUint8Array = (encoder, uint8Array) => {
503
788
  * @module decoding
504
789
  */
505
790
 
791
+ const errorUnexpectedEndOfArray = create('Unexpected end of array');
792
+ const errorIntegerOutOfRange = create('Integer out of Range');
793
+
506
794
  /**
507
795
  * A Decoder handles the decoding of an Uint8Array.
508
796
  */
@@ -582,24 +870,28 @@ const readUint8 = decoder => decoder.arr[decoder.pos++];
582
870
  */
583
871
  const readVarUint = decoder => {
584
872
  let num = 0;
585
- let len = 0;
586
- while (true) {
873
+ let mult = 1;
874
+ const len = decoder.arr.length;
875
+ while (decoder.pos < len) {
587
876
  const r = decoder.arr[decoder.pos++];
588
- num = num | ((r & BITS7) << len);
589
- len += 7;
877
+ // num = num | ((r & binary.BITS7) << len)
878
+ num = num + (r & BITS7) * mult; // shift $r << (7*#iterations) and add it to num
879
+ mult *= 128; // next iteration, shift 7 "more" to the left
590
880
  if (r < BIT8) {
591
- return num >>> 0 // return unsigned number!
881
+ return num
592
882
  }
593
- /* istanbul ignore if */
594
- if (len > 35) {
595
- throw new Error('Integer out of range!')
883
+ /* c8 ignore start */
884
+ if (num > MAX_SAFE_INTEGER) {
885
+ throw errorIntegerOutOfRange
596
886
  }
887
+ /* c8 ignore stop */
597
888
  }
889
+ throw errorUnexpectedEndOfArray
598
890
  };
599
891
 
600
892
  /**
601
- * Read string of variable length
602
- * * varUint is used to store the length of the string
893
+ * We don't test this function anymore as we use native decoding/encoding by default now.
894
+ * Better not modify this anymore..
603
895
  *
604
896
  * Transforming utf8 to a string is pretty expensive. The code performs 10x better
605
897
  * when String.fromCodePoint is fed with all characters as arguments.
@@ -610,7 +902,8 @@ const readVarUint = decoder => {
610
902
  * @param {Decoder} decoder
611
903
  * @return {String} The read String.
612
904
  */
613
- const readVarString = decoder => {
905
+ /* c8 ignore start */
906
+ const _readVarStringPolyfill = decoder => {
614
907
  let remainingLen = readVarUint(decoder);
615
908
  if (remainingLen === 0) {
616
909
  return ''
@@ -634,6 +927,27 @@ const readVarString = decoder => {
634
927
  return decodeURIComponent(escape(encodedString))
635
928
  }
636
929
  };
930
+ /* c8 ignore stop */
931
+
932
+ /**
933
+ * @function
934
+ * @param {Decoder} decoder
935
+ * @return {String} The read String
936
+ */
937
+ const _readVarStringNative = decoder =>
938
+ /** @type any */ (utf8TextDecoder).decode(readVarUint8Array(decoder));
939
+
940
+ /**
941
+ * Read string of variable length
942
+ * * varUint is used to store the length of the string
943
+ *
944
+ * @function
945
+ * @param {Decoder} decoder
946
+ * @return {String} The read String
947
+ *
948
+ */
949
+ /* c8 ignore next */
950
+ const readVarString = utf8TextDecoder ? _readVarStringNative : _readVarStringPolyfill;
637
951
 
638
952
  /**
639
953
  * Utility functions to work with buffers (Uint8Array).
@@ -662,7 +976,7 @@ const createUint8ArrayViewFromArrayBuffer = (buffer, byteOffset, length) => new
662
976
  */
663
977
  const createUint8ArrayFromArrayBuffer = buffer => new Uint8Array(buffer);
664
978
 
665
- /* istanbul ignore next */
979
+ /* c8 ignore start */
666
980
  /**
667
981
  * @param {Uint8Array} bytes
668
982
  * @return {string}
@@ -675,6 +989,7 @@ const toBase64Browser = bytes => {
675
989
  // eslint-disable-next-line no-undef
676
990
  return btoa(s)
677
991
  };
992
+ /* c8 ignore stop */
678
993
 
679
994
  /**
680
995
  * @param {Uint8Array} bytes
@@ -682,7 +997,7 @@ const toBase64Browser = bytes => {
682
997
  */
683
998
  const toBase64Node = bytes => Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength).toString('base64');
684
999
 
685
- /* istanbul ignore next */
1000
+ /* c8 ignore start */
686
1001
  /**
687
1002
  * @param {string} s
688
1003
  * @return {Uint8Array}
@@ -696,6 +1011,7 @@ const fromBase64Browser = s => {
696
1011
  }
697
1012
  return bytes
698
1013
  };
1014
+ /* c8 ignore stop */
699
1015
 
700
1016
  /**
701
1017
  * @param {string} s
@@ -705,17 +1021,17 @@ const fromBase64Node = s => {
705
1021
  return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength)
706
1022
  };
707
1023
 
708
- /* istanbul ignore next */
1024
+ /* c8 ignore next */
709
1025
  const toBase64 = isBrowser ? toBase64Browser : toBase64Node;
710
1026
 
711
- /* istanbul ignore next */
1027
+ /* c8 ignore next */
712
1028
  const fromBase64 = isBrowser ? fromBase64Browser : fromBase64Node;
713
1029
 
714
1030
  /* eslint-env browser */
715
1031
 
716
1032
  /**
717
1033
  * @typedef {Object} Channel
718
- * @property {Set<Function>} Channel.subs
1034
+ * @property {Set<function(any, any):any>} Channel.subs
719
1035
  * @property {any} Channel.bc
720
1036
  */
721
1037
 
@@ -724,6 +1040,7 @@ const fromBase64 = isBrowser ? fromBase64Browser : fromBase64Node;
724
1040
  */
725
1041
  const channels = new Map();
726
1042
 
1043
+ /* c8 ignore start */
727
1044
  class LocalStoragePolyfill {
728
1045
  /**
729
1046
  * @param {string} room
@@ -734,7 +1051,11 @@ class LocalStoragePolyfill {
734
1051
  * @type {null|function({data:ArrayBuffer}):void}
735
1052
  */
736
1053
  this.onmessage = null;
737
- onChange(e => e.key === room && this.onmessage !== null && this.onmessage({ data: fromBase64(e.newValue || '') }));
1054
+ /**
1055
+ * @param {any} e
1056
+ */
1057
+ this._onChange = e => e.key === room && this.onmessage !== null && this.onmessage({ data: fromBase64(e.newValue || '') });
1058
+ onChange(this._onChange);
738
1059
  }
739
1060
 
740
1061
  /**
@@ -743,9 +1064,15 @@ class LocalStoragePolyfill {
743
1064
  postMessage (buf) {
744
1065
  varStorage.setItem(this.room, toBase64(createUint8ArrayFromArrayBuffer(buf)));
745
1066
  }
1067
+
1068
+ close () {
1069
+ offChange(this._onChange);
1070
+ }
746
1071
  }
1072
+ /* c8 ignore stop */
747
1073
 
748
1074
  // Use BroadcastChannel or Polyfill
1075
+ /* c8 ignore next */
749
1076
  const BC = typeof BroadcastChannel === 'undefined' ? LocalStoragePolyfill : BroadcastChannel;
750
1077
 
751
1078
  /**
@@ -754,12 +1081,13 @@ const BC = typeof BroadcastChannel === 'undefined' ? LocalStoragePolyfill : Broa
754
1081
  */
755
1082
  const getChannel = room =>
756
1083
  setIfUndefined(channels, room, () => {
757
- const subs = new Set();
1084
+ const subs = create$1();
758
1085
  const bc = new BC(room);
759
1086
  /**
760
1087
  * @param {{data:ArrayBuffer}} e
761
1088
  */
762
- bc.onmessage = e => subs.forEach(sub => sub(e.data));
1089
+ /* c8 ignore next */
1090
+ bc.onmessage = e => subs.forEach(sub => sub(e.data, 'broadcastchannel'));
763
1091
  return {
764
1092
  bc, subs
765
1093
  }
@@ -770,18 +1098,29 @@ const getChannel = room =>
770
1098
  *
771
1099
  * @function
772
1100
  * @param {string} room
773
- * @param {function(any):any} f
1101
+ * @param {function(any, any):any} f
774
1102
  */
775
- const subscribe = (room, f) => getChannel(room).subs.add(f);
1103
+ const subscribe = (room, f) => {
1104
+ getChannel(room).subs.add(f);
1105
+ return f
1106
+ };
776
1107
 
777
1108
  /**
778
1109
  * Unsubscribe from `publish` global events.
779
1110
  *
780
1111
  * @function
781
1112
  * @param {string} room
782
- * @param {function(any):any} f
783
- */
784
- const unsubscribe = (room, f) => getChannel(room).subs.delete(f);
1113
+ * @param {function(any, any):any} f
1114
+ */
1115
+ const unsubscribe = (room, f) => {
1116
+ const channel = getChannel(room);
1117
+ const unsubscribed = channel.subs.delete(f);
1118
+ if (unsubscribed && channel.subs.size === 0) {
1119
+ channel.bc.close();
1120
+ channels.delete(room);
1121
+ }
1122
+ return unsubscribed
1123
+ };
785
1124
 
786
1125
  /**
787
1126
  * Publish data to all subscribers (including subscribers on this tab)
@@ -789,11 +1128,12 @@ const unsubscribe = (room, f) => getChannel(room).subs.delete(f);
789
1128
  * @function
790
1129
  * @param {string} room
791
1130
  * @param {any} data
1131
+ * @param {any} [origin]
792
1132
  */
793
- const publish = (room, data) => {
1133
+ const publish = (room, data, origin = null) => {
794
1134
  const c = getChannel(room);
795
1135
  c.bc.postMessage(data);
796
- c.subs.forEach(sub => sub(data));
1136
+ c.subs.forEach(sub => sub(data, origin));
797
1137
  };
798
1138
 
799
1139
  /**
@@ -809,30 +1149,6 @@ const publish = (room, data) => {
809
1149
  */
810
1150
  const getUnixTime = Date.now;
811
1151
 
812
- /**
813
- * Utility module to work with sets.
814
- *
815
- * @module set
816
- */
817
-
818
- const create = () => new Set();
819
-
820
- /**
821
- * Utility module to work with Arrays.
822
- *
823
- * @module array
824
- */
825
-
826
- /**
827
- * Transforms something array-like to an actual Array.
828
- *
829
- * @function
830
- * @template T
831
- * @param {ArrayLike<T>|Iterable<T>} arraylike
832
- * @return {T}
833
- */
834
- const from = Array.from;
835
-
836
1152
  /**
837
1153
  * Observable class prototype.
838
1154
  *
@@ -850,7 +1166,7 @@ class Observable {
850
1166
  * Some desc.
851
1167
  * @type {Map<N, any>}
852
1168
  */
853
- this._observers = create$1();
1169
+ this._observers = create$2();
854
1170
  }
855
1171
 
856
1172
  /**
@@ -858,7 +1174,7 @@ class Observable {
858
1174
  * @param {function} f
859
1175
  */
860
1176
  on (name, f) {
861
- setIfUndefined(this._observers, name, create).add(f);
1177
+ setIfUndefined(this._observers, name, create$1).add(f);
862
1178
  }
863
1179
 
864
1180
  /**
@@ -901,148 +1217,14 @@ class Observable {
901
1217
  */
902
1218
  emit (name, args) {
903
1219
  // copy all listeners to an array first to make sure that no event is emitted to listeners that are subscribed while the event handler is called.
904
- return from((this._observers.get(name) || create$1()).values()).forEach(f => f(...args))
1220
+ return from((this._observers.get(name) || create$2()).values()).forEach(f => f(...args))
905
1221
  }
906
1222
 
907
1223
  destroy () {
908
- this._observers = create$1();
1224
+ this._observers = create$2();
909
1225
  }
910
1226
  }
911
1227
 
912
- /**
913
- * Utility functions for working with EcmaScript objects.
914
- *
915
- * @module object
916
- */
917
-
918
- /**
919
- * @param {Object<string,any>} obj
920
- */
921
- const keys = Object.keys;
922
-
923
- /**
924
- * @template R
925
- * @param {Object<string,any>} obj
926
- * @param {function(any,string):R} f
927
- * @return {Array<R>}
928
- */
929
- const map = (obj, f) => {
930
- const results = [];
931
- for (const key in obj) {
932
- results.push(f(obj[key], key));
933
- }
934
- return results
935
- };
936
-
937
- /**
938
- * @param {Object<string,any>} obj
939
- * @return {number}
940
- */
941
- const length = obj => keys(obj).length;
942
-
943
- /**
944
- * Calls `Object.prototype.hasOwnProperty`.
945
- *
946
- * @param {any} obj
947
- * @param {string|symbol} key
948
- * @return {boolean}
949
- */
950
- const hasProperty = (obj, key) => Object.prototype.hasOwnProperty.call(obj, key);
951
-
952
- /**
953
- * Common functions and function call helpers.
954
- *
955
- * @module function
956
- */
957
-
958
- /**
959
- * @template T
960
- *
961
- * @param {T} a
962
- * @param {T} b
963
- * @return {boolean}
964
- */
965
- const equalityStrict = (a, b) => a === b;
966
-
967
- /**
968
- * @param {any} a
969
- * @param {any} b
970
- * @return {boolean}
971
- */
972
- const equalityDeep = (a, b) => {
973
- if (a == null || b == null) {
974
- return equalityStrict(a, b)
975
- }
976
- if (a.constructor !== b.constructor) {
977
- return false
978
- }
979
- if (a === b) {
980
- return true
981
- }
982
- switch (a.constructor) {
983
- case ArrayBuffer:
984
- a = new Uint8Array(a);
985
- b = new Uint8Array(b);
986
- // eslint-disable-next-line no-fallthrough
987
- case Uint8Array: {
988
- if (a.byteLength !== b.byteLength) {
989
- return false
990
- }
991
- for (let i = 0; i < a.length; i++) {
992
- if (a[i] !== b[i]) {
993
- return false
994
- }
995
- }
996
- break
997
- }
998
- case Set: {
999
- if (a.size !== b.size) {
1000
- return false
1001
- }
1002
- for (const value of a) {
1003
- if (!b.has(value)) {
1004
- return false
1005
- }
1006
- }
1007
- break
1008
- }
1009
- case Map: {
1010
- if (a.size !== b.size) {
1011
- return false
1012
- }
1013
- for (const key of a.keys()) {
1014
- if (!b.has(key) || !equalityDeep(a.get(key), b.get(key))) {
1015
- return false
1016
- }
1017
- }
1018
- break
1019
- }
1020
- case Object:
1021
- if (length(a) !== length(b)) {
1022
- return false
1023
- }
1024
- for (const key in a) {
1025
- if (!hasProperty(a, key) || !equalityDeep(a[key], b[key])) {
1026
- return false
1027
- }
1028
- }
1029
- break
1030
- case Array:
1031
- if (a.length !== b.length) {
1032
- return false
1033
- }
1034
- for (let i = 0; i < a.length; i++) {
1035
- if (!equalityDeep(a[i], b[i])) {
1036
- return false
1037
- }
1038
- }
1039
- break
1040
- default:
1041
- return false
1042
- }
1043
- return true
1044
- };
1045
-
1046
1228
  /**
1047
1229
  * @module awareness-protocol
1048
1230
  */
@@ -1408,7 +1590,7 @@ class IncomingMessage {
1408
1590
  return writeVarUint8Array(this.encoder, data);
1409
1591
  }
1410
1592
  length() {
1411
- return length$1(this.encoder);
1593
+ return length(this.encoder);
1412
1594
  }
1413
1595
  }
1414
1596
 
@@ -2204,7 +2386,11 @@ class HocuspocusProvider extends EventEmitter {
2204
2386
  }
2205
2387
  setConfiguration(configuration = {}) {
2206
2388
  if (!configuration.websocketProvider && configuration.url) {
2207
- this.configuration.websocketProvider = new HocuspocusProviderWebsocket({ url: configuration.url });
2389
+ const websocketProviderConfig = configuration;
2390
+ this.configuration.websocketProvider = new HocuspocusProviderWebsocket({
2391
+ url: websocketProviderConfig.url,
2392
+ parameters: websocketProviderConfig.parameters,
2393
+ });
2208
2394
  }
2209
2395
  this.configuration = { ...this.configuration, ...configuration };
2210
2396
  }