@hocuspocus/common 2.8.0 → 2.9.0-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.
@@ -3,27 +3,40 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  /**
6
- * @param {string} s
7
- * @return {string}
6
+ * Common Math expressions.
7
+ *
8
+ * @module math
8
9
  */
9
- const toLowerCase = s => s.toLowerCase();
10
10
 
11
- const trimLeftRegex = /^\s*/g;
11
+ const floor = Math.floor;
12
12
 
13
13
  /**
14
- * @param {string} s
15
- * @return {string}
14
+ * @function
15
+ * @param {number} a
16
+ * @param {number} b
17
+ * @return {number} The smaller element of a and b
18
+ */
19
+ const min = (a, b) => a < b ? a : b;
20
+
21
+ /**
22
+ * @function
23
+ * @param {number} a
24
+ * @param {number} b
25
+ * @return {number} The bigger element of a and b
16
26
  */
17
- const trimLeft = s => s.replace(trimLeftRegex, '');
27
+ const max = (a, b) => a > b ? a : b;
18
28
 
19
- const fromCamelCaseRegex = /([A-Z])/g;
29
+ /* eslint-env browser */
30
+ const BIT8 = 128;
31
+ const BITS7 = 127;
20
32
 
21
33
  /**
22
- * @param {string} s
23
- * @param {string} separator
24
- * @return {string}
34
+ * Utility helpers for working with numbers.
35
+ *
36
+ * @module number
25
37
  */
26
- const fromCamelCase = (s, separator) => trimLeft(s.replace(fromCamelCaseRegex, match => `${separator}${toLowerCase(match)}`));
38
+
39
+ const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER;
27
40
 
28
41
  /**
29
42
  * @param {string} str
@@ -70,244 +83,155 @@ if (utf8TextDecoder && utf8TextDecoder.decode(new Uint8Array()).length === 1) {
70
83
  }
71
84
 
72
85
  /**
73
- * Utility module to work with key-value stores.
74
- *
75
- * @module map
76
- */
77
-
78
- /**
79
- * Creates a new Map instance.
86
+ * Efficient schema-less binary encoding with support for variable length encoding.
80
87
  *
81
- * @function
82
- * @return {Map<any, any>}
88
+ * Use [lib0/encoding] with [lib0/decoding]. Every encoding function has a corresponding decoding function.
83
89
  *
84
- * @function
85
- */
86
- const create$1 = () => new Map();
87
-
88
- /**
89
- * Often used conditions.
90
+ * Encodes numbers in little-endian order (least to most significant byte order)
91
+ * and is compatible with Golang's binary encoding (https://golang.org/pkg/encoding/binary/)
92
+ * which is also used in Protocol Buffers.
90
93
  *
91
- * @module conditions
92
- */
93
-
94
- /**
95
- * @template T
96
- * @param {T|null|undefined} v
97
- * @return {T|null}
98
- */
99
- /* c8 ignore next */
100
- const undefinedToNull = v => v === undefined ? null : v;
101
-
102
- /* eslint-env browser */
103
-
104
- /**
105
- * Isomorphic variable storage.
94
+ * ```js
95
+ * // encoding step
96
+ * const encoder = encoding.createEncoder()
97
+ * encoding.writeVarUint(encoder, 256)
98
+ * encoding.writeVarString(encoder, 'Hello world!')
99
+ * const buf = encoding.toUint8Array(encoder)
100
+ * ```
106
101
  *
107
- * Uses LocalStorage in the browser and falls back to in-memory storage.
102
+ * ```js
103
+ * // decoding step
104
+ * const decoder = decoding.createDecoder(buf)
105
+ * decoding.readVarUint(decoder) // => 256
106
+ * decoding.readVarString(decoder) // => 'Hello world!'
107
+ * decoding.hasContent(decoder) // => false - all data is read
108
+ * ```
108
109
  *
109
- * @module storage
110
+ * @module encoding
110
111
  */
111
112
 
112
- /* c8 ignore start */
113
- class VarStoragePolyfill {
114
- constructor () {
115
- this.map = new Map();
116
- }
117
-
118
- /**
119
- * @param {string} key
120
- * @param {any} newValue
121
- */
122
- setItem (key, newValue) {
123
- this.map.set(key, newValue);
124
- }
125
-
126
- /**
127
- * @param {string} key
128
- */
129
- getItem (key) {
130
- return this.map.get(key)
131
- }
132
- }
133
- /* c8 ignore stop */
134
-
135
113
  /**
136
- * @type {any}
114
+ * Write one byte to the encoder.
115
+ *
116
+ * @function
117
+ * @param {Encoder} encoder
118
+ * @param {number} num The byte that is to be encoded.
137
119
  */
