@hocuspocus/common 2.8.0 → 2.8.1

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