@bare-ts/lib 0.1.0 → 0.1.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.
package/dist/index.js CHANGED
@@ -1,988 +1,4 @@
1
- // src/codec/u8-array.ts
2
- import assert4 from "assert";
3
-
4
- // src/codec/primitive.ts
5
- import assert3 from "assert";
6
-
7
- // src/core/bare-error.ts
8
- var BareError = class extends Error {
9
- constructor(offset, issue, opts) {
10
- super(`(byte:${offset}) ${issue}`);
11
- this.name = "BareError";
12
- this.issue = issue;
13
- this.offset = offset;
14
- this.cause = opts == null ? void 0 : opts.cause;
15
- }
16
- };
17
-
18
- // src/core/config.ts
19
- import assert from "assert";
20
-
21
- // src/util/validator.ts
22
- function isI8(val) {
23
- return val === val << 24 >> 24;
24
- }
25
- function isI16(val) {
26
- return val === val << 16 >> 16;
27
- }
28
- function isI32(val) {
29
- return val === (val | 0);
30
- }
31
- function isI64(val) {
32
- return val === BigInt.asIntN(64, val);
33
- }
34
- function isSafeU64(val) {
35
- return Number.isSafeInteger(val) && val >= 0;
36
- }
37
- function isU8(val) {
38
- return val === (val & 255);
39
- }
40
- function isU16(val) {
41
- return val === (val & 65535);
42
- }
43
- function isU32(val) {
44
- return val === val >>> 0;
45
- }
46
- function isU64(val) {
47
- return val === BigInt.asUintN(64, val);
48
- }
49
-
50
- // src/core/config.ts
51
- var TOO_LARGE_NUMBER = "too large number";
52
- var initialBufferLength = 1024;
53
- var maxBufferLength = 1024 * 1024 * 32;
54
- var textDecoderThreshold = 256;
55
- var textEncoderThreshold = 256;
56
- function Config(part) {
57
- const config = Object.assign({
58
- initialBufferLength,
59
- maxBufferLength,
60
- textDecoderThreshold,
61
- textEncoderThreshold
62
- }, part);
63
- assert(isU32(config.initialBufferLength), TOO_LARGE_NUMBER);
64
- assert(isU32(config.maxBufferLength), TOO_LARGE_NUMBER);
65
- assert(isU32(config.textDecoderThreshold), TOO_LARGE_NUMBER);
66
- assert(isU32(config.textEncoderThreshold), TOO_LARGE_NUMBER);
67
- assert(config.initialBufferLength <= config.maxBufferLength, "initialBufferLength must be lower than or equal to maxBufferLength");
68
- return config;
69
- }
70
-
71
- // src/core/byte-cursor.ts
72
- import assert2 from "assert";
73
- var TOO_LARGE_BUFFER = "too large buffer";
74
- var ByteCursor = class {
75
- constructor(bytes, config) {
76
- this.config = config;
77
- this.offset = 0;
78
- this.view = bytes instanceof Uint8Array ? new DataView(bytes.buffer, bytes.byteOffset, bytes.length) : new DataView(bytes);
79
- if (this.view.byteLength > config.maxBufferLength) {
80
- throw new BareError(0, TOO_LARGE_BUFFER);
81
- }
82
- }
83
- check(min) {
84
- if (this.offset + min > this.view.byteLength) {
85
- throw new BareError(this.offset, "missing bytes");
86
- }
87
- }
88
- reserve(min) {
89
- const { config, offset, view } = this;
90
- if (offset + min > view.byteLength) {
91
- const bytes = new Uint8Array(view.buffer);
92
- const minExtraLength = min + offset - view.byteLength;
93
- assert2(bytes.length === view.byteOffset + view.byteLength, "un-growable buffer");
94
- assert2(bytes.length + minExtraLength <= config.maxBufferLength, TOO_LARGE_BUFFER);
95
- const newLen = Math.min(bytes.length + Math.max(minExtraLength, bytes.length), config.maxBufferLength);
96
- const newBytes = new Uint8Array(newLen);
97
- newBytes.set(bytes);
98
- this.view = new DataView(newBytes.buffer, view.byteOffset);
99
- }
100
- }
101
- read(len) {
102
- this.check(len);
103
- const bufferOffset = this.view.byteOffset + this.offset;
104
- this.offset += len;
105
- return new Uint8Array(this.view.buffer, bufferOffset, len);
106
- }
107
- write(bytes) {
108
- const len = bytes.length;
109
- if (len !== 0) {
110
- this.reserve(len);
111
- const writableArea = new Uint8Array(this.view.buffer, this.offset, len);
112
- writableArea.set(bytes);
113
- this.offset += len;
114
- }
115
- }
116
- };
117
-
118
- // src/codec/primitive.ts
119
- var NAN_NOT_ALLOWED = "NaN is not allowed";
120
- var NON_CANONICAL_REPRESENTATION = "must be canonical";
121
- var TOO_LARGE_NUMBER2 = "too large number";
122
- function decodeBool(bc) {
123
- const val = decodeU8(bc);
124
- if (val > 1) {
125
- bc.offset--;
126
- throw new BareError(bc.offset, "a bool must be equal to 0 or 1");
127
- }
128
- return val !== 0;
129
- }
130
- function encodeBool(bc, x) {
131
- encodeU8(bc, x ? 1 : 0);
132
- }
133
- function decodeF32(bc) {
134
- bc.check(4);
135
- const result = bc.view.getFloat32(bc.offset, true);
136
- if (Number.isNaN(result)) {
137
- throw new BareError(bc.offset, NAN_NOT_ALLOWED);
138
- }
139
- bc.offset += 4;
140
- return result;
141
- }
142
- function encodeF32(bc, x) {
143
- assert3(!Number.isNaN(x), NAN_NOT_ALLOWED);
144
- bc.reserve(4);
145
- bc.view.setFloat32(bc.offset, x, true);
146
- assert3(Math.abs(bc.view.getFloat32(bc.offset, true) - x) <= Number.EPSILON, TOO_LARGE_NUMBER2);
147
- bc.offset += 4;
148
- }
149
- function decodeF64(bc) {
150
- bc.check(8);
151
- const result = bc.view.getFloat64(bc.offset, true);
152
- if (Number.isNaN(result)) {
153
- throw new BareError(bc.offset, NAN_NOT_ALLOWED);
154
- }
155
- bc.offset += 8;
156
- return result;
157
- }
158
- function encodeF64(bc, x) {
159
- assert3(!Number.isNaN(x), NAN_NOT_ALLOWED);
160
- bc.reserve(8);
161
- bc.view.setFloat64(bc.offset, x, true);
162
- bc.offset += 8;
163
- }
164
- function decodeI8(bc) {
165
- bc.check(1);
166
- return bc.view.getInt8(bc.offset++);
167
- }
168
- function encodeI8(bc, x) {
169
- assert3(isI8(x), TOO_LARGE_NUMBER2);
170
- bc.reserve(1);
171
- bc.view.setInt8(bc.offset++, x);
172
- }
173
- function decodeI16(bc) {
174
- bc.check(2);
175
- const result = bc.view.getInt16(bc.offset, true);
176
- bc.offset += 2;
177
- return result;
178
- }
179
- function encodeI16(bc, x) {
180
- assert3(isI16(x), TOO_LARGE_NUMBER2);
181
- bc.reserve(2);
182
- bc.view.setInt16(bc.offset, x, true);
183
- bc.offset += 2;
184
- }
185
- function decodeI32(bc) {
186
- bc.check(4);
187
- const result = bc.view.getInt32(bc.offset, true);
188
- bc.offset += 4;
189
- return result;
190
- }
191
- function encodeI32(bc, x) {
192
- assert3(isI32(x), TOO_LARGE_NUMBER2);
193
- bc.reserve(4);
194
- bc.view.setInt32(bc.offset, x, true);
195
- bc.offset += 4;
196
- }
197
- function decodeI64(bc) {
198
- bc.check(8);
199
- const result = bc.view.getBigInt64(bc.offset, true);
200
- bc.offset += 8;
201
- return result;
202
- }
203
- function encodeI64(bc, x) {
204
- assert3(isI64(x), TOO_LARGE_NUMBER2);
205
- bc.reserve(8);
206
- bc.view.setBigInt64(bc.offset, x, true);
207
- bc.offset += 8;
208
- }
209
- function decodeI64Safe(bc) {
210
- const result = decodeU32(bc) + decodeI32(bc) * 4294967296;
211
- if (!Number.isSafeInteger(result)) {
212
- bc.offset -= 8;
213
- throw new BareError(bc.offset, TOO_LARGE_NUMBER2);
214
- }
215
- return result;
216
- }
217
- function encodeI64Safe(bc, x) {
218
- assert3(Number.isSafeInteger(x), TOO_LARGE_NUMBER2);
219
- const lowest32 = x >>> 0;
220
- encodeU32(bc, lowest32);
221
- let highest32 = x / 4294967296 | 0;
222
- if (x < 0) {
223
- highest32 = ~Math.abs(highest32) >>> 0;
224
- if (lowest32 === 0) {
225
- highest32++;
226
- }
227
- }
228
- encodeU32(bc, highest32);
229
- }
230
- function decodeInt(bc) {
231
- const zigZag = decodeUint(bc);
232
- return zigZag >> BigInt(1) ^ -(zigZag & BigInt(1));
233
- }
234
- function encodeInt(bc, x) {
235
- assert3(isI64(x), TOO_LARGE_NUMBER2);
236
- const zigZag = x >> BigInt(63) ^ x << BigInt(1);
237
- encodeUint(bc, zigZag);
238
- }
239
- var INT_SAFE_MAX_BYTE_COUNT = 8;
240
- function decodeIntSafe(bc) {
241
- const firstByte = decodeU8(bc);
242
- let result = (firstByte & 127) >> 1;
243
- if (firstByte >= 128) {
244
- let shiftMul = 64;
245
- let byteCount = 1;
246
- let byte;
247
- do {
248
- byte = decodeU8(bc);
249
- result += (byte & 127) * shiftMul;
250
- shiftMul *= 128;
251
- byteCount++;
252
- } while (byte >= 128 && byteCount < INT_SAFE_MAX_BYTE_COUNT);
253
- if (byte === 0) {
254
- bc.offset -= byteCount - 1;
255
- throw new BareError(bc.offset, "must be canonical");
256
- }
257
- if (byteCount === INT_SAFE_MAX_BYTE_COUNT && (byte > 31 || firstByte === 255)) {
258
- bc.offset -= byteCount - 1;
259
- throw new BareError(bc.offset, TOO_LARGE_NUMBER2);
260
- }
261
- }
262
- const isNeg = (firstByte & 1) === 1;
263
- if (isNeg) {
264
- result = -result - 1;
265
- }
266
- return result;
267
- }
268
- function encodeIntSafe(bc, x) {
269
- assert3(Number.isSafeInteger(x), TOO_LARGE_NUMBER2);
270
- const sign = x < 0 ? 1 : 0;
271
- if (x < 0) {
272
- x = -(x + 1);
273
- }
274
- const firstByte = (x & 63) << 1 | sign;
275
- x = Math.floor(x / 64);
276
- if (x > 0) {
277
- encodeU8(bc, 128 | firstByte);
278
- encodeUintSafe(bc, x);
279
- } else {
280
- encodeU8(bc, firstByte);
281
- }
282
- }
283
- function decodeU8(bc) {
284
- bc.check(1);
285
- return bc.view.getUint8(bc.offset++);
286
- }
287
- function encodeU8(bc, x) {
288
- assert3(isU8(x), TOO_LARGE_NUMBER2);
289
- bc.reserve(1);
290
- bc.view.setUint8(bc.offset++, x);
291
- }
292
- function decodeU16(bc) {
293
- bc.check(2);
294
- const result = bc.view.getUint16(bc.offset, true);
295
- bc.offset += 2;
296
- return result;
297
- }
298
- function encodeU16(bc, x) {
299
- assert3(isU16(x), TOO_LARGE_NUMBER2);
300
- bc.reserve(2);
301
- bc.view.setUint16(bc.offset, x, true);
302
- bc.offset += 2;
303
- }
304
- function decodeU32(bc) {
305
- bc.check(4);
306
- const result = bc.view.getUint32(bc.offset, true);
307
- bc.offset += 4;
308
- return result;
309
- }
310
- function encodeU32(bc, x) {
311
- assert3(isU32(x), TOO_LARGE_NUMBER2);
312
- bc.reserve(4);
313
- bc.view.setUint32(bc.offset, x, true);
314
- bc.offset += 4;
315
- }
316
- function decodeU64(bc) {
317
- bc.check(8);
318
- const result = bc.view.getBigUint64(bc.offset, true);
319
- bc.offset += 8;
320
- return result;
321
- }
322
- function encodeU64(bc, x) {
323
- assert3(isU64(x), TOO_LARGE_NUMBER2);
324
- bc.reserve(8);
325
- bc.view.setBigUint64(bc.offset, x, true);
326
- bc.offset += 8;
327
- }
328
- function decodeU64Safe(bc) {
329
- const result = decodeU32(bc) + decodeU32(bc) * 4294967296;
330
- if (!isSafeU64(result)) {
331
- bc.offset -= 8;
332
- throw new BareError(bc.offset, TOO_LARGE_NUMBER2);
333
- }
334
- return result;
335
- }
336
- function encodeU64Safe(bc, x) {
337
- assert3(isSafeU64(x), TOO_LARGE_NUMBER2);
338
- encodeU32(bc, x >>> 0);
339
- encodeU32(bc, x / 4294967296 >>> 0);
340
- }
341
- var UINT_MAX_BYTE_COUNT = 10;
342
- function decodeUint(bc) {
343
- let byteCount = 0;
344
- let byte;
345
- let low = 0;
346
- let shiftMul = 1;
347
- do {
348
- byte = decodeU8(bc);
349
- low += (byte & 127) * shiftMul;
350
- shiftMul *= 128;
351
- byteCount++;
352
- } while (byte >= 128 && byteCount < 7);
353
- let height = 0;
354
- shiftMul = 1;
355
- while (byte >= 128 && byteCount < UINT_MAX_BYTE_COUNT) {
356
- byte = decodeU8(bc);
357
- height += (byte & 127) * shiftMul;
358
- shiftMul *= 128;
359
- byteCount++;
360
- }
361
- if (byteCount !== 1 && byte === 0 || byteCount === UINT_MAX_BYTE_COUNT && byte > 1) {
362
- bc.offset -= byteCount;
363
- throw new BareError(bc.offset, NON_CANONICAL_REPRESENTATION);
364
- }
365
- return BigInt(low) + (BigInt(height) << BigInt(7 * 7));
366
- }
367
- function encodeUint(bc, x) {
368
- assert3(isU64(x), TOO_LARGE_NUMBER2);
369
- let tmp = Number(BigInt.asUintN(7 * 7, x));
370
- let rest = Number(x >> BigInt(7 * 7));
371
- let byteCount = 0;
372
- while (tmp >= 128 || rest !== 0) {
373
- encodeU8(bc, 128 | tmp & 127);
374
- tmp = Math.floor(tmp / 128);
375
- byteCount++;
376
- if (byteCount === 7) {
377
- tmp = rest;
378
- rest = 0;
379
- }
380
- }
381
- encodeU8(bc, tmp);
382
- }
383
- var UINT_SAFE_MAX_BYTE_COUNT = 8;
384
- function decodeUintSafe(bc) {
385
- let result = 0;
386
- let shiftMul = 1;
387
- let byteCount = 0;
388
- let byte;
389
- do {
390
- byte = decodeU8(bc);
391
- result += (byte & 127) * shiftMul;
392
- shiftMul *= 128;
393
- byteCount++;
394
- } while (byte >= 128 && byteCount < UINT_SAFE_MAX_BYTE_COUNT);
395
- if (byteCount !== 1 && byte === 0) {
396
- bc.offset -= byteCount - 1;
397
- throw new BareError(bc.offset - byteCount + 1, NON_CANONICAL_REPRESENTATION);
398
- }
399
- if (byteCount === UINT_SAFE_MAX_BYTE_COUNT && byte > 15) {
400
- bc.offset -= byteCount - 1;
401
- throw new BareError(bc.offset, TOO_LARGE_NUMBER2);
402
- }
403
- return result;
404
- }
405
- function encodeUintSafe(bc, x) {
406
- assert3(isSafeU64(x), TOO_LARGE_NUMBER2);
407
- while (x >= 128) {
408
- encodeU8(bc, 128 | x & 127);
409
- x = Math.floor(x / 128);
410
- }
411
- encodeU8(bc, x);
412
- }
413
- function decodeVoid(_dc) {
414
- return void 0;
415
- }
416
- function encodeVoid(_dc, _x) {
417
- }
418
-
419
- // src/codec/u8-array.ts
420
- function decodeU8Array(bc) {
421
- const len = decodeUintSafe(bc);
422
- return bc.read(len).slice();
423
- }
424
- function encodeU8Array(bc, x) {
425
- encodeUintSafe(bc, x.length);
426
- bc.write(x);
427
- }
428
- function decodeU8FixedArray(bc, len) {
429
- return bc.read(len).slice();
430
- }
431
- function encodeU8FixedArray(bc, x, len) {
432
- assert4(x.length === len);
433
- bc.write(x);
434
- }
435
-
436
- // src/codec/data.ts
437
- function decodeData(bc) {
438
- return decodeU8Array(bc).buffer;
439
- }
440
- function encodeData(bc, x) {
441
- encodeU8Array(bc, new Uint8Array(x));
442
- }
443
- function decodeFixedData(bc, len) {
444
- return decodeU8FixedArray(bc, len).buffer;
445
- }
446
- function encodeFixedData(bc, x, len) {
447
- encodeU8FixedArray(bc, new Uint8Array(x), len);
448
- }
449
-
450
- // src/codec/float-array.ts
451
- import assert5 from "assert";
452
-
453
- // src/util/util.ts
454
- var IS_LITTLE_ENDIAN_PLATFORM = new DataView(Uint16Array.of(1).buffer).getUint8(0) === 1;
455
-
456
- // src/codec/float-array.ts
457
- var NAN_NOT_ALLOWED2 = "NaN is not allowed";
458
- var decodeF32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? decodeF32FixedArrayLE : decodeF32FixedArrayBE;
459
- function decodeF32FixedArrayLE(bc, len) {
460
- const byteLen = len * 4;
461
- const result = new Float32Array(decodeFixedData(bc, byteLen));
462
- if (result.some(Number.isNaN)) {
463
- throw new BareError(bc.offset, NAN_NOT_ALLOWED2);
464
- }
465
- return result;
466
- }
467
- function decodeF32FixedArrayBE(bc, len) {
468
- bc.check(len * 4);
469
- const result = new Float32Array(len);
470
- for (let i = 0; i < len; i++)
471
- result[i] = decodeF32(bc);
472
- return result;
473
- }
474
- var encodeF32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? encodeF32FixedArrayLE : encodeF32FixedArrayBE;
475
- function encodeF32FixedArrayLE(bc, x, len) {
476
- assert5(x.length === len);
477
- assert5(!x.every(Number.isNaN), NAN_NOT_ALLOWED2);
478
- bc.write(new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
479
- }
480
- function encodeF32FixedArrayBE(bc, val, len) {
481
- assert5(val.length === len);
482
- bc.reserve(val.length * 4);
483
- for (let i = 0; i < val.length; i++)
484
- encodeF32(bc, val[i]);
485
- }
486
- function decodeF32Array(bc) {
487
- return decodeF32FixedArray(bc, decodeUintSafe(bc));
488
- }
489
- function encodeF32Array(bc, x) {
490
- encodeUintSafe(bc, x.length);
491
- if (x.length !== 0) {
492
- encodeF32FixedArray(bc, x, x.length);
493
- }
494
- }
495
- var decodeF64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? decodeF64FixedArrayLE : decodeF64FixedArrayBE;
496
- function decodeF64FixedArrayLE(bc, len) {
497
- const byteLen = len * 8;
498
- const result = new Float64Array(decodeFixedData(bc, byteLen));
499
- if (result.some(Number.isNaN)) {
500
- throw new BareError(bc.offset, NAN_NOT_ALLOWED2);
501
- }
502
- return result;
503
- }
504
- function decodeF64FixedArrayBE(bc, len) {
505
- bc.check(len * 8);
506
- const result = new Float64Array(len);
507
- for (let i = 0; i < len; i++)
508
- result[i] = decodeF64(bc);
509
- return result;
510
- }
511
- var encodeF64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? encodeF64FixedArrayLE : encodeF64FixedArrayBE;
512
- function encodeF64FixedArrayLE(bc, x, len) {
513
- assert5(x.length === len);
514
- assert5(!x.every(Number.isNaN), NAN_NOT_ALLOWED2);
515
- bc.write(new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
516
- }
517
- function encodeF64FixedArrayBE(bc, x, len) {
518
- assert5(x.length === len);
519
- bc.reserve(x.length * 8);
520
- for (let i = 0; i < x.length; i++)
521
- encodeF64(bc, x[i]);
522
- }
523
- function decodeF64Array(bc) {
524
- return decodeF64FixedArray(bc, decodeUintSafe(bc));
525
- }
526
- function encodeF64Array(bc, x) {
527
- encodeUintSafe(bc, x.length);
528
- if (x.length !== 0) {
529
- encodeF64FixedArray(bc, x, x.length);
530
- }
531
- }
532
-
533
- // src/codec/i16-array.ts
534
- import assert6 from "assert";
535
- var I16_BYTE_COUNT = 2;
536
- var decodeI16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? decodeI16FixedArrayLE : decodeI16FixedArrayBE;
537
- function decodeI16Array(bc) {
538
- return decodeI16FixedArray(bc, decodeUintSafe(bc));
539
- }
540
- function decodeI16FixedArrayLE(bc, len) {
541
- const byteCount = len * I16_BYTE_COUNT;
542
- return new Int16Array(decodeFixedData(bc, byteCount));
543
- }
544
- function decodeI16FixedArrayBE(bc, len) {
545
- bc.check(len * I16_BYTE_COUNT);
546
- const result = new Int16Array(len);
547
- for (let i = 0; i < len; i++)
548
- result[i] = decodeI16(bc);
549
- return result;
550
- }
551
- var encodeI16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? encodeI16FixedArrayLE : encodeI16FixedArrayBE;
552
- function encodeI16Array(bc, x) {
553
- encodeUintSafe(bc, x.length);
554
- if (x.length !== 0) {
555
- encodeI16FixedArray(bc, x, x.length);
556
- }
557
- }
558
- function encodeI16FixedArrayLE(bc, x, len) {
559
- assert6(x.length === len);
560
- bc.write(new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
561
- }
562
- function encodeI16FixedArrayBE(bc, x, len) {
563
- assert6(x.length === len);
564
- bc.reserve(x.length * I16_BYTE_COUNT);
565
- for (let i = 0; i < x.length; i++)
566
- encodeI16(bc, x[i]);
567
- }
568
-
569
- // src/codec/i32-array.ts
570
- import assert7 from "assert";
571
- var I32_BYTE_COUNT = 4;
572
- var decodeI32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? decodeI32FixedArrayLE : decodeI32FixedArrayBE;
573
- function decodeI32Array(bc) {
574
- return decodeI32FixedArray(bc, decodeUintSafe(bc));
575
- }
576
- function decodeI32FixedArrayLE(bc, len) {
577
- const byteCount = len * I32_BYTE_COUNT;
578
- return new Int32Array(decodeFixedData(bc, byteCount));
579
- }
580
- function decodeI32FixedArrayBE(bc, len) {
581
- bc.check(len * I32_BYTE_COUNT);
582
- const result = new Int32Array(len);
583
- for (let i = 0; i < len; i++)
584
- result[i] = decodeI32(bc);
585
- return result;
586
- }
587
- var encodeI32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? encodeI32FixedArrayLE : encodeI32FixedArrayBE;
588
- function encodeI32Array(bc, x) {
589
- encodeUintSafe(bc, x.length);
590
- if (x.length !== 0) {
591
- encodeI32FixedArray(bc, x, x.length);
592
- }
593
- }
594
- function encodeI32FixedArrayLE(bc, x, len) {
595
- assert7(x.length === len);
596
- bc.write(new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
597
- }
598
- function encodeI32FixedArrayBE(bc, x, len) {
599
- assert7(x.length === len);
600
- bc.reserve(x.length * I32_BYTE_COUNT);
601
- for (let i = 0; i < x.length; i++)
602
- encodeI32(bc, x[i]);
603
- }
604
-
605
- // src/codec/i64-array.ts
606
- import assert8 from "assert";
607
- var I64_BYTE_COUNT = 8;
608
- var decodeI64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? decodeI64FixedArrayLE : decodeI64FixedArrayBE;
609
- function decodeI64Array(bc) {
610
- return decodeI64FixedArray(bc, decodeUintSafe(bc));
611
- }
612
- function decodeI64FixedArrayLE(bc, len) {
613
- const byteCount = len * I64_BYTE_COUNT;
614
- return new BigInt64Array(decodeFixedData(bc, byteCount));
615
- }
616
- function decodeI64FixedArrayBE(bc, len) {
617
- bc.check(len * I64_BYTE_COUNT);
618
- const result = new BigInt64Array(len);
619
- for (let i = 0; i < len; i++)
620
- result[i] = decodeI64(bc);
621
- return result;
622
- }
623
- var encodeI64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? encodeI64FixedArrayLE : encodeI64FixedArrayBE;
624
- function encodeI64Array(bc, x) {
625
- encodeUintSafe(bc, x.length);
626
- if (x.length !== 0) {
627
- encodeI64FixedArray(bc, x, x.length);
628
- }
629
- }
630
- function encodeI64FixedArrayLE(bc, x, len) {
631
- assert8(x.length === len);
632
- bc.write(new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
633
- }
634
- function encodeI64FixedArrayBE(bc, x, len) {
635
- assert8(x.length === len);
636
- bc.reserve(x.length * I64_BYTE_COUNT);
637
- for (let i = 0; i < x.length; i++)
638
- encodeI64(bc, x[i]);
639
- }
640
-
641
- // src/codec/i8-array.ts
642
- function decodeI8Array(bc) {
643
- return decodeI8FixedArray(bc, decodeUintSafe(bc));
644
- }
645
- function encodeI8Array(bc, x) {
646
- encodeUintSafe(bc, x.length);
647
- encodeI8FixedArray(bc, x, x.length);
648
- }
649
- function decodeI8FixedArray(bc, len) {
650
- return new Int8Array(decodeFixedData(bc, len));
651
- }
652
- function encodeI8FixedArray(bc, x, len) {
653
- encodeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength), len);
654
- }
655
-
656
- // src/codec/string.ts
657
- function decodeString(bc) {
658
- const strBytesLen = decodeUintSafe(bc);
659
- const strBytes = bc.read(strBytesLen);
660
- try {
661
- return strBytesLen < bc.config.textDecoderThreshold ? decodeUtf8Js(strBytes) : UTF8_DECODER.decode(strBytes);
662
- } catch (cause) {
663
- throw new BareError(bc.offset - strBytesLen, "invalid UTF-8 string", {
664
- cause
665
- });
666
- }
667
- }
668
- function encodeString(bc, x) {
669
- if (x.length < bc.config.textEncoderThreshold) {
670
- const byteLen = utf8ByteLength(x);
671
- encodeUintSafe(bc, byteLen);
672
- bc.reserve(byteLen);
673
- encodeUtf8Js(bc, x);
674
- } else {
675
- const strBytes = UTF8_ENCODER.encode(x);
676
- encodeUintSafe(bc, strBytes.length);
677
- bc.write(strBytes);
678
- }
679
- }
680
- function decodeUtf8Js(bytes) {
681
- const bytesLen = bytes.length;
682
- let result = "";
683
- let i = 0;
684
- while (i < bytesLen) {
685
- let codePoint = bytes[i++] | 0;
686
- switch (Math.clz32(~codePoint << 24)) {
687
- case 0:
688
- break;
689
- case 2: {
690
- if (i < bytesLen) {
691
- codePoint = (codePoint & 31) << 6 | bytes[i] & 63;
692
- }
693
- if (codePoint >> 7 === 0 || bytes[i] >> 6 !== 2) {
694
- throw TypeError("Decoding failed");
695
- }
696
- i += 1;
697
- break;
698
- }
699
- case 3: {
700
- if (i + 1 < bytesLen) {
701
- codePoint = (codePoint & 15) << 12 | (bytes[i] & 63) << 6 | bytes[i + 1] & 63;
702
- }
703
- if (codePoint >> 11 === 0 || bytes[i] >> 6 !== 2 || bytes[i + 1] >> 6 !== 2 || codePoint >> 11 === 27) {
704
- throw TypeError("Decoding failed");
705
- }
706
- i += 2;
707
- break;
708
- }
709
- case 4: {
710
- if (i + 2 < bytesLen) {
711
- codePoint = (codePoint & 7) << 18 | (bytes[i] & 63) << 12 | (bytes[i + 1] & 63) << 6 | bytes[i + 2] & 63;
712
- }
713
- if (codePoint >> 16 === 0 || codePoint > 1114111 || bytes[i] >> 6 !== 2 || bytes[i + 1] >> 6 !== 2 || bytes[i + 2] >> 6 !== 2) {
714
- throw TypeError("Decoding failed");
715
- }
716
- i += 3;
717
- break;
718
- }
719
- default:
720
- throw TypeError("Decoding failed");
721
- }
722
- result += String.fromCodePoint(codePoint);
723
- }
724
- return result;
725
- }
726
- function encodeUtf8Js(bc, s) {
727
- const sLen = s.length;
728
- let offset = bc.offset;
729
- const view = bc.view;
730
- let i = 0;
731
- while (i < sLen) {
732
- const codePoint = s.codePointAt(i++);
733
- if (codePoint < 128) {
734
- view.setUint8(offset++, codePoint);
735
- } else if (codePoint < 2048) {
736
- view.setUint8(offset++, 192 | codePoint >> 6);
737
- view.setUint8(offset++, 128 | codePoint & 63);
738
- } else if (codePoint < 65536) {
739
- view.setUint8(offset++, 224 | codePoint >> 12);
740
- view.setUint8(offset++, 128 | codePoint >> 6 & 63);
741
- view.setUint8(offset++, 128 | codePoint & 63);
742
- } else {
743
- view.setUint8(offset++, 240 | codePoint >> 18);
744
- view.setUint8(offset++, 128 | codePoint >> 12 & 63);
745
- view.setUint8(offset++, 128 | codePoint >> 6 & 63);
746
- view.setUint8(offset++, 128 | codePoint & 63);
747
- i++;
748
- }
749
- }
750
- bc.offset = offset;
751
- }
752
- function utf8ByteLength(s) {
753
- const sLen = s.length;
754
- let result = 0;
755
- let i = 0;
756
- while (i < sLen) {
757
- const codePoint = s.codePointAt(i);
758
- i++;
759
- if (codePoint < 128) {
760
- result++;
761
- } else if (codePoint < 2048) {
762
- result += 2;
763
- } else if (codePoint < 65536) {
764
- result += 3;
765
- } else {
766
- result += 4;
767
- i++;
768
- }
769
- }
770
- return result;
771
- }
772
- var UTF8_DECODER = new TextDecoder("utf-8", { fatal: true });
773
- var UTF8_ENCODER = new TextEncoder();
774
-
775
- // src/codec/u16-array.ts
776
- import assert9 from "assert";
777
- var U16_BYTE_COUNT = 2;
778
- var decodeU16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? decodeU16FixedArrayLE : decodeU16FixedArrayBE;
779
- function decodeU16Array(bc) {
780
- return decodeU16FixedArray(bc, decodeUintSafe(bc));
781
- }
782
- function decodeU16FixedArrayLE(bc, len) {
783
- const byteCount = len * U16_BYTE_COUNT;
784
- return new Uint16Array(decodeFixedData(bc, byteCount));
785
- }
786
- function decodeU16FixedArrayBE(bc, len) {
787
- bc.check(len * U16_BYTE_COUNT);
788
- const result = new Uint16Array(len);
789
- for (let i = 0; i < len; i++)
790
- result[i] = decodeU16(bc);
791
- return result;
792
- }
793
- var encodeU16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? encodeU16FixedArrayLE : encodeU16FixedArrayBE;
794
- function encodeU16Array(bc, x) {
795
- encodeUintSafe(bc, x.length);
796
- if (x.length !== 0) {
797
- encodeU16FixedArray(bc, x, x.length);
798
- }
799
- }
800
- function encodeU16FixedArrayLE(bc, x, len) {
801
- assert9(x.length === len);
802
- bc.write(new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
803
- }
804
- function encodeU16FixedArrayBE(bc, x, len) {
805
- assert9(x.length === len);
806
- bc.reserve(x.length * U16_BYTE_COUNT);
807
- for (let i = 0; i < x.length; i++)
808
- encodeU16(bc, x[i]);
809
- }
810
-
811
- // src/codec/u32-array.ts
812
- import assert10 from "assert";
813
- var U32_BYTE_COUNT = 4;
814
- var decodeU32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? decodeU32FixedArrayLE : decodeU32FixedArrayBE;
815
- function decodeU32Array(bc) {
816
- return decodeU32FixedArray(bc, decodeUintSafe(bc));
817
- }
818
- function decodeU32FixedArrayLE(bc, len) {
819
- const byteCount = len * U32_BYTE_COUNT;
820
- return new Uint32Array(decodeFixedData(bc, byteCount));
821
- }
822
- function decodeU32FixedArrayBE(bc, len) {
823
- bc.check(len * U32_BYTE_COUNT);
824
- const result = new Uint32Array(len);
825
- for (let i = 0; i < len; i++)
826
- result[i] = decodeU32(bc);
827
- return result;
828
- }
829
- var encodeU32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? encodeU32FixedArrayLE : encodeU32FixedArrayBE;
830
- function encodeU32Array(bc, x) {
831
- encodeUintSafe(bc, x.length);
832
- if (x.length !== 0) {
833
- encodeU32FixedArray(bc, x, x.length);
834
- }
835
- }
836
- function encodeU32FixedArrayLE(bc, x, len) {
837
- assert10(x.length === len);
838
- bc.write(new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
839
- }
840
- function encodeU32FixedArrayBE(bc, x, len) {
841
- assert10(x.length === len);
842
- bc.reserve(x.length * U32_BYTE_COUNT);
843
- for (let i = 0; i < x.length; i++)
844
- encodeU32(bc, x[i]);
845
- }
846
-
847
- // src/codec/u64-array.ts
848
- import assert11 from "assert";
849
- var U64_BYTE_COUNT = 8;
850
- var decodeU64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? decodeU64FixedArrayLE : decodeU64FixedArrayBE;
851
- function decodeU64Array(bc) {
852
- return decodeU64FixedArray(bc, decodeUintSafe(bc));
853
- }
854
- function decodeU64FixedArrayLE(bc, len) {
855
- const byteCount = len * U64_BYTE_COUNT;
856
- return new BigUint64Array(decodeFixedData(bc, byteCount));
857
- }
858
- function decodeU64FixedArrayBE(bc, len) {
859
- bc.check(len * U64_BYTE_COUNT);
860
- const result = new BigUint64Array(len);
861
- for (let i = 0; i < len; i++)
862
- result[i] = decodeU64(bc);
863
- return result;
864
- }
865
- var encodeU64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? encodeU64FixedArrayLE : encodeU64FixedArrayBE;
866
- function encodeU64Array(bc, x) {
867
- encodeUintSafe(bc, x.length);
868
- if (x.length !== 0) {
869
- encodeU64FixedArray(bc, x, x.length);
870
- }
871
- }
872
- function encodeU64FixedArrayLE(bc, x, len) {
873
- assert11(x.length === len);
874
- bc.write(new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
875
- }
876
- function encodeU64FixedArrayBE(bc, x, len) {
877
- assert11(x.length === len);
878
- bc.reserve(x.length * U64_BYTE_COUNT);
879
- for (let i = 0; i < x.length; i++)
880
- encodeU64(bc, x[i]);
881
- }
882
-
883
- // src/codec/u8-clamped-array.ts
884
- function decodeU8ClampedArray(bc) {
885
- return decodeU8ClampedFixedArray(bc, decodeUintSafe(bc));
886
- }
887
- function encodeU8ClampedArray(bc, x) {
888
- encodeUintSafe(bc, x.length);
889
- encodeU8ClampedFixedArray(bc, x, x.length);
890
- }
891
- function decodeU8ClampedFixedArray(bc, len) {
892
- return new Uint8ClampedArray(decodeFixedData(bc, len));
893
- }
894
- function encodeU8ClampedFixedArray(bc, x, len) {
895
- encodeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength), len);
896
- }
897
- export {
898
- BareError,
899
- ByteCursor,
900
- Config,
901
- decodeBool,
902
- decodeData,
903
- decodeF32,
904
- decodeF32Array,
905
- decodeF32FixedArray,
906
- decodeF64,
907
- decodeF64Array,
908
- decodeF64FixedArray,
909
- decodeFixedData,
910
- decodeI16,
911
- decodeI16Array,
912
- decodeI16FixedArray,
913
- decodeI32,
914
- decodeI32Array,
915
- decodeI32FixedArray,
916
- decodeI64,
917
- decodeI64Array,
918
- decodeI64FixedArray,
919
- decodeI64Safe,
920
- decodeI8,
921
- decodeI8Array,
922
- decodeI8FixedArray,
923
- decodeInt,
924
- decodeIntSafe,
925
- decodeString,
926
- decodeU16,
927
- decodeU16Array,
928
- decodeU16FixedArray,
929
- decodeU32,
930
- decodeU32Array,
931
- decodeU32FixedArray,
932
- decodeU64,
933
- decodeU64Array,
934
- decodeU64FixedArray,
935
- decodeU64Safe,
936
- decodeU8,
937
- decodeU8Array,
938
- decodeU8ClampedArray,
939
- decodeU8ClampedFixedArray,
940
- decodeU8FixedArray,
941
- decodeUint,
942
- decodeUintSafe,
943
- decodeVoid,
944
- encodeBool,
945
- encodeData,
946
- encodeF32,
947
- encodeF32Array,
948
- encodeF32FixedArray,
949
- encodeF64,
950
- encodeF64Array,
951
- encodeF64FixedArray,
952
- encodeFixedData,
953
- encodeI16,
954
- encodeI16Array,
955
- encodeI16FixedArray,
956
- encodeI32,
957
- encodeI32Array,
958
- encodeI32FixedArray,
959
- encodeI64,
960
- encodeI64Array,
961
- encodeI64FixedArray,
962
- encodeI64Safe,
963
- encodeI8,
964
- encodeI8Array,
965
- encodeI8FixedArray,
966
- encodeInt,
967
- encodeIntSafe,
968
- encodeString,
969
- encodeU16,
970
- encodeU16Array,
971
- encodeU16FixedArray,
972
- encodeU32,
973
- encodeU32Array,
974
- encodeU32FixedArray,
975
- encodeU64,
976
- encodeU64Array,
977
- encodeU64FixedArray,
978
- encodeU64Safe,
979
- encodeU8,
980
- encodeU8Array,
981
- encodeU8ClampedArray,
982
- encodeU8ClampedFixedArray,
983
- encodeU8FixedArray,
984
- encodeUint,
985
- encodeUintSafe,
986
- encodeVoid
987
- };
1
+ // src/index.ts
2
+ export * from "./codec/index.js";
3
+ export * from "./core/index.js";
988
4
  //# sourceMappingURL=index.js.map