lib0 0.2.86 → 0.2.87
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/buffer.d.ts.map +1 -1
- package/buffer.js +3 -6
- package/coverage/tmp/coverage-29846-1697198852740-0.json +1 -0
- package/decoding.d.ts.map +1 -1
- package/decoding.js +1 -2
- package/dist/aes-gcm.cjs +8 -13
- package/dist/aes-gcm.cjs.map +1 -1
- package/dist/{broadcastchannel-036aba21.cjs → broadcastchannel-554b85cd.cjs} +4 -4
- package/dist/{broadcastchannel-036aba21.cjs.map → broadcastchannel-554b85cd.cjs.map} +1 -1
- package/dist/broadcastchannel.cjs +6 -4
- package/dist/broadcastchannel.cjs.map +1 -1
- package/dist/buffer-f9b1a143.cjs +198 -0
- package/dist/buffer-f9b1a143.cjs.map +1 -0
- package/dist/buffer.cjs +18 -16
- package/dist/buffer.cjs.map +1 -1
- package/dist/buffer.d.ts.map +1 -1
- package/dist/decoding-fca97826.cjs +791 -0
- package/dist/decoding-fca97826.cjs.map +1 -0
- package/dist/decoding.cjs +40 -45
- package/dist/decoding.cjs.map +1 -1
- package/dist/decoding.d.ts.map +1 -1
- package/dist/{buffer-bc255c75.cjs → encoding-5bb79b9b.cjs} +9 -983
- package/dist/encoding-5bb79b9b.cjs.map +1 -0
- package/dist/encoding.cjs +1 -8
- package/dist/encoding.cjs.map +1 -1
- package/dist/encoding.d.ts.map +1 -1
- package/dist/{environment-ad129e4d.cjs → environment-819e9403.cjs} +4 -4
- package/dist/environment-819e9403.cjs.map +1 -0
- package/dist/environment.cjs +1 -1
- package/dist/environment.d.ts.map +1 -1
- package/dist/index.cjs +11 -9
- package/dist/index.cjs.map +1 -1
- package/dist/{indexeddb-2dd34f7d.cjs → indexeddb-8d495848.cjs} +1 -4
- package/dist/indexeddb-8d495848.cjs.map +1 -0
- package/dist/indexeddb.cjs +1 -1
- package/dist/indexeddb.d.ts.map +1 -1
- package/dist/jwt.cjs +9 -7
- package/dist/jwt.cjs.map +1 -1
- package/dist/logging.cjs +1 -1
- package/dist/logging.common.cjs +1 -1
- package/dist/logging.node.cjs +1 -1
- package/dist/observable.cjs +5 -5
- package/dist/observable.cjs.map +1 -1
- package/dist/observable.d.ts +10 -12
- package/dist/observable.d.ts.map +1 -1
- package/dist/{prng-d6655349.cjs → prng-33ae0b2e.cjs} +3 -3
- package/dist/{prng-d6655349.cjs.map → prng-33ae0b2e.cjs.map} +1 -1
- package/dist/prng.cjs +5 -3
- package/dist/prng.cjs.map +1 -1
- package/dist/rabin-gf2-polynomial.cjs +6 -4
- package/dist/rabin-gf2-polynomial.cjs.map +1 -1
- package/dist/rabin-uncached.cjs +6 -4
- package/dist/rabin-uncached.cjs.map +1 -1
- package/dist/rabin.cjs +7 -5
- package/dist/rabin.cjs.map +1 -1
- package/dist/storage.cjs +1 -1
- package/dist/storage.cjs.map +1 -1
- package/dist/testing.cjs +5 -3
- package/dist/testing.cjs.map +1 -1
- package/dist/{websocket-aa53d26d.cjs → websocket-ccb9bf5d.cjs} +1 -1
- package/dist/{websocket-aa53d26d.cjs.map → websocket-ccb9bf5d.cjs.map} +1 -1
- package/dist/websocket.cjs +1 -1
- package/encoding.d.ts.map +1 -1
- package/encoding.js +2 -3
- package/environment.d.ts.map +1 -1
- package/environment.js +3 -3
- package/indexeddb.d.ts.map +1 -1
- package/indexeddb.js +0 -3
- package/observable.d.ts +10 -12
- package/observable.d.ts.map +1 -1
- package/observable.js +5 -5
- package/package.json +21 -2
- package/storage.js +1 -1
- package/coverage/tmp/coverage-12951-1695475540140-0.json +0 -1
- package/dist/buffer-bc255c75.cjs.map +0 -1
- package/dist/environment-ad129e4d.cjs.map +0 -1
- package/dist/indexeddb-2dd34f7d.cjs.map +0 -1
|
@@ -0,0 +1,791 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var binary = require('./binary-ac8e39e2.cjs');
|
|
4
|
+
var math = require('./math-08e068f9.cjs');
|
|
5
|
+
var number = require('./number-466d8922.cjs');
|
|
6
|
+
var string = require('./string-6d104757.cjs');
|
|
7
|
+
var error = require('./error-8582d695.cjs');
|
|
8
|
+
var encoding = require('./encoding-5bb79b9b.cjs');
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Efficient schema-less binary decoding with support for variable length encoding.
|
|
12
|
+
*
|
|
13
|
+
* Use [lib0/decoding] with [lib0/encoding]. Every encoding function has a corresponding decoding function.
|
|
14
|
+
*
|
|
15
|
+
* Encodes numbers in little-endian order (least to most significant byte order)
|
|
16
|
+
* and is compatible with Golang's binary encoding (https://golang.org/pkg/encoding/binary/)
|
|
17
|
+
* which is also used in Protocol Buffers.
|
|
18
|
+
*
|
|
19
|
+
* ```js
|
|
20
|
+
* // encoding step
|
|
21
|
+
* const encoder = encoding.createEncoder()
|
|
22
|
+
* encoding.writeVarUint(encoder, 256)
|
|
23
|
+
* encoding.writeVarString(encoder, 'Hello world!')
|
|
24
|
+
* const buf = encoding.toUint8Array(encoder)
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* ```js
|
|
28
|
+
* // decoding step
|
|
29
|
+
* const decoder = decoding.createDecoder(buf)
|
|
30
|
+
* decoding.readVarUint(decoder) // => 256
|
|
31
|
+
* decoding.readVarString(decoder) // => 'Hello world!'
|
|
32
|
+
* decoding.hasContent(decoder) // => false - all data is read
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* @module decoding
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
const errorUnexpectedEndOfArray = error.create('Unexpected end of array');
|
|
39
|
+
const errorIntegerOutOfRange = error.create('Integer out of Range');
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* A Decoder handles the decoding of an Uint8Array.
|
|
43
|
+
*/
|
|
44
|
+
class Decoder {
|
|
45
|
+
/**
|
|
46
|
+
* @param {Uint8Array} uint8Array Binary data to decode
|
|
47
|
+
*/
|
|
48
|
+
constructor (uint8Array) {
|
|
49
|
+
/**
|
|
50
|
+
* Decoding target.
|
|
51
|
+
*
|
|
52
|
+
* @type {Uint8Array}
|
|
53
|
+
*/
|
|
54
|
+
this.arr = uint8Array;
|
|
55
|
+
/**
|
|
56
|
+
* Current decoding position.
|
|
57
|
+
*
|
|
58
|
+
* @type {number}
|
|
59
|
+
*/
|
|
60
|
+
this.pos = 0;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* @function
|
|
66
|
+
* @param {Uint8Array} uint8Array
|
|
67
|
+
* @return {Decoder}
|
|
68
|
+
*/
|
|
69
|
+
const createDecoder = uint8Array => new Decoder(uint8Array);
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* @function
|
|
73
|
+
* @param {Decoder} decoder
|
|
74
|
+
* @return {boolean}
|
|
75
|
+
*/
|
|
76
|
+
const hasContent = decoder => decoder.pos !== decoder.arr.length;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Clone a decoder instance.
|
|
80
|
+
* Optionally set a new position parameter.
|
|
81
|
+
*
|
|
82
|
+
* @function
|
|
83
|
+
* @param {Decoder} decoder The decoder instance
|
|
84
|
+
* @param {number} [newPos] Defaults to current position
|
|
85
|
+
* @return {Decoder} A clone of `decoder`
|
|
86
|
+
*/
|
|
87
|
+
const clone = (decoder, newPos = decoder.pos) => {
|
|
88
|
+
const _decoder = createDecoder(decoder.arr);
|
|
89
|
+
_decoder.pos = newPos;
|
|
90
|
+
return _decoder
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Create an Uint8Array view of the next `len` bytes and advance the position by `len`.
|
|
95
|
+
*
|
|
96
|
+
* Important: The Uint8Array still points to the underlying ArrayBuffer. Make sure to discard the result as soon as possible to prevent any memory leaks.
|
|
97
|
+
* Use `buffer.copyUint8Array` to copy the result into a new Uint8Array.
|
|
98
|
+
*
|
|
99
|
+
* @function
|
|
100
|
+
* @param {Decoder} decoder The decoder instance
|
|
101
|
+
* @param {number} len The length of bytes to read
|
|
102
|
+
* @return {Uint8Array}
|
|
103
|
+
*/
|
|
104
|
+
const readUint8Array = (decoder, len) => {
|
|
105
|
+
const view = new Uint8Array(decoder.arr.buffer, decoder.pos + decoder.arr.byteOffset, len);
|
|
106
|
+
decoder.pos += len;
|
|
107
|
+
return view
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Read variable length Uint8Array.
|
|
112
|
+
*
|
|
113
|
+
* Important: The Uint8Array still points to the underlying ArrayBuffer. Make sure to discard the result as soon as possible to prevent any memory leaks.
|
|
114
|
+
* Use `buffer.copyUint8Array` to copy the result into a new Uint8Array.
|
|
115
|
+
*
|
|
116
|
+
* @function
|
|
117
|
+
* @param {Decoder} decoder
|
|
118
|
+
* @return {Uint8Array}
|
|
119
|
+
*/
|
|
120
|
+
const readVarUint8Array = decoder => readUint8Array(decoder, readVarUint(decoder));
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Read the rest of the content as an ArrayBuffer
|
|
124
|
+
* @function
|
|
125
|
+
* @param {Decoder} decoder
|
|
126
|
+
* @return {Uint8Array}
|
|
127
|
+
*/
|
|
128
|
+
const readTailAsUint8Array = decoder => readUint8Array(decoder, decoder.arr.length - decoder.pos);
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Skip one byte, jump to the next position.
|
|
132
|
+
* @function
|
|
133
|
+
* @param {Decoder} decoder The decoder instance
|
|
134
|
+
* @return {number} The next position
|
|
135
|
+
*/
|
|
136
|
+
const skip8 = decoder => decoder.pos++;
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Read one byte as unsigned integer.
|
|
140
|
+
* @function
|
|
141
|
+
* @param {Decoder} decoder The decoder instance
|
|
142
|
+
* @return {number} Unsigned 8-bit integer
|
|
143
|
+
*/
|
|
144
|
+
const readUint8 = decoder => decoder.arr[decoder.pos++];
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Read 2 bytes as unsigned integer.
|
|
148
|
+
*
|
|
149
|
+
* @function
|
|
150
|
+
* @param {Decoder} decoder
|
|
151
|
+
* @return {number} An unsigned integer.
|
|
152
|
+
*/
|
|
153
|
+
const readUint16 = decoder => {
|
|
154
|
+
const uint =
|
|
155
|
+
decoder.arr[decoder.pos] +
|
|
156
|
+
(decoder.arr[decoder.pos + 1] << 8);
|
|
157
|
+
decoder.pos += 2;
|
|
158
|
+
return uint
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Read 4 bytes as unsigned integer.
|
|
163
|
+
*
|
|
164
|
+
* @function
|
|
165
|
+
* @param {Decoder} decoder
|
|
166
|
+
* @return {number} An unsigned integer.
|
|
167
|
+
*/
|
|
168
|
+
const readUint32 = decoder => {
|
|
169
|
+
const uint =
|
|
170
|
+
(decoder.arr[decoder.pos] +
|
|
171
|
+
(decoder.arr[decoder.pos + 1] << 8) +
|
|
172
|
+
(decoder.arr[decoder.pos + 2] << 16) +
|
|
173
|
+
(decoder.arr[decoder.pos + 3] << 24)) >>> 0;
|
|
174
|
+
decoder.pos += 4;
|
|
175
|
+
return uint
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Read 4 bytes as unsigned integer in big endian order.
|
|
180
|
+
* (most significant byte first)
|
|
181
|
+
*
|
|
182
|
+
* @function
|
|
183
|
+
* @param {Decoder} decoder
|
|
184
|
+
* @return {number} An unsigned integer.
|
|
185
|
+
*/
|
|
186
|
+
const readUint32BigEndian = decoder => {
|
|
187
|
+
const uint =
|
|
188
|
+
(decoder.arr[decoder.pos + 3] +
|
|
189
|
+
(decoder.arr[decoder.pos + 2] << 8) +
|
|
190
|
+
(decoder.arr[decoder.pos + 1] << 16) +
|
|
191
|
+
(decoder.arr[decoder.pos] << 24)) >>> 0;
|
|
192
|
+
decoder.pos += 4;
|
|
193
|
+
return uint
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Look ahead without incrementing the position
|
|
198
|
+
* to the next byte and read it as unsigned integer.
|
|
199
|
+
*
|
|
200
|
+
* @function
|
|
201
|
+
* @param {Decoder} decoder
|
|
202
|
+
* @return {number} An unsigned integer.
|
|
203
|
+
*/
|
|
204
|
+
const peekUint8 = decoder => decoder.arr[decoder.pos];
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Look ahead without incrementing the position
|
|
208
|
+
* to the next byte and read it as unsigned integer.
|
|
209
|
+
*
|
|
210
|
+
* @function
|
|
211
|
+
* @param {Decoder} decoder
|
|
212
|
+
* @return {number} An unsigned integer.
|
|
213
|
+
*/
|
|
214
|
+
const peekUint16 = decoder =>
|
|
215
|
+
decoder.arr[decoder.pos] +
|
|
216
|
+
(decoder.arr[decoder.pos + 1] << 8);
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Look ahead without incrementing the position
|
|
220
|
+
* to the next byte and read it as unsigned integer.
|
|
221
|
+
*
|
|
222
|
+
* @function
|
|
223
|
+
* @param {Decoder} decoder
|
|
224
|
+
* @return {number} An unsigned integer.
|
|
225
|
+
*/
|
|
226
|
+
const peekUint32 = decoder => (
|
|
227
|
+
decoder.arr[decoder.pos] +
|
|
228
|
+
(decoder.arr[decoder.pos + 1] << 8) +
|
|
229
|
+
(decoder.arr[decoder.pos + 2] << 16) +
|
|
230
|
+
(decoder.arr[decoder.pos + 3] << 24)
|
|
231
|
+
) >>> 0;
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Read unsigned integer (32bit) with variable length.
|
|
235
|
+
* 1/8th of the storage is used as encoding overhead.
|
|
236
|
+
* * numbers < 2^7 is stored in one bytlength
|
|
237
|
+
* * numbers < 2^14 is stored in two bylength
|
|
238
|
+
*
|
|
239
|
+
* @function
|
|
240
|
+
* @param {Decoder} decoder
|
|
241
|
+
* @return {number} An unsigned integer.length
|
|
242
|
+
*/
|
|
243
|
+
const readVarUint = decoder => {
|
|
244
|
+
let num = 0;
|
|
245
|
+
let mult = 1;
|
|
246
|
+
const len = decoder.arr.length;
|
|
247
|
+
while (decoder.pos < len) {
|
|
248
|
+
const r = decoder.arr[decoder.pos++];
|
|
249
|
+
// num = num | ((r & binary.BITS7) << len)
|
|
250
|
+
num = num + (r & binary.BITS7) * mult; // shift $r << (7*#iterations) and add it to num
|
|
251
|
+
mult *= 128; // next iteration, shift 7 "more" to the left
|
|
252
|
+
if (r < binary.BIT8) {
|
|
253
|
+
return num
|
|
254
|
+
}
|
|
255
|
+
/* c8 ignore start */
|
|
256
|
+
if (num > number.MAX_SAFE_INTEGER) {
|
|
257
|
+
throw errorIntegerOutOfRange
|
|
258
|
+
}
|
|
259
|
+
/* c8 ignore stop */
|
|
260
|
+
}
|
|
261
|
+
throw errorUnexpectedEndOfArray
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Read signed integer (32bit) with variable length.
|
|
266
|
+
* 1/8th of the storage is used as encoding overhead.
|
|
267
|
+
* * numbers < 2^7 is stored in one bytlength
|
|
268
|
+
* * numbers < 2^14 is stored in two bylength
|
|
269
|
+
* @todo This should probably create the inverse ~num if number is negative - but this would be a breaking change.
|
|
270
|
+
*
|
|
271
|
+
* @function
|
|
272
|
+
* @param {Decoder} decoder
|
|
273
|
+
* @return {number} An unsigned integer.length
|
|
274
|
+
*/
|
|
275
|
+
const readVarInt = decoder => {
|
|
276
|
+
let r = decoder.arr[decoder.pos++];
|
|
277
|
+
let num = r & binary.BITS6;
|
|
278
|
+
let mult = 64;
|
|
279
|
+
const sign = (r & binary.BIT7) > 0 ? -1 : 1;
|
|
280
|
+
if ((r & binary.BIT8) === 0) {
|
|
281
|
+
// don't continue reading
|
|
282
|
+
return sign * num
|
|
283
|
+
}
|
|
284
|
+
const len = decoder.arr.length;
|
|
285
|
+
while (decoder.pos < len) {
|
|
286
|
+
r = decoder.arr[decoder.pos++];
|
|
287
|
+
// num = num | ((r & binary.BITS7) << len)
|
|
288
|
+
num = num + (r & binary.BITS7) * mult;
|
|
289
|
+
mult *= 128;
|
|
290
|
+
if (r < binary.BIT8) {
|
|
291
|
+
return sign * num
|
|
292
|
+
}
|
|
293
|
+
/* c8 ignore start */
|
|
294
|
+
if (num > number.MAX_SAFE_INTEGER) {
|
|
295
|
+
throw errorIntegerOutOfRange
|
|
296
|
+
}
|
|
297
|
+
/* c8 ignore stop */
|
|
298
|
+
}
|
|
299
|
+
throw errorUnexpectedEndOfArray
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Look ahead and read varUint without incrementing position
|
|
304
|
+
*
|
|
305
|
+
* @function
|
|
306
|
+
* @param {Decoder} decoder
|
|
307
|
+
* @return {number}
|
|
308
|
+
*/
|
|
309
|
+
const peekVarUint = decoder => {
|
|
310
|
+
const pos = decoder.pos;
|
|
311
|
+
const s = readVarUint(decoder);
|
|
312
|
+
decoder.pos = pos;
|
|
313
|
+
return s
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Look ahead and read varUint without incrementing position
|
|
318
|
+
*
|
|
319
|
+
* @function
|
|
320
|
+
* @param {Decoder} decoder
|
|
321
|
+
* @return {number}
|
|
322
|
+
*/
|
|
323
|
+
const peekVarInt = decoder => {
|
|
324
|
+
const pos = decoder.pos;
|
|
325
|
+
const s = readVarInt(decoder);
|
|
326
|
+
decoder.pos = pos;
|
|
327
|
+
return s
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* We don't test this function anymore as we use native decoding/encoding by default now.
|
|
332
|
+
* Better not modify this anymore..
|
|
333
|
+
*
|
|
334
|
+
* Transforming utf8 to a string is pretty expensive. The code performs 10x better
|
|
335
|
+
* when String.fromCodePoint is fed with all characters as arguments.
|
|
336
|
+
* But most environments have a maximum number of arguments per functions.
|
|
337
|
+
* For effiency reasons we apply a maximum of 10000 characters at once.
|
|
338
|
+
*
|
|
339
|
+
* @function
|
|
340
|
+
* @param {Decoder} decoder
|
|
341
|
+
* @return {String} The read String.
|
|
342
|
+
*/
|
|
343
|
+
/* c8 ignore start */
|
|
344
|
+
const _readVarStringPolyfill = decoder => {
|
|
345
|
+
let remainingLen = readVarUint(decoder);
|
|
346
|
+
if (remainingLen === 0) {
|
|
347
|
+
return ''
|
|
348
|
+
} else {
|
|
349
|
+
let encodedString = String.fromCodePoint(readUint8(decoder)); // remember to decrease remainingLen
|
|
350
|
+
if (--remainingLen < 100) { // do not create a Uint8Array for small strings
|
|
351
|
+
while (remainingLen--) {
|
|
352
|
+
encodedString += String.fromCodePoint(readUint8(decoder));
|
|
353
|
+
}
|
|
354
|
+
} else {
|
|
355
|
+
while (remainingLen > 0) {
|
|
356
|
+
const nextLen = remainingLen < 10000 ? remainingLen : 10000;
|
|
357
|
+
// this is dangerous, we create a fresh array view from the existing buffer
|
|
358
|
+
const bytes = decoder.arr.subarray(decoder.pos, decoder.pos + nextLen);
|
|
359
|
+
decoder.pos += nextLen;
|
|
360
|
+
// Starting with ES5.1 we can supply a generic array-like object as arguments
|
|
361
|
+
encodedString += String.fromCodePoint.apply(null, /** @type {any} */ (bytes));
|
|
362
|
+
remainingLen -= nextLen;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
return decodeURIComponent(escape(encodedString))
|
|
366
|
+
}
|
|
367
|
+
};
|
|
368
|
+
/* c8 ignore stop */
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* @function
|
|
372
|
+
* @param {Decoder} decoder
|
|
373
|
+
* @return {String} The read String
|
|
374
|
+
*/
|
|
375
|
+
const _readVarStringNative = decoder =>
|
|
376
|
+
/** @type any */ (string.utf8TextDecoder).decode(readVarUint8Array(decoder));
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* Read string of variable length
|
|
380
|
+
* * varUint is used to store the length of the string
|
|
381
|
+
*
|
|
382
|
+
* @function
|
|
383
|
+
* @param {Decoder} decoder
|
|
384
|
+
* @return {String} The read String
|
|
385
|
+
*
|
|
386
|
+
*/
|
|
387
|
+
/* c8 ignore next */
|
|
388
|
+
const readVarString = string.utf8TextDecoder ? _readVarStringNative : _readVarStringPolyfill;
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* @param {Decoder} decoder
|
|
392
|
+
* @return {Uint8Array}
|
|
393
|
+
*/
|
|
394
|
+
const readTerminatedUint8Array = decoder => {
|
|
395
|
+
const encoder = encoding.createEncoder();
|
|
396
|
+
let b;
|
|
397
|
+
while (true) {
|
|
398
|
+
b = readUint8(decoder);
|
|
399
|
+
if (b === 0) {
|
|
400
|
+
return encoding.toUint8Array(encoder)
|
|
401
|
+
}
|
|
402
|
+
if (b === 1) {
|
|
403
|
+
b = readUint8(decoder);
|
|
404
|
+
}
|
|
405
|
+
encoding.write(encoder, b);
|
|
406
|
+
}
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* @param {Decoder} decoder
|
|
411
|
+
* @return {string}
|
|
412
|
+
*/
|
|
413
|
+
const readTerminatedString = decoder => string.decodeUtf8(readTerminatedUint8Array(decoder));
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* Look ahead and read varString without incrementing position
|
|
417
|
+
*
|
|
418
|
+
* @function
|
|
419
|
+
* @param {Decoder} decoder
|
|
420
|
+
* @return {string}
|
|
421
|
+
*/
|
|
422
|
+
const peekVarString = decoder => {
|
|
423
|
+
const pos = decoder.pos;
|
|
424
|
+
const s = readVarString(decoder);
|
|
425
|
+
decoder.pos = pos;
|
|
426
|
+
return s
|
|
427
|
+
};
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* @param {Decoder} decoder
|
|
431
|
+
* @param {number} len
|
|
432
|
+
* @return {DataView}
|
|
433
|
+
*/
|
|
434
|
+
const readFromDataView = (decoder, len) => {
|
|
435
|
+
const dv = new DataView(decoder.arr.buffer, decoder.arr.byteOffset + decoder.pos, len);
|
|
436
|
+
decoder.pos += len;
|
|
437
|
+
return dv
|
|
438
|
+
};
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* @param {Decoder} decoder
|
|
442
|
+
*/
|
|
443
|
+
const readFloat32 = decoder => readFromDataView(decoder, 4).getFloat32(0, false);
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* @param {Decoder} decoder
|
|
447
|
+
*/
|
|
448
|
+
const readFloat64 = decoder => readFromDataView(decoder, 8).getFloat64(0, false);
|
|
449
|
+
|
|
450
|
+
/**
|
|
451
|
+
* @param {Decoder} decoder
|
|
452
|
+
*/
|
|
453
|
+
const readBigInt64 = decoder => /** @type {any} */ (readFromDataView(decoder, 8)).getBigInt64(0, false);
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* @param {Decoder} decoder
|
|
457
|
+
*/
|
|
458
|
+
const readBigUint64 = decoder => /** @type {any} */ (readFromDataView(decoder, 8)).getBigUint64(0, false);
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* @type {Array<function(Decoder):any>}
|
|
462
|
+
*/
|
|
463
|
+
const readAnyLookupTable = [
|
|
464
|
+
decoder => undefined, // CASE 127: undefined
|
|
465
|
+
decoder => null, // CASE 126: null
|
|
466
|
+
readVarInt, // CASE 125: integer
|
|
467
|
+
readFloat32, // CASE 124: float32
|
|
468
|
+
readFloat64, // CASE 123: float64
|
|
469
|
+
readBigInt64, // CASE 122: bigint
|
|
470
|
+
decoder => false, // CASE 121: boolean (false)
|
|
471
|
+
decoder => true, // CASE 120: boolean (true)
|
|
472
|
+
readVarString, // CASE 119: string
|
|
473
|
+
decoder => { // CASE 118: object<string,any>
|
|
474
|
+
const len = readVarUint(decoder);
|
|
475
|
+
/**
|
|
476
|
+
* @type {Object<string,any>}
|
|
477
|
+
*/
|
|
478
|
+
const obj = {};
|
|
479
|
+
for (let i = 0; i < len; i++) {
|
|
480
|
+
const key = readVarString(decoder);
|
|
481
|
+
obj[key] = readAny(decoder);
|
|
482
|
+
}
|
|
483
|
+
return obj
|
|
484
|
+
},
|
|
485
|
+
decoder => { // CASE 117: array<any>
|
|
486
|
+
const len = readVarUint(decoder);
|
|
487
|
+
const arr = [];
|
|
488
|
+
for (let i = 0; i < len; i++) {
|
|
489
|
+
arr.push(readAny(decoder));
|
|
490
|
+
}
|
|
491
|
+
return arr
|
|
492
|
+
},
|
|
493
|
+
readVarUint8Array // CASE 116: Uint8Array
|
|
494
|
+
];
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* @param {Decoder} decoder
|
|
498
|
+
*/
|
|
499
|
+
const readAny = decoder => readAnyLookupTable[127 - readUint8(decoder)](decoder);
|
|
500
|
+
|
|
501
|
+
/**
|
|
502
|
+
* T must not be null.
|
|
503
|
+
*
|
|
504
|
+
* @template T
|
|
505
|
+
*/
|
|
506
|
+
class RleDecoder extends Decoder {
|
|
507
|
+
/**
|
|
508
|
+
* @param {Uint8Array} uint8Array
|
|
509
|
+
* @param {function(Decoder):T} reader
|
|
510
|
+
*/
|
|
511
|
+
constructor (uint8Array, reader) {
|
|
512
|
+
super(uint8Array);
|
|
513
|
+
/**
|
|
514
|
+
* The reader
|
|
515
|
+
*/
|
|
516
|
+
this.reader = reader;
|
|
517
|
+
/**
|
|
518
|
+
* Current state
|
|
519
|
+
* @type {T|null}
|
|
520
|
+
*/
|
|
521
|
+
this.s = null;
|
|
522
|
+
this.count = 0;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
read () {
|
|
526
|
+
if (this.count === 0) {
|
|
527
|
+
this.s = this.reader(this);
|
|
528
|
+
if (hasContent(this)) {
|
|
529
|
+
this.count = readVarUint(this) + 1; // see encoder implementation for the reason why this is incremented
|
|
530
|
+
} else {
|
|
531
|
+
this.count = -1; // read the current value forever
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
this.count--;
|
|
535
|
+
return /** @type {T} */ (this.s)
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
class IntDiffDecoder extends Decoder {
|
|
540
|
+
/**
|
|
541
|
+
* @param {Uint8Array} uint8Array
|
|
542
|
+
* @param {number} start
|
|
543
|
+
*/
|
|
544
|
+
constructor (uint8Array, start) {
|
|
545
|
+
super(uint8Array);
|
|
546
|
+
/**
|
|
547
|
+
* Current state
|
|
548
|
+
* @type {number}
|
|
549
|
+
*/
|
|
550
|
+
this.s = start;
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
/**
|
|
554
|
+
* @return {number}
|
|
555
|
+
*/
|
|
556
|
+
read () {
|
|
557
|
+
this.s += readVarInt(this);
|
|
558
|
+
return this.s
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
class RleIntDiffDecoder extends Decoder {
|
|
563
|
+
/**
|
|
564
|
+
* @param {Uint8Array} uint8Array
|
|
565
|
+
* @param {number} start
|
|
566
|
+
*/
|
|
567
|
+
constructor (uint8Array, start) {
|
|
568
|
+
super(uint8Array);
|
|
569
|
+
/**
|
|
570
|
+
* Current state
|
|
571
|
+
* @type {number}
|
|
572
|
+
*/
|
|
573
|
+
this.s = start;
|
|
574
|
+
this.count = 0;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
/**
|
|
578
|
+
* @return {number}
|
|
579
|
+
*/
|
|
580
|
+
read () {
|
|
581
|
+
if (this.count === 0) {
|
|
582
|
+
this.s += readVarInt(this);
|
|
583
|
+
if (hasContent(this)) {
|
|
584
|
+
this.count = readVarUint(this) + 1; // see encoder implementation for the reason why this is incremented
|
|
585
|
+
} else {
|
|
586
|
+
this.count = -1; // read the current value forever
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
this.count--;
|
|
590
|
+
return /** @type {number} */ (this.s)
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
class UintOptRleDecoder extends Decoder {
|
|
595
|
+
/**
|
|
596
|
+
* @param {Uint8Array} uint8Array
|
|
597
|
+
*/
|
|
598
|
+
constructor (uint8Array) {
|
|
599
|
+
super(uint8Array);
|
|
600
|
+
/**
|
|
601
|
+
* @type {number}
|
|
602
|
+
*/
|
|
603
|
+
this.s = 0;
|
|
604
|
+
this.count = 0;
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
read () {
|
|
608
|
+
if (this.count === 0) {
|
|
609
|
+
this.s = readVarInt(this);
|
|
610
|
+
// if the sign is negative, we read the count too, otherwise count is 1
|
|
611
|
+
const isNegative = math.isNegativeZero(this.s);
|
|
612
|
+
this.count = 1;
|
|
613
|
+
if (isNegative) {
|
|
614
|
+
this.s = -this.s;
|
|
615
|
+
this.count = readVarUint(this) + 2;
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
this.count--;
|
|
619
|
+
return /** @type {number} */ (this.s)
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
class IncUintOptRleDecoder extends Decoder {
|
|
624
|
+
/**
|
|
625
|
+
* @param {Uint8Array} uint8Array
|
|
626
|
+
*/
|
|
627
|
+
constructor (uint8Array) {
|
|
628
|
+
super(uint8Array);
|
|
629
|
+
/**
|
|
630
|
+
* @type {number}
|
|
631
|
+
*/
|
|
632
|
+
this.s = 0;
|
|
633
|
+
this.count = 0;
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
read () {
|
|
637
|
+
if (this.count === 0) {
|
|
638
|
+
this.s = readVarInt(this);
|
|
639
|
+
// if the sign is negative, we read the count too, otherwise count is 1
|
|
640
|
+
const isNegative = math.isNegativeZero(this.s);
|
|
641
|
+
this.count = 1;
|
|
642
|
+
if (isNegative) {
|
|
643
|
+
this.s = -this.s;
|
|
644
|
+
this.count = readVarUint(this) + 2;
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
this.count--;
|
|
648
|
+
return /** @type {number} */ (this.s++)
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
class IntDiffOptRleDecoder extends Decoder {
|
|
653
|
+
/**
|
|
654
|
+
* @param {Uint8Array} uint8Array
|
|
655
|
+
*/
|
|
656
|
+
constructor (uint8Array) {
|
|
657
|
+
super(uint8Array);
|
|
658
|
+
/**
|
|
659
|
+
* @type {number}
|
|
660
|
+
*/
|
|
661
|
+
this.s = 0;
|
|
662
|
+
this.count = 0;
|
|
663
|
+
this.diff = 0;
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
/**
|
|
667
|
+
* @return {number}
|
|
668
|
+
*/
|
|
669
|
+
read () {
|
|
670
|
+
if (this.count === 0) {
|
|
671
|
+
const diff = readVarInt(this);
|
|
672
|
+
// if the first bit is set, we read more data
|
|
673
|
+
const hasCount = diff & 1;
|
|
674
|
+
this.diff = math.floor(diff / 2); // shift >> 1
|
|
675
|
+
this.count = 1;
|
|
676
|
+
if (hasCount) {
|
|
677
|
+
this.count = readVarUint(this) + 2;
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
this.s += this.diff;
|
|
681
|
+
this.count--;
|
|
682
|
+
return this.s
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
class StringDecoder {
|
|
687
|
+
/**
|
|
688
|
+
* @param {Uint8Array} uint8Array
|
|
689
|
+
*/
|
|
690
|
+
constructor (uint8Array) {
|
|
691
|
+
this.decoder = new UintOptRleDecoder(uint8Array);
|
|
692
|
+
this.str = readVarString(this.decoder);
|
|
693
|
+
/**
|
|
694
|
+
* @type {number}
|
|
695
|
+
*/
|
|
696
|
+
this.spos = 0;
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
/**
|
|
700
|
+
* @return {string}
|
|
701
|
+
*/
|
|
702
|
+
read () {
|
|
703
|
+
const end = this.spos + this.decoder.read();
|
|
704
|
+
const res = this.str.slice(this.spos, end);
|
|
705
|
+
this.spos = end;
|
|
706
|
+
return res
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
var decoding = /*#__PURE__*/Object.freeze({
|
|
711
|
+
__proto__: null,
|
|
712
|
+
Decoder: Decoder,
|
|
713
|
+
createDecoder: createDecoder,
|
|
714
|
+
hasContent: hasContent,
|
|
715
|
+
clone: clone,
|
|
716
|
+
readUint8Array: readUint8Array,
|
|
717
|
+
readVarUint8Array: readVarUint8Array,
|
|
718
|
+
readTailAsUint8Array: readTailAsUint8Array,
|
|
719
|
+
skip8: skip8,
|
|
720
|
+
readUint8: readUint8,
|
|
721
|
+
readUint16: readUint16,
|
|
722
|
+
readUint32: readUint32,
|
|
723
|
+
readUint32BigEndian: readUint32BigEndian,
|
|
724
|
+
peekUint8: peekUint8,
|
|
725
|
+
peekUint16: peekUint16,
|
|
726
|
+
peekUint32: peekUint32,
|
|
727
|
+
readVarUint: readVarUint,
|
|
728
|
+
readVarInt: readVarInt,
|
|
729
|
+
peekVarUint: peekVarUint,
|
|
730
|
+
peekVarInt: peekVarInt,
|
|
731
|
+
_readVarStringPolyfill: _readVarStringPolyfill,
|
|
732
|
+
_readVarStringNative: _readVarStringNative,
|
|
733
|
+
readVarString: readVarString,
|
|
734
|
+
readTerminatedUint8Array: readTerminatedUint8Array,
|
|
735
|
+
readTerminatedString: readTerminatedString,
|
|
736
|
+
peekVarString: peekVarString,
|
|
737
|
+
readFromDataView: readFromDataView,
|
|
738
|
+
readFloat32: readFloat32,
|
|
739
|
+
readFloat64: readFloat64,
|
|
740
|
+
readBigInt64: readBigInt64,
|
|
741
|
+
readBigUint64: readBigUint64,
|
|
742
|
+
readAny: readAny,
|
|
743
|
+
RleDecoder: RleDecoder,
|
|
744
|
+
IntDiffDecoder: IntDiffDecoder,
|
|
745
|
+
RleIntDiffDecoder: RleIntDiffDecoder,
|
|
746
|
+
UintOptRleDecoder: UintOptRleDecoder,
|
|
747
|
+
IncUintOptRleDecoder: IncUintOptRleDecoder,
|
|
748
|
+
IntDiffOptRleDecoder: IntDiffOptRleDecoder,
|
|
749
|
+
StringDecoder: StringDecoder
|
|
750
|
+
});
|
|
751
|
+
|
|
752
|
+
exports.Decoder = Decoder;
|
|
753
|
+
exports.IncUintOptRleDecoder = IncUintOptRleDecoder;
|
|
754
|
+
exports.IntDiffDecoder = IntDiffDecoder;
|
|
755
|
+
exports.IntDiffOptRleDecoder = IntDiffOptRleDecoder;
|
|
756
|
+
exports.RleDecoder = RleDecoder;
|
|
757
|
+
exports.RleIntDiffDecoder = RleIntDiffDecoder;
|
|
758
|
+
exports.StringDecoder = StringDecoder;
|
|
759
|
+
exports.UintOptRleDecoder = UintOptRleDecoder;
|
|
760
|
+
exports._readVarStringNative = _readVarStringNative;
|
|
761
|
+
exports._readVarStringPolyfill = _readVarStringPolyfill;
|
|
762
|
+
exports.clone = clone;
|
|
763
|
+
exports.createDecoder = createDecoder;
|
|
764
|
+
exports.decoding = decoding;
|
|
765
|
+
exports.hasContent = hasContent;
|
|
766
|
+
exports.peekUint16 = peekUint16;
|
|
767
|
+
exports.peekUint32 = peekUint32;
|
|
768
|
+
exports.peekUint8 = peekUint8;
|
|
769
|
+
exports.peekVarInt = peekVarInt;
|
|
770
|
+
exports.peekVarString = peekVarString;
|
|
771
|
+
exports.peekVarUint = peekVarUint;
|
|
772
|
+
exports.readAny = readAny;
|
|
773
|
+
exports.readBigInt64 = readBigInt64;
|
|
774
|
+
exports.readBigUint64 = readBigUint64;
|
|
775
|
+
exports.readFloat32 = readFloat32;
|
|
776
|
+
exports.readFloat64 = readFloat64;
|
|
777
|
+
exports.readFromDataView = readFromDataView;
|
|
778
|
+
exports.readTailAsUint8Array = readTailAsUint8Array;
|
|
779
|
+
exports.readTerminatedString = readTerminatedString;
|
|
780
|
+
exports.readTerminatedUint8Array = readTerminatedUint8Array;
|
|
781
|
+
exports.readUint16 = readUint16;
|
|
782
|
+
exports.readUint32 = readUint32;
|
|
783
|
+
exports.readUint32BigEndian = readUint32BigEndian;
|
|
784
|
+
exports.readUint8 = readUint8;
|
|
785
|
+
exports.readUint8Array = readUint8Array;
|
|
786
|
+
exports.readVarInt = readVarInt;
|
|
787
|
+
exports.readVarString = readVarString;
|
|
788
|
+
exports.readVarUint = readVarUint;
|
|
789
|
+
exports.readVarUint8Array = readVarUint8Array;
|
|
790
|
+
exports.skip8 = skip8;
|
|
791
|
+
//# sourceMappingURL=decoding-fca97826.cjs.map
|