138
- let _localStorage = new VarStoragePolyfill();
139
- let usePolyfill = true;
140
-
141
- /* c8 ignore start */
142
- try {
143
- // if the same-origin rule is violated, accessing localStorage might thrown an error
144
- if (typeof localStorage !== 'undefined') {
145
- _localStorage = localStorage;
146
- usePolyfill = false;
120
+ const write = (encoder, num) => {
121
+ const bufferLen = encoder.cbuf.length;
122
+ if (encoder.cpos === bufferLen) {
123
+ encoder.bufs.push(encoder.cbuf);
124
+ encoder.cbuf = new Uint8Array(bufferLen * 2);
125
+ encoder.cpos = 0;
147
126
  }
148
- } catch (e) { }
149
- /* c8 ignore stop */
150
-
151
- /**
152
- * This is basically localStorage in browser, or a polyfill in nodejs
153
- */
154
- /* c8 ignore next */
155
- const varStorage = _localStorage;
127
+ encoder.cbuf[encoder.cpos++] = num;
128
+ };
156
129
 
157
130
  /**
158
- * Common functions and function call helpers.
131
+ * Write a variable length unsigned integer. Max encodable integer is 2^53.
159
132
  *
160
- * @module function
133
+ * @function
134
+ * @param {Encoder} encoder
135
+ * @param {number} num The number that is to be encoded.
161
136
  */
137
+ const writeVarUint = (encoder, num) => {
138
+ while (num > BITS7) {
139
+ write(encoder, BIT8 | (BITS7 & num));
140
+ num = floor(num / 128); // shift >>> 7
141
+ }
142
+ write(encoder, BITS7 & num);
143
+ };
162
144
 
163
145
  /**
164
- * @template V
165
- * @template {V} OPTS
166
- *
167
- * @param {V} value
168
- * @param {Array<OPTS>} options
146
+ * A cache to store strings temporarily
169
147
  */
170
- // @ts-ignore
171
- const isOneOf = (value, options) => options.includes(value);
148
+ const _strBuffer = new Uint8Array(30000);
149
+ const _maxStrBSize = _strBuffer.length / 3;
172
150
 
173
151
  /**
174
- * Isomorphic module to work access the environment (query params, env variables).
152
+ * Write a variable length string.
175
153
  *
176
- * @module map
177
- */
178
-
179
- /* c8 ignore next */
180
- // @ts-ignore
181
- const isNode = typeof process !== 'undefined' && process.release &&
182
- /node|io\.js/.test(process.release.name);
183
- /* c8 ignore next 3 */
184
- typeof navigator !== 'undefined'
185
- ? /Mac/.test(navigator.platform)
186
- : false;
187
-
188
- /**
189
- * @type {Map<string,string>}
154
+ * @function
155
+ * @param {Encoder} encoder
156
+ * @param {String} str The string that is to be encoded.
190
157
  */
191
- let params;
192
-
193
- /* c8 ignore start */
194
- const computeParams = () => {
195
- if (params === undefined) {
196
- if (isNode) {
197
- params = create$1();
198
- const pargs = process.argv;
199
- let currParamName = null;
200
- for (let i = 0; i < pargs.length; i++) {
201
- const parg = pargs[i];
202
- if (parg[0] === '-') {
203
- if (currParamName !== null) {
204
- params.set(currParamName, '');
205
- }
206
- currParamName = parg;
207
- } else {
208
- if (currParamName !== null) {
209
- params.set(currParamName, parg);
210
- currParamName = null;
211
- }
212
- }
213
- }
214
- if (currParamName !== null) {
215
- params.set(currParamName, '');
216
- }
217
- // in ReactNative for example this would not be true (unless connected to the Remote Debugger)
218
- } else if (typeof location === 'object') {
219
- params = create$1(); // eslint-disable-next-line no-undef
220
- (location.search || '?').slice(1).split('&').forEach((kv) => {
221
- if (kv.length !== 0) {
222
- const [key, value] = kv.split('=');
223
- params.set(`--${fromCamelCase(key, '-')}`, value);
224
- params.set(`-${fromCamelCase(key, '-')}`, value);
225
- }
226
- });
227
- } else {
228
- params = create$1();
158
+ const _writeVarStringNative = (encoder, str) => {
159
+ if (str.length < _maxStrBSize) {
160
+ // We can encode the string into the existing buffer
161
+ /* c8 ignore next */
162
+ const written = utf8TextEncoder.encodeInto(str, _strBuffer).written || 0;
163
+ writeVarUint(encoder, written);
164
+ for (let i = 0; i < written; i++) {
165
+ write(encoder, _strBuffer[i]);
229
166
  }
167
+ } else {
168
+ writeVarUint8Array(encoder, encodeUtf8(str));
230
169
  }
231
- return params
232
170
  };
233
- /* c8 ignore stop */
234
171
 
235
172
  /**
236
- * @param {string} name
237
- * @return {boolean}
238
- */
239
- /* c8 ignore next */
240
- const hasParam = (name) => computeParams().has(name);
241
-
242
- /**
243
- * @param {string} name
244
- * @return {string|null}
245
- */
246
- /* c8 ignore next 4 */
247
- const getVariable = (name) =>
248
- isNode
249
- ? undefinedToNull(process.env[name.toUpperCase()])
250
- : undefinedToNull(varStorage.getItem(name));
251
-
252
- /**
253
- * @param {string} name
254
- * @return {boolean}
255
- */
256
- /* c8 ignore next 2 */
257
- const hasConf = (name) =>
258
- hasParam('--' + name) || getVariable(name) !== null;
259
-
260
- /* c8 ignore next */
261
- hasConf('production');
262
-
263
- /* c8 ignore next 2 */
264
- const forceColor = isNode &&
265
- isOneOf(process.env.FORCE_COLOR, ['true', '1', '2']);
266
-
267
- /* c8 ignore start */
268
- !hasParam('no-colors') &&
269
- (!isNode || process.stdout.isTTY || forceColor) && (
270
- !isNode || hasParam('color') || forceColor ||
271
- getVariable('COLORTERM') !== null ||
272
- (getVariable('TERM') || '').includes('color')
273
- );
274
- /* c8 ignore stop */
275
-
276
- /* eslint-env browser */
277
- const BIT8 = 128;
278
- const BITS7 = 127;
279
-
280
- /**
281
- * Common Math expressions.
173
+ * Write a variable length string.
282
174
  *
283
- * @module math
175
+ * @function
176
+ * @param {Encoder} encoder
177
+ * @param {String} str The string that is to be encoded.
284
178
  */
285
-
286
- const floor = Math.floor;
179
+ const _writeVarStringPolyfill = (encoder, str) => {
180
+ const encodedString = unescape(encodeURIComponent(str));
181
+ const len = encodedString.length;
182
+ writeVarUint(encoder, len);
183
+ for (let i = 0; i < len; i++) {
184
+ write(encoder, /** @type {number} */ (encodedString.codePointAt(i)));
185
+ }
186
+ };
287
187
 
288
188
  /**
189
+ * Write a variable length string.
190
+ *
289
191
  * @function
290
- * @param {number} a
291
- * @param {number} b
292
- * @return {number} The smaller element of a and b
192
+ * @param {Encoder} encoder
193
+ * @param {String} str The string that is to be encoded.
293
194
  */
294
- const min = (a, b) => a < b ? a : b;
195
+ /* c8 ignore next */
196
+ const writeVarString = (utf8TextEncoder && /** @type {any} */ (utf8TextEncoder).encodeInto) ? _writeVarStringNative : _writeVarStringPolyfill;
295
197
 
296
198
  /**
199
+ * Append fixed-length Uint8Array to the encoder.
200
+ *
297
201
  * @function
298
- * @param {number} a
299
- * @param {number} b
300
- * @return {number} The bigger element of a and b
202
+ * @param {Encoder} encoder
203
+ * @param {Uint8Array} uint8Array
301
204
  */
302
- const max = (a, b) => a > b ? a : b;
205
+ const writeUint8Array = (encoder, uint8Array) => {
206
+ const bufferLen = encoder.cbuf.length;
207
+ const cpos = encoder.cpos;
208
+ const leftCopyLen = min(bufferLen - cpos, uint8Array.length);
209
+ const rightCopyLen = uint8Array.length - leftCopyLen;
210
+ encoder.cbuf.set(uint8Array.subarray(0, leftCopyLen), cpos);
211
+ encoder.cpos += leftCopyLen;
212
+ if (rightCopyLen > 0) {
213
+ // Still something to write, write right half..
214
+ // Append new buffer
215
+ encoder.bufs.push(encoder.cbuf);
216
+ // must have at least size of remaining buffer
217
+ encoder.cbuf = new Uint8Array(max(bufferLen * 2, rightCopyLen));
218
+ // copy array
219
+ encoder.cbuf.set(uint8Array.subarray(leftCopyLen));
220
+ encoder.cpos = rightCopyLen;
221
+ }
222
+ };
303
223
 
304
224
  /**
305
- * Utility helpers for working with numbers.
225
+ * Append an Uint8Array to Encoder.
306
226
  *
307
- * @module number
227
+ * @function
228
+ * @param {Encoder} encoder
229
+ * @param {Uint8Array} uint8Array
308
230
  */
309
-
310
- const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER;
231
+ const writeVarUint8Array = (encoder, uint8Array) => {
232
+ writeVarUint(encoder, uint8Array.byteLength);
233
+ writeUint8Array(encoder, uint8Array);
234
+ };
311
235
 
312
236
  /**
313
237
  * Error helpers.
@@ -365,7 +289,7 @@ const errorIntegerOutOfRange = create('Integer out of Range');
365
289
  * @return {Uint8Array}
366
290
  */
367
291
  const readUint8Array = (decoder, len) => {
368
- const view = createUint8ArrayViewFromArrayBuffer(decoder.arr.buffer, decoder.pos + decoder.arr.byteOffset, len);
292
+ const view = new Uint8Array(decoder.arr.buffer, decoder.pos + decoder.arr.byteOffset, len);
369
293
  decoder.pos += len;
370
294
  return view
371
295
  };
@@ -481,172 +405,6 @@ const _readVarStringNative = decoder =>
481
405
  /* c8 ignore next */
482
406
  const readVarString = utf8TextDecoder ? _readVarStringNative : _readVarStringPolyfill;
483
407
 
484
- /**
485
- * Utility functions to work with buffers (Uint8Array).
486
- *
487
- * @module buffer
488
- */
489
-
490
- /**
491
- * Create Uint8Array with initial content from buffer
492
- *
493
- * @param {ArrayBuffer} buffer
494
- * @param {number} byteOffset
495
- * @param {number} length
496
- */
497
- const createUint8ArrayViewFromArrayBuffer = (buffer, byteOffset, length) => new Uint8Array(buffer, byteOffset, length);
498
-
499
- /**
500
- * Efficient schema-less binary encoding with support for variable length encoding.
501
- *
502
- * Use [lib0/encoding] with [lib0/decoding]. Every encoding function has a corresponding decoding function.
503
- *
504
- * Encodes numbers in little-endian order (least to most significant byte order)
505
- * and is compatible with Golang's binary encoding (https://golang.org/pkg/encoding/binary/)
506
- * which is also used in Protocol Buffers.
507
- *
508
- * ```js
509
- * // encoding step
510
- * const encoder = encoding.createEncoder()
511
- * encoding.writeVarUint(encoder, 256)
512
- * encoding.writeVarString(encoder, 'Hello world!')
513
- * const buf = encoding.toUint8Array(encoder)
514
- * ```
515
- *
516
- * ```js
517
- * // decoding step
518
- * const decoder = decoding.createDecoder(buf)
519
- * decoding.readVarUint(decoder) // => 256
520
- * decoding.readVarString(decoder) // => 'Hello world!'
521
- * decoding.hasContent(decoder) // => false - all data is read
522
- * ```
523
- *
524
- * @module encoding
525
- */
526
-
527
- /**
528
- * Write one byte to the encoder.
529
- *
530
- * @function
531
- * @param {Encoder} encoder
532
- * @param {number} num The byte that is to be encoded.
533
- */
534
- const write = (encoder, num) => {
535
- const bufferLen = encoder.cbuf.length;
536
- if (encoder.cpos === bufferLen) {
537
- encoder.bufs.push(encoder.cbuf);
538
- encoder.cbuf = new Uint8Array(bufferLen * 2);
539
- encoder.cpos = 0;
540
- }
541
- encoder.cbuf[encoder.cpos++] = num;
542
- };
543
-
544
- /**
545
- * Write a variable length unsigned integer. Max encodable integer is 2^53.
546
- *
547
- * @function
548
- * @param {Encoder} encoder
549
- * @param {number} num The number that is to be encoded.
550
- */
551
- const writeVarUint = (encoder, num) => {
552
- while (num > BITS7) {
553
- write(encoder, BIT8 | (BITS7 & num));
554
- num = floor(num / 128); // shift >>> 7
555
- }
556
- write(encoder, BITS7 & num);
557
- };
558
-
559
- /**
560
- * A cache to store strings temporarily
561
- */
562
- const _strBuffer = new Uint8Array(30000);
563
- const _maxStrBSize = _strBuffer.length / 3;
564
-
565
- /**
566
- * Write a variable length string.
567
- *
568
- * @function
569
- * @param {Encoder} encoder
570
- * @param {String} str The string that is to be encoded.
571
- */
572
- const _writeVarStringNative = (encoder, str) => {
573
- if (str.length < _maxStrBSize) {
574
- // We can encode the string into the existing buffer
575
- /* c8 ignore next */
576
- const written = utf8TextEncoder.encodeInto(str, _strBuffer).written || 0;
577
- writeVarUint(encoder, written);
578
- for (let i = 0; i < written; i++) {
579
- write(encoder, _strBuffer[i]);
580
- }
581
- } else {
582
- writeVarUint8Array(encoder, encodeUtf8(str));
583
- }
584
- };
585
-
586
- /**
587
- * Write a variable length string.
588
- *
589
- * @function
590
- * @param {Encoder} encoder
591
- * @param {String} str The string that is to be encoded.
592
- */
593
- const _writeVarStringPolyfill = (encoder, str) => {
594
- const encodedString = unescape(encodeURIComponent(str));
595
- const len = encodedString.length;
596
- writeVarUint(encoder, len);
597
- for (let i = 0; i < len; i++) {
598
- write(encoder, /** @type {number} */ (encodedString.codePointAt(i)));
599
- }
600
- };
601
-
602
- /**
603
- * Write a variable length string.
604
- *
605
- * @function
606
- * @param {Encoder} encoder
607
- * @param {String} str The string that is to be encoded.
608
- */
609
- /* c8 ignore next */
610
- const writeVarString = (utf8TextEncoder && /** @type {any} */ (utf8TextEncoder).encodeInto) ? _writeVarStringNative : _writeVarStringPolyfill;
611
-
612
- /**
613
- * Append fixed-length Uint8Array to the encoder.
614
- *
615
- * @function
616
- * @param {Encoder} encoder
617
- * @param {Uint8Array} uint8Array
618
- */
619
- const writeUint8Array = (encoder, uint8Array) => {
620
- const bufferLen = encoder.cbuf.length;
621
- const cpos = encoder.cpos;
622
- const leftCopyLen = min(bufferLen - cpos, uint8Array.length);
623
- const rightCopyLen = uint8Array.length - leftCopyLen;
624
- encoder.cbuf.set(uint8Array.subarray(0, leftCopyLen), cpos);
625
- encoder.cpos += leftCopyLen;
626
- if (rightCopyLen > 0) {
627
- // Still something to write, write right half..
628
- // Append new buffer
629
- encoder.bufs.push(encoder.cbuf);
630
- // must have at least size of remaining buffer
631
- encoder.cbuf = new Uint8Array(max(bufferLen * 2, rightCopyLen));
632
- // copy array
633
- encoder.cbuf.set(uint8Array.subarray(leftCopyLen));
634
- encoder.cpos = rightCopyLen;
635
- }
636
- };
637
-
638
- /**
639
- * Append an Uint8Array to Encoder.
640
- *
641
- * @function
642
- * @param {Encoder} encoder
643
- * @param {Uint8Array} uint8Array
644
- */
645
- const writeVarUint8Array = (encoder, uint8Array) => {
646
- writeVarUint(encoder, uint8Array.byteLength);
647
- writeUint8Array(encoder, uint8Array);
648
- };
649
-
650
408
  var AuthMessageType;
651
409
  (function (AuthMessageType) {
652
410
  AuthMessageType[AuthMessageType["Token"] = 0] = "Token";